]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/stc.cpp.in
forgot to commit - no changes
[wxWidgets.git] / contrib / 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 #ifdef __WXMSW__
92 // Let Scintilla see the double click as a second click
93 EVT_LEFT_DCLICK (wxStyledTextCtrl::OnMouseLeftDown)
94 #endif
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)
107 END_EVENT_TABLE()
108
109
110 IMPLEMENT_CLASS(wxStyledTextCtrl, wxControl)
111 IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent, wxCommandEvent)
112
113 //----------------------------------------------------------------------
114 // Constructor and Destructor
115
116 wxStyledTextCtrl::wxStyledTextCtrl(wxWindow *parent,
117 wxWindowID id,
118 const wxPoint& pos,
119 const wxSize& size,
120 long style,
121 const wxString& name) :
122 wxControl(parent, id, pos, size,
123 style | wxVSCROLL | wxHSCROLL | wxWANTS_CHARS | wxCLIP_CHILDREN,
124 wxDefaultValidator, name)
125 {
126 m_swx = new ScintillaWX(this);
127 m_stopWatch.Start();
128 }
129
130
131 wxStyledTextCtrl::~wxStyledTextCtrl() {
132 delete m_swx;
133 }
134
135
136 //----------------------------------------------------------------------
137
138 long wxStyledTextCtrl::SendMsg(int msg, long wp, long lp) {
139
140 return m_swx->WndProc(msg, wp, lp);
141 }
142
143
144 #ifdef MAKELONG
145 #undef MAKELONG
146 #endif
147
148 #define MAKELONG(a, b) ((a) | ((b) << 16))
149
150
151 static long wxColourAsLong(const wxColour& co) {
152 return (((long)co.Blue() << 16) |
153 ((long)co.Green() << 8) |
154 ((long)co.Red()));
155 }
156
157 static wxColour wxColourFromLong(long c) {
158 wxColour clr;
159 clr.Set(c & 0xff, (c >> 8) & 0xff, (c >> 16) & 0xff);
160 return clr;
161 }
162
163
164 static wxColour wxColourFromSpec(const wxString& spec) {
165 // spec should be #RRGGBB
166 char* junk;
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);
171 }
172
173
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.
178
179 %(METHOD_IMPS)s
180
181 // END of generated section
182 //----------------------------------------------------------------------
183
184
185 // Returns the line number of the line with the caret.
186 int wxStyledTextCtrl::GetCurrentLine() {
187 int line = LineFromPosition(GetCurrentPos());
188 return line;
189 }
190
191
192 // Extract style settings from a spec-string which is composed of one or
193 // more of the following comma separated elements:
194 //
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
203 //
204 void wxStyledTextCtrl::StyleSetSpec(int styleNum, const wxString& spec) {
205
206 wxStringTokenizer tkz(spec, ",");
207 while (tkz.HasMoreTokens()) {
208 wxString token = tkz.GetNextToken();
209
210 wxString option = token.BeforeFirst(':');
211 wxString val = token.AfterFirst(':');
212
213 if (option == "bold")
214 StyleSetBold(styleNum, true);
215
216 else if (option == "italic")
217 StyleSetItalic(styleNum, true);
218
219 else if (option == "underline")
220 StyleSetUnderline(styleNum, true);
221
222 else if (option == "eol")
223 StyleSetEOLFilled(styleNum, true);
224
225 else if (option == "size") {
226 long points;
227 if (val.ToLong(&points))
228 StyleSetSize(styleNum, points);
229 }
230
231 else if (option == "face")
232 StyleSetFaceName(styleNum, val);
233
234 else if (option == "fore")
235 StyleSetForeground(styleNum, wxColourFromSpec(val));
236
237 else if (option == "back")
238 StyleSetBackground(styleNum, wxColourFromSpec(val));
239 }
240 }
241
242
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();
251
252 // TODO: add encoding/charset mapping
253 StyleSetFontAttr(styleNum, size, faceName, bold, italic, under);
254 }
255
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,
260 bool underline) {
261 StyleSetSize(styleNum, size);
262 StyleSetFaceName(styleNum, faceName);
263 StyleSetBold(styleNum, bold);
264 StyleSetItalic(styleNum, italic);
265 StyleSetUnderline(styleNum, underline);
266
267 // TODO: add encoding/charset mapping
268 }
269
270
271 // Perform one of the operations defined by the wxSTC_CMD_* constants.
272 void wxStyledTextCtrl::CmdKeyExecute(int cmd) {
273 SendMsg(cmd);
274 }
275
276
277 // Set the left and right margin in the edit area, measured in pixels.
278 void wxStyledTextCtrl::SetMargins(int left, int right) {
279 SetMarginLeft(left);
280 SetMarginRight(right);
281 }
282
283
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);
288 if (endPos != NULL)
289 *endPos = SendMsg(SCI_GETSELECTIONEND);
290 }
291
292
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);
298 }
299
300 // Scroll enough to make the given line visible
301 void wxStyledTextCtrl::ScrollToLine(int line) {
302 m_swx->DoScrollToLine(line);
303 }
304
305
306 // Scroll enough to make the given column visible
307 void wxStyledTextCtrl::ScrollToColumn(int column) {
308 m_swx->DoScrollToColumn(column);
309 }
310
311
312
313 //----------------------------------------------------------------------
314 // Event handlers
315
316 void wxStyledTextCtrl::OnPaint(wxPaintEvent& evt) {
317 wxPaintDC dc(this);
318 wxRegion region = GetUpdateRegion();
319
320 m_swx->DoPaint(&dc, region.GetBox());
321 }
322
323 void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent& evt) {
324 if (evt.GetOrientation() == wxHORIZONTAL)
325 m_swx->DoHScroll(evt.GetEventType(), evt.GetPosition());
326 else
327 m_swx->DoVScroll(evt.GetEventType(), evt.GetPosition());
328 }
329
330 void wxStyledTextCtrl::OnSize(wxSizeEvent& evt) {
331 wxSize sz = GetClientSize();
332 m_swx->DoSize(sz.x, sz.y);
333 }
334
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());
339 }
340
341 void wxStyledTextCtrl::OnMouseMove(wxMouseEvent& evt) {
342 wxPoint pt = evt.GetPosition();
343 m_swx->DoButtonMove(Point(pt.x, pt.y));
344 }
345
346 void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent& evt) {
347 wxPoint pt = evt.GetPosition();
348 m_swx->DoButtonUp(Point(pt.x, pt.y), m_stopWatch.Time(),
349 evt.ControlDown());
350 }
351
352
353 void wxStyledTextCtrl::OnMouseRightUp(wxMouseEvent& evt) {
354 wxPoint pt = evt.GetPosition();
355 m_swx->DoContextMenu(Point(pt.x, pt.y));
356 }
357
358
359 void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent& evt) {
360 m_swx->DoMouseWheel(evt.GetWheelRotation(),
361 evt.GetWheelDelta(),
362 evt.GetLinesPerAction());
363 }
364
365
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()) {
371
372 m_swx->DoAddChar(key);
373 }
374 else {
375 evt.Skip();
376 }
377 }
378
379 void wxStyledTextCtrl::OnKeyDown(wxKeyEvent& evt) {
380 long key = evt.KeyCode();
381 key = toupper(key);
382 int processed = m_swx->DoKeyDown(key, evt.ShiftDown(),
383 evt.ControlDown(), evt.AltDown());
384 if (! processed)
385 evt.Skip();
386 }
387
388 void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent& evt) {
389 m_swx->DoLoseFocus();
390 }
391
392 void wxStyledTextCtrl::OnGainFocus(wxFocusEvent& evt) {
393 m_swx->DoGainFocus();
394 }
395
396 void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent& evt) {
397 m_swx->DoSysColourChange();
398 }
399
400 void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent& evt) {
401 // do nothing to help avoid flashing
402 }
403
404
405
406 void wxStyledTextCtrl::OnMenu(wxCommandEvent& evt) {
407 m_swx->DoCommand(evt.GetId());
408 }
409
410
411 void wxStyledTextCtrl::OnListBox(wxCommandEvent& evt) {
412 m_swx->DoOnListBox();
413 }
414
415
416 //----------------------------------------------------------------------
417 // Turn notifications from Scintilla into events
418
419
420 void wxStyledTextCtrl::NotifyChange() {
421 wxStyledTextEvent evt(wxEVT_STC_CHANGE, GetId());
422 GetEventHandler()->ProcessEvent(evt);
423 }
424
425 void wxStyledTextCtrl::NotifyParent(SCNotification* _scn) {
426 SCNotification& scn = *_scn;
427 int eventType = 0;
428 switch (scn.nmhdr.code) {
429 case SCN_STYLENEEDED:
430 eventType = wxEVT_STC_STYLENEEDED;
431 break;
432 case SCN_CHARADDED:
433 eventType = wxEVT_STC_CHARADDED;
434 break;
435 case SCN_UPDATEUI:
436 eventType = wxEVT_STC_UPDATEUI;
437 break;
438 case SCN_SAVEPOINTREACHED:
439 eventType = wxEVT_STC_SAVEPOINTREACHED;
440 break;
441 case SCN_SAVEPOINTLEFT:
442 eventType = wxEVT_STC_SAVEPOINTLEFT;
443 break;
444 case SCN_MODIFYATTEMPTRO:
445 eventType = wxEVT_STC_ROMODIFYATTEMPT;
446 break;
447 case SCN_DOUBLECLICK:
448 eventType = wxEVT_STC_DOUBLECLICK;
449 break;
450 case SCN_MODIFIED:
451 eventType = wxEVT_STC_MODIFIED;
452 break;
453 case SCN_KEY:
454 eventType = wxEVT_STC_KEY;
455 break;
456 case SCN_MACRORECORD:
457 eventType = wxEVT_STC_MACRORECORD;
458 break;
459 case SCN_MARGINCLICK:
460 eventType = wxEVT_STC_MARGINCLICK;
461 break;
462 case SCN_NEEDSHOWN:
463 eventType = wxEVT_STC_NEEDSHOWN;
464 break;
465 case SCN_POSCHANGED:
466 eventType = wxEVT_STC_POSCHANGED;
467 break;
468 }
469 if (eventType) {
470 wxStyledTextEvent evt(eventType, GetId());
471 evt.SetPosition(scn.position);
472 evt.SetKey(scn.ch);
473 evt.SetModifiers(scn.modifiers);
474 if (eventType == wxEVT_STC_MODIFIED) {
475 evt.SetModificationType(scn.modificationType);
476 if (scn.text)
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);
483 }
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);
490 }
491
492 GetEventHandler()->ProcessEvent(evt);
493 }
494 }
495
496
497
498 //----------------------------------------------------------------------
499 //----------------------------------------------------------------------
500 //----------------------------------------------------------------------
501
502 wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType, int id)
503 : wxCommandEvent(commandType, id)
504 {
505 m_position = 0;
506 m_key = 0;
507 m_modifiers = 0;
508 m_modificationType = 0;
509 m_length = 0;
510 m_linesAdded = 0;
511 m_line = 0;
512 m_foldLevelNow = 0;
513 m_foldLevelPrev = 0;
514 m_margin = 0;
515 m_message = 0;
516 m_wParam = 0;
517 m_lParam = 0;
518
519
520 }
521
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; }
525
526 void wxStyledTextEvent::CopyObject(wxObject& obj) const {
527 wxCommandEvent::CopyObject(obj);
528
529 wxStyledTextEvent* o = (wxStyledTextEvent*)&obj;
530 o->m_position = m_position;
531 o->m_key = m_key;
532 o->m_modifiers = m_modifiers;
533 o->m_modificationType = m_modificationType;
534 o->m_text = m_text;
535 o->m_length = m_length;
536 o->m_linesAdded = m_linesAdded;
537 o->m_line = m_line;
538 o->m_foldLevelNow = m_foldLevelNow;
539 o->m_foldLevelPrev = m_foldLevelPrev;
540
541 o->m_margin = m_margin;
542
543 o->m_message = m_message;
544 o->m_wParam = m_wParam;
545 o->m_lParam = m_lParam;
546
547
548
549 }
550
551 //----------------------------------------------------------------------
552 //----------------------------------------------------------------------
553