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