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
wxColourFromCA(const ColourAllocated
& ca
) {
64 ColourDesired
cd(ca
.AsLong());
65 return wxColour((unsigned char)cd
.GetRed(),
66 (unsigned char)cd
.GetGreen(),
67 (unsigned char)cd
.GetBlue());
70 wxColour
wxColourFromCAandAlpha(const ColourAllocated
& ca
, int alpha
) {
71 ColourDesired
cd(ca
.AsLong());
72 return wxColour((unsigned char)cd
.GetRed(),
73 (unsigned char)cd
.GetGreen(),
74 (unsigned char)cd
.GetBlue(),
75 (unsigned char)alpha
);
78 //----------------------------------------------------------------------
82 allowRealization
= false;
84 entries
= new ColourPair
[size
];
93 void Palette::Release() {
97 entries
= new ColourPair
[size
];
100 // This method either adds a colour to the list of wanted colours (want==true)
101 // or retrieves the allocated colour back to the ColourPair.
102 // This is one method to make it easier to keep the code for wanting and retrieving in sync.
103 void Palette::WantFind(ColourPair
&cp
, bool want
) {
105 for (int i
=0; i
< used
; i
++) {
106 if (entries
[i
].desired
== cp
.desired
)
111 int sizeNew
= size
* 2;
112 ColourPair
*entriesNew
= new ColourPair
[sizeNew
];
113 for (int j
=0; j
<size
; j
++) {
114 entriesNew
[j
] = entries
[j
];
117 entries
= entriesNew
;
121 entries
[used
].desired
= cp
.desired
;
122 entries
[used
].allocated
.Set(cp
.desired
.AsLong());
125 for (int i
=0; i
< used
; i
++) {
126 if (entries
[i
].desired
== cp
.desired
) {
127 cp
.allocated
= entries
[i
].allocated
;
131 cp
.allocated
.Set(cp
.desired
.AsLong());
135 void Palette::Allocate(Window
&) {
136 if (allowRealization
) {
141 //----------------------------------------------------------------------
151 void Font::Create(const char *faceName
, int characterSet
,
152 int size
, bool bold
, bool italic
,
153 int WXUNUSED(extraFontFlag
)) {
156 // The minus one is done because since Scintilla uses SC_SHARSET_DEFAULT
157 // internally and we need to have wxFONENCODING_DEFAULT == SC_SHARSET_DEFAULT
158 // so we adjust the encoding before passing it to Scintilla. See also
159 // wxStyledTextCtrl::StyleSetCharacterSet
160 wxFontEncoding encoding
= (wxFontEncoding
)(characterSet
-1);
162 wxFontEncodingArray ea
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
166 wxFont
* font
= new wxFont(size
,
168 italic
? wxITALIC
: wxNORMAL
,
169 bold
? wxBOLD
: wxNORMAL
,
177 void Font::Release() {
183 //----------------------------------------------------------------------
185 class SurfaceImpl
: public Surface
{
198 virtual void Init(WindowID wid
);
199 virtual void Init(SurfaceID sid
, WindowID wid
);
200 virtual void InitPixMap(int width
, int height
, Surface
*surface_
, WindowID wid
);
202 virtual void Release();
203 virtual bool Initialised();
204 virtual void PenColour(ColourAllocated fore
);
205 virtual int LogPixelsY();
206 virtual int DeviceHeightFont(int points
);
207 virtual void MoveTo(int x_
, int y_
);
208 virtual void LineTo(int x_
, int y_
);
209 virtual void Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
);
210 virtual void RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
211 virtual void FillRectangle(PRectangle rc
, ColourAllocated back
);
212 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
);
213 virtual void RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
214 virtual void AlphaRectangle(PRectangle rc
, int cornerSize
, ColourAllocated fill
, int alphaFill
,
215 ColourAllocated outline
, int alphaOutline
, int flags
);
216 virtual void Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
217 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
);
219 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
220 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
221 virtual void DrawTextTransparent(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
);
222 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, int *positions
);
223 virtual int WidthText(Font
&font_
, const char *s
, int len
);
224 virtual int WidthChar(Font
&font_
, char ch
);
225 virtual int Ascent(Font
&font_
);
226 virtual int Descent(Font
&font_
);
227 virtual int InternalLeading(Font
&font_
);
228 virtual int ExternalLeading(Font
&font_
);
229 virtual int Height(Font
&font_
);
230 virtual int AverageCharWidth(Font
&font_
);
232 virtual int SetPalette(Palette
*pal
, bool inBackGround
);
233 virtual void SetClip(PRectangle rc
);
234 virtual void FlushCachedState();
236 virtual void SetUnicodeMode(bool unicodeMode_
);
237 virtual void SetDBCSMode(int codePage
);
239 void BrushColour(ColourAllocated back
);
240 void SetFont(Font
&font_
);
245 SurfaceImpl::SurfaceImpl() :
246 hdc(0), hdcOwned(0), bitmap(0),
247 x(0), y(0), unicodeMode(0)
250 SurfaceImpl::~SurfaceImpl() {
254 void SurfaceImpl::Init(WindowID wid
) {
257 hdc
= new wxMemoryDC();
260 // On Mac and GTK the DC is not really valid until it has a bitmap
261 // selected into it. So instead of just creating the DC with no bitmap,
262 // go ahead and give it one.
263 InitPixMap(1,1,NULL
,wid
);
267 void SurfaceImpl::Init(SurfaceID hdc_
, WindowID
) {
272 void SurfaceImpl::InitPixMap(int width
, int height
, Surface
*WXUNUSED(surface_
), WindowID
) {
274 hdc
= new wxMemoryDC();
276 if (width
< 1) width
= 1;
277 if (height
< 1) height
= 1;
278 bitmap
= new wxBitmap(width
, height
);
279 ((wxMemoryDC
*)hdc
)->SelectObject(*bitmap
);
283 void SurfaceImpl::Release() {
285 ((wxMemoryDC
*)hdc
)->SelectObject(wxNullBitmap
);
297 bool SurfaceImpl::Initialised() {
302 void SurfaceImpl::PenColour(ColourAllocated fore
) {
303 hdc
->SetPen(wxPen(wxColourFromCA(fore
)));
306 void SurfaceImpl::BrushColour(ColourAllocated back
) {
307 hdc
->SetBrush(wxBrush(wxColourFromCA(back
)));
310 void SurfaceImpl::SetFont(Font
&font_
) {
312 hdc
->SetFont(*((wxFont
*)font_
.GetID()));
316 int SurfaceImpl::LogPixelsY() {
317 return hdc
->GetPPI().y
;
320 int SurfaceImpl::DeviceHeightFont(int points
) {
324 void SurfaceImpl::MoveTo(int x_
, int y_
) {
329 void SurfaceImpl::LineTo(int x_
, int y_
) {
330 hdc
->DrawLine(x
,y
, x_
,y_
);
335 void SurfaceImpl::Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
) {
338 hdc
->DrawPolygon(npts
, (wxPoint
*)pts
);
341 void SurfaceImpl::RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
344 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
347 void SurfaceImpl::FillRectangle(PRectangle rc
, ColourAllocated back
) {
349 hdc
->SetPen(*wxTRANSPARENT_PEN
);
350 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
353 void SurfaceImpl::FillRectangle(PRectangle rc
, Surface
&surfacePattern
) {
355 if (((SurfaceImpl
&)surfacePattern
).bitmap
)
356 br
= wxBrush(*((SurfaceImpl
&)surfacePattern
).bitmap
);
357 else // Something is wrong so display in red
358 br
= wxBrush(*wxRED
);
359 hdc
->SetPen(*wxTRANSPARENT_PEN
);
361 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
364 void SurfaceImpl::RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
367 hdc
->DrawRoundedRectangle(wxRectFromPRectangle(rc
), 4);
371 #define wxPy_premultiply(p, a) ((p) * (a) / 0xff)
373 #define wxPy_premultiply(p, a) (p)
376 void SurfaceImpl::AlphaRectangle(PRectangle rc
, int cornerSize
,
377 ColourAllocated fill
, int alphaFill
,
378 ColourAllocated outline
, int alphaOutline
,
380 #if wxUSE_GRAPHICS_CONTEXT
381 wxGCDC
dc(*(wxMemoryDC
*)hdc
);
382 wxColour
penColour(wxColourFromCAandAlpha(outline
, alphaOutline
));
383 wxColour
brushColour(wxColourFromCAandAlpha(fill
, alphaFill
));
384 dc
.SetPen(wxPen(penColour
));
385 dc
.SetBrush(wxBrush(brushColour
));
386 dc
.DrawRoundedRectangle(wxRectFromPRectangle(rc
), cornerSize
);
390 #ifdef wxHAS_RAW_BITMAP
392 // TODO: do something with cornerSize
393 wxUnusedVar(cornerSize
);
396 wxRect r
= wxRectFromPRectangle(rc
);
397 wxBitmap
bmp(r
.width
, r
.height
, 32);
398 wxAlphaPixelData
pixData(bmp
);
400 // Set the fill pixels
401 ColourDesired
cdf(fill
.AsLong());
402 int red
= cdf
.GetRed();
403 int green
= cdf
.GetGreen();
404 int blue
= cdf
.GetBlue();
406 wxAlphaPixelData::Iterator
p(pixData
);
407 for (y
=0; y
<r
.height
; y
++) {
408 p
.MoveTo(pixData
, 0, y
);
409 for (x
=0; x
<r
.width
; x
++) {
410 p
.Red() = wxPy_premultiply(red
, alphaFill
);
411 p
.Green() = wxPy_premultiply(green
, alphaFill
);
412 p
.Blue() = wxPy_premultiply(blue
, alphaFill
);
413 p
.Alpha() = alphaFill
;
418 // Set the outline pixels
419 ColourDesired
cdo(outline
.AsLong());
421 green
= cdo
.GetGreen();
422 blue
= cdo
.GetBlue();
423 for (x
=0; x
<r
.width
; x
++) {
424 p
.MoveTo(pixData
, x
, 0);
425 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
426 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
427 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
428 p
.Alpha() = alphaOutline
;
429 p
.MoveTo(pixData
, x
, r
.height
-1);
430 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
431 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
432 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
433 p
.Alpha() = alphaOutline
;
436 for (y
=0; y
<r
.height
; y
++) {
437 p
.MoveTo(pixData
, 0, y
);
438 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
439 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
440 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
441 p
.Alpha() = alphaOutline
;
442 p
.MoveTo(pixData
, r
.width
-1, y
);
443 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
444 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
445 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
446 p
.Alpha() = alphaOutline
;
450 hdc
->DrawBitmap(bmp
, r
.x
, r
.y
, true);
453 wxUnusedVar(cornerSize
);
454 wxUnusedVar(alphaFill
);
455 wxUnusedVar(alphaOutline
);
456 RectangleDraw(rc
, outline
, fill
);
461 void SurfaceImpl::Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
464 hdc
->DrawEllipse(wxRectFromPRectangle(rc
));
467 void SurfaceImpl::Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
) {
468 wxRect r
= wxRectFromPRectangle(rc
);
469 hdc
->Blit(r
.x
, r
.y
, r
.width
, r
.height
,
470 ((SurfaceImpl
&)surfaceSource
).hdc
,
471 from
.x
, from
.y
, wxCOPY
);
474 void SurfaceImpl::DrawTextNoClip(PRectangle rc
, Font
&font
, int ybase
,
475 const char *s
, int len
,
476 ColourAllocated fore
, ColourAllocated back
) {
478 hdc
->SetTextForeground(wxColourFromCA(fore
));
479 hdc
->SetTextBackground(wxColourFromCA(back
));
480 FillRectangle(rc
, back
);
482 // ybase is where the baseline should be, but wxWin uses the upper left
483 // corner, so I need to calculate the real position for the text...
484 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
487 void SurfaceImpl::DrawTextClipped(PRectangle rc
, Font
&font
, int ybase
,
488 const char *s
, int len
,
489 ColourAllocated fore
, ColourAllocated back
) {
491 hdc
->SetTextForeground(wxColourFromCA(fore
));
492 hdc
->SetTextBackground(wxColourFromCA(back
));
493 FillRectangle(rc
, back
);
494 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
496 // see comments above
497 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
498 hdc
->DestroyClippingRegion();
502 void SurfaceImpl::DrawTextTransparent(PRectangle rc
, Font
&font
, int ybase
,
503 const char *s
, int len
,
504 ColourAllocated fore
) {
507 hdc
->SetTextForeground(wxColourFromCA(fore
));
508 hdc
->SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
510 // ybase is where the baseline should be, but wxWin uses the upper left
511 // corner, so I need to calculate the real position for the text...
512 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
514 hdc
->SetBackgroundMode(wxBRUSHSTYLE_SOLID
);
518 void SurfaceImpl::MeasureWidths(Font
&font
, const char *s
, int len
, int *positions
) {
520 wxString str
= stc2wx(s
, len
);
525 hdc
->GetPartialTextExtents(str
, tpos
);
528 // Map the widths for UCS-2 characters back to the UTF-8 input string
529 // NOTE: I don't think this is right for when sizeof(wxChar) > 2, ie wxGTK2
530 // so figure it out and fix it!
533 while ((int)i
< len
) {
534 unsigned char uch
= (unsigned char)s
[i
];
535 positions
[i
++] = tpos
[ui
];
537 if (uch
< (0x80 + 0x40 + 0x20)) {
538 positions
[i
++] = tpos
[ui
];
540 positions
[i
++] = tpos
[ui
];
541 positions
[i
++] = tpos
[ui
];
548 // If not unicode then just use the widths we have
550 std::copy(tpos
.begin(), tpos
.end(), positions
);
552 memcpy(positions
, tpos
.begin(), len
* sizeof(int));
558 int SurfaceImpl::WidthText(Font
&font
, const char *s
, int len
) {
563 hdc
->GetTextExtent(stc2wx(s
, len
), &w
, &h
);
568 int SurfaceImpl::WidthChar(Font
&font
, char ch
) {
572 char s
[2] = { ch
, 0 };
574 hdc
->GetTextExtent(stc2wx(s
, 1), &w
, &h
);
578 #define EXTENT_TEST wxT(" `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
580 int SurfaceImpl::Ascent(Font
&font
) {
583 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
588 int SurfaceImpl::Descent(Font
&font
) {
591 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
595 int SurfaceImpl::InternalLeading(Font
&WXUNUSED(font
)) {
599 int SurfaceImpl::ExternalLeading(Font
&font
) {
602 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
606 int SurfaceImpl::Height(Font
&font
) {
608 return hdc
->GetCharHeight() + 1;
611 int SurfaceImpl::AverageCharWidth(Font
&font
) {
613 return hdc
->GetCharWidth();
616 int SurfaceImpl::SetPalette(Palette
*WXUNUSED(pal
), bool WXUNUSED(inBackGround
)) {
620 void SurfaceImpl::SetClip(PRectangle rc
) {
621 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
624 void SurfaceImpl::FlushCachedState() {
627 void SurfaceImpl::SetUnicodeMode(bool unicodeMode_
) {
628 unicodeMode
=unicodeMode_
;
631 void SurfaceImpl::SetDBCSMode(int WXUNUSED(codePage
)) {
632 // dbcsMode = codePage == SC_CP_DBCS;
636 Surface
*Surface::Allocate() {
637 return new SurfaceImpl
;
641 //----------------------------------------------------------------------
644 inline wxWindow
* GETWIN(WindowID id
) { return (wxWindow
*)id
; }
649 void Window::Destroy() {
652 GETWIN(wid
)->Destroy();
657 bool Window::HasFocus() {
658 return wxWindow::FindFocus() == GETWIN(wid
);
661 PRectangle
Window::GetPosition() {
662 if (! wid
) return PRectangle();
663 wxRect
rc(GETWIN(wid
)->GetPosition(), GETWIN(wid
)->GetSize());
664 return PRectangleFromwxRect(rc
);
667 void Window::SetPosition(PRectangle rc
) {
668 wxRect r
= wxRectFromPRectangle(rc
);
669 GETWIN(wid
)->SetSize(r
);
672 void Window::SetPositionRelative(PRectangle rc
, Window
) {
673 SetPosition(rc
); // ????
676 PRectangle
Window::GetClientPosition() {
677 if (! wid
) return PRectangle();
678 wxSize sz
= GETWIN(wid
)->GetClientSize();
679 return PRectangle(0, 0, sz
.x
, sz
.y
);
682 void Window::Show(bool show
) {
683 GETWIN(wid
)->Show(show
);
686 void Window::InvalidateAll() {
687 GETWIN(wid
)->Refresh(false);
690 void Window::InvalidateRectangle(PRectangle rc
) {
691 wxRect r
= wxRectFromPRectangle(rc
);
692 GETWIN(wid
)->Refresh(false, &r
);
695 void Window::SetFont(Font
&font
) {
696 GETWIN(wid
)->SetFont(*((wxFont
*)font
.GetID()));
699 void Window::SetCursor(Cursor curs
) {
700 wxStockCursor cursorId
;
704 cursorId
= wxCURSOR_IBEAM
;
707 cursorId
= wxCURSOR_ARROW
;
710 cursorId
= wxCURSOR_ARROW
; // ** no up arrow... wxCURSOR_UPARROW;
713 cursorId
= wxCURSOR_WAIT
;
716 cursorId
= wxCURSOR_SIZEWE
;
719 cursorId
= wxCURSOR_SIZENS
;
721 case cursorReverseArrow
:
722 cursorId
= wxCURSOR_RIGHT_ARROW
;
725 cursorId
= wxCURSOR_HAND
;
728 cursorId
= wxCURSOR_ARROW
;
732 wxCursor wc
= wxCursor(cursorId
);
733 if(curs
!= cursorLast
)
735 GETWIN(wid
)->SetCursor(wc
);
741 void Window::SetTitle(const char *s
) {
742 GETWIN(wid
)->SetLabel(stc2wx(s
));
746 // Returns rectangle of monitor pt is on
747 PRectangle
Window::GetMonitorRect(Point pt
) {
749 if (! wid
) return PRectangle();
751 // Get the display the point is found on
752 int n
= wxDisplay::GetFromPoint(wxPoint(pt
.x
, pt
.y
));
753 wxDisplay
dpy(n
== wxNOT_FOUND
? 0 : n
);
754 rect
= dpy
.GetGeometry();
758 return PRectangleFromwxRect(rect
);
761 //----------------------------------------------------------------------
762 // Helper classes for ListBox
765 // This is a simple subclass of wxListView that just resets focus to the
766 // parent when it gets it.
767 class wxSTCListBox
: public wxListView
{
769 wxSTCListBox(wxWindow
* parent
, wxWindowID id
,
770 const wxPoint
& pos
, const wxSize
& size
,
775 Hide(); // don't flicker as we move it around...
777 Create(parent
, id
, pos
, size
, style
);
781 void OnFocus(wxFocusEvent
& event
) {
782 GetParent()->SetFocus();
786 void OnKillFocus(wxFocusEvent
& WXUNUSED(event
)) {
787 // Do nothing. Prevents base class from resetting the colors...
791 // For some reason I don't understand yet the focus doesn't really leave
792 // the listbox like it should, so if we get any events feed them back to
794 void OnKeyDown(wxKeyEvent
& event
) {
795 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
797 void OnChar(wxKeyEvent
& event
) {
798 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
801 // And we need to force the focus back when being destroyed
803 GetGrandParent()->SetFocus();
808 DECLARE_EVENT_TABLE()
811 BEGIN_EVENT_TABLE(wxSTCListBox
, wxListView
)
812 EVT_SET_FOCUS( wxSTCListBox::OnFocus
)
813 EVT_KILL_FOCUS(wxSTCListBox::OnKillFocus
)
815 EVT_KEY_DOWN( wxSTCListBox::OnKeyDown
)
816 EVT_CHAR( wxSTCListBox::OnChar
)
822 #if wxUSE_POPUPWIN //-----------------------------------
823 #include "wx/popupwin.h"
825 // A popup window to place the wxSTCListBox upon
826 class wxSTCListBoxWin
: public wxPopupWindow
830 CallBackAction doubleClickAction
;
831 void* doubleClickActionData
;
833 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point
WXUNUSED(location
)) :
834 wxPopupWindow(parent
, wxBORDER_SIMPLE
)
837 lv
= new wxSTCListBox(parent
, id
, wxPoint(-50,-50), wxDefaultSize
,
838 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxBORDER_NONE
);
839 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
840 lv
->InsertColumn(0, wxEmptyString
);
841 lv
->InsertColumn(1, wxEmptyString
);
843 // NOTE: We need to fool the wxListView into thinking that it has the
844 // focus so it will use the normal selection colour and will look
845 // "right" to the user. But since the wxPopupWindow or its children
846 // can't receive focus then we have to pull a fast one and temporarily
847 // parent the listctrl on the STC window and then call SetFocus and
848 // then reparent it back to the popup.
857 // Set position in client coords
858 virtual void DoSetSize(int x
, int y
,
859 int width
, int height
,
860 int sizeFlags
= wxSIZE_AUTO
) {
861 if (x
!= wxDefaultCoord
) {
862 GetParent()->ClientToScreen(&x
, NULL
);
864 if (y
!= wxDefaultCoord
) {
865 GetParent()->ClientToScreen(NULL
, &y
);
867 wxPopupWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
870 // return position as if it were in client coords
871 virtual void DoGetPosition( int *x
, int *y
) const {
873 wxPopupWindow::DoGetPosition(&sx
, &sy
);
874 GetParent()->ScreenToClient(&sx
, &sy
);
881 if ( !wxPendingDelete
.Member(this) )
882 wxPendingDelete
.Append(this);
888 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
891 il
->GetSize(0, w
, h
);
898 void SetDoubleClickAction(CallBackAction action
, void *data
) {
899 doubleClickAction
= action
;
900 doubleClickActionData
= data
;
904 void OnFocus(wxFocusEvent
& event
) {
905 GetParent()->SetFocus();
909 void OnSize(wxSizeEvent
& event
) {
910 // resize the child to fill the popup
911 wxSize sz
= GetClientSize();
912 lv
->SetSize(0, 0, sz
.x
, sz
.y
);
913 // reset the column widths
914 lv
->SetColumnWidth(0, IconWidth()+4);
915 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
916 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
920 void OnActivate(wxListEvent
& WXUNUSED(event
)) {
921 doubleClickAction(doubleClickActionData
);
924 wxListView
* GetLB() { return lv
; }
927 DECLARE_EVENT_TABLE()
931 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxPopupWindow
)
932 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
933 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
934 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
939 #else // !wxUSE_POPUPWIN -----------------------------------
940 #include "wx/frame.h"
942 // A normal window to place the wxSTCListBox upon, but make it behave as much
943 // like a wxPopupWindow as possible
944 class wxSTCListBoxWin
: public wxFrame
{
947 CallBackAction doubleClickAction
;
948 void* doubleClickActionData
;
950 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point location
) :
951 wxFrame(parent
, id
, wxEmptyString
, wxPoint(location
.x
, location
.y
), wxSize(0,0),
953 | wxFRAME_FLOAT_ON_PARENT
963 lv
= new wxSTCListBox(this, id
, wxDefaultPosition
, wxDefaultSize
,
964 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxNO_BORDER
);
965 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
966 lv
->InsertColumn(0, wxEmptyString
);
967 lv
->InsertColumn(1, wxEmptyString
);
969 // Eventhough we immediately reset the focus to the parent, this helps
970 // things to look right...
977 // On OSX and (possibly others) there can still be pending
978 // messages/events for the list control when Scintilla wants to
979 // close it, so do a pending delete of it instead of destroying
984 // The bottom edge of this window is not getting properly
985 // refreshed upon deletion, so help it out...
986 wxWindow
* p
= GetParent();
987 wxRect
r(GetPosition(), GetSize());
988 r
.SetHeight(r
.GetHeight()+1);
989 p
->Refresh(false, &r
);
991 if ( !wxPendingDelete
.Member(this) )
992 wxPendingDelete
.Append(this);
999 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
1002 il
->GetSize(0, w
, h
);
1009 void SetDoubleClickAction(CallBackAction action
, void *data
)
1011 doubleClickAction
= action
;
1012 doubleClickActionData
= data
;
1016 void OnFocus(wxFocusEvent
& event
)
1019 GetParent()->SetFocus();
1023 void OnSize(wxSizeEvent
& event
)
1026 wxSize sz
= GetClientSize();
1028 // reset the column widths
1029 lv
->SetColumnWidth(0, IconWidth()+4);
1030 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
1031 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
1035 void ActivateParent()
1037 // Although we're a frame, we always want the parent to be active, so
1038 // raise it whenever we get shown, focused, etc.
1039 wxTopLevelWindow
*frame
= wxDynamicCast(
1040 wxGetTopLevelParent(GetParent()), wxTopLevelWindow
);
1046 virtual void DoSetSize(int x
, int y
,
1047 int width
, int height
,
1048 int sizeFlags
= wxSIZE_AUTO
)
1050 // convert coords to screen coords since we're a top-level window
1051 if (x
!= wxDefaultCoord
) {
1052 GetParent()->ClientToScreen(&x
, NULL
);
1054 if (y
!= wxDefaultCoord
) {
1055 GetParent()->ClientToScreen(NULL
, &y
);
1057 wxFrame::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1060 virtual bool Show(bool show
= true)
1062 bool rv
= wxFrame::Show(show
);
1066 GetParent()->Refresh(false);
1071 void OnActivate(wxListEvent
& WXUNUSED(event
))
1073 doubleClickAction(doubleClickActionData
);
1076 wxListView
* GetLB() { return lv
; }
1079 DECLARE_EVENT_TABLE()
1083 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxWindow
)
1084 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
1085 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
1086 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
1089 #endif // wxUSE_POPUPWIN -----------------------------------
1092 inline wxSTCListBoxWin
* GETLBW(WindowID win
) {
1093 return ((wxSTCListBoxWin
*)win
);
1096 inline wxListView
* GETLB(WindowID win
) {
1097 return GETLBW(win
)->GetLB();
1100 //----------------------------------------------------------------------
1102 class ListBoxImpl
: public ListBox
{
1106 int desiredVisibleRows
;
1109 Point location
; // Caret location at which the list is opened
1110 wxImageList
* imgList
;
1111 wxArrayInt
* imgTypeMap
;
1117 virtual void SetFont(Font
&font
);
1118 virtual void Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
);
1119 virtual void SetAverageCharWidth(int width
);
1120 virtual void SetVisibleRows(int rows
);
1121 virtual int GetVisibleRows() const;
1122 virtual PRectangle
GetDesiredRect();
1123 virtual int CaretFromEdge();
1124 virtual void Clear();
1125 virtual void Append(char *s
, int type
= -1);
1126 void Append(const wxString
& text
, int type
);
1127 virtual int Length();
1128 virtual void Select(int n
);
1129 virtual int GetSelection();
1130 virtual int Find(const char *prefix
);
1131 virtual void GetValue(int n
, char *value
, int len
);
1132 virtual void RegisterImage(int type
, const char *xpm_data
);
1133 virtual void ClearRegisteredImages();
1134 virtual void SetDoubleClickAction(CallBackAction
, void *);
1135 virtual void SetList(const char* list
, char separator
, char typesep
);
1139 ListBoxImpl::ListBoxImpl()
1140 : lineHeight(10), unicodeMode(false),
1141 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
1142 imgList(NULL
), imgTypeMap(NULL
)
1146 ListBoxImpl::~ListBoxImpl() {
1158 void ListBoxImpl::SetFont(Font
&font
) {
1159 GETLB(wid
)->SetFont(*((wxFont
*)font
.GetID()));
1163 void ListBoxImpl::Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
) {
1164 location
= location_
;
1165 lineHeight
= lineHeight_
;
1166 unicodeMode
= unicodeMode_
;
1168 wid
= new wxSTCListBoxWin(GETWIN(parent
.GetID()), ctrlID
, location
);
1169 if (imgList
!= NULL
)
1170 GETLB(wid
)->SetImageList(imgList
, wxIMAGE_LIST_SMALL
);
1174 void ListBoxImpl::SetAverageCharWidth(int width
) {
1175 aveCharWidth
= width
;
1179 void ListBoxImpl::SetVisibleRows(int rows
) {
1180 desiredVisibleRows
= rows
;
1184 int ListBoxImpl::GetVisibleRows() const {
1185 return desiredVisibleRows
;
1188 PRectangle
ListBoxImpl::GetDesiredRect() {
1189 // wxListCtrl doesn't have a DoGetBestSize, so instead we kept track of
1190 // the max size in Append and calculate it here...
1191 int maxw
= maxStrWidth
* aveCharWidth
;
1194 // give it a default if there are no lines, and/or add a bit more
1195 if (maxw
== 0) maxw
= 100;
1196 maxw
+= aveCharWidth
* 3 +
1197 GETLBW(wid
)->IconWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1201 // estimate a desired height
1202 int count
= GETLB(wid
)->GetItemCount();
1205 GETLB(wid
)->GetItemRect(0, rect
);
1206 maxh
= count
* rect
.GetHeight();
1207 if (maxh
> 140) // TODO: Use desiredVisibleRows??
1210 // Try to make the size an exact multiple of some number of lines
1211 int lines
= maxh
/ rect
.GetHeight();
1212 maxh
= (lines
+ 1) * rect
.GetHeight() + 2;
1226 int ListBoxImpl::CaretFromEdge() {
1227 return 4 + GETLBW(wid
)->IconWidth();
1231 void ListBoxImpl::Clear() {
1232 GETLB(wid
)->DeleteAllItems();
1236 void ListBoxImpl::Append(char *s
, int type
) {
1237 Append(stc2wx(s
), type
);
1240 void ListBoxImpl::Append(const wxString
& text
, int type
) {
1241 long count
= GETLB(wid
)->GetItemCount();
1242 long itemID
= GETLB(wid
)->InsertItem(count
, wxEmptyString
);
1244 GETLB(wid
)->SetItem(itemID
, 1, text
);
1245 maxStrWidth
= wxMax(maxStrWidth
, text
.length());
1247 wxCHECK_RET(imgTypeMap
, wxT("Unexpected NULL imgTypeMap"));
1248 idx
= imgTypeMap
->Item(type
);
1250 GETLB(wid
)->SetItemImage(itemID
, idx
, idx
);
1253 void ListBoxImpl::SetList(const char* list
, char separator
, char typesep
) {
1254 GETLB(wid
)->Freeze();
1256 wxStringTokenizer
tkzr(stc2wx(list
), (wxChar
)separator
);
1257 while ( tkzr
.HasMoreTokens() ) {
1258 wxString token
= tkzr
.GetNextToken();
1260 int pos
= token
.Find(typesep
);
1262 token
.Mid(pos
+1).ToLong(&type
);
1263 token
.Truncate(pos
);
1265 Append(token
, (int)type
);
1271 int ListBoxImpl::Length() {
1272 return GETLB(wid
)->GetItemCount();
1276 void ListBoxImpl::Select(int n
) {
1282 GETLB(wid
)->EnsureVisible(n
);
1283 GETLB(wid
)->Select(n
, select
);
1287 int ListBoxImpl::GetSelection() {
1288 return GETLB(wid
)->GetFirstSelected();
1292 int ListBoxImpl::Find(const char *WXUNUSED(prefix
)) {
1298 void ListBoxImpl::GetValue(int n
, char *value
, int len
) {
1302 item
.SetMask(wxLIST_MASK_TEXT
);
1303 GETLB(wid
)->GetItem(item
);
1304 strncpy(value
, wx2stc(item
.GetText()), len
);
1305 value
[len
-1] = '\0';
1309 void ListBoxImpl::RegisterImage(int type
, const char *xpm_data
) {
1310 wxMemoryInputStream
stream(xpm_data
, strlen(xpm_data
)+1);
1311 wxImage
img(stream
, wxBITMAP_TYPE_XPM
);
1315 // assumes all images are the same size
1316 imgList
= new wxImageList(bmp
.GetWidth(), bmp
.GetHeight(), true);
1317 imgTypeMap
= new wxArrayInt
;
1320 int idx
= imgList
->Add(bmp
);
1322 // do we need to extend the mapping array?
1323 wxArrayInt
& itm
= *imgTypeMap
;
1324 if ( itm
.GetCount() < (size_t)type
+1)
1325 itm
.Add(-1, type
- itm
.GetCount() + 1);
1327 // Add an item that maps type to the image index
1331 void ListBoxImpl::ClearRegisteredImages() {
1341 GETLB(wid
)->SetImageList(NULL
, wxIMAGE_LIST_SMALL
);
1345 void ListBoxImpl::SetDoubleClickAction(CallBackAction action
, void *data
) {
1346 GETLBW(wid
)->SetDoubleClickAction(action
, data
);
1350 ListBox::ListBox() {
1353 ListBox::~ListBox() {
1356 ListBox
*ListBox::Allocate() {
1357 return new ListBoxImpl();
1360 //----------------------------------------------------------------------
1362 Menu::Menu() : mid(0) {
1365 void Menu::CreatePopUp() {
1370 void Menu::Destroy() {
1372 delete (wxMenu
*)mid
;
1376 void Menu::Show(Point pt
, Window
&w
) {
1377 GETWIN(w
.GetID())->PopupMenu((wxMenu
*)mid
, pt
.x
- 4, pt
.y
);
1381 //----------------------------------------------------------------------
1383 DynamicLibrary
*DynamicLibrary::Load(const char *WXUNUSED(modulePath
)) {
1384 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1388 //----------------------------------------------------------------------
1390 ColourDesired
Platform::Chrome() {
1392 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1393 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1396 ColourDesired
Platform::ChromeHighlight() {
1398 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
);
1399 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1402 const char *Platform::DefaultFont() {
1403 static char buf
[128];
1404 strcpy(buf
, wxNORMAL_FONT
->GetFaceName().mbc_str());
1408 int Platform::DefaultFontSize() {
1409 return wxNORMAL_FONT
->GetPointSize();
1412 unsigned int Platform::DoubleClickTime() {
1413 return 500; // **** ::GetDoubleClickTime();
1416 bool Platform::MouseButtonBounce() {
1420 bool Platform::IsKeyDown(int WXUNUSED(key
)) {
1421 return false; // I don't think we'll need this.
1424 long Platform::SendScintilla(WindowID w
,
1426 unsigned long wParam
,
1429 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1430 return stc
->SendMsg(msg
, wParam
, lParam
);
1433 long Platform::SendScintillaPointer(WindowID w
,
1435 unsigned long wParam
,
1438 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1439 return stc
->SendMsg(msg
, wParam
, (wxIntPtr
)lParam
);
1443 // These are utility functions not really tied to a platform
1445 int Platform::Minimum(int a
, int b
) {
1452 int Platform::Maximum(int a
, int b
) {
1461 void Platform::DebugDisplay(const char *s
) {
1463 wxLogDebug(stc2wx(s
));
1469 void Platform::DebugPrintf(const char *format
, ...) {
1473 va_start(pArguments
, format
);
1474 vsprintf(buffer
,format
,pArguments
);
1476 Platform::DebugDisplay(buffer
);
1478 wxUnusedVar(format
);
1483 static bool assertionPopUps
= true;
1485 bool Platform::ShowAssertionPopUps(bool assertionPopUps_
) {
1486 bool ret
= assertionPopUps
;
1487 assertionPopUps
= assertionPopUps_
;
1491 void Platform::Assert(const char *c
, const char *file
, int line
) {
1494 sprintf(buffer
, "Assertion [%s] failed at %s %d", c
, file
, line
);
1495 if (assertionPopUps
) {
1497 wxMessageBox(stc2wx(buffer
),
1498 wxT("Assertion failure"),
1499 wxICON_HAND
| wxOK
);
1501 strcat(buffer
, "\r\n");
1502 Platform::DebugDisplay(buffer
);
1513 int Platform::Clamp(int val
, int minVal
, int maxVal
) {
1522 bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage
), char WXUNUSED(ch
)) {
1526 int Platform::DBCSCharLength(int WXUNUSED(codePage
), const char *WXUNUSED(s
)) {
1530 int Platform::DBCSCharMaxLength() {
1535 //----------------------------------------------------------------------
1537 ElapsedTime::ElapsedTime() {
1538 wxLongLong localTime
= wxGetLocalTimeMillis();
1539 littleBit
= localTime
.GetLo();
1540 bigBit
= localTime
.GetHi();
1543 double ElapsedTime::Duration(bool reset
) {
1544 wxLongLong
prevTime(bigBit
, littleBit
);
1545 wxLongLong localTime
= wxGetLocalTimeMillis();
1547 littleBit
= localTime
.GetLo();
1548 bigBit
= localTime
.GetHi();
1550 wxLongLong duration
= localTime
- prevTime
;
1551 double result
= duration
.ToDouble();
1557 //----------------------------------------------------------------------
1561 #include "UniConversion.h"
1563 // Convert using Scintilla's functions instead of wx's, Scintilla's are more
1564 // forgiving and won't assert...
1566 wxString
stc2wx(const char* str
, size_t len
)
1569 return wxEmptyString
;
1571 size_t wclen
= UTF16Length(str
, len
);
1572 wxWCharBuffer
buffer(wclen
+1);
1574 size_t actualLen
= UTF16FromUTF8(str
, len
, buffer
.data(), wclen
+1);
1575 return wxString(buffer
.data(), actualLen
);
1580 wxString
stc2wx(const char* str
)
1582 return stc2wx(str
, strlen(str
));
1586 const wxWX2MBbuf
wx2stc(const wxString
& str
)
1588 const wchar_t* wcstr
= str
.c_str();
1589 size_t wclen
= str
.length();
1590 size_t len
= UTF8Length(wcstr
, wclen
);
1592 wxCharBuffer
buffer(len
+1);
1593 UTF8FromUTF16(wcstr
, wclen
, buffer
.data(), len
);
1595 // TODO check NULL termination!!