]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/PlatWX.cpp
some basic docs for wxBase
[wxWidgets.git] / contrib / src / stc / PlatWX.cpp
1 // Scintilla source code edit control
2 // PlatWX.cxx - implementation of platform facilities on wxWindows
3 // Copyright 1998-1999 by Neil Hodgson <neilh@scintilla.org>
4 // Robin Dunn <robin@aldunn.com>
5 // The License.txt file describes the conditions under which this software may be distributed.
6
7 #include <ctype.h>
8
9 #include "Platform.h"
10 #include "wx/stc/stc.h"
11
12
13 #ifdef __WXGTK__
14 #include <gtk/gtk.h>
15 #endif
16
17 Point Point::FromLong(long lpoint) {
18 return Point(lpoint & 0xFFFF, lpoint >> 16);
19 }
20
21 wxRect wxRectFromPRectangle(PRectangle prc) {
22 wxRect rc(prc.left, prc.top,
23 prc.right-prc.left, prc.bottom-prc.top);
24 return rc;
25 }
26
27 PRectangle PRectangleFromwxRect(wxRect rc) {
28 return PRectangle(rc.GetLeft(), rc.GetTop(), rc.GetRight(), rc.GetBottom());
29 }
30
31 Colour::Colour(long lcol) {
32 co.Set(lcol & 0xff, (lcol >> 8) & 0xff, (lcol >> 16) & 0xff);
33 }
34
35 Colour::Colour(unsigned int red, unsigned int green, unsigned int blue) {
36 co.Set(red, green, blue);
37 }
38
39 bool Colour::operator==(const Colour &other) const {
40 return co == other.co;
41 }
42
43 long Colour::AsLong() const {
44 return (((long)co.Blue() << 16) |
45 ((long)co.Green() << 8) |
46 ((long)co.Red()));
47 }
48
49 unsigned int Colour::GetRed() {
50 return co.Red();
51 }
52
53 unsigned int Colour::GetGreen() {
54 return co.Green();
55 }
56
57 unsigned int Colour::GetBlue() {
58 return co.Blue();
59 }
60
61 Palette::Palette() {
62 used = 0;
63 allowRealization = false;
64 }
65
66 Palette::~Palette() {
67 Release();
68 }
69
70 void Palette::Release() {
71 used = 0;
72 }
73
74 // This method either adds a colour to the list of wanted colours (want==true)
75 // or retrieves the allocated colour back to the ColourPair.
76 // This is one method to make it easier to keep the code for wanting and retrieving in sync.
77 void Palette::WantFind(ColourPair &cp, bool want) {
78 if (want) {
79 for (int i=0; i < used; i++) {
80 if (entries[i].desired == cp.desired)
81 return;
82 }
83
84 if (used < numEntries) {
85 entries[used].desired = cp.desired;
86 entries[used].allocated = cp.desired;
87 used++;
88 }
89 } else {
90 for (int i=0; i < used; i++) {
91 if (entries[i].desired == cp.desired) {
92 cp.allocated = entries[i].allocated;
93 return;
94 }
95 }
96 cp.allocated = cp.desired;
97 }
98 }
99
100 void Palette::Allocate(Window &) {
101 if (allowRealization) {
102 }
103 }
104
105
106 Font::Font() {
107 id = 0;
108 ascent = 0;
109 }
110
111 Font::~Font() {
112 }
113
114 void Font::Create(const char *faceName, int characterSet, int size, bool bold, bool italic) {
115 // TODO: what to do about the characterSet?
116
117 Release();
118 id = new wxFont(size,
119 wxDEFAULT,
120 italic ? wxITALIC : wxNORMAL,
121 bold ? wxBOLD : wxNORMAL,
122 false,
123 faceName,
124 wxFONTENCODING_DEFAULT);
125 }
126
127
128 void Font::Release() {
129 if (id)
130 delete id;
131 id = 0;
132 }
133
134
135 Surface::Surface() :
136 hdc(0), hdcOwned(0), bitmap(0),
137 x(0), y(0) {
138 }
139
140 Surface::~Surface() {
141 Release();
142 }
143
144 void Surface::Release() {
145 if (bitmap) {
146 ((wxMemoryDC*)hdc)->SelectObject(wxNullBitmap);
147 delete bitmap;
148 bitmap = 0;
149 }
150 if (hdcOwned) {
151 delete hdc;
152 hdc = 0;
153 hdcOwned = false;
154 }
155 }
156
157
158 bool Surface::Initialised() {
159 return hdc != 0;
160 }
161
162 void Surface::Init() {
163 Release();
164 hdc = new wxMemoryDC();
165 hdcOwned = true;
166 }
167
168 void Surface::Init(SurfaceID hdc_) {
169 Release();
170 hdc = hdc_;
171 }
172
173 void Surface::InitPixMap(int width, int height, Surface *surface_) {
174 Release();
175 hdc = new wxMemoryDC(surface_->hdc);
176 hdcOwned = true;
177 if (width < 1) width = 1;
178 if (height < 1) height = 1;
179 bitmap = new wxBitmap(width, height);
180 ((wxMemoryDC*)hdc)->SelectObject(*bitmap);
181 }
182
183 void Surface::PenColour(Colour fore) {
184 hdc->SetPen(wxPen(fore.co, 1, wxSOLID));
185 }
186
187 void Surface::BrushColor(Colour back) {
188 hdc->SetBrush(wxBrush(back.co, wxSOLID));
189 }
190
191 void Surface::SetFont(Font &font_) {
192 if (font_.GetID()) {
193 hdc->SetFont(*font_.GetID());
194 }
195 }
196
197 int Surface::LogPixelsY() {
198 return hdc->GetPPI().y;
199 }
200
201
202 int Surface::DeviceHeightFont(int points) {
203 return points;
204 }
205
206
207 void Surface::MoveTo(int x_, int y_) {
208 x = x_;
209 y = y_;
210 }
211
212 void Surface::LineTo(int x_, int y_) {
213 hdc->DrawLine(x,y, x_,y_);
214 x = x_;
215 y = y_;
216 }
217
218 void Surface::Polygon(Point *pts, int npts, Colour fore,
219 Colour back) {
220 PenColour(fore);
221 BrushColor(back);
222 hdc->DrawPolygon(npts, (wxPoint*)pts);
223 }
224
225 void Surface::RectangleDraw(PRectangle rc, Colour fore, Colour back) {
226 PenColour(fore);
227 BrushColor(back);
228 hdc->DrawRectangle(wxRectFromPRectangle(rc));
229 }
230
231 void Surface::FillRectangle(PRectangle rc, Colour back) {
232 BrushColor(back);
233 hdc->SetPen(*wxTRANSPARENT_PEN);
234 hdc->DrawRectangle(wxRectFromPRectangle(rc));
235 }
236
237 void Surface::FillRectangle(PRectangle rc, Surface &surfacePattern) {
238 wxBrush br;
239 if (surfacePattern.bitmap)
240 br = wxBrush(*surfacePattern.bitmap);
241 else // Something is wrong so display in red
242 br = wxBrush(*wxRED, wxSOLID);
243 hdc->SetPen(*wxTRANSPARENT_PEN);
244 hdc->SetBrush(br);
245 hdc->DrawRectangle(wxRectFromPRectangle(rc));
246 }
247
248 void Surface::RoundedRectangle(PRectangle rc, Colour fore, Colour back) {
249 PenColour(fore);
250 BrushColor(back);
251 hdc->DrawRoundedRectangle(wxRectFromPRectangle(rc), 4);
252 }
253
254 void Surface::Ellipse(PRectangle rc, Colour fore, Colour back) {
255 PenColour(fore);
256 BrushColor(back);
257 hdc->DrawEllipse(wxRectFromPRectangle(rc));
258 }
259
260 void Surface::Copy(PRectangle rc, Point from, Surface &surfaceSource) {
261 wxRect r = wxRectFromPRectangle(rc);
262 hdc->Blit(r.x, r.y, r.width, r.height,
263 surfaceSource.hdc, from.x, from.y, wxCOPY);
264 }
265
266 void Surface::DrawText(PRectangle rc, Font &font, int ybase,
267 const char *s, int len, Colour fore, Colour back) {
268 SetFont(font);
269 hdc->SetTextForeground(fore.co);
270 hdc->SetTextBackground(back.co);
271 FillRectangle(rc, back);
272
273 // ybase is where the baseline should be, but wxWin uses the upper left
274 // corner, so I need to calculate the real position for the text...
275 hdc->DrawText(wxString(s, len), rc.left, ybase - font.ascent);
276 }
277
278 void Surface::DrawTextClipped(PRectangle rc, Font &font, int ybase, const char *s, int len, Colour fore, Colour back) {
279 SetFont(font);
280 hdc->SetTextForeground(fore.co);
281 hdc->SetTextBackground(back.co);
282 FillRectangle(rc, back);
283 hdc->SetClippingRegion(wxRectFromPRectangle(rc));
284
285 // see comments above
286 hdc->DrawText(wxString(s, len), rc.left, ybase - font.ascent);
287 hdc->DestroyClippingRegion();
288 }
289
290 int Surface::WidthText(Font &font, const char *s, int len) {
291 SetFont(font);
292 int w;
293 int h;
294 hdc->GetTextExtent(wxString(s, len), &w, &h);
295 return w;
296 }
297
298 void Surface::MeasureWidths(Font &font, const char *s, int len, int *positions) {
299 SetFont(font);
300 int totalWidth = 0;
301 for (int i=0; i<len; i++) {
302 int w;
303 int h;
304 hdc->GetTextExtent(s[i], &w, &h);
305 totalWidth += w;
306 positions[i] = totalWidth;
307 }
308 }
309
310 int Surface::WidthChar(Font &font, char ch) {
311 SetFont(font);
312 int w;
313 int h;
314 hdc->GetTextExtent(ch, &w, &h);
315 return w;
316 }
317
318 #define EXTENT_TEST " `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
319
320 int Surface::Ascent(Font &font) {
321 SetFont(font);
322 int w, h, d, e;
323 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
324 font.ascent = h - d;
325 return font.ascent;
326 }
327
328 int Surface::Descent(Font &font) {
329 SetFont(font);
330 int w, h, d, e;
331 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
332 return d;
333 }
334
335 int Surface::InternalLeading(Font &font) {
336 return 0;
337 }
338
339 int Surface::ExternalLeading(Font &font) {
340 SetFont(font);
341 int w, h, d, e;
342 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
343 return e;
344 }
345
346 int Surface::Height(Font &font) {
347 SetFont(font);
348 return hdc->GetCharHeight();
349 }
350
351 int Surface::AverageCharWidth(Font &font) {
352 SetFont(font);
353 return hdc->GetCharWidth();
354 }
355
356 int Surface::SetPalette(Palette *pal, bool inBackGround) {
357 return 0;
358 }
359
360 void Surface::SetClip(PRectangle rc) {
361 hdc->SetClippingRegion(wxRectFromPRectangle(rc));
362 }
363
364 void Surface::FlushCachedState() {
365 }
366
367 Window::~Window() {
368 }
369
370 void Window::Destroy() {
371 if (id)
372 id->Destroy();
373 id = 0;
374 }
375
376 bool Window::HasFocus() {
377 return wxWindow::FindFocus() == id;
378 }
379
380 PRectangle Window::GetPosition() {
381 wxRect rc(id->GetPosition(), id->GetSize());
382 return PRectangleFromwxRect(rc);
383 }
384
385 void Window::SetPosition(PRectangle rc) {
386 wxRect r = wxRectFromPRectangle(rc);
387 id->SetSize(r);
388 }
389
390 void Window::SetPositionRelative(PRectangle rc, Window) {
391 SetPosition(rc); // ????
392 }
393
394 PRectangle Window::GetClientPosition() {
395 wxSize sz = id->GetClientSize();
396 return PRectangle(0, 0, sz.x, sz.y);
397 }
398
399 void Window::Show(bool show) {
400 id->Show(show);
401 }
402
403 void Window::InvalidateAll() {
404 id->Refresh(false);
405 }
406
407 void Window::InvalidateRectangle(PRectangle rc) {
408 wxRect r = wxRectFromPRectangle(rc);
409 id->Refresh(false, &r);
410 }
411
412 void Window::SetFont(Font &font) {
413 id->SetFont(*font.GetID());
414 }
415
416 void Window::SetCursor(Cursor curs) {
417 int cursorId;
418
419 switch (curs) {
420 case cursorText:
421 cursorId = wxCURSOR_IBEAM;
422 break;
423 case cursorArrow:
424 cursorId = wxCURSOR_ARROW;
425 break;
426 case cursorUp:
427 cursorId = wxCURSOR_ARROW; // ** no up arrow... wxCURSOR_UPARROW;
428 break;
429 case cursorWait:
430 cursorId = wxCURSOR_WAIT;
431 break;
432 case cursorHoriz:
433 cursorId = wxCURSOR_SIZEWE;
434 break;
435 case cursorVert:
436 cursorId = wxCURSOR_SIZENS;
437 break;
438 case cursorReverseArrow:
439 cursorId = wxCURSOR_POINT_RIGHT;
440 break;
441 default:
442 cursorId = wxCURSOR_ARROW;
443 break;
444 }
445
446 id->SetCursor(wxCursor(cursorId));
447 }
448
449
450 void Window::SetTitle(const char *s) {
451 id->SetTitle(s);
452 }
453
454
455 class wxSTCListBox : public wxListBox {
456 public:
457 wxSTCListBox(wxWindow* parent, wxWindowID id)
458 : wxListBox(parent, id, wxDefaultPosition, wxDefaultSize,
459 0, NULL, wxLB_SINGLE | wxSIMPLE_BORDER) // | wxLB_SORT )
460 {}
461
462 void OnFocus(wxFocusEvent& event) {
463 GetParent()->SetFocus();
464 event.Skip();
465 }
466
467 private:
468 DECLARE_EVENT_TABLE()
469 };
470
471 BEGIN_EVENT_TABLE(wxSTCListBox, wxListBox)
472 EVT_SET_FOCUS(wxSTCListBox::OnFocus)
473 END_EVENT_TABLE()
474
475
476 ListBox::ListBox() {
477 }
478
479 ListBox::~ListBox() {
480 }
481
482 void ListBox::Create(Window &parent, int ctrlID) {
483 id = new wxSTCListBox(parent.id, ctrlID);
484 }
485
486 void ListBox::SetVisibleRows(int rows) {
487 desiredVisibleRows = rows;
488 }
489
490 PRectangle ListBox::GetDesiredRect() {
491 wxSize sz = ((wxListBox*)id)->GetBestSize();
492 PRectangle rc;
493 rc.top = 0;
494 rc.left = 0;
495 if (sz.x > 400)
496 sz.x = 400;
497 if (sz.y > 160) // TODO: Use desiredVisibleRows??
498 sz.y = 160;
499 rc.right = sz.x;
500 rc.bottom = sz.y;
501 return rc;
502 }
503
504 void ListBox::SetAverageCharWidth(int width) {
505 aveCharWidth = width;
506 }
507
508 void ListBox::SetFont(Font &font) {
509 Window::SetFont(font);
510 }
511
512 void ListBox::Clear() {
513 ((wxListBox*)id)->Clear();
514 }
515
516 void ListBox::Append(char *s) {
517 ((wxListBox*)id)->Append(s);
518 }
519
520 int ListBox::Length() {
521 return ((wxListBox*)id)->GetCount();
522 }
523
524 void ListBox::Select(int n) {
525 ((wxListBox*)id)->SetSelection(n);
526 #ifdef __WXGTK__
527 if (n > 4)
528 n = n - 4;
529 else
530 n = 1;
531 ((wxListBox*)id)->SetFirstItem(n);
532 #endif
533 }
534
535 int ListBox::GetSelection() {
536 return ((wxListBox*)id)->GetSelection();
537 }
538
539 int ListBox::Find(const char *prefix) {
540 // No longer used
541 return -1;
542 }
543
544 void ListBox::GetValue(int n, char *value, int len) {
545 wxString text = ((wxListBox*)id)->GetString(n);
546 strncpy(value, text.c_str(), len);
547 value[len-1] = '\0';
548 }
549
550 void ListBox::Sort() {
551 // wxWindows keeps sorted so no need to sort
552 }
553
554
555 Menu::Menu() : id(0) {
556 }
557
558 void Menu::CreatePopUp() {
559 Destroy();
560 id = new wxMenu();
561 }
562
563 void Menu::Destroy() {
564 if (id)
565 delete id;
566 id = 0;
567 }
568
569 void Menu::Show(Point pt, Window &w) {
570 w.GetID()->PopupMenu(id, pt.x - 4, pt.y);
571 Destroy();
572 }
573
574
575 Colour Platform::Chrome() {
576 wxColour c;
577 c = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
578 return Colour(c.Red(), c.Green(), c.Blue());
579 }
580
581 Colour Platform::ChromeHighlight() {
582 wxColour c;
583 c = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHIGHLIGHT);
584 return Colour(c.Red(), c.Green(), c.Blue());
585 }
586
587 const char *Platform::DefaultFont() {
588 return wxNORMAL_FONT->GetFaceName();
589 }
590
591 int Platform::DefaultFontSize() {
592 return 8;
593 }
594
595 unsigned int Platform::DoubleClickTime() {
596 return 500; // **** ::GetDoubleClickTime();
597 }
598
599 void Platform::DebugDisplay(const char *s) {
600 wxLogDebug(s);
601 }
602
603 bool Platform::IsKeyDown(int key) {
604 return false; // I don't think we'll need this.
605 }
606
607 long Platform::SendScintilla(WindowID w,
608 unsigned int msg,
609 unsigned long wParam,
610 long lParam) {
611
612 wxStyledTextCtrl* stc = (wxStyledTextCtrl*)w;
613 return stc->SendMsg(msg, wParam, lParam);
614 }
615
616
617 // These are utility functions not really tied to a platform
618
619 int Platform::Minimum(int a, int b) {
620 if (a < b)
621 return a;
622 else
623 return b;
624 }
625
626 int Platform::Maximum(int a, int b) {
627 if (a > b)
628 return a;
629 else
630 return b;
631 }
632
633 #define TRACE
634
635 void Platform::DebugPrintf(const char *format, ...) {
636 #ifdef TRACE
637 char buffer[2000];
638 va_list pArguments;
639 va_start(pArguments, format);
640 vsprintf(buffer,format,pArguments);
641 va_end(pArguments);
642 Platform::DebugDisplay(buffer);
643 #endif
644 }
645
646
647 static bool assertionPopUps = true;
648
649 bool Platform::ShowAssertionPopUps(bool assertionPopUps_) {
650 bool ret = assertionPopUps;
651 assertionPopUps = assertionPopUps_;
652 return ret;
653 }
654
655 void Platform::Assert(const char *c, const char *file, int line) {
656 char buffer[2000];
657 sprintf(buffer, "Assertion [%s] failed at %s %d", c, file, line);
658 if (assertionPopUps) {
659 int idButton = wxMessageBox(buffer, "Assertion failure",
660 wxICON_HAND | wxOK);
661 // if (idButton == IDRETRY) {
662 // ::DebugBreak();
663 // } else if (idButton == IDIGNORE) {
664 // // all OK
665 // } else {
666 // abort();
667 // }
668 } else {
669 strcat(buffer, "\r\n");
670 Platform::DebugDisplay(buffer);
671 abort();
672 }
673 }
674
675
676 int Platform::Clamp(int val, int minVal, int maxVal) {
677 if (val > maxVal)
678 val = maxVal;
679 if (val < minVal)
680 val = minVal;
681 return val;
682 }
683
684
685
686
687
688