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