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