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