]>
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 bitmap
= new wxBitmap(width
, height
);
178 ((wxMemoryDC
*)hdc
)->SelectObject(*bitmap
);
181 void Surface::PenColour(Colour fore
) {
182 hdc
->SetPen(wxPen(fore
.co
, 1, wxSOLID
));
185 void Surface::BrushColor(Colour back
) {
186 hdc
->SetBrush(wxBrush(back
.co
, wxSOLID
));
189 void Surface::SetFont(Font
&font_
) {
191 hdc
->SetFont(*font_
.GetID());
195 int Surface::LogPixelsY() {
196 return hdc
->GetPPI().y
;
200 int Surface::DeviceHeightFont(int points
) {
205 void Surface::MoveTo(int x_
, int y_
) {
210 void Surface::LineTo(int x_
, int y_
) {
211 hdc
->DrawLine(x
,y
, x_
,y_
);
216 void Surface::Polygon(Point
*pts
, int npts
, Colour fore
,
220 hdc
->DrawPolygon(npts
, (wxPoint
*)pts
);
223 void Surface::RectangleDraw(PRectangle rc
, Colour fore
, Colour back
) {
226 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
229 void Surface::FillRectangle(PRectangle rc
, Colour back
) {
231 hdc
->SetPen(*wxTRANSPARENT_PEN
);
232 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
235 void Surface::FillRectangle(PRectangle rc
, Surface
&surfacePattern
) {
237 if (surfacePattern
.bitmap
)
238 br
= wxBrush(*surfacePattern
.bitmap
);
239 else // Something is wrong so display in red
240 br
= wxBrush(*wxRED
, wxSOLID
);
241 hdc
->SetPen(*wxTRANSPARENT_PEN
);
243 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
246 void Surface::RoundedRectangle(PRectangle rc
, Colour fore
, Colour back
) {
249 hdc
->DrawRoundedRectangle(wxRectFromPRectangle(rc
), 4);
252 void Surface::Ellipse(PRectangle rc
, Colour fore
, Colour back
) {
255 hdc
->DrawEllipse(wxRectFromPRectangle(rc
));
258 void Surface::Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
) {
259 wxRect r
= wxRectFromPRectangle(rc
);
260 hdc
->Blit(r
.x
, r
.y
, r
.width
, r
.height
,
261 surfaceSource
.hdc
, from
.x
, from
.y
, wxCOPY
);
264 void Surface::DrawText(PRectangle rc
, Font
&font
, int ybase
,
265 const char *s
, int len
, Colour fore
, Colour back
) {
267 hdc
->SetTextForeground(fore
.co
);
268 hdc
->SetTextBackground(back
.co
);
269 FillRectangle(rc
, back
);
271 // ybase is where the baseline should be, but wxWin uses the upper left
272 // corner, so I need to calculate the real position for the text...
273 hdc
->DrawText(wxString(s
, len
), rc
.left
, ybase
- font
.ascent
);
276 void Surface::DrawTextClipped(PRectangle rc
, Font
&font
, int ybase
, const char *s
, int len
, Colour fore
, Colour back
) {
278 hdc
->SetTextForeground(fore
.co
);
279 hdc
->SetTextBackground(back
.co
);
280 FillRectangle(rc
, back
);
281 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
283 // see comments above
284 hdc
->DrawText(wxString(s
, len
), rc
.left
, ybase
- font
.ascent
);
285 hdc
->DestroyClippingRegion();
288 int Surface::WidthText(Font
&font
, const char *s
, int len
) {
292 hdc
->GetTextExtent(wxString(s
, len
), &w
, &h
);
296 void Surface::MeasureWidths(Font
&font
, const char *s
, int len
, int *positions
) {
299 for (int i
=0; i
<len
; i
++) {
302 hdc
->GetTextExtent(s
[i
], &w
, &h
);
304 positions
[i
] = totalWidth
;
308 int Surface::WidthChar(Font
&font
, char ch
) {
312 hdc
->GetTextExtent(ch
, &w
, &h
);
316 #define EXTENT_TEST " `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
318 int Surface::Ascent(Font
&font
) {
321 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
326 int Surface::Descent(Font
&font
) {
329 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
333 int Surface::InternalLeading(Font
&font
) {
337 int Surface::ExternalLeading(Font
&font
) {
340 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
344 int Surface::Height(Font
&font
) {
346 return hdc
->GetCharHeight();
349 int Surface::AverageCharWidth(Font
&font
) {
351 return hdc
->GetCharWidth();
354 int Surface::SetPalette(Palette
*pal
, bool inBackGround
) {
358 void Surface::SetClip(PRectangle rc
) {
359 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
362 void Surface::FlushCachedState() {
368 void Window::Destroy() {
374 bool Window::HasFocus() {
375 return wxWindow::FindFocus() == id
;
378 PRectangle
Window::GetPosition() {
379 wxRect
rc(id
->GetPosition(), id
->GetSize());
380 return PRectangleFromwxRect(rc
);
383 void Window::SetPosition(PRectangle rc
) {
384 wxRect r
= wxRectFromPRectangle(rc
);
388 void Window::SetPositionRelative(PRectangle rc
, Window
) {
389 SetPosition(rc
); // ????
392 PRectangle
Window::GetClientPosition() {
393 wxSize sz
= id
->GetClientSize();
394 return PRectangle(0, 0, sz
.x
, sz
.y
);
397 void Window::Show(bool show
) {
401 void Window::InvalidateAll() {
405 void Window::InvalidateRectangle(PRectangle rc
) {
406 wxRect r
= wxRectFromPRectangle(rc
);
407 id
->Refresh(false, &r
);
410 void Window::SetFont(Font
&font
) {
411 id
->SetFont(*font
.GetID());
414 void Window::SetCursor(Cursor curs
) {
419 cursorId
= wxCURSOR_IBEAM
;
422 cursorId
= wxCURSOR_ARROW
;
425 cursorId
= wxCURSOR_ARROW
; // ** no up arrow... wxCURSOR_UPARROW;
428 cursorId
= wxCURSOR_WAIT
;
431 cursorId
= wxCURSOR_SIZEWE
;
434 cursorId
= wxCURSOR_SIZENS
;
436 case cursorReverseArrow
:
437 cursorId
= wxCURSOR_POINT_RIGHT
;
440 cursorId
= wxCURSOR_ARROW
;
444 id
->SetCursor(wxCursor(cursorId
));
448 void Window::SetTitle(const char *s
) {
453 class wxSTCListBox
: public wxListBox
{
455 wxSTCListBox(wxWindow
* parent
, wxWindowID id
)
456 : wxListBox(parent
, id
, wxDefaultPosition
, wxDefaultSize
,
457 0, NULL
, wxLB_SINGLE
| wxLB_SORT
| wxSIMPLE_BORDER
)
460 void OnFocus(wxFocusEvent
& event
) {
461 GetParent()->SetFocus();
466 void DoSetFirstItem(int n
);
470 DECLARE_EVENT_TABLE()
473 BEGIN_EVENT_TABLE(wxSTCListBox
, wxListBox
)
474 EVT_SET_FOCUS(wxSTCListBox::OnFocus
)
481 // This can be removed after 2.2.2 I think
482 void wxSTCListBox::DoSetFirstItem( int n
)
484 wxCHECK_RET( m_list
, wxT("invalid listbox") );
486 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list
))
489 // terribly efficient
490 const gchar
*vadjustment_key
= "gtk-vadjustment";
491 guint vadjustment_key_id
= g_quark_from_static_string (vadjustment_key
);
493 GtkAdjustment
*adjustment
=
494 (GtkAdjustment
*) gtk_object_get_data_by_id (GTK_OBJECT (m_list
), vadjustment_key_id
);
495 wxCHECK_RET( adjustment
, wxT("invalid listbox code") );
497 GList
*target
= g_list_nth( m_list
->children
, n
);
498 wxCHECK_RET( target
, wxT("invalid listbox index") );
500 GtkWidget
*item
= GTK_WIDGET(target
->data
);
501 wxCHECK_RET( item
, wxT("invalid listbox code") );
503 // find the last item before this one which is already realized
505 for ( nItemsBefore
= 0; item
&& (item
->allocation
.y
== -1); nItemsBefore
++ )
507 target
= target
->prev
;
510 // nothing we can do if there are no allocated items yet
514 item
= GTK_WIDGET(target
->data
);
517 gtk_adjustment_set_value(adjustment
,
519 nItemsBefore
*item
->allocation
.height
);
527 ListBox::~ListBox() {
530 void ListBox::Create(Window
&parent
, int ctrlID
) {
531 id
= new wxSTCListBox(parent
.id
, ctrlID
);
532 // id = new wxListBox(parent.id, ctrlID, wxDefaultPosition, wxDefaultSize,
533 // 0, NULL, wxLB_SINGLE | wxLB_SORT | wxSIMPLE_BORDER);
536 PRectangle
ListBox::GetDesiredRect() {
537 wxSize sz
= ((wxListBox
*)id
)->GetBestSize();
541 if (sz
.x
> 150) // TODO: A better way to determine these max sizes
551 void ListBox::SetAverageCharWidth(int width
) {
552 aveCharWidth
= width
;
555 void ListBox::SetFont(Font
&font
) {
556 Window::SetFont(font
);
559 void ListBox::Clear() {
560 ((wxListBox
*)id
)->Clear();
563 void ListBox::Append(char *s
) {
564 ((wxListBox
*)id
)->Append(s
);
567 int ListBox::Length() {
568 return ((wxListBox
*)id
)->GetCount();
571 void ListBox::Select(int n
) {
572 ((wxListBox
*)id
)->SetSelection(n
);
578 ((wxListBox
*)id
)->SetFirstItem(n
);
582 int ListBox::GetSelection() {
583 return ((wxListBox
*)id
)->GetSelection();
586 int ListBox::Find(const char *prefix
) {
588 for (int x
=0; x
< ((wxListBox
*)id
)->GetCount(); x
++) {
589 wxString text
= ((wxListBox
*)id
)->GetString(x
);
590 if (text
.StartsWith(prefix
))
597 void ListBox::GetValue(int n
, char *value
, int len
) {
598 wxString text
= ((wxListBox
*)id
)->GetString(n
);
599 strncpy(value
, text
.c_str(), len
);
603 void ListBox::Sort() {
604 // wxWindows keeps sorted so no need to sort
608 Menu::Menu() : id(0) {
611 void Menu::CreatePopUp() {
616 void Menu::Destroy() {
622 void Menu::Show(Point pt
, Window
&w
) {
623 w
.GetID()->PopupMenu(id
, pt
.x
- 4, pt
.y
);
628 Colour
Platform::Chrome() {
630 c
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
631 return Colour(c
.Red(), c
.Green(), c
.Blue());
634 Colour
Platform::ChromeHighlight() {
636 c
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHIGHLIGHT
);
637 return Colour(c
.Red(), c
.Green(), c
.Blue());
640 const char *Platform::DefaultFont() {
641 return wxNORMAL_FONT
->GetFaceName();
644 int Platform::DefaultFontSize() {
648 unsigned int Platform::DoubleClickTime() {
649 return 500; // **** ::GetDoubleClickTime();
652 void Platform::DebugDisplay(const char *s
) {
656 bool Platform::IsKeyDown(int key
) {
657 return false; // I don't think we'll need this.
660 long Platform::SendScintilla(WindowID w
,
662 unsigned long wParam
,
665 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
666 return stc
->SendMsg(msg
, wParam
, lParam
);
670 // These are utility functions not really tied to a platform
672 int Platform::Minimum(int a
, int b
) {
679 int Platform::Maximum(int a
, int b
) {
688 void Platform::DebugPrintf(const char *format
, ...) {
692 va_start(pArguments
, format
);
693 vsprintf(buffer
,format
,pArguments
);
695 Platform::DebugDisplay(buffer
);
700 static bool assertionPopUps
= true;
702 bool Platform::ShowAssertionPopUps(bool assertionPopUps_
) {
703 bool ret
= assertionPopUps
;
704 assertionPopUps
= assertionPopUps_
;
708 void Platform::Assert(const char *c
, const char *file
, int line
) {
710 sprintf(buffer
, "Assertion [%s] failed at %s %d", c
, file
, line
);
711 if (assertionPopUps
) {
712 int idButton
= wxMessageBox(buffer
, "Assertion failure",
714 // if (idButton == IDRETRY) {
716 // } else if (idButton == IDIGNORE) {
722 strcat(buffer
, "\r\n");
723 Platform::DebugDisplay(buffer
);
729 int Platform::Clamp(int val
, int minVal
, int maxVal
) {