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"
48 Point
Point::FromLong(long lpoint
) {
49 return Point(lpoint
& 0xFFFF, lpoint
>> 16);
52 wxRect
wxRectFromPRectangle(PRectangle prc
) {
53 wxRect
r(prc
.left
, prc
.top
,
54 prc
.Width(), prc
.Height());
58 PRectangle
PRectangleFromwxRect(wxRect rc
) {
59 return PRectangle(rc
.GetLeft(), rc
.GetTop(),
60 rc
.GetRight()+1, rc
.GetBottom()+1);
63 wxColour
wxColourFromCD(ColourDesired
& cd
) {
64 return wxColour((unsigned char)cd
.GetRed(),
65 (unsigned char)cd
.GetGreen(),
66 (unsigned char)cd
.GetBlue());
69 wxColour
wxColourFromCDandAlpha(ColourDesired
& cd
, int alpha
) {
70 return wxColour((unsigned char)cd
.GetRed(),
71 (unsigned char)cd
.GetGreen(),
72 (unsigned char)cd
.GetBlue(),
73 (unsigned char)alpha
);
76 //----------------------------------------------------------------------
86 void Font::Create(const FontParameters
&fp
) {
89 // The minus one is done because since Scintilla uses SC_CHARSET_DEFAULT
90 // internally and we need to have wxFONENCODING_DEFAULT == SC_SHARSET_DEFAULT
91 // so we adjust the encoding before passing it to Scintilla. See also
92 // wxStyledTextCtrl::StyleSetCharacterSet
93 wxFontEncoding encoding
= (wxFontEncoding
)(fp
.characterSet
-1);
95 wxFontEncodingArray ea
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
100 if (fp
.weight
<= 300)
101 weight
= wxFONTWEIGHT_LIGHT
;
102 else if (fp
.weight
>= 700)
103 weight
= wxFONTWEIGHT_BOLD
;
105 weight
= wxFONTWEIGHT_NORMAL
;
107 wxFont
* font
= new wxFont(fp
.size
,
108 wxFONTFAMILY_DEFAULT
,
109 fp
.italic
? wxFONTSTYLE_ITALIC
: wxFONTSTYLE_NORMAL
,
118 void Font::Release() {
124 //----------------------------------------------------------------------
126 class SurfaceImpl
: public Surface
{
139 virtual void Init(WindowID wid
);
140 virtual void Init(SurfaceID sid
, WindowID wid
);
141 virtual void InitPixMap(int width
, int height
, Surface
*surface_
, WindowID wid
);
143 virtual void Release();
144 virtual bool Initialised();
145 virtual void PenColour(ColourDesired fore
);
146 virtual int LogPixelsY();
147 virtual int DeviceHeightFont(int points
);
148 virtual void MoveTo(int x_
, int y_
);
149 virtual void LineTo(int x_
, int y_
);
150 virtual void Polygon(Point
*pts
, int npts
, ColourDesired fore
, ColourDesired back
);
151 virtual void RectangleDraw(PRectangle rc
, ColourDesired fore
, ColourDesired back
);
152 virtual void FillRectangle(PRectangle rc
, ColourDesired back
);
153 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
);
154 virtual void RoundedRectangle(PRectangle rc
, ColourDesired fore
, ColourDesired back
);
155 virtual void AlphaRectangle(PRectangle rc
, int cornerSize
, ColourDesired fill
, int alphaFill
,
156 ColourDesired outline
, int alphaOutline
, int flags
);
157 virtual void DrawRGBAImage(PRectangle rc
, int width
, int height
,
158 const unsigned char *pixelsImage
);
159 virtual void Ellipse(PRectangle rc
, ColourDesired fore
, ColourDesired back
);
160 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
);
162 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, XYPOSITION ybase
, const char *s
, int len
, ColourDesired fore
, ColourDesired back
);
163 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, XYPOSITION ybase
, const char *s
, int len
, ColourDesired fore
, ColourDesired back
);
164 virtual void DrawTextTransparent(PRectangle rc
, Font
&font_
, XYPOSITION ybase
, const char *s
, int len
, ColourDesired fore
);
165 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, XYPOSITION
*positions
);
166 virtual XYPOSITION
WidthText(Font
&font_
, const char *s
, int len
);
167 virtual XYPOSITION
WidthChar(Font
&font_
, char ch
);
168 virtual XYPOSITION
Ascent(Font
&font_
);
169 virtual XYPOSITION
Descent(Font
&font_
);
170 virtual XYPOSITION
InternalLeading(Font
&font_
);
171 virtual XYPOSITION
ExternalLeading(Font
&font_
);
172 virtual XYPOSITION
Height(Font
&font_
);
173 virtual XYPOSITION
AverageCharWidth(Font
&font_
);
175 virtual void SetClip(PRectangle rc
);
176 virtual void FlushCachedState();
178 virtual void SetUnicodeMode(bool unicodeMode_
);
179 virtual void SetDBCSMode(int codePage
);
181 void BrushColour(ColourDesired back
);
182 void SetFont(Font
&font_
);
187 SurfaceImpl::SurfaceImpl() :
188 hdc(0), hdcOwned(0), bitmap(0),
189 x(0), y(0), unicodeMode(0)
192 SurfaceImpl::~SurfaceImpl() {
196 void SurfaceImpl::Init(WindowID wid
) {
199 hdc
= new wxMemoryDC();
202 // On Mac and GTK the DC is not really valid until it has a bitmap
203 // selected into it. So instead of just creating the DC with no bitmap,
204 // go ahead and give it one.
205 InitPixMap(1,1,NULL
,wid
);
209 void SurfaceImpl::Init(SurfaceID hdc_
, WindowID
) {
214 void SurfaceImpl::InitPixMap(int width
, int height
, Surface
*WXUNUSED(surface_
), WindowID
) {
216 hdc
= new wxMemoryDC();
218 if (width
< 1) width
= 1;
219 if (height
< 1) height
= 1;
220 bitmap
= new wxBitmap(width
, height
);
221 ((wxMemoryDC
*)hdc
)->SelectObject(*bitmap
);
225 void SurfaceImpl::Release() {
227 ((wxMemoryDC
*)hdc
)->SelectObject(wxNullBitmap
);
239 bool SurfaceImpl::Initialised() {
244 void SurfaceImpl::PenColour(ColourDesired fore
) {
245 hdc
->SetPen(wxPen(wxColourFromCD(fore
)));
248 void SurfaceImpl::BrushColour(ColourDesired back
) {
249 hdc
->SetBrush(wxBrush(wxColourFromCD(back
)));
252 void SurfaceImpl::SetFont(Font
&font_
) {
254 hdc
->SetFont(*((wxFont
*)font_
.GetID()));
258 int SurfaceImpl::LogPixelsY() {
259 return hdc
->GetPPI().y
;
262 int SurfaceImpl::DeviceHeightFont(int points
) {
266 void SurfaceImpl::MoveTo(int x_
, int y_
) {
271 void SurfaceImpl::LineTo(int x_
, int y_
) {
272 hdc
->DrawLine(x
,y
, x_
,y_
);
277 void SurfaceImpl::Polygon(Point
*pts
, int npts
, ColourDesired fore
, ColourDesired back
) {
280 hdc
->DrawPolygon(npts
, (wxPoint
*)pts
);
283 void SurfaceImpl::RectangleDraw(PRectangle rc
, ColourDesired fore
, ColourDesired back
) {
286 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
289 void SurfaceImpl::FillRectangle(PRectangle rc
, ColourDesired back
) {
291 hdc
->SetPen(*wxTRANSPARENT_PEN
);
292 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
295 void SurfaceImpl::FillRectangle(PRectangle rc
, Surface
&surfacePattern
) {
297 if (((SurfaceImpl
&)surfacePattern
).bitmap
)
298 br
= wxBrush(*((SurfaceImpl
&)surfacePattern
).bitmap
);
299 else // Something is wrong so display in red
300 br
= wxBrush(*wxRED
);
301 hdc
->SetPen(*wxTRANSPARENT_PEN
);
303 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
306 void SurfaceImpl::RoundedRectangle(PRectangle rc
, ColourDesired fore
, ColourDesired back
) {
309 hdc
->DrawRoundedRectangle(wxRectFromPRectangle(rc
), 4);
312 #if defined(__WXMSW__) || defined(__WXMAC__)
313 #define wxPy_premultiply(p, a) ((p) * (a) / 0xff)
315 #define wxPy_premultiply(p, a) (p)
318 void SurfaceImpl::AlphaRectangle(PRectangle rc
, int cornerSize
,
319 ColourDesired fill
, int alphaFill
,
320 ColourDesired outline
, int alphaOutline
,
322 #if wxUSE_GRAPHICS_CONTEXT
323 wxGCDC
dc(*(wxMemoryDC
*)hdc
);
324 wxColour
penColour(wxColourFromCDandAlpha(outline
, alphaOutline
));
325 wxColour
brushColour(wxColourFromCDandAlpha(fill
, alphaFill
));
326 dc
.SetPen(wxPen(penColour
));
327 dc
.SetBrush(wxBrush(brushColour
));
328 dc
.DrawRoundedRectangle(wxRectFromPRectangle(rc
), cornerSize
);
332 #ifdef wxHAS_RAW_BITMAP
334 // TODO: do something with cornerSize
335 wxUnusedVar(cornerSize
);
338 wxRect r
= wxRectFromPRectangle(rc
);
339 wxBitmap
bmp(r
.width
, r
.height
, 32);
341 // This block is needed to ensure that the changes done to the bitmap via
342 // pixel data object are committed before the bitmap is drawn.
344 wxAlphaPixelData
pixData(bmp
);
346 // Set the fill pixels
347 ColourDesired
cdf(fill
.AsLong());
348 int red
= cdf
.GetRed();
349 int green
= cdf
.GetGreen();
350 int blue
= cdf
.GetBlue();
352 wxAlphaPixelData::Iterator
p(pixData
);
353 for (y
=0; y
<r
.height
; y
++) {
354 p
.MoveTo(pixData
, 0, y
);
355 for (x
=0; x
<r
.width
; x
++) {
356 p
.Red() = wxPy_premultiply(red
, alphaFill
);
357 p
.Green() = wxPy_premultiply(green
, alphaFill
);
358 p
.Blue() = wxPy_premultiply(blue
, alphaFill
);
359 p
.Alpha() = alphaFill
;
364 // Set the outline pixels
365 ColourDesired
cdo(outline
.AsLong());
367 green
= cdo
.GetGreen();
368 blue
= cdo
.GetBlue();
369 for (x
=0; x
<r
.width
; x
++) {
370 p
.MoveTo(pixData
, x
, 0);
371 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
372 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
373 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
374 p
.Alpha() = alphaOutline
;
375 p
.MoveTo(pixData
, x
, r
.height
-1);
376 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
377 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
378 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
379 p
.Alpha() = alphaOutline
;
382 for (y
=0; y
<r
.height
; y
++) {
383 p
.MoveTo(pixData
, 0, y
);
384 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
385 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
386 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
387 p
.Alpha() = alphaOutline
;
388 p
.MoveTo(pixData
, r
.width
-1, y
);
389 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
390 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
391 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
392 p
.Alpha() = alphaOutline
;
397 hdc
->DrawBitmap(bmp
, r
.x
, r
.y
, true);
400 wxUnusedVar(cornerSize
);
401 wxUnusedVar(alphaFill
);
402 wxUnusedVar(alphaOutline
);
403 RectangleDraw(rc
, outline
, fill
);
408 #ifdef wxHAS_RAW_BITMAP
409 wxBitmap
BitmapFromRGBAImage(int width
, int height
, const unsigned char *pixelsImage
)
412 wxBitmap
bmp(width
, height
, 32);
413 wxAlphaPixelData
pixData(bmp
);
415 wxAlphaPixelData::Iterator
p(pixData
);
416 for (y
=0; y
<height
; y
++) {
417 p
.MoveTo(pixData
, 0, y
);
418 for (x
=0; x
<width
; x
++) {
419 unsigned char red
= *pixelsImage
++;
420 unsigned char green
= *pixelsImage
++;
421 unsigned char blue
= *pixelsImage
++;
422 unsigned char alpha
= *pixelsImage
++;
424 p
.Red() = wxPy_premultiply(red
, alpha
);
425 p
.Green() = wxPy_premultiply(green
, alpha
);
426 p
.Blue() = wxPy_premultiply(blue
, alpha
);
436 void SurfaceImpl::DrawRGBAImage(PRectangle rc
, int width
, int height
,
437 const unsigned char *pixelsImage
)
439 #ifdef wxHAS_RAW_BITMAP
440 wxRect r
= wxRectFromPRectangle(rc
);
441 wxBitmap bmp
= BitmapFromRGBAImage(width
, height
, pixelsImage
);
442 hdc
->DrawBitmap(bmp
, r
.x
, r
.y
, true);
447 void SurfaceImpl::Ellipse(PRectangle rc
, ColourDesired fore
, ColourDesired back
) {
450 hdc
->DrawEllipse(wxRectFromPRectangle(rc
));
453 void SurfaceImpl::Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
) {
454 wxRect r
= wxRectFromPRectangle(rc
);
455 hdc
->Blit(r
.x
, r
.y
, r
.width
, r
.height
,
456 ((SurfaceImpl
&)surfaceSource
).hdc
,
457 from
.x
, from
.y
, wxCOPY
);
460 void SurfaceImpl::DrawTextNoClip(PRectangle rc
, Font
&font
, XYPOSITION ybase
,
461 const char *s
, int len
,
462 ColourDesired fore
, ColourDesired back
) {
464 hdc
->SetTextForeground(wxColourFromCD(fore
));
465 hdc
->SetTextBackground(wxColourFromCD(back
));
466 FillRectangle(rc
, back
);
468 // ybase is where the baseline should be, but wxWin uses the upper left
469 // corner, so I need to calculate the real position for the text...
470 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
473 void SurfaceImpl::DrawTextClipped(PRectangle rc
, Font
&font
, XYPOSITION ybase
,
474 const char *s
, int len
,
475 ColourDesired fore
, ColourDesired back
) {
477 hdc
->SetTextForeground(wxColourFromCD(fore
));
478 hdc
->SetTextBackground(wxColourFromCD(back
));
479 FillRectangle(rc
, back
);
480 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
482 // see comments above
483 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
484 hdc
->DestroyClippingRegion();
488 void SurfaceImpl::DrawTextTransparent(PRectangle rc
, Font
&font
, XYPOSITION ybase
,
489 const char *s
, int len
,
490 ColourDesired fore
) {
493 hdc
->SetTextForeground(wxColourFromCD(fore
));
494 hdc
->SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
496 // ybase is where the baseline should be, but wxWin uses the upper left
497 // corner, so I need to calculate the real position for the text...
498 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
500 hdc
->SetBackgroundMode(wxBRUSHSTYLE_SOLID
);
504 void SurfaceImpl::MeasureWidths(Font
&font
, const char *s
, int len
, XYPOSITION
*positions
) {
506 wxString str
= stc2wx(s
, len
);
511 hdc
->GetPartialTextExtents(str
, tpos
);
514 // Map the widths for UCS-2 characters back to the UTF-8 input string
515 // NOTE: I don't think this is right for when sizeof(wxChar) > 2, ie wxGTK2
516 // so figure it out and fix it!
519 while ((int)i
< len
) {
520 unsigned char uch
= (unsigned char)s
[i
];
521 positions
[i
++] = tpos
[ui
];
523 if (uch
< (0x80 + 0x40 + 0x20)) {
524 positions
[i
++] = tpos
[ui
];
526 positions
[i
++] = tpos
[ui
];
527 positions
[i
++] = tpos
[ui
];
534 // If not unicode then just use the widths we have
535 #if wxUSE_STD_CONTAINERS
536 std::copy(tpos
.begin(), tpos
.end(), positions
);
538 memcpy(positions
, tpos
.begin(), len
* sizeof(int));
544 XYPOSITION
SurfaceImpl::WidthText(Font
&font
, const char *s
, int len
) {
549 hdc
->GetTextExtent(stc2wx(s
, len
), &w
, &h
);
554 XYPOSITION
SurfaceImpl::WidthChar(Font
&font
, char ch
) {
558 char s
[2] = { ch
, 0 };
560 hdc
->GetTextExtent(stc2wx(s
, 1), &w
, &h
);
564 #define EXTENT_TEST wxT(" `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
566 XYPOSITION
SurfaceImpl::Ascent(Font
&font
) {
569 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
574 XYPOSITION
SurfaceImpl::Descent(Font
&font
) {
577 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
581 XYPOSITION
SurfaceImpl::InternalLeading(Font
&WXUNUSED(font
)) {
585 XYPOSITION
SurfaceImpl::ExternalLeading(Font
&font
) {
588 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
592 XYPOSITION
SurfaceImpl::Height(Font
&font
) {
594 return hdc
->GetCharHeight() + 1;
597 XYPOSITION
SurfaceImpl::AverageCharWidth(Font
&font
) {
599 return hdc
->GetCharWidth();
602 void SurfaceImpl::SetClip(PRectangle rc
) {
603 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
606 void SurfaceImpl::FlushCachedState() {
609 void SurfaceImpl::SetUnicodeMode(bool unicodeMode_
) {
610 unicodeMode
=unicodeMode_
;
613 void SurfaceImpl::SetDBCSMode(int WXUNUSED(codePage
)) {
614 // dbcsMode = codePage == SC_CP_DBCS;
618 Surface
*Surface::Allocate(int WXUNUSED(technology
)) {
619 return new SurfaceImpl
;
623 //----------------------------------------------------------------------
626 inline wxWindow
* GETWIN(WindowID id
) { return (wxWindow
*)id
; }
631 void Window::Destroy() {
634 GETWIN(wid
)->Destroy();
639 bool Window::HasFocus() {
640 return wxWindow::FindFocus() == GETWIN(wid
);
643 PRectangle
Window::GetPosition() {
644 if (! wid
) return PRectangle();
645 wxRect
rc(GETWIN(wid
)->GetPosition(), GETWIN(wid
)->GetSize());
646 return PRectangleFromwxRect(rc
);
649 void Window::SetPosition(PRectangle rc
) {
650 wxRect r
= wxRectFromPRectangle(rc
);
651 GETWIN(wid
)->SetSize(r
);
654 void Window::SetPositionRelative(PRectangle rc
, Window
) {
655 SetPosition(rc
); // ????
658 PRectangle
Window::GetClientPosition() {
659 if (! wid
) return PRectangle();
660 wxSize sz
= GETWIN(wid
)->GetClientSize();
661 return PRectangle(0, 0, sz
.x
, sz
.y
);
664 void Window::Show(bool show
) {
665 GETWIN(wid
)->Show(show
);
668 void Window::InvalidateAll() {
669 GETWIN(wid
)->Refresh(false);
672 void Window::InvalidateRectangle(PRectangle rc
) {
673 wxRect r
= wxRectFromPRectangle(rc
);
674 GETWIN(wid
)->Refresh(false, &r
);
677 void Window::SetFont(Font
&font
) {
678 GETWIN(wid
)->SetFont(*((wxFont
*)font
.GetID()));
681 void Window::SetCursor(Cursor curs
) {
682 wxStockCursor cursorId
;
686 cursorId
= wxCURSOR_IBEAM
;
689 cursorId
= wxCURSOR_ARROW
;
692 cursorId
= wxCURSOR_ARROW
; // ** no up arrow... wxCURSOR_UPARROW;
695 cursorId
= wxCURSOR_WAIT
;
698 cursorId
= wxCURSOR_SIZEWE
;
701 cursorId
= wxCURSOR_SIZENS
;
703 case cursorReverseArrow
:
704 cursorId
= wxCURSOR_RIGHT_ARROW
;
707 cursorId
= wxCURSOR_HAND
;
710 cursorId
= wxCURSOR_ARROW
;
714 wxCursor wc
= wxCursor(cursorId
);
715 if(curs
!= cursorLast
)
717 GETWIN(wid
)->SetCursor(wc
);
723 void Window::SetTitle(const char *s
) {
724 GETWIN(wid
)->SetLabel(stc2wx(s
));
728 // Returns rectangle of monitor pt is on
729 PRectangle
Window::GetMonitorRect(Point pt
) {
731 if (! wid
) return PRectangle();
733 // Get the display the point is found on
734 int n
= wxDisplay::GetFromPoint(wxPoint(pt
.x
, pt
.y
));
735 wxDisplay
dpy(n
== wxNOT_FOUND
? 0 : n
);
736 rect
= dpy
.GetGeometry();
740 return PRectangleFromwxRect(rect
);
743 //----------------------------------------------------------------------
744 // Helper classes for ListBox
747 // This is a simple subclass of wxListView that just resets focus to the
748 // parent when it gets it.
749 class wxSTCListBox
: public wxListView
{
751 wxSTCListBox(wxWindow
* parent
, wxWindowID id
,
752 const wxPoint
& pos
, const wxSize
& size
,
757 Hide(); // don't flicker as we move it around...
759 Create(parent
, id
, pos
, size
, style
);
763 void OnFocus(wxFocusEvent
& event
) {
764 GetParent()->SetFocus();
768 void OnKillFocus(wxFocusEvent
& WXUNUSED(event
)) {
769 // Do nothing. Prevents base class from resetting the colors...
773 // For some reason I don't understand yet the focus doesn't really leave
774 // the listbox like it should, so if we get any events feed them back to
776 void OnKeyDown(wxKeyEvent
& event
) {
777 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
779 void OnChar(wxKeyEvent
& event
) {
780 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
783 // And we need to force the focus back when being destroyed
785 GetGrandParent()->SetFocus();
790 DECLARE_EVENT_TABLE()
793 BEGIN_EVENT_TABLE(wxSTCListBox
, wxListView
)
794 EVT_SET_FOCUS( wxSTCListBox::OnFocus
)
795 EVT_KILL_FOCUS(wxSTCListBox::OnKillFocus
)
797 EVT_KEY_DOWN( wxSTCListBox::OnKeyDown
)
798 EVT_CHAR( wxSTCListBox::OnChar
)
804 #if wxUSE_POPUPWIN //-----------------------------------
805 #include "wx/popupwin.h"
807 // A popup window to place the wxSTCListBox upon
808 class wxSTCListBoxWin
: public wxPopupWindow
812 CallBackAction doubleClickAction
;
813 void* doubleClickActionData
;
815 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point
WXUNUSED(location
)) :
816 wxPopupWindow(parent
, wxBORDER_SIMPLE
)
819 lv
= new wxSTCListBox(parent
, id
, wxPoint(-50,-50), wxDefaultSize
,
820 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxBORDER_NONE
);
821 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
822 lv
->InsertColumn(0, wxEmptyString
);
823 lv
->InsertColumn(1, wxEmptyString
);
825 // NOTE: We need to fool the wxListView into thinking that it has the
826 // focus so it will use the normal selection colour and will look
827 // "right" to the user. But since the wxPopupWindow or its children
828 // can't receive focus then we have to pull a fast one and temporarily
829 // parent the listctrl on the STC window and then call SetFocus and
830 // then reparent it back to the popup.
836 #if defined(__WXOSX_COCOA__) || defined(__WXGTK__)
837 // This color will end up being our border
838 SetBackgroundColour(wxColour(0xC0, 0xC0, 0xC0));
843 // Set position in client coords
844 virtual void DoSetSize(int x
, int y
,
845 int width
, int height
,
846 int sizeFlags
= wxSIZE_AUTO
) {
847 if (x
!= wxDefaultCoord
) {
848 GetParent()->ClientToScreen(&x
, NULL
);
850 if (y
!= wxDefaultCoord
) {
851 GetParent()->ClientToScreen(NULL
, &y
);
853 wxPopupWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
856 // return position as if it were in client coords
857 virtual void DoGetPosition( int *x
, int *y
) const {
859 wxPopupWindow::DoGetPosition(&sx
, &sy
);
860 GetParent()->ScreenToClient(&sx
, &sy
);
867 if ( !wxPendingDelete
.Member(this) )
868 wxPendingDelete
.Append(this);
874 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
877 il
->GetSize(0, w
, h
);
884 void SetDoubleClickAction(CallBackAction action
, void *data
) {
885 doubleClickAction
= action
;
886 doubleClickActionData
= data
;
890 void OnFocus(wxFocusEvent
& event
) {
891 GetParent()->SetFocus();
895 void OnSize(wxSizeEvent
& event
) {
896 // resize the child to fill the popup
897 wxSize sz
= GetClientSize();
902 #if defined(__WXOSX_COCOA__) || defined(__WXGTK__)
903 // make room for the parent's bg color to show, to act as a border
908 lv
->SetSize(x
, y
, w
, h
);
909 // reset the column widths
910 lv
->SetColumnWidth(0, IconWidth()+4);
911 lv
->SetColumnWidth(1, w
- 2 - lv
->GetColumnWidth(0) -
912 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
916 void OnActivate(wxListEvent
& WXUNUSED(event
)) {
917 doubleClickAction(doubleClickActionData
);
920 wxListView
* GetLB() { return lv
; }
923 DECLARE_EVENT_TABLE()
927 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxPopupWindow
)
928 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
929 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
930 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
935 #else // !wxUSE_POPUPWIN -----------------------------------
936 #include "wx/frame.h"
938 // A normal window to place the wxSTCListBox upon, but make it behave as much
939 // like a wxPopupWindow as possible
940 class wxSTCListBoxWin
: public wxFrame
{
943 CallBackAction doubleClickAction
;
944 void* doubleClickActionData
;
946 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point location
) :
947 wxFrame(parent
, id
, wxEmptyString
, wxPoint(location
.x
, location
.y
), wxSize(0,0),
949 | wxFRAME_FLOAT_ON_PARENT
959 lv
= new wxSTCListBox(this, id
, wxDefaultPosition
, wxDefaultSize
,
960 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxNO_BORDER
);
961 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
962 lv
->InsertColumn(0, wxEmptyString
);
963 lv
->InsertColumn(1, wxEmptyString
);
965 // Eventhough we immediately reset the focus to the parent, this helps
966 // things to look right...
973 // On OSX and (possibly others) there can still be pending
974 // messages/events for the list control when Scintilla wants to
975 // close it, so do a pending delete of it instead of destroying
980 // The bottom edge of this window is not getting properly
981 // refreshed upon deletion, so help it out...
982 wxWindow
* p
= GetParent();
983 wxRect
r(GetPosition(), GetSize());
984 r
.SetHeight(r
.GetHeight()+1);
985 p
->Refresh(false, &r
);
987 if ( !wxPendingDelete
.Member(this) )
988 wxPendingDelete
.Append(this);
995 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
998 il
->GetSize(0, w
, h
);
1005 void SetDoubleClickAction(CallBackAction action
, void *data
)
1007 doubleClickAction
= action
;
1008 doubleClickActionData
= data
;
1012 void OnFocus(wxFocusEvent
& event
)
1015 GetParent()->SetFocus();
1019 void OnSize(wxSizeEvent
& event
)
1022 wxSize sz
= GetClientSize();
1024 // reset the column widths
1025 lv
->SetColumnWidth(0, IconWidth()+4);
1026 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
1027 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
1031 void ActivateParent()
1033 // Although we're a frame, we always want the parent to be active, so
1034 // raise it whenever we get shown, focused, etc.
1035 wxTopLevelWindow
*frame
= wxDynamicCast(
1036 wxGetTopLevelParent(GetParent()), wxTopLevelWindow
);
1042 virtual void DoSetSize(int x
, int y
,
1043 int width
, int height
,
1044 int sizeFlags
= wxSIZE_AUTO
)
1046 // convert coords to screen coords since we're a top-level window
1047 if (x
!= wxDefaultCoord
) {
1048 GetParent()->ClientToScreen(&x
, NULL
);
1050 if (y
!= wxDefaultCoord
) {
1051 GetParent()->ClientToScreen(NULL
, &y
);
1053 wxFrame::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1056 virtual bool Show(bool show
= true)
1058 bool rv
= wxFrame::Show(show
);
1062 GetParent()->Refresh(false);
1067 void OnActivate(wxListEvent
& WXUNUSED(event
))
1069 doubleClickAction(doubleClickActionData
);
1072 wxListView
* GetLB() { return lv
; }
1075 DECLARE_EVENT_TABLE()
1079 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxWindow
)
1080 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
1081 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
1082 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
1085 #endif // wxUSE_POPUPWIN -----------------------------------
1088 inline wxSTCListBoxWin
* GETLBW(WindowID win
) {
1089 return ((wxSTCListBoxWin
*)win
);
1092 inline wxListView
* GETLB(WindowID win
) {
1093 return GETLBW(win
)->GetLB();
1096 //----------------------------------------------------------------------
1098 class ListBoxImpl
: public ListBox
{
1102 int desiredVisibleRows
;
1105 Point location
; // Caret location at which the list is opened
1106 wxImageList
* imgList
;
1107 wxArrayInt
* imgTypeMap
;
1112 static ListBox
*Allocate();
1114 virtual void SetFont(Font
&font
);
1115 virtual void Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
, int technology_
);
1116 virtual void SetAverageCharWidth(int width
);
1117 virtual void SetVisibleRows(int rows
);
1118 virtual int GetVisibleRows() const;
1119 virtual PRectangle
GetDesiredRect();
1120 virtual int CaretFromEdge();
1121 virtual void Clear();
1122 virtual void Append(char *s
, int type
= -1);
1123 void Append(const wxString
& text
, int type
);
1124 virtual int Length();
1125 virtual void Select(int n
);
1126 virtual int GetSelection();
1127 virtual int Find(const char *prefix
);
1128 virtual void GetValue(int n
, char *value
, int len
);
1129 virtual void RegisterImage(int type
, const char *xpm_data
);
1130 void RegisterImageHelper(int type
, wxBitmap
& bmp
);
1131 virtual void RegisterRGBAImage(int type
, int width
, int height
, const unsigned char *pixelsImage
);
1132 virtual void ClearRegisteredImages();
1133 virtual void SetDoubleClickAction(CallBackAction
, void *);
1134 virtual void SetList(const char* list
, char separator
, char typesep
);
1138 ListBoxImpl::ListBoxImpl()
1139 : lineHeight(10), unicodeMode(false),
1140 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
1141 imgList(NULL
), imgTypeMap(NULL
)
1145 ListBoxImpl::~ListBoxImpl() {
1147 wxDELETE(imgTypeMap
);
1151 void ListBoxImpl::SetFont(Font
&font
) {
1152 GETLB(wid
)->SetFont(*((wxFont
*)font
.GetID()));
1156 void ListBoxImpl::Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
, int WXUNUSED(technology_
)) {
1157 location
= location_
;
1158 lineHeight
= lineHeight_
;
1159 unicodeMode
= unicodeMode_
;
1161 wid
= new wxSTCListBoxWin(GETWIN(parent
.GetID()), ctrlID
, location
);
1162 if (imgList
!= NULL
)
1163 GETLB(wid
)->SetImageList(imgList
, wxIMAGE_LIST_SMALL
);
1167 void ListBoxImpl::SetAverageCharWidth(int width
) {
1168 aveCharWidth
= width
;
1172 void ListBoxImpl::SetVisibleRows(int rows
) {
1173 desiredVisibleRows
= rows
;
1177 int ListBoxImpl::GetVisibleRows() const {
1178 return desiredVisibleRows
;
1181 PRectangle
ListBoxImpl::GetDesiredRect() {
1182 // wxListCtrl doesn't have a DoGetBestSize, so instead we kept track of
1183 // the max size in Append and calculate it here...
1184 int maxw
= maxStrWidth
* aveCharWidth
;
1187 // give it a default if there are no lines, and/or add a bit more
1188 if (maxw
== 0) maxw
= 100;
1189 maxw
+= aveCharWidth
* 3 +
1190 GETLBW(wid
)->IconWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1194 // estimate a desired height
1195 int count
= GETLB(wid
)->GetItemCount();
1198 GETLB(wid
)->GetItemRect(0, rect
);
1199 maxh
= count
* rect
.GetHeight();
1200 if (maxh
> 140) // TODO: Use desiredVisibleRows??
1203 // Try to make the size an exact multiple of some number of lines
1204 int lines
= maxh
/ rect
.GetHeight();
1205 maxh
= (lines
+ 1) * rect
.GetHeight() + 2;
1219 int ListBoxImpl::CaretFromEdge() {
1220 return 4 + GETLBW(wid
)->IconWidth();
1224 void ListBoxImpl::Clear() {
1225 GETLB(wid
)->DeleteAllItems();
1229 void ListBoxImpl::Append(char *s
, int type
) {
1230 Append(stc2wx(s
), type
);
1233 void ListBoxImpl::Append(const wxString
& text
, int type
) {
1234 long count
= GETLB(wid
)->GetItemCount();
1235 long itemID
= GETLB(wid
)->InsertItem(count
, wxEmptyString
);
1237 GETLB(wid
)->SetItem(itemID
, 1, text
);
1238 maxStrWidth
= wxMax(maxStrWidth
, text
.length());
1240 wxCHECK_RET(imgTypeMap
, wxT("Unexpected NULL imgTypeMap"));
1241 idx
= imgTypeMap
->Item(type
);
1243 GETLB(wid
)->SetItemImage(itemID
, idx
, idx
);
1246 void ListBoxImpl::SetList(const char* list
, char separator
, char typesep
) {
1247 GETLB(wid
)->Freeze();
1249 wxStringTokenizer
tkzr(stc2wx(list
), (wxChar
)separator
);
1250 while ( tkzr
.HasMoreTokens() ) {
1251 wxString token
= tkzr
.GetNextToken();
1253 int pos
= token
.Find(typesep
);
1255 token
.Mid(pos
+1).ToLong(&type
);
1256 token
.Truncate(pos
);
1258 Append(token
, (int)type
);
1264 int ListBoxImpl::Length() {
1265 return GETLB(wid
)->GetItemCount();
1269 void ListBoxImpl::Select(int n
) {
1275 GETLB(wid
)->EnsureVisible(n
);
1276 GETLB(wid
)->Select(n
, select
);
1280 int ListBoxImpl::GetSelection() {
1281 return GETLB(wid
)->GetFirstSelected();
1285 int ListBoxImpl::Find(const char *WXUNUSED(prefix
)) {
1291 void ListBoxImpl::GetValue(int n
, char *value
, int len
) {
1295 item
.SetMask(wxLIST_MASK_TEXT
);
1296 GETLB(wid
)->GetItem(item
);
1297 strncpy(value
, wx2stc(item
.GetText()), len
);
1298 value
[len
-1] = '\0';
1301 void ListBoxImpl::RegisterImageHelper(int type
, wxBitmap
& bmp
)
1304 // assumes all images are the same size
1305 imgList
= new wxImageList(bmp
.GetWidth(), bmp
.GetHeight(), true);
1306 imgTypeMap
= new wxArrayInt
;
1309 int idx
= imgList
->Add(bmp
);
1311 // do we need to extend the mapping array?
1312 wxArrayInt
& itm
= *imgTypeMap
;
1313 if ( itm
.GetCount() < (size_t)type
+1)
1314 itm
.Add(-1, type
- itm
.GetCount() + 1);
1316 // Add an item that maps type to the image index
1320 void ListBoxImpl::RegisterImage(int type
, const char *xpm_data
) {
1321 wxMemoryInputStream
stream(xpm_data
, strlen(xpm_data
)+1);
1322 wxImage
img(stream
, wxBITMAP_TYPE_XPM
);
1324 RegisterImageHelper(type
, bmp
);
1328 void ListBoxImpl::RegisterRGBAImage(int type
, int width
, int height
,
1329 const unsigned char *pixelsImage
)
1331 #ifdef wxHAS_RAW_BITMAP
1332 wxBitmap bmp
= BitmapFromRGBAImage(width
, height
, pixelsImage
);
1333 RegisterImageHelper(type
, bmp
);
1338 void ListBoxImpl::ClearRegisteredImages() {
1340 wxDELETE(imgTypeMap
);
1342 GETLB(wid
)->SetImageList(NULL
, wxIMAGE_LIST_SMALL
);
1346 void ListBoxImpl::SetDoubleClickAction(CallBackAction action
, void *data
) {
1347 GETLBW(wid
)->SetDoubleClickAction(action
, data
);
1351 ListBox::ListBox() {
1354 ListBox::~ListBox() {
1357 ListBox
*ListBox::Allocate() {
1358 return new ListBoxImpl();
1361 //----------------------------------------------------------------------
1363 Menu::Menu() : mid(0) {
1366 void Menu::CreatePopUp() {
1371 void Menu::Destroy() {
1373 delete (wxMenu
*)mid
;
1377 void Menu::Show(Point pt
, Window
&w
) {
1378 GETWIN(w
.GetID())->PopupMenu((wxMenu
*)mid
, pt
.x
- 4, pt
.y
);
1382 //----------------------------------------------------------------------
1384 DynamicLibrary
*DynamicLibrary::Load(const char *WXUNUSED(modulePath
)) {
1385 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1389 //----------------------------------------------------------------------
1391 ColourDesired
Platform::Chrome() {
1393 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1394 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1397 ColourDesired
Platform::ChromeHighlight() {
1399 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
);
1400 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1403 const char *Platform::DefaultFont() {
1404 static char buf
[128];
1405 strcpy(buf
, wxNORMAL_FONT
->GetFaceName().mbc_str());
1409 int Platform::DefaultFontSize() {
1410 return wxNORMAL_FONT
->GetPointSize();
1413 unsigned int Platform::DoubleClickTime() {
1414 return 500; // **** ::GetDoubleClickTime();
1417 bool Platform::MouseButtonBounce() {
1421 bool Platform::IsKeyDown(int WXUNUSED(key
)) {
1422 return false; // I don't think we'll need this.
1425 long Platform::SendScintilla(WindowID w
,
1427 unsigned long wParam
,
1430 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1431 return stc
->SendMsg(msg
, wParam
, lParam
);
1434 long Platform::SendScintillaPointer(WindowID w
,
1436 unsigned long wParam
,
1439 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1440 return stc
->SendMsg(msg
, wParam
, (wxIntPtr
)lParam
);
1444 // These are utility functions not really tied to a platform
1446 int Platform::Minimum(int a
, int b
) {
1453 int Platform::Maximum(int a
, int b
) {
1462 void Platform::DebugDisplay(const char *s
) {
1464 wxLogDebug(stc2wx(s
));
1470 void Platform::DebugPrintf(const char *format
, ...) {
1474 va_start(pArguments
, format
);
1475 vsprintf(buffer
,format
,pArguments
);
1477 Platform::DebugDisplay(buffer
);
1479 wxUnusedVar(format
);
1484 static bool assertionPopUps
= true;
1486 bool Platform::ShowAssertionPopUps(bool assertionPopUps_
) {
1487 bool ret
= assertionPopUps
;
1488 assertionPopUps
= assertionPopUps_
;
1492 void Platform::Assert(const char *c
, const char *file
, int line
) {
1495 sprintf(buffer
, "Assertion [%s] failed at %s %d", c
, file
, line
);
1496 if (assertionPopUps
) {
1498 wxMessageBox(stc2wx(buffer
),
1499 wxT("Assertion failure"),
1500 wxICON_HAND
| wxOK
);
1502 strcat(buffer
, "\r\n");
1503 Platform::DebugDisplay(buffer
);
1514 int Platform::Clamp(int val
, int minVal
, int maxVal
) {
1523 bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage
), char WXUNUSED(ch
)) {
1527 int Platform::DBCSCharLength(int WXUNUSED(codePage
), const char *WXUNUSED(s
)) {
1531 int Platform::DBCSCharMaxLength() {
1536 //----------------------------------------------------------------------
1538 ElapsedTime::ElapsedTime() {
1539 wxLongLong localTime
= wxGetLocalTimeMillis();
1540 littleBit
= localTime
.GetLo();
1541 bigBit
= localTime
.GetHi();
1544 double ElapsedTime::Duration(bool reset
) {
1545 wxLongLong
prevTime(bigBit
, littleBit
);
1546 wxLongLong localTime
= wxGetLocalTimeMillis();
1548 littleBit
= localTime
.GetLo();
1549 bigBit
= localTime
.GetHi();
1551 wxLongLong duration
= localTime
- prevTime
;
1552 double result
= duration
.ToDouble();
1558 //----------------------------------------------------------------------
1562 #include "UniConversion.h"
1564 // Convert using Scintilla's functions instead of wx's, Scintilla's are more
1565 // forgiving and won't assert...
1567 wxString
stc2wx(const char* str
, size_t len
)
1570 return wxEmptyString
;
1572 size_t wclen
= UTF16Length(str
, len
);
1573 wxWCharBuffer
buffer(wclen
+1);
1575 size_t actualLen
= UTF16FromUTF8(str
, len
, buffer
.data(), wclen
+1);
1576 return wxString(buffer
.data(), actualLen
);
1581 wxString
stc2wx(const char* str
)
1583 return stc2wx(str
, strlen(str
));
1587 wxWX2MBbuf
wx2stc(const wxString
& str
)
1589 const wchar_t* wcstr
= str
.c_str();
1590 size_t wclen
= str
.length();
1591 size_t len
= UTF8Length(wcstr
, wclen
);
1593 wxCharBuffer
buffer(len
+1);
1594 UTF8FromUTF16(wcstr
, wclen
, buffer
.data(), len
);
1596 // TODO check NULL termination!!