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