1 // Scintilla source code edit control
2 // PlatWX.cxx - implementation of platform facilities on wxWidgets
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.
9 #define Point macPoint // These names are also defined by some mac headers so
10 #define Style macStyle // change their names, and then undef before we need them
13 #include <wx/encconv.h>
14 #include <wx/listctrl.h>
15 #include <wx/mstream.h>
17 #include <wx/imaglist.h>
24 #include "wx/stc/stc.h"
28 Point
Point::FromLong(long lpoint
) {
29 return Point(lpoint
& 0xFFFF, lpoint
>> 16);
32 wxRect
wxRectFromPRectangle(PRectangle prc
) {
33 wxRect
r(prc
.left
, prc
.top
,
34 prc
.Width(), prc
.Height());
38 PRectangle
PRectangleFromwxRect(wxRect rc
) {
39 return PRectangle(rc
.GetLeft(), rc
.GetTop(),
40 rc
.GetRight()+1, rc
.GetBottom()+1);
43 wxColour
wxColourFromCA(const ColourAllocated
& ca
) {
44 ColourDesired
cd(ca
.AsLong());
45 return wxColour((unsigned char)cd
.GetRed(),
46 (unsigned char)cd
.GetGreen(),
47 (unsigned char)cd
.GetBlue());
50 //----------------------------------------------------------------------
54 allowRealization
= false;
61 void Palette::Release() {
65 // This method either adds a colour to the list of wanted colours (want==true)
66 // or retrieves the allocated colour back to the ColourPair.
67 // This is one method to make it easier to keep the code for wanting and retrieving in sync.
68 void Palette::WantFind(ColourPair
&cp
, bool want
) {
70 for (int i
=0; i
< used
; i
++) {
71 if (entries
[i
].desired
== cp
.desired
)
75 if (used
< numEntries
) {
76 entries
[used
].desired
= cp
.desired
;
77 entries
[used
].allocated
.Set(cp
.desired
.AsLong());
81 for (int i
=0; i
< used
; i
++) {
82 if (entries
[i
].desired
== cp
.desired
) {
83 cp
.allocated
= entries
[i
].allocated
;
87 cp
.allocated
.Set(cp
.desired
.AsLong());
91 void Palette::Allocate(Window
&) {
92 if (allowRealization
) {
97 //----------------------------------------------------------------------
107 void Font::Create(const char *faceName
, int characterSet
, int size
, bool bold
, bool italic
, bool extraFontFlag
) {
108 wxFontEncoding encoding
;
112 switch (characterSet
) {
114 case wxSTC_CHARSET_ANSI
:
115 case wxSTC_CHARSET_DEFAULT
:
116 encoding
= wxFONTENCODING_DEFAULT
;
119 case wxSTC_CHARSET_BALTIC
:
120 encoding
= wxFONTENCODING_ISO8859_13
;
123 case wxSTC_CHARSET_CHINESEBIG5
:
124 encoding
= wxFONTENCODING_CP950
;
127 case wxSTC_CHARSET_EASTEUROPE
:
128 encoding
= wxFONTENCODING_ISO8859_2
;
131 case wxSTC_CHARSET_GB2312
:
132 encoding
= wxFONTENCODING_CP936
;
135 case wxSTC_CHARSET_GREEK
:
136 encoding
= wxFONTENCODING_ISO8859_7
;
139 case wxSTC_CHARSET_HANGUL
:
140 encoding
= wxFONTENCODING_CP949
;
143 case wxSTC_CHARSET_MAC
:
144 encoding
= wxFONTENCODING_DEFAULT
;
147 case wxSTC_CHARSET_OEM
:
148 encoding
= wxFONTENCODING_DEFAULT
;
151 case wxSTC_CHARSET_RUSSIAN
:
152 encoding
= wxFONTENCODING_KOI8
;
155 case wxSTC_CHARSET_SHIFTJIS
:
156 encoding
= wxFONTENCODING_CP932
;
159 case wxSTC_CHARSET_SYMBOL
:
160 encoding
= wxFONTENCODING_DEFAULT
;
163 case wxSTC_CHARSET_TURKISH
:
164 encoding
= wxFONTENCODING_ISO8859_9
;
167 case wxSTC_CHARSET_JOHAB
:
168 encoding
= wxFONTENCODING_DEFAULT
;
171 case wxSTC_CHARSET_HEBREW
:
172 encoding
= wxFONTENCODING_ISO8859_8
;
175 case wxSTC_CHARSET_ARABIC
:
176 encoding
= wxFONTENCODING_ISO8859_6
;
179 case wxSTC_CHARSET_VIETNAMESE
:
180 encoding
= wxFONTENCODING_DEFAULT
;
183 case wxSTC_CHARSET_THAI
:
184 encoding
= wxFONTENCODING_ISO8859_11
;
188 wxFontEncodingArray ea
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
192 wxFont
* font
= new wxFont(size
,
194 italic
? wxITALIC
: wxNORMAL
,
195 bold
? wxBOLD
: wxNORMAL
,
199 font
->SetNoAntiAliasing(!extraFontFlag
);
204 void Font::Release() {
210 //----------------------------------------------------------------------
212 class SurfaceImpl
: public Surface
{
225 virtual void Init(WindowID wid
);
226 virtual void Init(SurfaceID sid
, WindowID wid
);
227 virtual void InitPixMap(int width
, int height
, Surface
*surface_
, WindowID wid
);
229 virtual void Release();
230 virtual bool Initialised();
231 virtual void PenColour(ColourAllocated fore
);
232 virtual int LogPixelsY();
233 virtual int DeviceHeightFont(int points
);
234 virtual void MoveTo(int x_
, int y_
);
235 virtual void LineTo(int x_
, int y_
);
236 virtual void Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
);
237 virtual void RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
238 virtual void FillRectangle(PRectangle rc
, ColourAllocated back
);
239 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
);
240 virtual void RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
241 virtual void Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
242 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
);
244 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
245 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
246 virtual void DrawTextTransparent(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
);
247 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, int *positions
);
248 virtual int WidthText(Font
&font_
, const char *s
, int len
);
249 virtual int WidthChar(Font
&font_
, char ch
);
250 virtual int Ascent(Font
&font_
);
251 virtual int Descent(Font
&font_
);
252 virtual int InternalLeading(Font
&font_
);
253 virtual int ExternalLeading(Font
&font_
);
254 virtual int Height(Font
&font_
);
255 virtual int AverageCharWidth(Font
&font_
);
257 virtual int SetPalette(Palette
*pal
, bool inBackGround
);
258 virtual void SetClip(PRectangle rc
);
259 virtual void FlushCachedState();
261 virtual void SetUnicodeMode(bool unicodeMode_
);
262 virtual void SetDBCSMode(int codePage
);
264 void BrushColour(ColourAllocated back
);
265 void SetFont(Font
&font_
);
270 SurfaceImpl::SurfaceImpl() :
271 hdc(0), hdcOwned(0), bitmap(0),
272 x(0), y(0), unicodeMode(0)
275 SurfaceImpl::~SurfaceImpl() {
279 void SurfaceImpl::Init(WindowID wid
) {
282 hdc
= new wxMemoryDC();
285 // On Mac and GTK the DC is not really valid until it has a bitmap
286 // selected into it. So instead of just creating the DC with no bitmap,
287 // go ahead and give it one.
288 InitPixMap(1,1,NULL
,wid
);
292 void SurfaceImpl::Init(SurfaceID hdc_
, WindowID
) {
297 void SurfaceImpl::InitPixMap(int width
, int height
, Surface
*WXUNUSED(surface_
), WindowID
) {
299 hdc
= new wxMemoryDC();
301 if (width
< 1) width
= 1;
302 if (height
< 1) height
= 1;
303 bitmap
= new wxBitmap(width
, height
);
304 ((wxMemoryDC
*)hdc
)->SelectObject(*bitmap
);
308 void SurfaceImpl::Release() {
310 ((wxMemoryDC
*)hdc
)->SelectObject(wxNullBitmap
);
322 bool SurfaceImpl::Initialised() {
327 void SurfaceImpl::PenColour(ColourAllocated fore
) {
328 hdc
->SetPen(wxPen(wxColourFromCA(fore
), 1, wxSOLID
));
331 void SurfaceImpl::BrushColour(ColourAllocated back
) {
332 hdc
->SetBrush(wxBrush(wxColourFromCA(back
), wxSOLID
));
335 void SurfaceImpl::SetFont(Font
&font_
) {
337 hdc
->SetFont(*((wxFont
*)font_
.GetID()));
341 int SurfaceImpl::LogPixelsY() {
342 return hdc
->GetPPI().y
;
345 int SurfaceImpl::DeviceHeightFont(int points
) {
349 void SurfaceImpl::MoveTo(int x_
, int y_
) {
354 void SurfaceImpl::LineTo(int x_
, int y_
) {
355 hdc
->DrawLine(x
,y
, x_
,y_
);
360 void SurfaceImpl::Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
) {
363 hdc
->DrawPolygon(npts
, (wxPoint
*)pts
);
366 void SurfaceImpl::RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
369 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
372 void SurfaceImpl::FillRectangle(PRectangle rc
, ColourAllocated back
) {
374 hdc
->SetPen(*wxTRANSPARENT_PEN
);
375 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
378 void SurfaceImpl::FillRectangle(PRectangle rc
, Surface
&surfacePattern
) {
380 if (((SurfaceImpl
&)surfacePattern
).bitmap
)
381 br
= wxBrush(*((SurfaceImpl
&)surfacePattern
).bitmap
);
382 else // Something is wrong so display in red
383 br
= wxBrush(*wxRED
, wxSOLID
);
384 hdc
->SetPen(*wxTRANSPARENT_PEN
);
386 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
389 void SurfaceImpl::RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
392 hdc
->DrawRoundedRectangle(wxRectFromPRectangle(rc
), 4);
395 void SurfaceImpl::Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
398 hdc
->DrawEllipse(wxRectFromPRectangle(rc
));
401 void SurfaceImpl::Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
) {
402 wxRect r
= wxRectFromPRectangle(rc
);
403 hdc
->Blit(r
.x
, r
.y
, r
.width
, r
.height
,
404 ((SurfaceImpl
&)surfaceSource
).hdc
,
405 from
.x
, from
.y
, wxCOPY
);
408 void SurfaceImpl::DrawTextNoClip(PRectangle rc
, Font
&font
, int ybase
,
409 const char *s
, int len
,
410 ColourAllocated fore
, ColourAllocated back
) {
412 hdc
->SetTextForeground(wxColourFromCA(fore
));
413 hdc
->SetTextBackground(wxColourFromCA(back
));
414 FillRectangle(rc
, back
);
416 // ybase is where the baseline should be, but wxWin uses the upper left
417 // corner, so I need to calculate the real position for the text...
418 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
421 void SurfaceImpl::DrawTextClipped(PRectangle rc
, Font
&font
, int ybase
,
422 const char *s
, int len
,
423 ColourAllocated fore
, ColourAllocated back
) {
425 hdc
->SetTextForeground(wxColourFromCA(fore
));
426 hdc
->SetTextBackground(wxColourFromCA(back
));
427 FillRectangle(rc
, back
);
428 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
430 // see comments above
431 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
432 hdc
->DestroyClippingRegion();
436 void SurfaceImpl::DrawTextTransparent(PRectangle rc
, Font
&font
, int ybase
,
437 const char *s
, int len
,
438 ColourAllocated fore
) {
441 hdc
->SetTextForeground(wxColourFromCA(fore
));
442 hdc
->SetBackgroundMode(wxTRANSPARENT
);
444 // ybase is where the baseline should be, but wxWin uses the upper left
445 // corner, so I need to calculate the real position for the text...
446 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
448 hdc
->SetBackgroundMode(wxSOLID
);
452 void SurfaceImpl::MeasureWidths(Font
&font
, const char *s
, int len
, int *positions
) {
454 wxString str
= stc2wx(s
, len
);
459 hdc
->GetPartialTextExtents(str
, tpos
);
462 // Map the widths for UCS-2 characters back to the UTF-8 input string
463 // NOTE: I don't think this is right for when sizeof(wxChar) > 2, ie wxGTK2
464 // so figure it out and fix it!
467 while ((int)i
< len
) {
468 unsigned char uch
= (unsigned char)s
[i
];
469 positions
[i
++] = tpos
[ui
];
471 if (uch
< (0x80 + 0x40 + 0x20)) {
472 positions
[i
++] = tpos
[ui
];
474 positions
[i
++] = tpos
[ui
];
475 positions
[i
++] = tpos
[ui
];
482 // If not unicode then just use the widths we have
483 memcpy(positions
, tpos
.begin(), len
* sizeof(int));
488 int SurfaceImpl::WidthText(Font
&font
, const char *s
, int len
) {
493 hdc
->GetTextExtent(stc2wx(s
, len
), &w
, &h
);
498 int SurfaceImpl::WidthChar(Font
&font
, char ch
) {
502 char s
[2] = { ch
, 0 };
504 hdc
->GetTextExtent(stc2wx(s
, 1), &w
, &h
);
508 #define EXTENT_TEST wxT(" `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
510 int SurfaceImpl::Ascent(Font
&font
) {
513 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
518 int SurfaceImpl::Descent(Font
&font
) {
521 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
525 int SurfaceImpl::InternalLeading(Font
&WXUNUSED(font
)) {
529 int SurfaceImpl::ExternalLeading(Font
&font
) {
532 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
536 int SurfaceImpl::Height(Font
&font
) {
538 return hdc
->GetCharHeight() + 1;
541 int SurfaceImpl::AverageCharWidth(Font
&font
) {
543 return hdc
->GetCharWidth();
546 int SurfaceImpl::SetPalette(Palette
*WXUNUSED(pal
), bool WXUNUSED(inBackGround
)) {
550 void SurfaceImpl::SetClip(PRectangle rc
) {
551 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
554 void SurfaceImpl::FlushCachedState() {
557 void SurfaceImpl::SetUnicodeMode(bool unicodeMode_
) {
558 unicodeMode
=unicodeMode_
;
561 void SurfaceImpl::SetDBCSMode(int WXUNUSED(codePage
)) {
562 // dbcsMode = codePage == SC_CP_DBCS;
566 Surface
*Surface::Allocate() {
567 return new SurfaceImpl
;
571 //----------------------------------------------------------------------
574 inline wxWindow
* GETWIN(WindowID id
) { return (wxWindow
*)id
; }
579 void Window::Destroy() {
582 GETWIN(id
)->Destroy();
587 bool Window::HasFocus() {
588 return wxWindow::FindFocus() == GETWIN(id
);
591 PRectangle
Window::GetPosition() {
592 if (! id
) return PRectangle();
593 wxRect
rc(GETWIN(id
)->GetPosition(), GETWIN(id
)->GetSize());
594 return PRectangleFromwxRect(rc
);
597 void Window::SetPosition(PRectangle rc
) {
598 wxRect r
= wxRectFromPRectangle(rc
);
599 GETWIN(id
)->SetSize(r
);
602 void Window::SetPositionRelative(PRectangle rc
, Window
) {
603 SetPosition(rc
); // ????
606 PRectangle
Window::GetClientPosition() {
607 if (! id
) return PRectangle();
608 wxSize sz
= GETWIN(id
)->GetClientSize();
609 return PRectangle(0, 0, sz
.x
, sz
.y
);
612 void Window::Show(bool show
) {
613 GETWIN(id
)->Show(show
);
616 void Window::InvalidateAll() {
617 GETWIN(id
)->Refresh(false);
621 void Window::InvalidateRectangle(PRectangle rc
) {
622 wxRect r
= wxRectFromPRectangle(rc
);
623 GETWIN(id
)->Refresh(false, &r
);
627 void Window::SetFont(Font
&font
) {
628 GETWIN(id
)->SetFont(*((wxFont
*)font
.GetID()));
631 void Window::SetCursor(Cursor curs
) {
636 cursorId
= wxCURSOR_IBEAM
;
639 cursorId
= wxCURSOR_ARROW
;
642 cursorId
= wxCURSOR_ARROW
; // ** no up arrow... wxCURSOR_UPARROW;
645 cursorId
= wxCURSOR_WAIT
;
648 cursorId
= wxCURSOR_SIZEWE
;
651 cursorId
= wxCURSOR_SIZENS
;
653 case cursorReverseArrow
:
654 cursorId
= wxCURSOR_RIGHT_ARROW
;
657 cursorId
= wxCURSOR_HAND
;
660 cursorId
= wxCURSOR_ARROW
;
664 wxCursor wc
= wxStockCursor(cursorId
) ;
666 wxCursor wc
= wxCursor(cursorId
) ;
668 GETWIN(id
)->SetCursor(wc
);
672 void Window::SetTitle(const char *s
) {
673 GETWIN(id
)->SetTitle(stc2wx(s
));
677 //----------------------------------------------------------------------
678 // Helper classes for ListBox
681 // This is a simple subclass of wxListView that just resets focus to the
682 // parent when it gets it.
683 class wxSTCListBox
: public wxListView
{
685 wxSTCListBox(wxWindow
* parent
, wxWindowID id
,
686 const wxPoint
& pos
, const wxSize
& size
,
688 : wxListView(parent
, id
, pos
, size
, style
)
692 void OnFocus(wxFocusEvent
& event
) {
693 GetParent()->SetFocus();
697 void OnKillFocus(wxFocusEvent
& WXUNUSED(event
)) {
698 // Do nothing. Prevents base class from resetting the colors...
702 // For some reason I don't understand yet the focus doesn't really leave
703 // the listbox like it should, so if we get any events feed them back to
705 void OnKeyDown(wxKeyEvent
& event
) {
706 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
708 void OnChar(wxKeyEvent
& event
) {
709 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
712 // And we need to force the focus back when being destroyed
714 GetGrandParent()->SetFocus();
719 DECLARE_EVENT_TABLE()
722 BEGIN_EVENT_TABLE(wxSTCListBox
, wxListView
)
723 EVT_SET_FOCUS( wxSTCListBox::OnFocus
)
724 EVT_KILL_FOCUS(wxSTCListBox::OnKillFocus
)
726 EVT_KEY_DOWN( wxSTCListBox::OnKeyDown
)
727 EVT_CHAR( wxSTCListBox::OnChar
)
734 // A window to place the wxSTCListBox upon
735 class wxSTCListBoxWin
: public wxWindow
{
738 CallBackAction doubleClickAction
;
739 void* doubleClickActionData
;
741 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
) :
742 wxWindow(parent
, id
, wxDefaultPosition
, wxSize(0,0), wxSIMPLE_BORDER
)
745 lv
= new wxSTCListBox(this, id
, wxDefaultPosition
, wxDefaultSize
,
746 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxNO_BORDER
);
747 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
748 lv
->InsertColumn(0, wxEmptyString
);
749 lv
->InsertColumn(1, wxEmptyString
);
751 // Eventhough we immediately reset the focus to the parent, this helps
752 // things to look right...
759 // On OSX and (possibly others) there can still be pending
760 // messages/events for the list control when Scintilla wants to
761 // close it, so do a pending delete of it instead of destroying
765 // The bottom edge of this window is not getting properly
766 // refreshed upon deletion, so help it out...
767 wxWindow
* p
= GetParent();
768 wxRect
r(GetPosition(), GetSize());
769 r
.SetHeight(r
.GetHeight()+1);
770 p
->Refresh(false, &r
);
772 if ( !wxPendingDelete
.Member(this) )
773 wxPendingDelete
.Append(this);
779 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
782 il
->GetSize(0, w
, h
);
789 void SetDoubleClickAction(CallBackAction action
, void *data
) {
790 doubleClickAction
= action
;
791 doubleClickActionData
= data
;
795 void OnFocus(wxFocusEvent
& event
) {
796 GetParent()->SetFocus();
800 void OnSize(wxSizeEvent
& event
) {
802 wxSize sz
= GetClientSize();
804 // reset the column widths
805 lv
->SetColumnWidth(0, IconWidth()+4);
806 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
807 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
812 virtual bool Show(bool show
= true) {
813 bool rv
= wxWindow::Show(show
);
814 GetParent()->Refresh(false);
819 void OnActivate(wxListEvent
& WXUNUSED(event
)) {
820 doubleClickAction(doubleClickActionData
);
823 wxListView
* GetLB() { return lv
; }
826 DECLARE_EVENT_TABLE()
830 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxWindow
)
831 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
832 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
833 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
838 inline wxSTCListBoxWin
* GETLBW(WindowID win
) {
839 return ((wxSTCListBoxWin
*)win
);
842 inline wxListView
* GETLB(WindowID win
) {
843 return GETLBW(win
)->GetLB();
846 //----------------------------------------------------------------------
848 class ListBoxImpl
: public ListBox
{
852 int desiredVisibleRows
;
855 wxImageList
* imgList
;
856 wxArrayInt
* imgTypeMap
;
862 virtual void SetFont(Font
&font
);
863 virtual void Create(Window
&parent
, int ctrlID
, int lineHeight_
, bool unicodeMode_
);
864 virtual void SetAverageCharWidth(int width
);
865 virtual void SetVisibleRows(int rows
);
866 virtual PRectangle
GetDesiredRect();
867 virtual int CaretFromEdge();
868 virtual void Clear();
869 virtual void Append(char *s
, int type
= -1);
870 virtual int Length();
871 virtual void Select(int n
);
872 virtual int GetSelection();
873 virtual int Find(const char *prefix
);
874 virtual void GetValue(int n
, char *value
, int len
);
875 virtual void RegisterImage(int type
, const char *xpm_data
);
876 virtual void ClearRegisteredImages();
877 virtual void SetDoubleClickAction(CallBackAction
, void *);
882 ListBoxImpl::ListBoxImpl()
883 : lineHeight(10), unicodeMode(false),
884 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
885 imgList(NULL
), imgTypeMap(NULL
)
889 ListBoxImpl::~ListBoxImpl() {
901 void ListBoxImpl::SetFont(Font
&font
) {
902 GETLB(id
)->SetFont(*((wxFont
*)font
.GetID()));
906 void ListBoxImpl::Create(Window
&parent
, int ctrlID
, int lineHeight_
, bool unicodeMode_
) {
907 lineHeight
= lineHeight_
;
908 unicodeMode
= unicodeMode_
;
910 id
= new wxSTCListBoxWin(GETWIN(parent
.GetID()), ctrlID
);
912 GETLB(id
)->SetImageList(imgList
, wxIMAGE_LIST_SMALL
);
916 void ListBoxImpl::SetAverageCharWidth(int width
) {
917 aveCharWidth
= width
;
921 void ListBoxImpl::SetVisibleRows(int rows
) {
922 desiredVisibleRows
= rows
;
926 PRectangle
ListBoxImpl::GetDesiredRect() {
927 // wxListCtrl doesn't have a DoGetBestSize, so instead we kept track of
928 // the max size in Append and calculate it here...
929 int maxw
= maxStrWidth
;
932 // give it a default if there are no lines, and/or add a bit more
933 if (maxw
== 0) maxw
= 100;
934 maxw
+= aveCharWidth
* 3 +
935 GETLBW(id
)->IconWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
939 // estimate a desired height
940 int count
= GETLB(id
)->GetItemCount();
943 GETLB(id
)->GetItemRect(0, rect
);
944 maxh
= count
* rect
.GetHeight();
945 if (maxh
> 140) // TODO: Use desiredVisibleRows??
948 // Try to make the size an exact multiple of some number of lines
949 int lines
= maxh
/ rect
.GetHeight();
950 maxh
= (lines
+ 1) * rect
.GetHeight() + 2;
964 int ListBoxImpl::CaretFromEdge() {
965 return 4 + GETLBW(id
)->IconWidth();
969 void ListBoxImpl::Clear() {
970 GETLB(id
)->DeleteAllItems();
974 void ListBoxImpl::Append(char *s
, int type
) {
975 wxString text
= stc2wx(s
);
976 long count
= GETLB(id
)->GetItemCount();
977 long itemID
= GETLB(id
)->InsertItem(count
, wxEmptyString
);
978 GETLB(id
)->SetItem(itemID
, 1, text
);
980 GETLB(id
)->GetTextExtent(text
, &itemWidth
, NULL
);
981 maxStrWidth
= wxMax(maxStrWidth
, itemWidth
);
983 wxCHECK_RET(imgTypeMap
, wxT("Unexpected NULL imgTypeMap"));
984 long idx
= imgTypeMap
->Item(type
);
985 GETLB(id
)->SetItemImage(itemID
, idx
, idx
);
990 int ListBoxImpl::Length() {
991 return GETLB(id
)->GetItemCount();
995 void ListBoxImpl::Select(int n
) {
1001 GETLB(id
)->Focus(n
);
1002 GETLB(id
)->Select(n
, select
);
1006 int ListBoxImpl::GetSelection() {
1007 return GETLB(id
)->GetFirstSelected();
1011 int ListBoxImpl::Find(const char *WXUNUSED(prefix
)) {
1017 void ListBoxImpl::GetValue(int n
, char *value
, int len
) {
1021 item
.SetMask(wxLIST_MASK_TEXT
);
1022 GETLB(id
)->GetItem(item
);
1023 strncpy(value
, wx2stc(item
.GetText()), len
);
1024 value
[len
-1] = '\0';
1028 void ListBoxImpl::RegisterImage(int type
, const char *xpm_data
) {
1029 wxMemoryInputStream
stream(xpm_data
, strlen(xpm_data
)+1);
1030 wxImage
img(stream
, wxBITMAP_TYPE_XPM
);
1034 // assumes all images are the same size
1035 imgList
= new wxImageList(bmp
.GetWidth(), bmp
.GetHeight(), true);
1036 imgTypeMap
= new wxArrayInt
;
1039 int idx
= imgList
->Add(bmp
);
1041 // do we need to extend the mapping array?
1042 wxArrayInt
& itm
= *imgTypeMap
;
1043 if ( itm
.GetCount() < (size_t)type
+1)
1044 itm
.Add(-1, type
- itm
.GetCount() + 1);
1046 // Add an item that maps type to the image index
1050 void ListBoxImpl::ClearRegisteredImages() {
1060 GETLB(id
)->SetImageList(NULL
, wxIMAGE_LIST_SMALL
);
1064 void ListBoxImpl::SetDoubleClickAction(CallBackAction action
, void *data
) {
1065 GETLBW(id
)->SetDoubleClickAction(action
, data
);
1070 ListBox::ListBox() {
1073 ListBox::~ListBox() {
1076 ListBox
*ListBox::Allocate() {
1077 return new ListBoxImpl();
1080 //----------------------------------------------------------------------
1082 Menu::Menu() : id(0) {
1085 void Menu::CreatePopUp() {
1090 void Menu::Destroy() {
1096 void Menu::Show(Point pt
, Window
&w
) {
1097 GETWIN(w
.GetID())->PopupMenu((wxMenu
*)id
, pt
.x
- 4, pt
.y
);
1101 //----------------------------------------------------------------------
1103 DynamicLibrary
*DynamicLibrary::Load(const char *WXUNUSED(modulePath
)) {
1104 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1108 //----------------------------------------------------------------------
1110 ColourDesired
Platform::Chrome() {
1112 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1113 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1116 ColourDesired
Platform::ChromeHighlight() {
1118 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
);
1119 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1122 const char *Platform::DefaultFont() {
1123 static char buf
[128];
1124 strcpy(buf
, wxNORMAL_FONT
->GetFaceName().mbc_str());
1128 int Platform::DefaultFontSize() {
1129 return wxNORMAL_FONT
->GetPointSize();
1132 unsigned int Platform::DoubleClickTime() {
1133 return 500; // **** ::GetDoubleClickTime();
1136 bool Platform::MouseButtonBounce() {
1139 void Platform::DebugDisplay(const char *s
) {
1140 wxLogDebug(stc2wx(s
));
1143 bool Platform::IsKeyDown(int WXUNUSED(key
)) {
1144 return false; // I don't think we'll need this.
1147 long Platform::SendScintilla(WindowID w
,
1149 unsigned long wParam
,
1152 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1153 return stc
->SendMsg(msg
, wParam
, lParam
);
1156 long Platform::SendScintillaPointer(WindowID w
,
1158 unsigned long wParam
,
1161 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1162 return stc
->SendMsg(msg
, wParam
, (long)lParam
);
1166 // These are utility functions not really tied to a platform
1168 int Platform::Minimum(int a
, int b
) {
1175 int Platform::Maximum(int a
, int b
) {
1184 void Platform::DebugPrintf(const char *format
, ...) {
1188 va_start(pArguments
, format
);
1189 vsprintf(buffer
,format
,pArguments
);
1191 Platform::DebugDisplay(buffer
);
1196 static bool assertionPopUps
= true;
1198 bool Platform::ShowAssertionPopUps(bool assertionPopUps_
) {
1199 bool ret
= assertionPopUps
;
1200 assertionPopUps
= assertionPopUps_
;
1204 void Platform::Assert(const char *c
, const char *file
, int line
) {
1206 sprintf(buffer
, "Assertion [%s] failed at %s %d", c
, file
, line
);
1207 if (assertionPopUps
) {
1209 wxMessageBox(stc2wx(buffer
),
1210 wxT("Assertion failure"),
1211 wxICON_HAND
| wxOK
);
1212 // if (idButton == IDRETRY) {
1214 // } else if (idButton == IDIGNORE) {
1220 strcat(buffer
, "\r\n");
1221 Platform::DebugDisplay(buffer
);
1227 int Platform::Clamp(int val
, int minVal
, int maxVal
) {
1236 bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage
), char WXUNUSED(ch
)) {
1240 int Platform::DBCSCharLength(int WXUNUSED(codePage
), const char *WXUNUSED(s
)) {
1244 int Platform::DBCSCharMaxLength() {
1249 //----------------------------------------------------------------------
1251 ElapsedTime::ElapsedTime() {
1255 double ElapsedTime::Duration(bool reset
) {
1256 double result
= wxGetElapsedTime(reset
);
1262 //----------------------------------------------------------------------
1266 #include "UniConversion.h"
1268 // Convert using Scintilla's functions instead of wx's, Scintilla's are more
1269 // forgiving and won't assert...
1271 wxString
stc2wx(const char* str
, size_t len
)
1274 return wxEmptyString
;
1276 size_t wclen
= UCS2Length(str
, len
);
1277 wxWCharBuffer
buffer(wclen
+1);
1279 size_t actualLen
= UCS2FromUTF8(str
, len
, buffer
.data(), wclen
+1);
1280 return wxString(buffer
.data(), actualLen
);
1285 wxString
stc2wx(const char* str
)
1287 return stc2wx(str
, strlen(str
));
1291 const wxWX2MBbuf
wx2stc(const wxString
& str
)
1293 const wchar_t* wcstr
= str
.c_str();
1294 size_t wclen
= str
.length();
1295 size_t len
= UTF8Length(wcstr
, wclen
);
1297 wxCharBuffer
buffer(len
+1);
1298 UTF8FromUCS2(wcstr
, wclen
, buffer
.data(), len
);
1300 // TODO check NULL termination!!