]> git.saurik.com Git - wxWidgets.git/blob - src/stc/stc.cpp.in
ANSI 'C' fix (decs in "for" clauses no longer have any scope outside clause itself)
[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 BEGIN_EVENT_TABLE(wxStyledTextCtrl, wxControl)
70 EVT_PAINT (wxStyledTextCtrl::OnPaint)
71 EVT_SCROLLWIN (wxStyledTextCtrl::OnScrollWin)
72 EVT_SIZE (wxStyledTextCtrl::OnSize)
73 EVT_LEFT_DOWN (wxStyledTextCtrl::OnMouseLeftDown)
74 EVT_MOTION (wxStyledTextCtrl::OnMouseMove)
75 EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp)
76 EVT_RIGHT_UP (wxStyledTextCtrl::OnMouseRightUp)
77 EVT_CHAR (wxStyledTextCtrl::OnChar)
78 EVT_KEY_DOWN (wxStyledTextCtrl::OnKeyDown)
79 EVT_KILL_FOCUS (wxStyledTextCtrl::OnLoseFocus)
80 EVT_SET_FOCUS (wxStyledTextCtrl::OnGainFocus)
81 EVT_SYS_COLOUR_CHANGED (wxStyledTextCtrl::OnSysColourChanged)
82 EVT_ERASE_BACKGROUND (wxStyledTextCtrl::OnEraseBackground)
83 EVT_MENU_RANGE (-1, -1, wxStyledTextCtrl::OnMenu)
84 EVT_LISTBOX_DCLICK (-1, wxStyledTextCtrl::OnListBox)
85 END_EVENT_TABLE()
86
87
88 IMPLEMENT_CLASS(wxStyledTextCtrl, wxControl)
89 IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent, wxCommandEvent)
90
91 //----------------------------------------------------------------------
92 // Constructor and Destructor
93
94 wxStyledTextCtrl::wxStyledTextCtrl(wxWindow *parent,
95 wxWindowID id,
96 const wxPoint& pos,
97 const wxSize& size,
98 long style,
99 const wxString& name) :
100 wxControl(parent, id, pos, size,
101 style | wxVSCROLL | wxHSCROLL | wxWANTS_CHARS | wxCLIP_CHILDREN,
102 wxDefaultValidator, name)
103 {
104 m_swx = new ScintillaWX(this);
105 m_stopWatch.Start();
106 }
107
108
109 wxStyledTextCtrl::~wxStyledTextCtrl() {
110 delete m_swx;
111 }
112
113
114 //----------------------------------------------------------------------
115
116 long wxStyledTextCtrl::SendMsg(int msg, long wp, long lp) {
117
118 return m_swx->WndProc(msg, wp, lp);
119 }
120
121
122 #ifdef MAKELONG
123 #undef MAKELONG
124 #endif
125
126 #define MAKELONG(a, b) ((a) | ((b) << 16))
127
128
129 static long wxColourAsLong(const wxColour& co) {
130 return (((long)co.Blue() << 16) |
131 ((long)co.Green() << 8) |
132 ((long)co.Red()));
133 }
134
135 static wxColour wxColourFromLong(long c) {
136 wxColour clr;
137 clr.Set(c & 0xff, (c >> 8) & 0xff, (c >> 16) & 0xff);
138 return clr;
139 }
140
141
142 static wxColour wxColourFromSpec(const wxString& spec) {
143 // spec should be #RRGGBB
144 char* junk;
145 int red = strtol(spec.Mid(1,2), &junk, 16);
146 int green = strtol(spec.Mid(3,2), &junk, 16);
147 int blue = strtol(spec.Mid(5,2), &junk, 16);
148 return wxColour(red, green, blue);
149 }
150
151
152 //----------------------------------------------------------------------
153 // BEGIN generated section. The following code is automatically generated
154 // by gen_iface.py from the contents of Scintilla.iface. Do not edit
155 // this file. Edit stc.cpp.in or gen_iface.py instead and regenerate.
156
157 %(METHOD_IMPS)s
158
159 // END of generated section
160 //----------------------------------------------------------------------
161
162
163 // Returns the line number of the line with the caret.
164 int wxStyledTextCtrl::GetCurrentLine() {
165 int line = LineFromPosition(GetCurrentPos());
166 return line;
167 }
168
169
170 // Extract style settings from a spec-string which is composed of one or
171 // more of the following comma separated elements:
172 //
173 // bold turns on bold
174 // italic turns on italics
175 // fore:#RRGGBB sets the foreground colour
176 // back:#RRGGBB sets the background colour
177 // face:[facename] sets the font face name to use
178 // size:[num] sets the font size in points
179 // eol turns on eol filling
180 // underline turns on underlining
181 //
182 void wxStyledTextCtrl::StyleSetSpec(int styleNum, const wxString& spec) {
183
184 wxStringTokenizer tkz(spec, ",");
185 while (tkz.HasMoreTokens()) {
186 wxString token = tkz.GetNextToken();
187
188 wxString option = token.BeforeFirst(':');
189 wxString val = token.AfterFirst(':');
190
191 if (option == "bold")
192 StyleSetBold(styleNum, true);
193
194 else if (option == "italic")
195 StyleSetItalic(styleNum, true);
196
197 else if (option == "underline")
198 StyleSetUnderline(styleNum, true);
199
200 else if (option == "eol")
201 StyleSetEOLFilled(styleNum, true);
202
203 else if (option == "size") {
204 long points;
205 if (val.ToLong(&points))
206 StyleSetSize(styleNum, points);
207 }
208
209 else if (option == "face")
210 StyleSetFaceName(styleNum, val);
211
212 else if (option == "fore")
213 StyleSetForeground(styleNum, wxColourFromSpec(val));
214
215 else if (option == "back")
216 StyleSetBackground(styleNum, wxColourFromSpec(val));
217 }
218 }
219
220
221 // Set style size, face, bold, italic, and underline attributes from
222 // a wxFont's attributes.
223 void wxStyledTextCtrl::StyleSetFont(int styleNum, wxFont& font) {
224 int size = font.GetPointSize();
225 wxString faceName = font.GetFaceName();
226 bool bold = font.GetWeight() == wxBOLD;
227 bool italic = font.GetStyle() != wxNORMAL;
228 bool under = font.GetUnderlined();
229
230 // TODO: add encoding/charset mapping
231 StyleSetFontAttr(styleNum, size, faceName, bold, italic, under);
232 }
233
234 // Set all font style attributes at once.
235 void wxStyledTextCtrl::StyleSetFontAttr(int styleNum, int size,
236 const wxString& faceName,
237 bool bold, bool italic,
238 bool underline) {
239 StyleSetSize(styleNum, size);
240 StyleSetFaceName(styleNum, faceName);
241 StyleSetBold(styleNum, bold);
242 StyleSetItalic(styleNum, italic);
243 StyleSetUnderline(styleNum, underline);
244
245 // TODO: add encoding/charset mapping
246 }
247
248
249 // Perform one of the operations defined by the wxSTC_CMD_* constants.
250 void wxStyledTextCtrl::CmdKeyExecute(int cmd) {
251 SendMsg(cmd);
252 }
253
254
255 // Set the left and right margin in the edit area, measured in pixels.
256 void wxStyledTextCtrl::SetMargins(int left, int right) {
257 SetMarginLeft(left);
258 SetMarginRight(right);
259 }
260
261
262 // Retrieve the start and end positions of the current selection.
263 void wxStyledTextCtrl::GetSelection(int* startPos, int* endPos) {
264 if (startPos != NULL)
265 *startPos = SendMsg(SCI_GETSELECTIONSTART);
266 if (endPos != NULL)
267 *endPos = SendMsg(SCI_GETSELECTIONEND);
268 }
269
270
271 // Retrieve the point in the window where a position is displayed.
272 wxPoint wxStyledTextCtrl::PointFromPosition(int pos) {
273 int x = SendMsg(SCI_POINTXFROMPOSITION, 0, pos);
274 int y = SendMsg(SCI_POINTYFROMPOSITION, 0, pos);
275 return wxPoint(x, y);
276 }
277
278 // Scroll enough to make the given line visible
279 void wxStyledTextCtrl::ScrollToLine(int line) {
280 m_swx->DoScrollToLine(line);
281 }
282
283
284 // Scroll enough to make the given column visible
285 void wxStyledTextCtrl::ScrollToColumn(int column) {
286 m_swx->DoScrollToColumn(column);
287 }
288
289
290
291 //----------------------------------------------------------------------
292 // Event handlers
293
294 void wxStyledTextCtrl::OnPaint(wxPaintEvent& evt) {
295 wxPaintDC dc(this);
296 wxRegion region = GetUpdateRegion();
297
298 m_swx->DoPaint(&dc, region.GetBox());
299 }
300
301 void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent& evt) {
302 if (evt.GetOrientation() == wxHORIZONTAL)
303 m_swx->DoHScroll(evt.GetEventType(), evt.GetPosition());
304 else
305 m_swx->DoVScroll(evt.GetEventType(), evt.GetPosition());
306 }
307
308 void wxStyledTextCtrl::OnSize(wxSizeEvent& evt) {
309 wxSize sz = GetClientSize();
310 m_swx->DoSize(sz.x, sz.y);
311 }
312
313 void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent& evt) {
314 wxPoint pt = evt.GetPosition();
315 m_swx->DoButtonDown(Point(pt.x, pt.y), m_stopWatch.Time(),
316 evt.ShiftDown(), evt.ControlDown(), evt.AltDown());
317 }
318
319 void wxStyledTextCtrl::OnMouseMove(wxMouseEvent& evt) {
320 wxPoint pt = evt.GetPosition();
321 m_swx->DoButtonMove(Point(pt.x, pt.y));
322 }
323
324 void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent& evt) {
325 wxPoint pt = evt.GetPosition();
326 m_swx->DoButtonUp(Point(pt.x, pt.y), m_stopWatch.Time(),
327 evt.ControlDown());
328 }
329
330
331 void wxStyledTextCtrl::OnMouseRightUp(wxMouseEvent& evt) {
332 wxPoint pt = evt.GetPosition();
333 m_swx->DoContextMenu(Point(pt.x, pt.y));
334 }
335
336 void wxStyledTextCtrl::OnChar(wxKeyEvent& evt) {
337 long key = evt.KeyCode();
338 if ((key > WXK_ESCAPE) &&
339 (key != WXK_DELETE) && (key < 255) &&
340 !evt.ControlDown() && !evt.AltDown()) {
341
342 m_swx->DoAddChar(key);
343 }
344 else {
345 evt.Skip();
346 }
347 }
348
349 void wxStyledTextCtrl::OnKeyDown(wxKeyEvent& evt) {
350 long key = evt.KeyCode();
351 key = toupper(key);
352 int processed = m_swx->DoKeyDown(key, evt.ShiftDown(),
353 evt.ControlDown(), evt.AltDown());
354 if (! processed)
355 evt.Skip();
356 }
357
358 void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent& evt) {
359 m_swx->DoLoseFocus();
360 }
361
362 void wxStyledTextCtrl::OnGainFocus(wxFocusEvent& evt) {
363 m_swx->DoGainFocus();
364 }
365
366 void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent& evt) {
367 m_swx->DoSysColourChange();
368 }
369
370 void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent& evt) {
371 // do nothing to help avoid flashing
372 }
373
374
375
376 void wxStyledTextCtrl::OnMenu(wxCommandEvent& evt) {
377 m_swx->DoCommand(evt.GetId());
378 }
379
380
381 void wxStyledTextCtrl::OnListBox(wxCommandEvent& evt) {
382 m_swx->DoOnListBox();
383 }
384
385
386 //----------------------------------------------------------------------
387 // Turn notifications from Scintilla into events
388
389
390 void wxStyledTextCtrl::NotifyChange() {
391 wxStyledTextEvent evt(wxEVT_STC_CHANGE, GetId());
392 GetEventHandler()->ProcessEvent(evt);
393 }
394
395 void wxStyledTextCtrl::NotifyParent(SCNotification* _scn) {
396 SCNotification& scn = *_scn;
397 int eventType = 0;
398 switch (scn.nmhdr.code) {
399 case SCN_STYLENEEDED:
400 eventType = wxEVT_STC_STYLENEEDED;
401 break;
402 case SCN_CHARADDED:
403 eventType = wxEVT_STC_CHARADDED;
404 break;
405 case SCN_UPDATEUI:
406 eventType = wxEVT_STC_UPDATEUI;
407 break;
408 case SCN_SAVEPOINTREACHED:
409 eventType = wxEVT_STC_SAVEPOINTREACHED;
410 break;
411 case SCN_SAVEPOINTLEFT:
412 eventType = wxEVT_STC_SAVEPOINTLEFT;
413 break;
414 case SCN_MODIFYATTEMPTRO:
415 eventType = wxEVT_STC_ROMODIFYATTEMPT;
416 break;
417 case SCN_DOUBLECLICK:
418 eventType = wxEVT_STC_DOUBLECLICK;
419 break;
420 case SCN_MODIFIED:
421 eventType = wxEVT_STC_MODIFIED;
422 break;
423 case SCN_KEY:
424 eventType = wxEVT_STC_KEY;
425 break;
426 case SCN_MACRORECORD:
427 eventType = wxEVT_STC_MACRORECORD;
428 break;
429 case SCN_MARGINCLICK:
430 eventType = wxEVT_STC_MARGINCLICK;
431 break;
432 case SCN_NEEDSHOWN:
433 eventType = wxEVT_STC_NEEDSHOWN;
434 break;
435 case SCN_POSCHANGED:
436 eventType = wxEVT_STC_POSCHANGED;
437 break;
438 }
439 if (eventType) {
440 wxStyledTextEvent evt(eventType, GetId());
441 evt.SetPosition(scn.position);
442 evt.SetKey(scn.ch);
443 evt.SetModifiers(scn.modifiers);
444 if (eventType == wxEVT_STC_MODIFIED) {
445 evt.SetModificationType(scn.modificationType);
446 if (scn.text)
447 evt.SetText(wxString(scn.text, scn.length));
448 evt.SetLength(scn.length);
449 evt.SetLinesAdded(scn.linesAdded);
450 evt.SetLine(scn.line);
451 evt.SetFoldLevelNow(scn.foldLevelNow);
452 evt.SetFoldLevelPrev(scn.foldLevelPrev);
453 }
454 if (eventType == wxEVT_STC_MARGINCLICK)
455 evt.SetMargin(scn.margin);
456 if (eventType == wxEVT_STC_MACRORECORD) {
457 evt.SetMessage(scn.message);
458 evt.SetWParam(scn.wParam);
459 evt.SetLParam(scn.lParam);
460 }
461
462 GetEventHandler()->ProcessEvent(evt);
463 }
464 }
465
466
467
468 //----------------------------------------------------------------------
469 //----------------------------------------------------------------------
470 //----------------------------------------------------------------------
471
472 wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType, int id)
473 : wxCommandEvent(commandType, id)
474 {
475 m_position = 0;
476 m_key = 0;
477 m_modifiers = 0;
478 m_modificationType = 0;
479 m_length = 0;
480 m_linesAdded = 0;
481 m_line = 0;
482 m_foldLevelNow = 0;
483 m_foldLevelPrev = 0;
484 m_margin = 0;
485 m_message = 0;
486 m_wParam = 0;
487 m_lParam = 0;
488
489
490 }
491
492 bool wxStyledTextEvent::GetShift() const { return (m_modifiers & SCI_SHIFT) != 0; }
493 bool wxStyledTextEvent::GetControl() const { return (m_modifiers & SCI_CTRL) != 0; }
494 bool wxStyledTextEvent::GetAlt() const { return (m_modifiers & SCI_ALT) != 0; }
495
496 void wxStyledTextEvent::CopyObject(wxObject& obj) const {
497 wxCommandEvent::CopyObject(obj);
498
499 wxStyledTextEvent* o = (wxStyledTextEvent*)&obj;
500 o->m_position = m_position;
501 o->m_key = m_key;
502 o->m_modifiers = m_modifiers;
503 o->m_modificationType = m_modificationType;
504 o->m_text = m_text;
505 o->m_length = m_length;
506 o->m_linesAdded = m_linesAdded;
507 o->m_line = m_line;
508 o->m_foldLevelNow = m_foldLevelNow;
509 o->m_foldLevelPrev = m_foldLevelPrev;
510
511 o->m_margin = m_margin;
512
513 o->m_message = m_message;
514 o->m_wParam = m_wParam;
515 o->m_lParam = m_lParam;
516
517
518
519 }
520
521 //----------------------------------------------------------------------
522 //----------------------------------------------------------------------
523