]> git.saurik.com Git - wxWidgets.git/blame - src/stc/PlatWX.cpp
build fix (part of r59673, r59656)
[wxWidgets.git] / src / stc / PlatWX.cpp
CommitLineData
9ce192d4 1// Scintilla source code edit control
be5a51fb 2// PlatWX.cxx - implementation of platform facilities on wxWidgets
9ce192d4
RD
3// Copyright 1998-1999 by Neil Hodgson <neilh@scintilla.org>
4// Robin Dunn <robin@aldunn.com>
5// The License.txt file describes the conditions under which this software may be distributed.
6
54429bb3
RD
7// For compilers that support precompilation, includes "wx.h".
8#include "wx/wxprec.h"
9
10#ifdef __BORLANDC__
11 #pragma hdrstop
12#endif
13
29825f5f 14#if wxUSE_STC
54429bb3
RD
15
16#ifndef WX_PRECOMP
29825f5f
PC
17 #include "wx/menu.h"
18 #include "wx/dcmemory.h"
19 #include "wx/settings.h"
54429bb3
RD
20#endif // WX_PRECOMP
21
f6bcfd97 22#include <ctype.h>
9ce192d4 23
7e0c58e9
RD
24#if wxUSE_DISPLAY
25#include "wx/display.h"
26#endif
27
d6655166
WS
28#include "wx/encconv.h"
29#include "wx/listctrl.h"
30#include "wx/mstream.h"
31#include "wx/image.h"
32#include "wx/imaglist.h"
1e9bafca 33#include "wx/tokenzr.h"
8b3d52fc 34
3fb1e059 35#ifdef wxHAS_RAW_BITMAP
6a93571d 36#include "wx/rawbmp.h"
8b6c20ce 37#endif
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 293void SurfaceImpl::PenColour(ColourAllocated fore) {
cb129171 294 hdc->SetPen(wxPen(wxColourFromCA(fore)));
9ce192d4
RD
295}
296
1a2fb4cd 297void SurfaceImpl::BrushColour(ColourAllocated back) {
cb129171 298 hdc->SetBrush(wxBrush(wxColourFromCA(back)));
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 348 else // Something is wrong so display in red
cb129171 349 br = wxBrush(*wxRED);
9ce192d4
RD
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*/) {
3fb1e059 371#ifdef wxHAS_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);
8b6c20ce
RD
432
433#else
434 wxUnusedVar(cornerSize);
435 wxUnusedVar(alphaFill);
436 wxUnusedVar(alphaOutline);
437 RectangleDraw(rc, outline, fill);
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 487 hdc->SetTextForeground(wxColourFromCA(fore));
cb129171 488 hdc->SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
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 493
cb129171 494 hdc->SetBackgroundMode(wxBRUSHSTYLE_SOLID);
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) {
d4f392ea 680 wxStockCursor cursorId;
9ce192d4
RD
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 }
d4f392ea
FM
711
712 wxCursor wc = wxCursor(cursorId);
713 if(curs != cursorLast)
714 {
715 GETWIN(id)->SetCursor(wc);
716 cursorLast = curs;
717 }
9ce192d4
RD
718}
719
720
721void Window::SetTitle(const char *s) {
fd39aa23 722 GETWIN(id)->SetLabel(stc2wx(s));
9ce192d4
RD
723}
724
725
7e0c58e9
RD
726// Returns rectangle of monitor pt is on
727PRectangle Window::GetMonitorRect(Point pt) {
728 wxRect rect;
729 if (! id) return PRectangle();
730#if wxUSE_DISPLAY
731 // Get the display the point is found on
732 int n = wxDisplay::GetFromPoint(wxPoint(pt.x, pt.y));
733 wxDisplay dpy(n == wxNOT_FOUND ? 0 : n);
734 rect = dpy.GetGeometry();
735#endif
736 return PRectangleFromwxRect(rect);
737}
738
769a9cb2
RD
739//----------------------------------------------------------------------
740// Helper classes for ListBox
741
267484bc 742
b0d0494f 743// This is a simple subclass of wxListView that just resets focus to the
9e730a78
RD
744// parent when it gets it.
745class wxSTCListBox : public wxListView {
451c5cc7 746public:
9e730a78
RD
747 wxSTCListBox(wxWindow* parent, wxWindowID id,
748 const wxPoint& pos, const wxSize& size,
749 long style)
382fe640
RD
750 : wxListView()
751 {
752#ifdef __WXMSW__
753 Hide(); // don't flicker as we move it around...
754#endif
755 Create(parent, id, pos, size, style);
756 }
451c5cc7 757
7e126a07 758
451c5cc7
RD
759 void OnFocus(wxFocusEvent& event) {
760 GetParent()->SetFocus();
761 event.Skip();
762 }
763
5f9eb69c 764 void OnKillFocus(wxFocusEvent& WXUNUSED(event)) {
b0d0494f
RD
765 // Do nothing. Prevents base class from resetting the colors...
766 }
7e126a07 767
719ee9c3
RD
768#ifdef __WXMAC__
769 // For some reason I don't understand yet the focus doesn't really leave
770 // the listbox like it should, so if we get any events feed them back to
771 // the wxSTC
772 void OnKeyDown(wxKeyEvent& event) {
773 GetGrandParent()->GetEventHandler()->ProcessEvent(event);
774 }
775 void OnChar(wxKeyEvent& event) {
776 GetGrandParent()->GetEventHandler()->ProcessEvent(event);
777 }
b0d0494f 778
719ee9c3
RD
779 // And we need to force the focus back when being destroyed
780 ~wxSTCListBox() {
781 GetGrandParent()->SetFocus();
7e126a07
WS
782 }
783#endif
784
451c5cc7
RD
785private:
786 DECLARE_EVENT_TABLE()
787};
788
9e730a78
RD
789BEGIN_EVENT_TABLE(wxSTCListBox, wxListView)
790 EVT_SET_FOCUS( wxSTCListBox::OnFocus)
b0d0494f 791 EVT_KILL_FOCUS(wxSTCListBox::OnKillFocus)
719ee9c3
RD
792#ifdef __WXMAC__
793 EVT_KEY_DOWN( wxSTCListBox::OnKeyDown)
794 EVT_CHAR( wxSTCListBox::OnChar)
795#endif
451c5cc7
RD
796END_EVENT_TABLE()
797
798
799
d35ad26e 800#if wxUSE_POPUPWIN //-----------------------------------
819850ff 801#include "wx/popupwin.h"
382fe640
RD
802
803
804//
805// TODO: Refactor these two classes to have a common base (or a mix-in) to get
806// rid of the code duplication. (Either that or convince somebody to
807// implement wxPopupWindow for the Mac!!)
808//
809// In the meantime, be careful to duplicate any changes as needed...
d35ad26e
WS
810//
811
812// A popup window to place the wxSTCListBox upon
382fe640
RD
813class wxSTCListBoxWin : public wxPopupWindow
814{
815private:
816 wxListView* lv;
817 CallBackAction doubleClickAction;
818 void* doubleClickActionData;
819public:
d35ad26e 820 wxSTCListBoxWin(wxWindow* parent, wxWindowID id, Point WXUNUSED(location)) :
382fe640
RD
821 wxPopupWindow(parent, wxBORDER_NONE)
822 {
038c0333 823
382fe640
RD
824 SetBackgroundColour(*wxBLACK); // for our simple border
825
67879c5b 826 lv = new wxSTCListBox(parent, id, wxPoint(-50,-50), wxDefaultSize,
382fe640
RD
827 wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER | wxBORDER_NONE);
828 lv->SetCursor(wxCursor(wxCURSOR_ARROW));
829 lv->InsertColumn(0, wxEmptyString);
830 lv->InsertColumn(1, wxEmptyString);
831
832 // NOTE: We need to fool the wxListView into thinking that it has the
833 // focus so it will use the normal selection colour and will look
834 // "right" to the user. But since the wxPopupWindow or its children
835 // can't receive focus then we have to pull a fast one and temporarily
836 // parent the listctrl on the STC window and then call SetFocus and
d35ad26e 837 // then reparent it back to the popup.
382fe640
RD
838 lv->SetFocus();
839 lv->Reparent(this);
840#ifdef __WXMSW__
841 lv->Show();
842#endif
843 }
844
845
846 // Set position in client coords
847 virtual void DoSetSize(int x, int y,
848 int width, int height,
849 int sizeFlags = wxSIZE_AUTO) {
850 if (x != wxDefaultCoord) {
851 GetParent()->ClientToScreen(&x, NULL);
852 }
853 if (y != wxDefaultCoord) {
854 GetParent()->ClientToScreen(NULL, &y);
855 }
856 wxPopupWindow::DoSetSize(x, y, width, height, sizeFlags);
857 }
858
859 // return position as if it were in client coords
860 virtual void DoGetPosition( int *x, int *y ) const {
861 int sx, sy;
862 wxPopupWindow::DoGetPosition(&sx, &sy);
863 GetParent()->ScreenToClient(&sx, &sy);
864 if (x) *x = sx;
865 if (y) *y = sy;
866 }
867
868
869 bool Destroy() {
870 if ( !wxPendingDelete.Member(this) )
871 wxPendingDelete.Append(this);
872 return true;
873 }
874
875
876 int IconWidth() {
877 wxImageList* il = lv->GetImageList(wxIMAGE_LIST_SMALL);
878 if (il != NULL) {
879 int w, h;
880 il->GetSize(0, w, h);
881 return w;
882 }
883 return 0;
884 }
885
886
887 void SetDoubleClickAction(CallBackAction action, void *data) {
888 doubleClickAction = action;
889 doubleClickActionData = data;
890 }
891
892
893 void OnFocus(wxFocusEvent& event) {
894 GetParent()->SetFocus();
895 event.Skip();
896 }
897
898 void OnSize(wxSizeEvent& event) {
899 // resize the child
900 wxSize sz = GetSize();
901 sz.x -= 2;
902 sz.y -= 2;
903 lv->SetSize(1, 1, sz.x, sz.y);
904 // reset the column widths
905 lv->SetColumnWidth(0, IconWidth()+4);
906 lv->SetColumnWidth(1, sz.x - 2 - lv->GetColumnWidth(0) -
907 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X));
908 event.Skip();
909 }
910
911 void OnActivate(wxListEvent& WXUNUSED(event)) {
912 doubleClickAction(doubleClickActionData);
913 }
914
915 wxListView* GetLB() { return lv; }
916
917private:
918 DECLARE_EVENT_TABLE()
919
920};
921
922BEGIN_EVENT_TABLE(wxSTCListBoxWin, wxPopupWindow)
923 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus)
924 EVT_SIZE ( wxSTCListBoxWin::OnSize)
925 EVT_LIST_ITEM_ACTIVATED(wxID_ANY, wxSTCListBoxWin::OnActivate)
926END_EVENT_TABLE()
927
d35ad26e 928
382fe640 929
67879c5b 930#else // !wxUSE_POPUPWIN -----------------------------------
819850ff 931#include "wx/frame.h"
267484bc 932
67879c5b
RD
933// A normal window to place the wxSTCListBox upon, but make it behave as much
934// like a wxPopupWindow as possible
935class wxSTCListBoxWin : public wxFrame {
9e730a78
RD
936private:
937 wxListView* lv;
938 CallBackAction doubleClickAction;
939 void* doubleClickActionData;
f97d84a6 940public:
1e9bafca 941 wxSTCListBoxWin(wxWindow* parent, wxWindowID id, Point location) :
67879c5b
RD
942 wxFrame(parent, id, wxEmptyString, wxPoint(location.x, location.y), wxSize(0,0),
943 wxFRAME_NO_TASKBAR
944 | wxFRAME_FLOAT_ON_PARENT
945#ifdef __WXMAC__
038c0333 946 | wxPOPUP_WINDOW
67879c5b
RD
947 | wxNO_BORDER
948#else
949 | wxSIMPLE_BORDER
950#endif
951 )
9e730a78
RD
952 {
953
9e730a78
RD
954 lv = new wxSTCListBox(this, id, wxDefaultPosition, wxDefaultSize,
955 wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER | wxNO_BORDER);
956 lv->SetCursor(wxCursor(wxCURSOR_ARROW));
957 lv->InsertColumn(0, wxEmptyString);
958 lv->InsertColumn(1, wxEmptyString);
b0d0494f
RD
959
960 // Eventhough we immediately reset the focus to the parent, this helps
961 // things to look right...
962 lv->SetFocus();
963
6612393c 964 Hide();
038c0333 965 }
6612393c 966
7e126a07 967
a4f3565e
RD
968 // On OSX and (possibly others) there can still be pending
969 // messages/events for the list control when Scintilla wants to
970 // close it, so do a pending delete of it instead of destroying
971 // immediately.
67879c5b
RD
972 bool Destroy()
973 {
d13ea3aa 974#ifdef __WXMAC__
719ee9c3 975 // The bottom edge of this window is not getting properly
d13ea3aa
RD
976 // refreshed upon deletion, so help it out...
977 wxWindow* p = GetParent();
978 wxRect r(GetPosition(), GetSize());
979 r.SetHeight(r.GetHeight()+1);
980 p->Refresh(false, &r);
981#endif
a4f3565e
RD
982 if ( !wxPendingDelete.Member(this) )
983 wxPendingDelete.Append(this);
7e126a07 984 return true;
f97d84a6
RD
985 }
986
7e126a07 987
67879c5b
RD
988 int IconWidth()
989 {
9e730a78
RD
990 wxImageList* il = lv->GetImageList(wxIMAGE_LIST_SMALL);
991 if (il != NULL) {
992 int w, h;
993 il->GetSize(0, w, h);
994 return w;
995 }
996 return 0;
997 }
f97d84a6 998
769a9cb2 999
67879c5b
RD
1000 void SetDoubleClickAction(CallBackAction action, void *data)
1001 {
9e730a78
RD
1002 doubleClickAction = action;
1003 doubleClickActionData = data;
1004 }
451c5cc7 1005
769a9cb2 1006
67879c5b
RD
1007 void OnFocus(wxFocusEvent& event)
1008 {
1009 ActivateParent();
9e730a78
RD
1010 GetParent()->SetFocus();
1011 event.Skip();
1012 }
769a9cb2 1013
67879c5b
RD
1014 void OnSize(wxSizeEvent& event)
1015 {
d13ea3aa 1016 // resize the child
9e730a78 1017 wxSize sz = GetClientSize();
d13ea3aa 1018 lv->SetSize(sz);
9e730a78
RD
1019 // reset the column widths
1020 lv->SetColumnWidth(0, IconWidth()+4);
1021 lv->SetColumnWidth(1, sz.x - 2 - lv->GetColumnWidth(0) -
1022 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X));
1023 event.Skip();
769a9cb2 1024 }
769a9cb2 1025
67879c5b
RD
1026 void ActivateParent()
1027 {
1028 // Although we're a frame, we always want the parent to be active, so
1029 // raise it whenever we get shown, focused, etc.
1030 wxTopLevelWindow *frame = wxDynamicCast(
1031 wxGetTopLevelParent(GetParent()), wxTopLevelWindow);
1032 if (frame)
1033 frame->Raise();
1034 }
038c0333 1035
67879c5b
RD
1036
1037 virtual void DoSetSize(int x, int y,
1038 int width, int height,
1039 int sizeFlags = wxSIZE_AUTO)
1040 {
1041 // convert coords to screen coords since we're a top-level window
1042 if (x != wxDefaultCoord) {
1043 GetParent()->ClientToScreen(&x, NULL);
1044 }
1045 if (y != wxDefaultCoord) {
1046 GetParent()->ClientToScreen(NULL, &y);
1047 }
1048 wxFrame::DoSetSize(x, y, width, height, sizeFlags);
1049 }
1050
1051 virtual bool Show(bool show = true)
1052 {
1053 bool rv = wxFrame::Show(show);
1054 if (rv && show)
1055 ActivateParent();
719493e1 1056#ifdef __WXMAC__
719493e1 1057 GetParent()->Refresh(false);
67879c5b 1058#endif
719493e1
RD
1059 return rv;
1060 }
d35ad26e 1061
67879c5b
RD
1062 void OnActivate(wxListEvent& WXUNUSED(event))
1063 {
9e730a78 1064 doubleClickAction(doubleClickActionData);
769a9cb2 1065 }
9e730a78
RD
1066
1067 wxListView* GetLB() { return lv; }
769a9cb2
RD
1068
1069private:
769a9cb2
RD
1070 DECLARE_EVENT_TABLE()
1071};
1072
9e730a78
RD
1073
1074BEGIN_EVENT_TABLE(wxSTCListBoxWin, wxWindow)
7e126a07
WS
1075 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus)
1076 EVT_SIZE ( wxSTCListBoxWin::OnSize)
1077 EVT_LIST_ITEM_ACTIVATED(wxID_ANY, wxSTCListBoxWin::OnActivate)
769a9cb2 1078END_EVENT_TABLE()
769a9cb2 1079
382fe640 1080#endif // wxUSE_POPUPWIN -----------------------------------
9e730a78
RD
1081
1082
1083inline wxSTCListBoxWin* GETLBW(WindowID win) {
1084 return ((wxSTCListBoxWin*)win);
1085}
1086
1087inline wxListView* GETLB(WindowID win) {
1088 return GETLBW(win)->GetLB();
1a2fb4cd 1089}
769a9cb2
RD
1090
1091//----------------------------------------------------------------------
1092
9e730a78
RD
1093class ListBoxImpl : public ListBox {
1094private:
1095 int lineHeight;
1096 bool unicodeMode;
1097 int desiredVisibleRows;
1098 int aveCharWidth;
80cac95e 1099 size_t maxStrWidth;
1e9bafca 1100 Point location; // Caret location at which the list is opened
9e730a78
RD
1101 wxImageList* imgList;
1102 wxArrayInt* imgTypeMap;
1103
1104public:
1105 ListBoxImpl();
1106 ~ListBoxImpl();
1107
1108 virtual void SetFont(Font &font);
1e9bafca 1109 virtual void Create(Window &parent, int ctrlID, Point location_, int lineHeight_, bool unicodeMode_);
9e730a78
RD
1110 virtual void SetAverageCharWidth(int width);
1111 virtual void SetVisibleRows(int rows);
1e9bafca 1112 virtual int GetVisibleRows() const;
9e730a78
RD
1113 virtual PRectangle GetDesiredRect();
1114 virtual int CaretFromEdge();
1115 virtual void Clear();
1116 virtual void Append(char *s, int type = -1);
1e9bafca 1117 void Append(const wxString& text, int type);
9e730a78
RD
1118 virtual int Length();
1119 virtual void Select(int n);
1120 virtual int GetSelection();
1121 virtual int Find(const char *prefix);
1122 virtual void GetValue(int n, char *value, int len);
9e730a78
RD
1123 virtual void RegisterImage(int type, const char *xpm_data);
1124 virtual void ClearRegisteredImages();
1125 virtual void SetDoubleClickAction(CallBackAction, void *);
1e9bafca 1126 virtual void SetList(const char* list, char separator, char typesep);
9e730a78
RD
1127};
1128
1129
1130ListBoxImpl::ListBoxImpl()
1131 : lineHeight(10), unicodeMode(false),
1132 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
1133 imgList(NULL), imgTypeMap(NULL)
1134{
9ce192d4
RD
1135}
1136
9e730a78
RD
1137ListBoxImpl::~ListBoxImpl() {
1138 if (imgList) {
1139 delete imgList;
1140 imgList = NULL;
1141 }
1142 if (imgTypeMap) {
1143 delete imgTypeMap;
1144 imgTypeMap = NULL;
1145 }
9ce192d4
RD
1146}
1147
9e730a78
RD
1148
1149void ListBoxImpl::SetFont(Font &font) {
1150 GETLB(id)->SetFont(*((wxFont*)font.GetID()));
1151}
1152
1153
1e9bafca
RD
1154void ListBoxImpl::Create(Window &parent, int ctrlID, Point location_, int lineHeight_, bool unicodeMode_) {
1155 location = location_;
9e730a78
RD
1156 lineHeight = lineHeight_;
1157 unicodeMode = unicodeMode_;
1158 maxStrWidth = 0;
1e9bafca 1159 id = new wxSTCListBoxWin(GETWIN(parent.GetID()), ctrlID, location);
9e730a78
RD
1160 if (imgList != NULL)
1161 GETLB(id)->SetImageList(imgList, wxIMAGE_LIST_SMALL);
9ce192d4
RD
1162}
1163
9e730a78
RD
1164
1165void ListBoxImpl::SetAverageCharWidth(int width) {
1166 aveCharWidth = width;
1167}
1168
1169
1170void ListBoxImpl::SetVisibleRows(int rows) {
769a9cb2 1171 desiredVisibleRows = rows;
f3c2c221
RD
1172}
1173
9e730a78 1174
1e9bafca
RD
1175int ListBoxImpl::GetVisibleRows() const {
1176 return desiredVisibleRows;
1177}
1178
9e730a78
RD
1179PRectangle ListBoxImpl::GetDesiredRect() {
1180 // wxListCtrl doesn't have a DoGetBestSize, so instead we kept track of
1181 // the max size in Append and calculate it here...
1e9bafca 1182 int maxw = maxStrWidth * aveCharWidth;
1e545382 1183 int maxh ;
9e730a78
RD
1184
1185 // give it a default if there are no lines, and/or add a bit more
1186 if (maxw == 0) maxw = 100;
1187 maxw += aveCharWidth * 3 +
1188 GETLBW(id)->IconWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1189 if (maxw > 350)
1190 maxw = 350;
1191
1192 // estimate a desired height
1193 int count = GETLB(id)->GetItemCount();
1194 if (count) {
1195 wxRect rect;
1196 GETLB(id)->GetItemRect(0, rect);
1197 maxh = count * rect.GetHeight();
1198 if (maxh > 140) // TODO: Use desiredVisibleRows??
1199 maxh = 140;
1200
1201 // Try to make the size an exact multiple of some number of lines
1202 int lines = maxh / rect.GetHeight();
1203 maxh = (lines + 1) * rect.GetHeight() + 2;
1204 }
1205 else
1206 maxh = 100;
1207
d134f170
RD
1208 PRectangle rc;
1209 rc.top = 0;
1210 rc.left = 0;
9e730a78
RD
1211 rc.right = maxw;
1212 rc.bottom = maxh;
d134f170
RD
1213 return rc;
1214}
1215
d134f170 1216
9e730a78
RD
1217int ListBoxImpl::CaretFromEdge() {
1218 return 4 + GETLBW(id)->IconWidth();
d134f170
RD
1219}
1220
9e730a78
RD
1221
1222void ListBoxImpl::Clear() {
1223 GETLB(id)->DeleteAllItems();
9ce192d4
RD
1224}
1225
9e730a78
RD
1226
1227void ListBoxImpl::Append(char *s, int type) {
1e9bafca
RD
1228 Append(stc2wx(s), type);
1229}
1230
1231void ListBoxImpl::Append(const wxString& text, int type) {
9e730a78
RD
1232 long count = GETLB(id)->GetItemCount();
1233 long itemID = GETLB(id)->InsertItem(count, wxEmptyString);
038c0333 1234 long idx = -1;
9e730a78 1235 GETLB(id)->SetItem(itemID, 1, text);
d35ad26e 1236 maxStrWidth = wxMax(maxStrWidth, text.length());
9e730a78
RD
1237 if (type != -1) {
1238 wxCHECK_RET(imgTypeMap, wxT("Unexpected NULL imgTypeMap"));
d67c3388 1239 idx = imgTypeMap->Item(type);
9e730a78 1240 }
d67c3388 1241 GETLB(id)->SetItemImage(itemID, idx, idx);
9ce192d4
RD
1242}
1243
1e9bafca
RD
1244void ListBoxImpl::SetList(const char* list, char separator, char typesep) {
1245 GETLB(id)->Freeze();
d35ad26e 1246 Clear();
1e9bafca
RD
1247 wxStringTokenizer tkzr(stc2wx(list), (wxChar)separator);
1248 while ( tkzr.HasMoreTokens() ) {
1249 wxString token = tkzr.GetNextToken();
1250 long type = -1;
1251 int pos = token.Find(typesep);
1252 if (pos != -1) {
1253 token.Mid(pos+1).ToLong(&type);
1254 token.Truncate(pos);
1255 }
1256 Append(token, (int)type);
1257 }
1258 GETLB(id)->Thaw();
1259}
1260
9e730a78
RD
1261
1262int ListBoxImpl::Length() {
1263 return GETLB(id)->GetItemCount();
9ce192d4
RD
1264}
1265
9e730a78
RD
1266
1267void ListBoxImpl::Select(int n) {
7e126a07 1268 bool select = true;
895066d8
RD
1269 if (n == -1) {
1270 n = 0;
7e126a07 1271 select = false;
895066d8 1272 }
67879c5b 1273 GETLB(id)->EnsureVisible(n);
9e730a78 1274 GETLB(id)->Select(n, select);
9ce192d4
RD
1275}
1276
9e730a78
RD
1277
1278int ListBoxImpl::GetSelection() {
1279 return GETLB(id)->GetFirstSelected();
9ce192d4
RD
1280}
1281
9e730a78 1282
88a8b04e 1283int ListBoxImpl::Find(const char *WXUNUSED(prefix)) {
b8b0e402 1284 // No longer used
7e126a07 1285 return wxNOT_FOUND;
9ce192d4
RD
1286}
1287
9e730a78
RD
1288
1289void ListBoxImpl::GetValue(int n, char *value, int len) {
1290 wxListItem item;
1291 item.SetId(n);
1292 item.SetColumn(1);
1293 item.SetMask(wxLIST_MASK_TEXT);
1294 GETLB(id)->GetItem(item);
1295 strncpy(value, wx2stc(item.GetText()), len);
9ce192d4
RD
1296 value[len-1] = '\0';
1297}
1298
9e730a78
RD
1299
1300void ListBoxImpl::RegisterImage(int type, const char *xpm_data) {
1301 wxMemoryInputStream stream(xpm_data, strlen(xpm_data)+1);
c8b75e94
WS
1302 wxImage img(stream, wxBITMAP_TYPE_XPM);
1303 wxBitmap bmp(img);
9e730a78
RD
1304
1305 if (! imgList) {
1306 // assumes all images are the same size
7e126a07 1307 imgList = new wxImageList(bmp.GetWidth(), bmp.GetHeight(), true);
9e730a78
RD
1308 imgTypeMap = new wxArrayInt;
1309 }
1310
1311 int idx = imgList->Add(bmp);
1312
1313 // do we need to extend the mapping array?
1314 wxArrayInt& itm = *imgTypeMap;
9a8ffadd 1315 if ( itm.GetCount() < (size_t)type+1)
9e730a78
RD
1316 itm.Add(-1, type - itm.GetCount() + 1);
1317
1318 // Add an item that maps type to the image index
1319 itm[type] = idx;
1320}
1321
1322void ListBoxImpl::ClearRegisteredImages() {
1323 if (imgList) {
1324 delete imgList;
1325 imgList = NULL;
1326 }
1327 if (imgTypeMap) {
1328 delete imgTypeMap;
1329 imgTypeMap = NULL;
1330 }
1331 if (id)
1332 GETLB(id)->SetImageList(NULL, wxIMAGE_LIST_SMALL);
1333}
1334
1335
1336void ListBoxImpl::SetDoubleClickAction(CallBackAction action, void *data) {
1337 GETLBW(id)->SetDoubleClickAction(action, data);
1338}
1339
1340
9e730a78
RD
1341ListBox::ListBox() {
1342}
1343
1344ListBox::~ListBox() {
1345}
1346
1347ListBox *ListBox::Allocate() {
1348 return new ListBoxImpl();
9ce192d4
RD
1349}
1350
1a2fb4cd 1351//----------------------------------------------------------------------
9ce192d4
RD
1352
1353Menu::Menu() : id(0) {
1354}
1355
1356void Menu::CreatePopUp() {
1357 Destroy();
1358 id = new wxMenu();
1359}
1360
1361void Menu::Destroy() {
1362 if (id)
1a2fb4cd 1363 delete (wxMenu*)id;
9ce192d4
RD
1364 id = 0;
1365}
1366
1367void Menu::Show(Point pt, Window &w) {
1a2fb4cd 1368 GETWIN(w.GetID())->PopupMenu((wxMenu*)id, pt.x - 4, pt.y);
9ce192d4
RD
1369 Destroy();
1370}
1371
1a2fb4cd 1372//----------------------------------------------------------------------
9ce192d4 1373
88a8b04e 1374DynamicLibrary *DynamicLibrary::Load(const char *WXUNUSED(modulePath)) {
e14d10b0
RD
1375 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1376 return NULL;
1377}
1378
1379//----------------------------------------------------------------------
1380
1a2fb4cd 1381ColourDesired Platform::Chrome() {
9ce192d4 1382 wxColour c;
e1c6c6ae 1383 c = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
1a2fb4cd 1384 return ColourDesired(c.Red(), c.Green(), c.Blue());
9ce192d4
RD
1385}
1386
1a2fb4cd 1387ColourDesired Platform::ChromeHighlight() {
9ce192d4 1388 wxColour c;
e1c6c6ae 1389 c = wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT);
1a2fb4cd 1390 return ColourDesired(c.Red(), c.Green(), c.Blue());
9ce192d4
RD
1391}
1392
1393const char *Platform::DefaultFont() {
10ef30eb
RD
1394 static char buf[128];
1395 strcpy(buf, wxNORMAL_FONT->GetFaceName().mbc_str());
1396 return buf;
9ce192d4
RD
1397}
1398
1399int Platform::DefaultFontSize() {
9e730a78 1400 return wxNORMAL_FONT->GetPointSize();
9ce192d4
RD
1401}
1402
1403unsigned int Platform::DoubleClickTime() {
1404 return 500; // **** ::GetDoubleClickTime();
1405}
1406
9e730a78 1407bool Platform::MouseButtonBounce() {
7e126a07 1408 return false;
9e730a78 1409}
9ce192d4 1410
88a8b04e 1411bool Platform::IsKeyDown(int WXUNUSED(key)) {
9ce192d4
RD
1412 return false; // I don't think we'll need this.
1413}
1414
1415long Platform::SendScintilla(WindowID w,
1416 unsigned int msg,
1417 unsigned long wParam,
1418 long lParam) {
1419
1420 wxStyledTextCtrl* stc = (wxStyledTextCtrl*)w;
1421 return stc->SendMsg(msg, wParam, lParam);
1422}
1423
a834585d
RD
1424long Platform::SendScintillaPointer(WindowID w,
1425 unsigned int msg,
1426 unsigned long wParam,
1427 void *lParam) {
1428
1429 wxStyledTextCtrl* stc = (wxStyledTextCtrl*)w;
b796ba39 1430 return stc->SendMsg(msg, wParam, (wxIntPtr)lParam);
a834585d
RD
1431}
1432
9ce192d4
RD
1433
1434// These are utility functions not really tied to a platform
1435
1436int Platform::Minimum(int a, int b) {
1437 if (a < b)
1438 return a;
1439 else
1440 return b;
1441}
1442
1443int Platform::Maximum(int a, int b) {
1444 if (a > b)
1445 return a;
1446 else
1447 return b;
1448}
1449
74e1efdd
VZ
1450//#define TRACE
1451
1452void Platform::DebugDisplay(const char *s) {
1453#ifdef TRACE
1454 wxLogDebug(stc2wx(s));
1455#else
1456 wxUnusedVar(s);
1457#endif
1458}
9ce192d4
RD
1459
1460void Platform::DebugPrintf(const char *format, ...) {
1461#ifdef TRACE
1462 char buffer[2000];
1463 va_list pArguments;
1464 va_start(pArguments, format);
1465 vsprintf(buffer,format,pArguments);
1466 va_end(pArguments);
1467 Platform::DebugDisplay(buffer);
8d3a5374
VZ
1468#else
1469 wxUnusedVar(format);
9ce192d4
RD
1470#endif
1471}
1472
65ec6247
RD
1473
1474static bool assertionPopUps = true;
1475
1476bool Platform::ShowAssertionPopUps(bool assertionPopUps_) {
7e126a07
WS
1477 bool ret = assertionPopUps;
1478 assertionPopUps = assertionPopUps_;
1479 return ret;
65ec6247
RD
1480}
1481
1482void Platform::Assert(const char *c, const char *file, int line) {
74e1efdd 1483#ifdef TRACE
7e126a07
WS
1484 char buffer[2000];
1485 sprintf(buffer, "Assertion [%s] failed at %s %d", c, file, line);
1486 if (assertionPopUps) {
1487 /*int idButton = */
1488 wxMessageBox(stc2wx(buffer),
1489 wxT("Assertion failure"),
1490 wxICON_HAND | wxOK);
7e126a07
WS
1491 } else {
1492 strcat(buffer, "\r\n");
1493 Platform::DebugDisplay(buffer);
1494 abort();
1495 }
74e1efdd
VZ
1496#else
1497 wxUnusedVar(c);
1498 wxUnusedVar(file);
1499 wxUnusedVar(line);
1500#endif
65ec6247
RD
1501}
1502
1503
9ce192d4
RD
1504int Platform::Clamp(int val, int minVal, int maxVal) {
1505 if (val > maxVal)
1506 val = maxVal;
1507 if (val < minVal)
1508 val = minVal;
1509 return val;
1510}
1511
1512
88a8b04e 1513bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage), char WXUNUSED(ch)) {
1a2fb4cd
RD
1514 return false;
1515}
1516
88a8b04e 1517int Platform::DBCSCharLength(int WXUNUSED(codePage), const char *WXUNUSED(s)) {
215ef7ae 1518 return 1;
9e730a78
RD
1519}
1520
1521int Platform::DBCSCharMaxLength() {
215ef7ae 1522 return 1;
9e730a78 1523}
1a2fb4cd
RD
1524
1525
1526//----------------------------------------------------------------------
1527
1528ElapsedTime::ElapsedTime() {
1d94e268
WS
1529 wxLongLong localTime = wxGetLocalTimeMillis();
1530 littleBit = localTime.GetLo();
1531 bigBit = localTime.GetHi();
1a2fb4cd
RD
1532}
1533
1534double ElapsedTime::Duration(bool reset) {
1d94e268
WS
1535 wxLongLong prevTime(bigBit, littleBit);
1536 wxLongLong localTime = wxGetLocalTimeMillis();
1537 if(reset) {
1538 littleBit = localTime.GetLo();
1539 bigBit = localTime.GetHi();
1540 }
1541 wxLongLong duration = localTime - prevTime;
1542 double result = duration.ToDouble();
1a2fb4cd
RD
1543 result /= 1000.0;
1544 return result;
1545}
1546
1547
1548//----------------------------------------------------------------------
9ce192d4 1549
d99859e4 1550#if wxUSE_UNICODE
6636ac1e
RD
1551
1552#include "UniConversion.h"
1553
1554// Convert using Scintilla's functions instead of wx's, Scintilla's are more
1555// forgiving and won't assert...
c8b75e94 1556
d99859e4
RD
1557wxString stc2wx(const char* str, size_t len)
1558{
81bfa5ae
RD
1559 if (!len)
1560 return wxEmptyString;
8a07b43c 1561
7e0c58e9 1562 size_t wclen = UTF16Length(str, len);
6636ac1e
RD
1563 wxWCharBuffer buffer(wclen+1);
1564
7e0c58e9 1565 size_t actualLen = UTF16FromUTF8(str, len, buffer.data(), wclen+1);
6636ac1e
RD
1566 return wxString(buffer.data(), actualLen);
1567}
1568
d99859e4 1569
d99859e4 1570
6636ac1e
RD
1571wxString stc2wx(const char* str)
1572{
1573 return stc2wx(str, strlen(str));
d99859e4 1574}
6636ac1e
RD
1575
1576
1577const wxWX2MBbuf wx2stc(const wxString& str)
1578{
1579 const wchar_t* wcstr = str.c_str();
1580 size_t wclen = str.length();
1581 size_t len = UTF8Length(wcstr, wclen);
1582
1583 wxCharBuffer buffer(len+1);
7e0c58e9 1584 UTF8FromUTF16(wcstr, wclen, buffer.data(), len);
6636ac1e
RD
1585
1586 // TODO check NULL termination!!
1587
6636ac1e
RD
1588 return buffer;
1589}
1590
d99859e4 1591#endif
29825f5f
PC
1592
1593#endif // wxUSE_STC