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 "ContractionState.h"
25 #include "CellBuffer.h"
28 #include "Indicator.h"
30 #include "LineMarker.h"
32 #include "ViewStyle.h"
33 #include "AutoComplete.h"
36 #include "ScintillaBase.h"
38 ScintillaBase::ScintillaBase() {
39 displayPopupMenu
= true;
43 lexLanguage
= SCLEX_CONTAINER
;
44 performingStyle
= false;
46 for (int wl
= 0;wl
< numWordLists
;wl
++)
47 keyWordLists
[wl
] = new WordList
;
48 keyWordLists
[numWordLists
] = 0;
52 ScintillaBase::~ScintillaBase() {
54 for (int wl
= 0;wl
< numWordLists
;wl
++)
55 delete keyWordLists
[wl
];
59 void ScintillaBase::Finalise() {
64 void ScintillaBase::RefreshColourPalette(Palette
&pal
, bool want
) {
65 Editor::RefreshColourPalette(pal
, want
);
66 ct
.RefreshColourPalette(pal
, want
);
69 void ScintillaBase::AddCharUTF(char *s
, unsigned int len
, bool treatAsDBCS
) {
70 bool isFillUp
= ac
.Active() && ac
.IsFillUpChar(*s
);
72 Editor::AddCharUTF(s
, len
, treatAsDBCS
);
75 AutoCompleteCharacterAdded(s
[0]);
76 // For fill ups add the character after the autocompletion has
77 // triggered so containers see the key so can display a calltip.
79 Editor::AddCharUTF(s
, len
, treatAsDBCS
);
84 void ScintillaBase::Command(int cmdId
) {
88 case idAutoComplete
: // Nothing to do
92 case idCallTip
: // Nothing to do
97 WndProc(SCI_UNDO
, 0, 0);
101 WndProc(SCI_REDO
, 0, 0);
105 WndProc(SCI_CUT
, 0, 0);
109 WndProc(SCI_COPY
, 0, 0);
113 WndProc(SCI_PASTE
, 0, 0);
117 WndProc(SCI_CLEAR
, 0, 0);
121 WndProc(SCI_SELECTALL
, 0, 0);
126 int ScintillaBase::KeyCommand(unsigned int iMessage
) {
127 // Most key commands cancel autocompletion mode
135 AutoCompleteMove( -1);
141 AutoCompleteMove( -5);
144 AutoCompleteMove( -5000);
147 AutoCompleteMove(5000);
151 AutoCompleteCharacterDeleted();
152 EnsureCaretVisible();
154 case SCI_DELETEBACKNOTLINE
:
156 AutoCompleteCharacterDeleted();
157 EnsureCaretVisible();
160 AutoCompleteCompleted();
163 AutoCompleteCompleted();
171 if (ct
.inCallTipMode
) {
173 (iMessage
!= SCI_CHARLEFT
) &&
174 (iMessage
!= SCI_CHARLEFTEXTEND
) &&
175 (iMessage
!= SCI_CHARRIGHT
) &&
176 (iMessage
!= SCI_CHARLEFTEXTEND
) &&
177 (iMessage
!= SCI_EDITTOGGLEOVERTYPE
) &&
178 (iMessage
!= SCI_DELETEBACK
) &&
179 (iMessage
!= SCI_DELETEBACKNOTLINE
)
183 if ((iMessage
== SCI_DELETEBACK
) || (iMessage
== SCI_DELETEBACKNOTLINE
)) {
184 if (currentPos
<= ct
.posStartCallTip
) {
189 return Editor::KeyCommand(iMessage
);
192 void ScintillaBase::AutoCompleteDoubleClick(void* p
) {
193 ScintillaBase
* sci
= reinterpret_cast<ScintillaBase
*>(p
);
194 sci
->AutoCompleteCompleted();
197 void ScintillaBase::AutoCompleteStart(int lenEntered
, const char *list
) {
198 //Platform::DebugPrintf("AutoComplete %s\n", list);
201 if (ac
.chooseSingle
&& (listType
== 0)) {
202 if (list
&& !strchr(list
, ac
.GetSeparator())) {
203 const char *typeSep
= strchr(list
, ac
.GetTypesep());
204 size_t lenInsert
= (typeSep
) ? (typeSep
-list
) : strlen(list
);
206 SetEmptySelection(currentPos
- lenEntered
);
207 pdoc
->DeleteChars(currentPos
, lenEntered
);
208 SetEmptySelection(currentPos
);
209 pdoc
->InsertString(currentPos
, list
, lenInsert
);
210 SetEmptySelection(currentPos
+ lenInsert
);
212 SetEmptySelection(currentPos
);
213 pdoc
->InsertString(currentPos
, list
+ lenEntered
, lenInsert
- lenEntered
);
214 SetEmptySelection(currentPos
+ lenInsert
- lenEntered
);
219 ac
.Start(wMain
, idAutoComplete
, currentPos
, LocationFromPosition(currentPos
),
220 lenEntered
, vs
.lineHeight
, IsUnicodeMode());
222 PRectangle rcClient
= GetClientRectangle();
223 Point pt
= LocationFromPosition(currentPos
- lenEntered
);
227 if (pt
.x
>= rcClient
.right
- widthLB
) {
228 HorizontalScrollTo(xOffset
+ pt
.x
- rcClient
.right
+ widthLB
);
230 pt
= LocationFromPosition(currentPos
);
233 rcac
.left
= pt
.x
- ac
.lb
->CaretFromEdge();
234 if (pt
.y
>= rcClient
.bottom
- heightLB
&& // Wont fit below.
235 pt
.y
>= (rcClient
.bottom
+ rcClient
.top
) / 2) { // and there is more room above.
236 rcac
.top
= pt
.y
- heightLB
;
238 heightLB
+= rcac
.top
;
242 rcac
.top
= pt
.y
+ vs
.lineHeight
;
244 rcac
.right
= rcac
.left
+ widthLB
;
245 rcac
.bottom
= Platform::Minimum(rcac
.top
+ heightLB
, rcClient
.bottom
);
246 ac
.lb
->SetPositionRelative(rcac
, wMain
);
247 ac
.lb
->SetFont(vs
.styles
[STYLE_DEFAULT
].font
);
248 unsigned int aveCharWidth
= vs
.styles
[STYLE_DEFAULT
].aveCharWidth
;
249 ac
.lb
->SetAverageCharWidth(aveCharWidth
);
250 ac
.lb
->SetDoubleClickAction(AutoCompleteDoubleClick
, this);
254 // Fiddle the position of the list so it is right next to the target and wide enough for all its strings
255 PRectangle rcList
= ac
.lb
->GetDesiredRect();
256 int heightAlloced
= rcList
.bottom
- rcList
.top
;
257 widthLB
= Platform::Maximum(widthLB
, rcList
.right
- rcList
.left
);
258 if (maxListWidth
!= 0)
259 widthLB
= Platform::Minimum(widthLB
, aveCharWidth
*maxListWidth
);
260 // Make an allowance for large strings in list
261 rcList
.left
= pt
.x
- ac
.lb
->CaretFromEdge();
262 rcList
.right
= rcList
.left
+ widthLB
;
263 if (((pt
.y
+ vs
.lineHeight
) >= (rcClient
.bottom
- heightAlloced
)) && // Wont fit below.
264 ((pt
.y
+ vs
.lineHeight
/ 2) >= (rcClient
.bottom
+ rcClient
.top
) / 2)) { // and there is more room above.
265 rcList
.top
= pt
.y
- heightAlloced
;
267 rcList
.top
= pt
.y
+ vs
.lineHeight
;
269 rcList
.bottom
= rcList
.top
+ heightAlloced
;
270 ac
.lb
->SetPositionRelative(rcList
, wMain
);
272 if (lenEntered
!= 0) {
273 AutoCompleteMoveToCurrentWord();
277 void ScintillaBase::AutoCompleteCancel() {
281 void ScintillaBase::AutoCompleteMove(int delta
) {
285 void ScintillaBase::AutoCompleteMoveToCurrentWord() {
286 char wordCurrent
[1000];
288 int startWord
= ac
.posStart
- ac
.startLen
;
289 for (i
= startWord
; i
< currentPos
&& i
- startWord
< 1000; i
++)
290 wordCurrent
[i
- startWord
] = pdoc
->CharAt(i
);
291 wordCurrent
[Platform::Minimum(i
- startWord
, 999)] = '\0';
292 ac
.Select(wordCurrent
);
295 void ScintillaBase::AutoCompleteCharacterAdded(char ch
) {
296 if (ac
.IsFillUpChar(ch
)) {
297 AutoCompleteCompleted();
298 } else if (ac
.IsStopChar(ch
)) {
301 AutoCompleteMoveToCurrentWord();
305 void ScintillaBase::AutoCompleteCharacterDeleted() {
306 if (currentPos
< ac
.posStart
- ac
.startLen
) {
308 } else if (ac
.cancelAtStartPos
&& (currentPos
<= ac
.posStart
)) {
311 AutoCompleteMoveToCurrentWord();
315 void ScintillaBase::AutoCompleteCompleted() {
316 int item
= ac
.lb
->GetSelection();
320 ac
.lb
->GetValue(item
, selected
, sizeof(selected
));
328 listSelected
= selected
;
329 SCNotification scn
= {0};
330 scn
.nmhdr
.code
= listType
> 0 ? SCN_USERLISTSELECTION
: SCN_AUTOCSELECTION
;
332 scn
.wParam
= listType
;
333 scn
.listType
= listType
;
334 Position firstPos
= ac
.posStart
- ac
.startLen
;
335 scn
.lParam
= firstPos
;
336 scn
.text
= listSelected
.c_str();
346 Position endPos
= currentPos
;
347 if (ac
.dropRestOfWord
)
348 endPos
= pdoc
->ExtendWordSelect(endPos
, 1, true);
349 if (endPos
< firstPos
)
351 pdoc
->BeginUndoAction();
352 if (endPos
!= firstPos
) {
353 pdoc
->DeleteChars(firstPos
, endPos
- firstPos
);
355 SetEmptySelection(ac
.posStart
);
357 SString piece
= selected
;
358 pdoc
->InsertString(firstPos
, piece
.c_str());
359 SetEmptySelection(firstPos
+ static_cast<int>(piece
.length()));
361 pdoc
->EndUndoAction();
364 int ScintillaBase::AutoCompleteGetCurrent() {
365 return ac
.lb
->GetSelection();
368 void ScintillaBase::CallTipShow(Point pt
, const char *defn
) {
369 AutoCompleteCancel();
370 pt
.y
+= vs
.lineHeight
;
371 PRectangle rc
= ct
.CallTipStart(currentPos
, pt
,
373 vs
.styles
[STYLE_DEFAULT
].fontName
,
374 vs
.styles
[STYLE_DEFAULT
].sizeZoomed
,
376 vs
.styles
[STYLE_DEFAULT
].characterSet
,
378 // If the call-tip window would be out of the client
379 // space, adjust so it displays above the text.
380 PRectangle rcClient
= GetClientRectangle();
381 if (rc
.bottom
> rcClient
.bottom
) {
382 int offset
= vs
.lineHeight
+ rc
.Height();
386 // Now display the window.
387 CreateCallTipWindow(rc
);
388 ct
.wCallTip
.SetPositionRelative(rc
, wMain
);
392 void ScintillaBase::CallTipClick() {
393 SCNotification scn
= {0};
394 scn
.nmhdr
.code
= SCN_CALLTIPCLICK
;
395 scn
.position
= ct
.clickPlace
;
399 void ScintillaBase::ContextMenu(Point pt
) {
400 if (displayPopupMenu
) {
401 bool writable
= !WndProc(SCI_GETREADONLY
, 0, 0);
403 AddToPopUp("Undo", idcmdUndo
, writable
&& pdoc
->CanUndo());
404 AddToPopUp("Redo", idcmdRedo
, writable
&& pdoc
->CanRedo());
406 AddToPopUp("Cut", idcmdCut
, writable
&& currentPos
!= anchor
);
407 AddToPopUp("Copy", idcmdCopy
, currentPos
!= anchor
);
408 AddToPopUp("Paste", idcmdPaste
, writable
&& WndProc(SCI_CANPASTE
, 0, 0));
409 AddToPopUp("Delete", idcmdDelete
, writable
&& currentPos
!= anchor
);
411 AddToPopUp("Select All", idcmdSelectAll
);
412 popup
.Show(pt
, wMain
);
416 void ScintillaBase::CancelModes() {
417 AutoCompleteCancel();
419 Editor::CancelModes();
422 void ScintillaBase::ButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
424 Editor::ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
428 void ScintillaBase::SetLexer(uptr_t wParam
) {
429 lexLanguage
= wParam
;
430 lexCurrent
= LexerModule::Find(lexLanguage
);
432 lexCurrent
= LexerModule::Find(SCLEX_NULL
);
435 void ScintillaBase::SetLexerLanguage(const char *languageName
) {
436 lexLanguage
= SCLEX_CONTAINER
;
437 lexCurrent
= LexerModule::Find(languageName
);
439 lexCurrent
= LexerModule::Find(SCLEX_NULL
);
441 lexLanguage
= lexCurrent
->GetLanguage();
444 void ScintillaBase::Colourise(int start
, int end
) {
445 if (!performingStyle
) {
446 // Protect against reentrance, which may occur, for example, when
447 // fold points are discovered while performing styling and the folding
448 // code looks for child lines which may trigger styling.
449 performingStyle
= true;
451 int lengthDoc
= pdoc
->Length();
454 int len
= end
- start
;
456 PLATFORM_ASSERT(len
>= 0);
457 PLATFORM_ASSERT(start
+ len
<= lengthDoc
);
459 //WindowAccessor styler(wMain.GetID(), props);
460 DocumentAccessor
styler(pdoc
, props
, wMain
.GetID());
464 styleStart
= styler
.StyleAt(start
- 1);
465 styler
.SetCodePage(pdoc
->dbcsCodePage
);
467 if (lexCurrent
&& (len
> 0)) { // Should always succeed as null lexer should always be available
468 lexCurrent
->Lex(start
, len
, styleStart
, keyWordLists
, styler
);
470 if (styler
.GetPropertyInt("fold")) {
471 lexCurrent
->Fold(start
, len
, styleStart
, keyWordLists
, styler
);
476 performingStyle
= false;
481 void ScintillaBase::NotifyStyleToNeeded(int endStyleNeeded
) {
483 if (lexLanguage
!= SCLEX_CONTAINER
) {
484 int endStyled
= WndProc(SCI_GETENDSTYLED
, 0, 0);
485 int lineEndStyled
= WndProc(SCI_LINEFROMPOSITION
, endStyled
, 0);
486 endStyled
= WndProc(SCI_POSITIONFROMLINE
, lineEndStyled
, 0);
487 Colourise(endStyled
, endStyleNeeded
);
491 Editor::NotifyStyleToNeeded(endStyleNeeded
);
494 sptr_t
ScintillaBase::WndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
) {
498 AutoCompleteStart(wParam
, reinterpret_cast<const char *>(lParam
));
501 case SCI_AUTOCCANCEL
:
502 AutoCompleteCancel();
505 case SCI_AUTOCACTIVE
:
508 case SCI_AUTOCPOSSTART
:
511 case SCI_AUTOCCOMPLETE
:
512 AutoCompleteCompleted();
515 case SCI_AUTOCSETSEPARATOR
:
516 ac
.SetSeparator(static_cast<char>(wParam
));
519 case SCI_AUTOCGETSEPARATOR
:
520 return ac
.GetSeparator();
523 ac
.SetStopChars(reinterpret_cast<char *>(lParam
));
526 case SCI_AUTOCSELECT
:
527 ac
.Select(reinterpret_cast<char *>(lParam
));
530 case SCI_AUTOCGETCURRENT
:
531 return AutoCompleteGetCurrent();
533 case SCI_AUTOCSETCANCELATSTART
:
534 ac
.cancelAtStartPos
= wParam
!= 0;
537 case SCI_AUTOCGETCANCELATSTART
:
538 return ac
.cancelAtStartPos
;
540 case SCI_AUTOCSETFILLUPS
:
541 ac
.SetFillUpChars(reinterpret_cast<char *>(lParam
));
544 case SCI_AUTOCSETCHOOSESINGLE
:
545 ac
.chooseSingle
= wParam
!= 0;
548 case SCI_AUTOCGETCHOOSESINGLE
:
549 return ac
.chooseSingle
;
551 case SCI_AUTOCSETIGNORECASE
:
552 ac
.ignoreCase
= wParam
!= 0;
555 case SCI_AUTOCGETIGNORECASE
:
556 return ac
.ignoreCase
;
558 case SCI_USERLISTSHOW
:
560 AutoCompleteStart(0, reinterpret_cast<const char *>(lParam
));
563 case SCI_AUTOCSETAUTOHIDE
:
564 ac
.autoHide
= wParam
!= 0;
567 case SCI_AUTOCGETAUTOHIDE
:
570 case SCI_AUTOCSETDROPRESTOFWORD
:
571 ac
.dropRestOfWord
= wParam
!= 0;
574 case SCI_AUTOCGETDROPRESTOFWORD
:
575 return ac
.dropRestOfWord
;
577 case SCI_AUTOCSETMAXHEIGHT
:
578 ac
.lb
->SetVisibleRows(wParam
);
581 case SCI_AUTOCGETMAXHEIGHT
:
582 return ac
.lb
->GetVisibleRows();
584 case SCI_AUTOCSETMAXWIDTH
:
585 maxListWidth
= wParam
;
588 case SCI_AUTOCGETMAXWIDTH
:
591 case SCI_REGISTERIMAGE
:
592 ac
.lb
->RegisterImage(wParam
, reinterpret_cast<const char *>(lParam
));
595 case SCI_CLEARREGISTEREDIMAGES
:
596 ac
.lb
->ClearRegisteredImages();
599 case SCI_AUTOCSETTYPESEPARATOR
:
600 ac
.SetTypesep(static_cast<char>(wParam
));
603 case SCI_AUTOCGETTYPESEPARATOR
:
604 return ac
.GetTypesep();
606 case SCI_CALLTIPSHOW
:
607 CallTipShow(LocationFromPosition(wParam
),
608 reinterpret_cast<const char *>(lParam
));
611 case SCI_CALLTIPCANCEL
:
615 case SCI_CALLTIPACTIVE
:
616 return ct
.inCallTipMode
;
618 case SCI_CALLTIPPOSSTART
:
619 return ct
.posStartCallTip
;
621 case SCI_CALLTIPSETHLT
:
622 ct
.SetHighlight(wParam
, lParam
);
625 case SCI_CALLTIPSETBACK
:
626 ct
.colourBG
= ColourDesired(wParam
);
627 InvalidateStyleRedraw();
630 case SCI_CALLTIPSETFORE
:
631 ct
.colourUnSel
= ColourDesired(wParam
);
632 InvalidateStyleRedraw();
635 case SCI_CALLTIPSETFOREHLT
:
636 ct
.colourSel
= ColourDesired(wParam
);
637 InvalidateStyleRedraw();
641 displayPopupMenu
= wParam
!= 0;
647 lexLanguage
= wParam
;
654 if (lexLanguage
== SCLEX_CONTAINER
) {
655 pdoc
->ModifiedAt(wParam
);
656 NotifyStyleToNeeded((lParam
== -1) ? pdoc
->Length() : lParam
);
658 Colourise(wParam
, lParam
);
663 case SCI_SETPROPERTY
:
664 props
.Set(reinterpret_cast<const char *>(wParam
),
665 reinterpret_cast<const char *>(lParam
));
668 case SCI_GETPROPERTY
: {
669 SString val
= props
.Get(reinterpret_cast<const char *>(wParam
));
670 const int n
= val
.length();
672 char *ptr
= reinterpret_cast<char *>(lParam
);
673 memcpy(ptr
, val
.c_str(), n
);
674 ptr
[n
] = '\0'; // terminate
676 return n
; // Not including NUL
679 case SCI_GETPROPERTYEXPANDED
: {
680 SString val
= props
.GetExpanded(reinterpret_cast<const char *>(wParam
));
681 const int n
= val
.length();
683 char *ptr
= reinterpret_cast<char *>(lParam
);
684 memcpy(ptr
, val
.c_str(), n
);
685 ptr
[n
] = '\0'; // terminate
687 return n
; // Not including NUL
690 case SCI_GETPROPERTYINT
:
691 return props
.GetInt(reinterpret_cast<const char *>(wParam
), lParam
);
693 case SCI_SETKEYWORDS
:
694 if (wParam
< numWordLists
) {
695 keyWordLists
[wParam
]->Clear();
696 keyWordLists
[wParam
]->Set(reinterpret_cast<const char *>(lParam
));
700 case SCI_SETLEXERLANGUAGE
:
701 SetLexerLanguage(reinterpret_cast<const char *>(lParam
));
704 case SCI_GETSTYLEBITSNEEDED
:
705 return lexCurrent
? lexCurrent
->GetStyleBitsNeeded() : 5;
709 return Editor::WndProc(iMessage
, wParam
, lParam
);