]> git.saurik.com Git - wxWidgets.git/blame - src/stc/stc.cpp.in
fixed GTK critical warning when closing MDI child (patch 529369)
[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
f97d84a6
RD
25
26//----------------------------------------------------------------------
27
28const wxChar* wxSTCNameStr = "stcwindow";
29
d25f5fbb
RD
30DEFINE_EVENT_TYPE( wxEVT_STC_CHANGE )
31DEFINE_EVENT_TYPE( wxEVT_STC_STYLENEEDED )
32DEFINE_EVENT_TYPE( wxEVT_STC_CHARADDED )
d25f5fbb
RD
33DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTREACHED )
34DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTLEFT )
35DEFINE_EVENT_TYPE( wxEVT_STC_ROMODIFYATTEMPT )
65ec6247 36DEFINE_EVENT_TYPE( wxEVT_STC_KEY )
d25f5fbb 37DEFINE_EVENT_TYPE( wxEVT_STC_DOUBLECLICK )
65ec6247 38DEFINE_EVENT_TYPE( wxEVT_STC_UPDATEUI )
d25f5fbb 39DEFINE_EVENT_TYPE( wxEVT_STC_MODIFIED )
d25f5fbb
RD
40DEFINE_EVENT_TYPE( wxEVT_STC_MACRORECORD )
41DEFINE_EVENT_TYPE( wxEVT_STC_MARGINCLICK )
42DEFINE_EVENT_TYPE( wxEVT_STC_NEEDSHOWN )
43DEFINE_EVENT_TYPE( wxEVT_STC_POSCHANGED )
65ec6247
RD
44DEFINE_EVENT_TYPE( wxEVT_STC_PAINTED )
45DEFINE_EVENT_TYPE( wxEVT_STC_USERLISTSELECTION )
46DEFINE_EVENT_TYPE( wxEVT_STC_URIDROPPED )
47DEFINE_EVENT_TYPE( wxEVT_STC_DWELLSTART )
48DEFINE_EVENT_TYPE( wxEVT_STC_DWELLEND )
a29a241f
RD
49DEFINE_EVENT_TYPE( wxEVT_STC_START_DRAG )
50DEFINE_EVENT_TYPE( wxEVT_STC_DRAG_OVER )
51DEFINE_EVENT_TYPE( wxEVT_STC_DO_DROP )
d25f5fbb
RD
52
53
f97d84a6
RD
54BEGIN_EVENT_TABLE(wxStyledTextCtrl, wxControl)
55 EVT_PAINT (wxStyledTextCtrl::OnPaint)
56 EVT_SCROLLWIN (wxStyledTextCtrl::OnScrollWin)
5fa4613c 57 EVT_SCROLL (wxStyledTextCtrl::OnScroll)
f97d84a6
RD
58 EVT_SIZE (wxStyledTextCtrl::OnSize)
59 EVT_LEFT_DOWN (wxStyledTextCtrl::OnMouseLeftDown)
4ceb1196
RD
60#ifdef __WXMSW__
61 // Let Scintilla see the double click as a second click
62 EVT_LEFT_DCLICK (wxStyledTextCtrl::OnMouseLeftDown)
63#endif
f97d84a6
RD
64 EVT_MOTION (wxStyledTextCtrl::OnMouseMove)
65 EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp)
ddf2da08
RD
66#ifdef __WXGTK__
67 EVT_RIGHT_UP (wxStyledTextCtrl::OnMouseRightUp)
68#else
65ec6247 69 EVT_CONTEXT_MENU (wxStyledTextCtrl::OnContextMenu)
ddf2da08 70#endif
37d62433 71 EVT_MOUSEWHEEL (wxStyledTextCtrl::OnMouseWheel)
f97d84a6
RD
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)
80END_EVENT_TABLE()
81
82
83IMPLEMENT_CLASS(wxStyledTextCtrl, wxControl)
84IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent, wxCommandEvent)
85
1a2fb4cd
RD
86// forces the linking of the lexer modules
87int Scintilla_LinkLexers();
88
f97d84a6
RD
89//----------------------------------------------------------------------
90// Constructor and Destructor
91
92wxStyledTextCtrl::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{
1a2fb4cd 102 Scintilla_LinkLexers();
f97d84a6
RD
103 m_swx = new ScintillaWX(this);
104 m_stopWatch.Start();
d6582821 105 m_lastKeyDownConsumed = FALSE;
5fa4613c
RD
106 m_vScrollBar = NULL;
107 m_hScrollBar = NULL;
f97d84a6
RD
108}
109
110
111wxStyledTextCtrl::~wxStyledTextCtrl() {
112 delete m_swx;
113}
114
115
116//----------------------------------------------------------------------
117
118long wxStyledTextCtrl::SendMsg(int msg, long wp, long lp) {
119
120 return m_swx->WndProc(msg, wp, lp);
121}
122
123
124#ifdef MAKELONG
125#undef MAKELONG
126#endif
127
128#define MAKELONG(a, b) ((a) | ((b) << 16))
129
130
131static long wxColourAsLong(const wxColour& co) {
132 return (((long)co.Blue() << 16) |
133 ((long)co.Green() << 8) |
134 ((long)co.Red()));
135}
136
137static wxColour wxColourFromLong(long c) {
138 wxColour clr;
139 clr.Set(c & 0xff, (c >> 8) & 0xff, (c >> 16) & 0xff);
140 return clr;
141}
142
143
144static wxColour wxColourFromSpec(const wxString& spec) {
145 // spec should be #RRGGBB
146 char* junk;
147 int red = strtol(spec.Mid(1,2), &junk, 16);
148 int green = strtol(spec.Mid(3,2), &junk, 16);
149 int blue = strtol(spec.Mid(5,2), &junk, 16);
150 return wxColour(red, green, blue);
151}
152
153
154//----------------------------------------------------------------------
155// BEGIN generated section. The following code is automatically generated
156// by gen_iface.py from the contents of Scintilla.iface. Do not edit
157// this file. Edit stc.cpp.in or gen_iface.py instead and regenerate.
158
159%(METHOD_IMPS)s
160
161// END of generated section
162//----------------------------------------------------------------------
163
164
165// Returns the line number of the line with the caret.
166int wxStyledTextCtrl::GetCurrentLine() {
167 int line = LineFromPosition(GetCurrentPos());
168 return line;
169}
170
171
172// Extract style settings from a spec-string which is composed of one or
173// more of the following comma separated elements:
174//
175// bold turns on bold
176// italic turns on italics
177// fore:#RRGGBB sets the foreground colour
178// back:#RRGGBB sets the background colour
179// face:[facename] sets the font face name to use
180// size:[num] sets the font size in points
181// eol turns on eol filling
182// underline turns on underlining
183//
184void wxStyledTextCtrl::StyleSetSpec(int styleNum, const wxString& spec) {
185
186 wxStringTokenizer tkz(spec, ",");
187 while (tkz.HasMoreTokens()) {
188 wxString token = tkz.GetNextToken();
189
190 wxString option = token.BeforeFirst(':');
191 wxString val = token.AfterFirst(':');
192
193 if (option == "bold")
194 StyleSetBold(styleNum, true);
195
196 else if (option == "italic")
197 StyleSetItalic(styleNum, true);
198
199 else if (option == "underline")
200 StyleSetUnderline(styleNum, true);
201
202 else if (option == "eol")
203 StyleSetEOLFilled(styleNum, true);
204
205 else if (option == "size") {
206 long points;
207 if (val.ToLong(&points))
208 StyleSetSize(styleNum, points);
209 }
210
211 else if (option == "face")
212 StyleSetFaceName(styleNum, val);
213
214 else if (option == "fore")
215 StyleSetForeground(styleNum, wxColourFromSpec(val));
216
217 else if (option == "back")
218 StyleSetBackground(styleNum, wxColourFromSpec(val));
219 }
220}
221
222
223// Set style size, face, bold, italic, and underline attributes from
224// a wxFont's attributes.
225void wxStyledTextCtrl::StyleSetFont(int styleNum, wxFont& font) {
226 int size = font.GetPointSize();
227 wxString faceName = font.GetFaceName();
228 bool bold = font.GetWeight() == wxBOLD;
229 bool italic = font.GetStyle() != wxNORMAL;
230 bool under = font.GetUnderlined();
231
232 // TODO: add encoding/charset mapping
233 StyleSetFontAttr(styleNum, size, faceName, bold, italic, under);
234}
235
236// Set all font style attributes at once.
237void wxStyledTextCtrl::StyleSetFontAttr(int styleNum, int size,
238 const wxString& faceName,
239 bool bold, bool italic,
240 bool underline) {
241 StyleSetSize(styleNum, size);
242 StyleSetFaceName(styleNum, faceName);
243 StyleSetBold(styleNum, bold);
244 StyleSetItalic(styleNum, italic);
245 StyleSetUnderline(styleNum, underline);
246
247 // TODO: add encoding/charset mapping
248}
249
250
251// Perform one of the operations defined by the wxSTC_CMD_* constants.
252void wxStyledTextCtrl::CmdKeyExecute(int cmd) {
253 SendMsg(cmd);
254}
255
256
257// Set the left and right margin in the edit area, measured in pixels.
258void wxStyledTextCtrl::SetMargins(int left, int right) {
259 SetMarginLeft(left);
260 SetMarginRight(right);
261}
262
263
264// Retrieve the start and end positions of the current selection.
265void wxStyledTextCtrl::GetSelection(int* startPos, int* endPos) {
266 if (startPos != NULL)
267 *startPos = SendMsg(SCI_GETSELECTIONSTART);
268 if (endPos != NULL)
269 *endPos = SendMsg(SCI_GETSELECTIONEND);
270}
271
272
273// Retrieve the point in the window where a position is displayed.
274wxPoint wxStyledTextCtrl::PointFromPosition(int pos) {
275 int x = SendMsg(SCI_POINTXFROMPOSITION, 0, pos);
276 int y = SendMsg(SCI_POINTYFROMPOSITION, 0, pos);
277 return wxPoint(x, y);
278}
279
280// Scroll enough to make the given line visible
281void wxStyledTextCtrl::ScrollToLine(int line) {
282 m_swx->DoScrollToLine(line);
283}
284
285
286// Scroll enough to make the given column visible
287void wxStyledTextCtrl::ScrollToColumn(int column) {
288 m_swx->DoScrollToColumn(column);
289}
290
291
292
293//----------------------------------------------------------------------
294// Event handlers
295
296void wxStyledTextCtrl::OnPaint(wxPaintEvent& evt) {
297 wxPaintDC dc(this);
298 wxRegion region = GetUpdateRegion();
299
300 m_swx->DoPaint(&dc, region.GetBox());
301}
302
303void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent& evt) {
304 if (evt.GetOrientation() == wxHORIZONTAL)
305 m_swx->DoHScroll(evt.GetEventType(), evt.GetPosition());
306 else
307 m_swx->DoVScroll(evt.GetEventType(), evt.GetPosition());
308}
309
5fa4613c
RD
310void wxStyledTextCtrl::OnScroll(wxScrollEvent& evt) {
311 wxScrollBar* sb = wxDynamicCast(evt.GetEventObject(), wxScrollBar);
312 if (sb) {
313 if (sb->IsVertical())
314 m_swx->DoVScroll(evt.GetEventType(), evt.GetPosition());
315 else
316 m_swx->DoHScroll(evt.GetEventType(), evt.GetPosition());
317 }
318}
319
f97d84a6
RD
320void wxStyledTextCtrl::OnSize(wxSizeEvent& evt) {
321 wxSize sz = GetClientSize();
322 m_swx->DoSize(sz.x, sz.y);
323}
324
325void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent& evt) {
326 wxPoint pt = evt.GetPosition();
327 m_swx->DoButtonDown(Point(pt.x, pt.y), m_stopWatch.Time(),
328 evt.ShiftDown(), evt.ControlDown(), evt.AltDown());
329}
330
331void wxStyledTextCtrl::OnMouseMove(wxMouseEvent& evt) {
332 wxPoint pt = evt.GetPosition();
333 m_swx->DoButtonMove(Point(pt.x, pt.y));
334}
335
336void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent& evt) {
337 wxPoint pt = evt.GetPosition();
338 m_swx->DoButtonUp(Point(pt.x, pt.y), m_stopWatch.Time(),
339 evt.ControlDown());
340}
341
342
ddf2da08
RD
343void wxStyledTextCtrl::OnMouseRightUp(wxMouseEvent& evt) {
344 wxPoint pt = evt.GetPosition();
345 m_swx->DoContextMenu(Point(pt.x, pt.y));
346}
347
348
65ec6247 349void wxStyledTextCtrl::OnContextMenu(wxContextMenuEvent& evt) {
f97d84a6 350 wxPoint pt = evt.GetPosition();
65ec6247 351 ScreenToClient(&pt.x, &pt.y);
f97d84a6
RD
352 m_swx->DoContextMenu(Point(pt.x, pt.y));
353}
354
37d62433
RD
355
356void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent& evt) {
357 m_swx->DoMouseWheel(evt.GetWheelRotation(),
358 evt.GetWheelDelta(),
65ec6247
RD
359 evt.GetLinesPerAction(),
360 evt.ControlDown());
37d62433
RD
361}
362
363
f97d84a6
RD
364void wxStyledTextCtrl::OnChar(wxKeyEvent& evt) {
365 long key = evt.KeyCode();
f3c2c221
RD
366
367// printf("OnChar key:%%d consumed:%%d ctrl:%%d alt:%%d\n",
368// key, m_lastKeyDownConsumed, evt.ControlDown(), evt.AltDown());
369
370 // AltGr keys???
371