]>
Commit | Line | Data |
---|---|---|
9ce192d4 | 1 | // Scintilla source code edit control |
65ec6247 RD |
2 | /** @file AutoComplete.cxx |
3 | ** Defines the auto completion list box. | |
4 | **/ | |
5 | // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org> | |
9ce192d4 RD |
6 | // The License.txt file describes the conditions under which this software may be distributed. |
7 | ||
8 | #include <stdlib.h> | |
9 | #include <string.h> | |
65ec6247 | 10 | #include <stdio.h> |
9ce192d4 RD |
11 | |
12 | #include "Platform.h" | |
13 | ||
65ec6247 | 14 | #include "PropSet.h" |
9ce192d4 RD |
15 | #include "AutoComplete.h" |
16 | ||
d134f170 RD |
17 | AutoComplete::AutoComplete() : |
18 | active(false), | |
19 | separator(' '), | |
20 | ignoreCase(false), | |
21 | chooseSingle(false), | |
22 | posStart(0), | |
23 | startLen(0), | |
65ec6247 RD |
24 | cancelAtStartPos(true), |
25 | autoHide(true) { | |
d134f170 RD |
26 | stopChars[0] = '\0'; |
27 | fillUpChars[0] = '\0'; | |
9ce192d4 RD |
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 | ||
d134f170 RD |
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 | ||
f6bcfd97 BP |
66 | void AutoComplete::SetSeparator(char separator_) { |
67 | separator = separator_; | |
68 | } | |
69 | ||
70 | char AutoComplete::GetSeparator() { | |
71 | return separator; | |
72 | } | |
73 | ||
d134f170 | 74 | void AutoComplete::SetList(const char *list) { |
9ce192d4 RD |
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++) { | |
f6bcfd97 | 82 | if (words[i] == separator) { |
9ce192d4 RD |
83 | words[i] = '\0'; |
84 | lb.Append(startword); | |
9ce192d4 RD |
85 | startword = words + i + 1; |
86 | } | |
87 | } | |
88 | if (startword) { | |
89 | lb.Append(startword); | |
9ce192d4 RD |
90 | } |
91 | delete []words; | |
92 | } | |
9ce192d4 RD |
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(); | |
9ce192d4 RD |
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) { | |
65ec6247 RD |
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); | |
9ce192d4 RD |
157 | } |
158 |