btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / kits / interface / textview_support / InlineInput.cpp
blob51837111eb024eb2fc07f4867f74c671e919f63e
1 /*
2 * Copyright 2003-2006, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Stefano Ceccherini (burton666@libero.it)
7 */
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 ?
15 #include "InlineInput.h"
17 #include <cstdlib>
19 struct clause
21 int32 start;
22 int32 end;
26 /*! \brief Constructs a InlineInput object.
27 \param messenger The BMessenger of the input server method addon.
29 BTextView::InlineInput::InlineInput(BMessenger messenger)
31 fMessenger(messenger),
32 fActive(false),
33 fOffset(0),
34 fLength(0),
35 fSelectionOffset(0),
36 fSelectionLength(0),
37 fNumClauses(0),
38 fClauses(NULL)
43 /*! \brief Destructs the object, free the allocated memory.
45 BTextView::InlineInput::~InlineInput()
47 ResetClauses();
51 /*! \brief Returns a pointer to the Input Server Method BMessenger
52 which requested the transaction.
54 const BMessenger *
55 BTextView::InlineInput::Method() const
57 return &fMessenger;
61 bool
62 BTextView::InlineInput::IsActive() const
64 return fActive;
68 void
69 BTextView::InlineInput::SetActive(bool active)
71 fActive = active;
75 /*! \brief Return the length of the inputted text.
77 int32
78 BTextView::InlineInput::Length() const
80 return fLength;
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.
88 void
89 BTextView::InlineInput::SetLength(int32 len)
91 fLength = len;
95 /*! \brief Returns the offset into the BTextView of the text.
97 int32
98 BTextView::InlineInput::Offset() const
100 return fOffset;
104 /*! \brief Sets the offset into the BTextView of the text.
105 \param offset The offset where the text has been inserted.
107 void
108 BTextView::InlineInput::SetOffset(int32 offset)
110 fOffset = offset;
114 /*! \brief Returns the length of the selection, if any.
116 int32
117 BTextView::InlineInput::SelectionLength() const
119 return fSelectionLength;
123 /*! \brief Sets the length of the selection.
124 \param length The length of the selection.
126 void
127 BTextView::InlineInput::SetSelectionLength(int32 length)
129 fSelectionLength = length;
133 /*! \brief Returns the offset into the method string of the selection.
135 int32
136 BTextView::InlineInput::SelectionOffset() const
138 return fSelectionOffset;
142 /*! \brief Sets the offset into the method string of the selection.
143 \param offset The offset where the selection starts.
145 void
146 BTextView::InlineInput::SetSelectionOffset(int32 offset)
148 fSelectionOffset = offset;
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.
156 bool
157 BTextView::InlineInput::AddClause(int32 start, int32 end)
159 void *newData = realloc(fClauses, (fNumClauses + 1) * sizeof(clause));
160 if (newData == NULL)
161 return false;
163 fClauses = (clause *)newData;
164 fClauses[fNumClauses].start = start;
165 fClauses[fNumClauses].end = end;
166 fNumClauses++;
167 return true;
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.
177 bool
178 BTextView::InlineInput::GetClause(int32 index, int32 *start, int32 *end) const
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;
190 return result;
194 int32
195 BTextView::InlineInput::CountClauses() const
197 return fNumClauses;
201 /*! \brief Deletes any added clause.
203 void
204 BTextView::InlineInput::ResetClauses()
206 fNumClauses = 0;
207 free(fClauses);
208 fClauses = NULL;