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