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"
20 #include "DocumentAccessor.h"
23 #include "SplitVector.h"
24 #include "Partitioning.h"
25 #include "RunStyles.h"
26 #include "ContractionState.h"
27 #include "CellBuffer.h"
30 #include "Indicator.h"
32 #include "LineMarker.h"
34 #include "ViewStyle.h"
35 #include "AutoComplete.h"
36 #include "CharClassify.h"
37 #include "Decoration.h"
39 #include "PositionCache.h"
41 #include "ScintillaBase.h"
44 using namespace Scintilla
;
47 ScintillaBase::ScintillaBase() {
48 displayPopupMenu
= true;
52 lexLanguage
= SCLEX_CONTAINER
;
53 performingStyle
= false;
55 for (int wl
= 0;wl
< numWordLists
;wl
++)
56 keyWordLists
[wl
] = new WordList
;
57 keyWordLists
[numWordLists
] = 0;
61 ScintillaBase::~ScintillaBase() {
63 for (int wl
= 0;wl
< numWordLists
;wl
++)
64 delete keyWordLists
[wl
];
68 void ScintillaBase::Finalise() {
73 void ScintillaBase::RefreshColourPalette(Palette
&pal
, bool want
) {
74 Editor::RefreshColourPalette(pal
, want
);
75 ct
.RefreshColourPalette(pal
, want
);
78 void ScintillaBase::AddCharUTF(char *s
, unsigned int len
, bool treatAsDBCS
) {
79 bool isFillUp
= ac
.Active() && ac
.IsFillUpChar(*s
);
81 Editor::AddCharUTF(s
, len
, treatAsDBCS
);
84 AutoCompleteCharacterAdded(s
[0]);
85 // For fill ups add the character after the autocompletion has
86 // triggered so containers see the key so can display a calltip.
88 Editor::AddCharUTF(s
, len
, treatAsDBCS
);
93 void ScintillaBase::Command(int cmdId
) {
97 case idAutoComplete
: // Nothing to do
101 case idCallTip
: // Nothing to do
106 WndProc(SCI_UNDO
, 0, 0);
110 WndProc(SCI_REDO
, 0, 0);
114 WndProc(SCI_CUT
, 0, 0);
118 WndProc(SCI_COPY
, 0, 0);
122 WndProc(SCI_PASTE
, 0, 0);
126 WndProc(SCI_CLEAR
, 0, 0);
130 WndProc(SCI_SELECTALL
, 0, 0);
135 int ScintillaBase::KeyCommand(unsigned int iMessage
) {
136 // Most key commands cancel autocompletion mode
144 AutoCompleteMove( -1);
150 AutoCompleteMove( -5);
153 AutoCompleteMove( -5000);
156 AutoCompleteMove(5000);
160 AutoCompleteCharacterDeleted();
161 EnsureCaretVisible();
163 case SCI_DELETEBACKNOTLINE
:
165 AutoCompleteCharacterDeleted();
166 EnsureCaretVisible();
169 AutoCompleteCompleted();
172 AutoCompleteCompleted();
180 if (ct
.inCallTipMode
) {
182 (iMessage
!= SCI_CHARLEFT
) &&
183 (iMessage
!= SCI_CHARLEFTEXTEND
) &&
184 (iMessage
!= SCI_CHARRIGHT
) &&
185 (iMessage
!= SCI_CHARRIGHTEXTEND
) &&
186 (iMessage
!= SCI_EDITTOGGLEOVERTYPE
) &&
187 (iMessage
!= SCI_DELETEBACK
) &&
188 (iMessage
!= SCI_DELETEBACKNOTLINE
)
192 if ((iMessage
== SCI_DELETEBACK
) || (iMessage
== SCI_DELETEBACKNOTLINE
)) {
193 if (currentPos
<= ct
.posStartCallTip
) {
198 return Editor::KeyCommand(iMessage
);
201 void ScintillaBase::AutoCompleteDoubleClick(void* p
) {
202 ScintillaBase
* sci
= reinterpret_cast<ScintillaBase
*>(p
);
203 sci
->AutoCompleteCompleted();
206 void ScintillaBase::AutoCompleteStart(int lenEntered
, const char *list
) {
207 //Platform::DebugPrintf("AutoComplete %s\n", list);
210 if (ac
.chooseSingle
&& (listType
== 0)) {
211 if (list
&& !strchr(list
, ac
.GetSeparator())) {
212 const char *typeSep
= strchr(list
, ac
.GetTypesep());
213 size_t lenInsert
= (typeSep
) ? (typeSep
-list
) : strlen(list
);
215 SetEmptySelection(currentPos
- lenEntered
);
216 pdoc
->DeleteChars(currentPos
, lenEntered
);
217 SetEmptySelection(currentPos
);
218 pdoc
->InsertString(currentPos
, list
, lenInsert
);
219 SetEmptySelection(currentPos
+ lenInsert
);
221 SetEmptySelection(currentPos
);
222 pdoc
->InsertString(currentPos
, list
+ lenEntered
, lenInsert
- lenEntered
);
223 SetEmptySelection(currentPos
+ lenInsert
- lenEntered
);
228 ac
.Start(wMain
, idAutoComplete
, currentPos
, LocationFromPosition(currentPos
),
229 lenEntered
, vs
.lineHeight
, IsUnicodeMode());
231 PRectangle rcClient
= GetClientRectangle();
232 Point pt
= LocationFromPosition(currentPos
- lenEntered
);
233 PRectangle rcPopupBounds
= wMain
.GetMonitorRect(pt
);
234 if (rcPopupBounds
.Height() == 0)
235 rcPopupBounds
= rcClient
;
239 if (pt
.x
>= rcClient
.right
- widthLB
) {
240 HorizontalScrollTo(xOffset
+ pt
.x
- rcClient
.right
+ widthLB
);
242 pt
= LocationFromPosition(currentPos
);
245 rcac
.left
= pt
.x
- ac
.lb
->CaretFromEdge();
246 if (pt
.y
>= rcPopupBounds
.bottom
- heightLB
&& // Wont fit below.
247 pt
.y
>= (rcPopupBounds
.bottom
+ rcPopupBounds
.top
) / 2) { // and there is more room above.
248 rcac
.top
= pt
.y
- heightLB
;
249 if (rcac
.top
< rcPopupBounds
.top
) {
250 heightLB
-= (rcPopupBounds
.top
- rcac
.top
);
251 rcac
.top
= rcPopupBounds
.top
;
254 rcac
.top
= pt
.y
+ vs
.lineHeight
;
256 rcac
.right
= rcac
.left
+ widthLB
;
257 rcac
.bottom
= Platform::Minimum(rcac
.top
+ heightLB
, rcPopupBounds
.bottom
);
258 ac
.lb
->SetPositionRelative(rcac
, wMain
);
259 ac
.lb
->SetFont(vs
.styles
[STYLE_DEFAULT
].font
);
260 unsigned int aveCharWidth
= vs
.styles
[STYLE_DEFAULT
].aveCharWidth
;
261 ac
.lb
->SetAverageCharWidth(aveCharWidth
);
262 ac
.lb
->SetDoubleClickAction(AutoCompleteDoubleClick
, this);
266 // Fiddle the position of the list so it is right next to the target and wide enough for all its strings
267 PRectangle rcList
= ac
.lb
->GetDesiredRect();
268 int heightAlloced
= rcList
.bottom
- rcList
.top
;
269 widthLB
= Platform::Maximum(widthLB
, rcList
.right
- rcList
.left
);
270 if (maxListWidth
!= 0)
271 widthLB
= Platform::Minimum(widthLB
, aveCharWidth
*maxListWidth
);
272 // Make an allowance for large strings in list
273 rcList
.left
= pt
.x
- ac
.lb
->CaretFromEdge();
274 rcList
.right
= rcList
.left
+ widthLB
;
275 if (((pt
.y
+ vs
.lineHeight
) >= (rcPopupBounds
.bottom
- heightAlloced
)) && // Wont fit below.
276 ((pt
.y
+ vs
.lineHeight
/ 2) >= (rcPopupBounds
.bottom
+ rcPopupBounds
.top
) / 2)) { // and there is more room above.
277 rcList
.top
= pt
.y
- heightAlloced
;
279 rcList
.top
= pt
.y
+ vs
.lineHeight
;
281 rcList
.bottom
= rcList
.top
+ heightAlloced
;
282 ac
.lb
->SetPositionRelative(rcList
, wMain
);
284 if (lenEntered
!= 0) {
285 AutoCompleteMoveToCurrentWord();
289 void ScintillaBase::AutoCompleteCancel() {
293 void ScintillaBase::AutoCompleteMove(int delta
) {
297 void ScintillaBase::AutoCompleteMoveToCurrentWord() {
298 char wordCurrent
[1000];
300 int startWord
= ac
.posStart
- ac
.startLen
;
301 for (i
= startWord
; i
< currentPos
&& i
- startWord
< 1000; i
++)
302 wordCurrent
[i
- startWord
] = pdoc
->CharAt(i
);
303 wordCurrent
[Platform::Minimum(i
- startWord
, 999)] = '\0';
304 ac
.Select(wordCurrent
);
307 void ScintillaBase::AutoCompleteCharacterAdded(char ch
) {
308 if (ac
.IsFillUpChar(ch
)) {
309 AutoCompleteCompleted();
310 } else if (ac
.IsStopChar(ch
)) {
313 AutoCompleteMoveToCurrentWord();
317 void ScintillaBase::AutoCompleteCharacterDeleted() {
318 if (currentPos
< ac
.posStart
- ac
.startLen
) {
320 } else if (ac
.cancelAtStartPos
&& (currentPos
<= ac
.posStart
)) {
323 AutoCompleteMoveToCurrentWord();
327 void ScintillaBase::AutoCompleteCompleted() {
328 int item
= ac
.lb
->GetSelection();
332 ac
.lb
->GetValue(item
, selected
, sizeof(selected
));
340 listSelected
= selected
;
341 SCNotification scn
= {0};
342 scn
.nmhdr
.code
= listType
> 0 ? SCN_USERLISTSELECTION
: SCN_AUTOCSELECTION
;
344 scn
.wParam
= listType
;
345 scn
.listType
= listType
;
346 Position firstPos
= ac
.posStart
- ac
.startLen
;
347 scn
.lParam
= firstPos
;
348 scn
.text
= listSelected
.c_str();
358 Position endPos
= currentPos
;
359 if (ac
.dropRestOfWord
)
360 endPos
= pdoc
->ExtendWordSelect(endPos
, 1, true);
361 if (endPos
< firstPos
)
363 pdoc
->BeginUndoAction();
364 if (endPos
!= firstPos
) {
365 pdoc
->DeleteChars(firstPos
, endPos
- firstPos
);
367 SetEmptySelection(ac
.posStart
);
369 SString piece
= selected
;
370 pdoc
->InsertCString(firstPos
, piece
.c_str());
371 SetEmptySelection(firstPos
+ static_cast<int>(piece
.length()));
373 pdoc
->EndUndoAction();
376 int ScintillaBase::AutoCompleteGetCurrent() {
377 return ac
.lb
->GetSelection();
380 void ScintillaBase::CallTipShow(Point pt
, const char *defn
) {
381 AutoCompleteCancel();
382 pt
.y
+= vs
.lineHeight
;
383 // If container knows about STYLE_CALLTIP then use it in place of the
384 // STYLE_DEFAULT for the face name, size and character set. Also use it
385 // for the foreground and background colour.
386 int ctStyle
= ct
.UseStyleCallTip() ? STYLE_CALLTIP
: STYLE_DEFAULT
;
387 if (ct
.UseStyleCallTip()) {
388 ct
.SetForeBack(vs
.styles
[STYLE_CALLTIP
].fore
, vs
.styles
[STYLE_CALLTIP
].back
);
390 PRectangle rc
= ct
.CallTipStart(currentPos
, pt
,
392 vs
.styles
[ctStyle
].fontName
,
393 vs
.styles
[ctStyle
].sizeZoomed
,
395 vs
.styles
[ctStyle
].characterSet
,
397 // If the call-tip window would be out of the client
398 // space, adjust so it displays above the text.
399 PRectangle rcClient
= GetClientRectangle();
400 if (rc
.bottom
> rcClient
.bottom
) {
401 int offset
= vs
.lineHeight
+ rc
.Height();
405 // Now display the window.
406 CreateCallTipWindow(rc
);
407 ct
.wCallTip
.SetPositionRelative(rc
, wMain
);
411 void ScintillaBase::CallTipClick() {
412 SCNotification scn
= {0};
413 scn
.nmhdr
.code
= SCN_CALLTIPCLICK
;
414 scn
.position
= ct
.clickPlace
;
418 void ScintillaBase::ContextMenu(Point pt
) {
419 if (displayPopupMenu
) {
420 bool writable
= !WndProc(SCI_GETREADONLY
, 0, 0);
422 AddToPopUp("Undo", idcmdUndo
, writable
&& pdoc
->CanUndo());
423 AddToPopUp("Redo", idcmdRedo
, writable
&& pdoc
->CanRedo());
425 AddToPopUp("Cut", idcmdCut
, writable
&& currentPos
!= anchor
);
426 AddToPopUp("Copy", idcmdCopy
, currentPos
!= anchor
);
427 AddToPopUp("Paste", idcmdPaste
, writable
&& WndProc(SCI_CANPASTE
, 0, 0));
428 AddToPopUp("Delete", idcmdDelete
, writable
&& currentPos
!= anchor
);
430 AddToPopUp("Select All", idcmdSelectAll
);
431 popup
.Show(pt
, wMain
);
435 void ScintillaBase::CancelModes() {
436 AutoCompleteCancel();
438 Editor::CancelModes();
441 void ScintillaBase::ButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
443 Editor::ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
447 void ScintillaBase::SetLexer(uptr_t wParam
) {
448 lexLanguage
= wParam
;
449 lexCurrent
= LexerModule::Find(lexLanguage
);
451 lexCurrent
= LexerModule::Find(SCLEX_NULL
);
454 void ScintillaBase::SetLexerLanguage(const char *languageName
) {
455 lexLanguage
= SCLEX_CONTAINER
;
456 lexCurrent
= LexerModule::Find(languageName
);
458 lexCurrent
= LexerModule::Find(SCLEX_NULL
);
460 lexLanguage
= lexCurrent
->GetLanguage();
463 void ScintillaBase::Colourise(int start
, int end
) {
464 if (!performingStyle
) {
465 // Protect against reentrance, which may occur, for example, when
466 // fold points are discovered while performing styling and the folding
467 // code looks for child lines which may trigger styling.
468 performingStyle
= true;
470 int lengthDoc
= pdoc
->Length();
473 int len
= end
- start
;
475 PLATFORM_ASSERT(len
>= 0);
476 PLATFORM_ASSERT(start
+ len
<= lengthDoc
);
478 //WindowAccessor styler(wMain.GetID(), props);
479 DocumentAccessor
styler(pdoc
, props
, wMain
.GetID());
483 styleStart
= styler
.StyleAt(start
- 1) & pdoc
->stylingBitsMask
;
484 styler
.SetCodePage(pdoc
->dbcsCodePage
);
486 if (lexCurrent
&& (len
> 0)) { // Should always succeed as null lexer should always be available
487 lexCurrent
->Lex(start
, len
, styleStart
, keyWordLists
, styler
);
489 if (styler
.GetPropertyInt("fold")) {
490 lexCurrent
->Fold(start
, len
, styleStart
, keyWordLists
, styler
);
495 performingStyle
= false;
500 void ScintillaBase::NotifyStyleToNeeded(int endStyleNeeded
) {
502 if (lexLanguage
!= SCLEX_CONTAINER
) {
503 int endStyled
= WndProc(SCI_GETENDSTYLED
, 0, 0);
504 int lineEndStyled
= WndProc(SCI_LINEFROMPOSITION
, endStyled
, 0);
505 endStyled
= WndProc(SCI_POSITIONFROMLINE
, lineEndStyled
, 0);
506 Colourise(endStyled
, endStyleNeeded
);
510 Editor::NotifyStyleToNeeded(endStyleNeeded
);
513 sptr_t
ScintillaBase::WndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
) {
517 AutoCompleteStart(wParam
, reinterpret_cast<const char *>(lParam
));
520 case SCI_AUTOCCANCEL
:
521 AutoCompleteCancel();
524 case SCI_AUTOCACTIVE
:
527 case SCI_AUTOCPOSSTART
:
530 case SCI_AUTOCCOMPLETE
:
531 AutoCompleteCompleted();
534 case SCI_AUTOCSETSEPARATOR
:
535 ac
.SetSeparator(static_cast<char>(wParam
));
538 case SCI_AUTOCGETSEPARATOR
:
539 return ac
.GetSeparator();
542 ac
.SetStopChars(reinterpret_cast<char *>(lParam
));
545 case SCI_AUTOCSELECT
:
546 ac
.Select(reinterpret_cast<char *>(lParam
));
549 case SCI_AUTOCGETCURRENT
:
550 return AutoCompleteGetCurrent();
552 case SCI_AUTOCSETCANCELATSTART
:
553 ac
.cancelAtStartPos
= wParam
!= 0;
556 case SCI_AUTOCGETCANCELATSTART
:
557 return ac
.cancelAtStartPos
;
559 case SCI_AUTOCSETFILLUPS
:
560 ac
.SetFillUpChars(reinterpret_cast<char *>(lParam
));
563 case SCI_AUTOCSETCHOOSESINGLE
:
564 ac
.chooseSingle
= wParam
!= 0;
567 case SCI_AUTOCGETCHOOSESINGLE
:
568 return ac
.chooseSingle
;
570 case SCI_AUTOCSETIGNORECASE
:
571 ac
.ignoreCase
= wParam
!= 0;
574 case SCI_AUTOCGETIGNORECASE
:
575 return ac
.ignoreCase
;
577 case SCI_USERLISTSHOW
:
579 AutoCompleteStart(0, reinterpret_cast<const char *>(lParam
));
582 case SCI_AUTOCSETAUTOHIDE
:
583 ac
.autoHide
= wParam
!= 0;
586 case SCI_AUTOCGETAUTOHIDE
:
589 case SCI_AUTOCSETDROPRESTOFWORD
:
590 ac
.dropRestOfWord
= wParam
!= 0;
593 case SCI_AUTOCGETDROPRESTOFWORD
:
594 return ac
.dropRestOfWord
;
596 case SCI_AUTOCSETMAXHEIGHT
:
597 ac
.lb
->SetVisibleRows(wParam
);
600 case SCI_AUTOCGETMAXHEIGHT
:
601 return ac
.lb
->GetVisibleRows();
603 case SCI_AUTOCSETMAXWIDTH
:
604 maxListWidth
= wParam
;
607 case SCI_AUTOCGETMAXWIDTH
:
610 case SCI_REGISTERIMAGE
:
611 ac
.lb
->RegisterImage(wParam
, reinterpret_cast<const char *>(lParam
));
614 case SCI_CLEARREGISTEREDIMAGES
:
615 ac
.lb
->ClearRegisteredImages();
618 case SCI_AUTOCSETTYPESEPARATOR
:
619 ac
.SetTypesep(static_cast<char>(wParam
));
622 case SCI_AUTOCGETTYPESEPARATOR
:
623 return ac
.GetTypesep();
625 case SCI_CALLTIPSHOW
:
626 CallTipShow(LocationFromPosition(wParam
),
627 reinterpret_cast<const char *>(lParam
));
630 case SCI_CALLTIPCANCEL
:
634 case SCI_CALLTIPACTIVE
:
635 return ct
.inCallTipMode
;
637 case SCI_CALLTIPPOSSTART
:
638 return ct
.posStartCallTip
;
640 case SCI_CALLTIPSETHLT
:
641 ct
.SetHighlight(wParam
, lParam
);
644 case SCI_CALLTIPSETBACK
:
645 ct
.colourBG
= ColourDesired(wParam
);
646 vs
.styles
[STYLE_CALLTIP
].back
= ct
.colourBG
;
647 InvalidateStyleRedraw();
650 case SCI_CALLTIPSETFORE
:
651 ct
.colourUnSel
= ColourDesired(wParam
);
652 vs
.styles
[STYLE_CALLTIP
].fore
= ct
.colourUnSel
;
653 InvalidateStyleRedraw();
656 case SCI_CALLTIPSETFOREHLT
:
657 ct
.colourSel
= ColourDesired(wParam
);
658 InvalidateStyleRedraw();
661 case SCI_CALLTIPUSESTYLE
:
662 ct
.SetTabSize((int)wParam
);
663 InvalidateStyleRedraw();
667 displayPopupMenu
= wParam
!= 0;
673 lexLanguage
= wParam
;
680 if (lexLanguage
== SCLEX_CONTAINER
) {
681 pdoc
->ModifiedAt(wParam
);
682 NotifyStyleToNeeded((lParam
== -1) ? pdoc
->Length() : lParam
);
684 Colourise(wParam
, lParam
);
689 case SCI_SETPROPERTY
:
690 props
.Set(reinterpret_cast<const char *>(wParam
),
691 reinterpret_cast<const char *>(lParam
));
694 case SCI_GETPROPERTY
: {
695 SString val
= props
.Get(reinterpret_cast<const char *>(wParam
));
696 const int n
= val
.length();
698 char *ptr
= reinterpret_cast<char *>(lParam
);
699 memcpy(ptr
, val
.c_str(), n
);
700 ptr
[n
] = '\0'; // terminate
702 return n
; // Not including NUL
705 case SCI_GETPROPERTYEXPANDED
: {
706 SString val
= props
.GetExpanded(reinterpret_cast<const char *>(wParam
));
707 const int n
= val
.length();
709 char *ptr
= reinterpret_cast<char *>(lParam
);
710 memcpy(ptr
, val
.c_str(), n
);
711 ptr
[n
] = '\0'; // terminate
713 return n
; // Not including NUL
716 case SCI_GETPROPERTYINT
:
717 return props
.GetInt(reinterpret_cast<const char *>(wParam
), lParam
);
719 case SCI_SETKEYWORDS
:
720 if (wParam
< numWordLists
) {
721 keyWordLists
[wParam
]->Clear();
722 keyWordLists
[wParam
]->Set(reinterpret_cast<const char *>(lParam
));
726 case SCI_SETLEXERLANGUAGE
:
727 SetLexerLanguage(reinterpret_cast<const char *>(lParam
));
730 case SCI_GETSTYLEBITSNEEDED
:
731 return lexCurrent
? lexCurrent
->GetStyleBitsNeeded() : 5;
735 return Editor::WndProc(iMessage
, wParam
, lParam
);