]>
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(),
29 rc
.GetRight()+1, rc
.GetBottom()+1);
32 Colour::Colour(long lcol
) {
33 co
.Set(lcol
& 0xff, (lcol
>> 8) & 0xff, (lcol
>> 16) & 0xff);
36 Colour::Colour(unsigned int red
, unsigned int green
, unsigned int blue
) {
37 co
.Set(red
, green
, blue
);
40 bool Colour::operator==(const Colour
&other
) const {
41 return co
== other
.co
;
44 long Colour::AsLong() const {
45 return (((long)co
.Blue() << 16) |
46 ((long)co
.Green() << 8) |
50 unsigned int Colour::GetRed() {
54 unsigned int Colour::GetGreen() {
58 unsigned int Colour::GetBlue() {
64 allowRealization
= false;
71 void Palette::Release() {
75 // This method either adds a colour to the list of wanted colours (want==true)
76 // or retrieves the allocated colour back to the ColourPair.
77 // This is one method to make it easier to keep the code for wanting and retrieving in sync.
78 void Palette::WantFind(ColourPair
&cp
, bool want
) {
80 for (int i
=0; i
< used
; i
++) {
81 if (entries
[i
].desired
== cp
.desired
)
85 if (used
< numEntries
) {
86 entries
[used
].desired
= cp
.desired
;
87 entries
[used
].allocated
= cp
.desired
;
91 for (int i
=0; i
< used
; i
++) {
92 if (entries
[i
].desired
== cp
.desired
) {
93 cp
.allocated
= entries
[i
].allocated
;
97 cp
.allocated
= cp
.desired
;
101 void Palette::Allocate(Window
&) {
102 if (allowRealization
) {
115 void Font::Create(const char *faceName
, int characterSet
, int size
, bool bold
, bool italic
) {
116 // TODO: what to do about the characterSet?
119 id
= new wxFont(size
,
121 italic
? wxITALIC
: wxNORMAL
,
122 bold
? wxBOLD
: wxNORMAL
,
125 wxFONTENCODING_DEFAULT
);
129 void Font::Release() {
137 hdc(0), hdcOwned(0), bitmap(0),
141 Surface::~Surface() {
145 void Surface::Release() {
147 ((wxMemoryDC
*)hdc
)->SelectObject(wxNullBitmap
);
159 bool Surface::Initialised() {
163 void Surface::Init() {
165 hdc
= new wxMemoryDC();
169 void Surface::Init(SurfaceID hdc_
) {
174 void Surface::InitPixMap(int width
, int height
, Surface
*surface_
) {
176 hdc
= new wxMemoryDC(surface_
->hdc
);
178 if (width
< 1) width
= 1;
179 if (height
< 1) height
= 1;
180 bitmap
= new wxBitmap(width
, height
);
181 ((wxMemoryDC
*)hdc
)->SelectObject(*bitmap
);
184 void Surface::PenColour(Colour fore
) {
185 hdc
->SetPen(wxPen(fore
.co
, 1, wxSOLID
));
188 void Surface::BrushColor(Colour back
) {
189 hdc
->SetBrush(wxBrush(back
.co
, wxSOLID
));
192 void Surface::SetFont(Font
&font_
) {
194 hdc
->SetFont(*font_
.GetID());
198 int Surface::LogPixelsY() {
199 return hdc
->GetPPI().y
;
203 int Surface::DeviceHeightFont(int points
) {
208 void Surface::MoveTo(int x_
, int y_
) {
213 void Surface::LineTo(int x_
, int y_
) {
214 hdc
->DrawLine(x
,y
, x_
,y_
);
219 void Surface::Polygon(Point
*pts
, int npts
, Colour fore
,
223 hdc
->DrawPolygon(npts
, (wxPoint
*)pts
);
226 void Surface::RectangleDraw(PRectangle rc
, Colour fore
, Colour back
) {
229 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
232 void Surface::FillRectangle(PRectangle rc
, Colour back
) {
234 hdc
->SetPen(*wxTRANSPARENT_PEN
);
235 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
238 void Surface::FillRectangle(PRectangle rc
, Surface
&surfacePattern
) {
240 if (surfacePattern
.bitmap
)
241 br
= wxBrush(*surfacePattern
.bitmap
);
242 else // Something is wrong so display in red
243 br
= wxBrush(*wxRED
, wxSOLID
);
244 hdc
->SetPen(*wxTRANSPARENT_PEN
);
246 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
249 void Surface::RoundedRectangle(PRectangle rc
, Colour fore
, Colour back
) {
252 hdc
->DrawRoundedRectangle(wxRectFromPRectangle(rc
), 4);
255 void Surface::Ellipse(PRectangle rc
, Colour fore
, Colour back
) {
258 hdc
->DrawEllipse(wxRectFromPRectangle(rc
));
261 void Surface::Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
) {
262 wxRect r
= wxRectFromPRectangle(rc
);
263 hdc
->Blit(r
.x
, r
.y
, r
.width
, r
.height
,
264 surfaceSource
.hdc
, from
.x
, from
.y
, wxCOPY
);
267 void Surface::DrawText(PRectangle rc
, Font
&font
, int ybase
,
268 const char *s
, int len
, Colour fore
, Colour back
) {
270 hdc
->SetTextForeground(fore
.co
);
271 hdc
->SetTextBackground(back
.co
);
272 FillRectangle(rc
, back
);
274 // ybase is where the baseline should be, but wxWin uses the upper left
275 // corner, so I need to calculate the real position for the text...
276 hdc
->DrawText(wxString(s
, len
), rc
.left
, ybase
- font
.ascent
);
279 void Surface::DrawTextClipped(PRectangle rc
, Font
&font
, int ybase
, const char *s
, int len
, Colour fore
, Colour back
) {
281 hdc
->SetTextForeground(fore
.co
);
282 hdc
->SetTextBackground(back
.co
);
283 FillRectangle(rc
, back
);
284 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
286 // see comments above
287 hdc
->DrawText(wxString(s
, len
), rc
.left
, ybase
- font
.ascent
);
288 hdc
->DestroyClippingRegion();
291 int Surface::WidthText(Font
&font
, const char *s
, int len
) {
295 hdc
->GetTextExtent(wxString(s
, len
), &w
, &h
);
299 void Surface::MeasureWidths(Font
&font
, const char *s
, int len
, int *positions
) {
302 for (int i
=0; i
<len
; i
++) {
305 hdc
->GetTextExtent(s
[i
], &w
, &h
);
307 positions
[i
] = totalWidth
;
311 int Surface::WidthChar(Font
&font
, char ch
) {
315 hdc
->GetTextExtent(ch
, &w
, &h
);
319 #define EXTENT_TEST " `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
321 int Surface::Ascent(Font
&font
) {
324 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
329 int Surface::Descent(Font
&font
) {
332 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
336 int Surface::InternalLeading(Font
&font
) {
340 int Surface::ExternalLeading(Font
&font
) {
343 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
347 int Surface::Height(Font
&font
) {
349 return hdc
->GetCharHeight();
352 int Surface::AverageCharWidth(Font
&font
) {
354 return hdc
->GetCharWidth();
357 int Surface::SetPalette(Palette
*pal
, bool inBackGround
) {
361 void Surface::SetClip(PRectangle rc
) {
362 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
365 void Surface::FlushCachedState() {
371 void Window::Destroy() {
377 bool Window::HasFocus() {
378 return wxWindow::FindFocus() == id
;
381 PRectangle
Window::GetPosition() {
382 wxRect
rc(id
->GetPosition(), id
->GetSize());
383 return PRectangleFromwxRect(rc
);
386 void Window::SetPosition(PRectangle rc
) {
387 wxRect r
= wxRectFromPRectangle(rc
);
391 void Window::SetPositionRelative(PRectangle rc
, Window
) {
392 SetPosition(rc
); // ????
395 PRectangle
Window::GetClientPosition() {
396 wxSize sz
= id
->GetClientSize();
397 return PRectangle(0, 0, sz
.x
, sz
.y
);
400 void Window::Show(bool show
) {
404 void Window::InvalidateAll() {
408 void Window::InvalidateRectangle(PRectangle rc
) {
409 wxRect r
= wxRectFromPRectangle(rc
);
410 id
->Refresh(false, &r
);
413 void Window::SetFont(Font
&font
) {
414 id
->SetFont(*font
.GetID());
417 void Window::SetCursor(Cursor curs
) {
422 cursorId
= wxCURSOR_IBEAM
;
425 cursorId
= wxCURSOR_ARROW
;
428 cursorId
= wxCURSOR_ARROW
; // ** no up arrow... wxCURSOR_UPARROW;
431 cursorId
= wxCURSOR_WAIT
;
434 cursorId
= wxCURSOR_SIZEWE
;
437 cursorId
= wxCURSOR_SIZENS
;
439 case cursorReverseArrow
:
440 cursorId
= wxCURSOR_POINT_RIGHT
;
443 cursorId
= wxCURSOR_ARROW
;
447 id
->SetCursor(wxCursor(cursorId
));
451 void Window::SetTitle(const char *s
) {
456 //----------------------------------------------------------------------
457 // Helper classes for ListBox
459 // A wxListBox that gives focus to its parent if it gets it.
460 class wxSTCListBox
: public wxListBox
{
462 wxSTCListBox(wxWindow
* parent
, wxWindowID id
)
463 : wxListBox(parent
, id
, wxDefaultPosition
, wxDefaultSize
,
464 0, NULL
, wxLB_SINGLE
| wxSIMPLE_BORDER
)
467 void OnFocus(wxFocusEvent
& event
) {
468 GetParent()->SetFocus();
473 DECLARE_EVENT_TABLE()
476 BEGIN_EVENT_TABLE(wxSTCListBox
, wxListBox
)
477 EVT_SET_FOCUS(wxSTCListBox::OnFocus
)
482 // A window to place the listbox upon. If wxPopupWindow is supported then
483 // that will be used so the listbox can extend beyond the client area of the
487 #include <wx/popupwin.h>
488 #define wxSTCListBoxWinBase wxPopupWindow
489 #define param2 wxBORDER_NONE // popup's 2nd param is flags
491 #define wxSTCListBoxWinBase wxWindow
492 #define param2 -1 // wxWindows 2nd param is ID
495 class wxSTCListBoxWin
: public wxSTCListBoxWinBase
{
497 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
)
498 : wxSTCListBoxWinBase(parent
, param2
) {
499 lb
= new wxSTCListBox(this, id
);
502 void OnSize(wxSizeEvent
& event
) {
503 lb
->SetSize(GetSize());
505 void OnFocus(wxFocusEvent
& event
) {
506 GetParent()->SetFocus();
510 wxListBox
* GetLB() { return lb
; }
513 virtual void DoSetSize(int x
, int y
,
514 int width
, int height
,
515 int sizeFlags
= wxSIZE_AUTO
) {
517 GetParent()->ClientToScreen(&x
, NULL
);
519 GetParent()->ClientToScreen(NULL
, &y
);
520 wxSTCListBoxWinBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
526 DECLARE_EVENT_TABLE()
529 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxSTCListBoxWinBase
)
530 EVT_SIZE (wxSTCListBoxWin::OnSize
)
531 EVT_SET_FOCUS (wxSTCListBoxWin::OnFocus
)
535 #define GETLB(win) (((wxSTCListBoxWin*)win)->GetLB())
537 //----------------------------------------------------------------------
542 ListBox::~ListBox() {
545 void ListBox::Create(Window
&parent
, int ctrlID
) {
546 id
= new wxSTCListBoxWin(parent
.id
, ctrlID
);
549 void ListBox::SetVisibleRows(int rows
) {
550 desiredVisibleRows
= rows
;
553 PRectangle
ListBox::GetDesiredRect() {
554 wxSize sz
= GETLB(id
)->GetBestSize();
560 if (sz
.y
> 160) // TODO: Use desiredVisibleRows??
567 void ListBox::SetAverageCharWidth(int width
) {
568 aveCharWidth
= width
;
571 void ListBox::SetFont(Font
&font
) {
572 GETLB(id
)->SetFont(*font
.GetID());
575 void ListBox::Clear() {
579 void ListBox::Append(char *s
) {
580 GETLB(id
)->Append(s
);
583 int ListBox::Length() {
584 return GETLB(id
)->GetCount();
587 void ListBox::Select(int n
) {
588 GETLB(id
)->SetSelection(n
);
594 GETLB(id
)->SetFirstItem(n
);
598 int ListBox::GetSelection() {
599 return GETLB(id
)->GetSelection();
602 int ListBox::Find(const char *prefix
) {
607 void ListBox::GetValue(int n
, char *value
, int len
) {
608 wxString text
= GETLB(id
)->GetString(n
);
609 strncpy(value
, text
.c_str(), len
);
613 void ListBox::Sort() {
617 Menu::Menu() : id(0) {
620 void Menu::CreatePopUp() {
625 void Menu::Destroy() {
631 void Menu::Show(Point pt
, Window
&w
) {
632 w
.GetID()->PopupMenu(id
, pt
.x
- 4, pt
.y
);
637 Colour
Platform::Chrome() {
639 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
640 return Colour(c
.Red(), c
.Green(), c
.Blue());
643 Colour
Platform::ChromeHighlight() {
645 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
);
646 return Colour(c
.Red(), c
.Green(), c
.Blue());
649 const char *Platform::DefaultFont() {
650 return wxNORMAL_FONT
->GetFaceName();
653 int Platform::DefaultFontSize() {
657 unsigned int Platform::DoubleClickTime() {
658 return 500; // **** ::GetDoubleClickTime();
661 void Platform::DebugDisplay(const char *s
) {
665 bool Platform::IsKeyDown(int key
) {
666 return false; // I don't think we'll need this.
669 long Platform::SendScintilla(WindowID w
,
671 unsigned long wParam
,
674 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
675 return stc
->SendMsg(msg
, wParam
, lParam
);
679 // These are utility functions not really tied to a platform
681 int Platform::Minimum(int a
, int b
) {
688 int Platform::Maximum(int a
, int b
) {
697 void Platform::DebugPrintf(const char *format
, ...) {
701 va_start(pArguments
, format
);
702 vsprintf(buffer
,format
,pArguments
);
704 Platform::DebugDisplay(buffer
);
709 static bool assertionPopUps
= true;
711 bool Platform::ShowAssertionPopUps(bool assertionPopUps_
) {
712 bool ret
= assertionPopUps
;
713 assertionPopUps
= assertionPopUps_
;
717 void Platform::Assert(const char *c
, const char *file
, int line
) {
719 sprintf(buffer
, "Assertion [%s] failed at %s %d", c
, file
, line
);
720 if (assertionPopUps
) {
721 int idButton
= wxMessageBox(buffer
, "Assertion failure",
723 // if (idButton == IDRETRY) {
725 // } else if (idButton == IDIGNORE) {
731 strcat(buffer
, "\r\n");
732 Platform::DebugDisplay(buffer
);
738 int Platform::Clamp(int val
, int minVal
, int maxVal
) {