Patch #809286 and a couple other minor changes to the stc sample
[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 #include <wx/listctrl.h>
12 #include <wx/mstream.h>
13 #include <wx/image.h>
14 #include <wx/imaglist.h>
15
16 #include "Platform.h"
17 #include "PlatWX.h"
18 #include "wx/stc/stc.h"
19
20
21 #ifdef __WXGTK__
22 #include <gtk/gtk.h>
23 #endif
24
25
26 Point Point::FromLong(long lpoint) {
27 return Point(lpoint & 0xFFFF, lpoint >> 16);
28 }
29
30 wxRect wxRectFromPRectangle(PRectangle prc) {
31 wxRect rc(prc.left, prc.top,
32 prc.right-prc.left, prc.bottom-prc.top);
33 return rc;
34 }
35
36 PRectangle PRectangleFromwxRect(wxRect rc) {
37 return PRectangle(rc.GetLeft(), rc.GetTop(),
38 rc.GetRight()+1, rc.GetBottom()+1);
39 }
40
41 wxColour wxColourFromCA(const ColourAllocated& ca) {
42 ColourDesired cd(ca.AsLong());
43 return wxColour(cd.GetRed(), cd.GetGreen(), cd.GetBlue());
44 }
45
46 //----------------------------------------------------------------------
47
48 Palette::Palette() {
49 used = 0;
50 allowRealization = false;
51 }
52
53 Palette::~Palette() {
54 Release();
55 }
56
57 void Palette::Release() {
58 used = 0;
59 }
60
61 // This method either adds a colour to the list of wanted colours (want==true)
62 // or retrieves the allocated colour back to the ColourPair.
63 // This is one method to make it easier to keep the code for wanting and retrieving in sync.
64 void Palette::WantFind(ColourPair &cp, bool want) {
65 if (want) {
66 for (int i=0; i < used; i++) {
67 if (entries[i].desired == cp.desired)
68 return;
69 }
70
71 if (used < numEntries) {
72 entries[used].desired = cp.desired;
73 entries[used].allocated.Set(cp.desired.AsLong());
74 used++;
75 }
76 } else {
77 for (int i=0; i < used; i++) {
78 if (entries[i].desired == cp.desired) {
79 cp.allocated = entries[i].allocated;
80 return;
81 }
82 }
83 cp.allocated.Set(cp.desired.AsLong());
84 }
85 }
86
87 void Palette::Allocate(Window &) {
88 if (allowRealization) {
89 }
90 }
91
92
93 //----------------------------------------------------------------------
94
95 Font::Font() {
96 id = 0;
97 ascent = 0;
98 }
99
100 Font::~Font() {
101 }
102
103 void Font::Create(const char *faceName, int characterSet, int size, bool bold, bool italic) {
104 wxFontEncoding encoding;
105
106 Release();
107
108 switch (characterSet) {
109 default:
110 case wxSTC_CHARSET_ANSI:
111 case wxSTC_CHARSET_DEFAULT:
112 encoding = wxFONTENCODING_DEFAULT;
113 break;
114
115 case wxSTC_CHARSET_BALTIC:
116 encoding = wxFONTENCODING_ISO8859_13;
117 break;
118
119 case wxSTC_CHARSET_CHINESEBIG5:
120 encoding = wxFONTENCODING_CP950;
121 break;
122
123 case wxSTC_CHARSET_EASTEUROPE:
124 encoding = wxFONTENCODING_ISO8859_2;
125 break;
126
127 case wxSTC_CHARSET_GB2312:
128 encoding = wxFONTENCODING_CP936;
129 break;
130
131 case wxSTC_CHARSET_GREEK:
132 encoding = wxFONTENCODING_ISO8859_7;
133 break;
134
135 case wxSTC_CHARSET_HANGUL:
136 encoding = wxFONTENCODING_CP949;
137 break;
138
139 case wxSTC_CHARSET_MAC:
140 encoding = wxFONTENCODING_DEFAULT;
141 break;
142
143 case wxSTC_CHARSET_OEM:
144 encoding = wxFONTENCODING_DEFAULT;
145 break;
146
147 case wxSTC_CHARSET_RUSSIAN:
148 encoding = wxFONTENCODING_KOI8;
149 break;
150
151 case wxSTC_CHARSET_SHIFTJIS:
152 encoding = wxFONTENCODING_CP932;
153 break;
154
155 case wxSTC_CHARSET_SYMBOL:
156 encoding = wxFONTENCODING_DEFAULT;
157 break;
158
159 case wxSTC_CHARSET_TURKISH:
160 encoding = wxFONTENCODING_ISO8859_9;
161 break;
162
163 case wxSTC_CHARSET_JOHAB:
164 encoding = wxFONTENCODING_DEFAULT;
165 break;
166
167 case wxSTC_CHARSET_HEBREW:
168 encoding = wxFONTENCODING_ISO8859_8;
169 break;
170
171 case wxSTC_CHARSET_ARABIC:
172 encoding = wxFONTENCODING_ISO8859_6;
173 break;
174
175 case wxSTC_CHARSET_VIETNAMESE:
176 encoding = wxFONTENCODING_DEFAULT;
177 break;
178
179 case wxSTC_CHARSET_THAI:
180 encoding = wxFONTENCODING_ISO8859_11;
181 break;
182 }
183
184 wxFontEncodingArray ea = wxEncodingConverter::GetPlatformEquivalents(encoding);
185 if (ea.GetCount())
186 encoding = ea[0];
187
188 id = new wxFont(size,
189 wxDEFAULT,
190 italic ? wxITALIC : wxNORMAL,
191 bold ? wxBOLD : wxNORMAL,
192 false,
193 stc2wx(faceName),
194 encoding);
195 }
196
197
198 void Font::Release() {
199 if (id)
200 delete (wxFont*)id;
201 id = 0;
202 }
203
204 //----------------------------------------------------------------------
205
206 class SurfaceImpl : public Surface {
207 private:
208 wxDC* hdc;
209 bool hdcOwned;
210 wxBitmap* bitmap;
211 int x;
212 int y;
213 bool unicodeMode;
214
215 public:
216 SurfaceImpl();
217 ~SurfaceImpl();
218
219 virtual void Init(WindowID wid);
220 virtual void Init(SurfaceID sid, WindowID wid);
221 virtual void InitPixMap(int width, int height, Surface *surface_, WindowID wid);
222
223 virtual void Release();
224 virtual bool Initialised();
225 virtual void PenColour(ColourAllocated fore);
226 virtual int LogPixelsY();
227 virtual int DeviceHeightFont(int points);
228 virtual void MoveTo(int x_, int y_);
229 virtual void LineTo(int x_, int y_);
230 virtual void Polygon(Point *pts, int npts, ColourAllocated fore, ColourAllocated back);
231 virtual void RectangleDraw(PRectangle rc, ColourAllocated fore, ColourAllocated back);
232 virtual void FillRectangle(PRectangle rc, ColourAllocated back);
233 virtual void FillRectangle(PRectangle rc, Surface &surfacePattern);
234 virtual void RoundedRectangle(PRectangle rc, ColourAllocated fore, ColourAllocated back);
235 virtual void Ellipse(PRectangle rc, ColourAllocated fore, ColourAllocated back);
236 virtual void Copy(PRectangle rc, Point from, Surface &surfaceSource);
237
238 virtual void DrawTextNoClip(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore, ColourAllocated back);
239 virtual void DrawTextClipped(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore, ColourAllocated back);
240 virtual void DrawTextTransparent(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore);
241 virtual void MeasureWidths(Font &font_, const char *s, int len, int *positions);
242 virtual int WidthText(Font &font_, const char *s, int len);
243 virtual int WidthChar(Font &font_, char ch);
244 virtual int Ascent(Font &font_);
245 virtual int Descent(Font &font_);
246 virtual int InternalLeading(Font &font_);
247 virtual int ExternalLeading(Font &font_);
248 virtual int Height(Font &font_);
249 virtual int AverageCharWidth(Font &font_);
250
251 virtual int SetPalette(Palette *pal, bool inBackGround);
252 virtual void SetClip(PRectangle rc);
253 virtual void FlushCachedState();
254
255 virtual void SetUnicodeMode(bool unicodeMode_);
256 virtual void SetDBCSMode(int codePage);
257
258 void BrushColour(ColourAllocated back);
259 void SetFont(Font &font_);
260 };
261
262
263
264 SurfaceImpl::SurfaceImpl() :
265 hdc(0), hdcOwned(0), bitmap(0),
266 x(0), y(0), unicodeMode(0)
267 {}
268
269 SurfaceImpl::~SurfaceImpl() {
270 Release();
271 }
272
273 void SurfaceImpl::Init(WindowID wid) {
274 #if 0
275 Release();
276 hdc = new wxMemoryDC();
277 hdcOwned = true;
278 #else
279 // On Mac and GTK the DC is not really valid until it has a bitmap
280 // selected into it. So instead of just creating the DC with no bitmap,
281 // go ahead and give it one.
282 InitPixMap(1,1,NULL,wid);
283 #endif
284 }
285
286 void SurfaceImpl::Init(SurfaceID hdc_, WindowID) {
287 Release();
288 hdc = (wxDC*)hdc_;
289 }
290
291 void SurfaceImpl::InitPixMap(int width, int height, Surface *WXUNUSED(surface_), WindowID) {
292 Release();
293 hdc = new wxMemoryDC();
294 hdcOwned = true;
295 if (width < 1) width = 1;
296 if (height < 1) height = 1;
297 bitmap = new wxBitmap(width, height);
298 ((wxMemoryDC*)hdc)->SelectObject(*bitmap);
299 }
300
301
302 void SurfaceImpl::Release() {
303 if (bitmap) {
304 ((wxMemoryDC*)hdc)->SelectObject(wxNullBitmap);
305 delete bitmap;
306 bitmap = 0;
307 }
308 if (hdcOwned) {
309 delete hdc;
310 hdc = 0;
311 hdcOwned = false;
312 }
313 }
314
315
316 bool SurfaceImpl::Initialised() {
317 return hdc != 0;
318 }
319
320
321 void SurfaceImpl::PenColour(ColourAllocated fore) {
322 hdc->SetPen(wxPen(wxColourFromCA(fore), 1, wxSOLID));
323 }
324
325 void SurfaceImpl::BrushColour(ColourAllocated back) {
326 hdc->SetBrush(wxBrush(wxColourFromCA(back), wxSOLID));
327 }
328
329 void SurfaceImpl::SetFont(Font &font_) {
330 if (font_.GetID()) {
331 hdc->SetFont(*((wxFont*)font_.GetID()));
332 }
333 }
334
335 int SurfaceImpl::LogPixelsY() {
336 return hdc->GetPPI().y;
337 }
338
339 int SurfaceImpl::DeviceHeightFont(int points) {
340 return points;
341 }
342
343 void SurfaceImpl::MoveTo(int x_, int y_) {
344 x = x_;
345 y = y_;
346 }
347
348 void SurfaceImpl::LineTo(int x_, int y_) {
349 hdc->DrawLine(x,y, x_,y_);
350 x = x_;
351 y = y_;
352 }
353
354 void SurfaceImpl::Polygon(Point *pts, int npts, ColourAllocated fore, ColourAllocated back) {
355 PenColour(fore);
356 BrushColour(back);
357 hdc->DrawPolygon(npts, (wxPoint*)pts);
358 }
359
360 void SurfaceImpl::RectangleDraw(PRectangle rc, ColourAllocated fore, ColourAllocated back) {
361 PenColour(fore);
362 BrushColour(back);
363 hdc->DrawRectangle(wxRectFromPRectangle(rc));
364 }
365
366 void SurfaceImpl::FillRectangle(PRectangle rc, ColourAllocated back) {
367 BrushColour(back);
368 hdc->SetPen(*wxTRANSPARENT_PEN);
369 hdc->DrawRectangle(wxRectFromPRectangle(rc));
370 }
371
372 void SurfaceImpl::FillRectangle(PRectangle rc, Surface &surfacePattern) {
373 wxBrush br;
374 if (((SurfaceImpl&)surfacePattern).bitmap)
375 br = wxBrush(*((SurfaceImpl&)surfacePattern).bitmap);
376 else // Something is wrong so display in red
377 br = wxBrush(*wxRED, wxSOLID);
378 hdc->SetPen(*wxTRANSPARENT_PEN);
379 hdc->SetBrush(br);
380 hdc->DrawRectangle(wxRectFromPRectangle(rc));
381 }
382
383 void SurfaceImpl::RoundedRectangle(PRectangle rc, ColourAllocated fore, ColourAllocated back) {
384 PenColour(fore);
385 BrushColour(back);
386 hdc->DrawRoundedRectangle(wxRectFromPRectangle(rc), 4);
387 }
388
389 void SurfaceImpl::Ellipse(PRectangle rc, ColourAllocated fore, ColourAllocated back) {
390 PenColour(fore);
391 BrushColour(back);
392 hdc->DrawEllipse(wxRectFromPRectangle(rc));
393 }
394
395 void SurfaceImpl::Copy(PRectangle rc, Point from, Surface &surfaceSource) {
396 wxRect r = wxRectFromPRectangle(rc);
397 hdc->Blit(r.x, r.y, r.width, r.height,
398 ((SurfaceImpl&)surfaceSource).hdc,
399 from.x, from.y, wxCOPY);
400 }
401
402 void SurfaceImpl::DrawTextNoClip(PRectangle rc, Font &font, int ybase,
403 const char *s, int len,
404 ColourAllocated fore, ColourAllocated back) {
405 SetFont(font);
406 hdc->SetTextForeground(wxColourFromCA(fore));
407 hdc->SetTextBackground(wxColourFromCA(back));
408 FillRectangle(rc, back);
409
410 // ybase is where the baseline should be, but wxWin uses the upper left
411 // corner, so I need to calculate the real position for the text...
412 hdc->DrawText(stc2wx(s, len), rc.left, ybase - font.ascent);
413 }
414
415 void SurfaceImpl::DrawTextClipped(PRectangle rc, Font &font, int ybase,
416 const char *s, int len,
417 ColourAllocated fore, ColourAllocated back) {
418 SetFont(font);
419 hdc->SetTextForeground(wxColourFromCA(fore));
420 hdc->SetTextBackground(wxColourFromCA(back));
421 FillRectangle(rc, back);
422 hdc->SetClippingRegion(wxRectFromPRectangle(rc));
423
424 // see comments above
425 hdc->DrawText(stc2wx(s, len), rc.left, ybase - font.ascent);
426 hdc->DestroyClippingRegion();
427 }
428
429
430 void SurfaceImpl::DrawTextTransparent(PRectangle rc, Font &font, int ybase,
431 const char *s, int len,
432 ColourAllocated fore) {
433
434 SetFont(font);
435 hdc->SetTextForeground(wxColourFromCA(fore));
436 hdc->SetBackgroundMode(wxTRANSPARENT);
437
438 // ybase is where the baseline should be, but wxWin uses the upper left
439 // corner, so I need to calculate the real position for the text...
440 hdc->DrawText(stc2wx(s, len), rc.left, ybase - font.ascent);
441
442 hdc->SetBackgroundMode(wxSOLID);
443 }
444
445
446 void SurfaceImpl::MeasureWidths(Font &font, const char *s, int len, int *positions) {
447
448 wxString str = stc2wx(s, len);
449 SetFont(font);
450
451 #ifndef __WXMAC__
452 // Calculate the position of each character based on the widths of
453 // the previous characters
454 int* tpos = new int[len+1];
455 int totalWidth = 0;
456 size_t i;
457 for (i=0; i<str.Length(); i++) {
458 int w, h;
459 hdc->GetTextExtent(str[i], &w, &h);
460 totalWidth += w;
461 tpos[i] = totalWidth;
462 }
463 #else
464 // Instead of a running total, remeasure from the begining of the
465 // text for each character's position. This is because with AA fonts
466 // on OS X widths can be fractions of pixels wide when more than one
467 // are drawn together, so the sum of all character widths is not necessarily
468 // (and probably not) the same as the whole string width.
469 int* tpos = new int[len+1];
470 size_t i;
471 for (i=0; i<str.Length(); i++) {
472 int w, h;
473 hdc->GetTextExtent(str.Left(i+1), &w, &h);
474 tpos[i] = w;
475 }
476 #endif
477
478
479 #if wxUSE_UNICODE
480 // Map the widths for UCS-2 characters back to the UTF-8 input string
481 // NOTE: I don't think this is right for when sizeof(wxChar) > 2, ie wxGTK2
482 // so figure it out and fix it!
483 i = 0;
484 size_t ui = 0;
485 while ((int)i < len) {
486 unsigned char uch = (unsigned char)s[i];
487 positions[i++] = tpos[ui];
488 if (uch >= 0x80) {
489 if (uch < (0x80 + 0x40 + 0x20)) {
490 positions[i++] = tpos[ui];
491 } else {
492 positions[i++] = tpos[ui];
493 positions[i++] = tpos[ui];
494 }
495 }
496 ui++;
497 }
498 #else
499
500 // If not unicode then just use the widths we have
501 memcpy(positions, tpos, len * sizeof(*tpos));
502 #endif
503
504 delete [] tpos;
505 }
506
507
508 int SurfaceImpl::WidthText(Font &font, const char *s, int len) {
509 SetFont(font);
510 int w;
511 int h;
512
513 hdc->GetTextExtent(stc2wx(s, len), &w, &h);
514 return w;
515 }
516
517
518 int SurfaceImpl::WidthChar(Font &font, char ch) {
519 SetFont(font);
520 int w;
521 int h;
522 char s[2] = { ch, 0 };
523
524 hdc->GetTextExtent(stc2wx(s, 1), &w, &h);
525 return w;
526 }
527
528 #define EXTENT_TEST wxT(" `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
529
530 int SurfaceImpl::Ascent(Font &font) {
531 SetFont(font);
532 int w, h, d, e;
533 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
534 font.ascent = h - d;
535 return font.ascent;
536 }
537
538 int SurfaceImpl::Descent(Font &font) {
539 SetFont(font);
540 int w, h, d, e;
541 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
542 return d;
543 }
544
545 int SurfaceImpl::InternalLeading(Font &WXUNUSED(font)) {
546 return 0;
547 }
548
549 int SurfaceImpl::ExternalLeading(Font &font) {
550 SetFont(font);
551 int w, h, d, e;
552 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
553 return e;
554 }
555
556 int SurfaceImpl::Height(Font &font) {
557 SetFont(font);
558 return hdc->GetCharHeight();
559 }
560
561 int SurfaceImpl::AverageCharWidth(Font &font) {
562 SetFont(font);
563 return hdc->GetCharWidth();
564 }
565
566 int SurfaceImpl::SetPalette(Palette *WXUNUSED(pal), bool WXUNUSED(inBackGround)) {
567 return 0;
568 }
569
570 void SurfaceImpl::SetClip(PRectangle rc) {
571 hdc->SetClippingRegion(wxRectFromPRectangle(rc));
572 }
573
574 void SurfaceImpl::FlushCachedState() {
575 }
576
577 void SurfaceImpl::SetUnicodeMode(bool unicodeMode_) {
578 unicodeMode=unicodeMode_;
579 }
580
581 void SurfaceImpl::SetDBCSMode(int WXUNUSED(codePage)) {
582 // dbcsMode = codePage == SC_CP_DBCS;
583 }
584
585
586 Surface *Surface::Allocate() {
587 return new SurfaceImpl;
588 }
589
590
591 //----------------------------------------------------------------------
592
593
594 inline wxWindow* GETWIN(WindowID id) { return (wxWindow*)id; }
595
596 Window::~Window() {
597 }
598
599 void Window::Destroy() {
600 if (id) {
601 Show(FALSE);
602 GETWIN(id)->Destroy();
603 }
604 id = 0;
605 }
606
607 bool Window::HasFocus() {
608 return wxWindow::FindFocus() == GETWIN(id);
609 }
610
611 PRectangle Window::GetPosition() {
612 if (! id) return PRectangle();
613 wxRect rc(GETWIN(id)->GetPosition(), GETWIN(id)->GetSize());
614 return PRectangleFromwxRect(rc);
615 }
616
617 void Window::SetPosition(PRectangle rc) {
618 wxRect r = wxRectFromPRectangle(rc);
619 GETWIN(id)->SetSize(r);
620 }
621
622 void Window::SetPositionRelative(PRectangle rc, Window) {
623 SetPosition(rc); // ????
624 }
625
626 PRectangle Window::GetClientPosition() {
627 if (! id) return PRectangle();
628 wxSize sz = GETWIN(id)->GetClientSize();
629 return PRectangle(0, 0, sz.x, sz.y);
630 }
631
632 void Window::Show(bool show) {
633 GETWIN(id)->Show(show);
634 }
635
636 void Window::InvalidateAll() {
637 GETWIN(id)->Refresh(false);
638 wxWakeUpIdle();
639 }
640
641 void Window::InvalidateRectangle(PRectangle rc) {
642 wxRect r = wxRectFromPRectangle(rc);
643 GETWIN(id)->Refresh(false, &r);
644 wxWakeUpIdle();
645 }
646
647 void Window::SetFont(Font &font) {
648 GETWIN(id)->SetFont(*((wxFont*)font.GetID()));
649 }
650
651 void Window::SetCursor(Cursor curs) {
652 int cursorId;
653
654 switch (curs) {
655 case cursorText:
656 cursorId = wxCURSOR_IBEAM;
657 break;
658 case cursorArrow:
659 cursorId = wxCURSOR_ARROW;
660 break;
661 case cursorUp:
662 cursorId = wxCURSOR_ARROW; // ** no up arrow... wxCURSOR_UPARROW;
663 break;
664 case cursorWait:
665 cursorId = wxCURSOR_WAIT;
666 break;
667 case cursorHoriz:
668 cursorId = wxCURSOR_SIZEWE;
669 break;
670 case cursorVert:
671 cursorId = wxCURSOR_SIZENS;
672 break;
673 case cursorReverseArrow:
674 cursorId = wxCURSOR_RIGHT_ARROW;
675 break;
676 case cursorHand:
677 cursorId = wxCURSOR_HAND;
678 break;
679 default:
680 cursorId = wxCURSOR_ARROW;
681 break;
682 }
683 #ifdef __WXMOTIF__
684 wxCursor wc = wxStockCursor(cursorId) ;
685 #else
686 wxCursor wc = wxCursor(cursorId) ;
687 #endif
688 GETWIN(id)->SetCursor(wc);
689 }
690
691
692 void Window::SetTitle(const char *s) {
693 GETWIN(id)->SetTitle(stc2wx(s));
694 }
695
696
697 //----------------------------------------------------------------------
698 // Helper classes for ListBox
699
700
701 // This is a simple subclass of wxLIstView that just resets focus to the
702 // parent when it gets it.
703 class wxSTCListBox : public wxListView {
704 public:
705 wxSTCListBox(wxWindow* parent, wxWindowID id,
706 const wxPoint& pos, const wxSize& size,
707 long style)
708 : wxListView(parent, id, pos, size, style)
709 {}
710
711 void OnFocus(wxFocusEvent& event) {
712 GetParent()->SetFocus();
713 event.Skip();
714 }
715
716 private:
717 DECLARE_EVENT_TABLE()
718 };
719
720 BEGIN_EVENT_TABLE(wxSTCListBox, wxListView)
721 EVT_SET_FOCUS( wxSTCListBox::OnFocus)
722 END_EVENT_TABLE()
723
724
725
726
727 // A window to place the wxSTCListBox upon
728 class wxSTCListBoxWin : public wxWindow {
729 private:
730 wxListView* lv;
731 CallBackAction doubleClickAction;
732 void* doubleClickActionData;
733 public:
734 wxSTCListBoxWin(wxWindow* parent, wxWindowID id) :
735 wxWindow(parent, id, wxDefaultPosition, wxSize(0,0), wxNO_BORDER )
736 {
737
738 SetBackgroundColour(*wxBLACK);
739 lv = new wxSTCListBox(this, id, wxDefaultPosition, wxDefaultSize,
740 wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER | wxNO_BORDER);
741 lv->SetCursor(wxCursor(wxCURSOR_ARROW));
742 lv->InsertColumn(0, wxEmptyString);
743 lv->InsertColumn(1, wxEmptyString);
744 Hide();
745 }
746
747 int IconWidth() {
748 wxImageList* il = lv->GetImageList(wxIMAGE_LIST_SMALL);
749 if (il != NULL) {
750 int w, h;
751 il->GetSize(0, w, h);
752 return w;
753 }
754 return 0;
755 }
756
757
758 void SetDoubleClickAction(CallBackAction action, void *data) {
759 doubleClickAction = action;
760 doubleClickActionData = data;
761 }
762
763
764 void OnFocus(wxFocusEvent& event) {
765 GetParent()->SetFocus();
766 event.Skip();
767 }
768
769 void OnSize(wxSizeEvent& event) {
770 // resize the child, leaving a 1 pixel border
771 wxSize sz = GetClientSize();
772 lv->SetSize(1, 1, sz.x-2, sz.y-2);
773 // reset the column widths
774 lv->SetColumnWidth(0, IconWidth()+4);
775 lv->SetColumnWidth(1, sz.x - 2 - lv->GetColumnWidth(0) -
776 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X));
777 event.Skip();
778 }
779
780 void OnActivate(wxListEvent& event) {
781 doubleClickAction(doubleClickActionData);
782 }
783
784 wxListView* GetLB() { return lv; }
785
786 private:
787 DECLARE_EVENT_TABLE()
788 };
789
790
791 BEGIN_EVENT_TABLE(wxSTCListBoxWin, wxWindow)
792 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus)
793 EVT_SIZE ( wxSTCListBoxWin::OnSize)
794 EVT_LIST_ITEM_ACTIVATED(-1, wxSTCListBoxWin::OnActivate)
795 END_EVENT_TABLE()
796
797
798
799 inline wxSTCListBoxWin* GETLBW(WindowID win) {
800 return ((wxSTCListBoxWin*)win);
801 }
802
803 inline wxListView* GETLB(WindowID win) {
804 return GETLBW(win)->GetLB();
805 }
806
807 //----------------------------------------------------------------------
808
809 class ListBoxImpl : public ListBox {
810 private:
811 int lineHeight;
812 bool unicodeMode;
813 int desiredVisibleRows;
814 int aveCharWidth;
815 int maxStrWidth;
816 wxImageList* imgList;
817 wxArrayInt* imgTypeMap;
818
819 public:
820 ListBoxImpl();
821 ~ListBoxImpl();
822
823 virtual void SetFont(Font &font);
824 virtual void Create(Window &parent, int ctrlID, int lineHeight_, bool unicodeMode_);
825 virtual void SetAverageCharWidth(int width);
826 virtual void SetVisibleRows(int rows);
827 virtual PRectangle GetDesiredRect();
828 virtual int CaretFromEdge();
829 virtual void Clear();
830 virtual void Append(char *s, int type = -1);
831 virtual int Length();
832 virtual void Select(int n);
833 virtual int GetSelection();
834 virtual int Find(const char *prefix);
835 virtual void GetValue(int n, char *value, int len);
836 virtual void Sort();
837 virtual void RegisterImage(int type, const char *xpm_data);
838 virtual void ClearRegisteredImages();
839 virtual void SetDoubleClickAction(CallBackAction, void *);
840
841 };
842
843
844 ListBoxImpl::ListBoxImpl()
845 : lineHeight(10), unicodeMode(false),
846 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
847 imgList(NULL), imgTypeMap(NULL)
848 {
849 }
850
851 ListBoxImpl::~ListBoxImpl() {
852 if (imgList) {
853 delete imgList;
854 imgList = NULL;
855 }
856 if (imgTypeMap) {
857 delete imgTypeMap;
858 imgTypeMap = NULL;
859 }
860 }
861
862
863 void ListBoxImpl::SetFont(Font &font) {
864 GETLB(id)->SetFont(*((wxFont*)font.GetID()));
865 }
866
867
868 void ListBoxImpl::Create(Window &parent, int ctrlID, int lineHeight_, bool unicodeMode_) {
869 lineHeight = lineHeight_;
870 unicodeMode = unicodeMode_;
871 maxStrWidth = 0;
872 id = new wxSTCListBoxWin(GETWIN(parent.GetID()), ctrlID);
873 if (imgList != NULL)
874 GETLB(id)->SetImageList(imgList, wxIMAGE_LIST_SMALL);
875 }
876
877
878 void ListBoxImpl::SetAverageCharWidth(int width) {
879 aveCharWidth = width;
880 }
881
882
883 void ListBoxImpl::SetVisibleRows(int rows) {
884 desiredVisibleRows = rows;
885 }
886
887
888 PRectangle ListBoxImpl::GetDesiredRect() {
889 // wxListCtrl doesn't have a DoGetBestSize, so instead we kept track of
890 // the max size in Append and calculate it here...
891 int maxw = maxStrWidth;
892 int maxh ;
893
894 // give it a default if there are no lines, and/or add a bit more
895 if (maxw == 0) maxw = 100;
896 maxw += aveCharWidth * 3 +
897 GETLBW(id)->IconWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
898 if (maxw > 350)
899 maxw = 350;
900
901 // estimate a desired height
902 int count = GETLB(id)->GetItemCount();
903 if (count) {
904 wxRect rect;
905 GETLB(id)->GetItemRect(0, rect);
906 maxh = count * rect.GetHeight();
907 if (maxh > 140) // TODO: Use desiredVisibleRows??
908 maxh = 140;
909
910 // Try to make the size an exact multiple of some number of lines
911 int lines = maxh / rect.GetHeight();
912 maxh = (lines + 1) * rect.GetHeight() + 2;
913 }
914 else
915 maxh = 100;
916
917 PRectangle rc;
918 rc.top = 0;
919 rc.left = 0;
920 rc.right = maxw;
921 rc.bottom = maxh;
922 return rc;
923 }
924
925
926 int ListBoxImpl::CaretFromEdge() {
927 return 4 + GETLBW(id)->IconWidth();
928 }
929
930
931 void ListBoxImpl::Clear() {
932 GETLB(id)->DeleteAllItems();
933 }
934
935
936 void ListBoxImpl::Append(char *s, int type) {
937 wxString text = stc2wx(s);
938 long count = GETLB(id)->GetItemCount();
939 long itemID = GETLB(id)->InsertItem(count, wxEmptyString);
940 GETLB(id)->SetItem(itemID, 1, text);
941 int itemWidth = 0;
942 GETLB(id)->GetTextExtent(text, &itemWidth, NULL);
943 maxStrWidth = wxMax(maxStrWidth, itemWidth);
944 if (type != -1) {
945 wxCHECK_RET(imgTypeMap, wxT("Unexpected NULL imgTypeMap"));
946 long idx = imgTypeMap->Item(type);
947 GETLB(id)->SetItemImage(itemID, idx, idx);
948 }
949 }
950
951
952 int ListBoxImpl::Length() {
953 return GETLB(id)->GetItemCount();
954 }
955
956
957 void ListBoxImpl::Select(int n) {
958 bool select = TRUE;
959 if (n == -1) {
960 n = 0;
961 select = FALSE;
962 }
963 GETLB(id)->Focus(n);
964 GETLB(id)->Select(n, select);
965 }
966
967
968 int ListBoxImpl::GetSelection() {
969 return GETLB(id)->GetFirstSelected();
970 }
971
972
973 int ListBoxImpl::Find(const char *WXUNUSED(prefix)) {
974 // No longer used
975 return -1;
976 }
977
978
979 void ListBoxImpl::GetValue(int n, char *value, int len) {
980 wxListItem item;
981 item.SetId(n);
982 item.SetColumn(1);
983 item.SetMask(wxLIST_MASK_TEXT);
984 GETLB(id)->GetItem(item);
985 strncpy(value, wx2stc(item.GetText()), len);
986 value[len-1] = '\0';
987 }
988
989 void ListBoxImpl::Sort() {
990 }
991
992
993 void ListBoxImpl::RegisterImage(int type, const char *xpm_data) {
994 wxMemoryInputStream stream(xpm_data, strlen(xpm_data)+1);
995 wxBitmap bmp(wxImage(stream, wxBITMAP_TYPE_XPM));
996
997 if (! imgList) {
998 // assumes all images are the same size
999 imgList = new wxImageList(bmp.GetWidth(), bmp.GetHeight(), TRUE);
1000 imgTypeMap = new wxArrayInt;
1001 }
1002
1003 int idx = imgList->Add(bmp);
1004
1005 // do we need to extend the mapping array?
1006 wxArrayInt& itm = *imgTypeMap;
1007 if ( itm.GetCount() < (size_t)type+1)
1008 itm.Add(-1, type - itm.GetCount() + 1);
1009
1010 // Add an item that maps type to the image index
1011 itm[type] = idx;
1012 }
1013
1014 void ListBoxImpl::ClearRegisteredImages() {
1015 if (imgList) {
1016 delete imgList;
1017 imgList = NULL;
1018 }
1019 if (imgTypeMap) {
1020 delete imgTypeMap;
1021 imgTypeMap = NULL;
1022 }
1023 if (id)
1024 GETLB(id)->SetImageList(NULL, wxIMAGE_LIST_SMALL);
1025 }
1026
1027
1028 void ListBoxImpl::SetDoubleClickAction(CallBackAction action, void *data) {
1029 GETLBW(id)->SetDoubleClickAction(action, data);
1030 }
1031
1032
1033
1034 ListBox::ListBox() {
1035 }
1036
1037 ListBox::~ListBox() {
1038 }
1039
1040 ListBox *ListBox::Allocate() {
1041 return new ListBoxImpl();
1042 }
1043
1044 //----------------------------------------------------------------------
1045
1046 Menu::Menu() : id(0) {
1047 }
1048
1049 void Menu::CreatePopUp() {
1050 Destroy();
1051 id = new wxMenu();
1052 }
1053
1054 void Menu::Destroy() {
1055 if (id)
1056 delete (wxMenu*)id;
1057 id = 0;
1058 }
1059
1060 void Menu::Show(Point pt, Window &w) {
1061 GETWIN(w.GetID())->PopupMenu((wxMenu*)id, pt.x - 4, pt.y);
1062 Destroy();
1063 }
1064
1065 //----------------------------------------------------------------------
1066
1067 DynamicLibrary *DynamicLibrary::Load(const char *WXUNUSED(modulePath)) {
1068 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1069 return NULL;
1070 }
1071
1072 //----------------------------------------------------------------------
1073
1074 ColourDesired Platform::Chrome() {
1075 wxColour c;
1076 c = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
1077 return ColourDesired(c.Red(), c.Green(), c.Blue());
1078 }
1079
1080 ColourDesired Platform::ChromeHighlight() {
1081 wxColour c;
1082 c = wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT);
1083 return ColourDesired(c.Red(), c.Green(), c.Blue());
1084 }
1085
1086 const char *Platform::DefaultFont() {
1087 static char buf[128];
1088 strcpy(buf, wxNORMAL_FONT->GetFaceName().mbc_str());
1089 return buf;
1090 }
1091
1092 int Platform::DefaultFontSize() {
1093 return wxNORMAL_FONT->GetPointSize();
1094 }
1095
1096 unsigned int Platform::DoubleClickTime() {
1097 return 500; // **** ::GetDoubleClickTime();
1098 }
1099
1100 bool Platform::MouseButtonBounce() {
1101 return FALSE;
1102 }
1103 void Platform::DebugDisplay(const char *s) {
1104 wxLogDebug(stc2wx(s));
1105 }
1106
1107 bool Platform::IsKeyDown(int WXUNUSED(key)) {
1108 return false; // I don't think we'll need this.
1109 }
1110
1111 long Platform::SendScintilla(WindowID w,
1112 unsigned int msg,
1113 unsigned long wParam,
1114 long lParam) {
1115
1116 wxStyledTextCtrl* stc = (wxStyledTextCtrl*)w;
1117 return stc->SendMsg(msg, wParam, lParam);
1118 }
1119
1120 long Platform::SendScintillaPointer(WindowID w,
1121 unsigned int msg,
1122 unsigned long wParam,
1123 void *lParam) {
1124
1125 wxStyledTextCtrl* stc = (wxStyledTextCtrl*)w;
1126 return stc->SendMsg(msg, wParam, (long)lParam);
1127 }
1128
1129
1130 // These are utility functions not really tied to a platform
1131
1132 int Platform::Minimum(int a, int b) {
1133 if (a < b)
1134 return a;
1135 else
1136 return b;
1137 }
1138
1139 int Platform::Maximum(int a, int b) {
1140 if (a > b)
1141 return a;
1142 else
1143 return b;
1144 }
1145
1146 #define TRACE
1147
1148 void Platform::DebugPrintf(const char *format, ...) {
1149 #ifdef TRACE
1150 char buffer[2000];
1151 va_list pArguments;
1152 va_start(pArguments, format);
1153 vsprintf(buffer,format,pArguments);
1154 va_end(pArguments);
1155 Platform::DebugDisplay(buffer);
1156 #endif
1157 }
1158
1159
1160 static bool assertionPopUps = true;
1161
1162 bool Platform::ShowAssertionPopUps(bool assertionPopUps_) {
1163 bool ret = assertionPopUps;
1164 assertionPopUps = assertionPopUps_;
1165 return ret;
1166 }
1167
1168 void Platform::Assert(const char *c, const char *file, int line) {
1169 char buffer[2000];
1170 sprintf(buffer, "Assertion [%s] failed at %s %d", c, file, line);
1171 if (assertionPopUps) {
1172 /*int idButton = */
1173 wxMessageBox(stc2wx(buffer),
1174 wxT("Assertion failure"),
1175 wxICON_HAND | wxOK);
1176 // if (idButton == IDRETRY) {
1177 // ::DebugBreak();
1178 // } else if (idButton == IDIGNORE) {
1179 // // all OK
1180 // } else {
1181 // abort();
1182 // }
1183 } else {
1184 strcat(buffer, "\r\n");
1185 Platform::DebugDisplay(buffer);
1186 abort();
1187 }
1188 }
1189
1190
1191 int Platform::Clamp(int val, int minVal, int maxVal) {
1192 if (val > maxVal)
1193 val = maxVal;
1194 if (val < minVal)
1195 val = minVal;
1196 return val;
1197 }
1198
1199
1200 bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage), char WXUNUSED(ch)) {
1201 return false;
1202 }
1203
1204 int Platform::DBCSCharLength(int WXUNUSED(codePage), const char *WXUNUSED(s)) {
1205 return 1;
1206 }
1207
1208 int Platform::DBCSCharMaxLength() {
1209 return 1;
1210 }
1211
1212
1213 //----------------------------------------------------------------------
1214
1215 ElapsedTime::ElapsedTime() {
1216 wxStartTimer();
1217 }
1218
1219 double ElapsedTime::Duration(bool reset) {
1220 double result = wxGetElapsedTime(reset);
1221 result /= 1000.0;
1222 return result;
1223 }
1224
1225
1226 //----------------------------------------------------------------------
1227
1228 #if wxUSE_UNICODE
1229 wxString stc2wx(const char* str, size_t len)
1230 {
1231 char *buffer=new char[len+1];
1232 strncpy(buffer, str, len);
1233 buffer[len]=0;
1234
1235 wxString cstr(buffer, wxConvUTF8);
1236
1237 delete[] buffer;
1238 return cstr;
1239 }
1240 #endif
1241
1242
1243
1244
1245
1246