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"
41 Point
Point::FromLong(long lpoint
) {
42 return Point(lpoint
& 0xFFFF, lpoint
>> 16);
45 wxRect
wxRectFromPRectangle(PRectangle prc
) {
46 wxRect
r(prc
.left
, prc
.top
,
47 prc
.Width(), prc
.Height());
51 PRectangle
PRectangleFromwxRect(wxRect rc
) {
52 return PRectangle(rc
.GetLeft(), rc
.GetTop(),
53 rc
.GetRight()+1, rc
.GetBottom()+1);
56 wxColour
wxColourFromCA(const ColourAllocated
& ca
) {
57 ColourDesired
cd(ca
.AsLong());
58 return wxColour((unsigned char)cd
.GetRed(),
59 (unsigned char)cd
.GetGreen(),
60 (unsigned char)cd
.GetBlue());
63 //----------------------------------------------------------------------
67 allowRealization
= false;
69 entries
= new ColourPair
[size
];
78 void Palette::Release() {
82 entries
= new ColourPair
[size
];
85 // This method either adds a colour to the list of wanted colours (want==true)
86 // or retrieves the allocated colour back to the ColourPair.
87 // This is one method to make it easier to keep the code for wanting and retrieving in sync.
88 void Palette::WantFind(ColourPair
&cp
, bool want
) {
90 for (int i
=0; i
< used
; i
++) {
91 if (entries
[i
].desired
== cp
.desired
)
96 int sizeNew
= size
* 2;
97 ColourPair
*entriesNew
= new ColourPair
[sizeNew
];
98 for (int j
=0; j
<size
; j
++) {
99 entriesNew
[j
] = entries
[j
];
102 entries
= entriesNew
;
106 entries
[used
].desired
= cp
.desired
;
107 entries
[used
].allocated
.Set(cp
.desired
.AsLong());
110 for (int i
=0; i
< used
; i
++) {
111 if (entries
[i
].desired
== cp
.desired
) {
112 cp
.allocated
= entries
[i
].allocated
;
116 cp
.allocated
.Set(cp
.desired
.AsLong());
120 void Palette::Allocate(Window
&) {
121 if (allowRealization
) {
126 //----------------------------------------------------------------------
136 void Font::Create(const char *faceName
, int characterSet
, int size
, bool bold
, bool italic
, bool extraFontFlag
) {
140 // The minus one is done because since Scintilla uses SC_SHARSET_DEFAULT
141 // internally and we need to have wxFONENCODING_DEFAULT == SC_SHARSET_DEFAULT
142 // so we adjust the encoding before passing it to Scintilla. See also
143 // wxStyledTextCtrl::StyleSetCharacterSet
144 wxFontEncoding encoding
= (wxFontEncoding
)(characterSet
-1);
146 wxFontEncodingArray ea
= wxEncodingConverter::GetPlatformEquivalents(encoding
);
150 wxFont
* font
= new wxFont(size
,
152 italic
? wxITALIC
: wxNORMAL
,
153 bold
? wxBOLD
: wxNORMAL
,
157 font
->SetNoAntiAliasing(!extraFontFlag
);
162 void Font::Release() {
168 //----------------------------------------------------------------------
170 class SurfaceImpl
: public Surface
{
183 virtual void Init(WindowID wid
);
184 virtual void Init(SurfaceID sid
, WindowID wid
);
185 virtual void InitPixMap(int width
, int height
, Surface
*surface_
, WindowID wid
);
187 virtual void Release();
188 virtual bool Initialised();
189 virtual void PenColour(ColourAllocated fore
);
190 virtual int LogPixelsY();
191 virtual int DeviceHeightFont(int points
);
192 virtual void MoveTo(int x_
, int y_
);
193 virtual void LineTo(int x_
, int y_
);
194 virtual void Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
);
195 virtual void RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
196 virtual void FillRectangle(PRectangle rc
, ColourAllocated back
);
197 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
);
198 virtual void RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
199 virtual void AlphaRectangle(PRectangle rc
, int cornerSize
, ColourAllocated fill
, int alphaFill
,
200 ColourAllocated outline
, int alphaOutline
, int flags
);
201 virtual void Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
);
202 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
);
204 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
205 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
);
206 virtual void DrawTextTransparent(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
);
207 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, int *positions
);
208 virtual int WidthText(Font
&font_
, const char *s
, int len
);
209 virtual int WidthChar(Font
&font_
, char ch
);
210 virtual int Ascent(Font
&font_
);
211 virtual int Descent(Font
&font_
);
212 virtual int InternalLeading(Font
&font_
);
213 virtual int ExternalLeading(Font
&font_
);
214 virtual int Height(Font
&font_
);
215 virtual int AverageCharWidth(Font
&font_
);
217 virtual int SetPalette(Palette
*pal
, bool inBackGround
);
218 virtual void SetClip(PRectangle rc
);
219 virtual void FlushCachedState();
221 virtual void SetUnicodeMode(bool unicodeMode_
);
222 virtual void SetDBCSMode(int codePage
);
224 void BrushColour(ColourAllocated back
);
225 void SetFont(Font
&font_
);
230 SurfaceImpl::SurfaceImpl() :
231 hdc(0), hdcOwned(0), bitmap(0),
232 x(0), y(0), unicodeMode(0)
235 SurfaceImpl::~SurfaceImpl() {
239 void SurfaceImpl::Init(WindowID wid
) {
242 hdc
= new wxMemoryDC();
245 // On Mac and GTK the DC is not really valid until it has a bitmap
246 // selected into it. So instead of just creating the DC with no bitmap,
247 // go ahead and give it one.
248 InitPixMap(1,1,NULL
,wid
);
252 void SurfaceImpl::Init(SurfaceID hdc_
, WindowID
) {
257 void SurfaceImpl::InitPixMap(int width
, int height
, Surface
*WXUNUSED(surface_
), WindowID
) {
259 hdc
= new wxMemoryDC();
261 if (width
< 1) width
= 1;
262 if (height
< 1) height
= 1;
263 bitmap
= new wxBitmap(width
, height
);
264 ((wxMemoryDC
*)hdc
)->SelectObject(*bitmap
);
268 void SurfaceImpl::Release() {
270 ((wxMemoryDC
*)hdc
)->SelectObject(wxNullBitmap
);
282 bool SurfaceImpl::Initialised() {
287 void SurfaceImpl::PenColour(ColourAllocated fore
) {
288 hdc
->SetPen(wxPen(wxColourFromCA(fore
), 1, wxSOLID
));
291 void SurfaceImpl::BrushColour(ColourAllocated back
) {
292 hdc
->SetBrush(wxBrush(wxColourFromCA(back
), wxSOLID
));
295 void SurfaceImpl::SetFont(Font
&font_
) {
297 hdc
->SetFont(*((wxFont
*)font_
.GetID()));
301 int SurfaceImpl::LogPixelsY() {
302 return hdc
->GetPPI().y
;
305 int SurfaceImpl::DeviceHeightFont(int points
) {
309 void SurfaceImpl::MoveTo(int x_
, int y_
) {
314 void SurfaceImpl::LineTo(int x_
, int y_
) {
315 hdc
->DrawLine(x
,y
, x_
,y_
);
320 void SurfaceImpl::Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
) {
323 hdc
->DrawPolygon(npts
, (wxPoint
*)pts
);
326 void SurfaceImpl::RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
329 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
332 void SurfaceImpl::FillRectangle(PRectangle rc
, ColourAllocated back
) {
334 hdc
->SetPen(*wxTRANSPARENT_PEN
);
335 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
338 void SurfaceImpl::FillRectangle(PRectangle rc
, Surface
&surfacePattern
) {
340 if (((SurfaceImpl
&)surfacePattern
).bitmap
)
341 br
= wxBrush(*((SurfaceImpl
&)surfacePattern
).bitmap
);
342 else // Something is wrong so display in red
343 br
= wxBrush(*wxRED
, wxSOLID
);
344 hdc
->SetPen(*wxTRANSPARENT_PEN
);
346 hdc
->DrawRectangle(wxRectFromPRectangle(rc
));
349 void SurfaceImpl::RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
352 hdc
->DrawRoundedRectangle(wxRectFromPRectangle(rc
), 4);
356 #define wxPy_premultiply(p, a) ((p) * (a) / 0xff)
358 #define wxPy_premultiply(p, a) (p)
361 void SurfaceImpl::AlphaRectangle(PRectangle rc
, int cornerSize
,
362 ColourAllocated fill
, int alphaFill
,
363 ColourAllocated outline
, int alphaOutline
,
365 #ifdef wxHAVE_RAW_BITMAP
367 // TODO: do something with cornerSize
368 wxUnusedVar(cornerSize
);
371 wxRect r
= wxRectFromPRectangle(rc
);
372 wxBitmap
bmp(r
.width
, r
.height
, 32);
373 wxAlphaPixelData
pixData(bmp
);
375 // Set the fill pixels
376 ColourDesired
cdf(fill
.AsLong());
377 int red
= cdf
.GetRed();
378 int green
= cdf
.GetGreen();
379 int blue
= cdf
.GetBlue();
381 wxAlphaPixelData::Iterator
p(pixData
);
382 for (y
=0; y
<r
.height
; y
++) {
383 p
.MoveTo(pixData
, 0, y
);
384 for (x
=0; x
<r
.width
; x
++) {
385 p
.Red() = wxPy_premultiply(red
, alphaFill
);
386 p
.Green() = wxPy_premultiply(green
, alphaFill
);
387 p
.Blue() = wxPy_premultiply(blue
, alphaFill
);
388 p
.Alpha() = alphaFill
;
393 // Set the outline pixels
394 ColourDesired
cdo(outline
.AsLong());
396 green
= cdo
.GetGreen();
397 blue
= cdo
.GetBlue();
398 for (x
=0; x
<r
.width
; x
++) {
399 p
.MoveTo(pixData
, x
, 0);
400 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
401 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
402 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
403 p
.Alpha() = alphaOutline
;
404 p
.MoveTo(pixData
, x
, r
.height
-1);
405 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
406 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
407 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
408 p
.Alpha() = alphaOutline
;
411 for (y
=0; y
<r
.height
; y
++) {
412 p
.MoveTo(pixData
, 0, y
);
413 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
414 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
415 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
416 p
.Alpha() = alphaOutline
;
417 p
.MoveTo(pixData
, r
.width
-1, y
);
418 p
.Red() = wxPy_premultiply(red
, alphaOutline
);
419 p
.Green() = wxPy_premultiply(green
, alphaOutline
);
420 p
.Blue() = wxPy_premultiply(blue
, alphaOutline
);
421 p
.Alpha() = alphaOutline
;
425 hdc
->DrawBitmap(bmp
, r
.x
, r
.y
, true);
428 wxUnusedVar(cornerSize
);
429 wxUnusedVar(alphaFill
);
430 wxUnusedVar(alphaOutline
);
431 RectangleDraw(rc
, outline
, fill
);
435 void SurfaceImpl::Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
) {
438 hdc
->DrawEllipse(wxRectFromPRectangle(rc
));
441 void SurfaceImpl::Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
) {
442 wxRect r
= wxRectFromPRectangle(rc
);
443 hdc
->Blit(r
.x
, r
.y
, r
.width
, r
.height
,
444 ((SurfaceImpl
&)surfaceSource
).hdc
,
445 from
.x
, from
.y
, wxCOPY
);
448 void SurfaceImpl::DrawTextNoClip(PRectangle rc
, Font
&font
, int ybase
,
449 const char *s
, int len
,
450 ColourAllocated fore
, ColourAllocated back
) {
452 hdc
->SetTextForeground(wxColourFromCA(fore
));
453 hdc
->SetTextBackground(wxColourFromCA(back
));
454 FillRectangle(rc
, back
);
456 // ybase is where the baseline should be, but wxWin uses the upper left
457 // corner, so I need to calculate the real position for the text...
458 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
461 void SurfaceImpl::DrawTextClipped(PRectangle rc
, Font
&font
, int ybase
,
462 const char *s
, int len
,
463 ColourAllocated fore
, ColourAllocated back
) {
465 hdc
->SetTextForeground(wxColourFromCA(fore
));
466 hdc
->SetTextBackground(wxColourFromCA(back
));
467 FillRectangle(rc
, back
);
468 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
470 // see comments above
471 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
472 hdc
->DestroyClippingRegion();
476 void SurfaceImpl::DrawTextTransparent(PRectangle rc
, Font
&font
, int ybase
,
477 const char *s
, int len
,
478 ColourAllocated fore
) {
481 hdc
->SetTextForeground(wxColourFromCA(fore
));
482 hdc
->SetBackgroundMode(wxTRANSPARENT
);
484 // ybase is where the baseline should be, but wxWin uses the upper left
485 // corner, so I need to calculate the real position for the text...
486 hdc
->DrawText(stc2wx(s
, len
), rc
.left
, ybase
- font
.ascent
);
488 hdc
->SetBackgroundMode(wxSOLID
);
492 void SurfaceImpl::MeasureWidths(Font
&font
, const char *s
, int len
, int *positions
) {
494 wxString str
= stc2wx(s
, len
);
499 hdc
->GetPartialTextExtents(str
, tpos
);
502 // Map the widths for UCS-2 characters back to the UTF-8 input string
503 // NOTE: I don't think this is right for when sizeof(wxChar) > 2, ie wxGTK2
504 // so figure it out and fix it!
507 while ((int)i
< len
) {
508 unsigned char uch
= (unsigned char)s
[i
];
509 positions
[i
++] = tpos
[ui
];
511 if (uch
< (0x80 + 0x40 + 0x20)) {
512 positions
[i
++] = tpos
[ui
];
514 positions
[i
++] = tpos
[ui
];
515 positions
[i
++] = tpos
[ui
];
522 // If not unicode then just use the widths we have
524 std::copy(tpos
.begin(), tpos
.end(), positions
);
526 memcpy(positions
, tpos
.begin(), len
* sizeof(int));
532 int SurfaceImpl::WidthText(Font
&font
, const char *s
, int len
) {
537 hdc
->GetTextExtent(stc2wx(s
, len
), &w
, &h
);
542 int SurfaceImpl::WidthChar(Font
&font
, char ch
) {
546 char s
[2] = { ch
, 0 };
548 hdc
->GetTextExtent(stc2wx(s
, 1), &w
, &h
);
552 #define EXTENT_TEST wxT(" `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
554 int SurfaceImpl::Ascent(Font
&font
) {
557 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
562 int SurfaceImpl::Descent(Font
&font
) {
565 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
569 int SurfaceImpl::InternalLeading(Font
&WXUNUSED(font
)) {
573 int SurfaceImpl::ExternalLeading(Font
&font
) {
576 hdc
->GetTextExtent(EXTENT_TEST
, &w
, &h
, &d
, &e
);
580 int SurfaceImpl::Height(Font
&font
) {
582 return hdc
->GetCharHeight() + 1;
585 int SurfaceImpl::AverageCharWidth(Font
&font
) {
587 return hdc
->GetCharWidth();
590 int SurfaceImpl::SetPalette(Palette
*WXUNUSED(pal
), bool WXUNUSED(inBackGround
)) {
594 void SurfaceImpl::SetClip(PRectangle rc
) {
595 hdc
->SetClippingRegion(wxRectFromPRectangle(rc
));
598 void SurfaceImpl::FlushCachedState() {
601 void SurfaceImpl::SetUnicodeMode(bool unicodeMode_
) {
602 unicodeMode
=unicodeMode_
;
605 void SurfaceImpl::SetDBCSMode(int WXUNUSED(codePage
)) {
606 // dbcsMode = codePage == SC_CP_DBCS;
610 Surface
*Surface::Allocate() {
611 return new SurfaceImpl
;
615 //----------------------------------------------------------------------
618 inline wxWindow
* GETWIN(WindowID id
) { return (wxWindow
*)id
; }
623 void Window::Destroy() {
626 GETWIN(id
)->Destroy();
631 bool Window::HasFocus() {
632 return wxWindow::FindFocus() == GETWIN(id
);
635 PRectangle
Window::GetPosition() {
636 if (! id
) return PRectangle();
637 wxRect
rc(GETWIN(id
)->GetPosition(), GETWIN(id
)->GetSize());
638 return PRectangleFromwxRect(rc
);
641 void Window::SetPosition(PRectangle rc
) {
642 wxRect r
= wxRectFromPRectangle(rc
);
643 GETWIN(id
)->SetSize(r
);
646 void Window::SetPositionRelative(PRectangle rc
, Window
) {
647 SetPosition(rc
); // ????
650 PRectangle
Window::GetClientPosition() {
651 if (! id
) return PRectangle();
652 wxSize sz
= GETWIN(id
)->GetClientSize();
653 return PRectangle(0, 0, sz
.x
, sz
.y
);
656 void Window::Show(bool show
) {
657 GETWIN(id
)->Show(show
);
660 void Window::InvalidateAll() {
661 GETWIN(id
)->Refresh(false);
664 void Window::InvalidateRectangle(PRectangle rc
) {
665 wxRect r
= wxRectFromPRectangle(rc
);
666 GETWIN(id
)->Refresh(false, &r
);
669 void Window::SetFont(Font
&font
) {
670 GETWIN(id
)->SetFont(*((wxFont
*)font
.GetID()));
673 void Window::SetCursor(Cursor curs
) {
678 cursorId
= wxCURSOR_IBEAM
;
681 cursorId
= wxCURSOR_ARROW
;
684 cursorId
= wxCURSOR_ARROW
; // ** no up arrow... wxCURSOR_UPARROW;
687 cursorId
= wxCURSOR_WAIT
;
690 cursorId
= wxCURSOR_SIZEWE
;
693 cursorId
= wxCURSOR_SIZENS
;
695 case cursorReverseArrow
:
696 cursorId
= wxCURSOR_RIGHT_ARROW
;
699 cursorId
= wxCURSOR_HAND
;
702 cursorId
= wxCURSOR_ARROW
;
706 wxCursor wc
= wxStockCursor(cursorId
) ;
708 wxCursor wc
= wxCursor(cursorId
) ;
710 if(curs
!= cursorLast
)
712 GETWIN(id
)->SetCursor(wc
);
718 void Window::SetTitle(const char *s
) {
719 GETWIN(id
)->SetLabel(stc2wx(s
));
723 //----------------------------------------------------------------------
724 // Helper classes for ListBox
727 // This is a simple subclass of wxListView that just resets focus to the
728 // parent when it gets it.
729 class wxSTCListBox
: public wxListView
{
731 wxSTCListBox(wxWindow
* parent
, wxWindowID id
,
732 const wxPoint
& pos
, const wxSize
& size
,
737 Hide(); // don't flicker as we move it around...
739 Create(parent
, id
, pos
, size
, style
);
743 void OnFocus(wxFocusEvent
& event
) {
744 GetParent()->SetFocus();
748 void OnKillFocus(wxFocusEvent
& WXUNUSED(event
)) {
749 // Do nothing. Prevents base class from resetting the colors...
753 // For some reason I don't understand yet the focus doesn't really leave
754 // the listbox like it should, so if we get any events feed them back to
756 void OnKeyDown(wxKeyEvent
& event
) {
757 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
759 void OnChar(wxKeyEvent
& event
) {
760 GetGrandParent()->GetEventHandler()->ProcessEvent(event
);
763 // And we need to force the focus back when being destroyed
765 GetGrandParent()->SetFocus();
770 DECLARE_EVENT_TABLE()
773 BEGIN_EVENT_TABLE(wxSTCListBox
, wxListView
)
774 EVT_SET_FOCUS( wxSTCListBox::OnFocus
)
775 EVT_KILL_FOCUS(wxSTCListBox::OnKillFocus
)
777 EVT_KEY_DOWN( wxSTCListBox::OnKeyDown
)
778 EVT_CHAR( wxSTCListBox::OnChar
)
784 #if wxUSE_POPUPWIN //-----------------------------------
785 #include "wx/popupwin.h"
789 // TODO: Refactor these two classes to have a common base (or a mix-in) to get
790 // rid of the code duplication. (Either that or convince somebody to
791 // implement wxPopupWindow for the Mac!!)
793 // In the meantime, be careful to duplicate any changes as needed...
796 // A popup window to place the wxSTCListBox upon
797 class wxSTCListBoxWin
: public wxPopupWindow
801 CallBackAction doubleClickAction
;
802 void* doubleClickActionData
;
804 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point
WXUNUSED(location
)) :
805 wxPopupWindow(parent
, wxBORDER_NONE
)
808 SetBackgroundColour(*wxBLACK
); // for our simple border
810 lv
= new wxSTCListBox(parent
, id
, wxPoint(-50,-50), wxDefaultSize
,
811 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxBORDER_NONE
);
812 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
813 lv
->InsertColumn(0, wxEmptyString
);
814 lv
->InsertColumn(1, wxEmptyString
);
816 // NOTE: We need to fool the wxListView into thinking that it has the
817 // focus so it will use the normal selection colour and will look
818 // "right" to the user. But since the wxPopupWindow or its children
819 // can't receive focus then we have to pull a fast one and temporarily
820 // parent the listctrl on the STC window and then call SetFocus and
821 // then reparent it back to the popup.
830 // Set position in client coords
831 virtual void DoSetSize(int x
, int y
,
832 int width
, int height
,
833 int sizeFlags
= wxSIZE_AUTO
) {
834 if (x
!= wxDefaultCoord
) {
835 GetParent()->ClientToScreen(&x
, NULL
);
837 if (y
!= wxDefaultCoord
) {
838 GetParent()->ClientToScreen(NULL
, &y
);
840 wxPopupWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
843 // return position as if it were in client coords
844 virtual void DoGetPosition( int *x
, int *y
) const {
846 wxPopupWindow::DoGetPosition(&sx
, &sy
);
847 GetParent()->ScreenToClient(&sx
, &sy
);
854 if ( !wxPendingDelete
.Member(this) )
855 wxPendingDelete
.Append(this);
861 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
864 il
->GetSize(0, w
, h
);
871 void SetDoubleClickAction(CallBackAction action
, void *data
) {
872 doubleClickAction
= action
;
873 doubleClickActionData
= data
;
877 void OnFocus(wxFocusEvent
& event
) {
878 GetParent()->SetFocus();
882 void OnSize(wxSizeEvent
& event
) {
884 wxSize sz
= GetSize();
887 lv
->SetSize(1, 1, sz
.x
, sz
.y
);
888 // reset the column widths
889 lv
->SetColumnWidth(0, IconWidth()+4);
890 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
891 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
895 void OnActivate(wxListEvent
& WXUNUSED(event
)) {
896 doubleClickAction(doubleClickActionData
);
899 wxListView
* GetLB() { return lv
; }
902 DECLARE_EVENT_TABLE()
906 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxPopupWindow
)
907 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
908 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
909 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
914 #else // !wxUSE_POPUPWIN -----------------------------------
915 #include "wx/frame.h"
917 // A normal window to place the wxSTCListBox upon, but make it behave as much
918 // like a wxPopupWindow as possible
919 class wxSTCListBoxWin
: public wxFrame
{
922 CallBackAction doubleClickAction
;
923 void* doubleClickActionData
;
925 wxSTCListBoxWin(wxWindow
* parent
, wxWindowID id
, Point location
) :
926 wxFrame(parent
, id
, wxEmptyString
, wxPoint(location
.x
, location
.y
), wxSize(0,0),
928 | wxFRAME_FLOAT_ON_PARENT
938 lv
= new wxSTCListBox(this, id
, wxDefaultPosition
, wxDefaultSize
,
939 wxLC_REPORT
| wxLC_SINGLE_SEL
| wxLC_NO_HEADER
| wxNO_BORDER
);
940 lv
->SetCursor(wxCursor(wxCURSOR_ARROW
));
941 lv
->InsertColumn(0, wxEmptyString
);
942 lv
->InsertColumn(1, wxEmptyString
);
944 // Eventhough we immediately reset the focus to the parent, this helps
945 // things to look right...
952 // On OSX and (possibly others) there can still be pending
953 // messages/events for the list control when Scintilla wants to
954 // close it, so do a pending delete of it instead of destroying
959 // The bottom edge of this window is not getting properly
960 // refreshed upon deletion, so help it out...
961 wxWindow
* p
= GetParent();
962 wxRect
r(GetPosition(), GetSize());
963 r
.SetHeight(r
.GetHeight()+1);
964 p
->Refresh(false, &r
);
966 if ( !wxPendingDelete
.Member(this) )
967 wxPendingDelete
.Append(this);
974 wxImageList
* il
= lv
->GetImageList(wxIMAGE_LIST_SMALL
);
977 il
->GetSize(0, w
, h
);
984 void SetDoubleClickAction(CallBackAction action
, void *data
)
986 doubleClickAction
= action
;
987 doubleClickActionData
= data
;
991 void OnFocus(wxFocusEvent
& event
)
994 GetParent()->SetFocus();
998 void OnSize(wxSizeEvent
& event
)
1001 wxSize sz
= GetClientSize();
1003 // reset the column widths
1004 lv
->SetColumnWidth(0, IconWidth()+4);
1005 lv
->SetColumnWidth(1, sz
.x
- 2 - lv
->GetColumnWidth(0) -
1006 wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
));
1010 void ActivateParent()
1012 // Although we're a frame, we always want the parent to be active, so
1013 // raise it whenever we get shown, focused, etc.
1014 wxTopLevelWindow
*frame
= wxDynamicCast(
1015 wxGetTopLevelParent(GetParent()), wxTopLevelWindow
);
1021 virtual void DoSetSize(int x
, int y
,
1022 int width
, int height
,
1023 int sizeFlags
= wxSIZE_AUTO
)
1025 // convert coords to screen coords since we're a top-level window
1026 if (x
!= wxDefaultCoord
) {
1027 GetParent()->ClientToScreen(&x
, NULL
);
1029 if (y
!= wxDefaultCoord
) {
1030 GetParent()->ClientToScreen(NULL
, &y
);
1032 wxFrame::DoSetSize(x
, y
, width
, height
, sizeFlags
);
1035 virtual bool Show(bool show
= true)
1037 bool rv
= wxFrame::Show(show
);
1041 GetParent()->Refresh(false);
1046 void OnActivate(wxListEvent
& WXUNUSED(event
))
1048 doubleClickAction(doubleClickActionData
);
1051 wxListView
* GetLB() { return lv
; }
1054 DECLARE_EVENT_TABLE()
1058 BEGIN_EVENT_TABLE(wxSTCListBoxWin
, wxWindow
)
1059 EVT_SET_FOCUS ( wxSTCListBoxWin::OnFocus
)
1060 EVT_SIZE ( wxSTCListBoxWin::OnSize
)
1061 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxSTCListBoxWin::OnActivate
)
1064 #endif // wxUSE_POPUPWIN -----------------------------------
1067 inline wxSTCListBoxWin
* GETLBW(WindowID win
) {
1068 return ((wxSTCListBoxWin
*)win
);
1071 inline wxListView
* GETLB(WindowID win
) {
1072 return GETLBW(win
)->GetLB();
1075 //----------------------------------------------------------------------
1077 class ListBoxImpl
: public ListBox
{
1081 int desiredVisibleRows
;
1084 Point location
; // Caret location at which the list is opened
1085 wxImageList
* imgList
;
1086 wxArrayInt
* imgTypeMap
;
1092 virtual void SetFont(Font
&font
);
1093 virtual void Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
);
1094 virtual void SetAverageCharWidth(int width
);
1095 virtual void SetVisibleRows(int rows
);
1096 virtual int GetVisibleRows() const;
1097 virtual PRectangle
GetDesiredRect();
1098 virtual int CaretFromEdge();
1099 virtual void Clear();
1100 virtual void Append(char *s
, int type
= -1);
1101 void Append(const wxString
& text
, int type
);
1102 virtual int Length();
1103 virtual void Select(int n
);
1104 virtual int GetSelection();
1105 virtual int Find(const char *prefix
);
1106 virtual void GetValue(int n
, char *value
, int len
);
1107 virtual void RegisterImage(int type
, const char *xpm_data
);
1108 virtual void ClearRegisteredImages();
1109 virtual void SetDoubleClickAction(CallBackAction
, void *);
1110 virtual void SetList(const char* list
, char separator
, char typesep
);
1114 ListBoxImpl::ListBoxImpl()
1115 : lineHeight(10), unicodeMode(false),
1116 desiredVisibleRows(5), aveCharWidth(8), maxStrWidth(0),
1117 imgList(NULL
), imgTypeMap(NULL
)
1121 ListBoxImpl::~ListBoxImpl() {
1133 void ListBoxImpl::SetFont(Font
&font
) {
1134 GETLB(id
)->SetFont(*((wxFont
*)font
.GetID()));
1138 void ListBoxImpl::Create(Window
&parent
, int ctrlID
, Point location_
, int lineHeight_
, bool unicodeMode_
) {
1139 location
= location_
;
1140 lineHeight
= lineHeight_
;
1141 unicodeMode
= unicodeMode_
;
1143 id
= new wxSTCListBoxWin(GETWIN(parent
.GetID()), ctrlID
, location
);
1144 if (imgList
!= NULL
)
1145 GETLB(id
)->SetImageList(imgList
, wxIMAGE_LIST_SMALL
);
1149 void ListBoxImpl::SetAverageCharWidth(int width
) {
1150 aveCharWidth
= width
;
1154 void ListBoxImpl::SetVisibleRows(int rows
) {
1155 desiredVisibleRows
= rows
;
1159 int ListBoxImpl::GetVisibleRows() const {
1160 return desiredVisibleRows
;
1163 PRectangle
ListBoxImpl::GetDesiredRect() {
1164 // wxListCtrl doesn't have a DoGetBestSize, so instead we kept track of
1165 // the max size in Append and calculate it here...
1166 int maxw
= maxStrWidth
* aveCharWidth
;
1169 // give it a default if there are no lines, and/or add a bit more
1170 if (maxw
== 0) maxw
= 100;
1171 maxw
+= aveCharWidth
* 3 +
1172 GETLBW(id
)->IconWidth() + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1176 // estimate a desired height
1177 int count
= GETLB(id
)->GetItemCount();
1180 GETLB(id
)->GetItemRect(0, rect
);
1181 maxh
= count
* rect
.GetHeight();
1182 if (maxh
> 140) // TODO: Use desiredVisibleRows??
1185 // Try to make the size an exact multiple of some number of lines
1186 int lines
= maxh
/ rect
.GetHeight();
1187 maxh
= (lines
+ 1) * rect
.GetHeight() + 2;
1201 int ListBoxImpl::CaretFromEdge() {
1202 return 4 + GETLBW(id
)->IconWidth();
1206 void ListBoxImpl::Clear() {
1207 GETLB(id
)->DeleteAllItems();
1211 void ListBoxImpl::Append(char *s
, int type
) {
1212 Append(stc2wx(s
), type
);
1215 void ListBoxImpl::Append(const wxString
& text
, int type
) {
1216 long count
= GETLB(id
)->GetItemCount();
1217 long itemID
= GETLB(id
)->InsertItem(count
, wxEmptyString
);
1218 GETLB(id
)->SetItem(itemID
, 1, text
);
1219 maxStrWidth
= wxMax(maxStrWidth
, text
.length());
1221 wxCHECK_RET(imgTypeMap
, wxT("Unexpected NULL imgTypeMap"));
1222 long idx
= imgTypeMap
->Item(type
);
1223 GETLB(id
)->SetItemImage(itemID
, idx
, idx
);
1227 void ListBoxImpl::SetList(const char* list
, char separator
, char typesep
) {
1228 GETLB(id
)->Freeze();
1230 wxStringTokenizer
tkzr(stc2wx(list
), (wxChar
)separator
);
1231 while ( tkzr
.HasMoreTokens() ) {
1232 wxString token
= tkzr
.GetNextToken();
1234 int pos
= token
.Find(typesep
);
1236 token
.Mid(pos
+1).ToLong(&type
);
1237 token
.Truncate(pos
);
1239 Append(token
, (int)type
);
1245 int ListBoxImpl::Length() {
1246 return GETLB(id
)->GetItemCount();
1250 void ListBoxImpl::Select(int n
) {
1256 GETLB(id
)->EnsureVisible(n
);
1257 GETLB(id
)->Select(n
, select
);
1261 int ListBoxImpl::GetSelection() {
1262 return GETLB(id
)->GetFirstSelected();
1266 int ListBoxImpl::Find(const char *WXUNUSED(prefix
)) {
1272 void ListBoxImpl::GetValue(int n
, char *value
, int len
) {
1276 item
.SetMask(wxLIST_MASK_TEXT
);
1277 GETLB(id
)->GetItem(item
);
1278 strncpy(value
, wx2stc(item
.GetText()), len
);
1279 value
[len
-1] = '\0';
1283 void ListBoxImpl::RegisterImage(int type
, const char *xpm_data
) {
1284 wxMemoryInputStream
stream(xpm_data
, strlen(xpm_data
)+1);
1285 wxImage
img(stream
, wxBITMAP_TYPE_XPM
);
1289 // assumes all images are the same size
1290 imgList
= new wxImageList(bmp
.GetWidth(), bmp
.GetHeight(), true);
1291 imgTypeMap
= new wxArrayInt
;
1294 int idx
= imgList
->Add(bmp
);
1296 // do we need to extend the mapping array?
1297 wxArrayInt
& itm
= *imgTypeMap
;
1298 if ( itm
.GetCount() < (size_t)type
+1)
1299 itm
.Add(-1, type
- itm
.GetCount() + 1);
1301 // Add an item that maps type to the image index
1305 void ListBoxImpl::ClearRegisteredImages() {
1315 GETLB(id
)->SetImageList(NULL
, wxIMAGE_LIST_SMALL
);
1319 void ListBoxImpl::SetDoubleClickAction(CallBackAction action
, void *data
) {
1320 GETLBW(id
)->SetDoubleClickAction(action
, data
);
1324 ListBox::ListBox() {
1327 ListBox::~ListBox() {
1330 ListBox
*ListBox::Allocate() {
1331 return new ListBoxImpl();
1334 //----------------------------------------------------------------------
1336 Menu::Menu() : id(0) {
1339 void Menu::CreatePopUp() {
1344 void Menu::Destroy() {
1350 void Menu::Show(Point pt
, Window
&w
) {
1351 GETWIN(w
.GetID())->PopupMenu((wxMenu
*)id
, pt
.x
- 4, pt
.y
);
1355 //----------------------------------------------------------------------
1357 DynamicLibrary
*DynamicLibrary::Load(const char *WXUNUSED(modulePath
)) {
1358 wxFAIL_MSG(wxT("Dynamic lexer loading not implemented yet"));
1362 //----------------------------------------------------------------------
1364 ColourDesired
Platform::Chrome() {
1366 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
1367 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1370 ColourDesired
Platform::ChromeHighlight() {
1372 c
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT
);
1373 return ColourDesired(c
.Red(), c
.Green(), c
.Blue());
1376 const char *Platform::DefaultFont() {
1377 static char buf
[128];
1378 strcpy(buf
, wxNORMAL_FONT
->GetFaceName().mbc_str());
1382 int Platform::DefaultFontSize() {
1383 return wxNORMAL_FONT
->GetPointSize();
1386 unsigned int Platform::DoubleClickTime() {
1387 return 500; // **** ::GetDoubleClickTime();
1390 bool Platform::MouseButtonBounce() {
1394 bool Platform::IsKeyDown(int WXUNUSED(key
)) {
1395 return false; // I don't think we'll need this.
1398 long Platform::SendScintilla(WindowID w
,
1400 unsigned long wParam
,
1403 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1404 return stc
->SendMsg(msg
, wParam
, lParam
);
1407 long Platform::SendScintillaPointer(WindowID w
,
1409 unsigned long wParam
,
1412 wxStyledTextCtrl
* stc
= (wxStyledTextCtrl
*)w
;
1413 return stc
->SendMsg(msg
, wParam
, (long)lParam
);
1417 // These are utility functions not really tied to a platform
1419 int Platform::Minimum(int a
, int b
) {
1426 int Platform::Maximum(int a
, int b
) {
1435 void Platform::DebugDisplay(const char *s
) {
1437 wxLogDebug(stc2wx(s
));
1443 void Platform::DebugPrintf(const char *format
, ...) {
1447 va_start(pArguments
, format
);
1448 vsprintf(buffer
,format
,pArguments
);
1450 Platform::DebugDisplay(buffer
);
1452 wxUnusedVar(format
);
1457 static bool assertionPopUps
= true;
1459 bool Platform::ShowAssertionPopUps(bool assertionPopUps_
) {
1460 bool ret
= assertionPopUps
;
1461 assertionPopUps
= assertionPopUps_
;
1465 void Platform::Assert(const char *c
, const char *file
, int line
) {
1468 sprintf(buffer
, "Assertion [%s] failed at %s %d", c
, file
, line
);
1469 if (assertionPopUps
) {
1471 wxMessageBox(stc2wx(buffer
),
1472 wxT("Assertion failure"),
1473 wxICON_HAND
| wxOK
);
1475 strcat(buffer
, "\r\n");
1476 Platform::DebugDisplay(buffer
);
1487 int Platform::Clamp(int val
, int minVal
, int maxVal
) {
1496 bool Platform::IsDBCSLeadByte(int WXUNUSED(codePage
), char WXUNUSED(ch
)) {
1500 int Platform::DBCSCharLength(int WXUNUSED(codePage
), const char *WXUNUSED(s
)) {
1504 int Platform::DBCSCharMaxLength() {
1509 //----------------------------------------------------------------------
1511 ElapsedTime::ElapsedTime() {
1512 wxLongLong localTime
= wxGetLocalTimeMillis();
1513 littleBit
= localTime
.GetLo();
1514 bigBit
= localTime
.GetHi();
1517 double ElapsedTime::Duration(bool reset
) {
1518 wxLongLong
prevTime(bigBit
, littleBit
);
1519 wxLongLong localTime
= wxGetLocalTimeMillis();
1521 littleBit
= localTime
.GetLo();
1522 bigBit
= localTime
.GetHi();
1524 wxLongLong duration
= localTime
- prevTime
;
1525 double result
= duration
.ToDouble();
1531 //----------------------------------------------------------------------
1535 #include "UniConversion.h"
1537 // Convert using Scintilla's functions instead of wx's, Scintilla's are more
1538 // forgiving and won't assert...
1540 wxString
stc2wx(const char* str
, size_t len
)
1543 return wxEmptyString
;
1545 size_t wclen
= UCS2Length(str
, len
);
1546 wxWCharBuffer
buffer(wclen
+1);
1548 size_t actualLen
= UCS2FromUTF8(str
, len
, buffer
.data(), wclen
+1);
1549 return wxString(buffer
.data(), actualLen
);
1554 wxString
stc2wx(const char* str
)
1556 return stc2wx(str
, strlen(str
));
1560 const wxWX2MBbuf
wx2stc(const wxString
& str
)
1562 const wchar_t* wcstr
= str
.c_str();
1563 size_t wclen
= str
.length();
1564 size_t len
= UTF8Length(wcstr
, wclen
);
1566 wxCharBuffer
buffer(len
+1);
1567 UTF8FromUCS2(wcstr
, wclen
, buffer
.data(), len
);
1569 // TODO check NULL termination!!