]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/stc/PlatWX.cpp
OS/2 fixes
[wxWidgets.git] / src / stc / PlatWX.cpp
... / ...
CommitLineData
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
12Point Point::FromLong(long lpoint) {
13 return Point(lpoint & 0xFFFF, lpoint >> 16);
14}
15
16wxRect wxRectFromPRectangle(PRectangle prc) {
17 wxRect rc(prc.left, prc.top,
18 prc.right-prc.left, prc.bottom-prc.top);
19 return rc;
20}
21
22PRectangle PRectangleFromwxRect(wxRect rc) {
23 return PRectangle(rc.GetLeft(), rc.GetTop(), rc.GetRight(), rc.GetBottom());
24}
25
26Colour::Colour(long lcol) {
27 co.Set(lcol & 0xff, (lcol >> 8) & 0xff, (lcol >> 16) & 0xff);
28}
29
30Colour::Colour(unsigned int red, unsigned int green, unsigned int blue) {
31 co.Set(red, green, blue);
32}
33
34bool Colour::operator==(const Colour &other) const {
35 return co == other.co;
36}
37
38long Colour::AsLong() const {
39 return (((long)co.Blue() << 16) |
40 ((long)co.Green() << 8) |
41 ((long)co.Red()));
42}
43
44unsigned int Colour::GetRed() {
45 return co.Red();
46}
47
48unsigned int Colour::GetGreen() {
49 return co.Green();
50}
51
52unsigned int Colour::GetBlue() {
53 return co.Blue();
54}
55
56Palette::Palette() {
57 used = 0;
58 allowRealization = false;
59}
60
61Palette::~Palette() {
62 Release();
63}
64
65void 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.
72void 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
95void Palette::Allocate(Window &) {
96 if (allowRealization) {
97 }
98}
99
100
101Font::Font() {
102 id = 0;
103 ascent = 0;
104}
105
106Font::~Font() {
107}
108
109void 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
121void Font::Release() {
122 if (id)
123 delete id;
124 id = 0;
125}
126
127
128Surface::Surface() :
129 hdc(0), hdcOwned(0), bitmap(0),
130 x(0), y(0) {
131}
132
133Surface::~Surface() {
134 Release();
135}
136
137void 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
151bool Surface::Initialised() {
152 return hdc != 0;
153}
154
155void Surface::Init() {
156 Release();
157 hdc = new wxMemoryDC();
158 hdcOwned = true;
159 // **** ::SetTextAlign(hdc, TA_BASELINE);
160}
161
162void Surface::Init(SurfaceID hdc_) {
163 Release();
164 hdc = hdc_;
165 // **** ::SetTextAlign(hdc, TA_BASELINE);
166}
167
168void 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
177void Surface::PenColour(Colour fore) {
178 hdc->SetPen(wxPen(fore.co, 1, wxSOLID));
179}
180
181void Surface::BrushColor(Colour back) {
182 hdc->SetBrush(wxBrush(back.co, wxSOLID));
183}
184
185void Surface::SetFont(Font &font_) {
186 if (font_.GetID()) {
187 hdc->SetFont(*font_.GetID());
188 }
189}
190
191int Surface::LogPixelsY() {
192 return hdc->GetPPI().y;
193}
194
195
196int Surface::DeviceHeightFont(int points) {
197 return points * LogPixelsY() / 72;
198}
199
200
201void Surface::MoveTo(int x_, int y_) {
202 x = x_;
203 y = y_;
204}
205
206void Surface::LineTo(int x_, int y_) {
207 hdc->DrawLine(x,y, x_,y_);
208 x = x_;
209 y = y_;
210}
211
212void 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
219void Surface::RectangleDraw(PRectangle rc, Colour fore, Colour back) {
220 PenColour(fore);
221 BrushColor(back);
222 hdc->DrawRectangle(wxRectFromPRectangle(rc));
223}
224
225void Surface::FillRectangle(PRectangle rc, Colour back) {
226 BrushColor(back);
227 hdc->SetPen(*wxTRANSPARENT_PEN);
228 hdc->DrawRectangle(wxRectFromPRectangle(rc));
229}
230
231void 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
242void Surface::RoundedRectangle(PRectangle rc, Colour fore, Colour back) {
243 PenColour(fore);
244 BrushColor(back);
245 hdc->DrawRoundedRectangle(wxRectFromPRectangle(rc), 4);
246}
247
248void Surface::Ellipse(PRectangle rc, Colour fore, Colour back) {
249 PenColour(fore);
250 BrushColor(back);
251 hdc->DrawEllipse(wxRectFromPRectangle(rc));
252}
253
254void 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
260void 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
272void 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
284int 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
292void 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
304int 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
314int 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
322int 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
329int Surface::InternalLeading(Font &font) {
330 return 0;
331}
332
333int 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
340int Surface::Height(Font &font) {
341 SetFont(font);
342 return hdc->GetCharHeight();
343}
344
345int Surface::AverageCharWidth(Font &font) {
346 SetFont(font);
347 return hdc->GetCharWidth();
348}
349
350int Surface::SetPalette(Palette *pal, bool inBackGround) {
351 return 0; // **** figure out what to do with palettes...
352}
353
354void Surface::SetClip(PRectangle rc) {
355 hdc->SetClippingRegion(wxRectFromPRectangle(rc));
356}
357
358void Surface::FlushCachedState() {
359}
360
361Window::~Window() {
362}
363
364void Window::Destroy() {
365 if (id)
366 id->Destroy();
367 id = 0;
368}
369
370bool Window::HasFocus() {
371 return wxWindow::FindFocus() == id;
372}
373
374PRectangle Window::GetPosition() {
375 wxRect rc(id->GetPosition(), id->GetSize());
376 return PRectangleFromwxRect(rc);
377}
378
379void Window::SetPosition(PRectangle rc) {
380 wxRect r = wxRectFromPRectangle(rc);
381 id->SetSize(r);
382}
383
384void Window::SetPositionRelative(PRectangle rc, Window) {
385 SetPosition(rc); // ????
386}
387
388PRectangle Window::GetClientPosition() {
389 wxSize sz = id->GetClientSize();
390 return PRectangle(0, 0, sz.x, sz.y);
391}
392
393void Window::Show(bool show) {
394 id->Show(show);
395}
396
397void Window::InvalidateAll() {
398 id->Refresh(false);
399}
400
401void Window::InvalidateRectangle(PRectangle rc) {
402 wxRect r = wxRectFromPRectangle(rc);
403 id->Refresh(false, &r);
404}
405
406void Window::SetFont(Font &font) {
407 id->SetFont(*font.GetID());
408}
409
410void 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
444void Window::SetTitle(const char *s) {
445 id->SetTitle(s);
446}
447
448
449
450ListBox::ListBox() {
451}
452
453ListBox::~ListBox() {
454}
455
456void ListBox::Create(Window &parent, int ctrlID) {
457 id = new wxListBox(parent.id, ctrlID, wxDefaultPosition, wxDefaultSize,
458 0, NULL, wxLB_SINGLE | wxLB_SORT);
459}
460
461PRectangle 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
472void ListBox::SetAverageCharWidth(int width) {
473 aveCharWidth = width;
474}
475
476void ListBox::SetFont(Font &font) {
477 Window::SetFont(font);
478}
479
480void ListBox::Clear() {
481 ((wxListBox*)id)->Clear();
482}
483
484void ListBox::Append(char *s) {
485 ((wxListBox*)id)->Append(s);
486}
487
488int ListBox::Length() {
489 return ((wxListBox*)id)->Number();
490}
491
492void ListBox::Select(int n) {
493 ((wxListBox*)id)->SetSelection(n);
494}
495
496int ListBox::GetSelection() {
497 return ((wxListBox*)id)->GetSelection();
498}
499
500int 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
511void 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
517void ListBox::Sort() {
518 // wxWindows keeps sorted so no need to sort
519}
520
521
522Menu::Menu() : id(0) {
523}
524
525void Menu::CreatePopUp() {
526 Destroy();
527 id = new wxMenu();
528}
529
530void Menu::Destroy() {
531 if (id)
532 delete id;
533 id = 0;
534}
535
536void Menu::Show(Point pt, Window &w) {
537 w.GetID()->PopupMenu(id, pt.x - 4, pt.y);
538 Destroy();
539}
540
541
542Colour Platform::Chrome() {
543 wxColour c;
544 c = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
545 return Colour(c.Red(), c.Green(), c.Blue());
546}
547
548Colour Platform::ChromeHighlight() {
549 wxColour c;
550 c = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHIGHLIGHT);
551 return Colour(c.Red(), c.Green(), c.Blue());
552}
553
554const char *Platform::DefaultFont() {
555 return wxNORMAL_FONT->GetFaceName();
556}
557
558int Platform::DefaultFontSize() {
559 return 8;
560}
561
562unsigned int Platform::DoubleClickTime() {
563 return 500; // **** ::GetDoubleClickTime();
564}
565
566void Platform::DebugDisplay(const char *s) {
567 wxLogDebug(s);
568}
569
570bool Platform::IsKeyDown(int key) {
571 return false; // I don't think we'll need this.
572}
573
574long 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
586int Platform::Minimum(int a, int b) {
587 if (a < b)
588 return a;
589 else
590 return b;
591}
592
593int Platform::Maximum(int a, int b) {
594 if (a > b)
595 return a;
596 else
597 return b;
598}
599
600#define TRACE
601
602void 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
613int 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