]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/ScintillaWX.cpp
Added DnD related events to wxSTC
[wxWidgets.git] / contrib / src / stc / ScintillaWX.cpp
1 ////////////////////////////////////////////////////////////////////////////
2 // Name: ScintillaWX.cxx
3 // Purpose: A wxWindows implementation of Scintilla. A class derived
4 // from ScintillaBase that uses the "wx platform" defined in
5 // PlatformWX.cxx This class is one end of a bridge between
6 // the wx world and the Scintilla world. It needs a peer
7 // object of type wxStyledTextCtrl to function.
8 //
9 // Author: Robin Dunn
10 //
11 // Created: 13-Jan-2000
12 // RCS-ID: $Id$
13 // Copyright: (c) 2000 by Total Control Software
14 // Licence: wxWindows license
15 /////////////////////////////////////////////////////////////////////////////
16
17 #include <ctype.h>
18
19 #include "ScintillaWX.h"
20 #include "wx/stc/stc.h"
21
22
23 //----------------------------------------------------------------------
24
25 const int H_SCROLL_MAX = 2000;
26 const int H_SCROLL_STEP = 20;
27 const int H_SCROLL_PAGE = 200;
28
29 //----------------------------------------------------------------------
30 // Helper classes
31
32 class wxSTCTimer : public wxTimer {
33 public:
34 wxSTCTimer(ScintillaWX* swx) {
35 this->swx = swx;
36 }
37
38 void Notify() {
39 swx->DoTick();
40 }
41
42 private:
43 ScintillaWX* swx;
44 };
45
46
47
48 bool wxSTCDropTarget::OnDropText(wxCoord x, wxCoord y, const wxString& data) {
49 return swx->DoDropText(x, y, data);
50 }
51
52 wxDragResult wxSTCDropTarget::OnEnter(wxCoord x, wxCoord y, wxDragResult def) {
53 return swx->DoDragEnter(x, y, def);
54 }
55
56 wxDragResult wxSTCDropTarget::OnDragOver(wxCoord x, wxCoord y, wxDragResult def) {
57 return swx->DoDragOver(x, y, def);
58 }
59
60 void wxSTCDropTarget::OnLeave() {
61 swx->DoDragLeave();
62 }
63
64
65 class wxSTCCallTip : public wxWindow {
66 public:
67 wxSTCCallTip(wxWindow* parent, int ID, CallTip* ct)
68 : wxWindow(parent, ID)
69 {
70 m_ct = ct;
71 }
72
73 void OnPaint(wxPaintEvent& evt) {
74 wxPaintDC dc(this);
75 Surface surfaceWindow;
76 surfaceWindow.Init(&dc);
77 m_ct->PaintCT(&surfaceWindow);
78 surfaceWindow.Release();
79 }
80
81 CallTip* m_ct;
82 DECLARE_EVENT_TABLE()
83 };
84
85 BEGIN_EVENT_TABLE(wxSTCCallTip, wxWindow)
86 EVT_PAINT(wxSTCCallTip::OnPaint)
87 END_EVENT_TABLE()
88
89 //----------------------------------------------------------------------
90 // Constructor/Destructor
91
92
93 ScintillaWX::ScintillaWX(wxStyledTextCtrl* win) {
94 capturedMouse = false;
95 wMain = win;
96 stc = win;
97 wheelRotation = 0;
98 Initialise();
99 }
100
101
102 ScintillaWX::~ScintillaWX() {
103 SetTicking(false);
104 }
105
106 //----------------------------------------------------------------------
107 // base class virtuals
108
109
110 void ScintillaWX::Initialise() {
111 //ScintillaBase::Initialise();
112 dropTarget = new wxSTCDropTarget;
113 dropTarget->SetScintilla(this);
114 stc->SetDropTarget(dropTarget);
115 }
116
117
118 void ScintillaWX::Finalise() {
119 ScintillaBase::Finalise();
120 }
121
122
123 void ScintillaWX::StartDrag() {
124 wxString dragText(drag.s, drag.len);
125
126 // Send an event to allow the drag text to be changed
127 wxStyledTextEvent evt(wxEVT_STC_START_DRAG, stc->GetId());
128 evt.SetEventObject(stc);
129 evt.SetDragText(dragText);
130 evt.SetDragAllowMove(TRUE);
131 evt.SetPosition(wxMin(stc->GetSelectionStart(),
132 stc->GetSelectionEnd()));
133 stc->GetEventHandler()->ProcessEvent(evt);
134 dragText = evt.GetDragText();
135
136 if (dragText.Length()) {
137 wxDropSource source(wMain.GetID());
138 wxTextDataObject data(dragText);
139 wxDragResult result;
140
141 source.SetData(data);
142 dropWentOutside = TRUE;
143 result = source.DoDragDrop(evt.GetDragAllowMove());
144 if (result == wxDragMove && dropWentOutside)
145 ClearSelection();
146 inDragDrop = FALSE;
147 SetDragPosition(invalidPosition);
148 }
149 }
150
151
152 void ScintillaWX::SetTicking(bool on) {
153 wxSTCTimer* steTimer;
154 if (timer.ticking != on) {
155 timer.ticking = on;
156 if (timer.ticking) {
157 steTimer = new wxSTCTimer(this);
158 steTimer->Start(timer.tickSize);
159 timer.tickerID = (int)steTimer;
160 } else {
161 steTimer = (wxSTCTimer*)timer.tickerID;
162 steTimer->Stop();
163 delete steTimer;
164 timer.tickerID = 0;
165 }
166 }
167 timer.ticksToWait = caret.period;
168 }
169
170
171 void ScintillaWX::SetMouseCapture(bool on) {
172 if (on && !capturedMouse)
173 wMain.GetID()->CaptureMouse();
174 else if (!on && capturedMouse)
175 wMain.GetID()->ReleaseMouse();
176 capturedMouse = on;
177 }
178
179
180 bool ScintillaWX::HaveMouseCapture() {
181 return capturedMouse;
182 }
183
184
185 void ScintillaWX::ScrollText(int linesToMove) {
186 int dy = vs.lineHeight * (linesToMove);
187 wMain.GetID()->ScrollWindow(0, dy);
188 wMain.GetID()->Update();
189 }
190
191 void ScintillaWX::SetVerticalScrollPos() {
192 wMain.GetID()->SetScrollPos(wxVERTICAL, topLine);
193 }
194
195 void ScintillaWX::SetHorizontalScrollPos() {
196 wMain.GetID()->SetScrollPos(wxHORIZONTAL, xOffset);
197 }
198
199
200 bool ScintillaWX::ModifyScrollBars(int nMax, int nPage) {
201 bool modified = false;
202 int sbMax = wMain.GetID()->GetScrollRange(wxVERTICAL);
203 int sbThumb = wMain.GetID()->GetScrollThumb(wxVERTICAL);
204 int sbPos = wMain.GetID()->GetScrollPos(wxVERTICAL);
205
206
207 if (sbMax != nMax || sbThumb != nPage) {
208 wMain.GetID()->SetScrollbar(wxVERTICAL, sbPos, nPage, nMax);
209 modified = true;
210 }
211
212 sbMax = wMain.GetID()->GetScrollRange(wxHORIZONTAL);
213 sbThumb = wMain.GetID()->GetScrollThumb(wxHORIZONTAL);
214 if ((sbMax != H_SCROLL_MAX) || (sbThumb != H_SCROLL_STEP)) {
215 wMain.GetID()->SetScrollbar(wxHORIZONTAL, 0, H_SCROLL_STEP, H_SCROLL_MAX);
216 modified = true;
217 }
218 return modified;
219 }
220
221
222 void ScintillaWX::NotifyChange() {
223 stc->NotifyChange();
224 }
225
226
227 void ScintillaWX::NotifyParent(SCNotification scn) {
228 stc->NotifyParent(&scn);
229 }
230
231
232
233 void ScintillaWX::Copy() {
234 if (currentPos != anchor) {
235 SelectionText st;
236 CopySelectionRange(&st);
237 wxTheClipboard->Open();
238 wxTheClipboard->SetData(new wxTextDataObject(wxString(st.s, st.len)));
239 wxTheClipboard->Close();
240 }
241 }
242
243
244 void ScintillaWX::Paste() {
245 pdoc->BeginUndoAction();
246 ClearSelection();
247
248 wxTextDataObject data;
249 bool canPaste;
250
251 wxTheClipboard->Open();
252 canPaste = wxTheClipboard->GetData(data);
253 wxTheClipboard->Close();
254 if (canPaste) {
255 wxString str = data.GetText();
256 int len = str.Length();
257 pdoc->InsertString(currentPos, str.c_str(), len);
258 SetEmptySelection(currentPos + len);
259 }
260
261 pdoc->EndUndoAction();
262 NotifyChange();
263 Redraw();
264 }
265
266
267 bool ScintillaWX::CanPaste() {
268 wxTextDataObject data;
269 bool canPaste;
270
271 wxTheClipboard->Open();
272 canPaste = wxTheClipboard->GetData(data);
273 wxTheClipboard->Close();
274
275 return canPaste;
276 }
277
278 void ScintillaWX::CreateCallTipWindow(PRectangle) {
279 ct.wCallTip = new wxSTCCallTip(wMain.GetID(), -1, &ct);
280 ct.wDraw = ct.wCallTip;
281 }
282
283
284 void ScintillaWX::AddToPopUp(const char *label, int cmd, bool enabled) {
285 if (!label[0])
286 popup.GetID()->AppendSeparator();
287 else
288 popup.GetID()->Append(cmd, label);
289
290 if (!enabled)
291 popup.GetID()->Enable(cmd, enabled);
292 }
293
294
295 void ScintillaWX::ClaimSelection() {
296
297 }
298
299
300 long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
301 return 0;
302 }
303
304 long ScintillaWX::WndProc(unsigned int iMessage, unsigned long wParam, long lParam) {
305 // switch (iMessage) {
306 // case EM_CANPASTE:
307 // return CanPaste();
308 // default:
309 return ScintillaBase::WndProc(iMessage, wParam, lParam);
310 // }
311 // return 0;
312 }
313
314
315
316 //----------------------------------------------------------------------
317 // Event delegates
318
319 void ScintillaWX::DoPaint(wxDC* dc, wxRect rect) {
320
321 paintState = painting;
322 Surface surfaceWindow;
323 surfaceWindow.Init(dc);
324 PRectangle rcPaint = PRectangleFromwxRect(rect);
325 dc->BeginDrawing();
326 Paint(&surfaceWindow, rcPaint);
327 dc->EndDrawing();
328 surfaceWindow.Release();
329 if (paintState == paintAbandoned) {
330 // Painting area was insufficient to cover new styling or brace highlight positions
331 FullPaint();
332 }
333 paintState = notPainting;
334 #ifdef __WXGTK__
335 // On wxGTK the editor window paints can overwrite the listbox...
336 if (ac.Active())
337 ((wxWindow*)ac.lb.GetID())->Refresh(TRUE);
338 #endif
339 }
340
341
342 void ScintillaWX::DoHScroll(int type, int pos) {
343 int xPos = xOffset;
344 if (type == wxEVT_SCROLLWIN_LINEUP)
345 xPos -= H_SCROLL_STEP;
346 else if (type == wxEVT_SCROLLWIN_LINEDOWN)
347 xPos += H_SCROLL_STEP;
348 else if (type == wxEVT_SCROLLWIN_PAGEUP)
349 xPos -= H_SCROLL_PAGE;
350 else if (type == wxEVT_SCROLLWIN_PAGEDOWN)
351 xPos += H_SCROLL_PAGE;
352 else if (type == wxEVT_SCROLLWIN_TOP)
353 xPos = 0;
354 else if (type == wxEVT_SCROLLWIN_BOTTOM)
355 xPos = H_SCROLL_MAX;
356 else if (type == wxEVT_SCROLLWIN_THUMBTRACK)
357 xPos = pos;
358
359 HorizontalScrollTo(xPos);
360 }
361
362 void ScintillaWX::DoVScroll(int type, int pos) {
363 int topLineNew = topLine;
364 if (type == wxEVT_SCROLLWIN_LINEUP)
365 topLineNew -= 1;
366 else if (type == wxEVT_SCROLLWIN_LINEDOWN)
367 topLineNew += 1;
368 else if (type == wxEVT_SCROLLWIN_PAGEUP)
369 topLineNew -= LinesToScroll();
370 else if (type == wxEVT_SCROLLWIN_PAGEDOWN)
371 topLineNew += LinesToScroll();
372 else if (type == wxEVT_SCROLLWIN_TOP)
373 topLineNew = 0;
374 else if (type == wxEVT_SCROLLWIN_BOTTOM)
375 topLineNew = MaxScrollPos();
376 else if (type == wxEVT_SCROLLWIN_THUMBTRACK)
377 topLineNew = pos;
378
379 ScrollTo(topLineNew);
380 }
381
382
383 void ScintillaWX::DoMouseWheel(int rotation, int delta, int linesPerAction, int ctrlDown) {
384 int topLineNew = topLine;
385 int lines;
386
387 if (ctrlDown) { // Zoom the fonts if Ctrl key down
388 if (rotation < 0) {
389 KeyCommand(SCI_ZOOMIN);
390 }
391 else {
392 KeyCommand(SCI_ZOOMOUT);
393 }
394 }
395 else { // otherwise just scroll the window
396 wheelRotation += rotation;
397 lines = wheelRotation / delta;
398 wheelRotation -= lines * delta;
399 if (lines != 0) {
400 lines *= linesPerAction;
401 topLineNew -= lines;
402 ScrollTo(topLineNew);
403 }
404 }
405 }
406
407
408 void ScintillaWX::DoSize(int width, int height) {
409 PRectangle rcClient(0,0,width,height);
410 SetScrollBarsTo(rcClient);
411 DropGraphics();
412 }
413
414 void ScintillaWX::DoLoseFocus(){
415 SetFocusState(false);
416 }
417
418 void ScintillaWX::DoGainFocus(){
419 SetFocusState(true);
420 }
421
422 void ScintillaWX::DoSysColourChange() {
423 InvalidateStyleData();
424 }
425
426 void ScintillaWX::DoButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt) {
427 ButtonDown(pt, curTime, shift, ctrl, alt);
428 }
429
430 void ScintillaWX::DoButtonUp(Point pt, unsigned int curTime, bool ctrl) {
431 ButtonUp(pt, curTime, ctrl);
432 }
433
434 void ScintillaWX::DoButtonMove(Point pt) {
435 ButtonMove(pt);
436 }
437
438
439 void ScintillaWX::DoAddChar(char ch) {
440 AddChar(ch);
441 }
442
443 int ScintillaWX::DoKeyDown(int key, bool shift, bool ctrl, bool alt, bool* consumed) {
444 #ifdef __WXGTK__
445 // Ctrl chars (A-Z) end up with the wrong keycode on wxGTK...
446 if (ctrl && key >= 1 && key <= 26)
447 key += 'A' - 1;
448 #endif
449
450 switch (key) {
451 case WXK_DOWN: key = SCK_DOWN; break;
452 case WXK_UP: key = SCK_UP; break;
453 case WXK_LEFT: key = SCK_LEFT; break;
454 case WXK_RIGHT: key = SCK_RIGHT; break;
455 case WXK_HOME: key = SCK_HOME; break;
456 case WXK_END: key = SCK_END; break;
457 case WXK_PRIOR: key = SCK_PRIOR; break;
458 case WXK_NEXT: key = SCK_NEXT; break;
459 case WXK_DELETE: key = SCK_DELETE; break;
460 case WXK_INSERT: key = SCK_INSERT; break;
461 case WXK_ESCAPE: key = SCK_ESCAPE; break;
462 case WXK_BACK: key = SCK_BACK; break;
463 case WXK_TAB: key = SCK_TAB; break;
464 case WXK_RETURN: key = SCK_RETURN; break;
465 case WXK_ADD:
466 case WXK_NUMPAD_ADD:
467 key = SCK_ADD; break;
468 case WXK_SUBTRACT:
469 case WXK_NUMPAD_SUBTRACT:
470 key = SCK_SUBTRACT; break;
471 case WXK_DIVIDE:
472 case WXK_NUMPAD_DIVIDE:
473 key = SCK_DIVIDE; break;
474 case WXK_CONTROL: key = 0; break;
475 case WXK_ALT: key = 0; break;
476 case WXK_SHIFT: key = 0; break;
477 case WXK_MENU: key = 0; break;
478 }
479
480 int rv = KeyDown(key, shift, ctrl, alt, consumed);
481
482 if (key)
483 return rv;
484 else
485 return 1;
486 }
487
488
489 void ScintillaWX::DoCommand(int ID) {
490 Command(ID);
491 }
492
493
494 void ScintillaWX::DoContextMenu(Point pt) {
495 ContextMenu(pt);
496 }
497
498 void ScintillaWX::DoOnListBox() {
499 AutoCompleteCompleted();
500 }
501
502 //----------------------------------------------------------------------
503
504 bool ScintillaWX::DoDropText(long x, long y, const wxString& data) {
505 SetDragPosition(invalidPosition);
506
507 // Send an event to allow the drag details to be changed
508 wxStyledTextEvent evt(wxEVT_STC_DO_DROP, stc->GetId());
509 evt.SetEventObject(stc);
510 evt.SetDragResult(dragResult);
511 evt.SetX(x);
512 evt.SetY(y);
513 evt.SetPosition(PositionFromLocation(Point(x,y)));
514 evt.SetDragText(data);
515 stc->GetEventHandler()->ProcessEvent(evt);
516
517 dragResult = evt.GetDragResult();
518 if (dragResult == wxDragMove || dragResult == wxDragCopy) {
519 DropAt(evt.GetPosition(),
520 evt.GetDragText(),
521 dragResult == wxDragMove,
522 FALSE); // TODO: rectangular?
523 return TRUE;
524 }
525 return FALSE;
526 }
527
528
529 wxDragResult ScintillaWX::DoDragEnter(wxCoord x, wxCoord y, wxDragResult def) {
530 dragResult = def;
531 return dragResult;
532 }
533
534
535 wxDragResult ScintillaWX::DoDragOver(wxCoord x, wxCoord y, wxDragResult def) {
536 SetDragPosition(PositionFromLocation(Point(x, y)));
537
538 // Send an event to allow the drag result to be changed
539 wxStyledTextEvent evt(wxEVT_STC_DRAG_OVER, stc->GetId());
540 evt.SetEventObject(stc);
541 evt.SetDragResult(def);
542 evt.SetX(x);
543 evt.SetY(y);
544 evt.SetPosition(PositionFromLocation(Point(x,y)));
545 stc->GetEventHandler()->ProcessEvent(evt);
546
547 dragResult = evt.GetDragResult();
548 return dragResult;
549 }
550
551
552 void ScintillaWX::DoDragLeave() {
553 SetDragPosition(invalidPosition);
554 }
555
556 //----------------------------------------------------------------------
557
558 // Redraw all of text area. This paint will not be abandoned.
559 void ScintillaWX::FullPaint() {
560 paintState = painting;
561 rcPaint = GetTextRectangle();
562 paintingAllText = true;
563 wxClientDC dc(wMain.GetID());
564 Surface surfaceWindow;
565 surfaceWindow.Init(&dc);
566 Paint(&surfaceWindow, rcPaint);
567 surfaceWindow.Release();
568
569 // wMain.GetID()->Refresh(FALSE);
570
571 paintState = notPainting;
572 }
573
574
575 void ScintillaWX::DoScrollToLine(int line) {
576 ScrollTo(line);
577 }
578
579
580 void ScintillaWX::DoScrollToColumn(int column) {
581 HorizontalScrollTo(column * vs.spaceWidth);
582 }
583
584
585
586 //----------------------------------------------------------------------
587 //----------------------------------------------------------------------