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 #include "wx/rawbmp.h"
39 #include "wx/stc/stc.h"
40 #include "wx/stc/private.h"
44 Point
Point::FromLong(long lpoint
) {
45 return Point(lpoint
& 0xFFFF, lpoint
>> 16);
48 wxRect
wxRectFromPRectangle(PRectangle prc
) {
49 wxRect
r(prc
.left
, prc
.top
,
50 prc
.Width(), prc
.Height());
54 PRectangle
PRectangleFromwxRect(wxRect rc
) {
55 return PRectangle(rc
.GetLeft(), rc
.GetTop(),
56 rc
.GetRight()+1, rc
.GetBottom()+1);
59 wxColour
wxColourFromCA(const ColourAllocated
& ca
) {
60 ColourDesired
cd(ca
.AsLong());
61 return wxColour((unsigned char)cd
.GetRed(),
62 (unsigned char)cd
.GetGreen(),
63 (unsigned char)cd
.GetBlue());
66 //----------------------------------------------------------------------
70 allowRealization
= false;
72 entries
= new ColourPair
[size
];
81 void Palette::Release() {
85 entries
= new ColourPair
[size
];
88 // This method either adds a colour to the list of wanted colours (want==true)
89 // or retrieves the allocated colour back to the ColourPair.
90 // This is one method to make it easier to keep the code for wanting and retrieving in sync.
91 void Palette::WantFind(ColourPair
&cp
, bool want
) {
93 for (int i
=0; i
< used
; i
++) {
94 if (entries
[i
].desired
== cp
.desired
)
99 int sizeNew
= size
* 2;
100 ColourPair
*entriesNew
= new ColourPair
[sizeNew
];
101 for (int j
=0; j
<size
; j
++) {
102 entriesNew
[j
] = entries
[j
];
105 entries
= entriesNew
;
109 entries
[used
].desired
= cp
.desired
;
110 entries
[used
].allocated
.Set(cp
.desired
.AsLong());
113 for (int i
=0; i
< used
; i
++) {
114 if (entries
[i
].desired
== cp
.desired
) {
115 cp
.allocated
= entries
[i
].allocated
;
119 cp
.allocated
.Set(cp
.desired
.AsLong());
123 void Palette::Allocate(Window
&) {
124 if (allowRealization
) {
129 //----------------------------------------------------------------------
139 void Font::Create(const char *faceName
, int characterSet
,
140 int size
, bool bold
, bool italic
,
141 bool extraFontFlag
) {
144 // The minus one is done because since Scintilla uses SC_SHARSET_DEFAULT
145 // internally and we need to have wxFONENCODING_DEFAULT == SC_SHARSET_DEFAULT
146 // so we adjust the encoding before passing it to Scintilla. See also
147 // wxStyledTextCtrl::StyleSetCharacterSet
148 wxFontEncoding encoding
= (wxFontEncoding
)(characterSet
-1);
150 wxFontEncodingArray ea
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
154 wxFont
* font
= new wxFont(size
,
156 italic
? wxITALIC
: wxNORMAL
,
157 bold
? wxBOLD
: wxNORMAL
,
161 font
->SetNoAntiAliasing(!extraFontFlag
);
166 void Font::Release() {
172 //----------------------------------------------------------------------
174 class SurfaceImpl
: public Surface
{
187 virtual void Init(WindowID wid
);
188 virtual void Init(SurfaceID sid
, WindowID wid
);
189 virtual void InitPixMap(int width
, int height
, Surface
*surface_
, WindowID wid
);
191 virtual void Release();
192 virtual bool Initialised();
193 virtual void PenColour(ColourAllocated fore
);
194 virtual int LogPixelsY();
195 virtual int DeviceHeightFont(int points
);
196 virtual void MoveTo(int x_
, int y_
);
197 virtual void LineTo(int x_
, int y_
);
198 virtual void Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
);
199 virtual void RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
200 virtual void FillRectangle(PRectangle rc
, ColourAllocated back
);
201 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
);
202 virtual void RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
203 virtual void AlphaRectangle(PRectangle rc
, int cornerSize
, ColourAllocated fill
, int alphaFill
,
204 ColourAllocated outline
, int alphaOutline
, int flags
);
205 virtual void Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
206 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
);
208 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
209 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
210 virtual void DrawTextTransparent(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
);
211 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, int *positions
);
212 virtual int WidthText(Font
&font_
, const char *s
, int len
);
213 virtual int WidthChar(Font
&font_
, char ch
);
214 virtual int Ascent(Font
&font_
);
215 virtual int Descent(Font
&font_
);
216 virtual int InternalLeading(Font
&font_
);
217 virtual int ExternalLeading(Font
&font_
);
218 virtual int Height(Font
&font_
);
219 virtual int AverageCharWidth(Font
&font_
);
221 virtual int SetPalette(Palette
*pal
, bool inBackGround
);
222 virtual void SetClip(PRectangle rc
);
223 virtual void FlushCachedState();
225 virtual void SetUnicodeMode(bool unicodeMode_
);
226 virtual void SetDBCSMode(int codePage
);
228 void BrushColour(ColourAllocated back
);
229 void SetFont(Font
&font_
);
234 SurfaceImpl::SurfaceImpl() :
235 hdc(0), hdcOwned(0), bitmap(0),
236 x(0), y(0), unicodeMode(0)
239 SurfaceImpl::~SurfaceImpl() {
243 void SurfaceImpl::Init(WindowID wid
) {
246 hdc
= new wxMemoryDC();
249 // On Mac and GTK the DC is not really valid until it has a bitmap
250 // selected into it. So instead of just creating the DC with no bitmap,
251 // go ahead and give it one.
252 InitPixMap(1,1,NULL
,wid
);
256 void SurfaceImpl::Init(SurfaceID hdc_
, WindowID
) {
261 void SurfaceImpl::InitPixMap(int width
, int height
, Surface
*WXUNUSED(surface_
), WindowID
) {
263 hdc
= new wxMemoryDC();
265 if (width
< 1) width
= 1;
266 if (height
< 1) height
= 1;
267 bitmap
= new wxBitmap(width
, height
);
268 ((wxMemoryDC
*)hdc
)->SelectObject(*bitmap
);
272 void SurfaceImpl::Release() {
274 ((wxMemoryDC
*)hdc
)->SelectObject(wxNullBitmap
);
286 bool SurfaceImpl::Initialised() {
291 void SurfaceImpl::PenColour(ColourAllocated fore
) {
292 hdc
->SetPen(wxPen(wxColourFromCA(fore
)));
295 void SurfaceImpl::BrushColour(ColourAllocated back
) {
296 hdc
->SetBrush(wxBrush(wxColourFromCA(back
)));
299 void SurfaceImpl::SetFont(Font
&font_
) {
301 hdc
->SetFont(*((wxFont
*)font_
.GetID()));
305 int SurfaceImpl::LogPixelsY() {
306 return hdc
->GetPPI().y
;
309 int SurfaceImpl::DeviceHeightFont(int points
) {
313 void SurfaceImpl::MoveTo(int x_
, int y_
) {
318 void SurfaceImpl::LineTo(int x_
, int y_
) {
319 hdc
->DrawLine(x
,y
, x_
,y_
);
324 void SurfaceImpl::Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
) {
327 hdc
->DrawPolygon(npts
, (wxPoint
*)pts
);
330 void SurfaceImpl::RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
333 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
336 void SurfaceImpl::FillRectangle(PRectangle rc
, ColourAllocated back
) {
338 hdc
->SetPen(*wxTRANSPARENT_PEN
);
339 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
342 void SurfaceImpl::FillRectangle(PRectangle rc
, Surface
&surfacePattern
) {
344 if (((SurfaceImpl
&)surfacePattern
).bitmap
)
345 br
= wxBrush(*((SurfaceImpl
&)surfacePattern
).bitmap
);
346 else // Something is wrong so display in red
347 br
= wxBrush(*wxRED
);
348 hdc
->SetPen(*wxTRANSPARENT_PEN
);
350 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
353 void SurfaceImpl::RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
356 hdc
->DrawRoundedRectangle(wxRectFromPRectangle(rc
), 4);
360 #define wxPy_premultiply(p, a) ((p) * (a) / 0xff)
362 #define wxPy_premultiply(p, a) (p)
365 void SurfaceImpl::AlphaRectangle(PRectangle rc
, int cornerSize
,
366 ColourAllocated fill
, int alphaFill
,
367 ColourAllocated outline
, int alphaOutline
,
370 // TODO: do something with cornerSize
371 wxUnusedVar(cornerSize
);
374 wxRect r
= wxRectFromPRectangle(rc
);
375 wxBitmap
bmp(r
.width
, r
.height
, 32);
376 wxAlphaPixelData
pixData(bmp
);
378 // Set the fill pixels
379 ColourDesired
cdf(fill
.AsLong());
380 int red
= cdf
.GetRed();
381 int green
= cdf
.GetGreen();
382 int blue
= cdf
.GetBlue();
384 wxAlphaPixelData::Iterator
p(pixData
);
385 for (y
=0; y
<r
.height
; y
++) {
386 p
.MoveTo(pixData
, 0, y
);
387 for (x
=0; x
<r
.width
; x
++) {
388 p
.Red() = wxPy_premultiply(red
, alphaFill
);
389 p
.Green() = wxPy_premultiply(green
, alphaFill
);
390 p
.Blue() = wxPy_premultiply(blue
, alphaFill
);
391 p
.Alpha() = alphaFill
;
396 // Set the outline pixels
397 ColourDesired
cdo(outline
.AsLong());
399 green
= cdo
.GetGreen();
400 blue
= cdo
.GetBlue();
401 for (x
=0; x
<r
.width
; x
++) {
402 p
.MoveTo(pixData
, x
, 0);
403 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
404 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
405 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
406 p
.Alpha() = alphaOutline
;
407 p
.MoveTo(pixData
, x
, r
.height
-1);
408 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
409 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
410 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
411 p
.Alpha() = alphaOutline
;
414 for (y
=0; y
<r
.height
; y
++) {
415 p
.MoveTo(pixData
, 0, y
);
416 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
417 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
418 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
419 p
.Alpha() = alphaOutline
;
420 p
.MoveTo(pixData
, r
.width
-1, y
);
421 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
422 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
423 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
424 p
.Alpha() = alphaOutline
;
428 hdc
->DrawBitmap(bmp
, r
.x
, r
.y
, true);
431 void SurfaceImpl::Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
434 hdc
->DrawEllipse(wxRectFromPRectangle(rc
));
437 void SurfaceImpl::Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
) {
438 wxRect r
= wxRectFromPRectangle(rc
);
439 hdc
->Blit(r
.x
, r
.y
, r
.width
, r
.height
,
440 ((SurfaceImpl
&)surfaceSource
).hdc
,
441 from
.x
, from
.y
, wxCOPY
);
444 void SurfaceImpl::DrawTextNoClip(PRectangle rc
, Font
&font
, int ybase
,
445 const char *s
, int len
,
446 ColourAllocated fore
, ColourAllocated back
) {
448 hdc
->SetTextForeground(wxColourFromCA(fore
));
449 hdc
->SetTextBackground(wxColourFromCA(back
));
450 FillRectangle(rc
, back
);
452 // ybase is where the baseline should be, but wxWin uses the upper left
453 // corner, so I need to calculate the real position for the text...
454 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
457 void SurfaceImpl::DrawTextClipped(PRectangle rc
, Font
&font
, int ybase
,
458 const char *s
, int len
,
459 ColourAllocated fore
, ColourAllocated back
) {
461 hdc
->SetTextForeground(wxColourFromCA(fore
));
462 hdc
->SetTextBackground(wxColourFromCA(back
));
463 FillRectangle(rc
, back
);
464 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
466 // see comments above
467 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
468 hdc
->DestroyClippingRegion();
472 void SurfaceImpl::DrawTextTransparent(PRectangle rc
, Font
&font
, int ybase
,
473 const char *s
, int len
,
474 ColourAllocated fore
) {
477 hdc
->SetTextForeground(wxColourFromCA(fore
));
478 hdc
->SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
480 // ybase is where the baseline should be, but wxWin uses the upper left
481 // corner, so I need to calculate the real position for the text...
482 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
484 hdc
->SetBackgroundMode(wxBRUSHSTYLE_SOLID
);
488 void SurfaceImpl::MeasureWidths(Font
&font
, const char *s
, int len
, int *positions
) {
490 wxString str
= stc2wx(s
, len
);
495 hdc
->GetPartialTextExtents(str
, tpos
);
498 // Map the widths for UCS-2 characters back to the UTF-8 input string
499 // NOTE: I don't think this is right for when sizeof(wxChar) > 2, ie wxGTK2
500 // so figure it out and fix it!
503 while ((int)i
< len
) {
504 unsigned char uch
= (unsigned char)s
[i
];
505 positions
[i
++] = tpos
[ui
];
507 if (uch
< (0x80 + 0x40 + 0x20)) {
508 positions
[i
++] = tpos
[ui
];
510 positions
[i
++] = tpos
[ui
];
511 positions
[i
++] = tpos
[ui
];
518 // If not unicode then just use the widths we have
520 std::copy(tpos
.begin(), tpos
.end(), positions
);
522 memcpy(positions
, tpos
.begin(), len
* sizeof(int));
528 int SurfaceImpl::WidthText(Font
&font
, const char *s
, int len
) {
533 hdc
->GetTextExtent(stc2wx(s
, len
), &w
, &h
);
538 int SurfaceImpl::WidthChar(Font
&font
, char ch
) {
542 char s
[2] = { ch
, 0 };
544 hdc
->GetTextExtent(stc2wx(s
, 1), &w
, &h
);
548 #define EXTENT_TEST wxT(" `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
550 int SurfaceImpl::Ascent(Font
&font
) {
553 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
558 int SurfaceImpl::Descent(Font
&font
) {
561 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
565 int SurfaceImpl::InternalLeading(Font
&WXUNUSED(font
)) {
569 int SurfaceImpl::ExternalLeading(Font
&font
) {
572 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
576 int SurfaceImpl::Height(Font
&font
) {
578 return hdc
->GetCharHeight() + 1;
581 int SurfaceImpl::AverageCharWidth(Font
&font
) {
583 return hdc
->GetCharWidth();
586 int SurfaceImpl::SetPalette(Palette
*WXUNUSED(pal
), bool WXUNUSED(inBackGround
)) {
590 void SurfaceImpl::SetClip(PRectangle rc
) {
591 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
594 void SurfaceImpl::FlushCachedState() {
597 void SurfaceImpl::SetUnicodeMode(bool unicodeMode_
) {
598 unicodeMode
=unicodeMode_
;
601 void SurfaceImpl::SetDBCSMode(int WXUNUSED(codePage
)) {
602 // dbcsMode = codePage == SC_CP_DBCS;
606 Surface
*Surface::Allocate() {
607 return new SurfaceImpl
;
611 //----------------------------------------------------------------------
614 inline wxWindow
* GETWIN(WindowID id
) { return (wxWindow
*)id
; }
619 void Window::Destroy() {
622 GETWIN(id
)->Destroy();
627 bool Window::HasFocus() {
628 return wxWindow::FindFocus() == GETWIN(id
);
631 PRectangle
Window::GetPosition() {
632 if (! id
) return PRectangle();
633 wxRect
rc(GETWIN(id
)->GetPosition(), GETWIN(id
)->GetSize());
634 return PRectangleFromwxRect(rc
);
637 void Window::SetPosition(PRectangle rc
) {
638 wxRect r
= wxRectFromPRectangle(rc
);
639 GETWIN(id
)->SetSize(r
);
642 void Window::SetPositionRelative(PRectangle rc
, Window
) {
643 SetPosition(rc
); // ????
646 PRectangle
Window::GetClientPosition() {
647 if (! id
) return PRectangle();
648 wxSize sz
= GETWIN(id
)->GetClientSize();
649 return PRectangle(0, 0, sz
.x
, sz
.y
);
652 void Window::Show(bool show
) {
653 GETWIN(id
)->Show(show
);
656 void Window::InvalidateAll() {
657 GETWIN(id
)->Refresh(false);
660 void Window::InvalidateRectangle(PRectangle rc
) {
661 wxRect r
= wxRectFromPRectangle(rc
);
662 GETWIN(id
)->Refresh(false, &r
);
665 void Window::SetFont(Font
&font
) {
666 GETWIN(id
)->SetFont(*((wxFont
*)font
.GetID()));
669 void Window::SetCursor(Cursor curs
) {
674 cursorId
= wxCURSOR_IBEAM
;
677 cursorId
= wxCURSOR_ARROW
;
680 cursorId
= wxCURSOR_ARROW
; // ** no up arrow... wxCURSOR_UPARROW;
683 cursorId
= wxCURSOR_WAIT
;
686 cursorId
= wxCURSOR_SIZEWE
;
689 cursorId
= wxCURSOR_SIZENS
;
691 case cursorReverseArrow
:
692 cursorId
= wxCURSOR_RIGHT_ARROW
;
695 cursorId
= wxCURSOR_HAND
;
698 cursorId
= wxCURSOR_ARROW
;
702 wxCursor wc
= wxStockCursor(cursorId
) ;
704 wxCursor wc
= wxCursor(cursorId
) ;
706 if(curs
!= cursorLast
)
708 GETWIN(id
)->SetCursor(wc
);
714 void Window::SetTitle(const char *s
) {
715 GETWIN(id
)->SetLabel(stc2wx(s
));
719 // Returns rectangle of monitor pt is on
720 PRectangle
Window::GetMonitorRect(Point pt
) {
722 if (! id
) return PRectangle();
724 // Get the display the point is found on
725 int n
= wxDisplay::GetFromPoint(wxPoint(pt
.x
, pt
.y
));
726 wxDisplay
dpy(n
== wxNOT_FOUND
? 0 : n
);
727 rect
= dpy
.GetGeometry();
729 return PRectangleFromwxRect(rect
);
732 //----------------------------------------------------------------------
733 // Helper classes for ListBox
736 // This is a simple subclass of wxListView that just resets focus to the
737 // parent when it gets it.
738 class wxSTCListBox
: public wxListView
{
740 wxSTCListBox(wxWindow
* parent
, wxWindowID id
,
741 const wxPoint
& pos
, const wxSize
& size
,
746 Hide(); // don't flicker as we move it around...
748 Create(parent
, id
, pos
, size
, style
);
752 void OnFocus(wxFocusEvent
& event
) {
753 GetParent()->SetFocus();
757 void OnKillFocus(wxFocusEvent
& WXUNUSED(event
)) {
758 // Do nothing. Prevents base class from resetting the colors...
762 // For some reason I don't understand yet the focus doesn't really leave
763 // the listbox like it should, so if we get any events feed them back to
765 void OnKeyDown(wxKeyEvent
& event
) {
766 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
768 void OnChar(wxKeyEvent
& event
) {
769 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
772 // And we need to force the focus back when being destroyed
774 GetGrandParent()->SetFocus();
779 DECLARE_EVENT_TABLE()
782 BEGIN_EVENT_TABLE(wxSTCListBox
, wxListView
)
783 EVT_SET_FOCUS( wxSTCListBox::OnFocus
)
784 EVT_KILL_FOCUS(wxSTCListBox::OnKillFocus
)
786 EVT_KEY_DOWN( wxSTCListBox::OnKeyDown
)
787 EVT_CHAR( wxSTCListBox::OnChar
)
793 #if wxUSE_POPUPWIN //-----------------------------------
794 #include "wx/popupwin.h"
798 // TODO: Refactor these two classes to have a common base (or a mix-in) to get
799 // rid of the code duplication. (Either that or convince somebody to
800 // implement wxPopupWindow for the Mac!!)
802 // In the meantime, be careful to duplicate any changes as needed...
805 // A popup window to place the wxSTCListBox upon
806 class wxSTCListBoxWin
: public wxPopupWindow
810 CallBackAction doubleClickAction
;
811 void* doubleClickActionData
;
813 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point
WXUNUSED(location
)) :
814 wxPopupWindow(parent
, wxBORDER_NONE
)
817 SetBackgroundColour(*wxBLACK
); // for our simple border
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.
839 // Set position in client coords
840 virtual void DoSetSize(int x
, int y
,
841 int width
, int height
,
842 int sizeFlags
= wxSIZE_AUTO
) {
843 if (x
!= wxDefaultCoord
) {
844 GetParent()->ClientToScreen(&x
, NULL
);
846 if (y
!= wxDefaultCoord
) {
847 GetParent()->ClientToScreen(NULL
, &y
);
849 wxPopupWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
852 // return position as if it were in client coords
853 virtual void DoGetPosition( int *x
, int *y
) const {
855 wxPopupWindow::DoGetPosition(&sx
, &sy
);
856 GetParent()->ScreenToClient(&sx
, &sy
);
863 if ( !wxPendingDelete
.Member(this) )
864 wxPendingDelete
.Append(this);
870 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
873 il
->GetSize(0, w
, h
);
880 void SetDoubleClickAction(CallBackAction action
, void *data
) {
881 doubleClickAction
= action
;
882 doubleClickActionData
= data
;
886 void OnFocus(wxFocusEvent
& event
) {
887 GetParent()->SetFocus();
891 void OnSize(wxSizeEvent
& event
) {
893 wxSize sz
= GetSize();
896 lv
->SetSize(1, 1, sz
.x
, sz
.y
);
897 // reset the column widths
898 lv
->SetColumnWidth(0, IconWidth()+4);
899 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
900 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
904 void OnActivate(wxListEvent
& WXUNUSED(event
)) {
905 doubleClickAction(doubleClickActionData
);
908 wxListView
* GetLB() { return lv
; }
911 DECLARE_EVENT_TABLE()
915 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxPopupWindow
)
916 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
917 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
918 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
923 #else // !wxUSE_POPUPWIN -----------------------------------
924 #include "wx/frame.h"
926 // A normal window to place the wxSTCListBox upon, but make it behave as much
927 // like a wxPopupWindow as possible
928 class wxSTCListBoxWin
: public wxFrame
{
931 CallBackAction doubleClickAction
;
932 void* doubleClickActionData
;
934 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point location
) :
935 wxFrame(parent
, id
, wxEmptyString
, wxPoint(location
.x
, location
.y
), wxSize(0,0),
937 | wxFRAME_FLOAT_ON_PARENT
947 lv
= new wxSTCListBox(this, id
, wxDefaultPosition
, wxDefaultSize
,
948 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxNO_BORDER
);
949 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
950 lv
->InsertColumn(0, wxEmptyString
);
951 lv
->InsertColumn(1, wxEmptyString
);
953 // Eventhough we immediately reset the focus to the parent, this helps
954 // things to look right...
961 // On OSX and (possibly others) there can still be pending
962 // messages/events for the list control when Scintilla wants to
963 // close it, so do a pending delete of it instead of destroying
968 // The bottom edge of this window is not getting properly
969 // refreshed upon deletion, so help it out...
970 wxWindow
* p
= GetParent();
971 wxRect
r(GetPosition(), GetSize());
972 r
.SetHeight(r
.GetHeight()+1);
973 p
->Refresh(false, &r
);
975 if ( !wxPendingDelete
.Member(this) )
976 wxPendingDelete
.Append(this);
983 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
986 il
->GetSize(0, w
, h
);
993 void SetDoubleClickAction(CallBackAction action
, void *data
)
995 doubleClickAction
= action
;
996 doubleClickActionData
= data
;
1000 void OnFocus(wxFocusEvent
& event
)
1003 GetParent()->SetFocus();
1007 void OnSize(wxSizeEvent
& event
)
1010 wxSize sz
= GetClientSize();
1012 // reset the column widths
1013 lv
->SetColumnWidth(0, IconWidth()+4);
1014 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
1015 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
1019 void ActivateParent()
1021 // Although we're a frame, we always want the parent to be active, so
1022 // raise it whenever we get shown, focused, etc.
1023 wxTopLevelWindow
*frame
= wxDynamicCast(
1024 wxGetTopLevelParent(GetParent()), wxTopLevelWindow
);
1030 virtual void DoSetSize(int x
, int y
,
1031 int width
, int height
,
1032 int sizeFlags
= wxSIZE_AUTO
)
1034 // convert coords to screen coords since we're a top-level window
1035 if (x
!= wxDefaultCoord
) {
1036 GetParent()->ClientToScreen(&x
, NULL
);
1038 if (y
!= wxDefaultCoord
) {
1039 GetParent()->ClientToScreen(NULL
, &y
);
1041 wxFrame::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1044 virtual bool Show(bool show
= true)
1046 bool rv
= wxFrame::Show(show
);
1050 GetParent()->Refresh(false);
1055 void OnActivate(wxListEvent
& WXUNUSED(event
))
1057 doubleClickAction(doubleClickActionData
);
1060 wxListView
* GetLB() { return lv
; }
1063 DECLARE_EVENT_TABLE()
1067 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxWindow
)
1068 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
1069 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
1070 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
1073 #endif // wxUSE_POPUPWIN -----------------------------------
1076 inline wxSTCListBoxWin
* GETLBW(WindowID win
) {
1077 return ((wxSTCListBoxWin
*)win
);
1080 inline wxListView
* GETLB(WindowID win
) {
1081 return GETLBW(win
)->GetLB();
1084 //----------------------------------------------------------------------
1086 class ListBoxImpl
: public ListBox
{
1090 int desiredVisibleRows
;
1093 Point location
; // Caret location at which the list is opened
1094 wxImageList
* imgList
;
1095 wxArrayInt
* imgTypeMap
;
1101 virtual void SetFont(Font
&font
);
1102 virtual void Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
);
1103 virtual void SetAverageCharWidth(int width
);
1104 virtual void SetVisibleRows(int rows
);
1105 virtual int GetVisibleRows() const;
1106 virtual PRectangle
GetDesiredRect();
1107 virtual int CaretFromEdge();
1108 virtual void Clear();
1109 virtual void Append(char *s
, int type
= -1);
1110 void Append(const wxString
& text
, int type
);
1111 virtual int Length();
1112 virtual void Select(int n
);
1113 virtual int GetSelection();
1114 virtual int Find(const char *prefix
);
1115 virtual void GetValue(int n
, char *value
, int len
);
1116 virtual void RegisterImage(int type
, const char *xpm_data
);
1117 virtual void ClearRegisteredImages();
1118 virtual void SetDoubleClickAction(CallBackAction
, void *);
1119 virtual void SetList(const char* list
, char separator
, char typesep
);
1123 ListBoxImpl::ListBoxImpl()
1124 : lineHeight(10), unicodeMode(false),
1125 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
1126 imgList(NULL
), imgTypeMap(NULL
)
1130 ListBoxImpl::~ListBoxImpl() {
1142 void ListBoxImpl::SetFont(Font
&font
) {
1143 GETLB(id
)->SetFont(*((wxFont
*)font
.GetID()));
1147 void ListBoxImpl::Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
) {
1148 location
= location_
;
1149 lineHeight
= lineHeight_
;
1150 unicodeMode
= unicodeMode_
;
1152 id
= new wxSTCListBoxWin(GETWIN(parent
.GetID()), ctrlID
, location
);
1153 if (imgList
!= NULL
)
1154 GETLB(id
)->SetImageList(imgList
, wxIMAGE_LIST_SMALL
);
1158 void ListBoxImpl::SetAverageCharWidth(int width
) {
1159 aveCharWidth
= width
;
1163 void ListBoxImpl::SetVisibleRows(int rows
) {
1164 desiredVisibleRows
= rows
;
1168 int ListBoxImpl::GetVisibleRows() const {
1169 return desiredVisibleRows
;
1172 PRectangle
ListBoxImpl::GetDesiredRect() {
1173 // wxListCtrl doesn't have a DoGetBestSize, so instead we kept track of
1174 // the max size in Append and calculate it here...
1175 int maxw
= maxStrWidth
* aveCharWidth
;
1178 // give it a default if there are no lines, and/or add a bit more
1179 if (maxw
== 0) maxw
= 100;
1180 maxw
+= aveCharWidth
* 3 +
1181 GETLBW(id
)->IconWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1185 // estimate a desired height
1186 int count
= GETLB(id
)->GetItemCount();
1189 GETLB(id
)->GetItemRect(0, rect
);
1190 maxh
= count
* rect
.GetHeight();
1191 if (maxh
> 140) // TODO: Use desiredVisibleRows??
1194 // Try to make the size an exact multiple of some number of lines
1195 int lines
= maxh
/ rect
.GetHeight();
1196 maxh
= (lines
+ 1) * rect
.GetHeight() + 2;
1210 int ListBoxImpl::CaretFromEdge() {
1211 return 4 + GETLBW(id
)->IconWidth();
1215 void ListBoxImpl::Clear() {
1216 GETLB(id
)->DeleteAllItems();
1220 void ListBoxImpl::Append(char *s
, int type
) {
1221 Append(stc2wx(s
), type
);
1224 void ListBoxImpl::Append(const wxString
& text
, int type
) {
1225 long count
= GETLB(id
)->GetItemCount();
1226 long itemID
= GETLB(id
)->InsertItem(count
, wxEmptyString
);
1228 GETLB(id
)->SetItem(itemID
, 1, text
);
1229 maxStrWidth
= wxMax(maxStrWidth
, text
.length());
1231 wxCHECK_RET(imgTypeMap
, wxT("Unexpected NULL imgTypeMap"));
1232 idx
= imgTypeMap
->Item(type
);
1234 GETLB(id
)->SetItemImage(itemID
, idx
, idx
);
1237 void ListBoxImpl::SetList(const char* list
, char separator
, char typesep
) {
1238 GETLB(id
)->Freeze();
1240 wxStringTokenizer
tkzr(stc2wx(list
), (wxChar
)separator
);
1241 while ( tkzr
.HasMoreTokens() ) {
1242 wxString token
= tkzr
.GetNextToken();
1244 int pos
= token
.Find(typesep
);
1246 token
.Mid(pos
+1).ToLong(&type
);
1247 token
.Truncate(pos
);
1249 Append(token
, (int)type
);
1255 int ListBoxImpl::Length() {
1256 return GETLB(id
)->GetItemCount();
1260 void ListBoxImpl::Select(int n
) {
1266 GETLB(id
)->EnsureVisible(n
);
1267 GETLB(id
)->Select(n
, select
);
1271 int ListBoxImpl::GetSelection() {
1272 return GETLB(id
)->GetFirstSelected();
1276 int ListBoxImpl::Find(const char *WXUNUSED(prefix
)) {
1282 void ListBoxImpl::GetValue(int n
, char *value
, int len
) {
1286 item
.SetMask(wxLIST_MASK_TEXT
);
1287 GETLB(id
)->GetItem(item
);
1288 strncpy(value
, wx2stc(item
.GetText()), len
);
1289 value
[len
-1] = '\0';
1293 void ListBoxImpl::RegisterImage(int type
, const char *xpm_data
) {
1294 wxMemoryInputStream
stream(xpm_data
, strlen(xpm_data
)+1);
1295 wxImage
img(stream
, wxBITMAP_TYPE_XPM
);
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::ClearRegisteredImages() {
1325 GETLB(id
)->SetImageList(NULL
, wxIMAGE_LIST_SMALL
);
1329 void ListBoxImpl::SetDoubleClickAction(CallBackAction action
, void *data
) {
1330 GETLBW(id
)->SetDoubleClickAction(action
, data
);
1334 ListBox::ListBox() {
1337 ListBox::~ListBox() {
1340 ListBox
*ListBox::Allocate() {
1341 return new ListBoxImpl();
1344 //----------------------------------------------------------------------
1346 Menu::Menu() : id(0) {
1349 void Menu::CreatePopUp() {
1354 void Menu::Destroy() {
1360 void Menu::Show(Point pt
, Window
&w
) {
1361 GETWIN(w
.GetID())->PopupMenu((wxMenu
*)id
, pt
.x
- 4, pt
.y
);
1365 //----------------------------------------------------------------------
1367 DynamicLibrary
*DynamicLibrary::Load(const char *WXUNUSED(modulePath
)) {
1368 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1372 //----------------------------------------------------------------------
1374 ColourDesired
Platform::Chrome() {
1376 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1377 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1380 ColourDesired
Platform::ChromeHighlight() {
1382 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
);
1383 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1386 const char *Platform::DefaultFont() {
1387 static char buf
[128];
1388 strcpy(buf
, wxNORMAL_FONT
->GetFaceName().mbc_str());
1392 int Platform::DefaultFontSize() {
1393 return wxNORMAL_FONT
->GetPointSize();
1396 unsigned int Platform::DoubleClickTime() {
1397 return 500; // **** ::GetDoubleClickTime();
1400 bool Platform::MouseButtonBounce() {
1404 bool Platform::IsKeyDown(int WXUNUSED(key
)) {
1405 return false; // I don't think we'll need this.
1408 long Platform::SendScintilla(WindowID w
,
1410 unsigned long wParam
,
1413 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1414 return stc
->SendMsg(msg
, wParam
, lParam
);
1417 long Platform::SendScintillaPointer(WindowID w
,
1419 unsigned long wParam
,
1422 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1423 return stc
->SendMsg(msg
, wParam
, (long)lParam
);
1427 // These are utility functions not really tied to a platform
1429 int Platform::Minimum(int a
, int b
) {
1436 int Platform::Maximum(int a
, int b
) {
1445 void Platform::DebugDisplay(const char *s
) {
1447 wxLogDebug(stc2wx(s
));
1453 void Platform::DebugPrintf(const char *format
, ...) {
1457 va_start(pArguments
, format
);
1458 vsprintf(buffer
,format
,pArguments
);
1460 Platform::DebugDisplay(buffer
);
1462 wxUnusedVar(format
);
1467 static bool assertionPopUps
= true;
1469 bool Platform::ShowAssertionPopUps(bool assertionPopUps_
) {
1470 bool ret
= assertionPopUps
;
1471 assertionPopUps
= assertionPopUps_
;
1475 void Platform::Assert(const char *c
, const char *file
, int line
) {
1478 sprintf(buffer
, "Assertion [%s] failed at %s %d", c
, file
, line
);
1479 if (assertionPopUps
) {
1481 wxMessageBox(stc2wx(buffer
),
1482 wxT("Assertion failure"),
1483 wxICON_HAND
| wxOK
);
1485 strcat(buffer
, "\r\n");
1486 Platform::DebugDisplay(buffer
);
1497 int Platform::Clamp(int val
, int minVal
, int maxVal
) {
1506 bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage
), char WXUNUSED(ch
)) {
1510 int Platform::DBCSCharLength(int WXUNUSED(codePage
), const char *WXUNUSED(s
)) {
1514 int Platform::DBCSCharMaxLength() {
1519 //----------------------------------------------------------------------
1521 ElapsedTime::ElapsedTime() {
1522 wxLongLong localTime
= wxGetLocalTimeMillis();
1523 littleBit
= localTime
.GetLo();
1524 bigBit
= localTime
.GetHi();
1527 double ElapsedTime::Duration(bool reset
) {
1528 wxLongLong
prevTime(bigBit
, littleBit
);
1529 wxLongLong localTime
= wxGetLocalTimeMillis();
1531 littleBit
= localTime
.GetLo();
1532 bigBit
= localTime
.GetHi();
1534 wxLongLong duration
= localTime
- prevTime
;
1535 double result
= duration
.ToDouble();
1541 //----------------------------------------------------------------------
1545 #include "UniConversion.h"
1547 // Convert using Scintilla's functions instead of wx's, Scintilla's are more
1548 // forgiving and won't assert...
1550 wxString
stc2wx(const char* str
, size_t len
)
1553 return wxEmptyString
;
1555 size_t wclen
= UTF16Length(str
, len
);
1556 wxWCharBuffer
buffer(wclen
+1);
1558 size_t actualLen
= UTF16FromUTF8(str
, len
, buffer
.data(), wclen
+1);
1559 return wxString(buffer
.data(), actualLen
);
1564 wxString
stc2wx(const char* str
)
1566 return stc2wx(str
, strlen(str
));
1570 const wxWX2MBbuf
wx2stc(const wxString
& str
)
1572 const wchar_t* wcstr
= str
.c_str();
1573 size_t wclen
= str
.length();
1574 size_t len
= UTF8Length(wcstr
, wclen
);
1576 wxCharBuffer
buffer(len
+1);
1577 UTF8FromUTF16(wcstr
, wclen
, buffer
.data(), len
);
1579 // TODO check NULL termination!!