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_SIZE (wxStyledTextCtrl::OnSize
)
105 EVT_LEFT_DOWN (wxStyledTextCtrl::OnMouseLeftDown
)
107 // Let Scintilla see the double click as a second click
108 EVT_LEFT_DCLICK (wxStyledTextCtrl::OnMouseLeftDown
)
110 EVT_MOTION (wxStyledTextCtrl::OnMouseMove
)
111 EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp
)
112 EVT_CONTEXT_MENU (wxStyledTextCtrl::OnContextMenu
)
113 EVT_MOUSEWHEEL (wxStyledTextCtrl::OnMouseWheel
)
114 EVT_CHAR (wxStyledTextCtrl::OnChar
)
115 EVT_KEY_DOWN (wxStyledTextCtrl::OnKeyDown
)
116 EVT_KILL_FOCUS (wxStyledTextCtrl::OnLoseFocus
)
117 EVT_SET_FOCUS (wxStyledTextCtrl::OnGainFocus
)
118 EVT_SYS_COLOUR_CHANGED (wxStyledTextCtrl::OnSysColourChanged
)
119 EVT_ERASE_BACKGROUND (wxStyledTextCtrl::OnEraseBackground
)
120 EVT_MENU_RANGE (-1, -1, wxStyledTextCtrl::OnMenu
)
121 EVT_LISTBOX_DCLICK (-1, wxStyledTextCtrl::OnListBox
)
125 IMPLEMENT_CLASS(wxStyledTextCtrl
, wxControl
)
126 IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent
, wxCommandEvent
)
128 //----------------------------------------------------------------------
129 // Constructor and Destructor
131 wxStyledTextCtrl::wxStyledTextCtrl(wxWindow
*parent
,
136 const wxString
& name
) :
137 wxControl(parent
, id
, pos
, size
,
138 style
| wxVSCROLL
| wxHSCROLL
| wxWANTS_CHARS
| wxCLIP_CHILDREN
,
139 wxDefaultValidator
, name
)
141 m_swx
= new ScintillaWX(this);
143 m_lastKeyDownConsumed
= FALSE
;
147 wxStyledTextCtrl::~wxStyledTextCtrl() {
152 //----------------------------------------------------------------------
154 long wxStyledTextCtrl::SendMsg(int msg
, long wp
, long lp
) {
156 return m_swx
->WndProc(msg
, wp
, lp
);
164 #define MAKELONG(a, b) ((a) | ((b) << 16))
167 static long wxColourAsLong(const wxColour
& co
) {
168 return (((long)co
.Blue() << 16) |
169 ((long)co
.Green() << 8) |
173 static wxColour
wxColourFromLong(long c
) {
175 clr
.Set(c
& 0xff, (c
>> 8) & 0xff, (c
>> 16) & 0xff);
180 static wxColour
wxColourFromSpec(const wxString
& spec
) {
181 // spec should be #RRGGBB
183 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
184 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
185 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
186 return wxColour(red
, green
, blue
);
190 //----------------------------------------------------------------------
191 // BEGIN generated section. The following code is automatically generated
192 // by gen_iface.py from the contents of Scintilla.iface. Do not edit
193 // this file. Edit stc.cpp.in or gen_iface.py instead and regenerate.
197 // END of generated section
198 //----------------------------------------------------------------------
201 // Returns the line number of the line with the caret.
202 int wxStyledTextCtrl::GetCurrentLine() {
203 int line
= LineFromPosition(GetCurrentPos());
208 // Extract style settings from a spec-string which is composed of one or
209 // more of the following comma separated elements:
211 // bold turns on bold
212 // italic turns on italics
213 // fore:#RRGGBB sets the foreground colour
214 // back:#RRGGBB sets the background colour
215 // face:[facename] sets the font face name to use
216 // size:[num] sets the font size in points
217 // eol turns on eol filling
218 // underline turns on underlining
220 void wxStyledTextCtrl::StyleSetSpec(int styleNum
, const wxString
& spec
) {
222 wxStringTokenizer
tkz(spec
, ",");
223 while (tkz
.HasMoreTokens()) {
224 wxString token
= tkz
.GetNextToken();
226 wxString option
= token
.BeforeFirst(':');
227 wxString val
= token
.AfterFirst(':');
229 if (option
== "bold")
230 StyleSetBold(styleNum
, true);
232 else if (option
== "italic")
233 StyleSetItalic(styleNum
, true);
235 else if (option
== "underline")
236 StyleSetUnderline(styleNum
, true);
238 else if (option
== "eol")
239 StyleSetEOLFilled(styleNum
, true);
241 else if (option
== "size") {
243 if (val
.ToLong(&points
))
244 StyleSetSize(styleNum
, points
);
247 else if (option
== "face")
248 StyleSetFaceName(styleNum
, val
);
250 else if (option
== "fore")
251 StyleSetForeground(styleNum
, wxColourFromSpec(val
));
253 else if (option
== "back")
254 StyleSetBackground(styleNum
, wxColourFromSpec(val
));
259 // Set style size, face, bold, italic, and underline attributes from
260 // a wxFont's attributes.
261 void wxStyledTextCtrl::StyleSetFont(int styleNum
, wxFont
& font
) {
262 int size
= font
.GetPointSize();
263 wxString faceName
= font
.GetFaceName();
264 bool bold
= font
.GetWeight() == wxBOLD
;
265 bool italic
= font
.GetStyle() != wxNORMAL
;
266 bool under
= font
.GetUnderlined();
268 // TODO: add encoding/charset mapping
269 StyleSetFontAttr(styleNum
, size
, faceName
, bold
, italic
, under
);
272 // Set all font style attributes at once.
273 void wxStyledTextCtrl::StyleSetFontAttr(int styleNum
, int size
,
274 const wxString
& faceName
,
275 bool bold
, bool italic
,
277 StyleSetSize(styleNum
, size
);
278 StyleSetFaceName(styleNum
, faceName
);
279 StyleSetBold(styleNum
, bold
);
280 StyleSetItalic(styleNum
, italic
);
281 StyleSetUnderline(styleNum
, underline
);
283 // TODO: add encoding/charset mapping
287 // Perform one of the operations defined by the wxSTC_CMD_* constants.
288 void wxStyledTextCtrl::CmdKeyExecute(int cmd
) {
293 // Set the left and right margin in the edit area, measured in pixels.
294 void wxStyledTextCtrl::SetMargins(int left
, int right
) {
296 SetMarginRight(right
);
300 // Retrieve the start and end positions of the current selection.
301 void wxStyledTextCtrl::GetSelection(int* startPos
, int* endPos
) {
302 if (startPos
!= NULL
)
303 *startPos
= SendMsg(SCI_GETSELECTIONSTART
);
305 *endPos
= SendMsg(SCI_GETSELECTIONEND
);
309 // Retrieve the point in the window where a position is displayed.
310 wxPoint
wxStyledTextCtrl::PointFromPosition(int pos
) {
311 int x
= SendMsg(SCI_POINTXFROMPOSITION
, 0, pos
);
312 int y
= SendMsg(SCI_POINTYFROMPOSITION
, 0, pos
);
313 return wxPoint(x
, y
);
316 // Scroll enough to make the given line visible
317 void wxStyledTextCtrl::ScrollToLine(int line
) {
318 m_swx
->DoScrollToLine(line
);
322 // Scroll enough to make the given column visible
323 void wxStyledTextCtrl::ScrollToColumn(int column
) {
324 m_swx
->DoScrollToColumn(column
);
329 //----------------------------------------------------------------------
332 void wxStyledTextCtrl::OnPaint(wxPaintEvent
& evt
) {
334 wxRegion region
= GetUpdateRegion();
336 m_swx
->DoPaint(&dc
, region
.GetBox());
339 void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent
& evt
) {
340 if (evt
.GetOrientation() == wxHORIZONTAL
)
341 m_swx
->DoHScroll(evt
.GetEventType(), evt
.GetPosition());
343 m_swx
->DoVScroll(evt
.GetEventType(), evt
.GetPosition());
346 void wxStyledTextCtrl::OnSize(wxSizeEvent
& evt
) {
347 wxSize sz
= GetClientSize();
348 m_swx
->DoSize(sz
.x
, sz
.y
);
351 void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent
& evt
) {
352 wxPoint pt
= evt
.GetPosition();
353 m_swx
->DoButtonDown(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
354 evt
.ShiftDown(), evt
.ControlDown(), evt
.AltDown());
357 void wxStyledTextCtrl::OnMouseMove(wxMouseEvent
& evt
) {
358 wxPoint pt
= evt
.GetPosition();
359 m_swx
->DoButtonMove(Point(pt
.x
, pt
.y
));
362 void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent
& evt
) {
363 wxPoint pt
= evt
.GetPosition();
364 m_swx
->DoButtonUp(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
369 void wxStyledTextCtrl::OnContextMenu(wxContextMenuEvent
& evt
) {
370 wxPoint pt
= evt
.GetPosition();
371 ScreenToClient(&pt
.x
, &pt
.y
);
372 m_swx
->DoContextMenu(Point(pt
.x
, pt
.y
));
376 void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent
& evt
) {
377 m_swx
->DoMouseWheel(evt
.GetWheelRotation(),
379 evt
.GetLinesPerAction(),
384 void wxStyledTextCtrl::OnChar(wxKeyEvent
& evt
) {
385 long key
= evt
.KeyCode();
387 // printf("OnChar key:%%d consumed:%%d ctrl:%%d alt:%%d\n",
388 // key, m_lastKeyDownConsumed, evt.ControlDown(), evt.AltDown());
391 // \|@#¬[]{}?£$~ ã,õ,Ã,Õ, ñ, Ñ
393 // On (some?) non-US keyboards the AltGr key is required to enter some
394 // common characters. It comes to us as both Alt and Ctrl down so we need
395 // to let the char through in that case, otherwise if only ctrl or only
396 // alt let's skip it.
397 bool ctrl
= evt
.ControlDown();
398 bool alt
= evt
.AltDown();
399 bool skip
= (ctrl
|| alt
&& ! (ctrl
&& alt
));
401 if (key
<= 0xff && !iscntrl(key
) && !m_lastKeyDownConsumed
&& !skip
) {
402 m_swx
->DoAddChar(key
);
409 void wxStyledTextCtrl::OnKeyDown(wxKeyEvent
& evt
) {
410 long key
= evt
.KeyCode();
411 bool shift
= evt
.ShiftDown(),
412 ctrl
= evt
.ControlDown(),
415 int processed
= m_swx
->DoKeyDown(key
, shift
, ctrl
, alt
, &m_lastKeyDownConsumed
);
417 // printf("key: %%d shift: %%d ctrl: %%d alt: %%d processed: %%d consumed: %%d\n",
418 // key, shift, ctrl, alt, processed, m_lastKeyDownConsumed);
420 if (!processed
&& !m_lastKeyDownConsumed
)
425 void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent
& evt
) {
426 m_swx
->DoLoseFocus();
430 void wxStyledTextCtrl::OnGainFocus(wxFocusEvent
& evt
) {
431 m_swx
->DoGainFocus();
435 void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& evt
) {
436 m_swx
->DoSysColourChange();
440 void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent
& evt
) {
441 // do nothing to help avoid flashing
446 void wxStyledTextCtrl::OnMenu(wxCommandEvent
& evt
) {
447 m_swx
->DoCommand(evt
.GetId());
451 void wxStyledTextCtrl::OnListBox(wxCommandEvent
& evt
) {
452 m_swx
->DoOnListBox();
456 //----------------------------------------------------------------------
457 // Turn notifications from Scintilla into events
460 void wxStyledTextCtrl::NotifyChange() {
461 wxStyledTextEvent
evt(wxEVT_STC_CHANGE
, GetId());
462 evt
.SetEventObject(this);
463 GetEventHandler()->ProcessEvent(evt
);
466 void wxStyledTextCtrl::NotifyParent(SCNotification
* _scn
) {
467 SCNotification
& scn
= *_scn
;
468 wxStyledTextEvent
evt(0, GetId());
470 evt
.SetEventObject(this);
471 evt
.SetPosition(scn
.position
);
473 evt
.SetModifiers(scn
.modifiers
);
475 switch (scn
.nmhdr
.code
) {
476 case SCN_STYLENEEDED
:
477 evt
.SetEventType(wxEVT_STC_STYLENEEDED
);
481 evt
.SetEventType(wxEVT_STC_CHARADDED
);
484 case SCN_SAVEPOINTREACHED
:
485 evt
.SetEventType(wxEVT_STC_SAVEPOINTREACHED
);
488 case SCN_SAVEPOINTLEFT
:
489 evt
.SetEventType(wxEVT_STC_SAVEPOINTLEFT
);
492 case SCN_MODIFYATTEMPTRO
:
493 evt
.SetEventType(wxEVT_STC_ROMODIFYATTEMPT
);
497 evt
.SetEventType(wxEVT_STC_KEY
);
500 case SCN_DOUBLECLICK
:
501 evt
.SetEventType(wxEVT_STC_DOUBLECLICK
);
505 evt
.SetEventType(wxEVT_STC_UPDATEUI
);
509 evt
.SetEventType(wxEVT_STC_MODIFIED
);
510 evt
.SetModificationType(scn
.modificationType
);
512 evt
.SetText(wxString(scn
.text
, scn
.length
));
513 evt
.SetLength(scn
.length
);
514 evt
.SetLinesAdded(scn
.linesAdded
);
515 evt
.SetLine(scn
.line
);
516 evt
.SetFoldLevelNow(scn
.foldLevelNow
);
517 evt
.SetFoldLevelPrev(scn
.foldLevelPrev
);
520 case SCN_MACRORECORD
:
521 evt
.SetEventType(wxEVT_STC_MACRORECORD
);
522 evt
.SetMessage(scn
.message
);
523 evt
.SetWParam(scn
.wParam
);
524 evt
.SetLParam(scn
.lParam
);
527 case SCN_MARGINCLICK
:
528 evt
.SetEventType(wxEVT_STC_MARGINCLICK
);
529 evt
.SetMargin(scn
.margin
);
533 evt
.SetEventType(wxEVT_STC_NEEDSHOWN
);
534 evt
.SetLength(scn
.length
);
538 evt
.SetEventType(wxEVT_STC_POSCHANGED
);
542 evt
.SetEventType(wxEVT_STC_PAINTED
);
545 case SCN_USERLISTSELECTION
:
546 evt
.SetEventType(wxEVT_STC_USERLISTSELECTION
);
547 evt
.SetListType(scn
.listType
);
548 evt
.SetText(scn
.text
);
552 evt
.SetEventType(wxEVT_STC_URIDROPPED
);
553 evt
.SetText(scn
.text
);
557 evt
.SetEventType(wxEVT_STC_DWELLSTART
);
563 evt
.SetEventType(wxEVT_STC_DWELLEND
);
572 GetEventHandler()->ProcessEvent(evt
);
576 //----------------------------------------------------------------------
577 //----------------------------------------------------------------------
578 //----------------------------------------------------------------------
580 wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType
, int id
)
581 : wxCommandEvent(commandType
, id
)
586 m_modificationType
= 0;
599 m_dragAllowMove
= FALSE
;
600 m_dragResult
= wxDragNone
;
603 bool wxStyledTextEvent::GetShift() const { return (m_modifiers
& SCI_SHIFT
) != 0; }
604 bool wxStyledTextEvent::GetControl() const { return (m_modifiers
& SCI_CTRL
) != 0; }
605 bool wxStyledTextEvent::GetAlt() const { return (m_modifiers
& SCI_ALT
) != 0; }
607 void wxStyledTextEvent::CopyObject(wxObject
& obj
) const {
608 wxCommandEvent::CopyObject(obj
);
610 wxStyledTextEvent
* o
= (wxStyledTextEvent
*)&obj
;
611 o
->m_position
= m_position
;
613 o
->m_modifiers
= m_modifiers
;
614 o
->m_modificationType
= m_modificationType
;
616 o
->m_length
= m_length
;
617 o
->m_linesAdded
= m_linesAdded
;
619 o
->m_foldLevelNow
= m_foldLevelNow
;
620 o
->m_foldLevelPrev
= m_foldLevelPrev
;
622 o
->m_margin
= m_margin
;
624 o
->m_message
= m_message
;
625 o
->m_wParam
= m_wParam
;
626 o
->m_lParam
= m_lParam
;
628 o
->m_listType
= m_listType
;
632 o
->m_dragText
= m_dragText
;
633 o
->m_dragAllowMove
=m_dragAllowMove
;
634 o
->m_dragResult
= m_dragResult
;
637 //----------------------------------------------------------------------
638 //----------------------------------------------------------------------