]>
git.saurik.com Git - wxWidgets.git/blob - 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.
10 #include "wx/stc/stc.h"
17 Point
Point::FromLong(long lpoint
) {
18 return Point(lpoint
& 0xFFFF, lpoint
>> 16);
21 wxRect
wxRectFromPRectangle(PRectangle prc
) {
22 wxRect
rc(prc
.left
, prc
.top
,
23 prc
.right
-prc
.left
, prc
.bottom
-prc
.top
);
27 PRectangle
PRectangleFromwxRect(wxRect rc
) {
28 return PRectangle(rc
.GetLeft(), rc
.GetTop(), rc
.GetRight(), rc
.GetBottom());
31 Colour::Colour(long lcol
) {
32 co
.Set(lcol
& 0xff, (lcol
>> 8) & 0xff, (lcol
>> 16) & 0xff);
35 Colour::Colour(unsigned int red
, unsigned int green
, unsigned int blue
) {
36 co
.Set(red
, green
, blue
);
39 bool Colour::operator==(const Colour
&other
) const {
40 return co
== other
.co
;
43 long Colour::AsLong() const {
44 return (((long)co
.Blue() << 16) |
45 ((long)co
.Green() << 8) |
49 unsigned int Colour::GetRed() {
53 unsigned int Colour::GetGreen() {
57 unsigned int Colour::GetBlue() {
63 allowRealization
= false;
70 void Palette::Release() {
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.
77 void Palette::WantFind(ColourPair
&cp
, bool want
) {
79 for (int i
=0; i
< used
; i
++) {
80 if (entries
[i
].desired
== cp
.desired
)
84 if (used
< numEntries
) {
85 entries
[used
].desired
= cp
.desired
;
86 entries
[used
].allocated
= cp
.desired
;
90 for (int i
=0; i
< used
; i
++) {
91 if (entries
[i
].desired
== cp
.desired
) {
92 cp
.allocated
= entries
[i
].allocated
;
96 cp
.allocated
= cp
.desired
;
100 void Palette::Allocate(Window
&) {
101 if (allowRealization
) {
114 void Font::Create(const char *faceName
, int characterSet
, int size
, bool bold
, bool italic
) {
115 // TODO: what to do about the characterSet?
118 id
= new wxFont(size
,
120 italic
? wxITALIC
: wxNORMAL
,
121 bold
? wxBOLD
: wxNORMAL
,
124 wxFONTENCODING_DEFAULT
);
128 void Font::Release() {
136 hdc(0), hdcOwned(0), bitmap(0),
140 Surface::~Surface() {
144 void Surface::Release() {
146 ((wxMemoryDC
*)hdc
)->SelectObject(wxNullBitmap
);
158 bool Surface::Initialised() {
162 void Surface::Init() {
164 hdc
= new wxMemoryDC();
168 void Surface::Init(SurfaceID hdc_
) {
173 void Surface::InitPixMap(int width
, int height
, Surface
*surface_
) {
175 hdc
= new wxMemoryDC(surface_
->hdc
);
177 if (width
< 1) width
= 1;
178 if (height
< 1) height
= 1;
179 bitmap
= new wxBitmap(width
, height
);
180 ((wxMemoryDC
*)hdc
)->SelectObject(*bitmap
);
183 void Surface::PenColour(Colour fore
) {
184 hdc
->SetPen(wxPen(fore
.co
, 1, wxSOLID
));
187 void Surface::BrushColor(Colour back
) {
188 hdc
->SetBrush(wxBrush(back
.co
, wxSOLID
));
191 void Surface::SetFont(Font
&font_
) {
193 hdc
->SetFont(*font_
.GetID());
197 int Surface::LogPixelsY() {
198 return hdc
->GetPPI().y
;
202 int Surface::DeviceHeightFont(int points
) {
207 void Surface::MoveTo(int x_
, int y_
) {
212 void Surface::LineTo(int x_
, int y_
) {
213 hdc
->DrawLine(x
,y
, x_
,y_
);
218 void Surface::Polygon(Point
*pts
, int npts
, Colour fore
,
222 hdc
->DrawPolygon(npts
, (wxPoint
*)pts
);
225 void Surface::RectangleDraw(PRectangle rc
, Colour fore
, Colour back
) {
228 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
231 void Surface::FillRectangle(PRectangle rc
, Colour back
) {
233 hdc
->SetPen(*wxTRANSPARENT_PEN
);
234 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
237 void Surface::FillRectangle(PRectangle rc
, Surface
&surfacePattern
) {
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
);
245 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
248 void Surface::RoundedRectangle(PRectangle rc
, Colour fore
, Colour back
) {
251 hdc
->DrawRoundedRectangle(wxRectFromPRectangle(rc
), 4);
254 void Surface::Ellipse(PRectangle rc
, Colour fore
, Colour back
) {
257 hdc
->DrawEllipse(wxRectFromPRectangle(rc
));
260 void Surface::Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
) {
261 wxRect r
= wxRectFromPRectangle(rc
);
262 hdc
->Blit(r
.x
, r
.y
, r
.width
, r
.height
,
263 surfaceSource
.hdc
, from
.x
, from
.y
, wxCOPY
);
266 void Surface::DrawText(PRectangle rc
, Font
&font
, int ybase
,
267 const char *s
, int len
, Colour fore
, Colour back
) {
269 hdc
->SetTextForeground(fore
.co
);
270 hdc
->SetTextBackground(back
.co
);
271 FillRectangle(rc
, back
);
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
);
278 void Surface::DrawTextClipped(PRectangle rc
, Font
&font
, int ybase
, const char *s
, int len
, Colour fore
, Colour back
) {
280 hdc
->SetTextForeground(fore
.co
);
281 hdc
->SetTextBackground(back
.co
);
282 FillRectangle(rc
, back
);
283 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
285 // see comments above
286 hdc
->DrawText(wxString(s
, len
), rc
.left
, ybase
- font
.ascent
);
287 hdc
->DestroyClippingRegion();
290 int Surface::WidthText(Font
&font
, const char *s
, int len
) {
294 hdc
->GetTextExtent(wxString(s
, len
), &w
, &h
);
298 void Surface::MeasureWidths(Font
&font
, const char *s
, int len
, int *positions
) {
301 for (int i
=0; i
<len
; i
++) {
304 hdc
->GetTextExtent(s
[i
], &w
, &h
);
306 positions
[i
] = totalWidth
;
310 int Surface::WidthChar(Font
&font
, char ch
) {
314 hdc
->GetTextExtent(ch
, &w
, &h
);
318 #define EXTENT_TEST " `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
320 int Surface::Ascent(Font
&font
) {
323 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
328 int Surface::Descent(Font
&font
) {
331 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
335 int Surface::InternalLeading(Font
&font
) {
339 int Surface::ExternalLeading(Font
&font
) {
342 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
346 int Surface::Height(Font
&font
) {
348 return hdc
->GetCharHeight();
351 int Surface::AverageCharWidth(Font
&font
) {
353 return hdc
->GetCharWidth();
356 int Surface::SetPalette(Palette
*pal
, bool inBackGround
) {
360 void Surface::SetClip(PRectangle rc
) {
361 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
364 void Surface::FlushCachedState() {
370 void Window::Destroy() {
376 bool Window::HasFocus() {
377 return wxWindow::FindFocus() == id
;
380 PRectangle
Window::GetPosition() {
381 wxRect
rc(id
->GetPosition(), id
->GetSize());
382 return PRectangleFromwxRect(rc
);
385 void Window::SetPosition(PRectangle rc
) {
386 wxRect r
= wxRectFromPRectangle(rc
);
390 void Window::SetPositionRelative(PRectangle rc
, Window
) {
391 SetPosition(rc
); // ????
394 PRectangle
Window::GetClientPosition() {
395 wxSize sz
= id
->GetClientSize();
396 return PRectangle(0, 0, sz
.x
, sz
.y
);
399 void Window::Show(bool show
) {
403 void Window::InvalidateAll() {
407 void Window::InvalidateRectangle(PRectangle rc
) {
408 wxRect r
= wxRectFromPRectangle(rc
);
409 id
->Refresh(false, &r
);
412 void Window::SetFont(Font
&font
) {
413 id
->SetFont(*font
.GetID());
416 void Window::SetCursor(Cursor curs
) {
421 cursorId
= wxCURSOR_IBEAM
;
424 cursorId
= wxCURSOR_ARROW
;
427 cursorId
= wxCURSOR_ARROW
; // ** no up arrow... wxCURSOR_UPARROW;
430 cursorId
= wxCURSOR_WAIT
;
433 cursorId
= wxCURSOR_SIZEWE
;
436 cursorId
= wxCURSOR_SIZENS
;
438 case cursorReverseArrow
:
439 cursorId
= wxCURSOR_POINT_RIGHT
;
442 cursorId
= wxCURSOR_ARROW
;
446 id
->SetCursor(wxCursor(cursorId
));
450 void Window::SetTitle(const char *s
) {
455 class wxSTCListBox
: public wxListBox
{
457 wxSTCListBox(wxWindow
* parent
, wxWindowID id
)
458 : wxListBox(parent
, id
, wxDefaultPosition
, wxDefaultSize
,
459 0, NULL
, wxLB_SINGLE
| wxLB_SORT
| wxSIMPLE_BORDER
)
462 void OnFocus(wxFocusEvent
& event
) {
463 GetParent()->SetFocus();
468 void DoSetFirstItem(int n
);
472 DECLARE_EVENT_TABLE()
475 BEGIN_EVENT_TABLE(wxSTCListBox
, wxListBox
)
476 EVT_SET_FOCUS(wxSTCListBox::OnFocus
)
483 // This can be removed after 2.2.2 I think
484 void wxSTCListBox::DoSetFirstItem( int n
)
486 wxCHECK_RET( m_list
, wxT("invalid listbox") );
488 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list
))
491 // terribly efficient
492 const gchar
*vadjustment_key
= "gtk-vadjustment";
493 guint vadjustment_key_id
= g_quark_from_static_string (vadjustment_key
);
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") );
499 GList
*target
= g_list_nth( m_list
->children
, n
);
500 wxCHECK_RET( target
, wxT("invalid listbox index") );
502 GtkWidget
*item
= GTK_WIDGET(target
->data
);
503 wxCHECK_RET( item
, wxT("invalid listbox code") );
505 // find the last item before this one which is already realized
507 for ( nItemsBefore
= 0; item
&& (item
->allocation
.y
== -1); nItemsBefore
++ )
509 target
= target
->prev
;
512 // nothing we can do if there are no allocated items yet
516 item
= GTK_WIDGET(target
->data
);
519 gtk_adjustment_set_value(adjustment
,
521 nItemsBefore
*item
->allocation
.height
);
529 ListBox::~ListBox() {
532 void ListBox::Create(Window
&parent
, int ctrlID
) {
533 id
= new wxSTCListBox(parent
.id
, ctrlID
);
534 // id = new wxListBox(parent.id, ctrlID, wxDefaultPosition, wxDefaultSize,
535 // 0, NULL, wxLB_SINGLE | wxLB_SORT | wxSIMPLE_BORDER);
538 PRectangle
ListBox::GetDesiredRect() {
539 wxSize sz
= ((wxListBox
*)id
)->GetBestSize();
543 if (sz
.x
> 150) // TODO: A better way to determine these max sizes
553 void ListBox::SetAverageCharWidth(int width
) {
554 aveCharWidth
= width
;
557 void ListBox::SetFont(Font
&font
) {
558 Window::SetFont(font
);
561 void ListBox::Clear() {
562 ((wxListBox
*)id
)->Clear();
565 void ListBox::Append(char *s
) {
566 ((wxListBox
*)id
)->Append(s
);
569 int ListBox::Length() {
570 return ((wxListBox
*)id
)->GetCount();
573 void ListBox::Select(int n
) {
574 ((wxListBox
*)id
)->SetSelection(n
);
580 ((wxListBox
*)id
)->SetFirstItem(n
);
584 int ListBox::GetSelection() {
585 return ((wxListBox
*)id
)->GetSelection();
588 int ListBox::Find(const char *prefix
) {
590 for (int x
=0; x
< ((wxListBox
*)id
)->GetCount(); x
++) {
591 wxString text
= ((wxListBox
*)id
)->GetString(x
);
592 if (text
.StartsWith(prefix
))
599 void ListBox::GetValue(int n
, char *value
, int len
) {
600 wxString text
= ((wxListBox
*)id
)->GetString(n
);
601 strncpy(value
, text
.c_str(), len
);
605 void ListBox::Sort() {
606 // wxWindows keeps sorted so no need to sort
610 Menu::Menu() : id(0) {
613 void Menu::CreatePopUp() {
618 void Menu::Destroy() {
624 void Menu::Show(Point pt
, Window
&w
) {
625 w
.GetID()->PopupMenu(id
, pt
.x
- 4, pt
.y
);
630 Colour
Platform::Chrome() {
632 c
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
633 return Colour(c
.Red(), c
.Green(), c
.Blue());
636 Colour
Platform::ChromeHighlight() {
638 c
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHIGHLIGHT
);
639 return Colour(c
.Red(), c
.Green(), c
.Blue());
642 const char *Platform::DefaultFont() {
643 return wxNORMAL_FONT
->GetFaceName();
646 int Platform::DefaultFontSize() {
650 unsigned int Platform::DoubleClickTime() {
651 return 500; // **** ::GetDoubleClickTime();
654 void Platform::DebugDisplay(const char *s
) {
658 bool Platform::IsKeyDown(int key
) {
659 return false; // I don't think we'll need this.
662 long Platform::SendScintilla(WindowID w
,
664 unsigned long wParam
,
667 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
668 return stc
->SendMsg(msg
, wParam
, lParam
);
672 // These are utility functions not really tied to a platform
674 int Platform::Minimum(int a
, int b
) {
681 int Platform::Maximum(int a
, int b
) {
690 void Platform::DebugPrintf(const char *format
, ...) {
694 va_start(pArguments
, format
);
695 vsprintf(buffer
,format
,pArguments
);
697 Platform::DebugDisplay(buffer
);
702 static bool assertionPopUps
= true;
704 bool Platform::ShowAssertionPopUps(bool assertionPopUps_
) {
705 bool ret
= assertionPopUps
;
706 assertionPopUps
= assertionPopUps_
;
710 void Platform::Assert(const char *c
, const char *file
, int line
) {
712 sprintf(buffer
, "Assertion [%s] failed at %s %d", c
, file
, line
);
713 if (assertionPopUps
) {
714 int idButton
= wxMessageBox(buffer
, "Assertion failure",
716 // if (idButton == IDRETRY) {
718 // } else if (idButton == IDIGNORE) {
724 strcat(buffer
, "\r\n");
725 Platform::DebugDisplay(buffer
);
731 int Platform::Clamp(int val
, int minVal
, int maxVal
) {