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 #if defined(__WXMSW__) || defined(__WXMAC__)
94 // Let Scintilla see the double click as a second click
95 EVT_LEFT_DCLICK (wxStyledTextCtrl::OnMouseLeftDown
)
97 EVT_MOTION (wxStyledTextCtrl::OnMouseMove
)
98 EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp
)
99 #if defined(__WXGTK__) || defined(__WXMAC__)
100 EVT_RIGHT_UP (wxStyledTextCtrl::OnMouseRightUp
)
102 EVT_CONTEXT_MENU (wxStyledTextCtrl::OnContextMenu
)
104 EVT_MOUSEWHEEL (wxStyledTextCtrl::OnMouseWheel
)
105 EVT_CHAR (wxStyledTextCtrl::OnChar
)
106 EVT_KEY_DOWN (wxStyledTextCtrl::OnKeyDown
)
107 EVT_KILL_FOCUS (wxStyledTextCtrl::OnLoseFocus
)
108 EVT_SET_FOCUS (wxStyledTextCtrl::OnGainFocus
)
109 EVT_SYS_COLOUR_CHANGED (wxStyledTextCtrl::OnSysColourChanged
)
110 EVT_ERASE_BACKGROUND (wxStyledTextCtrl::OnEraseBackground
)
111 EVT_MENU_RANGE (10, 16, wxStyledTextCtrl::OnMenu
)
112 EVT_LISTBOX_DCLICK (-1, wxStyledTextCtrl::OnListBox
)
116 IMPLEMENT_CLASS(wxStyledTextCtrl
, wxControl
)
117 IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent
, wxCommandEvent
)
120 // forces the linking of the lexer modules
121 int Scintilla_LinkLexers();
124 //----------------------------------------------------------------------
125 // Constructor and Destructor
127 wxStyledTextCtrl::wxStyledTextCtrl(wxWindow
*parent
,
132 const wxString
& name
) :
133 wxControl(parent
, id
, pos
, size
,
134 style
| wxVSCROLL
| wxHSCROLL
| wxWANTS_CHARS
| wxCLIP_CHILDREN
,
135 wxDefaultValidator
, name
)
138 Scintilla_LinkLexers();
140 m_swx
= new ScintillaWX(this);
142 m_lastKeyDownConsumed
= FALSE
;
146 // Put Scintilla into unicode (UTF-8) mode
147 SetCodePage(wxSTC_CP_UTF8
);
152 wxStyledTextCtrl::~wxStyledTextCtrl() {
157 //----------------------------------------------------------------------
159 long wxStyledTextCtrl::SendMsg(int msg
, long wp
, long lp
) {
161 return m_swx
->WndProc(msg
, wp
, lp
);
166 //----------------------------------------------------------------------
167 // BEGIN generated section. The following code is automatically generated
168 // by gen_iface.py from the contents of Scintilla.iface. Do not edit
169 // this file. Edit stc.cpp.in or gen_iface.py instead and regenerate.
173 // END of generated section
174 //----------------------------------------------------------------------
177 // Returns the line number of the line with the caret.
178 int wxStyledTextCtrl::GetCurrentLine() {
179 int line
= LineFromPosition(GetCurrentPos());
184 // Extract style settings from a spec-string which is composed of one or
185 // more of the following comma separated elements:
187 // bold turns on bold
188 // italic turns on italics
189 // fore:#RRGGBB sets the foreground colour
190 // back:#RRGGBB sets the background colour
191 // face:[facename] sets the font face name to use
192 // size:[num] sets the font size in points
193 // eol turns on eol filling
194 // underline turns on underlining
196 void wxStyledTextCtrl::StyleSetSpec(int styleNum
, const wxString
& spec
) {
198 wxStringTokenizer
tkz(spec
, wxT(","));
199 while (tkz
.HasMoreTokens()) {
200 wxString token
= tkz
.GetNextToken();
202 wxString option
= token
.BeforeFirst(':');
203 wxString val
= token
.AfterFirst(':');
205 if (option
== wxT("bold"))
206 StyleSetBold(styleNum
, true);
208 else if (option
== wxT("italic"))
209 StyleSetItalic(styleNum
, true);
211 else if (option
== wxT("underline"))
212 StyleSetUnderline(styleNum
, true);
214 else if (option
== wxT("eol"))
215 StyleSetEOLFilled(styleNum
, true);
217 else if (option
== wxT("size")) {
219 if (val
.ToLong(&points
))
220 StyleSetSize(styleNum
, points
);
223 else if (option
== wxT("face"))
224 StyleSetFaceName(styleNum
, val
);
226 else if (option
== wxT("fore"))
227 StyleSetForeground(styleNum
, wxColourFromSpec(val
));
229 else if (option
== wxT("back"))
230 StyleSetBackground(styleNum
, wxColourFromSpec(val
));
235 // Set style size, face, bold, italic, and underline attributes from
236 // a wxFont's attributes.
237 void wxStyledTextCtrl::StyleSetFont(int styleNum
, wxFont
& font
) {
238 int size
= font
.GetPointSize();
239 wxString faceName
= font
.GetFaceName();
240 bool bold
= font
.GetWeight() == wxBOLD
;
241 bool italic
= font
.GetStyle() != wxNORMAL
;
242 bool under
= font
.GetUnderlined();
244 // TODO: add encoding/charset mapping
245 StyleSetFontAttr(styleNum
, size
, faceName
, bold
, italic
, under
);
248 // Set all font style attributes at once.
249 void wxStyledTextCtrl::StyleSetFontAttr(int styleNum
, int size
,
250 const wxString
& faceName
,
251 bool bold
, bool italic
,
253 StyleSetSize(styleNum
, size
);
254 StyleSetFaceName(styleNum
, faceName
);
255 StyleSetBold(styleNum
, bold
);
256 StyleSetItalic(styleNum
, italic
);
257 StyleSetUnderline(styleNum
, underline
);
259 // TODO: add encoding/charset mapping
263 // Perform one of the operations defined by the wxSTC_CMD_* constants.
264 void wxStyledTextCtrl::CmdKeyExecute(int cmd
) {
269 // Set the left and right margin in the edit area, measured in pixels.
270 void wxStyledTextCtrl::SetMargins(int left
, int right
) {
272 SetMarginRight(right
);
276 // Retrieve the start and end positions of the current selection.
277 void wxStyledTextCtrl::GetSelection(int* startPos
, int* endPos
) {
278 if (startPos
!= NULL
)
279 *startPos
= SendMsg(SCI_GETSELECTIONSTART
);
281 *endPos
= SendMsg(SCI_GETSELECTIONEND
);
285 // Retrieve the point in the window where a position is displayed.
286 wxPoint
wxStyledTextCtrl::PointFromPosition(int pos
) {
287 int x
= SendMsg(SCI_POINTXFROMPOSITION
, 0, pos
);
288 int y
= SendMsg(SCI_POINTYFROMPOSITION
, 0, pos
);
289 return wxPoint(x
, y
);
292 // Scroll enough to make the given line visible
293 void wxStyledTextCtrl::ScrollToLine(int line
) {
294 m_swx
->DoScrollToLine(line
);
298 // Scroll enough to make the given column visible
299 void wxStyledTextCtrl::ScrollToColumn(int column
) {
300 m_swx
->DoScrollToColumn(column
);
305 //----------------------------------------------------------------------
308 void wxStyledTextCtrl::OnPaint(wxPaintEvent
& evt
) {
310 wxRegion region
= GetUpdateRegion();
312 m_swx
->DoPaint(&dc
, region
.GetBox());
315 void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent
& evt
) {
316 if (evt
.GetOrientation() == wxHORIZONTAL
)
317 m_swx
->DoHScroll(evt
.GetEventType(), evt
.GetPosition());
319 m_swx
->DoVScroll(evt
.GetEventType(), evt
.GetPosition());
322 void wxStyledTextCtrl::OnScroll(wxScrollEvent
& evt
) {
323 wxScrollBar
* sb
= wxDynamicCast(evt
.GetEventObject(), wxScrollBar
);
325 if (sb
->IsVertical())
326 m_swx
->DoVScroll(evt
.GetEventType(), evt
.GetPosition());
328 m_swx
->DoHScroll(evt
.GetEventType(), evt
.GetPosition());
332 void wxStyledTextCtrl::OnSize(wxSizeEvent
& evt
) {
333 wxSize sz
= GetClientSize();
334 m_swx
->DoSize(sz
.x
, sz
.y
);
337 void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent
& evt
) {
339 wxPoint pt
= evt
.GetPosition();
340 m_swx
->DoButtonDown(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
341 evt
.ShiftDown(), evt
.ControlDown(), evt
.AltDown());
344 void wxStyledTextCtrl::OnMouseMove(wxMouseEvent
& evt
) {
345 wxPoint pt
= evt
.GetPosition();
346 m_swx
->DoButtonMove(Point(pt
.x
, pt
.y
));
349 void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent
& evt
) {
350 wxPoint pt
= evt
.GetPosition();
351 m_swx
->DoButtonUp(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
356 void wxStyledTextCtrl::OnMouseRightUp(wxMouseEvent
& evt
) {
357 wxPoint pt
= evt
.GetPosition();
358 m_swx
->DoContextMenu(Point(pt
.x
, pt
.y
));
362 void wxStyledTextCtrl::OnContextMenu(wxContextMenuEvent
& evt
) {
363 wxPoint pt
= evt
.GetPosition();
364 ScreenToClient(&pt
.x
, &pt
.y
);
365 m_swx
->DoContextMenu(Point(pt
.x
, pt
.y
));
369 void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent
& evt
) {
370 m_swx
->DoMouseWheel(evt
.GetWheelRotation(),
372 evt
.GetLinesPerAction(),
378 void wxStyledTextCtrl::OnChar(wxKeyEvent
& evt
) {
379 int key
= evt
.GetKeyCode();
381 // On (some?) non-US keyboards the AltGr key is required to enter some
382 // common characters. It comes to us as both Alt and Ctrl down so we need
383 // to let the char through in that case, otherwise if only ctrl or only
384 // alt let's skip it.
385 bool ctrl
= evt
.ControlDown();
386 bool alt
= evt
.AltDown();
387 bool skip
= ((ctrl
|| alt
) && ! (ctrl
&& alt
));
389 // printf("OnChar key:%%d consumed:%%d ctrl:%%d alt:%%d skip:%%d\n",
390 // key, m_lastKeyDownConsumed, ctrl, alt, skip);
392 if (key
<= WXK_START
&& /*key >= 32 &&*/ !m_lastKeyDownConsumed
&& !skip
) {
393 m_swx
->DoAddChar(key
);
400 void wxStyledTextCtrl::OnKeyDown(wxKeyEvent
& evt
) {
401 int key
= evt
.GetKeyCode();
402 bool shift
= evt
.ShiftDown(),
403 ctrl
= evt
.ControlDown(),
406 int processed
= m_swx
->DoKeyDown(key
, shift
, ctrl
, alt
, &m_lastKeyDownConsumed
);
408 // printf("KeyDn key:%%d shift:%%d ctrl:%%d alt:%%d processed:%%d consumed:%%d\n",
409 // key, shift, ctrl, alt, processed, m_lastKeyDownConsumed);
411 if (!processed
&& !m_lastKeyDownConsumed
)
416 void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent
& evt
) {
417 m_swx
->DoLoseFocus();
421 void wxStyledTextCtrl::OnGainFocus(wxFocusEvent
& evt
) {
422 m_swx
->DoGainFocus();
426 void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& evt
) {
427 m_swx
->DoSysColourChange();
431 void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent
& evt
) {
432 // do nothing to help avoid flashing
437 void wxStyledTextCtrl::OnMenu(wxCommandEvent
& evt
) {
438 m_swx
->DoCommand(evt
.GetId());
442 void wxStyledTextCtrl::OnListBox(wxCommandEvent
& evt
) {
443 m_swx
->DoOnListBox();
447 //----------------------------------------------------------------------
448 // Turn notifications from Scintilla into events
451 void wxStyledTextCtrl::NotifyChange() {
452 wxStyledTextEvent
evt(wxEVT_STC_CHANGE
, GetId());
453 evt
.SetEventObject(this);
454 GetEventHandler()->ProcessEvent(evt
);
457 void wxStyledTextCtrl::NotifyParent(SCNotification
* _scn
) {
458 SCNotification
& scn
= *_scn
;
459 wxStyledTextEvent
evt(0, GetId());
461 evt
.SetEventObject(this);
462 evt
.SetPosition(scn
.position
);
464 evt
.SetModifiers(scn
.modifiers
);
466 switch (scn
.nmhdr
.code
) {
467 case SCN_STYLENEEDED
:
468 evt
.SetEventType(wxEVT_STC_STYLENEEDED
);
472 evt
.SetEventType(wxEVT_STC_CHARADDED
);
475 case SCN_SAVEPOINTREACHED
:
476 evt
.SetEventType(wxEVT_STC_SAVEPOINTREACHED
);
479 case SCN_SAVEPOINTLEFT
:
480 evt
.SetEventType(wxEVT_STC_SAVEPOINTLEFT
);
483 case SCN_MODIFYATTEMPTRO
:
484 evt
.SetEventType(wxEVT_STC_ROMODIFYATTEMPT
);
488 evt
.SetEventType(wxEVT_STC_KEY
);
491 case SCN_DOUBLECLICK
:
492 evt
.SetEventType(wxEVT_STC_DOUBLECLICK
);
496 evt
.SetEventType(wxEVT_STC_UPDATEUI
);
500 evt
.SetEventType(wxEVT_STC_MODIFIED
);
501 evt
.SetModificationType(scn
.modificationType
);
503 // The unicode conversion MUST have a null byte to terminate the
504 // string so move it into a buffer first and give it one.
505 wxMemoryBuffer
buf(scn
.length
+1);
506 buf
.AppendData((void*)scn
.text
, scn
.length
);
508 evt
.SetText(stc2wx(buf
));
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_PAINTED
);
538 case SCN_USERLISTSELECTION
:
539 evt
.SetEventType(wxEVT_STC_USERLISTSELECTION
);
540 evt
.SetListType(scn
.listType
);
541 evt
.SetText(scn
.text
);
545 evt
.SetEventType(wxEVT_STC_URIDROPPED
);
546 evt
.SetText(scn
.text
);
550 evt
.SetEventType(wxEVT_STC_DWELLSTART
);
556 evt
.SetEventType(wxEVT_STC_DWELLEND
);
562 evt
.SetEventType(wxEVT_STC_ZOOM
);
569 GetEventHandler()->ProcessEvent(evt
);
573 //----------------------------------------------------------------------
574 //----------------------------------------------------------------------
575 //----------------------------------------------------------------------
577 wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType
, int id
)
578 : wxCommandEvent(commandType
, id
)
583 m_modificationType
= 0;
596 m_dragAllowMove
= FALSE
;
597 #if wxUSE_DRAG_AND_DROP
598 m_dragResult
= wxDragNone
;
602 bool wxStyledTextEvent::GetShift() const { return (m_modifiers
& SCI_SHIFT
) != 0; }
603 bool wxStyledTextEvent::GetControl() const { return (m_modifiers
& SCI_CTRL
) != 0; }
604 bool wxStyledTextEvent::GetAlt() const { return (m_modifiers
& SCI_ALT
) != 0; }
607 wxStyledTextEvent::wxStyledTextEvent(const wxStyledTextEvent
& event
):
608 wxCommandEvent(event
)
610 m_position
= event
.m_position
;
612 m_modifiers
= event
.m_modifiers
;
613 m_modificationType
= event
.m_modificationType
;
614 m_text
= event
.m_text
;
615 m_length
= event
.m_length
;
616 m_linesAdded
= event
.m_linesAdded
;
617 m_line
= event
.m_line
;
618 m_foldLevelNow
= event
.m_foldLevelNow
;
619 m_foldLevelPrev
= event
.m_foldLevelPrev
;
621 m_margin
= event
.m_margin
;
623 m_message
= event
.m_message
;
624 m_wParam
= event
.m_wParam
;
625 m_lParam
= event
.m_lParam
;
627 m_listType
= event
.m_listType
;
631 m_dragText
= event
.m_dragText
;
632 m_dragAllowMove
=event
.m_dragAllowMove
;
633 #if wxUSE_DRAG_AND_DROP
634 m_dragResult
= event
.m_dragResult
;
638 //----------------------------------------------------------------------
639 //----------------------------------------------------------------------