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 acActiveBeforeCharAdded
= ac
.Active();
68 if (!acActiveBeforeCharAdded
|| !ac
.IsFillUpChar(*s
))
69 Editor::AddCharUTF(s
, len
, treatAsDBCS
);
70 if (acActiveBeforeCharAdded
)
71 AutoCompleteChanged(s
[0]);
74 void ScintillaBase::Command(int cmdId
) {
78 case idAutoComplete
: // Nothing to do
82 case idCallTip
: // Nothing to do
87 WndProc(SCI_UNDO
, 0, 0);
91 WndProc(SCI_REDO
, 0, 0);
95 WndProc(SCI_CUT
, 0, 0);
99 WndProc(SCI_COPY
, 0, 0);
103 WndProc(SCI_PASTE
, 0, 0);
107 WndProc(SCI_CLEAR
, 0, 0);
111 WndProc(SCI_SELECTALL
, 0, 0);
116 int ScintillaBase::KeyCommand(unsigned int iMessage
) {
117 // Most key commands cancel autocompletion mode
125 AutoCompleteMove( -1);
131 AutoCompleteMove( -5);
134 AutoCompleteMove( -5000);
137 AutoCompleteMove(5000);
141 AutoCompleteChanged();
142 EnsureCaretVisible();
144 case SCI_DELETEBACKNOTLINE
:
146 AutoCompleteChanged();
147 EnsureCaretVisible();
150 AutoCompleteCompleted();
153 AutoCompleteCompleted();
161 if (ct
.inCallTipMode
) {
163 (iMessage
!= SCI_CHARLEFT
) &&
164 (iMessage
!= SCI_CHARLEFTEXTEND
) &&
165 (iMessage
!= SCI_CHARRIGHT
) &&
166 (iMessage
!= SCI_CHARLEFTEXTEND
) &&
167 (iMessage
!= SCI_EDITTOGGLEOVERTYPE
) &&
168 (iMessage
!= SCI_DELETEBACK
) &&
169 (iMessage
!= SCI_DELETEBACKNOTLINE
)
173 if ((iMessage
== SCI_DELETEBACK
) || (iMessage
== SCI_DELETEBACKNOTLINE
)) {
174 if (currentPos
<= ct
.posStartCallTip
) {
179 return Editor::KeyCommand(iMessage
);
182 void ScintillaBase::AutoCompleteDoubleClick(void* p
) {
183 ScintillaBase
* sci
= reinterpret_cast<ScintillaBase
*>(p
);
184 sci
->AutoCompleteCompleted();
187 void ScintillaBase::AutoCompleteStart(int lenEntered
, const char *list
) {
188 //Platform::DebugPrintf("AutoComplete %s\n", list);
191 if (ac
.chooseSingle
&& (listType
== 0)) {
192 if (list
&& !strchr(list
, ac
.GetSeparator())) {
194 SetEmptySelection(currentPos
- lenEntered
);
195 pdoc
->DeleteChars(currentPos
, lenEntered
);
196 SetEmptySelection(currentPos
);
197 pdoc
->InsertString(currentPos
, list
);
198 SetEmptySelection(currentPos
+ strlen(list
));
200 SetEmptySelection(currentPos
);
201 pdoc
->InsertString(currentPos
, list
+ lenEntered
);
202 SetEmptySelection(currentPos
+ strlen(list
+ lenEntered
));
207 ac
.Start(wMain
, idAutoComplete
, currentPos
, lenEntered
);
209 PRectangle rcClient
= GetClientRectangle();
210 Point pt
= LocationFromPosition(currentPos
- lenEntered
);
214 if (pt
.x
>= rcClient
.right
- widthLB
) {
215 HorizontalScrollTo(xOffset
+ pt
.x
- rcClient
.right
+ widthLB
);
217 pt
= LocationFromPosition(currentPos
);
220 rcac
.left
= pt
.x
- 5;
221 if (pt
.y
>= rcClient
.bottom
- heightLB
&& // Wont fit below.
222 pt
.y
>= (rcClient
.bottom
+ rcClient
.top
) / 2) { // and there is more room above.
223 rcac
.top
= pt
.y
- heightLB
;
225 heightLB
+= rcac
.top
;
229 rcac
.top
= pt
.y
+ vs
.lineHeight
;
231 rcac
.right
= rcac
.left
+ widthLB
;
232 rcac
.bottom
= Platform::Minimum(rcac
.top
+ heightLB
, rcClient
.bottom
);
233 ac
.lb
.SetPositionRelative(rcac
, wMain
);
234 ac
.lb
.SetFont(vs
.styles
[STYLE_DEFAULT
].font
);
235 ac
.lb
.SetAverageCharWidth(vs
.styles
[STYLE_DEFAULT
].aveCharWidth
);
236 ac
.lb
.SetDoubleClickAction(AutoCompleteDoubleClick
, this);
240 // Fiddle the position of the list so it is right next to the target and wide enough for all its strings
241 PRectangle rcList
= ac
.lb
.GetDesiredRect();
242 int heightAlloced
= rcList
.bottom
- rcList
.top
;
243 widthLB
= Platform::Maximum(widthLB
, rcList
.right
- rcList
.left
);
244 // Make an allowance for large strings in list
245 rcList
.left
= pt
.x
- 5;
246 rcList
.right
= rcList
.left
+ widthLB
;
247 if (pt
.y
>= rcClient
.bottom
- heightLB
&& // Wont fit below.
248 pt
.y
>= (rcClient
.bottom
+ rcClient
.top
) / 2) { // and there is more room above.
249 rcList
.top
= pt
.y
- heightAlloced
;
251 rcList
.top
= pt
.y
+ vs
.lineHeight
;
253 rcList
.bottom
= rcList
.top
+ heightAlloced
;
254 ac
.lb
.SetPositionRelative(rcList
, wMain
);
256 if (lenEntered
!= 0) {
257 AutoCompleteMoveToCurrentWord();
261 void ScintillaBase::AutoCompleteCancel() {
265 void ScintillaBase::AutoCompleteMove(int delta
) {
269 void ScintillaBase::AutoCompleteMoveToCurrentWord() {
270 char wordCurrent
[1000];
272 int startWord
= ac
.posStart
- ac
.startLen
;
273 for (i
= startWord
; i
< currentPos
; i
++)
274 wordCurrent
[i
- startWord
] = pdoc
->CharAt(i
);
275 wordCurrent
[i
- startWord
] = '\0';
276 ac
.Select(wordCurrent
);
279 void ScintillaBase::AutoCompleteChanged(char ch
) {
280 if (ac
.IsFillUpChar(ch
)) {
281 AutoCompleteCompleted(ch
);
282 } else if (currentPos
<= ac
.posStart
- ac
.startLen
) {
284 } else if (ac
.cancelAtStartPos
&& currentPos
<= ac
.posStart
) {
286 } else if (ac
.IsStopChar(ch
)) {
289 AutoCompleteMoveToCurrentWord();
293 void ScintillaBase::AutoCompleteCompleted(char fillUp
/*='\0'*/) {
294 int item
= ac
.lb
.GetSelection();
297 ac
.lb
.GetValue(item
, selected
, sizeof(selected
));
302 userListSelected
= selected
;
304 scn
.nmhdr
.code
= SCN_USERLISTSELECTION
;
306 scn
.wParam
= listType
;
308 scn
.text
= userListSelected
.c_str();
313 Position firstPos
= ac
.posStart
- ac
.startLen
;
314 Position endPos
= currentPos
;
315 if (ac
.dropRestOfWord
)
316 endPos
= pdoc
->ExtendWordSelect(endPos
, 1, true);
317 if (endPos
< firstPos
)
319 pdoc
->BeginUndoAction();
320 if (endPos
!= firstPos
) {
321 pdoc
->DeleteChars(firstPos
, endPos
- firstPos
);
323 SetEmptySelection(ac
.posStart
);
325 SString piece
= selected
;
328 pdoc
->InsertString(firstPos
, piece
.c_str());
329 SetEmptySelection(firstPos
+ piece
.length());
331 pdoc
->EndUndoAction();
334 void ScintillaBase::ContextMenu(Point pt
) {
335 bool writable
= !WndProc(SCI_GETREADONLY
, 0, 0);
337 AddToPopUp("Undo", idcmdUndo
, writable
&& pdoc
->CanUndo());
338 AddToPopUp("Redo", idcmdRedo
, writable
&& pdoc
->CanRedo());
340 AddToPopUp("Cut", idcmdCut
, writable
&& currentPos
!= anchor
);
341 AddToPopUp("Copy", idcmdCopy
, currentPos
!= anchor
);
342 AddToPopUp("Paste", idcmdPaste
, writable
&& WndProc(SCI_CANPASTE
, 0, 0));
343 AddToPopUp("Delete", idcmdDelete
, writable
&& currentPos
!= anchor
);
345 AddToPopUp("Select All", idcmdSelectAll
);
346 popup
.Show(pt
, wMain
);
349 void ScintillaBase::CancelModes() {
350 AutoCompleteCancel();
352 Editor::CancelModes();
355 void ScintillaBase::ButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
357 Editor::ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
361 void ScintillaBase::SetLexer(uptr_t wParam
) {
362 lexLanguage
= wParam
;
363 lexCurrent
= LexerModule::Find(lexLanguage
);
365 lexCurrent
= LexerModule::Find(SCLEX_NULL
);
368 void ScintillaBase::SetLexerLanguage(const char *languageName
) {
369 lexLanguage
= SCLEX_CONTAINER
;
370 lexCurrent
= LexerModule::Find(languageName
);
372 lexCurrent
= LexerModule::Find(SCLEX_NULL
);
374 lexLanguage
= lexCurrent
->GetLanguage();
377 void ScintillaBase::Colourise(int start
, int end
) {
378 int lengthDoc
= pdoc
->Length();
381 int len
= end
- start
;
383 PLATFORM_ASSERT(len
>= 0);
384 PLATFORM_ASSERT(start
+ len
<= lengthDoc
);
386 //WindowAccessor styler(wMain.GetID(), props);
387 DocumentAccessor
styler(pdoc
, props
, wMain
.GetID());
391 styleStart
= styler
.StyleAt(start
- 1);
392 styler
.SetCodePage(pdoc
->dbcsCodePage
);
394 if (lexCurrent
&& (len
> 0)) { // Should always succeed as null lexer should always be available
395 lexCurrent
->Lex(start
, len
, styleStart
, keyWordLists
, styler
);
397 if (styler
.GetPropertyInt("fold")) {
398 lexCurrent
->Fold(start
, len
, styleStart
, keyWordLists
, styler
);
405 void ScintillaBase::NotifyStyleToNeeded(int endStyleNeeded
) {
407 if (lexLanguage
!= SCLEX_CONTAINER
) {
408 int endStyled
= WndProc(SCI_GETENDSTYLED
, 0, 0);
409 int lineEndStyled
= WndProc(SCI_LINEFROMPOSITION
, endStyled
, 0);
410 endStyled
= WndProc(SCI_POSITIONFROMLINE
, lineEndStyled
, 0);
411 Colourise(endStyled
, endStyleNeeded
);
415 Editor::NotifyStyleToNeeded(endStyleNeeded
);
418 sptr_t
ScintillaBase::WndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
) {
422 AutoCompleteStart(wParam
, reinterpret_cast<const char *>(lParam
));
425 case SCI_AUTOCCANCEL
:
426 AutoCompleteCancel();
429 case SCI_AUTOCACTIVE
:
432 case SCI_AUTOCPOSSTART
:
435 case SCI_AUTOCCOMPLETE
:
436 AutoCompleteCompleted();
439 case SCI_AUTOCSETSEPARATOR
:
440 ac
.SetSeparator(static_cast<char>(wParam
));
443 case SCI_AUTOCGETSEPARATOR
:
444 return ac
.GetSeparator();
447 ac
.SetStopChars(reinterpret_cast<char *>(lParam
));
450 case SCI_AUTOCSELECT
:
451 ac
.Select(reinterpret_cast<char *>(lParam
));
454 case SCI_AUTOCSETCANCELATSTART
:
455 ac
.cancelAtStartPos
= wParam
!= 0;
458 case SCI_AUTOCGETCANCELATSTART
:
459 return ac
.cancelAtStartPos
;
461 case SCI_AUTOCSETFILLUPS
:
462 ac
.SetFillUpChars(reinterpret_cast<char *>(lParam
));
465 case SCI_AUTOCSETCHOOSESINGLE
:
466 ac
.chooseSingle
= wParam
!= 0;
469 case SCI_AUTOCGETCHOOSESINGLE
:
470 return ac
.chooseSingle
;
472 case SCI_AUTOCSETIGNORECASE
:
473 ac
.ignoreCase
= wParam
!= 0;
476 case SCI_AUTOCGETIGNORECASE
:
477 return ac
.ignoreCase
;
479 case SCI_USERLISTSHOW
:
481 AutoCompleteStart(0, reinterpret_cast<const char *>(lParam
));
484 case SCI_AUTOCSETAUTOHIDE
:
485 ac
.autoHide
= wParam
!= 0;
488 case SCI_AUTOCGETAUTOHIDE
:
491 case SCI_AUTOCSETDROPRESTOFWORD
:
492 ac
.dropRestOfWord
= wParam
!= 0;
495 case SCI_AUTOCGETDROPRESTOFWORD
:
496 return ac
.dropRestOfWord
;
498 case SCI_CALLTIPSHOW
: {
499 AutoCompleteCancel();
500 if (!ct
.wCallTip
.Created()) {
501 Point pt
= LocationFromPosition(wParam
);
502 pt
.y
+= vs
.lineHeight
;
503 PRectangle rc
= ct
.CallTipStart(currentPos
, pt
,
504 reinterpret_cast<char *>(lParam
),
505 vs
.styles
[STYLE_DEFAULT
].fontName
,
506 vs
.styles
[STYLE_DEFAULT
].sizeZoomed
,
508 // If the call-tip window would be out of the client
509 // space, adjust so it displays above the text.
510 PRectangle rcClient
= GetClientRectangle();
511 if (rc
.bottom
> rcClient
.bottom
) {
512 int offset
= vs
.lineHeight
+ rc
.Height();
516 // Now display the window.
517 CreateCallTipWindow(rc
);
518 ct
.wCallTip
.SetPositionRelative(rc
, wMain
);
524 case SCI_CALLTIPCANCEL
:
528 case SCI_CALLTIPACTIVE
:
529 return ct
.inCallTipMode
;
531 case SCI_CALLTIPPOSSTART
:
532 return ct
.posStartCallTip
;
534 case SCI_CALLTIPSETHLT
:
535 ct
.SetHighlight(wParam
, lParam
);
538 case SCI_CALLTIPSETBACK
:
539 ct
.colourBG
= ColourDesired(wParam
);
540 InvalidateStyleRedraw();
544 displayPopupMenu
= wParam
!= 0;
550 lexLanguage
= wParam
;
557 Colourise(wParam
, lParam
);
561 case SCI_SETPROPERTY
:
562 props
.Set(reinterpret_cast<const char *>(wParam
),
563 reinterpret_cast<const char *>(lParam
));
566 case SCI_SETKEYWORDS
:
567 if (wParam
< numWordLists
) {
568 keyWordLists
[wParam
]->Clear();
569 keyWordLists
[wParam
]->Set(reinterpret_cast<const char *>(lParam
));
573 case SCI_SETLEXERLANGUAGE
:
574 SetLexerLanguage(reinterpret_cast<const char *>(lParam
));
580 return Editor::WndProc(iMessage
, wParam
, lParam
);