]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/stc.cpp.in
First pass at wxPython for Mac (darwin only so far). It
[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 lmAda;
32 extern LexerModule lmAVE;
33 extern LexerModule lmConf;
34 extern LexerModule lmCPP;
35 extern LexerModule lmNncrontab;
36 extern LexerModule lmEiffel;
37 extern LexerModule lmHTML;
38 extern LexerModule lmLISP;
39 extern LexerModule lmLua;
40 extern LexerModule lmBatch; // In LexOthers.cxx
41 extern LexerModule lmPascal;
42 extern LexerModule lmPerl;
43 extern LexerModule lmPython;
44 extern LexerModule lmRuby;
45 extern LexerModule lmSQL;
46 extern LexerModule lmVB;
47
48 if ( &lmAda
49 && &lmAVE
50 && &lmConf
51 && &lmCPP
52 && &lmNncrontab
53 && &lmEiffel
54 && &lmHTML
55 && &lmLISP
56 && &lmLua
57 && &lmBatch
58 && &lmPascal
59 && &lmPerl
60 && &lmPython
61 && &lmRuby
62 && &lmSQL
63 && &lmVB )
64 {
65 return 1;
66 }
67 else
68 {
69 return 0;
70 }
71 }
72
73 //----------------------------------------------------------------------
74
75 const wxChar* wxSTCNameStr = "stcwindow";
76
77 DEFINE_EVENT_TYPE( wxEVT_STC_CHANGE )
78 DEFINE_EVENT_TYPE( wxEVT_STC_STYLENEEDED )
79 DEFINE_EVENT_TYPE( wxEVT_STC_CHARADDED )
80 DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTREACHED )
81 DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTLEFT )
82 DEFINE_EVENT_TYPE( wxEVT_STC_ROMODIFYATTEMPT )
83 DEFINE_EVENT_TYPE( wxEVT_STC_KEY )
84 DEFINE_EVENT_TYPE( wxEVT_STC_DOUBLECLICK )
85 DEFINE_EVENT_TYPE( wxEVT_STC_UPDATEUI )
86 DEFINE_EVENT_TYPE( wxEVT_STC_MODIFIED )
87 DEFINE_EVENT_TYPE( wxEVT_STC_MACRORECORD )
88 DEFINE_EVENT_TYPE( wxEVT_STC_MARGINCLICK )
89 DEFINE_EVENT_TYPE( wxEVT_STC_NEEDSHOWN )
90 DEFINE_EVENT_TYPE( wxEVT_STC_POSCHANGED )
91 DEFINE_EVENT_TYPE( wxEVT_STC_PAINTED )
92 DEFINE_EVENT_TYPE( wxEVT_STC_USERLISTSELECTION )
93 DEFINE_EVENT_TYPE( wxEVT_STC_URIDROPPED )
94 DEFINE_EVENT_TYPE( wxEVT_STC_DWELLSTART )
95 DEFINE_EVENT_TYPE( wxEVT_STC_DWELLEND )
96 DEFINE_EVENT_TYPE( wxEVT_STC_START_DRAG )
97 DEFINE_EVENT_TYPE( wxEVT_STC_DRAG_OVER )
98 DEFINE_EVENT_TYPE( wxEVT_STC_DO_DROP )
99
100
101 BEGIN_EVENT_TABLE(wxStyledTextCtrl, wxControl)
102 EVT_PAINT (wxStyledTextCtrl::OnPaint)
103 EVT_SCROLLWIN (wxStyledTextCtrl::OnScrollWin)
104 EVT_SIZE (wxStyledTextCtrl::OnSize)
105 EVT_LEFT_DOWN (wxStyledTextCtrl::OnMouseLeftDown)
106 #ifdef __WXMSW__
107 // Let Scintilla see the double click as a second click
108 EVT_LEFT_DCLICK (wxStyledTextCtrl::OnMouseLeftDown)
109 #endif
110 EVT_MOTION (wxStyledTextCtrl::OnMouseMove)
111 EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp)
112 EVT_CONTEXT_MENU (wxStyledTextCtrl::OnContextMenu)
113 EVT_MOUSEWHEEL (wxStyledTextCtrl::OnMouseWheel)
114 EVT_CHAR (wxStyledTextCtrl::OnChar)
115 EVT_KEY_DOWN (wxStyledTextCtrl::OnKeyDown)
116 EVT_KILL_FOCUS (wxStyledTextCtrl::OnLoseFocus)
117 EVT_SET_FOCUS (wxStyledTextCtrl::OnGainFocus)
118 EVT_SYS_COLOUR_CHANGED (wxStyledTextCtrl::OnSysColourChanged)
119 EVT_ERASE_BACKGROUND (wxStyledTextCtrl::OnEraseBackground)
120 EVT_MENU_RANGE (-1, -1, wxStyledTextCtrl::OnMenu)
121 EVT_LISTBOX_DCLICK (-1, wxStyledTextCtrl::OnListBox)
122 END_EVENT_TABLE()
123
124
125 IMPLEMENT_CLASS(wxStyledTextCtrl, wxControl)
126 IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent, wxCommandEvent)
127
128 //----------------------------------------------------------------------
129 // Constructor and Destructor
130
131 wxStyledTextCtrl::wxStyledTextCtrl(wxWindow *parent,
132 wxWindowID id,
133 const wxPoint& pos,
134 const wxSize& size,
135 long style,
136 const wxString& name) :
137 wxControl(parent, id, pos, size,
138 style | wxVSCROLL | wxHSCROLL | wxWANTS_CHARS | wxCLIP_CHILDREN,
139 wxDefaultValidator, name)
140 {
141 m_swx = new ScintillaWX(this);
142 m_stopWatch.Start();
143 m_lastKeyDownConsumed = FALSE;
144 }
145
146
147 wxStyledTextCtrl::~wxStyledTextCtrl() {
148 delete m_swx;
149 }
150
151
152 //----------------------------------------------------------------------
153
154 long wxStyledTextCtrl::SendMsg(int msg, long wp, long lp) {
155
156 return m_swx->WndProc(msg, wp, lp);
157 }
158
159
160 #ifdef MAKELONG
161 #undef MAKELONG
162 #endif
163
164 #define MAKELONG(a, b) ((a) | ((b) << 16))
165
166
167 static long wxColourAsLong(const wxColour& co) {
168 return (((long)co.Blue() << 16) |
169 ((long)co.Green() << 8) |
170 ((long)co.Red()));
171 }
172
173 static wxColour wxColourFromLong(long c) {
174 wxColour clr;
175 clr.Set(c & 0xff, (c >> 8) & 0xff, (c >> 16) & 0xff);
176 return clr;
177 }
178
179
180 static wxColour wxColourFromSpec(const wxString& spec) {
181 // spec should be #RRGGBB
182 char* junk;
183 int red = strtol(spec.Mid(1,2), &junk, 16);
184 int green = strtol(spec.Mid(3,2), &junk, 16);
185 int blue = strtol(spec.Mid(5,2), &junk, 16);
186 return wxColour(red, green, blue);
187 }
188
189
190 //----------------------------------------------------------------------
191 // BEGIN generated section. The following code is automatically generated
192 // by gen_iface.py from the contents of Scintilla.iface. Do not edit
193 // this file. Edit stc.cpp.in or gen_iface.py instead and regenerate.
194
195 %(METHOD_IMPS)s
196
197 // END of generated section
198 //----------------------------------------------------------------------
199
200
201 // Returns the line number of the line with the caret.
202 int wxStyledTextCtrl::GetCurrentLine() {
203 int line = LineFromPosition(GetCurrentPos());
204 return line;
205 }
206
207
208 // Extract style settings from a spec-string which is composed of one or
209 // more of the following comma separated elements:
210 //
211 // bold turns on bold
212 // italic turns on italics
213 // fore:#RRGGBB sets the foreground colour
214 // back:#RRGGBB sets the background colour
215 // face:[facename] sets the font face name to use
216 // size:[num] sets the font size in points
217 // eol turns on eol filling
218 // underline turns on underlining
219 //
220 void wxStyledTextCtrl::StyleSetSpec(int styleNum, const wxString& spec) {
221
222 wxStringTokenizer tkz(spec, ",");
223 while (tkz.HasMoreTokens()) {
224 wxString token = tkz.GetNextToken();
225
226 wxString option = token.BeforeFirst(':');
227 wxString val = token.AfterFirst(':');
228
229 if (option == "bold")
230 StyleSetBold(styleNum, true);
231
232 else if (option == "italic")
233 StyleSetItalic(styleNum, true);
234
235 else if (option == "underline")
236 StyleSetUnderline(styleNum, true);
237
238 else if (option == "eol")
239 StyleSetEOLFilled(styleNum, true);
240
241 else if (option == "size") {
242 long points;
243 if (val.ToLong(&points))
244 StyleSetSize(styleNum, points);
245 }
246
247 else if (option == "face")
248 StyleSetFaceName(styleNum, val);
249
250 else if (option == "fore")
251 StyleSetForeground(styleNum, wxColourFromSpec(val));
252
253 else if (option == "back")
254 StyleSetBackground(styleNum, wxColourFromSpec(val));
255 }
256 }
257
258
259 // Set style size, face, bold, italic, and underline attributes from
260 // a wxFont's attributes.
261 void wxStyledTextCtrl::StyleSetFont(int styleNum, wxFont& font) {
262 int size = font.GetPointSize();
263 wxString faceName = font.GetFaceName();
264 bool bold = font.GetWeight() == wxBOLD;
265 bool italic = font.GetStyle() != wxNORMAL;
266 bool under = font.GetUnderlined();
267
268 // TODO: add encoding/charset mapping
269 StyleSetFontAttr(styleNum, size, faceName, bold, italic, under);
270 }
271
272 // Set all font style attributes at once.
273 void wxStyledTextCtrl::StyleSetFontAttr(int styleNum, int size,
274 const wxString& faceName,
275 bool bold, bool italic,
276 bool underline) {
277 StyleSetSize(styleNum, size);
278 StyleSetFaceName(styleNum, faceName);
279 StyleSetBold(styleNum, bold);
280 StyleSetItalic(styleNum, italic);
281 StyleSetUnderline(styleNum, underline);
282
283 // TODO: add encoding/charset mapping
284 }
285
286
287 // Perform one of the operations defined by the wxSTC_CMD_* constants.
288 void wxStyledTextCtrl::CmdKeyExecute(int cmd) {
289 SendMsg(cmd);
290 }
291
292
293 // Set the left and right margin in the edit area, measured in pixels.
294 void wxStyledTextCtrl::SetMargins(int left, int right) {
295 SetMarginLeft(left);
296 SetMarginRight(right);
297 }
298
299
300 // Retrieve the start and end positions of the current selection.
301 void wxStyledTextCtrl::GetSelection(int* startPos, int* endPos) {
302 if (startPos != NULL)
303 *startPos = SendMsg(SCI_GETSELECTIONSTART);
304 if (endPos != NULL)
305 *endPos = SendMsg(SCI_GETSELECTIONEND);
306 }
307
308
309 // Retrieve the point in the window where a position is displayed.
310 wxPoint wxStyledTextCtrl::PointFromPosition(int pos) {
311 int x = SendMsg(SCI_POINTXFROMPOSITION, 0, pos);
312 int y = SendMsg(SCI_POINTYFROMPOSITION, 0, pos);
313 return wxPoint(x, y);
314 }
315
316 // Scroll enough to make the given line visible
317 void wxStyledTextCtrl::ScrollToLine(int line) {
318 m_swx->DoScrollToLine(line);
319 }
320
321
322 // Scroll enough to make the given column visible
323 void wxStyledTextCtrl::ScrollToColumn(int column) {
324 m_swx->DoScrollToColumn(column);
325 }
326
327
328
329 //----------------------------------------------------------------------
330 // Event handlers
331
332 void wxStyledTextCtrl::OnPaint(wxPaintEvent& evt) {
333 wxPaintDC dc(this);
334 wxRegion region = GetUpdateRegion();
335
336 m_swx->DoPaint(&dc, region.GetBox());
337 }
338
339 void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent& evt) {
340 if (evt.GetOrientation() == wxHORIZONTAL)
341 m_swx->DoHScroll(evt.GetEventType(), evt.GetPosition());
342 else
343 m_swx->DoVScroll(evt.GetEventType(), evt.GetPosition());
344 }
345
346 void wxStyledTextCtrl::OnSize(wxSizeEvent& evt) {
347 wxSize sz = GetClientSize();
348 m_swx->DoSize(sz.x, sz.y);
349 }
350
351 void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent& evt) {
352 wxPoint pt = evt.GetPosition();
353 m_swx->DoButtonDown(Point(pt.x, pt.y), m_stopWatch.Time(),
354 evt.ShiftDown(), evt.ControlDown(), evt.AltDown());
355 }
356
357 void wxStyledTextCtrl::OnMouseMove(wxMouseEvent& evt) {
358 wxPoint pt = evt.GetPosition();
359 m_swx->DoButtonMove(Point(pt.x, pt.y));
360 }
361
362 void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent& evt) {
363 wxPoint pt = evt.GetPosition();
364 m_swx->DoButtonUp(Point(pt.x, pt.y), m_stopWatch.Time(),
365 evt.ControlDown());
366 }
367
368
369 void wxStyledTextCtrl::OnContextMenu(wxContextMenuEvent& evt) {
370 wxPoint pt = evt.GetPosition();
371 ScreenToClient(&pt.x, &pt.y);
372 m_swx->DoContextMenu(Point(pt.x, pt.y));
373 }
374
375
376 void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent& evt) {
377 m_swx->DoMouseWheel(evt.GetWheelRotation(),
378 evt.GetWheelDelta(),
379 evt.GetLinesPerAction(),
380 evt.ControlDown());
381 }
382
383
384 void wxStyledTextCtrl::OnChar(wxKeyEvent& evt) {
385 long key = evt.KeyCode();
386
387 // printf("OnChar key:%%d consumed:%%d ctrl:%%d alt:%%d\n",
388 // key, m_lastKeyDownConsumed, evt.ControlDown(), evt.AltDown());
389
390 // AltGr keys???
391 // \|@#¬[]{}?£$~ ã,õ,Ã,Õ, ñ, Ñ
392
393 // On (some?) non-US keyboards the AltGr key is required to enter some
394 // common characters. It comes to us as both Alt and Ctrl down so we need
395 // to let the char through in that case, otherwise if only ctrl or only
396 // alt let's skip it.
397 bool ctrl = evt.ControlDown();
398 bool alt = evt.AltDown();
399 bool skip = (ctrl || alt && ! (ctrl && alt));
400
401 if (key <= 0xff && !iscntrl(key) && !m_lastKeyDownConsumed && !skip) {
402 m_swx->DoAddChar(key);
403 return;
404 }
405 evt.Skip();
406 }
407
408
409 void wxStyledTextCtrl::OnKeyDown(wxKeyEvent& evt) {
410 long key = evt.KeyCode();
411 bool shift = evt.ShiftDown(),
412 ctrl = evt.ControlDown(),
413 alt = evt.AltDown();
414
415 int processed = m_swx->DoKeyDown(key, shift, ctrl, alt, &m_lastKeyDownConsumed);
416
417 // printf("key: %%d shift: %%d ctrl: %%d alt: %%d processed: %%d consumed: %%d\n",
418 // key, shift, ctrl, alt, processed, m_lastKeyDownConsumed);
419
420 if (!processed && !m_lastKeyDownConsumed)
421 evt.Skip();
422 }
423
424
425 void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent& evt) {
426 m_swx->DoLoseFocus();
427 }
428
429
430 void wxStyledTextCtrl::OnGainFocus(wxFocusEvent& evt) {
431 m_swx->DoGainFocus();
432 }
433
434
435 void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent& evt) {
436 m_swx->DoSysColourChange();
437 }
438
439
440 void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent& evt) {
441 // do nothing to help avoid flashing
442 }
443
444
445
446 void wxStyledTextCtrl::OnMenu(wxCommandEvent& evt) {
447 m_swx->DoCommand(evt.GetId());
448 }
449
450
451 void wxStyledTextCtrl::OnListBox(wxCommandEvent& evt) {
452 m_swx->DoOnListBox();
453 }
454
455
456 //----------------------------------------------------------------------
457 // Turn notifications from Scintilla into events
458
459
460 void wxStyledTextCtrl::NotifyChange() {
461 wxStyledTextEvent evt(wxEVT_STC_CHANGE, GetId());
462 evt.SetEventObject(this);
463 GetEventHandler()->ProcessEvent(evt);
464 }
465
466 void wxStyledTextCtrl::NotifyParent(SCNotification* _scn) {
467 SCNotification& scn = *_scn;
468 wxStyledTextEvent evt(0, GetId());
469
470 evt.SetEventObject(this);
471 evt.SetPosition(scn.position);
472 evt.SetKey(scn.ch);
473 evt.SetModifiers(scn.modifiers);
474
475 switch (scn.nmhdr.code) {
476 case SCN_STYLENEEDED:
477 evt.SetEventType(wxEVT_STC_STYLENEEDED);
478 break;
479
480 case SCN_CHARADDED:
481 evt.SetEventType(wxEVT_STC_CHARADDED);
482 break;
483
484 case SCN_SAVEPOINTREACHED:
485 evt.SetEventType(wxEVT_STC_SAVEPOINTREACHED);
486 break;
487
488 case SCN_SAVEPOINTLEFT:
489 evt.SetEventType(wxEVT_STC_SAVEPOINTLEFT);
490 break;
491
492 case SCN_MODIFYATTEMPTRO:
493 evt.SetEventType(wxEVT_STC_ROMODIFYATTEMPT);
494 break;
495
496 case SCN_KEY:
497 evt.SetEventType(wxEVT_STC_KEY);
498 break;
499
500 case SCN_DOUBLECLICK:
501 evt.SetEventType(wxEVT_STC_DOUBLECLICK);
502 break;
503
504 case SCN_UPDATEUI:
505 evt.SetEventType(wxEVT_STC_UPDATEUI);
506 break;
507
508 case SCN_MODIFIED:
509 evt.SetEventType(wxEVT_STC_MODIFIED);
510 evt.SetModificationType(scn.modificationType);
511 if (scn.text)
512 evt.SetText(wxString(scn.text, scn.length));
513 evt.SetLength(scn.length);
514 evt.SetLinesAdded(scn.linesAdded);
515 evt.SetLine(scn.line);
516 evt.SetFoldLevelNow(scn.foldLevelNow);
517 evt.SetFoldLevelPrev(scn.foldLevelPrev);
518 break;
519
520 case SCN_MACRORECORD:
521 evt.SetEventType(wxEVT_STC_MACRORECORD);
522 evt.SetMessage(scn.message);
523 evt.SetWParam(scn.wParam);
524 evt.SetLParam(scn.lParam);
525 break;
526
527 case SCN_MARGINCLICK:
528 evt.SetEventType(wxEVT_STC_MARGINCLICK);
529 evt.SetMargin(scn.margin);
530 break;
531
532 case SCN_NEEDSHOWN:
533 evt.SetEventType(wxEVT_STC_NEEDSHOWN);
534 evt.SetLength(scn.length);
535 break;
536
537 case SCN_POSCHANGED:
538 evt.SetEventType(wxEVT_STC_POSCHANGED);
539 break;
540
541 case SCN_PAINTED:
542 evt.SetEventType(wxEVT_STC_PAINTED);
543 break;
544
545 case SCN_USERLISTSELECTION:
546 evt.SetEventType(wxEVT_STC_USERLISTSELECTION);
547 evt.SetListType(scn.listType);
548 evt.SetText(scn.text);
549 break;
550
551 case SCN_URIDROPPED:
552 evt.SetEventType(wxEVT_STC_URIDROPPED);
553 evt.SetText(scn.text);
554 break;
555
556 case SCN_DWELLSTART:
557 evt.SetEventType(wxEVT_STC_DWELLSTART);
558 evt.SetX(scn.x);
559 evt.SetY(scn.y);
560 break;
561
562 case SCN_DWELLEND:
563 evt.SetEventType(wxEVT_STC_DWELLEND);
564 evt.SetX(scn.x);
565 evt.SetY(scn.y);
566 break;
567
568 default:
569 return;
570 }
571
572 GetEventHandler()->ProcessEvent(evt);
573 }
574
575
576 //----------------------------------------------------------------------
577 //----------------------------------------------------------------------
578 //----------------------------------------------------------------------
579
580 wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType, int id)
581 : wxCommandEvent(commandType, id)
582 {
583 m_position = 0;
584 m_key = 0;
585 m_modifiers = 0;
586 m_modificationType = 0;
587 m_length = 0;
588 m_linesAdded = 0;
589 m_line = 0;
590 m_foldLevelNow = 0;
591 m_foldLevelPrev = 0;
592 m_margin = 0;
593 m_message = 0;
594 m_wParam = 0;
595 m_lParam = 0;
596 m_listType = 0;
597 m_x = 0;
598 m_y = 0;
599 m_dragAllowMove = FALSE;
600 m_dragResult = wxDragNone;
601 }
602
603 bool wxStyledTextEvent::GetShift() const { return (m_modifiers & SCI_SHIFT) != 0; }
604 bool wxStyledTextEvent::GetControl() const { return (m_modifiers & SCI_CTRL) != 0; }
605 bool wxStyledTextEvent::GetAlt() const { return (m_modifiers & SCI_ALT) != 0; }
606
607 void wxStyledTextEvent::CopyObject(wxObject& obj) const {
608 wxCommandEvent::CopyObject(obj);
609
610 wxStyledTextEvent* o = (wxStyledTextEvent*)&obj;
611 o->m_position = m_position;
612 o->m_key = m_key;
613 o->m_modifiers = m_modifiers;
614 o->m_modificationType = m_modificationType;
615 o->m_text = m_text;
616 o->m_length = m_length;
617 o->m_linesAdded = m_linesAdded;
618 o->m_line = m_line;
619 o->m_foldLevelNow = m_foldLevelNow;
620 o->m_foldLevelPrev = m_foldLevelPrev;
621
622 o->m_margin = m_margin;
623
624 o->m_message = m_message;
625 o->m_wParam = m_wParam;
626 o->m_lParam = m_lParam;
627
628 o->m_listType = m_listType;
629 o->m_x = m_x;
630 o->m_y = m_y;
631
632 o->m_dragText = m_dragText;
633 o->m_dragAllowMove =m_dragAllowMove;
634 o->m_dragResult = m_dragResult;
635 }
636
637 //----------------------------------------------------------------------
638 //----------------------------------------------------------------------
639
640
641
642