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
19struct 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*/
29BTextView::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*/
45BTextView::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*/
54const BMessenger *
55BTextView::InlineInput::Method() const
56{
57	return &fMessenger;
58}
59
60
61bool
62BTextView::InlineInput::IsActive() const
63{
64	return fActive;
65}
66
67
68void
69BTextView::InlineInput::SetActive(bool active)
70{
71	fActive = active;
72}
73
74
75/*! \brief Return the length of the inputted text.
76*/
77int32
78BTextView::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*/
88void
89BTextView::InlineInput::SetLength(int32 len)
90{
91	fLength = len;
92}
93
94
95/*! \brief Returns the offset into the BTextView of the text.
96*/
97int32
98BTextView::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*/
107void
108BTextView::InlineInput::SetOffset(int32 offset)
109{
110	fOffset = offset;
111}
112
113
114/*! \brief Returns the length of the selection, if any.
115*/
116int32
117BTextView::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*/
126void
127BTextView::InlineInput::SetSelectionLength(int32 length)
128{
129	fSelectionLength = length;
130}
131
132
133/*! \brief Returns the offset into the method string of the selection.
134*/
135int32
136BTextView::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*/
145void
146BTextView::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*/
156bool
157BTextView::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*/
177bool
178BTextView::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
194int32
195BTextView::InlineInput::CountClauses() const
196{
197	return fNumClauses;
198}
199
200
201/*! \brief Deletes any added clause.
202*/
203void
204BTextView::InlineInput::ResetClauses()
205{
206	fNumClauses = 0;
207	free(fClauses);
208	fClauses = NULL;
209}
210