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