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