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);
140 m_lastKeyDownConsumed
= FALSE
;
144 wxStyledTextCtrl::~wxStyledTextCtrl() {
149 //----------------------------------------------------------------------
151 long wxStyledTextCtrl::SendMsg(int msg
, long wp
, long lp
) {
153 return m_swx
->WndProc(msg
, wp
, lp
);
161 #define MAKELONG(a, b) ((a) | ((b) << 16))
164 static long wxColourAsLong(const wxColour
& co
) {
165 return (((long)co
.Blue() << 16) |
166 ((long)co
.Green() << 8) |
170 static wxColour
wxColourFromLong(long c
) {
172 clr
.Set(c
& 0xff, (c
>> 8) & 0xff, (c
>> 16) & 0xff);
177 static wxColour
wxColourFromSpec(const wxString
& spec
) {
178 // spec should be #RRGGBB
180 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
181 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
182 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
183 return wxColour(red
, green
, blue
);
187 //----------------------------------------------------------------------
188 // BEGIN generated section. The following code is automatically generated
189 // by gen_iface.py from the contents of Scintilla.iface. Do not edit
190 // this file. Edit stc.cpp.in or gen_iface.py instead and regenerate.
194 // END of generated section
195 //----------------------------------------------------------------------
198 // Returns the line number of the line with the caret.
199 int wxStyledTextCtrl::GetCurrentLine() {
200 int line
= LineFromPosition(GetCurrentPos());
205 // Extract style settings from a spec-string which is composed of one or
206 // more of the following comma separated elements:
208 // bold turns on bold
209 // italic turns on italics
210 // fore:#RRGGBB sets the foreground colour
211 // back:#RRGGBB sets the background colour
212 // face:[facename] sets the font face name to use
213 // size:[num] sets the font size in points
214 // eol turns on eol filling
215 // underline turns on underlining
217 void wxStyledTextCtrl::StyleSetSpec(int styleNum
, const wxString
& spec
) {
219 wxStringTokenizer
tkz(spec
, ",");
220 while (tkz
.HasMoreTokens()) {
221 wxString token
= tkz
.GetNextToken();
223 wxString option
= token
.BeforeFirst(':');
224 wxString val
= token
.AfterFirst(':');
226 if (option
== "bold")
227 StyleSetBold(styleNum
, true);
229 else if (option
== "italic")
230 StyleSetItalic(styleNum
, true);
232 else if (option
== "underline")
233 StyleSetUnderline(styleNum
, true);
235 else if (option
== "eol")
236 StyleSetEOLFilled(styleNum
, true);
238 else if (option
== "size") {
240 if (val
.ToLong(&points
))
241 StyleSetSize(styleNum
, points
);
244 else if (option
== "face")
245 StyleSetFaceName(styleNum
, val
);
247 else if (option
== "fore")
248 StyleSetForeground(styleNum
, wxColourFromSpec(val
));
250 else if (option
== "back")
251 StyleSetBackground(styleNum
, wxColourFromSpec(val
));
256 // Set style size, face, bold, italic, and underline attributes from
257 // a wxFont's attributes.
258 void wxStyledTextCtrl::StyleSetFont(int styleNum
, wxFont
& font
) {
259 int size
= font
.GetPointSize();
260 wxString faceName
= font
.GetFaceName();
261 bool bold
= font
.GetWeight() == wxBOLD
;
262 bool italic
= font
.GetStyle() != wxNORMAL
;
263 bool under
= font
.GetUnderlined();
265 // TODO: add encoding/charset mapping
266 StyleSetFontAttr(styleNum
, size
, faceName
, bold
, italic
, under
);
269 // Set all font style attributes at once.
270 void wxStyledTextCtrl::StyleSetFontAttr(int styleNum
, int size
,
271 const wxString
& faceName
,
272 bool bold
, bool italic
,
274 StyleSetSize(styleNum
, size
);
275 StyleSetFaceName(styleNum
, faceName
);
276 StyleSetBold(styleNum
, bold
);
277 StyleSetItalic(styleNum
, italic
);
278 StyleSetUnderline(styleNum
, underline
);
280 // TODO: add encoding/charset mapping
284 // Perform one of the operations defined by the wxSTC_CMD_* constants.
285 void wxStyledTextCtrl::CmdKeyExecute(int cmd
) {
290 // Set the left and right margin in the edit area, measured in pixels.
291 void wxStyledTextCtrl::SetMargins(int left
, int right
) {
293 SetMarginRight(right
);
297 // Retrieve the start and end positions of the current selection.
298 void wxStyledTextCtrl::GetSelection(int* startPos
, int* endPos
) {
299 if (startPos
!= NULL
)
300 *startPos
= SendMsg(SCI_GETSELECTIONSTART
);
302 *endPos
= SendMsg(SCI_GETSELECTIONEND
);
306 // Retrieve the point in the window where a position is displayed.
307 wxPoint
wxStyledTextCtrl::PointFromPosition(int pos
) {
308 int x
= SendMsg(SCI_POINTXFROMPOSITION
, 0, pos
);
309 int y
= SendMsg(SCI_POINTYFROMPOSITION
, 0, pos
);
310 return wxPoint(x
, y
);
313 // Scroll enough to make the given line visible
314 void wxStyledTextCtrl::ScrollToLine(int line
) {
315 m_swx
->DoScrollToLine(line
);
319 // Scroll enough to make the given column visible
320 void wxStyledTextCtrl::ScrollToColumn(int column
) {
321 m_swx
->DoScrollToColumn(column
);
326 //----------------------------------------------------------------------
329 void wxStyledTextCtrl::OnPaint(wxPaintEvent
& evt
) {
331 wxRegion region
= GetUpdateRegion();
333 m_swx
->DoPaint(&dc
, region
.GetBox());
336 void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent
& evt
) {
337 if (evt
.GetOrientation() == wxHORIZONTAL
)
338 m_swx
->DoHScroll(evt
.GetEventType(), evt
.GetPosition());
340 m_swx
->DoVScroll(evt
.GetEventType(), evt
.GetPosition());
343 void wxStyledTextCtrl::OnSize(wxSizeEvent
& evt
) {
344 wxSize sz
= GetClientSize();
345 m_swx
->DoSize(sz
.x
, sz
.y
);
348 void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent
& evt
) {
349 wxPoint pt
= evt
.GetPosition();
350 m_swx
->DoButtonDown(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
351 evt
.ShiftDown(), evt
.ControlDown(), evt
.AltDown());
354 void wxStyledTextCtrl::OnMouseMove(wxMouseEvent
& evt
) {
355 wxPoint pt
= evt
.GetPosition();
356 m_swx
->DoButtonMove(Point(pt
.x
, pt
.y
));
359 void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent
& evt
) {
360 wxPoint pt
= evt
.GetPosition();
361 m_swx
->DoButtonUp(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
366 void wxStyledTextCtrl::OnContextMenu(wxContextMenuEvent
& evt
) {
367 wxPoint pt
= evt
.GetPosition();
368 ScreenToClient(&pt
.x
, &pt
.y
);
369 m_swx
->DoContextMenu(Point(pt
.x
, pt
.y
));
373 void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent
& evt
) {
374 m_swx
->DoMouseWheel(evt
.GetWheelRotation(),
376 evt
.GetLinesPerAction(),
381 void wxStyledTextCtrl::OnChar(wxKeyEvent
& evt
) {
382 long key
= evt
.KeyCode();
384 // printf("OnChar key:%%d consumed:%%d ctrl:%%d alt:%%d\n",
385 // key, m_lastKeyDownConsumed, evt.ControlDown(), evt.AltDown());
388 // \|@#¬[]{}?£$~ ã,õ,Ã,Õ, ñ, Ñ
390 // On (some?) non-US keyboards the AltGr key is required to enter some
391 // common characters. It comes to us as both Alt and Ctrl down so we need
392 // to let the char through in that case, otherwise if only ctrl or only
393 // alt let's skip it.
394 bool ctrl
= evt
.ControlDown();
395 bool alt
= evt
.AltDown();
396 bool skip
= (ctrl
|| alt
&& ! (ctrl
&& alt
));
398 if (key
<= 0xff && !iscntrl(key
) && !m_lastKeyDownConsumed
&& !skip
) {
399 m_swx
->DoAddChar(key
);
406 void wxStyledTextCtrl::OnKeyDown(wxKeyEvent
& evt
) {
407 long key
= evt
.KeyCode();
408 bool shift
= evt
.ShiftDown(),
409 ctrl
= evt
.ControlDown(),
412 int processed
= m_swx
->DoKeyDown(key
, shift
, ctrl
, alt
, &m_lastKeyDownConsumed
);
414 // printf("key: %%d shift: %%d ctrl: %%d alt: %%d processed: %%d consumed: %%d\n",
415 // key, shift, ctrl, alt, processed, m_lastKeyDownConsumed);
417 if (!processed
&& !m_lastKeyDownConsumed
)
422 void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent
& evt
) {
423 m_swx
->DoLoseFocus();
427 void wxStyledTextCtrl::OnGainFocus(wxFocusEvent
& evt
) {
428 m_swx
->DoGainFocus();
432 void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& evt
) {
433 m_swx
->DoSysColourChange();
437 void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent
& evt
) {
438 // do nothing to help avoid flashing
443 void wxStyledTextCtrl::OnMenu(wxCommandEvent
& evt
) {
444 m_swx
->DoCommand(evt
.GetId());
448 void wxStyledTextCtrl::OnListBox(wxCommandEvent
& evt
) {
449 m_swx
->DoOnListBox();
453 //----------------------------------------------------------------------
454 // Turn notifications from Scintilla into events
457 void wxStyledTextCtrl::NotifyChange() {
458 wxStyledTextEvent
evt(wxEVT_STC_CHANGE
, GetId());
459 GetEventHandler()->ProcessEvent(evt
);
462 void wxStyledTextCtrl::NotifyParent(SCNotification
* _scn
) {
463 SCNotification
& scn
= *_scn
;
464 wxStyledTextEvent
evt(0, GetId());
466 evt
.SetPosition(scn
.position
);
468 evt
.SetModifiers(scn
.modifiers
);
470 switch (scn
.nmhdr
.code
) {
471 case SCN_STYLENEEDED
:
472 evt
.SetEventType(wxEVT_STC_STYLENEEDED
);
476 evt
.SetEventType(wxEVT_STC_CHARADDED
);
479 case SCN_SAVEPOINTREACHED
:
480 evt
.SetEventType(wxEVT_STC_SAVEPOINTREACHED
);
483 case SCN_SAVEPOINTLEFT
:
484 evt
.SetEventType(wxEVT_STC_SAVEPOINTLEFT
);
487 case SCN_MODIFYATTEMPTRO
:
488 evt
.SetEventType(wxEVT_STC_ROMODIFYATTEMPT
);
492 evt
.SetEventType(wxEVT_STC_KEY
);
495 case SCN_DOUBLECLICK
:
496 evt
.SetEventType(wxEVT_STC_DOUBLECLICK
);
500 evt
.SetEventType(wxEVT_STC_UPDATEUI
);
504 evt
.SetEventType(wxEVT_STC_MODIFIED
);
505 evt
.SetModificationType(scn
.modificationType
);
507 evt
.SetText(wxString(scn
.text
, scn
.length
));
508 evt
.SetLength(scn
.length
);
509 evt
.SetLinesAdded(scn
.linesAdded
);
510 evt
.SetLine(scn
.line
);
511 evt
.SetFoldLevelNow(scn
.foldLevelNow
);
512 evt
.SetFoldLevelPrev(scn
.foldLevelPrev
);
515 case SCN_MACRORECORD
:
516 evt
.SetEventType(wxEVT_STC_MACRORECORD
);
517 evt
.SetMessage(scn
.message
);
518 evt
.SetWParam(scn
.wParam
);
519 evt
.SetLParam(scn
.lParam
);
522 case SCN_MARGINCLICK
:
523 evt
.SetEventType(wxEVT_STC_MARGINCLICK
);
524 evt
.SetMargin(scn
.margin
);
528 evt
.SetEventType(wxEVT_STC_NEEDSHOWN
);
529 evt
.SetLength(scn
.length
);
533 evt
.SetEventType(wxEVT_STC_POSCHANGED
);
537 evt
.SetEventType(wxEVT_STC_PAINTED
);
540 case SCN_USERLISTSELECTION
:
541 evt
.SetEventType(wxEVT_STC_USERLISTSELECTION
);
542 evt
.SetListType(scn
.listType
);
543 evt
.SetText(scn
.text
);
547 evt
.SetEventType(wxEVT_STC_URIDROPPED
);
548 evt
.SetText(scn
.text
);
552 evt
.SetEventType(wxEVT_STC_DWELLSTART
);
558 evt
.SetEventType(wxEVT_STC_DWELLEND
);
567 GetEventHandler()->ProcessEvent(evt
);
572 //----------------------------------------------------------------------
573 //----------------------------------------------------------------------
574 //----------------------------------------------------------------------
576 wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType
, int id
)
577 : wxCommandEvent(commandType
, id
)
582 m_modificationType
= 0;
597 bool wxStyledTextEvent::GetShift() const { return (m_modifiers
& SCI_SHIFT
) != 0; }
598 bool wxStyledTextEvent::GetControl() const { return (m_modifiers
& SCI_CTRL
) != 0; }
599 bool wxStyledTextEvent::GetAlt() const { return (m_modifiers
& SCI_ALT
) != 0; }
601 void wxStyledTextEvent::CopyObject(wxObject
& obj
) const {
602 wxCommandEvent::CopyObject(obj
);
604 wxStyledTextEvent
* o
= (wxStyledTextEvent
*)&obj
;
605 o
->m_position
= m_position
;
607 o
->m_modifiers
= m_modifiers
;
608 o
->m_modificationType
= m_modificationType
;
610 o
->m_length
= m_length
;
611 o
->m_linesAdded
= m_linesAdded
;
613 o
->m_foldLevelNow
= m_foldLevelNow
;
614 o
->m_foldLevelPrev
= m_foldLevelPrev
;
616 o
->m_margin
= m_margin
;
618 o
->m_message
= m_message
;
619 o
->m_wParam
= m_wParam
;
620 o
->m_lParam
= m_lParam
;
622 o
->m_listType
= m_listType
;
628 //----------------------------------------------------------------------
629 //----------------------------------------------------------------------