1 // Scintilla source code edit control
2 /** @file ScintillaBase.cxx
3 ** An enhanced subclass of Editor with calltips, autocomplete and context menu.
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.
15 #include "Scintilla.h"
20 #include "DocumentAccessor.h"
23 #include "ContractionState.h"
25 #include "CellBuffer.h"
28 #include "Indicator.h"
29 #include "LineMarker.h"
31 #include "ViewStyle.h"
32 #include "AutoComplete.h"
35 #include "ScintillaBase.h"
37 ScintillaBase::ScintillaBase() {
38 displayPopupMenu
= true;
41 lexLanguage
= SCLEX_CONTAINER
;
43 for (int wl
= 0;wl
< numWordLists
;wl
++)
44 keyWordLists
[wl
] = new WordList
;
45 keyWordLists
[numWordLists
] = 0;
49 ScintillaBase::~ScintillaBase() {
51 for (int wl
= 0;wl
< numWordLists
;wl
++)
52 delete keyWordLists
[wl
];
56 void ScintillaBase::Finalise() {
61 void ScintillaBase::RefreshColourPalette(Palette
&pal
, bool want
) {
62 Editor::RefreshColourPalette(pal
, want
);
63 ct
.RefreshColourPalette(pal
, want
);
66 void ScintillaBase::AddCharUTF(char *s
, unsigned int len
, bool treatAsDBCS
) {
67 bool isFillUp
= ac
.Active() && ac
.IsFillUpChar(*s
);
69 Editor::AddCharUTF(s
, len
, treatAsDBCS
);
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.
76 Editor::AddCharUTF(s
, len
, treatAsDBCS
);
81 void ScintillaBase::Command(int cmdId
) {
85 case idAutoComplete
: // Nothing to do
89 case idCallTip
: // Nothing to do
94 WndProc(SCI_UNDO
, 0, 0);
98 WndProc(SCI_REDO
, 0, 0);
102 WndProc(SCI_CUT
, 0, 0);
106 WndProc(SCI_COPY
, 0, 0);
110 WndProc(SCI_PASTE
, 0, 0);
114 WndProc(SCI_CLEAR
, 0, 0);
118 WndProc(SCI_SELECTALL
, 0, 0);
123 int ScintillaBase::KeyCommand(unsigned int iMessage
) {
124 // Most key commands cancel autocompletion mode
132 AutoCompleteMove( -1);
138 AutoCompleteMove( -5);
141 AutoCompleteMove( -5000);
144 AutoCompleteMove(5000);
148 AutoCompleteCharacterDeleted();
149 EnsureCaretVisible();
151 case SCI_DELETEBACKNOTLINE
:
153 AutoCompleteCharacterDeleted();
154 EnsureCaretVisible();
157 AutoCompleteCompleted();
160 AutoCompleteCompleted();
168 if (ct
.inCallTipMode
) {
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
)
180 if ((iMessage
== SCI_DELETEBACK
) || (iMessage
== SCI_DELETEBACKNOTLINE
)) {
181 if (currentPos
<= ct
.posStartCallTip
) {
186 return Editor::KeyCommand(iMessage
);
189 void ScintillaBase::AutoCompleteDoubleClick(void* p
) {
190 ScintillaBase
* sci
= reinterpret_cast<ScintillaBase
*>(p
);
191 sci
->AutoCompleteCompleted();
194 void ScintillaBase::AutoCompleteStart(int lenEntered
, const char *list
) {
195 //Platform::DebugPrintf("AutoComplete %s\n", list);
198 if (ac
.chooseSingle
&& (listType
== 0)) {
199 if (list
&& !strchr(list
, ac
.GetSeparator())) {
201 SetEmptySelection(currentPos
- lenEntered
);
202 pdoc
->DeleteChars(currentPos
, lenEntered
);
203 SetEmptySelection(currentPos
);
204 pdoc
->InsertString(currentPos
, list
);
205 SetEmptySelection(currentPos
+ strlen(list
));
207 SetEmptySelection(currentPos
);
208 pdoc
->InsertString(currentPos
, list
+ lenEntered
);
209 SetEmptySelection(currentPos
+ strlen(list
+ lenEntered
));
214 ac
.Start(wMain
, idAutoComplete
, currentPos
, lenEntered
);
216 PRectangle rcClient
= GetClientRectangle();
217 Point pt
= LocationFromPosition(currentPos
- lenEntered
);
221 if (pt
.x
>= rcClient
.right
- widthLB
) {
222 HorizontalScrollTo(xOffset
+ pt
.x
- rcClient
.right
+ widthLB
);
224 pt
= LocationFromPosition(currentPos
);
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
;
232 heightLB
+= rcac
.top
;
236 rcac
.top
= pt
.y
+ vs
.lineHeight
;
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);
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
;
258 rcList
.top
= pt
.y
+ vs
.lineHeight
;
260 rcList
.bottom
= rcList
.top
+ heightAlloced
;
261 ac
.lb
.SetPositionRelative(rcList
, wMain
);
263 if (lenEntered
!= 0) {
264 AutoCompleteMoveToCurrentWord();
268 void ScintillaBase::AutoCompleteCancel() {
272 void ScintillaBase::AutoCompleteMove(int delta
) {
276 void ScintillaBase::AutoCompleteMoveToCurrentWord() {
277 char wordCurrent
[1000];
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
);
286 void ScintillaBase::AutoCompleteCharacterAdded(char ch
) {
287 if (ac
.IsFillUpChar(ch
)) {
288 AutoCompleteCompleted();
289 } else if (ac
.IsStopChar(ch
)) {
292 AutoCompleteMoveToCurrentWord();
296 void ScintillaBase::AutoCompleteCharacterDeleted() {
297 if (currentPos
<= ac
.posStart
- ac
.startLen
) {
299 } else if (ac
.cancelAtStartPos
&& (currentPos
<= ac
.posStart
)) {
302 AutoCompleteMoveToCurrentWord();
306 void ScintillaBase::AutoCompleteCompleted() {
307 int item
= ac
.lb
.GetSelection();
310 ac
.lb
.GetValue(item
, selected
, sizeof(selected
));
315 userListSelected
= selected
;
317 scn
.nmhdr
.code
= SCN_USERLISTSELECTION
;
319 scn
.wParam
= listType
;
320 scn
.listType
= listType
;
322 scn
.text
= userListSelected
.c_str();
327 Position firstPos
= ac
.posStart
- ac
.startLen
;
328 Position endPos
= currentPos
;
329 if (ac
.dropRestOfWord
)
330 endPos
= pdoc
->ExtendWordSelect(endPos
, 1, true);
331 if (endPos
< firstPos
)
333 pdoc
->BeginUndoAction();
334 if (endPos
!= firstPos
) {
335 pdoc
->DeleteChars(firstPos
, endPos
- firstPos
);
337 SetEmptySelection(ac
.posStart
);
339 SString piece
= selected
;
340 pdoc
->InsertString(firstPos
, piece
.c_str());
341 SetEmptySelection(firstPos
+ piece
.length());
343 pdoc
->EndUndoAction();
346 void ScintillaBase::ContextMenu(Point pt
) {
347 if (displayPopupMenu
) {
348 bool writable
= !WndProc(SCI_GETREADONLY
, 0, 0);
350 AddToPopUp("Undo", idcmdUndo
, writable
&& pdoc
->CanUndo());
351 AddToPopUp("Redo", idcmdRedo
, writable
&& pdoc
->CanRedo());
353 AddToPopUp("Cut", idcmdCut
, writable
&& currentPos
!= anchor
);
354 AddToPopUp("Copy", idcmdCopy
, currentPos
!= anchor
);
355 AddToPopUp("Paste", idcmdPaste
, writable
&& WndProc(SCI_CANPASTE
, 0, 0));
356 AddToPopUp("Delete", idcmdDelete
, writable
&& currentPos
!= anchor
);
358 AddToPopUp("Select All", idcmdSelectAll
);
359 popup
.Show(pt
, wMain
);
363 void ScintillaBase::CancelModes() {
364 AutoCompleteCancel();
366 Editor::CancelModes();
369 void ScintillaBase::ButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
371 Editor::ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
375 void ScintillaBase::SetLexer(uptr_t wParam
) {
376 lexLanguage
= wParam
;
377 lexCurrent
= LexerModule::Find(lexLanguage
);
379 lexCurrent
= LexerModule::Find(SCLEX_NULL
);
382 void ScintillaBase::SetLexerLanguage(const char *languageName
) {
383 lexLanguage
= SCLEX_CONTAINER
;
384 lexCurrent
= LexerModule::Find(languageName
);
386 lexCurrent
= LexerModule::Find(SCLEX_NULL
);
388 lexLanguage
= lexCurrent
->GetLanguage();
391 void ScintillaBase::Colourise(int start
, int end
) {
392 int lengthDoc
= pdoc
->Length();
395 int len
= end
- start
;
397 PLATFORM_ASSERT(len
>= 0);
398 PLATFORM_ASSERT(start
+ len
<= lengthDoc
);
400 //WindowAccessor styler(wMain.GetID(), props);
401 DocumentAccessor
styler(pdoc
, props
, wMain
.GetID());
405 styleStart
= styler
.StyleAt(start
- 1);
406 styler
.SetCodePage(pdoc
->dbcsCodePage
);
408 if (lexCurrent
&& (len
> 0)) { // Should always succeed as null lexer should always be available
409 lexCurrent
->Lex(start
, len
, styleStart
, keyWordLists
, styler
);
411 if (styler
.GetPropertyInt("fold")) {
412 lexCurrent
->Fold(start
, len
, styleStart
, keyWordLists
, styler
);
419 void ScintillaBase::NotifyStyleToNeeded(int endStyleNeeded
) {
421 if (lexLanguage
!= SCLEX_CONTAINER
) {
422 int endStyled
= WndProc(SCI_GETENDSTYLED
, 0, 0);
423 int lineEndStyled
= WndProc(SCI_LINEFROMPOSITION
, endStyled
, 0);
424 endStyled
= WndProc(SCI_POSITIONFROMLINE
, lineEndStyled
, 0);
425 Colourise(endStyled
, endStyleNeeded
);
429 Editor::NotifyStyleToNeeded(endStyleNeeded
);
432 sptr_t
ScintillaBase::WndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
) {
436 AutoCompleteStart(wParam
, reinterpret_cast<const char *>(lParam
));
439 case SCI_AUTOCCANCEL
:
440 AutoCompleteCancel();
443 case SCI_AUTOCACTIVE
:
446 case SCI_AUTOCPOSSTART
:
449 case SCI_AUTOCCOMPLETE
:
450 AutoCompleteCompleted();
453 case SCI_AUTOCSETSEPARATOR
:
454 ac
.SetSeparator(static_cast<char>(wParam
));
457 case SCI_AUTOCGETSEPARATOR
:
458 return ac
.GetSeparator();
461 ac
.SetStopChars(reinterpret_cast<char *>(lParam
));
464 case SCI_AUTOCSELECT
:
465 ac
.Select(reinterpret_cast<char *>(lParam
));
468 case SCI_AUTOCSETCANCELATSTART
:
469 ac
.cancelAtStartPos
= wParam
!= 0;
472 case SCI_AUTOCGETCANCELATSTART
:
473 return ac
.cancelAtStartPos
;
475 case SCI_AUTOCSETFILLUPS
:
476 ac
.SetFillUpChars(reinterpret_cast<char *>(lParam
));
479 case SCI_AUTOCSETCHOOSESINGLE
:
480 ac
.chooseSingle
= wParam
!= 0;
483 case SCI_AUTOCGETCHOOSESINGLE
:
484 return ac
.chooseSingle
;
486 case SCI_AUTOCSETIGNORECASE
:
487 ac
.ignoreCase
= wParam
!= 0;
490 case SCI_AUTOCGETIGNORECASE
:
491 return ac
.ignoreCase
;
493 case SCI_USERLISTSHOW
:
495 AutoCompleteStart(0, reinterpret_cast<const char *>(lParam
));
498 case SCI_AUTOCSETAUTOHIDE
:
499 ac
.autoHide
= wParam
!= 0;
502 case SCI_AUTOCGETAUTOHIDE
:
505 case SCI_AUTOCSETDROPRESTOFWORD
:
506 ac
.dropRestOfWord
= wParam
!= 0;
509 case SCI_AUTOCGETDROPRESTOFWORD
:
510 return ac
.dropRestOfWord
;
512 case SCI_CALLTIPSHOW
: {
513 AutoCompleteCancel();
514 if (!ct
.wCallTip
.Created()) {
515 Point pt
= LocationFromPosition(wParam
);
516 pt
.y
+= vs
.lineHeight
;
517 PRectangle rc
= ct
.CallTipStart(currentPos
, pt
,
518 reinterpret_cast<char *>(lParam
),
519 vs
.styles
[STYLE_DEFAULT
].fontName
,
520 vs
.styles
[STYLE_DEFAULT
].sizeZoomed
,
522 // If the call-tip window would be out of the client
523 // space, adjust so it displays above the text.
524 PRectangle rcClient
= GetClientRectangle();
525 if (rc
.bottom
> rcClient
.bottom
) {
526 int offset
= vs
.lineHeight
+ rc
.Height();
530 // Now display the window.
531 CreateCallTipWindow(rc
);
532 ct
.wCallTip
.SetPositionRelative(rc
, wMain
);
538 case SCI_CALLTIPCANCEL
:
542 case SCI_CALLTIPACTIVE
:
543 return ct
.inCallTipMode
;
545 case SCI_CALLTIPPOSSTART
:
546 return ct
.posStartCallTip
;
548 case SCI_CALLTIPSETHLT
:
549 ct
.SetHighlight(wParam
, lParam
);
552 case SCI_CALLTIPSETBACK
:
553 ct
.colourBG
= ColourDesired(wParam
);
554 InvalidateStyleRedraw();
558 displayPopupMenu
= wParam
!= 0;
564 lexLanguage
= wParam
;
571 Colourise(wParam
, lParam
);
575 case SCI_SETPROPERTY
:
576 props
.Set(reinterpret_cast<const char *>(wParam
),
577 reinterpret_cast<const char *>(lParam
));
580 case SCI_SETKEYWORDS
:
581 if (wParam
< numWordLists
) {
582 keyWordLists
[wParam
]->Clear();
583 keyWordLists
[wParam
]->Set(reinterpret_cast<const char *>(lParam
));
587 case SCI_SETLEXERLANGUAGE
:
588 SetLexerLanguage(reinterpret_cast<const char *>(lParam
));
594 return Editor::WndProc(iMessage
, wParam
, lParam
);