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