]>
git.saurik.com Git - wxWidgets.git/blob - contrib/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
) {
116 id
= new wxFont(size
,
118 italic
? wxITALIC
: wxNORMAL
,
119 bold
? wxBOLD
: wxNORMAL
,
122 wxFONTENCODING_DEFAULT
);
126 void Font::Release() {
134 hdc(0), hdcOwned(0), bitmap(0),
138 Surface::~Surface() {
142 void Surface::Release() {
144 ((wxMemoryDC
*)hdc
)->SelectObject(wxNullBitmap
);
156 bool Surface::Initialised() {
160 void Surface::Init() {
162 hdc
= new wxMemoryDC();
164 // **** ::SetTextAlign(hdc, TA_BASELINE);
167 void Surface::Init(SurfaceID hdc_
) {
170 // **** ::SetTextAlign(hdc, TA_BASELINE);
173 void Surface::InitPixMap(int width
, int height
, Surface
*surface_
) {
175 hdc
= new wxMemoryDC(surface_
->hdc
);
177 bitmap
= new wxBitmap(width
, height
);
178 ((wxMemoryDC
*)hdc
)->SelectObject(*bitmap
);
179 // **** ::SetTextAlign(hdc, TA_BASELINE);
182 void Surface::PenColour(Colour fore
) {
183 hdc
->SetPen(wxPen(fore
.co
, 1, wxSOLID
));
186 void Surface::BrushColor(Colour back
) {
187 hdc
->SetBrush(wxBrush(back
.co
, wxSOLID
));
190 void Surface::SetFont(Font
&font_
) {
192 hdc
->SetFont(*font_
.GetID());
196 int Surface::LogPixelsY() {
197 return hdc
->GetPPI().y
;
201 int Surface::DeviceHeightFont(int points
) {
202 return points
* LogPixelsY() / 72;
206 void Surface::MoveTo(int x_
, int y_
) {
211 void Surface::LineTo(int x_
, int y_
) {
212 hdc
->DrawLine(x
,y
, x_
,y_
);
217 void Surface::Polygon(Point
*pts
, int npts
, Colour fore
,
221 hdc
->DrawPolygon(npts
, (wxPoint
*)pts
);
224 void Surface::RectangleDraw(PRectangle rc
, Colour fore
, Colour back
) {
227 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
230 void Surface::FillRectangle(PRectangle rc
, Colour back
) {
232 hdc
->SetPen(*wxTRANSPARENT_PEN
);
233 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
236 void Surface::FillRectangle(PRectangle rc
, Surface
&surfacePattern
) {
238 if (surfacePattern
.bitmap
)
239 br
= wxBrush(*surfacePattern
.bitmap
);
240 else // Something is wrong so display in red
241 br
= wxBrush(*wxRED
, wxSOLID
);
242 hdc
->SetPen(*wxTRANSPARENT_PEN
);
244 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
247 void Surface::RoundedRectangle(PRectangle rc
, Colour fore
, Colour back
) {
250 hdc
->DrawRoundedRectangle(wxRectFromPRectangle(rc
), 4);
253 void Surface::Ellipse(PRectangle rc
, Colour fore
, Colour back
) {
256 hdc
->DrawEllipse(wxRectFromPRectangle(rc
));
259 void Surface::Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
) {
260 wxRect r
= wxRectFromPRectangle(rc
);
261 hdc
->Blit(r
.x
, r
.y
, r
.width
, r
.height
,
262 surfaceSource
.hdc
, from
.x
, from
.y
, wxCOPY
);
265 void Surface::DrawText(PRectangle rc
, Font
&font
, int ybase
,
266 const char *s
, int len
, Colour fore
, Colour back
) {
268 hdc
->SetTextForeground(fore
.co
);
269 hdc
->SetTextBackground(back
.co
);
270 FillRectangle(rc
, back
);
272 // ybase is where the baseline should be, but wxWin uses the upper left
273 // corner, so I need to calculate the real position for the text...
274 hdc
->DrawText(wxString(s
, len
), rc
.left
, ybase
- font
.ascent
);
277 void Surface::DrawTextClipped(PRectangle rc
, Font
&font
, int ybase
, const char *s
, int len
, Colour fore
, Colour back
) {
279 hdc
->SetTextForeground(fore
.co
);
280 hdc
->SetTextBackground(back
.co
);
281 FillRectangle(rc
, back
);
282 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
284 // see comments above
285 hdc
->DrawText(wxString(s
, len
), rc
.left
, ybase
- font
.ascent
);
286 hdc
->DestroyClippingRegion();
289 int Surface::WidthText(Font
&font
, const char *s
, int len
) {
293 hdc
->GetTextExtent(wxString(s
, len
), &w
, &h
);
297 void Surface::MeasureWidths(Font
&font
, const char *s
, int len
, int *positions
) {
300 for (int i
=0; i
<len
; i
++) {
303 hdc
->GetTextExtent(s
[i
], &w
, &h
);
305 positions
[i
] = totalWidth
;
309 int Surface::WidthChar(Font
&font
, char ch
) {
313 hdc
->GetTextExtent(ch
, &w
, &h
);
317 #define EXTENT_TEST " `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
319 int Surface::Ascent(Font
&font
) {
322 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
327 int Surface::Descent(Font
&font
) {
330 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
334 int Surface::InternalLeading(Font
&font
) {
338 int Surface::ExternalLeading(Font
&font
) {
341 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
345 int Surface::Height(Font
&font
) {
347 return hdc
->GetCharHeight();
350 int Surface::AverageCharWidth(Font
&font
) {
352 return hdc
->GetCharWidth();
355 int Surface::SetPalette(Palette
*pal
, bool inBackGround
) {
356 return 0; // **** figure out what to do with palettes...
359 void Surface::SetClip(PRectangle rc
) {
360 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
363 void Surface::FlushCachedState() {
369 void Window::Destroy() {
375 bool Window::HasFocus() {
376 return wxWindow::FindFocus() == id
;
379 PRectangle
Window::GetPosition() {
380 wxRect
rc(id
->GetPosition(), id
->GetSize());
381 return PRectangleFromwxRect(rc
);
384 void Window::SetPosition(PRectangle rc
) {
385 wxRect r
= wxRectFromPRectangle(rc
);
389 void Window::SetPositionRelative(PRectangle rc
, Window
) {
390 SetPosition(rc
); // ????
393 PRectangle
Window::GetClientPosition() {
394 wxSize sz
= id
->GetClientSize();
395 return PRectangle(0, 0, sz
.x
, sz
.y
);
398 void Window::Show(bool show
) {
402 void Window::InvalidateAll() {
406 void Window::InvalidateRectangle(PRectangle rc
) {
407 wxRect r
= wxRectFromPRectangle(rc
);
408 id
->Refresh(false, &r
);
411 void Window::SetFont(Font
&font
) {
412 id
->SetFont(*font
.GetID());
415 void Window::SetCursor(Cursor curs
) {
420 cursorId
= wxCURSOR_IBEAM
;
423 cursorId
= wxCURSOR_ARROW
;
426 cursorId
= wxCURSOR_ARROW
; // ** no up arrow... wxCURSOR_UPARROW;
429 cursorId
= wxCURSOR_WAIT
;
432 cursorId
= wxCURSOR_SIZEWE
;
435 cursorId
= wxCURSOR_SIZENS
;
437 case cursorReverseArrow
:
438 cursorId
= wxCURSOR_POINT_RIGHT
;
441 cursorId
= wxCURSOR_ARROW
;
445 id
->SetCursor(wxCursor(cursorId
));
449 void Window::SetTitle(const char *s
) {
454 class wxSTCListBox
: public wxListBox
{
456 wxSTCListBox(wxWindow
* parent
, wxWindowID id
)
457 : wxListBox(parent
, id
, wxDefaultPosition
, wxDefaultSize
,
458 0, NULL
, wxLB_SINGLE
| wxLB_SORT
| wxSIMPLE_BORDER
)
461 void OnFocus(wxFocusEvent
& event
) {
462 GetParent()->SetFocus();
467 void DoSetFirstItem(int n
);
471 DECLARE_EVENT_TABLE()
474 BEGIN_EVENT_TABLE(wxSTCListBox
, wxListBox
)
475 EVT_SET_FOCUS(wxSTCListBox::OnFocus
)
482 // This can be removed after 2.2.2 I think
483 void wxSTCListBox::DoSetFirstItem( int n
)
485 wxCHECK_RET( m_list
, wxT("invalid listbox") );
487 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list
))
490 // terribly efficient
491 const gchar
*vadjustment_key
= "gtk-vadjustment";
492 guint vadjustment_key_id
= g_quark_from_static_string (vadjustment_key
);
494 GtkAdjustment
*adjustment
=
495 (GtkAdjustment
*) gtk_object_get_data_by_id (GTK_OBJECT (m_list
), vadjustment_key_id
);
496 wxCHECK_RET( adjustment
, wxT("invalid listbox code") );
498 GList
*target
= g_list_nth( m_list
->children
, n
);
499 wxCHECK_RET( target
, wxT("invalid listbox index") );
501 GtkWidget
*item
= GTK_WIDGET(target
->data
);
502 wxCHECK_RET( item
, wxT("invalid listbox code") );
504 // find the last item before this one which is already realized
506 for ( nItemsBefore
= 0; item
&& (item
->allocation
.y
== -1); nItemsBefore
++ )
508 target
= target
->prev
;
511 // nothing we can do if there are no allocated items yet
515 item
= GTK_WIDGET(target
->data
);
518 gtk_adjustment_set_value(adjustment
,
520 nItemsBefore
*item
->allocation
.height
);
528 ListBox::~ListBox() {
531 void ListBox::Create(Window
&parent
, int ctrlID
) {
532 id
= new wxSTCListBox(parent
.id
, ctrlID
);
533 // id = new wxListBox(parent.id, ctrlID, wxDefaultPosition, wxDefaultSize,
534 // 0, NULL, wxLB_SINGLE | wxLB_SORT | wxSIMPLE_BORDER);
537 PRectangle
ListBox::GetDesiredRect() {
538 wxSize sz
= ((wxListBox
*)id
)->GetBestSize();
542 if (sz
.x
> 150) // TODO: A better way to determine these max sizes
552 void ListBox::SetAverageCharWidth(int width
) {
553 aveCharWidth
= width
;
556 void ListBox::SetFont(Font
&font
) {
557 Window::SetFont(font
);
560 void ListBox::Clear() {
561 ((wxListBox
*)id
)->Clear();
564 void ListBox::Append(char *s
) {
565 ((wxListBox
*)id
)->Append(s
);
568 int ListBox::Length() {
569 return ((wxListBox
*)id
)->GetCount();
572 void ListBox::Select(int n
) {
573 ((wxListBox
*)id
)->SetSelection(n
);
579 ((wxListBox
*)id
)->SetFirstItem(n
);
583 int ListBox::GetSelection() {
584 return ((wxListBox
*)id
)->GetSelection();
587 int ListBox::Find(const char *prefix
) {
589 for (int x
=0; x
< ((wxListBox
*)id
)->GetCount(); x
++) {
590 wxString text
= ((wxListBox
*)id
)->GetString(x
);
591 if (text
.StartsWith(prefix
))
598 void ListBox::GetValue(int n
, char *value
, int len
) {
599 wxString text
= ((wxListBox
*)id
)->GetString(n
);
600 strncpy(value
, text
.c_str(), len
);
604 void ListBox::Sort() {
605 // wxWindows keeps sorted so no need to sort
609 Menu::Menu() : id(0) {
612 void Menu::CreatePopUp() {
617 void Menu::Destroy() {
623 void Menu::Show(Point pt
, Window
&w
) {
624 w
.GetID()->PopupMenu(id
, pt
.x
- 4, pt
.y
);
629 Colour
Platform::Chrome() {
631 c
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
632 return Colour(c
.Red(), c
.Green(), c
.Blue());
635 Colour
Platform::ChromeHighlight() {
637 c
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHIGHLIGHT
);
638 return Colour(c
.Red(), c
.Green(), c
.Blue());
641 const char *Platform::DefaultFont() {
642 return wxNORMAL_FONT
->GetFaceName();
645 int Platform::DefaultFontSize() {
649 unsigned int Platform::DoubleClickTime() {
650 return 500; // **** ::GetDoubleClickTime();
653 void Platform::DebugDisplay(const char *s
) {
657 bool Platform::IsKeyDown(int key
) {
658 return false; // I don't think we'll need this.
661 long Platform::SendScintilla(WindowID w
,
663 unsigned long wParam
,
666 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
667 return stc
->SendMsg(msg
, wParam
, lParam
);
671 // These are utility functions not really tied to a platform
673 int Platform::Minimum(int a
, int b
) {
680 int Platform::Maximum(int a
, int b
) {
689 void Platform::DebugPrintf(const char *format
, ...) {
693 va_start(pArguments
, format
);
694 vsprintf(buffer
,format
,pArguments
);
696 Platform::DebugDisplay(buffer
);
700 int Platform::Clamp(int val
, int minVal
, int maxVal
) {