]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/AutoComplete.cxx
7f7412e43fa9f10d37910ff87248d6ff616a2267
[wxWidgets.git] / src / stc / scintilla / src / AutoComplete.cxx
1 // Scintilla source code edit control
2 /** @file AutoComplete.cxx
3 ** Defines the auto completion list box.
4 **/
5 // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdio.h>
11
12 #include "Platform.h"
13
14 #include "PropSet.h"
15 #include "AutoComplete.h"
16
17 AutoComplete::AutoComplete() :
18 active(false),
19 separator(' '),
20 ignoreCase(false),
21 chooseSingle(false),
22 posStart(0),
23 startLen(0),
24 cancelAtStartPos(true),
25 autoHide(true) {
26 stopChars[0] = '\0';
27 fillUpChars[0] = '\0';
28 }
29
30 AutoComplete::~AutoComplete() {
31 lb.Destroy();
32 }
33
34 bool AutoComplete::Active() {
35 return active;
36 }
37
38 void AutoComplete::Start(Window &parent, int ctrlID, int position, int startLen_) {
39 if (!lb.Created()) {
40 lb.Create(parent, ctrlID);
41 }
42 lb.Clear();
43 active = true;
44 startLen = startLen_;
45 posStart = position;
46 }
47
48 void AutoComplete::SetStopChars(const char *stopChars_) {
49 strncpy(stopChars, stopChars_, sizeof(stopChars));
50 stopChars[sizeof(stopChars) - 1] = '\0';
51 }
52
53 bool AutoComplete::IsStopChar(char ch) {
54 return ch && strchr(stopChars, ch);
55 }
56
57 void AutoComplete::SetFillUpChars(const char *fillUpChars_) {
58 strncpy(fillUpChars, fillUpChars_, sizeof(fillUpChars));
59 fillUpChars[sizeof(fillUpChars) - 1] = '\0';
60 }
61
62 bool AutoComplete::IsFillUpChar(char ch) {
63 return ch && strchr(fillUpChars, ch);
64 }
65
66 void AutoComplete::SetSeparator(char separator_) {
67 separator = separator_;
68 }
69
70 char AutoComplete::GetSeparator() {
71 return separator;
72 }
73
74 void AutoComplete::SetList(const char *list) {
75 lb.Clear();
76 char *words = new char[strlen(list) + 1];
77 if (words) {
78 strcpy(words, list);
79 char *startword = words;
80 int i = 0;
81 for (; words && words[i]; i++) {
82 if (words[i] == separator) {
83 words[i] = '\0';
84 lb.Append(startword);
85 startword = words + i + 1;
86 }
87 }
88 if (startword) {
89 lb.Append(startword);
90 }
91 delete []words;
92 }
93 }
94
95 void AutoComplete::Show() {
96 lb.Show();
97 lb.Select(0);
98 }
99
100 void AutoComplete::Cancel() {
101 if (lb.Created()) {
102 lb.Destroy();
103 active = false;
104 }
105 }
106
107
108 void AutoComplete::Move(int delta) {
109 int count = lb.Length();
110 int current = lb.GetSelection();
111 current += delta;
112 if (current >= count)
113 current = count - 1;
114 if (current < 0)
115 current = 0;
116 lb.Select(current);
117 }
118
119 void AutoComplete::Select(const char *word) {
120 int lenWord = strlen(word);
121 int location = -1;
122 const int maxItemLen=1000;
123 char item[maxItemLen];
124 int start = 0; // lower bound of the api array block to search
125 int end = lb.Length() - 1; // upper bound of the api array block to search
126 while ((start <= end) && (location == -1)) { // Binary searching loop
127 int pivot = (start + end) / 2;
128 lb.GetValue(pivot, item, maxItemLen);
129 int cond;
130 if (ignoreCase)
131 cond = CompareNCaseInsensitive(word, item, lenWord);
132 else
133 cond = strncmp(word, item, lenWord);
134 if (!cond) {
135 // Find first match
136 while (pivot > start) {
137 lb.GetValue(pivot-1, item, maxItemLen);
138 if (ignoreCase)
139 cond = CompareNCaseInsensitive(word, item, lenWord);
140 else
141 cond = strncmp(word, item, lenWord);
142 if (0 != cond)
143 break;
144 --pivot;
145 }
146 location = pivot;
147 } else if (cond < 0) {
148 end = pivot - 1;
149 } else if (cond > 0) {
150 start = pivot + 1;
151 }
152 }
153 if (location == -1 && autoHide)
154 Cancel();
155 else
156 lb.Select(location);
157 }
158