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