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 lmEiffel
;
36 extern LexerModule lmHTML
;
37 extern LexerModule lmLISP
;
38 extern LexerModule lmLua
;
39 extern LexerModule lmBatch
; // In LexOthers.cxx
40 extern LexerModule lmPascal
;
41 extern LexerModule lmPerl
;
42 extern LexerModule lmPython
;
43 extern LexerModule lmRuby
;
44 extern LexerModule lmSQL
;
45 extern LexerModule lmVB
;
71 //----------------------------------------------------------------------
73 const wxChar
* wxSTCNameStr
= "stcwindow";
75 DEFINE_EVENT_TYPE( wxEVT_STC_CHANGE
)
76 DEFINE_EVENT_TYPE( wxEVT_STC_STYLENEEDED
)
77 DEFINE_EVENT_TYPE( wxEVT_STC_CHARADDED
)
78 DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTREACHED
)
79 DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTLEFT
)
80 DEFINE_EVENT_TYPE( wxEVT_STC_ROMODIFYATTEMPT
)
81 DEFINE_EVENT_TYPE( wxEVT_STC_KEY
)
82 DEFINE_EVENT_TYPE( wxEVT_STC_DOUBLECLICK
)
83 DEFINE_EVENT_TYPE( wxEVT_STC_UPDATEUI
)
84 DEFINE_EVENT_TYPE( wxEVT_STC_MODIFIED
)
85 DEFINE_EVENT_TYPE( wxEVT_STC_MACRORECORD
)
86 DEFINE_EVENT_TYPE( wxEVT_STC_MARGINCLICK
)
87 DEFINE_EVENT_TYPE( wxEVT_STC_NEEDSHOWN
)
88 DEFINE_EVENT_TYPE( wxEVT_STC_POSCHANGED
)
89 DEFINE_EVENT_TYPE( wxEVT_STC_PAINTED
)
90 DEFINE_EVENT_TYPE( wxEVT_STC_USERLISTSELECTION
)
91 DEFINE_EVENT_TYPE( wxEVT_STC_URIDROPPED
)
92 DEFINE_EVENT_TYPE( wxEVT_STC_DWELLSTART
)
93 DEFINE_EVENT_TYPE( wxEVT_STC_DWELLEND
)
98 BEGIN_EVENT_TABLE(wxStyledTextCtrl
, wxControl
)
99 EVT_PAINT (wxStyledTextCtrl::OnPaint
)
100 EVT_SCROLLWIN (wxStyledTextCtrl::OnScrollWin
)
101 EVT_SIZE (wxStyledTextCtrl::OnSize
)
102 EVT_LEFT_DOWN (wxStyledTextCtrl::OnMouseLeftDown
)
104 // Let Scintilla see the double click as a second click
105 EVT_LEFT_DCLICK (wxStyledTextCtrl::OnMouseLeftDown
)
107 EVT_MOTION (wxStyledTextCtrl::OnMouseMove
)
108 EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp
)
109 EVT_CONTEXT_MENU (wxStyledTextCtrl::OnContextMenu
)
110 EVT_MOUSEWHEEL (wxStyledTextCtrl::OnMouseWheel
)
111 EVT_CHAR (wxStyledTextCtrl::OnChar
)
112 EVT_KEY_DOWN (wxStyledTextCtrl::OnKeyDown
)
113 EVT_KILL_FOCUS (wxStyledTextCtrl::OnLoseFocus
)
114 EVT_SET_FOCUS (wxStyledTextCtrl::OnGainFocus
)
115 EVT_SYS_COLOUR_CHANGED (wxStyledTextCtrl::OnSysColourChanged
)
116 EVT_ERASE_BACKGROUND (wxStyledTextCtrl::OnEraseBackground
)
117 EVT_MENU_RANGE (-1, -1, wxStyledTextCtrl::OnMenu
)
118 EVT_LISTBOX_DCLICK (-1, wxStyledTextCtrl::OnListBox
)
122 IMPLEMENT_CLASS(wxStyledTextCtrl
, wxControl
)
123 IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent
, wxCommandEvent
)
125 //----------------------------------------------------------------------
126 // Constructor and Destructor
128 wxStyledTextCtrl::wxStyledTextCtrl(wxWindow
*parent
,
133 const wxString
& name
) :
134 wxControl(parent
, id
, pos
, size
,
135 style
| wxVSCROLL
| wxHSCROLL
| wxWANTS_CHARS
| wxCLIP_CHILDREN
,
136 wxDefaultValidator
, name
)
138 m_swx
= new ScintillaWX(this);
143 wxStyledTextCtrl::~wxStyledTextCtrl() {
148 //----------------------------------------------------------------------
150 long wxStyledTextCtrl::SendMsg(int msg
, long wp
, long lp
) {
152 return m_swx
->WndProc(msg
, wp
, lp
);
160 #define MAKELONG(a, b) ((a) | ((b) << 16))
163 static long wxColourAsLong(const wxColour
& co
) {
164 return (((long)co
.Blue() << 16) |
165 ((long)co
.Green() << 8) |
169 static wxColour
wxColourFromLong(long c
) {
171 clr
.Set(c
& 0xff, (c
>> 8) & 0xff, (c
>> 16) & 0xff);
176 static wxColour
wxColourFromSpec(const wxString
& spec
) {
177 // spec should be #RRGGBB
179 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
180 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
181 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
182 return wxColour(red
, green
, blue
);
186 //----------------------------------------------------------------------
187 // BEGIN generated section. The following code is automatically generated
188 // by gen_iface.py from the contents of Scintilla.iface. Do not edit
189 // this file. Edit stc.cpp.in or gen_iface.py instead and regenerate.
193 // END of generated section
194 //----------------------------------------------------------------------
197 // Returns the line number of the line with the caret.
198 int wxStyledTextCtrl::GetCurrentLine() {
199 int line
= LineFromPosition(GetCurrentPos());
204 // Extract style settings from a spec-string which is composed of one or
205 // more of the following comma separated elements:
207 // bold turns on bold
208 // italic turns on italics
209 // fore:#RRGGBB sets the foreground colour
210 // back:#RRGGBB sets the background colour
211 // face:[facename] sets the font face name to use
212 // size:[num] sets the font size in points
213 // eol turns on eol filling
214 // underline turns on underlining
216 void wxStyledTextCtrl::StyleSetSpec(int styleNum
, const wxString
& spec
) {
218 wxStringTokenizer
tkz(spec
, ",");
219 while (tkz
.HasMoreTokens()) {
220 wxString token
= tkz
.GetNextToken();
222 wxString option
= token
.BeforeFirst(':');
223 wxString val
= token
.AfterFirst(':');
225 if (option
== "bold")
226 StyleSetBold(styleNum
, true);
228 else if (option
== "italic")
229 StyleSetItalic(styleNum
, true);
231 else if (option
== "underline")
232 StyleSetUnderline(styleNum
, true);
234 else if (option
== "eol")
235 StyleSetEOLFilled(styleNum
, true);
237 else if (option
== "size") {
239 if (val
.ToLong(&points
))
240 StyleSetSize(styleNum
, points
);
243 else if (option
== "face")
244 StyleSetFaceName(styleNum
, val
);
246 else if (option
== "fore")
247 StyleSetForeground(styleNum
, wxColourFromSpec(val
));
249 else if (option
== "back")
250 StyleSetBackground(styleNum
, wxColourFromSpec(val
));
255 // Set style size, face, bold, italic, and underline attributes from
256 // a wxFont's attributes.
257 void wxStyledTextCtrl::StyleSetFont(int styleNum
, wxFont
& font
) {
258 int size
= font
.GetPointSize();
259 wxString faceName
= font
.GetFaceName();
260 bool bold
= font
.GetWeight() == wxBOLD
;
261 bool italic
= font
.GetStyle() != wxNORMAL
;
262 bool under
= font
.GetUnderlined();
264 // TODO: add encoding/charset mapping
265 StyleSetFontAttr(styleNum
, size
, faceName
, bold
, italic
, under
);
268 // Set all font style attributes at once.
269 void wxStyledTextCtrl::StyleSetFontAttr(int styleNum
, int size
,
270 const wxString
& faceName
,
271 bool bold
, bool italic
,
273 StyleSetSize(styleNum
, size
);
274 StyleSetFaceName(styleNum
, faceName
);
275 StyleSetBold(styleNum
, bold
);
276 StyleSetItalic(styleNum
, italic
);
277 StyleSetUnderline(styleNum
, underline
);
279 // TODO: add encoding/charset mapping
283 // Perform one of the operations defined by the wxSTC_CMD_* constants.
284 void wxStyledTextCtrl::CmdKeyExecute(int cmd
) {
289 // Set the left and right margin in the edit area, measured in pixels.
290 void wxStyledTextCtrl::SetMargins(int left
, int right
) {
292 SetMarginRight(right
);
296 // Retrieve the start and end positions of the current selection.
297 void wxStyledTextCtrl::GetSelection(int* startPos
, int* endPos
) {
298 if (startPos
!= NULL
)
299 *startPos
= SendMsg(SCI_GETSELECTIONSTART
);
301 *endPos
= SendMsg(SCI_GETSELECTIONEND
);
305 // Retrieve the point in the window where a position is displayed.
306 wxPoint
wxStyledTextCtrl::PointFromPosition(int pos
) {
307 int x
= SendMsg(SCI_POINTXFROMPOSITION
, 0, pos
);
308 int y
= SendMsg(SCI_POINTYFROMPOSITION
, 0, pos
);
309 return wxPoint(x
, y
);
312 // Scroll enough to make the given line visible
313 void wxStyledTextCtrl::ScrollToLine(int line
) {
314 m_swx
->DoScrollToLine(line
);
318 // Scroll enough to make the given column visible
319 void wxStyledTextCtrl::ScrollToColumn(int column
) {
320 m_swx
->DoScrollToColumn(column
);
325 //----------------------------------------------------------------------
328 void wxStyledTextCtrl::OnPaint(wxPaintEvent
& evt
) {
330 wxRegion region
= GetUpdateRegion();
332 m_swx
->DoPaint(&dc
, region
.GetBox());
335 void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent
& evt
) {
336 if (evt
.GetOrientation() == wxHORIZONTAL
)
337 m_swx
->DoHScroll(evt
.GetEventType(), evt
.GetPosition());
339 m_swx
->DoVScroll(evt
.GetEventType(), evt
.GetPosition());
342 void wxStyledTextCtrl::OnSize(wxSizeEvent
& evt
) {
343 wxSize sz
= GetClientSize();
344 m_swx
->DoSize(sz
.x
, sz
.y
);
347 void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent
& evt
) {
348 wxPoint pt
= evt
.GetPosition();
349 m_swx
->DoButtonDown(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
350 evt
.ShiftDown(), evt
.ControlDown(), evt
.AltDown());
353 void wxStyledTextCtrl::OnMouseMove(wxMouseEvent
& evt
) {
354 wxPoint pt
= evt
.GetPosition();
355 m_swx
->DoButtonMove(Point(pt
.x
, pt
.y
));
358 void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent
& evt
) {
359 wxPoint pt
= evt
.GetPosition();
360 m_swx
->DoButtonUp(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
365 void wxStyledTextCtrl::OnContextMenu(wxContextMenuEvent
& evt
) {
366 wxPoint pt
= evt
.GetPosition();
367 ScreenToClient(&pt
.x
, &pt
.y
);
368 m_swx
->DoContextMenu(Point(pt
.x
, pt
.y
));
372 void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent
& evt
) {
373 m_swx
->DoMouseWheel(evt
.GetWheelRotation(),
375 evt
.GetLinesPerAction(),
380 void wxStyledTextCtrl::OnChar(wxKeyEvent
& evt
) {
381 long key
= evt
.KeyCode();
382 if ((key
> WXK_ESCAPE
) &&
383 (key
!= WXK_DELETE
) && (key
< 255) &&
384 !evt
.ControlDown() && !evt
.AltDown()) {
386 m_swx
->DoAddChar(key
);
393 void wxStyledTextCtrl::OnKeyDown(wxKeyEvent
& evt
) {
394 long key
= evt
.KeyCode();
395 //key = toupper(key); //**** ????
396 bool consumed
= FALSE
;
397 int processed
= m_swx
->DoKeyDown(key
,
402 if (!processed
&& !consumed
)
406 void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent
& evt
) {
407 m_swx
->DoLoseFocus();
410 void wxStyledTextCtrl::OnGainFocus(wxFocusEvent
& evt
) {
411 m_swx
->DoGainFocus();
414 void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& evt
) {
415 m_swx
->DoSysColourChange();
418 void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent
& evt
) {
419 // do nothing to help avoid flashing
424 void wxStyledTextCtrl::OnMenu(wxCommandEvent
& evt
) {
425 m_swx
->DoCommand(evt
.GetId());
429 void wxStyledTextCtrl::OnListBox(wxCommandEvent
& evt
) {
430 m_swx
->DoOnListBox();
434 //----------------------------------------------------------------------
435 // Turn notifications from Scintilla into events
438 void wxStyledTextCtrl::NotifyChange() {
439 wxStyledTextEvent
evt(wxEVT_STC_CHANGE
, GetId());
440 GetEventHandler()->ProcessEvent(evt
);
443 void wxStyledTextCtrl::NotifyParent(SCNotification
* _scn
) {
444 SCNotification
& scn
= *_scn
;
445 wxStyledTextEvent
evt(0, GetId());
447 evt
.SetPosition(scn
.position
);
449 evt
.SetModifiers(scn
.modifiers
);
451 switch (scn
.nmhdr
.code
) {
452 case SCN_STYLENEEDED
:
453 evt
.SetEventType(wxEVT_STC_STYLENEEDED
);
457 evt
.SetEventType(wxEVT_STC_CHARADDED
);
460 case SCN_SAVEPOINTREACHED
:
461 evt
.SetEventType(wxEVT_STC_SAVEPOINTREACHED
);
464 case SCN_SAVEPOINTLEFT
:
465 evt
.SetEventType(wxEVT_STC_SAVEPOINTLEFT
);
468 case SCN_MODIFYATTEMPTRO
:
469 evt
.SetEventType(wxEVT_STC_ROMODIFYATTEMPT
);
473 evt
.SetEventType(wxEVT_STC_KEY
);
476 case SCN_DOUBLECLICK
:
477 evt
.SetEventType(wxEVT_STC_DOUBLECLICK
);
481 evt
.SetEventType(wxEVT_STC_UPDATEUI
);
485 evt
.SetEventType(wxEVT_STC_MODIFIED
);
486 evt
.SetModificationType(scn
.modificationType
);
488 evt
.SetText(wxString(scn
.text
, scn
.length
));
489 evt
.SetLength(scn
.length
);
490 evt
.SetLinesAdded(scn
.linesAdded
);
491 evt
.SetLine(scn
.line
);
492 evt
.SetFoldLevelNow(scn
.foldLevelNow
);
493 evt
.SetFoldLevelPrev(scn
.foldLevelPrev
);
496 case SCN_MACRORECORD
:
497 evt
.SetEventType(wxEVT_STC_MACRORECORD
);
498 evt
.SetMessage(scn
.message
);
499 evt
.SetWParam(scn
.wParam
);
500 evt
.SetLParam(scn
.lParam
);
503 case SCN_MARGINCLICK
:
504 evt
.SetEventType(wxEVT_STC_MARGINCLICK
);
505 evt
.SetMargin(scn
.margin
);
509 evt
.SetEventType(wxEVT_STC_NEEDSHOWN
);
510 evt
.SetLength(scn
.length
);
514 evt
.SetEventType(wxEVT_STC_POSCHANGED
);
518 evt
.SetEventType(wxEVT_STC_PAINTED
);
521 case SCN_USERLISTSELECTION
:
522 evt
.SetEventType(wxEVT_STC_USERLISTSELECTION
);
523 evt
.SetListType(scn
.listType
);
524 evt
.SetText(scn
.text
);
528 evt
.SetEventType(wxEVT_STC_URIDROPPED
);
529 evt
.SetText(scn
.text
);
533 evt
.SetEventType(wxEVT_STC_DWELLSTART
);
539 evt
.SetEventType(wxEVT_STC_DWELLEND
);
548 GetEventHandler()->ProcessEvent(evt
);
553 //----------------------------------------------------------------------
554 //----------------------------------------------------------------------
555 //----------------------------------------------------------------------
557 wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType
, int id
)
558 : wxCommandEvent(commandType
, id
)
563 m_modificationType
= 0;
578 bool wxStyledTextEvent::GetShift() const { return (m_modifiers
& SCI_SHIFT
) != 0; }
579 bool wxStyledTextEvent::GetControl() const { return (m_modifiers
& SCI_CTRL
) != 0; }
580 bool wxStyledTextEvent::GetAlt() const { return (m_modifiers
& SCI_ALT
) != 0; }
582 void wxStyledTextEvent::CopyObject(wxObject
& obj
) const {
583 wxCommandEvent::CopyObject(obj
);
585 wxStyledTextEvent
* o
= (wxStyledTextEvent
*)&obj
;
586 o
->m_position
= m_position
;
588 o
->m_modifiers
= m_modifiers
;
589 o
->m_modificationType
= m_modificationType
;
591 o
->m_length
= m_length
;
592 o
->m_linesAdded
= m_linesAdded
;
594 o
->m_foldLevelNow
= m_foldLevelNow
;
595 o
->m_foldLevelPrev
= m_foldLevelPrev
;
597 o
->m_margin
= m_margin
;
599 o
->m_message
= m_message
;
600 o
->m_wParam
= m_wParam
;
601 o
->m_lParam
= m_lParam
;
603 o
->m_listType
= m_listType
;
609 //----------------------------------------------------------------------
610 //----------------------------------------------------------------------