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"
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
,
142 int size
, bool bold
, bool italic
,
143 bool extraFontFlag
) {
146 // The minus one is done because since Scintilla uses SC_SHARSET_DEFAULT
147 // internally and we need to have wxFONENCODING_DEFAULT == SC_SHARSET_DEFAULT
148 // so we adjust the encoding before passing it to Scintilla. See also
149 // wxStyledTextCtrl::StyleSetCharacterSet
150 wxFontEncoding encoding
= (wxFontEncoding
)(characterSet
-1);
152 wxFontEncodingArray ea
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
156 wxFont
* font
= new wxFont(size
,
158 italic
? wxITALIC
: wxNORMAL
,
159 bold
? wxBOLD
: wxNORMAL
,
163 font
->SetNoAntiAliasing(!extraFontFlag
);
168 void Font::Release() {
174 //----------------------------------------------------------------------
176 class SurfaceImpl
: public Surface
{
189 virtual void Init(WindowID wid
);
190 virtual void Init(SurfaceID sid
, WindowID wid
);
191 virtual void InitPixMap(int width
, int height
, Surface
*surface_
, WindowID wid
);
193 virtual void Release();
194 virtual bool Initialised();
195 virtual void PenColour(ColourAllocated fore
);
196 virtual int LogPixelsY();
197 virtual int DeviceHeightFont(int points
);
198 virtual void MoveTo(int x_
, int y_
);
199 virtual void LineTo(int x_
, int y_
);
200 virtual void Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
);
201 virtual void RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
202 virtual void FillRectangle(PRectangle rc
, ColourAllocated back
);
203 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
);
204 virtual void RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
205 virtual void AlphaRectangle(PRectangle rc
, int cornerSize
, ColourAllocated fill
, int alphaFill
,
206 ColourAllocated outline
, int alphaOutline
, int flags
);
207 virtual void Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
208 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
);
210 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
211 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
212 virtual void DrawTextTransparent(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
);
213 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, int *positions
);
214 virtual int WidthText(Font
&font_
, const char *s
, int len
);
215 virtual int WidthChar(Font
&font_
, char ch
);
216 virtual int Ascent(Font
&font_
);
217 virtual int Descent(Font
&font_
);
218 virtual int InternalLeading(Font
&font_
);
219 virtual int ExternalLeading(Font
&font_
);
220 virtual int Height(Font
&font_
);
221 virtual int AverageCharWidth(Font
&font_
);
223 virtual int SetPalette(Palette
*pal
, bool inBackGround
);
224 virtual void SetClip(PRectangle rc
);
225 virtual void FlushCachedState();
227 virtual void SetUnicodeMode(bool unicodeMode_
);
228 virtual void SetDBCSMode(int codePage
);
230 void BrushColour(ColourAllocated back
);
231 void SetFont(Font
&font_
);
236 SurfaceImpl::SurfaceImpl() :
237 hdc(0), hdcOwned(0), bitmap(0),
238 x(0), y(0), unicodeMode(0)
241 SurfaceImpl::~SurfaceImpl() {
245 void SurfaceImpl::Init(WindowID wid
) {
248 hdc
= new wxMemoryDC();
251 // On Mac and GTK the DC is not really valid until it has a bitmap
252 // selected into it. So instead of just creating the DC with no bitmap,
253 // go ahead and give it one.
254 InitPixMap(1,1,NULL
,wid
);
258 void SurfaceImpl::Init(SurfaceID hdc_
, WindowID
) {
263 void SurfaceImpl::InitPixMap(int width
, int height
, Surface
*WXUNUSED(surface_
), WindowID
) {
265 hdc
= new wxMemoryDC();
267 if (width
< 1) width
= 1;
268 if (height
< 1) height
= 1;
269 bitmap
= new wxBitmap(width
, height
);
270 ((wxMemoryDC
*)hdc
)->SelectObject(*bitmap
);
274 void SurfaceImpl::Release() {
276 ((wxMemoryDC
*)hdc
)->SelectObject(wxNullBitmap
);
288 bool SurfaceImpl::Initialised() {
293 void SurfaceImpl::PenColour(ColourAllocated fore
) {
294 hdc
->SetPen(wxPen(wxColourFromCA(fore
)));
297 void SurfaceImpl::BrushColour(ColourAllocated back
) {
298 hdc
->SetBrush(wxBrush(wxColourFromCA(back
)));
301 void SurfaceImpl::SetFont(Font
&font_
) {
303 hdc
->SetFont(*((wxFont
*)font_
.GetID()));
307 int SurfaceImpl::LogPixelsY() {
308 return hdc
->GetPPI().y
;
311 int SurfaceImpl::DeviceHeightFont(int points
) {
315 void SurfaceImpl::MoveTo(int x_
, int y_
) {
320 void SurfaceImpl::LineTo(int x_
, int y_
) {
321 hdc
->DrawLine(x
,y
, x_
,y_
);
326 void SurfaceImpl::Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
) {
329 hdc
->DrawPolygon(npts
, (wxPoint
*)pts
);
332 void SurfaceImpl::RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
335 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
338 void SurfaceImpl::FillRectangle(PRectangle rc
, ColourAllocated back
) {
340 hdc
->SetPen(*wxTRANSPARENT_PEN
);
341 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
344 void SurfaceImpl::FillRectangle(PRectangle rc
, Surface
&surfacePattern
) {
346 if (((SurfaceImpl
&)surfacePattern
).bitmap
)
347 br
= wxBrush(*((SurfaceImpl
&)surfacePattern
).bitmap
);
348 else // Something is wrong so display in red
349 br
= wxBrush(*wxRED
);
350 hdc
->SetPen(*wxTRANSPARENT_PEN
);
352 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
355 void SurfaceImpl::RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
358 hdc
->DrawRoundedRectangle(wxRectFromPRectangle(rc
), 4);
362 #define wxPy_premultiply(p, a) ((p) * (a) / 0xff)
364 #define wxPy_premultiply(p, a) (p)
367 void SurfaceImpl::AlphaRectangle(PRectangle rc
, int cornerSize
,
368 ColourAllocated fill
, int alphaFill
,
369 ColourAllocated outline
, int alphaOutline
,
371 #ifdef wxHAS_RAW_BITMAP
373 // TODO: do something with cornerSize
374 wxUnusedVar(cornerSize
);
377 wxRect r
= wxRectFromPRectangle(rc
);
378 wxBitmap
bmp(r
.width
, r
.height
, 32);
379 wxAlphaPixelData
pixData(bmp
);
381 // Set the fill pixels
382 ColourDesired
cdf(fill
.AsLong());
383 int red
= cdf
.GetRed();
384 int green
= cdf
.GetGreen();
385 int blue
= cdf
.GetBlue();
387 wxAlphaPixelData::Iterator
p(pixData
);
388 for (y
=0; y
<r
.height
; y
++) {
389 p
.MoveTo(pixData
, 0, y
);
390 for (x
=0; x
<r
.width
; x
++) {
391 p
.Red() = wxPy_premultiply(red
, alphaFill
);
392 p
.Green() = wxPy_premultiply(green
, alphaFill
);
393 p
.Blue() = wxPy_premultiply(blue
, alphaFill
);
394 p
.Alpha() = alphaFill
;
399 // Set the outline pixels
400 ColourDesired
cdo(outline
.AsLong());
402 green
= cdo
.GetGreen();
403 blue
= cdo
.GetBlue();
404 for (x
=0; x
<r
.width
; x
++) {
405 p
.MoveTo(pixData
, x
, 0);
406 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
407 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
408 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
409 p
.Alpha() = alphaOutline
;
410 p
.MoveTo(pixData
, x
, r
.height
-1);
411 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
412 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
413 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
414 p
.Alpha() = alphaOutline
;
417 for (y
=0; y
<r
.height
; y
++) {
418 p
.MoveTo(pixData
, 0, y
);
419 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
420 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
421 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
422 p
.Alpha() = alphaOutline
;
423 p
.MoveTo(pixData
, r
.width
-1, y
);
424 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
425 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
426 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
427 p
.Alpha() = alphaOutline
;
431 hdc
->DrawBitmap(bmp
, r
.x
, r
.y
, true);
434 wxUnusedVar(cornerSize
);
435 wxUnusedVar(alphaFill
);
436 wxUnusedVar(alphaOutline
);
437 RectangleDraw(rc
, outline
, fill
);
441 void SurfaceImpl::Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
444 hdc
->DrawEllipse(wxRectFromPRectangle(rc
));
447 void SurfaceImpl::Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
) {
448 wxRect r
= wxRectFromPRectangle(rc
);
449 hdc
->Blit(r
.x
, r
.y
, r
.width
, r
.height
,
450 ((SurfaceImpl
&)surfaceSource
).hdc
,
451 from
.x
, from
.y
, wxCOPY
);
454 void SurfaceImpl::DrawTextNoClip(PRectangle rc
, Font
&font
, int ybase
,
455 const char *s
, int len
,
456 ColourAllocated fore
, ColourAllocated back
) {
458 hdc
->SetTextForeground(wxColourFromCA(fore
));
459 hdc
->SetTextBackground(wxColourFromCA(back
));
460 FillRectangle(rc
, back
);
462 // ybase is where the baseline should be, but wxWin uses the upper left
463 // corner, so I need to calculate the real position for the text...
464 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
467 void SurfaceImpl::DrawTextClipped(PRectangle rc
, Font
&font
, int ybase
,
468 const char *s
, int len
,
469 ColourAllocated fore
, ColourAllocated back
) {
471 hdc
->SetTextForeground(wxColourFromCA(fore
));
472 hdc
->SetTextBackground(wxColourFromCA(back
));
473 FillRectangle(rc
, back
);
474 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
476 // see comments above
477 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
478 hdc
->DestroyClippingRegion();
482 void SurfaceImpl::DrawTextTransparent(PRectangle rc
, Font
&font
, int ybase
,
483 const char *s
, int len
,
484 ColourAllocated fore
) {
487 hdc
->SetTextForeground(wxColourFromCA(fore
));
488 hdc
->SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
490 // ybase is where the baseline should be, but wxWin uses the upper left
491 // corner, so I need to calculate the real position for the text...
492 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
494 hdc
->SetBackgroundMode(wxBRUSHSTYLE_SOLID
);
498 void SurfaceImpl::MeasureWidths(Font
&font
, const char *s
, int len
, int *positions
) {
500 wxString str
= stc2wx(s
, len
);
505 hdc
->GetPartialTextExtents(str
, tpos
);
508 // Map the widths for UCS-2 characters back to the UTF-8 input string
509 // NOTE: I don't think this is right for when sizeof(wxChar) > 2, ie wxGTK2
510 // so figure it out and fix it!
513 while ((int)i
< len
) {
514 unsigned char uch
= (unsigned char)s
[i
];
515 positions
[i
++] = tpos
[ui
];
517 if (uch
< (0x80 + 0x40 + 0x20)) {
518 positions
[i
++] = tpos
[ui
];
520 positions
[i
++] = tpos
[ui
];
521 positions
[i
++] = tpos
[ui
];
528 // If not unicode then just use the widths we have
530 std::copy(tpos
.begin(), tpos
.end(), positions
);
532 memcpy(positions
, tpos
.begin(), len
* sizeof(int));
538 int SurfaceImpl::WidthText(Font
&font
, const char *s
, int len
) {
543 hdc
->GetTextExtent(stc2wx(s
, len
), &w
, &h
);
548 int SurfaceImpl::WidthChar(Font
&font
, char ch
) {
552 char s
[2] = { ch
, 0 };
554 hdc
->GetTextExtent(stc2wx(s
, 1), &w
, &h
);
558 #define EXTENT_TEST wxT(" `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
560 int SurfaceImpl::Ascent(Font
&font
) {
563 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
568 int SurfaceImpl::Descent(Font
&font
) {
571 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
575 int SurfaceImpl::InternalLeading(Font
&WXUNUSED(font
)) {
579 int SurfaceImpl::ExternalLeading(Font
&font
) {
582 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
586 int SurfaceImpl::Height(Font
&font
) {
588 return hdc
->GetCharHeight() + 1;
591 int SurfaceImpl::AverageCharWidth(Font
&font
) {
593 return hdc
->GetCharWidth();
596 int SurfaceImpl::SetPalette(Palette
*WXUNUSED(pal
), bool WXUNUSED(inBackGround
)) {
600 void SurfaceImpl::SetClip(PRectangle rc
) {
601 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
604 void SurfaceImpl::FlushCachedState() {
607 void SurfaceImpl::SetUnicodeMode(bool unicodeMode_
) {
608 unicodeMode
=unicodeMode_
;
611 void SurfaceImpl::SetDBCSMode(int WXUNUSED(codePage
)) {
612 // dbcsMode = codePage == SC_CP_DBCS;
616 Surface
*Surface::Allocate() {
617 return new SurfaceImpl
;
621 //----------------------------------------------------------------------
624 inline wxWindow
* GETWIN(WindowID id
) { return (wxWindow
*)id
; }
629 void Window::Destroy() {
632 GETWIN(id
)->Destroy();
637 bool Window::HasFocus() {
638 return wxWindow::FindFocus() == GETWIN(id
);
641 PRectangle
Window::GetPosition() {
642 if (! id
) return PRectangle();
643 wxRect
rc(GETWIN(id
)->GetPosition(), GETWIN(id
)->GetSize());
644 return PRectangleFromwxRect(rc
);
647 void Window::SetPosition(PRectangle rc
) {
648 wxRect r
= wxRectFromPRectangle(rc
);
649 GETWIN(id
)->SetSize(r
);
652 void Window::SetPositionRelative(PRectangle rc
, Window
) {
653 SetPosition(rc
); // ????
656 PRectangle
Window::GetClientPosition() {
657 if (! id
) return PRectangle();
658 wxSize sz
= GETWIN(id
)->GetClientSize();
659 return PRectangle(0, 0, sz
.x
, sz
.y
);
662 void Window::Show(bool show
) {
663 GETWIN(id
)->Show(show
);
666 void Window::InvalidateAll() {
667 GETWIN(id
)->Refresh(false);
670 void Window::InvalidateRectangle(PRectangle rc
) {
671 wxRect r
= wxRectFromPRectangle(rc
);
672 GETWIN(id
)->Refresh(false, &r
);
675 void Window::SetFont(Font
&font
) {
676 GETWIN(id
)->SetFont(*((wxFont
*)font
.GetID()));
679 void Window::SetCursor(Cursor curs
) {
684 cursorId
= wxCURSOR_IBEAM
;
687 cursorId
= wxCURSOR_ARROW
;
690 cursorId
= wxCURSOR_ARROW
; // ** no up arrow... wxCURSOR_UPARROW;
693 cursorId
= wxCURSOR_WAIT
;
696 cursorId
= wxCURSOR_SIZEWE
;
699 cursorId
= wxCURSOR_SIZENS
;
701 case cursorReverseArrow
:
702 cursorId
= wxCURSOR_RIGHT_ARROW
;
705 cursorId
= wxCURSOR_HAND
;
708 cursorId
= wxCURSOR_ARROW
;
712 wxCursor wc
= wxStockCursor(cursorId
) ;
714 wxCursor wc
= wxCursor(cursorId
) ;
716 if(curs
!= cursorLast
)
718 GETWIN(id
)->SetCursor(wc
);
724 void Window::SetTitle(const char *s
) {
725 GETWIN(id
)->SetLabel(stc2wx(s
));
729 // Returns rectangle of monitor pt is on
730 PRectangle
Window::GetMonitorRect(Point pt
) {
732 if (! id
) return PRectangle();
734 // Get the display the point is found on
735 int n
= wxDisplay::GetFromPoint(wxPoint(pt
.x
, pt
.y
));
736 wxDisplay
dpy(n
== wxNOT_FOUND
? 0 : n
);
737 rect
= dpy
.GetGeometry();
739 return PRectangleFromwxRect(rect
);
742 //----------------------------------------------------------------------
743 // Helper classes for ListBox
746 // This is a simple subclass of wxListView that just resets focus to the
747 // parent when it gets it.
748 class wxSTCListBox
: public wxListView
{
750 wxSTCListBox(wxWindow
* parent
, wxWindowID id
,
751 const wxPoint
& pos
, const wxSize
& size
,
756 Hide(); // don't flicker as we move it around...
758 Create(parent
, id
, pos
, size
, style
);
762 void OnFocus(wxFocusEvent
& event
) {
763 GetParent()->SetFocus();
767 void OnKillFocus(wxFocusEvent
& WXUNUSED(event
)) {
768 // Do nothing. Prevents base class from resetting the colors...
772 // For some reason I don't understand yet the focus doesn't really leave
773 // the listbox like it should, so if we get any events feed them back to
775 void OnKeyDown(wxKeyEvent
& event
) {
776 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
778 void OnChar(wxKeyEvent
& event
) {
779 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
782 // And we need to force the focus back when being destroyed
784 GetGrandParent()->SetFocus();
789 DECLARE_EVENT_TABLE()
792 BEGIN_EVENT_TABLE(wxSTCListBox
, wxListView
)
793 EVT_SET_FOCUS( wxSTCListBox::OnFocus
)
794 EVT_KILL_FOCUS(wxSTCListBox::OnKillFocus
)
796 EVT_KEY_DOWN( wxSTCListBox::OnKeyDown
)
797 EVT_CHAR( wxSTCListBox::OnChar
)
803 #if wxUSE_POPUPWIN //-----------------------------------
804 #include "wx/popupwin.h"
808 // TODO: Refactor these two classes to have a common base (or a mix-in) to get
809 // rid of the code duplication. (Either that or convince somebody to
810 // implement wxPopupWindow for the Mac!!)
812 // In the meantime, be careful to duplicate any changes as needed...
815 // A popup window to place the wxSTCListBox upon
816 class wxSTCListBoxWin
: public wxPopupWindow
820 CallBackAction doubleClickAction
;
821 void* doubleClickActionData
;
823 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point
WXUNUSED(location
)) :
824 wxPopupWindow(parent
, wxBORDER_NONE
)
827 SetBackgroundColour(*wxBLACK
); // for our simple border
829 lv
= new wxSTCListBox(parent
, id
, wxPoint(-50,-50), wxDefaultSize
,
830 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxBORDER_NONE
);
831 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
832 lv
->InsertColumn(0, wxEmptyString
);
833 lv
->InsertColumn(1, wxEmptyString
);
835 // NOTE: We need to fool the wxListView into thinking that it has the
836 // focus so it will use the normal selection colour and will look
837 // "right" to the user. But since the wxPopupWindow or its children
838 // can't receive focus then we have to pull a fast one and temporarily
839 // parent the listctrl on the STC window and then call SetFocus and
840 // then reparent it back to the popup.
849 // Set position in client coords
850 virtual void DoSetSize(int x
, int y
,
851 int width
, int height
,
852 int sizeFlags
= wxSIZE_AUTO
) {
853 if (x
!= wxDefaultCoord
) {
854 GetParent()->ClientToScreen(&x
, NULL
);
856 if (y
!= wxDefaultCoord
) {
857 GetParent()->ClientToScreen(NULL
, &y
);
859 wxPopupWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
862 // return position as if it were in client coords
863 virtual void DoGetPosition( int *x
, int *y
) const {
865 wxPopupWindow::DoGetPosition(&sx
, &sy
);
866 GetParent()->ScreenToClient(&sx
, &sy
);
873 if ( !wxPendingDelete
.Member(this) )
874 wxPendingDelete
.Append(this);
880 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
883 il
->GetSize(0, w
, h
);
890 void SetDoubleClickAction(CallBackAction action
, void *data
) {
891 doubleClickAction
= action
;
892 doubleClickActionData
= data
;
896 void OnFocus(wxFocusEvent
& event
) {
897 GetParent()->SetFocus();
901 void OnSize(wxSizeEvent
& event
) {
903 wxSize sz
= GetSize();
906 lv
->SetSize(1, 1, sz
.x
, sz
.y
);
907 // reset the column widths
908 lv
->SetColumnWidth(0, IconWidth()+4);
909 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
910 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
914 void OnActivate(wxListEvent
& WXUNUSED(event
)) {
915 doubleClickAction(doubleClickActionData
);
918 wxListView
* GetLB() { return lv
; }
921 DECLARE_EVENT_TABLE()
925 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxPopupWindow
)
926 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
927 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
928 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
933 #else // !wxUSE_POPUPWIN -----------------------------------
934 #include "wx/frame.h"
936 // A normal window to place the wxSTCListBox upon, but make it behave as much
937 // like a wxPopupWindow as possible
938 class wxSTCListBoxWin
: public wxFrame
{
941 CallBackAction doubleClickAction
;
942 void* doubleClickActionData
;
944 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point location
) :
945 wxFrame(parent
, id
, wxEmptyString
, wxPoint(location
.x
, location
.y
), wxSize(0,0),
947 | wxFRAME_FLOAT_ON_PARENT
957 lv
= new wxSTCListBox(this, id
, wxDefaultPosition
, wxDefaultSize
,
958 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxNO_BORDER
);
959 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
960 lv
->InsertColumn(0, wxEmptyString
);
961 lv
->InsertColumn(1, wxEmptyString
);
963 // Eventhough we immediately reset the focus to the parent, this helps
964 // things to look right...
971 // On OSX and (possibly others) there can still be pending
972 // messages/events for the list control when Scintilla wants to
973 // close it, so do a pending delete of it instead of destroying
978 // The bottom edge of this window is not getting properly
979 // refreshed upon deletion, so help it out...
980 wxWindow
* p
= GetParent();
981 wxRect
r(GetPosition(), GetSize());
982 r
.SetHeight(r
.GetHeight()+1);
983 p
->Refresh(false, &r
);
985 if ( !wxPendingDelete
.Member(this) )
986 wxPendingDelete
.Append(this);
993 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
996 il
->GetSize(0, w
, h
);
1003 void SetDoubleClickAction(CallBackAction action
, void *data
)
1005 doubleClickAction
= action
;
1006 doubleClickActionData
= data
;
1010 void OnFocus(wxFocusEvent
& event
)
1013 GetParent()->SetFocus();
1017 void OnSize(wxSizeEvent
& event
)
1020 wxSize sz
= GetClientSize();
1022 // reset the column widths
1023 lv
->SetColumnWidth(0, IconWidth()+4);
1024 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
1025 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
1029 void ActivateParent()
1031 // Although we're a frame, we always want the parent to be active, so
1032 // raise it whenever we get shown, focused, etc.
1033 wxTopLevelWindow
*frame
= wxDynamicCast(
1034 wxGetTopLevelParent(GetParent()), wxTopLevelWindow
);
1040 virtual void DoSetSize(int x
, int y
,
1041 int width
, int height
,
1042 int sizeFlags
= wxSIZE_AUTO
)
1044 // convert coords to screen coords since we're a top-level window
1045 if (x
!= wxDefaultCoord
) {
1046 GetParent()->ClientToScreen(&x
, NULL
);
1048 if (y
!= wxDefaultCoord
) {
1049 GetParent()->ClientToScreen(NULL
, &y
);
1051 wxFrame::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1054 virtual bool Show(bool show
= true)
1056 bool rv
= wxFrame::Show(show
);
1060 GetParent()->Refresh(false);
1065 void OnActivate(wxListEvent
& WXUNUSED(event
))
1067 doubleClickAction(doubleClickActionData
);
1070 wxListView
* GetLB() { return lv
; }
1073 DECLARE_EVENT_TABLE()
1077 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxWindow
)
1078 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
1079 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
1080 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
1083 #endif // wxUSE_POPUPWIN -----------------------------------
1086 inline wxSTCListBoxWin
* GETLBW(WindowID win
) {
1087 return ((wxSTCListBoxWin
*)win
);
1090 inline wxListView
* GETLB(WindowID win
) {
1091 return GETLBW(win
)->GetLB();
1094 //----------------------------------------------------------------------
1096 class ListBoxImpl
: public ListBox
{
1100 int desiredVisibleRows
;
1103 Point location
; // Caret location at which the list is opened
1104 wxImageList
* imgList
;
1105 wxArrayInt
* imgTypeMap
;
1111 virtual void SetFont(Font
&font
);
1112 virtual void Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
);
1113 virtual void SetAverageCharWidth(int width
);
1114 virtual void SetVisibleRows(int rows
);
1115 virtual int GetVisibleRows() const;
1116 virtual PRectangle
GetDesiredRect();
1117 virtual int CaretFromEdge();
1118 virtual void Clear();
1119 virtual void Append(char *s
, int type
= -1);
1120 void Append(const wxString
& text
, int type
);
1121 virtual int Length();
1122 virtual void Select(int n
);
1123 virtual int GetSelection();
1124 virtual int Find(const char *prefix
);
1125 virtual void GetValue(int n
, char *value
, int len
);
1126 virtual void RegisterImage(int type
, const char *xpm_data
);
1127 virtual void ClearRegisteredImages();
1128 virtual void SetDoubleClickAction(CallBackAction
, void *);
1129 virtual void SetList(const char* list
, char separator
, char typesep
);
1133 ListBoxImpl::ListBoxImpl()
1134 : lineHeight(10), unicodeMode(false),
1135 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
1136 imgList(NULL
), imgTypeMap(NULL
)
1140 ListBoxImpl::~ListBoxImpl() {
1152 void ListBoxImpl::SetFont(Font
&font
) {
1153 GETLB(id
)->SetFont(*((wxFont
*)font
.GetID()));
1157 void ListBoxImpl::Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
) {
1158 location
= location_
;
1159 lineHeight
= lineHeight_
;
1160 unicodeMode
= unicodeMode_
;
1162 id
= new wxSTCListBoxWin(GETWIN(parent
.GetID()), ctrlID
, location
);
1163 if (imgList
!= NULL
)
1164 GETLB(id
)->SetImageList(imgList
, wxIMAGE_LIST_SMALL
);
1168 void ListBoxImpl::SetAverageCharWidth(int width
) {
1169 aveCharWidth
= width
;
1173 void ListBoxImpl::SetVisibleRows(int rows
) {
1174 desiredVisibleRows
= rows
;
1178 int ListBoxImpl::GetVisibleRows() const {
1179 return desiredVisibleRows
;
1182 PRectangle
ListBoxImpl::GetDesiredRect() {
1183 // wxListCtrl doesn't have a DoGetBestSize, so instead we kept track of
1184 // the max size in Append and calculate it here...
1185 int maxw
= maxStrWidth
* aveCharWidth
;
1188 // give it a default if there are no lines, and/or add a bit more
1189 if (maxw
== 0) maxw
= 100;
1190 maxw
+= aveCharWidth
* 3 +
1191 GETLBW(id
)->IconWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1195 // estimate a desired height
1196 int count
= GETLB(id
)->GetItemCount();
1199 GETLB(id
)->GetItemRect(0, rect
);
1200 maxh
= count
* rect
.GetHeight();
1201 if (maxh
> 140) // TODO: Use desiredVisibleRows??
1204 // Try to make the size an exact multiple of some number of lines
1205 int lines
= maxh
/ rect
.GetHeight();
1206 maxh
= (lines
+ 1) * rect
.GetHeight() + 2;
1220 int ListBoxImpl::CaretFromEdge() {
1221 return 4 + GETLBW(id
)->IconWidth();
1225 void ListBoxImpl::Clear() {
1226 GETLB(id
)->DeleteAllItems();
1230 void ListBoxImpl::Append(char *s
, int type
) {
1231 Append(stc2wx(s
), type
);
1234 void ListBoxImpl::Append(const wxString
& text
, int type
) {
1235 long count
= GETLB(id
)->GetItemCount();
1236 long itemID
= GETLB(id
)->InsertItem(count
, wxEmptyString
);
1238 GETLB(id
)->SetItem(itemID
, 1, text
);
1239 maxStrWidth
= wxMax(maxStrWidth
, text
.length());
1241 wxCHECK_RET(imgTypeMap
, wxT("Unexpected NULL imgTypeMap"));
1242 idx
= imgTypeMap
->Item(type
);
1244 GETLB(id
)->SetItemImage(itemID
, idx
, idx
);
1247 void ListBoxImpl::SetList(const char* list
, char separator
, char typesep
) {
1248 GETLB(id
)->Freeze();
1250 wxStringTokenizer
tkzr(stc2wx(list
), (wxChar
)separator
);
1251 while ( tkzr
.HasMoreTokens() ) {
1252 wxString token
= tkzr
.GetNextToken();
1254 int pos
= token
.Find(typesep
);
1256 token
.Mid(pos
+1).ToLong(&type
);
1257 token
.Truncate(pos
);
1259 Append(token
, (int)type
);
1265 int ListBoxImpl::Length() {
1266 return GETLB(id
)->GetItemCount();
1270 void ListBoxImpl::Select(int n
) {
1276 GETLB(id
)->EnsureVisible(n
);
1277 GETLB(id
)->Select(n
, select
);
1281 int ListBoxImpl::GetSelection() {
1282 return GETLB(id
)->GetFirstSelected();
1286 int ListBoxImpl::Find(const char *WXUNUSED(prefix
)) {
1292 void ListBoxImpl::GetValue(int n
, char *value
, int len
) {
1296 item
.SetMask(wxLIST_MASK_TEXT
);
1297 GETLB(id
)->GetItem(item
);
1298 strncpy(value
, wx2stc(item
.GetText()), len
);
1299 value
[len
-1] = '\0';
1303 void ListBoxImpl::RegisterImage(int type
, const char *xpm_data
) {
1304 wxMemoryInputStream
stream(xpm_data
, strlen(xpm_data
)+1);
1305 wxImage
img(stream
, wxBITMAP_TYPE_XPM
);
1309 // assumes all images are the same size
1310 imgList
= new wxImageList(bmp
.GetWidth(), bmp
.GetHeight(), true);
1311 imgTypeMap
= new wxArrayInt
;
1314 int idx
= imgList
->Add(bmp
);
1316 // do we need to extend the mapping array?
1317 wxArrayInt
& itm
= *imgTypeMap
;
1318 if ( itm
.GetCount() < (size_t)type
+1)
1319 itm
.Add(-1, type
- itm
.GetCount() + 1);
1321 // Add an item that maps type to the image index
1325 void ListBoxImpl::ClearRegisteredImages() {
1335 GETLB(id
)->SetImageList(NULL
, wxIMAGE_LIST_SMALL
);
1339 void ListBoxImpl::SetDoubleClickAction(CallBackAction action
, void *data
) {
1340 GETLBW(id
)->SetDoubleClickAction(action
, data
);
1344 ListBox::ListBox() {
1347 ListBox::~ListBox() {
1350 ListBox
*ListBox::Allocate() {
1351 return new ListBoxImpl();
1354 //----------------------------------------------------------------------
1356 Menu::Menu() : id(0) {
1359 void Menu::CreatePopUp() {
1364 void Menu::Destroy() {
1370 void Menu::Show(Point pt
, Window
&w
) {
1371 GETWIN(w
.GetID())->PopupMenu((wxMenu
*)id
, pt
.x
- 4, pt
.y
);
1375 //----------------------------------------------------------------------
1377 DynamicLibrary
*DynamicLibrary::Load(const char *WXUNUSED(modulePath
)) {
1378 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1382 //----------------------------------------------------------------------
1384 ColourDesired
Platform::Chrome() {
1386 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1387 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1390 ColourDesired
Platform::ChromeHighlight() {
1392 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
);
1393 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1396 const char *Platform::DefaultFont() {
1397 static char buf
[128];
1398 strcpy(buf
, wxNORMAL_FONT
->GetFaceName().mbc_str());
1402 int Platform::DefaultFontSize() {
1403 return wxNORMAL_FONT
->GetPointSize();
1406 unsigned int Platform::DoubleClickTime() {
1407 return 500; // **** ::GetDoubleClickTime();
1410 bool Platform::MouseButtonBounce() {
1414 bool Platform::IsKeyDown(int WXUNUSED(key
)) {
1415 return false; // I don't think we'll need this.
1418 long Platform::SendScintilla(WindowID w
,
1420 unsigned long wParam
,
1423 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1424 return stc
->SendMsg(msg
, wParam
, lParam
);
1427 long Platform::SendScintillaPointer(WindowID w
,
1429 unsigned long wParam
,
1432 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1433 return stc
->SendMsg(msg
, wParam
, (wxIntPtr
)lParam
);
1437 // These are utility functions not really tied to a platform
1439 int Platform::Minimum(int a
, int b
) {
1446 int Platform::Maximum(int a
, int b
) {
1455 void Platform::DebugDisplay(const char *s
) {
1457 wxLogDebug(stc2wx(s
));
1463 void Platform::DebugPrintf(const char *format
, ...) {
1467 va_start(pArguments
, format
);
1468 vsprintf(buffer
,format
,pArguments
);
1470 Platform::DebugDisplay(buffer
);
1472 wxUnusedVar(format
);
1477 static bool assertionPopUps
= true;
1479 bool Platform::ShowAssertionPopUps(bool assertionPopUps_
) {
1480 bool ret
= assertionPopUps
;
1481 assertionPopUps
= assertionPopUps_
;
1485 void Platform::Assert(const char *c
, const char *file
, int line
) {
1488 sprintf(buffer
, "Assertion [%s] failed at %s %d", c
, file
, line
);
1489 if (assertionPopUps
) {
1491 wxMessageBox(stc2wx(buffer
),
1492 wxT("Assertion failure"),
1493 wxICON_HAND
| wxOK
);
1495 strcat(buffer
, "\r\n");
1496 Platform::DebugDisplay(buffer
);
1507 int Platform::Clamp(int val
, int minVal
, int maxVal
) {
1516 bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage
), char WXUNUSED(ch
)) {
1520 int Platform::DBCSCharLength(int WXUNUSED(codePage
), const char *WXUNUSED(s
)) {
1524 int Platform::DBCSCharMaxLength() {
1529 //----------------------------------------------------------------------
1531 ElapsedTime::ElapsedTime() {
1532 wxLongLong localTime
= wxGetLocalTimeMillis();
1533 littleBit
= localTime
.GetLo();
1534 bigBit
= localTime
.GetHi();
1537 double ElapsedTime::Duration(bool reset
) {
1538 wxLongLong
prevTime(bigBit
, littleBit
);
1539 wxLongLong localTime
= wxGetLocalTimeMillis();
1541 littleBit
= localTime
.GetLo();
1542 bigBit
= localTime
.GetHi();
1544 wxLongLong duration
= localTime
- prevTime
;
1545 double result
= duration
.ToDouble();
1551 //----------------------------------------------------------------------
1555 #include "UniConversion.h"
1557 // Convert using Scintilla's functions instead of wx's, Scintilla's are more
1558 // forgiving and won't assert...
1560 wxString
stc2wx(const char* str
, size_t len
)
1563 return wxEmptyString
;
1565 size_t wclen
= UTF16Length(str
, len
);
1566 wxWCharBuffer
buffer(wclen
+1);
1568 size_t actualLen
= UTF16FromUTF8(str
, len
, buffer
.data(), wclen
+1);
1569 return wxString(buffer
.data(), actualLen
);
1574 wxString
stc2wx(const char* str
)
1576 return stc2wx(str
, strlen(str
));
1580 const wxWX2MBbuf
wx2stc(const wxString
& str
)
1582 const wchar_t* wcstr
= str
.c_str();
1583 size_t wclen
= str
.length();
1584 size_t len
= UTF8Length(wcstr
, wclen
);
1586 wxCharBuffer
buffer(len
+1);
1587 UTF8FromUTF16(wcstr
, wclen
, buffer
.data(), len
);
1589 // TODO check NULL termination!!