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.
7 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/encconv.h"
21 #include "wx/listctrl.h"
22 #include "wx/mstream.h"
24 #include "wx/imaglist.h"
25 #include "wx/tokenzr.h"
27 #ifdef wxHAVE_RAW_BITMAP
28 #include "wx/rawbmp.h"
33 #include "wx/stc/stc.h"
37 Point
Point::FromLong(long lpoint
) {
38 return Point(lpoint
& 0xFFFF, lpoint
>> 16);
41 wxRect
wxRectFromPRectangle(PRectangle prc
) {
42 wxRect
r(prc
.left
, prc
.top
,
43 prc
.Width(), prc
.Height());
47 PRectangle
PRectangleFromwxRect(wxRect rc
) {
48 return PRectangle(rc
.GetLeft(), rc
.GetTop(),
49 rc
.GetRight()+1, rc
.GetBottom()+1);
52 wxColour
wxColourFromCA(const ColourAllocated
& ca
) {
53 ColourDesired
cd(ca
.AsLong());
54 return wxColour((unsigned char)cd
.GetRed(),
55 (unsigned char)cd
.GetGreen(),
56 (unsigned char)cd
.GetBlue());
59 //----------------------------------------------------------------------
63 allowRealization
= false;
65 entries
= new ColourPair
[size
];
74 void Palette::Release() {
78 entries
= new ColourPair
[size
];
81 // This method either adds a colour to the list of wanted colours (want==true)
82 // or retrieves the allocated colour back to the ColourPair.
83 // This is one method to make it easier to keep the code for wanting and retrieving in sync.
84 void Palette::WantFind(ColourPair
&cp
, bool want
) {
86 for (int i
=0; i
< used
; i
++) {
87 if (entries
[i
].desired
== cp
.desired
)
92 int sizeNew
= size
* 2;
93 ColourPair
*entriesNew
= new ColourPair
[sizeNew
];
94 for (int j
=0; j
<size
; j
++) {
95 entriesNew
[j
] = entries
[j
];
102 entries
[used
].desired
= cp
.desired
;
103 entries
[used
].allocated
.Set(cp
.desired
.AsLong());
106 for (int i
=0; i
< used
; i
++) {
107 if (entries
[i
].desired
== cp
.desired
) {
108 cp
.allocated
= entries
[i
].allocated
;
112 cp
.allocated
.Set(cp
.desired
.AsLong());
116 void Palette::Allocate(Window
&) {
117 if (allowRealization
) {
122 //----------------------------------------------------------------------
132 void Font::Create(const char *faceName
, int characterSet
, int size
, bool bold
, bool italic
, bool extraFontFlag
) {
136 // The minus one is done because since Scintilla uses SC_SHARSET_DEFAULT
137 // internally and we need to have wxFONENCODING_DEFAULT == SC_SHARSET_DEFAULT
138 // so we adjust the encoding before passing it to Scintilla. See also
139 // wxStyledTextCtrl::StyleSetCharacterSet
140 wxFontEncoding encoding
= (wxFontEncoding
)(characterSet
-1);
142 wxFontEncodingArray ea
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
146 wxFont
* font
= new wxFont(size
,
148 italic
? wxITALIC
: wxNORMAL
,
149 bold
? wxBOLD
: wxNORMAL
,
153 font
->SetNoAntiAliasing(!extraFontFlag
);
158 void Font::Release() {
164 //----------------------------------------------------------------------
166 class SurfaceImpl
: public Surface
{
179 virtual void Init(WindowID wid
);
180 virtual void Init(SurfaceID sid
, WindowID wid
);
181 virtual void InitPixMap(int width
, int height
, Surface
*surface_
, WindowID wid
);
183 virtual void Release();
184 virtual bool Initialised();
185 virtual void PenColour(ColourAllocated fore
);
186 virtual int LogPixelsY();
187 virtual int DeviceHeightFont(int points
);
188 virtual void MoveTo(int x_
, int y_
);
189 virtual void LineTo(int x_
, int y_
);
190 virtual void Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
);
191 virtual void RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
192 virtual void FillRectangle(PRectangle rc
, ColourAllocated back
);
193 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
);
194 virtual void RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
195 virtual void AlphaRectangle(PRectangle rc
, int cornerSize
, ColourAllocated fill
, int alphaFill
,
196 ColourAllocated outline
, int alphaOutline
, int flags
);
197 virtual void Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
198 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
);
200 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
201 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
202 virtual void DrawTextTransparent(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
);
203 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, int *positions
);
204 virtual int WidthText(Font
&font_
, const char *s
, int len
);
205 virtual int WidthChar(Font
&font_
, char ch
);
206 virtual int Ascent(Font
&font_
);
207 virtual int Descent(Font
&font_
);
208 virtual int InternalLeading(Font
&font_
);
209 virtual int ExternalLeading(Font
&font_
);
210 virtual int Height(Font
&font_
);
211 virtual int AverageCharWidth(Font
&font_
);
213 virtual int SetPalette(Palette
*pal
, bool inBackGround
);
214 virtual void SetClip(PRectangle rc
);
215 virtual void FlushCachedState();
217 virtual void SetUnicodeMode(bool unicodeMode_
);
218 virtual void SetDBCSMode(int codePage
);
220 void BrushColour(ColourAllocated back
);
221 void SetFont(Font
&font_
);
226 SurfaceImpl::SurfaceImpl() :
227 hdc(0), hdcOwned(0), bitmap(0),
228 x(0), y(0), unicodeMode(0)
231 SurfaceImpl::~SurfaceImpl() {
235 void SurfaceImpl::Init(WindowID wid
) {
238 hdc
= new wxMemoryDC();
241 // On Mac and GTK the DC is not really valid until it has a bitmap
242 // selected into it. So instead of just creating the DC with no bitmap,
243 // go ahead and give it one.
244 InitPixMap(1,1,NULL
,wid
);
248 void SurfaceImpl::Init(SurfaceID hdc_
, WindowID
) {
253 void SurfaceImpl::InitPixMap(int width
, int height
, Surface
*WXUNUSED(surface_
), WindowID
) {
255 hdc
= new wxMemoryDC();
257 if (width
< 1) width
= 1;
258 if (height
< 1) height
= 1;
259 bitmap
= new wxBitmap(width
, height
);
260 ((wxMemoryDC
*)hdc
)->SelectObject(*bitmap
);
264 void SurfaceImpl::Release() {
266 ((wxMemoryDC
*)hdc
)->SelectObject(wxNullBitmap
);
278 bool SurfaceImpl::Initialised() {
283 void SurfaceImpl::PenColour(ColourAllocated fore
) {
284 hdc
->SetPen(wxPen(wxColourFromCA(fore
), 1, wxSOLID
));
287 void SurfaceImpl::BrushColour(ColourAllocated back
) {
288 hdc
->SetBrush(wxBrush(wxColourFromCA(back
), wxSOLID
));
291 void SurfaceImpl::SetFont(Font
&font_
) {
293 hdc
->SetFont(*((wxFont
*)font_
.GetID()));
297 int SurfaceImpl::LogPixelsY() {
298 return hdc
->GetPPI().y
;
301 int SurfaceImpl::DeviceHeightFont(int points
) {
305 void SurfaceImpl::MoveTo(int x_
, int y_
) {
310 void SurfaceImpl::LineTo(int x_
, int y_
) {
311 hdc
->DrawLine(x
,y
, x_
,y_
);
316 void SurfaceImpl::Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
) {
319 hdc
->DrawPolygon(npts
, (wxPoint
*)pts
);
322 void SurfaceImpl::RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
325 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
328 void SurfaceImpl::FillRectangle(PRectangle rc
, ColourAllocated back
) {
330 hdc
->SetPen(*wxTRANSPARENT_PEN
);
331 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
334 void SurfaceImpl::FillRectangle(PRectangle rc
, Surface
&surfacePattern
) {
336 if (((SurfaceImpl
&)surfacePattern
).bitmap
)
337 br
= wxBrush(*((SurfaceImpl
&)surfacePattern
).bitmap
);
338 else // Something is wrong so display in red
339 br
= wxBrush(*wxRED
, wxSOLID
);
340 hdc
->SetPen(*wxTRANSPARENT_PEN
);
342 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
345 void SurfaceImpl::RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
348 hdc
->DrawRoundedRectangle(wxRectFromPRectangle(rc
), 4);
352 #define wxPy_premultiply(p, a) ((p) * (a) / 0xff)
354 #define wxPy_premultiply(p, a) (p)
357 void SurfaceImpl::AlphaRectangle(PRectangle rc
, int cornerSize
,
358 ColourAllocated fill
, int alphaFill
,
359 ColourAllocated outline
, int alphaOutline
,
361 #ifdef wxHAVE_RAW_BITMAP
363 // TODO: do something with cornerSize
364 wxUnusedVar(cornerSize
);
367 wxRect r
= wxRectFromPRectangle(rc
);
368 wxBitmap
bmp(r
.width
, r
.height
, 32);
369 wxAlphaPixelData
pixData(bmp
);
372 // Set the fill pixels
373 ColourDesired
cdf(fill
.AsLong());
374 int red
= cdf
.GetRed();
375 int green
= cdf
.GetGreen();
376 int blue
= cdf
.GetBlue();
378 wxAlphaPixelData::Iterator
p(pixData
);
379 for (y
=0; y
<r
.height
; y
++) {
380 p
.MoveTo(pixData
, 0, y
);
381 for (x
=0; x
<r
.width
; x
++) {
382 p
.Red() = wxPy_premultiply(red
, alphaFill
);
383 p
.Green() = wxPy_premultiply(green
, alphaFill
);
384 p
.Blue() = wxPy_premultiply(blue
, alphaFill
);
385 p
.Alpha() = alphaFill
;
390 // Set the outline pixels
391 ColourDesired
cdo(outline
.AsLong());
393 green
= cdo
.GetGreen();
394 blue
= cdo
.GetBlue();
395 for (x
=0; x
<r
.width
; x
++) {
396 p
.MoveTo(pixData
, x
, 0);
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
;
401 p
.MoveTo(pixData
, x
, r
.height
-1);
402 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
403 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
404 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
405 p
.Alpha() = alphaOutline
;
408 for (y
=0; y
<r
.height
; y
++) {
409 p
.MoveTo(pixData
, 0, y
);
410 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
411 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
412 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
413 p
.Alpha() = alphaOutline
;
414 p
.MoveTo(pixData
, r
.width
-1, y
);
415 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
416 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
417 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
418 p
.Alpha() = alphaOutline
;
422 hdc
->DrawBitmap(bmp
, r
.x
, r
.y
, true);
425 wxUnusedVar(cornerSize
);
426 wxUnusedVar(alphaFill
);
427 wxUnusedVar(alphaOutline
);
428 RectangleDraw(rc
, outline
, fill
);
432 void SurfaceImpl::Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
435 hdc
->DrawEllipse(wxRectFromPRectangle(rc
));
438 void SurfaceImpl::Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
) {
439 wxRect r
= wxRectFromPRectangle(rc
);
440 hdc
->Blit(r
.x
, r
.y
, r
.width
, r
.height
,
441 ((SurfaceImpl
&)surfaceSource
).hdc
,
442 from
.x
, from
.y
, wxCOPY
);
445 void SurfaceImpl::DrawTextNoClip(PRectangle rc
, Font
&font
, int ybase
,
446 const char *s
, int len
,
447 ColourAllocated fore
, ColourAllocated back
) {
449 hdc
->SetTextForeground(wxColourFromCA(fore
));
450 hdc
->SetTextBackground(wxColourFromCA(back
));
451 FillRectangle(rc
, back
);
453 // ybase is where the baseline should be, but wxWin uses the upper left
454 // corner, so I need to calculate the real position for the text...
455 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
458 void SurfaceImpl::DrawTextClipped(PRectangle rc
, Font
&font
, int ybase
,
459 const char *s
, int len
,
460 ColourAllocated fore
, ColourAllocated back
) {
462 hdc
->SetTextForeground(wxColourFromCA(fore
));
463 hdc
->SetTextBackground(wxColourFromCA(back
));
464 FillRectangle(rc
, back
);
465 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
467 // see comments above
468 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
469 hdc
->DestroyClippingRegion();
473 void SurfaceImpl::DrawTextTransparent(PRectangle rc
, Font
&font
, int ybase
,
474 const char *s
, int len
,
475 ColourAllocated fore
) {
478 hdc
->SetTextForeground(wxColourFromCA(fore
));
479 hdc
->SetBackgroundMode(wxTRANSPARENT
);
481 // ybase is where the baseline should be, but wxWin uses the upper left
482 // corner, so I need to calculate the real position for the text...
483 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
485 hdc
->SetBackgroundMode(wxSOLID
);
489 void SurfaceImpl::MeasureWidths(Font
&font
, const char *s
, int len
, int *positions
) {
491 wxString str
= stc2wx(s
, len
);
496 hdc
->GetPartialTextExtents(str
, tpos
);
499 // Map the widths for UCS-2 characters back to the UTF-8 input string
500 // NOTE: I don't think this is right for when sizeof(wxChar) > 2, ie wxGTK2
501 // so figure it out and fix it!
504 while ((int)i
< len
) {
505 unsigned char uch
= (unsigned char)s
[i
];
506 positions
[i
++] = tpos
[ui
];
508 if (uch
< (0x80 + 0x40 + 0x20)) {
509 positions
[i
++] = tpos
[ui
];
511 positions
[i
++] = tpos
[ui
];
512 positions
[i
++] = tpos
[ui
];
519 // If not unicode then just use the widths we have
521 std::copy(tpos
.begin(), tpos
.end(), positions
);
523 memcpy(positions
, tpos
.begin(), len
* sizeof(int));
529 int SurfaceImpl::WidthText(Font
&font
, const char *s
, int len
) {
534 hdc
->GetTextExtent(stc2wx(s
, len
), &w
, &h
);
539 int SurfaceImpl::WidthChar(Font
&font
, char ch
) {
543 char s
[2] = { ch
, 0 };
545 hdc
->GetTextExtent(stc2wx(s
, 1), &w
, &h
);
549 #define EXTENT_TEST wxT(" `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
551 int SurfaceImpl::Ascent(Font
&font
) {
554 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
559 int SurfaceImpl::Descent(Font
&font
) {
562 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
566 int SurfaceImpl::InternalLeading(Font
&WXUNUSED(font
)) {
570 int SurfaceImpl::ExternalLeading(Font
&font
) {
573 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
577 int SurfaceImpl::Height(Font
&font
) {
579 return hdc
->GetCharHeight() + 1;
582 int SurfaceImpl::AverageCharWidth(Font
&font
) {
584 return hdc
->GetCharWidth();
587 int SurfaceImpl::SetPalette(Palette
*WXUNUSED(pal
), bool WXUNUSED(inBackGround
)) {
591 void SurfaceImpl::SetClip(PRectangle rc
) {
592 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
595 void SurfaceImpl::FlushCachedState() {
598 void SurfaceImpl::SetUnicodeMode(bool unicodeMode_
) {
599 unicodeMode
=unicodeMode_
;
602 void SurfaceImpl::SetDBCSMode(int WXUNUSED(codePage
)) {
603 // dbcsMode = codePage == SC_CP_DBCS;
607 Surface
*Surface::Allocate() {
608 return new SurfaceImpl
;
612 //----------------------------------------------------------------------
615 inline wxWindow
* GETWIN(WindowID id
) { return (wxWindow
*)id
; }
620 void Window::Destroy() {
623 GETWIN(id
)->Destroy();
628 bool Window::HasFocus() {
629 return wxWindow::FindFocus() == GETWIN(id
);
632 PRectangle
Window::GetPosition() {
633 if (! id
) return PRectangle();
634 wxRect
rc(GETWIN(id
)->GetPosition(), GETWIN(id
)->GetSize());
635 return PRectangleFromwxRect(rc
);
638 void Window::SetPosition(PRectangle rc
) {
639 wxRect r
= wxRectFromPRectangle(rc
);
640 GETWIN(id
)->SetSize(r
);
643 void Window::SetPositionRelative(PRectangle rc
, Window
) {
644 SetPosition(rc
); // ????
647 PRectangle
Window::GetClientPosition() {
648 if (! id
) return PRectangle();
649 wxSize sz
= GETWIN(id
)->GetClientSize();
650 return PRectangle(0, 0, sz
.x
, sz
.y
);
653 void Window::Show(bool show
) {
654 GETWIN(id
)->Show(show
);
657 void Window::InvalidateAll() {
658 GETWIN(id
)->Refresh(false);
661 void Window::InvalidateRectangle(PRectangle rc
) {
662 wxRect r
= wxRectFromPRectangle(rc
);
663 GETWIN(id
)->Refresh(false, &r
);
666 void Window::SetFont(Font
&font
) {
667 GETWIN(id
)->SetFont(*((wxFont
*)font
.GetID()));
670 void Window::SetCursor(Cursor curs
) {
675 cursorId
= wxCURSOR_IBEAM
;
678 cursorId
= wxCURSOR_ARROW
;
681 cursorId
= wxCURSOR_ARROW
; // ** no up arrow... wxCURSOR_UPARROW;
684 cursorId
= wxCURSOR_WAIT
;
687 cursorId
= wxCURSOR_SIZEWE
;
690 cursorId
= wxCURSOR_SIZENS
;
692 case cursorReverseArrow
:
693 cursorId
= wxCURSOR_RIGHT_ARROW
;
696 cursorId
= wxCURSOR_HAND
;
699 cursorId
= wxCURSOR_ARROW
;
703 wxCursor wc
= wxStockCursor(cursorId
) ;
705 wxCursor wc
= wxCursor(cursorId
) ;
707 if(curs
!= cursorLast
)
709 GETWIN(id
)->SetCursor(wc
);
715 void Window::SetTitle(const char *s
) {
716 GETWIN(id
)->SetLabel(stc2wx(s
));
720 //----------------------------------------------------------------------
721 // Helper classes for ListBox
724 // This is a simple subclass of wxListView that just resets focus to the
725 // parent when it gets it.
726 class wxSTCListBox
: public wxListView
{
728 wxSTCListBox(wxWindow
* parent
, wxWindowID id
,
729 const wxPoint
& pos
, const wxSize
& size
,
734 Hide(); // don't flicker as we move it around...
736 Create(parent
, id
, pos
, size
, style
);
740 void OnFocus(wxFocusEvent
& event
) {
741 GetParent()->SetFocus();
745 void OnKillFocus(wxFocusEvent
& WXUNUSED(event
)) {
746 // Do nothing. Prevents base class from resetting the colors...
750 // For some reason I don't understand yet the focus doesn't really leave
751 // the listbox like it should, so if we get any events feed them back to
753 void OnKeyDown(wxKeyEvent
& event
) {
754 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
756 void OnChar(wxKeyEvent
& event
) {
757 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
760 // And we need to force the focus back when being destroyed
762 GetGrandParent()->SetFocus();
767 DECLARE_EVENT_TABLE()
770 BEGIN_EVENT_TABLE(wxSTCListBox
, wxListView
)
771 EVT_SET_FOCUS( wxSTCListBox::OnFocus
)
772 EVT_KILL_FOCUS(wxSTCListBox::OnKillFocus
)
774 EVT_KEY_DOWN( wxSTCListBox::OnKeyDown
)
775 EVT_CHAR( wxSTCListBox::OnChar
)
781 #if wxUSE_POPUPWIN //-----------------------------------
782 #include <wx/popupwin.h>
786 // TODO: Refactor these two classes to have a common base (or a mix-in) to get
787 // rid of the code duplication. (Either that or convince somebody to
788 // implement wxPopupWindow for the Mac!!)
790 // In the meantime, be careful to duplicate any changes as needed...
793 // A popup window to place the wxSTCListBox upon
794 class wxSTCListBoxWin
: public wxPopupWindow
798 CallBackAction doubleClickAction
;
799 void* doubleClickActionData
;
801 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point
WXUNUSED(location
)) :
802 wxPopupWindow(parent
, wxBORDER_NONE
)
805 SetBackgroundColour(*wxBLACK
); // for our simple border
807 lv
= new wxSTCListBox(parent
, id
, wxPoint(-50,-50), wxDefaultSize
,
808 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxBORDER_NONE
);
809 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
810 lv
->InsertColumn(0, wxEmptyString
);
811 lv
->InsertColumn(1, wxEmptyString
);
813 // NOTE: We need to fool the wxListView into thinking that it has the
814 // focus so it will use the normal selection colour and will look
815 // "right" to the user. But since the wxPopupWindow or its children
816 // can't receive focus then we have to pull a fast one and temporarily
817 // parent the listctrl on the STC window and then call SetFocus and
818 // then reparent it back to the popup.
827 // Set position in client coords
828 virtual void DoSetSize(int x
, int y
,
829 int width
, int height
,
830 int sizeFlags
= wxSIZE_AUTO
) {
831 if (x
!= wxDefaultCoord
) {
832 GetParent()->ClientToScreen(&x
, NULL
);
834 if (y
!= wxDefaultCoord
) {
835 GetParent()->ClientToScreen(NULL
, &y
);
837 wxPopupWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
840 // return position as if it were in client coords
841 virtual void DoGetPosition( int *x
, int *y
) const {
843 wxPopupWindow::DoGetPosition(&sx
, &sy
);
844 GetParent()->ScreenToClient(&sx
, &sy
);
851 if ( !wxPendingDelete
.Member(this) )
852 wxPendingDelete
.Append(this);
858 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
861 il
->GetSize(0, w
, h
);
868 void SetDoubleClickAction(CallBackAction action
, void *data
) {
869 doubleClickAction
= action
;
870 doubleClickActionData
= data
;
874 void OnFocus(wxFocusEvent
& event
) {
875 GetParent()->SetFocus();
879 void OnSize(wxSizeEvent
& event
) {
881 wxSize sz
= GetSize();
884 lv
->SetSize(1, 1, sz
.x
, sz
.y
);
885 // reset the column widths
886 lv
->SetColumnWidth(0, IconWidth()+4);
887 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
888 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
892 void OnActivate(wxListEvent
& WXUNUSED(event
)) {
893 doubleClickAction(doubleClickActionData
);
896 wxListView
* GetLB() { return lv
; }
899 DECLARE_EVENT_TABLE()
903 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxPopupWindow
)
904 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
905 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
906 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
911 #else // !wxUSE_POPUPWIN -----------------------------------
913 // A normal window to place the wxSTCListBox upon, but make it behave as much
914 // like a wxPopupWindow as possible
915 class wxSTCListBoxWin
: public wxFrame
{
918 CallBackAction doubleClickAction
;
919 void* doubleClickActionData
;
921 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point location
) :
922 wxFrame(parent
, id
, wxEmptyString
, wxPoint(location
.x
, location
.y
), wxSize(0,0),
924 | wxFRAME_FLOAT_ON_PARENT
934 lv
= new wxSTCListBox(this, id
, wxDefaultPosition
, wxDefaultSize
,
935 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxNO_BORDER
);
936 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
937 lv
->InsertColumn(0, wxEmptyString
);
938 lv
->InsertColumn(1, wxEmptyString
);
940 // Eventhough we immediately reset the focus to the parent, this helps
941 // things to look right...
948 // On OSX and (possibly others) there can still be pending
949 // messages/events for the list control when Scintilla wants to
950 // close it, so do a pending delete of it instead of destroying
955 // The bottom edge of this window is not getting properly
956 // refreshed upon deletion, so help it out...
957 wxWindow
* p
= GetParent();
958 wxRect
r(GetPosition(), GetSize());
959 r
.SetHeight(r
.GetHeight()+1);
960 p
->Refresh(false, &r
);
962 if ( !wxPendingDelete
.Member(this) )
963 wxPendingDelete
.Append(this);
970 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
973 il
->GetSize(0, w
, h
);
980 void SetDoubleClickAction(CallBackAction action
, void *data
)
982 doubleClickAction
= action
;
983 doubleClickActionData
= data
;
987 void OnFocus(wxFocusEvent
& event
)
990 GetParent()->SetFocus();
994 void OnSize(wxSizeEvent
& event
)
997 wxSize sz
= GetClientSize();
999 // reset the column widths
1000 lv
->SetColumnWidth(0, IconWidth()+4);
1001 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
1002 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
1006 void ActivateParent()
1008 // Although we're a frame, we always want the parent to be active, so
1009 // raise it whenever we get shown, focused, etc.
1010 wxTopLevelWindow
*frame
= wxDynamicCast(
1011 wxGetTopLevelParent(GetParent()), wxTopLevelWindow
);
1017 virtual void DoSetSize(int x
, int y
,
1018 int width
, int height
,
1019 int sizeFlags
= wxSIZE_AUTO
)
1021 // convert coords to screen coords since we're a top-level window
1022 if (x
!= wxDefaultCoord
) {
1023 GetParent()->ClientToScreen(&x
, NULL
);
1025 if (y
!= wxDefaultCoord
) {
1026 GetParent()->ClientToScreen(NULL
, &y
);
1028 wxFrame::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1031 virtual bool Show(bool show
= true)
1033 bool rv
= wxFrame::Show(show
);
1037 GetParent()->Refresh(false);
1042 void OnActivate(wxListEvent
& WXUNUSED(event
))
1044 doubleClickAction(doubleClickActionData
);
1047 wxListView
* GetLB() { return lv
; }
1050 DECLARE_EVENT_TABLE()
1054 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxWindow
)
1055 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
1056 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
1057 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
1060 #endif // wxUSE_POPUPWIN -----------------------------------
1063 inline wxSTCListBoxWin
* GETLBW(WindowID win
) {
1064 return ((wxSTCListBoxWin
*)win
);
1067 inline wxListView
* GETLB(WindowID win
) {
1068 return GETLBW(win
)->GetLB();
1071 //----------------------------------------------------------------------
1073 class ListBoxImpl
: public ListBox
{
1077 int desiredVisibleRows
;
1080 Point location
; // Caret location at which the list is opened
1081 wxImageList
* imgList
;
1082 wxArrayInt
* imgTypeMap
;
1088 virtual void SetFont(Font
&font
);
1089 virtual void Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
);
1090 virtual void SetAverageCharWidth(int width
);
1091 virtual void SetVisibleRows(int rows
);
1092 virtual int GetVisibleRows() const;
1093 virtual PRectangle
GetDesiredRect();
1094 virtual int CaretFromEdge();
1095 virtual void Clear();
1096 virtual void Append(char *s
, int type
= -1);
1097 void Append(const wxString
& text
, int type
);
1098 virtual int Length();
1099 virtual void Select(int n
);
1100 virtual int GetSelection();
1101 virtual int Find(const char *prefix
);
1102 virtual void GetValue(int n
, char *value
, int len
);
1103 virtual void RegisterImage(int type
, const char *xpm_data
);
1104 virtual void ClearRegisteredImages();
1105 virtual void SetDoubleClickAction(CallBackAction
, void *);
1106 virtual void SetList(const char* list
, char separator
, char typesep
);
1110 ListBoxImpl::ListBoxImpl()
1111 : lineHeight(10), unicodeMode(false),
1112 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
1113 imgList(NULL
), imgTypeMap(NULL
)
1117 ListBoxImpl::~ListBoxImpl() {
1129 void ListBoxImpl::SetFont(Font
&font
) {
1130 GETLB(id
)->SetFont(*((wxFont
*)font
.GetID()));
1134 void ListBoxImpl::Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
) {
1135 location
= location_
;
1136 lineHeight
= lineHeight_
;
1137 unicodeMode
= unicodeMode_
;
1139 id
= new wxSTCListBoxWin(GETWIN(parent
.GetID()), ctrlID
, location
);
1140 if (imgList
!= NULL
)
1141 GETLB(id
)->SetImageList(imgList
, wxIMAGE_LIST_SMALL
);
1145 void ListBoxImpl::SetAverageCharWidth(int width
) {
1146 aveCharWidth
= width
;
1150 void ListBoxImpl::SetVisibleRows(int rows
) {
1151 desiredVisibleRows
= rows
;
1155 int ListBoxImpl::GetVisibleRows() const {
1156 return desiredVisibleRows
;
1159 PRectangle
ListBoxImpl::GetDesiredRect() {
1160 // wxListCtrl doesn't have a DoGetBestSize, so instead we kept track of
1161 // the max size in Append and calculate it here...
1162 int maxw
= maxStrWidth
* aveCharWidth
;
1165 // give it a default if there are no lines, and/or add a bit more
1166 if (maxw
== 0) maxw
= 100;
1167 maxw
+= aveCharWidth
* 3 +
1168 GETLBW(id
)->IconWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1172 // estimate a desired height
1173 int count
= GETLB(id
)->GetItemCount();
1176 GETLB(id
)->GetItemRect(0, rect
);
1177 maxh
= count
* rect
.GetHeight();
1178 if (maxh
> 140) // TODO: Use desiredVisibleRows??
1181 // Try to make the size an exact multiple of some number of lines
1182 int lines
= maxh
/ rect
.GetHeight();
1183 maxh
= (lines
+ 1) * rect
.GetHeight() + 2;
1197 int ListBoxImpl::CaretFromEdge() {
1198 return 4 + GETLBW(id
)->IconWidth();
1202 void ListBoxImpl::Clear() {
1203 GETLB(id
)->DeleteAllItems();
1207 void ListBoxImpl::Append(char *s
, int type
) {
1208 Append(stc2wx(s
), type
);
1211 void ListBoxImpl::Append(const wxString
& text
, int type
) {
1212 long count
= GETLB(id
)->GetItemCount();
1213 long itemID
= GETLB(id
)->InsertItem(count
, wxEmptyString
);
1214 GETLB(id
)->SetItem(itemID
, 1, text
);
1215 maxStrWidth
= wxMax(maxStrWidth
, text
.length());
1217 wxCHECK_RET(imgTypeMap
, wxT("Unexpected NULL imgTypeMap"));
1218 long idx
= imgTypeMap
->Item(type
);
1219 GETLB(id
)->SetItemImage(itemID
, idx
, idx
);
1223 void ListBoxImpl::SetList(const char* list
, char separator
, char typesep
) {
1224 GETLB(id
)->Freeze();
1226 wxStringTokenizer
tkzr(stc2wx(list
), (wxChar
)separator
);
1227 while ( tkzr
.HasMoreTokens() ) {
1228 wxString token
= tkzr
.GetNextToken();
1230 int pos
= token
.Find(typesep
);
1232 token
.Mid(pos
+1).ToLong(&type
);
1233 token
.Truncate(pos
);
1235 Append(token
, (int)type
);
1241 int ListBoxImpl::Length() {
1242 return GETLB(id
)->GetItemCount();
1246 void ListBoxImpl::Select(int n
) {
1252 GETLB(id
)->EnsureVisible(n
);
1253 GETLB(id
)->Select(n
, select
);
1257 int ListBoxImpl::GetSelection() {
1258 return GETLB(id
)->GetFirstSelected();
1262 int ListBoxImpl::Find(const char *WXUNUSED(prefix
)) {
1268 void ListBoxImpl::GetValue(int n
, char *value
, int len
) {
1272 item
.SetMask(wxLIST_MASK_TEXT
);
1273 GETLB(id
)->GetItem(item
);
1274 strncpy(value
, wx2stc(item
.GetText()), len
);
1275 value
[len
-1] = '\0';
1279 void ListBoxImpl::RegisterImage(int type
, const char *xpm_data
) {
1280 wxMemoryInputStream
stream(xpm_data
, strlen(xpm_data
)+1);
1281 wxImage
img(stream
, wxBITMAP_TYPE_XPM
);
1285 // assumes all images are the same size
1286 imgList
= new wxImageList(bmp
.GetWidth(), bmp
.GetHeight(), true);
1287 imgTypeMap
= new wxArrayInt
;
1290 int idx
= imgList
->Add(bmp
);
1292 // do we need to extend the mapping array?
1293 wxArrayInt
& itm
= *imgTypeMap
;
1294 if ( itm
.GetCount() < (size_t)type
+1)
1295 itm
.Add(-1, type
- itm
.GetCount() + 1);
1297 // Add an item that maps type to the image index
1301 void ListBoxImpl::ClearRegisteredImages() {
1311 GETLB(id
)->SetImageList(NULL
, wxIMAGE_LIST_SMALL
);
1315 void ListBoxImpl::SetDoubleClickAction(CallBackAction action
, void *data
) {
1316 GETLBW(id
)->SetDoubleClickAction(action
, data
);
1320 ListBox::ListBox() {
1323 ListBox::~ListBox() {
1326 ListBox
*ListBox::Allocate() {
1327 return new ListBoxImpl();
1330 //----------------------------------------------------------------------
1332 Menu::Menu() : id(0) {
1335 void Menu::CreatePopUp() {
1340 void Menu::Destroy() {
1346 void Menu::Show(Point pt
, Window
&w
) {
1347 GETWIN(w
.GetID())->PopupMenu((wxMenu
*)id
, pt
.x
- 4, pt
.y
);
1351 //----------------------------------------------------------------------
1353 DynamicLibrary
*DynamicLibrary::Load(const char *WXUNUSED(modulePath
)) {
1354 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1358 //----------------------------------------------------------------------
1360 ColourDesired
Platform::Chrome() {
1362 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1363 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1366 ColourDesired
Platform::ChromeHighlight() {
1368 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
);
1369 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1372 const char *Platform::DefaultFont() {
1373 static char buf
[128];
1374 strcpy(buf
, wxNORMAL_FONT
->GetFaceName().mbc_str());
1378 int Platform::DefaultFontSize() {
1379 return wxNORMAL_FONT
->GetPointSize();
1382 unsigned int Platform::DoubleClickTime() {
1383 return 500; // **** ::GetDoubleClickTime();
1386 bool Platform::MouseButtonBounce() {
1390 bool Platform::IsKeyDown(int WXUNUSED(key
)) {
1391 return false; // I don't think we'll need this.
1394 long Platform::SendScintilla(WindowID w
,
1396 unsigned long wParam
,
1399 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1400 return stc
->SendMsg(msg
, wParam
, lParam
);
1403 long Platform::SendScintillaPointer(WindowID w
,
1405 unsigned long wParam
,
1408 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1409 return stc
->SendMsg(msg
, wParam
, (long)lParam
);
1413 // These are utility functions not really tied to a platform
1415 int Platform::Minimum(int a
, int b
) {
1422 int Platform::Maximum(int a
, int b
) {
1431 void Platform::DebugDisplay(const char *s
) {
1433 wxLogDebug(stc2wx(s
));
1439 void Platform::DebugPrintf(const char *format
, ...) {
1443 va_start(pArguments
, format
);
1444 vsprintf(buffer
,format
,pArguments
);
1446 Platform::DebugDisplay(buffer
);
1448 wxUnusedVar(format
);
1453 static bool assertionPopUps
= true;
1455 bool Platform::ShowAssertionPopUps(bool assertionPopUps_
) {
1456 bool ret
= assertionPopUps
;
1457 assertionPopUps
= assertionPopUps_
;
1461 void Platform::Assert(const char *c
, const char *file
, int line
) {
1464 sprintf(buffer
, "Assertion [%s] failed at %s %d", c
, file
, line
);
1465 if (assertionPopUps
) {
1467 wxMessageBox(stc2wx(buffer
),
1468 wxT("Assertion failure"),
1469 wxICON_HAND
| wxOK
);
1471 strcat(buffer
, "\r\n");
1472 Platform::DebugDisplay(buffer
);
1483 int Platform::Clamp(int val
, int minVal
, int maxVal
) {
1492 bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage
), char WXUNUSED(ch
)) {
1496 int Platform::DBCSCharLength(int WXUNUSED(codePage
), const char *WXUNUSED(s
)) {
1500 int Platform::DBCSCharMaxLength() {
1505 //----------------------------------------------------------------------
1507 ElapsedTime::ElapsedTime() {
1508 wxLongLong localTime
= wxGetLocalTimeMillis();
1509 littleBit
= localTime
.GetLo();
1510 bigBit
= localTime
.GetHi();
1513 double ElapsedTime::Duration(bool reset
) {
1514 wxLongLong
prevTime(bigBit
, littleBit
);
1515 wxLongLong localTime
= wxGetLocalTimeMillis();
1517 littleBit
= localTime
.GetLo();
1518 bigBit
= localTime
.GetHi();
1520 wxLongLong duration
= localTime
- prevTime
;
1521 double result
= duration
.ToDouble();
1527 //----------------------------------------------------------------------
1531 #include "UniConversion.h"
1533 // Convert using Scintilla's functions instead of wx's, Scintilla's are more
1534 // forgiving and won't assert...
1536 wxString
stc2wx(const char* str
, size_t len
)
1539 return wxEmptyString
;
1541 size_t wclen
= UCS2Length(str
, len
);
1542 wxWCharBuffer
buffer(wclen
+1);
1544 size_t actualLen
= UCS2FromUTF8(str
, len
, buffer
.data(), wclen
+1);
1545 return wxString(buffer
.data(), actualLen
);
1550 wxString
stc2wx(const char* str
)
1552 return stc2wx(str
, strlen(str
));
1556 const wxWX2MBbuf
wx2stc(const wxString
& str
)
1558 const wchar_t* wcstr
= str
.c_str();
1559 size_t wclen
= str
.length();
1560 size_t len
= UTF8Length(wcstr
, wclen
);
1562 wxCharBuffer
buffer(len
+1);
1563 UTF8FromUCS2(wcstr
, wclen
, buffer
.data(), len
);
1565 // TODO check NULL termination!!