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