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