2 * Copyright 2002-2006, project beam (http://sourceforge.net/projects/beam).
3 * All rights reserved. Distributed under the terms of the MIT License.
6 * Oliver Tappe <beam@hirschkaefer.de>
9 #include "TextViewCompleter.h"
12 #include <TextControl.h>
15 #include "AutoCompleterDefaultImpl.h"
18 // #pragma mark - TextViewWrapper
21 TextViewCompleter::TextViewWrapper::TextViewWrapper(BTextView
* textView
)
29 TextViewCompleter::TextViewWrapper::GetEditViewState(BString
& text
,
32 if (fTextView
&& fTextView
->LockLooper()) {
33 text
= fTextView
->Text();
36 fTextView
->GetSelection(caretPos
, &end
);
38 fTextView
->UnlockLooper();
44 TextViewCompleter::TextViewWrapper::SetEditViewState(const BString
& text
,
45 int32 caretPos
, int32 selectionLength
)
47 if (fTextView
&& fTextView
->LockLooper()) {
48 fTextView
->SetText(text
.String(), text
.Length());
49 fTextView
->Select(caretPos
, caretPos
+ selectionLength
);
50 fTextView
->ScrollToSelection();
51 fTextView
->UnlockLooper();
57 TextViewCompleter::TextViewWrapper::GetAdjustmentFrame()
59 BRect frame
= fTextView
->Bounds();
60 frame
= fTextView
->ConvertToScreen(frame
);
69 TextViewCompleter::TextViewCompleter(BTextView
* textView
, ChoiceModel
* model
,
70 PatternSelector
* patternSelector
)
72 BAutoCompleter(new TextViewWrapper(textView
), model
,
73 new BDefaultChoiceView(), patternSelector
),
74 BMessageFilter(B_KEY_DOWN
),
76 fModificationsReported(false)
78 fTextView
->AddFilter(this);
82 TextViewCompleter::~TextViewCompleter()
84 fTextView
->RemoveFilter(this);
89 TextViewCompleter::SetModificationsReported(bool reported
)
91 fModificationsReported
= reported
;
96 TextViewCompleter::TextModified(bool updateChoices
)
98 EditViewStateChanged(updateChoices
);
103 TextViewCompleter::Filter(BMessage
* message
, BHandler
** target
)
107 if ((!target
|| message
->FindString("bytes", &bytes
) != B_OK
108 || message
->FindInt32("modifiers", &modifiers
) != B_OK
)
109 || (modifiers
& (B_CONTROL_KEY
| B_COMMAND_KEY
| B_OPTION_KEY
110 | B_SHIFT_KEY
)) != 0) {
111 return B_DISPATCH_MESSAGE
;
117 // Insert the current choice into the text view, so the user can
118 // continue typing. This will not trigger another evaluation, since
119 // we don't invoke EditViewStateChanged().
121 return B_SKIP_MESSAGE
;
126 return B_SKIP_MESSAGE
;
129 int32 index
= SelectedChoiceIndex() - CountVisibleChoices();
130 index
= max_c(index
, 0);
133 return B_SKIP_MESSAGE
;
137 int32 index
= SelectedChoiceIndex() + CountVisibleChoices();
138 index
= min_c(index
, CountChoices() - 1);
141 return B_SKIP_MESSAGE
;
146 return B_DISPATCH_MESSAGE
;
148 if (IsChoiceSelected()) {
150 EditViewStateChanged();
153 return B_DISPATCH_MESSAGE
;
155 // make sure that the choices-view is closed when tabbing out:
157 return B_DISPATCH_MESSAGE
;
160 if (!fModificationsReported
) {
161 // dispatch message to textview manually...
162 Looper()->DispatchMessage(message
, *target
);
163 // ...and propagate the new state to the auto-completer:
164 EditViewStateChanged();
165 return B_SKIP_MESSAGE
;
167 return B_DISPATCH_MESSAGE
;