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 lmCPP
;
32 extern LexerModule lmHTML
;
33 extern LexerModule lmXML
;
34 extern LexerModule lmProps
;
35 extern LexerModule lmErrorList
;
36 extern LexerModule lmMake
;
37 extern LexerModule lmBatch
;
38 extern LexerModule lmPerl
;
39 extern LexerModule lmPython
;
40 extern LexerModule lmSQL
;
41 extern LexerModule lmVB
;
65 //----------------------------------------------------------------------
67 const wxChar
* wxSTCNameStr
= "stcwindow";
70 DEFINE_EVENT_TYPE( wxEVT_STC_CHANGE
)
71 DEFINE_EVENT_TYPE( wxEVT_STC_STYLENEEDED
)
72 DEFINE_EVENT_TYPE( wxEVT_STC_CHARADDED
)
73 DEFINE_EVENT_TYPE( wxEVT_STC_UPDATEUI
)
74 DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTREACHED
)
75 DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTLEFT
)
76 DEFINE_EVENT_TYPE( wxEVT_STC_ROMODIFYATTEMPT
)
77 DEFINE_EVENT_TYPE( wxEVT_STC_DOUBLECLICK
)
78 DEFINE_EVENT_TYPE( wxEVT_STC_MODIFIED
)
79 DEFINE_EVENT_TYPE( wxEVT_STC_KEY
)
80 DEFINE_EVENT_TYPE( wxEVT_STC_MACRORECORD
)
81 DEFINE_EVENT_TYPE( wxEVT_STC_MARGINCLICK
)
82 DEFINE_EVENT_TYPE( wxEVT_STC_NEEDSHOWN
)
83 DEFINE_EVENT_TYPE( wxEVT_STC_POSCHANGED
)
86 BEGIN_EVENT_TABLE(wxStyledTextCtrl
, wxControl
)
87 EVT_PAINT (wxStyledTextCtrl::OnPaint
)
88 EVT_SCROLLWIN (wxStyledTextCtrl::OnScrollWin
)
89 EVT_SIZE (wxStyledTextCtrl::OnSize
)
90 EVT_LEFT_DOWN (wxStyledTextCtrl::OnMouseLeftDown
)
91 EVT_MOTION (wxStyledTextCtrl::OnMouseMove
)
92 EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp
)
93 EVT_RIGHT_UP (wxStyledTextCtrl::OnMouseRightUp
)
94 EVT_MOUSEWHEEL (wxStyledTextCtrl::OnMouseWheel
)
95 EVT_CHAR (wxStyledTextCtrl::OnChar
)
96 EVT_KEY_DOWN (wxStyledTextCtrl::OnKeyDown
)
97 EVT_KILL_FOCUS (wxStyledTextCtrl::OnLoseFocus
)
98 EVT_SET_FOCUS (wxStyledTextCtrl::OnGainFocus
)
99 EVT_SYS_COLOUR_CHANGED (wxStyledTextCtrl::OnSysColourChanged
)
100 EVT_ERASE_BACKGROUND (wxStyledTextCtrl::OnEraseBackground
)
101 EVT_MENU_RANGE (-1, -1, wxStyledTextCtrl::OnMenu
)
102 EVT_LISTBOX_DCLICK (-1, wxStyledTextCtrl::OnListBox
)
106 IMPLEMENT_CLASS(wxStyledTextCtrl
, wxControl
)
107 IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent
, wxCommandEvent
)
109 //----------------------------------------------------------------------
110 // Constructor and Destructor
112 wxStyledTextCtrl::wxStyledTextCtrl(wxWindow
*parent
,
117 const wxString
& name
) :
118 wxControl(parent
, id
, pos
, size
,
119 style
| wxVSCROLL
| wxHSCROLL
| wxWANTS_CHARS
| wxCLIP_CHILDREN
,
120 wxDefaultValidator
, name
)
122 m_swx
= new ScintillaWX(this);
127 wxStyledTextCtrl::~wxStyledTextCtrl() {
132 //----------------------------------------------------------------------
134 long wxStyledTextCtrl::SendMsg(int msg
, long wp
, long lp
) {
136 return m_swx
->WndProc(msg
, wp
, lp
);
144 #define MAKELONG(a, b) ((a) | ((b) << 16))
147 static long wxColourAsLong(const wxColour
& co
) {
148 return (((long)co
.Blue() << 16) |
149 ((long)co
.Green() << 8) |
153 static wxColour
wxColourFromLong(long c
) {
155 clr
.Set(c
& 0xff, (c
>> 8) & 0xff, (c
>> 16) & 0xff);
160 static wxColour
wxColourFromSpec(const wxString
& spec
) {
161 // spec should be #RRGGBB
163 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
164 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
165 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
166 return wxColour(red
, green
, blue
);
170 //----------------------------------------------------------------------
171 // BEGIN generated section. The following code is automatically generated
172 // by gen_iface.py from the contents of Scintilla.iface. Do not edit
173 // this file. Edit stc.cpp.in or gen_iface.py instead and regenerate.
177 // END of generated section
178 //----------------------------------------------------------------------
181 // Returns the line number of the line with the caret.
182 int wxStyledTextCtrl::GetCurrentLine() {
183 int line
= LineFromPosition(GetCurrentPos());
188 // Extract style settings from a spec-string which is composed of one or
189 // more of the following comma separated elements:
191 // bold turns on bold
192 // italic turns on italics
193 // fore:#RRGGBB sets the foreground colour
194 // back:#RRGGBB sets the background colour
195 // face:[facename] sets the font face name to use
196 // size:[num] sets the font size in points
197 // eol turns on eol filling
198 // underline turns on underlining
200 void wxStyledTextCtrl::StyleSetSpec(int styleNum
, const wxString
& spec
) {
202 wxStringTokenizer
tkz(spec
, ",");
203 while (tkz
.HasMoreTokens()) {
204 wxString token
= tkz
.GetNextToken();
206 wxString option
= token
.BeforeFirst(':');
207 wxString val
= token
.AfterFirst(':');
209 if (option
== "bold")
210 StyleSetBold(styleNum
, true);
212 else if (option
== "italic")
213 StyleSetItalic(styleNum
, true);
215 else if (option
== "underline")
216 StyleSetUnderline(styleNum
, true);
218 else if (option
== "eol")
219 StyleSetEOLFilled(styleNum
, true);
221 else if (option
== "size") {
223 if (val
.ToLong(&points
))
224 StyleSetSize(styleNum
, points
);
227 else if (option
== "face")
228 StyleSetFaceName(styleNum
, val
);
230 else if (option
== "fore")
231 StyleSetForeground(styleNum
, wxColourFromSpec(val
));
233 else if (option
== "back")
234 StyleSetBackground(styleNum
, wxColourFromSpec(val
));
239 // Set style size, face, bold, italic, and underline attributes from
240 // a wxFont's attributes.
241 void wxStyledTextCtrl::StyleSetFont(int styleNum
, wxFont
& font
) {
242 int size
= font
.GetPointSize();
243 wxString faceName
= font
.GetFaceName();
244 bool bold
= font
.GetWeight() == wxBOLD
;
245 bool italic
= font
.GetStyle() != wxNORMAL
;
246 bool under
= font
.GetUnderlined();
248 // TODO: add encoding/charset mapping
249 StyleSetFontAttr(styleNum
, size
, faceName
, bold
, italic
, under
);
252 // Set all font style attributes at once.
253 void wxStyledTextCtrl::StyleSetFontAttr(int styleNum
, int size
,
254 const wxString
& faceName
,
255 bool bold
, bool italic
,
257 StyleSetSize(styleNum
, size
);
258 StyleSetFaceName(styleNum
, faceName
);
259 StyleSetBold(styleNum
, bold
);
260 StyleSetItalic(styleNum
, italic
);
261 StyleSetUnderline(styleNum
, underline
);
263 // TODO: add encoding/charset mapping
267 // Perform one of the operations defined by the wxSTC_CMD_* constants.
268 void wxStyledTextCtrl::CmdKeyExecute(int cmd
) {
273 // Set the left and right margin in the edit area, measured in pixels.
274 void wxStyledTextCtrl::SetMargins(int left
, int right
) {
276 SetMarginRight(right
);
280 // Retrieve the start and end positions of the current selection.
281 void wxStyledTextCtrl::GetSelection(int* startPos
, int* endPos
) {
282 if (startPos
!= NULL
)
283 *startPos
= SendMsg(SCI_GETSELECTIONSTART
);
285 *endPos
= SendMsg(SCI_GETSELECTIONEND
);
289 // Retrieve the point in the window where a position is displayed.
290 wxPoint
wxStyledTextCtrl::PointFromPosition(int pos
) {
291 int x
= SendMsg(SCI_POINTXFROMPOSITION
, 0, pos
);
292 int y
= SendMsg(SCI_POINTYFROMPOSITION
, 0, pos
);
293 return wxPoint(x
, y
);
296 // Scroll enough to make the given line visible
297 void wxStyledTextCtrl::ScrollToLine(int line
) {
298 m_swx
->DoScrollToLine(line
);
302 // Scroll enough to make the given column visible
303 void wxStyledTextCtrl::ScrollToColumn(int column
) {
304 m_swx
->DoScrollToColumn(column
);
309 //----------------------------------------------------------------------
312 void wxStyledTextCtrl::OnPaint(wxPaintEvent
& evt
) {
314 wxRegion region
= GetUpdateRegion();
316 m_swx
->DoPaint(&dc
, region
.GetBox());
319 void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent
& evt
) {
320 if (evt
.GetOrientation() == wxHORIZONTAL
)
321 m_swx
->DoHScroll(evt
.GetEventType(), evt
.GetPosition());
323 m_swx
->DoVScroll(evt
.GetEventType(), evt
.GetPosition());
326 void wxStyledTextCtrl::OnSize(wxSizeEvent
& evt
) {
327 wxSize sz
= GetClientSize();
328 m_swx
->DoSize(sz
.x
, sz
.y
);
331 void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent
& evt
) {
332 wxPoint pt
= evt
.GetPosition();
333 m_swx
->DoButtonDown(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
334 evt
.ShiftDown(), evt
.ControlDown(), evt
.AltDown());
337 void wxStyledTextCtrl::OnMouseMove(wxMouseEvent
& evt
) {
338 wxPoint pt
= evt
.GetPosition();
339 m_swx
->DoButtonMove(Point(pt
.x
, pt
.y
));
342 void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent
& evt
) {
343 wxPoint pt
= evt
.GetPosition();
344 m_swx
->DoButtonUp(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
349 void wxStyledTextCtrl::OnMouseRightUp(wxMouseEvent
& evt
) {
350 wxPoint pt
= evt
.GetPosition();
351 m_swx
->DoContextMenu(Point(pt
.x
, pt
.y
));
355 void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent
& evt
) {
356 m_swx
->DoMouseWheel(evt
.GetWheelRotation(),
358 evt
.GetLinesPerAction());
362 void wxStyledTextCtrl::OnChar(wxKeyEvent
& evt
) {
363 long key
= evt
.KeyCode();
364 if ((key
> WXK_ESCAPE
) &&
365 (key
!= WXK_DELETE
) && (key
< 255) &&
366 !evt
.ControlDown() && !evt
.AltDown()) {
368 m_swx
->DoAddChar(key
);
375 void wxStyledTextCtrl::OnKeyDown(wxKeyEvent
& evt
) {
376 long key
= evt
.KeyCode();
378 int processed
= m_swx
->DoKeyDown(key
, evt
.ShiftDown(),
379 evt
.ControlDown(), evt
.AltDown());
384 void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent
& evt
) {
385 m_swx
->DoLoseFocus();
388 void wxStyledTextCtrl::OnGainFocus(wxFocusEvent
& evt
) {
389 m_swx
->DoGainFocus();
392 void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& evt
) {
393 m_swx
->DoSysColourChange();
396 void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent
& evt
) {
397 // do nothing to help avoid flashing
402 void wxStyledTextCtrl::OnMenu(wxCommandEvent
& evt
) {
403 m_swx
->DoCommand(evt
.GetId());
407 void wxStyledTextCtrl::OnListBox(wxCommandEvent
& evt
) {
408 m_swx
->DoOnListBox();
412 //----------------------------------------------------------------------
413 // Turn notifications from Scintilla into events
416 void wxStyledTextCtrl::NotifyChange() {
417 wxStyledTextEvent
evt(wxEVT_STC_CHANGE
, GetId());
418 GetEventHandler()->ProcessEvent(evt
);
421 void wxStyledTextCtrl::NotifyParent(SCNotification
* _scn
) {
422 SCNotification
& scn
= *_scn
;
424 switch (scn
.nmhdr
.code
) {
425 case SCN_STYLENEEDED
:
426 eventType
= wxEVT_STC_STYLENEEDED
;
429 eventType
= wxEVT_STC_CHARADDED
;
432 eventType
= wxEVT_STC_UPDATEUI
;
434 case SCN_SAVEPOINTREACHED
:
435 eventType
= wxEVT_STC_SAVEPOINTREACHED
;
437 case SCN_SAVEPOINTLEFT
:
438 eventType
= wxEVT_STC_SAVEPOINTLEFT
;
440 case SCN_MODIFYATTEMPTRO
:
441 eventType
= wxEVT_STC_ROMODIFYATTEMPT
;
443 case SCN_DOUBLECLICK
:
444 eventType
= wxEVT_STC_DOUBLECLICK
;
447 eventType
= wxEVT_STC_MODIFIED
;
450 eventType
= wxEVT_STC_KEY
;
452 case SCN_MACRORECORD
:
453 eventType
= wxEVT_STC_MACRORECORD
;
455 case SCN_MARGINCLICK
:
456 eventType
= wxEVT_STC_MARGINCLICK
;
459 eventType
= wxEVT_STC_NEEDSHOWN
;
462 eventType
= wxEVT_STC_POSCHANGED
;
466 wxStyledTextEvent
evt(eventType
, GetId());
467 evt
.SetPosition(scn
.position
);
469 evt
.SetModifiers(scn
.modifiers
);
470 if (eventType
== wxEVT_STC_MODIFIED
) {
471 evt
.SetModificationType(scn
.modificationType
);
473 evt
.SetText(wxString(scn
.text
, scn
.length
));
474 evt
.SetLength(scn
.length
);
475 evt
.SetLinesAdded(scn
.linesAdded
);
476 evt
.SetLine(scn
.line
);
477 evt
.SetFoldLevelNow(scn
.foldLevelNow
);
478 evt
.SetFoldLevelPrev(scn
.foldLevelPrev
);
480 if (eventType
== wxEVT_STC_MARGINCLICK
)
481 evt
.SetMargin(scn
.margin
);
482 if (eventType
== wxEVT_STC_MACRORECORD
) {
483 evt
.SetMessage(scn
.message
);
484 evt
.SetWParam(scn
.wParam
);
485 evt
.SetLParam(scn
.lParam
);
488 GetEventHandler()->ProcessEvent(evt
);
494 //----------------------------------------------------------------------
495 //----------------------------------------------------------------------
496 //----------------------------------------------------------------------
498 wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType
, int id
)
499 : wxCommandEvent(commandType
, id
)
504 m_modificationType
= 0;
518 bool wxStyledTextEvent::GetShift() const { return (m_modifiers
& SCI_SHIFT
) != 0; }
519 bool wxStyledTextEvent::GetControl() const { return (m_modifiers
& SCI_CTRL
) != 0; }
520 bool wxStyledTextEvent::GetAlt() const { return (m_modifiers
& SCI_ALT
) != 0; }
522 void wxStyledTextEvent::CopyObject(wxObject
& obj
) const {
523 wxCommandEvent::CopyObject(obj
);
525 wxStyledTextEvent
* o
= (wxStyledTextEvent
*)&obj
;
526 o
->m_position
= m_position
;
528 o
->m_modifiers
= m_modifiers
;
529 o
->m_modificationType
= m_modificationType
;
531 o
->m_length
= m_length
;
532 o
->m_linesAdded
= m_linesAdded
;
534 o
->m_foldLevelNow
= m_foldLevelNow
;
535 o
->m_foldLevelPrev
= m_foldLevelPrev
;
537 o
->m_margin
= m_margin
;
539 o
->m_message
= m_message
;
540 o
->m_wParam
= m_wParam
;
541 o
->m_lParam
= m_lParam
;
547 //----------------------------------------------------------------------
548 //----------------------------------------------------------------------