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