]> git.saurik.com Git - wxWidgets.git/blob - src/stc/PlatWX.cpp
add ms78 projects
[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 wxHAVE_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), 1, wxSOLID));
295 }
296
297 void SurfaceImpl::BrushColour(ColourAllocated back) {
298 hdc->SetBrush(wxBrush(wxColourFromCA(back), wxSOLID));
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, wxSOLID);
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 wxHAVE_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(wxTRANSPARENT);
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(wxSOLID);
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 int 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 #ifdef __WXMOTIF__
712 wxCursor wc = wxStockCursor(cursorId) ;
713 #else
714 wxCursor wc = wxCursor(cursorId) ;
715 #endif
716 if(curs != cursorLast)
717 {
718 GETWIN(id)->SetCursor(wc);
719 cursorLast = curs;
720 }
721 }
722
723
724 void Window::SetTitle(const char *s) {
725 GETWIN(id)->SetLabel(stc2wx(s));
726 }
727
728
729 // Returns rectangle of monitor pt is on
730 PRectangle Window::GetMonitorRect(Point pt) {
731 wxRect rect;
732 if (! id) return PRectangle();
733 #if wxUSE_DISPLAY
734 // Get the display the point is found on
735 int n = wxDisplay::GetFromPoint(wxPoint(pt.x, pt.y));
736 wxDisplay dpy(n == wxNOT_FOUND ? 0 : n);
737 rect = dpy.GetGeometry();
738 #endif
739 return PRectangleFromwxRect(rect);
740 }
741
742 //----------------------------------------------------------------------
743 // Helper classes for ListBox
744
745
746 // This is a simple subclass of wxListView that just resets focus to the
747 // parent when it gets it.
748 class wxSTCListBox : public wxListView {
749 public:
750 wxSTCListBox(wxWindow* parent, wxWindowID id,
751 const wxPoint& pos, const wxSize& size,
752 long style)
753 : wxListView()
754 {
755 #ifdef __WXMSW__
756 Hide(); // don't flicker as we move it around...
757 #endif
758 Create(parent, id, pos, size, style);
759 }
760
761
762 void OnFocus(wxFocusEvent& event) {
763 GetParent()->SetFocus();
764 event.Skip();
765 }
766
767 void OnKillFocus(wxFocusEvent& WXUNUSED(event)) {
768 // Do nothing. Prevents base class from resetting the colors...
769 }
770
771 #ifdef __WXMAC__
772 // For some reason I don't understand yet the focus doesn't really leave
773 // the listbox like it should, so if we get any events feed them back to
774 // the wxSTC
775 void OnKeyDown(wxKeyEvent& event) {
776 GetGrandParent()->GetEventHandler()->ProcessEvent(event);
777 }
778 void OnChar(wxKeyEvent& event) {
779 GetGrandParent()->GetEventHandler()->ProcessEvent(event);
780 }
781
782 // And we need to force the focus back when being destroyed
783 ~wxSTCListBox() {
784 GetGrandParent()->SetFocus();
785 }
786 #endif
787
788 private:
789 DECLARE_EVENT_TABLE()
790 };
791
792 BEGIN_EVENT_TABLE(wxSTCListBox, wxListView)
793 EVT_SET_FOCUS( wxSTCListBox::OnFocus)
794 EVT_KILL_FOCUS(wxSTCListBox::OnKillFocus)
795 #ifdef __WXMAC__
796 EVT_KEY_DOWN( wxSTCListBox::OnKeyDown)
797 EVT_CHAR( wxSTCListBox::OnChar)
798 #endif
799 END_EVENT_TABLE()
800
801
802
803 #if wxUSE_POPUPWIN //-----------------------------------
804 #include "wx/popupwin.h"
805
806
807 //
808 // TODO: Refactor these two classes to have a common base (or a mix-in) to get
809 // rid of the code duplication. (Either that or convince somebody to
810 // implement wxPopupWindow for the Mac!!)
811 //
812 // In the meantime, be careful to duplicate any changes as needed...
813 //
814
815 // A popup window to place the wxSTCListBox upon
816 class wxSTCListBoxWin : public wxPopupWindow
817 {
818 private:
819 wxListView* lv;
820 CallBackAction doubleClickAction;
821 void* doubleClickActionData;
822 public:
823 wxSTCListBoxWin(wxWindow* parent, wxWindowID id, Point WXUNUSED(location)) :
824 wxPopupWindow(parent, wxBORDER_NONE)
825 {
826
827 SetBackgroundColour(*wxBLACK); // for our simple border
828
829 lv = new wxSTCListBox(parent, id, wxPoint(-50,-50), wxDefaultSize,
830 wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER | wxBORDER_NONE);
831 lv->SetCursor(wxCursor(wxCURSOR_ARROW));
832 lv->InsertColumn(0, wxEmptyString);
833 lv->InsertColumn(1, wxEmptyString);
834
835 // NOTE: We need to fool the wxListView into thinking that it has the
836 // focus so it will use the normal selection colour and will look
837 // "right" to the user. But since the wxPopupWindow or its children
838 // can't receive focus then we have to pull a fast one and temporarily
839 // parent the listctrl on the STC window and then call SetFocus and
840 // then reparent it back to the popup.
841 lv->SetFocus();
842 lv->Reparent(this);
843 #ifdef __WXMSW__
844 lv->Show();
845 #endif
846 }
847
848
849 // Set position in client coords
850 virtual void DoSetSize(int x, int y,
851 int width, int height,
852 int sizeFlags = wxSIZE_AUTO) {
853 if (x != wxDefaultCoord) {
854 GetParent()->ClientToScreen(&x, NULL);
855 }
856 if (y != wxDefaultCoord) {
857 GetParent()->ClientToScreen(NULL, &y);
858 }
859 wxPopupWindow::DoSetSize(x, y, width, height, sizeFlags);
860 }
861
862 // return position as if it were in client coords
863 virtual void DoGetPosition( int *x, int *y ) const {
864 int sx, sy;
865 wxPopupWindow::DoGetPosition(&sx, &sy);
866 GetParent()->ScreenToClient(&sx, &sy);
867 if (x) *x = sx;
868 if (y) *y = sy;
869 }
870
871
872 bool Destroy() {
873 if ( !wxPendingDelete.Member(this) )
874 wxPendingDelete.Append(this);
875 return true;
876 }
877
878
879 int IconWidth() {
880 wxImageList* il = lv->GetImageList(wxIMAGE_LIST_SMALL);
881 if (il != NULL) {
882 int w, h;
883 il->GetSize(0, w, h);
884 return w;
885 }
886 return 0;
887 }
888
889
890 void SetDoubleClickAction(CallBackAction action, void *data) {
891 doubleClickAction = action;
892 doubleClickActionData = data;
893 }
894
895
896 void OnFocus(wxFocusEvent& event) {
897 GetParent()->SetFocus();
898 event.Skip();
899 }
900
901 void OnSize(wxSizeEvent& event) {
902 // resize the child
903 wxSize sz = GetSize();
904 sz.x -= 2;
905 sz.y -= 2;
906 lv->SetSize(1, 1, sz.x, sz.y);
907 // reset the column widths
908 lv->SetColumnWidth(0, IconWidth()+4);
909 lv->SetColumnWidth(1, sz.x - 2 - lv->GetColumnWidth(0) -
910 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X));
911 event.Skip();
912 }
913
914 void OnActivate(wxListEvent& WXUNUSED(event)) {
915 doubleClickAction(doubleClickActionData);
916 }
917
918 wxListView* GetLB() { return lv; }
919
920 private:
921 DECLARE_EVENT_TABLE()
922
923 };
924
925 BEGIN_EVENT_TABLE(wxSTCListBoxWin, wxPopupWindow)
926 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus)
927 EVT_SIZE ( wxSTCListBoxWin::OnSize)
928 EVT_LIST_ITEM_ACTIVATED(wxID_ANY, wxSTCListBoxWin::OnActivate)
929 END_EVENT_TABLE()
930
931
932
933 #else // !wxUSE_POPUPWIN -----------------------------------
934 #include "wx/frame.h"
935
936 // A normal window to place the wxSTCListBox upon, but make it behave as much
937 // like a wxPopupWindow as possible
938 class wxSTCListBoxWin : public wxFrame {
939 private:
940 wxListView* lv;
941 CallBackAction doubleClickAction;
942 void* doubleClickActionData;
943 public:
944 wxSTCListBoxWin(wxWindow* parent, wxWindowID id, Point location) :
945 wxFrame(parent, id, wxEmptyString, wxPoint(location.x, location.y), wxSize(0,0),
946 wxFRAME_NO_TASKBAR
947 | wxFRAME_FLOAT_ON_PARENT
948 #ifdef __WXMAC__
949 | wxPOPUP_WINDOW
950 | wxNO_BORDER
951 #else
952 | wxSIMPLE_BORDER
953 #endif
954 )
955 {
956
957 lv = new wxSTCListBox(this, id, wxDefaultPosition, wxDefaultSize,
958 wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER | wxNO_BORDER);
959 lv->SetCursor(wxCursor(wxCURSOR_ARROW));
960 lv->InsertColumn(0, wxEmptyString);
961 lv->InsertColumn(1, wxEmptyString);
962
963 // Eventhough we immediately reset the focus to the parent, this helps
964 // things to look right...
965 lv->SetFocus();
966
967 Hide();
968 }
969
970
971 // On OSX and (possibly others) there can still be pending
972 // messages/events for the list control when Scintilla wants to
973 // close it, so do a pending delete of it instead of destroying
974 // immediately.
975 bool Destroy()
976 {
977 #ifdef __WXMAC__
978 // The bottom edge of this window is not getting properly
979 // refreshed upon deletion, so help it out...
980 wxWindow* p = GetParent();
981 wxRect r(GetPosition(), GetSize());
982 r.SetHeight(r.GetHeight()+1);
983 p->Refresh(false, &r);
984 #endif
985 if ( !wxPendingDelete.Member(this) )
986 wxPendingDelete.Append(this);
987 return true;
988 }
989
990
991 int IconWidth()
992 {
993 wxImageList* il = lv->GetImageList(wxIMAGE_LIST_SMALL);
994 if (il != NULL) {
995 int w, h;
996 il->GetSize(0, w, h);
997 return w;
998 }
999 return 0;
1000 }
1001
1002
1003 void SetDoubleClickAction(CallBackAction action, void *data)
1004 {
1005 doubleClickAction = action;
1006 doubleClickActionData = data;
1007 }
1008
1009
1010 void OnFocus(wxFocusEvent& event)
1011 {
1012 ActivateParent();
1013 GetParent()->SetFocus();
1014 event.Skip();
1015 }
1016
1017 void OnSize(wxSizeEvent& event)
1018 {
1019 // resize the child
1020 wxSize sz = GetClientSize();
1021 lv->SetSize(sz);
1022 // reset the column widths
1023 lv->SetColumnWidth(0, IconWidth()+4);
1024 lv->SetColumnWidth(1, sz.x - 2 - lv->GetColumnWidth(0) -
1025 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X));
1026 event.Skip();
1027 }
1028
1029 void ActivateParent()
1030 {
1031 // Although we're a frame, we always want the parent to be active, so
1032 // raise it whenever we get shown, focused, etc.
1033 wxTopLevelWindow *frame = wxDynamicCast(
1034 wxGetTopLevelParent(GetParent()), wxTopLevelWindow);
1035 if (frame)
1036 frame->Raise();
1037 }
1038
1039
1040 virtual void DoSetSize(int x, int y,
1041 int width, int height,
1042 int sizeFlags = wxSIZE_AUTO)
1043 {
1044 // convert coords to screen coords since we're a top-level window
1045 if (x != wxDefaultCoord) {
1046 GetParent()->ClientToScreen(&x, NULL);
1047 }
1048 if (y != wxDefaultCoord) {
1049 GetParent()->ClientToScreen(NULL, &y);
1050 }
1051 wxFrame::DoSetSize(x, y, width, height, sizeFlags);
1052 }
1053
1054 virtual bool Show(bool show = true)
1055 {
1056 bool rv = wxFrame::Show(show);
1057 if (rv && show)
1058 ActivateParent();
1059 #ifdef __WXMAC__
1060 GetParent()->Refresh(false);
1061 #endif
1062 return rv;
1063 }
1064
1065 void OnActivate(wxListEvent& WXUNUSED(event))
1066 {
1067 doubleClickAction(doubleClickActionData);
1068 }
1069
1070 wxListView* GetLB() { return lv; }
1071
1072 private:
1073 DECLARE_EVENT_TABLE()
1074 };
1075
1076
1077 BEGIN_EVENT_TABLE(wxSTCListBoxWin, wxWindow)
1078 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus)
1079 EVT_SIZE ( wxSTCListBoxWin::OnSize)
1080 EVT_LIST_ITEM_ACTIVATED(wxID_ANY, wxSTCListBoxWin::OnActivate)
1081 END_EVENT_TABLE()
1082
1083 #endif // wxUSE_POPUPWIN -----------------------------------
1084
1085
1086 inline wxSTCListBoxWin* GETLBW(WindowID win) {
1087 return ((wxSTCListBoxWin*)win);
1088 }
1089
1090 inline wxListView* GETLB(WindowID win) {
1091 return GETLBW(win)->GetLB();
1092 }
1093
1094 //----------------------------------------------------------------------
1095
1096 class ListBoxImpl : public ListBox {
1097 private:
1098 int lineHeight;
1099 bool unicodeMode;
1100 int desiredVisibleRows;
1101 int aveCharWidth;
1102 size_t maxStrWidth;
1103 Point location; // Caret location at which the list is opened
1104 wxImageList* imgList;
1105 wxArrayInt* imgTypeMap;
1106
1107 public:
1108 ListBoxImpl();
1109 ~ListBoxImpl();
1110
1111 virtual void SetFont(Font &font);
1112 virtual void Create(Window &parent, int ctrlID, Point location_, int lineHeight_, bool unicodeMode_);
1113 virtual void SetAverageCharWidth(int width);
1114 virtual void SetVisibleRows(int rows);
1115 virtual int GetVisibleRows() const;
1116 virtual PRectangle GetDesiredRect();
1117 virtual int CaretFromEdge();
1118 virtual void Clear();
1119 virtual void Append(char *s, int type = -1);
1120 void Append(const wxString& text, int type);
1121 virtual int Length();
1122 virtual void Select(int n);
1123 virtual int GetSelection();
1124 virtual int Find(const char *prefix);
1125 virtual void GetValue(int n, char *value, int len);
1126 virtual void RegisterImage(int type, const char *xpm_data);
1127 virtual void ClearRegisteredImages();
1128 virtual void SetDoubleClickAction(CallBackAction, void *);
1129 virtual void SetList(const char* list, char separator, char typesep);
1130 };
1131
1132
1133 ListBoxImpl::ListBoxImpl()
1134 : lineHeight(10), unicodeMode(false),
1135 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
1136 imgList(NULL), imgTypeMap(NULL)
1137 {
1138 }
1139
1140 ListBoxImpl::~ListBoxImpl() {
1141 if (imgList) {
1142 delete imgList;
1143 imgList = NULL;
1144 }
1145 if (imgTypeMap) {
1146 delete imgTypeMap;
1147 imgTypeMap = NULL;
1148 }
1149 }
1150
1151
1152 void ListBoxImpl::SetFont(Font &font) {
1153 GETLB(id)->SetFont(*((wxFont*)font.GetID()));
1154 }
1155
1156
1157 void ListBoxImpl::Create(Window &parent, int ctrlID, Point location_, int lineHeight_, bool unicodeMode_) {
1158 location = location_;
1159 lineHeight = lineHeight_;
1160 unicodeMode = unicodeMode_;
1161 maxStrWidth = 0;
1162 id = new wxSTCListBoxWin(GETWIN(parent.GetID()), ctrlID, location);
1163 if (imgList != NULL)
1164 GETLB(id)->SetImageList(imgList, wxIMAGE_LIST_SMALL);
1165 }
1166
1167
1168 void ListBoxImpl::SetAverageCharWidth(int width) {
1169 aveCharWidth = width;
1170 }
1171
1172
1173 void ListBoxImpl::SetVisibleRows(int rows) {
1174 desiredVisibleRows = rows;
1175 }
1176
1177
1178 int ListBoxImpl::GetVisibleRows() const {
1179 return desiredVisibleRows;
1180 }
1181
1182 PRectangle ListBoxImpl::GetDesiredRect() {
1183 // wxListCtrl doesn't have a DoGetBestSize, so instead we kept track of
1184 // the max size in Append and calculate it here...
1185 int maxw = maxStrWidth * aveCharWidth;
1186 int maxh ;
1187
1188 // give it a default if there are no lines, and/or add a bit more
1189 if (maxw == 0) maxw = 100;
1190 maxw += aveCharWidth * 3 +
1191 GETLBW(id)->IconWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1192 if (maxw > 350)
1193 maxw = 350;
1194
1195 // estimate a desired height
1196 int count = GETLB(id)->GetItemCount();
1197 if (count) {
1198 wxRect rect;
1199 GETLB(id)->GetItemRect(0, rect);
1200 maxh = count * rect.GetHeight();
1201 if (maxh > 140) // TODO: Use desiredVisibleRows??
1202 maxh = 140;
1203
1204 // Try to make the size an exact multiple of some number of lines
1205 int lines = maxh / rect.GetHeight();
1206 maxh = (lines + 1) * rect.GetHeight() + 2;
1207 }
1208 else
1209 maxh = 100;
1210
1211 PRectangle rc;
1212 rc.top = 0;
1213 rc.left = 0;
1214 rc.right = maxw;
1215 rc.bottom = maxh;
1216 return rc;
1217 }
1218
1219
1220 int ListBoxImpl::CaretFromEdge() {
1221 return 4 + GETLBW(id)->IconWidth();
1222 }
1223
1224
1225 void ListBoxImpl::Clear() {
1226 GETLB(id)->DeleteAllItems();
1227 }
1228
1229
1230 void ListBoxImpl::Append(char *s, int type) {
1231 Append(stc2wx(s), type);
1232 }
1233
1234 void ListBoxImpl::Append(const wxString& text, int type) {
1235 long count = GETLB(id)->GetItemCount();
1236 long itemID = GETLB(id)->InsertItem(count, wxEmptyString);
1237 long idx = -1;
1238 GETLB(id)->SetItem(itemID, 1, text);
1239 maxStrWidth = wxMax(maxStrWidth, text.length());
1240 if (type != -1) {
1241 wxCHECK_RET(imgTypeMap, wxT("Unexpected NULL imgTypeMap"));
1242 idx = imgTypeMap->Item(type);
1243 }
1244 GETLB(id)->SetItemImage(itemID, idx, idx);
1245 }
1246
1247 void ListBoxImpl::SetList(const char* list, char separator, char typesep) {
1248 GETLB(id)->Freeze();
1249 Clear();
1250 wxStringTokenizer tkzr(stc2wx(list), (wxChar)separator);
1251 while ( tkzr.HasMoreTokens() ) {
1252 wxString token = tkzr.GetNextToken();
1253 long type = -1;
1254 int pos = token.Find(typesep);
1255 if (pos != -1) {
1256 token.Mid(pos+1).ToLong(&type);
1257 token.Truncate(pos);
1258 }
1259 Append(token, (int)type);
1260 }
1261 GETLB(id)->Thaw();
1262 }
1263
1264
1265 int ListBoxImpl::Length() {
1266 return GETLB(id)->GetItemCount();
1267 }
1268
1269
1270 void ListBoxImpl::Select(int n) {
1271 bool select = true;
1272 if (n == -1) {
1273 n = 0;
1274 select = false;
1275 }
1276 GETLB(id)->EnsureVisible(n);
1277 GETLB(id)->Select(n, select);
1278 }
1279
1280
1281 int ListBoxImpl::GetSelection() {
1282 return GETLB(id)->GetFirstSelected();
1283 }
1284
1285
1286 int ListBoxImpl::Find(const char *WXUNUSED(prefix)) {
1287 // No longer used
1288 return wxNOT_FOUND;
1289 }
1290
1291
1292 void ListBoxImpl::GetValue(int n, char *value, int len) {
1293 wxListItem item;
1294 item.SetId(n);
1295 item.SetColumn(1);
1296 item.SetMask(wxLIST_MASK_TEXT);
1297 GETLB(id)->GetItem(item);
1298 strncpy(value, wx2stc(item.GetText()), len);
1299 value[len-1] = '\0';
1300 }
1301
1302
1303 void ListBoxImpl::RegisterImage(int type, const char *xpm_data) {
1304 wxMemoryInputStream stream(xpm_data, strlen(xpm_data)+1);
1305 wxImage img(stream, wxBITMAP_TYPE_XPM);
1306 wxBitmap bmp(img);
1307
1308 if (! imgList) {
1309 // assumes all images are the same size
1310 imgList = new wxImageList(bmp.GetWidth(), bmp.GetHeight(), true);
1311 imgTypeMap = new wxArrayInt;
1312 }
1313
1314 int idx = imgList->Add(bmp);
1315
1316 // do we need to extend the mapping array?
1317 wxArrayInt& itm = *imgTypeMap;
1318 if ( itm.GetCount() < (size_t)type+1)
1319 itm.Add(-1, type - itm.GetCount() + 1);
1320
1321 // Add an item that maps type to the image index
1322 itm[type] = idx;
1323 }
1324
1325 void ListBoxImpl::ClearRegisteredImages() {
1326 if (imgList) {
1327 delete imgList;
1328 imgList = NULL;
1329 }
1330 if (imgTypeMap) {
1331 delete imgTypeMap;
1332 imgTypeMap = NULL;
1333 }
1334 if (id)
1335 GETLB(id)->SetImageList(NULL, wxIMAGE_LIST_SMALL);
1336 }
1337
1338
1339 void ListBoxImpl::SetDoubleClickAction(CallBackAction action, void *data) {
1340 GETLBW(id)->SetDoubleClickAction(action, data);
1341 }
1342
1343
1344 ListBox::ListBox() {
1345 }
1346
1347 ListBox::~ListBox() {
1348 }
1349
1350 ListBox *ListBox::Allocate() {
1351 return new ListBoxImpl();
1352 }
1353
1354 //----------------------------------------------------------------------
1355
1356 Menu::Menu() : id(0) {
1357 }
1358
1359 void Menu::CreatePopUp() {
1360 Destroy();
1361 id = new wxMenu();
1362 }
1363
1364 void Menu::Destroy() {
1365 if (id)
1366 delete (wxMenu*)id;
1367 id = 0;
1368 }
1369
1370 void Menu::Show(Point pt, Window &w) {
1371 GETWIN(w.GetID())->PopupMenu((wxMenu*)id, pt.x - 4, pt.y);
1372 Destroy();
1373 }
1374
1375 //----------------------------------------------------------------------
1376
1377 DynamicLibrary *DynamicLibrary::Load(const char *WXUNUSED(modulePath)) {
1378 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1379 return NULL;
1380 }
1381
1382 //----------------------------------------------------------------------
1383
1384 ColourDesired Platform::Chrome() {
1385 wxColour c;
1386 c = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
1387 return ColourDesired(c.Red(), c.Green(), c.Blue());
1388 }
1389
1390 ColourDesired Platform::ChromeHighlight() {
1391 wxColour c;
1392 c = wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT);
1393 return ColourDesired(c.Red(), c.Green(), c.Blue());
1394 }
1395
1396 const char *Platform::DefaultFont() {
1397 static char buf[128];
1398 strcpy(buf, wxNORMAL_FONT->GetFaceName().mbc_str());
1399 return buf;
1400 }
1401
1402 int Platform::DefaultFontSize() {
1403 return wxNORMAL_FONT->GetPointSize();
1404 }
1405
1406 unsigned int Platform::DoubleClickTime() {
1407 return 500; // **** ::GetDoubleClickTime();
1408 }
1409
1410 bool Platform::MouseButtonBounce() {
1411 return false;
1412 }
1413
1414 bool Platform::IsKeyDown(int WXUNUSED(key)) {
1415 return false; // I don't think we'll need this.
1416 }
1417
1418 long Platform::SendScintilla(WindowID w,
1419 unsigned int msg,
1420 unsigned long wParam,
1421 long lParam) {
1422
1423 wxStyledTextCtrl* stc = (wxStyledTextCtrl*)w;
1424 return stc->SendMsg(msg, wParam, lParam);
1425 }
1426
1427 long Platform::SendScintillaPointer(WindowID w,
1428 unsigned int msg,
1429 unsigned long wParam,
1430 void *lParam) {
1431
1432 wxStyledTextCtrl* stc = (wxStyledTextCtrl*)w;
1433 return stc->SendMsg(msg, wParam, (long)lParam);
1434 }
1435
1436
1437 // These are utility functions not really tied to a platform
1438
1439 int Platform::Minimum(int a, int b) {
1440 if (a < b)
1441 return a;
1442 else
1443 return b;
1444 }
1445
1446 int Platform::Maximum(int a, int b) {
1447 if (a > b)
1448 return a;
1449 else
1450 return b;
1451 }
1452
1453 //#define TRACE
1454
1455 void Platform::DebugDisplay(const char *s) {
1456 #ifdef TRACE
1457 wxLogDebug(stc2wx(s));
1458 #else
1459 wxUnusedVar(s);
1460 #endif
1461 }
1462
1463 void Platform::DebugPrintf(const char *format, ...) {
1464 #ifdef TRACE
1465 char buffer[2000];
1466 va_list pArguments;
1467 va_start(pArguments, format);
1468 vsprintf(buffer,format,pArguments);
1469 va_end(pArguments);
1470 Platform::DebugDisplay(buffer);
1471 #else
1472 wxUnusedVar(format);
1473 #endif
1474 }
1475
1476
1477 static bool assertionPopUps = true;
1478
1479 bool Platform::ShowAssertionPopUps(bool assertionPopUps_) {
1480 bool ret = assertionPopUps;
1481 assertionPopUps = assertionPopUps_;
1482 return ret;
1483 }
1484
1485 void Platform::Assert(const char *c, const char *file, int line) {
1486 #ifdef TRACE
1487 char buffer[2000];
1488 sprintf(buffer, "Assertion [%s] failed at %s %d", c, file, line);
1489 if (assertionPopUps) {
1490 /*int idButton = */
1491 wxMessageBox(stc2wx(buffer),
1492 wxT("Assertion failure"),
1493 wxICON_HAND | wxOK);
1494 } else {
1495 strcat(buffer, "\r\n");
1496 Platform::DebugDisplay(buffer);
1497 abort();
1498 }
1499 #else
1500 wxUnusedVar(c);
1501 wxUnusedVar(file);
1502 wxUnusedVar(line);
1503 #endif
1504 }
1505
1506
1507 int Platform::Clamp(int val, int minVal, int maxVal) {
1508 if (val > maxVal)
1509 val = maxVal;
1510 if (val < minVal)
1511 val = minVal;
1512 return val;
1513 }
1514
1515
1516 bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage), char WXUNUSED(ch)) {
1517 return false;
1518 }
1519
1520 int Platform::DBCSCharLength(int WXUNUSED(codePage), const char *WXUNUSED(s)) {
1521 return 1;
1522 }
1523
1524 int Platform::DBCSCharMaxLength() {
1525 return 1;
1526 }
1527
1528
1529 //----------------------------------------------------------------------
1530
1531 ElapsedTime::ElapsedTime() {
1532 wxLongLong localTime = wxGetLocalTimeMillis();
1533 littleBit = localTime.GetLo();
1534 bigBit = localTime.GetHi();
1535 }
1536
1537 double ElapsedTime::Duration(bool reset) {
1538 wxLongLong prevTime(bigBit, littleBit);
1539 wxLongLong localTime = wxGetLocalTimeMillis();
1540 if(reset) {
1541 littleBit = localTime.GetLo();
1542 bigBit = localTime.GetHi();
1543 }
1544 wxLongLong duration = localTime - prevTime;
1545 double result = duration.ToDouble();
1546 result /= 1000.0;
1547 return result;
1548 }
1549
1550
1551 //----------------------------------------------------------------------
1552
1553 #if wxUSE_UNICODE
1554
1555 #include "UniConversion.h"
1556
1557 // Convert using Scintilla's functions instead of wx's, Scintilla's are more
1558 // forgiving and won't assert...
1559
1560 wxString stc2wx(const char* str, size_t len)
1561 {
1562 if (!len)
1563 return wxEmptyString;
1564
1565 size_t wclen = UTF16Length(str, len);
1566 wxWCharBuffer buffer(wclen+1);
1567
1568 size_t actualLen = UTF16FromUTF8(str, len, buffer.data(), wclen+1);
1569 return wxString(buffer.data(), actualLen);
1570 }
1571
1572
1573
1574 wxString stc2wx(const char* str)
1575 {
1576 return stc2wx(str, strlen(str));
1577 }
1578
1579
1580 const wxWX2MBbuf wx2stc(const wxString& str)
1581 {
1582 const wchar_t* wcstr = str.c_str();
1583 size_t wclen = str.length();
1584 size_t len = UTF8Length(wcstr, wclen);
1585
1586 wxCharBuffer buffer(len+1);
1587 UTF8FromUTF16(wcstr, wclen, buffer.data(), len);
1588
1589 // TODO check NULL termination!!
1590
1591 return buffer;
1592 }
1593
1594 #endif
1595
1596 #endif // wxUSE_STC