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
) {
680 wxStockCursor cursorId
;
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
= wxCursor(cursorId
);
713 if(curs
!= cursorLast
)
715 GETWIN(id
)->SetCursor(wc
);
721 void Window::SetTitle(const char *s
) {
722 GETWIN(id
)->SetLabel(stc2wx(s
));
726 // Returns rectangle of monitor pt is on
727 PRectangle
Window::GetMonitorRect(Point pt
) {
729 if (! id
) return PRectangle();
731 // Get the display the point is found on
732 int n
= wxDisplay::GetFromPoint(wxPoint(pt
.x
, pt
.y
));
733 wxDisplay
dpy(n
== wxNOT_FOUND
? 0 : n
);
734 rect
= dpy
.GetGeometry();
736 return PRectangleFromwxRect(rect
);
739 //----------------------------------------------------------------------
740 // Helper classes for ListBox
743 // This is a simple subclass of wxListView that just resets focus to the
744 // parent when it gets it.
745 class wxSTCListBox
: public wxListView
{
747 wxSTCListBox(wxWindow
* parent
, wxWindowID id
,
748 const wxPoint
& pos
, const wxSize
& size
,
753 Hide(); // don't flicker as we move it around...
755 Create(parent
, id
, pos
, size
, style
);
759 void OnFocus(wxFocusEvent
& event
) {
760 GetParent()->SetFocus();
764 void OnKillFocus(wxFocusEvent
& WXUNUSED(event
)) {
765 // Do nothing. Prevents base class from resetting the colors...
769 // For some reason I don't understand yet the focus doesn't really leave
770 // the listbox like it should, so if we get any events feed them back to
772 void OnKeyDown(wxKeyEvent
& event
) {
773 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
775 void OnChar(wxKeyEvent
& event
) {
776 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
779 // And we need to force the focus back when being destroyed
781 GetGrandParent()->SetFocus();
786 DECLARE_EVENT_TABLE()
789 BEGIN_EVENT_TABLE(wxSTCListBox
, wxListView
)
790 EVT_SET_FOCUS( wxSTCListBox::OnFocus
)
791 EVT_KILL_FOCUS(wxSTCListBox::OnKillFocus
)
793 EVT_KEY_DOWN( wxSTCListBox::OnKeyDown
)
794 EVT_CHAR( wxSTCListBox::OnChar
)
800 #if wxUSE_POPUPWIN //-----------------------------------
801 #include "wx/popupwin.h"
805 // TODO: Refactor these two classes to have a common base (or a mix-in) to get
806 // rid of the code duplication. (Either that or convince somebody to
807 // implement wxPopupWindow for the Mac!!)
809 // In the meantime, be careful to duplicate any changes as needed...
812 // A popup window to place the wxSTCListBox upon
813 class wxSTCListBoxWin
: public wxPopupWindow
817 CallBackAction doubleClickAction
;
818 void* doubleClickActionData
;
820 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point
WXUNUSED(location
)) :
821 wxPopupWindow(parent
, wxBORDER_NONE
)
824 SetBackgroundColour(*wxBLACK
); // for our simple border
826 lv
= new wxSTCListBox(parent
, id
, wxPoint(-50,-50), wxDefaultSize
,
827 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxBORDER_NONE
);
828 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
829 lv
->InsertColumn(0, wxEmptyString
);
830 lv
->InsertColumn(1, wxEmptyString
);
832 // NOTE: We need to fool the wxListView into thinking that it has the
833 // focus so it will use the normal selection colour and will look
834 // "right" to the user. But since the wxPopupWindow or its children
835 // can't receive focus then we have to pull a fast one and temporarily
836 // parent the listctrl on the STC window and then call SetFocus and
837 // then reparent it back to the popup.
846 // Set position in client coords
847 virtual void DoSetSize(int x
, int y
,
848 int width
, int height
,
849 int sizeFlags
= wxSIZE_AUTO
) {
850 if (x
!= wxDefaultCoord
) {
851 GetParent()->ClientToScreen(&x
, NULL
);
853 if (y
!= wxDefaultCoord
) {
854 GetParent()->ClientToScreen(NULL
, &y
);
856 wxPopupWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
859 // return position as if it were in client coords
860 virtual void DoGetPosition( int *x
, int *y
) const {
862 wxPopupWindow::DoGetPosition(&sx
, &sy
);
863 GetParent()->ScreenToClient(&sx
, &sy
);
870 if ( !wxPendingDelete
.Member(this) )
871 wxPendingDelete
.Append(this);
877 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
880 il
->GetSize(0, w
, h
);
887 void SetDoubleClickAction(CallBackAction action
, void *data
) {
888 doubleClickAction
= action
;
889 doubleClickActionData
= data
;
893 void OnFocus(wxFocusEvent
& event
) {
894 GetParent()->SetFocus();
898 void OnSize(wxSizeEvent
& event
) {
900 wxSize sz
= GetSize();
903 lv
->SetSize(1, 1, sz
.x
, sz
.y
);
904 // reset the column widths
905 lv
->SetColumnWidth(0, IconWidth()+4);
906 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
907 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
911 void OnActivate(wxListEvent
& WXUNUSED(event
)) {
912 doubleClickAction(doubleClickActionData
);
915 wxListView
* GetLB() { return lv
; }
918 DECLARE_EVENT_TABLE()
922 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxPopupWindow
)
923 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
924 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
925 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
930 #else // !wxUSE_POPUPWIN -----------------------------------
931 #include "wx/frame.h"
933 // A normal window to place the wxSTCListBox upon, but make it behave as much
934 // like a wxPopupWindow as possible
935 class wxSTCListBoxWin
: public wxFrame
{
938 CallBackAction doubleClickAction
;
939 void* doubleClickActionData
;
941 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point location
) :
942 wxFrame(parent
, id
, wxEmptyString
, wxPoint(location
.x
, location
.y
), wxSize(0,0),
944 | wxFRAME_FLOAT_ON_PARENT
954 lv
= new wxSTCListBox(this, id
, wxDefaultPosition
, wxDefaultSize
,
955 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxNO_BORDER
);
956 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
957 lv
->InsertColumn(0, wxEmptyString
);
958 lv
->InsertColumn(1, wxEmptyString
);
960 // Eventhough we immediately reset the focus to the parent, this helps
961 // things to look right...
968 // On OSX and (possibly others) there can still be pending
969 // messages/events for the list control when Scintilla wants to
970 // close it, so do a pending delete of it instead of destroying
975 // The bottom edge of this window is not getting properly
976 // refreshed upon deletion, so help it out...
977 wxWindow
* p
= GetParent();
978 wxRect
r(GetPosition(), GetSize());
979 r
.SetHeight(r
.GetHeight()+1);
980 p
->Refresh(false, &r
);
982 if ( !wxPendingDelete
.Member(this) )
983 wxPendingDelete
.Append(this);
990 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
993 il
->GetSize(0, w
, h
);
1000 void SetDoubleClickAction(CallBackAction action
, void *data
)
1002 doubleClickAction
= action
;
1003 doubleClickActionData
= data
;
1007 void OnFocus(wxFocusEvent
& event
)
1010 GetParent()->SetFocus();
1014 void OnSize(wxSizeEvent
& event
)
1017 wxSize sz
= GetClientSize();
1019 // reset the column widths
1020 lv
->SetColumnWidth(0, IconWidth()+4);
1021 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
1022 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
1026 void ActivateParent()
1028 // Although we're a frame, we always want the parent to be active, so
1029 // raise it whenever we get shown, focused, etc.
1030 wxTopLevelWindow
*frame
= wxDynamicCast(
1031 wxGetTopLevelParent(GetParent()), wxTopLevelWindow
);
1037 virtual void DoSetSize(int x
, int y
,
1038 int width
, int height
,
1039 int sizeFlags
= wxSIZE_AUTO
)
1041 // convert coords to screen coords since we're a top-level window
1042 if (x
!= wxDefaultCoord
) {
1043 GetParent()->ClientToScreen(&x
, NULL
);
1045 if (y
!= wxDefaultCoord
) {
1046 GetParent()->ClientToScreen(NULL
, &y
);
1048 wxFrame::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1051 virtual bool Show(bool show
= true)
1053 bool rv
= wxFrame::Show(show
);
1057 GetParent()->Refresh(false);
1062 void OnActivate(wxListEvent
& WXUNUSED(event
))
1064 doubleClickAction(doubleClickActionData
);
1067 wxListView
* GetLB() { return lv
; }
1070 DECLARE_EVENT_TABLE()
1074 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxWindow
)
1075 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
1076 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
1077 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
1080 #endif // wxUSE_POPUPWIN -----------------------------------
1083 inline wxSTCListBoxWin
* GETLBW(WindowID win
) {
1084 return ((wxSTCListBoxWin
*)win
);
1087 inline wxListView
* GETLB(WindowID win
) {
1088 return GETLBW(win
)->GetLB();
1091 //----------------------------------------------------------------------
1093 class ListBoxImpl
: public ListBox
{
1097 int desiredVisibleRows
;
1100 Point location
; // Caret location at which the list is opened
1101 wxImageList
* imgList
;
1102 wxArrayInt
* imgTypeMap
;
1108 virtual void SetFont(Font
&font
);
1109 virtual void Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
);
1110 virtual void SetAverageCharWidth(int width
);
1111 virtual void SetVisibleRows(int rows
);
1112 virtual int GetVisibleRows() const;
1113 virtual PRectangle
GetDesiredRect();
1114 virtual int CaretFromEdge();
1115 virtual void Clear();
1116 virtual void Append(char *s
, int type
= -1);
1117 void Append(const wxString
& text
, int type
);
1118 virtual int Length();
1119 virtual void Select(int n
);
1120 virtual int GetSelection();
1121 virtual int Find(const char *prefix
);
1122 virtual void GetValue(int n
, char *value
, int len
);
1123 virtual void RegisterImage(int type
, const char *xpm_data
);
1124 virtual void ClearRegisteredImages();
1125 virtual void SetDoubleClickAction(CallBackAction
, void *);
1126 virtual void SetList(const char* list
, char separator
, char typesep
);
1130 ListBoxImpl::ListBoxImpl()
1131 : lineHeight(10), unicodeMode(false),
1132 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
1133 imgList(NULL
), imgTypeMap(NULL
)
1137 ListBoxImpl::~ListBoxImpl() {
1149 void ListBoxImpl::SetFont(Font
&font
) {
1150 GETLB(id
)->SetFont(*((wxFont
*)font
.GetID()));
1154 void ListBoxImpl::Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
) {
1155 location
= location_
;
1156 lineHeight
= lineHeight_
;
1157 unicodeMode
= unicodeMode_
;
1159 id
= new wxSTCListBoxWin(GETWIN(parent
.GetID()), ctrlID
, location
);
1160 if (imgList
!= NULL
)
1161 GETLB(id
)->SetImageList(imgList
, wxIMAGE_LIST_SMALL
);
1165 void ListBoxImpl::SetAverageCharWidth(int width
) {
1166 aveCharWidth
= width
;
1170 void ListBoxImpl::SetVisibleRows(int rows
) {
1171 desiredVisibleRows
= rows
;
1175 int ListBoxImpl::GetVisibleRows() const {
1176 return desiredVisibleRows
;
1179 PRectangle
ListBoxImpl::GetDesiredRect() {
1180 // wxListCtrl doesn't have a DoGetBestSize, so instead we kept track of
1181 // the max size in Append and calculate it here...
1182 int maxw
= maxStrWidth
* aveCharWidth
;
1185 // give it a default if there are no lines, and/or add a bit more
1186 if (maxw
== 0) maxw
= 100;
1187 maxw
+= aveCharWidth
* 3 +
1188 GETLBW(id
)->IconWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1192 // estimate a desired height
1193 int count
= GETLB(id
)->GetItemCount();
1196 GETLB(id
)->GetItemRect(0, rect
);
1197 maxh
= count
* rect
.GetHeight();
1198 if (maxh
> 140) // TODO: Use desiredVisibleRows??
1201 // Try to make the size an exact multiple of some number of lines
1202 int lines
= maxh
/ rect
.GetHeight();
1203 maxh
= (lines
+ 1) * rect
.GetHeight() + 2;
1217 int ListBoxImpl::CaretFromEdge() {
1218 return 4 + GETLBW(id
)->IconWidth();
1222 void ListBoxImpl::Clear() {
1223 GETLB(id
)->DeleteAllItems();
1227 void ListBoxImpl::Append(char *s
, int type
) {
1228 Append(stc2wx(s
), type
);
1231 void ListBoxImpl::Append(const wxString
& text
, int type
) {
1232 long count
= GETLB(id
)->GetItemCount();
1233 long itemID
= GETLB(id
)->InsertItem(count
, wxEmptyString
);
1235 GETLB(id
)->SetItem(itemID
, 1, text
);
1236 maxStrWidth
= wxMax(maxStrWidth
, text
.length());
1238 wxCHECK_RET(imgTypeMap
, wxT("Unexpected NULL imgTypeMap"));
1239 idx
= imgTypeMap
->Item(type
);
1241 GETLB(id
)->SetItemImage(itemID
, idx
, idx
);
1244 void ListBoxImpl::SetList(const char* list
, char separator
, char typesep
) {
1245 GETLB(id
)->Freeze();
1247 wxStringTokenizer
tkzr(stc2wx(list
), (wxChar
)separator
);
1248 while ( tkzr
.HasMoreTokens() ) {
1249 wxString token
= tkzr
.GetNextToken();
1251 int pos
= token
.Find(typesep
);
1253 token
.Mid(pos
+1).ToLong(&type
);
1254 token
.Truncate(pos
);
1256 Append(token
, (int)type
);
1262 int ListBoxImpl::Length() {
1263 return GETLB(id
)->GetItemCount();
1267 void ListBoxImpl::Select(int n
) {
1273 GETLB(id
)->EnsureVisible(n
);
1274 GETLB(id
)->Select(n
, select
);
1278 int ListBoxImpl::GetSelection() {
1279 return GETLB(id
)->GetFirstSelected();
1283 int ListBoxImpl::Find(const char *WXUNUSED(prefix
)) {
1289 void ListBoxImpl::GetValue(int n
, char *value
, int len
) {
1293 item
.SetMask(wxLIST_MASK_TEXT
);
1294 GETLB(id
)->GetItem(item
);
1295 strncpy(value
, wx2stc(item
.GetText()), len
);
1296 value
[len
-1] = '\0';
1300 void ListBoxImpl::RegisterImage(int type
, const char *xpm_data
) {
1301 wxMemoryInputStream
stream(xpm_data
, strlen(xpm_data
)+1);
1302 wxImage
img(stream
, wxBITMAP_TYPE_XPM
);
1306 // assumes all images are the same size
1307 imgList
= new wxImageList(bmp
.GetWidth(), bmp
.GetHeight(), true);
1308 imgTypeMap
= new wxArrayInt
;
1311 int idx
= imgList
->Add(bmp
);
1313 // do we need to extend the mapping array?
1314 wxArrayInt
& itm
= *imgTypeMap
;
1315 if ( itm
.GetCount() < (size_t)type
+1)
1316 itm
.Add(-1, type
- itm
.GetCount() + 1);
1318 // Add an item that maps type to the image index
1322 void ListBoxImpl::ClearRegisteredImages() {
1332 GETLB(id
)->SetImageList(NULL
, wxIMAGE_LIST_SMALL
);
1336 void ListBoxImpl::SetDoubleClickAction(CallBackAction action
, void *data
) {
1337 GETLBW(id
)->SetDoubleClickAction(action
, data
);
1341 ListBox::ListBox() {
1344 ListBox::~ListBox() {
1347 ListBox
*ListBox::Allocate() {
1348 return new ListBoxImpl();
1351 //----------------------------------------------------------------------
1353 Menu::Menu() : id(0) {
1356 void Menu::CreatePopUp() {
1361 void Menu::Destroy() {
1367 void Menu::Show(Point pt
, Window
&w
) {
1368 GETWIN(w
.GetID())->PopupMenu((wxMenu
*)id
, pt
.x
- 4, pt
.y
);
1372 //----------------------------------------------------------------------
1374 DynamicLibrary
*DynamicLibrary::Load(const char *WXUNUSED(modulePath
)) {
1375 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1379 //----------------------------------------------------------------------
1381 ColourDesired
Platform::Chrome() {
1383 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1384 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1387 ColourDesired
Platform::ChromeHighlight() {
1389 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
);
1390 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1393 const char *Platform::DefaultFont() {
1394 static char buf
[128];
1395 strcpy(buf
, wxNORMAL_FONT
->GetFaceName().mbc_str());
1399 int Platform::DefaultFontSize() {
1400 return wxNORMAL_FONT
->GetPointSize();
1403 unsigned int Platform::DoubleClickTime() {
1404 return 500; // **** ::GetDoubleClickTime();
1407 bool Platform::MouseButtonBounce() {
1411 bool Platform::IsKeyDown(int WXUNUSED(key
)) {
1412 return false; // I don't think we'll need this.
1415 long Platform::SendScintilla(WindowID w
,
1417 unsigned long wParam
,
1420 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1421 return stc
->SendMsg(msg
, wParam
, lParam
);
1424 long Platform::SendScintillaPointer(WindowID w
,
1426 unsigned long wParam
,
1429 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1430 return stc
->SendMsg(msg
, wParam
, (wxIntPtr
)lParam
);
1434 // These are utility functions not really tied to a platform
1436 int Platform::Minimum(int a
, int b
) {
1443 int Platform::Maximum(int a
, int b
) {
1452 void Platform::DebugDisplay(const char *s
) {
1454 wxLogDebug(stc2wx(s
));
1460 void Platform::DebugPrintf(const char *format
, ...) {
1464 va_start(pArguments
, format
);
1465 vsprintf(buffer
,format
,pArguments
);
1467 Platform::DebugDisplay(buffer
);
1469 wxUnusedVar(format
);
1474 static bool assertionPopUps
= true;
1476 bool Platform::ShowAssertionPopUps(bool assertionPopUps_
) {
1477 bool ret
= assertionPopUps
;
1478 assertionPopUps
= assertionPopUps_
;
1482 void Platform::Assert(const char *c
, const char *file
, int line
) {
1485 sprintf(buffer
, "Assertion [%s] failed at %s %d", c
, file
, line
);
1486 if (assertionPopUps
) {
1488 wxMessageBox(stc2wx(buffer
),
1489 wxT("Assertion failure"),
1490 wxICON_HAND
| wxOK
);
1492 strcat(buffer
, "\r\n");
1493 Platform::DebugDisplay(buffer
);
1504 int Platform::Clamp(int val
, int minVal
, int maxVal
) {
1513 bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage
), char WXUNUSED(ch
)) {
1517 int Platform::DBCSCharLength(int WXUNUSED(codePage
), const char *WXUNUSED(s
)) {
1521 int Platform::DBCSCharMaxLength() {
1526 //----------------------------------------------------------------------
1528 ElapsedTime::ElapsedTime() {
1529 wxLongLong localTime
= wxGetLocalTimeMillis();
1530 littleBit
= localTime
.GetLo();
1531 bigBit
= localTime
.GetHi();
1534 double ElapsedTime::Duration(bool reset
) {
1535 wxLongLong
prevTime(bigBit
, littleBit
);
1536 wxLongLong localTime
= wxGetLocalTimeMillis();
1538 littleBit
= localTime
.GetLo();
1539 bigBit
= localTime
.GetHi();
1541 wxLongLong duration
= localTime
- prevTime
;
1542 double result
= duration
.ToDouble();
1548 //----------------------------------------------------------------------
1552 #include "UniConversion.h"
1554 // Convert using Scintilla's functions instead of wx's, Scintilla's are more
1555 // forgiving and won't assert...
1557 wxString
stc2wx(const char* str
, size_t len
)
1560 return wxEmptyString
;
1562 size_t wclen
= UTF16Length(str
, len
);
1563 wxWCharBuffer
buffer(wclen
+1);
1565 size_t actualLen
= UTF16FromUTF8(str
, len
, buffer
.data(), wclen
+1);
1566 return wxString(buffer
.data(), actualLen
);
1571 wxString
stc2wx(const char* str
)
1573 return stc2wx(str
, strlen(str
));
1577 const wxWX2MBbuf
wx2stc(const wxString
& str
)
1579 const wchar_t* wcstr
= str
.c_str();
1580 size_t wclen
= str
.length();
1581 size_t len
= UTF8Length(wcstr
, wclen
);
1583 wxCharBuffer
buffer(len
+1);
1584 UTF8FromUTF16(wcstr
, wclen
, buffer
.data(), len
);
1586 // TODO check NULL termination!!