]>
Commit | Line | Data |
---|---|---|
f97d84a6 RD |
1 | //////////////////////////////////////////////////////////////////////////// |
2 | // Name: stc.cpp | |
be5a51fb | 3 | // Purpose: A wxWidgets implementation of Scintilla. This class is the |
f97d84a6 RD |
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 | ||
9e730a78 | 23 | #include <wx/wx.h> |
f97d84a6 | 24 | #include <wx/tokenzr.h> |
9e730a78 RD |
25 | #include <wx/mstream.h> |
26 | #include <wx/image.h> | |
51566b0b | 27 | #include <wx/file.h> |
f97d84a6 | 28 | |
f97d84a6 RD |
29 | |
30 | //---------------------------------------------------------------------- | |
31 | ||
10ef30eb | 32 | const wxChar* wxSTCNameStr = wxT("stcwindow"); |
f97d84a6 | 33 | |
451c5cc7 RD |
34 | #ifdef MAKELONG |
35 | #undef MAKELONG | |
36 | #endif | |
37 | ||
38 | #define MAKELONG(a, b) ((a) | ((b) << 16)) | |
39 | ||
40 | ||
41 | static long wxColourAsLong(const wxColour& co) { | |
42 | return (((long)co.Blue() << 16) | | |
43 | ((long)co.Green() << 8) | | |
44 | ((long)co.Red())); | |
45 | } | |
46 | ||
47 | static wxColour wxColourFromLong(long c) { | |
48 | wxColour clr; | |
49 | clr.Set(c & 0xff, (c >> 8) & 0xff, (c >> 16) & 0xff); | |
50 | return clr; | |
51 | } | |
52 | ||
53 | ||
54 | static wxColour wxColourFromSpec(const wxString& spec) { | |
55 | // spec should be "#RRGGBB" | |
56 | long red, green, blue; | |
57 | red = green = blue = 0; | |
58 | spec.Mid(1,2).ToLong(&red, 16); | |
59 | spec.Mid(3,2).ToLong(&green, 16); | |
60 | spec.Mid(5,2).ToLong(&blue, 16); | |
61 | return wxColour(red, green, blue); | |
62 | } | |
63 | ||
64 | //---------------------------------------------------------------------- | |
65 | ||
d25f5fbb RD |
66 | DEFINE_EVENT_TYPE( wxEVT_STC_CHANGE ) |
67 | DEFINE_EVENT_TYPE( wxEVT_STC_STYLENEEDED ) | |
68 | DEFINE_EVENT_TYPE( wxEVT_STC_CHARADDED ) | |
d25f5fbb RD |
69 | DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTREACHED ) |
70 | DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTLEFT ) | |
71 | DEFINE_EVENT_TYPE( wxEVT_STC_ROMODIFYATTEMPT ) | |
65ec6247 | 72 | DEFINE_EVENT_TYPE( wxEVT_STC_KEY ) |
d25f5fbb | 73 | DEFINE_EVENT_TYPE( wxEVT_STC_DOUBLECLICK ) |
65ec6247 | 74 | DEFINE_EVENT_TYPE( wxEVT_STC_UPDATEUI ) |
d25f5fbb | 75 | DEFINE_EVENT_TYPE( wxEVT_STC_MODIFIED ) |
d25f5fbb RD |
76 | DEFINE_EVENT_TYPE( wxEVT_STC_MACRORECORD ) |
77 | DEFINE_EVENT_TYPE( wxEVT_STC_MARGINCLICK ) | |
78 | DEFINE_EVENT_TYPE( wxEVT_STC_NEEDSHOWN ) | |
65ec6247 RD |
79 | DEFINE_EVENT_TYPE( wxEVT_STC_PAINTED ) |
80 | DEFINE_EVENT_TYPE( wxEVT_STC_USERLISTSELECTION ) | |
81 | DEFINE_EVENT_TYPE( wxEVT_STC_URIDROPPED ) | |
82 | DEFINE_EVENT_TYPE( wxEVT_STC_DWELLSTART ) | |
83 | DEFINE_EVENT_TYPE( wxEVT_STC_DWELLEND ) | |
a29a241f RD |
84 | DEFINE_EVENT_TYPE( wxEVT_STC_START_DRAG ) |
85 | DEFINE_EVENT_TYPE( wxEVT_STC_DRAG_OVER ) | |
86 | DEFINE_EVENT_TYPE( wxEVT_STC_DO_DROP ) | |
a834585d | 87 | DEFINE_EVENT_TYPE( wxEVT_STC_ZOOM ) |
9e730a78 RD |
88 | DEFINE_EVENT_TYPE( wxEVT_STC_HOTSPOT_CLICK ) |
89 | DEFINE_EVENT_TYPE( wxEVT_STC_HOTSPOT_DCLICK ) | |
90 | DEFINE_EVENT_TYPE( wxEVT_STC_CALLTIP_CLICK ) | |
91 | ||
d25f5fbb RD |
92 | |
93 | ||
f97d84a6 RD |
94 | BEGIN_EVENT_TABLE(wxStyledTextCtrl, wxControl) |
95 | EVT_PAINT (wxStyledTextCtrl::OnPaint) | |
96 | EVT_SCROLLWIN (wxStyledTextCtrl::OnScrollWin) | |
5fa4613c | 97 | EVT_SCROLL (wxStyledTextCtrl::OnScroll) |
f97d84a6 RD |
98 | EVT_SIZE (wxStyledTextCtrl::OnSize) |
99 | EVT_LEFT_DOWN (wxStyledTextCtrl::OnMouseLeftDown) | |
4ceb1196 RD |
100 | // Let Scintilla see the double click as a second click |
101 | EVT_LEFT_DCLICK (wxStyledTextCtrl::OnMouseLeftDown) | |
f97d84a6 RD |
102 | EVT_MOTION (wxStyledTextCtrl::OnMouseMove) |
103 | EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp) | |
451c5cc7 | 104 | #if defined(__WXGTK__) || defined(__WXMAC__) |
ddf2da08 RD |
105 | EVT_RIGHT_UP (wxStyledTextCtrl::OnMouseRightUp) |
106 | #else | |
65ec6247 | 107 | EVT_CONTEXT_MENU (wxStyledTextCtrl::OnContextMenu) |
ddf2da08 | 108 | #endif |
37d62433 | 109 | EVT_MOUSEWHEEL (wxStyledTextCtrl::OnMouseWheel) |
2b5f62a0 | 110 | EVT_MIDDLE_UP (wxStyledTextCtrl::OnMouseMiddleUp) |
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) | |
dd4aa550 | 117 | EVT_MENU_RANGE (10, 16, wxStyledTextCtrl::OnMenu) |
f97d84a6 RD |
118 | EVT_LISTBOX_DCLICK (-1, wxStyledTextCtrl::OnListBox) |
119 | END_EVENT_TABLE() | |
120 | ||
121 | ||
122 | IMPLEMENT_CLASS(wxStyledTextCtrl, wxControl) | |
123 | IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent, wxCommandEvent) | |
124 | ||
40716a51 | 125 | #ifdef LINK_LEXERS |
1a2fb4cd | 126 | // forces the linking of the lexer modules |
a834585d | 127 | int Scintilla_LinkLexers(); |
40716a51 | 128 | #endif |
1a2fb4cd | 129 | |
f97d84a6 RD |
130 | //---------------------------------------------------------------------- |
131 | // Constructor and Destructor | |
132 | ||
133 | wxStyledTextCtrl::wxStyledTextCtrl(wxWindow *parent, | |
134 | wxWindowID id, | |
135 | const wxPoint& pos, | |
136 | const wxSize& size, | |
137 | long style, | |
39c0acb6 RD |
138 | const wxString& name) |
139 | { | |
140 | m_swx = NULL; | |
141 | Create(parent, id, pos, size, style, name); | |
142 | } | |
143 | ||
144 | ||
145 | void wxStyledTextCtrl::Create(wxWindow *parent, | |
146 | wxWindowID id, | |
147 | const wxPoint& pos, | |
148 | const wxSize& size, | |
149 | long style, | |
150 | const wxString& name) | |
f97d84a6 | 151 | { |
2659dad3 RD |
152 | #ifdef __WXMAC__ |
153 | style |= wxVSCROLL | wxHSCROLL; | |
154 | #endif | |
39c0acb6 | 155 | wxControl::Create(parent, id, pos, size, |
12082683 | 156 | style | wxWANTS_CHARS | wxCLIP_CHILDREN, |
39c0acb6 RD |
157 | wxDefaultValidator, name); |
158 | ||
40716a51 | 159 | #ifdef LINK_LEXERS |
a834585d | 160 | Scintilla_LinkLexers(); |
40716a51 | 161 | #endif |
f97d84a6 RD |
162 | m_swx = new ScintillaWX(this); |
163 | m_stopWatch.Start(); | |
d6582821 | 164 | m_lastKeyDownConsumed = FALSE; |
5fa4613c RD |
165 | m_vScrollBar = NULL; |
166 | m_hScrollBar = NULL; | |
10ef30eb RD |
167 | #if wxUSE_UNICODE |
168 | // Put Scintilla into unicode (UTF-8) mode | |
169 | SetCodePage(wxSTC_CP_UTF8); | |
170 | #endif | |
f97d84a6 RD |
171 | } |
172 | ||
173 | ||
174 | wxStyledTextCtrl::~wxStyledTextCtrl() { | |
175 | delete m_swx; | |
176 | } | |
177 | ||
178 | ||
179 | //---------------------------------------------------------------------- | |
180 | ||
181 | long wxStyledTextCtrl::SendMsg(int msg, long wp, long lp) { | |
182 | ||
183 | return m_swx->WndProc(msg, wp, lp); | |
184 | } | |
185 | ||
186 | ||
f97d84a6 RD |
187 | |
188 | //---------------------------------------------------------------------- | |
189 | // BEGIN generated section. The following code is automatically generated | |
190 | // by gen_iface.py from the contents of Scintilla.iface. Do not edit | |
191 | // this file. Edit stc.cpp.in or gen_iface.py instead and regenerate. | |
192 | ||
193 | %(METHOD_IMPS)s | |
194 | ||
195 | // END of generated section | |
196 | //---------------------------------------------------------------------- | |
197 | ||
198 | ||
199 | // Returns the line number of the line with the caret. | |
200 | int wxStyledTextCtrl::GetCurrentLine() { | |
201 | int line = LineFromPosition(GetCurrentPos()); | |
202 | return line; | |
203 | } | |
204 | ||
205 | ||
206 | // Extract style settings from a spec-string which is composed of one or | |
207 | // more of the following comma separated elements: | |
208 | // | |
209 | // bold turns on bold | |
210 | // italic turns on italics | |
211 | // fore:#RRGGBB sets the foreground colour | |
212 | // back:#RRGGBB sets the background colour | |
213 | // face:[facename] sets the font face name to use | |
214 | // size:[num] sets the font size in points | |
215 | // eol turns on eol filling | |
216 | // underline turns on underlining | |
217 | // | |
218 | void wxStyledTextCtrl::StyleSetSpec(int styleNum, const wxString& spec) { | |
219 | ||
451c5cc7 | 220 | wxStringTokenizer tkz(spec, wxT(",")); |
f97d84a6 RD |
221 | while (tkz.HasMoreTokens()) { |
222 | wxString token = tkz.GetNextToken(); | |
223 | ||
224 | wxString option = token.BeforeFirst(':'); | |
225 | wxString val = token.AfterFirst(':'); | |
226 | ||
451c5cc7 | 227 | if (option == wxT("bold")) |
f97d84a6 RD |
228 | StyleSetBold(styleNum, true); |
229 | ||
451c5cc7 | 230 | else if (option == wxT("italic")) |
f97d84a6 RD |
231 | StyleSetItalic(styleNum, true); |
232 | ||
451c5cc7 | 233 | else if (option == wxT("underline")) |
f97d84a6 RD |
234 | StyleSetUnderline(styleNum, true); |
235 | ||
451c5cc7 | 236 | else if (option == wxT("eol")) |
f97d84a6 RD |
237 | StyleSetEOLFilled(styleNum, true); |
238 | ||
451c5cc7 | 239 | else if (option == wxT("size")) { |
f97d84a6 RD |
240 | long points; |
241 | if (val.ToLong(&points)) | |
242 | StyleSetSize(styleNum, points); | |
243 | } | |
244 | ||
451c5cc7 | 245 | else if (option == wxT("face")) |
f97d84a6 RD |
246 | StyleSetFaceName(styleNum, val); |
247 | ||
451c5cc7 | 248 | else if (option == wxT("fore")) |
f97d84a6 RD |
249 | StyleSetForeground(styleNum, wxColourFromSpec(val)); |
250 | ||
451c5cc7 | 251 | else if (option == wxT("back")) |
f97d84a6 RD |
252 | StyleSetBackground(styleNum, wxColourFromSpec(val)); |
253 | } | |
254 | } | |
255 | ||
256 | ||
257 | // Set style size, face, bold, italic, and underline attributes from | |
258 | // a wxFont's attributes. | |
259 | void wxStyledTextCtrl::StyleSetFont(int styleNum, wxFont& font) { | |
7475e814 RD |
260 | #ifdef __WXGTK__ |
261 | // Ensure that the native font is initialized | |
262 | int x, y; | |
263 | GetTextExtent(wxT("X"), &x, &y, NULL, NULL, &font); | |
264 | #endif | |
f97d84a6 RD |
265 | int size = font.GetPointSize(); |
266 | wxString faceName = font.GetFaceName(); | |
267 | bool bold = font.GetWeight() == wxBOLD; | |
268 | bool italic = font.GetStyle() != wxNORMAL; | |
269 | bool under = font.GetUnderlined(); | |
270 | ||
271 | // TODO: add encoding/charset mapping | |
272 | StyleSetFontAttr(styleNum, size, faceName, bold, italic, under); | |
273 | } | |
274 | ||
275 | // Set all font style attributes at once. | |
276 | void wxStyledTextCtrl::StyleSetFontAttr(int styleNum, int size, | |
277 | const wxString& faceName, | |
278 | bool bold, bool italic, | |
279 | bool underline) { | |
280 | StyleSetSize(styleNum, size); | |
281 | StyleSetFaceName(styleNum, faceName); | |
282 | StyleSetBold(styleNum, bold); | |
283 | StyleSetItalic(styleNum, italic); | |
284 | StyleSetUnderline(styleNum, underline); | |
285 | ||
286 | // TODO: add encoding/charset mapping | |
287 | } | |
288 | ||
289 | ||
290 | // Perform one of the operations defined by the wxSTC_CMD_* constants. | |
291 | void wxStyledTextCtrl::CmdKeyExecute(int cmd) { | |
292 | SendMsg(cmd); | |
293 | } | |
294 | ||
295 | ||
296 | // Set the left and right margin in the edit area, measured in pixels. | |
297 | void wxStyledTextCtrl::SetMargins(int left, int right) { | |
298 | SetMarginLeft(left); | |
299 | SetMarginRight(right); | |
300 | } | |
301 | ||
302 | ||
303 | // Retrieve the start and end positions of the current selection. | |
304 | void wxStyledTextCtrl::GetSelection(int* startPos, int* endPos) { | |
305 | if (startPos != NULL) | |
306 | *startPos = SendMsg(SCI_GETSELECTIONSTART); | |
307 | if (endPos != NULL) | |
308 | *endPos = SendMsg(SCI_GETSELECTIONEND); | |
309 | } | |
310 | ||
311 | ||
312 | // Retrieve the point in the window where a position is displayed. | |
313 | wxPoint wxStyledTextCtrl::PointFromPosition(int pos) { | |
314 | int x = SendMsg(SCI_POINTXFROMPOSITION, 0, pos); | |
315 | int y = SendMsg(SCI_POINTYFROMPOSITION, 0, pos); | |
316 | return wxPoint(x, y); | |
317 | } | |
318 | ||
319 | // Scroll enough to make the given line visible | |
320 | void wxStyledTextCtrl::ScrollToLine(int line) { | |
321 | m_swx->DoScrollToLine(line); | |
322 | } | |
323 | ||
324 | ||
325 | // Scroll enough to make the given column visible | |
326 | void wxStyledTextCtrl::ScrollToColumn(int column) { | |
327 | m_swx->DoScrollToColumn(column); | |
328 | } | |
329 | ||
330 | ||
51566b0b RD |
331 | bool wxStyledTextCtrl::SaveFile(const wxString& filename) |
332 | { | |
333 | wxFile file(filename, wxFile::write); | |
334 | ||
335 | if (!file.IsOpened()) | |
336 | return FALSE; | |
337 | ||
4a65f2c8 | 338 | bool success = file.Write(GetText(), *wxConvCurrent); |
51566b0b RD |
339 | |
340 | if (success) | |
341 | SetSavePoint(); | |
342 | ||
343 | return success; | |
344 | } | |
345 | ||
346 | bool wxStyledTextCtrl::LoadFile(const wxString& filename) | |
347 | { | |
041973c5 | 348 | bool success = false; |
51566b0b RD |
349 | wxFile file(filename, wxFile::read); |
350 | ||
041973c5 | 351 | if (file.IsOpened()) |
51566b0b | 352 | { |
041973c5 RD |
353 | wxString contents; |
354 | off_t len = file.Length(); | |
041973c5 RD |
355 | if (len > 0) |
356 | { | |
1e545382 | 357 | #if wxUSE_UNICODE |
1c26fbe0 | 358 | wxMemoryBuffer buffer(len+1); |
8e5ec9cc | 359 | success = (file.Read(buffer.GetData(), len) == len); |
9efe0302 RD |
360 | if (success) { |
361 | ((char*)buffer.GetData())[len] = 0; | |
362 | contents = wxString(buffer, *wxConvCurrent, len); | |
363 | } | |
4a65f2c8 | 364 | #else |
8e5ec9cc MB |
365 | wxString buffer; |
366 | success = (file.Read(wxStringBuffer(buffer, len), len) == len); | |
4a65f2c8 RD |
367 | contents = buffer; |
368 | #endif | |
041973c5 RD |
369 | } |
370 | else | |
371 | success = true; // empty file is ok | |
372 | ||
373 | if (success) | |
374 | { | |
375 | SetText(contents); | |
376 | EmptyUndoBuffer(); | |
377 | SetSavePoint(); | |
378 | } | |
51566b0b RD |
379 | } |
380 | ||
381 | return success; | |
382 | } | |
383 | ||
f97d84a6 | 384 | |
2fcce896 | 385 | #if wxUSE_DRAG_AND_DROP |
4a65f2c8 RD |
386 | wxDragResult wxStyledTextCtrl::DoDragOver(wxCoord x, wxCoord y, wxDragResult def) { |
387 | return m_swx->DoDragOver(x, y, def); | |
388 | } | |
389 | ||
390 | ||
391 | bool wxStyledTextCtrl::DoDropText(long x, long y, const wxString& data) { | |
392 | return m_swx->DoDropText(x, y, data); | |
393 | } | |
2fcce896 | 394 | #endif |
4a65f2c8 RD |
395 | |
396 | ||
d1558f3d RD |
397 | void wxStyledTextCtrl::SetUseAntiAliasing(bool useAA) { |
398 | m_swx->SetUseAntiAliasing(useAA); | |
399 | } | |
400 | ||
401 | bool wxStyledTextCtrl::GetUseAntiAliasing() { | |
402 | return m_swx->GetUseAntiAliasing(); | |
403 | } | |
404 | ||
f97d84a6 RD |
405 | //---------------------------------------------------------------------- |
406 | // Event handlers | |
407 | ||
88a8b04e | 408 | void wxStyledTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(evt)) { |
f97d84a6 | 409 | wxPaintDC dc(this); |
9e730a78 | 410 | m_swx->DoPaint(&dc, GetUpdateRegion().GetBox()); |
f97d84a6 RD |
411 | } |
412 | ||
413 | void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent& evt) { | |
414 | if (evt.GetOrientation() == wxHORIZONTAL) | |
415 | m_swx->DoHScroll(evt.GetEventType(), evt.GetPosition()); | |
416 | else | |
417 | m_swx->DoVScroll(evt.GetEventType(), evt.GetPosition()); | |
418 | } | |
419 | ||
5fa4613c RD |
420 | void wxStyledTextCtrl::OnScroll(wxScrollEvent& evt) { |
421 | wxScrollBar* sb = wxDynamicCast(evt.GetEventObject(), wxScrollBar); | |
422 | if (sb) { | |
423 | if (sb->IsVertical()) | |
424 | m_swx->DoVScroll(evt.GetEventType(), evt.GetPosition()); | |
425 | else | |
426 | m_swx->DoHScroll(evt.GetEventType(), evt.GetPosition()); | |
427 | } | |
428 | } | |
429 | ||
88a8b04e | 430 | void wxStyledTextCtrl::OnSize(wxSizeEvent& WXUNUSED(evt)) { |
39c0acb6 RD |
431 | if (m_swx) { |
432 | wxSize sz = GetClientSize(); | |
433 | m_swx->DoSize(sz.x, sz.y); | |
434 | } | |
f97d84a6 RD |
435 | } |
436 | ||
437 | void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent& evt) { | |
cb1871ca | 438 | SetFocus(); |
f97d84a6 | 439 | wxPoint pt = evt.GetPosition(); |
2b5f62a0 | 440 | m_swx->DoLeftButtonDown(Point(pt.x, pt.y), m_stopWatch.Time(), |
f97d84a6 RD |
441 | evt.ShiftDown(), evt.ControlDown(), evt.AltDown()); |
442 | } | |
443 | ||
444 | void wxStyledTextCtrl::OnMouseMove(wxMouseEvent& evt) { | |
445 | wxPoint pt = evt.GetPosition(); | |
2b5f62a0 | 446 | m_swx->DoLeftButtonMove(Point(pt.x, pt.y)); |
f97d84a6 RD |
447 | } |
448 | ||
449 | void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent& evt) { | |
450 | wxPoint pt = evt.GetPosition(); | |
2b5f62a0 | 451 | m_swx->DoLeftButtonUp(Point(pt.x, pt.y), m_stopWatch.Time(), |
f97d84a6 RD |
452 | evt.ControlDown()); |
453 | } | |
454 | ||
455 | ||
ddf2da08 RD |
456 | void wxStyledTextCtrl::OnMouseRightUp(wxMouseEvent& evt) { |
457 | wxPoint pt = evt.GetPosition(); | |
458 | m_swx->DoContextMenu(Point(pt.x, pt.y)); | |
459 | } | |
460 | ||
461 | ||
2b5f62a0 VZ |
462 | void wxStyledTextCtrl::OnMouseMiddleUp(wxMouseEvent& evt) { |
463 | wxPoint pt = evt.GetPosition(); | |
464 | m_swx->DoMiddleButtonUp(Point(pt.x, pt.y)); | |
465 | } | |
466 | ||
65ec6247 | 467 | void wxStyledTextCtrl::OnContextMenu(wxContextMenuEvent& evt) { |
f97d84a6 | 468 | wxPoint pt = evt.GetPosition(); |
65ec6247 | 469 | ScreenToClient(&pt.x, &pt.y); |
25484746 RD |
470 | /* |
471 | Show context menu at event point if it's within the window, | |
472 | or at caret location if not | |
473 | */ | |
474 | wxHitTest ht = this->HitTest(pt); | |
475 | if (ht != wxHT_WINDOW_INSIDE) { | |
476 | pt = this->PointFromPosition(this->GetCurrentPos()); | |
477 | } | |
f97d84a6 RD |
478 | m_swx->DoContextMenu(Point(pt.x, pt.y)); |
479 | } | |
480 | ||
37d62433 RD |
481 | |
482 | void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent& evt) { | |
483 | m_swx->DoMouseWheel(evt.GetWheelRotation(), | |
484 | evt.GetWheelDelta(), | |
65ec6247 | 485 | evt.GetLinesPerAction(), |
9b9337da RD |
486 | evt.ControlDown(), |
487 | evt.IsPageScroll()); | |
37d62433 RD |
488 | } |
489 | ||
490 | ||
f97d84a6 | 491 | void wxStyledTextCtrl::OnChar(wxKeyEvent& evt) { |
f3c2c221 RD |
492 | // On (some?) non-US keyboards the AltGr key is required to enter some |
493 | // common characters. It comes to us as both Alt and Ctrl down so we need | |
494 | // to let the char through in that case, otherwise if only ctrl or only | |
495 | // alt let's skip it. | |
496 | bool ctrl = evt.ControlDown(); | |
497 | bool alt = evt.AltDown(); | |
00c64037 | 498 | bool skip = ((ctrl || alt) && ! (ctrl && alt)); |
f3c2c221 | 499 | |
2b5f62a0 VZ |
500 | int key = evt.GetKeyCode(); |
501 | ||
451c5cc7 RD |
502 | // printf("OnChar key:%%d consumed:%%d ctrl:%%d alt:%%d skip:%%d\n", |
503 | // key, m_lastKeyDownConsumed, ctrl, alt, skip); | |
10ef30eb | 504 | |
25484746 | 505 | if ( (key <= WXK_START || key > WXK_COMMAND) && |
2b5f62a0 | 506 | !m_lastKeyDownConsumed && !skip) { |
f97d84a6 | 507 | m_swx->DoAddChar(key); |
f3c2c221 | 508 | return; |
f97d84a6 | 509 | } |
f3c2c221 | 510 | evt.Skip(); |
f97d84a6 RD |
511 | } |
512 | ||
d6582821 | 513 | |
f97d84a6 | 514 | void wxStyledTextCtrl::OnKeyDown(wxKeyEvent& evt) { |
10ef30eb | 515 | int key = evt.GetKeyCode(); |
f3c2c221 | 516 | bool shift = evt.ShiftDown(), |
10ef30eb | 517 | ctrl = evt.ControlDown(), |
88a8b04e RD |
518 | alt = evt.AltDown(), |
519 | meta = evt.MetaDown(); | |
f3c2c221 | 520 | |
88a8b04e | 521 | int processed = m_swx->DoKeyDown(key, shift, ctrl, alt, meta, &m_lastKeyDownConsumed); |
f3c2c221 | 522 | |
451c5cc7 RD |
523 | // printf("KeyDn key:%%d shift:%%d ctrl:%%d alt:%%d processed:%%d consumed:%%d\n", |
524 | // key, shift, ctrl, alt, processed, m_lastKeyDownConsumed); | |
f3c2c221 | 525 | |
d6582821 | 526 | if (!processed && !m_lastKeyDownConsumed) |
f97d84a6 RD |
527 | evt.Skip(); |
528 | } | |
529 | ||
d6582821 | 530 | |
b6bfd8e8 | 531 | void wxStyledTextCtrl::OnLoseFocus(wxFocusEvent& evt) { |
ec830416 | 532 | m_swx->DoLoseFocus(); |
b6bfd8e8 | 533 | evt.Skip(); |
f97d84a6 RD |
534 | } |
535 | ||
d6582821 | 536 | |
b6bfd8e8 | 537 | void wxStyledTextCtrl::OnGainFocus(wxFocusEvent& evt) { |
f97d84a6 | 538 | m_swx->DoGainFocus(); |
b6bfd8e8 | 539 | evt.Skip(); |
f97d84a6 RD |
540 | } |
541 | ||
d6582821 | 542 | |
88a8b04e | 543 | void wxStyledTextCtrl::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(evt)) { |
f97d84a6 RD |
544 | m_swx->DoSysColourChange(); |
545 | } | |
546 | ||
d6582821 | 547 | |
88a8b04e | 548 | void wxStyledTextCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(evt)) { |
f97d84a6 RD |
549 | // do nothing to help avoid flashing |
550 | } | |
551 | ||
552 | ||
553 | ||
554 | void wxStyledTextCtrl::OnMenu(wxCommandEvent& evt) { | |
555 | m_swx->DoCommand(evt.GetId()); | |
556 | } | |
557 | ||
558 | ||
88a8b04e | 559 | void wxStyledTextCtrl::OnListBox(wxCommandEvent& WXUNUSED(evt)) { |
f97d84a6 RD |
560 | m_swx->DoOnListBox(); |
561 | } | |
562 | ||
563 | ||
8e54aaed RD |
564 | void wxStyledTextCtrl::OnIdle(wxIdleEvent& evt) { |
565 | m_swx->DoOnIdle(evt); | |
566 | } | |
567 | ||
568 | ||
f97d84a6 RD |
569 | //---------------------------------------------------------------------- |
570 | // Turn notifications from Scintilla into events | |
571 | ||
572 | ||
573 | void wxStyledTextCtrl::NotifyChange() { | |
574 | wxStyledTextEvent evt(wxEVT_STC_CHANGE, GetId()); | |
a29a241f | 575 | evt.SetEventObject(this); |
f97d84a6 RD |
576 | GetEventHandler()->ProcessEvent(evt); |
577 | } | |
578 | ||
2b5f62a0 VZ |
579 | |
580 | static void SetEventText(wxStyledTextEvent& evt, const char* text, | |
581 | size_t length) { | |
582 | if(!text) return; | |
583 | ||
584 | // The unicode conversion MUST have a null byte to terminate the | |
585 | // string so move it into a buffer first and give it one. | |
586 | wxMemoryBuffer buf(length+1); | |
587 | buf.AppendData((void*)text, length); | |
588 | buf.AppendByte(0); | |
589 | evt.SetText(stc2wx(buf)); | |
590 | } | |
591 | ||
592 | ||
f97d84a6 RD |
593 | void wxStyledTextCtrl::NotifyParent(SCNotification* _scn) { |
594 | SCNotification& scn = *_scn; | |
65ec6247 RD |
595 | wxStyledTextEvent evt(0, GetId()); |
596 | ||
a29a241f | 597 | evt.SetEventObject(this); |
65ec6247 RD |
598 | evt.SetPosition(scn.position); |
599 | evt.SetKey(scn.ch); | |
600 | evt.SetModifiers(scn.modifiers); | |
601 | ||
f97d84a6 RD |
602 | switch (scn.nmhdr.code) { |
603 | case SCN_STYLENEEDED: | |
65ec6247 | 604 | evt.SetEventType(wxEVT_STC_STYLENEEDED); |
f97d84a6 | 605 | break; |
65ec6247 | 606 | |
f97d84a6 | 607 | case SCN_CHARADDED: |
65ec6247 | 608 | evt.SetEventType(wxEVT_STC_CHARADDED); |
f97d84a6 | 609 | break; |
65ec6247 | 610 | |
f97d84a6 | 611 | case SCN_SAVEPOINTREACHED: |
65ec6247 | 612 | evt.SetEventType(wxEVT_STC_SAVEPOINTREACHED); |
f97d84a6 | 613 | break; |
65ec6247 | 614 | |
f97d84a6 | 615 | case SCN_SAVEPOINTLEFT: |
65ec6247 | 616 | evt.SetEventType(wxEVT_STC_SAVEPOINTLEFT); |
f97d84a6 | 617 | break; |
65ec6247 | 618 | |
f97d84a6 | 619 | case SCN_MODIFYATTEMPTRO: |
65ec6247 RD |
620 | evt.SetEventType(wxEVT_STC_ROMODIFYATTEMPT); |
621 | break; | |
622 | ||
623 | case SCN_KEY: | |
624 | evt.SetEventType(wxEVT_STC_KEY); | |
f97d84a6 | 625 | break; |
65ec6247 | 626 | |
f97d84a6 | 627 | case SCN_DOUBLECLICK: |
65ec6247 | 628 | evt.SetEventType(wxEVT_STC_DOUBLECLICK); |
f97d84a6 | 629 | break; |
65ec6247 RD |
630 | |
631 | case SCN_UPDATEUI: | |
632 | evt.SetEventType(wxEVT_STC_UPDATEUI); | |
f97d84a6 | 633 | break; |
65ec6247 RD |
634 | |
635 | case SCN_MODIFIED: | |
636 | evt.SetEventType(wxEVT_STC_MODIFIED); | |
637 | evt.SetModificationType(scn.modificationType); | |
2b5f62a0 | 638 | SetEventText(evt, scn.text, scn.length); |
65ec6247 RD |
639 | evt.SetLength(scn.length); |
640 | evt.SetLinesAdded(scn.linesAdded); | |
641 | evt.SetLine(scn.line); | |
642 | evt.SetFoldLevelNow(scn.foldLevelNow); | |
643 | evt.SetFoldLevelPrev(scn.foldLevelPrev); | |
f97d84a6 | 644 | break; |
65ec6247 | 645 | |
f97d84a6 | 646 | case SCN_MACRORECORD: |
65ec6247 RD |
647 | evt.SetEventType(wxEVT_STC_MACRORECORD); |
648 | evt.SetMessage(scn.message); | |
649 | evt.SetWParam(scn.wParam); | |
650 | evt.SetLParam(scn.lParam); | |
f97d84a6 | 651 | break; |
65ec6247 | 652 | |
f97d84a6 | 653 | case SCN_MARGINCLICK: |
65ec6247 RD |
654 | evt.SetEventType(wxEVT_STC_MARGINCLICK); |
655 | evt.SetMargin(scn.margin); | |
f97d84a6 | 656 | break; |
65ec6247 | 657 | |
f97d84a6 | 658 | case SCN_NEEDSHOWN: |
65ec6247 RD |
659 | evt.SetEventType(wxEVT_STC_NEEDSHOWN); |
660 | evt.SetLength(scn.length); | |
f97d84a6 | 661 | break; |
65ec6247 | 662 | |
65ec6247 RD |
663 | case SCN_PAINTED: |
664 | evt.SetEventType(wxEVT_STC_PAINTED); | |
665 | break; | |
666 | ||
667 | case SCN_USERLISTSELECTION: | |
668 | evt.SetEventType(wxEVT_STC_USERLISTSELECTION); | |
669 | evt.SetListType(scn.listType); | |
2b5f62a0 | 670 | SetEventText(evt, scn.text, strlen(scn.text)); |
f97d84a6 | 671 | break; |
f97d84a6 | 672 | |
65ec6247 RD |
673 | case SCN_URIDROPPED: |
674 | evt.SetEventType(wxEVT_STC_URIDROPPED); | |
2b5f62a0 | 675 | SetEventText(evt, scn.text, strlen(scn.text)); |
65ec6247 RD |
676 | break; |
677 | ||
678 | case SCN_DWELLSTART: | |
679 | evt.SetEventType(wxEVT_STC_DWELLSTART); | |
680 | evt.SetX(scn.x); | |
681 | evt.SetY(scn.y); | |
682 | break; | |
683 | ||
684 | case SCN_DWELLEND: | |
685 | evt.SetEventType(wxEVT_STC_DWELLEND); | |
686 | evt.SetX(scn.x); | |
687 | evt.SetY(scn.y); | |
688 | break; | |
689 | ||
a834585d RD |
690 | case SCN_ZOOM: |
691 | evt.SetEventType(wxEVT_STC_ZOOM); | |
692 | break; | |
693 | ||
9e730a78 RD |
694 | case SCN_HOTSPOTCLICK: |
695 | evt.SetEventType(wxEVT_STC_HOTSPOT_CLICK); | |
696 | break; | |
697 | ||
698 | case SCN_HOTSPOTDOUBLECLICK: | |
699 | evt.SetEventType(wxEVT_STC_HOTSPOT_DCLICK); | |
700 | break; | |
701 | ||
702 | case SCN_CALLTIPCLICK: | |
703 | evt.SetEventType(wxEVT_STC_CALLTIP_CLICK); | |
704 | break; | |
705 | ||
65ec6247 RD |
706 | default: |
707 | return; | |
f97d84a6 | 708 | } |
65ec6247 RD |
709 | |
710 | GetEventHandler()->ProcessEvent(evt); | |
f97d84a6 RD |
711 | } |
712 | ||
713 | ||
f97d84a6 RD |
714 | //---------------------------------------------------------------------- |
715 | //---------------------------------------------------------------------- | |
716 | //---------------------------------------------------------------------- | |
717 | ||
718 | wxStyledTextEvent::wxStyledTextEvent(wxEventType commandType, int id) | |
719 | : wxCommandEvent(commandType, id) | |
720 | { | |
721 | m_position = 0; | |
722 | m_key = 0; | |
723 | m_modifiers = 0; | |
724 | m_modificationType = 0; | |
725 | m_length = 0; | |
726 | m_linesAdded = 0; | |
727 | m_line = 0; | |
728 | m_foldLevelNow = 0; | |
729 | m_foldLevelPrev = 0; | |
730 | m_margin = 0; | |
731 | m_message = 0; | |
732 | m_wParam = 0; | |
733 | m_lParam = 0; | |
65ec6247 RD |
734 | m_listType = 0; |
735 | m_x = 0; | |
736 | m_y = 0; | |
a29a241f | 737 | m_dragAllowMove = FALSE; |
92bbd64f | 738 | #if wxUSE_DRAG_AND_DROP |
a29a241f | 739 | m_dragResult = wxDragNone; |
92bbd64f | 740 | #endif |
f97d84a6 RD |
741 | } |
742 | ||
743 | bool wxStyledTextEvent::GetShift() const { return (m_modifiers & SCI_SHIFT) != 0; } | |
744 | bool wxStyledTextEvent::GetControl() const { return (m_modifiers & SCI_CTRL) != 0; } | |
745 | bool wxStyledTextEvent::GetAlt() const { return (m_modifiers & SCI_ALT) != 0; } | |
746 | ||
f97d84a6 | 747 | |
5fa4613c RD |
748 | wxStyledTextEvent::wxStyledTextEvent(const wxStyledTextEvent& event): |
749 | wxCommandEvent(event) | |
750 | { | |
751 | m_position = event.m_position; | |
752 | m_key = event.m_key; | |
753 | m_modifiers = event.m_modifiers; | |
754 | m_modificationType = event.m_modificationType; | |
755 | m_text = event.m_text; | |
756 | m_length = event.m_length; | |
757 | m_linesAdded = event.m_linesAdded; | |
758 | m_line = event.m_line; | |
759 | m_foldLevelNow = event.m_foldLevelNow; | |
760 | m_foldLevelPrev = event.m_foldLevelPrev; | |
761 | ||
762 | m_margin = event.m_margin; | |
763 | ||
764 | m_message = event.m_message; | |
765 | m_wParam = event.m_wParam; | |
766 | m_lParam = event.m_lParam; | |
767 | ||
768 | m_listType = event.m_listType; | |
769 | m_x = event.m_x; | |
770 | m_y = event.m_y; | |
f97d84a6 | 771 | |
5fa4613c RD |
772 | m_dragText = event.m_dragText; |
773 | m_dragAllowMove =event.m_dragAllowMove; | |
92bbd64f | 774 | #if wxUSE_DRAG_AND_DROP |
5fa4613c | 775 | m_dragResult = event.m_dragResult; |
92bbd64f | 776 | #endif |
f97d84a6 RD |
777 | } |
778 | ||
779 | //---------------------------------------------------------------------- | |
780 | //---------------------------------------------------------------------- | |
781 | ||
a29a241f RD |
782 | |
783 | ||
784 | ||
5fa4613c RD |
785 | |
786 | ||
787 | ||
788 | ||
789 |