]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/ScintillaBase.cxx
7958661521984f9f4e104d70cf17557ba28dd37f
[wxWidgets.git] / contrib / 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 AutoCompleteChanged(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 AutoCompleteChanged();
149 EnsureCaretVisible();
150 return 0;
151 case SCI_DELETEBACKNOTLINE:
152 DelCharBack(false);
153 AutoCompleteChanged();
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::AutoCompleteChanged(char ch) {
287 if (ac.IsFillUpChar(ch)) {
288 AutoCompleteCompleted();
289 } else if (currentPos <= ac.posStart - ac.startLen) {
290 ac.Cancel();
291 } else if (ac.cancelAtStartPos && currentPos <= ac.posStart) {
292 ac.Cancel();
293 } else if (ac.IsStopChar(ch)) {
294 ac.Cancel();
295 } else {
296 AutoCompleteMoveToCurrentWord();
297 }
298 }
299
300 void ScintillaBase::AutoCompleteCompleted() {
301 int item = ac.lb.GetSelection();
302 char selected[1000];
303 if (item != -1) {
304 ac.lb.GetValue(item, selected, sizeof(selected));
305 }
306 ac.Cancel();
307
308 if (listType > 0) {
309 userListSelected = selected;
310 SCNotification scn;
311 scn.nmhdr.code = SCN_USERLISTSELECTION;
312 scn.message = 0;
313 scn.wParam = listType;
314 scn.lParam = 0;
315 scn.text = userListSelected.c_str();
316 NotifyParent(scn);
317 return;
318 }
319
320 Position firstPos = ac.posStart - ac.startLen;
321 Position endPos = currentPos;
322 if (ac.dropRestOfWord)
323 endPos = pdoc->ExtendWordSelect(endPos, 1, true);
324 if (endPos < firstPos)
325 return;
326 pdoc->BeginUndoAction();
327 if (endPos != firstPos) {
328 pdoc->DeleteChars(firstPos, endPos - firstPos);
329 }
330 SetEmptySelection(ac.posStart);
331 if (item != -1) {
332 SString piece = selected;
333 pdoc->InsertString(firstPos, piece.c_str());
334 SetEmptySelection(firstPos + piece.length());
335 }
336 pdoc->EndUndoAction();
337 }
338
339 void ScintillaBase::ContextMenu(Point pt) {
340 if (displayPopupMenu) {
341 bool writable = !WndProc(SCI_GETREADONLY, 0, 0);
342 popup.CreatePopUp();
343 AddToPopUp("Undo", idcmdUndo, writable && pdoc->CanUndo());
344 AddToPopUp("Redo", idcmdRedo, writable && pdoc->CanRedo());
345 AddToPopUp("");
346 AddToPopUp("Cut", idcmdCut, writable && currentPos != anchor);
347 AddToPopUp("Copy", idcmdCopy, currentPos != anchor);
348 AddToPopUp("Paste", idcmdPaste, writable && WndProc(SCI_CANPASTE, 0, 0));
349 AddToPopUp("Delete", idcmdDelete, writable && currentPos != anchor);
350 AddToPopUp("");
351 AddToPopUp("Select All", idcmdSelectAll);
352 popup.Show(pt, wMain);
353 }
354 }
355
356 void ScintillaBase::CancelModes() {
357 AutoCompleteCancel();
358 ct.CallTipCancel();
359 Editor::CancelModes();
360 }
361
362 void ScintillaBase::ButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt) {
363 CancelModes();
364 Editor::ButtonDown(pt, curTime, shift, ctrl, alt);
365 }
366
367 #ifdef SCI_LEXER
368 void ScintillaBase::SetLexer(uptr_t wParam) {
369 lexLanguage = wParam;
370 lexCurrent = LexerModule::Find(lexLanguage);
371 if (!lexCurrent)
372 lexCurrent = LexerModule::Find(SCLEX_NULL);
373 }
374
375 void ScintillaBase::SetLexerLanguage(const char *languageName) {
376 lexLanguage = SCLEX_CONTAINER;
377 lexCurrent = LexerModule::Find(languageName);
378 if (!lexCurrent)
379 lexCurrent = LexerModule::Find(SCLEX_NULL);
380 if (lexCurrent)
381 lexLanguage = lexCurrent->GetLanguage();
382 }
383
384 void ScintillaBase::Colourise(int start, int end) {
385 int lengthDoc = pdoc->Length();
386 if (end == -1)
387 end = lengthDoc;
388 int len = end - start;
389
390 PLATFORM_ASSERT(len >= 0);
391 PLATFORM_ASSERT(start + len <= lengthDoc);
392
393 //WindowAccessor styler(wMain.GetID(), props);
394 DocumentAccessor styler(pdoc, props, wMain.GetID());
395
396 int styleStart = 0;
397 if (start > 0)
398 styleStart = styler.StyleAt(start - 1);
399 styler.SetCodePage(pdoc->dbcsCodePage);
400
401 if (lexCurrent && (len > 0)) { // Should always succeed as null lexer should always be available
402 lexCurrent->Lex(start, len, styleStart, keyWordLists, styler);
403 styler.Flush();
404 if (styler.GetPropertyInt("fold")) {
405 lexCurrent->Fold(start, len, styleStart, keyWordLists, styler);
406 styler.Flush();
407 }
408 }
409 }
410 #endif
411
412 void ScintillaBase::NotifyStyleToNeeded(int endStyleNeeded) {
413 #ifdef SCI_LEXER
414 if (lexLanguage != SCLEX_CONTAINER) {
415 int endStyled = WndProc(SCI_GETENDSTYLED, 0, 0);
416 int lineEndStyled = WndProc(SCI_LINEFROMPOSITION, endStyled, 0);
417 endStyled = WndProc(SCI_POSITIONFROMLINE, lineEndStyled, 0);
418 Colourise(endStyled, endStyleNeeded);
419 return;
420 }
421 #endif
422 Editor::NotifyStyleToNeeded(endStyleNeeded);
423 }
424
425 sptr_t ScintillaBase::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) {
426 switch (iMessage) {
427 case SCI_AUTOCSHOW:
428 listType = 0;
429 AutoCompleteStart(wParam, reinterpret_cast<const char *>(lParam));
430 break;
431
432 case SCI_AUTOCCANCEL:
433 AutoCompleteCancel();
434 break;
435
436 case SCI_AUTOCACTIVE:
437 return ac.Active();
438
439 case SCI_AUTOCPOSSTART:
440 return ac.posStart;
441
442 case SCI_AUTOCCOMPLETE:
443 AutoCompleteCompleted();
444 break;
445
446 case SCI_AUTOCSETSEPARATOR:
447 ac.SetSeparator(static_cast<char>(wParam));
448 break;
449
450 case SCI_AUTOCGETSEPARATOR:
451 return ac.GetSeparator();
452
453 case SCI_AUTOCSTOPS:
454 ac.SetStopChars(reinterpret_cast<char *>(lParam));
455 break;
456
457 case SCI_AUTOCSELECT:
458 ac.Select(reinterpret_cast<char *>(lParam));
459 break;
460
461 case SCI_AUTOCSETCANCELATSTART:
462 ac.cancelAtStartPos = wParam != 0;
463 break;
464
465 case SCI_AUTOCGETCANCELATSTART:
466 return ac.cancelAtStartPos;
467
468 case SCI_AUTOCSETFILLUPS:
469 ac.SetFillUpChars(reinterpret_cast<char *>(lParam));
470 break;
471
472 case SCI_AUTOCSETCHOOSESINGLE:
473 ac.chooseSingle = wParam != 0;
474 break;
475
476 case SCI_AUTOCGETCHOOSESINGLE:
477 return ac.chooseSingle;
478
479 case SCI_AUTOCSETIGNORECASE:
480 ac.ignoreCase = wParam != 0;
481 break;
482
483 case SCI_AUTOCGETIGNORECASE:
484 return ac.ignoreCase;
485
486 case SCI_USERLISTSHOW:
487 listType = wParam;
488 AutoCompleteStart(0, reinterpret_cast<const char *>(lParam));
489 break;
490
491 case SCI_AUTOCSETAUTOHIDE:
492 ac.autoHide = wParam != 0;
493 break;
494
495 case SCI_AUTOCGETAUTOHIDE:
496 return ac.autoHide;
497
498 case SCI_AUTOCSETDROPRESTOFWORD:
499 ac.dropRestOfWord = wParam != 0;
500 break;
501
502 case SCI_AUTOCGETDROPRESTOFWORD:
503 return ac.dropRestOfWord;
504
505 case SCI_CALLTIPSHOW: {
506 AutoCompleteCancel();
507 if (!ct.wCallTip.Created()) {
508 Point pt = LocationFromPosition(wParam);
509 pt.y += vs.lineHeight;
510 PRectangle rc = ct.CallTipStart(currentPos, pt,
511 reinterpret_cast<char *>(lParam),
512 vs.styles[STYLE_DEFAULT].fontName,
513 vs.styles[STYLE_DEFAULT].sizeZoomed,
514 IsUnicodeMode());
515 // If the call-tip window would be out of the client
516 // space, adjust so it displays above the text.
517 PRectangle rcClient = GetClientRectangle();
518 if (rc.bottom > rcClient.bottom) {
519 int offset = vs.lineHeight + rc.Height();
520 rc.top -= offset;
521 rc.bottom -= offset;
522 }
523 // Now display the window.
524 CreateCallTipWindow(rc);
525 ct.wCallTip.SetPositionRelative(rc, wMain);
526 ct.wCallTip.Show();
527 }
528 }
529 break;
530
531 case SCI_CALLTIPCANCEL:
532 ct.CallTipCancel();
533 break;
534
535 case SCI_CALLTIPACTIVE:
536 return ct.inCallTipMode;
537
538 case SCI_CALLTIPPOSSTART:
539 return ct.posStartCallTip;
540
541 case SCI_CALLTIPSETHLT:
542 ct.SetHighlight(wParam, lParam);
543 break;
544
545 case SCI_CALLTIPSETBACK:
546 ct.colourBG = ColourDesired(wParam);
547 InvalidateStyleRedraw();
548 break;
549
550 case SCI_USEPOPUP:
551 displayPopupMenu = wParam != 0;
552 break;
553
554 #ifdef SCI_LEXER
555 case SCI_SETLEXER:
556 SetLexer(wParam);
557 lexLanguage = wParam;
558 break;
559
560 case SCI_GETLEXER:
561 return lexLanguage;
562
563 case SCI_COLOURISE:
564 Colourise(wParam, lParam);
565 Redraw();
566 break;
567
568 case SCI_SETPROPERTY:
569 props.Set(reinterpret_cast<const char *>(wParam),
570 reinterpret_cast<const char *>(lParam));
571 break;
572
573 case SCI_SETKEYWORDS:
574 if (wParam < numWordLists) {
575 keyWordLists[wParam]->Clear();
576 keyWordLists[wParam]->Set(reinterpret_cast<const char *>(lParam));
577 }
578 break;
579
580 case SCI_SETLEXERLANGUAGE:
581 SetLexerLanguage(reinterpret_cast<const char *>(lParam));
582 break;
583
584 #endif
585
586 default:
587 return Editor::WndProc(iMessage, wParam, lParam);
588 }
589 return 0l;
590 }