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