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