]> git.saurik.com Git - wxWidgets.git/blob - src/stc/PlatWX.cpp
OS/2 fixes
[wxWidgets.git] / src / stc / PlatWX.cpp
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
7 #include <ctype.h>
8
9 #include "Platform.h"
10 #include "wx/stc/stc.h"
11
12 Point Point::FromLong(long lpoint) {
13 return Point(lpoint & 0xFFFF, lpoint >> 16);
14 }
15
16 wxRect wxRectFromPRectangle(PRectangle prc) {
17 wxRect rc(prc.left, prc.top,
18 prc.right-prc.left, prc.bottom-prc.top);
19 return rc;
20 }
21
22 PRectangle PRectangleFromwxRect(wxRect rc) {
23 return PRectangle(rc.GetLeft(), rc.GetTop(), rc.GetRight(), rc.GetBottom());
24 }
25
26 Colour::Colour(long lcol) {
27 co.Set(lcol & 0xff, (lcol >> 8) & 0xff, (lcol >> 16) & 0xff);
28 }
29
30 Colour::Colour(unsigned int red, unsigned int green, unsigned int blue) {
31 co.Set(red, green, blue);
32 }
33
34 bool Colour::operator==(const Colour &other) const {
35 return co == other.co;
36 }
37
38 long Colour::AsLong() const {
39 return (((long)co.Blue() << 16) |
40 ((long)co.Green() << 8) |
41 ((long)co.Red()));
42 }
43
44 unsigned int Colour::GetRed() {
45 return co.Red();
46 }
47
48 unsigned int Colour::GetGreen() {
49 return co.Green();
50 }
51
52 unsigned int Colour::GetBlue() {
53 return co.Blue();
54 }
55
56 Palette::Palette() {
57 used = 0;
58 allowRealization = false;
59 }
60
61 Palette::~Palette() {
62 Release();
63 }
64
65 void Palette::Release() {
66 used = 0;
67 }
68
69 // This method either adds a colour to the list of wanted colours (want==true)
70 // or retrieves the allocated colour back to the ColourPair.
71 // This is one method to make it easier to keep the code for wanting and retrieving in sync.
72 void Palette::WantFind(ColourPair &cp, bool want) {
73 if (want) {
74 for (int i=0; i < used; i++) {
75 if (entries[i].desired == cp.desired)
76 return;
77 }
78
79 if (used < numEntries) {
80 entries[used].desired = cp.desired;
81 entries[used].allocated = cp.desired;
82 used++;
83 }
84 } else {
85 for (int i=0; i < used; i++) {
86 if (entries[i].desired == cp.desired) {
87 cp.allocated = entries[i].allocated;
88 return;
89 }
90 }
91 cp.allocated = cp.desired;
92 }
93 }
94
95 void Palette::Allocate(Window &) {
96 if (allowRealization) {
97 }
98 }
99
100
101 Font::Font() {
102 id = 0;
103 ascent = 0;
104 }
105
106 Font::~Font() {
107 }
108
109 void Font::Create(const char *faceName, int characterSet, int size, bool bold, bool italic) {
110 Release();
111 id = new wxFont(size,
112 wxDEFAULT,
113 italic ? wxITALIC : wxNORMAL,
114 bold ? wxBOLD : wxNORMAL,
115 false,
116 faceName,
117 wxFONTENCODING_DEFAULT);
118 }
119
120
121 void Font::Release() {
122 if (id)
123 delete id;
124 id = 0;
125 }
126
127
128 Surface::Surface() :
129 hdc(0), hdcOwned(0), bitmap(0),
130 x(0), y(0) {
131 }
132
133 Surface::~Surface() {
134 Release();
135 }
136
137 void Surface::Release() {
138 if (bitmap) {
139 ((wxMemoryDC*)hdc)->SelectObject(wxNullBitmap);
140 delete bitmap;
141 bitmap = 0;
142 }
143 if (hdcOwned) {
144 delete hdc;
145 hdc = 0;
146 hdcOwned = false;
147 }
148 }
149
150
151 bool Surface::Initialised() {
152 return hdc != 0;
153 }
154
155 void Surface::Init() {
156 Release();
157 hdc = new wxMemoryDC();
158 hdcOwned = true;
159 // **** ::SetTextAlign(hdc, TA_BASELINE);
160 }
161
162 void Surface::Init(SurfaceID hdc_) {
163 Release();
164 hdc = hdc_;
165 // **** ::SetTextAlign(hdc, TA_BASELINE);
166 }
167
168 void Surface::InitPixMap(int width, int height, Surface *surface_) {
169 Release();
170 hdc = new wxMemoryDC(surface_->hdc);
171 hdcOwned = true;
172 bitmap = new wxBitmap(width, height);
173 ((wxMemoryDC*)hdc)->SelectObject(*bitmap);
174 // **** ::SetTextAlign(hdc, TA_BASELINE);
175 }
176
177 void Surface::PenColour(Colour fore) {
178 hdc->SetPen(wxPen(fore.co, 1, wxSOLID));
179 }
180
181 void Surface::BrushColor(Colour back) {
182 hdc->SetBrush(wxBrush(back.co, wxSOLID));
183 }
184
185 void Surface::SetFont(Font &font_) {
186 if (font_.GetID()) {
187 hdc->SetFont(*font_.GetID());
188 }
189 }
190
191 int Surface::LogPixelsY() {
192 return hdc->GetPPI().y;
193 }
194
195
196 int Surface::DeviceHeightFont(int points) {
197 return points * LogPixelsY() / 72;
198 }
199
200
201 void Surface::MoveTo(int x_, int y_) {
202 x = x_;
203 y = y_;
204 }
205
206 void Surface::LineTo(int x_, int y_) {
207 hdc->DrawLine(x,y, x_,y_);
208 x = x_;
209 y = y_;
210 }
211
212 void Surface::Polygon(Point *pts, int npts, Colour fore,
213 Colour back) {
214 PenColour(fore);
215 BrushColor(back);
216 hdc->DrawPolygon(npts, (wxPoint*)pts);
217 }
218
219 void Surface::RectangleDraw(PRectangle rc, Colour fore, Colour back) {
220 PenColour(fore);
221 BrushColor(back);
222 hdc->DrawRectangle(wxRectFromPRectangle(rc));
223 }
224
225 void Surface::FillRectangle(PRectangle rc, Colour back) {
226 BrushColor(back);
227 hdc->SetPen(*wxTRANSPARENT_PEN);
228 hdc->DrawRectangle(wxRectFromPRectangle(rc));
229 }
230
231 void Surface::FillRectangle(PRectangle rc, Surface &surfacePattern) {
232 wxBrush br;
233 if (surfacePattern.bitmap)
234 br = wxBrush(*surfacePattern.bitmap);
235 else // Something is wrong so display in red
236 br = wxBrush(*wxRED, wxSOLID);
237 hdc->SetPen(*wxTRANSPARENT_PEN);
238 hdc->SetBrush(br);
239 hdc->DrawRectangle(wxRectFromPRectangle(rc));
240 }
241
242 void Surface::RoundedRectangle(PRectangle rc, Colour fore, Colour back) {
243 PenColour(fore);
244 BrushColor(back);
245 hdc->DrawRoundedRectangle(wxRectFromPRectangle(rc), 4);
246 }
247
248 void Surface::Ellipse(PRectangle rc, Colour fore, Colour back) {
249 PenColour(fore);
250 BrushColor(back);
251 hdc->DrawEllipse(wxRectFromPRectangle(rc));
252 }
253
254 void Surface::Copy(PRectangle rc, Point from, Surface &surfaceSource) {
255 wxRect r = wxRectFromPRectangle(rc);
256 hdc->Blit(r.x, r.y, r.width, r.height,
257 surfaceSource.hdc, from.x, from.y, wxCOPY);
258 }
259
260 void Surface::DrawText(PRectangle rc, Font &font, int ybase,
261 const char *s, int len, Colour fore, Colour back) {
262 SetFont(font);
263 hdc->SetTextForeground(fore.co);
264 hdc->SetTextBackground(back.co);
265 FillRectangle(rc, back);
266
267 // ybase is where the baseline should be, but wxWin uses the upper left
268 // corner, so I need to calculate the real position for the text...
269 hdc->DrawText(wxString(s, len), rc.left, ybase - font.ascent);
270 }
271
272 void Surface::DrawTextClipped(PRectangle rc, Font &font, int ybase, const char *s, int len, Colour fore, Colour back) {
273 SetFont(font);
274 hdc->SetTextForeground(fore.co);
275 hdc->SetTextBackground(back.co);
276 FillRectangle(rc, back);
277 hdc->SetClippingRegion(wxRectFromPRectangle(rc));
278
279 // see comments above
280 hdc->DrawText(wxString(s, len), rc.left, ybase - font.ascent);
281 hdc->DestroyClippingRegion();
282 }
283
284 int Surface::WidthText(Font &font, const char *s, int len) {
285 SetFont(font);
286 int w;
287 int h;
288 hdc->GetTextExtent(wxString(s, len), &w, &h);
289 return w;
290 }
291
292 void Surface::MeasureWidths(Font &font, const char *s, int len, int *positions) {
293 SetFont(font);
294 int totalWidth = 0;
295 for (int i=0; i<len; i++) {
296 int w;
297 int h;
298 hdc->GetTextExtent(s[i], &w, &h);
299 totalWidth += w;
300 positions[i] = totalWidth;
301 }
302 }
303
304 int Surface::WidthChar(Font &font, char ch) {
305 SetFont(font);
306 int w;
307 int h;
308 hdc->GetTextExtent(ch, &w, &h);
309 return w;
310 }
311
312 #define EXTENT_TEST " `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
313
314 int Surface::Ascent(Font &font) {
315 SetFont(font);
316 int w, h, d, e;
317 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
318 font.ascent = h - d;
319 return font.ascent;
320 }
321
322 int Surface::Descent(Font &font) {
323 SetFont(font);
324 int w, h, d, e;
325 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
326 return d;
327 }
328
329 int Surface::InternalLeading(Font &font) {
330 return 0;
331 }
332
333 int Surface::ExternalLeading(Font &font) {
334 SetFont(font);
335 int w, h, d, e;
336 hdc->GetTextExtent(EXTENT_TEST, &w, &h, &d, &e);
337 return e;
338 }
339
340 int Surface::Height(Font &font) {
341 SetFont(font);
342 return hdc->GetCharHeight();
343 }
344
345 int Surface::AverageCharWidth(Font &font) {
346 SetFont(font);
347 return hdc->GetCharWidth();
348 }
349
350 int Surface::SetPalette(Palette *pal, bool inBackGround) {
351 return 0; // **** figure out what to do with palettes...
352 }
353
354 void Surface::SetClip(PRectangle rc) {
355 hdc->SetClippingRegion(wxRectFromPRectangle(rc));
356 }
357
358 void Surface::FlushCachedState() {
359 }
360
361 Window::~Window() {
362 }
363
364 void Window::Destroy() {
365 if (id)
366 id->Destroy();
367 id = 0;
368 }
369
370 bool Window::HasFocus() {
371 return wxWindow::FindFocus() == id;
372 }
373
374 PRectangle Window::GetPosition() {
375 wxRect rc(id->GetPosition(), id->GetSize());
376 return PRectangleFromwxRect(rc);
377 }
378
379 void Window::SetPosition(PRectangle rc) {
380 wxRect r = wxRectFromPRectangle(rc);
381 id->SetSize(r);
382 }
383
384 void Window::SetPositionRelative(PRectangle rc, Window) {
385 SetPosition(rc); // ????
386 }
387
388 PRectangle Window::GetClientPosition() {
389 wxSize sz = id->GetClientSize();
390 return PRectangle(0, 0, sz.x, sz.y);
391 }
392
393 void Window::Show(bool show) {
394 id->Show(show);
395 }
396
397 void Window::InvalidateAll() {
398 id->Refresh(false);
399 }
400
401 void Window::InvalidateRectangle(PRectangle rc) {
402 wxRect r = wxRectFromPRectangle(rc);
403 id->Refresh(false, &r);
404 }
405
406 void Window::SetFont(Font &font) {
407 id->SetFont(*font.GetID());
408 }
409
410 void Window::SetCursor(Cursor curs) {
411 int cursorId;
412
413 switch (curs) {
414 case cursorText:
415 cursorId = wxCURSOR_IBEAM;
416 break;
417 case cursorArrow:
418 cursorId = wxCURSOR_ARROW;
419 break;
420 case cursorUp:
421 cursorId = wxCURSOR_ARROW; // ** no up arrow... wxCURSOR_UPARROW;
422 break;
423 case cursorWait:
424 cursorId = wxCURSOR_WAIT;
425 break;
426 case cursorHoriz:
427 cursorId = wxCURSOR_SIZEWE;
428 break;
429 case cursorVert:
430 cursorId = wxCURSOR_SIZENS;
431 break;
432 case cursorReverseArrow:
433 cursorId = wxCURSOR_POINT_RIGHT;
434 break;
435 default:
436 cursorId = wxCURSOR_ARROW;
437 break;
438 }
439
440 id->SetCursor(wxCursor(cursorId));
441 }
442
443
444 void Window::SetTitle(const char *s) {
445 id->SetTitle(s);
446 }
447
448
449
450 ListBox::ListBox() {
451 }
452
453 ListBox::~ListBox() {
454 }
455
456 void ListBox::Create(Window &parent, int ctrlID) {
457 id = new wxListBox(parent.id, ctrlID, wxDefaultPosition, wxDefaultSize,
458 0, NULL, wxLB_SINGLE | wxLB_SORT);
459 }
460
461 PRectangle ListBox::GetDesiredRect() {
462 wxSize sz = ((wxListBox*)id)->GetBestSize();
463 PRectangle rc;
464 rc.top = 0;
465 rc.left = 0;
466 rc.right = sz.x;
467 rc.bottom = sz.y;
468
469 return rc;
470 }
471
472 void ListBox::SetAverageCharWidth(int width) {
473 aveCharWidth = width;
474 }
475
476 void ListBox::SetFont(Font &font) {
477 Window::SetFont(font);
478 }
479
480 void ListBox::Clear() {
481 ((wxListBox*)id)->Clear();
482 }
483
484 void ListBox::Append(char *s) {
485 ((wxListBox*)id)->Append(s);
486 }
487
488 int ListBox::Length() {
489 return ((wxListBox*)id)->Number();
490 }
491
492 void ListBox::Select(int n) {
493 ((wxListBox*)id)->SetSelection(n);
494 }
495
496 int ListBox::GetSelection() {
497 return ((wxListBox*)id)->GetSelection();
498 }
499
500 int ListBox::Find(const char *prefix) {
501 if (prefix) {
502 for (int x=0; x < ((wxListBox*)id)->Number(); x++) {
503 wxString text = ((wxListBox*)id)->GetString(x);
504 if (text.StartsWith(prefix))
505 return x;
506 }
507 }
508 return -1;
509 }
510
511 void ListBox::GetValue(int n, char *value, int len) {
512 wxString text = ((wxListBox*)id)->GetString(n);
513 strncpy(value, text.c_str(), len);
514 value[len-1] = '\0';
515 }
516
517 void ListBox::Sort() {
518 // wxWindows keeps sorted so no need to sort
519 }
520
521
522 Menu::Menu() : id(0) {
523 }
524
525 void Menu::CreatePopUp() {
526 Destroy();
527 id = new wxMenu();
528 }
529
530 void Menu::Destroy() {
531 if (id)
532 delete id;
533 id = 0;
534 }
535
536 void Menu::Show(Point pt, Window &w) {
537 w.GetID()->PopupMenu(id, pt.x - 4, pt.y);
538 Destroy();
539 }
540
541
542 Colour Platform::Chrome() {
543 wxColour c;
544 c = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
545 return Colour(c.Red(), c.Green(), c.Blue());
546 }
547
548 Colour Platform::ChromeHighlight() {
549 wxColour c;
550 c = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHIGHLIGHT);
551 return Colour(c.Red(), c.Green(), c.Blue());
552 }
553
554 const char *Platform::DefaultFont() {
555 return wxNORMAL_FONT->GetFaceName();
556 }
557
558 int Platform::DefaultFontSize() {
559 return 8;
560 }
561
562 unsigned int Platform::DoubleClickTime() {
563 return 500; // **** ::GetDoubleClickTime();
564 }
565
566 void Platform::DebugDisplay(const char *s) {
567 wxLogDebug(s);
568 }
569
570 bool Platform::IsKeyDown(int key) {
571 return false; // I don't think we'll need this.
572 }
573
574 long Platform::SendScintilla(WindowID w,
575 unsigned int msg,
576 unsigned long wParam,
577 long lParam) {
578
579 wxStyledTextCtrl* stc = (wxStyledTextCtrl*)w;
580 return stc->SendMsg(msg, wParam, lParam);
581 }
582
583
584 // These are utility functions not really tied to a platform
585
586 int Platform::Minimum(int a, int b) {
587 if (a < b)
588 return a;
589 else
590 return b;
591 }
592
593 int Platform::Maximum(int a, int b) {
594 if (a > b)
595 return a;
596 else
597 return b;
598 }
599
600 #define TRACE
601
602 void Platform::DebugPrintf(const char *format, ...) {
603 #ifdef TRACE
604 char buffer[2000];
605 va_list pArguments;
606 va_start(pArguments, format);
607 vsprintf(buffer,format,pArguments);
608 va_end(pArguments);
609 Platform::DebugDisplay(buffer);
610 #endif
611 }
612
613 int Platform::Clamp(int val, int minVal, int maxVal) {
614 if (val > maxVal)
615 val = maxVal;
616 if (val < minVal)
617 val = minVal;
618 return val;
619 }
620
621
622
623
624
625