]> git.saurik.com Git - wxWidgets.git/blame - src/stc/PlatWX.cpp
XML code fixes (patch #815196)
[wxWidgets.git] / src / stc / PlatWX.cpp
CommitLineData
9ce192d4
RD
1// Scintilla source code edit control
2// PlatWX.cxx - implementation of platform facilities on wxWindows
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
f6bcfd97 7#include <ctype.h>
9ce192d4 8
1a2fb4cd 9#include <wx/wx.h>
10ef30eb 10#include <wx/encconv.h>
9e730a78
RD
11#include <wx/listctrl.h>
12#include <wx/mstream.h>
13#include <wx/image.h>
14#include <wx/imaglist.h>
1a2fb4cd 15
9ce192d4 16#include "Platform.h"
1a2fb4cd 17#include "PlatWX.h"
9ce192d4
RD
18#include "wx/stc/stc.h"
19
f97d84a6
RD
20
21#ifdef __WXGTK__
22#include <gtk/gtk.h>
23#endif
24
1a2fb4cd 25
9ce192d4 26Point Point::FromLong(long lpoint) {
f6bcfd97 27 return Point(lpoint & 0xFFFF, lpoint >> 16);
9ce192d4
RD
28}
29
30wxRect wxRectFromPRectangle(PRectangle prc) {
31 wxRect rc(prc.left, prc.top,
21156596 32 prc.right-prc.left, prc.bottom-prc.top);
9ce192d4
RD
33 return rc;
34}
35
36PRectangle PRectangleFromwxRect(wxRect rc) {
bec17edf
RD
37 return PRectangle(rc.GetLeft(), rc.GetTop(),
38 rc.GetRight()+1, rc.GetBottom()+1);
9ce192d4
RD
39}
40
1a2fb4cd
RD
41wxColour wxColourFromCA(const ColourAllocated& ca) {
42 ColourDesired cd(ca.AsLong());
43 return wxColour(cd.GetRed(), cd.GetGreen(), cd.GetBlue());
9ce192d4
RD
44}
45
1a2fb4cd 46//----------------------------------------------------------------------
9ce192d4
RD
47
48Palette::Palette() {
49 used = 0;
50 allowRealization = false;
51}
52
53Palette::~Palette() {
54 Release();
55}
56
57void Palette::Release() {
58 used = 0;
59}
60
61// This method either adds a colour to the list of wanted colours (want==true)
62// or retrieves the allocated colour back to the ColourPair.
63// This is one method to make it easier to keep the code for wanting and retrieving in sync.
64void Palette::WantFind(ColourPair &cp, bool want) {
65 if (want) {
66 for (int i=0; i < used; i++) {
67 if (entries[i].desired == cp.desired)
68 return;
69 }
70
71 if (used < numEntries) {
72 entries[used].desired = cp.desired;
1a2fb4cd 73 entries[used].allocated.Set(cp.desired.AsLong());
9ce192d4
RD
74 used++;
75 }
76 } else {
77 for (int i=0; i < used; i++) {
78 if (entries[i].desired == cp.desired) {
79 cp.allocated = entries[i].allocated;
80 return;
81 }
82 }
1a2fb4cd 83 cp.allocated.Set(cp.desired.AsLong());
9ce192d4
RD
84 }
85}
86
87void Palette::Allocate(Window &) {
88 if (allowRealization) {
89 }
90}
91
92
1a2fb4cd
RD
93//----------------------------------------------------------------------
94
9ce192d4
RD
95Font::Font() {
96 id = 0;
97 ascent = 0;
98}
99
100Font::~Font() {
101}
102
f6bcfd97 103void Font::Create(const char *faceName, int characterSet, int size, bool bold, bool italic) {
1a2fb4cd 104 wxFontEncoding encoding;
65ec6247 105
9ce192d4 106 Release();
1a2fb4cd
RD
107
108 switch (characterSet) {
109 default:
110 case wxSTC_CHARSET_ANSI:
111 case wxSTC_CHARSET_DEFAULT:
112 encoding = wxFONTENCODING_DEFAULT;
113 break;
114
115 case wxSTC_CHARSET_BALTIC:
116 encoding = wxFONTENCODING_ISO8859_13;
117 break;
118
119 case wxSTC_CHARSET_CHINESEBIG5:
120 encoding = wxFONTENCODING_CP950;
121 break;
122
123 case wxSTC_CHARSET_EASTEUROPE:
124 encoding = wxFONTENCODING_ISO8859_2;
125 break;
126
127 case wxSTC_CHARSET_GB2312:
128 encoding = wxFONTENCODING_CP936;
129 break;
130
131 case wxSTC_CHARSET_GREEK:
132 encoding = wxFONTENCODING_ISO8859_7;
133 break;
134
135 case wxSTC_CHARSET_HANGUL:
136 encoding = wxFONTENCODING_CP949;
137 break;
138
139 case wxSTC_CHARSET_MAC:
140 encoding = wxFONTENCODING_DEFAULT;
141 break;
142
143 case wxSTC_CHARSET_OEM:
144 encoding = wxFONTENCODING_DEFAULT;
145 break;
146
147 case wxSTC_CHARSET_RUSSIAN:
148 encoding = wxFONTENCODING_KOI8;
149 break;
150
151 case wxSTC_CHARSET_SHIFTJIS:
152 encoding = wxFONTENCODING_CP932;
153 break;
154
155 case wxSTC_CHARSET_SYMBOL:
156 encoding = wxFONTENCODING_DEFAULT;
157 break;
158
159 case wxSTC_CHARSET_TURKISH:
160 encoding = wxFONTENCODING_ISO8859_9;
161 break;
162
163 case wxSTC_CHARSET_JOHAB:
164 encoding = wxFONTENCODING_DEFAULT;
165 break;
166
167 case wxSTC_CHARSET_HEBREW:
168 encoding = wxFONTENCODING_ISO8859_8;
169 break;
170
171 case wxSTC_CHARSET_ARABIC:
172 encoding = wxFONTENCODING_ISO8859_6;
173 break;
174
175 case wxSTC_CHARSET_VIETNAMESE:
176 encoding = wxFONTENCODING_DEFAULT;
177 break;
178
179 case wxSTC_CHARSET_THAI:
180 encoding = wxFONTENCODING_ISO8859_11;
181 break;
182 }
183
10ef30eb
RD
184 wxFontEncodingArray ea = wxEncodingConverter::GetPlatformEquivalents(encoding);
185 if (ea.GetCount())
186 encoding = ea[0];
1a2fb4cd 187
9ce192d4
RD
188 id = new wxFont(size,
189 wxDEFAULT,
190 italic ? wxITALIC : wxNORMAL,
191 bold ? wxBOLD : wxNORMAL,
192 false,
0c5b83b0 193 stc2wx(faceName),
1a2fb4cd 194 encoding);
9ce192d4
RD
195}
196
197
198void Font::Release() {
199 if (id)
1a2fb4cd 200 delete (wxFont*)id;
9ce192d4
RD
201 id = 0;
202}
203
1a2fb4cd
RD
204//----------------------------------------------------------------------
205
206class SurfaceImpl : public Surface {
207private:
208 wxDC* hdc;
209 bool hdcOwned;
210 wxBitmap* bitmap;
211 int x;
212 int y;
213 bool unicodeMode;
9ce192d4 214
1a2fb4cd
RD
215public:
216 SurfaceImpl();
217 ~SurfaceImpl();
218
9e730a78
RD
219 virtual void Init(WindowID wid);
220 virtual void Init(SurfaceID sid, WindowID wid);
221 virtual void InitPixMap(int width, int height, Surface *surface_, WindowID wid);
222
223 virtual void Release();
224 virtual bool Initialised();
225 virtual void PenColour(ColourAllocated fore);
226 virtual int LogPixelsY();
227 virtual int DeviceHeightFont(int points);
228 virtual void MoveTo(int x_, int y_);
229 virtual void LineTo(int x_, int y_);
230 virtual void Polygon(Point *pts, int npts, ColourAllocated fore, ColourAllocated back);
231 virtual void RectangleDraw(PRectangle rc, ColourAllocated fore, ColourAllocated back);
232 virtual void FillRectangle(PRectangle rc, ColourAllocated back);
233 virtual void FillRectangle(PRectangle rc, Surface &surfacePattern);
234 virtual void RoundedRectangle(PRectangle rc, ColourAllocated fore, ColourAllocated back);
235 virtual void Ellipse(PRectangle rc, ColourAllocated fore, ColourAllocated back);
236 virtual void Copy(PRectangle rc, Point from, Surface &surfaceSource);
237
238 virtual void DrawTextNoClip(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore, ColourAllocated back);
239 virtual void DrawTextClipped(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore, ColourAllocated back);
240 virtual void DrawTextTransparent(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore);
241 virtual void MeasureWidths(Font &font_, const char *s, int len, int *positions);
242 virtual int WidthText(Font &font_, const char *s, int len);
243 virtual int WidthChar(Font &font_, char ch);
244 virtual int Ascent(Font &font_);
245 virtual int Descent(Font &font_);
246 virtual int InternalLeading(Font &font_);
247 virtual int ExternalLeading(Font &font_);
248 virtual int Height(Font &font_);
249 virtual int AverageCharWidth(Font &font_);
250
251 virtual int SetPalette(Palette *pal, bool inBackGround);
252 virtual void SetClip(PRectangle rc);
253 virtual void FlushCachedState();
254
255 virtual void SetUnicodeMode(bool unicodeMode_);
256 virtual void SetDBCSMode(int codePage);
1a2fb4cd
RD
257
258 void BrushColour(ColourAllocated back);
259 void SetFont(Font &font_);
260};
261
262
263
264SurfaceImpl::SurfaceImpl() :
9ce192d4 265 hdc(0), hdcOwned(0), bitmap(0),
1a2fb4cd
RD
266 x(0), y(0), unicodeMode(0)
267{}
9ce192d4 268
1a2fb4cd 269SurfaceImpl::~SurfaceImpl() {
9ce192d4
RD
270 Release();
271}
272
9e730a78 273void SurfaceImpl::Init(WindowID wid) {
81b32ce5 274#if 0
9ce192d4
RD
275 Release();
276 hdc = new wxMemoryDC();
277 hdcOwned = true;
81b32ce5 278#else
09bb2551 279 // On Mac and GTK the DC is not really valid until it has a bitmap
81b32ce5
RD
280 // selected into it. So instead of just creating the DC with no bitmap,
281 // go ahead and give it one.
9e730a78 282 InitPixMap(1,1,NULL,wid);
0f713d48 283#endif
9ce192d4
RD
284}
285
9e730a78 286void SurfaceImpl::Init(SurfaceID hdc_, WindowID) {
9ce192d4 287 Release();
1a2fb4cd 288 hdc = (wxDC*)hdc_;
9ce192d4
RD
289}
290
88a8b04e 291void SurfaceImpl::InitPixMap(int width, int height, Surface *WXUNUSED(surface_), WindowID) {
9ce192d4 292 Release();
1a2fb4cd 293 hdc = new wxMemoryDC();
9ce192d4 294 hdcOwned = true;
2beb01db
RD
295 if (width < 1) width = 1;
296 if (height < 1) height = 1;
21156596 297 bitmap = new wxBitmap(width, height);
9ce192d4 298 ((wxMemoryDC*)hdc)->SelectObject(*bitmap);
9ce192d4
RD
299}
300
9e730a78
RD
301
302void SurfaceImpl::Release() {
303 if (bitmap) {
304 ((wxMemoryDC*)hdc)->SelectObject(wxNullBitmap);
305 delete bitmap;
306 bitmap = 0;
307 }
308 if (hdcOwned) {
309 delete hdc;
310 hdc = 0;
311 hdcOwned = false;
312 }
313}
314
315
316bool SurfaceImpl::Initialised() {
317 return hdc != 0;
318}
319
320
1a2fb4cd
RD
321void SurfaceImpl::PenColour(ColourAllocated fore) {
322 hdc->SetPen(wxPen(wxColourFromCA(fore), 1, wxSOLID));
9ce192d4
RD
323}
324
1a2fb4cd
RD
325void SurfaceImpl::BrushColour(ColourAllocated back) {
326 hdc->SetBrush(wxBrush(wxColourFromCA(back), wxSOLID));
9ce192d4
RD
327}
328
1a2fb4cd 329void SurfaceImpl::SetFont(Font &font_) {
21156596 330 if (font_.GetID()) {
1a2fb4cd 331 hdc->SetFont(*((wxFont*)font_.GetID()));
f6bcfd97 332 }
9ce192d4
RD
333}
334
1a2fb4cd 335int SurfaceImpl::LogPixelsY() {
9ce192d4
RD
336 return hdc->GetPPI().y;
337}
338
1a2fb4cd 339int SurfaceImpl::DeviceHeightFont(int points) {
9968ba85 340 return points;
f6bcfd97
BP
341}
342
1a2fb4cd 343void SurfaceImpl::MoveTo(int x_, int y_) {
9ce192d4
RD
344 x = x_;
345 y = y_;
346}
347
1a2fb4cd 348void SurfaceImpl::LineTo(int x_, int y_) {
9ce192d4
RD
349 hdc->DrawLine(x,y, x_,y_);
350 x = x_;
351 y = y_;
352}
353
1a2fb4cd 354void SurfaceImpl::Polygon(Point *pts, int npts, ColourAllocated fore, ColourAllocated back) {
9ce192d4 355 PenColour(fore);
1a2fb4cd 356 BrushColour(back);
9ce192d4
RD
357 hdc->DrawPolygon(npts, (wxPoint*)pts);
358}
359
1a2fb4cd 360void SurfaceImpl::RectangleDraw(PRectangle rc, ColourAllocated fore, ColourAllocated back) {
9ce192d4 361 PenColour(fore);
1a2fb4cd 362 BrushColour(back);
9ce192d4
RD
363 hdc->DrawRectangle(wxRectFromPRectangle(rc));
364}
365
1a2fb4cd
RD
366void SurfaceImpl::FillRectangle(PRectangle rc, ColourAllocated back) {
367 BrushColour(back);
9ce192d4
RD
368 hdc->SetPen(*wxTRANSPARENT_PEN);
369 hdc->DrawRectangle(wxRectFromPRectangle(rc));
370}
371
1a2fb4cd 372void SurfaceImpl::FillRectangle(PRectangle rc, Surface &surfacePattern) {
9ce192d4 373 wxBrush br;
1a2fb4cd
RD
374 if (((SurfaceImpl&)surfacePattern).bitmap)
375 br = wxBrush(*((SurfaceImpl&)surfacePattern).bitmap);
9ce192d4
RD
376 else // Something is wrong so display in red
377 br = wxBrush(*wxRED, wxSOLID);
378 hdc->SetPen(*wxTRANSPARENT_PEN);
379 hdc->SetBrush(br);
380 hdc->DrawRectangle(wxRectFromPRectangle(rc));
381}
382
1a2fb4cd 383void SurfaceImpl::RoundedRectangle(PRectangle rc, ColourAllocated fore, ColourAllocated back) {
9ce192d4 384 PenColour(fore);
1a2fb4cd 385 BrushColour(back);
f6bcfd97 386 hdc->DrawRoundedRectangle(wxRectFromPRectangle(rc), 4);
9ce192d4
RD
387}
388
1a2fb4cd 389void SurfaceImpl::Ellipse(PRectangle rc, ColourAllocated fore, ColourAllocated back) {
9ce192d4 390 PenColour(fore);
1a2fb4cd 391 BrushColour(back);
9ce192d4
RD
392 hdc->DrawEllipse(wxRectFromPRectangle(rc));
393}
394
1a2fb4cd 395void SurfaceImpl::Copy(PRectangle rc, Point from, Surface &surfaceSource) {
f6bcfd97
BP
396 wxRect r = wxRectFromPRectangle(rc);
397 hdc->Blit(r.x, r.y, r.width, r.height,
1a2fb4cd
RD
398 ((SurfaceImpl&)surfaceSource).hdc,
399 from.x, from.y, wxCOPY);
9ce192d4
RD
400}
401
1a2fb4cd
RD
402void SurfaceImpl::DrawTextNoClip(PRectangle rc, Font &font, int ybase,
403 const char *s, int len,
404 ColourAllocated fore, ColourAllocated back) {
9ce192d4 405 SetFont(font);
1a2fb4cd
RD
406 hdc->SetTextForeground(wxColourFromCA(fore));
407 hdc->SetTextBackground(wxColourFromCA(back));
3d7a4fe8 408 FillRectangle(rc, back);
9ce192d4
RD
409
410 // ybase is where the baseline should be, but wxWin uses the upper left
411 // corner, so I need to calculate the real position for the text...
0c5b83b0 412 hdc->DrawText(stc2wx(s, len), rc.left, ybase - font.ascent);
9ce192d4
RD
413}
414
1a2fb4cd
RD
415void SurfaceImpl::DrawTextClipped(PRectangle rc, Font &font, int ybase,
416 const char *s, int len,
417 ColourAllocated fore, ColourAllocated back) {
9ce192d4 418 SetFont(font);
1a2fb4cd
RD
419 hdc->SetTextForeground(wxColourFromCA(fore));
420 hdc->SetTextBackground(wxColourFromCA(back));
3d7a4fe8 421 FillRectangle(rc, back);
9ce192d4
RD
422 hdc->SetClippingRegion(wxRectFromPRectangle(rc));
423
424 // see comments above
0c5b83b0 425 hdc->DrawText(stc2wx(s, len), rc.left, ybase - font.ascent);
3d7a4fe8 426 hdc->DestroyClippingRegion();
9ce192d4
RD
427}
428
9e730a78
RD
429
430void SurfaceImpl::DrawTextTransparent(PRectangle rc, Font &font, int ybase,
431 const char *s, int len,
432 ColourAllocated fore) {
433
9ce192d4 434 SetFont(font);
9e730a78
RD
435 hdc->SetTextForeground(wxColourFromCA(fore));
436 hdc->SetBackgroundMode(wxTRANSPARENT);
1a2fb4cd 437
9e730a78
RD
438 // ybase is where the baseline should be, but wxWin uses the upper left
439 // corner, so I need to calculate the real position for the text...
440 hdc->DrawText(stc2wx(s, len), rc.left, ybase - font.ascent);
441
442 hdc->SetBackgroundMode(wxSOLID);
9ce192d4
RD
443}
444
1a2fb4cd 445
10ef30eb 446void SurfaceImpl::MeasureWidths(Font &font, const char *s, int len, int *positions) {
9e730a78 447
0c5b83b0 448 wxString str = stc2wx(s, len);
9ce192d4 449 SetFont(font);
10ef30eb 450
9e730a78 451#ifndef __WXMAC__
10ef30eb
RD
452 // Calculate the position of each character based on the widths of
453 // the previous characters
3d7a4fe8 454 int* tpos = new int[len+1];
9ce192d4 455 int totalWidth = 0;
10ef30eb
RD
456 size_t i;
457 for (i=0; i<str.Length(); i++) {
458 int w, h;
1a2fb4cd 459 hdc->GetTextExtent(str[i], &w, &h);
9ce192d4 460 totalWidth += w;
10ef30eb 461 tpos[i] = totalWidth;
9ce192d4 462 }
9e730a78
RD
463#else
464 // Instead of a running total, remeasure from the begining of the
465 // text for each character's position. This is because with AA fonts
466 // on OS X widths can be fractions of pixels wide when more than one
467 // are drawn together, so the sum of all character widths is not necessarily
468 // (and probably not) the same as the whole string width.
3d7a4fe8 469 int* tpos = new int[len+1];
9e730a78
RD
470 size_t i;
471 for (i=0; i<str.Length(); i++) {
472 int w, h;
473 hdc->GetTextExtent(str.Left(i+1), &w, &h);
474 tpos[i] = w;
475 }
476#endif
477
10ef30eb
RD
478
479#if wxUSE_UNICODE
480 // Map the widths for UCS-2 characters back to the UTF-8 input string
9e730a78
RD
481 // NOTE: I don't think this is right for when sizeof(wxChar) > 2, ie wxGTK2
482 // so figure it out and fix it!
10ef30eb
RD
483 i = 0;
484 size_t ui = 0;
d99859e4 485 while ((int)i < len) {
10ef30eb
RD
486 unsigned char uch = (unsigned char)s[i];
487 positions[i++] = tpos[ui];
488 if (uch >= 0x80) {
489 if (uch < (0x80 + 0x40 + 0x20)) {
490 positions[i++] = tpos[ui];
491 } else {
492 positions[i++] = tpos[ui];
493 positions[i++] = tpos[ui];
494 }
495 }
496 ui++;
497 }
498#else
499
500 // If not unicode then just use the widths we have
501 memcpy(positions, tpos, len * sizeof(*tpos));
502#endif
503
504 delete [] tpos;
9ce192d4
RD
505}
506
10ef30eb 507
9e730a78
RD
508int SurfaceImpl::WidthText(Font &font, const char *s, int len) {
509 SetFont(font);
510 int w;
511 int h;
512
513 hdc->GetTextExtent(stc2wx(s, len), &w, &h);
514 return w;
515}
516
517
1a2fb4cd 518int SurfaceImpl::WidthChar(Font &font, char ch) {
9ce192d4
RD
519 SetFont(font);
520 int w;
521 int h;
10ef30eb
RD
522 char s[2] = { ch, 0 };
523
0c5b83b0 524 hdc->GetTextExtent(stc2wx(s, 1), &w, &h);
9ce192d4
RD
525 return w;
526}
527
1a2fb4cd 528#define EXTENT_TEST wxT(" `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
9ce192d4 529
1a2fb4cd 530int SurfaceImpl::Ascent(Font &font) {
9ce192d4
RD
531 SetFont(font);
532 int w, h, d, e;
533 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
534 font.ascent = h - d;
535 return font.ascent;
536}
537
1a2fb4cd 538int SurfaceImpl::Descent(Font &font) {
9ce192d4
RD
539 SetFont(font);
540 int w, h, d, e;
541 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
542 return d;
543}
544
88a8b04e 545int SurfaceImpl::InternalLeading(Font &WXUNUSED(font)) {
9ce192d4
RD
546 return 0;
547}
548
1a2fb4cd 549int SurfaceImpl::ExternalLeading(Font &font) {
9ce192d4
RD
550 SetFont(font);
551 int w, h, d, e;
552 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
553 return e;
554}
555
1a2fb4cd 556int SurfaceImpl::Height(Font &font) {
9ce192d4
RD
557 SetFont(font);
558 return hdc->GetCharHeight();
559}
560
1a2fb4cd 561int SurfaceImpl::AverageCharWidth(Font &font) {
9ce192d4
RD
562 SetFont(font);
563 return hdc->GetCharWidth();
564}
565
88a8b04e 566int SurfaceImpl::SetPalette(Palette *WXUNUSED(pal), bool WXUNUSED(inBackGround)) {
65ec6247 567 return 0;
9ce192d4
RD
568}
569
1a2fb4cd 570void SurfaceImpl::SetClip(PRectangle rc) {
9ce192d4
RD
571 hdc->SetClippingRegion(wxRectFromPRectangle(rc));
572}
573
1a2fb4cd 574void SurfaceImpl::FlushCachedState() {
f6bcfd97 575}
9ce192d4 576
1a2fb4cd 577void SurfaceImpl::SetUnicodeMode(bool unicodeMode_) {
1a2fb4cd
RD
578 unicodeMode=unicodeMode_;
579}
580
88a8b04e 581void SurfaceImpl::SetDBCSMode(int WXUNUSED(codePage)) {
9e730a78
RD
582 // dbcsMode = codePage == SC_CP_DBCS;
583}
584
585
1a2fb4cd
RD
586Surface *Surface::Allocate() {
587 return new SurfaceImpl;
588}
589
590
591//----------------------------------------------------------------------
592
593
594inline wxWindow* GETWIN(WindowID id) { return (wxWindow*)id; }
595
9ce192d4
RD
596Window::~Window() {
597}
598
599void Window::Destroy() {
9e730a78
RD
600 if (id) {
601 Show(FALSE);
1a2fb4cd 602 GETWIN(id)->Destroy();
9e730a78 603 }
9ce192d4
RD
604 id = 0;
605}
606
607bool Window::HasFocus() {
1a2fb4cd 608 return wxWindow::FindFocus() == GETWIN(id);
9ce192d4
RD
609}
610
611PRectangle Window::GetPosition() {
9e730a78 612 if (! id) return PRectangle();
1a2fb4cd 613 wxRect rc(GETWIN(id)->GetPosition(), GETWIN(id)->GetSize());
9ce192d4
RD
614 return PRectangleFromwxRect(rc);
615}
616
617void Window::SetPosition(PRectangle rc) {
f6bcfd97 618 wxRect r = wxRectFromPRectangle(rc);
1a2fb4cd 619 GETWIN(id)->SetSize(r);
9ce192d4
RD
620}
621
622void Window::SetPositionRelative(PRectangle rc, Window) {
623 SetPosition(rc); // ????
624}
625
626PRectangle Window::GetClientPosition() {
9e730a78 627 if (! id) return PRectangle();
1a2fb4cd 628 wxSize sz = GETWIN(id)->GetClientSize();
21156596 629 return PRectangle(0, 0, sz.x, sz.y);
9ce192d4
RD
630}
631
632void Window::Show(bool show) {
1a2fb4cd 633 GETWIN(id)->Show(show);
9ce192d4
RD
634}
635
636void Window::InvalidateAll() {
1a2fb4cd 637 GETWIN(id)->Refresh(false);
26f4607d 638 wxWakeUpIdle();
9ce192d4
RD
639}
640
641void Window::InvalidateRectangle(PRectangle rc) {
f6bcfd97 642 wxRect r = wxRectFromPRectangle(rc);
1a2fb4cd 643 GETWIN(id)->Refresh(false, &r);
26f4607d 644 wxWakeUpIdle();
9ce192d4
RD
645}
646
647void Window::SetFont(Font &font) {
1a2fb4cd 648 GETWIN(id)->SetFont(*((wxFont*)font.GetID()));
9ce192d4
RD
649}
650
651void Window::SetCursor(Cursor curs) {
652 int cursorId;
653
654 switch (curs) {
655 case cursorText:
656 cursorId = wxCURSOR_IBEAM;
657 break;
658 case cursorArrow:
659 cursorId = wxCURSOR_ARROW;
660 break;
661 case cursorUp:
662 cursorId = wxCURSOR_ARROW; // ** no up arrow... wxCURSOR_UPARROW;
663 break;
664 case cursorWait:
665 cursorId = wxCURSOR_WAIT;
666 break;
667 case cursorHoriz:
668 cursorId = wxCURSOR_SIZEWE;
669 break;
670 case cursorVert:
671 cursorId = wxCURSOR_SIZENS;
672 break;
673 case cursorReverseArrow:
15dadf31 674 cursorId = wxCURSOR_RIGHT_ARROW;
9ce192d4 675 break;
9e730a78
RD
676 case cursorHand:
677 cursorId = wxCURSOR_HAND;
1e545382 678 break;
9ce192d4
RD
679 default:
680 cursorId = wxCURSOR_ARROW;
681 break;
682 }
9f79d14b
CE
683#ifdef __WXMOTIF__
684 wxCursor wc = wxStockCursor(cursorId) ;
685#else
686 wxCursor wc = wxCursor(cursorId) ;
687#endif
cb1871ca 688 GETWIN(id)->SetCursor(wc);
9ce192d4
RD
689}
690
691
692void Window::SetTitle(const char *s) {
0c5b83b0 693 GETWIN(id)->SetTitle(stc2wx(s));
9ce192d4
RD
694}
695
696
769a9cb2
RD
697//----------------------------------------------------------------------
698// Helper classes for ListBox
699
267484bc 700
b0d0494f 701// This is a simple subclass of wxListView that just resets focus to the
9e730a78
RD
702// parent when it gets it.
703class wxSTCListBox : public wxListView {
451c5cc7 704public:
9e730a78
RD
705 wxSTCListBox(wxWindow* parent, wxWindowID id,
706 const wxPoint& pos, const wxSize& size,
707 long style)
708 : wxListView(parent, id, pos, size, style)
709 {}
451c5cc7
RD
710
711 void OnFocus(wxFocusEvent& event) {
712 GetParent()->SetFocus();
713 event.Skip();
714 }
715
b0d0494f
RD
716 void OnKillFocus(wxFocusEvent& event) {
717 // Do nothing. Prevents base class from resetting the colors...
718 }
719
451c5cc7
RD
720private:
721 DECLARE_EVENT_TABLE()
722};
723
9e730a78
RD
724BEGIN_EVENT_TABLE(wxSTCListBox, wxListView)
725 EVT_SET_FOCUS( wxSTCListBox::OnFocus)
b0d0494f 726 EVT_KILL_FOCUS(wxSTCListBox::OnKillFocus)
451c5cc7
RD
727END_EVENT_TABLE()
728
729
730
267484bc 731
9e730a78
RD
732// A window to place the wxSTCListBox upon
733class wxSTCListBoxWin : public wxWindow {
734private:
735 wxListView* lv;
736 CallBackAction doubleClickAction;
737 void* doubleClickActionData;
f97d84a6 738public:
9e730a78
RD
739 wxSTCListBoxWin(wxWindow* parent, wxWindowID id) :
740 wxWindow(parent, id, wxDefaultPosition, wxSize(0,0), wxNO_BORDER )
741 {
742
743 SetBackgroundColour(*wxBLACK);
744 lv = new wxSTCListBox(this, id, wxDefaultPosition, wxDefaultSize,
745 wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER | wxNO_BORDER);
746 lv->SetCursor(wxCursor(wxCURSOR_ARROW));
747 lv->InsertColumn(0, wxEmptyString);
748 lv->InsertColumn(1, wxEmptyString);
b0d0494f
RD
749
750 // Eventhough we immediately reset the focus to the parent, this helps
751 // things to look right...
752 lv->SetFocus();
753
9e730a78 754 Hide();
f97d84a6
RD
755 }
756
9e730a78
RD
757 int IconWidth() {
758 wxImageList* il = lv->GetImageList(wxIMAGE_LIST_SMALL);
759 if (il != NULL) {
760 int w, h;
761 il->GetSize(0, w, h);
762 return w;
763 }
764 return 0;
765 }
f97d84a6 766
769a9cb2 767
9e730a78
RD
768 void SetDoubleClickAction(CallBackAction action, void *data) {
769 doubleClickAction = action;
770 doubleClickActionData = data;
771 }
451c5cc7 772
769a9cb2 773
9e730a78
RD
774 void OnFocus(wxFocusEvent& event) {
775 GetParent()->SetFocus();
776 event.Skip();
777 }
769a9cb2
RD
778
779 void OnSize(wxSizeEvent& event) {
9e730a78
RD
780 // resize the child, leaving a 1 pixel border
781 wxSize sz = GetClientSize();
782 lv->SetSize(1, 1, sz.x-2, sz.y-2);
783 // reset the column widths
784 lv->SetColumnWidth(0, IconWidth()+4);
785 lv->SetColumnWidth(1, sz.x - 2 - lv->GetColumnWidth(0) -
786 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X));
787 event.Skip();
769a9cb2 788 }
769a9cb2 789
9e730a78
RD
790 void OnActivate(wxListEvent& event) {
791 doubleClickAction(doubleClickActionData);
769a9cb2 792 }
9e730a78
RD
793
794 wxListView* GetLB() { return lv; }
769a9cb2
RD
795
796private:
769a9cb2
RD
797 DECLARE_EVENT_TABLE()
798};
799
9e730a78
RD
800
801BEGIN_EVENT_TABLE(wxSTCListBoxWin, wxWindow)
802 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus)
803 EVT_SIZE ( wxSTCListBoxWin::OnSize)
804 EVT_LIST_ITEM_ACTIVATED(-1, wxSTCListBoxWin::OnActivate)
769a9cb2 805END_EVENT_TABLE()
769a9cb2 806
9e730a78
RD
807
808
809inline wxSTCListBoxWin* GETLBW(WindowID win) {
810 return ((wxSTCListBoxWin*)win);
811}
812
813inline wxListView* GETLB(WindowID win) {
814 return GETLBW(win)->GetLB();
1a2fb4cd 815}
769a9cb2
RD
816
817//----------------------------------------------------------------------
818
9e730a78
RD
819class ListBoxImpl : public ListBox {
820private:
821 int lineHeight;
822 bool unicodeMode;
823 int desiredVisibleRows;
824 int aveCharWidth;
825 int maxStrWidth;
826 wxImageList* imgList;
827 wxArrayInt* imgTypeMap;
828
829public:
830 ListBoxImpl();
831 ~ListBoxImpl();
832
833 virtual void SetFont(Font &font);
834 virtual void Create(Window &parent, int ctrlID, int lineHeight_, bool unicodeMode_);
835 virtual void SetAverageCharWidth(int width);
836 virtual void SetVisibleRows(int rows);
837 virtual PRectangle GetDesiredRect();
838 virtual int CaretFromEdge();
839 virtual void Clear();
840 virtual void Append(char *s, int type = -1);
841 virtual int Length();
842 virtual void Select(int n);
843 virtual int GetSelection();
844 virtual int Find(const char *prefix);
845 virtual void GetValue(int n, char *value, int len);
846 virtual void Sort();
847 virtual void RegisterImage(int type, const char *xpm_data);
848 virtual void ClearRegisteredImages();
849 virtual void SetDoubleClickAction(CallBackAction, void *);
850
851};
852
853
854ListBoxImpl::ListBoxImpl()
855 : lineHeight(10), unicodeMode(false),
856 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
857 imgList(NULL), imgTypeMap(NULL)
858{
9ce192d4
RD
859}
860
9e730a78
RD
861ListBoxImpl::~ListBoxImpl() {
862 if (imgList) {
863 delete imgList;
864 imgList = NULL;
865 }
866 if (imgTypeMap) {
867 delete imgTypeMap;
868 imgTypeMap = NULL;
869 }
9ce192d4
RD
870}
871
9e730a78
RD
872
873void ListBoxImpl::SetFont(Font &font) {
874 GETLB(id)->SetFont(*((wxFont*)font.GetID()));
875}
876
877
878void ListBoxImpl::Create(Window &parent, int ctrlID, int lineHeight_, bool unicodeMode_) {
879 lineHeight = lineHeight_;
880 unicodeMode = unicodeMode_;
881 maxStrWidth = 0;
1a2fb4cd 882 id = new wxSTCListBoxWin(GETWIN(parent.GetID()), ctrlID);
9e730a78
RD
883 if (imgList != NULL)
884 GETLB(id)->SetImageList(imgList, wxIMAGE_LIST_SMALL);
9ce192d4
RD
885}
886
9e730a78
RD
887
888void ListBoxImpl::SetAverageCharWidth(int width) {
889 aveCharWidth = width;
890}
891
892
893void ListBoxImpl::SetVisibleRows(int rows) {
769a9cb2 894 desiredVisibleRows = rows;
f3c2c221
RD
895}
896
9e730a78
RD
897
898PRectangle ListBoxImpl::GetDesiredRect() {
899 // wxListCtrl doesn't have a DoGetBestSize, so instead we kept track of
900 // the max size in Append and calculate it here...
901 int maxw = maxStrWidth;
1e545382 902 int maxh ;
9e730a78
RD
903
904 // give it a default if there are no lines, and/or add a bit more
905 if (maxw == 0) maxw = 100;
906 maxw += aveCharWidth * 3 +
907 GETLBW(id)->IconWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
908 if (maxw > 350)
909 maxw = 350;
910
911 // estimate a desired height
912 int count = GETLB(id)->GetItemCount();
913 if (count) {
914 wxRect rect;
915 GETLB(id)->GetItemRect(0, rect);
916 maxh = count * rect.GetHeight();
917 if (maxh > 140) // TODO: Use desiredVisibleRows??
918 maxh = 140;
919
920 // Try to make the size an exact multiple of some number of lines
921 int lines = maxh / rect.GetHeight();
922 maxh = (lines + 1) * rect.GetHeight() + 2;
923 }
924 else
925 maxh = 100;
926
d134f170
RD
927 PRectangle rc;
928 rc.top = 0;
929 rc.left = 0;
9e730a78
RD
930 rc.right = maxw;
931 rc.bottom = maxh;
d134f170
RD
932 return rc;
933}
934
d134f170 935
9e730a78
RD
936int ListBoxImpl::CaretFromEdge() {
937 return 4 + GETLBW(id)->IconWidth();
d134f170
RD
938}
939
9e730a78
RD
940
941void ListBoxImpl::Clear() {
942 GETLB(id)->DeleteAllItems();
9ce192d4
RD
943}
944
9e730a78
RD
945
946void ListBoxImpl::Append(char *s, int type) {
947 wxString text = stc2wx(s);
948 long count = GETLB(id)->GetItemCount();
949 long itemID = GETLB(id)->InsertItem(count, wxEmptyString);
950 GETLB(id)->SetItem(itemID, 1, text);
951 int itemWidth = 0;
952 GETLB(id)->GetTextExtent(text, &itemWidth, NULL);
953 maxStrWidth = wxMax(maxStrWidth, itemWidth);
954 if (type != -1) {
955 wxCHECK_RET(imgTypeMap, wxT("Unexpected NULL imgTypeMap"));
956 long idx = imgTypeMap->Item(type);
957 GETLB(id)->SetItemImage(itemID, idx, idx);
958 }
9ce192d4
RD
959}
960
9e730a78
RD
961
962int ListBoxImpl::Length() {
963 return GETLB(id)->GetItemCount();
9ce192d4
RD
964}
965
9e730a78
RD
966
967void ListBoxImpl::Select(int n) {
895066d8
RD
968 bool select = TRUE;
969 if (n == -1) {
970 n = 0;
971 select = FALSE;
972 }
9e730a78
RD
973 GETLB(id)->Focus(n);
974 GETLB(id)->Select(n, select);
9ce192d4
RD
975}
976
9e730a78
RD
977
978int ListBoxImpl::GetSelection() {
979 return GETLB(id)->GetFirstSelected();
9ce192d4
RD
980}
981
9e730a78 982
88a8b04e 983int ListBoxImpl::Find(const char *WXUNUSED(prefix)) {
b8b0e402 984 // No longer used
f6bcfd97 985 return -1;
9ce192d4
RD
986}
987
9e730a78
RD
988
989void ListBoxImpl::GetValue(int n, char *value, int len) {
990 wxListItem item;
991 item.SetId(n);
992 item.SetColumn(1);
993 item.SetMask(wxLIST_MASK_TEXT);
994 GETLB(id)->GetItem(item);
995 strncpy(value, wx2stc(item.GetText()), len);
9ce192d4
RD
996 value[len-1] = '\0';
997}
998
9e730a78
RD
999void ListBoxImpl::Sort() {
1000}
1001
1002
1003void ListBoxImpl::RegisterImage(int type, const char *xpm_data) {
1004 wxMemoryInputStream stream(xpm_data, strlen(xpm_data)+1);
1005 wxBitmap bmp(wxImage(stream, wxBITMAP_TYPE_XPM));
1006
1007 if (! imgList) {
1008 // assumes all images are the same size
1009 imgList = new wxImageList(bmp.GetWidth(), bmp.GetHeight(), TRUE);
1010 imgTypeMap = new wxArrayInt;
1011 }
1012
1013 int idx = imgList->Add(bmp);
1014
1015 // do we need to extend the mapping array?
1016 wxArrayInt& itm = *imgTypeMap;
9a8ffadd 1017 if ( itm.GetCount() < (size_t)type+1)
9e730a78
RD
1018 itm.Add(-1, type - itm.GetCount() + 1);
1019
1020 // Add an item that maps type to the image index
1021 itm[type] = idx;
1022}
1023
1024void ListBoxImpl::ClearRegisteredImages() {
1025 if (imgList) {
1026 delete imgList;
1027 imgList = NULL;
1028 }
1029 if (imgTypeMap) {
1030 delete imgTypeMap;
1031 imgTypeMap = NULL;
1032 }
1033 if (id)
1034 GETLB(id)->SetImageList(NULL, wxIMAGE_LIST_SMALL);
1035}
1036
1037
1038void ListBoxImpl::SetDoubleClickAction(CallBackAction action, void *data) {
1039 GETLBW(id)->SetDoubleClickAction(action, data);
1040}
1041
1042
1043
1044ListBox::ListBox() {
1045}
1046
1047ListBox::~ListBox() {
1048}
1049
1050ListBox *ListBox::Allocate() {
1051 return new ListBoxImpl();
9ce192d4
RD
1052}
1053
1a2fb4cd 1054//----------------------------------------------------------------------
9ce192d4
RD
1055
1056Menu::Menu() : id(0) {
1057}
1058
1059void Menu::CreatePopUp() {
1060 Destroy();
1061 id = new wxMenu();
1062}
1063
1064void Menu::Destroy() {
1065 if (id)
1a2fb4cd 1066 delete (wxMenu*)id;
9ce192d4
RD
1067 id = 0;
1068}
1069
1070void Menu::Show(Point pt, Window &w) {
1a2fb4cd 1071 GETWIN(w.GetID())->PopupMenu((wxMenu*)id, pt.x - 4, pt.y);
9ce192d4
RD
1072 Destroy();
1073}
1074
1a2fb4cd 1075//----------------------------------------------------------------------
9ce192d4 1076
88a8b04e 1077DynamicLibrary *DynamicLibrary::Load(const char *WXUNUSED(modulePath)) {
e14d10b0
RD
1078 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1079 return NULL;
1080}
1081
1082//----------------------------------------------------------------------
1083
1a2fb4cd 1084ColourDesired Platform::Chrome() {
9ce192d4 1085 wxColour c;
e1c6c6ae 1086 c = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
1a2fb4cd 1087 return ColourDesired(c.Red(), c.Green(), c.Blue());
9ce192d4
RD
1088}
1089
1a2fb4cd 1090ColourDesired Platform::ChromeHighlight() {
9ce192d4 1091 wxColour c;
e1c6c6ae 1092 c = wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT);
1a2fb4cd 1093 return ColourDesired(c.Red(), c.Green(), c.Blue());
9ce192d4
RD
1094}
1095
1096const char *Platform::DefaultFont() {
10ef30eb
RD
1097 static char buf[128];
1098 strcpy(buf, wxNORMAL_FONT->GetFaceName().mbc_str());
1099 return buf;
9ce192d4
RD
1100}
1101
1102int Platform::DefaultFontSize() {
9e730a78 1103 return wxNORMAL_FONT->GetPointSize();
9ce192d4
RD
1104}
1105
1106unsigned int Platform::DoubleClickTime() {
1107 return 500; // **** ::GetDoubleClickTime();
1108}
1109
9e730a78
RD
1110bool Platform::MouseButtonBounce() {
1111 return FALSE;
1112}
9ce192d4 1113void Platform::DebugDisplay(const char *s) {
0c5b83b0 1114 wxLogDebug(stc2wx(s));
9ce192d4
RD
1115}
1116
88a8b04e 1117bool Platform::IsKeyDown(int WXUNUSED(key)) {
9ce192d4
RD
1118 return false; // I don't think we'll need this.
1119}
1120
1121long Platform::SendScintilla(WindowID w,
1122 unsigned int msg,
1123 unsigned long wParam,
1124 long lParam) {
1125
1126 wxStyledTextCtrl* stc = (wxStyledTextCtrl*)w;
1127 return stc->SendMsg(msg, wParam, lParam);
1128}
1129
a834585d
RD
1130long Platform::SendScintillaPointer(WindowID w,
1131 unsigned int msg,
1132 unsigned long wParam,
1133 void *lParam) {
1134
1135 wxStyledTextCtrl* stc = (wxStyledTextCtrl*)w;
1136 return stc->SendMsg(msg, wParam, (long)lParam);
1137}
1138
9ce192d4
RD
1139
1140// These are utility functions not really tied to a platform
1141
1142int Platform::Minimum(int a, int b) {
1143 if (a < b)
1144 return a;
1145 else
1146 return b;
1147}
1148
1149int Platform::Maximum(int a, int b) {
1150 if (a > b)
1151 return a;
1152 else
1153 return b;
1154}
1155
1156#define TRACE
1157
1158void Platform::DebugPrintf(const char *format, ...) {
1159#ifdef TRACE
1160 char buffer[2000];
1161 va_list pArguments;
1162 va_start(pArguments, format);
1163 vsprintf(buffer,format,pArguments);
1164 va_end(pArguments);
1165 Platform::DebugDisplay(buffer);
1166#endif
1167}
1168
65ec6247
RD
1169
1170static bool assertionPopUps = true;
1171
1172bool Platform::ShowAssertionPopUps(bool assertionPopUps_) {
1173 bool ret = assertionPopUps;
1174 assertionPopUps = assertionPopUps_;
1175 return ret;
1176}
1177
1178void Platform::Assert(const char *c, const char *file, int line) {
1179 char buffer[2000];
1180 sprintf(buffer, "Assertion [%s] failed at %s %d", c, file, line);
1181 if (assertionPopUps) {
0c5b83b0
RD
1182 /*int idButton = */
1183 wxMessageBox(stc2wx(buffer),
1184 wxT("Assertion failure"),
1185 wxICON_HAND | wxOK);
65ec6247
RD
1186// if (idButton == IDRETRY) {
1187// ::DebugBreak();
1188// } else if (idButton == IDIGNORE) {
1189// // all OK
1190// } else {
1191// abort();
1192// }
1193 } else {
1194 strcat(buffer, "\r\n");
1195 Platform::DebugDisplay(buffer);
1196 abort();
1197 }
1198}
1199
1200
9ce192d4
RD
1201int Platform::Clamp(int val, int minVal, int maxVal) {
1202 if (val > maxVal)
1203 val = maxVal;
1204 if (val < minVal)
1205 val = minVal;
1206 return val;
1207}
1208
1209
88a8b04e 1210bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage), char WXUNUSED(ch)) {
1a2fb4cd
RD
1211 return false;
1212}
1213
88a8b04e 1214int Platform::DBCSCharLength(int WXUNUSED(codePage), const char *WXUNUSED(s)) {
215ef7ae 1215 return 1;
9e730a78
RD
1216}
1217
1218int Platform::DBCSCharMaxLength() {
215ef7ae 1219 return 1;
9e730a78 1220}
1a2fb4cd
RD
1221
1222
1223//----------------------------------------------------------------------
1224
1225ElapsedTime::ElapsedTime() {
1226 wxStartTimer();
1227}
1228
1229double ElapsedTime::Duration(bool reset) {
1230 double result = wxGetElapsedTime(reset);
1231 result /= 1000.0;
1232 return result;
1233}
1234
1235
1236//----------------------------------------------------------------------
9ce192d4 1237
d99859e4
RD
1238#if wxUSE_UNICODE
1239wxString stc2wx(const char* str, size_t len)
1240{
1241 char *buffer=new char[len+1];
1242 strncpy(buffer, str, len);
1243 buffer[len]=0;
1244
1245 wxString cstr(buffer, wxConvUTF8);
1246
1247 delete[] buffer;
1248 return cstr;
1249}
1250#endif
1251
1252
1253
9ce192d4
RD
1254
1255
1256