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"
17 #ifdef wxHAVE_RAW_BITMAP
18 #include "wx/rawbmp.h"
23 #include "wx/stc/stc.h"
27 Point
Point::FromLong(long lpoint
) {
28 return Point(lpoint
& 0xFFFF, lpoint
>> 16);
31 wxRect
wxRectFromPRectangle(PRectangle prc
) {
32 wxRect
r(prc
.left
, prc
.top
,
33 prc
.Width(), prc
.Height());
37 PRectangle
PRectangleFromwxRect(wxRect rc
) {
38 return PRectangle(rc
.GetLeft(), rc
.GetTop(),
39 rc
.GetRight()+1, rc
.GetBottom()+1);
42 wxColour
wxColourFromCA(const ColourAllocated
& ca
) {
43 ColourDesired
cd(ca
.AsLong());
44 return wxColour((unsigned char)cd
.GetRed(),
45 (unsigned char)cd
.GetGreen(),
46 (unsigned char)cd
.GetBlue());
49 //----------------------------------------------------------------------
53 allowRealization
= false;
55 entries
= new ColourPair
[size
];
64 void Palette::Release() {
68 entries
= new ColourPair
[size
];
71 // This method either adds a colour to the list of wanted colours (want==true)
72 // or retrieves the allocated colour back to the ColourPair.
73 // This is one method to make it easier to keep the code for wanting and retrieving in sync.
74 void Palette::WantFind(ColourPair
&cp
, bool want
) {
76 for (int i
=0; i
< used
; i
++) {
77 if (entries
[i
].desired
== cp
.desired
)
82 int sizeNew
= size
* 2;
83 ColourPair
*entriesNew
= new ColourPair
[sizeNew
];
84 for (int j
=0; j
<size
; j
++) {
85 entriesNew
[j
] = entries
[j
];
92 entries
[used
].desired
= cp
.desired
;
93 entries
[used
].allocated
.Set(cp
.desired
.AsLong());
96 for (int i
=0; i
< used
; i
++) {
97 if (entries
[i
].desired
== cp
.desired
) {
98 cp
.allocated
= entries
[i
].allocated
;
102 cp
.allocated
.Set(cp
.desired
.AsLong());
106 void Palette::Allocate(Window
&) {
107 if (allowRealization
) {
112 //----------------------------------------------------------------------
122 void Font::Create(const char *faceName
, int characterSet
, int size
, bool bold
, bool italic
, bool extraFontFlag
) {
126 // The minus one is done because since Scintilla uses SC_SHARSET_DEFAULT
127 // internally and we need to have wxFONENCODING_DEFAULT == SC_SHARSET_DEFAULT
128 // so we adjust the encoding before passing it to Scintilla. See also
129 // wxStyledTextCtrl::StyleSetCharacterSet
130 wxFontEncoding encoding
= (wxFontEncoding
)(characterSet
-1);
132 wxFontEncodingArray ea
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
136 wxFont
* font
= new wxFont(size
,
138 italic
? wxITALIC
: wxNORMAL
,
139 bold
? wxBOLD
: wxNORMAL
,
143 font
->SetNoAntiAliasing(!extraFontFlag
);
148 void Font::Release() {
154 //----------------------------------------------------------------------
156 class SurfaceImpl
: public Surface
{
169 virtual void Init(WindowID wid
);
170 virtual void Init(SurfaceID sid
, WindowID wid
);
171 virtual void InitPixMap(int width
, int height
, Surface
*surface_
, WindowID wid
);
173 virtual void Release();
174 virtual bool Initialised();
175 virtual void PenColour(ColourAllocated fore
);
176 virtual int LogPixelsY();
177 virtual int DeviceHeightFont(int points
);
178 virtual void MoveTo(int x_
, int y_
);
179 virtual void LineTo(int x_
, int y_
);
180 virtual void Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
);
181 virtual void RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
182 virtual void FillRectangle(PRectangle rc
, ColourAllocated back
);
183 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
);
184 virtual void RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
185 virtual void AlphaRectangle(PRectangle rc
, int cornerSize
, ColourAllocated fill
, int alphaFill
,
186 ColourAllocated outline
, int alphaOutline
, int flags
);
187 virtual void Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
188 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
);
190 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
191 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
192 virtual void DrawTextTransparent(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
);
193 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, int *positions
);
194 virtual int WidthText(Font
&font_
, const char *s
, int len
);
195 virtual int WidthChar(Font
&font_
, char ch
);
196 virtual int Ascent(Font
&font_
);
197 virtual int Descent(Font
&font_
);
198 virtual int InternalLeading(Font
&font_
);
199 virtual int ExternalLeading(Font
&font_
);
200 virtual int Height(Font
&font_
);
201 virtual int AverageCharWidth(Font
&font_
);
203 virtual int SetPalette(Palette
*pal
, bool inBackGround
);
204 virtual void SetClip(PRectangle rc
);
205 virtual void FlushCachedState();
207 virtual void SetUnicodeMode(bool unicodeMode_
);
208 virtual void SetDBCSMode(int codePage
);
210 void BrushColour(ColourAllocated back
);
211 void SetFont(Font
&font_
);
216 SurfaceImpl::SurfaceImpl() :
217 hdc(0), hdcOwned(0), bitmap(0),
218 x(0), y(0), unicodeMode(0)
221 SurfaceImpl::~SurfaceImpl() {
225 void SurfaceImpl::Init(WindowID wid
) {
228 hdc
= new wxMemoryDC();
231 // On Mac and GTK the DC is not really valid until it has a bitmap
232 // selected into it. So instead of just creating the DC with no bitmap,
233 // go ahead and give it one.
234 InitPixMap(1,1,NULL
,wid
);
238 void SurfaceImpl::Init(SurfaceID hdc_
, WindowID
) {
243 void SurfaceImpl::InitPixMap(int width
, int height
, Surface
*WXUNUSED(surface_
), WindowID
) {
245 hdc
= new wxMemoryDC();
247 if (width
< 1) width
= 1;
248 if (height
< 1) height
= 1;
249 bitmap
= new wxBitmap(width
, height
);
250 ((wxMemoryDC
*)hdc
)->SelectObject(*bitmap
);
254 void SurfaceImpl::Release() {
256 ((wxMemoryDC
*)hdc
)->SelectObject(wxNullBitmap
);
268 bool SurfaceImpl::Initialised() {
273 void SurfaceImpl::PenColour(ColourAllocated fore
) {
274 hdc
->SetPen(wxPen(wxColourFromCA(fore
), 1, wxSOLID
));
277 void SurfaceImpl::BrushColour(ColourAllocated back
) {
278 hdc
->SetBrush(wxBrush(wxColourFromCA(back
), wxSOLID
));
281 void SurfaceImpl::SetFont(Font
&font_
) {
283 hdc
->SetFont(*((wxFont
*)font_
.GetID()));
287 int SurfaceImpl::LogPixelsY() {
288 return hdc
->GetPPI().y
;
291 int SurfaceImpl::DeviceHeightFont(int points
) {
295 void SurfaceImpl::MoveTo(int x_
, int y_
) {
300 void SurfaceImpl::LineTo(int x_
, int y_
) {
301 hdc
->DrawLine(x
,y
, x_
,y_
);
306 void SurfaceImpl::Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
) {
309 hdc
->DrawPolygon(npts
, (wxPoint
*)pts
);
312 void SurfaceImpl::RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
315 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
318 void SurfaceImpl::FillRectangle(PRectangle rc
, ColourAllocated back
) {
320 hdc
->SetPen(*wxTRANSPARENT_PEN
);
321 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
324 void SurfaceImpl::FillRectangle(PRectangle rc
, Surface
&surfacePattern
) {
326 if (((SurfaceImpl
&)surfacePattern
).bitmap
)
327 br
= wxBrush(*((SurfaceImpl
&)surfacePattern
).bitmap
);
328 else // Something is wrong so display in red
329 br
= wxBrush(*wxRED
, wxSOLID
);
330 hdc
->SetPen(*wxTRANSPARENT_PEN
);
332 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
335 void SurfaceImpl::RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
338 hdc
->DrawRoundedRectangle(wxRectFromPRectangle(rc
), 4);
342 #define wxPy_premultiply(p, a) ((p) * (a) / 0xff)
344 #define wxPy_premultiply(p, a) (p)
347 void SurfaceImpl::AlphaRectangle(PRectangle rc
, int cornerSize
,
348 ColourAllocated fill
, int alphaFill
,
349 ColourAllocated outline
, int alphaOutline
,
351 #ifdef wxHAVE_RAW_BITMAP
353 wxRect r
= wxRectFromPRectangle(rc
);
354 wxBitmap
bmp(r
.width
, r
.height
, 32);
355 wxAlphaPixelData
pixData(bmp
);
358 // Set the fill pixels
359 ColourDesired
cdf(fill
.AsLong());
360 int red
= cdf
.GetRed();
361 int green
= cdf
.GetGreen();
362 int blue
= cdf
.GetBlue();
364 wxAlphaPixelData::Iterator
p(pixData
);
365 for (y
=0; y
<r
.height
; y
++) {
366 p
.MoveTo(pixData
, 0, y
);
367 for (x
=0; x
<r
.width
; x
++) {
368 p
.Red() = wxPy_premultiply(red
, alphaFill
);
369 p
.Green() = wxPy_premultiply(green
, alphaFill
);
370 p
.Blue() = wxPy_premultiply(blue
, alphaFill
);
371 p
.Alpha() = alphaFill
;
376 // Set the outline pixels
377 ColourDesired
cdo(outline
.AsLong());
379 green
= cdo
.GetGreen();
380 blue
= cdo
.GetBlue();
381 for (x
=0; x
<r
.width
; x
++) {
382 p
.MoveTo(pixData
, x
, 0);
383 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
384 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
385 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
386 p
.Alpha() = alphaOutline
;
387 p
.MoveTo(pixData
, x
, r
.height
-1);
388 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
389 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
390 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
391 p
.Alpha() = alphaOutline
;
394 for (y
=0; y
<r
.height
; y
++) {
395 p
.MoveTo(pixData
, 0, y
);
396 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
397 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
398 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
399 p
.Alpha() = alphaOutline
;
400 p
.MoveTo(pixData
, r
.width
-1, y
);
401 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
402 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
403 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
404 p
.Alpha() = alphaOutline
;
408 hdc
->DrawBitmap(bmp
, r
.x
, r
.y
, true);
411 RectangleDraw(rc
, outline
, fill
);
415 void SurfaceImpl::Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
418 hdc
->DrawEllipse(wxRectFromPRectangle(rc
));
421 void SurfaceImpl::Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
) {
422 wxRect r
= wxRectFromPRectangle(rc
);
423 hdc
->Blit(r
.x
, r
.y
, r
.width
, r
.height
,
424 ((SurfaceImpl
&)surfaceSource
).hdc
,
425 from
.x
, from
.y
, wxCOPY
);
428 void SurfaceImpl::DrawTextNoClip(PRectangle rc
, Font
&font
, int ybase
,
429 const char *s
, int len
,
430 ColourAllocated fore
, ColourAllocated back
) {
432 hdc
->SetTextForeground(wxColourFromCA(fore
));
433 hdc
->SetTextBackground(wxColourFromCA(back
));
434 FillRectangle(rc
, back
);
436 // ybase is where the baseline should be, but wxWin uses the upper left
437 // corner, so I need to calculate the real position for the text...
438 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
441 void SurfaceImpl::DrawTextClipped(PRectangle rc
, Font
&font
, int ybase
,
442 const char *s
, int len
,
443 ColourAllocated fore
, ColourAllocated back
) {
445 hdc
->SetTextForeground(wxColourFromCA(fore
));
446 hdc
->SetTextBackground(wxColourFromCA(back
));
447 FillRectangle(rc
, back
);
448 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
450 // see comments above
451 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
452 hdc
->DestroyClippingRegion();
456 void SurfaceImpl::DrawTextTransparent(PRectangle rc
, Font
&font
, int ybase
,
457 const char *s
, int len
,
458 ColourAllocated fore
) {
461 hdc
->SetTextForeground(wxColourFromCA(fore
));
462 hdc
->SetBackgroundMode(wxTRANSPARENT
);
464 // ybase is where the baseline should be, but wxWin uses the upper left
465 // corner, so I need to calculate the real position for the text...
466 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
468 hdc
->SetBackgroundMode(wxSOLID
);
472 void SurfaceImpl::MeasureWidths(Font
&font
, const char *s
, int len
, int *positions
) {
474 wxString str
= stc2wx(s
, len
);
479 hdc
->GetPartialTextExtents(str
, tpos
);
482 // Map the widths for UCS-2 characters back to the UTF-8 input string
483 // NOTE: I don't think this is right for when sizeof(wxChar) > 2, ie wxGTK2
484 // so figure it out and fix it!
487 while ((int)i
< len
) {
488 unsigned char uch
= (unsigned char)s
[i
];
489 positions
[i
++] = tpos
[ui
];
491 if (uch
< (0x80 + 0x40 + 0x20)) {
492 positions
[i
++] = tpos
[ui
];
494 positions
[i
++] = tpos
[ui
];
495 positions
[i
++] = tpos
[ui
];
502 // If not unicode then just use the widths we have
504 std::copy(tpos
.begin(), tpos
.end(), positions
);
506 memcpy(positions
, tpos
.begin(), len
* sizeof(int));
512 int SurfaceImpl::WidthText(Font
&font
, const char *s
, int len
) {
517 hdc
->GetTextExtent(stc2wx(s
, len
), &w
, &h
);
522 int SurfaceImpl::WidthChar(Font
&font
, char ch
) {
526 char s
[2] = { ch
, 0 };
528 hdc
->GetTextExtent(stc2wx(s
, 1), &w
, &h
);
532 #define EXTENT_TEST wxT(" `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
534 int SurfaceImpl::Ascent(Font
&font
) {
537 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
542 int SurfaceImpl::Descent(Font
&font
) {
545 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
549 int SurfaceImpl::InternalLeading(Font
&WXUNUSED(font
)) {
553 int SurfaceImpl::ExternalLeading(Font
&font
) {
556 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
560 int SurfaceImpl::Height(Font
&font
) {
562 return hdc
->GetCharHeight() + 1;
565 int SurfaceImpl::AverageCharWidth(Font
&font
) {
567 return hdc
->GetCharWidth();
570 int SurfaceImpl::SetPalette(Palette
*WXUNUSED(pal
), bool WXUNUSED(inBackGround
)) {
574 void SurfaceImpl::SetClip(PRectangle rc
) {
575 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
578 void SurfaceImpl::FlushCachedState() {
581 void SurfaceImpl::SetUnicodeMode(bool unicodeMode_
) {
582 unicodeMode
=unicodeMode_
;
585 void SurfaceImpl::SetDBCSMode(int WXUNUSED(codePage
)) {
586 // dbcsMode = codePage == SC_CP_DBCS;
590 Surface
*Surface::Allocate() {
591 return new SurfaceImpl
;
595 //----------------------------------------------------------------------
598 inline wxWindow
* GETWIN(WindowID id
) { return (wxWindow
*)id
; }
603 void Window::Destroy() {
606 GETWIN(id
)->Destroy();
611 bool Window::HasFocus() {
612 return wxWindow::FindFocus() == GETWIN(id
);
615 PRectangle
Window::GetPosition() {
616 if (! id
) return PRectangle();
617 wxRect
rc(GETWIN(id
)->GetPosition(), GETWIN(id
)->GetSize());
618 return PRectangleFromwxRect(rc
);
621 void Window::SetPosition(PRectangle rc
) {
622 wxRect r
= wxRectFromPRectangle(rc
);
623 GETWIN(id
)->SetSize(r
);
626 void Window::SetPositionRelative(PRectangle rc
, Window
) {
627 SetPosition(rc
); // ????
630 PRectangle
Window::GetClientPosition() {
631 if (! id
) return PRectangle();
632 wxSize sz
= GETWIN(id
)->GetClientSize();
633 return PRectangle(0, 0, sz
.x
, sz
.y
);
636 void Window::Show(bool show
) {
637 GETWIN(id
)->Show(show
);
640 void Window::InvalidateAll() {
641 GETWIN(id
)->Refresh(false);
644 void Window::InvalidateRectangle(PRectangle rc
) {
645 wxRect r
= wxRectFromPRectangle(rc
);
646 GETWIN(id
)->Refresh(false, &r
);
649 void Window::SetFont(Font
&font
) {
650 GETWIN(id
)->SetFont(*((wxFont
*)font
.GetID()));
653 void Window::SetCursor(Cursor curs
) {
658 cursorId
= wxCURSOR_IBEAM
;
661 cursorId
= wxCURSOR_ARROW
;
664 cursorId
= wxCURSOR_ARROW
; // ** no up arrow... wxCURSOR_UPARROW;
667 cursorId
= wxCURSOR_WAIT
;
670 cursorId
= wxCURSOR_SIZEWE
;
673 cursorId
= wxCURSOR_SIZENS
;
675 case cursorReverseArrow
:
676 cursorId
= wxCURSOR_RIGHT_ARROW
;
679 cursorId
= wxCURSOR_HAND
;
682 cursorId
= wxCURSOR_ARROW
;
686 wxCursor wc
= wxStockCursor(cursorId
) ;
688 wxCursor wc
= wxCursor(cursorId
) ;
690 if(curs
!= cursorLast
)
692 GETWIN(id
)->SetCursor(wc
);
698 void Window::SetTitle(const char *s
) {
699 GETWIN(id
)->SetLabel(stc2wx(s
));
703 //----------------------------------------------------------------------
704 // Helper classes for ListBox
707 // This is a simple subclass of wxListView that just resets focus to the
708 // parent when it gets it.
709 class wxSTCListBox
: public wxListView
{
711 wxSTCListBox(wxWindow
* parent
, wxWindowID id
,
712 const wxPoint
& pos
, const wxSize
& size
,
717 Hide(); // don't flicker as we move it around...
719 Create(parent
, id
, pos
, size
, style
);
723 void OnFocus(wxFocusEvent
& event
) {
724 GetParent()->SetFocus();
728 void OnKillFocus(wxFocusEvent
& WXUNUSED(event
)) {
729 // Do nothing. Prevents base class from resetting the colors...
733 // For some reason I don't understand yet the focus doesn't really leave
734 // the listbox like it should, so if we get any events feed them back to
736 void OnKeyDown(wxKeyEvent
& event
) {
737 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
739 void OnChar(wxKeyEvent
& event
) {
740 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
743 // And we need to force the focus back when being destroyed
745 GetGrandParent()->SetFocus();
750 DECLARE_EVENT_TABLE()
753 BEGIN_EVENT_TABLE(wxSTCListBox
, wxListView
)
754 EVT_SET_FOCUS( wxSTCListBox::OnFocus
)
755 EVT_KILL_FOCUS(wxSTCListBox::OnKillFocus
)
757 EVT_KEY_DOWN( wxSTCListBox::OnKeyDown
)
758 EVT_CHAR( wxSTCListBox::OnChar
)
764 #if wxUSE_POPUPWIN //-----------------------------------
765 #include <wx/popupwin.h>
769 // TODO: Refactor these two classes to have a common base (or a mix-in) to get
770 // rid of the code duplication. (Either that or convince somebody to
771 // implement wxPopupWindow for the Mac!!)
773 // In the meantime, be careful to duplicate any changes as needed...
776 // A popup window to place the wxSTCListBox upon
777 class wxSTCListBoxWin
: public wxPopupWindow
781 CallBackAction doubleClickAction
;
782 void* doubleClickActionData
;
784 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point
WXUNUSED(location
)) :
785 wxPopupWindow(parent
, wxBORDER_NONE
)
787 SetBackgroundColour(*wxBLACK
); // for our simple border
789 lv
= new wxSTCListBox(parent
, id
, wxDefaultPosition
, wxDefaultSize
,
790 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxBORDER_NONE
);
791 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
792 lv
->InsertColumn(0, wxEmptyString
);
793 lv
->InsertColumn(1, wxEmptyString
);
795 // NOTE: We need to fool the wxListView into thinking that it has the
796 // focus so it will use the normal selection colour and will look
797 // "right" to the user. But since the wxPopupWindow or its children
798 // can't receive focus then we have to pull a fast one and temporarily
799 // parent the listctrl on the STC window and then call SetFocus and
800 // then reparent it back to the popup.
809 // Set position in client coords
810 virtual void DoSetSize(int x
, int y
,
811 int width
, int height
,
812 int sizeFlags
= wxSIZE_AUTO
) {
813 if (x
!= wxDefaultCoord
) {
814 GetParent()->ClientToScreen(&x
, NULL
);
816 if (y
!= wxDefaultCoord
) {
817 GetParent()->ClientToScreen(NULL
, &y
);
819 wxPopupWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
822 // return position as if it were in client coords
823 virtual void DoGetPosition( int *x
, int *y
) const {
825 wxPopupWindow::DoGetPosition(&sx
, &sy
);
826 GetParent()->ScreenToClient(&sx
, &sy
);
833 if ( !wxPendingDelete
.Member(this) )
834 wxPendingDelete
.Append(this);
840 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
843 il
->GetSize(0, w
, h
);
850 void SetDoubleClickAction(CallBackAction action
, void *data
) {
851 doubleClickAction
= action
;
852 doubleClickActionData
= data
;
856 void OnFocus(wxFocusEvent
& event
) {
857 GetParent()->SetFocus();
861 void OnSize(wxSizeEvent
& event
) {
863 wxSize sz
= GetSize();
866 lv
->SetSize(1, 1, sz
.x
, sz
.y
);
867 // reset the column widths
868 lv
->SetColumnWidth(0, IconWidth()+4);
869 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
870 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
874 void OnActivate(wxListEvent
& WXUNUSED(event
)) {
875 doubleClickAction(doubleClickActionData
);
878 wxListView
* GetLB() { return lv
; }
881 DECLARE_EVENT_TABLE()
885 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxPopupWindow
)
886 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
887 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
888 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
893 #else // wxUSE_POPUPWIN -----------------------------------
895 // A normal window to place the wxSTCListBox upon.
896 class wxSTCListBoxWin
: public wxWindow
{
899 CallBackAction doubleClickAction
;
900 void* doubleClickActionData
;
902 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point location
) :
903 wxWindow(parent
, id
, wxPoint(location
.x
, location
.y
), wxSize(0,0), wxSIMPLE_BORDER
)
906 lv
= new wxSTCListBox(this, id
, wxDefaultPosition
, wxDefaultSize
,
907 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxNO_BORDER
);
908 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
909 lv
->InsertColumn(0, wxEmptyString
);
910 lv
->InsertColumn(1, wxEmptyString
);
912 // Eventhough we immediately reset the focus to the parent, this helps
913 // things to look right...
920 // On OSX and (possibly others) there can still be pending
921 // messages/events for the list control when Scintilla wants to
922 // close it, so do a pending delete of it instead of destroying
926 // The bottom edge of this window is not getting properly
927 // refreshed upon deletion, so help it out...
928 wxWindow
* p
= GetParent();
929 wxRect
r(GetPosition(), GetSize());
930 r
.SetHeight(r
.GetHeight()+1);
931 p
->Refresh(false, &r
);
933 if ( !wxPendingDelete
.Member(this) )
934 wxPendingDelete
.Append(this);
940 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
943 il
->GetSize(0, w
, h
);
950 void SetDoubleClickAction(CallBackAction action
, void *data
) {
951 doubleClickAction
= action
;
952 doubleClickActionData
= data
;
956 void OnFocus(wxFocusEvent
& event
) {
957 GetParent()->SetFocus();
961 void OnSize(wxSizeEvent
& event
) {
963 wxSize sz
= GetClientSize();
965 // reset the column widths
966 lv
->SetColumnWidth(0, IconWidth()+4);
967 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
968 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
973 virtual bool Show(bool show
= true) {
974 bool rv
= wxWindow::Show(show
);
975 GetParent()->Refresh(false);
980 void OnActivate(wxListEvent
& WXUNUSED(event
)) {
981 doubleClickAction(doubleClickActionData
);
984 wxListView
* GetLB() { return lv
; }
987 DECLARE_EVENT_TABLE()
991 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxWindow
)
992 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
993 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
994 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
997 #endif // wxUSE_POPUPWIN -----------------------------------
1000 inline wxSTCListBoxWin
* GETLBW(WindowID win
) {
1001 return ((wxSTCListBoxWin
*)win
);
1004 inline wxListView
* GETLB(WindowID win
) {
1005 return GETLBW(win
)->GetLB();
1008 //----------------------------------------------------------------------
1010 class ListBoxImpl
: public ListBox
{
1014 int desiredVisibleRows
;
1017 Point location
; // Caret location at which the list is opened
1018 wxImageList
* imgList
;
1019 wxArrayInt
* imgTypeMap
;
1025 virtual void SetFont(Font
&font
);
1026 virtual void Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
);
1027 virtual void SetAverageCharWidth(int width
);
1028 virtual void SetVisibleRows(int rows
);
1029 virtual int GetVisibleRows() const;
1030 virtual PRectangle
GetDesiredRect();
1031 virtual int CaretFromEdge();
1032 virtual void Clear();
1033 virtual void Append(char *s
, int type
= -1);
1034 void Append(const wxString
& text
, int type
);
1035 virtual int Length();
1036 virtual void Select(int n
);
1037 virtual int GetSelection();
1038 virtual int Find(const char *prefix
);
1039 virtual void GetValue(int n
, char *value
, int len
);
1040 virtual void RegisterImage(int type
, const char *xpm_data
);
1041 virtual void ClearRegisteredImages();
1042 virtual void SetDoubleClickAction(CallBackAction
, void *);
1043 virtual void SetList(const char* list
, char separator
, char typesep
);
1047 ListBoxImpl::ListBoxImpl()
1048 : lineHeight(10), unicodeMode(false),
1049 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
1050 imgList(NULL
), imgTypeMap(NULL
)
1054 ListBoxImpl::~ListBoxImpl() {
1066 void ListBoxImpl::SetFont(Font
&font
) {
1067 GETLB(id
)->SetFont(*((wxFont
*)font
.GetID()));
1071 void ListBoxImpl::Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
) {
1072 location
= location_
;
1073 lineHeight
= lineHeight_
;
1074 unicodeMode
= unicodeMode_
;
1076 id
= new wxSTCListBoxWin(GETWIN(parent
.GetID()), ctrlID
, location
);
1077 if (imgList
!= NULL
)
1078 GETLB(id
)->SetImageList(imgList
, wxIMAGE_LIST_SMALL
);
1082 void ListBoxImpl::SetAverageCharWidth(int width
) {
1083 aveCharWidth
= width
;
1087 void ListBoxImpl::SetVisibleRows(int rows
) {
1088 desiredVisibleRows
= rows
;
1092 int ListBoxImpl::GetVisibleRows() const {
1093 return desiredVisibleRows
;
1096 PRectangle
ListBoxImpl::GetDesiredRect() {
1097 // wxListCtrl doesn't have a DoGetBestSize, so instead we kept track of
1098 // the max size in Append and calculate it here...
1099 int maxw
= maxStrWidth
* aveCharWidth
;
1102 // give it a default if there are no lines, and/or add a bit more
1103 if (maxw
== 0) maxw
= 100;
1104 maxw
+= aveCharWidth
* 3 +
1105 GETLBW(id
)->IconWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1109 // estimate a desired height
1110 int count
= GETLB(id
)->GetItemCount();
1113 GETLB(id
)->GetItemRect(0, rect
);
1114 maxh
= count
* rect
.GetHeight();
1115 if (maxh
> 140) // TODO: Use desiredVisibleRows??
1118 // Try to make the size an exact multiple of some number of lines
1119 int lines
= maxh
/ rect
.GetHeight();
1120 maxh
= (lines
+ 1) * rect
.GetHeight() + 2;
1134 int ListBoxImpl::CaretFromEdge() {
1135 return 4 + GETLBW(id
)->IconWidth();
1139 void ListBoxImpl::Clear() {
1140 GETLB(id
)->DeleteAllItems();
1144 void ListBoxImpl::Append(char *s
, int type
) {
1145 Append(stc2wx(s
), type
);
1148 void ListBoxImpl::Append(const wxString
& text
, int type
) {
1149 long count
= GETLB(id
)->GetItemCount();
1150 long itemID
= GETLB(id
)->InsertItem(count
, wxEmptyString
);
1151 GETLB(id
)->SetItem(itemID
, 1, text
);
1152 maxStrWidth
= wxMax(maxStrWidth
, text
.length());
1154 wxCHECK_RET(imgTypeMap
, wxT("Unexpected NULL imgTypeMap"));
1155 long idx
= imgTypeMap
->Item(type
);
1156 GETLB(id
)->SetItemImage(itemID
, idx
, idx
);
1160 void ListBoxImpl::SetList(const char* list
, char separator
, char typesep
) {
1161 GETLB(id
)->Freeze();
1163 wxStringTokenizer
tkzr(stc2wx(list
), (wxChar
)separator
);
1164 while ( tkzr
.HasMoreTokens() ) {
1165 wxString token
= tkzr
.GetNextToken();
1167 int pos
= token
.Find(typesep
);
1169 token
.Mid(pos
+1).ToLong(&type
);
1170 token
.Truncate(pos
);
1172 Append(token
, (int)type
);
1178 int ListBoxImpl::Length() {
1179 return GETLB(id
)->GetItemCount();
1183 void ListBoxImpl::Select(int n
) {
1189 GETLB(id
)->Focus(n
);
1190 GETLB(id
)->Select(n
, select
);
1194 int ListBoxImpl::GetSelection() {
1195 return GETLB(id
)->GetFirstSelected();
1199 int ListBoxImpl::Find(const char *WXUNUSED(prefix
)) {
1205 void ListBoxImpl::GetValue(int n
, char *value
, int len
) {
1209 item
.SetMask(wxLIST_MASK_TEXT
);
1210 GETLB(id
)->GetItem(item
);
1211 strncpy(value
, wx2stc(item
.GetText()), len
);
1212 value
[len
-1] = '\0';
1216 void ListBoxImpl::RegisterImage(int type
, const char *xpm_data
) {
1217 wxMemoryInputStream
stream(xpm_data
, strlen(xpm_data
)+1);
1218 wxImage
img(stream
, wxBITMAP_TYPE_XPM
);
1222 // assumes all images are the same size
1223 imgList
= new wxImageList(bmp
.GetWidth(), bmp
.GetHeight(), true);
1224 imgTypeMap
= new wxArrayInt
;
1227 int idx
= imgList
->Add(bmp
);
1229 // do we need to extend the mapping array?
1230 wxArrayInt
& itm
= *imgTypeMap
;
1231 if ( itm
.GetCount() < (size_t)type
+1)
1232 itm
.Add(-1, type
- itm
.GetCount() + 1);
1234 // Add an item that maps type to the image index
1238 void ListBoxImpl::ClearRegisteredImages() {
1248 GETLB(id
)->SetImageList(NULL
, wxIMAGE_LIST_SMALL
);
1252 void ListBoxImpl::SetDoubleClickAction(CallBackAction action
, void *data
) {
1253 GETLBW(id
)->SetDoubleClickAction(action
, data
);
1257 ListBox::ListBox() {
1260 ListBox::~ListBox() {
1263 ListBox
*ListBox::Allocate() {
1264 return new ListBoxImpl();
1267 //----------------------------------------------------------------------
1269 Menu::Menu() : id(0) {
1272 void Menu::CreatePopUp() {
1277 void Menu::Destroy() {
1283 void Menu::Show(Point pt
, Window
&w
) {
1284 GETWIN(w
.GetID())->PopupMenu((wxMenu
*)id
, pt
.x
- 4, pt
.y
);
1288 //----------------------------------------------------------------------
1290 DynamicLibrary
*DynamicLibrary::Load(const char *WXUNUSED(modulePath
)) {
1291 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1295 //----------------------------------------------------------------------
1297 ColourDesired
Platform::Chrome() {
1299 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1300 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1303 ColourDesired
Platform::ChromeHighlight() {
1305 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
);
1306 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1309 const char *Platform::DefaultFont() {
1310 static char buf
[128];
1311 strcpy(buf
, wxNORMAL_FONT
->GetFaceName().mbc_str());
1315 int Platform::DefaultFontSize() {
1316 return wxNORMAL_FONT
->GetPointSize();
1319 unsigned int Platform::DoubleClickTime() {
1320 return 500; // **** ::GetDoubleClickTime();
1323 bool Platform::MouseButtonBounce() {
1326 void Platform::DebugDisplay(const char *s
) {
1327 wxLogDebug(stc2wx(s
));
1330 bool Platform::IsKeyDown(int WXUNUSED(key
)) {
1331 return false; // I don't think we'll need this.
1334 long Platform::SendScintilla(WindowID w
,
1336 unsigned long wParam
,
1339 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1340 return stc
->SendMsg(msg
, wParam
, lParam
);
1343 long Platform::SendScintillaPointer(WindowID w
,
1345 unsigned long wParam
,
1348 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1349 return stc
->SendMsg(msg
, wParam
, (long)lParam
);
1353 // These are utility functions not really tied to a platform
1355 int Platform::Minimum(int a
, int b
) {
1362 int Platform::Maximum(int a
, int b
) {
1371 void Platform::DebugPrintf(const char *format
, ...) {
1375 va_start(pArguments
, format
);
1376 vsprintf(buffer
,format
,pArguments
);
1378 Platform::DebugDisplay(buffer
);
1383 static bool assertionPopUps
= true;
1385 bool Platform::ShowAssertionPopUps(bool assertionPopUps_
) {
1386 bool ret
= assertionPopUps
;
1387 assertionPopUps
= assertionPopUps_
;
1391 void Platform::Assert(const char *c
, const char *file
, int line
) {
1393 sprintf(buffer
, "Assertion [%s] failed at %s %d", c
, file
, line
);
1394 if (assertionPopUps
) {
1396 wxMessageBox(stc2wx(buffer
),
1397 wxT("Assertion failure"),
1398 wxICON_HAND
| wxOK
);
1399 // if (idButton == IDRETRY) {
1401 // } else if (idButton == IDIGNORE) {
1407 strcat(buffer
, "\r\n");
1408 Platform::DebugDisplay(buffer
);
1414 int Platform::Clamp(int val
, int minVal
, int maxVal
) {
1423 bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage
), char WXUNUSED(ch
)) {
1427 int Platform::DBCSCharLength(int WXUNUSED(codePage
), const char *WXUNUSED(s
)) {
1431 int Platform::DBCSCharMaxLength() {
1436 //----------------------------------------------------------------------
1438 ElapsedTime::ElapsedTime() {
1439 wxLongLong localTime
= wxGetLocalTimeMillis();
1440 littleBit
= localTime
.GetLo();
1441 bigBit
= localTime
.GetHi();
1444 double ElapsedTime::Duration(bool reset
) {
1445 wxLongLong
prevTime(bigBit
, littleBit
);
1446 wxLongLong localTime
= wxGetLocalTimeMillis();
1448 littleBit
= localTime
.GetLo();
1449 bigBit
= localTime
.GetHi();
1451 wxLongLong duration
= localTime
- prevTime
;
1452 double result
= duration
.ToDouble();
1458 //----------------------------------------------------------------------
1462 #include "UniConversion.h"
1464 // Convert using Scintilla's functions instead of wx's, Scintilla's are more
1465 // forgiving and won't assert...
1467 wxString
stc2wx(const char* str
, size_t len
)
1470 return wxEmptyString
;
1472 size_t wclen
= UCS2Length(str
, len
);
1473 wxWCharBuffer
buffer(wclen
+1);
1475 size_t actualLen
= UCS2FromUTF8(str
, len
, buffer
.data(), wclen
+1);
1476 return wxString(buffer
.data(), actualLen
);
1481 wxString
stc2wx(const char* str
)
1483 return stc2wx(str
, strlen(str
));
1487 const wxWX2MBbuf
wx2stc(const wxString
& str
)
1489 const wchar_t* wcstr
= str
.c_str();
1490 size_t wclen
= str
.length();
1491 size_t len
= UTF8Length(wcstr
, wclen
);
1493 wxCharBuffer
buffer(len
+1);
1494 UTF8FromUCS2(wcstr
, wclen
, buffer
.data(), len
);
1496 // TODO check NULL termination!!