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 InlineInput object.
27 \param messenger The BMessenger of the input server method addon.
28 */
InlineInput(BMessenger messenger)29 BTextView::InlineInput::InlineInput(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 */
~InlineInput()45 BTextView::InlineInput::~InlineInput()
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 *
Method() const55 BTextView::InlineInput::Method() const
56 {
57 return &fMessenger;
58 }
59
60
61 bool
IsActive() const62 BTextView::InlineInput::IsActive() const
63 {
64 return fActive;
65 }
66
67
68 void
SetActive(bool active)69 BTextView::InlineInput::SetActive(bool active)
70 {
71 fActive = active;
72 }
73
74
75 /*! \brief Return the length of the inputted text.
76 */
77 int32
Length() const78 BTextView::InlineInput::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
SetLength(int32 len)89 BTextView::InlineInput::SetLength(int32 len)
90 {
91 fLength = len;
92 }
93
94
95 /*! \brief Returns the offset into the BTextView of the text.
96 */
97 int32
Offset() const98 BTextView::InlineInput::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
SetOffset(int32 offset)108 BTextView::InlineInput::SetOffset(int32 offset)
109 {
110 fOffset = offset;
111 }
112
113
114 /*! \brief Returns the length of the selection, if any.
115 */
116 int32
SelectionLength() const117 BTextView::InlineInput::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
SetSelectionLength(int32 length)127 BTextView::InlineInput::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
SelectionOffset() const136 BTextView::InlineInput::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
SetSelectionOffset(int32 offset)146 BTextView::InlineInput::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 bool
AddClause(int32 start,int32 end)157 BTextView::InlineInput::AddClause(int32 start, int32 end)
158 {
159 void *newData = realloc(fClauses, (fNumClauses + 1) * sizeof(clause));
160 if (newData == NULL)
161 return false;
162
163 fClauses = (clause *)newData;
164 fClauses[fNumClauses].start = start;
165 fClauses[fNumClauses].end = end;
166 fNumClauses++;
167 return true;
168 }
169
170
171 /*! \brief Gets the clause at the given index.
172 \param index The index of the clause to get.
173 \param start A pointer to an integer which will contain the clause's start offset.
174 \param end A pointer to an integer which will contain the clause's end offset.
175 \return \c true if the clause exists, \c false if not.
176 */
177 bool
GetClause(int32 index,int32 * start,int32 * end) const178 BTextView::InlineInput::GetClause(int32 index, int32 *start, int32 *end) const
179 {
180 bool result = false;
181 if (index >= 0 && index < fNumClauses) {
182 result = true;
183 clause *clause = &fClauses[index];
184 if (start)
185 *start = clause->start;
186 if (end)
187 *end = clause->end;
188 }
189
190 return result;
191 }
192
193
194 int32
CountClauses() const195 BTextView::InlineInput::CountClauses() const
196 {
197 return fNumClauses;
198 }
199
200
201 /*! \brief Deletes any added clause.
202 */
203 void
ResetClauses()204 BTextView::InlineInput::ResetClauses()
205 {
206 fNumClauses = 0;
207 free(fClauses);
208 fClauses = NULL;
209 }
210