xref: /haiku/src/kits/interface/textview_support/InlineInput.cpp (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
1 /*
2  * Copyright 2003-2006, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stefano Ceccherini (burton666@libero.it)
7  */
8 
9 // For a deeper understanding of this class, see the BeBook, sez.
10 // "The Input Server".
11 // TODO: the bebook says we should highlight in blue/red different "clauses".
12 // Though it looks like what really matters is the "selection" field in
13 // the BMessage sent by the input method addon. Have I missed something ?
14 
15 #include "InlineInput.h"
16 
17 #include <cstdlib>
18 
19 struct clause
20 {
21 	int32 start;
22 	int32 end;
23 };
24 
25 
26 /*! \brief Constructs a _BInlineInput_ object.
27 	\param messenger The BMessenger of the input server method addon.
28 */
29 _BInlineInput_::_BInlineInput_(BMessenger messenger)
30 	:
31 	fMessenger(messenger),
32 	fActive(false),
33 	fOffset(0),
34 	fLength(0),
35 	fSelectionOffset(0),
36 	fSelectionLength(0),
37 	fNumClauses(0),
38 	fClauses(NULL)
39 {
40 }
41 
42 
43 /*! \brief Destructs the object, free the allocated memory.
44 */
45 _BInlineInput_::~_BInlineInput_()
46 {
47 	ResetClauses();
48 }
49 
50 
51 /*! \brief Returns a pointer to the Input Server Method BMessenger
52 	which requested the transaction.
53 */
54 const BMessenger *
55 _BInlineInput_::Method() const
56 {
57 	return &fMessenger;
58 }
59 
60 
61 bool
62 _BInlineInput_::IsActive() const
63 {
64 	return fActive;
65 }
66 
67 
68 void
69 _BInlineInput_::SetActive(bool active)
70 {
71 	fActive = active;
72 }
73 
74 
75 /*! \brief Return the length of the inputted text.
76 */
77 int32
78 _BInlineInput_::Length() const
79 {
80 	return fLength;
81 }
82 
83 
84 /*! \brief Sets the length of the text inputted with the input method.
85 	\param len The length of the text, extracted from the
86 	B_INPUT_METHOD_CHANGED BMessage.
87 */
88 void
89 _BInlineInput_::SetLength(int32 len)
90 {
91 	fLength = len;
92 }
93 
94 
95 /*! \brief Returns the offset into the BTextView of the text.
96 */
97 int32
98 _BInlineInput_::Offset() const
99 {
100 	return fOffset;
101 }
102 
103 
104 /*! \brief Sets the offset into the BTextView of the text.
105 	\param offset The offset where the text has been inserted.
106 */
107 void
108 _BInlineInput_::SetOffset(int32 offset)
109 {
110 	fOffset = offset;
111 }
112 
113 
114 /*! \brief Returns the length of the selection, if any.
115 */
116 int32
117 _BInlineInput_::SelectionLength() const
118 {
119 	return fSelectionLength;
120 }
121 
122 
123 /*! \brief Sets the length of the selection.
124 	\param length The length of the selection.
125 */
126 void
127 _BInlineInput_::SetSelectionLength(int32 length)
128 {
129 	fSelectionLength = length;
130 }
131 
132 
133 /*! \brief Returns the offset into the method string of the selection.
134 */
135 int32
136 _BInlineInput_::SelectionOffset() const
137 {
138 	return fSelectionOffset;
139 }
140 
141 
142 /*! \brief Sets the offset into the method string of the selection.
143 	\param offset The offset where the selection starts.
144 */
145 void
146 _BInlineInput_::SetSelectionOffset(int32 offset)
147 {
148 	fSelectionOffset = offset;
149 }
150 
151 
152 /*! \brief Adds a clause (see "The Input Server" sez. for details).
153 	\param start The offset into the string where the clause starts.
154 	\param end The offset into the string where the clause finishes.
155 */
156 void
157 _BInlineInput_::AddClause(int32 start, int32 end)
158 {
159 	fClauses = (clause *)realloc(fClauses, ++fNumClauses * sizeof(clause));
160 	fClauses[fNumClauses - 1].start = start;
161 	fClauses[fNumClauses - 1].end = end;
162 }
163 
164 
165 /*! \brief Gets the clause at the given index.
166 	\param index The index of the clause to get.
167 	\param start A pointer to an integer which will contain the clause's start offset.
168 	\param end A pointer to an integer which will contain the clause's end offset.
169 	\return \c true if the clause exists, \c false if not.
170 */
171 bool
172 _BInlineInput_::GetClause(int32 index, int32 *start, int32 *end) const
173 {
174 	bool result = false;
175 	if (index >= 0 && index < fNumClauses) {
176 		result = true;
177 		clause *clause = &fClauses[index];
178 		if (start)
179 			*start = clause->start;
180 		if (end)
181 			*end = clause->end;
182 	}
183 
184 	return result;
185 }
186 
187 
188 int32
189 _BInlineInput_::CountClauses() const
190 {
191 	return fNumClauses;
192 }
193 
194 
195 /*! \brief Deletes any added clause.
196 */
197 void
198 _BInlineInput_::ResetClauses()
199 {
200 	fNumClauses = 0;
201 	free(fClauses);
202 	fClauses = NULL;
203 }
204