]> git.saurik.com Git - wxWidgets.git/blob - src/stc/stc.cpp.in
changed version number
[wxWidgets.git] / src / stc / stc.cpp.in
1 ////////////////////////////////////////////////////////////////////////////
2 // Name: stc.cpp
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.
9 //
10 // Author: Robin Dunn
11 //
12 // Created: 13-Jan-2000
13 // RCS-ID: $Id$
14 // Copyright: (c) 2000 by Total Control Software
15 // Licence: wxWindows license
16 /////////////////////////////////////////////////////////////////////////////
17
18 #include <ctype.h>
19
20 #include "wx/stc/stc.h"
21 #include "ScintillaWX.h"
22
23 #include <wx/tokenzr.h>
24
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)
28
29 int wxForceScintillaLexers(void)
30 {
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;
42
43 if (
44 &lmCPP
45 && &lmHTML
46 && &lmXML
47 && &lmProps
48 && &lmErrorList
49 && &lmMake
50 && &lmBatch
51 && &lmPerl
52 && &lmPython
53 && &lmSQL
54 && &lmVB
55 )
56 {
57 return 1;
58 }
59 else
60 {
61 return 0;
62 }
63 }
64
65 //----------------------------------------------------------------------
66
67 const wxChar* wxSTCNameStr = "stcwindow";
68
69
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 )
84
85
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)
103 END_EVENT_TABLE()
104
105
106 IMPLEMENT_CLASS(wxStyledTextCtrl, wxControl)
107 IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent, wxCommandEvent)
108
109 //----------------------------------------------------------------------
110 // Constructor and Destructor
111
112 wxStyledTextCtrl::wxStyledTextCtrl(wxWindow *parent,
113 wxWindowID id,
114 const wxPoint& pos,
115 const wxSize& size,
116 long style,
117 const wxString& name) :
118 wxControl(parent, id, pos, size,
119 style | wxVSCROLL | wxHSCROLL | wxWANTS_CHARS | wxCLIP_CHILDREN,
120 wxDefaultValidator, name)
121 {
122 m_swx = new ScintillaWX(this);
123 m_stopWatch.Start();
124 }
125
126
127 wxStyledTextCtrl::~wxStyledTextCtrl() {
128 delete m_swx;
129 }
130
131
132 //----------------------------------------------------------------------
133
134 long wxStyledTextCtrl::SendMsg(int msg, long wp, long lp) {
135
136 return m_swx->WndProc(msg, wp, lp);
137 }
138
139
140 #ifdef MAKELONG
141 #undef MAKELONG
142 #endif
143
144 #define MAKELONG(a, b) ((a) | ((b) << 16))
145
146
147 static long wxColourAsLong(const wxColour& co) {
148 return (((long)co.Blue() << 16) |
149 ((long)co.Green() << 8) |
150 ((long)co.Red()));
151 }
152
153 static wxColour wxColourFromLong(long c) {
154 wxColour clr;
155 clr.Set(c & 0xff, (c >> 8) & 0xff, (c >> 16) & 0xff);
156 return clr;
157 }
158
159
160 static wxColour wxColourFromSpec(const wxString& spec) {
161 // spec should be #RRGGBB
162 char* junk;
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);
167 }
168
169
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.
174
175 %(METHOD_IMPS)s
176
177 // END of generated section
178 //----------------------------------------------------------------------
179
180
181 // Returns the line number of the line with the caret.
182 int wxStyledTextCtrl::GetCurrentLine() {
183 int line = LineFromPosition(GetCurrentPos());
184 return line;
185 }
186
187
188 // Extract style settings from a spec-string which is composed of one or
189 // more of the following comma separated elements:
190 //
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
199 //
200 void wxStyledTextCtrl::StyleSetSpec(int styleNum, const wxString& spec) {
201
202 wxStringTokenizer tkz(spec, ",");
203 while (tkz.HasMoreTokens()) {
204 wxString token = tkz.GetNextToken();
205
206 wxString option = token.BeforeFirst(':');
207 wxString val = token.AfterFirst(':');
208
209 if (option == "bold")
210 StyleSetBold(styleNum, true);
211
212 else if (option == "italic")
213 StyleSetItalic(styleNum, true);
214
215 else if (option == "underline")
216 StyleSetUnderline(styleNum, true);
217
218 else if (option == "eol")
219 StyleSetEOLFilled(styleNum, true);
220
221 else if (option == "size") {
222 long points;
223 if (val.ToLong(&points))
224 StyleSetSize(styleNum, points);
225 }
226
227 else if (option == "face")
228 StyleSetFaceName(styleNum, val);
229
230 else if (option == "fore")
231 StyleSetForeground(styleNum, wxColourFromSpec(val));
232
233 else if (option == "back")
234 StyleSetBackground(styleNum, wxColourFromSpec(val));
235 }
236 }
237
238
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();
247
248 // TODO: add encoding/charset mapping
249 StyleSetFontAttr(styleNum, size, faceName, bold, italic, under);
250 }
251
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,
256 bool underline) {
257 StyleSetSize(styleNum, size);
258 StyleSetFaceName(styleNum, faceName);
259 StyleSetBold(styleNum, bold);
260 StyleSetItalic(styleNum, italic);
261 StyleSetUnderline(styleNum, underline);
262
263 // TODO: add encoding/charset mapping
264 }
265
266
267 // Perform one of the operations defined by the wxSTC_CMD_* constants.
268 void wxStyledTextCtrl::CmdKeyExecute(int cmd) {
269 SendMsg(cmd);
270 }
271
272
273 // Set the left and right margin in the edit area, measured in pixels.
274 void wxStyledTextCtrl::SetMargins(int left, int right) {
275 SetMarginLeft(left);
276 SetMarginRight(right);
277 }
278
279
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);
284 if (endPos != NULL)
285 *endPos = SendMsg(SCI_GETSELECTIONEND);
286 }
287
288
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);
294 }
295
296 // Scroll enough to make the given line visible
297 void wxStyledTextCtrl::ScrollToLine(int line) {
298 m_swx->DoScrollToLine(line);
299 }
300
301
302 // Scroll enough to make the given column visible
303 void wxStyledTextCtrl::ScrollToColumn(int column) {
304 m_swx->DoScrollToColumn(column);
305 }
306
307
308
309 //----------------------------------------------------------------------
310 // Event handlers
311
312 void wxStyledTextCtrl::OnPaint(wxPaintEvent& evt) {
313 wxPaintDC dc(this);
314 wxRegion region = GetUpdateRegion();
315
316 m_swx->DoPaint(&dc, region.GetBox());
317 }
318
319 void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent& evt) {
320 if (evt.GetOrientation() == wxHORIZONTAL)
321 m_swx->DoHScroll(evt.GetEventType(), evt.GetPosition());
322 else
323 m_swx->DoVScroll(evt.GetEventType(), evt.GetPosition());
324 }
325
326 void wxStyledTextCtrl::OnSize(wxSizeEvent& evt) {
327 wxSize sz = GetClientSize();
328 m_swx->DoSize(sz.x, sz.y);
329 }
330
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());
335 }
336
337 void wxStyledTextCtrl::OnMouseMove(wxMouseEvent& evt) {
338 wxPoint pt = evt.GetPosition();
339 m_swx->DoButtonMove(Point(pt.x, pt.y));
340 }
341
342 void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent& evt) {
343 wxPoint pt = evt.GetPosition();
344 m_swx->DoButtonUp(Point(pt.x, pt.y), m_stopWatch.Time(),
345 evt.ControlDown());
346 }
347
348
349 void wxStyledTextCtrl::OnMouseRightUp(wxMouseEvent& evt) {
350 wxPoint pt = evt.GetPosition();
351 m_swx->DoContextMenu(Point(pt.x, pt.y));
352 }
353
354
355 void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent& evt) {
356 m_swx->DoMouseWheel(evt.GetWheelRotation(),
357 evt.GetWheelDelta(),
358 evt.GetLinesPerAction());
359 }
360
361
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()) {
367
368 m_swx->DoAddChar(key);
369 }
370 else {
371 evt.Skip();
372 }
373 }
374
375 void wxStyledTextCtrl::OnKeyDown(wxKeyEvent& evt) {
376 long key = evt.KeyCode();
377 key = toupper(key);
378 int processed = m_swx->DoKeyDown(key, evt.ShiftDown(),
379 evt.ControlDown(), evt.AltDown());
380 if (! processed)
381 evt.Skip();
382 }
383
384 void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent& evt) {
385 m_swx->DoLoseFocus();
386 }
387
388 void wxStyledTextCtrl::OnGainFocus(wxFocusEvent& evt) {
389 m_swx->DoGainFocus();
390 }
391
392 void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent& evt) {
393 m_swx->DoSysColourChange();
394 }
395
396 void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent& evt) {
397 // do nothing to help avoid flashing
398 }
399
400
401
402 void wxStyledTextCtrl::OnMenu(wxCommandEvent& evt) {
403 m_swx->DoCommand(evt.GetId());
404 }
405
406
407 void wxStyledTextCtrl::OnListBox(wxCommandEvent& evt) {
408 m_swx->DoOnListBox();
409 }
410
411
412 //----------------------------------------------------------------------
413 // Turn notifications from Scintilla into events
414
415
416 void wxStyledTextCtrl::NotifyChange() {
417 wxStyledTextEvent evt(wxEVT_STC_CHANGE, GetId());
418 GetEventHandler()->ProcessEvent(evt);
419 }
420
421 void wxStyledTextCtrl::NotifyParent(SCNotification* _scn) {
422 SCNotification& scn = *_scn;
423 int eventType = 0;
424 switch (scn.nmhdr.code) {
425 case SCN_STYLENEEDED:
426 eventType = wxEVT_STC_STYLENEEDED;
427 break;
428 case SCN_CHARADDED:
429 eventType = wxEVT_STC_CHARADDED;
430 break;
431 case SCN_UPDATEUI:
432 eventType = wxEVT_STC_UPDATEUI;
433 break;
434 case SCN_SAVEPOINTREACHED:
435 eventType = wxEVT_STC_SAVEPOINTREACHED;
436 break;
437 case SCN_SAVEPOINTLEFT:
438 eventType = wxEVT_STC_SAVEPOINTLEFT;
439 break;
440 case SCN_MODIFYATTEMPTRO:
441 eventType = wxEVT_STC_ROMODIFYATTEMPT;
442 break;
443 case SCN_DOUBLECLICK:
444 eventType = wxEVT_STC_DOUBLECLICK;
445 break;
446 case SCN_MODIFIED:
447 eventType = wxEVT_STC_MODIFIED;
448 break;
449 case SCN_KEY:
450 eventType = wxEVT_STC_KEY;
451 break;
452 case SCN_MACRORECORD:
453 eventType = wxEVT_STC_MACRORECORD;
454 break;
455 case SCN_MARGINCLICK:
456 eventType = wxEVT_STC_MARGINCLICK;
457 break;
458 case SCN_NEEDSHOWN:
459 eventType = wxEVT_STC_NEEDSHOWN;
460 break;
461 case SCN_POSCHANGED:
462 eventType = wxEVT_STC_POSCHANGED;
463 break;
464 }
465 if (eventType) {
466 wxStyledTextEvent evt(eventType, GetId());
467 evt.SetPosition(scn.position);
468 evt.SetKey(scn.ch);
469 evt.SetModifiers(scn.modifiers);
470 if (eventType == wxEVT_STC_MODIFIED) {
471 evt.SetModificationType(scn.modificationType);
472 if (scn.text)
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);
479 }
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);
486 }
487
488 GetEventHandler()->ProcessEvent(evt);
489 }
490 }
491
492
493
494 //----------------------------------------------------------------------
495 //----------------------------------------------------------------------
496 //----------------------------------------------------------------------
497
498 wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType, int id)
499 : wxCommandEvent(commandType, id)
500 {
501 m_position = 0;
502 m_key = 0;
503 m_modifiers = 0;
504 m_modificationType = 0;
505 m_length = 0;
506 m_linesAdded = 0;
507 m_line = 0;
508 m_foldLevelNow = 0;
509 m_foldLevelPrev = 0;
510 m_margin = 0;
511 m_message = 0;
512 m_wParam = 0;
513 m_lParam = 0;
514
515
516 }
517
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; }
521
522 void wxStyledTextEvent::CopyObject(wxObject& obj) const {
523 wxCommandEvent::CopyObject(obj);
524
525 wxStyledTextEvent* o = (wxStyledTextEvent*)&obj;
526 o->m_position = m_position;
527 o->m_key = m_key;
528 o->m_modifiers = m_modifiers;
529 o->m_modificationType = m_modificationType;
530 o->m_text = m_text;
531 o->m_length = m_length;
532 o->m_linesAdded = m_linesAdded;
533 o->m_line = m_line;
534 o->m_foldLevelNow = m_foldLevelNow;
535 o->m_foldLevelPrev = m_foldLevelPrev;
536
537 o->m_margin = m_margin;
538
539 o->m_message = m_message;
540 o->m_wParam = m_wParam;
541 o->m_lParam = m_lParam;
542
543
544
545 }
546
547 //----------------------------------------------------------------------
548 //----------------------------------------------------------------------
549