]> git.saurik.com Git - wxWidgets.git/blame - src/stc/PlatWX.cpp
Applied [ 1867939 ] fixes for wxTreeCtrl crashes when exiting from label editing...
[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,
d67c3388 143 bool 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);
d67c3388 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);
038c0333 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;
038c0333 395 ++p;
6a93571d
RD
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);
038c0333 409 p.Alpha() = alphaOutline;
6a93571d
RD
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);
038c0333 414 p.Alpha() = alphaOutline;
6a93571d 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);
038c0333 422 p.Alpha() = alphaOutline;
6a93571d
RD
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);
038c0333 427 p.Alpha() = alphaOutline;
6a93571d 428 }
038c0333 429
6a93571d
RD
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 {
038c0333 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__
038c0333 949 | wxPOPUP_WINDOW
67879c5b
RD
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();
038c0333 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 }
038c0333 1038
67879c5b
RD
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);
038c0333 1237 long idx = -1;
9e730a78 1238 GETLB(id)->SetItem(itemID, 1, text);
d35ad26e 1239 maxStrWidth = wxMax(maxStrWidth, text.length());
9e730a78
RD
1240 if (type != -1) {
1241 wxCHECK_RET(imgTypeMap, wxT("Unexpected NULL imgTypeMap"));
d67c3388 1242 idx = imgTypeMap->Item(type);
9e730a78 1243 }
d67c3388 1244 GETLB(id)->SetItemImage(itemID, idx, idx);
9ce192d4
RD
1245}
1246
1e9bafca
RD
1247void ListBoxImpl::SetList(const char* list, char separator, char typesep) {
1248 GETLB(id)->Freeze();
d35ad26e 1249 Clear();
1e9bafca
RD
1250 wxStringTokenizer tkzr(stc2wx(list), (wxChar)separator);
1251 while ( tkzr.HasMoreTokens() ) {
1252 wxString token = tkzr.GetNextToken();
1253 long type = -1;
1254 int pos = token.Find(typesep);
1255 if (pos != -1) {
1256 token.Mid(pos+1).ToLong(&type);
1257 token.Truncate(pos);
1258 }
1259 Append(token, (int)type);
1260 }
1261 GETLB(id)->Thaw();
1262}
1263
9e730a78
RD
1264
1265int ListBoxImpl::Length() {
1266 return GETLB(id)->GetItemCount();
9ce192d4
RD
1267}
1268
9e730a78
RD
1269
1270void ListBoxImpl::Select(int n) {
7e126a07 1271 bool select = true;
895066d8
RD
1272 if (n == -1) {
1273 n = 0;
7e126a07 1274 select = false;
895066d8 1275 }
67879c5b 1276 GETLB(id)->EnsureVisible(n);
9e730a78 1277 GETLB(id)->Select(n, select);
9ce192d4
RD
1278}
1279
9e730a78
RD
1280
1281int ListBoxImpl::GetSelection() {
1282 return GETLB(id)->GetFirstSelected();
9ce192d4
RD
1283}
1284
9e730a78 1285
88a8b04e 1286int ListBoxImpl::Find(const char *WXUNUSED(prefix)) {
b8b0e402 1287 // No longer used
7e126a07 1288 return wxNOT_FOUND;
9ce192d4
RD
1289}
1290
9e730a78
RD
1291
1292void ListBoxImpl::GetValue(int n, char *value, int len) {
1293 wxListItem item;
1294 item.SetId(n);
1295 item.SetColumn(1);
1296 item.SetMask(wxLIST_MASK_TEXT);
1297 GETLB(id)->GetItem(item);
1298 strncpy(value, wx2stc(item.GetText()), len);
9ce192d4
RD
1299 value[len-1] = '\0';
1300}
1301
9e730a78
RD
1302
1303void ListBoxImpl::RegisterImage(int type, const char *xpm_data) {
1304 wxMemoryInputStream stream(xpm_data, strlen(xpm_data)+1);
c8b75e94
WS
1305 wxImage img(stream, wxBITMAP_TYPE_XPM);
1306 wxBitmap bmp(img);
9e730a78
RD
1307
1308 if (! imgList) {
1309 // assumes all images are the same size
7e126a07 1310 imgList = new wxImageList(bmp.GetWidth(), bmp.GetHeight(), true);
9e730a78
RD
1311 imgTypeMap = new wxArrayInt;
1312 }
1313
1314 int idx = imgList->Add(bmp);
1315
1316 // do we need to extend the mapping array?
1317 wxArrayInt& itm = *imgTypeMap;
9a8ffadd 1318 if ( itm.GetCount() < (size_t)type+1)
9e730a78
RD
1319 itm.Add(-1, type - itm.GetCount() + 1);
1320
1321 // Add an item that maps type to the image index
1322 itm[type] = idx;
1323}
1324
1325void ListBoxImpl::ClearRegisteredImages() {
1326 if (imgList) {
1327 delete imgList;
1328 imgList = NULL;
1329 }
1330 if (imgTypeMap) {
1331 delete imgTypeMap;
1332 imgTypeMap = NULL;
1333 }
1334 if (id)
1335 GETLB(id)->SetImageList(NULL, wxIMAGE_LIST_SMALL);
1336}
1337
1338
1339void ListBoxImpl::SetDoubleClickAction(CallBackAction action, void *data) {
1340 GETLBW(id)->SetDoubleClickAction(action, data);
1341}
1342
1343
9e730a78
RD
1344ListBox::ListBox() {
1345}
1346
1347ListBox::~ListBox() {
1348}
1349
1350ListBox *ListBox::Allocate() {
1351 return new ListBoxImpl();
9ce192d4
RD
1352}
1353
1a2fb4cd 1354//----------------------------------------------------------------------
9ce192d4
RD
1355
1356Menu::Menu() : id(0) {
1357}
1358
1359void Menu::CreatePopUp() {
1360 Destroy();
1361 id = new wxMenu();
1362}
1363
1364void Menu::Destroy() {
1365 if (id)
1a2fb4cd 1366 delete (wxMenu*)id;
9ce192d4
RD
1367 id = 0;
1368}
1369
1370void Menu::Show(Point pt, Window &w) {
1a2fb4cd 1371 GETWIN(w.GetID())->PopupMenu((wxMenu*)id, pt.x - 4, pt.y);
9ce192d4
RD
1372 Destroy();
1373}
1374
1a2fb4cd 1375//----------------------------------------------------------------------
9ce192d4 1376
88a8b04e 1377DynamicLibrary *DynamicLibrary::Load(const char *WXUNUSED(modulePath)) {
e14d10b0
RD
1378 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1379 return NULL;
1380}
1381
1382//----------------------------------------------------------------------
1383
1a2fb4cd 1384ColourDesired Platform::Chrome() {
9ce192d4 1385 wxColour c;
e1c6c6ae 1386 c = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
1a2fb4cd 1387 return ColourDesired(c.Red(), c.Green(), c.Blue());
9ce192d4
RD
1388}
1389
1a2fb4cd 1390ColourDesired Platform::ChromeHighlight() {
9ce192d4 1391 wxColour c;
e1c6c6ae 1392 c = wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT);
1a2fb4cd 1393 return ColourDesired(c.Red(), c.Green(), c.Blue());
9ce192d4
RD
1394}
1395
1396const char *Platform::DefaultFont() {
10ef30eb
RD
1397 static char buf[128];
1398 strcpy(buf, wxNORMAL_FONT->GetFaceName().mbc_str());
1399 return buf;
9ce192d4
RD
1400}
1401
1402int Platform::DefaultFontSize() {
9e730a78 1403 return wxNORMAL_FONT->GetPointSize();
9ce192d4
RD
1404}
1405
1406unsigned int Platform::DoubleClickTime() {
1407 return 500; // **** ::GetDoubleClickTime();
1408}
1409
9e730a78 1410bool Platform::MouseButtonBounce() {
7e126a07 1411 return false;
9e730a78 1412}
9ce192d4 1413
88a8b04e 1414bool Platform::IsKeyDown(int WXUNUSED(key)) {
9ce192d4
RD
1415 return false; // I don't think we'll need this.
1416}
1417
1418long Platform::SendScintilla(WindowID w,
1419 unsigned int msg,
1420 unsigned long wParam,
1421 long lParam) {
1422
1423 wxStyledTextCtrl* stc = (wxStyledTextCtrl*)w;
1424 return stc->SendMsg(msg, wParam, lParam);
1425}
1426
a834585d
RD
1427long Platform::SendScintillaPointer(WindowID w,
1428 unsigned int msg,
1429 unsigned long wParam,
1430 void *lParam) {
1431
1432 wxStyledTextCtrl* stc = (wxStyledTextCtrl*)w;
1433 return stc->SendMsg(msg, wParam, (long)lParam);
1434}
1435
9ce192d4
RD
1436
1437// These are utility functions not really tied to a platform
1438
1439int Platform::Minimum(int a, int b) {
1440 if (a < b)
1441 return a;
1442 else
1443 return b;
1444}
1445
1446int Platform::Maximum(int a, int b) {
1447 if (a > b)
1448 return a;
1449 else
1450 return b;
1451}
1452
74e1efdd
VZ
1453//#define TRACE
1454
1455void Platform::DebugDisplay(const char *s) {
1456#ifdef TRACE
1457 wxLogDebug(stc2wx(s));
1458#else
1459 wxUnusedVar(s);
1460#endif
1461}
9ce192d4
RD
1462
1463void Platform::DebugPrintf(const char *format, ...) {
1464#ifdef TRACE
1465 char buffer[2000];
1466 va_list pArguments;
1467 va_start(pArguments, format);
1468 vsprintf(buffer,format,pArguments);
1469 va_end(pArguments);
1470 Platform::DebugDisplay(buffer);
8d3a5374
VZ
1471#else
1472 wxUnusedVar(format);
9ce192d4
RD
1473#endif
1474}
1475
65ec6247
RD
1476
1477static bool assertionPopUps = true;
1478
1479bool Platform::ShowAssertionPopUps(bool assertionPopUps_) {
7e126a07
WS
1480 bool ret = assertionPopUps;
1481 assertionPopUps = assertionPopUps_;
1482 return ret;
65ec6247
RD
1483}
1484
1485void Platform::Assert(const char *c, const char *file, int line) {
74e1efdd 1486#ifdef TRACE
7e126a07
WS
1487 char buffer[2000];
1488 sprintf(buffer, "Assertion [%s] failed at %s %d", c, file, line);
1489 if (assertionPopUps) {
1490 /*int idButton = */
1491 wxMessageBox(stc2wx(buffer),
1492 wxT("Assertion failure"),
1493 wxICON_HAND | wxOK);
7e126a07
WS
1494 } else {
1495 strcat(buffer, "\r\n");
1496 Platform::DebugDisplay(buffer);
1497 abort();
1498 }
74e1efdd
VZ
1499#else
1500 wxUnusedVar(c);
1501 wxUnusedVar(file);
1502 wxUnusedVar(line);
1503#endif
65ec6247
RD
1504}
1505
1506
9ce192d4
RD
1507int Platform::Clamp(int val, int minVal, int maxVal) {
1508 if (val > maxVal)
1509 val = maxVal;
1510 if (val < minVal)
1511 val = minVal;
1512 return val;
1513}
1514
1515
88a8b04e 1516bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage), char WXUNUSED(ch)) {
1a2fb4cd
RD
1517 return false;
1518}
1519
88a8b04e 1520int Platform::DBCSCharLength(int WXUNUSED(codePage), const char *WXUNUSED(s)) {
215ef7ae 1521 return 1;
9e730a78
RD
1522}
1523
1524int Platform::DBCSCharMaxLength() {
215ef7ae 1525 return 1;
9e730a78 1526}
1a2fb4cd
RD
1527
1528
1529//----------------------------------------------------------------------
1530
1531ElapsedTime::ElapsedTime() {
1d94e268
WS
1532 wxLongLong localTime = wxGetLocalTimeMillis();
1533 littleBit = localTime.GetLo();
1534 bigBit = localTime.GetHi();
1a2fb4cd
RD
1535}
1536
1537double ElapsedTime::Duration(bool reset) {
1d94e268
WS
1538 wxLongLong prevTime(bigBit, littleBit);
1539 wxLongLong localTime = wxGetLocalTimeMillis();
1540 if(reset) {
1541 littleBit = localTime.GetLo();
1542 bigBit = localTime.GetHi();
1543 }
1544 wxLongLong duration = localTime - prevTime;
1545 double result = duration.ToDouble();
1a2fb4cd
RD
1546 result /= 1000.0;
1547 return result;
1548}
1549
1550
1551//----------------------------------------------------------------------
9ce192d4 1552
d99859e4 1553#if wxUSE_UNICODE
6636ac1e
RD
1554
1555#include "UniConversion.h"
1556
1557// Convert using Scintilla's functions instead of wx's, Scintilla's are more
1558// forgiving and won't assert...
c8b75e94 1559
d99859e4
RD
1560wxString stc2wx(const char* str, size_t len)
1561{
81bfa5ae
RD
1562 if (!len)
1563 return wxEmptyString;
8a07b43c 1564
7e0c58e9 1565 size_t wclen = UTF16Length(str, len);
6636ac1e
RD
1566 wxWCharBuffer buffer(wclen+1);
1567
7e0c58e9 1568 size_t actualLen = UTF16FromUTF8(str, len, buffer.data(), wclen+1);
6636ac1e
RD
1569 return wxString(buffer.data(), actualLen);
1570}
1571
d99859e4 1572
d99859e4 1573
6636ac1e
RD
1574wxString stc2wx(const char* str)
1575{
1576 return stc2wx(str, strlen(str));
d99859e4 1577}
6636ac1e
RD
1578
1579
1580const wxWX2MBbuf wx2stc(const wxString& str)
1581{
1582 const wchar_t* wcstr = str.c_str();
1583 size_t wclen = str.length();
1584 size_t len = UTF8Length(wcstr, wclen);
1585
1586 wxCharBuffer buffer(len+1);
7e0c58e9 1587 UTF8FromUTF16(wcstr, wclen, buffer.data(), len);
6636ac1e
RD
1588
1589 // TODO check NULL termination!!
1590
6636ac1e
RD
1591 return buffer;
1592}
1593
d99859e4 1594#endif
29825f5f
PC
1595
1596#endif // wxUSE_STC