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>
26 //----------------------------------------------------------------------
28 const wxChar
* wxSTCNameStr
= wxT("stcwindow");
34 #define MAKELONG(a, b) ((a) | ((b) << 16))
37 static long wxColourAsLong(const wxColour
& co
) {
38 return (((long)co
.Blue() << 16) |
39 ((long)co
.Green() << 8) |
43 static wxColour
wxColourFromLong(long c
) {
45 clr
.Set(c
& 0xff, (c
>> 8) & 0xff, (c
>> 16) & 0xff);
50 static wxColour
wxColourFromSpec(const wxString
& spec
) {
51 // spec should be "#RRGGBB"
52 long red
, green
, blue
;
53 red
= green
= blue
= 0;
54 spec
.Mid(1,2).ToLong(&red
, 16);
55 spec
.Mid(3,2).ToLong(&green
, 16);
56 spec
.Mid(5,2).ToLong(&blue
, 16);
57 return wxColour(red
, green
, blue
);
60 //----------------------------------------------------------------------
62 DEFINE_EVENT_TYPE( wxEVT_STC_CHANGE
)
63 DEFINE_EVENT_TYPE( wxEVT_STC_STYLENEEDED
)
64 DEFINE_EVENT_TYPE( wxEVT_STC_CHARADDED
)
65 DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTREACHED
)
66 DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTLEFT
)
67 DEFINE_EVENT_TYPE( wxEVT_STC_ROMODIFYATTEMPT
)
68 DEFINE_EVENT_TYPE( wxEVT_STC_KEY
)
69 DEFINE_EVENT_TYPE( wxEVT_STC_DOUBLECLICK
)
70 DEFINE_EVENT_TYPE( wxEVT_STC_UPDATEUI
)
71 DEFINE_EVENT_TYPE( wxEVT_STC_MODIFIED
)
72 DEFINE_EVENT_TYPE( wxEVT_STC_MACRORECORD
)
73 DEFINE_EVENT_TYPE( wxEVT_STC_MARGINCLICK
)
74 DEFINE_EVENT_TYPE( wxEVT_STC_NEEDSHOWN
)
75 DEFINE_EVENT_TYPE( wxEVT_STC_POSCHANGED
)
76 DEFINE_EVENT_TYPE( wxEVT_STC_PAINTED
)
77 DEFINE_EVENT_TYPE( wxEVT_STC_USERLISTSELECTION
)
78 DEFINE_EVENT_TYPE( wxEVT_STC_URIDROPPED
)
79 DEFINE_EVENT_TYPE( wxEVT_STC_DWELLSTART
)
80 DEFINE_EVENT_TYPE( wxEVT_STC_DWELLEND
)
81 DEFINE_EVENT_TYPE( wxEVT_STC_START_DRAG
)
82 DEFINE_EVENT_TYPE( wxEVT_STC_DRAG_OVER
)
83 DEFINE_EVENT_TYPE( wxEVT_STC_DO_DROP
)
86 BEGIN_EVENT_TABLE(wxStyledTextCtrl
, wxControl
)
87 EVT_PAINT (wxStyledTextCtrl::OnPaint
)
88 EVT_SCROLLWIN (wxStyledTextCtrl::OnScrollWin
)
89 EVT_SCROLL (wxStyledTextCtrl::OnScroll
)
90 EVT_SIZE (wxStyledTextCtrl::OnSize
)
91 EVT_LEFT_DOWN (wxStyledTextCtrl::OnMouseLeftDown
)
92 #if defined(__WXMSW__) || defined(__WXMAC__)
93 // Let Scintilla see the double click as a second click
94 EVT_LEFT_DCLICK (wxStyledTextCtrl::OnMouseLeftDown
)
96 EVT_MOTION (wxStyledTextCtrl::OnMouseMove
)
97 EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp
)
98 #if defined(__WXGTK__) || defined(__WXMAC__)
99 EVT_RIGHT_UP (wxStyledTextCtrl::OnMouseRightUp
)
101 EVT_CONTEXT_MENU (wxStyledTextCtrl::OnContextMenu
)
103 EVT_MOUSEWHEEL (wxStyledTextCtrl::OnMouseWheel
)
104 EVT_CHAR (wxStyledTextCtrl::OnChar
)
105 EVT_KEY_DOWN (wxStyledTextCtrl::OnKeyDown
)
106 EVT_KILL_FOCUS (wxStyledTextCtrl::OnLoseFocus
)
107 EVT_SET_FOCUS (wxStyledTextCtrl::OnGainFocus
)
108 EVT_SYS_COLOUR_CHANGED (wxStyledTextCtrl::OnSysColourChanged
)
109 EVT_ERASE_BACKGROUND (wxStyledTextCtrl::OnEraseBackground
)
110 EVT_MENU_RANGE (-1, -1, wxStyledTextCtrl::OnMenu
)
111 EVT_LISTBOX_DCLICK (-1, wxStyledTextCtrl::OnListBox
)
115 IMPLEMENT_CLASS(wxStyledTextCtrl
, wxControl
)
116 IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent
, wxCommandEvent
)
118 // forces the linking of the lexer modules
119 int Scintilla_LinkLexers();
121 //----------------------------------------------------------------------
122 // Constructor and Destructor
124 wxStyledTextCtrl::wxStyledTextCtrl(wxWindow
*parent
,
129 const wxString
& name
) :
130 wxControl(parent
, id
, pos
, size
,
131 style
| wxVSCROLL
| wxHSCROLL
| wxWANTS_CHARS
| wxCLIP_CHILDREN
,
132 wxDefaultValidator
, name
)
134 Scintilla_LinkLexers();
135 m_swx
= new ScintillaWX(this);
137 m_lastKeyDownConsumed
= FALSE
;
141 // Put Scintilla into unicode (UTF-8) mode
142 SetCodePage(wxSTC_CP_UTF8
);
147 wxStyledTextCtrl::~wxStyledTextCtrl() {
152 //----------------------------------------------------------------------
154 long wxStyledTextCtrl::SendMsg(int msg
, long wp
, long lp
) {
156 return m_swx
->WndProc(msg
, wp
, lp
);
161 //----------------------------------------------------------------------
162 // BEGIN generated section. The following code is automatically generated
163 // by gen_iface.py from the contents of Scintilla.iface. Do not edit
164 // this file. Edit stc.cpp.in or gen_iface.py instead and regenerate.
168 // END of generated section
169 //----------------------------------------------------------------------
172 // Returns the line number of the line with the caret.
173 int wxStyledTextCtrl::GetCurrentLine() {
174 int line
= LineFromPosition(GetCurrentPos());
179 // Extract style settings from a spec-string which is composed of one or
180 // more of the following comma separated elements:
182 // bold turns on bold
183 // italic turns on italics
184 // fore:#RRGGBB sets the foreground colour
185 // back:#RRGGBB sets the background colour
186 // face:[facename] sets the font face name to use
187 // size:[num] sets the font size in points
188 // eol turns on eol filling
189 // underline turns on underlining
191 void wxStyledTextCtrl::StyleSetSpec(int styleNum
, const wxString
& spec
) {
193 wxStringTokenizer
tkz(spec
, wxT(","));
194 while (tkz
.HasMoreTokens()) {
195 wxString token
= tkz
.GetNextToken();
197 wxString option
= token
.BeforeFirst(':');
198 wxString val
= token
.AfterFirst(':');
200 if (option
== wxT("bold"))
201 StyleSetBold(styleNum
, true);
203 else if (option
== wxT("italic"))
204 StyleSetItalic(styleNum
, true);
206 else if (option
== wxT("underline"))
207 StyleSetUnderline(styleNum
, true);
209 else if (option
== wxT("eol"))
210 StyleSetEOLFilled(styleNum
, true);
212 else if (option
== wxT("size")) {
214 if (val
.ToLong(&points
))
215 StyleSetSize(styleNum
, points
);
218 else if (option
== wxT("face"))
219 StyleSetFaceName(styleNum
, val
);
221 else if (option
== wxT("fore"))
222 StyleSetForeground(styleNum
, wxColourFromSpec(val
));
224 else if (option
== wxT("back"))
225 StyleSetBackground(styleNum
, wxColourFromSpec(val
));
230 // Set style size, face, bold, italic, and underline attributes from
231 // a wxFont's attributes.
232 void wxStyledTextCtrl::StyleSetFont(int styleNum
, wxFont
& font
) {
233 int size
= font
.GetPointSize();
234 wxString faceName
= font
.GetFaceName();
235 bool bold
= font
.GetWeight() == wxBOLD
;
236 bool italic
= font
.GetStyle() != wxNORMAL
;
237 bool under
= font
.GetUnderlined();
239 // TODO: add encoding/charset mapping
240 StyleSetFontAttr(styleNum
, size
, faceName
, bold
, italic
, under
);
243 // Set all font style attributes at once.
244 void wxStyledTextCtrl::StyleSetFontAttr(int styleNum
, int size
,
245 const wxString
& faceName
,
246 bool bold
, bool italic
,
248 StyleSetSize(styleNum
, size
);
249 StyleSetFaceName(styleNum
, faceName
);
250 StyleSetBold(styleNum
, bold
);
251 StyleSetItalic(styleNum
, italic
);
252 StyleSetUnderline(styleNum
, underline
);
254 // TODO: add encoding/charset mapping
258 // Perform one of the operations defined by the wxSTC_CMD_* constants.
259 void wxStyledTextCtrl::CmdKeyExecute(int cmd
) {
264 // Set the left and right margin in the edit area, measured in pixels.
265 void wxStyledTextCtrl::SetMargins(int left
, int right
) {
267 SetMarginRight(right
);
271 // Retrieve the start and end positions of the current selection.
272 void wxStyledTextCtrl::GetSelection(int* startPos
, int* endPos
) {
273 if (startPos
!= NULL
)
274 *startPos
= SendMsg(SCI_GETSELECTIONSTART
);
276 *endPos
= SendMsg(SCI_GETSELECTIONEND
);
280 // Retrieve the point in the window where a position is displayed.
281 wxPoint
wxStyledTextCtrl::PointFromPosition(int pos
) {
282 int x
= SendMsg(SCI_POINTXFROMPOSITION
, 0, pos
);
283 int y
= SendMsg(SCI_POINTYFROMPOSITION
, 0, pos
);
284 return wxPoint(x
, y
);
287 // Scroll enough to make the given line visible
288 void wxStyledTextCtrl::ScrollToLine(int line
) {
289 m_swx
->DoScrollToLine(line
);
293 // Scroll enough to make the given column visible
294 void wxStyledTextCtrl::ScrollToColumn(int column
) {
295 m_swx
->DoScrollToColumn(column
);
300 //----------------------------------------------------------------------
303 void wxStyledTextCtrl::OnPaint(wxPaintEvent
& evt
) {
305 wxRegion region
= GetUpdateRegion();
307 m_swx
->DoPaint(&dc
, region
.GetBox());
310 void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent
& evt
) {
311 if (evt
.GetOrientation() == wxHORIZONTAL
)
312 m_swx
->DoHScroll(evt
.GetEventType(), evt
.GetPosition());
314 m_swx
->DoVScroll(evt
.GetEventType(), evt
.GetPosition());
317 void wxStyledTextCtrl::OnScroll(wxScrollEvent
& evt
) {
318 wxScrollBar
* sb
= wxDynamicCast(evt
.GetEventObject(), wxScrollBar
);
320 if (sb
->IsVertical())
321 m_swx
->DoVScroll(evt
.GetEventType(), evt
.GetPosition());
323 m_swx
->DoHScroll(evt
.GetEventType(), evt
.GetPosition());
327 void wxStyledTextCtrl::OnSize(wxSizeEvent
& evt
) {
328 wxSize sz
= GetClientSize();
329 m_swx
->DoSize(sz
.x
, sz
.y
);
332 void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent
& evt
) {
333 wxPoint pt
= evt
.GetPosition();
334 m_swx
->DoButtonDown(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
335 evt
.ShiftDown(), evt
.ControlDown(), evt
.AltDown());
338 void wxStyledTextCtrl::OnMouseMove(wxMouseEvent
& evt
) {
339 wxPoint pt
= evt
.GetPosition();
340 m_swx
->DoButtonMove(Point(pt
.x
, pt
.y
));
343 void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent
& evt
) {
344 wxPoint pt
= evt
.GetPosition();
345 m_swx
->DoButtonUp(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
350 void wxStyledTextCtrl::OnMouseRightUp(wxMouseEvent
& evt
) {
351 wxPoint pt
= evt
.GetPosition();
352 m_swx
->DoContextMenu(Point(pt
.x
, pt
.y
));
356 void wxStyledTextCtrl::OnContextMenu(wxContextMenuEvent
& evt
) {
357 wxPoint pt
= evt
.GetPosition();
358 ScreenToClient(&pt
.x
, &pt
.y
);
359 m_swx
->DoContextMenu(Point(pt
.x
, pt
.y
));
363 void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent
& evt
) {
364 m_swx
->DoMouseWheel(evt
.GetWheelRotation(),
366 evt
.GetLinesPerAction(),
371 void wxStyledTextCtrl::OnChar(wxKeyEvent
& evt
) {
372 int key
= evt
.GetKeyCode();
374 // On (some?) non-US keyboards the AltGr key is required to enter some
375 // common characters. It comes to us as both Alt and Ctrl down so we need
376 // to let the char through in that case, otherwise if only ctrl or only
377 // alt let's skip it.
378 bool ctrl
= evt
.ControlDown();
379 bool alt
= evt
.AltDown();
380 bool skip
= ((ctrl
|| alt
) && ! (ctrl
&& alt
));
382 // printf("OnChar key:%%d consumed:%%d ctrl:%%d alt:%%d skip:%%d\n",
383 // key, m_lastKeyDownConsumed, ctrl, alt, skip);
385 if (key
<= WXK_START
&& /*key >= 32 &&*/ !m_lastKeyDownConsumed
&& !skip
) {
386 m_swx
->DoAddChar(key
);
393 void wxStyledTextCtrl::OnKeyDown(wxKeyEvent
& evt
) {
394 int key
= evt
.GetKeyCode();
395 bool shift
= evt
.ShiftDown(),
396 ctrl
= evt
.ControlDown(),
399 int processed
= m_swx
->DoKeyDown(key
, shift
, ctrl
, alt
, &m_lastKeyDownConsumed
);
401 // printf("KeyDn key:%%d shift:%%d ctrl:%%d alt:%%d processed:%%d consumed:%%d\n",
402 // key, shift, ctrl, alt, processed, m_lastKeyDownConsumed);
404 if (!processed
&& !m_lastKeyDownConsumed
)
409 void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent
& evt
) {
410 m_swx
->DoLoseFocus();
414 void wxStyledTextCtrl::OnGainFocus(wxFocusEvent
& evt
) {
415 m_swx
->DoGainFocus();
419 void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& evt
) {
420 m_swx
->DoSysColourChange();
424 void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent
& evt
) {
425 // do nothing to help avoid flashing
430 void wxStyledTextCtrl::OnMenu(wxCommandEvent
& evt
) {
431 m_swx
->DoCommand(evt
.GetId());
435 void wxStyledTextCtrl::OnListBox(wxCommandEvent
& evt
) {
436 m_swx
->DoOnListBox();
440 //----------------------------------------------------------------------
441 // Turn notifications from Scintilla into events
444 void wxStyledTextCtrl::NotifyChange() {
445 wxStyledTextEvent
evt(wxEVT_STC_CHANGE
, GetId());
446 evt
.SetEventObject(this);
447 GetEventHandler()->ProcessEvent(evt
);
450 void wxStyledTextCtrl::NotifyParent(SCNotification
* _scn
) {
451 SCNotification
& scn
= *_scn
;
452 wxStyledTextEvent
evt(0, GetId());
454 evt
.SetEventObject(this);
455 evt
.SetPosition(scn
.position
);
457 evt
.SetModifiers(scn
.modifiers
);
459 switch (scn
.nmhdr
.code
) {
460 case SCN_STYLENEEDED
:
461 evt
.SetEventType(wxEVT_STC_STYLENEEDED
);
465 evt
.SetEventType(wxEVT_STC_CHARADDED
);
468 case SCN_SAVEPOINTREACHED
:
469 evt
.SetEventType(wxEVT_STC_SAVEPOINTREACHED
);
472 case SCN_SAVEPOINTLEFT
:
473 evt
.SetEventType(wxEVT_STC_SAVEPOINTLEFT
);
476 case SCN_MODIFYATTEMPTRO
:
477 evt
.SetEventType(wxEVT_STC_ROMODIFYATTEMPT
);
481 evt
.SetEventType(wxEVT_STC_KEY
);
484 case SCN_DOUBLECLICK
:
485 evt
.SetEventType(wxEVT_STC_DOUBLECLICK
);
489 evt
.SetEventType(wxEVT_STC_UPDATEUI
);
493 evt
.SetEventType(wxEVT_STC_MODIFIED
);
494 evt
.SetModificationType(scn
.modificationType
);
496 // The unicode conversion MUST have a null byte to terminate the
497 // string so move it into a buffer first and give it one.
498 wxMemoryBuffer
buf(scn
.length
+1);
499 buf
.AppendData((void*)scn
.text
, scn
.length
);
501 evt
.SetText(stc2wx(buf
));
503 evt
.SetLength(scn
.length
);
504 evt
.SetLinesAdded(scn
.linesAdded
);
505 evt
.SetLine(scn
.line
);
506 evt
.SetFoldLevelNow(scn
.foldLevelNow
);
507 evt
.SetFoldLevelPrev(scn
.foldLevelPrev
);
510 case SCN_MACRORECORD
:
511 evt
.SetEventType(wxEVT_STC_MACRORECORD
);
512 evt
.SetMessage(scn
.message
);
513 evt
.SetWParam(scn
.wParam
);
514 evt
.SetLParam(scn
.lParam
);
517 case SCN_MARGINCLICK
:
518 evt
.SetEventType(wxEVT_STC_MARGINCLICK
);
519 evt
.SetMargin(scn
.margin
);
523 evt
.SetEventType(wxEVT_STC_NEEDSHOWN
);
524 evt
.SetLength(scn
.length
);
528 evt
.SetEventType(wxEVT_STC_PAINTED
);
531 case SCN_USERLISTSELECTION
:
532 evt
.SetEventType(wxEVT_STC_USERLISTSELECTION
);
533 evt
.SetListType(scn
.listType
);
534 evt
.SetText(scn
.text
);
538 evt
.SetEventType(wxEVT_STC_URIDROPPED
);
539 evt
.SetText(scn
.text
);
543 evt
.SetEventType(wxEVT_STC_DWELLSTART
);
549 evt
.SetEventType(wxEVT_STC_DWELLEND
);
558 GetEventHandler()->ProcessEvent(evt
);
562 //----------------------------------------------------------------------
563 //----------------------------------------------------------------------
564 //----------------------------------------------------------------------
566 wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType
, int id
)
567 : wxCommandEvent(commandType
, id
)
572 m_modificationType
= 0;
585 m_dragAllowMove
= FALSE
;
586 #if wxUSE_DRAG_AND_DROP
587 m_dragResult
= wxDragNone
;
591 bool wxStyledTextEvent::GetShift() const { return (m_modifiers
& SCI_SHIFT
) != 0; }
592 bool wxStyledTextEvent::GetControl() const { return (m_modifiers
& SCI_CTRL
) != 0; }
593 bool wxStyledTextEvent::GetAlt() const { return (m_modifiers
& SCI_ALT
) != 0; }
596 wxStyledTextEvent::wxStyledTextEvent(const wxStyledTextEvent
& event
):
597 wxCommandEvent(event
)
599 m_position
= event
.m_position
;
601 m_modifiers
= event
.m_modifiers
;
602 m_modificationType
= event
.m_modificationType
;
603 m_text
= event
.m_text
;
604 m_length
= event
.m_length
;
605 m_linesAdded
= event
.m_linesAdded
;
606 m_line
= event
.m_line
;
607 m_foldLevelNow
= event
.m_foldLevelNow
;
608 m_foldLevelPrev
= event
.m_foldLevelPrev
;
610 m_margin
= event
.m_margin
;
612 m_message
= event
.m_message
;
613 m_wParam
= event
.m_wParam
;
614 m_lParam
= event
.m_lParam
;
616 m_listType
= event
.m_listType
;
620 m_dragText
= event
.m_dragText
;
621 m_dragAllowMove
=event
.m_dragAllowMove
;
622 #if wxUSE_DRAG_AND_DROP
623 m_dragResult
= event
.m_dragResult
;
627 //----------------------------------------------------------------------
628 //----------------------------------------------------------------------