]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/ScintillaBase.cxx
a5e5a065d19ba9347417ffb7307f276e1757a6cf
[wxWidgets.git] / src / stc / scintilla / src / ScintillaBase.cxx
1 // Scintilla source code edit control
2 /** @file ScintillaBase.cxx
3 ** An enhanced subclass of Editor with calltips, autocomplete and context menu.
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 #include <ctype.h>
12
13 #include "Platform.h"
14
15 #include "Scintilla.h"
16 #include "PropSet.h"
17 #ifdef SCI_LEXER
18 #include "SciLexer.h"
19 #include "Accessor.h"
20 #include "DocumentAccessor.h"
21 #include "KeyWords.h"
22 #endif
23 #include "ContractionState.h"
24 #include "SVector.h"
25 #include "CellBuffer.h"
26 #include "CallTip.h"
27 #include "KeyMap.h"
28 #include "Indicator.h"
29 #include "LineMarker.h"
30 #include "Style.h"
31 #include "ViewStyle.h"
32 #include "AutoComplete.h"
33 #include "Document.h"
34 #include "Editor.h"
35 #include "ScintillaBase.h"
36
37 ScintillaBase::ScintillaBase() {
38 displayPopupMenu = true;
39 listType = 0;
40 #ifdef SCI_LEXER
41 lexLanguage = SCLEX_CONTAINER;
42 lexCurrent = 0;
43 for (int wl = 0;wl < numWordLists;wl++)
44 keyWordLists[wl] = new WordList;
45 keyWordLists[numWordLists] = 0;
46 #endif
47 }
48
49 ScintillaBase::~ScintillaBase() {
50 #ifdef SCI_LEXER
51 for (int wl = 0;wl < numWordLists;wl++)
52 delete keyWordLists[wl];
53 #endif
54 }
55
56 void ScintillaBase::Finalise() {
57 Editor::Finalise();
58 popup.Destroy();
59 }
60
61 void ScintillaBase::RefreshColourPalette(Palette &pal, bool want) {
62 Editor::RefreshColourPalette(pal, want);
63 ct.RefreshColourPalette(pal, want);
64 }
65
66 void ScintillaBase::AddCharUTF(char *s, unsigned int len, bool treatAsDBCS) {
67 bool isFillUp = ac.Active() && ac.IsFillUpChar(*s);
68 if (!isFillUp) {
69 Editor::AddCharUTF(s, len, treatAsDBCS);
70 }
71 if (ac.Active()) {
72 AutoCompleteCharacterAdded(s[0]);
73 // For fill ups add the character after the autocompletion has
74 // triggered so containers see the key so can display a calltip.
75 if (isFillUp) {
76 Editor::AddCharUTF(s, len, treatAsDBCS);
77 }
78 }
79 }
80
81 void ScintillaBase::Command(int cmdId) {
82
83 switch (cmdId) {
84
85 case idAutoComplete: // Nothing to do
86
87 break;
88
89 case idCallTip: // Nothing to do
90
91 break;
92
93 case idcmdUndo:
94 WndProc(SCI_UNDO, 0, 0);
95 break;
96
97 case idcmdRedo:
98 WndProc(SCI_REDO, 0, 0);
99 break;
100
101 case idcmdCut:
102 WndProc(SCI_CUT, 0, 0);
103 break;
104
105 case idcmdCopy:
106 WndProc(SCI_COPY, 0, 0);
107 break;
108
109 case idcmdPaste:
110 WndProc(SCI_PASTE, 0, 0);
111 break;
112
113 case idcmdDelete:
114 WndProc(SCI_CLEAR, 0, 0);
115 break;
116
117 case idcmdSelectAll:
118 WndProc(SCI_SELECTALL, 0, 0);
119 break;
120 }
121 }
122
123 int ScintillaBase::KeyCommand(unsigned int iMessage) {
124 // Most key commands cancel autocompletion mode
125 if (ac.Active()) {
126 switch (iMessage) {
127 // Except for these
128 case SCI_LINEDOWN:
129 AutoCompleteMove(1);
130 return 0;
131 case SCI_LINEUP:
132 AutoCompleteMove( -1);
133 return 0;
134 case SCI_PAGEDOWN:
135 AutoCompleteMove(5);
136 return 0;
137 case SCI_PAGEUP:
138 AutoCompleteMove( -5);
139 return 0;
140 case SCI_VCHOME:
141 AutoCompleteMove( -5000);
142 return 0;
143 case SCI_LINEEND:
144 AutoCompleteMove(5000);
145 return 0;
146 case SCI_DELETEBACK:
147 DelCharBack(true);
148 AutoCompleteCharacterDeleted();
149 EnsureCaretVisible();
150 return 0;
151 case SCI_DELETEBACKNOTLINE:
152 DelCharBack(false);
153 AutoCompleteCharacterDeleted();
154 EnsureCaretVisible();
155 return 0;
156 case SCI_TAB:
157 AutoCompleteCompleted();
158 return 0;
159 case SCI_NEWLINE:
160 AutoCompleteCompleted();
161 return 0;
162
163 default:
164 ac.Cancel();
165 }
166 }
167
168 if (ct.inCallTipMode) {
169 if (
170 (iMessage != SCI_CHARLEFT) &&
171 (iMessage != SCI_CHARLEFTEXTEND) &&
172 (iMessage != SCI_CHARRIGHT) &&
173 (iMessage != SCI_CHARLEFTEXTEND) &&
174 (iMessage != SCI_EDITTOGGLEOVERTYPE) &&
175 (iMessage != SCI_DELETEBACK) &&
176 (iMessage != SCI_DELETEBACKNOTLINE)
177 ) {
178 ct.CallTipCancel();
179 }
180 if ((iMessage == SCI_DELETEBACK) || (iMessage == SCI_DELETEBACKNOTLINE)) {
181 if (currentPos <= ct.posStartCallTip) {
182 ct.CallTipCancel();
183 }
184 }
185 }
186 return Editor::KeyCommand(iMessage);
187 }
188
189 void ScintillaBase::AutoCompleteDoubleClick(void* p) {
190 ScintillaBase* sci = reinterpret_cast<ScintillaBase*>(p);
191 sci->AutoCompleteCompleted();
192 }
193
194 void ScintillaBase::AutoCompleteStart(int lenEntered, const char *list) {
195 //Platform::DebugPrintf("AutoComplete %s\n", list);
196 ct.CallTipCancel();
197
198 if (ac.chooseSingle && (listType == 0)) {
199 if (list && !strchr(list, ac.GetSeparator())) {
200 if (ac.ignoreCase) {
201 SetEmptySelection(currentPos - lenEntered);
202 pdoc->DeleteChars(currentPos, lenEntered);
203 SetEmptySelection(currentPos);
204 pdoc->InsertString(currentPos, list);
205 SetEmptySelection(currentPos + strlen(list));
206 } else {
207 SetEmptySelection(currentPos);
208 pdoc->InsertString(currentPos, list + lenEntered);
209 SetEmptySelection(currentPos + strlen(list + lenEntered));
210 }
211 return;
212 }
213 }
214 ac.Start(wMain, idAutoComplete, currentPos, lenEntered);
215
216 PRectangle rcClient = GetClientRectangle();
217 Point pt = LocationFromPosition(currentPos - lenEntered);
218
219 int heightLB = 100;
220 int widthLB = 100;
221 if (pt.x >= rcClient.right - widthLB) {
222 HorizontalScrollTo(xOffset + pt.x - rcClient.right + widthLB);
223 Redraw();
224 pt = LocationFromPosition(currentPos);
225 }
226 PRectangle rcac;
227 rcac.left = pt.x - 5;
228 if (pt.y >= rcClient.bottom - heightLB && // Wont fit below.
229 pt.y >= (rcClient.bottom + rcClient.top) / 2) { // and there is more room above.
230 rcac.top = pt.y - heightLB;
231 if (rcac.top < 0) {
232 heightLB += rcac.top;
233 rcac.top = 0;
234 }
235 } else {
236 rcac.top = pt.y + vs.lineHeight;
237 }
238 rcac.right = rcac.left + widthLB;
239 rcac.bottom = Platform::Minimum(rcac.top + heightLB, rcClient.bottom);
240 ac.lb.SetPositionRelative(rcac, wMain);
241 ac.lb.SetFont(vs.styles[STYLE_DEFAULT].font);
242 ac.lb.SetAverageCharWidth(vs.styles[STYLE_DEFAULT].aveCharWidth);
243 ac.lb.SetDoubleClickAction(AutoCompleteDoubleClick, this);
244
245 ac.SetList(list);
246
247 // Fiddle the position of the list so it is right next to the target and wide enough for all its strings
248 PRectangle rcList = ac.lb.GetDesiredRect();
249 int heightAlloced = rcList.bottom - rcList.top;
250 widthLB = Platform::Maximum(widthLB, rcList.right - rcList.left);
251 // Make an allowance for large strings in list
252 rcList.left = pt.x - 5;
253 rcList.right = rcList.left + widthLB;
254 if (((pt.y + vs.lineHeight) >= (rcClient.bottom - heightAlloced)) && // Wont fit below.
255 ((pt.y + vs.lineHeight / 2) >= (rcClient.bottom + rcClient.top) / 2)) { // and there is more room above.
256 rcList.top = pt.y - heightAlloced;
257 } else {
258 rcList.top = pt.y + vs.lineHeight;
259 }
260 rcList.bottom = rcList.top + heightAlloced;
261 ac.lb.SetPositionRelative(rcList, wMain);
262 ac.Show();
263 if (lenEntered != 0) {
264 AutoCompleteMoveToCurrentWord();
265 }
266 }
267
268 void ScintillaBase::AutoCompleteCancel() {
269 ac.Cancel();
270 }
271
272 void ScintillaBase::AutoCompleteMove(int delta) {
273 ac.Move(delta);
274 }
275
276 void ScintillaBase::AutoCompleteMoveToCurrentWord() {
277 char wordCurrent[1000];
278 int i;
279 int startWord = ac.posStart - ac.startLen;
280 for (i = startWord; i < currentPos; i++)
281 wordCurrent[i - startWord] = pdoc->CharAt(i);
282 wordCurrent[i - startWord] = '\0';
283 ac.Select(wordCurrent);
284 }
285
286 void ScintillaBase::AutoCompleteCharacterAdded(char ch) {
287 if (ac.IsFillUpChar(ch)) {
288 AutoCompleteCompleted();
289 } else if (ac.IsStopChar(ch)) {
290 ac.Cancel();
291 } else {
292 AutoCompleteMoveToCurrentWord();
293 }
294 }
295
296 void ScintillaBase::AutoCompleteCharacterDeleted() {
297 if (currentPos <= ac.posStart - ac.startLen) {
298 ac.Cancel();
299 } else if (ac.cancelAtStartPos && (currentPos <= ac.posStart)) {
300 ac.Cancel();
301 } else {
302 AutoCompleteMoveToCurrentWord();
303 }
304 }
305
306 void ScintillaBase::AutoCompleteCompleted() {
307 int item = ac.lb.GetSelection();
308 char selected[1000];
309 if (item != -1) {
310 ac.lb.GetValue(item, selected, sizeof(selected));
311 }
312 ac.Cancel();
313
314 if (listType > 0) {
315 userListSelected = selected;
316 SCNotification scn;
317 scn.nmhdr.code = SCN_USERLISTSELECTION;
318 scn.message = 0;
319 scn.wParam = listType;
320 scn.lParam = 0;
321 scn.text = userListSelected.c_str();
322 NotifyParent(scn);
323 return;
324 }
325
326 Position firstPos = ac.posStart - ac.startLen;
327 Position endPos = currentPos;
328 if (ac.dropRestOfWord)
329 endPos = pdoc->ExtendWordSelect(endPos, 1, true);
330 if (endPos < firstPos)
331 return;
332 pdoc->BeginUndoAction();
333 if (endPos != firstPos) {
334 pdoc->DeleteChars(firstPos, endPos - firstPos);
335 }
336 SetEmptySelection(ac.posStart);
337 if (item != -1) {
338 SString piece = selected;
339 pdoc->InsertString(firstPos, piece.c_str());
340 SetEmptySelection(firstPos + piece.length());
341 }
342 pdoc->EndUndoAction();
343 }
344
345 void ScintillaBase::ContextMenu(Point pt) {
346 if (displayPopupMenu) {
347 bool writable = !WndProc(SCI_GETREADONLY, 0, 0);
348 popup.CreatePopUp();
349 AddToPopUp("Undo", idcmdUndo, writable && pdoc->CanUndo());
350 AddToPopUp("Redo", idcmdRedo, writable && pdoc->CanRedo());
351 AddToPopUp("");
352 AddToPopUp("Cut", idcmdCut, writable && currentPos != anchor);
353 AddToPopUp("Copy", idcmdCopy, currentPos != anchor);
354 AddToPopUp("Paste", idcmdPaste, writable && WndProc(SCI_CANPASTE, 0, 0));
355 AddToPopUp("Delete", idcmdDelete, writable && currentPos != anchor);
356 AddToPopUp("");
357 AddToPopUp("Select All", idcmdSelectAll);
358 popup.Show(pt, wMain);
359 }
360 }
361
362 void ScintillaBase::CancelModes() {
363 AutoCompleteCancel();
364 ct.CallTipCancel();
365 Editor::CancelModes();
366 }
367
368 void ScintillaBase::ButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt) {
369 CancelModes();
370 Editor::ButtonDown(pt, curTime, shift, ctrl, alt);
371 }
372
373 #ifdef SCI_LEXER
374 void ScintillaBase::SetLexer(uptr_t wParam) {
375 lexLanguage = wParam;
376 lexCurrent = LexerModule::Find(lexLanguage);
377 if (!lexCurrent)
378 lexCurrent = LexerModule::Find(SCLEX_NULL);
379 }
380
381 void ScintillaBase::SetLexerLanguage(const char *languageName) {
382 lexLanguage = SCLEX_CONTAINER;
383 lexCurrent = LexerModule::Find(languageName);
384 if (!lexCurrent)
385 lexCurrent = LexerModule::Find(SCLEX_NULL);
386 if (lexCurrent)
387 lexLanguage = lexCurrent->GetLanguage();
388 }
389
390 void ScintillaBase::Colourise(int start, int end) {
391 int lengthDoc = pdoc->Length();
392 if (end == -1)
393 end = lengthDoc;
394 int len = end - start;
395
396 PLATFORM_ASSERT(len >= 0);
397 PLATFORM_ASSERT(start + len <= lengthDoc);
398
399 //WindowAccessor styler(wMain.GetID(), props);
400 DocumentAccessor styler(pdoc, props, wMain.GetID());
401
402 int styleStart = 0;
403 if (start > 0)
404 styleStart = styler.StyleAt(start - 1);
405 styler.SetCodePage(pdoc->dbcsCodePage);
406
407 if (lexCurrent && (len > 0)) { // Should always succeed as null lexer should always be available
408 lexCurrent->Lex(start, len, styleStart, keyWordLists, styler);
409 styler.Flush();
410 if (styler.GetPropertyInt("fold")) {
411 lexCurrent->Fold(start, len, styleStart, keyWordLists, styler);
412 styler.Flush();
413 }
414 }
415 }
416 #endif
417
418 void ScintillaBase::NotifyStyleToNeeded(int endStyleNeeded) {
419 #ifdef SCI_LEXER
420 if (lexLanguage != SCLEX_CONTAINER) {
421 int endStyled = WndProc(SCI_GETENDSTYLED, 0, 0);
422 int lineEndStyled = WndProc(SCI_LINEFROMPOSITION, endStyled, 0);
423 endStyled = WndProc(SCI_POSITIONFROMLINE, lineEndStyled, 0);
424 Colourise(endStyled, endStyleNeeded);
425 return;
426 }
427 #endif
428 Editor::NotifyStyleToNeeded(endStyleNeeded);
429 }
430
431 sptr_t ScintillaBase::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) {
432 switch (iMessage) {
433 case SCI_AUTOCSHOW:
434 listType = 0;
435 AutoCompleteStart(wParam, reinterpret_cast<const char *>(lParam));
436 break;
437
438 case SCI_AUTOCCANCEL:
439 AutoCompleteCancel();
440 break;
441
442 case SCI_AUTOCACTIVE:
443 return ac.Active();
444
445 case SCI_AUTOCPOSSTART:
446 return ac.posStart;
447
448 case SCI_AUTOCCOMPLETE:
449 AutoCompleteCompleted();
450 break;
451
452 case SCI_AUTOCSETSEPARATOR:
453 ac.SetSeparator(static_cast<char>(wParam));
454 break;
455
456 case SCI_AUTOCGETSEPARATOR:
457 return ac.GetSeparator();
458
459 case SCI_AUTOCSTOPS:
460 ac.SetStopChars(reinterpret_cast<char *>(lParam));
461 break;
462
463 case SCI_AUTOCSELECT:
464 ac.Select(reinterpret_cast<char *>(lParam));
465 break;
466
467 case SCI_AUTOCSETCANCELATSTART:
468 ac.cancelAtStartPos = wParam != 0;
469 break;
470
471 case SCI_AUTOCGETCANCELATSTART:
472 return ac.cancelAtStartPos;
473
474 case SCI_AUTOCSETFILLUPS:
475 ac.SetFillUpChars(reinterpret_cast<char *>(lParam));
476 break;
477
478 case SCI_AUTOCSETCHOOSESINGLE:
479 ac.chooseSingle = wParam != 0;
480 break;
481
482 case SCI_AUTOCGETCHOOSESINGLE:
483 return ac.chooseSingle;
484
485 case SCI_AUTOCSETIGNORECASE:
486 ac.ignoreCase = wParam != 0;
487 break;
488
489 case SCI_AUTOCGETIGNORECASE:
490 return ac.ignoreCase;
491
492 case SCI_USERLISTSHOW:
493 listType = wParam;
494 AutoCompleteStart(0, reinterpret_cast<const char *>(lParam));
495 break;
496
497 case SCI_AUTOCSETAUTOHIDE:
498 ac.autoHide = wParam != 0;
499 break;
500
501 case SCI_AUTOCGETAUTOHIDE:
502 return ac.autoHide;
503
504 case SCI_AUTOCSETDROPRESTOFWORD:
505 ac.dropRestOfWord = wParam != 0;
506 break;
507
508 case SCI_AUTOCGETDROPRESTOFWORD:
509 return ac.dropRestOfWord;
510
511 case SCI_CALLTIPSHOW: {
512 AutoCompleteCancel();
513 if (!ct.wCallTip.Created()) {
514 Point pt = LocationFromPosition(wParam);
515 pt.y += vs.lineHeight;
516 PRectangle rc = ct.CallTipStart(currentPos, pt,
517 reinterpret_cast<char *>(lParam),
518 vs.styles[STYLE_DEFAULT].fontName,
519 vs.styles[STYLE_DEFAULT].sizeZoomed,
520 IsUnicodeMode());
521 // If the call-tip window would be out of the client
522 // space, adjust so it displays above the text.
523 PRectangle rcClient = GetClientRectangle();
524 if (rc.bottom > rcClient.bottom) {
525 int offset = vs.lineHeight + rc.Height();
526 rc.top -= offset;
527 rc.bottom -= offset;
528 }
529 // Now display the window.
530 CreateCallTipWindow(rc);
531 ct.wCallTip.SetPositionRelative(rc, wMain);
532 ct.wCallTip.Show();
533 }
534 }
535 break;
536
537 case SCI_CALLTIPCANCEL:
538 ct.CallTipCancel();
539 break;
540
541 case SCI_CALLTIPACTIVE:
542 return ct.inCallTipMode;
543
544 case SCI_CALLTIPPOSSTART:
545 return ct.posStartCallTip;
546
547 case SCI_CALLTIPSETHLT:
548 ct.SetHighlight(wParam, lParam);
549 break;
550
551 case SCI_CALLTIPSETBACK:
552 ct.colourBG = ColourDesired(wParam);
553 InvalidateStyleRedraw();
554 break;
555
556 case SCI_USEPOPUP:
557 displayPopupMenu = wParam != 0;
558 break;
559
560 #ifdef SCI_LEXER
561 case SCI_SETLEXER:
562 SetLexer(wParam);
563 lexLanguage = wParam;
564 break;
565
566 case SCI_GETLEXER:
567 return lexLanguage;
568
569 case SCI_COLOURISE:
570 Colourise(wParam, lParam);
571 Redraw();
572 break;
573
574 case SCI_SETPROPERTY:
575 props.Set(reinterpret_cast<const char *>(wParam),
576 reinterpret_cast<const char *>(lParam));
577 break;
578
579 case SCI_SETKEYWORDS:
580 if (wParam < numWordLists) {
581 keyWordLists[wParam]->Clear();
582 keyWordLists[wParam]->Set(reinterpret_cast<const char *>(lParam));
583 }
584 break;
585
586 case SCI_SETLEXERLANGUAGE:
587 SetLexerLanguage(reinterpret_cast<const char *>(lParam));
588 break;
589
590 #endif
591
592 default:
593 return Editor::WndProc(iMessage, wParam, lParam);
594 }
595 return 0l;
596 }