]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/AutoComplete.cxx
75e26fe28370ba47421f843707a6be0c4fac864a
[wxWidgets.git] / src / stc / scintilla / src / AutoComplete.cxx
1 // Scintilla source code edit control
2 // AutoComplete.cxx - defines the auto completion list box
3 // Copyright 1998-2000 by Neil Hodgson <neilh@scintilla.org>
4 // The License.txt file describes the conditions under which this software may be distributed.
5
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include "Platform.h"
10
11 #include "AutoComplete.h"
12
13 AutoComplete::AutoComplete() {
14 lb = 0;
15 active = false;
16 posStart = 0;
17 strcpy(stopChars, "");
18 separator = ' ';
19 }
20
21 AutoComplete::~AutoComplete() {
22 lb.Destroy();
23 }
24
25 bool AutoComplete::Active() {
26 return active;
27 }
28
29 void AutoComplete::Start(Window &parent, int ctrlID, int position, int startLen_) {
30 if (!lb.Created()) {
31 lb.Create(parent, ctrlID);
32 }
33 lb.Clear();
34 active = true;
35 startLen = startLen_;
36 posStart = position;
37 }
38
39 void AutoComplete::SetStopChars(const char *stopChars_) {
40 strncpy(stopChars, stopChars_, sizeof(stopChars));
41 stopChars[sizeof(stopChars) - 1] = '\0';
42 }
43
44 bool AutoComplete::IsStopChar(char ch) {
45 return ch && strchr(stopChars, ch);
46 }
47
48 void AutoComplete::SetSeparator(char separator_) {
49 separator = separator_;
50 }
51
52 char AutoComplete::GetSeparator() {
53 return separator;
54 }
55
56 int AutoComplete::SetList(const char *list) {
57 int maxStrLen = 12;
58 lb.Clear();
59 char *words = new char[strlen(list) + 1];
60 if (words) {
61 strcpy(words, list);
62 char *startword = words;
63 int i = 0;
64 for (; words && words[i]; i++) {
65 if (words[i] == separator) {
66 words[i] = '\0';
67 lb.Append(startword);
68 maxStrLen = Platform::Maximum(maxStrLen, strlen(startword));
69 startword = words + i + 1;
70 }
71 }
72 if (startword) {
73 lb.Append(startword);
74 maxStrLen = Platform::Maximum(maxStrLen, strlen(startword));
75 }
76 delete []words;
77 }
78 lb.Sort();
79 return maxStrLen;
80 }
81
82 void AutoComplete::Show() {
83 lb.Show();
84 lb.Select(0);
85 }
86
87 void AutoComplete::Cancel() {
88 if (lb.Created()) {
89 lb.Destroy();
90 lb = 0;
91 active = false;
92 }
93 }
94
95
96 void AutoComplete::Move(int delta) {
97 int count = lb.Length();
98 int current = lb.GetSelection();
99 current += delta;
100 if (current >= count)
101 current = count - 1;
102 if (current < 0)
103 current = 0;
104 lb.Select(current);
105 }
106
107 void AutoComplete::Select(const char *word) {
108 int pos = lb.Find(word);
109 //Platform::DebugPrintf("Autocompleting at <%s> %d\n", wordCurrent, pos);
110 if (pos != -1)
111 lb.Select(pos);
112 }
113