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"
24 #include "wx/encconv.h"
25 #include "wx/listctrl.h"
26 #include "wx/mstream.h"
28 #include "wx/imaglist.h"
29 #include "wx/tokenzr.h"
31 #ifdef wxHAVE_RAW_BITMAP
32 #include "wx/rawbmp.h"
37 #include "wx/stc/stc.h"
38 #include "wx/stc/private.h"
42 Point
Point::FromLong(long lpoint
) {
43 return Point(lpoint
& 0xFFFF, lpoint
>> 16);
46 wxRect
wxRectFromPRectangle(PRectangle prc
) {
47 wxRect
r(prc
.left
, prc
.top
,
48 prc
.Width(), prc
.Height());
52 PRectangle
PRectangleFromwxRect(wxRect rc
) {
53 return PRectangle(rc
.GetLeft(), rc
.GetTop(),
54 rc
.GetRight()+1, rc
.GetBottom()+1);
57 wxColour
wxColourFromCA(const ColourAllocated
& ca
) {
58 ColourDesired
cd(ca
.AsLong());
59 return wxColour((unsigned char)cd
.GetRed(),
60 (unsigned char)cd
.GetGreen(),
61 (unsigned char)cd
.GetBlue());
64 //----------------------------------------------------------------------
68 allowRealization
= false;
70 entries
= new ColourPair
[size
];
79 void Palette::Release() {
83 entries
= new ColourPair
[size
];
86 // This method either adds a colour to the list of wanted colours (want==true)
87 // or retrieves the allocated colour back to the ColourPair.
88 // This is one method to make it easier to keep the code for wanting and retrieving in sync.
89 void Palette::WantFind(ColourPair
&cp
, bool want
) {
91 for (int i
=0; i
< used
; i
++) {
92 if (entries
[i
].desired
== cp
.desired
)
97 int sizeNew
= size
* 2;
98 ColourPair
*entriesNew
= new ColourPair
[sizeNew
];
99 for (int j
=0; j
<size
; j
++) {
100 entriesNew
[j
] = entries
[j
];
103 entries
= entriesNew
;
107 entries
[used
].desired
= cp
.desired
;
108 entries
[used
].allocated
.Set(cp
.desired
.AsLong());
111 for (int i
=0; i
< used
; i
++) {
112 if (entries
[i
].desired
== cp
.desired
) {
113 cp
.allocated
= entries
[i
].allocated
;
117 cp
.allocated
.Set(cp
.desired
.AsLong());
121 void Palette::Allocate(Window
&) {
122 if (allowRealization
) {
127 //----------------------------------------------------------------------
137 void Font::Create(const char *faceName
, int characterSet
, int size
, bool bold
, bool italic
, bool extraFontFlag
) {
141 // The minus one is done because since Scintilla uses SC_SHARSET_DEFAULT
142 // internally and we need to have wxFONENCODING_DEFAULT == SC_SHARSET_DEFAULT
143 // so we adjust the encoding before passing it to Scintilla. See also
144 // wxStyledTextCtrl::StyleSetCharacterSet
145 wxFontEncoding encoding
= (wxFontEncoding
)(characterSet
-1);
147 wxFontEncodingArray ea
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
151 wxFont
* font
= new wxFont(size
,
153 italic
? wxITALIC
: wxNORMAL
,
154 bold
? wxBOLD
: wxNORMAL
,
158 font
->SetNoAntiAliasing(!extraFontFlag
);
163 void Font::Release() {
169 //----------------------------------------------------------------------
171 class SurfaceImpl
: public Surface
{
184 virtual void Init(WindowID wid
);
185 virtual void Init(SurfaceID sid
, WindowID wid
);
186 virtual void InitPixMap(int width
, int height
, Surface
*surface_
, WindowID wid
);
188 virtual void Release();
189 virtual bool Initialised();
190 virtual void PenColour(ColourAllocated fore
);
191 virtual int LogPixelsY();
192 virtual int DeviceHeightFont(int points
);
193 virtual void MoveTo(int x_
, int y_
);
194 virtual void LineTo(int x_
, int y_
);
195 virtual void Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
);
196 virtual void RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
197 virtual void FillRectangle(PRectangle rc
, ColourAllocated back
);
198 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
);
199 virtual void RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
200 virtual void AlphaRectangle(PRectangle rc
, int cornerSize
, ColourAllocated fill
, int alphaFill
,
201 ColourAllocated outline
, int alphaOutline
, int flags
);
202 virtual void Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
203 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
);
205 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
206 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
207 virtual void DrawTextTransparent(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
);
208 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, int *positions
);
209 virtual int WidthText(Font
&font_
, const char *s
, int len
);
210 virtual int WidthChar(Font
&font_
, char ch
);
211 virtual int Ascent(Font
&font_
);
212 virtual int Descent(Font
&font_
);
213 virtual int InternalLeading(Font
&font_
);
214 virtual int ExternalLeading(Font
&font_
);
215 virtual int Height(Font
&font_
);
216 virtual int AverageCharWidth(Font
&font_
);
218 virtual int SetPalette(Palette
*pal
, bool inBackGround
);
219 virtual void SetClip(PRectangle rc
);
220 virtual void FlushCachedState();
222 virtual void SetUnicodeMode(bool unicodeMode_
);
223 virtual void SetDBCSMode(int codePage
);
225 void BrushColour(ColourAllocated back
);
226 void SetFont(Font
&font_
);
231 SurfaceImpl::SurfaceImpl() :
232 hdc(0), hdcOwned(0), bitmap(0),
233 x(0), y(0), unicodeMode(0)
236 SurfaceImpl::~SurfaceImpl() {
240 void SurfaceImpl::Init(WindowID wid
) {
243 hdc
= new wxMemoryDC();
246 // On Mac and GTK the DC is not really valid until it has a bitmap
247 // selected into it. So instead of just creating the DC with no bitmap,
248 // go ahead and give it one.
249 InitPixMap(1,1,NULL
,wid
);
253 void SurfaceImpl::Init(SurfaceID hdc_
, WindowID
) {
258 void SurfaceImpl::InitPixMap(int width
, int height
, Surface
*WXUNUSED(surface_
), WindowID
) {
260 hdc
= new wxMemoryDC();
262 if (width
< 1) width
= 1;
263 if (height
< 1) height
= 1;
264 bitmap
= new wxBitmap(width
, height
);
265 ((wxMemoryDC
*)hdc
)->SelectObject(*bitmap
);
269 void SurfaceImpl::Release() {
271 ((wxMemoryDC
*)hdc
)->SelectObject(wxNullBitmap
);
283 bool SurfaceImpl::Initialised() {
288 void SurfaceImpl::PenColour(ColourAllocated fore
) {
289 hdc
->SetPen(wxPen(wxColourFromCA(fore
), 1, wxSOLID
));
292 void SurfaceImpl::BrushColour(ColourAllocated back
) {
293 hdc
->SetBrush(wxBrush(wxColourFromCA(back
), wxSOLID
));
296 void SurfaceImpl::SetFont(Font
&font_
) {
298 hdc
->SetFont(*((wxFont
*)font_
.GetID()));
302 int SurfaceImpl::LogPixelsY() {
303 return hdc
->GetPPI().y
;
306 int SurfaceImpl::DeviceHeightFont(int points
) {
310 void SurfaceImpl::MoveTo(int x_
, int y_
) {
315 void SurfaceImpl::LineTo(int x_
, int y_
) {
316 hdc
->DrawLine(x
,y
, x_
,y_
);
321 void SurfaceImpl::Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
) {
324 hdc
->DrawPolygon(npts
, (wxPoint
*)pts
);
327 void SurfaceImpl::RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
330 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
333 void SurfaceImpl::FillRectangle(PRectangle rc
, ColourAllocated back
) {
335 hdc
->SetPen(*wxTRANSPARENT_PEN
);
336 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
339 void SurfaceImpl::FillRectangle(PRectangle rc
, Surface
&surfacePattern
) {
341 if (((SurfaceImpl
&)surfacePattern
).bitmap
)
342 br
= wxBrush(*((SurfaceImpl
&)surfacePattern
).bitmap
);
343 else // Something is wrong so display in red
344 br
= wxBrush(*wxRED
, wxSOLID
);
345 hdc
->SetPen(*wxTRANSPARENT_PEN
);
347 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
350 void SurfaceImpl::RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
353 hdc
->DrawRoundedRectangle(wxRectFromPRectangle(rc
), 4);
357 #define wxPy_premultiply(p, a) ((p) * (a) / 0xff)
359 #define wxPy_premultiply(p, a) (p)
362 void SurfaceImpl::AlphaRectangle(PRectangle rc
, int cornerSize
,
363 ColourAllocated fill
, int alphaFill
,
364 ColourAllocated outline
, int alphaOutline
,
366 #ifdef wxHAVE_RAW_BITMAP
368 // TODO: do something with cornerSize
369 wxUnusedVar(cornerSize
);
372 wxRect r
= wxRectFromPRectangle(rc
);
373 wxBitmap
bmp(r
.width
, r
.height
, 32);
374 wxAlphaPixelData
pixData(bmp
);
376 // Set the fill pixels
377 ColourDesired
cdf(fill
.AsLong());
378 int red
= cdf
.GetRed();
379 int green
= cdf
.GetGreen();
380 int blue
= cdf
.GetBlue();
382 wxAlphaPixelData::Iterator
p(pixData
);
383 for (y
=0; y
<r
.height
; y
++) {
384 p
.MoveTo(pixData
, 0, y
);
385 for (x
=0; x
<r
.width
; x
++) {
386 p
.Red() = wxPy_premultiply(red
, alphaFill
);
387 p
.Green() = wxPy_premultiply(green
, alphaFill
);
388 p
.Blue() = wxPy_premultiply(blue
, alphaFill
);
389 p
.Alpha() = alphaFill
;
394 // Set the outline pixels
395 ColourDesired
cdo(outline
.AsLong());
397 green
= cdo
.GetGreen();
398 blue
= cdo
.GetBlue();
399 for (x
=0; x
<r
.width
; x
++) {
400 p
.MoveTo(pixData
, x
, 0);
401 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
402 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
403 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
404 p
.Alpha() = alphaOutline
;
405 p
.MoveTo(pixData
, x
, r
.height
-1);
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
;
412 for (y
=0; y
<r
.height
; y
++) {
413 p
.MoveTo(pixData
, 0, y
);
414 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
415 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
416 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
417 p
.Alpha() = alphaOutline
;
418 p
.MoveTo(pixData
, r
.width
-1, 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
;
426 hdc
->DrawBitmap(bmp
, r
.x
, r
.y
, true);
429 wxUnusedVar(cornerSize
);
430 wxUnusedVar(alphaFill
);
431 wxUnusedVar(alphaOutline
);
432 RectangleDraw(rc
, outline
, fill
);
436 void SurfaceImpl::Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
439 hdc
->DrawEllipse(wxRectFromPRectangle(rc
));
442 void SurfaceImpl::Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
) {
443 wxRect r
= wxRectFromPRectangle(rc
);
444 hdc
->Blit(r
.x
, r
.y
, r
.width
, r
.height
,
445 ((SurfaceImpl
&)surfaceSource
).hdc
,
446 from
.x
, from
.y
, wxCOPY
);
449 void SurfaceImpl::DrawTextNoClip(PRectangle rc
, Font
&font
, int ybase
,
450 const char *s
, int len
,
451 ColourAllocated fore
, ColourAllocated back
) {
453 hdc
->SetTextForeground(wxColourFromCA(fore
));
454 hdc
->SetTextBackground(wxColourFromCA(back
));
455 FillRectangle(rc
, back
);
457 // ybase is where the baseline should be, but wxWin uses the upper left
458 // corner, so I need to calculate the real position for the text...
459 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
462 void SurfaceImpl::DrawTextClipped(PRectangle rc
, Font
&font
, int ybase
,
463 const char *s
, int len
,
464 ColourAllocated fore
, ColourAllocated back
) {
466 hdc
->SetTextForeground(wxColourFromCA(fore
));
467 hdc
->SetTextBackground(wxColourFromCA(back
));
468 FillRectangle(rc
, back
);
469 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
471 // see comments above
472 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
473 hdc
->DestroyClippingRegion();
477 void SurfaceImpl::DrawTextTransparent(PRectangle rc
, Font
&font
, int ybase
,
478 const char *s
, int len
,
479 ColourAllocated fore
) {
482 hdc
->SetTextForeground(wxColourFromCA(fore
));
483 hdc
->SetBackgroundMode(wxTRANSPARENT
);
485 // ybase is where the baseline should be, but wxWin uses the upper left
486 // corner, so I need to calculate the real position for the text...
487 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
489 hdc
->SetBackgroundMode(wxSOLID
);
493 void SurfaceImpl::MeasureWidths(Font
&font
, const char *s
, int len
, int *positions
) {
495 wxString str
= stc2wx(s
, len
);
500 hdc
->GetPartialTextExtents(str
, tpos
);
503 // Map the widths for UCS-2 characters back to the UTF-8 input string
504 // NOTE: I don't think this is right for when sizeof(wxChar) > 2, ie wxGTK2
505 // so figure it out and fix it!
508 while ((int)i
< len
) {
509 unsigned char uch
= (unsigned char)s
[i
];
510 positions
[i
++] = tpos
[ui
];
512 if (uch
< (0x80 + 0x40 + 0x20)) {
513 positions
[i
++] = tpos
[ui
];
515 positions
[i
++] = tpos
[ui
];
516 positions
[i
++] = tpos
[ui
];
523 // If not unicode then just use the widths we have
525 std::copy(tpos
.begin(), tpos
.end(), positions
);
527 memcpy(positions
, tpos
.begin(), len
* sizeof(int));
533 int SurfaceImpl::WidthText(Font
&font
, const char *s
, int len
) {
538 hdc
->GetTextExtent(stc2wx(s
, len
), &w
, &h
);
543 int SurfaceImpl::WidthChar(Font
&font
, char ch
) {
547 char s
[2] = { ch
, 0 };
549 hdc
->GetTextExtent(stc2wx(s
, 1), &w
, &h
);
553 #define EXTENT_TEST wxT(" `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
555 int SurfaceImpl::Ascent(Font
&font
) {
558 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
563 int SurfaceImpl::Descent(Font
&font
) {
566 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
570 int SurfaceImpl::InternalLeading(Font
&WXUNUSED(font
)) {
574 int SurfaceImpl::ExternalLeading(Font
&font
) {
577 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
581 int SurfaceImpl::Height(Font
&font
) {
583 return hdc
->GetCharHeight() + 1;
586 int SurfaceImpl::AverageCharWidth(Font
&font
) {
588 return hdc
->GetCharWidth();
591 int SurfaceImpl::SetPalette(Palette
*WXUNUSED(pal
), bool WXUNUSED(inBackGround
)) {
595 void SurfaceImpl::SetClip(PRectangle rc
) {
596 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
599 void SurfaceImpl::FlushCachedState() {
602 void SurfaceImpl::SetUnicodeMode(bool unicodeMode_
) {
603 unicodeMode
=unicodeMode_
;
606 void SurfaceImpl::SetDBCSMode(int WXUNUSED(codePage
)) {
607 // dbcsMode = codePage == SC_CP_DBCS;
611 Surface
*Surface::Allocate() {
612 return new SurfaceImpl
;
616 //----------------------------------------------------------------------
619 inline wxWindow
* GETWIN(WindowID id
) { return (wxWindow
*)id
; }
624 void Window::Destroy() {
627 GETWIN(id
)->Destroy();
632 bool Window::HasFocus() {
633 return wxWindow::FindFocus() == GETWIN(id
);
636 PRectangle
Window::GetPosition() {
637 if (! id
) return PRectangle();
638 wxRect
rc(GETWIN(id
)->GetPosition(), GETWIN(id
)->GetSize());
639 return PRectangleFromwxRect(rc
);
642 void Window::SetPosition(PRectangle rc
) {
643 wxRect r
= wxRectFromPRectangle(rc
);
644 GETWIN(id
)->SetSize(r
);
647 void Window::SetPositionRelative(PRectangle rc
, Window
) {
648 SetPosition(rc
); // ????
651 PRectangle
Window::GetClientPosition() {
652 if (! id
) return PRectangle();
653 wxSize sz
= GETWIN(id
)->GetClientSize();
654 return PRectangle(0, 0, sz
.x
, sz
.y
);
657 void Window::Show(bool show
) {
658 GETWIN(id
)->Show(show
);
661 void Window::InvalidateAll() {
662 GETWIN(id
)->Refresh(false);
665 void Window::InvalidateRectangle(PRectangle rc
) {
666 wxRect r
= wxRectFromPRectangle(rc
);
667 GETWIN(id
)->Refresh(false, &r
);
670 void Window::SetFont(Font
&font
) {
671 GETWIN(id
)->SetFont(*((wxFont
*)font
.GetID()));
674 void Window::SetCursor(Cursor curs
) {
679 cursorId
= wxCURSOR_IBEAM
;
682 cursorId
= wxCURSOR_ARROW
;
685 cursorId
= wxCURSOR_ARROW
; // ** no up arrow... wxCURSOR_UPARROW;
688 cursorId
= wxCURSOR_WAIT
;
691 cursorId
= wxCURSOR_SIZEWE
;
694 cursorId
= wxCURSOR_SIZENS
;
696 case cursorReverseArrow
:
697 cursorId
= wxCURSOR_RIGHT_ARROW
;
700 cursorId
= wxCURSOR_HAND
;
703 cursorId
= wxCURSOR_ARROW
;
707 wxCursor wc
= wxStockCursor(cursorId
) ;
709 wxCursor wc
= wxCursor(cursorId
) ;
711 if(curs
!= cursorLast
)
713 GETWIN(id
)->SetCursor(wc
);
719 void Window::SetTitle(const char *s
) {
720 GETWIN(id
)->SetLabel(stc2wx(s
));
724 //----------------------------------------------------------------------
725 // Helper classes for ListBox
728 // This is a simple subclass of wxListView that just resets focus to the
729 // parent when it gets it.
730 class wxSTCListBox
: public wxListView
{
732 wxSTCListBox(wxWindow
* parent
, wxWindowID id
,
733 const wxPoint
& pos
, const wxSize
& size
,
738 Hide(); // don't flicker as we move it around...
740 Create(parent
, id
, pos
, size
, style
);
744 void OnFocus(wxFocusEvent
& event
) {
745 GetParent()->SetFocus();
749 void OnKillFocus(wxFocusEvent
& WXUNUSED(event
)) {
750 // Do nothing. Prevents base class from resetting the colors...
754 // For some reason I don't understand yet the focus doesn't really leave
755 // the listbox like it should, so if we get any events feed them back to
757 void OnKeyDown(wxKeyEvent
& event
) {
758 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
760 void OnChar(wxKeyEvent
& event
) {
761 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
764 // And we need to force the focus back when being destroyed
766 GetGrandParent()->SetFocus();
771 DECLARE_EVENT_TABLE()
774 BEGIN_EVENT_TABLE(wxSTCListBox
, wxListView
)
775 EVT_SET_FOCUS( wxSTCListBox::OnFocus
)
776 EVT_KILL_FOCUS(wxSTCListBox::OnKillFocus
)
778 EVT_KEY_DOWN( wxSTCListBox::OnKeyDown
)
779 EVT_CHAR( wxSTCListBox::OnChar
)
785 #if wxUSE_POPUPWIN //-----------------------------------
786 #include "wx/popupwin.h"
790 // TODO: Refactor these two classes to have a common base (or a mix-in) to get
791 // rid of the code duplication. (Either that or convince somebody to
792 // implement wxPopupWindow for the Mac!!)
794 // In the meantime, be careful to duplicate any changes as needed...
797 // A popup window to place the wxSTCListBox upon
798 class wxSTCListBoxWin
: public wxPopupWindow
802 CallBackAction doubleClickAction
;
803 void* doubleClickActionData
;
805 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point
WXUNUSED(location
)) :
806 wxPopupWindow(parent
, wxBORDER_NONE
)
809 SetBackgroundColour(*wxBLACK
); // for our simple border
811 lv
= new wxSTCListBox(parent
, id
, wxPoint(-50,-50), wxDefaultSize
,
812 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxBORDER_NONE
);
813 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
814 lv
->InsertColumn(0, wxEmptyString
);
815 lv
->InsertColumn(1, wxEmptyString
);
817 // NOTE: We need to fool the wxListView into thinking that it has the
818 // focus so it will use the normal selection colour and will look
819 // "right" to the user. But since the wxPopupWindow or its children
820 // can't receive focus then we have to pull a fast one and temporarily
821 // parent the listctrl on the STC window and then call SetFocus and
822 // then reparent it back to the popup.
831 // Set position in client coords
832 virtual void DoSetSize(int x
, int y
,
833 int width
, int height
,
834 int sizeFlags
= wxSIZE_AUTO
) {
835 if (x
!= wxDefaultCoord
) {
836 GetParent()->ClientToScreen(&x
, NULL
);
838 if (y
!= wxDefaultCoord
) {
839 GetParent()->ClientToScreen(NULL
, &y
);
841 wxPopupWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
844 // return position as if it were in client coords
845 virtual void DoGetPosition( int *x
, int *y
) const {
847 wxPopupWindow::DoGetPosition(&sx
, &sy
);
848 GetParent()->ScreenToClient(&sx
, &sy
);
855 if ( !wxPendingDelete
.Member(this) )
856 wxPendingDelete
.Append(this);
862 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
865 il
->GetSize(0, w
, h
);
872 void SetDoubleClickAction(CallBackAction action
, void *data
) {
873 doubleClickAction
= action
;
874 doubleClickActionData
= data
;
878 void OnFocus(wxFocusEvent
& event
) {
879 GetParent()->SetFocus();
883 void OnSize(wxSizeEvent
& event
) {
885 wxSize sz
= GetSize();
888 lv
->SetSize(1, 1, sz
.x
, sz
.y
);
889 // reset the column widths
890 lv
->SetColumnWidth(0, IconWidth()+4);
891 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
892 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
896 void OnActivate(wxListEvent
& WXUNUSED(event
)) {
897 doubleClickAction(doubleClickActionData
);
900 wxListView
* GetLB() { return lv
; }
903 DECLARE_EVENT_TABLE()
907 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxPopupWindow
)
908 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
909 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
910 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
915 #else // !wxUSE_POPUPWIN -----------------------------------
916 #include "wx/frame.h"
918 // A normal window to place the wxSTCListBox upon, but make it behave as much
919 // like a wxPopupWindow as possible
920 class wxSTCListBoxWin
: public wxFrame
{
923 CallBackAction doubleClickAction
;
924 void* doubleClickActionData
;
926 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point location
) :
927 wxFrame(parent
, id
, wxEmptyString
, wxPoint(location
.x
, location
.y
), wxSize(0,0),
929 | wxFRAME_FLOAT_ON_PARENT
939 lv
= new wxSTCListBox(this, id
, wxDefaultPosition
, wxDefaultSize
,
940 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxNO_BORDER
);
941 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
942 lv
->InsertColumn(0, wxEmptyString
);
943 lv
->InsertColumn(1, wxEmptyString
);
945 // Eventhough we immediately reset the focus to the parent, this helps
946 // things to look right...
953 // On OSX and (possibly others) there can still be pending
954 // messages/events for the list control when Scintilla wants to
955 // close it, so do a pending delete of it instead of destroying
960 // The bottom edge of this window is not getting properly
961 // refreshed upon deletion, so help it out...
962 wxWindow
* p
= GetParent();
963 wxRect
r(GetPosition(), GetSize());
964 r
.SetHeight(r
.GetHeight()+1);
965 p
->Refresh(false, &r
);
967 if ( !wxPendingDelete
.Member(this) )
968 wxPendingDelete
.Append(this);
975 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
978 il
->GetSize(0, w
, h
);
985 void SetDoubleClickAction(CallBackAction action
, void *data
)
987 doubleClickAction
= action
;
988 doubleClickActionData
= data
;
992 void OnFocus(wxFocusEvent
& event
)
995 GetParent()->SetFocus();
999 void OnSize(wxSizeEvent
& event
)
1002 wxSize sz
= GetClientSize();
1004 // reset the column widths
1005 lv
->SetColumnWidth(0, IconWidth()+4);
1006 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
1007 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
1011 void ActivateParent()
1013 // Although we're a frame, we always want the parent to be active, so
1014 // raise it whenever we get shown, focused, etc.
1015 wxTopLevelWindow
*frame
= wxDynamicCast(
1016 wxGetTopLevelParent(GetParent()), wxTopLevelWindow
);
1022 virtual void DoSetSize(int x
, int y
,
1023 int width
, int height
,
1024 int sizeFlags
= wxSIZE_AUTO
)
1026 // convert coords to screen coords since we're a top-level window
1027 if (x
!= wxDefaultCoord
) {
1028 GetParent()->ClientToScreen(&x
, NULL
);
1030 if (y
!= wxDefaultCoord
) {
1031 GetParent()->ClientToScreen(NULL
, &y
);
1033 wxFrame::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1036 virtual bool Show(bool show
= true)
1038 bool rv
= wxFrame::Show(show
);
1042 GetParent()->Refresh(false);
1047 void OnActivate(wxListEvent
& WXUNUSED(event
))
1049 doubleClickAction(doubleClickActionData
);
1052 wxListView
* GetLB() { return lv
; }
1055 DECLARE_EVENT_TABLE()
1059 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxWindow
)
1060 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
1061 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
1062 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
1065 #endif // wxUSE_POPUPWIN -----------------------------------
1068 inline wxSTCListBoxWin
* GETLBW(WindowID win
) {
1069 return ((wxSTCListBoxWin
*)win
);
1072 inline wxListView
* GETLB(WindowID win
) {
1073 return GETLBW(win
)->GetLB();
1076 //----------------------------------------------------------------------
1078 class ListBoxImpl
: public ListBox
{
1082 int desiredVisibleRows
;
1085 Point location
; // Caret location at which the list is opened
1086 wxImageList
* imgList
;
1087 wxArrayInt
* imgTypeMap
;
1093 virtual void SetFont(Font
&font
);
1094 virtual void Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
);
1095 virtual void SetAverageCharWidth(int width
);
1096 virtual void SetVisibleRows(int rows
);
1097 virtual int GetVisibleRows() const;
1098 virtual PRectangle
GetDesiredRect();
1099 virtual int CaretFromEdge();
1100 virtual void Clear();
1101 virtual void Append(char *s
, int type
= -1);
1102 void Append(const wxString
& text
, int type
);
1103 virtual int Length();
1104 virtual void Select(int n
);
1105 virtual int GetSelection();
1106 virtual int Find(const char *prefix
);
1107 virtual void GetValue(int n
, char *value
, int len
);
1108 virtual void RegisterImage(int type
, const char *xpm_data
);
1109 virtual void ClearRegisteredImages();
1110 virtual void SetDoubleClickAction(CallBackAction
, void *);
1111 virtual void SetList(const char* list
, char separator
, char typesep
);
1115 ListBoxImpl::ListBoxImpl()
1116 : lineHeight(10), unicodeMode(false),
1117 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
1118 imgList(NULL
), imgTypeMap(NULL
)
1122 ListBoxImpl::~ListBoxImpl() {
1134 void ListBoxImpl::SetFont(Font
&font
) {
1135 GETLB(id
)->SetFont(*((wxFont
*)font
.GetID()));
1139 void ListBoxImpl::Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
) {
1140 location
= location_
;
1141 lineHeight
= lineHeight_
;
1142 unicodeMode
= unicodeMode_
;
1144 id
= new wxSTCListBoxWin(GETWIN(parent
.GetID()), ctrlID
, location
);
1145 if (imgList
!= NULL
)
1146 GETLB(id
)->SetImageList(imgList
, wxIMAGE_LIST_SMALL
);
1150 void ListBoxImpl::SetAverageCharWidth(int width
) {
1151 aveCharWidth
= width
;
1155 void ListBoxImpl::SetVisibleRows(int rows
) {
1156 desiredVisibleRows
= rows
;
1160 int ListBoxImpl::GetVisibleRows() const {
1161 return desiredVisibleRows
;
1164 PRectangle
ListBoxImpl::GetDesiredRect() {
1165 // wxListCtrl doesn't have a DoGetBestSize, so instead we kept track of
1166 // the max size in Append and calculate it here...
1167 int maxw
= maxStrWidth
* aveCharWidth
;
1170 // give it a default if there are no lines, and/or add a bit more
1171 if (maxw
== 0) maxw
= 100;
1172 maxw
+= aveCharWidth
* 3 +
1173 GETLBW(id
)->IconWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1177 // estimate a desired height
1178 int count
= GETLB(id
)->GetItemCount();
1181 GETLB(id
)->GetItemRect(0, rect
);
1182 maxh
= count
* rect
.GetHeight();
1183 if (maxh
> 140) // TODO: Use desiredVisibleRows??
1186 // Try to make the size an exact multiple of some number of lines
1187 int lines
= maxh
/ rect
.GetHeight();
1188 maxh
= (lines
+ 1) * rect
.GetHeight() + 2;
1202 int ListBoxImpl::CaretFromEdge() {
1203 return 4 + GETLBW(id
)->IconWidth();
1207 void ListBoxImpl::Clear() {
1208 GETLB(id
)->DeleteAllItems();
1212 void ListBoxImpl::Append(char *s
, int type
) {
1213 Append(stc2wx(s
), type
);
1216 void ListBoxImpl::Append(const wxString
& text
, int type
) {
1217 long count
= GETLB(id
)->GetItemCount();
1218 long itemID
= GETLB(id
)->InsertItem(count
, wxEmptyString
);
1219 GETLB(id
)->SetItem(itemID
, 1, text
);
1220 maxStrWidth
= wxMax(maxStrWidth
, text
.length());
1222 wxCHECK_RET(imgTypeMap
, wxT("Unexpected NULL imgTypeMap"));
1223 long idx
= imgTypeMap
->Item(type
);
1224 GETLB(id
)->SetItemImage(itemID
, idx
, idx
);
1228 void ListBoxImpl::SetList(const char* list
, char separator
, char typesep
) {
1229 GETLB(id
)->Freeze();
1231 wxStringTokenizer
tkzr(stc2wx(list
), (wxChar
)separator
);
1232 while ( tkzr
.HasMoreTokens() ) {
1233 wxString token
= tkzr
.GetNextToken();
1235 int pos
= token
.Find(typesep
);
1237 token
.Mid(pos
+1).ToLong(&type
);
1238 token
.Truncate(pos
);
1240 Append(token
, (int)type
);
1246 int ListBoxImpl::Length() {
1247 return GETLB(id
)->GetItemCount();
1251 void ListBoxImpl::Select(int n
) {
1257 GETLB(id
)->EnsureVisible(n
);
1258 GETLB(id
)->Select(n
, select
);
1262 int ListBoxImpl::GetSelection() {
1263 return GETLB(id
)->GetFirstSelected();
1267 int ListBoxImpl::Find(const char *WXUNUSED(prefix
)) {
1273 void ListBoxImpl::GetValue(int n
, char *value
, int len
) {
1277 item
.SetMask(wxLIST_MASK_TEXT
);
1278 GETLB(id
)->GetItem(item
);
1279 strncpy(value
, wx2stc(item
.GetText()), len
);
1280 value
[len
-1] = '\0';
1284 void ListBoxImpl::RegisterImage(int type
, const char *xpm_data
) {
1285 wxMemoryInputStream
stream(xpm_data
, strlen(xpm_data
)+1);
1286 wxImage
img(stream
, wxBITMAP_TYPE_XPM
);
1290 // assumes all images are the same size
1291 imgList
= new wxImageList(bmp
.GetWidth(), bmp
.GetHeight(), true);
1292 imgTypeMap
= new wxArrayInt
;
1295 int idx
= imgList
->Add(bmp
);
1297 // do we need to extend the mapping array?
1298 wxArrayInt
& itm
= *imgTypeMap
;
1299 if ( itm
.GetCount() < (size_t)type
+1)
1300 itm
.Add(-1, type
- itm
.GetCount() + 1);
1302 // Add an item that maps type to the image index
1306 void ListBoxImpl::ClearRegisteredImages() {
1316 GETLB(id
)->SetImageList(NULL
, wxIMAGE_LIST_SMALL
);
1320 void ListBoxImpl::SetDoubleClickAction(CallBackAction action
, void *data
) {
1321 GETLBW(id
)->SetDoubleClickAction(action
, data
);
1325 ListBox::ListBox() {
1328 ListBox::~ListBox() {
1331 ListBox
*ListBox::Allocate() {
1332 return new ListBoxImpl();
1335 //----------------------------------------------------------------------
1337 Menu::Menu() : id(0) {
1340 void Menu::CreatePopUp() {
1345 void Menu::Destroy() {
1351 void Menu::Show(Point pt
, Window
&w
) {
1352 GETWIN(w
.GetID())->PopupMenu((wxMenu
*)id
, pt
.x
- 4, pt
.y
);
1356 //----------------------------------------------------------------------
1358 DynamicLibrary
*DynamicLibrary::Load(const char *WXUNUSED(modulePath
)) {
1359 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1363 //----------------------------------------------------------------------
1365 ColourDesired
Platform::Chrome() {
1367 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1368 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1371 ColourDesired
Platform::ChromeHighlight() {
1373 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
);
1374 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1377 const char *Platform::DefaultFont() {
1378 static char buf
[128];
1379 strcpy(buf
, wxNORMAL_FONT
->GetFaceName().mbc_str());
1383 int Platform::DefaultFontSize() {
1384 return wxNORMAL_FONT
->GetPointSize();
1387 unsigned int Platform::DoubleClickTime() {
1388 return 500; // **** ::GetDoubleClickTime();
1391 bool Platform::MouseButtonBounce() {
1395 bool Platform::IsKeyDown(int WXUNUSED(key
)) {
1396 return false; // I don't think we'll need this.
1399 long Platform::SendScintilla(WindowID w
,
1401 unsigned long wParam
,
1404 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1405 return stc
->SendMsg(msg
, wParam
, lParam
);
1408 long Platform::SendScintillaPointer(WindowID w
,
1410 unsigned long wParam
,
1413 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1414 return stc
->SendMsg(msg
, wParam
, (long)lParam
);
1418 // These are utility functions not really tied to a platform
1420 int Platform::Minimum(int a
, int b
) {
1427 int Platform::Maximum(int a
, int b
) {
1436 void Platform::DebugDisplay(const char *s
) {
1438 wxLogDebug(stc2wx(s
));
1444 void Platform::DebugPrintf(const char *format
, ...) {
1448 va_start(pArguments
, format
);
1449 vsprintf(buffer
,format
,pArguments
);
1451 Platform::DebugDisplay(buffer
);
1453 wxUnusedVar(format
);
1458 static bool assertionPopUps
= true;
1460 bool Platform::ShowAssertionPopUps(bool assertionPopUps_
) {
1461 bool ret
= assertionPopUps
;
1462 assertionPopUps
= assertionPopUps_
;
1466 void Platform::Assert(const char *c
, const char *file
, int line
) {
1469 sprintf(buffer
, "Assertion [%s] failed at %s %d", c
, file
, line
);
1470 if (assertionPopUps
) {
1472 wxMessageBox(stc2wx(buffer
),
1473 wxT("Assertion failure"),
1474 wxICON_HAND
| wxOK
);
1476 strcat(buffer
, "\r\n");
1477 Platform::DebugDisplay(buffer
);
1488 int Platform::Clamp(int val
, int minVal
, int maxVal
) {
1497 bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage
), char WXUNUSED(ch
)) {
1501 int Platform::DBCSCharLength(int WXUNUSED(codePage
), const char *WXUNUSED(s
)) {
1505 int Platform::DBCSCharMaxLength() {
1510 //----------------------------------------------------------------------
1512 ElapsedTime::ElapsedTime() {
1513 wxLongLong localTime
= wxGetLocalTimeMillis();
1514 littleBit
= localTime
.GetLo();
1515 bigBit
= localTime
.GetHi();
1518 double ElapsedTime::Duration(bool reset
) {
1519 wxLongLong
prevTime(bigBit
, littleBit
);
1520 wxLongLong localTime
= wxGetLocalTimeMillis();
1522 littleBit
= localTime
.GetLo();
1523 bigBit
= localTime
.GetHi();
1525 wxLongLong duration
= localTime
- prevTime
;
1526 double result
= duration
.ToDouble();
1532 //----------------------------------------------------------------------
1536 #include "UniConversion.h"
1538 // Convert using Scintilla's functions instead of wx's, Scintilla's are more
1539 // forgiving and won't assert...
1541 wxString
stc2wx(const char* str
, size_t len
)
1544 return wxEmptyString
;
1546 size_t wclen
= UCS2Length(str
, len
);
1547 wxWCharBuffer
buffer(wclen
+1);
1549 size_t actualLen
= UCS2FromUTF8(str
, len
, buffer
.data(), wclen
+1);
1550 return wxString(buffer
.data(), actualLen
);
1555 wxString
stc2wx(const char* str
)
1557 return stc2wx(str
, strlen(str
));
1561 const wxWX2MBbuf
wx2stc(const wxString
& str
)
1563 const wchar_t* wcstr
= str
.c_str();
1564 size_t wclen
= str
.length();
1565 size_t len
= UTF8Length(wcstr
, wclen
);
1567 wxCharBuffer
buffer(len
+1);
1568 UTF8FromUCS2(wcstr
, wclen
, buffer
.data(), len
);
1570 // TODO check NULL termination!!