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
)
84 DEFINE_EVENT_TYPE( wxEVT_STC_ZOOM
)
87 BEGIN_EVENT_TABLE(wxStyledTextCtrl
, wxControl
)
88 EVT_PAINT (wxStyledTextCtrl::OnPaint
)
89 EVT_SCROLLWIN (wxStyledTextCtrl::OnScrollWin
)
90 EVT_SCROLL (wxStyledTextCtrl::OnScroll
)
91 EVT_SIZE (wxStyledTextCtrl::OnSize
)
92 EVT_LEFT_DOWN (wxStyledTextCtrl::OnMouseLeftDown
)
93 // Let Scintilla see the double click as a second click
94 EVT_LEFT_DCLICK (wxStyledTextCtrl::OnMouseLeftDown
)
95 EVT_MOTION (wxStyledTextCtrl::OnMouseMove
)
96 EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp
)
97 #if defined(__WXGTK__) || defined(__WXMAC__)
98 EVT_RIGHT_UP (wxStyledTextCtrl::OnMouseRightUp
)
100 EVT_CONTEXT_MENU (wxStyledTextCtrl::OnContextMenu
)
102 EVT_MOUSEWHEEL (wxStyledTextCtrl::OnMouseWheel
)
103 EVT_MIDDLE_UP (wxStyledTextCtrl::OnMouseMiddleUp
)
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 (10, 16, wxStyledTextCtrl::OnMenu
)
111 EVT_LISTBOX_DCLICK (-1, wxStyledTextCtrl::OnListBox
)
115 IMPLEMENT_CLASS(wxStyledTextCtrl
, wxControl
)
116 IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent
, wxCommandEvent
)
119 // forces the linking of the lexer modules
120 int Scintilla_LinkLexers();
123 //----------------------------------------------------------------------
124 // Constructor and Destructor
126 wxStyledTextCtrl::wxStyledTextCtrl(wxWindow
*parent
,
131 const wxString
& name
) :
132 wxControl(parent
, id
, pos
, size
,
133 style
| wxVSCROLL
| wxHSCROLL
| wxWANTS_CHARS
| wxCLIP_CHILDREN
,
134 wxDefaultValidator
, name
)
137 Scintilla_LinkLexers();
139 m_swx
= new ScintillaWX(this);
141 m_lastKeyDownConsumed
= FALSE
;
145 // Put Scintilla into unicode (UTF-8) mode
146 SetCodePage(wxSTC_CP_UTF8
);
151 wxStyledTextCtrl::~wxStyledTextCtrl() {
156 //----------------------------------------------------------------------
158 long wxStyledTextCtrl::SendMsg(int msg
, long wp
, long lp
) {
160 return m_swx
->WndProc(msg
, wp
, lp
);
165 //----------------------------------------------------------------------
166 // BEGIN generated section. The following code is automatically generated
167 // by gen_iface.py from the contents of Scintilla.iface. Do not edit
168 // this file. Edit stc.cpp.in or gen_iface.py instead and regenerate.
172 // END of generated section
173 //----------------------------------------------------------------------
176 // Returns the line number of the line with the caret.
177 int wxStyledTextCtrl::GetCurrentLine() {
178 int line
= LineFromPosition(GetCurrentPos());
183 // Extract style settings from a spec-string which is composed of one or
184 // more of the following comma separated elements:
186 // bold turns on bold
187 // italic turns on italics
188 // fore:#RRGGBB sets the foreground colour
189 // back:#RRGGBB sets the background colour
190 // face:[facename] sets the font face name to use
191 // size:[num] sets the font size in points
192 // eol turns on eol filling
193 // underline turns on underlining
195 void wxStyledTextCtrl::StyleSetSpec(int styleNum
, const wxString
& spec
) {
197 wxStringTokenizer
tkz(spec
, wxT(","));
198 while (tkz
.HasMoreTokens()) {
199 wxString token
= tkz
.GetNextToken();
201 wxString option
= token
.BeforeFirst(':');
202 wxString val
= token
.AfterFirst(':');
204 if (option
== wxT("bold"))
205 StyleSetBold(styleNum
, true);
207 else if (option
== wxT("italic"))
208 StyleSetItalic(styleNum
, true);
210 else if (option
== wxT("underline"))
211 StyleSetUnderline(styleNum
, true);
213 else if (option
== wxT("eol"))
214 StyleSetEOLFilled(styleNum
, true);
216 else if (option
== wxT("size")) {
218 if (val
.ToLong(&points
))
219 StyleSetSize(styleNum
, points
);
222 else if (option
== wxT("face"))
223 StyleSetFaceName(styleNum
, val
);
225 else if (option
== wxT("fore"))
226 StyleSetForeground(styleNum
, wxColourFromSpec(val
));
228 else if (option
== wxT("back"))
229 StyleSetBackground(styleNum
, wxColourFromSpec(val
));
234 // Set style size, face, bold, italic, and underline attributes from
235 // a wxFont's attributes.
236 void wxStyledTextCtrl::StyleSetFont(int styleNum
, wxFont
& font
) {
237 int size
= font
.GetPointSize();
238 wxString faceName
= font
.GetFaceName();
239 bool bold
= font
.GetWeight() == wxBOLD
;
240 bool italic
= font
.GetStyle() != wxNORMAL
;
241 bool under
= font
.GetUnderlined();
243 // TODO: add encoding/charset mapping
244 StyleSetFontAttr(styleNum
, size
, faceName
, bold
, italic
, under
);
247 // Set all font style attributes at once.
248 void wxStyledTextCtrl::StyleSetFontAttr(int styleNum
, int size
,
249 const wxString
& faceName
,
250 bool bold
, bool italic
,
252 StyleSetSize(styleNum
, size
);
253 StyleSetFaceName(styleNum
, faceName
);
254 StyleSetBold(styleNum
, bold
);
255 StyleSetItalic(styleNum
, italic
);
256 StyleSetUnderline(styleNum
, underline
);
258 // TODO: add encoding/charset mapping
262 // Perform one of the operations defined by the wxSTC_CMD_* constants.
263 void wxStyledTextCtrl::CmdKeyExecute(int cmd
) {
268 // Set the left and right margin in the edit area, measured in pixels.
269 void wxStyledTextCtrl::SetMargins(int left
, int right
) {
271 SetMarginRight(right
);
275 // Retrieve the start and end positions of the current selection.
276 void wxStyledTextCtrl::GetSelection(int* startPos
, int* endPos
) {
277 if (startPos
!= NULL
)
278 *startPos
= SendMsg(SCI_GETSELECTIONSTART
);
280 *endPos
= SendMsg(SCI_GETSELECTIONEND
);
284 // Retrieve the point in the window where a position is displayed.
285 wxPoint
wxStyledTextCtrl::PointFromPosition(int pos
) {
286 int x
= SendMsg(SCI_POINTXFROMPOSITION
, 0, pos
);
287 int y
= SendMsg(SCI_POINTYFROMPOSITION
, 0, pos
);
288 return wxPoint(x
, y
);
291 // Scroll enough to make the given line visible
292 void wxStyledTextCtrl::ScrollToLine(int line
) {
293 m_swx
->DoScrollToLine(line
);
297 // Scroll enough to make the given column visible
298 void wxStyledTextCtrl::ScrollToColumn(int column
) {
299 m_swx
->DoScrollToColumn(column
);
304 //----------------------------------------------------------------------
307 void wxStyledTextCtrl::OnPaint(wxPaintEvent
& evt
) {
309 wxRegion region
= GetUpdateRegion();
311 m_swx
->DoPaint(&dc
, region
.GetBox());
314 void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent
& evt
) {
315 if (evt
.GetOrientation() == wxHORIZONTAL
)
316 m_swx
->DoHScroll(evt
.GetEventType(), evt
.GetPosition());
318 m_swx
->DoVScroll(evt
.GetEventType(), evt
.GetPosition());
321 void wxStyledTextCtrl::OnScroll(wxScrollEvent
& evt
) {
322 wxScrollBar
* sb
= wxDynamicCast(evt
.GetEventObject(), wxScrollBar
);
324 if (sb
->IsVertical())
325 m_swx
->DoVScroll(evt
.GetEventType(), evt
.GetPosition());
327 m_swx
->DoHScroll(evt
.GetEventType(), evt
.GetPosition());
331 void wxStyledTextCtrl::OnSize(wxSizeEvent
& evt
) {
332 wxSize sz
= GetClientSize();
333 m_swx
->DoSize(sz
.x
, sz
.y
);
336 void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent
& evt
) {
338 wxPoint pt
= evt
.GetPosition();
339 m_swx
->DoLeftButtonDown(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
340 evt
.ShiftDown(), evt
.ControlDown(), evt
.AltDown());
343 void wxStyledTextCtrl::OnMouseMove(wxMouseEvent
& evt
) {
344 wxPoint pt
= evt
.GetPosition();
345 m_swx
->DoLeftButtonMove(Point(pt
.x
, pt
.y
));
348 void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent
& evt
) {
349 wxPoint pt
= evt
.GetPosition();
350 m_swx
->DoLeftButtonUp(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
355 void wxStyledTextCtrl::OnMouseRightUp(wxMouseEvent
& evt
) {
356 wxPoint pt
= evt
.GetPosition();
357 m_swx
->DoContextMenu(Point(pt
.x
, pt
.y
));
361 void wxStyledTextCtrl::OnMouseMiddleUp(wxMouseEvent
& evt
) {
362 wxPoint pt
= evt
.GetPosition();
363 m_swx
->DoMiddleButtonUp(Point(pt
.x
, pt
.y
));
366 void wxStyledTextCtrl::OnContextMenu(wxContextMenuEvent
& evt
) {
367 wxPoint pt
= evt
.GetPosition();
368 ScreenToClient(&pt
.x
, &pt
.y
);
369 m_swx
->DoContextMenu(Point(pt
.x
, pt
.y
));
373 void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent
& evt
) {
374 m_swx
->DoMouseWheel(evt
.GetWheelRotation(),
376 evt
.GetLinesPerAction(),
382 void wxStyledTextCtrl::OnChar(wxKeyEvent
& evt
) {
383 // On (some?) non-US keyboards the AltGr key is required to enter some
384 // common characters. It comes to us as both Alt and Ctrl down so we need
385 // to let the char through in that case, otherwise if only ctrl or only
386 // alt let's skip it.
387 bool ctrl
= evt
.ControlDown();
388 bool alt
= evt
.AltDown();
389 bool skip
= ((ctrl
|| alt
) && ! (ctrl
&& alt
));
391 int key
= evt
.GetKeyCode();
393 // printf("OnChar key:%%d consumed:%%d ctrl:%%d alt:%%d skip:%%d\n",
394 // key, m_lastKeyDownConsumed, ctrl, alt, skip);
396 if ( (key
<= WXK_START
|| key
> WXK_NUMPAD_DIVIDE
) &&
397 !m_lastKeyDownConsumed
&& !skip
) {
398 m_swx
->DoAddChar(key
);
405 void wxStyledTextCtrl::OnKeyDown(wxKeyEvent
& evt
) {
406 int key
= evt
.GetKeyCode();
407 bool shift
= evt
.ShiftDown(),
408 ctrl
= evt
.ControlDown(),
411 int processed
= m_swx
->DoKeyDown(key
, shift
, ctrl
, alt
, &m_lastKeyDownConsumed
);
413 // printf("KeyDn key:%%d shift:%%d ctrl:%%d alt:%%d processed:%%d consumed:%%d\n",
414 // key, shift, ctrl, alt, processed, m_lastKeyDownConsumed);
416 if (!processed
&& !m_lastKeyDownConsumed
)
421 void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent
& evt
) {
422 m_swx
->DoLoseFocus();
426 void wxStyledTextCtrl::OnGainFocus(wxFocusEvent
& evt
) {
427 m_swx
->DoGainFocus();
431 void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& evt
) {
432 m_swx
->DoSysColourChange();
436 void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent
& evt
) {
437 // do nothing to help avoid flashing
442 void wxStyledTextCtrl::OnMenu(wxCommandEvent
& evt
) {
443 m_swx
->DoCommand(evt
.GetId());
447 void wxStyledTextCtrl::OnListBox(wxCommandEvent
& evt
) {
448 m_swx
->DoOnListBox();
452 //----------------------------------------------------------------------
453 // Turn notifications from Scintilla into events
456 void wxStyledTextCtrl::NotifyChange() {
457 wxStyledTextEvent
evt(wxEVT_STC_CHANGE
, GetId());
458 evt
.SetEventObject(this);
459 GetEventHandler()->ProcessEvent(evt
);
463 static void SetEventText(wxStyledTextEvent
& evt
, const char* text
,
467 // The unicode conversion MUST have a null byte to terminate the
468 // string so move it into a buffer first and give it one.
469 wxMemoryBuffer
buf(length
+1);
470 buf
.AppendData((void*)text
, length
);
472 evt
.SetText(stc2wx(buf
));
476 void wxStyledTextCtrl::NotifyParent(SCNotification
* _scn
) {
477 SCNotification
& scn
= *_scn
;
478 wxStyledTextEvent
evt(0, GetId());
480 evt
.SetEventObject(this);
481 evt
.SetPosition(scn
.position
);
483 evt
.SetModifiers(scn
.modifiers
);
485 switch (scn
.nmhdr
.code
) {
486 case SCN_STYLENEEDED
:
487 evt
.SetEventType(wxEVT_STC_STYLENEEDED
);
491 evt
.SetEventType(wxEVT_STC_CHARADDED
);
494 case SCN_SAVEPOINTREACHED
:
495 evt
.SetEventType(wxEVT_STC_SAVEPOINTREACHED
);
498 case SCN_SAVEPOINTLEFT
:
499 evt
.SetEventType(wxEVT_STC_SAVEPOINTLEFT
);
502 case SCN_MODIFYATTEMPTRO
:
503 evt
.SetEventType(wxEVT_STC_ROMODIFYATTEMPT
);
507 evt
.SetEventType(wxEVT_STC_KEY
);
510 case SCN_DOUBLECLICK
:
511 evt
.SetEventType(wxEVT_STC_DOUBLECLICK
);
515 evt
.SetEventType(wxEVT_STC_UPDATEUI
);
519 evt
.SetEventType(wxEVT_STC_MODIFIED
);
520 evt
.SetModificationType(scn
.modificationType
);
521 SetEventText(evt
, scn
.text
, scn
.length
);
522 evt
.SetLength(scn
.length
);
523 evt
.SetLinesAdded(scn
.linesAdded
);
524 evt
.SetLine(scn
.line
);
525 evt
.SetFoldLevelNow(scn
.foldLevelNow
);
526 evt
.SetFoldLevelPrev(scn
.foldLevelPrev
);
529 case SCN_MACRORECORD
:
530 evt
.SetEventType(wxEVT_STC_MACRORECORD
);
531 evt
.SetMessage(scn
.message
);
532 evt
.SetWParam(scn
.wParam
);
533 evt
.SetLParam(scn
.lParam
);
536 case SCN_MARGINCLICK
:
537 evt
.SetEventType(wxEVT_STC_MARGINCLICK
);
538 evt
.SetMargin(scn
.margin
);
542 evt
.SetEventType(wxEVT_STC_NEEDSHOWN
);
543 evt
.SetLength(scn
.length
);
547 evt
.SetEventType(wxEVT_STC_PAINTED
);
550 case SCN_USERLISTSELECTION
:
551 evt
.SetEventType(wxEVT_STC_USERLISTSELECTION
);
552 evt
.SetListType(scn
.listType
);
553 SetEventText(evt
, scn
.text
, strlen(scn
.text
));
557 evt
.SetEventType(wxEVT_STC_URIDROPPED
);
558 SetEventText(evt
, scn
.text
, strlen(scn
.text
));
562 evt
.SetEventType(wxEVT_STC_DWELLSTART
);
568 evt
.SetEventType(wxEVT_STC_DWELLEND
);
574 evt
.SetEventType(wxEVT_STC_ZOOM
);
581 GetEventHandler()->ProcessEvent(evt
);
585 //----------------------------------------------------------------------
586 //----------------------------------------------------------------------
587 //----------------------------------------------------------------------
589 wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType
, int id
)
590 : wxCommandEvent(commandType
, id
)
595 m_modificationType
= 0;
608 m_dragAllowMove
= FALSE
;
609 #if wxUSE_DRAG_AND_DROP
610 m_dragResult
= wxDragNone
;
614 bool wxStyledTextEvent::GetShift() const { return (m_modifiers
& SCI_SHIFT
) != 0; }
615 bool wxStyledTextEvent::GetControl() const { return (m_modifiers
& SCI_CTRL
) != 0; }
616 bool wxStyledTextEvent::GetAlt() const { return (m_modifiers
& SCI_ALT
) != 0; }
619 wxStyledTextEvent::wxStyledTextEvent(const wxStyledTextEvent
& event
):
620 wxCommandEvent(event
)
622 m_position
= event
.m_position
;
624 m_modifiers
= event
.m_modifiers
;
625 m_modificationType
= event
.m_modificationType
;
626 m_text
= event
.m_text
;
627 m_length
= event
.m_length
;
628 m_linesAdded
= event
.m_linesAdded
;
629 m_line
= event
.m_line
;
630 m_foldLevelNow
= event
.m_foldLevelNow
;
631 m_foldLevelPrev
= event
.m_foldLevelPrev
;
633 m_margin
= event
.m_margin
;
635 m_message
= event
.m_message
;
636 m_wParam
= event
.m_wParam
;
637 m_lParam
= event
.m_lParam
;
639 m_listType
= event
.m_listType
;
643 m_dragText
= event
.m_dragText
;
644 m_dragAllowMove
=event
.m_dragAllowMove
;
645 #if wxUSE_DRAG_AND_DROP
646 m_dragResult
= event
.m_dragResult
;
650 //----------------------------------------------------------------------
651 //----------------------------------------------------------------------