applied backgroundcolors.patch
[TortoiseGit.git] / ext / scintilla / src / AutoComplete.cxx
blobf0980c77c5f8e13be525fb8c785403b04fcab482
1 // Scintilla source code edit control
2 /** @file AutoComplete.cxx
3 ** Defines the auto completion list box.
4 **/
5 // Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <assert.h>
13 #include <string>
15 #include "Platform.h"
17 #include "CharacterSet.h"
18 #include "AutoComplete.h"
19 #include "Scintilla.h"
21 #ifdef SCI_NAMESPACE
22 using namespace Scintilla;
23 #endif
25 AutoComplete::AutoComplete() :
26 active(false),
27 separator(' '),
28 typesep('?'),
29 ignoreCase(false),
30 chooseSingle(false),
31 lb(0),
32 posStart(0),
33 startLen(0),
34 cancelAtStartPos(true),
35 autoHide(true),
36 dropRestOfWord(false),
37 ignoreCaseBehaviour(SC_CASEINSENSITIVEBEHAVIOUR_RESPECTCASE),
38 widthLBDefault(100),
39 heightLBDefault(100) {
40 lb = ListBox::Allocate();
41 stopChars[0] = '\0';
42 fillUpChars[0] = '\0';
45 AutoComplete::~AutoComplete() {
46 if (lb) {
47 lb->Destroy();
48 delete lb;
49 lb = 0;
53 bool AutoComplete::Active() const {
54 return active;
57 void AutoComplete::Start(Window &parent, int ctrlID,
58 int position, Point location, int startLen_,
59 int lineHeight, bool unicodeMode, int technology) {
60 if (active) {
61 Cancel();
63 lb->Create(parent, ctrlID, location, lineHeight, unicodeMode, technology);
64 lb->Clear();
65 active = true;
66 startLen = startLen_;
67 posStart = position;
70 void AutoComplete::SetStopChars(const char *stopChars_) {
71 strncpy(stopChars, stopChars_, sizeof(stopChars));
72 stopChars[sizeof(stopChars) - 1] = '\0';
75 bool AutoComplete::IsStopChar(char ch) {
76 return ch && strchr(stopChars, ch);
79 void AutoComplete::SetFillUpChars(const char *fillUpChars_) {
80 strncpy(fillUpChars, fillUpChars_, sizeof(fillUpChars));
81 fillUpChars[sizeof(fillUpChars) - 1] = '\0';
84 bool AutoComplete::IsFillUpChar(char ch) {
85 return ch && strchr(fillUpChars, ch);
88 void AutoComplete::SetSeparator(char separator_) {
89 separator = separator_;
92 char AutoComplete::GetSeparator() const {
93 return separator;
96 void AutoComplete::SetTypesep(char separator_) {
97 typesep = separator_;
100 char AutoComplete::GetTypesep() const {
101 return typesep;
104 void AutoComplete::SetList(const char *list) {
105 lb->SetList(list, separator, typesep);
108 int AutoComplete::GetSelection() const {
109 return lb->GetSelection();
112 std::string AutoComplete::GetValue(int item) const {
113 char value[maxItemLen];
114 lb->GetValue(item, value, sizeof(value));
115 return std::string(value);
118 void AutoComplete::Show(bool show) {
119 lb->Show(show);
120 if (show)
121 lb->Select(0);
124 void AutoComplete::Cancel() {
125 if (lb->Created()) {
126 lb->Clear();
127 lb->Destroy();
128 active = false;
133 void AutoComplete::Move(int delta) {
134 int count = lb->Length();
135 int current = lb->GetSelection();
136 current += delta;
137 if (current >= count)
138 current = count - 1;
139 if (current < 0)
140 current = 0;
141 lb->Select(current);
144 void AutoComplete::Select(const char *word) {
145 size_t lenWord = strlen(word);
146 int location = -1;
147 int start = 0; // lower bound of the api array block to search
148 int end = lb->Length() - 1; // upper bound of the api array block to search
149 while ((start <= end) && (location == -1)) { // Binary searching loop
150 int pivot = (start + end) / 2;
151 char item[maxItemLen];
152 lb->GetValue(pivot, item, maxItemLen);
153 int cond;
154 if (ignoreCase)
155 cond = CompareNCaseInsensitive(word, item, lenWord);
156 else
157 cond = strncmp(word, item, lenWord);
158 if (!cond) {
159 // Find first match
160 while (pivot > start) {
161 lb->GetValue(pivot-1, item, maxItemLen);
162 if (ignoreCase)
163 cond = CompareNCaseInsensitive(word, item, lenWord);
164 else
165 cond = strncmp(word, item, lenWord);
166 if (0 != cond)
167 break;
168 --pivot;
170 location = pivot;
171 if (ignoreCase
172 && ignoreCaseBehaviour == SC_CASEINSENSITIVEBEHAVIOUR_RESPECTCASE) {
173 // Check for exact-case match
174 for (; pivot <= end; pivot++) {
175 lb->GetValue(pivot, item, maxItemLen);
176 if (!strncmp(word, item, lenWord)) {
177 location = pivot;
178 break;
180 if (CompareNCaseInsensitive(word, item, lenWord))
181 break;
184 } else if (cond < 0) {
185 end = pivot - 1;
186 } else if (cond > 0) {
187 start = pivot + 1;
190 if (location == -1 && autoHide)
191 Cancel();
192 else
193 lb->Select(location);