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