]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/AutoComplete.cxx
48aaee38ae75cbb7d598f24184a076db443aec76
[wxWidgets.git] / contrib / 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-2002 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 dropRestOfWord(false) {
27 stopChars[0] = '\0';
28 fillUpChars[0] = '\0';
29 }
30
31 AutoComplete::~AutoComplete() {
32 lb.Destroy();
33 }
34
35 bool AutoComplete::Active() {
36 return active;
37 }
38
39 void AutoComplete::Start(Window &parent, int ctrlID, int position, int startLen_) {
40 if (!lb.Created()) {
41 lb.Create(parent, ctrlID);
42 }
43 lb.Clear();
44 active = true;
45 startLen = startLen_;
46 posStart = position;
47 }
48
49 void AutoComplete::SetStopChars(const char *stopChars_) {
50 strncpy(stopChars, stopChars_, sizeof(stopChars));
51 stopChars[sizeof(stopChars) - 1] = '\0';
52 }
53
54 bool AutoComplete::IsStopChar(char ch) {
55 return ch && strchr(stopChars, ch);
56 }
57
58 void AutoComplete::SetFillUpChars(const char *fillUpChars_) {
59 strncpy(fillUpChars, fillUpChars_, sizeof(fillUpChars));
60 fillUpChars[sizeof(fillUpChars) - 1] = '\0';
61 }
62
63 bool AutoComplete::IsFillUpChar(char ch) {
64 return ch && strchr(fillUpChars, ch);
65 }
66
67 void AutoComplete::SetSeparator(char separator_) {
68 separator = separator_;
69 }
70
71 char AutoComplete::GetSeparator() {
72 return separator;
73 }
74
75 void AutoComplete::SetList(const char *list) {
76 lb.Clear();
77 char *words = new char[strlen(list) + 1];
78 if (words) {
79 strcpy(words, list);
80 char *startword = words;
81 int i = 0;
82 for (; words && words[i]; i++) {
83 if (words[i] == separator) {
84 words[i] = '\0';
85 lb.Append(startword);
86 startword = words + i + 1;
87 }
88 }
89 if (startword) {
90 lb.Append(startword);
91 }
92 delete []words;
93 }
94 }
95
96 void AutoComplete::Show() {
97 lb.Show();
98 lb.Select(0);
99 }
100
101 void AutoComplete::Cancel() {
102 if (lb.Created()) {
103 lb.Destroy();
104 active = false;
105 }
106 }
107
108
109 void AutoComplete::Move(int delta) {
110 int count = lb.Length();
111 int current = lb.GetSelection();
112 current += delta;
113 if (current >= count)
114 current = count - 1;
115 if (current < 0)
116 current = 0;
117 lb.Select(current);
118 }
119
120 void AutoComplete::Select(const char *word) {
121 int lenWord = strlen(word);
122 int location = -1;
123 const int maxItemLen=1000;
124 char item[maxItemLen];
125 int start = 0; // lower bound of the api array block to search
126 int end = lb.Length() - 1; // upper bound of the api array block to search
127 while ((start <= end) && (location == -1)) { // Binary searching loop
128 int pivot = (start + end) / 2;
129 lb.GetValue(pivot, item, maxItemLen);
130 int cond;
131 if (ignoreCase)
132 cond = CompareNCaseInsensitive(word, item, lenWord);
133 else
134 cond = strncmp(word, item, lenWord);
135 if (!cond) {
136 // Find first match
137 while (pivot > start) {
138 lb.GetValue(pivot-1, item, maxItemLen);
139 if (ignoreCase)
140 cond = CompareNCaseInsensitive(word, item, lenWord);
141 else
142 cond = strncmp(word, item, lenWord);
143 if (0 != cond)
144 break;
145 --pivot;
146 }
147 location = pivot;
148 } else if (cond < 0) {
149 end = pivot - 1;
150 } else if (cond > 0) {
151 start = pivot + 1;
152 }
153 }
154 if (location == -1 && autoHide)
155 Cancel();
156 else
157 lb.Select(location);
158 }
159