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 #if wxUSE_DRAG_AND_DROP
97 DEFINE_EVENT_TYPE( wxEVT_STC_START_DRAG
)
98 DEFINE_EVENT_TYPE( wxEVT_STC_DRAG_OVER
)
99 DEFINE_EVENT_TYPE( wxEVT_STC_DO_DROP
)
103 BEGIN_EVENT_TABLE(wxStyledTextCtrl
, wxControl
)
104 EVT_PAINT (wxStyledTextCtrl::OnPaint
)
105 EVT_SCROLLWIN (wxStyledTextCtrl::OnScrollWin
)
106 EVT_SIZE (wxStyledTextCtrl::OnSize
)
107 EVT_LEFT_DOWN (wxStyledTextCtrl::OnMouseLeftDown
)
109 // Let Scintilla see the double click as a second click
110 EVT_LEFT_DCLICK (wxStyledTextCtrl::OnMouseLeftDown
)
112 EVT_MOTION (wxStyledTextCtrl::OnMouseMove
)
113 EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp
)
114 EVT_CONTEXT_MENU (wxStyledTextCtrl::OnContextMenu
)
115 EVT_MOUSEWHEEL (wxStyledTextCtrl::OnMouseWheel
)
116 EVT_CHAR (wxStyledTextCtrl::OnChar
)
117 EVT_KEY_DOWN (wxStyledTextCtrl::OnKeyDown
)
118 EVT_KILL_FOCUS (wxStyledTextCtrl::OnLoseFocus
)
119 EVT_SET_FOCUS (wxStyledTextCtrl::OnGainFocus
)
120 EVT_SYS_COLOUR_CHANGED (wxStyledTextCtrl::OnSysColourChanged
)
121 EVT_ERASE_BACKGROUND (wxStyledTextCtrl::OnEraseBackground
)
122 EVT_MENU_RANGE (-1, -1, wxStyledTextCtrl::OnMenu
)
123 EVT_LISTBOX_DCLICK (-1, wxStyledTextCtrl::OnListBox
)
127 IMPLEMENT_CLASS(wxStyledTextCtrl
, wxControl
)
128 IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent
, wxCommandEvent
)
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
)
143 m_swx
= new ScintillaWX(this);
145 m_lastKeyDownConsumed
= FALSE
;
149 wxStyledTextCtrl::~wxStyledTextCtrl() {
154 //----------------------------------------------------------------------
156 long wxStyledTextCtrl::SendMsg(int msg
, long wp
, long lp
) {
158 return m_swx
->WndProc(msg
, wp
, lp
);
166 #define MAKELONG(a, b) ((a) | ((b) << 16))
169 static long wxColourAsLong(const wxColour
& co
) {
170 return (((long)co
.Blue() << 16) |
171 ((long)co
.Green() << 8) |
175 static wxColour
wxColourFromLong(long c
) {
177 clr
.Set(c
& 0xff, (c
>> 8) & 0xff, (c
>> 16) & 0xff);
182 static wxColour
wxColourFromSpec(const wxString
& spec
) {
183 // spec should be #RRGGBB
185 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
186 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
187 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
188 return wxColour(red
, green
, blue
);
192 //----------------------------------------------------------------------
193 // BEGIN generated section. The following code is automatically generated
194 // by gen_iface.py from the contents of Scintilla.iface. Do not edit
195 // this file. Edit stc.cpp.in or gen_iface.py instead and regenerate.
199 // END of generated section
200 //----------------------------------------------------------------------
203 // Returns the line number of the line with the caret.
204 int wxStyledTextCtrl::GetCurrentLine() {
205 int line
= LineFromPosition(GetCurrentPos());
210 // Extract style settings from a spec-string which is composed of one or
211 // more of the following comma separated elements:
213 // bold turns on bold
214 // italic turns on italics
215 // fore:#RRGGBB sets the foreground colour
216 // back:#RRGGBB sets the background colour
217 // face:[facename] sets the font face name to use
218 // size:[num] sets the font size in points
219 // eol turns on eol filling
220 // underline turns on underlining
222 void wxStyledTextCtrl::StyleSetSpec(int styleNum
, const wxString
& spec
) {
224 wxStringTokenizer
tkz(spec
, ",");
225 while (tkz
.HasMoreTokens()) {
226 wxString token
= tkz
.GetNextToken();
228 wxString option
= token
.BeforeFirst(':');
229 wxString val
= token
.AfterFirst(':');
231 if (option
== "bold")
232 StyleSetBold(styleNum
, true);
234 else if (option
== "italic")
235 StyleSetItalic(styleNum
, true);
237 else if (option
== "underline")
238 StyleSetUnderline(styleNum
, true);
240 else if (option
== "eol")
241 StyleSetEOLFilled(styleNum
, true);
243 else if (option
== "size") {
245 if (val
.ToLong(&points
))
246 StyleSetSize(styleNum
, points
);
249 else if (option
== "face")
250 StyleSetFaceName(styleNum
, val
);
252 else if (option
== "fore")
253 StyleSetForeground(styleNum
, wxColourFromSpec(val
));
255 else if (option
== "back")
256 StyleSetBackground(styleNum
, wxColourFromSpec(val
));
261 // Set style size, face, bold, italic, and underline attributes from
262 // a wxFont's attributes.
263 void wxStyledTextCtrl::StyleSetFont(int styleNum
, wxFont
& font
) {
264 int size
= font
.GetPointSize();
265 wxString faceName
= font
.GetFaceName();
266 bool bold
= font
.GetWeight() == wxBOLD
;
267 bool italic
= font
.GetStyle() != wxNORMAL
;
268 bool under
= font
.GetUnderlined();
270 // TODO: add encoding/charset mapping
271 StyleSetFontAttr(styleNum
, size
, faceName
, bold
, italic
, under
);
274 // Set all font style attributes at once.
275 void wxStyledTextCtrl::StyleSetFontAttr(int styleNum
, int size
,
276 const wxString
& faceName
,
277 bool bold
, bool italic
,
279 StyleSetSize(styleNum
, size
);
280 StyleSetFaceName(styleNum
, faceName
);
281 StyleSetBold(styleNum
, bold
);
282 StyleSetItalic(styleNum
, italic
);
283 StyleSetUnderline(styleNum
, underline
);
285 // TODO: add encoding/charset mapping
289 // Perform one of the operations defined by the wxSTC_CMD_* constants.
290 void wxStyledTextCtrl::CmdKeyExecute(int cmd
) {
295 // Set the left and right margin in the edit area, measured in pixels.
296 void wxStyledTextCtrl::SetMargins(int left
, int right
) {
298 SetMarginRight(right
);
302 // Retrieve the start and end positions of the current selection.
303 void wxStyledTextCtrl::GetSelection(int* startPos
, int* endPos
) {
304 if (startPos
!= NULL
)
305 *startPos
= SendMsg(SCI_GETSELECTIONSTART
);
307 *endPos
= SendMsg(SCI_GETSELECTIONEND
);
311 // Retrieve the point in the window where a position is displayed.
312 wxPoint
wxStyledTextCtrl::PointFromPosition(int pos
) {
313 int x
= SendMsg(SCI_POINTXFROMPOSITION
, 0, pos
);
314 int y
= SendMsg(SCI_POINTYFROMPOSITION
, 0, pos
);
315 return wxPoint(x
, y
);
318 // Scroll enough to make the given line visible
319 void wxStyledTextCtrl::ScrollToLine(int line
) {
320 m_swx
->DoScrollToLine(line
);
324 // Scroll enough to make the given column visible
325 void wxStyledTextCtrl::ScrollToColumn(int column
) {
326 m_swx
->DoScrollToColumn(column
);
331 //----------------------------------------------------------------------
334 void wxStyledTextCtrl::OnPaint(wxPaintEvent
& evt
) {
336 wxRegion region
= GetUpdateRegion();
338 m_swx
->DoPaint(&dc
, region
.GetBox());
341 void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent
& evt
) {
342 if (evt
.GetOrientation() == wxHORIZONTAL
)
343 m_swx
->DoHScroll(evt
.GetEventType(), evt
.GetPosition());
345 m_swx
->DoVScroll(evt
.GetEventType(), evt
.GetPosition());
348 void wxStyledTextCtrl::OnSize(wxSizeEvent
& evt
) {
349 wxSize sz
= GetClientSize();
350 m_swx
->DoSize(sz
.x
, sz
.y
);
353 void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent
& evt
) {
354 wxPoint pt
= evt
.GetPosition();
355 m_swx
->DoButtonDown(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
356 evt
.ShiftDown(), evt
.ControlDown(), evt
.AltDown());
359 void wxStyledTextCtrl::OnMouseMove(wxMouseEvent
& evt
) {
360 wxPoint pt
= evt
.GetPosition();
361 m_swx
->DoButtonMove(Point(pt
.x
, pt
.y
));
364 void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent
& evt
) {
365 wxPoint pt
= evt
.GetPosition();
366 m_swx
->DoButtonUp(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
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(),
386 void wxStyledTextCtrl::OnChar(wxKeyEvent
& evt
) {
387 long key
= evt
.KeyCode();
389 // printf("OnChar key:%%d consumed:%%d ctrl:%%d alt:%%d\n",
390 // key, m_lastKeyDownConsumed, evt.ControlDown(), evt.AltDown());
393 // \|@#¬[]{}?£$~ ã,õ,Ã,Õ, ñ, Ñ
395 // On (some?) non-US keyboards the AltGr key is required to enter some
396 // common characters. It comes to us as both Alt and Ctrl down so we need
397 // to let the char through in that case, otherwise if only ctrl or only
398 // alt let's skip it.
399 bool ctrl
= evt
.ControlDown();
400 bool alt
= evt
.AltDown();
401 bool skip
= ((ctrl
|| alt
) && ! (ctrl
&& alt
));
403 if (key
<= 0xff && !iscntrl(key
) && !m_lastKeyDownConsumed
&& !skip
) {
404 m_swx
->DoAddChar(key
);
411 void wxStyledTextCtrl::OnKeyDown(wxKeyEvent
& evt
) {
412 long key
= evt
.KeyCode();
413 bool shift
= evt
.ShiftDown(),
414 ctrl
= evt
.ControlDown(),
417 int processed
= m_swx
->DoKeyDown(key
, shift
, ctrl
, alt
, &m_lastKeyDownConsumed
);
419 // printf("key: %%d shift: %%d ctrl: %%d alt: %%d processed: %%d consumed: %%d\n",
420 // key, shift, ctrl, alt, processed, m_lastKeyDownConsumed);
422 if (!processed
&& !m_lastKeyDownConsumed
)
427 void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent
& evt
) {
428 m_swx
->DoLoseFocus();
432 void wxStyledTextCtrl::OnGainFocus(wxFocusEvent
& evt
) {
433 m_swx
->DoGainFocus();
437 void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& evt
) {
438 m_swx
->DoSysColourChange();
442 void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent
& evt
) {
443 // do nothing to help avoid flashing
448 void wxStyledTextCtrl::OnMenu(wxCommandEvent
& evt
) {
449 m_swx
->DoCommand(evt
.GetId());
453 void wxStyledTextCtrl::OnListBox(wxCommandEvent
& evt
) {
454 m_swx
->DoOnListBox();
458 //----------------------------------------------------------------------
459 // Turn notifications from Scintilla into events
462 void wxStyledTextCtrl::NotifyChange() {
463 wxStyledTextEvent
evt(wxEVT_STC_CHANGE
, GetId());
464 evt
.SetEventObject(this);
465 GetEventHandler()->ProcessEvent(evt
);
468 void wxStyledTextCtrl::NotifyParent(SCNotification
* _scn
) {
469 SCNotification
& scn
= *_scn
;
470 wxStyledTextEvent
evt(0, GetId());
472 evt
.SetEventObject(this);
473 evt
.SetPosition(scn
.position
);
475 evt
.SetModifiers(scn
.modifiers
);
477 switch (scn
.nmhdr
.code
) {
478 case SCN_STYLENEEDED
:
479 evt
.SetEventType(wxEVT_STC_STYLENEEDED
);
483 evt
.SetEventType(wxEVT_STC_CHARADDED
);
486 case SCN_SAVEPOINTREACHED
:
487 evt
.SetEventType(wxEVT_STC_SAVEPOINTREACHED
);
490 case SCN_SAVEPOINTLEFT
:
491 evt
.SetEventType(wxEVT_STC_SAVEPOINTLEFT
);
494 case SCN_MODIFYATTEMPTRO
:
495 evt
.SetEventType(wxEVT_STC_ROMODIFYATTEMPT
);
499 evt
.SetEventType(wxEVT_STC_KEY
);
502 case SCN_DOUBLECLICK
:
503 evt
.SetEventType(wxEVT_STC_DOUBLECLICK
);
507 evt
.SetEventType(wxEVT_STC_UPDATEUI
);
511 evt
.SetEventType(wxEVT_STC_MODIFIED
);
512 evt
.SetModificationType(scn
.modificationType
);
514 evt
.SetText(wxString(scn
.text
, scn
.length
));
515 evt
.SetLength(scn
.length
);
516 evt
.SetLinesAdded(scn
.linesAdded
);
517 evt
.SetLine(scn
.line
);
518 evt
.SetFoldLevelNow(scn
.foldLevelNow
);
519 evt
.SetFoldLevelPrev(scn
.foldLevelPrev
);
522 case SCN_MACRORECORD
:
523 evt
.SetEventType(wxEVT_STC_MACRORECORD
);
524 evt
.SetMessage(scn
.message
);
525 evt
.SetWParam(scn
.wParam
);
526 evt
.SetLParam(scn
.lParam
);
529 case SCN_MARGINCLICK
:
530 evt
.SetEventType(wxEVT_STC_MARGINCLICK
);
531 evt
.SetMargin(scn
.margin
);
535 evt
.SetEventType(wxEVT_STC_NEEDSHOWN
);
536 evt
.SetLength(scn
.length
);
540 evt
.SetEventType(wxEVT_STC_POSCHANGED
);
544 evt
.SetEventType(wxEVT_STC_PAINTED
);
547 case SCN_USERLISTSELECTION
:
548 evt
.SetEventType(wxEVT_STC_USERLISTSELECTION
);
549 evt
.SetListType(scn
.listType
);
550 evt
.SetText(scn
.text
);
554 evt
.SetEventType(wxEVT_STC_URIDROPPED
);
555 evt
.SetText(scn
.text
);
559 evt
.SetEventType(wxEVT_STC_DWELLSTART
);
565 evt
.SetEventType(wxEVT_STC_DWELLEND
);
574 GetEventHandler()->ProcessEvent(evt
);
578 //----------------------------------------------------------------------
579 //----------------------------------------------------------------------
580 //----------------------------------------------------------------------
582 wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType
, int id
)
583 : wxCommandEvent(commandType
, id
)
588 m_modificationType
= 0;
601 #if wxUSE_DRAG_AND_DROP
602 m_dragAllowMove
= FALSE
;
603 m_dragResult
= wxDragNone
;
607 bool wxStyledTextEvent::GetShift() const { return (m_modifiers
& SCI_SHIFT
) != 0; }
608 bool wxStyledTextEvent::GetControl() const { return (m_modifiers
& SCI_CTRL
) != 0; }
609 bool wxStyledTextEvent::GetAlt() const { return (m_modifiers
& SCI_ALT
) != 0; }
611 void wxStyledTextEvent::CopyObject(wxObject
& obj
) const {
612 wxCommandEvent::CopyObject(obj
);
614 wxStyledTextEvent
* o
= (wxStyledTextEvent
*)&obj
;
615 o
->m_position
= m_position
;
617 o
->m_modifiers
= m_modifiers
;
618 o
->m_modificationType
= m_modificationType
;
620 o
->m_length
= m_length
;
621 o
->m_linesAdded
= m_linesAdded
;
623 o
->m_foldLevelNow
= m_foldLevelNow
;
624 o
->m_foldLevelPrev
= m_foldLevelPrev
;
626 o
->m_margin
= m_margin
;
628 o
->m_message
= m_message
;
629 o
->m_wParam
= m_wParam
;
630 o
->m_lParam
= m_lParam
;
632 o
->m_listType
= m_listType
;
636 #if wxUSE_DRAG_AND_DROP
637 o
->m_dragText
= m_dragText
;
638 o
->m_dragAllowMove
=m_dragAllowMove
;
639 o
->m_dragResult
= m_dragResult
;
643 //----------------------------------------------------------------------
644 //----------------------------------------------------------------------