1 ////////////////////////////////////////////////////////////////////////////
3 // Purpose: A wxWindows implementation of Scintilla. This class is the
4 // one meant to be used directly by wx applications. It does not
5 // derive directly from the Scintilla classes, but instead
6 // delegates most things to the real Scintilla class.
7 // This allows the use of Scintilla without polluting the
8 // namespace with all the classes and identifiers from Scintilla.
12 // Created: 13-Jan-2000
14 // Copyright: (c) 2000 by Total Control Software
15 // Licence: wxWindows license
16 /////////////////////////////////////////////////////////////////////////////
20 #include "wx/stc/stc.h"
21 #include "ScintillaWX.h"
23 #include <wx/tokenzr.h>
25 // The following code forces a reference to all of the Scintilla lexers.
26 // If we don't do something like this, then the linker tends to "optimize"
27 // them away. (eric@sourcegear.com)
29 int wxForceScintillaLexers(void)
31 extern LexerModule lmAda
;
32 extern LexerModule lmAVE
;
33 extern LexerModule lmConf
;
34 extern LexerModule lmCPP
;
35 extern LexerModule lmNncrontab
;
36 extern LexerModule lmEiffel
;
37 extern LexerModule lmHTML
;
38 extern LexerModule lmLISP
;
39 extern LexerModule lmLua
;
40 extern LexerModule lmBatch
; // In LexOthers.cxx
41 extern LexerModule lmPascal
;
42 extern LexerModule lmPerl
;
43 extern LexerModule lmPython
;
44 extern LexerModule lmRuby
;
45 extern LexerModule lmSQL
;
46 extern LexerModule lmVB
;
73 //----------------------------------------------------------------------
75 const wxChar
* wxSTCNameStr
= "stcwindow";
77 DEFINE_EVENT_TYPE( wxEVT_STC_CHANGE
)
78 DEFINE_EVENT_TYPE( wxEVT_STC_STYLENEEDED
)
79 DEFINE_EVENT_TYPE( wxEVT_STC_CHARADDED
)
80 DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTREACHED
)
81 DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTLEFT
)
82 DEFINE_EVENT_TYPE( wxEVT_STC_ROMODIFYATTEMPT
)
83 DEFINE_EVENT_TYPE( wxEVT_STC_KEY
)
84 DEFINE_EVENT_TYPE( wxEVT_STC_DOUBLECLICK
)
85 DEFINE_EVENT_TYPE( wxEVT_STC_UPDATEUI
)
86 DEFINE_EVENT_TYPE( wxEVT_STC_MODIFIED
)
87 DEFINE_EVENT_TYPE( wxEVT_STC_MACRORECORD
)
88 DEFINE_EVENT_TYPE( wxEVT_STC_MARGINCLICK
)
89 DEFINE_EVENT_TYPE( wxEVT_STC_NEEDSHOWN
)
90 DEFINE_EVENT_TYPE( wxEVT_STC_POSCHANGED
)
91 DEFINE_EVENT_TYPE( wxEVT_STC_PAINTED
)
92 DEFINE_EVENT_TYPE( wxEVT_STC_USERLISTSELECTION
)
93 DEFINE_EVENT_TYPE( wxEVT_STC_URIDROPPED
)
94 DEFINE_EVENT_TYPE( wxEVT_STC_DWELLSTART
)
95 DEFINE_EVENT_TYPE( wxEVT_STC_DWELLEND
)
100 BEGIN_EVENT_TABLE(wxStyledTextCtrl
, wxControl
)
101 EVT_PAINT (wxStyledTextCtrl::OnPaint
)
102 EVT_SCROLLWIN (wxStyledTextCtrl::OnScrollWin
)
103 EVT_SIZE (wxStyledTextCtrl::OnSize
)
104 EVT_LEFT_DOWN (wxStyledTextCtrl::OnMouseLeftDown
)
106 // Let Scintilla see the double click as a second click
107 EVT_LEFT_DCLICK (wxStyledTextCtrl::OnMouseLeftDown
)
109 EVT_MOTION (wxStyledTextCtrl::OnMouseMove
)
110 EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp
)
111 EVT_CONTEXT_MENU (wxStyledTextCtrl::OnContextMenu
)
112 EVT_MOUSEWHEEL (wxStyledTextCtrl::OnMouseWheel
)
113 EVT_CHAR (wxStyledTextCtrl::OnChar
)
114 EVT_KEY_DOWN (wxStyledTextCtrl::OnKeyDown
)
115 EVT_KILL_FOCUS (wxStyledTextCtrl::OnLoseFocus
)
116 EVT_SET_FOCUS (wxStyledTextCtrl::OnGainFocus
)
117 EVT_SYS_COLOUR_CHANGED (wxStyledTextCtrl::OnSysColourChanged
)
118 EVT_ERASE_BACKGROUND (wxStyledTextCtrl::OnEraseBackground
)
119 EVT_MENU_RANGE (-1, -1, wxStyledTextCtrl::OnMenu
)
120 EVT_LISTBOX_DCLICK (-1, wxStyledTextCtrl::OnListBox
)
124 IMPLEMENT_CLASS(wxStyledTextCtrl
, wxControl
)
125 IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent
, wxCommandEvent
)
127 //----------------------------------------------------------------------
128 // Constructor and Destructor
130 wxStyledTextCtrl::wxStyledTextCtrl(wxWindow
*parent
,
135 const wxString
& name
) :
136 wxControl(parent
, id
, pos
, size
,
137 style
| wxVSCROLL
| wxHSCROLL
| wxWANTS_CHARS
| wxCLIP_CHILDREN
,
138 wxDefaultValidator
, name
)
140 m_swx
= new ScintillaWX(this);
142 m_lastKeyDownConsumed
= FALSE
;
146 wxStyledTextCtrl::~wxStyledTextCtrl() {
151 //----------------------------------------------------------------------
153 long wxStyledTextCtrl::SendMsg(int msg
, long wp
, long lp
) {
155 return m_swx
->WndProc(msg
, wp
, lp
);
163 #define MAKELONG(a, b) ((a) | ((b) << 16))
166 static long wxColourAsLong(const wxColour
& co
) {
167 return (((long)co
.Blue() << 16) |
168 ((long)co
.Green() << 8) |
172 static wxColour
wxColourFromLong(long c
) {
174 clr
.Set(c
& 0xff, (c
>> 8) & 0xff, (c
>> 16) & 0xff);
179 static wxColour
wxColourFromSpec(const wxString
& spec
) {
180 // spec should be #RRGGBB
182 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
183 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
184 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
185 return wxColour(red
, green
, blue
);
189 //----------------------------------------------------------------------
190 // BEGIN generated section. The following code is automatically generated
191 // by gen_iface.py from the contents of Scintilla.iface. Do not edit
192 // this file. Edit stc.cpp.in or gen_iface.py instead and regenerate.
196 // END of generated section
197 //----------------------------------------------------------------------
200 // Returns the line number of the line with the caret.
201 int wxStyledTextCtrl::GetCurrentLine() {
202 int line
= LineFromPosition(GetCurrentPos());
207 // Extract style settings from a spec-string which is composed of one or
208 // more of the following comma separated elements:
210 // bold turns on bold
211 // italic turns on italics
212 // fore:#RRGGBB sets the foreground colour
213 // back:#RRGGBB sets the background colour
214 // face:[facename] sets the font face name to use
215 // size:[num] sets the font size in points
216 // eol turns on eol filling
217 // underline turns on underlining
219 void wxStyledTextCtrl::StyleSetSpec(int styleNum
, const wxString
& spec
) {
221 wxStringTokenizer
tkz(spec
, ",");
222 while (tkz
.HasMoreTokens()) {
223 wxString token
= tkz
.GetNextToken();
225 wxString option
= token
.BeforeFirst(':');
226 wxString val
= token
.AfterFirst(':');
228 if (option
== "bold")
229 StyleSetBold(styleNum
, true);
231 else if (option
== "italic")
232 StyleSetItalic(styleNum
, true);
234 else if (option
== "underline")
235 StyleSetUnderline(styleNum
, true);
237 else if (option
== "eol")
238 StyleSetEOLFilled(styleNum
, true);
240 else if (option
== "size") {
242 if (val
.ToLong(&points
))
243 StyleSetSize(styleNum
, points
);
246 else if (option
== "face")
247 StyleSetFaceName(styleNum
, val
);
249 else if (option
== "fore")
250 StyleSetForeground(styleNum
, wxColourFromSpec(val
));
252 else if (option
== "back")
253 StyleSetBackground(styleNum
, wxColourFromSpec(val
));
258 // Set style size, face, bold, italic, and underline attributes from
259 // a wxFont's attributes.
260 void wxStyledTextCtrl::StyleSetFont(int styleNum
, wxFont
& font
) {
261 int size
= font
.GetPointSize();
262 wxString faceName
= font
.GetFaceName();
263 bool bold
= font
.GetWeight() == wxBOLD
;
264 bool italic
= font
.GetStyle() != wxNORMAL
;
265 bool under
= font
.GetUnderlined();
267 // TODO: add encoding/charset mapping
268 StyleSetFontAttr(styleNum
, size
, faceName
, bold
, italic
, under
);
271 // Set all font style attributes at once.
272 void wxStyledTextCtrl::StyleSetFontAttr(int styleNum
, int size
,
273 const wxString
& faceName
,
274 bool bold
, bool italic
,
276 StyleSetSize(styleNum
, size
);
277 StyleSetFaceName(styleNum
, faceName
);
278 StyleSetBold(styleNum
, bold
);
279 StyleSetItalic(styleNum
, italic
);
280 StyleSetUnderline(styleNum
, underline
);
282 // TODO: add encoding/charset mapping
286 // Perform one of the operations defined by the wxSTC_CMD_* constants.
287 void wxStyledTextCtrl::CmdKeyExecute(int cmd
) {
292 // Set the left and right margin in the edit area, measured in pixels.
293 void wxStyledTextCtrl::SetMargins(int left
, int right
) {
295 SetMarginRight(right
);
299 // Retrieve the start and end positions of the current selection.
300 void wxStyledTextCtrl::GetSelection(int* startPos
, int* endPos
) {
301 if (startPos
!= NULL
)
302 *startPos
= SendMsg(SCI_GETSELECTIONSTART
);
304 *endPos
= SendMsg(SCI_GETSELECTIONEND
);
308 // Retrieve the point in the window where a position is displayed.
309 wxPoint
wxStyledTextCtrl::PointFromPosition(int pos
) {
310 int x
= SendMsg(SCI_POINTXFROMPOSITION
, 0, pos
);
311 int y
= SendMsg(SCI_POINTYFROMPOSITION
, 0, pos
);
312 return wxPoint(x
, y
);
315 // Scroll enough to make the given line visible
316 void wxStyledTextCtrl::ScrollToLine(int line
) {
317 m_swx
->DoScrollToLine(line
);
321 // Scroll enough to make the given column visible
322 void wxStyledTextCtrl::ScrollToColumn(int column
) {
323 m_swx
->DoScrollToColumn(column
);
328 //----------------------------------------------------------------------
331 void wxStyledTextCtrl::OnPaint(wxPaintEvent
& evt
) {
333 wxRegion region
= GetUpdateRegion();
335 m_swx
->DoPaint(&dc
, region
.GetBox());
338 void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent
& evt
) {
339 if (evt
.GetOrientation() == wxHORIZONTAL
)
340 m_swx
->DoHScroll(evt
.GetEventType(), evt
.GetPosition());
342 m_swx
->DoVScroll(evt
.GetEventType(), evt
.GetPosition());
345 void wxStyledTextCtrl::OnSize(wxSizeEvent
& evt
) {
346 wxSize sz
= GetClientSize();
347 m_swx
->DoSize(sz
.x
, sz
.y
);
350 void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent
& evt
) {
351 wxPoint pt
= evt
.GetPosition();
352 m_swx
->DoButtonDown(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
353 evt
.ShiftDown(), evt
.ControlDown(), evt
.AltDown());
356 void wxStyledTextCtrl::OnMouseMove(wxMouseEvent
& evt
) {
357 wxPoint pt
= evt
.GetPosition();
358 m_swx
->DoButtonMove(Point(pt
.x
, pt
.y
));
361 void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent
& evt
) {
362 wxPoint pt
= evt
.GetPosition();
363 m_swx
->DoButtonUp(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
368 void wxStyledTextCtrl::OnContextMenu(wxContextMenuEvent
& evt
) {
369 wxPoint pt
= evt
.GetPosition();
370 ScreenToClient(&pt
.x
, &pt
.y
);
371 m_swx
->DoContextMenu(Point(pt
.x
, pt
.y
));
375 void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent
& evt
) {
376 m_swx
->DoMouseWheel(evt
.GetWheelRotation(),
378 evt
.GetLinesPerAction(),
383 void wxStyledTextCtrl::OnChar(wxKeyEvent
& evt
) {
384 long key
= evt
.KeyCode();
386 // printf("OnChar key:%%d consumed:%%d ctrl:%%d alt:%%d\n",
387 // key, m_lastKeyDownConsumed, evt.ControlDown(), evt.AltDown());
390 // \|@#¬[]{}?£$~ ã,õ,Ã,Õ, ñ, Ñ
392 // On (some?) non-US keyboards the AltGr key is required to enter some
393 // common characters. It comes to us as both Alt and Ctrl down so we need
394 // to let the char through in that case, otherwise if only ctrl or only
395 // alt let's skip it.
396 bool ctrl
= evt
.ControlDown();
397 bool alt
= evt
.AltDown();
398 bool skip
= (ctrl
|| alt
&& ! (ctrl
&& alt
));
400 if (key
<= 0xff && !iscntrl(key
) && !m_lastKeyDownConsumed
&& !skip
) {
401 m_swx
->DoAddChar(key
);
408 void wxStyledTextCtrl::OnKeyDown(wxKeyEvent
& evt
) {
409 long key
= evt
.KeyCode();
410 bool shift
= evt
.ShiftDown(),
411 ctrl
= evt
.ControlDown(),
414 int processed
= m_swx
->DoKeyDown(key
, shift
, ctrl
, alt
, &m_lastKeyDownConsumed
);
416 // printf("key: %%d shift: %%d ctrl: %%d alt: %%d processed: %%d consumed: %%d\n",
417 // key, shift, ctrl, alt, processed, m_lastKeyDownConsumed);
419 if (!processed
&& !m_lastKeyDownConsumed
)
424 void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent
& evt
) {
425 m_swx
->DoLoseFocus();
429 void wxStyledTextCtrl::OnGainFocus(wxFocusEvent
& evt
) {
430 m_swx
->DoGainFocus();
434 void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& evt
) {
435 m_swx
->DoSysColourChange();
439 void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent
& evt
) {
440 // do nothing to help avoid flashing
445 void wxStyledTextCtrl::OnMenu(wxCommandEvent
& evt
) {
446 m_swx
->DoCommand(evt
.GetId());
450 void wxStyledTextCtrl::OnListBox(wxCommandEvent
& evt
) {
451 m_swx
->DoOnListBox();
455 //----------------------------------------------------------------------
456 // Turn notifications from Scintilla into events
459 void wxStyledTextCtrl::NotifyChange() {
460 wxStyledTextEvent
evt(wxEVT_STC_CHANGE
, GetId());
461 GetEventHandler()->ProcessEvent(evt
);
464 void wxStyledTextCtrl::NotifyParent(SCNotification
* _scn
) {
465 SCNotification
& scn
= *_scn
;
466 wxStyledTextEvent
evt(0, GetId());
468 evt
.SetPosition(scn
.position
);
470 evt
.SetModifiers(scn
.modifiers
);
472 switch (scn
.nmhdr
.code
) {
473 case SCN_STYLENEEDED
:
474 evt
.SetEventType(wxEVT_STC_STYLENEEDED
);
478 evt
.SetEventType(wxEVT_STC_CHARADDED
);
481 case SCN_SAVEPOINTREACHED
:
482 evt
.SetEventType(wxEVT_STC_SAVEPOINTREACHED
);
485 case SCN_SAVEPOINTLEFT
:
486 evt
.SetEventType(wxEVT_STC_SAVEPOINTLEFT
);
489 case SCN_MODIFYATTEMPTRO
:
490 evt
.SetEventType(wxEVT_STC_ROMODIFYATTEMPT
);
494 evt
.SetEventType(wxEVT_STC_KEY
);
497 case SCN_DOUBLECLICK
:
498 evt
.SetEventType(wxEVT_STC_DOUBLECLICK
);
502 evt
.SetEventType(wxEVT_STC_UPDATEUI
);
506 evt
.SetEventType(wxEVT_STC_MODIFIED
);
507 evt
.SetModificationType(scn
.modificationType
);
509 evt
.SetText(wxString(scn
.text
, scn
.length
));
510 evt
.SetLength(scn
.length
);
511 evt
.SetLinesAdded(scn
.linesAdded
);
512 evt
.SetLine(scn
.line
);
513 evt
.SetFoldLevelNow(scn
.foldLevelNow
);
514 evt
.SetFoldLevelPrev(scn
.foldLevelPrev
);
517 case SCN_MACRORECORD
:
518 evt
.SetEventType(wxEVT_STC_MACRORECORD
);
519 evt
.SetMessage(scn
.message
);
520 evt
.SetWParam(scn
.wParam
);
521 evt
.SetLParam(scn
.lParam
);
524 case SCN_MARGINCLICK
:
525 evt
.SetEventType(wxEVT_STC_MARGINCLICK
);
526 evt
.SetMargin(scn
.margin
);
530 evt
.SetEventType(wxEVT_STC_NEEDSHOWN
);
531 evt
.SetLength(scn
.length
);
535 evt
.SetEventType(wxEVT_STC_POSCHANGED
);
539 evt
.SetEventType(wxEVT_STC_PAINTED
);
542 case SCN_USERLISTSELECTION
:
543 evt
.SetEventType(wxEVT_STC_USERLISTSELECTION
);
544 evt
.SetListType(scn
.listType
);
545 evt
.SetText(scn
.text
);
549 evt
.SetEventType(wxEVT_STC_URIDROPPED
);
550 evt
.SetText(scn
.text
);
554 evt
.SetEventType(wxEVT_STC_DWELLSTART
);
560 evt
.SetEventType(wxEVT_STC_DWELLEND
);
569 GetEventHandler()->ProcessEvent(evt
);
574 //----------------------------------------------------------------------
575 //----------------------------------------------------------------------
576 //----------------------------------------------------------------------
578 wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType
, int id
)
579 : wxCommandEvent(commandType
, id
)
584 m_modificationType
= 0;
599 bool wxStyledTextEvent::GetShift() const { return (m_modifiers
& SCI_SHIFT
) != 0; }
600 bool wxStyledTextEvent::GetControl() const { return (m_modifiers
& SCI_CTRL
) != 0; }
601 bool wxStyledTextEvent::GetAlt() const { return (m_modifiers
& SCI_ALT
) != 0; }
603 void wxStyledTextEvent::CopyObject(wxObject
& obj
) const {
604 wxCommandEvent::CopyObject(obj
);
606 wxStyledTextEvent
* o
= (wxStyledTextEvent
*)&obj
;
607 o
->m_position
= m_position
;
609 o
->m_modifiers
= m_modifiers
;
610 o
->m_modificationType
= m_modificationType
;
612 o
->m_length
= m_length
;
613 o
->m_linesAdded
= m_linesAdded
;
615 o
->m_foldLevelNow
= m_foldLevelNow
;
616 o
->m_foldLevelPrev
= m_foldLevelPrev
;
618 o
->m_margin
= m_margin
;
620 o
->m_message
= m_message
;
621 o
->m_wParam
= m_wParam
;
622 o
->m_lParam
= m_lParam
;
624 o
->m_listType
= m_listType
;
630 //----------------------------------------------------------------------
631 //----------------------------------------------------------------------