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".
18 #include "wx/dcmemory.h"
19 #include "wx/settings.h"
25 #include "wx/display.h"
28 #include "wx/encconv.h"
29 #include "wx/listctrl.h"
30 #include "wx/mstream.h"
32 #include "wx/imaglist.h"
33 #include "wx/tokenzr.h"
35 #ifdef wxHAS_RAW_BITMAP
36 #include "wx/rawbmp.h"
38 #if wxUSE_GRAPHICS_CONTEXT
39 #include "wx/dcgraph.h"
44 #include "wx/stc/stc.h"
45 #include "wx/stc/private.h"
49 Point
Point::FromLong(long lpoint
) {
50 return Point(lpoint
& 0xFFFF, lpoint
>> 16);
53 wxRect
wxRectFromPRectangle(PRectangle prc
) {
54 wxRect
r(prc
.left
, prc
.top
,
55 prc
.Width(), prc
.Height());
59 PRectangle
PRectangleFromwxRect(wxRect rc
) {
60 return PRectangle(rc
.GetLeft(), rc
.GetTop(),
61 rc
.GetRight()+1, rc
.GetBottom()+1);
64 wxColour
wxColourFromCA(const ColourAllocated
& ca
) {
65 ColourDesired
cd(ca
.AsLong());
66 return wxColour((unsigned char)cd
.GetRed(),
67 (unsigned char)cd
.GetGreen(),
68 (unsigned char)cd
.GetBlue());
71 wxColour
wxColourFromCAandAlpha(const ColourAllocated
& ca
, int alpha
) {
72 ColourDesired
cd(ca
.AsLong());
73 return wxColour((unsigned char)cd
.GetRed(),
74 (unsigned char)cd
.GetGreen(),
75 (unsigned char)cd
.GetBlue(),
76 (unsigned char)alpha
);
79 //----------------------------------------------------------------------
83 allowRealization
= false;
85 entries
= new ColourPair
[size
];
94 void Palette::Release() {
98 entries
= new ColourPair
[size
];
101 // This method either adds a colour to the list of wanted colours (want==true)
102 // or retrieves the allocated colour back to the ColourPair.
103 // This is one method to make it easier to keep the code for wanting and retrieving in sync.
104 void Palette::WantFind(ColourPair
&cp
, bool want
) {
106 for (int i
=0; i
< used
; i
++) {
107 if (entries
[i
].desired
== cp
.desired
)
112 int sizeNew
= size
* 2;
113 ColourPair
*entriesNew
= new ColourPair
[sizeNew
];
114 for (int j
=0; j
<size
; j
++) {
115 entriesNew
[j
] = entries
[j
];
118 entries
= entriesNew
;
122 entries
[used
].desired
= cp
.desired
;
123 entries
[used
].allocated
.Set(cp
.desired
.AsLong());
126 for (int i
=0; i
< used
; i
++) {
127 if (entries
[i
].desired
== cp
.desired
) {
128 cp
.allocated
= entries
[i
].allocated
;
132 cp
.allocated
.Set(cp
.desired
.AsLong());
136 void Palette::Allocate(Window
&) {
137 if (allowRealization
) {
142 //----------------------------------------------------------------------
152 void Font::Create(const char *faceName
, int characterSet
,
153 int size
, bool bold
, bool italic
,
154 bool WXUNUSED(extraFontFlag
)) {
157 // The minus one is done because since Scintilla uses SC_SHARSET_DEFAULT
158 // internally and we need to have wxFONENCODING_DEFAULT == SC_SHARSET_DEFAULT
159 // so we adjust the encoding before passing it to Scintilla. See also
160 // wxStyledTextCtrl::StyleSetCharacterSet
161 wxFontEncoding encoding
= (wxFontEncoding
)(characterSet
-1);
163 wxFontEncodingArray ea
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
167 wxFont
* font
= new wxFont(size
,
169 italic
? wxITALIC
: wxNORMAL
,
170 bold
? wxBOLD
: wxNORMAL
,
178 void Font::Release() {
184 //----------------------------------------------------------------------
186 class SurfaceImpl
: public Surface
{
199 virtual void Init(WindowID wid
);
200 virtual void Init(SurfaceID sid
, WindowID wid
);
201 virtual void InitPixMap(int width
, int height
, Surface
*surface_
, WindowID wid
);
203 virtual void Release();
204 virtual bool Initialised();
205 virtual void PenColour(ColourAllocated fore
);
206 virtual int LogPixelsY();
207 virtual int DeviceHeightFont(int points
);
208 virtual void MoveTo(int x_
, int y_
);
209 virtual void LineTo(int x_
, int y_
);
210 virtual void Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
);
211 virtual void RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
212 virtual void FillRectangle(PRectangle rc
, ColourAllocated back
);
213 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
);
214 virtual void RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
215 virtual void AlphaRectangle(PRectangle rc
, int cornerSize
, ColourAllocated fill
, int alphaFill
,
216 ColourAllocated outline
, int alphaOutline
, int flags
);
217 virtual void Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
218 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
);
220 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
221 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
222 virtual void DrawTextTransparent(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
);
223 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, int *positions
);
224 virtual int WidthText(Font
&font_
, const char *s
, int len
);
225 virtual int WidthChar(Font
&font_
, char ch
);
226 virtual int Ascent(Font
&font_
);
227 virtual int Descent(Font
&font_
);
228 virtual int InternalLeading(Font
&font_
);
229 virtual int ExternalLeading(Font
&font_
);
230 virtual int Height(Font
&font_
);
231 virtual int AverageCharWidth(Font
&font_
);
233 virtual int SetPalette(Palette
*pal
, bool inBackGround
);
234 virtual void SetClip(PRectangle rc
);
235 virtual void FlushCachedState();
237 virtual void SetUnicodeMode(bool unicodeMode_
);
238 virtual void SetDBCSMode(int codePage
);
240 void BrushColour(ColourAllocated back
);
241 void SetFont(Font
&font_
);
246 SurfaceImpl::SurfaceImpl() :
247 hdc(0), hdcOwned(0), bitmap(0),
248 x(0), y(0), unicodeMode(0)
251 SurfaceImpl::~SurfaceImpl() {
255 void SurfaceImpl::Init(WindowID wid
) {
258 hdc
= new wxMemoryDC();
261 // On Mac and GTK the DC is not really valid until it has a bitmap
262 // selected into it. So instead of just creating the DC with no bitmap,
263 // go ahead and give it one.
264 InitPixMap(1,1,NULL
,wid
);
268 void SurfaceImpl::Init(SurfaceID hdc_
, WindowID
) {
273 void SurfaceImpl::InitPixMap(int width
, int height
, Surface
*WXUNUSED(surface_
), WindowID
) {
275 hdc
= new wxMemoryDC();
277 if (width
< 1) width
= 1;
278 if (height
< 1) height
= 1;
279 bitmap
= new wxBitmap(width
, height
);
280 ((wxMemoryDC
*)hdc
)->SelectObject(*bitmap
);
284 void SurfaceImpl::Release() {
286 ((wxMemoryDC
*)hdc
)->SelectObject(wxNullBitmap
);
298 bool SurfaceImpl::Initialised() {
303 void SurfaceImpl::PenColour(ColourAllocated fore
) {
304 hdc
->SetPen(wxPen(wxColourFromCA(fore
)));
307 void SurfaceImpl::BrushColour(ColourAllocated back
) {
308 hdc
->SetBrush(wxBrush(wxColourFromCA(back
)));
311 void SurfaceImpl::SetFont(Font
&font_
) {
313 hdc
->SetFont(*((wxFont
*)font_
.GetID()));
317 int SurfaceImpl::LogPixelsY() {
318 return hdc
->GetPPI().y
;
321 int SurfaceImpl::DeviceHeightFont(int points
) {
325 void SurfaceImpl::MoveTo(int x_
, int y_
) {
330 void SurfaceImpl::LineTo(int x_
, int y_
) {
331 hdc
->DrawLine(x
,y
, x_
,y_
);
336 void SurfaceImpl::Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
) {
339 hdc
->DrawPolygon(npts
, (wxPoint
*)pts
);
342 void SurfaceImpl::RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
345 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
348 void SurfaceImpl::FillRectangle(PRectangle rc
, ColourAllocated back
) {
350 hdc
->SetPen(*wxTRANSPARENT_PEN
);
351 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
354 void SurfaceImpl::FillRectangle(PRectangle rc
, Surface
&surfacePattern
) {
356 if (((SurfaceImpl
&)surfacePattern
).bitmap
)
357 br
= wxBrush(*((SurfaceImpl
&)surfacePattern
).bitmap
);
358 else // Something is wrong so display in red
359 br
= wxBrush(*wxRED
);
360 hdc
->SetPen(*wxTRANSPARENT_PEN
);
362 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
365 void SurfaceImpl::RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
368 hdc
->DrawRoundedRectangle(wxRectFromPRectangle(rc
), 4);
372 #define wxPy_premultiply(p, a) ((p) * (a) / 0xff)
374 #define wxPy_premultiply(p, a) (p)
377 void SurfaceImpl::AlphaRectangle(PRectangle rc
, int cornerSize
,
378 ColourAllocated fill
, int alphaFill
,
379 ColourAllocated outline
, int alphaOutline
,
381 #if wxUSE_GRAPHICS_CONTEXT
382 wxGCDC
dc(*(wxMemoryDC
*)hdc
);
383 wxColour
penColour(wxColourFromCAandAlpha(outline
, alphaOutline
));
384 wxColour
brushColour(wxColourFromCAandAlpha(fill
, alphaFill
));
385 dc
.SetPen(wxPen(penColour
));
386 dc
.SetBrush(wxBrush(brushColour
));
387 dc
.DrawRoundedRectangle(wxRectFromPRectangle(rc
), cornerSize
);
391 #ifdef wxHAS_RAW_BITMAP
393 // TODO: do something with cornerSize
394 wxUnusedVar(cornerSize
);
397 wxRect r
= wxRectFromPRectangle(rc
);
398 wxBitmap
bmp(r
.width
, r
.height
, 32);
399 wxAlphaPixelData
pixData(bmp
);
401 // Set the fill pixels
402 ColourDesired
cdf(fill
.AsLong());
403 int red
= cdf
.GetRed();
404 int green
= cdf
.GetGreen();
405 int blue
= cdf
.GetBlue();
407 wxAlphaPixelData::Iterator
p(pixData
);
408 for (y
=0; y
<r
.height
; y
++) {
409 p
.MoveTo(pixData
, 0, y
);
410 for (x
=0; x
<r
.width
; x
++) {
411 p
.Red() = wxPy_premultiply(red
, alphaFill
);
412 p
.Green() = wxPy_premultiply(green
, alphaFill
);
413 p
.Blue() = wxPy_premultiply(blue
, alphaFill
);
414 p
.Alpha() = alphaFill
;
419 // Set the outline pixels
420 ColourDesired
cdo(outline
.AsLong());
422 green
= cdo
.GetGreen();
423 blue
= cdo
.GetBlue();
424 for (x
=0; x
<r
.width
; x
++) {
425 p
.MoveTo(pixData
, x
, 0);
426 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
427 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
428 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
429 p
.Alpha() = alphaOutline
;
430 p
.MoveTo(pixData
, x
, r
.height
-1);
431 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
432 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
433 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
434 p
.Alpha() = alphaOutline
;
437 for (y
=0; y
<r
.height
; y
++) {
438 p
.MoveTo(pixData
, 0, y
);
439 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
440 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
441 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
442 p
.Alpha() = alphaOutline
;
443 p
.MoveTo(pixData
, r
.width
-1, y
);
444 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
445 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
446 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
447 p
.Alpha() = alphaOutline
;
451 hdc
->DrawBitmap(bmp
, r
.x
, r
.y
, true);
454 wxUnusedVar(cornerSize
);
455 wxUnusedVar(alphaFill
);
456 wxUnusedVar(alphaOutline
);
457 RectangleDraw(rc
, outline
, fill
);
462 void SurfaceImpl::Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
465 hdc
->DrawEllipse(wxRectFromPRectangle(rc
));
468 void SurfaceImpl::Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
) {
469 wxRect r
= wxRectFromPRectangle(rc
);
470 hdc
->Blit(r
.x
, r
.y
, r
.width
, r
.height
,
471 ((SurfaceImpl
&)surfaceSource
).hdc
,
472 from
.x
, from
.y
, wxCOPY
);
475 void SurfaceImpl::DrawTextNoClip(PRectangle rc
, Font
&font
, int ybase
,
476 const char *s
, int len
,
477 ColourAllocated fore
, ColourAllocated back
) {
479 hdc
->SetTextForeground(wxColourFromCA(fore
));
480 hdc
->SetTextBackground(wxColourFromCA(back
));
481 FillRectangle(rc
, back
);
483 // ybase is where the baseline should be, but wxWin uses the upper left
484 // corner, so I need to calculate the real position for the text...
485 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
488 void SurfaceImpl::DrawTextClipped(PRectangle rc
, Font
&font
, int ybase
,
489 const char *s
, int len
,
490 ColourAllocated fore
, ColourAllocated back
) {
492 hdc
->SetTextForeground(wxColourFromCA(fore
));
493 hdc
->SetTextBackground(wxColourFromCA(back
));
494 FillRectangle(rc
, back
);
495 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
497 // see comments above
498 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
499 hdc
->DestroyClippingRegion();
503 void SurfaceImpl::DrawTextTransparent(PRectangle rc
, Font
&font
, int ybase
,
504 const char *s
, int len
,
505 ColourAllocated fore
) {
508 hdc
->SetTextForeground(wxColourFromCA(fore
));
509 hdc
->SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
511 // ybase is where the baseline should be, but wxWin uses the upper left
512 // corner, so I need to calculate the real position for the text...
513 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
515 hdc
->SetBackgroundMode(wxBRUSHSTYLE_SOLID
);
519 void SurfaceImpl::MeasureWidths(Font
&font
, const char *s
, int len
, int *positions
) {
521 wxString str
= stc2wx(s
, len
);
526 hdc
->GetPartialTextExtents(str
, tpos
);
529 // Map the widths for UCS-2 characters back to the UTF-8 input string
530 // NOTE: I don't think this is right for when sizeof(wxChar) > 2, ie wxGTK2
531 // so figure it out and fix it!
534 while ((int)i
< len
) {
535 unsigned char uch
= (unsigned char)s
[i
];
536 positions
[i
++] = tpos
[ui
];
538 if (uch
< (0x80 + 0x40 + 0x20)) {
539 positions
[i
++] = tpos
[ui
];
541 positions
[i
++] = tpos
[ui
];
542 positions
[i
++] = tpos
[ui
];
549 // If not unicode then just use the widths we have
551 std::copy(tpos
.begin(), tpos
.end(), positions
);
553 memcpy(positions
, tpos
.begin(), len
* sizeof(int));
559 int SurfaceImpl::WidthText(Font
&font
, const char *s
, int len
) {
564 hdc
->GetTextExtent(stc2wx(s
, len
), &w
, &h
);
569 int SurfaceImpl::WidthChar(Font
&font
, char ch
) {
573 char s
[2] = { ch
, 0 };
575 hdc
->GetTextExtent(stc2wx(s
, 1), &w
, &h
);
579 #define EXTENT_TEST wxT(" `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
581 int SurfaceImpl::Ascent(Font
&font
) {
584 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
589 int SurfaceImpl::Descent(Font
&font
) {
592 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
596 int SurfaceImpl::InternalLeading(Font
&WXUNUSED(font
)) {
600 int SurfaceImpl::ExternalLeading(Font
&font
) {
603 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
607 int SurfaceImpl::Height(Font
&font
) {
609 return hdc
->GetCharHeight() + 1;
612 int SurfaceImpl::AverageCharWidth(Font
&font
) {
614 return hdc
->GetCharWidth();
617 int SurfaceImpl::SetPalette(Palette
*WXUNUSED(pal
), bool WXUNUSED(inBackGround
)) {
621 void SurfaceImpl::SetClip(PRectangle rc
) {
622 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
625 void SurfaceImpl::FlushCachedState() {
628 void SurfaceImpl::SetUnicodeMode(bool unicodeMode_
) {
629 unicodeMode
=unicodeMode_
;
632 void SurfaceImpl::SetDBCSMode(int WXUNUSED(codePage
)) {
633 // dbcsMode = codePage == SC_CP_DBCS;
637 Surface
*Surface::Allocate() {
638 return new SurfaceImpl
;
642 //----------------------------------------------------------------------
645 inline wxWindow
* GETWIN(WindowID id
) { return (wxWindow
*)id
; }
650 void Window::Destroy() {
653 GETWIN(id
)->Destroy();
658 bool Window::HasFocus() {
659 return wxWindow::FindFocus() == GETWIN(id
);
662 PRectangle
Window::GetPosition() {
663 if (! id
) return PRectangle();
664 wxRect
rc(GETWIN(id
)->GetPosition(), GETWIN(id
)->GetSize());
665 return PRectangleFromwxRect(rc
);
668 void Window::SetPosition(PRectangle rc
) {
669 wxRect r
= wxRectFromPRectangle(rc
);
670 GETWIN(id
)->SetSize(r
);
673 void Window::SetPositionRelative(PRectangle rc
, Window
) {
674 SetPosition(rc
); // ????
677 PRectangle
Window::GetClientPosition() {
678 if (! id
) return PRectangle();
679 wxSize sz
= GETWIN(id
)->GetClientSize();
680 return PRectangle(0, 0, sz
.x
, sz
.y
);
683 void Window::Show(bool show
) {
684 GETWIN(id
)->Show(show
);
687 void Window::InvalidateAll() {
688 GETWIN(id
)->Refresh(false);
691 void Window::InvalidateRectangle(PRectangle rc
) {
692 wxRect r
= wxRectFromPRectangle(rc
);
693 GETWIN(id
)->Refresh(false, &r
);
696 void Window::SetFont(Font
&font
) {
697 GETWIN(id
)->SetFont(*((wxFont
*)font
.GetID()));
700 void Window::SetCursor(Cursor curs
) {
701 wxStockCursor cursorId
;
705 cursorId
= wxCURSOR_IBEAM
;
708 cursorId
= wxCURSOR_ARROW
;
711 cursorId
= wxCURSOR_ARROW
; // ** no up arrow... wxCURSOR_UPARROW;
714 cursorId
= wxCURSOR_WAIT
;
717 cursorId
= wxCURSOR_SIZEWE
;
720 cursorId
= wxCURSOR_SIZENS
;
722 case cursorReverseArrow
:
723 cursorId
= wxCURSOR_RIGHT_ARROW
;
726 cursorId
= wxCURSOR_HAND
;
729 cursorId
= wxCURSOR_ARROW
;
733 wxCursor wc
= wxCursor(cursorId
);
734 if(curs
!= cursorLast
)
736 GETWIN(id
)->SetCursor(wc
);
742 void Window::SetTitle(const char *s
) {
743 GETWIN(id
)->SetLabel(stc2wx(s
));
747 // Returns rectangle of monitor pt is on
748 PRectangle
Window::GetMonitorRect(Point pt
) {
750 if (! id
) return PRectangle();
752 // Get the display the point is found on
753 int n
= wxDisplay::GetFromPoint(wxPoint(pt
.x
, pt
.y
));
754 wxDisplay
dpy(n
== wxNOT_FOUND
? 0 : n
);
755 rect
= dpy
.GetGeometry();
759 return PRectangleFromwxRect(rect
);
762 //----------------------------------------------------------------------
763 // Helper classes for ListBox
766 // This is a simple subclass of wxListView that just resets focus to the
767 // parent when it gets it.
768 class wxSTCListBox
: public wxListView
{
770 wxSTCListBox(wxWindow
* parent
, wxWindowID id
,
771 const wxPoint
& pos
, const wxSize
& size
,
776 Hide(); // don't flicker as we move it around...
778 Create(parent
, id
, pos
, size
, style
);
782 void OnFocus(wxFocusEvent
& event
) {
783 GetParent()->SetFocus();
787 void OnKillFocus(wxFocusEvent
& WXUNUSED(event
)) {
788 // Do nothing. Prevents base class from resetting the colors...
792 // For some reason I don't understand yet the focus doesn't really leave
793 // the listbox like it should, so if we get any events feed them back to
795 void OnKeyDown(wxKeyEvent
& event
) {
796 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
798 void OnChar(wxKeyEvent
& event
) {
799 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
802 // And we need to force the focus back when being destroyed
804 GetGrandParent()->SetFocus();
809 DECLARE_EVENT_TABLE()
812 BEGIN_EVENT_TABLE(wxSTCListBox
, wxListView
)
813 EVT_SET_FOCUS( wxSTCListBox::OnFocus
)
814 EVT_KILL_FOCUS(wxSTCListBox::OnKillFocus
)
816 EVT_KEY_DOWN( wxSTCListBox::OnKeyDown
)
817 EVT_CHAR( wxSTCListBox::OnChar
)
823 #if wxUSE_POPUPWIN //-----------------------------------
824 #include "wx/popupwin.h"
828 // TODO: Refactor these two classes to have a common base (or a mix-in) to get
829 // rid of the code duplication. (Either that or convince somebody to
830 // implement wxPopupWindow for the Mac!!)
832 // In the meantime, be careful to duplicate any changes as needed...
835 // A popup window to place the wxSTCListBox upon
836 class wxSTCListBoxWin
: public wxPopupWindow
840 CallBackAction doubleClickAction
;
841 void* doubleClickActionData
;
843 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point
WXUNUSED(location
)) :
844 wxPopupWindow(parent
, wxBORDER_NONE
)
847 SetBackgroundColour(*wxBLACK
); // for our simple border
849 lv
= new wxSTCListBox(parent
, id
, wxPoint(-50,-50), wxDefaultSize
,
850 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxBORDER_NONE
);
851 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
852 lv
->InsertColumn(0, wxEmptyString
);
853 lv
->InsertColumn(1, wxEmptyString
);
855 // NOTE: We need to fool the wxListView into thinking that it has the
856 // focus so it will use the normal selection colour and will look
857 // "right" to the user. But since the wxPopupWindow or its children
858 // can't receive focus then we have to pull a fast one and temporarily
859 // parent the listctrl on the STC window and then call SetFocus and
860 // then reparent it back to the popup.
869 // Set position in client coords
870 virtual void DoSetSize(int x
, int y
,
871 int width
, int height
,
872 int sizeFlags
= wxSIZE_AUTO
) {
873 if (x
!= wxDefaultCoord
) {
874 GetParent()->ClientToScreen(&x
, NULL
);
876 if (y
!= wxDefaultCoord
) {
877 GetParent()->ClientToScreen(NULL
, &y
);
879 wxPopupWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
882 // return position as if it were in client coords
883 virtual void DoGetPosition( int *x
, int *y
) const {
885 wxPopupWindow::DoGetPosition(&sx
, &sy
);
886 GetParent()->ScreenToClient(&sx
, &sy
);
893 if ( !wxPendingDelete
.Member(this) )
894 wxPendingDelete
.Append(this);
900 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
903 il
->GetSize(0, w
, h
);
910 void SetDoubleClickAction(CallBackAction action
, void *data
) {
911 doubleClickAction
= action
;
912 doubleClickActionData
= data
;
916 void OnFocus(wxFocusEvent
& event
) {
917 GetParent()->SetFocus();
921 void OnSize(wxSizeEvent
& event
) {
923 wxSize sz
= GetSize();
926 lv
->SetSize(1, 1, sz
.x
, sz
.y
);
927 // reset the column widths
928 lv
->SetColumnWidth(0, IconWidth()+4);
929 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
930 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
934 void OnActivate(wxListEvent
& WXUNUSED(event
)) {
935 doubleClickAction(doubleClickActionData
);
938 wxListView
* GetLB() { return lv
; }
941 DECLARE_EVENT_TABLE()
945 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxPopupWindow
)
946 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
947 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
948 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
953 #else // !wxUSE_POPUPWIN -----------------------------------
954 #include "wx/frame.h"
956 // A normal window to place the wxSTCListBox upon, but make it behave as much
957 // like a wxPopupWindow as possible
958 class wxSTCListBoxWin
: public wxFrame
{
961 CallBackAction doubleClickAction
;
962 void* doubleClickActionData
;
964 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point location
) :
965 wxFrame(parent
, id
, wxEmptyString
, wxPoint(location
.x
, location
.y
), wxSize(0,0),
967 | wxFRAME_FLOAT_ON_PARENT
977 lv
= new wxSTCListBox(this, id
, wxDefaultPosition
, wxDefaultSize
,
978 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxNO_BORDER
);
979 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
980 lv
->InsertColumn(0, wxEmptyString
);
981 lv
->InsertColumn(1, wxEmptyString
);
983 // Eventhough we immediately reset the focus to the parent, this helps
984 // things to look right...
991 // On OSX and (possibly others) there can still be pending
992 // messages/events for the list control when Scintilla wants to
993 // close it, so do a pending delete of it instead of destroying
998 // The bottom edge of this window is not getting properly
999 // refreshed upon deletion, so help it out...
1000 wxWindow
* p
= GetParent();
1001 wxRect
r(GetPosition(), GetSize());
1002 r
.SetHeight(r
.GetHeight()+1);
1003 p
->Refresh(false, &r
);
1005 if ( !wxPendingDelete
.Member(this) )
1006 wxPendingDelete
.Append(this);
1013 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
1016 il
->GetSize(0, w
, h
);
1023 void SetDoubleClickAction(CallBackAction action
, void *data
)
1025 doubleClickAction
= action
;
1026 doubleClickActionData
= data
;
1030 void OnFocus(wxFocusEvent
& event
)
1033 GetParent()->SetFocus();
1037 void OnSize(wxSizeEvent
& event
)
1040 wxSize sz
= GetClientSize();
1042 // reset the column widths
1043 lv
->SetColumnWidth(0, IconWidth()+4);
1044 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
1045 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
1049 void ActivateParent()
1051 // Although we're a frame, we always want the parent to be active, so
1052 // raise it whenever we get shown, focused, etc.
1053 wxTopLevelWindow
*frame
= wxDynamicCast(
1054 wxGetTopLevelParent(GetParent()), wxTopLevelWindow
);
1060 virtual void DoSetSize(int x
, int y
,
1061 int width
, int height
,
1062 int sizeFlags
= wxSIZE_AUTO
)
1064 // convert coords to screen coords since we're a top-level window
1065 if (x
!= wxDefaultCoord
) {
1066 GetParent()->ClientToScreen(&x
, NULL
);
1068 if (y
!= wxDefaultCoord
) {
1069 GetParent()->ClientToScreen(NULL
, &y
);
1071 wxFrame::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1074 virtual bool Show(bool show
= true)
1076 bool rv
= wxFrame::Show(show
);
1080 GetParent()->Refresh(false);
1085 void OnActivate(wxListEvent
& WXUNUSED(event
))
1087 doubleClickAction(doubleClickActionData
);
1090 wxListView
* GetLB() { return lv
; }
1093 DECLARE_EVENT_TABLE()
1097 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxWindow
)
1098 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
1099 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
1100 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
1103 #endif // wxUSE_POPUPWIN -----------------------------------
1106 inline wxSTCListBoxWin
* GETLBW(WindowID win
) {
1107 return ((wxSTCListBoxWin
*)win
);
1110 inline wxListView
* GETLB(WindowID win
) {
1111 return GETLBW(win
)->GetLB();
1114 //----------------------------------------------------------------------
1116 class ListBoxImpl
: public ListBox
{
1120 int desiredVisibleRows
;
1123 Point location
; // Caret location at which the list is opened
1124 wxImageList
* imgList
;
1125 wxArrayInt
* imgTypeMap
;
1131 virtual void SetFont(Font
&font
);
1132 virtual void Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
);
1133 virtual void SetAverageCharWidth(int width
);
1134 virtual void SetVisibleRows(int rows
);
1135 virtual int GetVisibleRows() const;
1136 virtual PRectangle
GetDesiredRect();
1137 virtual int CaretFromEdge();
1138 virtual void Clear();
1139 virtual void Append(char *s
, int type
= -1);
1140 void Append(const wxString
& text
, int type
);
1141 virtual int Length();
1142 virtual void Select(int n
);
1143 virtual int GetSelection();
1144 virtual int Find(const char *prefix
);
1145 virtual void GetValue(int n
, char *value
, int len
);
1146 virtual void RegisterImage(int type
, const char *xpm_data
);
1147 virtual void ClearRegisteredImages();
1148 virtual void SetDoubleClickAction(CallBackAction
, void *);
1149 virtual void SetList(const char* list
, char separator
, char typesep
);
1153 ListBoxImpl::ListBoxImpl()
1154 : lineHeight(10), unicodeMode(false),
1155 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
1156 imgList(NULL
), imgTypeMap(NULL
)
1160 ListBoxImpl::~ListBoxImpl() {
1172 void ListBoxImpl::SetFont(Font
&font
) {
1173 GETLB(id
)->SetFont(*((wxFont
*)font
.GetID()));
1177 void ListBoxImpl::Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
) {
1178 location
= location_
;
1179 lineHeight
= lineHeight_
;
1180 unicodeMode
= unicodeMode_
;
1182 id
= new wxSTCListBoxWin(GETWIN(parent
.GetID()), ctrlID
, location
);
1183 if (imgList
!= NULL
)
1184 GETLB(id
)->SetImageList(imgList
, wxIMAGE_LIST_SMALL
);
1188 void ListBoxImpl::SetAverageCharWidth(int width
) {
1189 aveCharWidth
= width
;
1193 void ListBoxImpl::SetVisibleRows(int rows
) {
1194 desiredVisibleRows
= rows
;
1198 int ListBoxImpl::GetVisibleRows() const {
1199 return desiredVisibleRows
;
1202 PRectangle
ListBoxImpl::GetDesiredRect() {
1203 // wxListCtrl doesn't have a DoGetBestSize, so instead we kept track of
1204 // the max size in Append and calculate it here...
1205 int maxw
= maxStrWidth
* aveCharWidth
;
1208 // give it a default if there are no lines, and/or add a bit more
1209 if (maxw
== 0) maxw
= 100;
1210 maxw
+= aveCharWidth
* 3 +
1211 GETLBW(id
)->IconWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1215 // estimate a desired height
1216 int count
= GETLB(id
)->GetItemCount();
1219 GETLB(id
)->GetItemRect(0, rect
);
1220 maxh
= count
* rect
.GetHeight();
1221 if (maxh
> 140) // TODO: Use desiredVisibleRows??
1224 // Try to make the size an exact multiple of some number of lines
1225 int lines
= maxh
/ rect
.GetHeight();
1226 maxh
= (lines
+ 1) * rect
.GetHeight() + 2;
1240 int ListBoxImpl::CaretFromEdge() {
1241 return 4 + GETLBW(id
)->IconWidth();
1245 void ListBoxImpl::Clear() {
1246 GETLB(id
)->DeleteAllItems();
1250 void ListBoxImpl::Append(char *s
, int type
) {
1251 Append(stc2wx(s
), type
);
1254 void ListBoxImpl::Append(const wxString
& text
, int type
) {
1255 long count
= GETLB(id
)->GetItemCount();
1256 long itemID
= GETLB(id
)->InsertItem(count
, wxEmptyString
);
1258 GETLB(id
)->SetItem(itemID
, 1, text
);
1259 maxStrWidth
= wxMax(maxStrWidth
, text
.length());
1261 wxCHECK_RET(imgTypeMap
, wxT("Unexpected NULL imgTypeMap"));
1262 idx
= imgTypeMap
->Item(type
);
1264 GETLB(id
)->SetItemImage(itemID
, idx
, idx
);
1267 void ListBoxImpl::SetList(const char* list
, char separator
, char typesep
) {
1268 GETLB(id
)->Freeze();
1270 wxStringTokenizer
tkzr(stc2wx(list
), (wxChar
)separator
);
1271 while ( tkzr
.HasMoreTokens() ) {
1272 wxString token
= tkzr
.GetNextToken();
1274 int pos
= token
.Find(typesep
);
1276 token
.Mid(pos
+1).ToLong(&type
);
1277 token
.Truncate(pos
);
1279 Append(token
, (int)type
);
1285 int ListBoxImpl::Length() {
1286 return GETLB(id
)->GetItemCount();
1290 void ListBoxImpl::Select(int n
) {
1296 GETLB(id
)->EnsureVisible(n
);
1297 GETLB(id
)->Select(n
, select
);
1301 int ListBoxImpl::GetSelection() {
1302 return GETLB(id
)->GetFirstSelected();
1306 int ListBoxImpl::Find(const char *WXUNUSED(prefix
)) {
1312 void ListBoxImpl::GetValue(int n
, char *value
, int len
) {
1316 item
.SetMask(wxLIST_MASK_TEXT
);
1317 GETLB(id
)->GetItem(item
);
1318 strncpy(value
, wx2stc(item
.GetText()), len
);
1319 value
[len
-1] = '\0';
1323 void ListBoxImpl::RegisterImage(int type
, const char *xpm_data
) {
1324 wxMemoryInputStream
stream(xpm_data
, strlen(xpm_data
)+1);
1325 wxImage
img(stream
, wxBITMAP_TYPE_XPM
);
1329 // assumes all images are the same size
1330 imgList
= new wxImageList(bmp
.GetWidth(), bmp
.GetHeight(), true);
1331 imgTypeMap
= new wxArrayInt
;
1334 int idx
= imgList
->Add(bmp
);
1336 // do we need to extend the mapping array?
1337 wxArrayInt
& itm
= *imgTypeMap
;
1338 if ( itm
.GetCount() < (size_t)type
+1)
1339 itm
.Add(-1, type
- itm
.GetCount() + 1);
1341 // Add an item that maps type to the image index
1345 void ListBoxImpl::ClearRegisteredImages() {
1355 GETLB(id
)->SetImageList(NULL
, wxIMAGE_LIST_SMALL
);
1359 void ListBoxImpl::SetDoubleClickAction(CallBackAction action
, void *data
) {
1360 GETLBW(id
)->SetDoubleClickAction(action
, data
);
1364 ListBox::ListBox() {
1367 ListBox::~ListBox() {
1370 ListBox
*ListBox::Allocate() {
1371 return new ListBoxImpl();
1374 //----------------------------------------------------------------------
1376 Menu::Menu() : id(0) {
1379 void Menu::CreatePopUp() {
1384 void Menu::Destroy() {
1390 void Menu::Show(Point pt
, Window
&w
) {
1391 GETWIN(w
.GetID())->PopupMenu((wxMenu
*)id
, pt
.x
- 4, pt
.y
);
1395 //----------------------------------------------------------------------
1397 DynamicLibrary
*DynamicLibrary::Load(const char *WXUNUSED(modulePath
)) {
1398 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1402 //----------------------------------------------------------------------
1404 ColourDesired
Platform::Chrome() {
1406 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1407 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1410 ColourDesired
Platform::ChromeHighlight() {
1412 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
);
1413 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1416 const char *Platform::DefaultFont() {
1417 static char buf
[128];
1418 strcpy(buf
, wxNORMAL_FONT
->GetFaceName().mbc_str());
1422 int Platform::DefaultFontSize() {
1423 return wxNORMAL_FONT
->GetPointSize();
1426 unsigned int Platform::DoubleClickTime() {
1427 return 500; // **** ::GetDoubleClickTime();
1430 bool Platform::MouseButtonBounce() {
1434 bool Platform::IsKeyDown(int WXUNUSED(key
)) {
1435 return false; // I don't think we'll need this.
1438 long Platform::SendScintilla(WindowID w
,
1440 unsigned long wParam
,
1443 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1444 return stc
->SendMsg(msg
, wParam
, lParam
);
1447 long Platform::SendScintillaPointer(WindowID w
,
1449 unsigned long wParam
,
1452 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1453 return stc
->SendMsg(msg
, wParam
, (wxIntPtr
)lParam
);
1457 // These are utility functions not really tied to a platform
1459 int Platform::Minimum(int a
, int b
) {
1466 int Platform::Maximum(int a
, int b
) {
1475 void Platform::DebugDisplay(const char *s
) {
1477 wxLogDebug(stc2wx(s
));
1483 void Platform::DebugPrintf(const char *format
, ...) {
1487 va_start(pArguments
, format
);
1488 vsprintf(buffer
,format
,pArguments
);
1490 Platform::DebugDisplay(buffer
);
1492 wxUnusedVar(format
);
1497 static bool assertionPopUps
= true;
1499 bool Platform::ShowAssertionPopUps(bool assertionPopUps_
) {
1500 bool ret
= assertionPopUps
;
1501 assertionPopUps
= assertionPopUps_
;
1505 void Platform::Assert(const char *c
, const char *file
, int line
) {
1508 sprintf(buffer
, "Assertion [%s] failed at %s %d", c
, file
, line
);
1509 if (assertionPopUps
) {
1511 wxMessageBox(stc2wx(buffer
),
1512 wxT("Assertion failure"),
1513 wxICON_HAND
| wxOK
);
1515 strcat(buffer
, "\r\n");
1516 Platform::DebugDisplay(buffer
);
1527 int Platform::Clamp(int val
, int minVal
, int maxVal
) {
1536 bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage
), char WXUNUSED(ch
)) {
1540 int Platform::DBCSCharLength(int WXUNUSED(codePage
), const char *WXUNUSED(s
)) {
1544 int Platform::DBCSCharMaxLength() {
1549 //----------------------------------------------------------------------
1551 ElapsedTime::ElapsedTime() {
1552 wxLongLong localTime
= wxGetLocalTimeMillis();
1553 littleBit
= localTime
.GetLo();
1554 bigBit
= localTime
.GetHi();
1557 double ElapsedTime::Duration(bool reset
) {
1558 wxLongLong
prevTime(bigBit
, littleBit
);
1559 wxLongLong localTime
= wxGetLocalTimeMillis();
1561 littleBit
= localTime
.GetLo();
1562 bigBit
= localTime
.GetHi();
1564 wxLongLong duration
= localTime
- prevTime
;
1565 double result
= duration
.ToDouble();
1571 //----------------------------------------------------------------------
1575 #include "UniConversion.h"
1577 // Convert using Scintilla's functions instead of wx's, Scintilla's are more
1578 // forgiving and won't assert...
1580 wxString
stc2wx(const char* str
, size_t len
)
1583 return wxEmptyString
;
1585 size_t wclen
= UTF16Length(str
, len
);
1586 wxWCharBuffer
buffer(wclen
+1);
1588 size_t actualLen
= UTF16FromUTF8(str
, len
, buffer
.data(), wclen
+1);
1589 return wxString(buffer
.data(), actualLen
);
1594 wxString
stc2wx(const char* str
)
1596 return stc2wx(str
, strlen(str
));
1600 const wxWX2MBbuf
wx2stc(const wxString
& str
)
1602 const wchar_t* wcstr
= str
.c_str();
1603 size_t wclen
= str
.length();
1604 size_t len
= UTF8Length(wcstr
, wclen
);
1606 wxCharBuffer
buffer(len
+1);
1607 UTF8FromUTF16(wcstr
, wclen
, buffer
.data(), len
);
1609 // TODO check NULL termination!!