]> git.saurik.com Git - wxWidgets.git/blob - src/stc/ScintillaWX.cpp
Changed CanPaste to just check IsSupported.
[wxWidgets.git] / 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 gotData;
250
251 wxTheClipboard->Open();
252 gotData = wxTheClipboard->GetData(data);
253 wxTheClipboard->Close();
254 if (gotData) {
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 bool canPaste;
269
270 wxTheClipboard->Open();
271 canPaste = wxTheClipboard->IsSupported( wxDF_TEXT );
272 wxTheClipboard->Close();
273
274 return canPaste;
275 }
276
277 void ScintillaWX::CreateCallTipWindow(PRectangle) {
278 ct.wCallTip = new wxSTCCallTip(wMain.GetID(), -1, &ct);
279 ct.wDraw = ct.wCallTip;
280 }
281
282
283 void ScintillaWX::AddToPopUp(const char *label, int cmd, bool enabled) {
284 if (!label[0])
285 popup.GetID()->AppendSeparator();
286 else
287 popup.GetID()->Append(cmd, label);
288
289 if (!enabled)
290 popup.GetID()->Enable(cmd, enabled);
291 }
292
293
294 void ScintillaWX::ClaimSelection() {
295
296 }
297
298
299 long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
300 return 0;
301 }
302
303 long ScintillaWX::WndProc(unsigned int iMessage, unsigned long wParam, long lParam) {
304 // switch (iMessage) {
305 // case EM_CANPASTE:
306 // return CanPaste();
307 // default:
308 return ScintillaBase::WndProc(iMessage, wParam, lParam);
309 // }
310 // return 0;
311 }
312
313
314
315 //----------------------------------------------------------------------
316 // Event delegates
317
318 void ScintillaWX::DoPaint(wxDC* dc, wxRect rect) {
319
320 paintState = painting;
321 Surface surfaceWindow;
322 surfaceWindow.Init(dc);
323 PRectangle rcPaint = PRectangleFromwxRect(rect);
324 dc->BeginDrawing();
325 Paint(&surfaceWindow, rcPaint);
326 dc->EndDrawing();
327 surfaceWindow.Release();
328 if (paintState == paintAbandoned) {
329 // Painting area was insufficient to cover new styling or brace highlight positions
330 FullPaint();
331 }
332 paintState = notPainting;
333 #ifdef __WXGTK__
334 // On wxGTK the editor window paints can overwrite the listbox...
335 if (ac.Active())
336 ((wxWindow*)ac.lb.GetID())->Refresh(TRUE);
337 #endif
338 }
339
340
341 void ScintillaWX::DoHScroll(int type, int pos) {
342 int xPos = xOffset;
343 if (type == wxEVT_SCROLLWIN_LINEUP)
344 xPos -= H_SCROLL_STEP;
345 else if (type == wxEVT_SCROLLWIN_LINEDOWN)
346 xPos += H_SCROLL_STEP;
347 else if (type == wxEVT_SCROLLWIN_PAGEUP)
348 xPos -= H_SCROLL_PAGE;
349 else if (type == wxEVT_SCROLLWIN_PAGEDOWN)
350 xPos += H_SCROLL_PAGE;
351 else if (type == wxEVT_SCROLLWIN_TOP)
352 xPos = 0;
353 else if (type == wxEVT_SCROLLWIN_BOTTOM)
354 xPos = H_SCROLL_MAX;
355 else if (type == wxEVT_SCROLLWIN_THUMBTRACK)
356 xPos = pos;
357
358 HorizontalScrollTo(xPos);
359 }
360
361 void ScintillaWX::DoVScroll(int type, int pos) {
362 int topLineNew = topLine;
363 if (type == wxEVT_SCROLLWIN_LINEUP)
364 topLineNew -= 1;
365 else if (type == wxEVT_SCROLLWIN_LINEDOWN)
366 topLineNew += 1;
367 else if (type == wxEVT_SCROLLWIN_PAGEUP)
368 topLineNew -= LinesToScroll();
369 else if (type == wxEVT_SCROLLWIN_PAGEDOWN)
370 topLineNew += LinesToScroll();
371 else if (type == wxEVT_SCROLLWIN_TOP)
372 topLineNew = 0;
373 else if (type == wxEVT_SCROLLWIN_BOTTOM)
374 topLineNew = MaxScrollPos();
375 else if (type == wxEVT_SCROLLWIN_THUMBTRACK)
376 topLineNew = pos;
377
378 ScrollTo(topLineNew);
379 }
380
381
382 void ScintillaWX::DoMouseWheel(int rotation, int delta, int linesPerAction, int ctrlDown) {
383 int topLineNew = topLine;
384 int lines;
385
386 if (ctrlDown) { // Zoom the fonts if Ctrl key down
387 if (rotation < 0) {
388 KeyCommand(SCI_ZOOMIN);
389 }
390 else {
391 KeyCommand(SCI_ZOOMOUT);
392 }
393 }
394 else { // otherwise just scroll the window
395 wheelRotation += rotation;
396 lines = wheelRotation / delta;
397 wheelRotation -= lines * delta;
398 if (lines != 0) {
399 lines *= linesPerAction;
400 topLineNew -= lines;
401 ScrollTo(topLineNew);
402 }
403 }
404 }
405
406
407 void ScintillaWX::DoSize(int width, int height) {
408 PRectangle rcClient(0,0,width,height);
409 SetScrollBarsTo(rcClient);
410 DropGraphics();
411 }
412
413 void ScintillaWX::DoLoseFocus(){
414 SetFocusState(false);
415 }
416
417 void ScintillaWX::DoGainFocus(){
418 SetFocusState(true);
419 }
420
421 void ScintillaWX::DoSysColourChange() {
422 InvalidateStyleData();
423 }
424
425 void ScintillaWX::DoButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt) {
426 ButtonDown(pt, curTime, shift, ctrl, alt);
427 }
428
429 void ScintillaWX::DoButtonUp(Point pt, unsigned int curTime, bool ctrl) {
430 ButtonUp(pt, curTime, ctrl);
431 }
432
433 void ScintillaWX::DoButtonMove(Point pt) {
434 ButtonMove(pt);
435 }
436
437
438 void ScintillaWX::DoAddChar(char ch) {
439 AddChar(ch);
440 }
441
442 int ScintillaWX::DoKeyDown(int key, bool shift, bool ctrl, bool alt, bool* consumed) {
443 #ifdef __WXGTK__
444 // Ctrl chars (A-Z) end up with the wrong keycode on wxGTK...
445 if (ctrl && key >= 1 && key <= 26)
446 key += 'A' - 1;
447 #endif
448
449 switch (key) {
450 case WXK_DOWN: key = SCK_DOWN; break;
451 case WXK_UP: key = SCK_UP; break;
452 case WXK_LEFT: key = SCK_LEFT; break;
453 case WXK_RIGHT: key = SCK_RIGHT; break;
454 case WXK_HOME: key = SCK_HOME; break;
455 case WXK_END: key = SCK_END; break;
456 case WXK_PRIOR: key = SCK_PRIOR; break;
457 case WXK_NEXT: key = SCK_NEXT; break;
458 case WXK_DELETE: key = SCK_DELETE; break;
459 case WXK_INSERT: key = SCK_INSERT; break;
460 case WXK_ESCAPE: key = SCK_ESCAPE; break;
461 case WXK_BACK: key = SCK_BACK; break;
462 case WXK_TAB: key = SCK_TAB; break;
463 case WXK_RETURN: key = SCK_RETURN; break;
464 case WXK_ADD:
465 case WXK_NUMPAD_ADD:
466 key = SCK_ADD; break;
467 case WXK_SUBTRACT:
468 case WXK_NUMPAD_SUBTRACT:
469 key = SCK_SUBTRACT; break;
470 case WXK_DIVIDE:
471 case WXK_NUMPAD_DIVIDE:
472 key = SCK_DIVIDE; break;
473 case WXK_CONTROL: key = 0; break;
474 case WXK_ALT: key = 0; break;
475 case WXK_SHIFT: key = 0; break;
476 case WXK_MENU: key = 0; break;
477 }
478
479 int rv = KeyDown(key, shift, ctrl, alt, consumed);
480
481 if (key)
482 return rv;
483 else
484 return 1;
485 }
486
487
488 void ScintillaWX::DoCommand(int ID) {
489 Command(ID);
490 }
491
492
493 void ScintillaWX::DoContextMenu(Point pt) {
494 ContextMenu(pt);
495 }
496
497 void ScintillaWX::DoOnListBox() {
498 AutoCompleteCompleted();
499 }
500
501 //----------------------------------------------------------------------
502
503 bool ScintillaWX::DoDropText(long x, long y, const wxString& data) {
504 SetDragPosition(invalidPosition);
505
506 // Send an event to allow the drag details to be changed
507 wxStyledTextEvent evt(wxEVT_STC_DO_DROP, stc->GetId());
508 evt.SetEventObject(stc);
509 evt.SetDragResult(dragResult);
510 evt.SetX(x);
511 evt.SetY(y);
512 evt.SetPosition(PositionFromLocation(Point(x,y)));
513 evt.SetDragText(data);
514 stc->GetEventHandler()->ProcessEvent(evt);
515
516 dragResult = evt.GetDragResult();
517 if (dragResult == wxDragMove || dragResult == wxDragCopy) {
518 DropAt(evt.GetPosition(),
519 evt.GetDragText(),
520 dragResult == wxDragMove,
521 FALSE); // TODO: rectangular?
522 return TRUE;
523 }
524 return FALSE;
525 }
526
527
528 wxDragResult ScintillaWX::DoDragEnter(wxCoord x, wxCoord y, wxDragResult def) {
529 dragResult = def;
530 return dragResult;
531 }
532
533
534 wxDragResult ScintillaWX::DoDragOver(wxCoord x, wxCoord y, wxDragResult def) {
535 SetDragPosition(PositionFromLocation(Point(x, y)));
536
537 // Send an event to allow the drag result to be changed
538 wxStyledTextEvent evt(wxEVT_STC_DRAG_OVER, stc->GetId());
539 evt.SetEventObject(stc);
540 evt.SetDragResult(def);
541 evt.SetX(x);
542 evt.SetY(y);
543 evt.SetPosition(PositionFromLocation(Point(x,y)));
544 stc->GetEventHandler()->ProcessEvent(evt);
545
546 dragResult = evt.GetDragResult();
547 return dragResult;
548 }
549
550
551 void ScintillaWX::DoDragLeave() {
552 SetDragPosition(invalidPosition);
553 }
554
555 //----------------------------------------------------------------------
556
557 // Redraw all of text area. This paint will not be abandoned.
558 void ScintillaWX::FullPaint() {
559 paintState = painting;
560 rcPaint = GetTextRectangle();
561 paintingAllText = true;
562 wxClientDC dc(wMain.GetID());
563 Surface surfaceWindow;
564 surfaceWindow.Init(&dc);
565 Paint(&surfaceWindow, rcPaint);
566 surfaceWindow.Release();
567
568 // wMain.GetID()->Refresh(FALSE);
569
570 paintState = notPainting;
571 }
572
573
574 void ScintillaWX::DoScrollToLine(int line) {
575 ScrollTo(line);
576 }
577
578
579 void ScintillaWX::DoScrollToColumn(int column) {
580 HorizontalScrollTo(column * vs.spaceWidth);
581 }
582
583
584
585 //----------------------------------------------------------------------
586 //----------------------------------------------------------------------