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