]> git.saurik.com Git - wxWidgets.git/blob - src/stc/PlatWX.cpp
Updated version to 2.5.5
[wxWidgets.git] / src / stc / PlatWX.cpp
1 // Scintilla source code edit control
2 // PlatWX.cxx - implementation of platform facilities on wxWidgets
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 Point Point::FromLong(long lpoint) {
22 return Point(lpoint & 0xFFFF, lpoint >> 16);
23 }
24
25 wxRect wxRectFromPRectangle(PRectangle prc) {
26 wxRect r(prc.left, prc.top,
27 prc.Width(), prc.Height());
28 return r;
29 }
30
31 PRectangle PRectangleFromwxRect(wxRect rc) {
32 return PRectangle(rc.GetLeft(), rc.GetTop(),
33 rc.GetRight()+1, rc.GetBottom()+1);
34 }
35
36 wxColour wxColourFromCA(const ColourAllocated& ca) {
37 ColourDesired cd(ca.AsLong());
38 return wxColour((unsigned char)cd.GetRed(),
39 (unsigned char)cd.GetGreen(),
40 (unsigned char)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, bool extraFontFlag) {
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 wxFont* font = new wxFont(size,
186 wxDEFAULT,
187 italic ? wxITALIC : wxNORMAL,
188 bold ? wxBOLD : wxNORMAL,
189 false,
190 stc2wx(faceName),
191 encoding);
192 font->SetNoAntiAliasing(!extraFontFlag);
193 id = font;
194 }
195
196
197 void Font::Release() {
198 if (id)
199 delete (wxFont*)id;
200 id = 0;
201 }
202
203 //----------------------------------------------------------------------
204
205 class SurfaceImpl : public Surface {
206 private:
207 wxDC* hdc;
208 bool hdcOwned;
209 wxBitmap* bitmap;
210 int x;
211 int y;
212 bool unicodeMode;
213
214 public:
215 SurfaceImpl();
216 ~SurfaceImpl();
217
218 virtual void Init(WindowID wid);
219 virtual void Init(SurfaceID sid, WindowID wid);
220 virtual void InitPixMap(int width, int height, Surface *surface_, WindowID wid);
221
222 virtual void Release();
223 virtual bool Initialised();
224 virtual void PenColour(ColourAllocated fore);
225 virtual int LogPixelsY();
226 virtual int DeviceHeightFont(int points);
227 virtual void MoveTo(int x_, int y_);
228 virtual void LineTo(int x_, int y_);
229 virtual void Polygon(Point *pts, int npts, ColourAllocated fore, ColourAllocated back);
230 virtual void RectangleDraw(PRectangle rc, ColourAllocated fore, ColourAllocated back);
231 virtual void FillRectangle(PRectangle rc, ColourAllocated back);
232 virtual void FillRectangle(PRectangle rc, Surface &surfacePattern);
233 virtual void RoundedRectangle(PRectangle rc, ColourAllocated fore, ColourAllocated back);
234 virtual void Ellipse(PRectangle rc, ColourAllocated fore, ColourAllocated back);
235 virtual void Copy(PRectangle rc, Point from, Surface &surfaceSource);
236
237 virtual void DrawTextNoClip(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore, ColourAllocated back);
238 virtual void DrawTextClipped(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore, ColourAllocated back);
239 virtual void DrawTextTransparent(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore);
240 virtual void MeasureWidths(Font &font_, const char *s, int len, int *positions);
241 virtual int WidthText(Font &font_, const char *s, int len);
242 virtual int WidthChar(Font &font_, char ch);
243 virtual int Ascent(Font &font_);
244 virtual int Descent(Font &font_);
245 virtual int InternalLeading(Font &font_);
246 virtual int ExternalLeading(Font &font_);
247 virtual int Height(Font &font_);
248 virtual int AverageCharWidth(Font &font_);
249
250 virtual int SetPalette(Palette *pal, bool inBackGround);
251 virtual void SetClip(PRectangle rc);
252 virtual void FlushCachedState();
253
254 virtual void SetUnicodeMode(bool unicodeMode_);
255 virtual void SetDBCSMode(int codePage);
256
257 void BrushColour(ColourAllocated back);
258 void SetFont(Font &font_);
259 };
260
261
262
263 SurfaceImpl::SurfaceImpl() :
264 hdc(0), hdcOwned(0), bitmap(0),
265 x(0), y(0), unicodeMode(0)
266 {}
267
268 SurfaceImpl::~SurfaceImpl() {
269 Release();
270 }
271
272 void SurfaceImpl::Init(WindowID wid) {
273 #if 0
274 Release();
275 hdc = new wxMemoryDC();
276 hdcOwned = true;
277 #else
278 // On Mac and GTK the DC is not really valid until it has a bitmap
279 // selected into it. So instead of just creating the DC with no bitmap,
280 // go ahead and give it one.
281 InitPixMap(1,1,NULL,wid);
282 #endif
283 }
284
285 void SurfaceImpl::Init(SurfaceID hdc_, WindowID) {
286 Release();
287 hdc = (wxDC*)hdc_;
288 }
289
290 void SurfaceImpl::InitPixMap(int width, int height, Surface *WXUNUSED(surface_), WindowID) {
291 Release();
292 hdc = new wxMemoryDC();
293 hdcOwned = true;
294 if (width < 1) width = 1;
295 if (height < 1) height = 1;
296 bitmap = new wxBitmap(width, height);
297 ((wxMemoryDC*)hdc)->SelectObject(*bitmap);
298 }
299
300
301 void SurfaceImpl::Release() {
302 if (bitmap) {
303 ((wxMemoryDC*)hdc)->SelectObject(wxNullBitmap);
304 delete bitmap;
305 bitmap = 0;
306 }
307 if (hdcOwned) {
308 delete hdc;
309 hdc = 0;
310 hdcOwned = false;
311 }
312 }
313
314
315 bool SurfaceImpl::Initialised() {
316 return hdc != 0;
317 }
318
319
320 void SurfaceImpl::PenColour(ColourAllocated fore) {
321 hdc->SetPen(wxPen(wxColourFromCA(fore), 1, wxSOLID));
322 }
323
324 void SurfaceImpl::BrushColour(ColourAllocated back) {
325 hdc->SetBrush(wxBrush(wxColourFromCA(back), wxSOLID));
326 }
327
328 void SurfaceImpl::SetFont(Font &font_) {
329 if (font_.GetID()) {
330 hdc->SetFont(*((wxFont*)font_.GetID()));
331 }
332 }
333
334 int SurfaceImpl::LogPixelsY() {
335 return hdc->GetPPI().y;
336 }
337
338 int SurfaceImpl::DeviceHeightFont(int points) {
339 return points;
340 }
341
342 void SurfaceImpl::MoveTo(int x_, int y_) {
343 x = x_;
344 y = y_;
345 }
346
347 void SurfaceImpl::LineTo(int x_, int y_) {
348 hdc->DrawLine(x,y, x_,y_);
349 x = x_;
350 y = y_;
351 }
352
353 void SurfaceImpl::Polygon(Point *pts, int npts, ColourAllocated fore, ColourAllocated back) {
354 PenColour(fore);
355 BrushColour(back);
356 hdc->DrawPolygon(npts, (wxPoint*)pts);
357 }
358
359 void SurfaceImpl::RectangleDraw(PRectangle rc, ColourAllocated fore, ColourAllocated back) {
360 PenColour(fore);
361 BrushColour(back);
362 hdc->DrawRectangle(wxRectFromPRectangle(rc));
363 }
364
365 void SurfaceImpl::FillRectangle(PRectangle rc, ColourAllocated back) {
366 BrushColour(back);
367 hdc->SetPen(*wxTRANSPARENT_PEN);
368 hdc->DrawRectangle(wxRectFromPRectangle(rc));
369 }
370
371 void SurfaceImpl::FillRectangle(PRectangle rc, Surface &surfacePattern) {
372 wxBrush br;
373 if (((SurfaceImpl&)surfacePattern).bitmap)
374 br = wxBrush(*((SurfaceImpl&)surfacePattern).bitmap);
375 else // Something is wrong so display in red
376 br = wxBrush(*wxRED, wxSOLID);
377 hdc->SetPen(*wxTRANSPARENT_PEN);
378 hdc->SetBrush(br);
379 hdc->DrawRectangle(wxRectFromPRectangle(rc));
380 }
381
382 void SurfaceImpl::RoundedRectangle(PRectangle rc, ColourAllocated fore, ColourAllocated back) {
383 PenColour(fore);
384 BrushColour(back);
385 hdc->DrawRoundedRectangle(wxRectFromPRectangle(rc), 4);
386 }
387
388 void SurfaceImpl::Ellipse(PRectangle rc, ColourAllocated fore, ColourAllocated back) {
389 PenColour(fore);
390 BrushColour(back);
391 hdc->DrawEllipse(wxRectFromPRectangle(rc));
392 }
393
394 void SurfaceImpl::Copy(PRectangle rc, Point from, Surface &surfaceSource) {
395 wxRect r = wxRectFromPRectangle(rc);
396 hdc->Blit(r.x, r.y, r.width, r.height,
397 ((SurfaceImpl&)surfaceSource).hdc,
398 from.x, from.y, wxCOPY);
399 }
400
401 void SurfaceImpl::DrawTextNoClip(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
409 // ybase is where the baseline should be, but wxWin uses the upper left
410 // corner, so I need to calculate the real position for the text...
411 hdc->DrawText(stc2wx(s, len), rc.left, ybase - font.ascent);
412 }
413
414 void SurfaceImpl::DrawTextClipped(PRectangle rc, Font &font, int ybase,
415 const char *s, int len,
416 ColourAllocated fore, ColourAllocated back) {
417 SetFont(font);
418 hdc->SetTextForeground(wxColourFromCA(fore));
419 hdc->SetTextBackground(wxColourFromCA(back));
420 FillRectangle(rc, back);
421 hdc->SetClippingRegion(wxRectFromPRectangle(rc));
422
423 // see comments above
424 hdc->DrawText(stc2wx(s, len), rc.left, ybase - font.ascent);
425 hdc->DestroyClippingRegion();
426 }
427
428
429 void SurfaceImpl::DrawTextTransparent(PRectangle rc, Font &font, int ybase,
430 const char *s, int len,
431 ColourAllocated fore) {
432
433 SetFont(font);
434 hdc->SetTextForeground(wxColourFromCA(fore));
435 hdc->SetBackgroundMode(wxTRANSPARENT);
436
437 // ybase is where the baseline should be, but wxWin uses the upper left
438 // corner, so I need to calculate the real position for the text...
439 hdc->DrawText(stc2wx(s, len), rc.left, ybase - font.ascent);
440
441 hdc->SetBackgroundMode(wxSOLID);
442 }
443
444
445 void SurfaceImpl::MeasureWidths(Font &font, const char *s, int len, int *positions) {
446
447 wxString str = stc2wx(s, len);
448 wxArrayInt tpos;
449
450 SetFont(font);
451
452 hdc->GetPartialTextExtents(str, tpos);
453
454 #if wxUSE_UNICODE
455 // Map the widths for UCS-2 characters back to the UTF-8 input string
456 // NOTE: I don't think this is right for when sizeof(wxChar) > 2, ie wxGTK2
457 // so figure it out and fix it!
458 size_t i = 0;
459 size_t ui = 0;
460 while ((int)i < len) {
461 unsigned char uch = (unsigned char)s[i];
462 positions[i++] = tpos[ui];
463 if (uch >= 0x80) {
464 if (uch < (0x80 + 0x40 + 0x20)) {
465 positions[i++] = tpos[ui];
466 } else {
467 positions[i++] = tpos[ui];
468 positions[i++] = tpos[ui];
469 }
470 }
471 ui++;
472 }
473 #else
474
475 // If not unicode then just use the widths we have
476 memcpy(positions, tpos.begin(), len * sizeof(int));
477 #endif
478 }
479
480
481 int SurfaceImpl::WidthText(Font &font, const char *s, int len) {
482 SetFont(font);
483 int w;
484 int h;
485
486 hdc->GetTextExtent(stc2wx(s, len), &w, &h);
487 return w;
488 }
489
490
491 int SurfaceImpl::WidthChar(Font &font, char ch) {
492 SetFont(font);
493 int w;
494 int h;
495 char s[2] = { ch, 0 };
496
497 hdc->GetTextExtent(stc2wx(s, 1), &w, &h);
498 return w;
499 }
500
501 #define EXTENT_TEST wxT(" `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
502
503 int SurfaceImpl::Ascent(Font &font) {
504 SetFont(font);
505 int w, h, d, e;
506 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
507 font.ascent = h - d;
508 return font.ascent;
509 }
510
511 int SurfaceImpl::Descent(Font &font) {
512 SetFont(font);
513 int w, h, d, e;
514 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
515 return d;
516 }
517
518 int SurfaceImpl::InternalLeading(Font &WXUNUSED(font)) {
519 return 0;
520 }
521
522 int SurfaceImpl::ExternalLeading(Font &font) {
523 SetFont(font);
524 int w, h, d, e;
525 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
526 return e;
527 }
528
529 int SurfaceImpl::Height(Font &font) {
530 SetFont(font);
531 return hdc->GetCharHeight() + 1;
532 }
533
534 int SurfaceImpl::AverageCharWidth(Font &font) {
535 SetFont(font);
536 return hdc->GetCharWidth();
537 }
538
539 int SurfaceImpl::SetPalette(Palette *WXUNUSED(pal), bool WXUNUSED(inBackGround)) {
540 return 0;
541 }
542
543 void SurfaceImpl::SetClip(PRectangle rc) {
544 hdc->SetClippingRegion(wxRectFromPRectangle(rc));
545 }
546
547 void SurfaceImpl::FlushCachedState() {
548 }
549
550 void SurfaceImpl::SetUnicodeMode(bool unicodeMode_) {
551 unicodeMode=unicodeMode_;
552 }
553
554 void SurfaceImpl::SetDBCSMode(int WXUNUSED(codePage)) {
555 // dbcsMode = codePage == SC_CP_DBCS;
556 }
557
558
559 Surface *Surface::Allocate() {
560 return new SurfaceImpl;
561 }
562
563
564 //----------------------------------------------------------------------
565
566
567 inline wxWindow* GETWIN(WindowID id) { return (wxWindow*)id; }
568
569 Window::~Window() {
570 }
571
572 void Window::Destroy() {
573 if (id) {
574 Show(false);
575 GETWIN(id)->Destroy();
576 }
577 id = 0;
578 }
579
580 bool Window::HasFocus() {
581 return wxWindow::FindFocus() == GETWIN(id);
582 }
583
584 PRectangle Window::GetPosition() {
585 if (! id) return PRectangle();
586 wxRect rc(GETWIN(id)->GetPosition(), GETWIN(id)->GetSize());
587 return PRectangleFromwxRect(rc);
588 }
589
590 void Window::SetPosition(PRectangle rc) {
591 wxRect r = wxRectFromPRectangle(rc);
592 GETWIN(id)->SetSize(r);
593 }
594
595 void Window::SetPositionRelative(PRectangle rc, Window) {
596 SetPosition(rc); // ????
597 }
598
599 PRectangle Window::GetClientPosition() {
600 if (! id) return PRectangle();
601 wxSize sz = GETWIN(id)->GetClientSize();
602 return PRectangle(0, 0, sz.x, sz.y);
603 }
604
605 void Window::Show(bool show) {
606 GETWIN(id)->Show(show);
607 }
608
609 void Window::InvalidateAll() {
610 GETWIN(id)->Refresh(false);
611 wxWakeUpIdle();
612 }
613
614 void Window::InvalidateRectangle(PRectangle rc) {
615 wxRect r = wxRectFromPRectangle(rc);
616 GETWIN(id)->Refresh(false, &r);
617 wxWakeUpIdle();
618 }
619
620 void Window::SetFont(Font &font) {
621 GETWIN(id)->SetFont(*((wxFont*)font.GetID()));
622 }
623
624 void Window::SetCursor(Cursor curs) {
625 int cursorId;
626
627 switch (curs) {
628 case cursorText:
629 cursorId = wxCURSOR_IBEAM;
630 break;
631 case cursorArrow:
632 cursorId = wxCURSOR_ARROW;
633 break;
634 case cursorUp:
635 cursorId = wxCURSOR_ARROW; // ** no up arrow... wxCURSOR_UPARROW;
636 break;
637 case cursorWait:
638 cursorId = wxCURSOR_WAIT;
639 break;
640 case cursorHoriz:
641 cursorId = wxCURSOR_SIZEWE;
642 break;
643 case cursorVert:
644 cursorId = wxCURSOR_SIZENS;
645 break;
646 case cursorReverseArrow:
647 cursorId = wxCURSOR_RIGHT_ARROW;
648 break;
649 case cursorHand:
650 cursorId = wxCURSOR_HAND;
651 break;
652 default:
653 cursorId = wxCURSOR_ARROW;
654 break;
655 }
656 #ifdef __WXMOTIF__
657 wxCursor wc = wxStockCursor(cursorId) ;
658 #else
659 wxCursor wc = wxCursor(cursorId) ;
660 #endif
661 GETWIN(id)->SetCursor(wc);
662 }
663
664
665 void Window::SetTitle(const char *s) {
666 GETWIN(id)->SetTitle(stc2wx(s));
667 }
668
669
670 //----------------------------------------------------------------------
671 // Helper classes for ListBox
672
673
674 // This is a simple subclass of wxListView that just resets focus to the
675 // parent when it gets it.
676 class wxSTCListBox : public wxListView {
677 public:
678 wxSTCListBox(wxWindow* parent, wxWindowID id,
679 const wxPoint& pos, const wxSize& size,
680 long style)
681 : wxListView(parent, id, pos, size, style)
682 {}
683
684
685 void OnFocus(wxFocusEvent& event) {
686 GetParent()->SetFocus();
687 event.Skip();
688 }
689
690 void OnKillFocus(wxFocusEvent& WXUNUSED(event)) {
691 // Do nothing. Prevents base class from resetting the colors...
692 }
693
694 #ifdef __WXMAC__
695 // For some reason I don't understand yet the focus doesn't really leave
696 // the listbox like it should, so if we get any events feed them back to
697 // the wxSTC
698 void OnKeyDown(wxKeyEvent& event) {
699 GetGrandParent()->GetEventHandler()->ProcessEvent(event);
700 }
701 void OnChar(wxKeyEvent& event) {
702 GetGrandParent()->GetEventHandler()->ProcessEvent(event);
703 }
704
705 // And we need to force the focus back when being destroyed
706 ~wxSTCListBox() {
707 GetGrandParent()->SetFocus();
708 }
709 #endif
710
711 private:
712 DECLARE_EVENT_TABLE()
713 };
714
715 BEGIN_EVENT_TABLE(wxSTCListBox, wxListView)
716 EVT_SET_FOCUS( wxSTCListBox::OnFocus)
717 EVT_KILL_FOCUS(wxSTCListBox::OnKillFocus)
718 #ifdef __WXMAC__
719 EVT_KEY_DOWN( wxSTCListBox::OnKeyDown)
720 EVT_CHAR( wxSTCListBox::OnChar)
721 #endif
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), wxSIMPLE_BORDER )
736 {
737
738 lv = new wxSTCListBox(this, id, wxDefaultPosition, wxDefaultSize,
739 wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER | wxNO_BORDER);
740 lv->SetCursor(wxCursor(wxCURSOR_ARROW));
741 lv->InsertColumn(0, wxEmptyString);
742 lv->InsertColumn(1, wxEmptyString);
743
744 // Eventhough we immediately reset the focus to the parent, this helps
745 // things to look right...
746 lv->SetFocus();
747
748 Hide();
749 }
750
751
752 // On OSX and (possibly others) there can still be pending
753 // messages/events for the list control when Scintilla wants to
754 // close it, so do a pending delete of it instead of destroying
755 // immediately.
756 bool Destroy() {
757 #ifdef __WXMAC__
758 // The bottom edge of this window is not getting properly
759 // refreshed upon deletion, so help it out...
760 wxWindow* p = GetParent();
761 wxRect r(GetPosition(), GetSize());
762 r.SetHeight(r.GetHeight()+1);
763 p->Refresh(false, &r);
764 #endif
765 if ( !wxPendingDelete.Member(this) )
766 wxPendingDelete.Append(this);
767 return true;
768 }
769
770
771 int IconWidth() {
772 wxImageList* il = lv->GetImageList(wxIMAGE_LIST_SMALL);
773 if (il != NULL) {
774 int w, h;
775 il->GetSize(0, w, h);
776 return w;
777 }
778 return 0;
779 }
780
781
782 void SetDoubleClickAction(CallBackAction action, void *data) {
783 doubleClickAction = action;
784 doubleClickActionData = data;
785 }
786
787
788 void OnFocus(wxFocusEvent& event) {
789 GetParent()->SetFocus();
790 event.Skip();
791 }
792
793 void OnSize(wxSizeEvent& event) {
794 // resize the child
795 wxSize sz = GetClientSize();
796 lv->SetSize(sz);
797 // reset the column widths
798 lv->SetColumnWidth(0, IconWidth()+4);
799 lv->SetColumnWidth(1, sz.x - 2 - lv->GetColumnWidth(0) -
800 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X));
801 event.Skip();
802 }
803
804 #ifdef __WXMAC__
805 virtual bool Show(bool show = true) {
806 bool rv = wxWindow::Show(show);
807 GetParent()->Refresh(false);
808 return rv;
809 }
810 #endif
811
812 void OnActivate(wxListEvent& WXUNUSED(event)) {
813 doubleClickAction(doubleClickActionData);
814 }
815
816 wxListView* GetLB() { return lv; }
817
818 private:
819 DECLARE_EVENT_TABLE()
820 };
821
822
823 BEGIN_EVENT_TABLE(wxSTCListBoxWin, wxWindow)
824 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus)
825 EVT_SIZE ( wxSTCListBoxWin::OnSize)
826 EVT_LIST_ITEM_ACTIVATED(wxID_ANY, wxSTCListBoxWin::OnActivate)
827 END_EVENT_TABLE()
828
829
830
831 inline wxSTCListBoxWin* GETLBW(WindowID win) {
832 return ((wxSTCListBoxWin*)win);
833 }
834
835 inline wxListView* GETLB(WindowID win) {
836 return GETLBW(win)->GetLB();
837 }
838
839 //----------------------------------------------------------------------
840
841 class ListBoxImpl : public ListBox {
842 private:
843 int lineHeight;
844 bool unicodeMode;
845 int desiredVisibleRows;
846 int aveCharWidth;
847 int maxStrWidth;
848 wxImageList* imgList;
849 wxArrayInt* imgTypeMap;
850
851 public:
852 ListBoxImpl();
853 ~ListBoxImpl();
854
855 virtual void SetFont(Font &font);
856 virtual void Create(Window &parent, int ctrlID, int lineHeight_, bool unicodeMode_);
857 virtual void SetAverageCharWidth(int width);
858 virtual void SetVisibleRows(int rows);
859 virtual PRectangle GetDesiredRect();
860 virtual int CaretFromEdge();
861 virtual void Clear();
862 virtual void Append(char *s, int type = -1);
863 virtual int Length();
864 virtual void Select(int n);
865 virtual int GetSelection();
866 virtual int Find(const char *prefix);
867 virtual void GetValue(int n, char *value, int len);
868 virtual void RegisterImage(int type, const char *xpm_data);
869 virtual void ClearRegisteredImages();
870 virtual void SetDoubleClickAction(CallBackAction, void *);
871
872 };
873
874
875 ListBoxImpl::ListBoxImpl()
876 : lineHeight(10), unicodeMode(false),
877 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
878 imgList(NULL), imgTypeMap(NULL)
879 {
880 }
881
882 ListBoxImpl::~ListBoxImpl() {
883 if (imgList) {
884 delete imgList;
885 imgList = NULL;
886 }
887 if (imgTypeMap) {
888 delete imgTypeMap;
889 imgTypeMap = NULL;
890 }
891 }
892
893
894 void ListBoxImpl::SetFont(Font &font) {
895 GETLB(id)->SetFont(*((wxFont*)font.GetID()));
896 }
897
898
899 void ListBoxImpl::Create(Window &parent, int ctrlID, int lineHeight_, bool unicodeMode_) {
900 lineHeight = lineHeight_;
901 unicodeMode = unicodeMode_;
902 maxStrWidth = 0;
903 id = new wxSTCListBoxWin(GETWIN(parent.GetID()), ctrlID);
904 if (imgList != NULL)
905 GETLB(id)->SetImageList(imgList, wxIMAGE_LIST_SMALL);
906 }
907
908
909 void ListBoxImpl::SetAverageCharWidth(int width) {
910 aveCharWidth = width;
911 }
912
913
914 void ListBoxImpl::SetVisibleRows(int rows) {
915 desiredVisibleRows = rows;
916 }
917
918
919 PRectangle ListBoxImpl::GetDesiredRect() {
920 // wxListCtrl doesn't have a DoGetBestSize, so instead we kept track of
921 // the max size in Append and calculate it here...
922 int maxw = maxStrWidth;
923 int maxh ;
924
925 // give it a default if there are no lines, and/or add a bit more
926 if (maxw == 0) maxw = 100;
927 maxw += aveCharWidth * 3 +
928 GETLBW(id)->IconWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
929 if (maxw > 350)
930 maxw = 350;
931
932 // estimate a desired height
933 int count = GETLB(id)->GetItemCount();
934 if (count) {
935 wxRect rect;
936 GETLB(id)->GetItemRect(0, rect);
937 maxh = count * rect.GetHeight();
938 if (maxh > 140) // TODO: Use desiredVisibleRows??
939 maxh = 140;
940
941 // Try to make the size an exact multiple of some number of lines
942 int lines = maxh / rect.GetHeight();
943 maxh = (lines + 1) * rect.GetHeight() + 2;
944 }
945 else
946 maxh = 100;
947
948 PRectangle rc;
949 rc.top = 0;
950 rc.left = 0;
951 rc.right = maxw;
952 rc.bottom = maxh;
953 return rc;
954 }
955
956
957 int ListBoxImpl::CaretFromEdge() {
958 return 4 + GETLBW(id)->IconWidth();
959 }
960
961
962 void ListBoxImpl::Clear() {
963 GETLB(id)->DeleteAllItems();
964 }
965
966
967 void ListBoxImpl::Append(char *s, int type) {
968 wxString text = stc2wx(s);
969 long count = GETLB(id)->GetItemCount();
970 long itemID = GETLB(id)->InsertItem(count, wxEmptyString);
971 GETLB(id)->SetItem(itemID, 1, text);
972 int itemWidth = 0;
973 GETLB(id)->GetTextExtent(text, &itemWidth, NULL);
974 maxStrWidth = wxMax(maxStrWidth, itemWidth);
975 if (type != -1) {
976 wxCHECK_RET(imgTypeMap, wxT("Unexpected NULL imgTypeMap"));
977 long idx = imgTypeMap->Item(type);
978 GETLB(id)->SetItemImage(itemID, idx, idx);
979 }
980 }
981
982
983 int ListBoxImpl::Length() {
984 return GETLB(id)->GetItemCount();
985 }
986
987
988 void ListBoxImpl::Select(int n) {
989 bool select = true;
990 if (n == -1) {
991 n = 0;
992 select = false;
993 }
994 GETLB(id)->Focus(n);
995 GETLB(id)->Select(n, select);
996 }
997
998
999 int ListBoxImpl::GetSelection() {
1000 return GETLB(id)->GetFirstSelected();
1001 }
1002
1003
1004 int ListBoxImpl::Find(const char *WXUNUSED(prefix)) {
1005 // No longer used
1006 return wxNOT_FOUND;
1007 }
1008
1009
1010 void ListBoxImpl::GetValue(int n, char *value, int len) {
1011 wxListItem item;
1012 item.SetId(n);
1013 item.SetColumn(1);
1014 item.SetMask(wxLIST_MASK_TEXT);
1015 GETLB(id)->GetItem(item);
1016 strncpy(value, wx2stc(item.GetText()), len);
1017 value[len-1] = '\0';
1018 }
1019
1020
1021 void ListBoxImpl::RegisterImage(int type, const char *xpm_data) {
1022 wxMemoryInputStream stream(xpm_data, strlen(xpm_data)+1);
1023 wxImage img(stream, wxBITMAP_TYPE_XPM);
1024 wxBitmap bmp(img);
1025
1026 if (! imgList) {
1027 // assumes all images are the same size
1028 imgList = new wxImageList(bmp.GetWidth(), bmp.GetHeight(), true);
1029 imgTypeMap = new wxArrayInt;
1030 }
1031
1032 int idx = imgList->Add(bmp);
1033
1034 // do we need to extend the mapping array?
1035 wxArrayInt& itm = *imgTypeMap;
1036 if ( itm.GetCount() < (size_t)type+1)
1037 itm.Add(-1, type - itm.GetCount() + 1);
1038
1039 // Add an item that maps type to the image index
1040 itm[type] = idx;
1041 }
1042
1043 void ListBoxImpl::ClearRegisteredImages() {
1044 if (imgList) {
1045 delete imgList;
1046 imgList = NULL;
1047 }
1048 if (imgTypeMap) {
1049 delete imgTypeMap;
1050 imgTypeMap = NULL;
1051 }
1052 if (id)
1053 GETLB(id)->SetImageList(NULL, wxIMAGE_LIST_SMALL);
1054 }
1055
1056
1057 void ListBoxImpl::SetDoubleClickAction(CallBackAction action, void *data) {
1058 GETLBW(id)->SetDoubleClickAction(action, data);
1059 }
1060
1061
1062
1063 ListBox::ListBox() {
1064 }
1065
1066 ListBox::~ListBox() {
1067 }
1068
1069 ListBox *ListBox::Allocate() {
1070 return new ListBoxImpl();
1071 }
1072
1073 //----------------------------------------------------------------------
1074
1075 Menu::Menu() : id(0) {
1076 }
1077
1078 void Menu::CreatePopUp() {
1079 Destroy();
1080 id = new wxMenu();
1081 }
1082
1083 void Menu::Destroy() {
1084 if (id)
1085 delete (wxMenu*)id;
1086 id = 0;
1087 }
1088
1089 void Menu::Show(Point pt, Window &w) {
1090 GETWIN(w.GetID())->PopupMenu((wxMenu*)id, pt.x - 4, pt.y);
1091 Destroy();
1092 }
1093
1094 //----------------------------------------------------------------------
1095
1096 DynamicLibrary *DynamicLibrary::Load(const char *WXUNUSED(modulePath)) {
1097 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1098 return NULL;
1099 }
1100
1101 //----------------------------------------------------------------------
1102
1103 ColourDesired Platform::Chrome() {
1104 wxColour c;
1105 c = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
1106 return ColourDesired(c.Red(), c.Green(), c.Blue());
1107 }
1108
1109 ColourDesired Platform::ChromeHighlight() {
1110 wxColour c;
1111 c = wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT);
1112 return ColourDesired(c.Red(), c.Green(), c.Blue());
1113 }
1114
1115 const char *Platform::DefaultFont() {
1116 static char buf[128];
1117 strcpy(buf, wxNORMAL_FONT->GetFaceName().mbc_str());
1118 return buf;
1119 }
1120
1121 int Platform::DefaultFontSize() {
1122 return wxNORMAL_FONT->GetPointSize();
1123 }
1124
1125 unsigned int Platform::DoubleClickTime() {
1126 return 500; // **** ::GetDoubleClickTime();
1127 }
1128
1129 bool Platform::MouseButtonBounce() {
1130 return false;
1131 }
1132 void Platform::DebugDisplay(const char *s) {
1133 wxLogDebug(stc2wx(s));
1134 }
1135
1136 bool Platform::IsKeyDown(int WXUNUSED(key)) {
1137 return false; // I don't think we'll need this.
1138 }
1139
1140 long Platform::SendScintilla(WindowID w,
1141 unsigned int msg,
1142 unsigned long wParam,
1143 long lParam) {
1144
1145 wxStyledTextCtrl* stc = (wxStyledTextCtrl*)w;
1146 return stc->SendMsg(msg, wParam, lParam);
1147 }
1148
1149 long Platform::SendScintillaPointer(WindowID w,
1150 unsigned int msg,
1151 unsigned long wParam,
1152 void *lParam) {
1153
1154 wxStyledTextCtrl* stc = (wxStyledTextCtrl*)w;
1155 return stc->SendMsg(msg, wParam, (long)lParam);
1156 }
1157
1158
1159 // These are utility functions not really tied to a platform
1160
1161 int Platform::Minimum(int a, int b) {
1162 if (a < b)
1163 return a;
1164 else
1165 return b;
1166 }
1167
1168 int Platform::Maximum(int a, int b) {
1169 if (a > b)
1170 return a;
1171 else
1172 return b;
1173 }
1174
1175 #define TRACE
1176
1177 void Platform::DebugPrintf(const char *format, ...) {
1178 #ifdef TRACE
1179 char buffer[2000];
1180 va_list pArguments;
1181 va_start(pArguments, format);
1182 vsprintf(buffer,format,pArguments);
1183 va_end(pArguments);
1184 Platform::DebugDisplay(buffer);
1185 #endif
1186 }
1187
1188
1189 static bool assertionPopUps = true;
1190
1191 bool Platform::ShowAssertionPopUps(bool assertionPopUps_) {
1192 bool ret = assertionPopUps;
1193 assertionPopUps = assertionPopUps_;
1194 return ret;
1195 }
1196
1197 void Platform::Assert(const char *c, const char *file, int line) {
1198 char buffer[2000];
1199 sprintf(buffer, "Assertion [%s] failed at %s %d", c, file, line);
1200 if (assertionPopUps) {
1201 /*int idButton = */
1202 wxMessageBox(stc2wx(buffer),
1203 wxT("Assertion failure"),
1204 wxICON_HAND | wxOK);
1205 // if (idButton == IDRETRY) {
1206 // ::DebugBreak();
1207 // } else if (idButton == IDIGNORE) {
1208 // // all OK
1209 // } else {
1210 // abort();
1211 // }
1212 } else {
1213 strcat(buffer, "\r\n");
1214 Platform::DebugDisplay(buffer);
1215 abort();
1216 }
1217 }
1218
1219
1220 int Platform::Clamp(int val, int minVal, int maxVal) {
1221 if (val > maxVal)
1222 val = maxVal;
1223 if (val < minVal)
1224 val = minVal;
1225 return val;
1226 }
1227
1228
1229 bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage), char WXUNUSED(ch)) {
1230 return false;
1231 }
1232
1233 int Platform::DBCSCharLength(int WXUNUSED(codePage), const char *WXUNUSED(s)) {
1234 return 1;
1235 }
1236
1237 int Platform::DBCSCharMaxLength() {
1238 return 1;
1239 }
1240
1241
1242 //----------------------------------------------------------------------
1243
1244 ElapsedTime::ElapsedTime() {
1245 wxStartTimer();
1246 }
1247
1248 double ElapsedTime::Duration(bool reset) {
1249 double result = wxGetElapsedTime(reset);
1250 result /= 1000.0;
1251 return result;
1252 }
1253
1254
1255 //----------------------------------------------------------------------
1256
1257 #if wxUSE_UNICODE
1258
1259 #include "UniConversion.h"
1260
1261 // Convert using Scintilla's functions instead of wx's, Scintilla's are more
1262 // forgiving and won't assert...
1263
1264 wxString stc2wx(const char* str, size_t len)
1265 {
1266 if (!len)
1267 return wxEmptyString;
1268
1269 size_t wclen = UCS2Length(str, len);
1270 wxWCharBuffer buffer(wclen+1);
1271
1272 size_t actualLen = UCS2FromUTF8(str, len, buffer.data(), wclen+1);
1273 return wxString(buffer.data(), actualLen);
1274 }
1275
1276
1277
1278 wxString stc2wx(const char* str)
1279 {
1280 return stc2wx(str, strlen(str));
1281 }
1282
1283
1284 const wxWX2MBbuf wx2stc(const wxString& str)
1285 {
1286 const wchar_t* wcstr = str.c_str();
1287 size_t wclen = str.length();
1288 size_t len = UTF8Length(wcstr, wclen);
1289
1290 wxCharBuffer buffer(len+1);
1291 UTF8FromUCS2(wcstr, wclen, buffer.data(), len);
1292
1293 // TODO check NULL termination!!
1294
1295 return buffer;
1296 }
1297
1298 #endif
1299
1300
1301
1302
1303
1304