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"
24 #include <wx/tokenzr.h>
25 #include <wx/mstream.h>
29 //----------------------------------------------------------------------
31 const wxChar
* wxSTCNameStr
= wxT("stcwindow");
37 #define MAKELONG(a, b) ((a) | ((b) << 16))
40 static long wxColourAsLong(const wxColour
& co
) {
41 return (((long)co
.Blue() << 16) |
42 ((long)co
.Green() << 8) |
46 static wxColour
wxColourFromLong(long c
) {
48 clr
.Set(c
& 0xff, (c
>> 8) & 0xff, (c
>> 16) & 0xff);
53 static wxColour
wxColourFromSpec(const wxString
& spec
) {
54 // spec should be "#RRGGBB"
55 long red
, green
, blue
;
56 red
= green
= blue
= 0;
57 spec
.Mid(1,2).ToLong(&red
, 16);
58 spec
.Mid(3,2).ToLong(&green
, 16);
59 spec
.Mid(5,2).ToLong(&blue
, 16);
60 return wxColour(red
, green
, blue
);
63 //----------------------------------------------------------------------
65 DEFINE_EVENT_TYPE( wxEVT_STC_CHANGE
)
66 DEFINE_EVENT_TYPE( wxEVT_STC_STYLENEEDED
)
67 DEFINE_EVENT_TYPE( wxEVT_STC_CHARADDED
)
68 DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTREACHED
)
69 DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTLEFT
)
70 DEFINE_EVENT_TYPE( wxEVT_STC_ROMODIFYATTEMPT
)
71 DEFINE_EVENT_TYPE( wxEVT_STC_KEY
)
72 DEFINE_EVENT_TYPE( wxEVT_STC_DOUBLECLICK
)
73 DEFINE_EVENT_TYPE( wxEVT_STC_UPDATEUI
)
74 DEFINE_EVENT_TYPE( wxEVT_STC_MODIFIED
)
75 DEFINE_EVENT_TYPE( wxEVT_STC_MACRORECORD
)
76 DEFINE_EVENT_TYPE( wxEVT_STC_MARGINCLICK
)
77 DEFINE_EVENT_TYPE( wxEVT_STC_NEEDSHOWN
)
78 DEFINE_EVENT_TYPE( wxEVT_STC_POSCHANGED
)
79 DEFINE_EVENT_TYPE( wxEVT_STC_PAINTED
)
80 DEFINE_EVENT_TYPE( wxEVT_STC_USERLISTSELECTION
)
81 DEFINE_EVENT_TYPE( wxEVT_STC_URIDROPPED
)
82 DEFINE_EVENT_TYPE( wxEVT_STC_DWELLSTART
)
83 DEFINE_EVENT_TYPE( wxEVT_STC_DWELLEND
)
84 DEFINE_EVENT_TYPE( wxEVT_STC_START_DRAG
)
85 DEFINE_EVENT_TYPE( wxEVT_STC_DRAG_OVER
)
86 DEFINE_EVENT_TYPE( wxEVT_STC_DO_DROP
)
87 DEFINE_EVENT_TYPE( wxEVT_STC_ZOOM
)
88 DEFINE_EVENT_TYPE( wxEVT_STC_HOTSPOT_CLICK
)
89 DEFINE_EVENT_TYPE( wxEVT_STC_HOTSPOT_DCLICK
)
90 DEFINE_EVENT_TYPE( wxEVT_STC_CALLTIP_CLICK
)
94 BEGIN_EVENT_TABLE(wxStyledTextCtrl
, wxControl
)
95 EVT_PAINT (wxStyledTextCtrl::OnPaint
)
96 EVT_SCROLLWIN (wxStyledTextCtrl::OnScrollWin
)
97 EVT_SCROLL (wxStyledTextCtrl::OnScroll
)
98 EVT_SIZE (wxStyledTextCtrl::OnSize
)
99 EVT_LEFT_DOWN (wxStyledTextCtrl::OnMouseLeftDown
)
100 // Let Scintilla see the double click as a second click
101 EVT_LEFT_DCLICK (wxStyledTextCtrl::OnMouseLeftDown
)
102 EVT_MOTION (wxStyledTextCtrl::OnMouseMove
)
103 EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp
)
104 #if defined(__WXGTK__) || defined(__WXMAC__)
105 EVT_RIGHT_UP (wxStyledTextCtrl::OnMouseRightUp
)
107 EVT_CONTEXT_MENU (wxStyledTextCtrl::OnContextMenu
)
109 EVT_MOUSEWHEEL (wxStyledTextCtrl::OnMouseWheel
)
110 EVT_MIDDLE_UP (wxStyledTextCtrl::OnMouseMiddleUp
)
111 EVT_CHAR (wxStyledTextCtrl::OnChar
)
112 EVT_KEY_DOWN (wxStyledTextCtrl::OnKeyDown
)
113 EVT_KILL_FOCUS (wxStyledTextCtrl::OnLoseFocus
)
114 EVT_SET_FOCUS (wxStyledTextCtrl::OnGainFocus
)
115 EVT_SYS_COLOUR_CHANGED (wxStyledTextCtrl::OnSysColourChanged
)
116 EVT_ERASE_BACKGROUND (wxStyledTextCtrl::OnEraseBackground
)
117 EVT_MENU_RANGE (10, 16, wxStyledTextCtrl::OnMenu
)
118 EVT_LISTBOX_DCLICK (-1, wxStyledTextCtrl::OnListBox
)
122 IMPLEMENT_CLASS(wxStyledTextCtrl
, wxControl
)
123 IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent
, wxCommandEvent
)
126 // forces the linking of the lexer modules
127 int Scintilla_LinkLexers();
130 //----------------------------------------------------------------------
131 // Constructor and Destructor
133 wxStyledTextCtrl::wxStyledTextCtrl(wxWindow
*parent
,
138 const wxString
& name
) :
139 wxControl(parent
, id
, pos
, size
,
140 style
| wxVSCROLL
| wxHSCROLL
| wxWANTS_CHARS
| wxCLIP_CHILDREN
,
141 wxDefaultValidator
, name
)
144 Scintilla_LinkLexers();
146 m_swx
= new ScintillaWX(this);
148 m_lastKeyDownConsumed
= FALSE
;
152 // Put Scintilla into unicode (UTF-8) mode
153 SetCodePage(wxSTC_CP_UTF8
);
158 wxStyledTextCtrl::~wxStyledTextCtrl() {
163 //----------------------------------------------------------------------
165 long wxStyledTextCtrl::SendMsg(int msg
, long wp
, long lp
) {
167 return m_swx
->WndProc(msg
, wp
, lp
);
172 //----------------------------------------------------------------------
173 // BEGIN generated section. The following code is automatically generated
174 // by gen_iface.py from the contents of Scintilla.iface. Do not edit
175 // this file. Edit stc.cpp.in or gen_iface.py instead and regenerate.
179 // END of generated section
180 //----------------------------------------------------------------------
183 // Returns the line number of the line with the caret.
184 int wxStyledTextCtrl::GetCurrentLine() {
185 int line
= LineFromPosition(GetCurrentPos());
190 // Extract style settings from a spec-string which is composed of one or
191 // more of the following comma separated elements:
193 // bold turns on bold
194 // italic turns on italics
195 // fore:#RRGGBB sets the foreground colour
196 // back:#RRGGBB sets the background colour
197 // face:[facename] sets the font face name to use
198 // size:[num] sets the font size in points
199 // eol turns on eol filling
200 // underline turns on underlining
202 void wxStyledTextCtrl::StyleSetSpec(int styleNum
, const wxString
& spec
) {
204 wxStringTokenizer
tkz(spec
, wxT(","));
205 while (tkz
.HasMoreTokens()) {
206 wxString token
= tkz
.GetNextToken();
208 wxString option
= token
.BeforeFirst(':');
209 wxString val
= token
.AfterFirst(':');
211 if (option
== wxT("bold"))
212 StyleSetBold(styleNum
, true);
214 else if (option
== wxT("italic"))
215 StyleSetItalic(styleNum
, true);
217 else if (option
== wxT("underline"))
218 StyleSetUnderline(styleNum
, true);
220 else if (option
== wxT("eol"))
221 StyleSetEOLFilled(styleNum
, true);
223 else if (option
== wxT("size")) {
225 if (val
.ToLong(&points
))
226 StyleSetSize(styleNum
, points
);
229 else if (option
== wxT("face"))
230 StyleSetFaceName(styleNum
, val
);
232 else if (option
== wxT("fore"))
233 StyleSetForeground(styleNum
, wxColourFromSpec(val
));
235 else if (option
== wxT("back"))
236 StyleSetBackground(styleNum
, wxColourFromSpec(val
));
241 // Set style size, face, bold, italic, and underline attributes from
242 // a wxFont's attributes.
243 void wxStyledTextCtrl::StyleSetFont(int styleNum
, wxFont
& font
) {
244 int size
= font
.GetPointSize();
245 wxString faceName
= font
.GetFaceName();
246 bool bold
= font
.GetWeight() == wxBOLD
;
247 bool italic
= font
.GetStyle() != wxNORMAL
;
248 bool under
= font
.GetUnderlined();
250 // TODO: add encoding/charset mapping
251 StyleSetFontAttr(styleNum
, size
, faceName
, bold
, italic
, under
);
254 // Set all font style attributes at once.
255 void wxStyledTextCtrl::StyleSetFontAttr(int styleNum
, int size
,
256 const wxString
& faceName
,
257 bool bold
, bool italic
,
259 StyleSetSize(styleNum
, size
);
260 StyleSetFaceName(styleNum
, faceName
);
261 StyleSetBold(styleNum
, bold
);
262 StyleSetItalic(styleNum
, italic
);
263 StyleSetUnderline(styleNum
, underline
);
265 // TODO: add encoding/charset mapping
269 // Perform one of the operations defined by the wxSTC_CMD_* constants.
270 void wxStyledTextCtrl::CmdKeyExecute(int cmd
) {
275 // Set the left and right margin in the edit area, measured in pixels.
276 void wxStyledTextCtrl::SetMargins(int left
, int right
) {
278 SetMarginRight(right
);
282 // Retrieve the start and end positions of the current selection.
283 void wxStyledTextCtrl::GetSelection(int* startPos
, int* endPos
) {
284 if (startPos
!= NULL
)
285 *startPos
= SendMsg(SCI_GETSELECTIONSTART
);
287 *endPos
= SendMsg(SCI_GETSELECTIONEND
);
291 // Retrieve the point in the window where a position is displayed.
292 wxPoint
wxStyledTextCtrl::PointFromPosition(int pos
) {
293 int x
= SendMsg(SCI_POINTXFROMPOSITION
, 0, pos
);
294 int y
= SendMsg(SCI_POINTYFROMPOSITION
, 0, pos
);
295 return wxPoint(x
, y
);
298 // Scroll enough to make the given line visible
299 void wxStyledTextCtrl::ScrollToLine(int line
) {
300 m_swx
->DoScrollToLine(line
);
304 // Scroll enough to make the given column visible
305 void wxStyledTextCtrl::ScrollToColumn(int column
) {
306 m_swx
->DoScrollToColumn(column
);
311 //----------------------------------------------------------------------
314 void wxStyledTextCtrl::OnPaint(wxPaintEvent
& evt
) {
316 m_swx
->DoPaint(&dc
, GetUpdateRegion().GetBox());
319 void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent
& evt
) {
320 if (evt
.GetOrientation() == wxHORIZONTAL
)
321 m_swx
->DoHScroll(evt
.GetEventType(), evt
.GetPosition());
323 m_swx
->DoVScroll(evt
.GetEventType(), evt
.GetPosition());
326 void wxStyledTextCtrl::OnScroll(wxScrollEvent
& evt
) {
327 wxScrollBar
* sb
= wxDynamicCast(evt
.GetEventObject(), wxScrollBar
);
329 if (sb
->IsVertical())
330 m_swx
->DoVScroll(evt
.GetEventType(), evt
.GetPosition());
332 m_swx
->DoHScroll(evt
.GetEventType(), evt
.GetPosition());
336 void wxStyledTextCtrl::OnSize(wxSizeEvent
& evt
) {
337 wxSize sz
= GetClientSize();
338 m_swx
->DoSize(sz
.x
, sz
.y
);
341 void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent
& evt
) {
343 wxPoint pt
= evt
.GetPosition();
344 m_swx
->DoLeftButtonDown(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
345 evt
.ShiftDown(), evt
.ControlDown(), evt
.AltDown());
348 void wxStyledTextCtrl::OnMouseMove(wxMouseEvent
& evt
) {
349 wxPoint pt
= evt
.GetPosition();
350 m_swx
->DoLeftButtonMove(Point(pt
.x
, pt
.y
));
353 void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent
& evt
) {
354 wxPoint pt
= evt
.GetPosition();
355 m_swx
->DoLeftButtonUp(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
360 void wxStyledTextCtrl::OnMouseRightUp(wxMouseEvent
& evt
) {
361 wxPoint pt
= evt
.GetPosition();
362 m_swx
->DoContextMenu(Point(pt
.x
, pt
.y
));
366 void wxStyledTextCtrl::OnMouseMiddleUp(wxMouseEvent
& evt
) {
367 wxPoint pt
= evt
.GetPosition();
368 m_swx
->DoMiddleButtonUp(Point(pt
.x
, pt
.y
));
371 void wxStyledTextCtrl::OnContextMenu(wxContextMenuEvent
& evt
) {
372 wxPoint pt
= evt
.GetPosition();
373 ScreenToClient(&pt
.x
, &pt
.y
);
374 m_swx
->DoContextMenu(Point(pt
.x
, pt
.y
));
378 void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent
& evt
) {
379 m_swx
->DoMouseWheel(evt
.GetWheelRotation(),
381 evt
.GetLinesPerAction(),
387 void wxStyledTextCtrl::OnChar(wxKeyEvent
& evt
) {
388 // On (some?) non-US keyboards the AltGr key is required to enter some
389 // common characters. It comes to us as both Alt and Ctrl down so we need
390 // to let the char through in that case, otherwise if only ctrl or only
391 // alt let's skip it.
392 bool ctrl
= evt
.ControlDown();
393 bool alt
= evt
.AltDown();
394 bool skip
= ((ctrl
|| alt
) && ! (ctrl
&& alt
));
396 int key
= evt
.GetKeyCode();
398 // printf("OnChar key:%%d consumed:%%d ctrl:%%d alt:%%d skip:%%d\n",
399 // key, m_lastKeyDownConsumed, ctrl, alt, skip);
401 if ( (key
<= WXK_START
|| key
> WXK_NUMPAD_DIVIDE
) &&
402 !m_lastKeyDownConsumed
&& !skip
) {
403 m_swx
->DoAddChar(key
);
410 void wxStyledTextCtrl::OnKeyDown(wxKeyEvent
& evt
) {
411 int key
= evt
.GetKeyCode();
412 bool shift
= evt
.ShiftDown(),
413 ctrl
= evt
.ControlDown(),
416 int processed
= m_swx
->DoKeyDown(key
, shift
, ctrl
, alt
, &m_lastKeyDownConsumed
);
418 // printf("KeyDn key:%%d shift:%%d ctrl:%%d alt:%%d processed:%%d consumed:%%d\n",
419 // key, shift, ctrl, alt, processed, m_lastKeyDownConsumed);
421 if (!processed
&& !m_lastKeyDownConsumed
)
426 void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent
& evt
) {
427 m_swx
->DoLoseFocus();
431 void wxStyledTextCtrl::OnGainFocus(wxFocusEvent
& evt
) {
432 m_swx
->DoGainFocus();
436 void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& evt
) {
437 m_swx
->DoSysColourChange();
441 void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent
& evt
) {
442 // do nothing to help avoid flashing
447 void wxStyledTextCtrl::OnMenu(wxCommandEvent
& evt
) {
448 m_swx
->DoCommand(evt
.GetId());
452 void wxStyledTextCtrl::OnListBox(wxCommandEvent
& evt
) {
453 m_swx
->DoOnListBox();
457 //----------------------------------------------------------------------
458 // Turn notifications from Scintilla into events
461 void wxStyledTextCtrl::NotifyChange() {
462 wxStyledTextEvent
evt(wxEVT_STC_CHANGE
, GetId());
463 evt
.SetEventObject(this);
464 GetEventHandler()->ProcessEvent(evt
);
468 static void SetEventText(wxStyledTextEvent
& evt
, const char* text
,
472 // The unicode conversion MUST have a null byte to terminate the
473 // string so move it into a buffer first and give it one.
474 wxMemoryBuffer
buf(length
+1);
475 buf
.AppendData((void*)text
, length
);
477 evt
.SetText(stc2wx(buf
));
481 void wxStyledTextCtrl::NotifyParent(SCNotification
* _scn
) {
482 SCNotification
& scn
= *_scn
;
483 wxStyledTextEvent
evt(0, GetId());
485 evt
.SetEventObject(this);
486 evt
.SetPosition(scn
.position
);
488 evt
.SetModifiers(scn
.modifiers
);
490 switch (scn
.nmhdr
.code
) {
491 case SCN_STYLENEEDED
:
492 evt
.SetEventType(wxEVT_STC_STYLENEEDED
);
496 evt
.SetEventType(wxEVT_STC_CHARADDED
);
499 case SCN_SAVEPOINTREACHED
:
500 evt
.SetEventType(wxEVT_STC_SAVEPOINTREACHED
);
503 case SCN_SAVEPOINTLEFT
:
504 evt
.SetEventType(wxEVT_STC_SAVEPOINTLEFT
);
507 case SCN_MODIFYATTEMPTRO
:
508 evt
.SetEventType(wxEVT_STC_ROMODIFYATTEMPT
);
512 evt
.SetEventType(wxEVT_STC_KEY
);
515 case SCN_DOUBLECLICK
:
516 evt
.SetEventType(wxEVT_STC_DOUBLECLICK
);
520 evt
.SetEventType(wxEVT_STC_UPDATEUI
);
524 evt
.SetEventType(wxEVT_STC_MODIFIED
);
525 evt
.SetModificationType(scn
.modificationType
);
526 SetEventText(evt
, scn
.text
, scn
.length
);
527 evt
.SetLength(scn
.length
);
528 evt
.SetLinesAdded(scn
.linesAdded
);
529 evt
.SetLine(scn
.line
);
530 evt
.SetFoldLevelNow(scn
.foldLevelNow
);
531 evt
.SetFoldLevelPrev(scn
.foldLevelPrev
);
534 case SCN_MACRORECORD
:
535 evt
.SetEventType(wxEVT_STC_MACRORECORD
);
536 evt
.SetMessage(scn
.message
);
537 evt
.SetWParam(scn
.wParam
);
538 evt
.SetLParam(scn
.lParam
);
541 case SCN_MARGINCLICK
:
542 evt
.SetEventType(wxEVT_STC_MARGINCLICK
);
543 evt
.SetMargin(scn
.margin
);
547 evt
.SetEventType(wxEVT_STC_NEEDSHOWN
);
548 evt
.SetLength(scn
.length
);
552 evt
.SetEventType(wxEVT_STC_PAINTED
);
555 case SCN_USERLISTSELECTION
:
556 evt
.SetEventType(wxEVT_STC_USERLISTSELECTION
);
557 evt
.SetListType(scn
.listType
);
558 SetEventText(evt
, scn
.text
, strlen(scn
.text
));
562 evt
.SetEventType(wxEVT_STC_URIDROPPED
);
563 SetEventText(evt
, scn
.text
, strlen(scn
.text
));
567 evt
.SetEventType(wxEVT_STC_DWELLSTART
);
573 evt
.SetEventType(wxEVT_STC_DWELLEND
);
579 evt
.SetEventType(wxEVT_STC_ZOOM
);
582 case SCN_HOTSPOTCLICK
:
583 evt
.SetEventType(wxEVT_STC_HOTSPOT_CLICK
);
586 case SCN_HOTSPOTDOUBLECLICK
:
587 evt
.SetEventType(wxEVT_STC_HOTSPOT_DCLICK
);
590 case SCN_CALLTIPCLICK
:
591 evt
.SetEventType(wxEVT_STC_CALLTIP_CLICK
);
598 GetEventHandler()->ProcessEvent(evt
);
602 //----------------------------------------------------------------------
603 //----------------------------------------------------------------------
604 //----------------------------------------------------------------------
606 wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType
, int id
)
607 : wxCommandEvent(commandType
, id
)
612 m_modificationType
= 0;
625 m_dragAllowMove
= FALSE
;
626 #if wxUSE_DRAG_AND_DROP
627 m_dragResult
= wxDragNone
;
631 bool wxStyledTextEvent::GetShift() const { return (m_modifiers
& SCI_SHIFT
) != 0; }
632 bool wxStyledTextEvent::GetControl() const { return (m_modifiers
& SCI_CTRL
) != 0; }
633 bool wxStyledTextEvent::GetAlt() const { return (m_modifiers
& SCI_ALT
) != 0; }
636 wxStyledTextEvent::wxStyledTextEvent(const wxStyledTextEvent
& event
):
637 wxCommandEvent(event
)
639 m_position
= event
.m_position
;
641 m_modifiers
= event
.m_modifiers
;
642 m_modificationType
= event
.m_modificationType
;
643 m_text
= event
.m_text
;
644 m_length
= event
.m_length
;
645 m_linesAdded
= event
.m_linesAdded
;
646 m_line
= event
.m_line
;
647 m_foldLevelNow
= event
.m_foldLevelNow
;
648 m_foldLevelPrev
= event
.m_foldLevelPrev
;
650 m_margin
= event
.m_margin
;
652 m_message
= event
.m_message
;
653 m_wParam
= event
.m_wParam
;
654 m_lParam
= event
.m_lParam
;
656 m_listType
= event
.m_listType
;
660 m_dragText
= event
.m_dragText
;
661 m_dragAllowMove
=event
.m_dragAllowMove
;
662 #if wxUSE_DRAG_AND_DROP
663 m_dragResult
= event
.m_dragResult
;
667 //----------------------------------------------------------------------
668 //----------------------------------------------------------------------