]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/stc/ScintillaWX.cpp
fix for typo
[wxWidgets.git] / contrib / src / stc / ScintillaWX.cpp
CommitLineData
9ce192d4
RD
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
f6bcfd97
BP
17#include <ctype.h>
18
9ce192d4
RD
19#include "ScintillaWX.h"
20#include "wx/stc/stc.h"
21
22
23//----------------------------------------------------------------------
24
25const int H_SCROLL_MAX = 2000;
26const int H_SCROLL_STEP = 20;
27const int H_SCROLL_PAGE = 200;
28
29//----------------------------------------------------------------------
30// Helper classes
31
32class wxSTCTimer : public wxTimer {
33public:
34 wxSTCTimer(ScintillaWX* swx) {
35 this->swx = swx;
36 }
37
38 void Notify() {
39 swx->DoTick();
40 }
41
42private:
43 ScintillaWX* swx;
44};
45
46
47
48bool wxSTCDropTarget::OnDropText(wxCoord x, wxCoord y, const wxString& data) {
49 return swx->DoDropText(x, y, data);
50}
51
52wxDragResult wxSTCDropTarget::OnEnter(wxCoord x, wxCoord y, wxDragResult def) {
53 return swx->DoDragEnter(x, y, def);
54}
55
56wxDragResult wxSTCDropTarget::OnDragOver(wxCoord x, wxCoord y, wxDragResult def) {
57 return swx->DoDragOver(x, y, def);
58}
59
60void wxSTCDropTarget::OnLeave() {
61 swx->DoDragLeave();
62}
63
64
f6bcfd97
BP
65class wxSTCCallTip : public wxWindow {
66public:
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
85BEGIN_EVENT_TABLE(wxSTCCallTip, wxWindow)
86 EVT_PAINT(wxSTCCallTip::OnPaint)
87END_EVENT_TABLE()
9ce192d4
RD
88
89//----------------------------------------------------------------------
90// Constructor/Destructor
91
92
93ScintillaWX::ScintillaWX(wxStyledTextCtrl* win) {
94 capturedMouse = false;
95 wMain = win;
9ce192d4 96 stc = win;
37d62433 97 wheelRotation = 0;
9ce192d4
RD
98 Initialise();
99}
100
101
102ScintillaWX::~ScintillaWX() {
103 SetTicking(false);
104}
105
106//----------------------------------------------------------------------
107// base class virtuals
108
109
110void ScintillaWX::Initialise() {
111 //ScintillaBase::Initialise();
9eb662e9
RD
112 dropTarget = new wxSTCDropTarget;
113 dropTarget->SetScintilla(this);
114 stc->SetDropTarget(dropTarget);
9ce192d4
RD
115}
116
117
118void ScintillaWX::Finalise() {
119 ScintillaBase::Finalise();
120}
121
122
123void ScintillaWX::StartDrag() {
a29a241f
RD
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 }
9ce192d4
RD
149}
150
151
152void 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
171void ScintillaWX::SetMouseCapture(bool on) {
8759d4d5 172 if (on && !capturedMouse)
9ce192d4 173 wMain.GetID()->CaptureMouse();
8759d4d5 174 else if (!on && capturedMouse)
9ce192d4
RD
175 wMain.GetID()->ReleaseMouse();
176 capturedMouse = on;
177}
178
179
180bool ScintillaWX::HaveMouseCapture() {
181 return capturedMouse;
182}
183
184
185void ScintillaWX::ScrollText(int linesToMove) {
186 int dy = vs.lineHeight * (linesToMove);
9ce192d4 187 wMain.GetID()->ScrollWindow(0, dy);
65ec6247 188 wMain.GetID()->Update();
9ce192d4
RD
189}
190
191void ScintillaWX::SetVerticalScrollPos() {
192 wMain.GetID()->SetScrollPos(wxVERTICAL, topLine);
193}
194
195void ScintillaWX::SetHorizontalScrollPos() {
196 wMain.GetID()->SetScrollPos(wxHORIZONTAL, xOffset);
197}
198
199
200bool 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
222void ScintillaWX::NotifyChange() {
223 stc->NotifyChange();
224}
225
226
227void ScintillaWX::NotifyParent(SCNotification scn) {
228 stc->NotifyParent(&scn);
229}
230
231
232
233void ScintillaWX::Copy() {
234 if (currentPos != anchor) {
b8b0e402
RD
235 SelectionText st;
236 CopySelectionRange(&st);
9ce192d4 237 wxTheClipboard->Open();
b8b0e402 238 wxTheClipboard->SetData(new wxTextDataObject(wxString(st.s, st.len)));
9ce192d4
RD
239 wxTheClipboard->Close();
240 }
241}
242
243
244void 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
267bool 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
278void ScintillaWX::CreateCallTipWindow(PRectangle) {
65ec6247 279 ct.wCallTip = new wxSTCCallTip(wMain.GetID(), -1, &ct);
9ce192d4
RD
280 ct.wDraw = ct.wCallTip;
281}
282
283
284void 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);
9ce192d4
RD
292}
293
294
295void ScintillaWX::ClaimSelection() {
296
297}
298
299
d134f170 300long ScintillaWX::DefWndProc(unsigned int /*iMessage*/, unsigned long /*wParam*/, long /*lParam*/) {
9ce192d4
RD
301 return 0;
302}
303
d134f170
RD
304long ScintillaWX::WndProc(unsigned int iMessage, unsigned long wParam, long lParam) {
305// switch (iMessage) {
306// case EM_CANPASTE:
307// return CanPaste();
308// default:
9ce192d4 309 return ScintillaBase::WndProc(iMessage, wParam, lParam);
d134f170
RD
310// }
311// return 0;
9ce192d4
RD
312}
313
314
315
316//----------------------------------------------------------------------
317// Event delegates
318
319void 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;
f97d84a6
RD
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
9ce192d4
RD
339}
340
341
342void ScintillaWX::DoHScroll(int type, int pos) {
343 int xPos = xOffset;
ce1ecc6d 344 if (type == wxEVT_SCROLLWIN_LINEUP)
9ce192d4 345 xPos -= H_SCROLL_STEP;
ce1ecc6d 346 else if (type == wxEVT_SCROLLWIN_LINEDOWN)
9ce192d4 347 xPos += H_SCROLL_STEP;
ce1ecc6d 348 else if (type == wxEVT_SCROLLWIN_PAGEUP)
9ce192d4 349 xPos -= H_SCROLL_PAGE;
ce1ecc6d 350 else if (type == wxEVT_SCROLLWIN_PAGEDOWN)
9ce192d4 351 xPos += H_SCROLL_PAGE;
ce1ecc6d 352 else if (type == wxEVT_SCROLLWIN_TOP)
9ce192d4 353 xPos = 0;
ce1ecc6d 354 else if (type == wxEVT_SCROLLWIN_BOTTOM)
9ce192d4 355 xPos = H_SCROLL_MAX;
ce1ecc6d 356 else if (type == wxEVT_SCROLLWIN_THUMBTRACK)
9ce192d4 357 xPos = pos;
ce1ecc6d 358
9ce192d4
RD
359 HorizontalScrollTo(xPos);
360}
361
362void ScintillaWX::DoVScroll(int type, int pos) {
363 int topLineNew = topLine;
ce1ecc6d 364 if (type == wxEVT_SCROLLWIN_LINEUP)
9ce192d4 365 topLineNew -= 1;
ce1ecc6d 366 else if (type == wxEVT_SCROLLWIN_LINEDOWN)
9ce192d4 367 topLineNew += 1;
ce1ecc6d 368 else if (type == wxEVT_SCROLLWIN_PAGEUP)
9ce192d4 369 topLineNew -= LinesToScroll();
ce1ecc6d 370 else if (type == wxEVT_SCROLLWIN_PAGEDOWN)
9ce192d4 371 topLineNew += LinesToScroll();
ce1ecc6d 372 else if (type == wxEVT_SCROLLWIN_TOP)
9ce192d4 373 topLineNew = 0;
ce1ecc6d 374 else if (type == wxEVT_SCROLLWIN_BOTTOM)
9ce192d4 375 topLineNew = MaxScrollPos();
ce1ecc6d 376 else if (type == wxEVT_SCROLLWIN_THUMBTRACK)
9ce192d4 377 topLineNew = pos;
ce1ecc6d 378
9ce192d4
RD
379 ScrollTo(topLineNew);
380}
381
37d62433 382
65ec6247 383void ScintillaWX::DoMouseWheel(int rotation, int delta, int linesPerAction, int ctrlDown) {
37d62433
RD
384 int topLineNew = topLine;
385 int lines;
386
65ec6247
RD
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 }
37d62433
RD
404 }
405}
406
407
9ce192d4
RD
408void ScintillaWX::DoSize(int width, int height) {
409 PRectangle rcClient(0,0,width,height);
410 SetScrollBarsTo(rcClient);
411 DropGraphics();
412}
413
414void ScintillaWX::DoLoseFocus(){
65ec6247 415 SetFocusState(false);
9ce192d4
RD
416}
417
418void ScintillaWX::DoGainFocus(){
65ec6247 419 SetFocusState(true);
9ce192d4
RD
420}
421
422void ScintillaWX::DoSysColourChange() {
423 InvalidateStyleData();
424}
425
426void ScintillaWX::DoButtonDown(Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt) {
427 ButtonDown(pt, curTime, shift, ctrl, alt);
428}
429
430void ScintillaWX::DoButtonUp(Point pt, unsigned int curTime, bool ctrl) {
431 ButtonUp(pt, curTime, ctrl);
432}
433
434void ScintillaWX::DoButtonMove(Point pt) {
435 ButtonMove(pt);
436}
437
438
439void ScintillaWX::DoAddChar(char ch) {
440 AddChar(ch);
441}
442
65ec6247 443int ScintillaWX::DoKeyDown(int key, bool shift, bool ctrl, bool alt, bool* consumed) {
39178e3b
RD
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
d134f170 450 switch (key) {
d6582821
RD
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;
d134f170
RD
478 }
479
d6582821 480 int rv = KeyDown(key, shift, ctrl, alt, consumed);
0122b7e3 481
d6582821
RD
482 if (key)
483 return rv;
484 else
485 return 1;
9ce192d4
RD
486}
487
488
489void ScintillaWX::DoCommand(int ID) {
490 Command(ID);
491}
492
493
494void ScintillaWX::DoContextMenu(Point pt) {
495 ContextMenu(pt);
496}
497
f6bcfd97
BP
498void ScintillaWX::DoOnListBox() {
499 AutoCompleteCompleted();
500}
9ce192d4
RD
501
502//----------------------------------------------------------------------
503
504bool ScintillaWX::DoDropText(long x, long y, const wxString& data) {
505 SetDragPosition(invalidPosition);
a29a241f
RD
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;
9ce192d4
RD
526}
527
528
529wxDragResult ScintillaWX::DoDragEnter(wxCoord x, wxCoord y, wxDragResult def) {
b8b0e402
RD
530 dragResult = def;
531 return dragResult;
9ce192d4
RD
532}
533
534
535wxDragResult ScintillaWX::DoDragOver(wxCoord x, wxCoord y, wxDragResult def) {
536 SetDragPosition(PositionFromLocation(Point(x, y)));
a29a241f
RD
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();
b8b0e402 548 return dragResult;
9ce192d4
RD
549}
550
551
552void ScintillaWX::DoDragLeave() {
553 SetDragPosition(invalidPosition);
554}
555
556//----------------------------------------------------------------------
557
558// Redraw all of text area. This paint will not be abandoned.
559void ScintillaWX::FullPaint() {
560 paintState = painting;
a7642be1
RD
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
9ce192d4
RD
571 paintState = notPainting;
572}
573
574
575void ScintillaWX::DoScrollToLine(int line) {
576 ScrollTo(line);
577}
578
579
580void ScintillaWX::DoScrollToColumn(int column) {
581 HorizontalScrollTo(column * vs.spaceWidth);
582}
583
584
585
586//----------------------------------------------------------------------
587//----------------------------------------------------------------------