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