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
= "stcwindow";
30 DEFINE_EVENT_TYPE( wxEVT_STC_CHANGE
)
31 DEFINE_EVENT_TYPE( wxEVT_STC_STYLENEEDED
)
32 DEFINE_EVENT_TYPE( wxEVT_STC_CHARADDED
)
33 DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTREACHED
)
34 DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTLEFT
)
35 DEFINE_EVENT_TYPE( wxEVT_STC_ROMODIFYATTEMPT
)
36 DEFINE_EVENT_TYPE( wxEVT_STC_KEY
)
37 DEFINE_EVENT_TYPE( wxEVT_STC_DOUBLECLICK
)
38 DEFINE_EVENT_TYPE( wxEVT_STC_UPDATEUI
)
39 DEFINE_EVENT_TYPE( wxEVT_STC_MODIFIED
)
40 DEFINE_EVENT_TYPE( wxEVT_STC_MACRORECORD
)
41 DEFINE_EVENT_TYPE( wxEVT_STC_MARGINCLICK
)
42 DEFINE_EVENT_TYPE( wxEVT_STC_NEEDSHOWN
)
43 DEFINE_EVENT_TYPE( wxEVT_STC_POSCHANGED
)
44 DEFINE_EVENT_TYPE( wxEVT_STC_PAINTED
)
45 DEFINE_EVENT_TYPE( wxEVT_STC_USERLISTSELECTION
)
46 DEFINE_EVENT_TYPE( wxEVT_STC_URIDROPPED
)
47 DEFINE_EVENT_TYPE( wxEVT_STC_DWELLSTART
)
48 DEFINE_EVENT_TYPE( wxEVT_STC_DWELLEND
)
49 DEFINE_EVENT_TYPE( wxEVT_STC_START_DRAG
)
50 DEFINE_EVENT_TYPE( wxEVT_STC_DRAG_OVER
)
51 DEFINE_EVENT_TYPE( wxEVT_STC_DO_DROP
)
54 BEGIN_EVENT_TABLE(wxStyledTextCtrl
, wxControl
)
55 EVT_PAINT (wxStyledTextCtrl::OnPaint
)
56 EVT_SCROLLWIN (wxStyledTextCtrl::OnScrollWin
)
57 EVT_SCROLL (wxStyledTextCtrl::OnScroll
)
58 EVT_SIZE (wxStyledTextCtrl::OnSize
)
59 EVT_LEFT_DOWN (wxStyledTextCtrl::OnMouseLeftDown
)
61 // Let Scintilla see the double click as a second click
62 EVT_LEFT_DCLICK (wxStyledTextCtrl::OnMouseLeftDown
)
64 EVT_MOTION (wxStyledTextCtrl::OnMouseMove
)
65 EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp
)
67 EVT_RIGHT_UP (wxStyledTextCtrl::OnMouseRightUp
)
69 EVT_CONTEXT_MENU (wxStyledTextCtrl::OnContextMenu
)
71 EVT_MOUSEWHEEL (wxStyledTextCtrl::OnMouseWheel
)
72 EVT_CHAR (wxStyledTextCtrl::OnChar
)
73 EVT_KEY_DOWN (wxStyledTextCtrl::OnKeyDown
)
74 EVT_KILL_FOCUS (wxStyledTextCtrl::OnLoseFocus
)
75 EVT_SET_FOCUS (wxStyledTextCtrl::OnGainFocus
)
76 EVT_SYS_COLOUR_CHANGED (wxStyledTextCtrl::OnSysColourChanged
)
77 EVT_ERASE_BACKGROUND (wxStyledTextCtrl::OnEraseBackground
)
78 EVT_MENU_RANGE (-1, -1, wxStyledTextCtrl::OnMenu
)
79 EVT_LISTBOX_DCLICK (-1, wxStyledTextCtrl::OnListBox
)
83 IMPLEMENT_CLASS(wxStyledTextCtrl
, wxControl
)
84 IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent
, wxCommandEvent
)
86 // forces the linking of the lexer modules
87 int Scintilla_LinkLexers();
89 //----------------------------------------------------------------------
90 // Constructor and Destructor
92 wxStyledTextCtrl::wxStyledTextCtrl(wxWindow
*parent
,
97 const wxString
& name
) :
98 wxControl(parent
, id
, pos
, size
,
99 style
| wxVSCROLL
| wxHSCROLL
| wxWANTS_CHARS
| wxCLIP_CHILDREN
,
100 wxDefaultValidator
, name
)
102 Scintilla_LinkLexers();
103 m_swx
= new ScintillaWX(this);
105 m_lastKeyDownConsumed
= FALSE
;
111 wxStyledTextCtrl::~wxStyledTextCtrl() {
116 //----------------------------------------------------------------------
118 long wxStyledTextCtrl::SendMsg(int msg
, long wp
, long lp
) {
120 return m_swx
->WndProc(msg
, wp
, lp
);
128 #define MAKELONG(a, b) ((a) | ((b) << 16))
131 static long wxColourAsLong(const wxColour
& co
) {
132 return (((long)co
.Blue() << 16) |
133 ((long)co
.Green() << 8) |
137 static wxColour
wxColourFromLong(long c
) {
139 clr
.Set(c
& 0xff, (c
>> 8) & 0xff, (c
>> 16) & 0xff);
144 static wxColour
wxColourFromSpec(const wxString
& spec
) {
145 // spec should be #RRGGBB
147 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
148 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
149 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
150 return wxColour(red
, green
, blue
);
154 //----------------------------------------------------------------------
155 // BEGIN generated section. The following code is automatically generated
156 // by gen_iface.py from the contents of Scintilla.iface. Do not edit
157 // this file. Edit stc.cpp.in or gen_iface.py instead and regenerate.
161 // END of generated section
162 //----------------------------------------------------------------------
165 // Returns the line number of the line with the caret.
166 int wxStyledTextCtrl::GetCurrentLine() {
167 int line
= LineFromPosition(GetCurrentPos());
172 // Extract style settings from a spec-string which is composed of one or
173 // more of the following comma separated elements:
175 // bold turns on bold
176 // italic turns on italics
177 // fore:#RRGGBB sets the foreground colour
178 // back:#RRGGBB sets the background colour
179 // face:[facename] sets the font face name to use
180 // size:[num] sets the font size in points
181 // eol turns on eol filling
182 // underline turns on underlining
184 void wxStyledTextCtrl::StyleSetSpec(int styleNum
, const wxString
& spec
) {
186 wxStringTokenizer
tkz(spec
, ",");
187 while (tkz
.HasMoreTokens()) {
188 wxString token
= tkz
.GetNextToken();
190 wxString option
= token
.BeforeFirst(':');
191 wxString val
= token
.AfterFirst(':');
193 if (option
== "bold")
194 StyleSetBold(styleNum
, true);
196 else if (option
== "italic")
197 StyleSetItalic(styleNum
, true);
199 else if (option
== "underline")
200 StyleSetUnderline(styleNum
, true);
202 else if (option
== "eol")
203 StyleSetEOLFilled(styleNum
, true);
205 else if (option
== "size") {
207 if (val
.ToLong(&points
))
208 StyleSetSize(styleNum
, points
);
211 else if (option
== "face")
212 StyleSetFaceName(styleNum
, val
);
214 else if (option
== "fore")
215 StyleSetForeground(styleNum
, wxColourFromSpec(val
));
217 else if (option
== "back")
218 StyleSetBackground(styleNum
, wxColourFromSpec(val
));
223 // Set style size, face, bold, italic, and underline attributes from
224 // a wxFont's attributes.
225 void wxStyledTextCtrl::StyleSetFont(int styleNum
, wxFont
& font
) {
226 int size
= font
.GetPointSize();
227 wxString faceName
= font
.GetFaceName();
228 bool bold
= font
.GetWeight() == wxBOLD
;
229 bool italic
= font
.GetStyle() != wxNORMAL
;
230 bool under
= font
.GetUnderlined();
232 // TODO: add encoding/charset mapping
233 StyleSetFontAttr(styleNum
, size
, faceName
, bold
, italic
, under
);
236 // Set all font style attributes at once.
237 void wxStyledTextCtrl::StyleSetFontAttr(int styleNum
, int size
,
238 const wxString
& faceName
,
239 bool bold
, bool italic
,
241 StyleSetSize(styleNum
, size
);
242 StyleSetFaceName(styleNum
, faceName
);
243 StyleSetBold(styleNum
, bold
);
244 StyleSetItalic(styleNum
, italic
);
245 StyleSetUnderline(styleNum
, underline
);
247 // TODO: add encoding/charset mapping
251 // Perform one of the operations defined by the wxSTC_CMD_* constants.
252 void wxStyledTextCtrl::CmdKeyExecute(int cmd
) {
257 // Set the left and right margin in the edit area, measured in pixels.
258 void wxStyledTextCtrl::SetMargins(int left
, int right
) {
260 SetMarginRight(right
);
264 // Retrieve the start and end positions of the current selection.
265 void wxStyledTextCtrl::GetSelection(int* startPos
, int* endPos
) {
266 if (startPos
!= NULL
)
267 *startPos
= SendMsg(SCI_GETSELECTIONSTART
);
269 *endPos
= SendMsg(SCI_GETSELECTIONEND
);
273 // Retrieve the point in the window where a position is displayed.
274 wxPoint
wxStyledTextCtrl::PointFromPosition(int pos
) {
275 int x
= SendMsg(SCI_POINTXFROMPOSITION
, 0, pos
);
276 int y
= SendMsg(SCI_POINTYFROMPOSITION
, 0, pos
);
277 return wxPoint(x
, y
);
280 // Scroll enough to make the given line visible
281 void wxStyledTextCtrl::ScrollToLine(int line
) {
282 m_swx
->DoScrollToLine(line
);
286 // Scroll enough to make the given column visible
287 void wxStyledTextCtrl::ScrollToColumn(int column
) {
288 m_swx
->DoScrollToColumn(column
);
293 //----------------------------------------------------------------------
296 void wxStyledTextCtrl::OnPaint(wxPaintEvent
& evt
) {
298 wxRegion region
= GetUpdateRegion();
300 m_swx
->DoPaint(&dc
, region
.GetBox());
303 void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent
& evt
) {
304 if (evt
.GetOrientation() == wxHORIZONTAL
)
305 m_swx
->DoHScroll(evt
.GetEventType(), evt
.GetPosition());
307 m_swx
->DoVScroll(evt
.GetEventType(), evt
.GetPosition());
310 void wxStyledTextCtrl::OnScroll(wxScrollEvent
& evt
) {
311 wxScrollBar
* sb
= wxDynamicCast(evt
.GetEventObject(), wxScrollBar
);
313 if (sb
->IsVertical())
314 m_swx
->DoVScroll(evt
.GetEventType(), evt
.GetPosition());
316 m_swx
->DoHScroll(evt
.GetEventType(), evt
.GetPosition());
320 void wxStyledTextCtrl::OnSize(wxSizeEvent
& evt
) {
321 wxSize sz
= GetClientSize();
322 m_swx
->DoSize(sz
.x
, sz
.y
);
325 void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent
& evt
) {
326 wxPoint pt
= evt
.GetPosition();
327 m_swx
->DoButtonDown(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
328 evt
.ShiftDown(), evt
.ControlDown(), evt
.AltDown());
331 void wxStyledTextCtrl::OnMouseMove(wxMouseEvent
& evt
) {
332 wxPoint pt
= evt
.GetPosition();
333 m_swx
->DoButtonMove(Point(pt
.x
, pt
.y
));
336 void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent
& evt
) {
337 wxPoint pt
= evt
.GetPosition();
338 m_swx
->DoButtonUp(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
343 void wxStyledTextCtrl::OnMouseRightUp(wxMouseEvent
& evt
) {
344 wxPoint pt
= evt
.GetPosition();
345 m_swx
->DoContextMenu(Point(pt
.x
, pt
.y
));
349 void wxStyledTextCtrl::OnContextMenu(wxContextMenuEvent
& evt
) {
350 wxPoint pt
= evt
.GetPosition();
351 ScreenToClient(&pt
.x
, &pt
.y
);
352 m_swx
->DoContextMenu(Point(pt
.x
, pt
.y
));
356 void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent
& evt
) {
357 m_swx
->DoMouseWheel(evt
.GetWheelRotation(),
359 evt
.GetLinesPerAction(),
364 void wxStyledTextCtrl::OnChar(wxKeyEvent
& evt
) {
365 long key
= evt
.KeyCode();
367 // printf("OnChar key:%%d consumed:%%d ctrl:%%d alt:%%d\n",
368 // key, m_lastKeyDownConsumed, evt.ControlDown(), evt.AltDown());
371 // \|@#¬[]{}?£$~ ã,õ,Ã,Õ, ñ, Ñ
373 // On (some?) non-US keyboards the AltGr key is required to enter some
374 // common characters. It comes to us as both Alt and Ctrl down so we need
375 // to let the char through in that case, otherwise if only ctrl or only
376 // alt let's skip it.
377 bool ctrl
= evt
.ControlDown();
378 bool alt
= evt
.AltDown();
379 bool skip
= ((ctrl
|| alt
) && ! (ctrl
&& alt
));
381 if (key
<= 0xff && key
>= 32 && !m_lastKeyDownConsumed
&& !skip
) {
382 m_swx
->DoAddChar(key
);
389 void wxStyledTextCtrl::OnKeyDown(wxKeyEvent
& evt
) {
390 long key
= evt
.KeyCode();
391 bool shift
= evt
.ShiftDown(),
392 ctrl
= evt
.ControlDown(),
395 int processed
= m_swx
->DoKeyDown(key
, shift
, ctrl
, alt
, &m_lastKeyDownConsumed
);
397 // printf("key: %%d shift: %%d ctrl: %%d alt: %%d processed: %%d consumed: %%d\n",
398 // key, shift, ctrl, alt, processed, m_lastKeyDownConsumed);
400 if (!processed
&& !m_lastKeyDownConsumed
)
405 void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent
& evt
) {
406 m_swx
->DoLoseFocus();
410 void wxStyledTextCtrl::OnGainFocus(wxFocusEvent
& evt
) {
411 m_swx
->DoGainFocus();
415 void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& evt
) {
416 m_swx
->DoSysColourChange();
420 void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent
& evt
) {
421 // do nothing to help avoid flashing
426 void wxStyledTextCtrl::OnMenu(wxCommandEvent
& evt
) {
427 m_swx
->DoCommand(evt
.GetId());
431 void wxStyledTextCtrl::OnListBox(wxCommandEvent
& evt
) {
432 m_swx
->DoOnListBox();
436 //----------------------------------------------------------------------
437 // Turn notifications from Scintilla into events
440 void wxStyledTextCtrl::NotifyChange() {
441 wxStyledTextEvent
evt(wxEVT_STC_CHANGE
, GetId());
442 evt
.SetEventObject(this);
443 GetEventHandler()->ProcessEvent(evt
);
446 void wxStyledTextCtrl::NotifyParent(SCNotification
* _scn
) {
447 SCNotification
& scn
= *_scn
;
448 wxStyledTextEvent
evt(0, GetId());
450 evt
.SetEventObject(this);
451 evt
.SetPosition(scn
.position
);
453 evt
.SetModifiers(scn
.modifiers
);
455 switch (scn
.nmhdr
.code
) {
456 case SCN_STYLENEEDED
:
457 evt
.SetEventType(wxEVT_STC_STYLENEEDED
);
461 evt
.SetEventType(wxEVT_STC_CHARADDED
);
464 case SCN_SAVEPOINTREACHED
:
465 evt
.SetEventType(wxEVT_STC_SAVEPOINTREACHED
);
468 case SCN_SAVEPOINTLEFT
:
469 evt
.SetEventType(wxEVT_STC_SAVEPOINTLEFT
);
472 case SCN_MODIFYATTEMPTRO
:
473 evt
.SetEventType(wxEVT_STC_ROMODIFYATTEMPT
);
477 evt
.SetEventType(wxEVT_STC_KEY
);
480 case SCN_DOUBLECLICK
:
481 evt
.SetEventType(wxEVT_STC_DOUBLECLICK
);
485 evt
.SetEventType(wxEVT_STC_UPDATEUI
);
489 evt
.SetEventType(wxEVT_STC_MODIFIED
);
490 evt
.SetModificationType(scn
.modificationType
);
492 evt
.SetText(wxString(scn
.text
, scn
.length
));
493 evt
.SetLength(scn
.length
);
494 evt
.SetLinesAdded(scn
.linesAdded
);
495 evt
.SetLine(scn
.line
);
496 evt
.SetFoldLevelNow(scn
.foldLevelNow
);
497 evt
.SetFoldLevelPrev(scn
.foldLevelPrev
);
500 case SCN_MACRORECORD
:
501 evt
.SetEventType(wxEVT_STC_MACRORECORD
);
502 evt
.SetMessage(scn
.message
);
503 evt
.SetWParam(scn
.wParam
);
504 evt
.SetLParam(scn
.lParam
);
507 case SCN_MARGINCLICK
:
508 evt
.SetEventType(wxEVT_STC_MARGINCLICK
);
509 evt
.SetMargin(scn
.margin
);
513 evt
.SetEventType(wxEVT_STC_NEEDSHOWN
);
514 evt
.SetLength(scn
.length
);
518 evt
.SetEventType(wxEVT_STC_PAINTED
);
521 case SCN_USERLISTSELECTION
:
522 evt
.SetEventType(wxEVT_STC_USERLISTSELECTION
);
523 evt
.SetListType(scn
.listType
);
524 evt
.SetText(scn
.text
);
528 evt
.SetEventType(wxEVT_STC_URIDROPPED
);
529 evt
.SetText(scn
.text
);
533 evt
.SetEventType(wxEVT_STC_DWELLSTART
);
539 evt
.SetEventType(wxEVT_STC_DWELLEND
);
548 GetEventHandler()->ProcessEvent(evt
);
552 //----------------------------------------------------------------------
553 //----------------------------------------------------------------------
554 //----------------------------------------------------------------------
556 wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType
, int id
)
557 : wxCommandEvent(commandType
, id
)
562 m_modificationType
= 0;
575 m_dragAllowMove
= FALSE
;
576 m_dragResult
= wxDragNone
;
579 bool wxStyledTextEvent::GetShift() const { return (m_modifiers
& SCI_SHIFT
) != 0; }
580 bool wxStyledTextEvent::GetControl() const { return (m_modifiers
& SCI_CTRL
) != 0; }
581 bool wxStyledTextEvent::GetAlt() const { return (m_modifiers
& SCI_ALT
) != 0; }
584 wxStyledTextEvent::wxStyledTextEvent(const wxStyledTextEvent
& event
):
585 wxCommandEvent(event
)
587 m_position
= event
.m_position
;
589 m_modifiers
= event
.m_modifiers
;
590 m_modificationType
= event
.m_modificationType
;
591 m_text
= event
.m_text
;
592 m_length
= event
.m_length
;
593 m_linesAdded
= event
.m_linesAdded
;
594 m_line
= event
.m_line
;
595 m_foldLevelNow
= event
.m_foldLevelNow
;
596 m_foldLevelPrev
= event
.m_foldLevelPrev
;
598 m_margin
= event
.m_margin
;
600 m_message
= event
.m_message
;
601 m_wParam
= event
.m_wParam
;
602 m_lParam
= event
.m_lParam
;
604 m_listType
= event
.m_listType
;
608 m_dragText
= event
.m_dragText
;
609 m_dragAllowMove
=event
.m_dragAllowMove
;
610 m_dragResult
= event
.m_dragResult
;
613 //----------------------------------------------------------------------
614 //----------------------------------------------------------------------