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