xref: /haiku/src/apps/terminal/InlineInput.cpp (revision 2b76973fa2401f7a5edf68e6470f3d3210cbcff3)
1 /*
2  * Copyright 2003-2009, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stefano Ceccherini (stefano.ceccherini@gmail.com)
7  */
8 
9 #include "InlineInput.h"
10 
11 #include <cstdlib>
12 
13 struct clause
14 {
15 	int32 start;
16 	int32 end;
17 };
18 
19 
20 InlineInput::InlineInput(BMessenger messenger)
21 	:
22 	fMessenger(messenger),
23 	fActive(false),
24 	fSelectionOffset(0),
25 	fSelectionLength(0),
26 	fNumClauses(0),
27 	fClauses(NULL)
28 {
29 }
30 
31 
32 InlineInput::~InlineInput()
33 {
34 	ResetClauses();
35 }
36 
37 
38 const BMessenger *
39 InlineInput::Method() const
40 {
41 	return &fMessenger;
42 }
43 
44 
45 const char *
46 InlineInput::String() const
47 {
48 	return fString.String();
49 }
50 
51 
52 void
53 InlineInput::SetString(const char *string)
54 {
55 	fString = string;
56 }
57 
58 
59 bool
60 InlineInput::IsActive() const
61 {
62 	return fActive;
63 }
64 
65 
66 void
67 InlineInput::SetActive(bool active)
68 {
69 	fActive = active;
70 }
71 
72 
73 int32
74 InlineInput::SelectionLength() const
75 {
76 	return fSelectionLength;
77 }
78 
79 
80 void
81 InlineInput::SetSelectionLength(int32 length)
82 {
83 	fSelectionLength = length;
84 }
85 
86 
87 int32
88 InlineInput::SelectionOffset() const
89 {
90 	return fSelectionOffset;
91 }
92 
93 
94 void
95 InlineInput::SetSelectionOffset(int32 offset)
96 {
97 	fSelectionOffset = offset;
98 }
99 
100 
101 bool
102 InlineInput::AddClause(int32 start, int32 end)
103 {
104 	void *newData = realloc(fClauses, (fNumClauses + 1) * sizeof(clause));
105 	if (newData == NULL)
106 		return false;
107 
108 	fClauses = (clause *)newData;
109 	fClauses[fNumClauses].start = start;
110 	fClauses[fNumClauses].end = end;
111 	fNumClauses++;
112 	return true;
113 }
114 
115 
116 bool
117 InlineInput::GetClause(int32 index, int32 *start, int32 *end) const
118 {
119 	bool result = false;
120 	if (index >= 0 && index < fNumClauses) {
121 		result = true;
122 		clause *clause = &fClauses[index];
123 		if (start)
124 			*start = clause->start;
125 		if (end)
126 			*end = clause->end;
127 	}
128 
129 	return result;
130 }
131 
132 
133 int32
134 InlineInput::CountClauses() const
135 {
136 	return fNumClauses;
137 }
138 
139 
140 void
141 InlineInput::ResetClauses()
142 {
143 	fNumClauses = 0;
144 	free(fClauses);
145 	fClauses = NULL;
146 }
147