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 wxHAVE_RAW_BITMAP
36 #include "wx/rawbmp.h"
41 #include "wx/stc/stc.h"
42 #include "wx/stc/private.h"
46 Point
Point::FromLong(long lpoint
) {
47 return Point(lpoint
& 0xFFFF, lpoint
>> 16);
50 wxRect
wxRectFromPRectangle(PRectangle prc
) {
51 wxRect
r(prc
.left
, prc
.top
,
52 prc
.Width(), prc
.Height());
56 PRectangle
PRectangleFromwxRect(wxRect rc
) {
57 return PRectangle(rc
.GetLeft(), rc
.GetTop(),
58 rc
.GetRight()+1, rc
.GetBottom()+1);
61 wxColour
wxColourFromCA(const ColourAllocated
& ca
) {
62 ColourDesired
cd(ca
.AsLong());
63 return wxColour((unsigned char)cd
.GetRed(),
64 (unsigned char)cd
.GetGreen(),
65 (unsigned char)cd
.GetBlue());
68 //----------------------------------------------------------------------
72 allowRealization
= false;
74 entries
= new ColourPair
[size
];
83 void Palette::Release() {
87 entries
= new ColourPair
[size
];
90 // This method either adds a colour to the list of wanted colours (want==true)
91 // or retrieves the allocated colour back to the ColourPair.
92 // This is one method to make it easier to keep the code for wanting and retrieving in sync.
93 void Palette::WantFind(ColourPair
&cp
, bool want
) {
95 for (int i
=0; i
< used
; i
++) {
96 if (entries
[i
].desired
== cp
.desired
)
101 int sizeNew
= size
* 2;
102 ColourPair
*entriesNew
= new ColourPair
[sizeNew
];
103 for (int j
=0; j
<size
; j
++) {
104 entriesNew
[j
] = entries
[j
];
107 entries
= entriesNew
;
111 entries
[used
].desired
= cp
.desired
;
112 entries
[used
].allocated
.Set(cp
.desired
.AsLong());
115 for (int i
=0; i
< used
; i
++) {
116 if (entries
[i
].desired
== cp
.desired
) {
117 cp
.allocated
= entries
[i
].allocated
;
121 cp
.allocated
.Set(cp
.desired
.AsLong());
125 void Palette::Allocate(Window
&) {
126 if (allowRealization
) {
131 //----------------------------------------------------------------------
141 void Font::Create(const char *faceName
, int characterSet
, int size
, bool bold
, bool italic
, bool extraFontFlag
) {
145 // The minus one is done because since Scintilla uses SC_SHARSET_DEFAULT
146 // internally and we need to have wxFONENCODING_DEFAULT == SC_SHARSET_DEFAULT
147 // so we adjust the encoding before passing it to Scintilla. See also
148 // wxStyledTextCtrl::StyleSetCharacterSet
149 wxFontEncoding encoding
= (wxFontEncoding
)(characterSet
-1);
151 wxFontEncodingArray ea
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
155 wxFont
* font
= new wxFont(size
,
157 italic
? wxITALIC
: wxNORMAL
,
158 bold
? wxBOLD
: wxNORMAL
,
162 //font->SetNoAntiAliasing(!extraFontFlag);
167 void Font::Release() {
173 //----------------------------------------------------------------------
175 class SurfaceImpl
: public Surface
{
188 virtual void Init(WindowID wid
);
189 virtual void Init(SurfaceID sid
, WindowID wid
);
190 virtual void InitPixMap(int width
, int height
, Surface
*surface_
, WindowID wid
);
192 virtual void Release();
193 virtual bool Initialised();
194 virtual void PenColour(ColourAllocated fore
);
195 virtual int LogPixelsY();
196 virtual int DeviceHeightFont(int points
);
197 virtual void MoveTo(int x_
, int y_
);
198 virtual void LineTo(int x_
, int y_
);
199 virtual void Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
);
200 virtual void RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
201 virtual void FillRectangle(PRectangle rc
, ColourAllocated back
);
202 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
);
203 virtual void RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
204 virtual void AlphaRectangle(PRectangle rc
, int cornerSize
, ColourAllocated fill
, int alphaFill
,
205 ColourAllocated outline
, int alphaOutline
, int flags
);
206 virtual void Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
207 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
);
209 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
210 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
211 virtual void DrawTextTransparent(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
);
212 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, int *positions
);
213 virtual int WidthText(Font
&font_
, const char *s
, int len
);
214 virtual int WidthChar(Font
&font_
, char ch
);
215 virtual int Ascent(Font
&font_
);
216 virtual int Descent(Font
&font_
);
217 virtual int InternalLeading(Font
&font_
);
218 virtual int ExternalLeading(Font
&font_
);
219 virtual int Height(Font
&font_
);
220 virtual int AverageCharWidth(Font
&font_
);
222 virtual int SetPalette(Palette
*pal
, bool inBackGround
);
223 virtual void SetClip(PRectangle rc
);
224 virtual void FlushCachedState();
226 virtual void SetUnicodeMode(bool unicodeMode_
);
227 virtual void SetDBCSMode(int codePage
);
229 void BrushColour(ColourAllocated back
);
230 void SetFont(Font
&font_
);
235 SurfaceImpl::SurfaceImpl() :
236 hdc(0), hdcOwned(0), bitmap(0),
237 x(0), y(0), unicodeMode(0)
240 SurfaceImpl::~SurfaceImpl() {
244 void SurfaceImpl::Init(WindowID wid
) {
247 hdc
= new wxMemoryDC();
250 // On Mac and GTK the DC is not really valid until it has a bitmap
251 // selected into it. So instead of just creating the DC with no bitmap,
252 // go ahead and give it one.
253 InitPixMap(1,1,NULL
,wid
);
257 void SurfaceImpl::Init(SurfaceID hdc_
, WindowID
) {
262 void SurfaceImpl::InitPixMap(int width
, int height
, Surface
*WXUNUSED(surface_
), WindowID
) {
264 hdc
= new wxMemoryDC();
266 if (width
< 1) width
= 1;
267 if (height
< 1) height
= 1;
268 bitmap
= new wxBitmap(width
, height
);
269 ((wxMemoryDC
*)hdc
)->SelectObject(*bitmap
);
273 void SurfaceImpl::Release() {
275 ((wxMemoryDC
*)hdc
)->SelectObject(wxNullBitmap
);
287 bool SurfaceImpl::Initialised() {
292 void SurfaceImpl::PenColour(ColourAllocated fore
) {
293 hdc
->SetPen(wxPen(wxColourFromCA(fore
), 1, wxSOLID
));
296 void SurfaceImpl::BrushColour(ColourAllocated back
) {
297 hdc
->SetBrush(wxBrush(wxColourFromCA(back
), wxSOLID
));
300 void SurfaceImpl::SetFont(Font
&font_
) {
302 hdc
->SetFont(*((wxFont
*)font_
.GetID()));
306 int SurfaceImpl::LogPixelsY() {
307 return hdc
->GetPPI().y
;
310 int SurfaceImpl::DeviceHeightFont(int points
) {
314 void SurfaceImpl::MoveTo(int x_
, int y_
) {
319 void SurfaceImpl::LineTo(int x_
, int y_
) {
320 hdc
->DrawLine(x
,y
, x_
,y_
);
325 void SurfaceImpl::Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
) {
328 hdc
->DrawPolygon(npts
, (wxPoint
*)pts
);
331 void SurfaceImpl::RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
334 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
337 void SurfaceImpl::FillRectangle(PRectangle rc
, ColourAllocated back
) {
339 hdc
->SetPen(*wxTRANSPARENT_PEN
);
340 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
343 void SurfaceImpl::FillRectangle(PRectangle rc
, Surface
&surfacePattern
) {
345 if (((SurfaceImpl
&)surfacePattern
).bitmap
)
346 br
= wxBrush(*((SurfaceImpl
&)surfacePattern
).bitmap
);
347 else // Something is wrong so display in red
348 br
= wxBrush(*wxRED
, wxSOLID
);
349 hdc
->SetPen(*wxTRANSPARENT_PEN
);
351 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
354 void SurfaceImpl::RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
357 hdc
->DrawRoundedRectangle(wxRectFromPRectangle(rc
), 4);
361 #define wxPy_premultiply(p, a) ((p) * (a) / 0xff)
363 #define wxPy_premultiply(p, a) (p)
366 void SurfaceImpl::AlphaRectangle(PRectangle rc
, int cornerSize
,
367 ColourAllocated fill
, int alphaFill
,
368 ColourAllocated outline
, int alphaOutline
,
370 #ifdef wxHAVE_RAW_BITMAP
372 // TODO: do something with cornerSize
373 wxUnusedVar(cornerSize
);
376 wxRect r
= wxRectFromPRectangle(rc
);
377 wxBitmap
bmp(r
.width
, r
.height
, 32);
378 wxAlphaPixelData
pixData(bmp
);
380 // Set the fill pixels
381 ColourDesired
cdf(fill
.AsLong());
382 int red
= cdf
.GetRed();
383 int green
= cdf
.GetGreen();
384 int blue
= cdf
.GetBlue();
386 wxAlphaPixelData::Iterator
p(pixData
);
387 for (y
=0; y
<r
.height
; y
++) {
388 p
.MoveTo(pixData
, 0, y
);
389 for (x
=0; x
<r
.width
; x
++) {
390 p
.Red() = wxPy_premultiply(red
, alphaFill
);
391 p
.Green() = wxPy_premultiply(green
, alphaFill
);
392 p
.Blue() = wxPy_premultiply(blue
, alphaFill
);
393 p
.Alpha() = alphaFill
;
398 // Set the outline pixels
399 ColourDesired
cdo(outline
.AsLong());
401 green
= cdo
.GetGreen();
402 blue
= cdo
.GetBlue();
403 for (x
=0; x
<r
.width
; x
++) {
404 p
.MoveTo(pixData
, x
, 0);
405 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
406 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
407 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
408 p
.Alpha() = alphaOutline
;
409 p
.MoveTo(pixData
, x
, r
.height
-1);
410 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
411 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
412 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
413 p
.Alpha() = alphaOutline
;
416 for (y
=0; y
<r
.height
; y
++) {
417 p
.MoveTo(pixData
, 0, y
);
418 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
419 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
420 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
421 p
.Alpha() = alphaOutline
;
422 p
.MoveTo(pixData
, r
.width
-1, y
);
423 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
424 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
425 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
426 p
.Alpha() = alphaOutline
;
430 hdc
->DrawBitmap(bmp
, r
.x
, r
.y
, true);
433 wxUnusedVar(cornerSize
);
434 wxUnusedVar(alphaFill
);
435 wxUnusedVar(alphaOutline
);
436 RectangleDraw(rc
, outline
, fill
);
440 void SurfaceImpl::Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
443 hdc
->DrawEllipse(wxRectFromPRectangle(rc
));
446 void SurfaceImpl::Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
) {
447 wxRect r
= wxRectFromPRectangle(rc
);
448 hdc
->Blit(r
.x
, r
.y
, r
.width
, r
.height
,
449 ((SurfaceImpl
&)surfaceSource
).hdc
,
450 from
.x
, from
.y
, wxCOPY
);
453 void SurfaceImpl::DrawTextNoClip(PRectangle rc
, Font
&font
, int ybase
,
454 const char *s
, int len
,
455 ColourAllocated fore
, ColourAllocated back
) {
457 hdc
->SetTextForeground(wxColourFromCA(fore
));
458 hdc
->SetTextBackground(wxColourFromCA(back
));
459 FillRectangle(rc
, back
);
461 // ybase is where the baseline should be, but wxWin uses the upper left
462 // corner, so I need to calculate the real position for the text...
463 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
466 void SurfaceImpl::DrawTextClipped(PRectangle rc
, Font
&font
, int ybase
,
467 const char *s
, int len
,
468 ColourAllocated fore
, ColourAllocated back
) {
470 hdc
->SetTextForeground(wxColourFromCA(fore
));
471 hdc
->SetTextBackground(wxColourFromCA(back
));
472 FillRectangle(rc
, back
);
473 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
475 // see comments above
476 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
477 hdc
->DestroyClippingRegion();
481 void SurfaceImpl::DrawTextTransparent(PRectangle rc
, Font
&font
, int ybase
,
482 const char *s
, int len
,
483 ColourAllocated fore
) {
486 hdc
->SetTextForeground(wxColourFromCA(fore
));
487 hdc
->SetBackgroundMode(wxTRANSPARENT
);
489 // ybase is where the baseline should be, but wxWin uses the upper left
490 // corner, so I need to calculate the real position for the text...
491 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
493 hdc
->SetBackgroundMode(wxSOLID
);
497 void SurfaceImpl::MeasureWidths(Font
&font
, const char *s
, int len
, int *positions
) {
499 wxString str
= stc2wx(s
, len
);
504 hdc
->GetPartialTextExtents(str
, tpos
);
507 // Map the widths for UCS-2 characters back to the UTF-8 input string
508 // NOTE: I don't think this is right for when sizeof(wxChar) > 2, ie wxGTK2
509 // so figure it out and fix it!
512 while ((int)i
< len
) {
513 unsigned char uch
= (unsigned char)s
[i
];
514 positions
[i
++] = tpos
[ui
];
516 if (uch
< (0x80 + 0x40 + 0x20)) {
517 positions
[i
++] = tpos
[ui
];
519 positions
[i
++] = tpos
[ui
];
520 positions
[i
++] = tpos
[ui
];
527 // If not unicode then just use the widths we have
529 std::copy(tpos
.begin(), tpos
.end(), positions
);
531 memcpy(positions
, tpos
.begin(), len
* sizeof(int));
537 int SurfaceImpl::WidthText(Font
&font
, const char *s
, int len
) {
542 hdc
->GetTextExtent(stc2wx(s
, len
), &w
, &h
);
547 int SurfaceImpl::WidthChar(Font
&font
, char ch
) {
551 char s
[2] = { ch
, 0 };
553 hdc
->GetTextExtent(stc2wx(s
, 1), &w
, &h
);
557 #define EXTENT_TEST wxT(" `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
559 int SurfaceImpl::Ascent(Font
&font
) {
562 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
567 int SurfaceImpl::Descent(Font
&font
) {
570 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
574 int SurfaceImpl::InternalLeading(Font
&WXUNUSED(font
)) {
578 int SurfaceImpl::ExternalLeading(Font
&font
) {
581 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
585 int SurfaceImpl::Height(Font
&font
) {
587 return hdc
->GetCharHeight() + 1;
590 int SurfaceImpl::AverageCharWidth(Font
&font
) {
592 return hdc
->GetCharWidth();
595 int SurfaceImpl::SetPalette(Palette
*WXUNUSED(pal
), bool WXUNUSED(inBackGround
)) {
599 void SurfaceImpl::SetClip(PRectangle rc
) {
600 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
603 void SurfaceImpl::FlushCachedState() {
606 void SurfaceImpl::SetUnicodeMode(bool unicodeMode_
) {
607 unicodeMode
=unicodeMode_
;
610 void SurfaceImpl::SetDBCSMode(int WXUNUSED(codePage
)) {
611 // dbcsMode = codePage == SC_CP_DBCS;
615 Surface
*Surface::Allocate() {
616 return new SurfaceImpl
;
620 //----------------------------------------------------------------------
623 inline wxWindow
* GETWIN(WindowID id
) { return (wxWindow
*)id
; }
628 void Window::Destroy() {
631 GETWIN(id
)->Destroy();
636 bool Window::HasFocus() {
637 return wxWindow::FindFocus() == GETWIN(id
);
640 PRectangle
Window::GetPosition() {
641 if (! id
) return PRectangle();
642 wxRect
rc(GETWIN(id
)->GetPosition(), GETWIN(id
)->GetSize());
643 return PRectangleFromwxRect(rc
);
646 void Window::SetPosition(PRectangle rc
) {
647 wxRect r
= wxRectFromPRectangle(rc
);
648 GETWIN(id
)->SetSize(r
);
651 void Window::SetPositionRelative(PRectangle rc
, Window
) {
652 SetPosition(rc
); // ????
655 PRectangle
Window::GetClientPosition() {
656 if (! id
) return PRectangle();
657 wxSize sz
= GETWIN(id
)->GetClientSize();
658 return PRectangle(0, 0, sz
.x
, sz
.y
);
661 void Window::Show(bool show
) {
662 GETWIN(id
)->Show(show
);
665 void Window::InvalidateAll() {
666 GETWIN(id
)->Refresh(false);
669 void Window::InvalidateRectangle(PRectangle rc
) {
670 wxRect r
= wxRectFromPRectangle(rc
);
671 GETWIN(id
)->Refresh(false, &r
);
674 void Window::SetFont(Font
&font
) {
675 GETWIN(id
)->SetFont(*((wxFont
*)font
.GetID()));
678 void Window::SetCursor(Cursor curs
) {
683 cursorId
= wxCURSOR_IBEAM
;
686 cursorId
= wxCURSOR_ARROW
;
689 cursorId
= wxCURSOR_ARROW
; // ** no up arrow... wxCURSOR_UPARROW;
692 cursorId
= wxCURSOR_WAIT
;
695 cursorId
= wxCURSOR_SIZEWE
;
698 cursorId
= wxCURSOR_SIZENS
;
700 case cursorReverseArrow
:
701 cursorId
= wxCURSOR_RIGHT_ARROW
;
704 cursorId
= wxCURSOR_HAND
;
707 cursorId
= wxCURSOR_ARROW
;
711 wxCursor wc
= wxStockCursor(cursorId
) ;
713 wxCursor wc
= wxCursor(cursorId
) ;
715 if(curs
!= cursorLast
)
717 GETWIN(id
)->SetCursor(wc
);
723 void Window::SetTitle(const char *s
) {
724 GETWIN(id
)->SetLabel(stc2wx(s
));
728 // Returns rectangle of monitor pt is on
729 PRectangle
Window::GetMonitorRect(Point pt
) {
731 if (! id
) return PRectangle();
733 // Get the display the point is found on
734 int n
= wxDisplay::GetFromPoint(wxPoint(pt
.x
, pt
.y
));
735 wxDisplay
dpy(n
== wxNOT_FOUND
? 0 : n
);
736 rect
= dpy
.GetGeometry();
738 return PRectangleFromwxRect(rect
);
741 //----------------------------------------------------------------------
742 // Helper classes for ListBox
745 // This is a simple subclass of wxListView that just resets focus to the
746 // parent when it gets it.
747 class wxSTCListBox
: public wxListView
{
749 wxSTCListBox(wxWindow
* parent
, wxWindowID id
,
750 const wxPoint
& pos
, const wxSize
& size
,
755 Hide(); // don't flicker as we move it around...
757 Create(parent
, id
, pos
, size
, style
);
761 void OnFocus(wxFocusEvent
& event
) {
762 GetParent()->SetFocus();
766 void OnKillFocus(wxFocusEvent
& WXUNUSED(event
)) {
767 // Do nothing. Prevents base class from resetting the colors...
771 // For some reason I don't understand yet the focus doesn't really leave
772 // the listbox like it should, so if we get any events feed them back to
774 void OnKeyDown(wxKeyEvent
& event
) {
775 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
777 void OnChar(wxKeyEvent
& event
) {
778 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
781 // And we need to force the focus back when being destroyed
783 GetGrandParent()->SetFocus();
788 DECLARE_EVENT_TABLE()
791 BEGIN_EVENT_TABLE(wxSTCListBox
, wxListView
)
792 EVT_SET_FOCUS( wxSTCListBox::OnFocus
)
793 EVT_KILL_FOCUS(wxSTCListBox::OnKillFocus
)
795 EVT_KEY_DOWN( wxSTCListBox::OnKeyDown
)
796 EVT_CHAR( wxSTCListBox::OnChar
)
802 #if wxUSE_POPUPWIN //-----------------------------------
803 #include "wx/popupwin.h"
807 // TODO: Refactor these two classes to have a common base (or a mix-in) to get
808 // rid of the code duplication. (Either that or convince somebody to
809 // implement wxPopupWindow for the Mac!!)
811 // In the meantime, be careful to duplicate any changes as needed...
814 // A popup window to place the wxSTCListBox upon
815 class wxSTCListBoxWin
: public wxPopupWindow
819 CallBackAction doubleClickAction
;
820 void* doubleClickActionData
;
822 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point
WXUNUSED(location
)) :
823 wxPopupWindow(parent
, wxBORDER_NONE
)
826 SetBackgroundColour(*wxBLACK
); // for our simple border
828 lv
= new wxSTCListBox(parent
, id
, wxPoint(-50,-50), wxDefaultSize
,
829 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxBORDER_NONE
);
830 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
831 lv
->InsertColumn(0, wxEmptyString
);
832 lv
->InsertColumn(1, wxEmptyString
);
834 // NOTE: We need to fool the wxListView into thinking that it has the
835 // focus so it will use the normal selection colour and will look
836 // "right" to the user. But since the wxPopupWindow or its children
837 // can't receive focus then we have to pull a fast one and temporarily
838 // parent the listctrl on the STC window and then call SetFocus and
839 // then reparent it back to the popup.
848 // Set position in client coords
849 virtual void DoSetSize(int x
, int y
,
850 int width
, int height
,
851 int sizeFlags
= wxSIZE_AUTO
) {
852 if (x
!= wxDefaultCoord
) {
853 GetParent()->ClientToScreen(&x
, NULL
);
855 if (y
!= wxDefaultCoord
) {
856 GetParent()->ClientToScreen(NULL
, &y
);
858 wxPopupWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
861 // return position as if it were in client coords
862 virtual void DoGetPosition( int *x
, int *y
) const {
864 wxPopupWindow::DoGetPosition(&sx
, &sy
);
865 GetParent()->ScreenToClient(&sx
, &sy
);
872 if ( !wxPendingDelete
.Member(this) )
873 wxPendingDelete
.Append(this);
879 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
882 il
->GetSize(0, w
, h
);
889 void SetDoubleClickAction(CallBackAction action
, void *data
) {
890 doubleClickAction
= action
;
891 doubleClickActionData
= data
;
895 void OnFocus(wxFocusEvent
& event
) {
896 GetParent()->SetFocus();
900 void OnSize(wxSizeEvent
& event
) {
902 wxSize sz
= GetSize();
905 lv
->SetSize(1, 1, sz
.x
, sz
.y
);
906 // reset the column widths
907 lv
->SetColumnWidth(0, IconWidth()+4);
908 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
909 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
913 void OnActivate(wxListEvent
& WXUNUSED(event
)) {
914 doubleClickAction(doubleClickActionData
);
917 wxListView
* GetLB() { return lv
; }
920 DECLARE_EVENT_TABLE()
924 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxPopupWindow
)
925 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
926 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
927 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
932 #else // !wxUSE_POPUPWIN -----------------------------------
933 #include "wx/frame.h"
935 // A normal window to place the wxSTCListBox upon, but make it behave as much
936 // like a wxPopupWindow as possible
937 class wxSTCListBoxWin
: public wxFrame
{
940 CallBackAction doubleClickAction
;
941 void* doubleClickActionData
;
943 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point location
) :
944 wxFrame(parent
, id
, wxEmptyString
, wxPoint(location
.x
, location
.y
), wxSize(0,0),
946 | wxFRAME_FLOAT_ON_PARENT
956 lv
= new wxSTCListBox(this, id
, wxDefaultPosition
, wxDefaultSize
,
957 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxNO_BORDER
);
958 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
959 lv
->InsertColumn(0, wxEmptyString
);
960 lv
->InsertColumn(1, wxEmptyString
);
962 // Eventhough we immediately reset the focus to the parent, this helps
963 // things to look right...
970 // On OSX and (possibly others) there can still be pending
971 // messages/events for the list control when Scintilla wants to
972 // close it, so do a pending delete of it instead of destroying
977 // The bottom edge of this window is not getting properly
978 // refreshed upon deletion, so help it out...
979 wxWindow
* p
= GetParent();
980 wxRect
r(GetPosition(), GetSize());
981 r
.SetHeight(r
.GetHeight()+1);
982 p
->Refresh(false, &r
);
984 if ( !wxPendingDelete
.Member(this) )
985 wxPendingDelete
.Append(this);
992 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
995 il
->GetSize(0, w
, h
);
1002 void SetDoubleClickAction(CallBackAction action
, void *data
)
1004 doubleClickAction
= action
;
1005 doubleClickActionData
= data
;
1009 void OnFocus(wxFocusEvent
& event
)
1012 GetParent()->SetFocus();
1016 void OnSize(wxSizeEvent
& event
)
1019 wxSize sz
= GetClientSize();
1021 // reset the column widths
1022 lv
->SetColumnWidth(0, IconWidth()+4);
1023 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
1024 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
1028 void ActivateParent()
1030 // Although we're a frame, we always want the parent to be active, so
1031 // raise it whenever we get shown, focused, etc.
1032 wxTopLevelWindow
*frame
= wxDynamicCast(
1033 wxGetTopLevelParent(GetParent()), wxTopLevelWindow
);
1039 virtual void DoSetSize(int x
, int y
,
1040 int width
, int height
,
1041 int sizeFlags
= wxSIZE_AUTO
)
1043 // convert coords to screen coords since we're a top-level window
1044 if (x
!= wxDefaultCoord
) {
1045 GetParent()->ClientToScreen(&x
, NULL
);
1047 if (y
!= wxDefaultCoord
) {
1048 GetParent()->ClientToScreen(NULL
, &y
);
1050 wxFrame::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1053 virtual bool Show(bool show
= true)
1055 bool rv
= wxFrame::Show(show
);
1059 GetParent()->Refresh(false);
1064 void OnActivate(wxListEvent
& WXUNUSED(event
))
1066 doubleClickAction(doubleClickActionData
);
1069 wxListView
* GetLB() { return lv
; }
1072 DECLARE_EVENT_TABLE()
1076 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxWindow
)
1077 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
1078 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
1079 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
1082 #endif // wxUSE_POPUPWIN -----------------------------------
1085 inline wxSTCListBoxWin
* GETLBW(WindowID win
) {
1086 return ((wxSTCListBoxWin
*)win
);
1089 inline wxListView
* GETLB(WindowID win
) {
1090 return GETLBW(win
)->GetLB();
1093 //----------------------------------------------------------------------
1095 class ListBoxImpl
: public ListBox
{
1099 int desiredVisibleRows
;
1102 Point location
; // Caret location at which the list is opened
1103 wxImageList
* imgList
;
1104 wxArrayInt
* imgTypeMap
;
1110 virtual void SetFont(Font
&font
);
1111 virtual void Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
);
1112 virtual void SetAverageCharWidth(int width
);
1113 virtual void SetVisibleRows(int rows
);
1114 virtual int GetVisibleRows() const;
1115 virtual PRectangle
GetDesiredRect();
1116 virtual int CaretFromEdge();
1117 virtual void Clear();
1118 virtual void Append(char *s
, int type
= -1);
1119 void Append(const wxString
& text
, int type
);
1120 virtual int Length();
1121 virtual void Select(int n
);
1122 virtual int GetSelection();
1123 virtual int Find(const char *prefix
);
1124 virtual void GetValue(int n
, char *value
, int len
);
1125 virtual void RegisterImage(int type
, const char *xpm_data
);
1126 virtual void ClearRegisteredImages();
1127 virtual void SetDoubleClickAction(CallBackAction
, void *);
1128 virtual void SetList(const char* list
, char separator
, char typesep
);
1132 ListBoxImpl::ListBoxImpl()
1133 : lineHeight(10), unicodeMode(false),
1134 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
1135 imgList(NULL
), imgTypeMap(NULL
)
1139 ListBoxImpl::~ListBoxImpl() {
1151 void ListBoxImpl::SetFont(Font
&font
) {
1152 GETLB(id
)->SetFont(*((wxFont
*)font
.GetID()));
1156 void ListBoxImpl::Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
) {
1157 location
= location_
;
1158 lineHeight
= lineHeight_
;
1159 unicodeMode
= unicodeMode_
;
1161 id
= new wxSTCListBoxWin(GETWIN(parent
.GetID()), ctrlID
, location
);
1162 if (imgList
!= NULL
)
1163 GETLB(id
)->SetImageList(imgList
, wxIMAGE_LIST_SMALL
);
1167 void ListBoxImpl::SetAverageCharWidth(int width
) {
1168 aveCharWidth
= width
;
1172 void ListBoxImpl::SetVisibleRows(int rows
) {
1173 desiredVisibleRows
= rows
;
1177 int ListBoxImpl::GetVisibleRows() const {
1178 return desiredVisibleRows
;
1181 PRectangle
ListBoxImpl::GetDesiredRect() {
1182 // wxListCtrl doesn't have a DoGetBestSize, so instead we kept track of
1183 // the max size in Append and calculate it here...
1184 int maxw
= maxStrWidth
* aveCharWidth
;
1187 // give it a default if there are no lines, and/or add a bit more
1188 if (maxw
== 0) maxw
= 100;
1189 maxw
+= aveCharWidth
* 3 +
1190 GETLBW(id
)->IconWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1194 // estimate a desired height
1195 int count
= GETLB(id
)->GetItemCount();
1198 GETLB(id
)->GetItemRect(0, rect
);
1199 maxh
= count
* rect
.GetHeight();
1200 if (maxh
> 140) // TODO: Use desiredVisibleRows??
1203 // Try to make the size an exact multiple of some number of lines
1204 int lines
= maxh
/ rect
.GetHeight();
1205 maxh
= (lines
+ 1) * rect
.GetHeight() + 2;
1219 int ListBoxImpl::CaretFromEdge() {
1220 return 4 + GETLBW(id
)->IconWidth();
1224 void ListBoxImpl::Clear() {
1225 GETLB(id
)->DeleteAllItems();
1229 void ListBoxImpl::Append(char *s
, int type
) {
1230 Append(stc2wx(s
), type
);
1233 void ListBoxImpl::Append(const wxString
& text
, int type
) {
1234 long count
= GETLB(id
)->GetItemCount();
1235 long itemID
= GETLB(id
)->InsertItem(count
, wxEmptyString
);
1236 GETLB(id
)->SetItem(itemID
, 1, text
);
1237 maxStrWidth
= wxMax(maxStrWidth
, text
.length());
1239 wxCHECK_RET(imgTypeMap
, wxT("Unexpected NULL imgTypeMap"));
1240 long idx
= imgTypeMap
->Item(type
);
1241 GETLB(id
)->SetItemImage(itemID
, idx
, idx
);
1245 void ListBoxImpl::SetList(const char* list
, char separator
, char typesep
) {
1246 GETLB(id
)->Freeze();
1248 wxStringTokenizer
tkzr(stc2wx(list
), (wxChar
)separator
);
1249 while ( tkzr
.HasMoreTokens() ) {
1250 wxString token
= tkzr
.GetNextToken();
1252 int pos
= token
.Find(typesep
);
1254 token
.Mid(pos
+1).ToLong(&type
);
1255 token
.Truncate(pos
);
1257 Append(token
, (int)type
);
1263 int ListBoxImpl::Length() {
1264 return GETLB(id
)->GetItemCount();
1268 void ListBoxImpl::Select(int n
) {
1274 GETLB(id
)->EnsureVisible(n
);
1275 GETLB(id
)->Select(n
, select
);
1279 int ListBoxImpl::GetSelection() {
1280 return GETLB(id
)->GetFirstSelected();
1284 int ListBoxImpl::Find(const char *WXUNUSED(prefix
)) {
1290 void ListBoxImpl::GetValue(int n
, char *value
, int len
) {
1294 item
.SetMask(wxLIST_MASK_TEXT
);
1295 GETLB(id
)->GetItem(item
);
1296 strncpy(value
, wx2stc(item
.GetText()), len
);
1297 value
[len
-1] = '\0';
1301 void ListBoxImpl::RegisterImage(int type
, const char *xpm_data
) {
1302 wxMemoryInputStream
stream(xpm_data
, strlen(xpm_data
)+1);
1303 wxImage
img(stream
, wxBITMAP_TYPE_XPM
);
1307 // assumes all images are the same size
1308 imgList
= new wxImageList(bmp
.GetWidth(), bmp
.GetHeight(), true);
1309 imgTypeMap
= new wxArrayInt
;
1312 int idx
= imgList
->Add(bmp
);
1314 // do we need to extend the mapping array?
1315 wxArrayInt
& itm
= *imgTypeMap
;
1316 if ( itm
.GetCount() < (size_t)type
+1)
1317 itm
.Add(-1, type
- itm
.GetCount() + 1);
1319 // Add an item that maps type to the image index
1323 void ListBoxImpl::ClearRegisteredImages() {
1333 GETLB(id
)->SetImageList(NULL
, wxIMAGE_LIST_SMALL
);
1337 void ListBoxImpl::SetDoubleClickAction(CallBackAction action
, void *data
) {
1338 GETLBW(id
)->SetDoubleClickAction(action
, data
);
1342 ListBox::ListBox() {
1345 ListBox::~ListBox() {
1348 ListBox
*ListBox::Allocate() {
1349 return new ListBoxImpl();
1352 //----------------------------------------------------------------------
1354 Menu::Menu() : id(0) {
1357 void Menu::CreatePopUp() {
1362 void Menu::Destroy() {
1368 void Menu::Show(Point pt
, Window
&w
) {
1369 GETWIN(w
.GetID())->PopupMenu((wxMenu
*)id
, pt
.x
- 4, pt
.y
);
1373 //----------------------------------------------------------------------
1375 DynamicLibrary
*DynamicLibrary::Load(const char *WXUNUSED(modulePath
)) {
1376 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1380 //----------------------------------------------------------------------
1382 ColourDesired
Platform::Chrome() {
1384 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1385 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1388 ColourDesired
Platform::ChromeHighlight() {
1390 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
);
1391 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1394 const char *Platform::DefaultFont() {
1395 static char buf
[128];
1396 strcpy(buf
, wxNORMAL_FONT
->GetFaceName().mbc_str());
1400 int Platform::DefaultFontSize() {
1401 return wxNORMAL_FONT
->GetPointSize();
1404 unsigned int Platform::DoubleClickTime() {
1405 return 500; // **** ::GetDoubleClickTime();
1408 bool Platform::MouseButtonBounce() {
1412 bool Platform::IsKeyDown(int WXUNUSED(key
)) {
1413 return false; // I don't think we'll need this.
1416 long Platform::SendScintilla(WindowID w
,
1418 unsigned long wParam
,
1421 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1422 return stc
->SendMsg(msg
, wParam
, lParam
);
1425 long Platform::SendScintillaPointer(WindowID w
,
1427 unsigned long wParam
,
1430 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1431 return stc
->SendMsg(msg
, wParam
, (long)lParam
);
1435 // These are utility functions not really tied to a platform
1437 int Platform::Minimum(int a
, int b
) {
1444 int Platform::Maximum(int a
, int b
) {
1453 void Platform::DebugDisplay(const char *s
) {
1455 wxLogDebug(stc2wx(s
));
1461 void Platform::DebugPrintf(const char *format
, ...) {
1465 va_start(pArguments
, format
);
1466 vsprintf(buffer
,format
,pArguments
);
1468 Platform::DebugDisplay(buffer
);
1470 wxUnusedVar(format
);
1475 static bool assertionPopUps
= true;
1477 bool Platform::ShowAssertionPopUps(bool assertionPopUps_
) {
1478 bool ret
= assertionPopUps
;
1479 assertionPopUps
= assertionPopUps_
;
1483 void Platform::Assert(const char *c
, const char *file
, int line
) {
1486 sprintf(buffer
, "Assertion [%s] failed at %s %d", c
, file
, line
);
1487 if (assertionPopUps
) {
1489 wxMessageBox(stc2wx(buffer
),
1490 wxT("Assertion failure"),
1491 wxICON_HAND
| wxOK
);
1493 strcat(buffer
, "\r\n");
1494 Platform::DebugDisplay(buffer
);
1505 int Platform::Clamp(int val
, int minVal
, int maxVal
) {
1514 bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage
), char WXUNUSED(ch
)) {
1518 int Platform::DBCSCharLength(int WXUNUSED(codePage
), const char *WXUNUSED(s
)) {
1522 int Platform::DBCSCharMaxLength() {
1527 //----------------------------------------------------------------------
1529 ElapsedTime::ElapsedTime() {
1530 wxLongLong localTime
= wxGetLocalTimeMillis();
1531 littleBit
= localTime
.GetLo();
1532 bigBit
= localTime
.GetHi();
1535 double ElapsedTime::Duration(bool reset
) {
1536 wxLongLong
prevTime(bigBit
, littleBit
);
1537 wxLongLong localTime
= wxGetLocalTimeMillis();
1539 littleBit
= localTime
.GetLo();
1540 bigBit
= localTime
.GetHi();
1542 wxLongLong duration
= localTime
- prevTime
;
1543 double result
= duration
.ToDouble();
1549 //----------------------------------------------------------------------
1553 #include "UniConversion.h"
1555 // Convert using Scintilla's functions instead of wx's, Scintilla's are more
1556 // forgiving and won't assert...
1558 wxString
stc2wx(const char* str
, size_t len
)
1561 return wxEmptyString
;
1563 size_t wclen
= UTF16Length(str
, len
);
1564 wxWCharBuffer
buffer(wclen
+1);
1566 size_t actualLen
= UTF16FromUTF8(str
, len
, buffer
.data(), wclen
+1);
1567 return wxString(buffer
.data(), actualLen
);
1572 wxString
stc2wx(const char* str
)
1574 return stc2wx(str
, strlen(str
));
1578 const wxWX2MBbuf
wx2stc(const wxString
& str
)
1580 const wchar_t* wcstr
= str
.c_str();
1581 size_t wclen
= str
.length();
1582 size_t len
= UTF8Length(wcstr
, wclen
);
1584 wxCharBuffer
buffer(len
+1);
1585 UTF8FromUTF16(wcstr
, wclen
, buffer
.data(), len
);
1587 // TODO check NULL termination!!