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"
15 #include "wx/tokenzr.h"
16 #include "wx/rawbmp.h"
20 #include "wx/stc/stc.h"
24 Point
Point::FromLong(long lpoint
) {
25 return Point(lpoint
& 0xFFFF, lpoint
>> 16);
28 wxRect
wxRectFromPRectangle(PRectangle prc
) {
29 wxRect
r(prc
.left
, prc
.top
,
30 prc
.Width(), prc
.Height());
34 PRectangle
PRectangleFromwxRect(wxRect rc
) {
35 return PRectangle(rc
.GetLeft(), rc
.GetTop(),
36 rc
.GetRight()+1, rc
.GetBottom()+1);
39 wxColour
wxColourFromCA(const ColourAllocated
& ca
) {
40 ColourDesired
cd(ca
.AsLong());
41 return wxColour((unsigned char)cd
.GetRed(),
42 (unsigned char)cd
.GetGreen(),
43 (unsigned char)cd
.GetBlue());
46 //----------------------------------------------------------------------
50 allowRealization
= false;
52 entries
= new ColourPair
[size
];
61 void Palette::Release() {
65 entries
= new ColourPair
[size
];
68 // This method either adds a colour to the list of wanted colours (want==true)
69 // or retrieves the allocated colour back to the ColourPair.
70 // This is one method to make it easier to keep the code for wanting and retrieving in sync.
71 void Palette::WantFind(ColourPair
&cp
, bool want
) {
73 for (int i
=0; i
< used
; i
++) {
74 if (entries
[i
].desired
== cp
.desired
)
79 int sizeNew
= size
* 2;
80 ColourPair
*entriesNew
= new ColourPair
[sizeNew
];
81 for (int j
=0; j
<size
; j
++) {
82 entriesNew
[j
] = entries
[j
];
89 entries
[used
].desired
= cp
.desired
;
90 entries
[used
].allocated
.Set(cp
.desired
.AsLong());
93 for (int i
=0; i
< used
; i
++) {
94 if (entries
[i
].desired
== cp
.desired
) {
95 cp
.allocated
= entries
[i
].allocated
;
99 cp
.allocated
.Set(cp
.desired
.AsLong());
103 void Palette::Allocate(Window
&) {
104 if (allowRealization
) {
109 //----------------------------------------------------------------------
119 void Font::Create(const char *faceName
, int characterSet
, int size
, bool bold
, bool italic
, bool extraFontFlag
) {
123 // The minus one is done because since Scintilla uses SC_SHARSET_DEFAULT
124 // internally and we need to have wxFONENCODING_DEFAULT == SC_SHARSET_DEFAULT
125 // so we adjust the encoding before passing it to Scintilla. See also
126 // wxStyledTextCtrl::StyleSetCharacterSet
127 wxFontEncoding encoding
= (wxFontEncoding
)(characterSet
-1);
129 wxFontEncodingArray ea
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
133 wxFont
* font
= new wxFont(size
,
135 italic
? wxITALIC
: wxNORMAL
,
136 bold
? wxBOLD
: wxNORMAL
,
140 font
->SetNoAntiAliasing(!extraFontFlag
);
145 void Font::Release() {
151 //----------------------------------------------------------------------
153 class SurfaceImpl
: public Surface
{
166 virtual void Init(WindowID wid
);
167 virtual void Init(SurfaceID sid
, WindowID wid
);
168 virtual void InitPixMap(int width
, int height
, Surface
*surface_
, WindowID wid
);
170 virtual void Release();
171 virtual bool Initialised();
172 virtual void PenColour(ColourAllocated fore
);
173 virtual int LogPixelsY();
174 virtual int DeviceHeightFont(int points
);
175 virtual void MoveTo(int x_
, int y_
);
176 virtual void LineTo(int x_
, int y_
);
177 virtual void Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
);
178 virtual void RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
179 virtual void FillRectangle(PRectangle rc
, ColourAllocated back
);
180 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
);
181 virtual void RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
182 virtual void AlphaRectangle(PRectangle rc
, int cornerSize
, ColourAllocated fill
, int alphaFill
,
183 ColourAllocated outline
, int alphaOutline
, int flags
);
184 virtual void Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
185 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
);
187 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
188 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
189 virtual void DrawTextTransparent(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
);
190 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, int *positions
);
191 virtual int WidthText(Font
&font_
, const char *s
, int len
);
192 virtual int WidthChar(Font
&font_
, char ch
);
193 virtual int Ascent(Font
&font_
);
194 virtual int Descent(Font
&font_
);
195 virtual int InternalLeading(Font
&font_
);
196 virtual int ExternalLeading(Font
&font_
);
197 virtual int Height(Font
&font_
);
198 virtual int AverageCharWidth(Font
&font_
);
200 virtual int SetPalette(Palette
*pal
, bool inBackGround
);
201 virtual void SetClip(PRectangle rc
);
202 virtual void FlushCachedState();
204 virtual void SetUnicodeMode(bool unicodeMode_
);
205 virtual void SetDBCSMode(int codePage
);
207 void BrushColour(ColourAllocated back
);
208 void SetFont(Font
&font_
);
213 SurfaceImpl::SurfaceImpl() :
214 hdc(0), hdcOwned(0), bitmap(0),
215 x(0), y(0), unicodeMode(0)
218 SurfaceImpl::~SurfaceImpl() {
222 void SurfaceImpl::Init(WindowID wid
) {
225 hdc
= new wxMemoryDC();
228 // On Mac and GTK the DC is not really valid until it has a bitmap
229 // selected into it. So instead of just creating the DC with no bitmap,
230 // go ahead and give it one.
231 InitPixMap(1,1,NULL
,wid
);
235 void SurfaceImpl::Init(SurfaceID hdc_
, WindowID
) {
240 void SurfaceImpl::InitPixMap(int width
, int height
, Surface
*WXUNUSED(surface_
), WindowID
) {
242 hdc
= new wxMemoryDC();
244 if (width
< 1) width
= 1;
245 if (height
< 1) height
= 1;
246 bitmap
= new wxBitmap(width
, height
);
247 ((wxMemoryDC
*)hdc
)->SelectObject(*bitmap
);
251 void SurfaceImpl::Release() {
253 ((wxMemoryDC
*)hdc
)->SelectObject(wxNullBitmap
);
265 bool SurfaceImpl::Initialised() {
270 void SurfaceImpl::PenColour(ColourAllocated fore
) {
271 hdc
->SetPen(wxPen(wxColourFromCA(fore
), 1, wxSOLID
));
274 void SurfaceImpl::BrushColour(ColourAllocated back
) {
275 hdc
->SetBrush(wxBrush(wxColourFromCA(back
), wxSOLID
));
278 void SurfaceImpl::SetFont(Font
&font_
) {
280 hdc
->SetFont(*((wxFont
*)font_
.GetID()));
284 int SurfaceImpl::LogPixelsY() {
285 return hdc
->GetPPI().y
;
288 int SurfaceImpl::DeviceHeightFont(int points
) {
292 void SurfaceImpl::MoveTo(int x_
, int y_
) {
297 void SurfaceImpl::LineTo(int x_
, int y_
) {
298 hdc
->DrawLine(x
,y
, x_
,y_
);
303 void SurfaceImpl::Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
) {
306 hdc
->DrawPolygon(npts
, (wxPoint
*)pts
);
309 void SurfaceImpl::RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
312 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
315 void SurfaceImpl::FillRectangle(PRectangle rc
, ColourAllocated back
) {
317 hdc
->SetPen(*wxTRANSPARENT_PEN
);
318 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
321 void SurfaceImpl::FillRectangle(PRectangle rc
, Surface
&surfacePattern
) {
323 if (((SurfaceImpl
&)surfacePattern
).bitmap
)
324 br
= wxBrush(*((SurfaceImpl
&)surfacePattern
).bitmap
);
325 else // Something is wrong so display in red
326 br
= wxBrush(*wxRED
, wxSOLID
);
327 hdc
->SetPen(*wxTRANSPARENT_PEN
);
329 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
332 void SurfaceImpl::RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
335 hdc
->DrawRoundedRectangle(wxRectFromPRectangle(rc
), 4);
339 #define wxPy_premultiply(p, a) ((p) * (a) / 0xff)
341 #define wxPy_premultiply(p, a) (p)
344 void SurfaceImpl::AlphaRectangle(PRectangle rc
, int cornerSize
,
345 ColourAllocated fill
, int alphaFill
,
346 ColourAllocated outline
, int alphaOutline
,
349 wxRect r
= wxRectFromPRectangle(rc
);
350 wxBitmap
bmp(r
.width
, r
.height
, 32);
351 wxAlphaPixelData
pixData(bmp
);
354 // Set the fill pixels
355 ColourDesired
cdf(fill
.AsLong());
356 int red
= cdf
.GetRed();
357 int green
= cdf
.GetGreen();
358 int blue
= cdf
.GetBlue();
360 wxAlphaPixelData::Iterator
p(pixData
);
361 for (y
=0; y
<r
.height
; y
++) {
362 p
.MoveTo(pixData
, 0, y
);
363 for (x
=0; x
<r
.width
; x
++) {
364 p
.Red() = wxPy_premultiply(red
, alphaFill
);
365 p
.Green() = wxPy_premultiply(green
, alphaFill
);
366 p
.Blue() = wxPy_premultiply(blue
, alphaFill
);
367 p
.Alpha() = alphaFill
;
372 // Set the outline pixels
373 ColourDesired
cdo(outline
.AsLong());
375 green
= cdo
.GetGreen();
376 blue
= cdo
.GetBlue();
377 for (x
=0; x
<r
.width
; x
++) {
378 p
.MoveTo(pixData
, x
, 0);
379 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
380 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
381 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
382 p
.Alpha() = alphaOutline
;
383 p
.MoveTo(pixData
, x
, r
.height
-1);
384 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
385 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
386 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
387 p
.Alpha() = alphaOutline
;
390 for (y
=0; y
<r
.height
; y
++) {
391 p
.MoveTo(pixData
, 0, y
);
392 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
393 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
394 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
395 p
.Alpha() = alphaOutline
;
396 p
.MoveTo(pixData
, r
.width
-1, y
);
397 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
398 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
399 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
400 p
.Alpha() = alphaOutline
;
404 hdc
->DrawBitmap(bmp
, r
.x
, r
.y
, true);
407 void SurfaceImpl::Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
410 hdc
->DrawEllipse(wxRectFromPRectangle(rc
));
413 void SurfaceImpl::Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
) {
414 wxRect r
= wxRectFromPRectangle(rc
);
415 hdc
->Blit(r
.x
, r
.y
, r
.width
, r
.height
,
416 ((SurfaceImpl
&)surfaceSource
).hdc
,
417 from
.x
, from
.y
, wxCOPY
);
420 void SurfaceImpl::DrawTextNoClip(PRectangle rc
, Font
&font
, int ybase
,
421 const char *s
, int len
,
422 ColourAllocated fore
, ColourAllocated back
) {
424 hdc
->SetTextForeground(wxColourFromCA(fore
));
425 hdc
->SetTextBackground(wxColourFromCA(back
));
426 FillRectangle(rc
, back
);
428 // ybase is where the baseline should be, but wxWin uses the upper left
429 // corner, so I need to calculate the real position for the text...
430 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
433 void SurfaceImpl::DrawTextClipped(PRectangle rc
, Font
&font
, int ybase
,
434 const char *s
, int len
,
435 ColourAllocated fore
, ColourAllocated back
) {
437 hdc
->SetTextForeground(wxColourFromCA(fore
));
438 hdc
->SetTextBackground(wxColourFromCA(back
));
439 FillRectangle(rc
, back
);
440 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
442 // see comments above
443 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
444 hdc
->DestroyClippingRegion();
448 void SurfaceImpl::DrawTextTransparent(PRectangle rc
, Font
&font
, int ybase
,
449 const char *s
, int len
,
450 ColourAllocated fore
) {
453 hdc
->SetTextForeground(wxColourFromCA(fore
));
454 hdc
->SetBackgroundMode(wxTRANSPARENT
);
456 // ybase is where the baseline should be, but wxWin uses the upper left
457 // corner, so I need to calculate the real position for the text...
458 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
460 hdc
->SetBackgroundMode(wxSOLID
);
464 void SurfaceImpl::MeasureWidths(Font
&font
, const char *s
, int len
, int *positions
) {
466 wxString str
= stc2wx(s
, len
);
471 hdc
->GetPartialTextExtents(str
, tpos
);
474 // Map the widths for UCS-2 characters back to the UTF-8 input string
475 // NOTE: I don't think this is right for when sizeof(wxChar) > 2, ie wxGTK2
476 // so figure it out and fix it!
479 while ((int)i
< len
) {
480 unsigned char uch
= (unsigned char)s
[i
];
481 positions
[i
++] = tpos
[ui
];
483 if (uch
< (0x80 + 0x40 + 0x20)) {
484 positions
[i
++] = tpos
[ui
];
486 positions
[i
++] = tpos
[ui
];
487 positions
[i
++] = tpos
[ui
];
494 // If not unicode then just use the widths we have
496 std::copy(tpos
.begin(), tpos
.end(), positions
);
498 memcpy(positions
, tpos
.begin(), len
* sizeof(int));
504 int SurfaceImpl::WidthText(Font
&font
, const char *s
, int len
) {
509 hdc
->GetTextExtent(stc2wx(s
, len
), &w
, &h
);
514 int SurfaceImpl::WidthChar(Font
&font
, char ch
) {
518 char s
[2] = { ch
, 0 };
520 hdc
->GetTextExtent(stc2wx(s
, 1), &w
, &h
);
524 #define EXTENT_TEST wxT(" `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
526 int SurfaceImpl::Ascent(Font
&font
) {
529 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
534 int SurfaceImpl::Descent(Font
&font
) {
537 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
541 int SurfaceImpl::InternalLeading(Font
&WXUNUSED(font
)) {
545 int SurfaceImpl::ExternalLeading(Font
&font
) {
548 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
552 int SurfaceImpl::Height(Font
&font
) {
554 return hdc
->GetCharHeight() + 1;
557 int SurfaceImpl::AverageCharWidth(Font
&font
) {
559 return hdc
->GetCharWidth();
562 int SurfaceImpl::SetPalette(Palette
*WXUNUSED(pal
), bool WXUNUSED(inBackGround
)) {
566 void SurfaceImpl::SetClip(PRectangle rc
) {
567 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
570 void SurfaceImpl::FlushCachedState() {
573 void SurfaceImpl::SetUnicodeMode(bool unicodeMode_
) {
574 unicodeMode
=unicodeMode_
;
577 void SurfaceImpl::SetDBCSMode(int WXUNUSED(codePage
)) {
578 // dbcsMode = codePage == SC_CP_DBCS;
582 Surface
*Surface::Allocate() {
583 return new SurfaceImpl
;
587 //----------------------------------------------------------------------
590 inline wxWindow
* GETWIN(WindowID id
) { return (wxWindow
*)id
; }
595 void Window::Destroy() {
598 GETWIN(id
)->Destroy();
603 bool Window::HasFocus() {
604 return wxWindow::FindFocus() == GETWIN(id
);
607 PRectangle
Window::GetPosition() {
608 if (! id
) return PRectangle();
609 wxRect
rc(GETWIN(id
)->GetPosition(), GETWIN(id
)->GetSize());
610 return PRectangleFromwxRect(rc
);
613 void Window::SetPosition(PRectangle rc
) {
614 wxRect r
= wxRectFromPRectangle(rc
);
615 GETWIN(id
)->SetSize(r
);
618 void Window::SetPositionRelative(PRectangle rc
, Window
) {
619 SetPosition(rc
); // ????
622 PRectangle
Window::GetClientPosition() {
623 if (! id
) return PRectangle();
624 wxSize sz
= GETWIN(id
)->GetClientSize();
625 return PRectangle(0, 0, sz
.x
, sz
.y
);
628 void Window::Show(bool show
) {
629 GETWIN(id
)->Show(show
);
632 void Window::InvalidateAll() {
633 GETWIN(id
)->Refresh(false);
636 void Window::InvalidateRectangle(PRectangle rc
) {
637 wxRect r
= wxRectFromPRectangle(rc
);
638 GETWIN(id
)->Refresh(false, &r
);
641 void Window::SetFont(Font
&font
) {
642 GETWIN(id
)->SetFont(*((wxFont
*)font
.GetID()));
645 void Window::SetCursor(Cursor curs
) {
650 cursorId
= wxCURSOR_IBEAM
;
653 cursorId
= wxCURSOR_ARROW
;
656 cursorId
= wxCURSOR_ARROW
; // ** no up arrow... wxCURSOR_UPARROW;
659 cursorId
= wxCURSOR_WAIT
;
662 cursorId
= wxCURSOR_SIZEWE
;
665 cursorId
= wxCURSOR_SIZENS
;
667 case cursorReverseArrow
:
668 cursorId
= wxCURSOR_RIGHT_ARROW
;
671 cursorId
= wxCURSOR_HAND
;
674 cursorId
= wxCURSOR_ARROW
;
678 wxCursor wc
= wxStockCursor(cursorId
) ;
680 wxCursor wc
= wxCursor(cursorId
) ;
682 if(curs
!= cursorLast
)
684 GETWIN(id
)->SetCursor(wc
);
690 void Window::SetTitle(const char *s
) {
691 GETWIN(id
)->SetLabel(stc2wx(s
));
695 //----------------------------------------------------------------------
696 // Helper classes for ListBox
699 // This is a simple subclass of wxListView that just resets focus to the
700 // parent when it gets it.
701 class wxSTCListBox
: public wxListView
{
703 wxSTCListBox(wxWindow
* parent
, wxWindowID id
,
704 const wxPoint
& pos
, const wxSize
& size
,
709 Hide(); // don't flicker as we move it around...
711 Create(parent
, id
, pos
, size
, style
);
715 void OnFocus(wxFocusEvent
& event
) {
716 GetParent()->SetFocus();
720 void OnKillFocus(wxFocusEvent
& WXUNUSED(event
)) {
721 // Do nothing. Prevents base class from resetting the colors...
725 // For some reason I don't understand yet the focus doesn't really leave
726 // the listbox like it should, so if we get any events feed them back to
728 void OnKeyDown(wxKeyEvent
& event
) {
729 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
731 void OnChar(wxKeyEvent
& event
) {
732 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
735 // And we need to force the focus back when being destroyed
737 GetGrandParent()->SetFocus();
742 DECLARE_EVENT_TABLE()
745 BEGIN_EVENT_TABLE(wxSTCListBox
, wxListView
)
746 EVT_SET_FOCUS( wxSTCListBox::OnFocus
)
747 EVT_KILL_FOCUS(wxSTCListBox::OnKillFocus
)
749 EVT_KEY_DOWN( wxSTCListBox::OnKeyDown
)
750 EVT_CHAR( wxSTCListBox::OnChar
)
756 #if wxUSE_POPUPWIN //-----------------------------------
757 #include <wx/popupwin.h>
761 // TODO: Refactor these two classes to have a common base (or a mix-in) to get
762 // rid of the code duplication. (Either that or convince somebody to
763 // implement wxPopupWindow for the Mac!!)
765 // In the meantime, be careful to duplicate any changes as needed...
768 // A popup window to place the wxSTCListBox upon
769 class wxSTCListBoxWin
: public wxPopupWindow
773 CallBackAction doubleClickAction
;
774 void* doubleClickActionData
;
776 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point
WXUNUSED(location
)) :
777 wxPopupWindow(parent
, wxBORDER_NONE
)
779 SetBackgroundColour(*wxBLACK
); // for our simple border
781 lv
= new wxSTCListBox(parent
, id
, wxDefaultPosition
, wxDefaultSize
,
782 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxBORDER_NONE
);
783 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
784 lv
->InsertColumn(0, wxEmptyString
);
785 lv
->InsertColumn(1, wxEmptyString
);
787 // NOTE: We need to fool the wxListView into thinking that it has the
788 // focus so it will use the normal selection colour and will look
789 // "right" to the user. But since the wxPopupWindow or its children
790 // can't receive focus then we have to pull a fast one and temporarily
791 // parent the listctrl on the STC window and then call SetFocus and
792 // then reparent it back to the popup.
801 // Set position in client coords
802 virtual void DoSetSize(int x
, int y
,
803 int width
, int height
,
804 int sizeFlags
= wxSIZE_AUTO
) {
805 if (x
!= wxDefaultCoord
) {
806 GetParent()->ClientToScreen(&x
, NULL
);
808 if (y
!= wxDefaultCoord
) {
809 GetParent()->ClientToScreen(NULL
, &y
);
811 wxPopupWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
814 // return position as if it were in client coords
815 virtual void DoGetPosition( int *x
, int *y
) const {
817 wxPopupWindow::DoGetPosition(&sx
, &sy
);
818 GetParent()->ScreenToClient(&sx
, &sy
);
825 if ( !wxPendingDelete
.Member(this) )
826 wxPendingDelete
.Append(this);
832 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
835 il
->GetSize(0, w
, h
);
842 void SetDoubleClickAction(CallBackAction action
, void *data
) {
843 doubleClickAction
= action
;
844 doubleClickActionData
= data
;
848 void OnFocus(wxFocusEvent
& event
) {
849 GetParent()->SetFocus();
853 void OnSize(wxSizeEvent
& event
) {
855 wxSize sz
= GetSize();
858 lv
->SetSize(1, 1, sz
.x
, sz
.y
);
859 // reset the column widths
860 lv
->SetColumnWidth(0, IconWidth()+4);
861 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
862 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
866 void OnActivate(wxListEvent
& WXUNUSED(event
)) {
867 doubleClickAction(doubleClickActionData
);
870 wxListView
* GetLB() { return lv
; }
873 DECLARE_EVENT_TABLE()
877 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxPopupWindow
)
878 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
879 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
880 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
885 #else // wxUSE_POPUPWIN -----------------------------------
887 // A normal window to place the wxSTCListBox upon.
888 class wxSTCListBoxWin
: public wxWindow
{
891 CallBackAction doubleClickAction
;
892 void* doubleClickActionData
;
894 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point location
) :
895 wxWindow(parent
, id
, wxPoint(location
.x
, location
.y
), wxSize(0,0), wxSIMPLE_BORDER
)
898 lv
= new wxSTCListBox(this, id
, wxDefaultPosition
, wxDefaultSize
,
899 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxNO_BORDER
);
900 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
901 lv
->InsertColumn(0, wxEmptyString
);
902 lv
->InsertColumn(1, wxEmptyString
);
904 // Eventhough we immediately reset the focus to the parent, this helps
905 // things to look right...
912 // On OSX and (possibly others) there can still be pending
913 // messages/events for the list control when Scintilla wants to
914 // close it, so do a pending delete of it instead of destroying
918 // The bottom edge of this window is not getting properly
919 // refreshed upon deletion, so help it out...
920 wxWindow
* p
= GetParent();
921 wxRect
r(GetPosition(), GetSize());
922 r
.SetHeight(r
.GetHeight()+1);
923 p
->Refresh(false, &r
);
925 if ( !wxPendingDelete
.Member(this) )
926 wxPendingDelete
.Append(this);
932 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
935 il
->GetSize(0, w
, h
);
942 void SetDoubleClickAction(CallBackAction action
, void *data
) {
943 doubleClickAction
= action
;
944 doubleClickActionData
= data
;
948 void OnFocus(wxFocusEvent
& event
) {
949 GetParent()->SetFocus();
953 void OnSize(wxSizeEvent
& event
) {
955 wxSize sz
= GetClientSize();
957 // reset the column widths
958 lv
->SetColumnWidth(0, IconWidth()+4);
959 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
960 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
965 virtual bool Show(bool show
= true) {
966 bool rv
= wxWindow::Show(show
);
967 GetParent()->Refresh(false);
972 void OnActivate(wxListEvent
& WXUNUSED(event
)) {
973 doubleClickAction(doubleClickActionData
);
976 wxListView
* GetLB() { return lv
; }
979 DECLARE_EVENT_TABLE()
983 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxWindow
)
984 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
985 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
986 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
989 #endif // wxUSE_POPUPWIN -----------------------------------
992 inline wxSTCListBoxWin
* GETLBW(WindowID win
) {
993 return ((wxSTCListBoxWin
*)win
);
996 inline wxListView
* GETLB(WindowID win
) {
997 return GETLBW(win
)->GetLB();
1000 //----------------------------------------------------------------------
1002 class ListBoxImpl
: public ListBox
{
1006 int desiredVisibleRows
;
1009 Point location
; // Caret location at which the list is opened
1010 wxImageList
* imgList
;
1011 wxArrayInt
* imgTypeMap
;
1017 virtual void SetFont(Font
&font
);
1018 virtual void Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
);
1019 virtual void SetAverageCharWidth(int width
);
1020 virtual void SetVisibleRows(int rows
);
1021 virtual int GetVisibleRows() const;
1022 virtual PRectangle
GetDesiredRect();
1023 virtual int CaretFromEdge();
1024 virtual void Clear();
1025 virtual void Append(char *s
, int type
= -1);
1026 void Append(const wxString
& text
, int type
);
1027 virtual int Length();
1028 virtual void Select(int n
);
1029 virtual int GetSelection();
1030 virtual int Find(const char *prefix
);
1031 virtual void GetValue(int n
, char *value
, int len
);
1032 virtual void RegisterImage(int type
, const char *xpm_data
);
1033 virtual void ClearRegisteredImages();
1034 virtual void SetDoubleClickAction(CallBackAction
, void *);
1035 virtual void SetList(const char* list
, char separator
, char typesep
);
1039 ListBoxImpl::ListBoxImpl()
1040 : lineHeight(10), unicodeMode(false),
1041 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
1042 imgList(NULL
), imgTypeMap(NULL
)
1046 ListBoxImpl::~ListBoxImpl() {
1058 void ListBoxImpl::SetFont(Font
&font
) {
1059 GETLB(id
)->SetFont(*((wxFont
*)font
.GetID()));
1063 void ListBoxImpl::Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
) {
1064 location
= location_
;
1065 lineHeight
= lineHeight_
;
1066 unicodeMode
= unicodeMode_
;
1068 id
= new wxSTCListBoxWin(GETWIN(parent
.GetID()), ctrlID
, location
);
1069 if (imgList
!= NULL
)
1070 GETLB(id
)->SetImageList(imgList
, wxIMAGE_LIST_SMALL
);
1074 void ListBoxImpl::SetAverageCharWidth(int width
) {
1075 aveCharWidth
= width
;
1079 void ListBoxImpl::SetVisibleRows(int rows
) {
1080 desiredVisibleRows
= rows
;
1084 int ListBoxImpl::GetVisibleRows() const {
1085 return desiredVisibleRows
;
1088 PRectangle
ListBoxImpl::GetDesiredRect() {
1089 // wxListCtrl doesn't have a DoGetBestSize, so instead we kept track of
1090 // the max size in Append and calculate it here...
1091 int maxw
= maxStrWidth
* aveCharWidth
;
1094 // give it a default if there are no lines, and/or add a bit more
1095 if (maxw
== 0) maxw
= 100;
1096 maxw
+= aveCharWidth
* 3 +
1097 GETLBW(id
)->IconWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1101 // estimate a desired height
1102 int count
= GETLB(id
)->GetItemCount();
1105 GETLB(id
)->GetItemRect(0, rect
);
1106 maxh
= count
* rect
.GetHeight();
1107 if (maxh
> 140) // TODO: Use desiredVisibleRows??
1110 // Try to make the size an exact multiple of some number of lines
1111 int lines
= maxh
/ rect
.GetHeight();
1112 maxh
= (lines
+ 1) * rect
.GetHeight() + 2;
1126 int ListBoxImpl::CaretFromEdge() {
1127 return 4 + GETLBW(id
)->IconWidth();
1131 void ListBoxImpl::Clear() {
1132 GETLB(id
)->DeleteAllItems();
1136 void ListBoxImpl::Append(char *s
, int type
) {
1137 Append(stc2wx(s
), type
);
1140 void ListBoxImpl::Append(const wxString
& text
, int type
) {
1141 long count
= GETLB(id
)->GetItemCount();
1142 long itemID
= GETLB(id
)->InsertItem(count
, wxEmptyString
);
1143 GETLB(id
)->SetItem(itemID
, 1, text
);
1144 maxStrWidth
= wxMax(maxStrWidth
, text
.length());
1146 wxCHECK_RET(imgTypeMap
, wxT("Unexpected NULL imgTypeMap"));
1147 long idx
= imgTypeMap
->Item(type
);
1148 GETLB(id
)->SetItemImage(itemID
, idx
, idx
);
1152 void ListBoxImpl::SetList(const char* list
, char separator
, char typesep
) {
1153 GETLB(id
)->Freeze();
1155 wxStringTokenizer
tkzr(stc2wx(list
), (wxChar
)separator
);
1156 while ( tkzr
.HasMoreTokens() ) {
1157 wxString token
= tkzr
.GetNextToken();
1159 int pos
= token
.Find(typesep
);
1161 token
.Mid(pos
+1).ToLong(&type
);
1162 token
.Truncate(pos
);
1164 Append(token
, (int)type
);
1170 int ListBoxImpl::Length() {
1171 return GETLB(id
)->GetItemCount();
1175 void ListBoxImpl::Select(int n
) {
1181 GETLB(id
)->Focus(n
);
1182 GETLB(id
)->Select(n
, select
);
1186 int ListBoxImpl::GetSelection() {
1187 return GETLB(id
)->GetFirstSelected();
1191 int ListBoxImpl::Find(const char *WXUNUSED(prefix
)) {
1197 void ListBoxImpl::GetValue(int n
, char *value
, int len
) {
1201 item
.SetMask(wxLIST_MASK_TEXT
);
1202 GETLB(id
)->GetItem(item
);
1203 strncpy(value
, wx2stc(item
.GetText()), len
);
1204 value
[len
-1] = '\0';
1208 void ListBoxImpl::RegisterImage(int type
, const char *xpm_data
) {
1209 wxMemoryInputStream
stream(xpm_data
, strlen(xpm_data
)+1);
1210 wxImage
img(stream
, wxBITMAP_TYPE_XPM
);
1214 // assumes all images are the same size
1215 imgList
= new wxImageList(bmp
.GetWidth(), bmp
.GetHeight(), true);
1216 imgTypeMap
= new wxArrayInt
;
1219 int idx
= imgList
->Add(bmp
);
1221 // do we need to extend the mapping array?
1222 wxArrayInt
& itm
= *imgTypeMap
;
1223 if ( itm
.GetCount() < (size_t)type
+1)
1224 itm
.Add(-1, type
- itm
.GetCount() + 1);
1226 // Add an item that maps type to the image index
1230 void ListBoxImpl::ClearRegisteredImages() {
1240 GETLB(id
)->SetImageList(NULL
, wxIMAGE_LIST_SMALL
);
1244 void ListBoxImpl::SetDoubleClickAction(CallBackAction action
, void *data
) {
1245 GETLBW(id
)->SetDoubleClickAction(action
, data
);
1249 ListBox::ListBox() {
1252 ListBox::~ListBox() {
1255 ListBox
*ListBox::Allocate() {
1256 return new ListBoxImpl();
1259 //----------------------------------------------------------------------
1261 Menu::Menu() : id(0) {
1264 void Menu::CreatePopUp() {
1269 void Menu::Destroy() {
1275 void Menu::Show(Point pt
, Window
&w
) {
1276 GETWIN(w
.GetID())->PopupMenu((wxMenu
*)id
, pt
.x
- 4, pt
.y
);
1280 //----------------------------------------------------------------------
1282 DynamicLibrary
*DynamicLibrary::Load(const char *WXUNUSED(modulePath
)) {
1283 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1287 //----------------------------------------------------------------------
1289 ColourDesired
Platform::Chrome() {
1291 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1292 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1295 ColourDesired
Platform::ChromeHighlight() {
1297 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
);
1298 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1301 const char *Platform::DefaultFont() {
1302 static char buf
[128];
1303 strcpy(buf
, wxNORMAL_FONT
->GetFaceName().mbc_str());
1307 int Platform::DefaultFontSize() {
1308 return wxNORMAL_FONT
->GetPointSize();
1311 unsigned int Platform::DoubleClickTime() {
1312 return 500; // **** ::GetDoubleClickTime();
1315 bool Platform::MouseButtonBounce() {
1318 void Platform::DebugDisplay(const char *s
) {
1319 wxLogDebug(stc2wx(s
));
1322 bool Platform::IsKeyDown(int WXUNUSED(key
)) {
1323 return false; // I don't think we'll need this.
1326 long Platform::SendScintilla(WindowID w
,
1328 unsigned long wParam
,
1331 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1332 return stc
->SendMsg(msg
, wParam
, lParam
);
1335 long Platform::SendScintillaPointer(WindowID w
,
1337 unsigned long wParam
,
1340 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1341 return stc
->SendMsg(msg
, wParam
, (long)lParam
);
1345 // These are utility functions not really tied to a platform
1347 int Platform::Minimum(int a
, int b
) {
1354 int Platform::Maximum(int a
, int b
) {
1363 void Platform::DebugPrintf(const char *format
, ...) {
1367 va_start(pArguments
, format
);
1368 vsprintf(buffer
,format
,pArguments
);
1370 Platform::DebugDisplay(buffer
);
1375 static bool assertionPopUps
= true;
1377 bool Platform::ShowAssertionPopUps(bool assertionPopUps_
) {
1378 bool ret
= assertionPopUps
;
1379 assertionPopUps
= assertionPopUps_
;
1383 void Platform::Assert(const char *c
, const char *file
, int line
) {
1385 sprintf(buffer
, "Assertion [%s] failed at %s %d", c
, file
, line
);
1386 if (assertionPopUps
) {
1388 wxMessageBox(stc2wx(buffer
),
1389 wxT("Assertion failure"),
1390 wxICON_HAND
| wxOK
);
1391 // if (idButton == IDRETRY) {
1393 // } else if (idButton == IDIGNORE) {
1399 strcat(buffer
, "\r\n");
1400 Platform::DebugDisplay(buffer
);
1406 int Platform::Clamp(int val
, int minVal
, int maxVal
) {
1415 bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage
), char WXUNUSED(ch
)) {
1419 int Platform::DBCSCharLength(int WXUNUSED(codePage
), const char *WXUNUSED(s
)) {
1423 int Platform::DBCSCharMaxLength() {
1428 //----------------------------------------------------------------------
1430 ElapsedTime::ElapsedTime() {
1431 wxLongLong localTime
= wxGetLocalTimeMillis();
1432 littleBit
= localTime
.GetLo();
1433 bigBit
= localTime
.GetHi();
1436 double ElapsedTime::Duration(bool reset
) {
1437 wxLongLong
prevTime(bigBit
, littleBit
);
1438 wxLongLong localTime
= wxGetLocalTimeMillis();
1440 littleBit
= localTime
.GetLo();
1441 bigBit
= localTime
.GetHi();
1443 wxLongLong duration
= localTime
- prevTime
;
1444 double result
= duration
.ToDouble();
1450 //----------------------------------------------------------------------
1454 #include "UniConversion.h"
1456 // Convert using Scintilla's functions instead of wx's, Scintilla's are more
1457 // forgiving and won't assert...
1459 wxString
stc2wx(const char* str
, size_t len
)
1462 return wxEmptyString
;
1464 size_t wclen
= UCS2Length(str
, len
);
1465 wxWCharBuffer
buffer(wclen
+1);
1467 size_t actualLen
= UCS2FromUTF8(str
, len
, buffer
.data(), wclen
+1);
1468 return wxString(buffer
.data(), actualLen
);
1473 wxString
stc2wx(const char* str
)
1475 return stc2wx(str
, strlen(str
));
1479 const wxWX2MBbuf
wx2stc(const wxString
& str
)
1481 const wchar_t* wcstr
= str
.c_str();
1482 size_t wclen
= str
.length();
1483 size_t len
= UTF8Length(wcstr
, wclen
);
1485 wxCharBuffer
buffer(len
+1);
1486 UTF8FromUCS2(wcstr
, wclen
, buffer
.data(), len
);
1488 // TODO check NULL termination!!