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