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>
25 // The following code forces a reference to all of the Scintilla lexers.
26 // If we don't do something like this, then the linker tends to "optimize"
27 // them away. (eric@sourcegear.com)
29 int wxForceScintillaLexers(void)
31 extern LexerModule lmAda
;
32 extern LexerModule lmAVE
;
33 extern LexerModule lmConf
;
34 extern LexerModule lmCPP
;
35 extern LexerModule lmNncrontab
;
36 extern LexerModule lmEiffel
;
37 extern LexerModule lmHTML
;
38 extern LexerModule lmLISP
;
39 extern LexerModule lmLua
;
40 extern LexerModule lmBatch
; // In LexOthers.cxx
41 extern LexerModule lmPascal
;
42 extern LexerModule lmPerl
;
43 extern LexerModule lmPython
;
44 extern LexerModule lmRuby
;
45 extern LexerModule lmSQL
;
46 extern LexerModule lmVB
;
73 //----------------------------------------------------------------------
75 const wxChar
* wxSTCNameStr
= "stcwindow";
77 DEFINE_EVENT_TYPE( wxEVT_STC_CHANGE
)
78 DEFINE_EVENT_TYPE( wxEVT_STC_STYLENEEDED
)
79 DEFINE_EVENT_TYPE( wxEVT_STC_CHARADDED
)
80 DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTREACHED
)
81 DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTLEFT
)
82 DEFINE_EVENT_TYPE( wxEVT_STC_ROMODIFYATTEMPT
)
83 DEFINE_EVENT_TYPE( wxEVT_STC_KEY
)
84 DEFINE_EVENT_TYPE( wxEVT_STC_DOUBLECLICK
)
85 DEFINE_EVENT_TYPE( wxEVT_STC_UPDATEUI
)
86 DEFINE_EVENT_TYPE( wxEVT_STC_MODIFIED
)
87 DEFINE_EVENT_TYPE( wxEVT_STC_MACRORECORD
)
88 DEFINE_EVENT_TYPE( wxEVT_STC_MARGINCLICK
)
89 DEFINE_EVENT_TYPE( wxEVT_STC_NEEDSHOWN
)
90 DEFINE_EVENT_TYPE( wxEVT_STC_POSCHANGED
)
91 DEFINE_EVENT_TYPE( wxEVT_STC_PAINTED
)
92 DEFINE_EVENT_TYPE( wxEVT_STC_USERLISTSELECTION
)
93 DEFINE_EVENT_TYPE( wxEVT_STC_URIDROPPED
)
94 DEFINE_EVENT_TYPE( wxEVT_STC_DWELLSTART
)
95 DEFINE_EVENT_TYPE( wxEVT_STC_DWELLEND
)
96 DEFINE_EVENT_TYPE( wxEVT_STC_START_DRAG
)
97 DEFINE_EVENT_TYPE( wxEVT_STC_DRAG_OVER
)
98 DEFINE_EVENT_TYPE( wxEVT_STC_DO_DROP
)
101 BEGIN_EVENT_TABLE(wxStyledTextCtrl
, wxControl
)
102 EVT_PAINT (wxStyledTextCtrl::OnPaint
)
103 EVT_SCROLLWIN (wxStyledTextCtrl::OnScrollWin
)
104 EVT_SCROLL (wxStyledTextCtrl::OnScroll
)
105 EVT_SIZE (wxStyledTextCtrl::OnSize
)
106 EVT_LEFT_DOWN (wxStyledTextCtrl::OnMouseLeftDown
)
108 // Let Scintilla see the double click as a second click
109 EVT_LEFT_DCLICK (wxStyledTextCtrl::OnMouseLeftDown
)
111 EVT_MOTION (wxStyledTextCtrl::OnMouseMove
)
112 EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp
)
114 EVT_RIGHT_UP (wxStyledTextCtrl::OnMouseRightUp
)
116 EVT_CONTEXT_MENU (wxStyledTextCtrl::OnContextMenu
)
118 EVT_MOUSEWHEEL (wxStyledTextCtrl::OnMouseWheel
)
119 EVT_CHAR (wxStyledTextCtrl::OnChar
)
120 EVT_KEY_DOWN (wxStyledTextCtrl::OnKeyDown
)
121 EVT_KILL_FOCUS (wxStyledTextCtrl::OnLoseFocus
)
122 EVT_SET_FOCUS (wxStyledTextCtrl::OnGainFocus
)
123 EVT_SYS_COLOUR_CHANGED (wxStyledTextCtrl::OnSysColourChanged
)
124 EVT_ERASE_BACKGROUND (wxStyledTextCtrl::OnEraseBackground
)
125 EVT_MENU_RANGE (-1, -1, wxStyledTextCtrl::OnMenu
)
126 EVT_LISTBOX_DCLICK (-1, wxStyledTextCtrl::OnListBox
)
130 IMPLEMENT_CLASS(wxStyledTextCtrl
, wxControl
)
131 IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent
, wxCommandEvent
)
133 //----------------------------------------------------------------------
134 // Constructor and Destructor
136 wxStyledTextCtrl::wxStyledTextCtrl(wxWindow
*parent
,
141 const wxString
& name
) :
142 wxControl(parent
, id
, pos
, size
,
143 style
| wxVSCROLL
| wxHSCROLL
| wxWANTS_CHARS
| wxCLIP_CHILDREN
,
144 wxDefaultValidator
, name
)
146 wxForceScintillaLexers();
147 m_swx
= new ScintillaWX(this);
149 m_lastKeyDownConsumed
= FALSE
;
155 wxStyledTextCtrl::~wxStyledTextCtrl() {
160 //----------------------------------------------------------------------
162 long wxStyledTextCtrl::SendMsg(int msg
, long wp
, long lp
) {
164 return m_swx
->WndProc(msg
, wp
, lp
);
172 #define MAKELONG(a, b) ((a) | ((b) << 16))
175 static long wxColourAsLong(const wxColour
& co
) {
176 return (((long)co
.Blue() << 16) |
177 ((long)co
.Green() << 8) |
181 static wxColour
wxColourFromLong(long c
) {
183 clr
.Set(c
& 0xff, (c
>> 8) & 0xff, (c
>> 16) & 0xff);
188 static wxColour
wxColourFromSpec(const wxString
& spec
) {
189 // spec should be #RRGGBB
191 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
192 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
193 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
194 return wxColour(red
, green
, blue
);
198 //----------------------------------------------------------------------
199 // BEGIN generated section. The following code is automatically generated
200 // by gen_iface.py from the contents of Scintilla.iface. Do not edit
201 // this file. Edit stc.cpp.in or gen_iface.py instead and regenerate.
205 // END of generated section
206 //----------------------------------------------------------------------
209 // Returns the line number of the line with the caret.
210 int wxStyledTextCtrl::GetCurrentLine() {
211 int line
= LineFromPosition(GetCurrentPos());
216 // Extract style settings from a spec-string which is composed of one or
217 // more of the following comma separated elements:
219 // bold turns on bold
220 // italic turns on italics
221 // fore:#RRGGBB sets the foreground colour
222 // back:#RRGGBB sets the background colour
223 // face:[facename] sets the font face name to use
224 // size:[num] sets the font size in points
225 // eol turns on eol filling
226 // underline turns on underlining
228 void wxStyledTextCtrl::StyleSetSpec(int styleNum
, const wxString
& spec
) {
230 wxStringTokenizer
tkz(spec
, ",");
231 while (tkz
.HasMoreTokens()) {
232 wxString token
= tkz
.GetNextToken();
234 wxString option
= token
.BeforeFirst(':');
235 wxString val
= token
.AfterFirst(':');
237 if (option
== "bold")
238 StyleSetBold(styleNum
, true);
240 else if (option
== "italic")
241 StyleSetItalic(styleNum
, true);
243 else if (option
== "underline")
244 StyleSetUnderline(styleNum
, true);
246 else if (option
== "eol")
247 StyleSetEOLFilled(styleNum
, true);
249 else if (option
== "size") {
251 if (val
.ToLong(&points
))
252 StyleSetSize(styleNum
, points
);
255 else if (option
== "face")
256 StyleSetFaceName(styleNum
, val
);
258 else if (option
== "fore")
259 StyleSetForeground(styleNum
, wxColourFromSpec(val
));
261 else if (option
== "back")
262 StyleSetBackground(styleNum
, wxColourFromSpec(val
));
267 // Set style size, face, bold, italic, and underline attributes from
268 // a wxFont's attributes.
269 void wxStyledTextCtrl::StyleSetFont(int styleNum
, wxFont
& font
) {
270 int size
= font
.GetPointSize();
271 wxString faceName
= font
.GetFaceName();
272 bool bold
= font
.GetWeight() == wxBOLD
;
273 bool italic
= font
.GetStyle() != wxNORMAL
;
274 bool under
= font
.GetUnderlined();
276 // TODO: add encoding/charset mapping
277 StyleSetFontAttr(styleNum
, size
, faceName
, bold
, italic
, under
);
280 // Set all font style attributes at once.
281 void wxStyledTextCtrl::StyleSetFontAttr(int styleNum
, int size
,
282 const wxString
& faceName
,
283 bool bold
, bool italic
,
285 StyleSetSize(styleNum
, size
);
286 StyleSetFaceName(styleNum
, faceName
);
287 StyleSetBold(styleNum
, bold
);
288 StyleSetItalic(styleNum
, italic
);
289 StyleSetUnderline(styleNum
, underline
);
291 // TODO: add encoding/charset mapping
295 // Perform one of the operations defined by the wxSTC_CMD_* constants.
296 void wxStyledTextCtrl::CmdKeyExecute(int cmd
) {
301 // Set the left and right margin in the edit area, measured in pixels.
302 void wxStyledTextCtrl::SetMargins(int left
, int right
) {
304 SetMarginRight(right
);
308 // Retrieve the start and end positions of the current selection.
309 void wxStyledTextCtrl::GetSelection(int* startPos
, int* endPos
) {
310 if (startPos
!= NULL
)
311 *startPos
= SendMsg(SCI_GETSELECTIONSTART
);
313 *endPos
= SendMsg(SCI_GETSELECTIONEND
);
317 // Retrieve the point in the window where a position is displayed.
318 wxPoint
wxStyledTextCtrl::PointFromPosition(int pos
) {
319 int x
= SendMsg(SCI_POINTXFROMPOSITION
, 0, pos
);
320 int y
= SendMsg(SCI_POINTYFROMPOSITION
, 0, pos
);
321 return wxPoint(x
, y
);
324 // Scroll enough to make the given line visible
325 void wxStyledTextCtrl::ScrollToLine(int line
) {
326 m_swx
->DoScrollToLine(line
);
330 // Scroll enough to make the given column visible
331 void wxStyledTextCtrl::ScrollToColumn(int column
) {
332 m_swx
->DoScrollToColumn(column
);
337 //----------------------------------------------------------------------
340 void wxStyledTextCtrl::OnPaint(wxPaintEvent
& evt
) {
342 wxRegion region
= GetUpdateRegion();
344 m_swx
->DoPaint(&dc
, region
.GetBox());
347 void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent
& evt
) {
348 if (evt
.GetOrientation() == wxHORIZONTAL
)
349 m_swx
->DoHScroll(evt
.GetEventType(), evt
.GetPosition());
351 m_swx
->DoVScroll(evt
.GetEventType(), evt
.GetPosition());
354 void wxStyledTextCtrl::OnScroll(wxScrollEvent
& evt
) {
355 wxScrollBar
* sb
= wxDynamicCast(evt
.GetEventObject(), wxScrollBar
);
357 if (sb
->IsVertical())
358 m_swx
->DoVScroll(evt
.GetEventType(), evt
.GetPosition());
360 m_swx
->DoHScroll(evt
.GetEventType(), evt
.GetPosition());
364 void wxStyledTextCtrl::OnSize(wxSizeEvent
& evt
) {
365 wxSize sz
= GetClientSize();
366 m_swx
->DoSize(sz
.x
, sz
.y
);
369 void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent
& evt
) {
370 wxPoint pt
= evt
.GetPosition();
371 m_swx
->DoButtonDown(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
372 evt
.ShiftDown(), evt
.ControlDown(), evt
.AltDown());
375 void wxStyledTextCtrl::OnMouseMove(wxMouseEvent
& evt
) {
376 wxPoint pt
= evt
.GetPosition();
377 m_swx
->DoButtonMove(Point(pt
.x
, pt
.y
));
380 void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent
& evt
) {
381 wxPoint pt
= evt
.GetPosition();
382 m_swx
->DoButtonUp(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
387 void wxStyledTextCtrl::OnMouseRightUp(wxMouseEvent
& evt
) {
388 wxPoint pt
= evt
.GetPosition();
389 m_swx
->DoContextMenu(Point(pt
.x
, pt
.y
));
393 void wxStyledTextCtrl::OnContextMenu(wxContextMenuEvent
& evt
) {
394 wxPoint pt
= evt
.GetPosition();
395 ScreenToClient(&pt
.x
, &pt
.y
);
396 m_swx
->DoContextMenu(Point(pt
.x
, pt
.y
));
400 void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent
& evt
) {
401 m_swx
->DoMouseWheel(evt
.GetWheelRotation(),
403 evt
.GetLinesPerAction(),
408 void wxStyledTextCtrl::OnChar(wxKeyEvent
& evt
) {
409 long key
= evt
.KeyCode();
411 // printf("OnChar key:%%d consumed:%%d ctrl:%%d alt:%%d\n",
412 // key, m_lastKeyDownConsumed, evt.ControlDown(), evt.AltDown());
415 // \|@#¬[]{}?£$~ ã,õ,Ã,Õ, ñ, Ñ
417 // On (some?) non-US keyboards the AltGr key is required to enter some
418 // common characters. It comes to us as both Alt and Ctrl down so we need
419 // to let the char through in that case, otherwise if only ctrl or only
420 // alt let's skip it.
421 bool ctrl
= evt
.ControlDown();
422 bool alt
= evt
.AltDown();
423 bool skip
= ((ctrl
|| alt
) && ! (ctrl
&& alt
));
425 if (key
<= 0xff && key
>= 32 && !m_lastKeyDownConsumed
&& !skip
) {
426 m_swx
->DoAddChar(key
);
433 void wxStyledTextCtrl::OnKeyDown(wxKeyEvent
& evt
) {
434 long key
= evt
.KeyCode();
435 bool shift
= evt
.ShiftDown(),
436 ctrl
= evt
.ControlDown(),
439 int processed
= m_swx
->DoKeyDown(key
, shift
, ctrl
, alt
, &m_lastKeyDownConsumed
);
441 // printf("key: %%d shift: %%d ctrl: %%d alt: %%d processed: %%d consumed: %%d\n",
442 // key, shift, ctrl, alt, processed, m_lastKeyDownConsumed);
444 if (!processed
&& !m_lastKeyDownConsumed
)
449 void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent
& evt
) {
450 m_swx
->DoLoseFocus();
454 void wxStyledTextCtrl::OnGainFocus(wxFocusEvent
& evt
) {
455 m_swx
->DoGainFocus();
459 void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& evt
) {
460 m_swx
->DoSysColourChange();
464 void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent
& evt
) {
465 // do nothing to help avoid flashing
470 void wxStyledTextCtrl::OnMenu(wxCommandEvent
& evt
) {
471 m_swx
->DoCommand(evt
.GetId());
475 void wxStyledTextCtrl::OnListBox(wxCommandEvent
& evt
) {
476 m_swx
->DoOnListBox();
480 //----------------------------------------------------------------------
481 // Turn notifications from Scintilla into events
484 void wxStyledTextCtrl::NotifyChange() {
485 wxStyledTextEvent
evt(wxEVT_STC_CHANGE
, GetId());
486 evt
.SetEventObject(this);
487 GetEventHandler()->ProcessEvent(evt
);
490 void wxStyledTextCtrl::NotifyParent(SCNotification
* _scn
) {
491 SCNotification
& scn
= *_scn
;
492 wxStyledTextEvent
evt(0, GetId());
494 evt
.SetEventObject(this);
495 evt
.SetPosition(scn
.position
);
497 evt
.SetModifiers(scn
.modifiers
);
499 switch (scn
.nmhdr
.code
) {
500 case SCN_STYLENEEDED
:
501 evt
.SetEventType(wxEVT_STC_STYLENEEDED
);
505 evt
.SetEventType(wxEVT_STC_CHARADDED
);
508 case SCN_SAVEPOINTREACHED
:
509 evt
.SetEventType(wxEVT_STC_SAVEPOINTREACHED
);
512 case SCN_SAVEPOINTLEFT
:
513 evt
.SetEventType(wxEVT_STC_SAVEPOINTLEFT
);
516 case SCN_MODIFYATTEMPTRO
:
517 evt
.SetEventType(wxEVT_STC_ROMODIFYATTEMPT
);
521 evt
.SetEventType(wxEVT_STC_KEY
);
524 case SCN_DOUBLECLICK
:
525 evt
.SetEventType(wxEVT_STC_DOUBLECLICK
);
529 evt
.SetEventType(wxEVT_STC_UPDATEUI
);
533 evt
.SetEventType(wxEVT_STC_MODIFIED
);
534 evt
.SetModificationType(scn
.modificationType
);
536 evt
.SetText(wxString(scn
.text
, scn
.length
));
537 evt
.SetLength(scn
.length
);
538 evt
.SetLinesAdded(scn
.linesAdded
);
539 evt
.SetLine(scn
.line
);
540 evt
.SetFoldLevelNow(scn
.foldLevelNow
);
541 evt
.SetFoldLevelPrev(scn
.foldLevelPrev
);
544 case SCN_MACRORECORD
:
545 evt
.SetEventType(wxEVT_STC_MACRORECORD
);
546 evt
.SetMessage(scn
.message
);
547 evt
.SetWParam(scn
.wParam
);
548 evt
.SetLParam(scn
.lParam
);
551 case SCN_MARGINCLICK
:
552 evt
.SetEventType(wxEVT_STC_MARGINCLICK
);
553 evt
.SetMargin(scn
.margin
);
557 evt
.SetEventType(wxEVT_STC_NEEDSHOWN
);
558 evt
.SetLength(scn
.length
);
562 evt
.SetEventType(wxEVT_STC_POSCHANGED
);
566 evt
.SetEventType(wxEVT_STC_PAINTED
);
569 case SCN_USERLISTSELECTION
:
570 evt
.SetEventType(wxEVT_STC_USERLISTSELECTION
);
571 evt
.SetListType(scn
.listType
);
572 evt
.SetText(scn
.text
);
576 evt
.SetEventType(wxEVT_STC_URIDROPPED
);
577 evt
.SetText(scn
.text
);
581 evt
.SetEventType(wxEVT_STC_DWELLSTART
);
587 evt
.SetEventType(wxEVT_STC_DWELLEND
);
596 GetEventHandler()->ProcessEvent(evt
);
600 //----------------------------------------------------------------------
601 //----------------------------------------------------------------------
602 //----------------------------------------------------------------------
604 wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType
, int id
)
605 : wxCommandEvent(commandType
, id
)
610 m_modificationType
= 0;
623 m_dragAllowMove
= FALSE
;
624 m_dragResult
= wxDragNone
;
627 bool wxStyledTextEvent::GetShift() const { return (m_modifiers
& SCI_SHIFT
) != 0; }
628 bool wxStyledTextEvent::GetControl() const { return (m_modifiers
& SCI_CTRL
) != 0; }
629 bool wxStyledTextEvent::GetAlt() const { return (m_modifiers
& SCI_ALT
) != 0; }
632 wxStyledTextEvent::wxStyledTextEvent(const wxStyledTextEvent
& event
):
633 wxCommandEvent(event
)
635 m_position
= event
.m_position
;
637 m_modifiers
= event
.m_modifiers
;
638 m_modificationType
= event
.m_modificationType
;
639 m_text
= event
.m_text
;
640 m_length
= event
.m_length
;
641 m_linesAdded
= event
.m_linesAdded
;
642 m_line
= event
.m_line
;
643 m_foldLevelNow
= event
.m_foldLevelNow
;
644 m_foldLevelPrev
= event
.m_foldLevelPrev
;
646 m_margin
= event
.m_margin
;
648 m_message
= event
.m_message
;
649 m_wParam
= event
.m_wParam
;
650 m_lParam
= event
.m_lParam
;
652 m_listType
= event
.m_listType
;
656 m_dragText
= event
.m_dragText
;
657 m_dragAllowMove
=event
.m_dragAllowMove
;
658 m_dragResult
= event
.m_dragResult
;
661 //----------------------------------------------------------------------
662 //----------------------------------------------------------------------