]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/ScintillaBase.cxx
87b84f70645c540d694d6e242550f647716fa1eb
1 // Scintilla source code edit control
2 // ScintillaBase.cxx - an enhanced subclass of Editor with calltips, autocomplete and context menu
3 // Copyright 1998-2000 by Neil Hodgson <neilh@scintilla.org>
4 // The License.txt file describes the conditions under which this software may be distributed.
13 #include "Scintilla.h"
18 #include "WindowAccessor.h"
19 #include "DocumentAccessor.h"
22 #include "ContractionState.h"
24 #include "CellBuffer.h"
27 #include "Indicator.h"
28 #include "LineMarker.h"
30 #include "ViewStyle.h"
31 #include "AutoComplete.h"
34 #include "ScintillaBase.h"
36 ScintillaBase::ScintillaBase() {
38 lexLanguage
= SCLEX_CONTAINER
;
39 for (int wl
=0;wl
<numWordLists
;wl
++)
40 keyWordLists
[wl
] = new WordList
;
44 ScintillaBase::~ScintillaBase() {}
46 void ScintillaBase::Finalise() {
50 void ScintillaBase::RefreshColourPalette(Palette
&pal
, bool want
) {
51 Editor::RefreshColourPalette(pal
, want
);
52 ct
.RefreshColourPalette(pal
, want
);
55 void ScintillaBase::AddCharUTF(char *s
, unsigned int len
) {
56 bool acActiveBeforeCharAdded
= ac
.Active();
57 Editor::AddCharUTF(s
, len
);
58 if (acActiveBeforeCharAdded
)
59 AutoCompleteChanged(s
[0]);
62 void ScintillaBase::Command(int cmdId
) {
66 case idAutoComplete
: // Nothing to do
69 case idCallTip
: // Nothing to do
73 WndProc(WM_UNDO
, 0, 0);
77 WndProc(SCI_REDO
, 0, 0);
81 WndProc(WM_CUT
, 0, 0);
85 WndProc(WM_COPY
, 0, 0);
89 WndProc(WM_PASTE
, 0, 0);
93 WndProc(WM_CLEAR
, 0, 0);
97 WndProc(SCI_SELECTALL
, 0, 0);
102 int ScintillaBase::KeyCommand(UINT iMessage
) {
103 // Most key commands cancel autocompletion mode
111 AutoCompleteMove( -1);
117 AutoCompleteMove( -5);
120 AutoCompleteMove( -5000);
123 AutoCompleteMove(5000);
127 AutoCompleteChanged();
128 EnsureCaretVisible();
131 AutoCompleteCompleted();
139 if (ct
.inCallTipMode
) {
141 (iMessage
!= SCI_CHARLEFT
) &&
142 (iMessage
!= SCI_CHARLEFTEXTEND
) &&
143 (iMessage
!= SCI_CHARRIGHT
) &&
144 (iMessage
!= SCI_CHARLEFTEXTEND
) &&
145 (iMessage
!= SCI_EDITTOGGLEOVERTYPE
) &&
146 (iMessage
!= SCI_DELETEBACK
)
150 if (iMessage
== SCI_DELETEBACK
) {
151 if (currentPos
<= ct
.posStartCallTip
) {
156 return Editor::KeyCommand(iMessage
);
159 void ScintillaBase::AutoCompleteStart(int lenEntered
, const char *list
) {
160 //Platform::DebugPrintf("AutoCOmplete %s\n", list);
163 ac
.Start(wDraw
, idAutoComplete
, currentPos
, lenEntered
);
165 PRectangle rcClient
= GetClientRectangle();
166 Point pt
= LocationFromPosition(currentPos
-lenEntered
);
168 //Platform::DebugPrintf("Auto complete %x\n", lbAutoComplete);
171 if (pt
.x
>= rcClient
.right
- widthLB
) {
172 HorizontalScrollTo(xOffset
+ pt
.x
- rcClient
.right
+ widthLB
);
174 pt
= LocationFromPosition(currentPos
);
177 rcac
.left
= pt
.x
- 5;
178 if (pt
.y
>= rcClient
.bottom
- heightLB
&& // Wont fit below.
179 pt
.y
>= (rcClient
.bottom
+ rcClient
.top
) / 2) { // and there is more room above.
180 rcac
.top
= pt
.y
- heightLB
;
182 heightLB
+= rcac
.top
;
186 rcac
.top
= pt
.y
+ vs
.lineHeight
;
188 rcac
.right
= rcac
.left
+ widthLB
;
189 rcac
.bottom
= Platform::Minimum(rcac
.top
+ heightLB
, rcClient
.bottom
);
190 ac
.lb
.SetPositionRelative(rcac
, wMain
);
191 ac
.lb
.SetFont(vs
.styles
[0].font
);
193 int maxStrLen
= ac
.SetList(list
);
195 // Fiddle the position of the list so it is right next to the target and wide enough for all its strings
196 PRectangle rcList
= ac
.lb
.GetPosition();
197 int heightAlloced
= rcList
.bottom
- rcList
.top
;
198 // Make an allowance for large strings in list
199 rcList
.left
= pt
.x
- 5;
200 rcList
.right
= rcList
.left
+ Platform::Maximum(widthLB
, maxStrLen
* 8 + 16);
201 if (pt
.y
>= rcClient
.bottom
- heightLB
&& // Wont fit below.
202 pt
.y
>= (rcClient
.bottom
+ rcClient
.top
) / 2) { // and there is more room above.
203 rcList
.top
= pt
.y
- heightAlloced
;
205 rcList
.top
= pt
.y
+ vs
.lineHeight
;
207 rcList
.bottom
= rcList
.top
+ heightAlloced
;
208 ac
.lb
.SetPositionRelative(rcList
, wMain
);
209 //lbAutoComplete.SetPosition(rcList);
213 void ScintillaBase::AutoCompleteCancel() {
217 void ScintillaBase::AutoCompleteMove(int delta
) {
221 void ScintillaBase::AutoCompleteChanged(char ch
) {
222 if (currentPos
<= ac
.posStart
) {
224 } else if (ac
.IsStopChar(ch
)) {
227 char wordCurrent
[1000];
229 int startWord
= ac
.posStart
- ac
.startLen
;
230 for (i
= startWord
; i
< currentPos
; i
++)
231 wordCurrent
[i
- startWord
] = pdoc
->CharAt(i
);
232 wordCurrent
[i
- startWord
] = '\0';
233 ac
.Select(wordCurrent
);
237 void ScintillaBase::AutoCompleteCompleted() {
238 int item
= ac
.lb
.GetSelection();
241 ac
.lb
.GetValue(item
, selected
, sizeof(selected
));
244 if (currentPos
!= ac
.posStart
) {
245 pdoc
->DeleteChars(ac
.posStart
, currentPos
- ac
.posStart
);
247 SetEmptySelection(ac
.posStart
);
249 pdoc
->InsertString(currentPos
, selected
+ ac
.startLen
);
250 SetEmptySelection(currentPos
+ strlen(selected
+ ac
.startLen
));
254 void ScintillaBase::ContextMenu(Point pt
) {
256 AddToPopUp("Undo", idcmdUndo
, pdoc
->CanUndo());
257 AddToPopUp("Redo", idcmdRedo
, pdoc
->CanRedo());
259 AddToPopUp("Cut", idcmdCut
, currentPos
!= anchor
);
260 AddToPopUp("Copy", idcmdCopy
, currentPos
!= anchor
);
261 AddToPopUp("Paste", idcmdPaste
, WndProc(EM_CANPASTE
, 0, 0));
262 AddToPopUp("Delete", idcmdDelete
, currentPos
!= anchor
);
264 AddToPopUp("Select All", idcmdSelectAll
);
265 popup
.Show(pt
, wMain
);
268 void ScintillaBase::ButtonDown(Point pt
, unsigned int curTime
, bool shift
, bool ctrl
, bool alt
) {
269 AutoCompleteCancel();
271 Editor::ButtonDown(pt
, curTime
, shift
, ctrl
, alt
);
275 void ScintillaBase::Colourise(int start
, int end
) {
276 int lengthDoc
= Platform::SendScintilla(wMain
.GetID(), SCI_GETLENGTH
, 0, 0);
279 int len
= end
- start
;
281 //WindowAccessor styler(wMain.GetID(), props);
282 DocumentAccessor
styler(pdoc
, props
);
286 styleStart
= styler
.StyleAt(start
- 1);
287 styler
.SetCodePage(pdoc
->dbcsCodePage
);
289 LexerModule::Colourise(start
, len
, styleStart
, lexLanguage
, keyWordLists
, styler
);
294 void ScintillaBase::NotifyStyleToNeeded(int endStyleNeeded
) {
296 if (lexLanguage
!= SCLEX_CONTAINER
) {
297 int endStyled
= Platform::SendScintilla(wMain
.GetID(), SCI_GETENDSTYLED
, 0, 0);
298 int lineEndStyled
= Platform::SendScintilla(wMain
.GetID(), EM_LINEFROMCHAR
, endStyled
, 0);
299 endStyled
= Platform::SendScintilla(wMain
.GetID(), EM_LINEINDEX
, lineEndStyled
, 0);
300 Colourise(endStyled
, endStyleNeeded
);
304 Editor::NotifyStyleToNeeded(endStyleNeeded
);
307 LRESULT
ScintillaBase::WndProc(UINT iMessage
, WPARAM wParam
, LPARAM lParam
) {
310 AutoCompleteStart(wParam
, reinterpret_cast<const char *>(lParam
));
313 case SCI_AUTOCCANCEL
:
314 AutoCompleteCancel();
317 case SCI_AUTOCACTIVE
:
320 case SCI_AUTOCPOSSTART
:
323 case SCI_AUTOCCOMPLETE
:
324 AutoCompleteCompleted();
327 case SCI_AUTOCSETSEPARATOR
:
328 ac
.SetSeparator(wParam
);
331 case SCI_AUTOCGETSEPARATOR
:
332 return ac
.GetSeparator();
335 ac
.SetStopChars(reinterpret_cast<char *>(lParam
));
338 case SCI_AUTOCSELECT
:
339 ac
.Select(reinterpret_cast<char *>(lParam
));
342 case SCI_CALLTIPSHOW
: {
343 AutoCompleteCancel();
344 if (!ct
.wCallTip
.Created()) {
345 PRectangle rc
= ct
.CallTipStart(currentPos
, LocationFromPosition(wParam
),
346 reinterpret_cast<char *>(lParam
),
347 vs
.styles
[0].fontName
, vs
.styles
[0].size
);
348 // If the call-tip window would be out of the client
349 // space, adjust so it displays above the text.
350 PRectangle rcClient
= GetClientRectangle();
351 if (rc
.bottom
> rcClient
.bottom
) {
352 int offset
= vs
.lineHeight
+ rc
.Height();
356 // Now display the window.
357 CreateCallTipWindow(rc
);
358 ct
.wCallTip
.SetPositionRelative(rc
, wDraw
);
364 case SCI_CALLTIPCANCEL
:
368 case SCI_CALLTIPACTIVE
:
369 return ct
.inCallTipMode
;
371 case SCI_CALLTIPPOSSTART
:
372 return ct
.posStartCallTip
;
374 case SCI_CALLTIPSETHLT
:
375 ct
.SetHighlight(wParam
, lParam
);
378 case SCI_CALLTIPSETBACK
:
379 ct
.colourBG
= Colour(wParam
);
380 InvalidateStyleRedraw();
385 lexLanguage
= wParam
;
392 Colourise(wParam
, lParam
);
396 case SCI_SETPROPERTY
:
397 props
.Set(reinterpret_cast<const char *>(wParam
),
398 reinterpret_cast<const char *>(lParam
));
401 case SCI_SETKEYWORDS
:
402 if (wParam
< numWordLists
) {
403 keyWordLists
[wParam
]->Clear();
404 keyWordLists
[wParam
]->Set(reinterpret_cast<const char *>(lParam
));
410 return Editor::WndProc(iMessage
, wParam
, lParam
);