r5116 | eht16 | 2010-08-05 22:13:47 +0100 (Thu, 05 Aug 2010) | 3 lines
[geany-mirror.git] / scintilla / AutoComplete.cxx
blobcd58f61726adcc243aa8b9ae25300c4fdc6efa8c
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>
12 #include "Platform.h"
14 #include "CharClassify.h"
15 #include "AutoComplete.h"
17 #ifdef SCI_NAMESPACE
18 using namespace Scintilla;
19 #endif
21 AutoComplete::AutoComplete() :
22 active(false),
23 separator(' '),
24 typesep('?'),
25 ignoreCase(false),
26 chooseSingle(false),
27 lb(0),
28 posStart(0),
29 startLen(0),
30 cancelAtStartPos(true),
31 autoHide(true),
32 dropRestOfWord(false) {
33 lb = ListBox::Allocate();
34 stopChars[0] = '\0';
35 fillUpChars[0] = '\0';
38 AutoComplete::~AutoComplete() {
39 if (lb) {
40 lb->Destroy();
41 delete lb;
42 lb = 0;
46 bool AutoComplete::Active() const {
47 return active;
50 void AutoComplete::Start(Window &parent, int ctrlID,
51 int position, Point location, int startLen_,
52 int lineHeight, bool unicodeMode) {
53 if (active) {
54 Cancel();
56 lb->Create(parent, ctrlID, location, lineHeight, unicodeMode);
57 lb->Clear();
58 active = true;
59 startLen = startLen_;
60 posStart = position;
63 void AutoComplete::SetStopChars(const char *stopChars_) {
64 strncpy(stopChars, stopChars_, sizeof(stopChars));
65 stopChars[sizeof(stopChars) - 1] = '\0';
68 bool AutoComplete::IsStopChar(char ch) {
69 return ch && strchr(stopChars, ch);
72 void AutoComplete::SetFillUpChars(const char *fillUpChars_) {
73 strncpy(fillUpChars, fillUpChars_, sizeof(fillUpChars));
74 fillUpChars[sizeof(fillUpChars) - 1] = '\0';
77 bool AutoComplete::IsFillUpChar(char ch) {
78 return ch && strchr(fillUpChars, ch);
81 void AutoComplete::SetSeparator(char separator_) {
82 separator = separator_;
85 char AutoComplete::GetSeparator() const {
86 return separator;
89 void AutoComplete::SetTypesep(char separator_) {
90 typesep = separator_;
93 char AutoComplete::GetTypesep() const {
94 return typesep;
97 void AutoComplete::SetList(const char *list) {
98 lb->SetList(list, separator, typesep);
101 void AutoComplete::Show(bool show) {
102 lb->Show(show);
103 if (show)
104 lb->Select(0);
107 void AutoComplete::Cancel() {
108 if (lb->Created()) {
109 lb->Clear();
110 lb->Destroy();
111 active = false;
116 void AutoComplete::Move(int delta) {
117 int count = lb->Length();
118 int current = lb->GetSelection();
119 current += delta;
120 if (current >= count)
121 current = count - 1;
122 if (current < 0)
123 current = 0;
124 lb->Select(current);
127 void AutoComplete::Select(const char *word) {
128 size_t lenWord = strlen(word);
129 int location = -1;
130 const int maxItemLen=1000;
131 char item[maxItemLen];
132 int start = 0; // lower bound of the api array block to search
133 int end = lb->Length() - 1; // upper bound of the api array block to search
134 while ((start <= end) && (location == -1)) { // Binary searching loop
135 int pivot = (start + end) / 2;
136 lb->GetValue(pivot, item, maxItemLen);
137 int cond;
138 if (ignoreCase)
139 cond = CompareNCaseInsensitive(word, item, lenWord);
140 else
141 cond = strncmp(word, item, lenWord);
142 if (!cond) {
143 // Find first match
144 while (pivot > start) {
145 lb->GetValue(pivot-1, item, maxItemLen);
146 if (ignoreCase)
147 cond = CompareNCaseInsensitive(word, item, lenWord);
148 else
149 cond = strncmp(word, item, lenWord);
150 if (0 != cond)
151 break;
152 --pivot;
154 location = pivot;
155 if (ignoreCase) {
156 // Check for exact-case match
157 for (; pivot <= end; pivot++) {
158 lb->GetValue(pivot, item, maxItemLen);
159 if (!strncmp(word, item, lenWord)) {
160 location = pivot;
161 break;
163 if (CompareNCaseInsensitive(word, item, lenWord))
164 break;
167 } else if (cond < 0) {
168 end = pivot - 1;
169 } else if (cond > 0) {
170 start = pivot + 1;
173 if (location == -1 && autoHide)
174 Cancel();
175 else
176 lb->Select(location);