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);
340 wxAlphaPixelData
pixData(bmp
);
342 // Set the fill pixels
343 ColourDesired
cdf(fill
.AsLong());
344 int red
= cdf
.GetRed();
345 int green
= cdf
.GetGreen();
346 int blue
= cdf
.GetBlue();
348 wxAlphaPixelData::Iterator
p(pixData
);
349 for (y
=0; y
<r
.height
; y
++) {
350 p
.MoveTo(pixData
, 0, y
);
351 for (x
=0; x
<r
.width
; x
++) {
352 p
.Red() = wxPy_premultiply(red
, alphaFill
);
353 p
.Green() = wxPy_premultiply(green
, alphaFill
);
354 p
.Blue() = wxPy_premultiply(blue
, alphaFill
);
355 p
.Alpha() = alphaFill
;
360 // Set the outline pixels
361 ColourDesired
cdo(outline
.AsLong());
363 green
= cdo
.GetGreen();
364 blue
= cdo
.GetBlue();
365 for (x
=0; x
<r
.width
; x
++) {
366 p
.MoveTo(pixData
, x
, 0);
367 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
368 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
369 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
370 p
.Alpha() = alphaOutline
;
371 p
.MoveTo(pixData
, x
, r
.height
-1);
372 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
373 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
374 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
375 p
.Alpha() = alphaOutline
;
378 for (y
=0; y
<r
.height
; y
++) {
379 p
.MoveTo(pixData
, 0, y
);
380 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
381 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
382 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
383 p
.Alpha() = alphaOutline
;
384 p
.MoveTo(pixData
, r
.width
-1, y
);
385 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
386 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
387 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
388 p
.Alpha() = alphaOutline
;
392 hdc
->DrawBitmap(bmp
, r
.x
, r
.y
, true);
395 wxUnusedVar(cornerSize
);
396 wxUnusedVar(alphaFill
);
397 wxUnusedVar(alphaOutline
);
398 RectangleDraw(rc
, outline
, fill
);
403 #ifdef wxHAS_RAW_BITMAP
404 wxBitmap
BitmapFromRGBAImage(int width
, int height
, const unsigned char *pixelsImage
)
407 wxBitmap
bmp(width
, height
, 32);
408 wxAlphaPixelData
pixData(bmp
);
410 wxAlphaPixelData::Iterator
p(pixData
);
411 for (y
=0; y
<height
; y
++) {
412 p
.MoveTo(pixData
, 0, y
);
413 for (x
=0; x
<width
; x
++) {
414 unsigned char red
= *pixelsImage
++;
415 unsigned char green
= *pixelsImage
++;
416 unsigned char blue
= *pixelsImage
++;
417 unsigned char alpha
= *pixelsImage
++;
419 p
.Red() = wxPy_premultiply(red
, alpha
);
420 p
.Green() = wxPy_premultiply(green
, alpha
);
421 p
.Blue() = wxPy_premultiply(blue
, alpha
);
431 void SurfaceImpl::DrawRGBAImage(PRectangle rc
, int width
, int height
,
432 const unsigned char *pixelsImage
)
434 #ifdef wxHAS_RAW_BITMAP
435 wxRect r
= wxRectFromPRectangle(rc
);
436 wxBitmap bmp
= BitmapFromRGBAImage(width
, height
, pixelsImage
);
437 hdc
->DrawBitmap(bmp
, r
.x
, r
.y
, true);
442 void SurfaceImpl::Ellipse(PRectangle rc
, ColourDesired fore
, ColourDesired back
) {
445 hdc
->DrawEllipse(wxRectFromPRectangle(rc
));
448 void SurfaceImpl::Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
) {
449 wxRect r
= wxRectFromPRectangle(rc
);
450 hdc
->Blit(r
.x
, r
.y
, r
.width
, r
.height
,
451 ((SurfaceImpl
&)surfaceSource
).hdc
,
452 from
.x
, from
.y
, wxCOPY
);
455 void SurfaceImpl::DrawTextNoClip(PRectangle rc
, Font
&font
, XYPOSITION ybase
,
456 const char *s
, int len
,
457 ColourDesired fore
, ColourDesired back
) {
459 hdc
->SetTextForeground(wxColourFromCD(fore
));
460 hdc
->SetTextBackground(wxColourFromCD(back
));
461 FillRectangle(rc
, back
);
463 // ybase is where the baseline should be, but wxWin uses the upper left
464 // corner, so I need to calculate the real position for the text...
465 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
468 void SurfaceImpl::DrawTextClipped(PRectangle rc
, Font
&font
, XYPOSITION ybase
,
469 const char *s
, int len
,
470 ColourDesired fore
, ColourDesired back
) {
472 hdc
->SetTextForeground(wxColourFromCD(fore
));
473 hdc
->SetTextBackground(wxColourFromCD(back
));
474 FillRectangle(rc
, back
);
475 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
477 // see comments above
478 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
479 hdc
->DestroyClippingRegion();
483 void SurfaceImpl::DrawTextTransparent(PRectangle rc
, Font
&font
, XYPOSITION ybase
,
484 const char *s
, int len
,
485 ColourDesired fore
) {
488 hdc
->SetTextForeground(wxColourFromCD(fore
));
489 hdc
->SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
491 // ybase is where the baseline should be, but wxWin uses the upper left
492 // corner, so I need to calculate the real position for the text...
493 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
495 hdc
->SetBackgroundMode(wxBRUSHSTYLE_SOLID
);
499 void SurfaceImpl::MeasureWidths(Font
&font
, const char *s
, int len
, XYPOSITION
*positions
) {
501 wxString str
= stc2wx(s
, len
);
506 hdc
->GetPartialTextExtents(str
, tpos
);
509 // Map the widths for UCS-2 characters back to the UTF-8 input string
510 // NOTE: I don't think this is right for when sizeof(wxChar) > 2, ie wxGTK2
511 // so figure it out and fix it!
514 while ((int)i
< len
) {
515 unsigned char uch
= (unsigned char)s
[i
];
516 positions
[i
++] = tpos
[ui
];
518 if (uch
< (0x80 + 0x40 + 0x20)) {
519 positions
[i
++] = tpos
[ui
];
521 positions
[i
++] = tpos
[ui
];
522 positions
[i
++] = tpos
[ui
];
529 // If not unicode then just use the widths we have
530 #if wxUSE_STD_CONTAINERS
531 std::copy(tpos
.begin(), tpos
.end(), positions
);
533 memcpy(positions
, tpos
.begin(), len
* sizeof(int));
539 XYPOSITION
SurfaceImpl::WidthText(Font
&font
, const char *s
, int len
) {
544 hdc
->GetTextExtent(stc2wx(s
, len
), &w
, &h
);
549 XYPOSITION
SurfaceImpl::WidthChar(Font
&font
, char ch
) {
553 char s
[2] = { ch
, 0 };
555 hdc
->GetTextExtent(stc2wx(s
, 1), &w
, &h
);
559 #define EXTENT_TEST wxT(" `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
561 XYPOSITION
SurfaceImpl::Ascent(Font
&font
) {
564 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
569 XYPOSITION
SurfaceImpl::Descent(Font
&font
) {
572 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
576 XYPOSITION
SurfaceImpl::InternalLeading(Font
&WXUNUSED(font
)) {
580 XYPOSITION
SurfaceImpl::ExternalLeading(Font
&font
) {
583 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
587 XYPOSITION
SurfaceImpl::Height(Font
&font
) {
589 return hdc
->GetCharHeight() + 1;
592 XYPOSITION
SurfaceImpl::AverageCharWidth(Font
&font
) {
594 return hdc
->GetCharWidth();
597 void SurfaceImpl::SetClip(PRectangle rc
) {
598 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
601 void SurfaceImpl::FlushCachedState() {
604 void SurfaceImpl::SetUnicodeMode(bool unicodeMode_
) {
605 unicodeMode
=unicodeMode_
;
608 void SurfaceImpl::SetDBCSMode(int WXUNUSED(codePage
)) {
609 // dbcsMode = codePage == SC_CP_DBCS;
613 Surface
*Surface::Allocate(int WXUNUSED(technology
)) {
614 return new SurfaceImpl
;
618 //----------------------------------------------------------------------
621 inline wxWindow
* GETWIN(WindowID id
) { return (wxWindow
*)id
; }
626 void Window::Destroy() {
629 GETWIN(wid
)->Destroy();
634 bool Window::HasFocus() {
635 return wxWindow::FindFocus() == GETWIN(wid
);
638 PRectangle
Window::GetPosition() {
639 if (! wid
) return PRectangle();
640 wxRect
rc(GETWIN(wid
)->GetPosition(), GETWIN(wid
)->GetSize());
641 return PRectangleFromwxRect(rc
);
644 void Window::SetPosition(PRectangle rc
) {
645 wxRect r
= wxRectFromPRectangle(rc
);
646 GETWIN(wid
)->SetSize(r
);
649 void Window::SetPositionRelative(PRectangle rc
, Window
) {
650 SetPosition(rc
); // ????
653 PRectangle
Window::GetClientPosition() {
654 if (! wid
) return PRectangle();
655 wxSize sz
= GETWIN(wid
)->GetClientSize();
656 return PRectangle(0, 0, sz
.x
, sz
.y
);
659 void Window::Show(bool show
) {
660 GETWIN(wid
)->Show(show
);
663 void Window::InvalidateAll() {
664 GETWIN(wid
)->Refresh(false);
667 void Window::InvalidateRectangle(PRectangle rc
) {
668 wxRect r
= wxRectFromPRectangle(rc
);
669 GETWIN(wid
)->Refresh(false, &r
);
672 void Window::SetFont(Font
&font
) {
673 GETWIN(wid
)->SetFont(*((wxFont
*)font
.GetID()));
676 void Window::SetCursor(Cursor curs
) {
677 wxStockCursor cursorId
;
681 cursorId
= wxCURSOR_IBEAM
;
684 cursorId
= wxCURSOR_ARROW
;
687 cursorId
= wxCURSOR_ARROW
; // ** no up arrow... wxCURSOR_UPARROW;
690 cursorId
= wxCURSOR_WAIT
;
693 cursorId
= wxCURSOR_SIZEWE
;
696 cursorId
= wxCURSOR_SIZENS
;
698 case cursorReverseArrow
:
699 cursorId
= wxCURSOR_RIGHT_ARROW
;
702 cursorId
= wxCURSOR_HAND
;
705 cursorId
= wxCURSOR_ARROW
;
709 wxCursor wc
= wxCursor(cursorId
);
710 if(curs
!= cursorLast
)
712 GETWIN(wid
)->SetCursor(wc
);
718 void Window::SetTitle(const char *s
) {
719 GETWIN(wid
)->SetLabel(stc2wx(s
));
723 // Returns rectangle of monitor pt is on
724 PRectangle
Window::GetMonitorRect(Point pt
) {
726 if (! wid
) return PRectangle();
728 // Get the display the point is found on
729 int n
= wxDisplay::GetFromPoint(wxPoint(pt
.x
, pt
.y
));
730 wxDisplay
dpy(n
== wxNOT_FOUND
? 0 : n
);
731 rect
= dpy
.GetGeometry();
735 return PRectangleFromwxRect(rect
);
738 //----------------------------------------------------------------------
739 // Helper classes for ListBox
742 // This is a simple subclass of wxListView that just resets focus to the
743 // parent when it gets it.
744 class wxSTCListBox
: public wxListView
{
746 wxSTCListBox(wxWindow
* parent
, wxWindowID id
,
747 const wxPoint
& pos
, const wxSize
& size
,
752 Hide(); // don't flicker as we move it around...
754 Create(parent
, id
, pos
, size
, style
);
758 void OnFocus(wxFocusEvent
& event
) {
759 GetParent()->SetFocus();
763 void OnKillFocus(wxFocusEvent
& WXUNUSED(event
)) {
764 // Do nothing. Prevents base class from resetting the colors...
768 // For some reason I don't understand yet the focus doesn't really leave
769 // the listbox like it should, so if we get any events feed them back to
771 void OnKeyDown(wxKeyEvent
& event
) {
772 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
774 void OnChar(wxKeyEvent
& event
) {
775 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
778 // And we need to force the focus back when being destroyed
780 GetGrandParent()->SetFocus();
785 DECLARE_EVENT_TABLE()
788 BEGIN_EVENT_TABLE(wxSTCListBox
, wxListView
)
789 EVT_SET_FOCUS( wxSTCListBox::OnFocus
)
790 EVT_KILL_FOCUS(wxSTCListBox::OnKillFocus
)
792 EVT_KEY_DOWN( wxSTCListBox::OnKeyDown
)
793 EVT_CHAR( wxSTCListBox::OnChar
)
799 #if wxUSE_POPUPWIN //-----------------------------------
800 #include "wx/popupwin.h"
802 // A popup window to place the wxSTCListBox upon
803 class wxSTCListBoxWin
: public wxPopupWindow
807 CallBackAction doubleClickAction
;
808 void* doubleClickActionData
;
810 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point
WXUNUSED(location
)) :
811 wxPopupWindow(parent
, wxBORDER_SIMPLE
)
814 lv
= new wxSTCListBox(parent
, id
, wxPoint(-50,-50), wxDefaultSize
,
815 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxBORDER_NONE
);
816 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
817 lv
->InsertColumn(0, wxEmptyString
);
818 lv
->InsertColumn(1, wxEmptyString
);
820 // NOTE: We need to fool the wxListView into thinking that it has the
821 // focus so it will use the normal selection colour and will look
822 // "right" to the user. But since the wxPopupWindow or its children
823 // can't receive focus then we have to pull a fast one and temporarily
824 // parent the listctrl on the STC window and then call SetFocus and
825 // then reparent it back to the popup.
831 #if defined(__WXOSX_COCOA__) || defined(__WXGTK__)
832 // This color will end up being our border
833 SetBackgroundColour(wxColour(0xC0, 0xC0, 0xC0));
838 // Set position in client coords
839 virtual void DoSetSize(int x
, int y
,
840 int width
, int height
,
841 int sizeFlags
= wxSIZE_AUTO
) {
842 if (x
!= wxDefaultCoord
) {
843 GetParent()->ClientToScreen(&x
, NULL
);
845 if (y
!= wxDefaultCoord
) {
846 GetParent()->ClientToScreen(NULL
, &y
);
848 wxPopupWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
851 // return position as if it were in client coords
852 virtual void DoGetPosition( int *x
, int *y
) const {
854 wxPopupWindow::DoGetPosition(&sx
, &sy
);
855 GetParent()->ScreenToClient(&sx
, &sy
);
862 if ( !wxPendingDelete
.Member(this) )
863 wxPendingDelete
.Append(this);
869 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
872 il
->GetSize(0, w
, h
);
879 void SetDoubleClickAction(CallBackAction action
, void *data
) {
880 doubleClickAction
= action
;
881 doubleClickActionData
= data
;
885 void OnFocus(wxFocusEvent
& event
) {
886 GetParent()->SetFocus();
890 void OnSize(wxSizeEvent
& event
) {
891 // resize the child to fill the popup
892 wxSize sz
= GetClientSize();
897 #if defined(__WXOSX_COCOA__) || defined(__WXGTK__)
898 // make room for the parent's bg color to show, to act as a border
903 lv
->SetSize(x
, y
, w
, h
);
904 // reset the column widths
905 lv
->SetColumnWidth(0, IconWidth()+4);
906 lv
->SetColumnWidth(1, w
- 2 - lv
->GetColumnWidth(0) -
907 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
911 void OnActivate(wxListEvent
& WXUNUSED(event
)) {
912 doubleClickAction(doubleClickActionData
);
915 wxListView
* GetLB() { return lv
; }
918 DECLARE_EVENT_TABLE()
922 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxPopupWindow
)
923 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
924 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
925 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
930 #else // !wxUSE_POPUPWIN -----------------------------------
931 #include "wx/frame.h"
933 // A normal window to place the wxSTCListBox upon, but make it behave as much
934 // like a wxPopupWindow as possible
935 class wxSTCListBoxWin
: public wxFrame
{
938 CallBackAction doubleClickAction
;
939 void* doubleClickActionData
;
941 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point location
) :
942 wxFrame(parent
, id
, wxEmptyString
, wxPoint(location
.x
, location
.y
), wxSize(0,0),
944 | wxFRAME_FLOAT_ON_PARENT
954 lv
= new wxSTCListBox(this, id
, wxDefaultPosition
, wxDefaultSize
,
955 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxNO_BORDER
);
956 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
957 lv
->InsertColumn(0, wxEmptyString
);
958 lv
->InsertColumn(1, wxEmptyString
);
960 // Eventhough we immediately reset the focus to the parent, this helps
961 // things to look right...
968 // On OSX and (possibly others) there can still be pending
969 // messages/events for the list control when Scintilla wants to
970 // close it, so do a pending delete of it instead of destroying
975 // The bottom edge of this window is not getting properly
976 // refreshed upon deletion, so help it out...
977 wxWindow
* p
= GetParent();
978 wxRect
r(GetPosition(), GetSize());
979 r
.SetHeight(r
.GetHeight()+1);
980 p
->Refresh(false, &r
);
982 if ( !wxPendingDelete
.Member(this) )
983 wxPendingDelete
.Append(this);
990 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
993 il
->GetSize(0, w
, h
);
1000 void SetDoubleClickAction(CallBackAction action
, void *data
)
1002 doubleClickAction
= action
;
1003 doubleClickActionData
= data
;
1007 void OnFocus(wxFocusEvent
& event
)
1010 GetParent()->SetFocus();
1014 void OnSize(wxSizeEvent
& event
)
1017 wxSize sz
= GetClientSize();
1019 // reset the column widths
1020 lv
->SetColumnWidth(0, IconWidth()+4);
1021 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
1022 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
1026 void ActivateParent()
1028 // Although we're a frame, we always want the parent to be active, so
1029 // raise it whenever we get shown, focused, etc.
1030 wxTopLevelWindow
*frame
= wxDynamicCast(
1031 wxGetTopLevelParent(GetParent()), wxTopLevelWindow
);
1037 virtual void DoSetSize(int x
, int y
,
1038 int width
, int height
,
1039 int sizeFlags
= wxSIZE_AUTO
)
1041 // convert coords to screen coords since we're a top-level window
1042 if (x
!= wxDefaultCoord
) {
1043 GetParent()->ClientToScreen(&x
, NULL
);
1045 if (y
!= wxDefaultCoord
) {
1046 GetParent()->ClientToScreen(NULL
, &y
);
1048 wxFrame::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1051 virtual bool Show(bool show
= true)
1053 bool rv
= wxFrame::Show(show
);
1057 GetParent()->Refresh(false);
1062 void OnActivate(wxListEvent
& WXUNUSED(event
))
1064 doubleClickAction(doubleClickActionData
);
1067 wxListView
* GetLB() { return lv
; }
1070 DECLARE_EVENT_TABLE()
1074 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxWindow
)
1075 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
1076 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
1077 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
1080 #endif // wxUSE_POPUPWIN -----------------------------------
1083 inline wxSTCListBoxWin
* GETLBW(WindowID win
) {
1084 return ((wxSTCListBoxWin
*)win
);
1087 inline wxListView
* GETLB(WindowID win
) {
1088 return GETLBW(win
)->GetLB();
1091 //----------------------------------------------------------------------
1093 class ListBoxImpl
: public ListBox
{
1097 int desiredVisibleRows
;
1100 Point location
; // Caret location at which the list is opened
1101 wxImageList
* imgList
;
1102 wxArrayInt
* imgTypeMap
;
1107 static ListBox
*Allocate();
1109 virtual void SetFont(Font
&font
);
1110 virtual void Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
, int technology_
);
1111 virtual void SetAverageCharWidth(int width
);
1112 virtual void SetVisibleRows(int rows
);
1113 virtual int GetVisibleRows() const;
1114 virtual PRectangle
GetDesiredRect();
1115 virtual int CaretFromEdge();
1116 virtual void Clear();
1117 virtual void Append(char *s
, int type
= -1);
1118 void Append(const wxString
& text
, int type
);
1119 virtual int Length();
1120 virtual void Select(int n
);
1121 virtual int GetSelection();
1122 virtual int Find(const char *prefix
);
1123 virtual void GetValue(int n
, char *value
, int len
);
1124 virtual void RegisterImage(int type
, const char *xpm_data
);
1125 void RegisterImageHelper(int type
, wxBitmap
& bmp
);
1126 virtual void RegisterRGBAImage(int type
, int width
, int height
, const unsigned char *pixelsImage
);
1127 virtual void ClearRegisteredImages();
1128 virtual void SetDoubleClickAction(CallBackAction
, void *);
1129 virtual void SetList(const char* list
, char separator
, char typesep
);
1133 ListBoxImpl::ListBoxImpl()
1134 : lineHeight(10), unicodeMode(false),
1135 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
1136 imgList(NULL
), imgTypeMap(NULL
)
1140 ListBoxImpl::~ListBoxImpl() {
1142 wxDELETE(imgTypeMap
);
1146 void ListBoxImpl::SetFont(Font
&font
) {
1147 GETLB(wid
)->SetFont(*((wxFont
*)font
.GetID()));
1151 void ListBoxImpl::Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
, int WXUNUSED(technology_
)) {
1152 location
= location_
;
1153 lineHeight
= lineHeight_
;
1154 unicodeMode
= unicodeMode_
;
1156 wid
= new wxSTCListBoxWin(GETWIN(parent
.GetID()), ctrlID
, location
);
1157 if (imgList
!= NULL
)
1158 GETLB(wid
)->SetImageList(imgList
, wxIMAGE_LIST_SMALL
);
1162 void ListBoxImpl::SetAverageCharWidth(int width
) {
1163 aveCharWidth
= width
;
1167 void ListBoxImpl::SetVisibleRows(int rows
) {
1168 desiredVisibleRows
= rows
;
1172 int ListBoxImpl::GetVisibleRows() const {
1173 return desiredVisibleRows
;
1176 PRectangle
ListBoxImpl::GetDesiredRect() {
1177 // wxListCtrl doesn't have a DoGetBestSize, so instead we kept track of
1178 // the max size in Append and calculate it here...
1179 int maxw
= maxStrWidth
* aveCharWidth
;
1182 // give it a default if there are no lines, and/or add a bit more
1183 if (maxw
== 0) maxw
= 100;
1184 maxw
+= aveCharWidth
* 3 +
1185 GETLBW(wid
)->IconWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1189 // estimate a desired height
1190 int count
= GETLB(wid
)->GetItemCount();
1193 GETLB(wid
)->GetItemRect(0, rect
);
1194 maxh
= count
* rect
.GetHeight();
1195 if (maxh
> 140) // TODO: Use desiredVisibleRows??
1198 // Try to make the size an exact multiple of some number of lines
1199 int lines
= maxh
/ rect
.GetHeight();
1200 maxh
= (lines
+ 1) * rect
.GetHeight() + 2;
1214 int ListBoxImpl::CaretFromEdge() {
1215 return 4 + GETLBW(wid
)->IconWidth();
1219 void ListBoxImpl::Clear() {
1220 GETLB(wid
)->DeleteAllItems();
1224 void ListBoxImpl::Append(char *s
, int type
) {
1225 Append(stc2wx(s
), type
);
1228 void ListBoxImpl::Append(const wxString
& text
, int type
) {
1229 long count
= GETLB(wid
)->GetItemCount();
1230 long itemID
= GETLB(wid
)->InsertItem(count
, wxEmptyString
);
1232 GETLB(wid
)->SetItem(itemID
, 1, text
);
1233 maxStrWidth
= wxMax(maxStrWidth
, text
.length());
1235 wxCHECK_RET(imgTypeMap
, wxT("Unexpected NULL imgTypeMap"));
1236 idx
= imgTypeMap
->Item(type
);
1238 GETLB(wid
)->SetItemImage(itemID
, idx
, idx
);
1241 void ListBoxImpl::SetList(const char* list
, char separator
, char typesep
) {
1242 GETLB(wid
)->Freeze();
1244 wxStringTokenizer
tkzr(stc2wx(list
), (wxChar
)separator
);
1245 while ( tkzr
.HasMoreTokens() ) {
1246 wxString token
= tkzr
.GetNextToken();
1248 int pos
= token
.Find(typesep
);
1250 token
.Mid(pos
+1).ToLong(&type
);
1251 token
.Truncate(pos
);
1253 Append(token
, (int)type
);
1259 int ListBoxImpl::Length() {
1260 return GETLB(wid
)->GetItemCount();
1264 void ListBoxImpl::Select(int n
) {
1270 GETLB(wid
)->EnsureVisible(n
);
1271 GETLB(wid
)->Select(n
, select
);
1275 int ListBoxImpl::GetSelection() {
1276 return GETLB(wid
)->GetFirstSelected();
1280 int ListBoxImpl::Find(const char *WXUNUSED(prefix
)) {
1286 void ListBoxImpl::GetValue(int n
, char *value
, int len
) {
1290 item
.SetMask(wxLIST_MASK_TEXT
);
1291 GETLB(wid
)->GetItem(item
);
1292 strncpy(value
, wx2stc(item
.GetText()), len
);
1293 value
[len
-1] = '\0';
1296 void ListBoxImpl::RegisterImageHelper(int type
, wxBitmap
& bmp
)
1299 // assumes all images are the same size
1300 imgList
= new wxImageList(bmp
.GetWidth(), bmp
.GetHeight(), true);
1301 imgTypeMap
= new wxArrayInt
;
1304 int idx
= imgList
->Add(bmp
);
1306 // do we need to extend the mapping array?
1307 wxArrayInt
& itm
= *imgTypeMap
;
1308 if ( itm
.GetCount() < (size_t)type
+1)
1309 itm
.Add(-1, type
- itm
.GetCount() + 1);
1311 // Add an item that maps type to the image index
1315 void ListBoxImpl::RegisterImage(int type
, const char *xpm_data
) {
1316 wxMemoryInputStream
stream(xpm_data
, strlen(xpm_data
)+1);
1317 wxImage
img(stream
, wxBITMAP_TYPE_XPM
);
1319 RegisterImageHelper(type
, bmp
);
1323 void ListBoxImpl::RegisterRGBAImage(int type
, int width
, int height
,
1324 const unsigned char *pixelsImage
)
1326 wxBitmap bmp
= BitmapFromRGBAImage(width
, height
, pixelsImage
);
1327 RegisterImageHelper(type
, bmp
);
1331 void ListBoxImpl::ClearRegisteredImages() {
1333 wxDELETE(imgTypeMap
);
1335 GETLB(wid
)->SetImageList(NULL
, wxIMAGE_LIST_SMALL
);
1339 void ListBoxImpl::SetDoubleClickAction(CallBackAction action
, void *data
) {
1340 GETLBW(wid
)->SetDoubleClickAction(action
, data
);
1344 ListBox::ListBox() {
1347 ListBox::~ListBox() {
1350 ListBox
*ListBox::Allocate() {
1351 return new ListBoxImpl();
1354 //----------------------------------------------------------------------
1356 Menu::Menu() : mid(0) {
1359 void Menu::CreatePopUp() {
1364 void Menu::Destroy() {
1366 delete (wxMenu
*)mid
;
1370 void Menu::Show(Point pt
, Window
&w
) {
1371 GETWIN(w
.GetID())->PopupMenu((wxMenu
*)mid
, pt
.x
- 4, pt
.y
);
1375 //----------------------------------------------------------------------
1377 DynamicLibrary
*DynamicLibrary::Load(const char *WXUNUSED(modulePath
)) {
1378 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1382 //----------------------------------------------------------------------
1384 ColourDesired
Platform::Chrome() {
1386 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1387 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1390 ColourDesired
Platform::ChromeHighlight() {
1392 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
);
1393 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1396 const char *Platform::DefaultFont() {
1397 static char buf
[128];
1398 strcpy(buf
, wxNORMAL_FONT
->GetFaceName().mbc_str());
1402 int Platform::DefaultFontSize() {
1403 return wxNORMAL_FONT
->GetPointSize();
1406 unsigned int Platform::DoubleClickTime() {
1407 return 500; // **** ::GetDoubleClickTime();
1410 bool Platform::MouseButtonBounce() {
1414 bool Platform::IsKeyDown(int WXUNUSED(key
)) {
1415 return false; // I don't think we'll need this.
1418 long Platform::SendScintilla(WindowID w
,
1420 unsigned long wParam
,
1423 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1424 return stc
->SendMsg(msg
, wParam
, lParam
);
1427 long Platform::SendScintillaPointer(WindowID w
,
1429 unsigned long wParam
,
1432 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1433 return stc
->SendMsg(msg
, wParam
, (wxIntPtr
)lParam
);
1437 // These are utility functions not really tied to a platform
1439 int Platform::Minimum(int a
, int b
) {
1446 int Platform::Maximum(int a
, int b
) {
1455 void Platform::DebugDisplay(const char *s
) {
1457 wxLogDebug(stc2wx(s
));
1463 void Platform::DebugPrintf(const char *format
, ...) {
1467 va_start(pArguments
, format
);
1468 vsprintf(buffer
,format
,pArguments
);
1470 Platform::DebugDisplay(buffer
);
1472 wxUnusedVar(format
);
1477 static bool assertionPopUps
= true;
1479 bool Platform::ShowAssertionPopUps(bool assertionPopUps_
) {
1480 bool ret
= assertionPopUps
;
1481 assertionPopUps
= assertionPopUps_
;
1485 void Platform::Assert(const char *c
, const char *file
, int line
) {
1488 sprintf(buffer
, "Assertion [%s] failed at %s %d", c
, file
, line
);
1489 if (assertionPopUps
) {
1491 wxMessageBox(stc2wx(buffer
),
1492 wxT("Assertion failure"),
1493 wxICON_HAND
| wxOK
);
1495 strcat(buffer
, "\r\n");
1496 Platform::DebugDisplay(buffer
);
1507 int Platform::Clamp(int val
, int minVal
, int maxVal
) {
1516 bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage
), char WXUNUSED(ch
)) {
1520 int Platform::DBCSCharLength(int WXUNUSED(codePage
), const char *WXUNUSED(s
)) {
1524 int Platform::DBCSCharMaxLength() {
1529 //----------------------------------------------------------------------
1531 ElapsedTime::ElapsedTime() {
1532 wxLongLong localTime
= wxGetLocalTimeMillis();
1533 littleBit
= localTime
.GetLo();
1534 bigBit
= localTime
.GetHi();
1537 double ElapsedTime::Duration(bool reset
) {
1538 wxLongLong
prevTime(bigBit
, littleBit
);
1539 wxLongLong localTime
= wxGetLocalTimeMillis();
1541 littleBit
= localTime
.GetLo();
1542 bigBit
= localTime
.GetHi();
1544 wxLongLong duration
= localTime
- prevTime
;
1545 double result
= duration
.ToDouble();
1551 //----------------------------------------------------------------------
1555 #include "UniConversion.h"
1557 // Convert using Scintilla's functions instead of wx's, Scintilla's are more
1558 // forgiving and won't assert...
1560 wxString
stc2wx(const char* str
, size_t len
)
1563 return wxEmptyString
;
1565 size_t wclen
= UTF16Length(str
, len
);
1566 wxWCharBuffer
buffer(wclen
+1);
1568 size_t actualLen
= UTF16FromUTF8(str
, len
, buffer
.data(), wclen
+1);
1569 return wxString(buffer
.data(), actualLen
);
1574 wxString
stc2wx(const char* str
)
1576 return stc2wx(str
, strlen(str
));
1580 wxWX2MBbuf
wx2stc(const wxString
& str
)
1582 const wchar_t* wcstr
= str
.c_str();
1583 size_t wclen
= str
.length();
1584 size_t len
= UTF8Length(wcstr
, wclen
);
1586 wxCharBuffer
buffer(len
+1);
1587 UTF8FromUTF16(wcstr
, wclen
, buffer
.data(), len
);
1589 // TODO check NULL termination!!