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
) {
105 // The minus one is done because since Scintilla uses SC_SHARSET_DEFAULT
106 // internally and we need to have wxFONENCODING_DEFAULT == SC_SHARSET_DEFAULT
107 // so we adjust the encoding before passing it to Scintilla. See also
108 // wxStyledTextCtrl::StyleSetCharacterSet
109 wxFontEncoding encoding
= (wxFontEncoding
)(characterSet
-1);
111 wxFontEncodingArray ea
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
115 wxFont
* font
= new wxFont(size
,
117 italic
? wxITALIC
: wxNORMAL
,
118 bold
? wxBOLD
: wxNORMAL
,
122 font
->SetNoAntiAliasing(!extraFontFlag
);
127 void Font::Release() {
133 //----------------------------------------------------------------------
135 class SurfaceImpl
: public Surface
{
148 virtual void Init(WindowID wid
);
149 virtual void Init(SurfaceID sid
, WindowID wid
);
150 virtual void InitPixMap(int width
, int height
, Surface
*surface_
, WindowID wid
);
152 virtual void Release();
153 virtual bool Initialised();
154 virtual void PenColour(ColourAllocated fore
);
155 virtual int LogPixelsY();
156 virtual int DeviceHeightFont(int points
);
157 virtual void MoveTo(int x_
, int y_
);
158 virtual void LineTo(int x_
, int y_
);
159 virtual void Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
);
160 virtual void RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
161 virtual void FillRectangle(PRectangle rc
, ColourAllocated back
);
162 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
);
163 virtual void RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
164 virtual void Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
165 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
);
167 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
168 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
169 virtual void DrawTextTransparent(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
);
170 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, int *positions
);
171 virtual int WidthText(Font
&font_
, const char *s
, int len
);
172 virtual int WidthChar(Font
&font_
, char ch
);
173 virtual int Ascent(Font
&font_
);
174 virtual int Descent(Font
&font_
);
175 virtual int InternalLeading(Font
&font_
);
176 virtual int ExternalLeading(Font
&font_
);
177 virtual int Height(Font
&font_
);
178 virtual int AverageCharWidth(Font
&font_
);
180 virtual int SetPalette(Palette
*pal
, bool inBackGround
);
181 virtual void SetClip(PRectangle rc
);
182 virtual void FlushCachedState();
184 virtual void SetUnicodeMode(bool unicodeMode_
);
185 virtual void SetDBCSMode(int codePage
);
187 void BrushColour(ColourAllocated back
);
188 void SetFont(Font
&font_
);
193 SurfaceImpl::SurfaceImpl() :
194 hdc(0), hdcOwned(0), bitmap(0),
195 x(0), y(0), unicodeMode(0)
198 SurfaceImpl::~SurfaceImpl() {
202 void SurfaceImpl::Init(WindowID wid
) {
205 hdc
= new wxMemoryDC();
208 // On Mac and GTK the DC is not really valid until it has a bitmap
209 // selected into it. So instead of just creating the DC with no bitmap,
210 // go ahead and give it one.
211 InitPixMap(1,1,NULL
,wid
);
215 void SurfaceImpl::Init(SurfaceID hdc_
, WindowID
) {
220 void SurfaceImpl::InitPixMap(int width
, int height
, Surface
*WXUNUSED(surface_
), WindowID
) {
222 hdc
= new wxMemoryDC();
224 if (width
< 1) width
= 1;
225 if (height
< 1) height
= 1;
226 bitmap
= new wxBitmap(width
, height
);
227 ((wxMemoryDC
*)hdc
)->SelectObject(*bitmap
);
231 void SurfaceImpl::Release() {
233 ((wxMemoryDC
*)hdc
)->SelectObject(wxNullBitmap
);
245 bool SurfaceImpl::Initialised() {
250 void SurfaceImpl::PenColour(ColourAllocated fore
) {
251 hdc
->SetPen(wxPen(wxColourFromCA(fore
), 1, wxSOLID
));
254 void SurfaceImpl::BrushColour(ColourAllocated back
) {
255 hdc
->SetBrush(wxBrush(wxColourFromCA(back
), wxSOLID
));
258 void SurfaceImpl::SetFont(Font
&font_
) {
260 hdc
->SetFont(*((wxFont
*)font_
.GetID()));
264 int SurfaceImpl::LogPixelsY() {
265 return hdc
->GetPPI().y
;
268 int SurfaceImpl::DeviceHeightFont(int points
) {
272 void SurfaceImpl::MoveTo(int x_
, int y_
) {
277 void SurfaceImpl::LineTo(int x_
, int y_
) {
278 hdc
->DrawLine(x
,y
, x_
,y_
);
283 void SurfaceImpl::Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
) {
286 hdc
->DrawPolygon(npts
, (wxPoint
*)pts
);
289 void SurfaceImpl::RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
292 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
295 void SurfaceImpl::FillRectangle(PRectangle rc
, ColourAllocated back
) {
297 hdc
->SetPen(*wxTRANSPARENT_PEN
);
298 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
301 void SurfaceImpl::FillRectangle(PRectangle rc
, Surface
&surfacePattern
) {
303 if (((SurfaceImpl
&)surfacePattern
).bitmap
)
304 br
= wxBrush(*((SurfaceImpl
&)surfacePattern
).bitmap
);
305 else // Something is wrong so display in red
306 br
= wxBrush(*wxRED
, wxSOLID
);
307 hdc
->SetPen(*wxTRANSPARENT_PEN
);
309 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
312 void SurfaceImpl::RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
315 hdc
->DrawRoundedRectangle(wxRectFromPRectangle(rc
), 4);
318 void SurfaceImpl::Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
321 hdc
->DrawEllipse(wxRectFromPRectangle(rc
));
324 void SurfaceImpl::Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
) {
325 wxRect r
= wxRectFromPRectangle(rc
);
326 hdc
->Blit(r
.x
, r
.y
, r
.width
, r
.height
,
327 ((SurfaceImpl
&)surfaceSource
).hdc
,
328 from
.x
, from
.y
, wxCOPY
);
331 void SurfaceImpl::DrawTextNoClip(PRectangle rc
, Font
&font
, int ybase
,
332 const char *s
, int len
,
333 ColourAllocated fore
, ColourAllocated back
) {
335 hdc
->SetTextForeground(wxColourFromCA(fore
));
336 hdc
->SetTextBackground(wxColourFromCA(back
));
337 FillRectangle(rc
, back
);
339 // ybase is where the baseline should be, but wxWin uses the upper left
340 // corner, so I need to calculate the real position for the text...
341 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
344 void SurfaceImpl::DrawTextClipped(PRectangle rc
, Font
&font
, int ybase
,
345 const char *s
, int len
,
346 ColourAllocated fore
, ColourAllocated back
) {
348 hdc
->SetTextForeground(wxColourFromCA(fore
));
349 hdc
->SetTextBackground(wxColourFromCA(back
));
350 FillRectangle(rc
, back
);
351 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
353 // see comments above
354 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
355 hdc
->DestroyClippingRegion();
359 void SurfaceImpl::DrawTextTransparent(PRectangle rc
, Font
&font
, int ybase
,
360 const char *s
, int len
,
361 ColourAllocated fore
) {
364 hdc
->SetTextForeground(wxColourFromCA(fore
));
365 hdc
->SetBackgroundMode(wxTRANSPARENT
);
367 // ybase is where the baseline should be, but wxWin uses the upper left
368 // corner, so I need to calculate the real position for the text...
369 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
371 hdc
->SetBackgroundMode(wxSOLID
);
375 void SurfaceImpl::MeasureWidths(Font
&font
, const char *s
, int len
, int *positions
) {
377 wxString str
= stc2wx(s
, len
);
382 hdc
->GetPartialTextExtents(str
, tpos
);
385 // Map the widths for UCS-2 characters back to the UTF-8 input string
386 // NOTE: I don't think this is right for when sizeof(wxChar) > 2, ie wxGTK2
387 // so figure it out and fix it!
390 while ((int)i
< len
) {
391 unsigned char uch
= (unsigned char)s
[i
];
392 positions
[i
++] = tpos
[ui
];
394 if (uch
< (0x80 + 0x40 + 0x20)) {
395 positions
[i
++] = tpos
[ui
];
397 positions
[i
++] = tpos
[ui
];
398 positions
[i
++] = tpos
[ui
];
405 // If not unicode then just use the widths we have
407 std::copy(tpos
.begin(), tpos
.end(), positions
);
409 memcpy(positions
, tpos
.begin(), len
* sizeof(int));
415 int SurfaceImpl::WidthText(Font
&font
, const char *s
, int len
) {
420 hdc
->GetTextExtent(stc2wx(s
, len
), &w
, &h
);
425 int SurfaceImpl::WidthChar(Font
&font
, char ch
) {
429 char s
[2] = { ch
, 0 };
431 hdc
->GetTextExtent(stc2wx(s
, 1), &w
, &h
);
435 #define EXTENT_TEST wxT(" `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
437 int SurfaceImpl::Ascent(Font
&font
) {
440 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
445 int SurfaceImpl::Descent(Font
&font
) {
448 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
452 int SurfaceImpl::InternalLeading(Font
&WXUNUSED(font
)) {
456 int SurfaceImpl::ExternalLeading(Font
&font
) {
459 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
463 int SurfaceImpl::Height(Font
&font
) {
465 return hdc
->GetCharHeight() + 1;
468 int SurfaceImpl::AverageCharWidth(Font
&font
) {
470 return hdc
->GetCharWidth();
473 int SurfaceImpl::SetPalette(Palette
*WXUNUSED(pal
), bool WXUNUSED(inBackGround
)) {
477 void SurfaceImpl::SetClip(PRectangle rc
) {
478 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
481 void SurfaceImpl::FlushCachedState() {
484 void SurfaceImpl::SetUnicodeMode(bool unicodeMode_
) {
485 unicodeMode
=unicodeMode_
;
488 void SurfaceImpl::SetDBCSMode(int WXUNUSED(codePage
)) {
489 // dbcsMode = codePage == SC_CP_DBCS;
493 Surface
*Surface::Allocate() {
494 return new SurfaceImpl
;
498 //----------------------------------------------------------------------
501 inline wxWindow
* GETWIN(WindowID id
) { return (wxWindow
*)id
; }
506 void Window::Destroy() {
509 GETWIN(id
)->Destroy();
514 bool Window::HasFocus() {
515 return wxWindow::FindFocus() == GETWIN(id
);
518 PRectangle
Window::GetPosition() {
519 if (! id
) return PRectangle();
520 wxRect
rc(GETWIN(id
)->GetPosition(), GETWIN(id
)->GetSize());
521 return PRectangleFromwxRect(rc
);
524 void Window::SetPosition(PRectangle rc
) {
525 wxRect r
= wxRectFromPRectangle(rc
);
526 GETWIN(id
)->SetSize(r
);
529 void Window::SetPositionRelative(PRectangle rc
, Window
) {
530 SetPosition(rc
); // ????
533 PRectangle
Window::GetClientPosition() {
534 if (! id
) return PRectangle();
535 wxSize sz
= GETWIN(id
)->GetClientSize();
536 return PRectangle(0, 0, sz
.x
, sz
.y
);
539 void Window::Show(bool show
) {
540 GETWIN(id
)->Show(show
);
543 void Window::InvalidateAll() {
544 GETWIN(id
)->Refresh(false);
548 void Window::InvalidateRectangle(PRectangle rc
) {
549 wxRect r
= wxRectFromPRectangle(rc
);
550 GETWIN(id
)->Refresh(false, &r
);
554 void Window::SetFont(Font
&font
) {
555 GETWIN(id
)->SetFont(*((wxFont
*)font
.GetID()));
558 void Window::SetCursor(Cursor curs
) {
563 cursorId
= wxCURSOR_IBEAM
;
566 cursorId
= wxCURSOR_ARROW
;
569 cursorId
= wxCURSOR_ARROW
; // ** no up arrow... wxCURSOR_UPARROW;
572 cursorId
= wxCURSOR_WAIT
;
575 cursorId
= wxCURSOR_SIZEWE
;
578 cursorId
= wxCURSOR_SIZENS
;
580 case cursorReverseArrow
:
581 cursorId
= wxCURSOR_RIGHT_ARROW
;
584 cursorId
= wxCURSOR_HAND
;
587 cursorId
= wxCURSOR_ARROW
;
591 wxCursor wc
= wxStockCursor(cursorId
) ;
593 wxCursor wc
= wxCursor(cursorId
) ;
595 if(curs
!= cursorLast
)
597 GETWIN(id
)->SetCursor(wc
);
603 void Window::SetTitle(const char *s
) {
604 GETWIN(id
)->SetTitle(stc2wx(s
));
608 //----------------------------------------------------------------------
609 // Helper classes for ListBox
612 // This is a simple subclass of wxListView that just resets focus to the
613 // parent when it gets it.
614 class wxSTCListBox
: public wxListView
{
616 wxSTCListBox(wxWindow
* parent
, wxWindowID id
,
617 const wxPoint
& pos
, const wxSize
& size
,
622 Hide(); // don't flicker as we move it around...
624 Create(parent
, id
, pos
, size
, style
);
628 void OnFocus(wxFocusEvent
& event
) {
629 GetParent()->SetFocus();
633 void OnKillFocus(wxFocusEvent
& WXUNUSED(event
)) {
634 // Do nothing. Prevents base class from resetting the colors...
638 // For some reason I don't understand yet the focus doesn't really leave
639 // the listbox like it should, so if we get any events feed them back to
641 void OnKeyDown(wxKeyEvent
& event
) {
642 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
644 void OnChar(wxKeyEvent
& event
) {
645 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
648 // And we need to force the focus back when being destroyed
650 GetGrandParent()->SetFocus();
655 DECLARE_EVENT_TABLE()
658 BEGIN_EVENT_TABLE(wxSTCListBox
, wxListView
)
659 EVT_SET_FOCUS( wxSTCListBox::OnFocus
)
660 EVT_KILL_FOCUS(wxSTCListBox::OnKillFocus
)
662 EVT_KEY_DOWN( wxSTCListBox::OnKeyDown
)
663 EVT_CHAR( wxSTCListBox::OnChar
)
669 #if wxUSE_POPUPWIN //-----------------------------------
670 #include <wx/popupwin.h>
674 // TODO: Refactor these two classes to have a common base (or a mix-in) to get
675 // rid of the code duplication. (Either that or convince somebody to
676 // implement wxPopupWindow for the Mac!!)
678 // In the meantime, be careful to duplicate any changes as needed...
681 // A popup window to place the wxSTCListBox upon
682 class wxSTCListBoxWin
: public wxPopupWindow
686 CallBackAction doubleClickAction
;
687 void* doubleClickActionData
;
689 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
) :
690 wxPopupWindow(parent
, wxBORDER_NONE
)
692 SetBackgroundColour(*wxBLACK
); // for our simple border
694 lv
= new wxSTCListBox(parent
, id
, wxDefaultPosition
, wxDefaultSize
,
695 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxBORDER_NONE
);
696 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
697 lv
->InsertColumn(0, wxEmptyString
);
698 lv
->InsertColumn(1, wxEmptyString
);
700 // NOTE: We need to fool the wxListView into thinking that it has the
701 // focus so it will use the normal selection colour and will look
702 // "right" to the user. But since the wxPopupWindow or its children
703 // can't receive focus then we have to pull a fast one and temporarily
704 // parent the listctrl on the STC window and then call SetFocus and
705 // then reparent it back to the popup.
714 // Set position in client coords
715 virtual void DoSetSize(int x
, int y
,
716 int width
, int height
,
717 int sizeFlags
= wxSIZE_AUTO
) {
718 if (x
!= wxDefaultCoord
) {
719 GetParent()->ClientToScreen(&x
, NULL
);
721 if (y
!= wxDefaultCoord
) {
722 GetParent()->ClientToScreen(NULL
, &y
);
724 wxPopupWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
727 // return position as if it were in client coords
728 virtual void DoGetPosition( int *x
, int *y
) const {
730 wxPopupWindow::DoGetPosition(&sx
, &sy
);
731 GetParent()->ScreenToClient(&sx
, &sy
);
738 if ( !wxPendingDelete
.Member(this) )
739 wxPendingDelete
.Append(this);
745 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
748 il
->GetSize(0, w
, h
);
755 void SetDoubleClickAction(CallBackAction action
, void *data
) {
756 doubleClickAction
= action
;
757 doubleClickActionData
= data
;
761 void OnFocus(wxFocusEvent
& event
) {
762 GetParent()->SetFocus();
766 void OnSize(wxSizeEvent
& event
) {
768 wxSize sz
= GetSize();
771 lv
->SetSize(1, 1, sz
.x
, sz
.y
);
772 // reset the column widths
773 lv
->SetColumnWidth(0, IconWidth()+4);
774 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
775 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
779 void OnActivate(wxListEvent
& WXUNUSED(event
)) {
780 doubleClickAction(doubleClickActionData
);
783 wxListView
* GetLB() { return lv
; }
786 DECLARE_EVENT_TABLE()
790 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxPopupWindow
)
791 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
792 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
793 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
798 #else // wxUSE_POPUPWIN -----------------------------------
800 // A normal window to place the wxSTCListBox upon.
801 class wxSTCListBoxWin
: public wxWindow
{
804 CallBackAction doubleClickAction
;
805 void* doubleClickActionData
;
807 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
) :
808 wxWindow(parent
, id
, wxDefaultPosition
, wxSize(0,0), wxSIMPLE_BORDER
)
811 lv
= new wxSTCListBox(this, id
, wxDefaultPosition
, wxDefaultSize
,
812 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxNO_BORDER
);
813 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
814 lv
->InsertColumn(0, wxEmptyString
);
815 lv
->InsertColumn(1, wxEmptyString
);
817 // Eventhough we immediately reset the focus to the parent, this helps
818 // things to look right...
825 // On OSX and (possibly others) there can still be pending
826 // messages/events for the list control when Scintilla wants to
827 // close it, so do a pending delete of it instead of destroying
831 // The bottom edge of this window is not getting properly
832 // refreshed upon deletion, so help it out...
833 wxWindow
* p
= GetParent();
834 wxRect
r(GetPosition(), GetSize());
835 r
.SetHeight(r
.GetHeight()+1);
836 p
->Refresh(false, &r
);
838 if ( !wxPendingDelete
.Member(this) )
839 wxPendingDelete
.Append(this);
845 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
848 il
->GetSize(0, w
, h
);
855 void SetDoubleClickAction(CallBackAction action
, void *data
) {
856 doubleClickAction
= action
;
857 doubleClickActionData
= data
;
861 void OnFocus(wxFocusEvent
& event
) {
862 GetParent()->SetFocus();
866 void OnSize(wxSizeEvent
& event
) {
868 wxSize sz
= GetClientSize();
870 // reset the column widths
871 lv
->SetColumnWidth(0, IconWidth()+4);
872 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
873 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
878 virtual bool Show(bool show
= true) {
879 bool rv
= wxWindow::Show(show
);
880 GetParent()->Refresh(false);
885 void OnActivate(wxListEvent
& WXUNUSED(event
)) {
886 doubleClickAction(doubleClickActionData
);
889 wxListView
* GetLB() { return lv
; }
892 DECLARE_EVENT_TABLE()
896 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxWindow
)
897 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
898 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
899 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
902 #endif // wxUSE_POPUPWIN -----------------------------------
905 inline wxSTCListBoxWin
* GETLBW(WindowID win
) {
906 return ((wxSTCListBoxWin
*)win
);
909 inline wxListView
* GETLB(WindowID win
) {
910 return GETLBW(win
)->GetLB();
913 //----------------------------------------------------------------------
915 class ListBoxImpl
: public ListBox
{
919 int desiredVisibleRows
;
922 wxImageList
* imgList
;
923 wxArrayInt
* imgTypeMap
;
929 virtual void SetFont(Font
&font
);
930 virtual void Create(Window
&parent
, int ctrlID
, int lineHeight_
, bool unicodeMode_
);
931 virtual void SetAverageCharWidth(int width
);
932 virtual void SetVisibleRows(int rows
);
933 virtual PRectangle
GetDesiredRect();
934 virtual int CaretFromEdge();
935 virtual void Clear();
936 virtual void Append(char *s
, int type
= -1);
937 virtual int Length();
938 virtual void Select(int n
);
939 virtual int GetSelection();
940 virtual int Find(const char *prefix
);
941 virtual void GetValue(int n
, char *value
, int len
);
942 virtual void RegisterImage(int type
, const char *xpm_data
);
943 virtual void ClearRegisteredImages();
944 virtual void SetDoubleClickAction(CallBackAction
, void *);
949 ListBoxImpl::ListBoxImpl()
950 : lineHeight(10), unicodeMode(false),
951 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
952 imgList(NULL
), imgTypeMap(NULL
)
956 ListBoxImpl::~ListBoxImpl() {
968 void ListBoxImpl::SetFont(Font
&font
) {
969 GETLB(id
)->SetFont(*((wxFont
*)font
.GetID()));
973 void ListBoxImpl::Create(Window
&parent
, int ctrlID
, int lineHeight_
, bool unicodeMode_
) {
974 lineHeight
= lineHeight_
;
975 unicodeMode
= unicodeMode_
;
977 id
= new wxSTCListBoxWin(GETWIN(parent
.GetID()), ctrlID
);
979 GETLB(id
)->SetImageList(imgList
, wxIMAGE_LIST_SMALL
);
983 void ListBoxImpl::SetAverageCharWidth(int width
) {
984 aveCharWidth
= width
;
988 void ListBoxImpl::SetVisibleRows(int rows
) {
989 desiredVisibleRows
= rows
;
993 PRectangle
ListBoxImpl::GetDesiredRect() {
994 // wxListCtrl doesn't have a DoGetBestSize, so instead we kept track of
995 // the max size in Append and calculate it here...
996 int maxw
= maxStrWidth
;
999 // give it a default if there are no lines, and/or add a bit more
1000 if (maxw
== 0) maxw
= 100;
1001 maxw
+= aveCharWidth
* 3 +
1002 GETLBW(id
)->IconWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1006 // estimate a desired height
1007 int count
= GETLB(id
)->GetItemCount();
1010 GETLB(id
)->GetItemRect(0, rect
);
1011 maxh
= count
* rect
.GetHeight();
1012 if (maxh
> 140) // TODO: Use desiredVisibleRows??
1015 // Try to make the size an exact multiple of some number of lines
1016 int lines
= maxh
/ rect
.GetHeight();
1017 maxh
= (lines
+ 1) * rect
.GetHeight() + 2;
1031 int ListBoxImpl::CaretFromEdge() {
1032 return 4 + GETLBW(id
)->IconWidth();
1036 void ListBoxImpl::Clear() {
1037 GETLB(id
)->DeleteAllItems();
1041 void ListBoxImpl::Append(char *s
, int type
) {
1042 wxString text
= stc2wx(s
);
1043 long count
= GETLB(id
)->GetItemCount();
1044 long itemID
= GETLB(id
)->InsertItem(count
, wxEmptyString
);
1045 GETLB(id
)->SetItem(itemID
, 1, text
);
1047 GETLB(id
)->GetTextExtent(text
, &itemWidth
, NULL
);
1048 maxStrWidth
= wxMax(maxStrWidth
, itemWidth
);
1050 wxCHECK_RET(imgTypeMap
, wxT("Unexpected NULL imgTypeMap"));
1051 long idx
= imgTypeMap
->Item(type
);
1052 GETLB(id
)->SetItemImage(itemID
, idx
, idx
);
1057 int ListBoxImpl::Length() {
1058 return GETLB(id
)->GetItemCount();
1062 void ListBoxImpl::Select(int n
) {
1068 GETLB(id
)->Focus(n
);
1069 GETLB(id
)->Select(n
, select
);
1073 int ListBoxImpl::GetSelection() {
1074 return GETLB(id
)->GetFirstSelected();
1078 int ListBoxImpl::Find(const char *WXUNUSED(prefix
)) {
1084 void ListBoxImpl::GetValue(int n
, char *value
, int len
) {
1088 item
.SetMask(wxLIST_MASK_TEXT
);
1089 GETLB(id
)->GetItem(item
);
1090 strncpy(value
, wx2stc(item
.GetText()), len
);
1091 value
[len
-1] = '\0';
1095 void ListBoxImpl::RegisterImage(int type
, const char *xpm_data
) {
1096 wxMemoryInputStream
stream(xpm_data
, strlen(xpm_data
)+1);
1097 wxImage
img(stream
, wxBITMAP_TYPE_XPM
);
1101 // assumes all images are the same size
1102 imgList
= new wxImageList(bmp
.GetWidth(), bmp
.GetHeight(), true);
1103 imgTypeMap
= new wxArrayInt
;
1106 int idx
= imgList
->Add(bmp
);
1108 // do we need to extend the mapping array?
1109 wxArrayInt
& itm
= *imgTypeMap
;
1110 if ( itm
.GetCount() < (size_t)type
+1)
1111 itm
.Add(-1, type
- itm
.GetCount() + 1);
1113 // Add an item that maps type to the image index
1117 void ListBoxImpl::ClearRegisteredImages() {
1127 GETLB(id
)->SetImageList(NULL
, wxIMAGE_LIST_SMALL
);
1131 void ListBoxImpl::SetDoubleClickAction(CallBackAction action
, void *data
) {
1132 GETLBW(id
)->SetDoubleClickAction(action
, data
);
1137 ListBox::ListBox() {
1140 ListBox::~ListBox() {
1143 ListBox
*ListBox::Allocate() {
1144 return new ListBoxImpl();
1147 //----------------------------------------------------------------------
1149 Menu::Menu() : id(0) {
1152 void Menu::CreatePopUp() {
1157 void Menu::Destroy() {
1163 void Menu::Show(Point pt
, Window
&w
) {
1164 GETWIN(w
.GetID())->PopupMenu((wxMenu
*)id
, pt
.x
- 4, pt
.y
);
1168 //----------------------------------------------------------------------
1170 DynamicLibrary
*DynamicLibrary::Load(const char *WXUNUSED(modulePath
)) {
1171 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1175 //----------------------------------------------------------------------
1177 ColourDesired
Platform::Chrome() {
1179 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1180 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1183 ColourDesired
Platform::ChromeHighlight() {
1185 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
);
1186 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1189 const char *Platform::DefaultFont() {
1190 static char buf
[128];
1191 strcpy(buf
, wxNORMAL_FONT
->GetFaceName().mbc_str());
1195 int Platform::DefaultFontSize() {
1196 return wxNORMAL_FONT
->GetPointSize();
1199 unsigned int Platform::DoubleClickTime() {
1200 return 500; // **** ::GetDoubleClickTime();
1203 bool Platform::MouseButtonBounce() {
1206 void Platform::DebugDisplay(const char *s
) {
1207 wxLogDebug(stc2wx(s
));
1210 bool Platform::IsKeyDown(int WXUNUSED(key
)) {
1211 return false; // I don't think we'll need this.
1214 long Platform::SendScintilla(WindowID w
,
1216 unsigned long wParam
,
1219 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1220 return stc
->SendMsg(msg
, wParam
, lParam
);
1223 long Platform::SendScintillaPointer(WindowID w
,
1225 unsigned long wParam
,
1228 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1229 return stc
->SendMsg(msg
, wParam
, (long)lParam
);
1233 // These are utility functions not really tied to a platform
1235 int Platform::Minimum(int a
, int b
) {
1242 int Platform::Maximum(int a
, int b
) {
1251 void Platform::DebugPrintf(const char *format
, ...) {
1255 va_start(pArguments
, format
);
1256 vsprintf(buffer
,format
,pArguments
);
1258 Platform::DebugDisplay(buffer
);
1263 static bool assertionPopUps
= true;
1265 bool Platform::ShowAssertionPopUps(bool assertionPopUps_
) {
1266 bool ret
= assertionPopUps
;
1267 assertionPopUps
= assertionPopUps_
;
1271 void Platform::Assert(const char *c
, const char *file
, int line
) {
1273 sprintf(buffer
, "Assertion [%s] failed at %s %d", c
, file
, line
);
1274 if (assertionPopUps
) {
1276 wxMessageBox(stc2wx(buffer
),
1277 wxT("Assertion failure"),
1278 wxICON_HAND
| wxOK
);
1279 // if (idButton == IDRETRY) {
1281 // } else if (idButton == IDIGNORE) {
1287 strcat(buffer
, "\r\n");
1288 Platform::DebugDisplay(buffer
);
1294 int Platform::Clamp(int val
, int minVal
, int maxVal
) {
1303 bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage
), char WXUNUSED(ch
)) {
1307 int Platform::DBCSCharLength(int WXUNUSED(codePage
), const char *WXUNUSED(s
)) {
1311 int Platform::DBCSCharMaxLength() {
1316 //----------------------------------------------------------------------
1318 ElapsedTime::ElapsedTime() {
1322 double ElapsedTime::Duration(bool reset
) {
1323 double result
= wxGetElapsedTime(reset
);
1329 //----------------------------------------------------------------------
1333 #include "UniConversion.h"
1335 // Convert using Scintilla's functions instead of wx's, Scintilla's are more
1336 // forgiving and won't assert...
1338 wxString
stc2wx(const char* str
, size_t len
)
1341 return wxEmptyString
;
1343 size_t wclen
= UCS2Length(str
, len
);
1344 wxWCharBuffer
buffer(wclen
+1);
1346 size_t actualLen
= UCS2FromUTF8(str
, len
, buffer
.data(), wclen
+1);
1347 return wxString(buffer
.data(), actualLen
);
1352 wxString
stc2wx(const char* str
)
1354 return stc2wx(str
, strlen(str
));
1358 const wxWX2MBbuf
wx2stc(const wxString
& str
)
1360 const wchar_t* wcstr
= str
.c_str();
1361 size_t wclen
= str
.length();
1362 size_t len
= UTF8Length(wcstr
, wclen
);
1364 wxCharBuffer
buffer(len
+1);
1365 UTF8FromUCS2(wcstr
, wclen
, buffer
.data(), len
);
1367 // TODO check NULL termination!!