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