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-2001 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
) {
67 bool acActiveBeforeCharAdded
= ac
.Active();
68 if (!acActiveBeforeCharAdded
|| !ac
.IsFillUpChar(*s
))
69 Editor::AddCharUTF(s
, len
);
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();
145 AutoCompleteCompleted();
148 AutoCompleteCompleted();
156 if (ct
.inCallTipMode
) {
158 (iMessage
!= SCI_CHARLEFT
) &&
159 (iMessage
!= SCI_CHARLEFTEXTEND
) &&
160 (iMessage
!= SCI_CHARRIGHT
) &&
161 (iMessage
!= SCI_CHARLEFTEXTEND
) &&
162 (iMessage
!= SCI_EDITTOGGLEOVERTYPE
) &&
163 (iMessage
!= SCI_DELETEBACK
)
167 if (iMessage
== SCI_DELETEBACK
) {
168 if (currentPos
<= ct
.posStartCallTip
) {
173 return Editor::KeyCommand(iMessage
);
176 void ScintillaBase::AutoCompleteStart(int lenEntered
, const char *list
) {
177 //Platform::DebugPrintf("AutoComplete %s\n", list);
180 if (ac
.chooseSingle
&& (listType
== 0)) {
181 if (list
&& !strchr(list
, ac
.GetSeparator())) {
183 SetEmptySelection(currentPos
- lenEntered
);
184 pdoc
->DeleteChars(currentPos
, lenEntered
);
185 SetEmptySelection(currentPos
);
186 pdoc
->InsertString(currentPos
, list
);
187 SetEmptySelection(currentPos
+ strlen(list
));
189 SetEmptySelection(currentPos
);
190 pdoc
->InsertString(currentPos
, list
+ lenEntered
);
191 SetEmptySelection(currentPos
+ strlen(list
+ lenEntered
));
196 ac
.Start(wMain
, idAutoComplete
, currentPos
, lenEntered
);
198 PRectangle rcClient
= GetClientRectangle();
199 Point pt
= LocationFromPosition(currentPos
- lenEntered
);
203 if (pt
.x
>= rcClient
.right
- widthLB
) {
204 HorizontalScrollTo(xOffset
+ pt
.x
- rcClient
.right
+ widthLB
);
206 pt
= LocationFromPosition(currentPos
);
209 rcac
.left
= pt
.x
- 5;
210 if (pt
.y
>= rcClient
.bottom
- heightLB
&& // Wont fit below.
211 pt
.y
>= (rcClient
.bottom
+ rcClient
.top
) / 2) { // and there is more room above.
212 rcac
.top
= pt
.y
- heightLB
;
214 heightLB
+= rcac
.top
;
218 rcac
.top
= pt
.y
+ vs
.lineHeight
;
220 rcac
.right
= rcac
.left
+ widthLB
;
221 rcac
.bottom
= Platform::Minimum(rcac
.top
+ heightLB
, rcClient
.bottom
);
222 ac
.lb
.SetPositionRelative(rcac
, wMain
);
223 ac
.lb
.SetFont(vs
.styles
[STYLE_DEFAULT
].font
);
224 ac
.lb
.SetAverageCharWidth(vs
.styles
[STYLE_DEFAULT
].aveCharWidth
);
228 // Fiddle the position of the list so it is right next to the target and wide enough for all its strings
229 PRectangle rcList
= ac
.lb
.GetDesiredRect();
230 int heightAlloced
= rcList
.bottom
- rcList
.top
;
231 widthLB
= Platform::Maximum(widthLB
, rcList
.right
- rcList
.left
);
232 // Make an allowance for large strings in list
233 rcList
.left
= pt
.x
- 5;
234 rcList
.right
= rcList
.left
+ widthLB
;
235 if (pt
.y
>= rcClient
.bottom
- heightLB
&& // Wont fit below.
236 pt
.y
>= (rcClient
.bottom
+ rcClient
.top
) / 2) { // and there is more room above.
237 rcList
.top
= pt
.y
- heightAlloced
;
239 rcList
.top
= pt
.y
+ vs
.lineHeight
;
241 rcList
.bottom
= rcList
.top
+ heightAlloced
;
242 ac
.lb
.SetPositionRelative(rcList
, wMain
);
244 if (lenEntered
!= 0) {
245 AutoCompleteMoveToCurrentWord();
249 void ScintillaBase::AutoCompleteCancel() {
253 void ScintillaBase::AutoCompleteMove(int delta
) {
257 void ScintillaBase::AutoCompleteMoveToCurrentWord() {
258 char wordCurrent
[1000];
260 int startWord
= ac
.posStart
- ac
.startLen
;
261 for (i
= startWord
; i
< currentPos
; i
++)
262 wordCurrent
[i
- startWord
] = pdoc
->CharAt(i
);
263 wordCurrent
[i
- startWord
] = '\0';
264 ac
.Select(wordCurrent
);
267 void ScintillaBase::AutoCompleteChanged(char ch
) {
268 if (ac
.IsFillUpChar(ch
)) {
269 AutoCompleteCompleted(ch
);
270 } else if (currentPos
<= ac
.posStart
- ac
.startLen
) {
272 } else if (ac
.cancelAtStartPos
&& currentPos
<= ac
.posStart
) {
274 } else if (ac
.IsStopChar(ch
)) {
277 AutoCompleteMoveToCurrentWord();
281 void ScintillaBase::AutoCompleteCompleted(char fillUp
/*='\0'*/) {
282 int item
= ac
.lb
.GetSelection();
285 ac
.lb
.GetValue(item
, selected
, sizeof(selected
));
290 userListSelected
= selected
;
292 scn
.nmhdr
.code
= SCN_USERLISTSELECTION
;
294 scn
.wParam
= listType
;
296 scn
.text
= userListSelected
.c_str();
301 Position firstPos
= ac
.posStart
- ac
.startLen
;
302 if (currentPos
< firstPos
)
304 if (currentPos
!= firstPos
) {
305 pdoc
->DeleteChars(firstPos
, currentPos
- firstPos
);
307 SetEmptySelection(ac
.posStart
);
309 SString piece
= selected
;
312 pdoc
->InsertString(firstPos
, piece
.c_str());
313 SetEmptySelection(firstPos
+ piece
.length());
317 void ScintillaBase::ContextMenu(Point pt
) {
318 bool writable
= !WndProc(SCI_GETREADONLY
, 0, 0);
320 AddToPopUp("Undo", idcmdUndo
, writable
&& pdoc
->CanUndo());
321 AddToPopUp("Redo", idcmdRedo
, writable
&& pdoc
->CanRedo());
323 AddToPopUp("Cut", idcmdCut
, writable
&& currentPos
!= anchor
);
324 AddToPopUp("Copy", idcmdCopy
, currentPos
!= anchor
);
325 AddToPopUp("Paste", idcmdPaste
, writable
&& WndProc(SCI_CANPASTE
, 0, 0));
326 AddToPopUp("Delete", idcmdDelete
, writable
&& currentPos
!= anchor
);
328 AddToPopUp("Select All", idcmdSelectAll
);
329 popup
.Show(pt
, wMain
);
332 void ScintillaBase::CancelModes() {
333 AutoCompleteCancel();
335 Editor::CancelModes();
338 void ScintillaBase::ButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
340 Editor::ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
344 void ScintillaBase::SetLexer(uptr_t wParam
) {
345 lexLanguage
= wParam
;
346 lexCurrent
= LexerModule::Find(lexLanguage
);
348 lexCurrent
= LexerModule::Find(SCLEX_NULL
);
351 void ScintillaBase::SetLexerLanguage(const char *languageName
) {
352 lexLanguage
= SCLEX_CONTAINER
;
353 lexCurrent
= LexerModule::Find(languageName
);
355 lexCurrent
= LexerModule::Find(SCLEX_NULL
);
357 lexLanguage
= lexCurrent
->GetLanguage();
360 void ScintillaBase::Colourise(int start
, int end
) {
361 int lengthDoc
= pdoc
->Length();
364 int len
= end
- start
;
366 PLATFORM_ASSERT(len
>= 0);
367 PLATFORM_ASSERT(start
+ len
<= lengthDoc
);
369 //WindowAccessor styler(wMain.GetID(), props);
370 DocumentAccessor
styler(pdoc
, props
, wMain
.GetID());
374 styleStart
= styler
.StyleAt(start
- 1);
375 styler
.SetCodePage(pdoc
->dbcsCodePage
);
377 if (lexCurrent
) { // Should always succeed as null lexer should always be available
378 lexCurrent
->Lex(start
, len
, styleStart
, keyWordLists
, styler
);
380 if (styler
.GetPropertyInt("fold")) {
381 lexCurrent
->Fold(start
, len
, styleStart
, keyWordLists
, styler
);
388 void ScintillaBase::NotifyStyleToNeeded(int endStyleNeeded
) {
390 if (lexLanguage
!= SCLEX_CONTAINER
) {
391 int endStyled
= WndProc(SCI_GETENDSTYLED
, 0, 0);
392 int lineEndStyled
= WndProc(SCI_LINEFROMPOSITION
, endStyled
, 0);
393 endStyled
= WndProc(SCI_POSITIONFROMLINE
, lineEndStyled
, 0);
394 Colourise(endStyled
, endStyleNeeded
);
398 Editor::NotifyStyleToNeeded(endStyleNeeded
);
401 sptr_t
ScintillaBase::WndProc(unsigned int iMessage
, uptr_t wParam
, sptr_t lParam
) {
405 AutoCompleteStart(wParam
, reinterpret_cast<const char *>(lParam
));
408 case SCI_AUTOCCANCEL
:
409 AutoCompleteCancel();
412 case SCI_AUTOCACTIVE
:
415 case SCI_AUTOCPOSSTART
:
418 case SCI_AUTOCCOMPLETE
:
419 AutoCompleteCompleted();
422 case SCI_AUTOCSETSEPARATOR
:
423 ac
.SetSeparator(static_cast<char>(wParam
));
426 case SCI_AUTOCGETSEPARATOR
:
427 return ac
.GetSeparator();
430 ac
.SetStopChars(reinterpret_cast<char *>(lParam
));
433 case SCI_AUTOCSELECT
:
434 ac
.Select(reinterpret_cast<char *>(lParam
));
437 case SCI_AUTOCSETCANCELATSTART
:
438 ac
.cancelAtStartPos
= wParam
;
441 case SCI_AUTOCGETCANCELATSTART
:
442 return ac
.cancelAtStartPos
;
444 case SCI_AUTOCSETFILLUPS
:
445 ac
.SetFillUpChars(reinterpret_cast<char *>(lParam
));
448 case SCI_AUTOCSETCHOOSESINGLE
:
449 ac
.chooseSingle
= wParam
;
452 case SCI_AUTOCGETCHOOSESINGLE
:
453 return ac
.chooseSingle
;
455 case SCI_AUTOCSETIGNORECASE
:
456 ac
.ignoreCase
= wParam
;
459 case SCI_AUTOCGETIGNORECASE
:
460 return ac
.ignoreCase
;
462 case SCI_USERLISTSHOW
:
464 AutoCompleteStart(0, reinterpret_cast<const char *>(lParam
));
467 case SCI_AUTOCSETAUTOHIDE
:
468 ac
.autoHide
= wParam
;
471 case SCI_AUTOCGETAUTOHIDE
:
474 case SCI_CALLTIPSHOW
: {
475 AutoCompleteCancel();
476 if (!ct
.wCallTip
.Created()) {
477 Point pt
= LocationFromPosition(wParam
);
478 pt
.y
+= vs
.lineHeight
;
479 PRectangle rc
= ct
.CallTipStart(currentPos
, pt
,
480 reinterpret_cast<char *>(lParam
),
481 vs
.styles
[STYLE_DEFAULT
].fontName
,
482 vs
.styles
[STYLE_DEFAULT
].sizeZoomed
);
483 // If the call-tip window would be out of the client
484 // space, adjust so it displays above the text.
485 PRectangle rcClient
= GetClientRectangle();
486 if (rc
.bottom
> rcClient
.bottom
) {
487 int offset
= vs
.lineHeight
+ rc
.Height();
491 // Now display the window.
492 CreateCallTipWindow(rc
);
493 ct
.wCallTip
.SetPositionRelative(rc
, wMain
);
499 case SCI_CALLTIPCANCEL
:
503 case SCI_CALLTIPACTIVE
:
504 return ct
.inCallTipMode
;
506 case SCI_CALLTIPPOSSTART
:
507 return ct
.posStartCallTip
;
509 case SCI_CALLTIPSETHLT
:
510 ct
.SetHighlight(wParam
, lParam
);
513 case SCI_CALLTIPSETBACK
:
514 ct
.colourBG
= Colour(wParam
);
515 InvalidateStyleRedraw();
519 displayPopupMenu
= wParam
;
525 lexLanguage
= wParam
;
532 Colourise(wParam
, lParam
);
536 case SCI_SETPROPERTY
:
537 props
.Set(reinterpret_cast<const char *>(wParam
),
538 reinterpret_cast<const char *>(lParam
));
541 case SCI_SETKEYWORDS
:
542 if (wParam
< numWordLists
) {
543 keyWordLists
[wParam
]->Clear();
544 keyWordLists
[wParam
]->Set(reinterpret_cast<const char *>(lParam
));
548 case SCI_SETLEXERLANGUAGE
:
549 SetLexerLanguage(reinterpret_cast<const char *>(lParam
));
555 return Editor::WndProc(iMessage
, wParam
, lParam
);