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-2003 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"
17 #include "PropSetSimple.h"
21 #include "DocumentAccessor.h"
24 #include "SplitVector.h"
25 #include "Partitioning.h"
26 #include "RunStyles.h"
27 #include "ContractionState.h"
28 #include "CellBuffer.h"
31 #include "Indicator.h"
33 #include "LineMarker.h"
35 #include "ViewStyle.h"
36 #include "AutoComplete.h"
37 #include "CharClassify.h"
38 #include "Decoration.h"
40 #include "Selection.h"
41 #include "PositionCache.h"
43 #include "ScintillaBase.h"
46 using namespace Scintilla
;
49 ScintillaBase::ScintillaBase() {
50 displayPopupMenu
= true;
54 lexLanguage
= SCLEX_CONTAINER
;
55 performingStyle
= false;
57 for (int wl
= 0;wl
< numWordLists
;wl
++)
58 keyWordLists
[wl
] = new WordList
;
59 keyWordLists
[numWordLists
] = 0;
63 ScintillaBase::~ScintillaBase() {
65 for (int wl
= 0;wl
< numWordLists
;wl
++)
66 delete keyWordLists
[wl
];
70 void ScintillaBase::Finalise() {
75 void ScintillaBase::RefreshColourPalette(Palette
&pal
, bool want
) {
76 Editor::RefreshColourPalette(pal
, want
);
77 ct
.RefreshColourPalette(pal
, want
);
80 void ScintillaBase::AddCharUTF(char *s
, unsigned int len
, bool treatAsDBCS
) {
81 bool isFillUp
= ac
.Active() && ac
.IsFillUpChar(*s
);
83 Editor::AddCharUTF(s
, len
, treatAsDBCS
);
86 AutoCompleteCharacterAdded(s
[0]);
87 // For fill ups add the character after the autocompletion has
88 // triggered so containers see the key so can display a calltip.
90 Editor::AddCharUTF(s
, len
, treatAsDBCS
);
95 void ScintillaBase::Command(int cmdId
) {
99 case idAutoComplete
: // Nothing to do
103 case idCallTip
: // Nothing to do
108 WndProc(SCI_UNDO
, 0, 0);
112 WndProc(SCI_REDO
, 0, 0);
116 WndProc(SCI_CUT
, 0, 0);
120 WndProc(SCI_COPY
, 0, 0);
124 WndProc(SCI_PASTE
, 0, 0);
128 WndProc(SCI_CLEAR
, 0, 0);
132 WndProc(SCI_SELECTALL
, 0, 0);
137 int ScintillaBase::KeyCommand(unsigned int iMessage
) {
138 // Most key commands cancel autocompletion mode
146 AutoCompleteMove( -1);
152 AutoCompleteMove( -5);
155 AutoCompleteMove( -5000);
158 AutoCompleteMove(5000);
162 AutoCompleteCharacterDeleted();
163 EnsureCaretVisible();
165 case SCI_DELETEBACKNOTLINE
:
167 AutoCompleteCharacterDeleted();
168 EnsureCaretVisible();
171 AutoCompleteCompleted();
174 AutoCompleteCompleted();
178 AutoCompleteCancel();
182 if (ct
.inCallTipMode
) {
184 (iMessage
!= SCI_CHARLEFT
) &&
185 (iMessage
!= SCI_CHARLEFTEXTEND
) &&
186 (iMessage
!= SCI_CHARRIGHT
) &&
187 (iMessage
!= SCI_CHARRIGHTEXTEND
) &&
188 (iMessage
!= SCI_EDITTOGGLEOVERTYPE
) &&
189 (iMessage
!= SCI_DELETEBACK
) &&
190 (iMessage
!= SCI_DELETEBACKNOTLINE
)
194 if ((iMessage
== SCI_DELETEBACK
) || (iMessage
== SCI_DELETEBACKNOTLINE
)) {
195 if (sel
.MainCaret() <= ct
.posStartCallTip
) {
200 return Editor::KeyCommand(iMessage
);
203 void ScintillaBase::AutoCompleteDoubleClick(void* p
) {
204 ScintillaBase
* sci
= reinterpret_cast<ScintillaBase
*>(p
);
205 sci
->AutoCompleteCompleted();
208 void ScintillaBase::AutoCompleteStart(int lenEntered
, const char *list
) {
209 //Platform::DebugPrintf("AutoComplete %s\n", list);
212 if (ac
.chooseSingle
&& (listType
== 0)) {
213 if (list
&& !strchr(list
, ac
.GetSeparator())) {
214 const char *typeSep
= strchr(list
, ac
.GetTypesep());
215 size_t lenInsert
= (typeSep
) ? (typeSep
-list
) : strlen(list
);
217 SetEmptySelection(sel
.MainCaret() - lenEntered
);
218 pdoc
->DeleteChars(sel
.MainCaret(), lenEntered
);
219 SetEmptySelection(sel
.MainCaret());
220 pdoc
->InsertString(sel
.MainCaret(), list
, lenInsert
);
221 SetEmptySelection(sel
.MainCaret() + lenInsert
);
223 SetEmptySelection(sel
.MainCaret());
224 pdoc
->InsertString(sel
.MainCaret(), list
+ lenEntered
, lenInsert
- lenEntered
);
225 SetEmptySelection(sel
.MainCaret() + lenInsert
- lenEntered
);
230 ac
.Start(wMain
, idAutoComplete
, sel
.MainCaret(), PointMainCaret(),
231 lenEntered
, vs
.lineHeight
, IsUnicodeMode());
233 PRectangle rcClient
= GetClientRectangle();
234 Point pt
= LocationFromPosition(sel
.MainCaret() - lenEntered
);
235 PRectangle rcPopupBounds
= wMain
.GetMonitorRect(pt
);
236 if (rcPopupBounds
.Height() == 0)
237 rcPopupBounds
= rcClient
;
241 if (pt
.x
>= rcClient
.right
- widthLB
) {
242 HorizontalScrollTo(xOffset
+ pt
.x
- rcClient
.right
+ widthLB
);
244 pt
= PointMainCaret();
247 rcac
.left
= pt
.x
- ac
.lb
->CaretFromEdge();
248 if (pt
.y
>= rcPopupBounds
.bottom
- heightLB
&& // Wont fit below.
249 pt
.y
>= (rcPopupBounds
.bottom
+ rcPopupBounds
.top
) / 2) { // and there is more room above.
250 rcac
.top
= pt
.y
- heightLB
;
251 if (rcac
.top
< rcPopupBounds
.top
) {
252 heightLB
-= (rcPopupBounds
.top
- rcac
.top
);
253 rcac
.top
= rcPopupBounds
.top
;
256 rcac
.top
= pt
.y
+ vs
.lineHeight
;
258 rcac
.right
= rcac
.left
+ widthLB
;
259 rcac
.bottom
= Platform::Minimum(rcac
.top
+ heightLB
, rcPopupBounds
.bottom
);
260 ac
.lb
->SetPositionRelative(rcac
, wMain
);
261 ac
.lb
->SetFont(vs
.styles
[STYLE_DEFAULT
].font
);
262 unsigned int aveCharWidth
= vs
.styles
[STYLE_DEFAULT
].aveCharWidth
;
263 ac
.lb
->SetAverageCharWidth(aveCharWidth
);
264 ac
.lb
->SetDoubleClickAction(AutoCompleteDoubleClick
, this);
268 // Fiddle the position of the list so it is right next to the target and wide enough for all its strings
269 PRectangle rcList
= ac
.lb
->GetDesiredRect();
270 int heightAlloced
= rcList
.bottom
- rcList
.top
;
271 widthLB
= Platform::Maximum(widthLB
, rcList
.right
- rcList
.left
);
272 if (maxListWidth
!= 0)
273 widthLB
= Platform::Minimum(widthLB
, aveCharWidth
*maxListWidth
);
274 // Make an allowance for large strings in list
275 rcList
.left
= pt
.x
- ac
.lb
->CaretFromEdge();
276 rcList
.right
= rcList
.left
+ widthLB
;
277 if (((pt
.y
+ vs
.lineHeight
) >= (rcPopupBounds
.bottom
- heightAlloced
)) && // Wont fit below.
278 ((pt
.y
+ vs
.lineHeight
/ 2) >= (rcPopupBounds
.bottom
+ rcPopupBounds
.top
) / 2)) { // and there is more room above.
279 rcList
.top
= pt
.y
- heightAlloced
;
281 rcList
.top
= pt
.y
+ vs
.lineHeight
;
283 rcList
.bottom
= rcList
.top
+ heightAlloced
;
284 ac
.lb
->SetPositionRelative(rcList
, wMain
);
286 if (lenEntered
!= 0) {
287 AutoCompleteMoveToCurrentWord();
291 void ScintillaBase::AutoCompleteCancel() {
293 SCNotification scn
= {0};
294 scn
.nmhdr
.code
= SCN_AUTOCCANCELLED
;
302 void ScintillaBase::AutoCompleteMove(int delta
) {
306 void ScintillaBase::AutoCompleteMoveToCurrentWord() {
307 char wordCurrent
[1000];
309 int startWord
= ac
.posStart
- ac
.startLen
;
310 for (i
= startWord
; i
< sel
.MainCaret() && i
- startWord
< 1000; i
++)
311 wordCurrent
[i
- startWord
] = pdoc
->CharAt(i
);
312 wordCurrent
[Platform::Minimum(i
- startWord
, 999)] = '\0';
313 ac
.Select(wordCurrent
);
316 void ScintillaBase::AutoCompleteCharacterAdded(char ch
) {
317 if (ac
.IsFillUpChar(ch
)) {
318 AutoCompleteCompleted();
319 } else if (ac
.IsStopChar(ch
)) {
320 AutoCompleteCancel();
322 AutoCompleteMoveToCurrentWord();
326 void ScintillaBase::AutoCompleteCharacterDeleted() {
327 if (sel
.MainCaret() < ac
.posStart
- ac
.startLen
) {
328 AutoCompleteCancel();
329 } else if (ac
.cancelAtStartPos
&& (sel
.MainCaret() <= ac
.posStart
)) {
330 AutoCompleteCancel();
332 AutoCompleteMoveToCurrentWord();
334 SCNotification scn
= {0};
335 scn
.nmhdr
.code
= SCN_AUTOCCHARDELETED
;
341 void ScintillaBase::AutoCompleteCompleted() {
342 int item
= ac
.lb
->GetSelection();
346 ac
.lb
->GetValue(item
, selected
, sizeof(selected
));
348 AutoCompleteCancel();
354 SCNotification scn
= {0};
355 scn
.nmhdr
.code
= listType
> 0 ? SCN_USERLISTSELECTION
: SCN_AUTOCSELECTION
;
357 scn
.wParam
= listType
;
358 scn
.listType
= listType
;
359 Position firstPos
= ac
.posStart
- ac
.startLen
;
360 scn
.lParam
= firstPos
;
371 Position endPos
= sel
.MainCaret();
372 if (ac
.dropRestOfWord
)
373 endPos
= pdoc
->ExtendWordSelect(endPos
, 1, true);
374 if (endPos
< firstPos
)
377 if (endPos
!= firstPos
) {
378 pdoc
->DeleteChars(firstPos
, endPos
- firstPos
);
380 SetEmptySelection(ac
.posStart
);
382 pdoc
->InsertCString(firstPos
, selected
);
383 SetEmptySelection(firstPos
+ static_cast<int>(strlen(selected
)));
387 int ScintillaBase::AutoCompleteGetCurrent() {
390 return ac
.lb
->GetSelection();
393 int ScintillaBase::AutoCompleteGetCurrentText(char *buffer
) {
395 int item
= ac
.lb
->GetSelection();
399 ac
.lb
->GetValue(item
, selected
, sizeof(selected
));
401 strcpy(buffer
, selected
);
402 return strlen(selected
);
410 void ScintillaBase::CallTipShow(Point pt
, const char *defn
) {
412 pt
.y
+= vs
.lineHeight
;
413 // If container knows about STYLE_CALLTIP then use it in place of the
414 // STYLE_DEFAULT for the face name, size and character set. Also use it
415 // for the foreground and background colour.
416 int ctStyle
= ct
.UseStyleCallTip() ? STYLE_CALLTIP
: STYLE_DEFAULT
;
417 if (ct
.UseStyleCallTip()) {
418 ct
.SetForeBack(vs
.styles
[STYLE_CALLTIP
].fore
, vs
.styles
[STYLE_CALLTIP
].back
);
420 PRectangle rc
= ct
.CallTipStart(sel
.MainCaret(), pt
,
422 vs
.styles
[ctStyle
].fontName
,
423 vs
.styles
[ctStyle
].sizeZoomed
,
425 vs
.styles
[ctStyle
].characterSet
,
427 // If the call-tip window would be out of the client
428 // space, adjust so it displays above the text.
429 PRectangle rcClient
= GetClientRectangle();
430 if (rc
.bottom
> rcClient
.bottom
) {
431 int offset
= vs
.lineHeight
+ rc
.Height();
435 // Now display the window.
436 CreateCallTipWindow(rc
);
437 ct
.wCallTip
.SetPositionRelative(rc
, wMain
);
441 void ScintillaBase::CallTipClick() {
442 SCNotification scn
= {0};
443 scn
.nmhdr
.code
= SCN_CALLTIPCLICK
;
444 scn
.position
= ct
.clickPlace
;
448 void ScintillaBase::ContextMenu(Point pt
) {
449 if (displayPopupMenu
) {
450 bool writable
= !WndProc(SCI_GETREADONLY
, 0, 0);
452 AddToPopUp("Undo", idcmdUndo
, writable
&& pdoc
->CanUndo());
453 AddToPopUp("Redo", idcmdRedo
, writable
&& pdoc
->CanRedo());
455 AddToPopUp("Cut", idcmdCut
, writable
&& !sel
.Empty());
456 AddToPopUp("Copy", idcmdCopy
, !sel
.Empty());
457 AddToPopUp("Paste", idcmdPaste
, writable
&& WndProc(SCI_CANPASTE
, 0, 0));
458 AddToPopUp("Delete", idcmdDelete
, writable
&& !sel
.Empty());
460 AddToPopUp("Select All", idcmdSelectAll
);
461 popup
.Show(pt
, wMain
);
465 void ScintillaBase::CancelModes() {
466 AutoCompleteCancel();
468 Editor::CancelModes();
471 void ScintillaBase::ButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
473 Editor::ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
477 void ScintillaBase::SetLexer(uptr_t wParam
) {
478 lexLanguage
= wParam
;
479 lexCurrent
= LexerModule::Find(lexLanguage
);
481 lexCurrent
= LexerModule::Find(SCLEX_NULL
);
482 int bits
= lexCurrent
? lexCurrent
->GetStyleBitsNeeded() : 5;
483 vs
.EnsureStyle((1 << bits
) - 1);
486 void ScintillaBase::SetLexerLanguage(const char *languageName
) {
487 lexLanguage
= SCLEX_CONTAINER
;
488 lexCurrent
= LexerModule::Find(languageName
);
490 lexCurrent
= LexerModule::Find(SCLEX_NULL
);
492 lexLanguage
= lexCurrent
->GetLanguage();
493 int bits
= lexCurrent
? lexCurrent
->GetStyleBitsNeeded() : 5;
494 vs
.EnsureStyle((1 << bits
) - 1);
497 void ScintillaBase::Colourise(int start
, int end
) {
498 if (!performingStyle
) {
499 // Protect against reentrance, which may occur, for example, when
500 // fold points are discovered while performing styling and the folding
501 // code looks for child lines which may trigger styling.
502 performingStyle
= true;
504 int lengthDoc
= pdoc
->Length();
507 int len
= end
- start
;
509 PLATFORM_ASSERT(len
>= 0);
510 PLATFORM_ASSERT(start
+ len
<= lengthDoc
);
512 //WindowAccessor styler(wMain.GetID(), props);
513 DocumentAccessor
styler(pdoc
, props
, wMain
.GetID());
517 styleStart
= styler
.StyleAt(start
- 1) & pdoc
->stylingBitsMask
;
518 styler
.SetCodePage(pdoc
->dbcsCodePage
);
520 if (lexCurrent
&& (len
> 0)) { // Should always succeed as null lexer should always be available
521 lexCurrent
->Lex(start
, len
, styleStart
, keyWordLists
, styler
);
523 if (styler
.GetPropertyInt("fold")) {
524 lexCurrent
->Fold(start
, len
, styleStart
, keyWordLists
, styler
);
529 performingStyle
= false;
534 void ScintillaBase::NotifyStyleToNeeded(int endStyleNeeded
) {
536 if (lexLanguage
!= SCLEX_CONTAINER
) {
537 int endStyled
= WndProc(SCI_GETENDSTYLED
, 0, 0);
538 int lineEndStyled
= WndProc(SCI_LINEFROMPOSITION
, endStyled
, 0);
539 endStyled
= WndProc(SCI_POSITIONFROMLINE
, lineEndStyled
, 0);
540 Colourise(endStyled
, endStyleNeeded
);
544 Editor::NotifyStyleToNeeded(endStyleNeeded
);
547 sptr_t
ScintillaBase::WndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
) {
551 AutoCompleteStart(wParam
, reinterpret_cast<const char *>(lParam
));
554 case SCI_AUTOCCANCEL
:
558 case SCI_AUTOCACTIVE
:
561 case SCI_AUTOCPOSSTART
:
564 case SCI_AUTOCCOMPLETE
:
565 AutoCompleteCompleted();
568 case SCI_AUTOCSETSEPARATOR
:
569 ac
.SetSeparator(static_cast<char>(wParam
));
572 case SCI_AUTOCGETSEPARATOR
:
573 return ac
.GetSeparator();
576 ac
.SetStopChars(reinterpret_cast<char *>(lParam
));
579 case SCI_AUTOCSELECT
:
580 ac
.Select(reinterpret_cast<char *>(lParam
));
583 case SCI_AUTOCGETCURRENT
:
584 return AutoCompleteGetCurrent();
586 case SCI_AUTOCGETCURRENTTEXT
:
587 return AutoCompleteGetCurrentText(reinterpret_cast<char *>(lParam
));
589 case SCI_AUTOCSETCANCELATSTART
:
590 ac
.cancelAtStartPos
= wParam
!= 0;
593 case SCI_AUTOCGETCANCELATSTART
:
594 return ac
.cancelAtStartPos
;
596 case SCI_AUTOCSETFILLUPS
:
597 ac
.SetFillUpChars(reinterpret_cast<char *>(lParam
));
600 case SCI_AUTOCSETCHOOSESINGLE
:
601 ac
.chooseSingle
= wParam
!= 0;
604 case SCI_AUTOCGETCHOOSESINGLE
:
605 return ac
.chooseSingle
;
607 case SCI_AUTOCSETIGNORECASE
:
608 ac
.ignoreCase
= wParam
!= 0;
611 case SCI_AUTOCGETIGNORECASE
:
612 return ac
.ignoreCase
;
614 case SCI_USERLISTSHOW
:
616 AutoCompleteStart(0, reinterpret_cast<const char *>(lParam
));
619 case SCI_AUTOCSETAUTOHIDE
:
620 ac
.autoHide
= wParam
!= 0;
623 case SCI_AUTOCGETAUTOHIDE
:
626 case SCI_AUTOCSETDROPRESTOFWORD
:
627 ac
.dropRestOfWord
= wParam
!= 0;
630 case SCI_AUTOCGETDROPRESTOFWORD
:
631 return ac
.dropRestOfWord
;
633 case SCI_AUTOCSETMAXHEIGHT
:
634 ac
.lb
->SetVisibleRows(wParam
);
637 case SCI_AUTOCGETMAXHEIGHT
:
638 return ac
.lb
->GetVisibleRows();
640 case SCI_AUTOCSETMAXWIDTH
:
641 maxListWidth
= wParam
;
644 case SCI_AUTOCGETMAXWIDTH
:
647 case SCI_REGISTERIMAGE
:
648 ac
.lb
->RegisterImage(wParam
, reinterpret_cast<const char *>(lParam
));
651 case SCI_CLEARREGISTEREDIMAGES
:
652 ac
.lb
->ClearRegisteredImages();
655 case SCI_AUTOCSETTYPESEPARATOR
:
656 ac
.SetTypesep(static_cast<char>(wParam
));
659 case SCI_AUTOCGETTYPESEPARATOR
:
660 return ac
.GetTypesep();
662 case SCI_CALLTIPSHOW
:
663 CallTipShow(LocationFromPosition(wParam
),
664 reinterpret_cast<const char *>(lParam
));
667 case SCI_CALLTIPCANCEL
:
671 case SCI_CALLTIPACTIVE
:
672 return ct
.inCallTipMode
;
674 case SCI_CALLTIPPOSSTART
:
675 return ct
.posStartCallTip
;
677 case SCI_CALLTIPSETHLT
:
678 ct
.SetHighlight(wParam
, lParam
);
681 case SCI_CALLTIPSETBACK
:
682 ct
.colourBG
= ColourDesired(wParam
);
683 vs
.styles
[STYLE_CALLTIP
].back
= ct
.colourBG
;
684 InvalidateStyleRedraw();
687 case SCI_CALLTIPSETFORE
:
688 ct
.colourUnSel
= ColourDesired(wParam
);
689 vs
.styles
[STYLE_CALLTIP
].fore
= ct
.colourUnSel
;
690 InvalidateStyleRedraw();
693 case SCI_CALLTIPSETFOREHLT
:
694 ct
.colourSel
= ColourDesired(wParam
);
695 InvalidateStyleRedraw();
698 case SCI_CALLTIPUSESTYLE
:
699 ct
.SetTabSize((int)wParam
);
700 InvalidateStyleRedraw();
704 displayPopupMenu
= wParam
!= 0;
710 lexLanguage
= wParam
;
717 if (lexLanguage
== SCLEX_CONTAINER
) {
718 pdoc
->ModifiedAt(wParam
);
719 NotifyStyleToNeeded((lParam
== -1) ? pdoc
->Length() : lParam
);
721 Colourise(wParam
, lParam
);
726 case SCI_SETPROPERTY
:
727 props
.Set(reinterpret_cast<const char *>(wParam
),
728 reinterpret_cast<const char *>(lParam
));
731 case SCI_GETPROPERTY
:
732 return StringResult(lParam
, props
.Get(reinterpret_cast<const char *>(wParam
)));
734 case SCI_GETPROPERTYEXPANDED
: {
735 char *val
= props
.Expanded(reinterpret_cast<const char *>(wParam
));
736 const int n
= strlen(val
);
738 char *ptr
= reinterpret_cast<char *>(lParam
);
742 return n
; // Not including NUL
745 case SCI_GETPROPERTYINT
:
746 return props
.GetInt(reinterpret_cast<const char *>(wParam
), lParam
);
748 case SCI_SETKEYWORDS
:
749 if (wParam
< numWordLists
) {
750 keyWordLists
[wParam
]->Clear();
751 keyWordLists
[wParam
]->Set(reinterpret_cast<const char *>(lParam
));
755 case SCI_SETLEXERLANGUAGE
:
756 SetLexerLanguage(reinterpret_cast<const char *>(lParam
));
759 case SCI_GETLEXERLANGUAGE
:
760 return StringResult(lParam
, lexCurrent
? lexCurrent
->languageName
: "");
762 case SCI_GETSTYLEBITSNEEDED
:
763 return lexCurrent
? lexCurrent
->GetStyleBitsNeeded() : 5;
768 return Editor::WndProc(iMessage
, wParam
, lParam
);