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";
69 BEGIN_EVENT_TABLE(wxStyledTextCtrl
, wxControl
)
70 EVT_PAINT (wxStyledTextCtrl::OnPaint
)
71 EVT_SCROLLWIN (wxStyledTextCtrl::OnScrollWin
)
72 EVT_SIZE (wxStyledTextCtrl::OnSize
)
73 EVT_LEFT_DOWN (wxStyledTextCtrl::OnMouseLeftDown
)
74 EVT_MOTION (wxStyledTextCtrl::OnMouseMove
)
75 EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp
)
76 EVT_RIGHT_UP (wxStyledTextCtrl::OnMouseRightUp
)
77 EVT_CHAR (wxStyledTextCtrl::OnChar
)
78 EVT_KEY_DOWN (wxStyledTextCtrl::OnKeyDown
)
79 EVT_KILL_FOCUS (wxStyledTextCtrl::OnLoseFocus
)
80 EVT_SET_FOCUS (wxStyledTextCtrl::OnGainFocus
)
81 EVT_SYS_COLOUR_CHANGED (wxStyledTextCtrl::OnSysColourChanged
)
82 EVT_ERASE_BACKGROUND (wxStyledTextCtrl::OnEraseBackground
)
83 EVT_MENU_RANGE (-1, -1, wxStyledTextCtrl::OnMenu
)
84 EVT_LISTBOX_DCLICK (-1, wxStyledTextCtrl::OnListBox
)
88 IMPLEMENT_CLASS(wxStyledTextCtrl
, wxControl
)
89 IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent
, wxCommandEvent
)
91 //----------------------------------------------------------------------
92 // Constructor and Destructor
94 wxStyledTextCtrl::wxStyledTextCtrl(wxWindow
*parent
,
99 const wxString
& name
) :
100 wxControl(parent
, id
, pos
, size
,
101 style
| wxVSCROLL
| wxHSCROLL
| wxWANTS_CHARS
| wxCLIP_CHILDREN
,
102 wxDefaultValidator
, name
)
104 m_swx
= new ScintillaWX(this);
109 wxStyledTextCtrl::~wxStyledTextCtrl() {
114 //----------------------------------------------------------------------
116 long wxStyledTextCtrl::SendMsg(int msg
, long wp
, long lp
) {
118 return m_swx
->WndProc(msg
, wp
, lp
);
126 #define MAKELONG(a, b) ((a) | ((b) << 16))
129 static long wxColourAsLong(const wxColour
& co
) {
130 return (((long)co
.Blue() << 16) |
131 ((long)co
.Green() << 8) |
135 static wxColour
wxColourFromLong(long c
) {
137 clr
.Set(c
& 0xff, (c
>> 8) & 0xff, (c
>> 16) & 0xff);
142 static wxColour
wxColourFromSpec(const wxString
& spec
) {
143 // spec should be #RRGGBB
145 int red
= strtol(spec
.Mid(1,2), &junk
, 16);
146 int green
= strtol(spec
.Mid(3,2), &junk
, 16);
147 int blue
= strtol(spec
.Mid(5,2), &junk
, 16);
148 return wxColour(red
, green
, blue
);
152 //----------------------------------------------------------------------
153 // BEGIN generated section. The following code is automatically generated
154 // by gen_iface.py from the contents of Scintilla.iface. Do not edit
155 // this file. Edit stc.cpp.in or gen_iface.py instead and regenerate.
159 // END of generated section
160 //----------------------------------------------------------------------
163 // Returns the line number of the line with the caret.
164 int wxStyledTextCtrl::GetCurrentLine() {
165 int line
= LineFromPosition(GetCurrentPos());
170 // Extract style settings from a spec-string which is composed of one or
171 // more of the following comma separated elements:
173 // bold turns on bold
174 // italic turns on italics
175 // fore:#RRGGBB sets the foreground colour
176 // back:#RRGGBB sets the background colour
177 // face:[facename] sets the font face name to use
178 // size:[num] sets the font size in points
179 // eol turns on eol filling
180 // underline turns on underlining
182 void wxStyledTextCtrl::StyleSetSpec(int styleNum
, const wxString
& spec
) {
184 wxStringTokenizer
tkz(spec
, ",");
185 while (tkz
.HasMoreTokens()) {
186 wxString token
= tkz
.GetNextToken();
188 wxString option
= token
.BeforeFirst(':');
189 wxString val
= token
.AfterFirst(':');
191 if (option
== "bold")
192 StyleSetBold(styleNum
, true);
194 else if (option
== "italic")
195 StyleSetItalic(styleNum
, true);
197 else if (option
== "underline")
198 StyleSetUnderline(styleNum
, true);
200 else if (option
== "eol")
201 StyleSetEOLFilled(styleNum
, true);
203 else if (option
== "size") {
205 if (val
.ToLong(&points
))
206 StyleSetSize(styleNum
, points
);
209 else if (option
== "face")
210 StyleSetFaceName(styleNum
, val
);
212 else if (option
== "fore")
213 StyleSetForeground(styleNum
, wxColourFromSpec(val
));
215 else if (option
== "back")
216 StyleSetBackground(styleNum
, wxColourFromSpec(val
));
221 // Set style size, face, bold, italic, and underline attributes from
222 // a wxFont's attributes.
223 void wxStyledTextCtrl::StyleSetFont(int styleNum
, wxFont
& font
) {
224 int size
= font
.GetPointSize();
225 wxString faceName
= font
.GetFaceName();
226 bool bold
= font
.GetWeight() == wxBOLD
;
227 bool italic
= font
.GetStyle() != wxNORMAL
;
228 bool under
= font
.GetUnderlined();
230 // TODO: add encoding/charset mapping
231 StyleSetFontAttr(styleNum
, size
, faceName
, bold
, italic
, under
);
234 // Set all font style attributes at once.
235 void wxStyledTextCtrl::StyleSetFontAttr(int styleNum
, int size
,
236 const wxString
& faceName
,
237 bool bold
, bool italic
,
239 StyleSetSize(styleNum
, size
);
240 StyleSetFaceName(styleNum
, faceName
);
241 StyleSetBold(styleNum
, bold
);
242 StyleSetItalic(styleNum
, italic
);
243 StyleSetUnderline(styleNum
, underline
);
245 // TODO: add encoding/charset mapping
249 // Perform one of the operations defined by the wxSTC_CMD_* constants.
250 void wxStyledTextCtrl::CmdKeyExecute(int cmd
) {
255 // Set the left and right margin in the edit area, measured in pixels.
256 void wxStyledTextCtrl::SetMargins(int left
, int right
) {
258 SetMarginRight(right
);
262 // Retrieve the start and end positions of the current selection.
263 void wxStyledTextCtrl::GetSelection(int* startPos
, int* endPos
) {
264 if (startPos
!= NULL
)
265 *startPos
= SendMsg(SCI_GETSELECTIONSTART
);
267 *endPos
= SendMsg(SCI_GETSELECTIONEND
);
271 // Retrieve the point in the window where a position is displayed.
272 wxPoint
wxStyledTextCtrl::PointFromPosition(int pos
) {
273 int x
= SendMsg(SCI_POINTXFROMPOSITION
, 0, pos
);
274 int y
= SendMsg(SCI_POINTYFROMPOSITION
, 0, pos
);
275 return wxPoint(x
, y
);
278 // Scroll enough to make the given line visible
279 void wxStyledTextCtrl::ScrollToLine(int line
) {
280 m_swx
->DoScrollToLine(line
);
284 // Scroll enough to make the given column visible
285 void wxStyledTextCtrl::ScrollToColumn(int column
) {
286 m_swx
->DoScrollToColumn(column
);
291 //----------------------------------------------------------------------
294 void wxStyledTextCtrl::OnPaint(wxPaintEvent
& evt
) {
296 wxRegion region
= GetUpdateRegion();
298 m_swx
->DoPaint(&dc
, region
.GetBox());
301 void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent
& evt
) {
302 if (evt
.GetOrientation() == wxHORIZONTAL
)
303 m_swx
->DoHScroll(evt
.GetEventType(), evt
.GetPosition());
305 m_swx
->DoVScroll(evt
.GetEventType(), evt
.GetPosition());
308 void wxStyledTextCtrl::OnSize(wxSizeEvent
& evt
) {
309 wxSize sz
= GetClientSize();
310 m_swx
->DoSize(sz
.x
, sz
.y
);
313 void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent
& evt
) {
314 wxPoint pt
= evt
.GetPosition();
315 m_swx
->DoButtonDown(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
316 evt
.ShiftDown(), evt
.ControlDown(), evt
.AltDown());
319 void wxStyledTextCtrl::OnMouseMove(wxMouseEvent
& evt
) {
320 wxPoint pt
= evt
.GetPosition();
321 m_swx
->DoButtonMove(Point(pt
.x
, pt
.y
));
324 void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent
& evt
) {
325 wxPoint pt
= evt
.GetPosition();
326 m_swx
->DoButtonUp(Point(pt
.x
, pt
.y
), m_stopWatch
.Time(),
331 void wxStyledTextCtrl::OnMouseRightUp(wxMouseEvent
& evt
) {
332 wxPoint pt
= evt
.GetPosition();
333 m_swx
->DoContextMenu(Point(pt
.x
, pt
.y
));
336 void wxStyledTextCtrl::OnChar(wxKeyEvent
& evt
) {
337 long key
= evt
.KeyCode();
338 if ((key
> WXK_ESCAPE
) &&
339 (key
!= WXK_DELETE
) && (key
< 255) &&
340 !evt
.ControlDown() && !evt
.AltDown()) {
342 m_swx
->DoAddChar(key
);
349 void wxStyledTextCtrl::OnKeyDown(wxKeyEvent
& evt
) {
350 long key
= evt
.KeyCode();
352 int processed
= m_swx
->DoKeyDown(key
, evt
.ShiftDown(),
353 evt
.ControlDown(), evt
.AltDown());
358 void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent
& evt
) {
359 m_swx
->DoLoseFocus();
362 void wxStyledTextCtrl::OnGainFocus(wxFocusEvent
& evt
) {
363 m_swx
->DoGainFocus();
366 void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent
& evt
) {
367 m_swx
->DoSysColourChange();
370 void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent
& evt
) {
371 // do nothing to help avoid flashing
376 void wxStyledTextCtrl::OnMenu(wxCommandEvent
& evt
) {
377 m_swx
->DoCommand(evt
.GetId());
381 void wxStyledTextCtrl::OnListBox(wxCommandEvent
& evt
) {
382 m_swx
->DoOnListBox();
386 //----------------------------------------------------------------------
387 // Turn notifications from Scintilla into events
390 void wxStyledTextCtrl::NotifyChange() {
391 wxStyledTextEvent
evt(wxEVT_STC_CHANGE
, GetId());
392 GetEventHandler()->ProcessEvent(evt
);
395 void wxStyledTextCtrl::NotifyParent(SCNotification
* _scn
) {
396 SCNotification
& scn
= *_scn
;
398 switch (scn
.nmhdr
.code
) {
399 case SCN_STYLENEEDED
:
400 eventType
= wxEVT_STC_STYLENEEDED
;
403 eventType
= wxEVT_STC_CHARADDED
;
406 eventType
= wxEVT_STC_UPDATEUI
;
408 case SCN_SAVEPOINTREACHED
:
409 eventType
= wxEVT_STC_SAVEPOINTREACHED
;
411 case SCN_SAVEPOINTLEFT
:
412 eventType
= wxEVT_STC_SAVEPOINTLEFT
;
414 case SCN_MODIFYATTEMPTRO
:
415 eventType
= wxEVT_STC_ROMODIFYATTEMPT
;
417 case SCN_DOUBLECLICK
:
418 eventType
= wxEVT_STC_DOUBLECLICK
;
421 eventType
= wxEVT_STC_MODIFIED
;
424 eventType
= wxEVT_STC_KEY
;
426 case SCN_MACRORECORD
:
427 eventType
= wxEVT_STC_MACRORECORD
;
429 case SCN_MARGINCLICK
:
430 eventType
= wxEVT_STC_MARGINCLICK
;
433 eventType
= wxEVT_STC_NEEDSHOWN
;
436 eventType
= wxEVT_STC_POSCHANGED
;
440 wxStyledTextEvent
evt(eventType
, GetId());
441 evt
.SetPosition(scn
.position
);
443 evt
.SetModifiers(scn
.modifiers
);
444 if (eventType
== wxEVT_STC_MODIFIED
) {
445 evt
.SetModificationType(scn
.modificationType
);
447 evt
.SetText(wxString(scn
.text
, scn
.length
));
448 evt
.SetLength(scn
.length
);
449 evt
.SetLinesAdded(scn
.linesAdded
);
450 evt
.SetLine(scn
.line
);
451 evt
.SetFoldLevelNow(scn
.foldLevelNow
);
452 evt
.SetFoldLevelPrev(scn
.foldLevelPrev
);
454 if (eventType
== wxEVT_STC_MARGINCLICK
)
455 evt
.SetMargin(scn
.margin
);
456 if (eventType
== wxEVT_STC_MACRORECORD
) {
457 evt
.SetMessage(scn
.message
);
458 evt
.SetWParam(scn
.wParam
);
459 evt
.SetLParam(scn
.lParam
);
462 GetEventHandler()->ProcessEvent(evt
);
468 //----------------------------------------------------------------------
469 //----------------------------------------------------------------------
470 //----------------------------------------------------------------------
472 wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType
, int id
)
473 : wxCommandEvent(commandType
, id
)
478 m_modificationType
= 0;
492 bool wxStyledTextEvent::GetShift() const { return (m_modifiers
& SCI_SHIFT
) != 0; }
493 bool wxStyledTextEvent::GetControl() const { return (m_modifiers
& SCI_CTRL
) != 0; }
494 bool wxStyledTextEvent::GetAlt() const { return (m_modifiers
& SCI_ALT
) != 0; }
496 void wxStyledTextEvent::CopyObject(wxObject
& obj
) const {
497 wxCommandEvent::CopyObject(obj
);
499 wxStyledTextEvent
* o
= (wxStyledTextEvent
*)&obj
;
500 o
->m_position
= m_position
;
502 o
->m_modifiers
= m_modifiers
;
503 o
->m_modificationType
= m_modificationType
;
505 o
->m_length
= m_length
;
506 o
->m_linesAdded
= m_linesAdded
;
508 o
->m_foldLevelNow
= m_foldLevelNow
;
509 o
->m_foldLevelPrev
= m_foldLevelPrev
;
511 o
->m_margin
= m_margin
;
513 o
->m_message
= m_message
;
514 o
->m_wParam
= m_wParam
;
515 o
->m_lParam
= m_lParam
;
521 //----------------------------------------------------------------------
522 //----------------------------------------------------------------------