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