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.
10 #include <wx/encconv.h>
11 #include <wx/listctrl.h>
12 #include <wx/mstream.h>
14 #include <wx/imaglist.h>
18 #include "wx/stc/stc.h"
22 Point
Point::FromLong(long lpoint
) {
23 return Point(lpoint
& 0xFFFF, lpoint
>> 16);
26 wxRect
wxRectFromPRectangle(PRectangle prc
) {
27 wxRect
r(prc
.left
, prc
.top
,
28 prc
.Width(), prc
.Height());
32 PRectangle
PRectangleFromwxRect(wxRect rc
) {
33 return PRectangle(rc
.GetLeft(), rc
.GetTop(),
34 rc
.GetRight()+1, rc
.GetBottom()+1);
37 wxColour
wxColourFromCA(const ColourAllocated
& ca
) {
38 ColourDesired
cd(ca
.AsLong());
39 return wxColour((unsigned char)cd
.GetRed(),
40 (unsigned char)cd
.GetGreen(),
41 (unsigned char)cd
.GetBlue());
44 //----------------------------------------------------------------------
48 allowRealization
= false;
55 void Palette::Release() {
59 // This method either adds a colour to the list of wanted colours (want==true)
60 // or retrieves the allocated colour back to the ColourPair.
61 // This is one method to make it easier to keep the code for wanting and retrieving in sync.
62 void Palette::WantFind(ColourPair
&cp
, bool want
) {
64 for (int i
=0; i
< used
; i
++) {
65 if (entries
[i
].desired
== cp
.desired
)
69 if (used
< numEntries
) {
70 entries
[used
].desired
= cp
.desired
;
71 entries
[used
].allocated
.Set(cp
.desired
.AsLong());
75 for (int i
=0; i
< used
; i
++) {
76 if (entries
[i
].desired
== cp
.desired
) {
77 cp
.allocated
= entries
[i
].allocated
;
81 cp
.allocated
.Set(cp
.desired
.AsLong());
85 void Palette::Allocate(Window
&) {
86 if (allowRealization
) {
91 //----------------------------------------------------------------------
101 void Font::Create(const char *faceName
, int characterSet
, int size
, bool bold
, bool italic
, bool extraFontFlag
) {
102 wxFontEncoding encoding
;
106 switch (characterSet
) {
108 case wxSTC_CHARSET_ANSI
:
109 case wxSTC_CHARSET_DEFAULT
:
110 encoding
= wxFONTENCODING_DEFAULT
;
113 case wxSTC_CHARSET_BALTIC
:
114 encoding
= wxFONTENCODING_ISO8859_13
;
117 case wxSTC_CHARSET_CHINESEBIG5
:
118 encoding
= wxFONTENCODING_CP950
;
121 case wxSTC_CHARSET_EASTEUROPE
:
122 encoding
= wxFONTENCODING_ISO8859_2
;
125 case wxSTC_CHARSET_GB2312
:
126 encoding
= wxFONTENCODING_CP936
;
129 case wxSTC_CHARSET_GREEK
:
130 encoding
= wxFONTENCODING_ISO8859_7
;
133 case wxSTC_CHARSET_HANGUL
:
134 encoding
= wxFONTENCODING_CP949
;
137 case wxSTC_CHARSET_MAC
:
138 encoding
= wxFONTENCODING_DEFAULT
;
141 case wxSTC_CHARSET_OEM
:
142 encoding
= wxFONTENCODING_DEFAULT
;
145 case wxSTC_CHARSET_RUSSIAN
:
146 encoding
= wxFONTENCODING_KOI8
;
149 case wxSTC_CHARSET_SHIFTJIS
:
150 encoding
= wxFONTENCODING_CP932
;
153 case wxSTC_CHARSET_SYMBOL
:
154 encoding
= wxFONTENCODING_DEFAULT
;
157 case wxSTC_CHARSET_TURKISH
:
158 encoding
= wxFONTENCODING_ISO8859_9
;
161 case wxSTC_CHARSET_JOHAB
:
162 encoding
= wxFONTENCODING_DEFAULT
;
165 case wxSTC_CHARSET_HEBREW
:
166 encoding
= wxFONTENCODING_ISO8859_8
;
169 case wxSTC_CHARSET_ARABIC
:
170 encoding
= wxFONTENCODING_ISO8859_6
;
173 case wxSTC_CHARSET_VIETNAMESE
:
174 encoding
= wxFONTENCODING_DEFAULT
;
177 case wxSTC_CHARSET_THAI
:
178 encoding
= wxFONTENCODING_ISO8859_11
;
182 wxFontEncodingArray ea
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
186 wxFont
* font
= new wxFont(size
,
188 italic
? wxITALIC
: wxNORMAL
,
189 bold
? wxBOLD
: wxNORMAL
,
193 font
->SetNoAntiAliasing(!extraFontFlag
);
198 void Font::Release() {
204 //----------------------------------------------------------------------
206 class SurfaceImpl
: public Surface
{
219 virtual void Init(WindowID wid
);
220 virtual void Init(SurfaceID sid
, WindowID wid
);
221 virtual void InitPixMap(int width
, int height
, Surface
*surface_
, WindowID wid
);
223 virtual void Release();
224 virtual bool Initialised();
225 virtual void PenColour(ColourAllocated fore
);
226 virtual int LogPixelsY();
227 virtual int DeviceHeightFont(int points
);
228 virtual void MoveTo(int x_
, int y_
);
229 virtual void LineTo(int x_
, int y_
);
230 virtual void Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
);
231 virtual void RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
232 virtual void FillRectangle(PRectangle rc
, ColourAllocated back
);
233 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
);
234 virtual void RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
235 virtual void Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
236 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
);
238 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
239 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
240 virtual void DrawTextTransparent(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
);
241 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, int *positions
);
242 virtual int WidthText(Font
&font_
, const char *s
, int len
);
243 virtual int WidthChar(Font
&font_
, char ch
);
244 virtual int Ascent(Font
&font_
);
245 virtual int Descent(Font
&font_
);
246 virtual int InternalLeading(Font
&font_
);
247 virtual int ExternalLeading(Font
&font_
);
248 virtual int Height(Font
&font_
);
249 virtual int AverageCharWidth(Font
&font_
);
251 virtual int SetPalette(Palette
*pal
, bool inBackGround
);
252 virtual void SetClip(PRectangle rc
);
253 virtual void FlushCachedState();
255 virtual void SetUnicodeMode(bool unicodeMode_
);
256 virtual void SetDBCSMode(int codePage
);
258 void BrushColour(ColourAllocated back
);
259 void SetFont(Font
&font_
);
264 SurfaceImpl::SurfaceImpl() :
265 hdc(0), hdcOwned(0), bitmap(0),
266 x(0), y(0), unicodeMode(0)
269 SurfaceImpl::~SurfaceImpl() {
273 void SurfaceImpl::Init(WindowID wid
) {
276 hdc
= new wxMemoryDC();
279 // On Mac and GTK the DC is not really valid until it has a bitmap
280 // selected into it. So instead of just creating the DC with no bitmap,
281 // go ahead and give it one.
282 InitPixMap(1,1,NULL
,wid
);
286 void SurfaceImpl::Init(SurfaceID hdc_
, WindowID
) {
291 void SurfaceImpl::InitPixMap(int width
, int height
, Surface
*WXUNUSED(surface_
), WindowID
) {
293 hdc
= new wxMemoryDC();
295 if (width
< 1) width
= 1;
296 if (height
< 1) height
= 1;
297 bitmap
= new wxBitmap(width
, height
);
298 ((wxMemoryDC
*)hdc
)->SelectObject(*bitmap
);
302 void SurfaceImpl::Release() {
304 ((wxMemoryDC
*)hdc
)->SelectObject(wxNullBitmap
);
316 bool SurfaceImpl::Initialised() {
321 void SurfaceImpl::PenColour(ColourAllocated fore
) {
322 hdc
->SetPen(wxPen(wxColourFromCA(fore
), 1, wxSOLID
));
325 void SurfaceImpl::BrushColour(ColourAllocated back
) {
326 hdc
->SetBrush(wxBrush(wxColourFromCA(back
), wxSOLID
));
329 void SurfaceImpl::SetFont(Font
&font_
) {
331 hdc
->SetFont(*((wxFont
*)font_
.GetID()));
335 int SurfaceImpl::LogPixelsY() {
336 return hdc
->GetPPI().y
;
339 int SurfaceImpl::DeviceHeightFont(int points
) {
343 void SurfaceImpl::MoveTo(int x_
, int y_
) {
348 void SurfaceImpl::LineTo(int x_
, int y_
) {
349 hdc
->DrawLine(x
,y
, x_
,y_
);
354 void SurfaceImpl::Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
) {
357 hdc
->DrawPolygon(npts
, (wxPoint
*)pts
);
360 void SurfaceImpl::RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
363 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
366 void SurfaceImpl::FillRectangle(PRectangle rc
, ColourAllocated back
) {
368 hdc
->SetPen(*wxTRANSPARENT_PEN
);
369 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
372 void SurfaceImpl::FillRectangle(PRectangle rc
, Surface
&surfacePattern
) {
374 if (((SurfaceImpl
&)surfacePattern
).bitmap
)
375 br
= wxBrush(*((SurfaceImpl
&)surfacePattern
).bitmap
);
376 else // Something is wrong so display in red
377 br
= wxBrush(*wxRED
, wxSOLID
);
378 hdc
->SetPen(*wxTRANSPARENT_PEN
);
380 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
383 void SurfaceImpl::RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
386 hdc
->DrawRoundedRectangle(wxRectFromPRectangle(rc
), 4);
389 void SurfaceImpl::Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
392 hdc
->DrawEllipse(wxRectFromPRectangle(rc
));
395 void SurfaceImpl::Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
) {
396 wxRect r
= wxRectFromPRectangle(rc
);
397 hdc
->Blit(r
.x
, r
.y
, r
.width
, r
.height
,
398 ((SurfaceImpl
&)surfaceSource
).hdc
,
399 from
.x
, from
.y
, wxCOPY
);
402 void SurfaceImpl::DrawTextNoClip(PRectangle rc
, Font
&font
, int ybase
,
403 const char *s
, int len
,
404 ColourAllocated fore
, ColourAllocated back
) {
406 hdc
->SetTextForeground(wxColourFromCA(fore
));
407 hdc
->SetTextBackground(wxColourFromCA(back
));
408 FillRectangle(rc
, back
);
410 // ybase is where the baseline should be, but wxWin uses the upper left
411 // corner, so I need to calculate the real position for the text...
412 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
415 void SurfaceImpl::DrawTextClipped(PRectangle rc
, Font
&font
, int ybase
,
416 const char *s
, int len
,
417 ColourAllocated fore
, ColourAllocated back
) {
419 hdc
->SetTextForeground(wxColourFromCA(fore
));
420 hdc
->SetTextBackground(wxColourFromCA(back
));
421 FillRectangle(rc
, back
);
422 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
424 // see comments above
425 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
426 hdc
->DestroyClippingRegion();
430 void SurfaceImpl::DrawTextTransparent(PRectangle rc
, Font
&font
, int ybase
,
431 const char *s
, int len
,
432 ColourAllocated fore
) {
435 hdc
->SetTextForeground(wxColourFromCA(fore
));
436 hdc
->SetBackgroundMode(wxTRANSPARENT
);
438 // ybase is where the baseline should be, but wxWin uses the upper left
439 // corner, so I need to calculate the real position for the text...
440 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
442 hdc
->SetBackgroundMode(wxSOLID
);
446 void SurfaceImpl::MeasureWidths(Font
&font
, const char *s
, int len
, int *positions
) {
448 wxString str
= stc2wx(s
, len
);
453 hdc
->GetPartialTextExtents(str
, tpos
);
456 // Map the widths for UCS-2 characters back to the UTF-8 input string
457 // NOTE: I don't think this is right for when sizeof(wxChar) > 2, ie wxGTK2
458 // so figure it out and fix it!
461 while ((int)i
< len
) {
462 unsigned char uch
= (unsigned char)s
[i
];
463 positions
[i
++] = tpos
[ui
];
465 if (uch
< (0x80 + 0x40 + 0x20)) {
466 positions
[i
++] = tpos
[ui
];
468 positions
[i
++] = tpos
[ui
];
469 positions
[i
++] = tpos
[ui
];
476 // If not unicode then just use the widths we have
477 memcpy(positions
, tpos
.begin(), len
* sizeof(int));
482 int SurfaceImpl::WidthText(Font
&font
, const char *s
, int len
) {
487 hdc
->GetTextExtent(stc2wx(s
, len
), &w
, &h
);
492 int SurfaceImpl::WidthChar(Font
&font
, char ch
) {
496 char s
[2] = { ch
, 0 };
498 hdc
->GetTextExtent(stc2wx(s
, 1), &w
, &h
);
502 #define EXTENT_TEST wxT(" `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
504 int SurfaceImpl::Ascent(Font
&font
) {
507 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
512 int SurfaceImpl::Descent(Font
&font
) {
515 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
519 int SurfaceImpl::InternalLeading(Font
&WXUNUSED(font
)) {
523 int SurfaceImpl::ExternalLeading(Font
&font
) {
526 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
530 int SurfaceImpl::Height(Font
&font
) {
532 return hdc
->GetCharHeight() + 1;
535 int SurfaceImpl::AverageCharWidth(Font
&font
) {
537 return hdc
->GetCharWidth();
540 int SurfaceImpl::SetPalette(Palette
*WXUNUSED(pal
), bool WXUNUSED(inBackGround
)) {
544 void SurfaceImpl::SetClip(PRectangle rc
) {
545 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
548 void SurfaceImpl::FlushCachedState() {
551 void SurfaceImpl::SetUnicodeMode(bool unicodeMode_
) {
552 unicodeMode
=unicodeMode_
;
555 void SurfaceImpl::SetDBCSMode(int WXUNUSED(codePage
)) {
556 // dbcsMode = codePage == SC_CP_DBCS;
560 Surface
*Surface::Allocate() {
561 return new SurfaceImpl
;
565 //----------------------------------------------------------------------
568 inline wxWindow
* GETWIN(WindowID id
) { return (wxWindow
*)id
; }
573 void Window::Destroy() {
576 GETWIN(id
)->Destroy();
581 bool Window::HasFocus() {
582 return wxWindow::FindFocus() == GETWIN(id
);
585 PRectangle
Window::GetPosition() {
586 if (! id
) return PRectangle();
587 wxRect
rc(GETWIN(id
)->GetPosition(), GETWIN(id
)->GetSize());
588 return PRectangleFromwxRect(rc
);
591 void Window::SetPosition(PRectangle rc
) {
592 wxRect r
= wxRectFromPRectangle(rc
);
593 GETWIN(id
)->SetSize(r
);
596 void Window::SetPositionRelative(PRectangle rc
, Window
) {
597 SetPosition(rc
); // ????
600 PRectangle
Window::GetClientPosition() {
601 if (! id
) return PRectangle();
602 wxSize sz
= GETWIN(id
)->GetClientSize();
603 return PRectangle(0, 0, sz
.x
, sz
.y
);
606 void Window::Show(bool show
) {
607 GETWIN(id
)->Show(show
);
610 void Window::InvalidateAll() {
611 GETWIN(id
)->Refresh(false);
615 void Window::InvalidateRectangle(PRectangle rc
) {
616 wxRect r
= wxRectFromPRectangle(rc
);
617 GETWIN(id
)->Refresh(false, &r
);
621 void Window::SetFont(Font
&font
) {
622 GETWIN(id
)->SetFont(*((wxFont
*)font
.GetID()));
625 void Window::SetCursor(Cursor curs
) {
630 cursorId
= wxCURSOR_IBEAM
;
633 cursorId
= wxCURSOR_ARROW
;
636 cursorId
= wxCURSOR_ARROW
; // ** no up arrow... wxCURSOR_UPARROW;
639 cursorId
= wxCURSOR_WAIT
;
642 cursorId
= wxCURSOR_SIZEWE
;
645 cursorId
= wxCURSOR_SIZENS
;
647 case cursorReverseArrow
:
648 cursorId
= wxCURSOR_RIGHT_ARROW
;
651 cursorId
= wxCURSOR_HAND
;
654 cursorId
= wxCURSOR_ARROW
;
658 wxCursor wc
= wxStockCursor(cursorId
) ;
660 wxCursor wc
= wxCursor(cursorId
) ;
662 GETWIN(id
)->SetCursor(wc
);
666 void Window::SetTitle(const char *s
) {
667 GETWIN(id
)->SetTitle(stc2wx(s
));
671 //----------------------------------------------------------------------
672 // Helper classes for ListBox
675 // This is a simple subclass of wxListView that just resets focus to the
676 // parent when it gets it.
677 class wxSTCListBox
: public wxListView
{
679 wxSTCListBox(wxWindow
* parent
, wxWindowID id
,
680 const wxPoint
& pos
, const wxSize
& size
,
682 : wxListView(parent
, id
, pos
, size
, style
)
686 void OnFocus(wxFocusEvent
& event
) {
687 GetParent()->SetFocus();
691 void OnKillFocus(wxFocusEvent
& WXUNUSED(event
)) {
692 // Do nothing. Prevents base class from resetting the colors...
696 // For some reason I don't understand yet the focus doesn't really leave
697 // the listbox like it should, so if we get any events feed them back to
699 void OnKeyDown(wxKeyEvent
& event
) {
700 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
702 void OnChar(wxKeyEvent
& event
) {
703 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
706 // And we need to force the focus back when being destroyed
708 GetGrandParent()->SetFocus();
713 DECLARE_EVENT_TABLE()
716 BEGIN_EVENT_TABLE(wxSTCListBox
, wxListView
)
717 EVT_SET_FOCUS( wxSTCListBox::OnFocus
)
718 EVT_KILL_FOCUS(wxSTCListBox::OnKillFocus
)
720 EVT_KEY_DOWN( wxSTCListBox::OnKeyDown
)
721 EVT_CHAR( wxSTCListBox::OnChar
)
728 // A window to place the wxSTCListBox upon
729 class wxSTCListBoxWin
: public wxWindow
{
732 CallBackAction doubleClickAction
;
733 void* doubleClickActionData
;
735 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
) :
736 wxWindow(parent
, id
, wxDefaultPosition
, wxSize(0,0), wxSIMPLE_BORDER
)
739 lv
= new wxSTCListBox(this, id
, wxDefaultPosition
, wxDefaultSize
,
740 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxNO_BORDER
);
741 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
742 lv
->InsertColumn(0, wxEmptyString
);
743 lv
->InsertColumn(1, wxEmptyString
);
745 // Eventhough we immediately reset the focus to the parent, this helps
746 // things to look right...
753 // On OSX and (possibly others) there can still be pending
754 // messages/events for the list control when Scintilla wants to
755 // close it, so do a pending delete of it instead of destroying
759 // The bottom edge of this window is not getting properly
760 // refreshed upon deletion, so help it out...
761 wxWindow
* p
= GetParent();
762 wxRect
r(GetPosition(), GetSize());
763 r
.SetHeight(r
.GetHeight()+1);
764 p
->Refresh(false, &r
);
766 if ( !wxPendingDelete
.Member(this) )
767 wxPendingDelete
.Append(this);
773 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
776 il
->GetSize(0, w
, h
);
783 void SetDoubleClickAction(CallBackAction action
, void *data
) {
784 doubleClickAction
= action
;
785 doubleClickActionData
= data
;
789 void OnFocus(wxFocusEvent
& event
) {
790 GetParent()->SetFocus();
794 void OnSize(wxSizeEvent
& event
) {
796 wxSize sz
= GetClientSize();
798 // reset the column widths
799 lv
->SetColumnWidth(0, IconWidth()+4);
800 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
801 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
806 virtual bool Show(bool show
= true) {
807 bool rv
= wxWindow::Show(show
);
808 GetParent()->Refresh(false);
813 void OnActivate(wxListEvent
& WXUNUSED(event
)) {
814 doubleClickAction(doubleClickActionData
);
817 wxListView
* GetLB() { return lv
; }
820 DECLARE_EVENT_TABLE()
824 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxWindow
)
825 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
826 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
827 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
832 inline wxSTCListBoxWin
* GETLBW(WindowID win
) {
833 return ((wxSTCListBoxWin
*)win
);
836 inline wxListView
* GETLB(WindowID win
) {
837 return GETLBW(win
)->GetLB();
840 //----------------------------------------------------------------------
842 class ListBoxImpl
: public ListBox
{
846 int desiredVisibleRows
;
849 wxImageList
* imgList
;
850 wxArrayInt
* imgTypeMap
;
856 virtual void SetFont(Font
&font
);
857 virtual void Create(Window
&parent
, int ctrlID
, int lineHeight_
, bool unicodeMode_
);
858 virtual void SetAverageCharWidth(int width
);
859 virtual void SetVisibleRows(int rows
);
860 virtual PRectangle
GetDesiredRect();
861 virtual int CaretFromEdge();
862 virtual void Clear();
863 virtual void Append(char *s
, int type
= -1);
864 virtual int Length();
865 virtual void Select(int n
);
866 virtual int GetSelection();
867 virtual int Find(const char *prefix
);
868 virtual void GetValue(int n
, char *value
, int len
);
869 virtual void RegisterImage(int type
, const char *xpm_data
);
870 virtual void ClearRegisteredImages();
871 virtual void SetDoubleClickAction(CallBackAction
, void *);
876 ListBoxImpl::ListBoxImpl()
877 : lineHeight(10), unicodeMode(false),
878 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
879 imgList(NULL
), imgTypeMap(NULL
)
883 ListBoxImpl::~ListBoxImpl() {
895 void ListBoxImpl::SetFont(Font
&font
) {
896 GETLB(id
)->SetFont(*((wxFont
*)font
.GetID()));
900 void ListBoxImpl::Create(Window
&parent
, int ctrlID
, int lineHeight_
, bool unicodeMode_
) {
901 lineHeight
= lineHeight_
;
902 unicodeMode
= unicodeMode_
;
904 id
= new wxSTCListBoxWin(GETWIN(parent
.GetID()), ctrlID
);
906 GETLB(id
)->SetImageList(imgList
, wxIMAGE_LIST_SMALL
);
910 void ListBoxImpl::SetAverageCharWidth(int width
) {
911 aveCharWidth
= width
;
915 void ListBoxImpl::SetVisibleRows(int rows
) {
916 desiredVisibleRows
= rows
;
920 PRectangle
ListBoxImpl::GetDesiredRect() {
921 // wxListCtrl doesn't have a DoGetBestSize, so instead we kept track of
922 // the max size in Append and calculate it here...
923 int maxw
= maxStrWidth
;
926 // give it a default if there are no lines, and/or add a bit more
927 if (maxw
== 0) maxw
= 100;
928 maxw
+= aveCharWidth
* 3 +
929 GETLBW(id
)->IconWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
933 // estimate a desired height
934 int count
= GETLB(id
)->GetItemCount();
937 GETLB(id
)->GetItemRect(0, rect
);
938 maxh
= count
* rect
.GetHeight();
939 if (maxh
> 140) // TODO: Use desiredVisibleRows??
942 // Try to make the size an exact multiple of some number of lines
943 int lines
= maxh
/ rect
.GetHeight();
944 maxh
= (lines
+ 1) * rect
.GetHeight() + 2;
958 int ListBoxImpl::CaretFromEdge() {
959 return 4 + GETLBW(id
)->IconWidth();
963 void ListBoxImpl::Clear() {
964 GETLB(id
)->DeleteAllItems();
968 void ListBoxImpl::Append(char *s
, int type
) {
969 wxString text
= stc2wx(s
);
970 long count
= GETLB(id
)->GetItemCount();
971 long itemID
= GETLB(id
)->InsertItem(count
, wxEmptyString
);
972 GETLB(id
)->SetItem(itemID
, 1, text
);
974 GETLB(id
)->GetTextExtent(text
, &itemWidth
, NULL
);
975 maxStrWidth
= wxMax(maxStrWidth
, itemWidth
);
977 wxCHECK_RET(imgTypeMap
, wxT("Unexpected NULL imgTypeMap"));
978 long idx
= imgTypeMap
->Item(type
);
979 GETLB(id
)->SetItemImage(itemID
, idx
, idx
);
984 int ListBoxImpl::Length() {
985 return GETLB(id
)->GetItemCount();
989 void ListBoxImpl::Select(int n
) {
996 GETLB(id
)->Select(n
, select
);
1000 int ListBoxImpl::GetSelection() {
1001 return GETLB(id
)->GetFirstSelected();
1005 int ListBoxImpl::Find(const char *WXUNUSED(prefix
)) {
1011 void ListBoxImpl::GetValue(int n
, char *value
, int len
) {
1015 item
.SetMask(wxLIST_MASK_TEXT
);
1016 GETLB(id
)->GetItem(item
);
1017 strncpy(value
, wx2stc(item
.GetText()), len
);
1018 value
[len
-1] = '\0';
1022 void ListBoxImpl::RegisterImage(int type
, const char *xpm_data
) {
1023 wxMemoryInputStream
stream(xpm_data
, strlen(xpm_data
)+1);
1024 wxImage
img(stream
, wxBITMAP_TYPE_XPM
);
1028 // assumes all images are the same size
1029 imgList
= new wxImageList(bmp
.GetWidth(), bmp
.GetHeight(), true);
1030 imgTypeMap
= new wxArrayInt
;
1033 int idx
= imgList
->Add(bmp
);
1035 // do we need to extend the mapping array?
1036 wxArrayInt
& itm
= *imgTypeMap
;
1037 if ( itm
.GetCount() < (size_t)type
+1)
1038 itm
.Add(-1, type
- itm
.GetCount() + 1);
1040 // Add an item that maps type to the image index
1044 void ListBoxImpl::ClearRegisteredImages() {
1054 GETLB(id
)->SetImageList(NULL
, wxIMAGE_LIST_SMALL
);
1058 void ListBoxImpl::SetDoubleClickAction(CallBackAction action
, void *data
) {
1059 GETLBW(id
)->SetDoubleClickAction(action
, data
);
1064 ListBox::ListBox() {
1067 ListBox::~ListBox() {
1070 ListBox
*ListBox::Allocate() {
1071 return new ListBoxImpl();
1074 //----------------------------------------------------------------------
1076 Menu::Menu() : id(0) {
1079 void Menu::CreatePopUp() {
1084 void Menu::Destroy() {
1090 void Menu::Show(Point pt
, Window
&w
) {
1091 GETWIN(w
.GetID())->PopupMenu((wxMenu
*)id
, pt
.x
- 4, pt
.y
);
1095 //----------------------------------------------------------------------
1097 DynamicLibrary
*DynamicLibrary::Load(const char *WXUNUSED(modulePath
)) {
1098 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1102 //----------------------------------------------------------------------
1104 ColourDesired
Platform::Chrome() {
1106 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1107 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1110 ColourDesired
Platform::ChromeHighlight() {
1112 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
);
1113 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1116 const char *Platform::DefaultFont() {
1117 static char buf
[128];
1118 strcpy(buf
, wxNORMAL_FONT
->GetFaceName().mbc_str());
1122 int Platform::DefaultFontSize() {
1123 return wxNORMAL_FONT
->GetPointSize();
1126 unsigned int Platform::DoubleClickTime() {
1127 return 500; // **** ::GetDoubleClickTime();
1130 bool Platform::MouseButtonBounce() {
1133 void Platform::DebugDisplay(const char *s
) {
1134 wxLogDebug(stc2wx(s
));
1137 bool Platform::IsKeyDown(int WXUNUSED(key
)) {
1138 return false; // I don't think we'll need this.
1141 long Platform::SendScintilla(WindowID w
,
1143 unsigned long wParam
,
1146 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1147 return stc
->SendMsg(msg
, wParam
, lParam
);
1150 long Platform::SendScintillaPointer(WindowID w
,
1152 unsigned long wParam
,
1155 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1156 return stc
->SendMsg(msg
, wParam
, (long)lParam
);
1160 // These are utility functions not really tied to a platform
1162 int Platform::Minimum(int a
, int b
) {
1169 int Platform::Maximum(int a
, int b
) {
1178 void Platform::DebugPrintf(const char *format
, ...) {
1182 va_start(pArguments
, format
);
1183 vsprintf(buffer
,format
,pArguments
);
1185 Platform::DebugDisplay(buffer
);
1190 static bool assertionPopUps
= true;
1192 bool Platform::ShowAssertionPopUps(bool assertionPopUps_
) {
1193 bool ret
= assertionPopUps
;
1194 assertionPopUps
= assertionPopUps_
;
1198 void Platform::Assert(const char *c
, const char *file
, int line
) {
1200 sprintf(buffer
, "Assertion [%s] failed at %s %d", c
, file
, line
);
1201 if (assertionPopUps
) {
1203 wxMessageBox(stc2wx(buffer
),
1204 wxT("Assertion failure"),
1205 wxICON_HAND
| wxOK
);
1206 // if (idButton == IDRETRY) {
1208 // } else if (idButton == IDIGNORE) {
1214 strcat(buffer
, "\r\n");
1215 Platform::DebugDisplay(buffer
);
1221 int Platform::Clamp(int val
, int minVal
, int maxVal
) {
1230 bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage
), char WXUNUSED(ch
)) {
1234 int Platform::DBCSCharLength(int WXUNUSED(codePage
), const char *WXUNUSED(s
)) {
1238 int Platform::DBCSCharMaxLength() {
1243 //----------------------------------------------------------------------
1245 ElapsedTime::ElapsedTime() {
1249 double ElapsedTime::Duration(bool reset
) {
1250 double result
= wxGetElapsedTime(reset
);
1256 //----------------------------------------------------------------------
1260 #include "UniConversion.h"
1262 // Convert using Scintilla's functions instead of wx's, Scintilla's are more
1263 // forgiving and won't assert...
1265 wxString
stc2wx(const char* str
, size_t len
)
1268 return wxEmptyString
;
1270 size_t wclen
= UCS2Length(str
, len
);
1271 wxWCharBuffer
buffer(wclen
+1);
1273 size_t actualLen
= UCS2FromUTF8(str
, len
, buffer
.data(), wclen
+1);
1274 return wxString(buffer
.data(), actualLen
);
1279 wxString
stc2wx(const char* str
)
1281 return stc2wx(str
, strlen(str
));
1285 const wxWX2MBbuf
wx2stc(const wxString
& str
)
1287 const wchar_t* wcstr
= str
.c_str();
1288 size_t wclen
= str
.length();
1289 size_t len
= UTF8Length(wcstr
, wclen
);
1291 wxCharBuffer
buffer(len
+1);
1292 UTF8FromUCS2(wcstr
, wclen
, buffer
.data(), len
);
1294 // TODO check NULL termination!!