]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/stc/PlatWX.cpp
Aliases for [G|S]etCaretLineBack
[wxWidgets.git] / contrib / src / stc / PlatWX.cpp
CommitLineData
9ce192d4 1// Scintilla source code edit control
be5a51fb 2// PlatWX.cxx - implementation of platform facilities on wxWidgets
9ce192d4
RD
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
f6bcfd97 7#include <ctype.h>
9ce192d4 8
d6655166
WS
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"
1e9bafca 15#include "wx/tokenzr.h"
8b3d52fc
RD
16
17#ifdef wxHAVE_RAW_BITMAP
6a93571d 18#include "wx/rawbmp.h"
8b3d52fc 19#endif
1a2fb4cd 20
9ce192d4 21#include "Platform.h"
1a2fb4cd 22#include "PlatWX.h"
9ce192d4
RD
23#include "wx/stc/stc.h"
24
f97d84a6 25
f9ee2e27 26
9ce192d4 27Point Point::FromLong(long lpoint) {
f6bcfd97 28 return Point(lpoint & 0xFFFF, lpoint >> 16);
9ce192d4
RD
29}
30
31wxRect wxRectFromPRectangle(PRectangle prc) {
d13ea3aa
RD
32 wxRect r(prc.left, prc.top,
33 prc.Width(), prc.Height());
34 return r;
9ce192d4
RD
35}
36
37PRectangle PRectangleFromwxRect(wxRect rc) {
bec17edf 38 return PRectangle(rc.GetLeft(), rc.GetTop(),
04ebdf40 39 rc.GetRight()+1, rc.GetBottom()+1);
9ce192d4
RD
40}
41
1a2fb4cd
RD
42wxColour wxColourFromCA(const ColourAllocated& ca) {
43 ColourDesired cd(ca.AsLong());
c8b75e94
WS
44 return wxColour((unsigned char)cd.GetRed(),
45 (unsigned char)cd.GetGreen(),
46 (unsigned char)cd.GetBlue());
9ce192d4
RD
47}
48
1a2fb4cd 49//----------------------------------------------------------------------
9ce192d4
RD
50
51Palette::Palette() {
52 used = 0;
53 allowRealization = false;
b8193d80
RD
54 size = 100;
55 entries = new ColourPair[size];
9ce192d4
RD
56}
57
58Palette::~Palette() {
59 Release();
b8193d80
RD
60 delete [] entries;
61 entries = 0;
9ce192d4
RD
62}
63
64void Palette::Release() {
65 used = 0;
b8193d80
RD
66 delete [] entries;
67 size = 100;
68 entries = new ColourPair[size];
9ce192d4
RD
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.
74void 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
b8193d80
RD
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;
9ce192d4 90 }
b8193d80
RD
91
92 entries[used].desired = cp.desired;
93 entries[used].allocated.Set(cp.desired.AsLong());
94 used++;
9ce192d4
RD
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 }
1a2fb4cd 102 cp.allocated.Set(cp.desired.AsLong());
9ce192d4
RD
103 }
104}
105
106void Palette::Allocate(Window &) {
107 if (allowRealization) {
108 }
109}
110
111
1a2fb4cd
RD
112//----------------------------------------------------------------------
113
9ce192d4
RD
114Font::Font() {
115 id = 0;
116 ascent = 0;
117}
118
119Font::~Font() {
120}
121
d1558f3d 122void Font::Create(const char *faceName, int characterSet, int size, bool bold, bool italic, bool extraFontFlag) {
65ec6247 123
9ce192d4 124 Release();
1a2fb4cd 125
3727c043
RD
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);
d35ad26e 131
10ef30eb
RD
132 wxFontEncodingArray ea = wxEncodingConverter::GetPlatformEquivalents(encoding);
133 if (ea.GetCount())
134 encoding = ea[0];
1a2fb4cd 135
d1558f3d 136 wxFont* font = new wxFont(size,
9ce192d4
RD
137 wxDEFAULT,
138 italic ? wxITALIC : wxNORMAL,
139 bold ? wxBOLD : wxNORMAL,
140 false,
0c5b83b0 141 stc2wx(faceName),
1a2fb4cd 142 encoding);
d1558f3d
RD
143 font->SetNoAntiAliasing(!extraFontFlag);
144 id = font;
9ce192d4
RD
145}
146
147
148void Font::Release() {
149 if (id)
1a2fb4cd 150 delete (wxFont*)id;
9ce192d4
RD
151 id = 0;
152}
153
1a2fb4cd
RD
154//----------------------------------------------------------------------
155
156class SurfaceImpl : public Surface {
157private:
158 wxDC* hdc;
159 bool hdcOwned;
160 wxBitmap* bitmap;
161 int x;
162 int y;
163 bool unicodeMode;
9ce192d4 164
1a2fb4cd
RD
165public:
166 SurfaceImpl();
167 ~SurfaceImpl();
168
9e730a78
RD
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);
b8193d80
RD
185 virtual void AlphaRectangle(PRectangle rc, int cornerSize, ColourAllocated fill, int alphaFill,
186 ColourAllocated outline, int alphaOutline, int flags);
9e730a78
RD
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);
1a2fb4cd
RD
209
210 void BrushColour(ColourAllocated back);
211 void SetFont(Font &font_);
212};
213
214
215
216SurfaceImpl::SurfaceImpl() :
9ce192d4 217 hdc(0), hdcOwned(0), bitmap(0),
1a2fb4cd
RD
218 x(0), y(0), unicodeMode(0)
219{}
9ce192d4 220
1a2fb4cd 221SurfaceImpl::~SurfaceImpl() {
9ce192d4
RD
222 Release();
223}
224
9e730a78 225void SurfaceImpl::Init(WindowID wid) {
81b32ce5 226#if 0
9ce192d4
RD
227 Release();
228 hdc = new wxMemoryDC();
229 hdcOwned = true;
81b32ce5 230#else
09bb2551 231 // On Mac and GTK the DC is not really valid until it has a bitmap
81b32ce5
RD
232 // selected into it. So instead of just creating the DC with no bitmap,
233 // go ahead and give it one.
9e730a78 234 InitPixMap(1,1,NULL,wid);
0f713d48 235#endif
9ce192d4
RD
236}
237
9e730a78 238void SurfaceImpl::Init(SurfaceID hdc_, WindowID) {
9ce192d4 239 Release();
1a2fb4cd 240 hdc = (wxDC*)hdc_;
9ce192d4
RD
241}
242
88a8b04e 243void SurfaceImpl::InitPixMap(int width, int height, Surface *WXUNUSED(surface_), WindowID) {
9ce192d4 244 Release();
1a2fb4cd 245 hdc = new wxMemoryDC();
9ce192d4 246 hdcOwned = true;
2beb01db
RD
247 if (width < 1) width = 1;
248 if (height < 1) height = 1;
21156596 249 bitmap = new wxBitmap(width, height);
9ce192d4 250 ((wxMemoryDC*)hdc)->SelectObject(*bitmap);
9ce192d4
RD
251}
252
9e730a78
RD
253
254void 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
268bool SurfaceImpl::Initialised() {
269 return hdc != 0;
270}
271
272
1a2fb4cd
RD
273void SurfaceImpl::PenColour(ColourAllocated fore) {
274 hdc->SetPen(wxPen(wxColourFromCA(fore), 1, wxSOLID));
9ce192d4
RD
275}
276
1a2fb4cd
RD
277void SurfaceImpl::BrushColour(ColourAllocated back) {
278 hdc->SetBrush(wxBrush(wxColourFromCA(back), wxSOLID));
9ce192d4
RD
279}
280
1a2fb4cd 281void SurfaceImpl::SetFont(Font &font_) {
21156596 282 if (font_.GetID()) {
1a2fb4cd 283 hdc->SetFont(*((wxFont*)font_.GetID()));
f6bcfd97 284 }
9ce192d4
RD
285}
286
1a2fb4cd 287int SurfaceImpl::LogPixelsY() {
9ce192d4
RD
288 return hdc->GetPPI().y;
289}
290
1a2fb4cd 291int SurfaceImpl::DeviceHeightFont(int points) {
9968ba85 292 return points;
f6bcfd97
BP
293}
294
1a2fb4cd 295void SurfaceImpl::MoveTo(int x_, int y_) {
9ce192d4
RD
296 x = x_;
297 y = y_;
298}
299
1a2fb4cd 300void SurfaceImpl::LineTo(int x_, int y_) {
9ce192d4
RD
301 hdc->DrawLine(x,y, x_,y_);
302 x = x_;
303 y = y_;
304}
305
1a2fb4cd 306void SurfaceImpl::Polygon(Point *pts, int npts, ColourAllocated fore, ColourAllocated back) {
9ce192d4 307 PenColour(fore);
1a2fb4cd 308 BrushColour(back);
9ce192d4
RD
309 hdc->DrawPolygon(npts, (wxPoint*)pts);
310}
311
1a2fb4cd 312void SurfaceImpl::RectangleDraw(PRectangle rc, ColourAllocated fore, ColourAllocated back) {
9ce192d4 313 PenColour(fore);
1a2fb4cd 314 BrushColour(back);
9ce192d4
RD
315 hdc->DrawRectangle(wxRectFromPRectangle(rc));
316}
317
1a2fb4cd
RD
318void SurfaceImpl::FillRectangle(PRectangle rc, ColourAllocated back) {
319 BrushColour(back);
9ce192d4
RD
320 hdc->SetPen(*wxTRANSPARENT_PEN);
321 hdc->DrawRectangle(wxRectFromPRectangle(rc));
322}
323
1a2fb4cd 324void SurfaceImpl::FillRectangle(PRectangle rc, Surface &surfacePattern) {
9ce192d4 325 wxBrush br;
1a2fb4cd
RD
326 if (((SurfaceImpl&)surfacePattern).bitmap)
327 br = wxBrush(*((SurfaceImpl&)surfacePattern).bitmap);
9ce192d4
RD
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
1a2fb4cd 335void SurfaceImpl::RoundedRectangle(PRectangle rc, ColourAllocated fore, ColourAllocated back) {
9ce192d4 336 PenColour(fore);
1a2fb4cd 337 BrushColour(back);
f6bcfd97 338 hdc->DrawRoundedRectangle(wxRectFromPRectangle(rc), 4);
9ce192d4
RD
339}
340
6a93571d
RD
341#ifdef __WXMSW__
342#define wxPy_premultiply(p, a) ((p) * (a) / 0xff)
343#else
344#define wxPy_premultiply(p, a) (p)
345#endif
346
b8193d80
RD
347void SurfaceImpl::AlphaRectangle(PRectangle rc, int cornerSize,
348 ColourAllocated fill, int alphaFill,
6a93571d
RD
349 ColourAllocated outline, int alphaOutline,
350 int /*flags*/) {
8b3d52fc 351#ifdef wxHAVE_RAW_BITMAP
6a93571d
RD
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 }
b8193d80 393
6a93571d
RD
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);
8b3d52fc
RD
409
410#else
411 RectangleDraw(rc, outline, fill);
412#endif
b8193d80
RD
413}
414
1a2fb4cd 415void SurfaceImpl::Ellipse(PRectangle rc, ColourAllocated fore, ColourAllocated back) {
9ce192d4 416 PenColour(fore);
1a2fb4cd 417 BrushColour(back);
9ce192d4
RD
418 hdc->DrawEllipse(wxRectFromPRectangle(rc));
419}
420
1a2fb4cd 421void SurfaceImpl::Copy(PRectangle rc, Point from, Surface &surfaceSource) {
f6bcfd97
BP
422 wxRect r = wxRectFromPRectangle(rc);
423 hdc->Blit(r.x, r.y, r.width, r.height,
1a2fb4cd
RD
424 ((SurfaceImpl&)surfaceSource).hdc,
425 from.x, from.y, wxCOPY);
9ce192d4
RD
426}
427
1a2fb4cd
RD
428void SurfaceImpl::DrawTextNoClip(PRectangle rc, Font &font, int ybase,
429 const char *s, int len,
430 ColourAllocated fore, ColourAllocated back) {
9ce192d4 431 SetFont(font);
1a2fb4cd
RD
432 hdc->SetTextForeground(wxColourFromCA(fore));
433 hdc->SetTextBackground(wxColourFromCA(back));
3d7a4fe8 434 FillRectangle(rc, back);
9ce192d4
RD
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...
d13ea3aa 438 hdc->DrawText(stc2wx(s, len), rc.left, ybase - font.ascent);
9ce192d4
RD
439}
440
1a2fb4cd
RD
441void SurfaceImpl::DrawTextClipped(PRectangle rc, Font &font, int ybase,
442 const char *s, int len,
443 ColourAllocated fore, ColourAllocated back) {
9ce192d4 444 SetFont(font);
1a2fb4cd
RD
445 hdc->SetTextForeground(wxColourFromCA(fore));
446 hdc->SetTextBackground(wxColourFromCA(back));
3d7a4fe8 447 FillRectangle(rc, back);
9ce192d4
RD
448 hdc->SetClippingRegion(wxRectFromPRectangle(rc));
449
450 // see comments above
d13ea3aa 451 hdc->DrawText(stc2wx(s, len), rc.left, ybase - font.ascent);
3d7a4fe8 452 hdc->DestroyClippingRegion();
9ce192d4
RD
453}
454
9e730a78
RD
455
456void SurfaceImpl::DrawTextTransparent(PRectangle rc, Font &font, int ybase,
457 const char *s, int len,
458 ColourAllocated fore) {
459
9ce192d4 460 SetFont(font);
9e730a78
RD
461 hdc->SetTextForeground(wxColourFromCA(fore));
462 hdc->SetBackgroundMode(wxTRANSPARENT);
1a2fb4cd 463
9e730a78
RD
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...
d13ea3aa 466 hdc->DrawText(stc2wx(s, len), rc.left, ybase - font.ascent);
9e730a78
RD
467
468 hdc->SetBackgroundMode(wxSOLID);
9ce192d4
RD
469}
470
1a2fb4cd 471
10ef30eb 472void SurfaceImpl::MeasureWidths(Font &font, const char *s, int len, int *positions) {
9e730a78 473
d1558f3d
RD
474 wxString str = stc2wx(s, len);
475 wxArrayInt tpos;
10ef30eb 476
d1558f3d 477 SetFont(font);
9e730a78 478
d1558f3d 479 hdc->GetPartialTextExtents(str, tpos);
10ef30eb
RD
480
481#if wxUSE_UNICODE
482 // Map the widths for UCS-2 characters back to the UTF-8 input string
9e730a78
RD
483 // NOTE: I don't think this is right for when sizeof(wxChar) > 2, ie wxGTK2
484 // so figure it out and fix it!
d1558f3d 485 size_t i = 0;
10ef30eb 486 size_t ui = 0;
d99859e4 487 while ((int)i < len) {
10ef30eb
RD
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
f3ee029e
MB
503#if wxUSE_STL
504 std::copy(tpos.begin(), tpos.end(), positions);
505#else
d1558f3d 506 memcpy(positions, tpos.begin(), len * sizeof(int));
10ef30eb 507#endif
f3ee029e 508#endif
9ce192d4
RD
509}
510
10ef30eb 511
9e730a78
RD
512int 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
1a2fb4cd 522int SurfaceImpl::WidthChar(Font &font, char ch) {
9ce192d4
RD
523 SetFont(font);
524 int w;
525 int h;
10ef30eb
RD
526 char s[2] = { ch, 0 };
527
0c5b83b0 528 hdc->GetTextExtent(stc2wx(s, 1), &w, &h);
9ce192d4
RD
529 return w;
530}
531
1a2fb4cd 532#define EXTENT_TEST wxT(" `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
9ce192d4 533
1a2fb4cd 534int SurfaceImpl::Ascent(Font &font) {
9ce192d4
RD
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
1a2fb4cd 542int SurfaceImpl::Descent(Font &font) {
9ce192d4
RD
543 SetFont(font);
544 int w, h, d, e;
545 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
546 return d;
547}
548
88a8b04e 549int SurfaceImpl::InternalLeading(Font &WXUNUSED(font)) {
9ce192d4
RD
550 return 0;
551}
552
1a2fb4cd 553int SurfaceImpl::ExternalLeading(Font &font) {
9ce192d4
RD
554 SetFont(font);
555 int w, h, d, e;
556 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
557 return e;
558}
559
1a2fb4cd 560int SurfaceImpl::Height(Font &font) {
9ce192d4 561 SetFont(font);
d13ea3aa 562 return hdc->GetCharHeight() + 1;
9ce192d4
RD
563}
564
1a2fb4cd 565int SurfaceImpl::AverageCharWidth(Font &font) {
9ce192d4
RD
566 SetFont(font);
567 return hdc->GetCharWidth();
568}
569
88a8b04e 570int SurfaceImpl::SetPalette(Palette *WXUNUSED(pal), bool WXUNUSED(inBackGround)) {
65ec6247 571 return 0;
9ce192d4
RD
572}
573
1a2fb4cd 574void SurfaceImpl::SetClip(PRectangle rc) {
9ce192d4
RD
575 hdc->SetClippingRegion(wxRectFromPRectangle(rc));
576}
577
1a2fb4cd 578void SurfaceImpl::FlushCachedState() {
f6bcfd97 579}
9ce192d4 580
1a2fb4cd 581void SurfaceImpl::SetUnicodeMode(bool unicodeMode_) {
1a2fb4cd
RD
582 unicodeMode=unicodeMode_;
583}
584
88a8b04e 585void SurfaceImpl::SetDBCSMode(int WXUNUSED(codePage)) {
9e730a78
RD
586 // dbcsMode = codePage == SC_CP_DBCS;
587}
588
589
1a2fb4cd
RD
590Surface *Surface::Allocate() {
591 return new SurfaceImpl;
592}
593
594
595//----------------------------------------------------------------------
596
597
598inline wxWindow* GETWIN(WindowID id) { return (wxWindow*)id; }
599
9ce192d4
RD
600Window::~Window() {
601}
602
603void Window::Destroy() {
9e730a78 604 if (id) {
7e126a07 605 Show(false);
1a2fb4cd 606 GETWIN(id)->Destroy();
9e730a78 607 }
9ce192d4
RD
608 id = 0;
609}
610
611bool Window::HasFocus() {
1a2fb4cd 612 return wxWindow::FindFocus() == GETWIN(id);
9ce192d4
RD
613}
614
615PRectangle Window::GetPosition() {
9e730a78 616 if (! id) return PRectangle();
1a2fb4cd 617 wxRect rc(GETWIN(id)->GetPosition(), GETWIN(id)->GetSize());
9ce192d4
RD
618 return PRectangleFromwxRect(rc);
619}
620
621void Window::SetPosition(PRectangle rc) {
f6bcfd97 622 wxRect r = wxRectFromPRectangle(rc);
1a2fb4cd 623 GETWIN(id)->SetSize(r);
9ce192d4
RD
624}
625
626void Window::SetPositionRelative(PRectangle rc, Window) {
627 SetPosition(rc); // ????
628}
629
630PRectangle Window::GetClientPosition() {
9e730a78 631 if (! id) return PRectangle();
1a2fb4cd 632 wxSize sz = GETWIN(id)->GetClientSize();
21156596 633 return PRectangle(0, 0, sz.x, sz.y);
9ce192d4
RD
634}
635
636void Window::Show(bool show) {
1a2fb4cd 637 GETWIN(id)->Show(show);
9ce192d4
RD
638}
639
640void Window::InvalidateAll() {
1a2fb4cd 641 GETWIN(id)->Refresh(false);
9ce192d4
RD
642}
643
644void Window::InvalidateRectangle(PRectangle rc) {
f6bcfd97 645 wxRect r = wxRectFromPRectangle(rc);
1a2fb4cd 646 GETWIN(id)->Refresh(false, &r);
9ce192d4
RD
647}
648
649void Window::SetFont(Font &font) {
1a2fb4cd 650 GETWIN(id)->SetFont(*((wxFont*)font.GetID()));
9ce192d4
RD
651}
652
653void 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:
15dadf31 676 cursorId = wxCURSOR_RIGHT_ARROW;
9ce192d4 677 break;
9e730a78
RD
678 case cursorHand:
679 cursorId = wxCURSOR_HAND;
1e545382 680 break;
9ce192d4
RD
681 default:
682 cursorId = wxCURSOR_ARROW;
683 break;
684 }
9f79d14b
CE
685#ifdef __WXMOTIF__
686 wxCursor wc = wxStockCursor(cursorId) ;
687#else
688 wxCursor wc = wxCursor(cursorId) ;
689#endif
5e99793e
VZ
690 if(curs != cursorLast)
691 {
692 GETWIN(id)->SetCursor(wc);
693 cursorLast = curs;
694 }
9ce192d4
RD
695}
696
697
698void Window::SetTitle(const char *s) {
fd39aa23 699 GETWIN(id)->SetLabel(stc2wx(s));
9ce192d4
RD
700}
701
702
769a9cb2
RD
703//----------------------------------------------------------------------
704// Helper classes for ListBox
705
267484bc 706
b0d0494f 707// This is a simple subclass of wxListView that just resets focus to the
9e730a78
RD
708// parent when it gets it.
709class wxSTCListBox : public wxListView {
451c5cc7 710public:
9e730a78
RD
711 wxSTCListBox(wxWindow* parent, wxWindowID id,
712 const wxPoint& pos, const wxSize& size,
713 long style)
382fe640
RD
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 }
451c5cc7 721
7e126a07 722
451c5cc7
RD
723 void OnFocus(wxFocusEvent& event) {
724 GetParent()->SetFocus();
725 event.Skip();
726 }
727
5f9eb69c 728 void OnKillFocus(wxFocusEvent& WXUNUSED(event)) {
b0d0494f
RD
729 // Do nothing. Prevents base class from resetting the colors...
730 }
7e126a07 731
719ee9c3
RD
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 }
b0d0494f 742
719ee9c3
RD
743 // And we need to force the focus back when being destroyed
744 ~wxSTCListBox() {
745 GetGrandParent()->SetFocus();
7e126a07
WS
746 }
747#endif
748
451c5cc7
RD
749private:
750 DECLARE_EVENT_TABLE()
751};
752
9e730a78
RD
753BEGIN_EVENT_TABLE(wxSTCListBox, wxListView)
754 EVT_SET_FOCUS( wxSTCListBox::OnFocus)
b0d0494f 755 EVT_KILL_FOCUS(wxSTCListBox::OnKillFocus)
719ee9c3
RD
756#ifdef __WXMAC__
757 EVT_KEY_DOWN( wxSTCListBox::OnKeyDown)
758 EVT_CHAR( wxSTCListBox::OnChar)
759#endif
451c5cc7
RD
760END_EVENT_TABLE()
761
762
763
d35ad26e 764#if wxUSE_POPUPWIN //-----------------------------------
382fe640
RD
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...
d35ad26e
WS
774//
775
776// A popup window to place the wxSTCListBox upon
382fe640
RD
777class wxSTCListBoxWin : public wxPopupWindow
778{
779private:
780 wxListView* lv;
781 CallBackAction doubleClickAction;
782 void* doubleClickActionData;
783public:
d35ad26e 784 wxSTCListBoxWin(wxWindow* parent, wxWindowID id, Point WXUNUSED(location)) :
382fe640
RD
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
d35ad26e 800 // then reparent it back to the popup.
382fe640
RD
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
880private:
881 DECLARE_EVENT_TABLE()
882
883};
884
885BEGIN_EVENT_TABLE(wxSTCListBoxWin, wxPopupWindow)
886 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus)
887 EVT_SIZE ( wxSTCListBoxWin::OnSize)
888 EVT_LIST_ITEM_ACTIVATED(wxID_ANY, wxSTCListBoxWin::OnActivate)
889END_EVENT_TABLE()
890
d35ad26e 891
382fe640
RD
892
893#else // wxUSE_POPUPWIN -----------------------------------
267484bc 894
382fe640 895// A normal window to place the wxSTCListBox upon.
9e730a78
RD
896class wxSTCListBoxWin : public wxWindow {
897private:
898 wxListView* lv;
899 CallBackAction doubleClickAction;
900 void* doubleClickActionData;
f97d84a6 901public:
1e9bafca
RD
902 wxSTCListBoxWin(wxWindow* parent, wxWindowID id, Point location) :
903 wxWindow(parent, id, wxPoint(location.x, location.y), wxSize(0,0), wxSIMPLE_BORDER )
9e730a78
RD
904 {
905
9e730a78
RD
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);
b0d0494f
RD
911
912 // Eventhough we immediately reset the focus to the parent, this helps
913 // things to look right...
914 lv->SetFocus();
915
6612393c
RD
916 Hide();
917 }
918
7e126a07 919
a4f3565e
RD
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() {
d13ea3aa 925#ifdef __WXMAC__
719ee9c3 926 // The bottom edge of this window is not getting properly
d13ea3aa
RD
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
a4f3565e
RD
933 if ( !wxPendingDelete.Member(this) )
934 wxPendingDelete.Append(this);
7e126a07 935 return true;
f97d84a6
RD
936 }
937
7e126a07 938
9e730a78
RD
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 }
f97d84a6 948
769a9cb2 949
9e730a78
RD
950 void SetDoubleClickAction(CallBackAction action, void *data) {
951 doubleClickAction = action;
952 doubleClickActionData = data;
953 }
451c5cc7 954
769a9cb2 955
9e730a78
RD
956 void OnFocus(wxFocusEvent& event) {
957 GetParent()->SetFocus();
958 event.Skip();
959 }
769a9cb2
RD
960
961 void OnSize(wxSizeEvent& event) {
d13ea3aa 962 // resize the child
9e730a78 963 wxSize sz = GetClientSize();
d13ea3aa 964 lv->SetSize(sz);
9e730a78
RD
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();
769a9cb2 970 }
769a9cb2 971
719493e1
RD
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
d35ad26e 979
5f9eb69c 980 void OnActivate(wxListEvent& WXUNUSED(event)) {
9e730a78 981 doubleClickAction(doubleClickActionData);
769a9cb2 982 }
9e730a78
RD
983
984 wxListView* GetLB() { return lv; }
769a9cb2
RD
985
986private:
769a9cb2
RD
987 DECLARE_EVENT_TABLE()
988};
989
9e730a78
RD
990
991BEGIN_EVENT_TABLE(wxSTCListBoxWin, wxWindow)
7e126a07
WS
992 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus)
993 EVT_SIZE ( wxSTCListBoxWin::OnSize)
994 EVT_LIST_ITEM_ACTIVATED(wxID_ANY, wxSTCListBoxWin::OnActivate)
769a9cb2 995END_EVENT_TABLE()
769a9cb2 996
382fe640 997#endif // wxUSE_POPUPWIN -----------------------------------
9e730a78
RD
998
999
1000inline wxSTCListBoxWin* GETLBW(WindowID win) {
1001 return ((wxSTCListBoxWin*)win);
1002}
1003
1004inline wxListView* GETLB(WindowID win) {
1005 return GETLBW(win)->GetLB();
1a2fb4cd 1006}
769a9cb2
RD
1007
1008//----------------------------------------------------------------------
1009
9e730a78
RD
1010class ListBoxImpl : public ListBox {
1011private:
1012 int lineHeight;
1013 bool unicodeMode;
1014 int desiredVisibleRows;
1015 int aveCharWidth;
80cac95e 1016 size_t maxStrWidth;
1e9bafca 1017 Point location; // Caret location at which the list is opened
9e730a78
RD
1018 wxImageList* imgList;
1019 wxArrayInt* imgTypeMap;
1020
1021public:
1022 ListBoxImpl();
1023 ~ListBoxImpl();
1024
1025 virtual void SetFont(Font &font);
1e9bafca 1026 virtual void Create(Window &parent, int ctrlID, Point location_, int lineHeight_, bool unicodeMode_);
9e730a78
RD
1027 virtual void SetAverageCharWidth(int width);
1028 virtual void SetVisibleRows(int rows);
1e9bafca 1029 virtual int GetVisibleRows() const;
9e730a78
RD
1030 virtual PRectangle GetDesiredRect();
1031 virtual int CaretFromEdge();
1032 virtual void Clear();
1033 virtual void Append(char *s, int type = -1);
1e9bafca 1034 void Append(const wxString& text, int type);
9e730a78
RD
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);
9e730a78
RD
1040 virtual void RegisterImage(int type, const char *xpm_data);
1041 virtual void ClearRegisteredImages();
1042 virtual void SetDoubleClickAction(CallBackAction, void *);
1e9bafca 1043 virtual void SetList(const char* list, char separator, char typesep);
9e730a78
RD
1044};
1045
1046
1047ListBoxImpl::ListBoxImpl()
1048 : lineHeight(10), unicodeMode(false),
1049 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
1050 imgList(NULL), imgTypeMap(NULL)
1051{
9ce192d4
RD
1052}
1053
9e730a78
RD
1054ListBoxImpl::~ListBoxImpl() {
1055 if (imgList) {
1056 delete imgList;
1057 imgList = NULL;
1058 }
1059 if (imgTypeMap) {
1060 delete imgTypeMap;
1061 imgTypeMap = NULL;
1062 }
9ce192d4
RD
1063}
1064
9e730a78
RD
1065
1066void ListBoxImpl::SetFont(Font &font) {
1067 GETLB(id)->SetFont(*((wxFont*)font.GetID()));
1068}
1069
1070
1e9bafca
RD
1071void ListBoxImpl::Create(Window &parent, int ctrlID, Point location_, int lineHeight_, bool unicodeMode_) {
1072 location = location_;
9e730a78
RD
1073 lineHeight = lineHeight_;
1074 unicodeMode = unicodeMode_;
1075 maxStrWidth = 0;
1e9bafca 1076 id = new wxSTCListBoxWin(GETWIN(parent.GetID()), ctrlID, location);
9e730a78
RD
1077 if (imgList != NULL)
1078 GETLB(id)->SetImageList(imgList, wxIMAGE_LIST_SMALL);
9ce192d4
RD
1079}
1080
9e730a78
RD
1081
1082void ListBoxImpl::SetAverageCharWidth(int width) {
1083 aveCharWidth = width;
1084}
1085
1086
1087void ListBoxImpl::SetVisibleRows(int rows) {
769a9cb2 1088 desiredVisibleRows = rows;
f3c2c221
RD
1089}
1090
9e730a78 1091
1e9bafca
RD
1092int ListBoxImpl::GetVisibleRows() const {
1093 return desiredVisibleRows;
1094}
1095
9e730a78
RD
1096PRectangle 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...
1e9bafca 1099 int maxw = maxStrWidth * aveCharWidth;
1e545382 1100 int maxh ;
9e730a78
RD
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
d134f170
RD
1125 PRectangle rc;
1126 rc.top = 0;
1127 rc.left = 0;
9e730a78
RD
1128 rc.right = maxw;
1129 rc.bottom = maxh;
d134f170
RD
1130 return rc;
1131}
1132
d134f170 1133
9e730a78
RD
1134int ListBoxImpl::CaretFromEdge() {
1135 return 4 + GETLBW(id)->IconWidth();
d134f170
RD
1136}
1137
9e730a78
RD
1138
1139void ListBoxImpl::Clear() {
1140 GETLB(id)->DeleteAllItems();
9ce192d4
RD
1141}
1142
9e730a78
RD
1143
1144void ListBoxImpl::Append(char *s, int type) {
1e9bafca
RD
1145 Append(stc2wx(s), type);
1146}
1147
1148void ListBoxImpl::Append(const wxString& text, int type) {
9e730a78
RD
1149 long count = GETLB(id)->GetItemCount();
1150 long itemID = GETLB(id)->InsertItem(count, wxEmptyString);
1151 GETLB(id)->SetItem(itemID, 1, text);
d35ad26e 1152 maxStrWidth = wxMax(maxStrWidth, text.length());
9e730a78
RD
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 }
9ce192d4
RD
1158}
1159
1e9bafca
RD
1160void ListBoxImpl::SetList(const char* list, char separator, char typesep) {
1161 GETLB(id)->Freeze();
d35ad26e 1162 Clear();
1e9bafca
RD
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
9e730a78
RD
1177
1178int ListBoxImpl::Length() {
1179 return GETLB(id)->GetItemCount();
9ce192d4
RD
1180}
1181
9e730a78
RD
1182
1183void ListBoxImpl::Select(int n) {
7e126a07 1184 bool select = true;
895066d8
RD
1185 if (n == -1) {
1186 n = 0;
7e126a07 1187 select = false;
895066d8 1188 }
9e730a78
RD
1189 GETLB(id)->Focus(n);
1190 GETLB(id)->Select(n, select);
9ce192d4
RD
1191}
1192
9e730a78
RD
1193
1194int ListBoxImpl::GetSelection() {
1195 return GETLB(id)->GetFirstSelected();
9ce192d4
RD
1196}
1197
9e730a78 1198
88a8b04e 1199int ListBoxImpl::Find(const char *WXUNUSED(prefix)) {
b8b0e402 1200 // No longer used
7e126a07 1201 return wxNOT_FOUND;
9ce192d4
RD
1202}
1203
9e730a78
RD
1204
1205void 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);
9ce192d4
RD
1212 value[len-1] = '\0';
1213}
1214
9e730a78
RD
1215
1216void ListBoxImpl::RegisterImage(int type, const char *xpm_data) {
1217 wxMemoryInputStream stream(xpm_data, strlen(xpm_data)+1);
c8b75e94
WS
1218 wxImage img(stream, wxBITMAP_TYPE_XPM);
1219 wxBitmap bmp(img);
9e730a78
RD
1220
1221 if (! imgList) {
1222 // assumes all images are the same size
7e126a07 1223 imgList = new wxImageList(bmp.GetWidth(), bmp.GetHeight(), true);
9e730a78
RD
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;
9a8ffadd 1231 if ( itm.GetCount() < (size_t)type+1)
9e730a78
RD
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
1238void 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
1252void ListBoxImpl::SetDoubleClickAction(CallBackAction action, void *data) {
1253 GETLBW(id)->SetDoubleClickAction(action, data);
1254}
1255
1256
9e730a78
RD
1257ListBox::ListBox() {
1258}
1259
1260ListBox::~ListBox() {
1261}
1262
1263ListBox *ListBox::Allocate() {
1264 return new ListBoxImpl();
9ce192d4
RD
1265}
1266
1a2fb4cd 1267//----------------------------------------------------------------------
9ce192d4
RD
1268
1269Menu::Menu() : id(0) {
1270}
1271
1272void Menu::CreatePopUp() {
1273 Destroy();
1274 id = new wxMenu();
1275}
1276
1277void Menu::Destroy() {
1278 if (id)
1a2fb4cd 1279 delete (wxMenu*)id;
9ce192d4
RD
1280 id = 0;
1281}
1282
1283void Menu::Show(Point pt, Window &w) {
1a2fb4cd 1284 GETWIN(w.GetID())->PopupMenu((wxMenu*)id, pt.x - 4, pt.y);
9ce192d4
RD
1285 Destroy();
1286}
1287
1a2fb4cd 1288//----------------------------------------------------------------------
9ce192d4 1289
88a8b04e 1290DynamicLibrary *DynamicLibrary::Load(const char *WXUNUSED(modulePath)) {
e14d10b0
RD
1291 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1292 return NULL;
1293}
1294
1295//----------------------------------------------------------------------
1296
1a2fb4cd 1297ColourDesired Platform::Chrome() {
9ce192d4 1298 wxColour c;
e1c6c6ae 1299 c = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
1a2fb4cd 1300 return ColourDesired(c.Red(), c.Green(), c.Blue());
9ce192d4
RD
1301}
1302
1a2fb4cd 1303ColourDesired Platform::ChromeHighlight() {
9ce192d4 1304 wxColour c;
e1c6c6ae 1305 c = wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT);
1a2fb4cd 1306 return ColourDesired(c.Red(), c.Green(), c.Blue());
9ce192d4
RD
1307}
1308
1309const char *Platform::DefaultFont() {
10ef30eb
RD
1310 static char buf[128];
1311 strcpy(buf, wxNORMAL_FONT->GetFaceName().mbc_str());
1312 return buf;
9ce192d4
RD
1313}
1314
1315int Platform::DefaultFontSize() {
9e730a78 1316 return wxNORMAL_FONT->GetPointSize();
9ce192d4
RD
1317}
1318
1319unsigned int Platform::DoubleClickTime() {
1320 return 500; // **** ::GetDoubleClickTime();
1321}
1322
9e730a78 1323bool Platform::MouseButtonBounce() {
7e126a07 1324 return false;
9e730a78 1325}
9ce192d4 1326void Platform::DebugDisplay(const char *s) {
0c5b83b0 1327 wxLogDebug(stc2wx(s));
9ce192d4
RD
1328}
1329
88a8b04e 1330bool Platform::IsKeyDown(int WXUNUSED(key)) {
9ce192d4
RD
1331 return false; // I don't think we'll need this.
1332}
1333
1334long 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
a834585d
RD
1343long 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
9ce192d4
RD
1352
1353// These are utility functions not really tied to a platform
1354
1355int Platform::Minimum(int a, int b) {
1356 if (a < b)
1357 return a;
1358 else
1359 return b;
1360}
1361
1362int Platform::Maximum(int a, int b) {
1363 if (a > b)
1364 return a;
1365 else
1366 return b;
1367}
1368
1369#define TRACE
1370
1371void 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
65ec6247
RD
1382
1383static bool assertionPopUps = true;
1384
1385bool Platform::ShowAssertionPopUps(bool assertionPopUps_) {
7e126a07
WS
1386 bool ret = assertionPopUps;
1387 assertionPopUps = assertionPopUps_;
1388 return ret;
65ec6247
RD
1389}
1390
1391void Platform::Assert(const char *c, const char *file, int line) {
7e126a07
WS
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 }
65ec6247
RD
1411}
1412
1413
9ce192d4
RD
1414int 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
88a8b04e 1423bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage), char WXUNUSED(ch)) {
1a2fb4cd
RD
1424 return false;
1425}
1426
88a8b04e 1427int Platform::DBCSCharLength(int WXUNUSED(codePage), const char *WXUNUSED(s)) {
215ef7ae 1428 return 1;
9e730a78
RD
1429}
1430
1431int Platform::DBCSCharMaxLength() {
215ef7ae 1432 return 1;
9e730a78 1433}
1a2fb4cd
RD
1434
1435
1436//----------------------------------------------------------------------
1437
1438ElapsedTime::ElapsedTime() {
1d94e268
WS
1439 wxLongLong localTime = wxGetLocalTimeMillis();
1440 littleBit = localTime.GetLo();
1441 bigBit = localTime.GetHi();
1a2fb4cd
RD
1442}
1443
1444double ElapsedTime::Duration(bool reset) {
1d94e268
WS
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();
1a2fb4cd
RD
1453 result /= 1000.0;
1454 return result;
1455}
1456
1457
1458//----------------------------------------------------------------------
9ce192d4 1459
d99859e4 1460#if wxUSE_UNICODE
6636ac1e
RD
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...
c8b75e94 1466
d99859e4
RD
1467wxString stc2wx(const char* str, size_t len)
1468{
81bfa5ae
RD
1469 if (!len)
1470 return wxEmptyString;
8a07b43c 1471
6636ac1e
RD
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
d99859e4 1479
d99859e4 1480
6636ac1e
RD
1481wxString stc2wx(const char* str)
1482{
1483 return stc2wx(str, strlen(str));
d99859e4 1484}
6636ac1e
RD
1485
1486
1487const 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
6636ac1e
RD
1498 return buffer;
1499}
1500
d99859e4 1501#endif