]> git.saurik.com Git - wxWidgets.git/blob - src/stc/PlatWX.cpp
Added some inline helpers so the dependence on wxUSE_UNICODE and
[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 Release();
288 hdc = new wxMemoryDC();
289 hdcOwned = true;
290 }
291
292 void SurfaceImpl::Init(SurfaceID hdc_) {
293 Release();
294 hdc = (wxDC*)hdc_;
295 }
296
297 void SurfaceImpl::InitPixMap(int width, int height, Surface *surface_) {
298 Release();
299 hdc = new wxMemoryDC();
300 hdcOwned = true;
301 if (width < 1) width = 1;
302 if (height < 1) height = 1;
303 bitmap = new wxBitmap(width, height);
304 ((wxMemoryDC*)hdc)->SelectObject(*bitmap);
305 }
306
307 void SurfaceImpl::PenColour(ColourAllocated fore) {
308 hdc->SetPen(wxPen(wxColourFromCA(fore), 1, wxSOLID));
309 }
310
311 void SurfaceImpl::BrushColour(ColourAllocated back) {
312 hdc->SetBrush(wxBrush(wxColourFromCA(back), wxSOLID));
313 }
314
315 void SurfaceImpl::SetFont(Font &font_) {
316 if (font_.GetID()) {
317 hdc->SetFont(*((wxFont*)font_.GetID()));
318 }
319 }
320
321 int SurfaceImpl::LogPixelsY() {
322 return hdc->GetPPI().y;
323 }
324
325 int SurfaceImpl::DeviceHeightFont(int points) {
326 return points;
327 }
328
329 void SurfaceImpl::MoveTo(int x_, int y_) {
330 x = x_;
331 y = y_;
332 }
333
334 void SurfaceImpl::LineTo(int x_, int y_) {
335 hdc->DrawLine(x,y, x_,y_);
336 x = x_;
337 y = y_;
338 }
339
340 void SurfaceImpl::Polygon(Point *pts, int npts, ColourAllocated fore, ColourAllocated back) {
341 PenColour(fore);
342 BrushColour(back);
343 hdc->DrawPolygon(npts, (wxPoint*)pts);
344 }
345
346 void SurfaceImpl::RectangleDraw(PRectangle rc, ColourAllocated fore, ColourAllocated back) {
347 PenColour(fore);
348 BrushColour(back);
349 hdc->DrawRectangle(wxRectFromPRectangle(rc));
350 }
351
352 void SurfaceImpl::FillRectangle(PRectangle rc, ColourAllocated back) {
353 BrushColour(back);
354 hdc->SetPen(*wxTRANSPARENT_PEN);
355 hdc->DrawRectangle(wxRectFromPRectangle(rc));
356 }
357
358 void SurfaceImpl::FillRectangle(PRectangle rc, Surface &surfacePattern) {
359 wxBrush br;
360 if (((SurfaceImpl&)surfacePattern).bitmap)
361 br = wxBrush(*((SurfaceImpl&)surfacePattern).bitmap);
362 else // Something is wrong so display in red
363 br = wxBrush(*wxRED, wxSOLID);
364 hdc->SetPen(*wxTRANSPARENT_PEN);
365 hdc->SetBrush(br);
366 hdc->DrawRectangle(wxRectFromPRectangle(rc));
367 }
368
369 void SurfaceImpl::RoundedRectangle(PRectangle rc, ColourAllocated fore, ColourAllocated back) {
370 PenColour(fore);
371 BrushColour(back);
372 hdc->DrawRoundedRectangle(wxRectFromPRectangle(rc), 4);
373 }
374
375 void SurfaceImpl::Ellipse(PRectangle rc, ColourAllocated fore, ColourAllocated back) {
376 PenColour(fore);
377 BrushColour(back);
378 hdc->DrawEllipse(wxRectFromPRectangle(rc));
379 }
380
381 void SurfaceImpl::Copy(PRectangle rc, Point from, Surface &surfaceSource) {
382 wxRect r = wxRectFromPRectangle(rc);
383 hdc->Blit(r.x, r.y, r.width, r.height,
384 ((SurfaceImpl&)surfaceSource).hdc,
385 from.x, from.y, wxCOPY);
386 }
387
388 void SurfaceImpl::DrawTextNoClip(PRectangle rc, Font &font, int ybase,
389 const char *s, int len,
390 ColourAllocated fore, ColourAllocated back) {
391 SetFont(font);
392 hdc->SetTextForeground(wxColourFromCA(fore));
393 hdc->SetTextBackground(wxColourFromCA(back));
394 FillRectangle(rc, back);
395
396 // ybase is where the baseline should be, but wxWin uses the upper left
397 // corner, so I need to calculate the real position for the text...
398 hdc->DrawText(stc2wx(s, len), rc.left, ybase - font.ascent);
399 }
400
401 void SurfaceImpl::DrawTextClipped(PRectangle rc, Font &font, int ybase,
402 const char *s, int len,
403 ColourAllocated fore, ColourAllocated back) {
404 SetFont(font);
405 hdc->SetTextForeground(wxColourFromCA(fore));
406 hdc->SetTextBackground(wxColourFromCA(back));
407 FillRectangle(rc, back);
408 hdc->SetClippingRegion(wxRectFromPRectangle(rc));
409
410 // see comments above
411 hdc->DrawText(stc2wx(s, len), rc.left, ybase - font.ascent);
412 hdc->DestroyClippingRegion();
413 }
414
415 int SurfaceImpl::WidthText(Font &font, const char *s, int len) {
416 SetFont(font);
417 int w;
418 int h;
419
420 hdc->GetTextExtent(stc2wx(s, len), &w, &h);
421 return w;
422 }
423
424
425 void SurfaceImpl::MeasureWidths(Font &font, const char *s, int len, int *positions) {
426 wxString str = stc2wx(s, len);
427 SetFont(font);
428
429 // Calculate the position of each character based on the widths of
430 // the previous characters
431 int* tpos = new int[len];
432 int totalWidth = 0;
433 size_t i;
434 for (i=0; i<str.Length(); i++) {
435 int w, h;
436 hdc->GetTextExtent(str[i], &w, &h);
437 totalWidth += w;
438 tpos[i] = totalWidth;
439 }
440
441 #if wxUSE_UNICODE
442 // Map the widths for UCS-2 characters back to the UTF-8 input string
443 i = 0;
444 size_t ui = 0;
445 while (i < len) {
446 unsigned char uch = (unsigned char)s[i];
447 positions[i++] = tpos[ui];
448 if (uch >= 0x80) {
449 if (uch < (0x80 + 0x40 + 0x20)) {
450 positions[i++] = tpos[ui];
451 } else {
452 positions[i++] = tpos[ui];
453 positions[i++] = tpos[ui];
454 }
455 }
456 ui++;
457 }
458 #else
459
460 // If not unicode then just use the widths we have
461 memcpy(positions, tpos, len * sizeof(*tpos));
462 #endif
463
464 delete [] tpos;
465 }
466
467
468 int SurfaceImpl::WidthChar(Font &font, char ch) {
469 SetFont(font);
470 int w;
471 int h;
472 char s[2] = { ch, 0 };
473
474 hdc->GetTextExtent(stc2wx(s, 1), &w, &h);
475 return w;
476 }
477
478 #define EXTENT_TEST wxT(" `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
479
480 int SurfaceImpl::Ascent(Font &font) {
481 SetFont(font);
482 int w, h, d, e;
483 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
484 font.ascent = h - d;
485 return font.ascent;
486 }
487
488 int SurfaceImpl::Descent(Font &font) {
489 SetFont(font);
490 int w, h, d, e;
491 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
492 return d;
493 }
494
495 int SurfaceImpl::InternalLeading(Font &font) {
496 return 0;
497 }
498
499 int SurfaceImpl::ExternalLeading(Font &font) {
500 SetFont(font);
501 int w, h, d, e;
502 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
503 return e;
504 }
505
506 int SurfaceImpl::Height(Font &font) {
507 SetFont(font);
508 return hdc->GetCharHeight();
509 }
510
511 int SurfaceImpl::AverageCharWidth(Font &font) {
512 SetFont(font);
513 return hdc->GetCharWidth();
514 }
515
516 int SurfaceImpl::SetPalette(Palette *pal, bool inBackGround) {
517 return 0;
518 }
519
520 void SurfaceImpl::SetClip(PRectangle rc) {
521 hdc->SetClippingRegion(wxRectFromPRectangle(rc));
522 }
523
524 void SurfaceImpl::FlushCachedState() {
525 }
526
527 void SurfaceImpl::SetUnicodeMode(bool unicodeMode_) {
528 unicodeMode=unicodeMode_;
529 #if wxUSE_UNICODE
530 wxASSERT_MSG(unicodeMode == wxUSE_UNICODE,
531 wxT("Only unicode may be used when wxUSE_UNICODE is on."));
532 #else
533 wxASSERT_MSG(unicodeMode == wxUSE_UNICODE,
534 wxT("Only non-unicode may be used when wxUSE_UNICODE is off."));
535 #endif
536 }
537
538 Surface *Surface::Allocate() {
539 return new SurfaceImpl;
540 }
541
542
543 //----------------------------------------------------------------------
544
545
546 inline wxWindow* GETWIN(WindowID id) { return (wxWindow*)id; }
547
548 Window::~Window() {
549 }
550
551 void Window::Destroy() {
552 if (id)
553 GETWIN(id)->Destroy();
554 id = 0;
555 }
556
557 bool Window::HasFocus() {
558 return wxWindow::FindFocus() == GETWIN(id);
559 }
560
561 PRectangle Window::GetPosition() {
562 wxRect rc(GETWIN(id)->GetPosition(), GETWIN(id)->GetSize());
563 return PRectangleFromwxRect(rc);
564 }
565
566 void Window::SetPosition(PRectangle rc) {
567 wxRect r = wxRectFromPRectangle(rc);
568 GETWIN(id)->SetSize(r);
569 }
570
571 void Window::SetPositionRelative(PRectangle rc, Window) {
572 SetPosition(rc); // ????
573 }
574
575 PRectangle Window::GetClientPosition() {
576 wxSize sz = GETWIN(id)->GetClientSize();
577 return PRectangle(0, 0, sz.x, sz.y);
578 }
579
580 void Window::Show(bool show) {
581 GETWIN(id)->Show(show);
582 }
583
584 void Window::InvalidateAll() {
585 GETWIN(id)->Refresh(false);
586 wxWakeUpIdle();
587 }
588
589 void Window::InvalidateRectangle(PRectangle rc) {
590 wxRect r = wxRectFromPRectangle(rc);
591 GETWIN(id)->Refresh(false, &r);
592 wxWakeUpIdle();
593 }
594
595 void Window::SetFont(Font &font) {
596 GETWIN(id)->SetFont(*((wxFont*)font.GetID()));
597 }
598
599 void Window::SetCursor(Cursor curs) {
600 int cursorId;
601
602 switch (curs) {
603 case cursorText:
604 cursorId = wxCURSOR_IBEAM;
605 break;
606 case cursorArrow:
607 cursorId = wxCURSOR_ARROW;
608 break;
609 case cursorUp:
610 cursorId = wxCURSOR_ARROW; // ** no up arrow... wxCURSOR_UPARROW;
611 break;
612 case cursorWait:
613 cursorId = wxCURSOR_WAIT;
614 break;
615 case cursorHoriz:
616 cursorId = wxCURSOR_SIZEWE;
617 break;
618 case cursorVert:
619 cursorId = wxCURSOR_SIZENS;
620 break;
621 case cursorReverseArrow:
622 cursorId = wxCURSOR_RIGHT_ARROW;
623 break;
624 default:
625 cursorId = wxCURSOR_ARROW;
626 break;
627 }
628
629 GETWIN(id)->SetCursor(wxCursor(cursorId));
630 }
631
632
633 void Window::SetTitle(const char *s) {
634 GETWIN(id)->SetTitle(stc2wx(s));
635 }
636
637
638 //----------------------------------------------------------------------
639 // Helper classes for ListBox
640
641
642 // #undef wxSTC_USE_POPUP
643 // #define wxSTC_USE_POPUP 0
644
645
646 // A wxListBox that gives focus back to its parent if it gets it.
647 class wxSTCListBox : public wxListBox {
648 public:
649 wxSTCListBox(wxWindow* parent, wxWindowID id)
650 : wxListBox(parent, id, wxDefaultPosition, wxDefaultSize,
651 0, NULL, wxLB_SINGLE | wxSIMPLE_BORDER)
652 {}
653
654 void OnKeyDown(wxKeyEvent& event) {
655 // Give the key events to the STC. It will then update
656 // the listbox as needed.
657 GetGrandParent()->GetEventHandler()->ProcessEvent(event);
658 }
659
660 private:
661 DECLARE_EVENT_TABLE()
662 };
663
664 BEGIN_EVENT_TABLE(wxSTCListBox, wxListBox)
665 EVT_KEY_DOWN(wxSTCListBox::OnKeyDown)
666 EVT_CHAR(wxSTCListBox::OnKeyDown)
667 END_EVENT_TABLE()
668
669
670
671 // A window to place the listbox upon. If wxPopupWindow is supported then
672 // that will be used so the listbox can extend beyond the client area of the
673 // wxSTC if needed.
674 #if wxUSE_POPUPWIN && wxSTC_USE_POPUP
675 #include <wx/popupwin.h>
676 #define wxSTCListBoxWinBase wxPopupWindow
677 #define param2 wxBORDER_NONE // popup's 2nd param is flags
678 #else
679 #define wxSTCListBoxWinBase wxWindow
680 #define param2 -1 // wxWindow's 2nd param is ID
681 #endif
682
683 class wxSTCListBoxWin : public wxSTCListBoxWinBase {
684 public:
685 wxSTCListBoxWin(wxWindow* parent, wxWindowID id)
686 : wxSTCListBoxWinBase(parent, param2) {
687 lb = new wxSTCListBox(this, id);
688 lb->SetCursor(wxCursor(wxCURSOR_ARROW));
689 lb->SetFocus();
690 }
691
692 void OnSize(wxSizeEvent& event) {
693 lb->SetSize(GetSize());
694 }
695
696 wxListBox* GetLB() { return lb; }
697
698 #if wxUSE_POPUPWIN && wxSTC_USE_POPUP
699 virtual void DoSetSize(int x, int y,
700 int width, int height,
701 int sizeFlags = wxSIZE_AUTO) {
702 if (x != -1)
703 GetParent()->ClientToScreen(&x, NULL);
704 if (y != -1)
705 GetParent()->ClientToScreen(NULL, &y);
706 wxSTCListBoxWinBase::DoSetSize(x, y, width, height, sizeFlags);
707 }
708 #endif
709
710 private:
711 wxSTCListBox* lb;
712 DECLARE_EVENT_TABLE()
713 };
714
715 BEGIN_EVENT_TABLE(wxSTCListBoxWin, wxSTCListBoxWinBase)
716 EVT_SIZE(wxSTCListBoxWin::OnSize)
717 END_EVENT_TABLE()
718
719
720 inline wxListBox* GETLB(WindowID win) {
721 return (((wxSTCListBoxWin*)win)->GetLB());
722 }
723
724 //----------------------------------------------------------------------
725
726 ListBox::ListBox() {
727 }
728
729 ListBox::~ListBox() {
730 }
731
732 void ListBox::Create(Window &parent, int ctrlID) {
733 id = new wxSTCListBoxWin(GETWIN(parent.GetID()), ctrlID);
734 }
735
736 void ListBox::SetVisibleRows(int rows) {
737 desiredVisibleRows = rows;
738 }
739
740 PRectangle ListBox::GetDesiredRect() {
741 wxSize sz = GETLB(id)->GetBestSize();
742 PRectangle rc;
743 rc.top = 0;
744 rc.left = 0;
745 if (sz.x > 400)
746 sz.x = 400;
747 if (sz.y > 140) // TODO: Use desiredVisibleRows??
748 sz.y = 140;
749 rc.right = sz.x;
750 rc.bottom = sz.y;
751 return rc;
752 }
753
754 void ListBox::SetAverageCharWidth(int width) {
755 aveCharWidth = width;
756 }
757
758 void ListBox::SetFont(Font &font) {
759 GETLB(id)->SetFont(*((wxFont*)font.GetID()));
760 }
761
762 void ListBox::Clear() {
763 GETLB(id)->Clear();
764 }
765
766 void ListBox::Append(char *s) {
767 GETLB(id)->Append(s);
768 }
769
770 int ListBox::Length() {
771 return GETLB(id)->GetCount();
772 }
773
774 void ListBox::Select(int n) {
775 GETLB(id)->SetSelection(n);
776 #ifdef __WXGTK__
777 if (n > 4)
778 n = n - 4;
779 else
780 n = 1;
781 GETLB(id)->SetFirstItem(n);
782 #endif
783 }
784
785 int ListBox::GetSelection() {
786 return GETLB(id)->GetSelection();
787 }
788
789 int ListBox::Find(const char *prefix) {
790 // No longer used
791 return -1;
792 }
793
794 void ListBox::GetValue(int n, char *value, int len) {
795 wxString text = GETLB(id)->GetString(n);
796 strncpy(value, wx2stc(text), len);
797 value[len-1] = '\0';
798 }
799
800 void ListBox::Sort() {
801 }
802
803 //----------------------------------------------------------------------
804
805 Menu::Menu() : id(0) {
806 }
807
808 void Menu::CreatePopUp() {
809 Destroy();
810 id = new wxMenu();
811 }
812
813 void Menu::Destroy() {
814 if (id)
815 delete (wxMenu*)id;
816 id = 0;
817 }
818
819 void Menu::Show(Point pt, Window &w) {
820 GETWIN(w.GetID())->PopupMenu((wxMenu*)id, pt.x - 4, pt.y);
821 Destroy();
822 }
823
824 //----------------------------------------------------------------------
825
826 ColourDesired Platform::Chrome() {
827 wxColour c;
828 c = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
829 return ColourDesired(c.Red(), c.Green(), c.Blue());
830 }
831
832 ColourDesired Platform::ChromeHighlight() {
833 wxColour c;
834 c = wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT);
835 return ColourDesired(c.Red(), c.Green(), c.Blue());
836 }
837
838 const char *Platform::DefaultFont() {
839 static char buf[128];
840 strcpy(buf, wxNORMAL_FONT->GetFaceName().mbc_str());
841 return buf;
842 }
843
844 int Platform::DefaultFontSize() {
845 return 8;
846 }
847
848 unsigned int Platform::DoubleClickTime() {
849 return 500; // **** ::GetDoubleClickTime();
850 }
851
852 void Platform::DebugDisplay(const char *s) {
853 wxLogDebug(stc2wx(s));
854 }
855
856 bool Platform::IsKeyDown(int key) {
857 return false; // I don't think we'll need this.
858 }
859
860 long Platform::SendScintilla(WindowID w,
861 unsigned int msg,
862 unsigned long wParam,
863 long lParam) {
864
865 wxStyledTextCtrl* stc = (wxStyledTextCtrl*)w;
866 return stc->SendMsg(msg, wParam, lParam);
867 }
868
869
870 // These are utility functions not really tied to a platform
871
872 int Platform::Minimum(int a, int b) {
873 if (a < b)
874 return a;
875 else
876 return b;
877 }
878
879 int Platform::Maximum(int a, int b) {
880 if (a > b)
881 return a;
882 else
883 return b;
884 }
885
886 #define TRACE
887
888 void Platform::DebugPrintf(const char *format, ...) {
889 #ifdef TRACE
890 char buffer[2000];
891 va_list pArguments;
892 va_start(pArguments, format);
893 vsprintf(buffer,format,pArguments);
894 va_end(pArguments);
895 Platform::DebugDisplay(buffer);
896 #endif
897 }
898
899
900 static bool assertionPopUps = true;
901
902 bool Platform::ShowAssertionPopUps(bool assertionPopUps_) {
903 bool ret = assertionPopUps;
904 assertionPopUps = assertionPopUps_;
905 return ret;
906 }
907
908 void Platform::Assert(const char *c, const char *file, int line) {
909 char buffer[2000];
910 sprintf(buffer, "Assertion [%s] failed at %s %d", c, file, line);
911 if (assertionPopUps) {
912 /*int idButton = */
913 wxMessageBox(stc2wx(buffer),
914 wxT("Assertion failure"),
915 wxICON_HAND | wxOK);
916 // if (idButton == IDRETRY) {
917 // ::DebugBreak();
918 // } else if (idButton == IDIGNORE) {
919 // // all OK
920 // } else {
921 // abort();
922 // }
923 } else {
924 strcat(buffer, "\r\n");
925 Platform::DebugDisplay(buffer);
926 abort();
927 }
928 }
929
930
931 int Platform::Clamp(int val, int minVal, int maxVal) {
932 if (val > maxVal)
933 val = maxVal;
934 if (val < minVal)
935 val = minVal;
936 return val;
937 }
938
939
940 bool Platform::IsDBCSLeadByte(int codePage, char ch) {
941 return false;
942 }
943
944
945
946 //----------------------------------------------------------------------
947
948 ElapsedTime::ElapsedTime() {
949 wxStartTimer();
950 }
951
952 double ElapsedTime::Duration(bool reset) {
953 double result = wxGetElapsedTime(reset);
954 result /= 1000.0;
955 return result;
956 }
957
958
959 //----------------------------------------------------------------------
960
961
962
963