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
)
92 // Let Scintilla see the double click as a second click
93 EVT_LEFT_DCLICK (wxStyledTextCtrl::OnMouseLeftDown
)
95 EVT_MOTION (wxStyledTextCtrl::OnMouseMove
)
96 EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp
)
97 EVT_RIGHT_UP (wxStyledTextCtrl::OnMouseRightUp
)
98 EVT_MOUSEWHEEL (wxStyledTextCtrl::OnMouseWheel
)
99 EVT_CHAR (wxStyledTextCtrl::OnChar
)
100 EVT_KEY_DOWN (wxStyledTextCtrl::OnKeyDown
)
101 EVT_KILL_FOCUS (wxStyledTextCtrl::OnLoseFocus
)
102 EVT_SET_FOCUS (wxStyledTextCtrl::OnGainFocus
)
103 EVT_SYS_COLOUR_CHANGED (wxStyledTextCtrl::OnSysColourChanged
)
104 EVT_ERASE_BACKGROUND (wxStyledTextCtrl::OnEraseBackground
)
105 EVT_MENU_RANGE (-1, -1, wxStyledTextCtrl::OnMenu
)
106 EVT_LISTBOX_DCLICK (-1, wxStyledTextCtrl::OnListBox
)
110 IMPLEMENT_CLASS(wxStyledTextCtrl
, wxControl
)
111 IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent
, wxCommandEvent
)
113 //----------------------------------------------------------------------
114 // Constructor and Destructor
116 wxStyledTextCtrl::wxStyledTextCtrl(wxWindow
*parent
,
121 const wxString
& name
) :
122 wxControl(parent
, id
, pos
, size
,
123 style
| wxVSCROLL
| wxHSCROLL
| wxWANTS_CHARS
| wxCLIP_CHILDREN
,
124 wxDefaultValidator
, name
)
126 m_swx
= new ScintillaWX(this);
131 wxStyledTextCtrl::~wxStyledTextCtrl() {
136 //----------------------------------------------------------------------
138 long wxStyledTextCtrl::SendMsg(int msg
, long wp
, long lp
) {
140 return m_swx
->WndProc(msg
, wp
, lp
);
148 #define MAKELONG(a, b) ((a) | ((b) << 16))
151 static long wxColourAsLong(const wxColour
& co
) {
152 return (((long)co
.Blue() << 16) |
153 ((long)co
.Green() << 8) |
157 static wxColour
wxColourFromLong(long c
) {
159 clr
.Set(c
& 0xff, (c
>> 8) & 0xff, (c
>> 16) & 0xff);
164 static wxColour
wxColourFromSpec(const wxString
& spec
) {
165 // spec should be #RRGGBB
167 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
168 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
169 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
170 return wxColour(red
, green
, blue
);
174 //----------------------------------------------------------------------
175 // BEGIN generated section. The following code is automatically generated
176 // by gen_iface.py from the contents of Scintilla.iface. Do not edit
177 // this file. Edit stc.cpp.in or gen_iface.py instead and regenerate.
181 // END of generated section
182 //----------------------------------------------------------------------
185 // Returns the line number of the line with the caret.
186 int wxStyledTextCtrl::GetCurrentLine() {
187 int line
= LineFromPosition(GetCurrentPos());
192 // Extract style settings from a spec-string which is composed of one or
193 // more of the following comma separated elements:
195 // bold turns on bold
196 // italic turns on italics
197 // fore:#RRGGBB sets the foreground colour
198 // back:#RRGGBB sets the background colour
199 // face:[facename] sets the font face name to use
200 // size:[num] sets the font size in points
201 // eol turns on eol filling
202 // underline turns on underlining
204 void wxStyledTextCtrl::StyleSetSpec(int styleNum
, const wxString
& spec
) {
206 wxStringTokenizer
tkz(spec
, ",");
207 while (tkz
.HasMoreTokens()) {
208 wxString token
= tkz
.GetNextToken();
210 wxString option
= token
.BeforeFirst(':');
211 wxString val
= token
.AfterFirst(':');
213 if (option
== "bold")
214 StyleSetBold(styleNum
, true);
216 else if (option
== "italic")
217 StyleSetItalic(styleNum
, true);
219 else if (option
== "underline")
220 StyleSetUnderline(styleNum
, true);
222 else if (option
== "eol")
223 StyleSetEOLFilled(styleNum
, true);
225 else if (option
== "size") {
227 if (val
.ToLong(&points
))
228 StyleSetSize(styleNum
, points
);
231 else if (option
== "face")
232 StyleSetFaceName(styleNum
, val
);
234 else if (option
== "fore")
235 StyleSetForeground(styleNum
, wxColourFromSpec(val
));
237 else if (option
== "back")
238 StyleSetBackground(styleNum
, wxColourFromSpec(val
));
243 // Set style size, face, bold, italic, and underline attributes from
244 // a wxFont's attributes.
245 void wxStyledTextCtrl::StyleSetFont(int styleNum
, wxFont
& font
) {
246 int size
= font
.GetPointSize();
247 wxString faceName
= font
.GetFaceName();
248 bool bold
= font
.GetWeight() == wxBOLD
;
249 bool italic
= font
.GetStyle() != wxNORMAL
;
250 bool under
= font
.GetUnderlined();
252 // TODO: add encoding/charset mapping
253 StyleSetFontAttr(styleNum
, size
, faceName
, bold
, italic
, under
);
256 // Set all font style attributes at once.
257 void wxStyledTextCtrl::StyleSetFontAttr(int styleNum
, int size
,
258 const wxString
& faceName
,
259 bool bold
, bool italic
,
261 StyleSetSize(styleNum
, size
);
262 StyleSetFaceName(styleNum
, faceName
);
263 StyleSetBold(styleNum
, bold
);
264 StyleSetItalic(styleNum
, italic
);
265 StyleSetUnderline(styleNum
, underline
);
267 // TODO: add encoding/charset mapping
271 // Perform one of the operations defined by the wxSTC_CMD_* constants.
272 void wxStyledTextCtrl::CmdKeyExecute(int cmd
) {
277 // Set the left and right margin in the edit area, measured in pixels.
278 void wxStyledTextCtrl::SetMargins(int left
, int right
) {
280 SetMarginRight(right
);
284 // Retrieve the start and end positions of the current selection.
285 void wxStyledTextCtrl::GetSelection(int* startPos
, int* endPos
) {
286 if (startPos
!= NULL
)
287 *startPos
= SendMsg(SCI_GETSELECTIONSTART
);
289 *endPos
= SendMsg(SCI_GETSELECTIONEND
);
293 // Retrieve the point in the window where a position is displayed.
294 wxPoint
wxStyledTextCtrl::PointFromPosition(int pos
) {
295 int x
= SendMsg(SCI_POINTXFROMPOSITION
, 0, pos
);
296 int y
= SendMsg(SCI_POINTYFROMPOSITION
, 0, pos
);
297 return wxPoint(x
, y
);
300 // Scroll enough to make the given line visible
301 void wxStyledTextCtrl::ScrollToLine(int line
) {
302 m_swx
->DoScrollToLine(line
);
306 // Scroll enough to make the given column visible
307 void wxStyledTextCtrl::ScrollToColumn(int column
) {
308 m_swx
->DoScrollToColumn(column
);
313 //----------------------------------------------------------------------
316 void wxStyledTextCtrl::OnPaint(wxPaintEvent
& evt
) {
318 wxRegion region
= GetUpdateRegion();
320 m_swx
->DoPaint(&dc
, region
.GetBox());
323 void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent
& evt
) {
324 if (evt
.GetOrientation() == wxHORIZONTAL
)
325 m_swx
->DoHScroll(evt
.GetEventType(), evt
.GetPosition());
327 m_swx
->DoVScroll(evt
.GetEventType(), evt
.GetPosition());
330 void wxStyledTextCtrl::OnSize(wxSizeEvent
& evt
) {
331 wxSize sz
= GetClientSize();
332 m_swx
->DoSize(sz
.x
, sz
.y
);
335 void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent
& evt
) {
336 wxPoint pt
= evt
.GetPosition();
337 m_swx
->DoButtonDown(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
338 evt
.ShiftDown(), evt
.ControlDown(), evt
.AltDown());
341 void wxStyledTextCtrl::OnMouseMove(wxMouseEvent
& evt
) {
342 wxPoint pt
= evt
.GetPosition();
343 m_swx
->DoButtonMove(Point(pt
.x
, pt
.y
));
346 void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent
& evt
) {
347 wxPoint pt
= evt
.GetPosition();
348 m_swx
->DoButtonUp(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
353 void wxStyledTextCtrl::OnMouseRightUp(wxMouseEvent
& evt
) {
354 wxPoint pt
= evt
.GetPosition();
355 m_swx
->DoContextMenu(Point(pt
.x
, pt
.y
));
359 void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent
& evt
) {
360 m_swx
->DoMouseWheel(evt
.GetWheelRotation(),
362 evt
.GetLinesPerAction());
366 void wxStyledTextCtrl::OnChar(wxKeyEvent
& evt
) {
367 long key
= evt
.KeyCode();
368 if ((key
> WXK_ESCAPE
) &&
369 (key
!= WXK_DELETE
) && (key
< 255) &&
370 !evt
.ControlDown() && !evt
.AltDown()) {
372 m_swx
->DoAddChar(key
);
379 void wxStyledTextCtrl::OnKeyDown(wxKeyEvent
& evt
) {
380 long key
= evt
.KeyCode();
382 int processed
= m_swx
->DoKeyDown(key
, evt
.ShiftDown(),
383 evt
.ControlDown(), evt
.AltDown());
388 void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent
& evt
) {
389 m_swx
->DoLoseFocus();
392 void wxStyledTextCtrl::OnGainFocus(wxFocusEvent
& evt
) {
393 m_swx
->DoGainFocus();
396 void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& evt
) {
397 m_swx
->DoSysColourChange();
400 void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent
& evt
) {
401 // do nothing to help avoid flashing
406 void wxStyledTextCtrl::OnMenu(wxCommandEvent
& evt
) {
407 m_swx
->DoCommand(evt
.GetId());
411 void wxStyledTextCtrl::OnListBox(wxCommandEvent
& evt
) {
412 m_swx
->DoOnListBox();
416 //----------------------------------------------------------------------
417 // Turn notifications from Scintilla into events
420 void wxStyledTextCtrl::NotifyChange() {
421 wxStyledTextEvent
evt(wxEVT_STC_CHANGE
, GetId());
422 GetEventHandler()->ProcessEvent(evt
);
425 void wxStyledTextCtrl::NotifyParent(SCNotification
* _scn
) {
426 SCNotification
& scn
= *_scn
;
428 switch (scn
.nmhdr
.code
) {
429 case SCN_STYLENEEDED
:
430 eventType
= wxEVT_STC_STYLENEEDED
;
433 eventType
= wxEVT_STC_CHARADDED
;
436 eventType
= wxEVT_STC_UPDATEUI
;
438 case SCN_SAVEPOINTREACHED
:
439 eventType
= wxEVT_STC_SAVEPOINTREACHED
;
441 case SCN_SAVEPOINTLEFT
:
442 eventType
= wxEVT_STC_SAVEPOINTLEFT
;
444 case SCN_MODIFYATTEMPTRO
:
445 eventType
= wxEVT_STC_ROMODIFYATTEMPT
;
447 case SCN_DOUBLECLICK
:
448 eventType
= wxEVT_STC_DOUBLECLICK
;
451 eventType
= wxEVT_STC_MODIFIED
;
454 eventType
= wxEVT_STC_KEY
;
456 case SCN_MACRORECORD
:
457 eventType
= wxEVT_STC_MACRORECORD
;
459 case SCN_MARGINCLICK
:
460 eventType
= wxEVT_STC_MARGINCLICK
;
463 eventType
= wxEVT_STC_NEEDSHOWN
;
466 eventType
= wxEVT_STC_POSCHANGED
;
470 wxStyledTextEvent
evt(eventType
, GetId());
471 evt
.SetPosition(scn
.position
);
473 evt
.SetModifiers(scn
.modifiers
);
474 if (eventType
== wxEVT_STC_MODIFIED
) {
475 evt
.SetModificationType(scn
.modificationType
);
477 evt
.SetText(wxString(scn
.text
, scn
.length
));
478 evt
.SetLength(scn
.length
);
479 evt
.SetLinesAdded(scn
.linesAdded
);
480 evt
.SetLine(scn
.line
);
481 evt
.SetFoldLevelNow(scn
.foldLevelNow
);
482 evt
.SetFoldLevelPrev(scn
.foldLevelPrev
);
484 if (eventType
== wxEVT_STC_MARGINCLICK
)
485 evt
.SetMargin(scn
.margin
);
486 if (eventType
== wxEVT_STC_MACRORECORD
) {
487 evt
.SetMessage(scn
.message
);
488 evt
.SetWParam(scn
.wParam
);
489 evt
.SetLParam(scn
.lParam
);
492 GetEventHandler()->ProcessEvent(evt
);
498 //----------------------------------------------------------------------
499 //----------------------------------------------------------------------
500 //----------------------------------------------------------------------
502 wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType
, int id
)
503 : wxCommandEvent(commandType
, id
)
508 m_modificationType
= 0;
522 bool wxStyledTextEvent::GetShift() const { return (m_modifiers
& SCI_SHIFT
) != 0; }
523 bool wxStyledTextEvent::GetControl() const { return (m_modifiers
& SCI_CTRL
) != 0; }
524 bool wxStyledTextEvent::GetAlt() const { return (m_modifiers
& SCI_ALT
) != 0; }
526 void wxStyledTextEvent::CopyObject(wxObject
& obj
) const {
527 wxCommandEvent::CopyObject(obj
);
529 wxStyledTextEvent
* o
= (wxStyledTextEvent
*)&obj
;
530 o
->m_position
= m_position
;
532 o
->m_modifiers
= m_modifiers
;
533 o
->m_modificationType
= m_modificationType
;
535 o
->m_length
= m_length
;
536 o
->m_linesAdded
= m_linesAdded
;
538 o
->m_foldLevelNow
= m_foldLevelNow
;
539 o
->m_foldLevelPrev
= m_foldLevelPrev
;
541 o
->m_margin
= m_margin
;
543 o
->m_message
= m_message
;
544 o
->m_wParam
= m_wParam
;
545 o
->m_lParam
= m_lParam
;
551 //----------------------------------------------------------------------
552 //----------------------------------------------------------------------