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