]> git.saurik.com Git - wxWidgets.git/blob - src/stc/PlatWX.cpp
dad221dbd2d18ade1a929c99b68d4f1e5120d60c
[wxWidgets.git] / 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 <wx/wx.h>
10 #include <wx/encconv.h>
11
12
13 #include "Platform.h"
14 #include "PlatWX.h"
15 #include "wx/stc/stc.h"
16
17
18 #ifdef __WXGTK__
19 #include <gtk/gtk.h>
20 #endif
21
22
23 Point Point::FromLong(long lpoint) {
24 return Point(lpoint & 0xFFFF, lpoint >> 16);
25 }
26
27 wxRect wxRectFromPRectangle(PRectangle prc) {
28 wxRect rc(prc.left, prc.top,
29 prc.right-prc.left, prc.bottom-prc.top);
30 return rc;
31 }
32
33 PRectangle PRectangleFromwxRect(wxRect rc) {
34 return PRectangle(rc.GetLeft(), rc.GetTop(),
35 rc.GetRight()+1, rc.GetBottom()+1);
36 }
37
38 wxColour wxColourFromCA(const ColourAllocated& ca) {
39 ColourDesired cd(ca.AsLong());
40 return wxColour(cd.GetRed(), cd.GetGreen(), cd.GetBlue());
41 }
42
43 //----------------------------------------------------------------------
44
45 Palette::Palette() {
46 used = 0;
47 allowRealization = false;
48 }
49
50 Palette::~Palette() {
51 Release();
52 }
53
54 void Palette::Release() {
55 used = 0;
56 }
57
58 // This method either adds a colour to the list of wanted colours (want==true)
59 // or retrieves the allocated colour back to the ColourPair.
60 // This is one method to make it easier to keep the code for wanting and retrieving in sync.
61 void Palette::WantFind(ColourPair &cp, bool want) {
62 if (want) {
63 for (int i=0; i < used; i++) {
64 if (entries[i].desired == cp.desired)
65 return;
66 }
67
68 if (used < numEntries) {
69 entries[used].desired = cp.desired;
70 entries[used].allocated.Set(cp.desired.AsLong());
71 used++;
72 }
73 } else {
74 for (int i=0; i < used; i++) {
75 if (entries[i].desired == cp.desired) {
76 cp.allocated = entries[i].allocated;
77 return;
78 }
79 }
80 cp.allocated.Set(cp.desired.AsLong());
81 }
82 }
83
84 void Palette::Allocate(Window &) {
85 if (allowRealization) {
86 }
87 }
88
89
90 //----------------------------------------------------------------------
91
92 Font::Font() {
93 id = 0;
94 ascent = 0;
95 }
96
97 Font::~Font() {
98 }
99
100 void Font::Create(const char *faceName, int characterSet, int size, bool bold, bool italic) {
101 wxFontEncoding encoding;
102
103 Release();
104
105 switch (characterSet) {
106 default:
107 case wxSTC_CHARSET_ANSI:
108 case wxSTC_CHARSET_DEFAULT:
109 encoding = wxFONTENCODING_DEFAULT;
110 break;
111
112 case wxSTC_CHARSET_BALTIC:
113 encoding = wxFONTENCODING_ISO8859_13;
114 break;
115
116 case wxSTC_CHARSET_CHINESEBIG5:
117 encoding = wxFONTENCODING_CP950;
118 break;
119
120 case wxSTC_CHARSET_EASTEUROPE:
121 encoding = wxFONTENCODING_ISO8859_2;
122 break;
123
124 case wxSTC_CHARSET_GB2312:
125 encoding = wxFONTENCODING_CP936;
126 break;
127
128 case wxSTC_CHARSET_GREEK:
129 encoding = wxFONTENCODING_ISO8859_7;
130 break;
131
132 case wxSTC_CHARSET_HANGUL:
133 encoding = wxFONTENCODING_CP949;
134 break;
135
136 case wxSTC_CHARSET_MAC:
137 encoding = wxFONTENCODING_DEFAULT;
138 break;
139
140 case wxSTC_CHARSET_OEM:
141 encoding = wxFONTENCODING_DEFAULT;
142 break;
143
144 case wxSTC_CHARSET_RUSSIAN:
145 encoding = wxFONTENCODING_KOI8;
146 break;
147
148 case wxSTC_CHARSET_SHIFTJIS:
149 encoding = wxFONTENCODING_CP932;
150 break;
151
152 case wxSTC_CHARSET_SYMBOL:
153 encoding = wxFONTENCODING_DEFAULT;
154 break;
155
156 case wxSTC_CHARSET_TURKISH:
157 encoding = wxFONTENCODING_ISO8859_9;
158 break;
159
160 case wxSTC_CHARSET_JOHAB:
161 encoding = wxFONTENCODING_DEFAULT;
162 break;
163
164 case wxSTC_CHARSET_HEBREW:
165 encoding = wxFONTENCODING_ISO8859_8;
166 break;
167
168 case wxSTC_CHARSET_ARABIC:
169 encoding = wxFONTENCODING_ISO8859_6;
170 break;
171
172 case wxSTC_CHARSET_VIETNAMESE:
173 encoding = wxFONTENCODING_DEFAULT;
174 break;
175
176 case wxSTC_CHARSET_THAI:
177 encoding = wxFONTENCODING_ISO8859_11;
178 break;
179 }
180
181 wxFontEncodingArray ea = wxEncodingConverter::GetPlatformEquivalents(encoding);
182 if (ea.GetCount())
183 encoding = ea[0];
184
185 id = new wxFont(size,
186 wxDEFAULT,
187 italic ? wxITALIC : wxNORMAL,
188 bold ? wxBOLD : wxNORMAL,
189 false,
190 stc2wx(faceName),
191 encoding);
192 }
193
194
195 void Font::Release() {
196 if (id)
197 delete (wxFont*)id;
198 id = 0;
199 }
200
201 //----------------------------------------------------------------------
202
203 class SurfaceImpl : public Surface {
204 private:
205 wxDC* hdc;
206 bool hdcOwned;
207 wxBitmap* bitmap;
208 int x;
209 int y;
210 bool unicodeMode;
211
212 public:
213 SurfaceImpl();
214 ~SurfaceImpl();
215
216 void Init();
217 void Init(SurfaceID sid);
218 void InitPixMap(int width, int height, Surface *surface_);
219
220 void Release();
221 bool Initialised();
222 void PenColour(ColourAllocated fore);
223 int LogPixelsY();
224 int DeviceHeightFont(int points);
225 void MoveTo(int x_, int y_);
226 void LineTo(int x_, int y_);
227 void Polygon(Point *pts, int npts, ColourAllocated fore, ColourAllocated back);
228 void RectangleDraw(PRectangle rc, ColourAllocated fore, ColourAllocated back);
229 void FillRectangle(PRectangle rc, ColourAllocated back);
230 void FillRectangle(PRectangle rc, Surface &surfacePattern);
231 void RoundedRectangle(PRectangle rc, ColourAllocated fore, ColourAllocated back);
232 void Ellipse(PRectangle rc, ColourAllocated fore, ColourAllocated back);
233 void Copy(PRectangle rc, Point from, Surface &surfaceSource);
234
235 void DrawTextNoClip(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore, ColourAllocated back);
236 void DrawTextClipped(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore, ColourAllocated back);
237 void MeasureWidths(Font &font_, const char *s, int len, int *positions);
238 int WidthText(Font &font_, const char *s, int len);
239 int WidthChar(Font &font_, char ch);
240 int Ascent(Font &font_);
241 int Descent(Font &font_);
242 int InternalLeading(Font &font_);
243 int ExternalLeading(Font &font_);
244 int Height(Font &font_);
245 int AverageCharWidth(Font &font_);
246
247 int SetPalette(Palette *pal, bool inBackGround);
248 void SetClip(PRectangle rc);
249 void FlushCachedState();
250
251 void SetUnicodeMode(bool unicodeMode_);
252
253 void BrushColour(ColourAllocated back);
254 void SetFont(Font &font_);
255 };
256
257
258
259 SurfaceImpl::SurfaceImpl() :
260 hdc(0), hdcOwned(0), bitmap(0),
261 x(0), y(0), unicodeMode(0)
262 {}
263
264 SurfaceImpl::~SurfaceImpl() {
265 Release();
266 }
267
268 void SurfaceImpl::Release() {
269 if (bitmap) {
270 ((wxMemoryDC*)hdc)->SelectObject(wxNullBitmap);
271 delete bitmap;
272 bitmap = 0;
273 }
274 if (hdcOwned) {
275 delete hdc;
276 hdc = 0;
277 hdcOwned = false;
278 }
279 }
280
281
282 bool SurfaceImpl::Initialised() {
283 return hdc != 0;
284 }
285
286 void SurfaceImpl::Init() {
287 #if 0
288 Release();
289 hdc = new wxMemoryDC();
290 hdcOwned = true;
291 #else
292 // On Mac and GTK(?) the DC is not really valid until it has a bitmap
293 // selected into it. So instead of just creating the DC with no bitmap,
294 // go ahead and give it one.
295 InitPixMap(1,1,NULL);
296 }
297
298 void SurfaceImpl::Init(SurfaceID hdc_) {
299 Release();
300 hdc = (wxDC*)hdc_;
301 }
302
303 void SurfaceImpl::InitPixMap(int width, int height, Surface *surface_) {
304 Release();
305 hdc = new wxMemoryDC();
306 hdcOwned = true;
307 if (width < 1) width = 1;
308 if (height < 1) height = 1;
309 bitmap = new wxBitmap(width, height);
310 ((wxMemoryDC*)hdc)->SelectObject(*bitmap);
311 }
312
313 void SurfaceImpl::PenColour(ColourAllocated fore) {
314 hdc->SetPen(wxPen(wxColourFromCA(fore), 1, wxSOLID));
315 }
316
317 void SurfaceImpl::BrushColour(ColourAllocated back) {
318 hdc->SetBrush(wxBrush(wxColourFromCA(back), wxSOLID));
319 }
320
321 void SurfaceImpl::SetFont(Font &font_) {
322 if (font_.GetID()) {
323 hdc->SetFont(*((wxFont*)font_.GetID()));
324 }
325 }
326
327 int SurfaceImpl::LogPixelsY() {
328 return hdc->GetPPI().y;
329 }
330
331 int SurfaceImpl::DeviceHeightFont(int points) {
332 return points;
333 }
334
335 void SurfaceImpl::MoveTo(int x_, int y_) {
336 x = x_;
337 y = y_;
338 }
339
340 void SurfaceImpl::LineTo(int x_, int y_) {
341 hdc->DrawLine(x,y, x_,y_);
342 x = x_;
343 y = y_;
344 }
345
346 void SurfaceImpl::Polygon(Point *pts, int npts, ColourAllocated fore, ColourAllocated back) {
347 PenColour(fore);
348 BrushColour(back);
349 hdc->DrawPolygon(npts, (wxPoint*)pts);
350 }
351
352 void SurfaceImpl::RectangleDraw(PRectangle rc, ColourAllocated fore, ColourAllocated back) {
353 PenColour(fore);
354 BrushColour(back);
355 hdc->DrawRectangle(wxRectFromPRectangle(rc));
356 }
357
358 void SurfaceImpl::FillRectangle(PRectangle rc, ColourAllocated back) {
359 BrushColour(back);
360 hdc->SetPen(*wxTRANSPARENT_PEN);
361 hdc->DrawRectangle(wxRectFromPRectangle(rc));
362 }
363
364 void SurfaceImpl::FillRectangle(PRectangle rc, Surface &surfacePattern) {
365 wxBrush br;
366 if (((SurfaceImpl&)surfacePattern).bitmap)
367 br = wxBrush(*((SurfaceImpl&)surfacePattern).bitmap);
368 else // Something is wrong so display in red
369 br = wxBrush(*wxRED, wxSOLID);
370 hdc->SetPen(*wxTRANSPARENT_PEN);
371 hdc->SetBrush(br);
372 hdc->DrawRectangle(wxRectFromPRectangle(rc));
373 }
374
375 void SurfaceImpl::RoundedRectangle(PRectangle rc, ColourAllocated fore, ColourAllocated back) {
376 PenColour(fore);
377 BrushColour(back);
378 hdc->DrawRoundedRectangle(wxRectFromPRectangle(rc), 4);
379 }
380
381 void SurfaceImpl::Ellipse(PRectangle rc, ColourAllocated fore, ColourAllocated back) {
382 PenColour(fore);
383 BrushColour(back);
384 hdc->DrawEllipse(wxRectFromPRectangle(rc));
385 }
386
387 void SurfaceImpl::Copy(PRectangle rc, Point from, Surface &surfaceSource) {
388 wxRect r = wxRectFromPRectangle(rc);
389 hdc->Blit(r.x, r.y, r.width, r.height,
390 ((SurfaceImpl&)surfaceSource).hdc,
391 from.x, from.y, wxCOPY);
392 }
393
394 void SurfaceImpl::DrawTextNoClip(PRectangle rc, Font &font, int ybase,
395 const char *s, int len,
396 ColourAllocated fore, ColourAllocated back) {
397 SetFont(font);
398 hdc->SetTextForeground(wxColourFromCA(fore));
399 hdc->SetTextBackground(wxColourFromCA(back));
400 FillRectangle(rc, back);
401
402 // ybase is where the baseline should be, but wxWin uses the upper left
403 // corner, so I need to calculate the real position for the text...
404 hdc->DrawText(stc2wx(s, len), rc.left, ybase - font.ascent);
405 }
406
407 void SurfaceImpl::DrawTextClipped(PRectangle rc, Font &font, int ybase,
408 const char *s, int len,
409 ColourAllocated fore, ColourAllocated back) {
410 SetFont(font);
411 hdc->SetTextForeground(wxColourFromCA(fore));
412 hdc->SetTextBackground(wxColourFromCA(back));
413 FillRectangle(rc, back);
414 hdc->SetClippingRegion(wxRectFromPRectangle(rc));
415
416 // see comments above
417 hdc->DrawText(stc2wx(s, len), rc.left, ybase - font.ascent);
418 hdc->DestroyClippingRegion();
419 }
420
421 int SurfaceImpl::WidthText(Font &font, const char *s, int len) {
422 SetFont(font);
423 int w;
424 int h;
425
426 hdc->GetTextExtent(stc2wx(s, len), &w, &h);
427 return w;
428 }
429
430
431 void SurfaceImpl::MeasureWidths(Font &font, const char *s, int len, int *positions) {
432 wxString str = stc2wx(s, len);
433 SetFont(font);
434
435 // Calculate the position of each character based on the widths of
436 // the previous characters
437 int* tpos = new int[len];
438 int totalWidth = 0;
439 size_t i;
440 for (i=0; i<str.Length(); i++) {
441 int w, h;
442 hdc->GetTextExtent(str[i], &w, &h);
443 totalWidth += w;
444 tpos[i] = totalWidth;
445 }
446
447 #if wxUSE_UNICODE
448 // Map the widths for UCS-2 characters back to the UTF-8 input string
449 i = 0;
450 size_t ui = 0;
451 while (i < len) {
452 unsigned char uch = (unsigned char)s[i];
453 positions[i++] = tpos[ui];
454 if (uch >= 0x80) {
455 if (uch < (0x80 + 0x40 + 0x20)) {
456 positions[i++] = tpos[ui];
457 } else {
458 positions[i++] = tpos[ui];
459 positions[i++] = tpos[ui];
460 }
461 }
462 ui++;
463 }
464 #else
465
466 // If not unicode then just use the widths we have
467 memcpy(positions, tpos, len * sizeof(*tpos));
468 #endif
469
470 delete [] tpos;
471 }
472
473
474 int SurfaceImpl::WidthChar(Font &font, char ch) {
475 SetFont(font);
476 int w;
477 int h;
478 char s[2] = { ch, 0 };
479
480 hdc->GetTextExtent(stc2wx(s, 1), &w, &h);
481 return w;
482 }
483
484 #define EXTENT_TEST wxT(" `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
485
486 int SurfaceImpl::Ascent(Font &font) {
487 SetFont(font);
488 int w, h, d, e;
489 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
490 font.ascent = h - d;
491 return font.ascent;
492 }
493
494 int SurfaceImpl::Descent(Font &font) {
495 SetFont(font);
496 int w, h, d, e;
497 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
498 return d;
499 }
500
501 int SurfaceImpl::InternalLeading(Font &font) {
502 return 0;
503 }
504
505 int SurfaceImpl::ExternalLeading(Font &font) {
506 SetFont(font);
507 int w, h, d, e;
508 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
509 return e;
510 }
511
512 int SurfaceImpl::Height(Font &font) {
513 SetFont(font);
514 return hdc->GetCharHeight();
515 }
516
517 int SurfaceImpl::AverageCharWidth(Font &font) {
518 SetFont(font);
519 return hdc->GetCharWidth();
520 }
521
522 int SurfaceImpl::SetPalette(Palette *pal, bool inBackGround) {
523 return 0;
524 }
525
526 void SurfaceImpl::SetClip(PRectangle rc) {
527 hdc->SetClippingRegion(wxRectFromPRectangle(rc));
528 }
529
530 void SurfaceImpl::FlushCachedState() {
531 }
532
533 void SurfaceImpl::SetUnicodeMode(bool unicodeMode_) {
534 unicodeMode=unicodeMode_;
535 #if wxUSE_UNICODE
536 wxASSERT_MSG(unicodeMode == wxUSE_UNICODE,
537 wxT("Only unicode may be used when wxUSE_UNICODE is on."));
538 #else
539 wxASSERT_MSG(unicodeMode == wxUSE_UNICODE,
540 wxT("Only non-unicode may be used when wxUSE_UNICODE is off."));
541 #endif
542 }
543
544 Surface *Surface::Allocate() {
545 return new SurfaceImpl;
546 }
547
548
549 //----------------------------------------------------------------------
550
551
552 inline wxWindow* GETWIN(WindowID id) { return (wxWindow*)id; }
553
554 Window::~Window() {
555 }
556
557 void Window::Destroy() {
558 if (id)
559 GETWIN(id)->Destroy();
560 id = 0;
561 }
562
563 bool Window::HasFocus() {
564 return wxWindow::FindFocus() == GETWIN(id);
565 }
566
567 PRectangle Window::GetPosition() {
568 wxRect rc(GETWIN(id)->GetPosition(), GETWIN(id)->GetSize());
569 return PRectangleFromwxRect(rc);
570 }
571
572 void Window::SetPosition(PRectangle rc) {
573 wxRect r = wxRectFromPRectangle(rc);
574 GETWIN(id)->SetSize(r);
575 }
576
577 void Window::SetPositionRelative(PRectangle rc, Window) {
578 SetPosition(rc); // ????
579 }
580
581 PRectangle Window::GetClientPosition() {
582 wxSize sz = GETWIN(id)->GetClientSize();
583 return PRectangle(0, 0, sz.x, sz.y);
584 }
585
586 void Window::Show(bool show) {
587 GETWIN(id)->Show(show);
588 }
589
590 void Window::InvalidateAll() {
591 GETWIN(id)->Refresh(false);
592 wxWakeUpIdle();
593 }
594
595 void Window::InvalidateRectangle(PRectangle rc) {
596 wxRect r = wxRectFromPRectangle(rc);
597 GETWIN(id)->Refresh(false, &r);
598 wxWakeUpIdle();
599 }
600
601 void Window::SetFont(Font &font) {
602 GETWIN(id)->SetFont(*((wxFont*)font.GetID()));
603 }
604
605 void Window::SetCursor(Cursor curs) {
606 int cursorId;
607
608 switch (curs) {
609 case cursorText:
610 cursorId = wxCURSOR_IBEAM;
611 break;
612 case cursorArrow:
613 cursorId = wxCURSOR_ARROW;
614 break;
615 case cursorUp:
616 cursorId = wxCURSOR_ARROW; // ** no up arrow... wxCURSOR_UPARROW;
617 break;
618 case cursorWait:
619 cursorId = wxCURSOR_WAIT;
620 break;
621 case cursorHoriz:
622 cursorId = wxCURSOR_SIZEWE;
623 break;
624 case cursorVert:
625 cursorId = wxCURSOR_SIZENS;
626 break;
627 case cursorReverseArrow:
628 cursorId = wxCURSOR_RIGHT_ARROW;
629 break;
630 default:
631 cursorId = wxCURSOR_ARROW;
632 break;
633 }
634
635 GETWIN(id)->SetCursor(wxCursor(cursorId));
636 }
637
638
639 void Window::SetTitle(const char *s) {
640 GETWIN(id)->SetTitle(stc2wx(s));
641 }
642
643
644 //----------------------------------------------------------------------
645 // Helper classes for ListBox
646
647
648 // #undef wxSTC_USE_POPUP
649 // #define wxSTC_USE_POPUP 0
650
651
652 // A wxListBox that gives focus back to its parent if it gets it.
653 class wxSTCListBox : public wxListBox {
654 public:
655 wxSTCListBox(wxWindow* parent, wxWindowID id)
656 : wxListBox(parent, id, wxDefaultPosition, wxDefaultSize,
657 0, NULL, wxLB_SINGLE | wxSIMPLE_BORDER)
658 {}
659
660 void OnKeyDown(wxKeyEvent& event) {
661 // Give the key events to the STC. It will then update
662 // the listbox as needed.
663 GetGrandParent()->GetEventHandler()->ProcessEvent(event);
664 }
665
666 private:
667 DECLARE_EVENT_TABLE()
668 };
669
670 BEGIN_EVENT_TABLE(wxSTCListBox, wxListBox)
671 EVT_KEY_DOWN(wxSTCListBox::OnKeyDown)
672 EVT_CHAR(wxSTCListBox::OnKeyDown)
673 END_EVENT_TABLE()
674
675
676
677 // A window to place the listbox upon. If wxPopupWindow is supported then
678 // that will be used so the listbox can extend beyond the client area of the
679 // wxSTC if needed.
680 #if wxUSE_POPUPWIN && wxSTC_USE_POPUP
681 #include <wx/popupwin.h>
682 #define wxSTCListBoxWinBase wxPopupWindow
683 #define param2 wxBORDER_NONE // popup's 2nd param is flags
684 #else
685 #define wxSTCListBoxWinBase wxWindow
686 #define param2 -1 // wxWindow's 2nd param is ID
687 #endif
688
689 class wxSTCListBoxWin : public wxSTCListBoxWinBase {
690 public:
691 wxSTCListBoxWin(wxWindow* parent, wxWindowID id)
692 : wxSTCListBoxWinBase(parent, param2) {
693 lb = new wxSTCListBox(this, id);
694 lb->SetCursor(wxCursor(wxCURSOR_ARROW));
695 lb->SetFocus();
696 }
697
698 void OnSize(wxSizeEvent& event) {
699 lb->SetSize(GetSize());
700 }
701
702 wxListBox* GetLB() { return lb; }
703
704 #if wxUSE_POPUPWIN && wxSTC_USE_POPUP
705 virtual void DoSetSize(int x, int y,
706 int width, int height,
707 int sizeFlags = wxSIZE_AUTO) {
708 if (x != -1)
709 GetParent()->ClientToScreen(&x, NULL);
710 if (y != -1)
711 GetParent()->ClientToScreen(NULL, &y);
712 wxSTCListBoxWinBase::DoSetSize(x, y, width, height, sizeFlags);
713 }
714 #endif
715
716 private:
717 wxSTCListBox* lb;
718 DECLARE_EVENT_TABLE()
719 };
720
721 BEGIN_EVENT_TABLE(wxSTCListBoxWin, wxSTCListBoxWinBase)
722 EVT_SIZE(wxSTCListBoxWin::OnSize)
723 END_EVENT_TABLE()
724
725
726 inline wxListBox* GETLB(WindowID win) {
727 return (((wxSTCListBoxWin*)win)->GetLB());
728 }
729
730 //----------------------------------------------------------------------
731
732 ListBox::ListBox() {
733 }
734
735 ListBox::~ListBox() {
736 }
737
738 void ListBox::Create(Window &parent, int ctrlID) {
739 id = new wxSTCListBoxWin(GETWIN(parent.GetID()), ctrlID);
740 }
741
742 void ListBox::SetVisibleRows(int rows) {
743 desiredVisibleRows = rows;
744 }
745
746 PRectangle ListBox::GetDesiredRect() {
747 wxSize sz = GETLB(id)->GetBestSize();
748 PRectangle rc;
749 rc.top = 0;
750 rc.left = 0;
751 if (sz.x > 400)
752 sz.x = 400;
753 if (sz.y > 140) // TODO: Use desiredVisibleRows??
754 sz.y = 140;
755 rc.right = sz.x;
756 rc.bottom = sz.y;
757 return rc;
758 }
759
760 void ListBox::SetAverageCharWidth(int width) {
761 aveCharWidth = width;
762 }
763
764 void ListBox::SetFont(Font &font) {
765 GETLB(id)->SetFont(*((wxFont*)font.GetID()));
766 }
767
768 void ListBox::Clear() {
769 GETLB(id)->Clear();
770 }
771
772 void ListBox::Append(char *s) {
773 GETLB(id)->Append(s);
774 }
775
776 int ListBox::Length() {
777 return GETLB(id)->GetCount();
778 }
779
780 void ListBox::Select(int n) {
781 GETLB(id)->SetSelection(n);
782 #ifdef __WXGTK__
783 if (n > 4)
784 n = n - 4;
785 else
786 n = 1;
787 GETLB(id)->SetFirstItem(n);
788 #endif
789 }
790
791 int ListBox::GetSelection() {
792 return GETLB(id)->GetSelection();
793 }
794
795 int ListBox::Find(const char *prefix) {
796 // No longer used
797 return -1;
798 }
799
800 void ListBox::GetValue(int n, char *value, int len) {
801 wxString text = GETLB(id)->GetString(n);
802 strncpy(value, wx2stc(text), len);
803 value[len-1] = '\0';
804 }
805
806 void ListBox::Sort() {
807 }
808
809 //----------------------------------------------------------------------
810
811 Menu::Menu() : id(0) {
812 }
813
814 void Menu::CreatePopUp() {
815 Destroy();
816 id = new wxMenu();
817 }
818
819 void Menu::Destroy() {
820 if (id)
821 delete (wxMenu*)id;
822 id = 0;
823 }
824
825 void Menu::Show(Point pt, Window &w) {
826 GETWIN(w.GetID())->PopupMenu((wxMenu*)id, pt.x - 4, pt.y);
827 Destroy();
828 }
829
830 //----------------------------------------------------------------------
831
832 ColourDesired Platform::Chrome() {
833 wxColour c;
834 c = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
835 return ColourDesired(c.Red(), c.Green(), c.Blue());
836 }
837
838 ColourDesired Platform::ChromeHighlight() {
839 wxColour c;
840 c = wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT);
841 return ColourDesired(c.Red(), c.Green(), c.Blue());
842 }
843
844 const char *Platform::DefaultFont() {
845 static char buf[128];
846 strcpy(buf, wxNORMAL_FONT->GetFaceName().mbc_str());
847 return buf;
848 }
849
850 int Platform::DefaultFontSize() {
851 return 8;
852 }
853
854 unsigned int Platform::DoubleClickTime() {
855 return 500; // **** ::GetDoubleClickTime();
856 }
857
858 void Platform::DebugDisplay(const char *s) {
859 wxLogDebug(stc2wx(s));
860 }
861
862 bool Platform::IsKeyDown(int key) {
863 return false; // I don't think we'll need this.
864 }
865
866 long Platform::SendScintilla(WindowID w,
867 unsigned int msg,
868 unsigned long wParam,
869 long lParam) {
870
871 wxStyledTextCtrl* stc = (wxStyledTextCtrl*)w;
872 return stc->SendMsg(msg, wParam, lParam);
873 }
874
875
876 // These are utility functions not really tied to a platform
877
878 int Platform::Minimum(int a, int b) {
879 if (a < b)
880 return a;
881 else
882 return b;
883 }
884
885 int Platform::Maximum(int a, int b) {
886 if (a > b)
887 return a;
888 else
889 return b;
890 }
891
892 #define TRACE
893
894 void Platform::DebugPrintf(const char *format, ...) {
895 #ifdef TRACE
896 char buffer[2000];
897 va_list pArguments;
898 va_start(pArguments, format);
899 vsprintf(buffer,format,pArguments);
900 va_end(pArguments);
901 Platform::DebugDisplay(buffer);
902 #endif
903 }
904
905
906 static bool assertionPopUps = true;
907
908 bool Platform::ShowAssertionPopUps(bool assertionPopUps_) {
909 bool ret = assertionPopUps;
910 assertionPopUps = assertionPopUps_;
911 return ret;
912 }
913
914 void Platform::Assert(const char *c, const char *file, int line) {
915 char buffer[2000];
916 sprintf(buffer, "Assertion [%s] failed at %s %d", c, file, line);
917 if (assertionPopUps) {
918 /*int idButton = */
919 wxMessageBox(stc2wx(buffer),
920 wxT("Assertion failure"),
921 wxICON_HAND | wxOK);
922 // if (idButton == IDRETRY) {
923 // ::DebugBreak();
924 // } else if (idButton == IDIGNORE) {
925 // // all OK
926 // } else {
927 // abort();
928 // }
929 } else {
930 strcat(buffer, "\r\n");
931 Platform::DebugDisplay(buffer);
932 abort();
933 }
934 }
935
936
937 int Platform::Clamp(int val, int minVal, int maxVal) {
938 if (val > maxVal)
939 val = maxVal;
940 if (val < minVal)
941 val = minVal;
942 return val;
943 }
944
945
946 bool Platform::IsDBCSLeadByte(int codePage, char ch) {
947 return false;
948 }
949
950
951
952 //----------------------------------------------------------------------
953
954 ElapsedTime::ElapsedTime() {
955 wxStartTimer();
956 }
957
958 double ElapsedTime::Duration(bool reset) {
959 double result = wxGetElapsedTime(reset);
960 result /= 1000.0;
961 return result;
962 }
963
964
965 //----------------------------------------------------------------------
966
967
968
969