1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/renderer.cpp
3 // Purpose: implementation of wxRendererNative for Windows
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/string.h"
29 #include "wx/window.h"
31 #include "wx/settings.h"
34 #include "wx/scopeguard.h"
35 #include "wx/splitter.h"
36 #include "wx/renderer.h"
37 #include "wx/msw/private.h"
38 #include "wx/msw/uxtheme.h"
40 // tmschema.h is in Win32 Platform SDK and might not be available with earlier
42 #ifndef CP_DROPDOWNBUTTON
43 #define BP_PUSHBUTTON 1
44 #define BP_RADIOBUTTON 2
46 #define RBS_UNCHECKEDNORMAL 1
47 #define RBS_CHECKEDNORMAL (RBS_UNCHECKEDNORMAL + 4)
48 #define RBS_MIXEDNORMAL (RBS_CHECKEDNORMAL + 4)
49 #define CBS_UNCHECKEDNORMAL 1
50 #define CBS_CHECKEDNORMAL (CBS_UNCHECKEDNORMAL + 4)
51 #define CBS_MIXEDNORMAL (CBS_CHECKEDNORMAL + 4)
56 #define PBS_DISABLED 4
57 #define PBS_DEFAULTED 5
59 #define CP_DROPDOWNBUTTON 1
63 #define CBXS_PRESSED 3
64 #define CBXS_DISABLED 4
71 #define HP_HEADERITEM 1
77 #define TMT_HEIGHT 2417
79 #define HP_HEADERSORTARROW 4
80 #define HSAS_SORTEDUP 1
81 #define HSAS_SORTEDDOWN 2
86 #define ETS_SELECTED 3
87 #define ETS_DISABLED 4
89 #define ETS_READONLY 6
91 #define TMT_FILLCOLOR 3802
92 #define TMT_TEXTCOLOR 3803
93 #define TMT_BORDERCOLOR 3801
94 #define TMT_EDGEFILLCOLOR 3808
96 #define WP_MINBUTTON 15
97 #define WP_MAXBUTTON 17
98 #define WP_CLOSEBUTTON 18
99 #define WP_RESTOREBUTTON 21
100 #define WP_HELPBUTTON 23
103 #if defined(__WXWINCE__)
113 #define DFCS_HOT 0x1000
116 // ----------------------------------------------------------------------------
117 // methods common to wxRendererMSW and wxRendererXP
118 // ----------------------------------------------------------------------------
120 class wxRendererMSWBase
: public wxDelegateRendererNative
123 wxRendererMSWBase() { }
124 wxRendererMSWBase(wxRendererNative
& rendererNative
)
125 : wxDelegateRendererNative(rendererNative
) { }
127 void DrawFocusRect(wxWindow
* win
,
132 void DrawItemSelectionRect(wxWindow
*win
,
138 // ----------------------------------------------------------------------------
139 // wxRendererMSW: wxRendererNative implementation for "old" Win32 systems
140 // ----------------------------------------------------------------------------
142 class wxRendererMSW
: public wxRendererMSWBase
147 static wxRendererNative
& Get();
149 virtual void DrawComboBoxDropButton(wxWindow
*win
,
154 virtual void DrawCheckBox(wxWindow
*win
,
159 DoDrawButton(DFCS_BUTTONCHECK
, win
, dc
, rect
, flags
);
162 virtual void DrawPushButton(wxWindow
*win
,
167 virtual void DrawChoice(wxWindow
* win
,
172 virtual void DrawComboBox(wxWindow
* win
,
177 virtual void DrawTextCtrl(wxWindow
* win
,
182 virtual void DrawRadioBitmap(wxWindow
* win
,
187 DoDrawButton(DFCS_BUTTONRADIO
, win
, dc
, rect
, flags
);
190 virtual void DrawTitleBarBitmap(wxWindow
*win
,
193 wxTitleBarButton button
,
196 virtual wxSize
GetCheckBoxSize(wxWindow
*win
);
198 virtual int GetHeaderButtonHeight(wxWindow
*win
);
200 virtual int GetHeaderButtonMargin(wxWindow
*win
);
203 // wrapper of DrawFrameControl()
204 void DoDrawFrameControl(UINT type
,
211 // common part of Draw{PushButton,CheckBox,RadioBitmap}(): wraps
212 // DrawFrameControl(DFC_BUTTON)
213 void DoDrawButton(UINT kind
,
219 DoDrawFrameControl(DFC_BUTTON
, kind
, win
, dc
, rect
, flags
);
222 wxDECLARE_NO_COPY_CLASS(wxRendererMSW
);
225 // ----------------------------------------------------------------------------
226 // wxRendererXP: wxRendererNative implementation for Windows XP and later
227 // ----------------------------------------------------------------------------
231 class wxRendererXP
: public wxRendererMSWBase
234 wxRendererXP() : wxRendererMSWBase(wxRendererMSW::Get()) { }
236 static wxRendererNative
& Get();
238 virtual int DrawHeaderButton(wxWindow
*win
,
242 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
,
243 wxHeaderButtonParams
* params
= NULL
);
245 virtual void DrawTreeItemButton(wxWindow
*win
,
249 virtual void DrawSplitterBorder(wxWindow
*win
,
253 virtual void DrawSplitterSash(wxWindow
*win
,
257 wxOrientation orient
,
259 virtual void DrawComboBoxDropButton(wxWindow
*win
,
263 virtual void DrawCheckBox(wxWindow
*win
,
268 if ( !DoDrawXPButton(BP_CHECKBOX
, win
, dc
, rect
, flags
) )
269 m_rendererNative
.DrawCheckBox(win
, dc
, rect
, flags
);
272 virtual void DrawPushButton(wxWindow
*win
,
277 if ( !DoDrawXPButton(BP_PUSHBUTTON
, win
, dc
, rect
, flags
) )
278 m_rendererNative
.DrawPushButton(win
, dc
, rect
, flags
);
281 virtual void DrawRadioBitmap(wxWindow
*win
,
286 if ( !DoDrawXPButton(BP_RADIOBUTTON
, win
, dc
, rect
, flags
) )
287 m_rendererNative
.DrawRadioBitmap(win
, dc
, rect
, flags
);
290 virtual void DrawTitleBarBitmap(wxWindow
*win
,
293 wxTitleBarButton button
,
296 virtual wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
299 // wrapper around DrawThemeBackground() translating flags to NORMAL/HOT/
300 // PUSHED/DISABLED states (and so suitable for drawing anything
302 void DoDrawButtonLike(HTHEME htheme
,
308 // common part of DrawCheckBox(), DrawPushButton() and DrawRadioBitmap()
309 bool DoDrawXPButton(int kind
,
315 wxDECLARE_NO_COPY_CLASS(wxRendererXP
);
318 #endif // wxUSE_UXTHEME
321 // ============================================================================
322 // wxRendererMSWBase implementation
323 // ============================================================================
325 void wxRendererMSWBase::DrawFocusRect(wxWindow
* WXUNUSED(win
),
331 wxCopyRectToRECT(rect
, rc
);
333 ::DrawFocusRect(GetHdcOf(dc
.GetTempHDC()), &rc
);
336 void wxRendererMSWBase::DrawItemSelectionRect(wxWindow
*win
,
342 if ( flags
& wxCONTROL_SELECTED
)
344 if ( flags
& wxCONTROL_FOCUSED
)
346 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
350 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
355 brush
= *wxTRANSPARENT_BRUSH
;
359 dc
.SetPen(*wxTRANSPARENT_PEN
);
360 dc
.DrawRectangle( rect
);
362 if ((flags
& wxCONTROL_FOCUSED
) && (flags
& wxCONTROL_CURRENT
))
363 DrawFocusRect( win
, dc
, rect
, flags
);
367 // ============================================================================
368 // wxRendererNative and wxRendererMSW implementation
369 // ============================================================================
372 wxRendererNative
& wxRendererNative::GetDefault()
375 wxUxThemeEngine
*themeEngine
= wxUxThemeEngine::Get();
376 if ( themeEngine
&& themeEngine
->IsAppThemed() )
377 return wxRendererXP::Get();
378 #endif // wxUSE_UXTHEME
380 return wxRendererMSW::Get();
384 wxRendererNative
& wxRendererMSW::Get()
386 static wxRendererMSW s_rendererMSW
;
388 return s_rendererMSW
;
392 wxRendererMSW::DrawComboBoxDropButton(wxWindow
* WXUNUSED(win
),
398 wxCopyRectToRECT(rect
, r
);
400 int style
= DFCS_SCROLLCOMBOBOX
;
401 if ( flags
& wxCONTROL_DISABLED
)
402 style
|= DFCS_INACTIVE
;
403 if ( flags
& wxCONTROL_PRESSED
)
404 style
|= DFCS_PUSHED
| DFCS_FLAT
;
406 ::DrawFrameControl(GetHdcOf(dc
.GetTempHDC()), &r
, DFC_SCROLL
, style
);
410 wxRendererMSW::DoDrawFrameControl(UINT type
,
412 wxWindow
* WXUNUSED(win
),
418 wxCopyRectToRECT(rect
, r
);
421 if ( flags
& wxCONTROL_CHECKED
)
422 style
|= DFCS_CHECKED
;
423 if ( flags
& wxCONTROL_DISABLED
)
424 style
|= DFCS_INACTIVE
;
425 if ( flags
& wxCONTROL_FLAT
)
427 if ( flags
& wxCONTROL_PRESSED
)
428 style
|= DFCS_PUSHED
;
429 if ( flags
& wxCONTROL_CURRENT
)
432 ::DrawFrameControl(GetHdcOf(dc
.GetTempHDC()), &r
, type
, style
);
436 wxRendererMSW::DrawPushButton(wxWindow
*win
,
438 const wxRect
& rectOrig
,
441 wxRect
rect(rectOrig
);
442 if ( flags
& wxCONTROL_ISDEFAULT
)
444 // DrawFrameControl() doesn't seem to support default buttons so we
445 // have to draw the border ourselves
446 wxDCPenChanger
pen(dc
, *wxBLACK_PEN
);
447 wxDCBrushChanger
brush(dc
, *wxTRANSPARENT_BRUSH
);
448 dc
.DrawRectangle(rect
);
452 DoDrawButton(DFCS_BUTTONPUSH
, win
, dc
, rect
, flags
);
456 wxRendererMSW::DrawTitleBarBitmap(wxWindow
*win
,
459 wxTitleBarButton button
,
465 case wxTITLEBAR_BUTTON_CLOSE
:
466 kind
= DFCS_CAPTIONCLOSE
;
469 case wxTITLEBAR_BUTTON_MAXIMIZE
:
470 kind
= DFCS_CAPTIONMAX
;
473 case wxTITLEBAR_BUTTON_ICONIZE
:
474 kind
= DFCS_CAPTIONMIN
;
477 case wxTITLEBAR_BUTTON_RESTORE
:
478 kind
= DFCS_CAPTIONRESTORE
;
481 case wxTITLEBAR_BUTTON_HELP
:
482 kind
= DFCS_CAPTIONHELP
;
486 wxFAIL_MSG( "unsupported title bar button" );
490 DoDrawFrameControl(DFC_CAPTION
, kind
, win
, dc
, rect
, flags
);
493 wxSize
wxRendererMSW::GetCheckBoxSize(wxWindow
* WXUNUSED(win
))
495 return wxSize(::GetSystemMetrics(SM_CXMENUCHECK
),
496 ::GetSystemMetrics(SM_CYMENUCHECK
));
499 int wxRendererMSW::GetHeaderButtonHeight(wxWindow
* WXUNUSED(win
))
501 // some "reasonable" value returned in case of error, it doesn't really
502 // correspond to anything but it's better than returning 0
503 static const int DEFAULT_HEIGHT
= 20;
506 // create a temporary header window just to get its geometry
507 HWND hwndHeader
= ::CreateWindow(WC_HEADER
, NULL
, 0,
508 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
);
510 return DEFAULT_HEIGHT
;
512 wxON_BLOCK_EXIT1( ::DestroyWindow
, hwndHeader
);
514 // initialize the struct filled with the values by Header_Layout()
515 RECT parentRect
= { 0, 0, 100, 100 };
516 WINDOWPOS wp
= { 0, 0, 0, 0, 0, 0, 0 };
517 HDLAYOUT hdl
= { &parentRect
, &wp
};
519 return Header_Layout(hwndHeader
, &hdl
) ? wp
.cy
: DEFAULT_HEIGHT
;
522 int wxRendererMSW::GetHeaderButtonMargin(wxWindow
*WXUNUSED(win
))
527 // Uses the theme to draw the border and fill for something like a wxTextCtrl
528 void wxRendererMSW::DrawTextCtrl(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
535 wxUxThemeHandle
hTheme(win
, L
"EDIT");
538 wxUxThemeEngine::Get()->GetThemeColor(hTheme
, EP_EDITTEXT
,
539 ETS_NORMAL
, TMT_FILLCOLOR
, &cref
);
540 fill
= wxRGBToColour(cref
);
543 if ( flags
& wxCONTROL_DISABLED
)
544 etsState
= ETS_DISABLED
;
546 etsState
= ETS_NORMAL
;
548 wxUxThemeEngine::Get()->GetThemeColor(hTheme
, EP_EDITTEXT
,
549 etsState
, TMT_BORDERCOLOR
, &cref
);
550 bdr
= wxRGBToColour(cref
);
555 fill
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
561 dc
.DrawRectangle(rect
);
565 // Draw the equivalent of a wxComboBox
566 void wxRendererMSW::DrawComboBox(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
568 // Draw the main part of the control same as TextCtrl
569 DrawTextCtrl(win
, dc
, rect
, flags
);
571 // Draw the button inside the border, on the right side
574 br
.x
+= br
.width
- br
.height
- 1;
575 br
.width
= br
.height
;
578 DrawComboBoxDropButton(win
, dc
, br
, flags
);
582 void wxRendererMSW::DrawChoice(wxWindow
* win
, wxDC
& dc
,
583 const wxRect
& rect
, int flags
)
585 DrawComboBox(win
, dc
, rect
, flags
);
588 // ============================================================================
589 // wxRendererXP implementation
590 // ============================================================================
595 wxRendererNative
& wxRendererXP::Get()
597 static wxRendererXP s_rendererXP
;
602 // NOTE: There is no guarantee that the button drawn fills the entire rect (XP
603 // default theme, for example), so the caller should have cleared button's
604 // background before this call. This is quite likely a wxMSW-specific thing.
606 wxRendererXP::DrawComboBoxDropButton(wxWindow
* win
,
611 wxUxThemeHandle
hTheme(win
, L
"COMBOBOX");
614 m_rendererNative
.DrawComboBoxDropButton(win
, dc
, rect
, flags
);
619 wxCopyRectToRECT(rect
, r
);
622 if ( flags
& wxCONTROL_PRESSED
)
623 state
= CBXS_PRESSED
;
624 else if ( flags
& wxCONTROL_CURRENT
)
626 else if ( flags
& wxCONTROL_DISABLED
)
627 state
= CBXS_DISABLED
;
631 wxUxThemeEngine::Get()->DrawThemeBackground
634 GetHdcOf(dc
.GetTempHDC()),
644 wxRendererXP::DrawHeaderButton(wxWindow
*win
,
648 wxHeaderSortIconType sortArrow
,
649 wxHeaderButtonParams
* params
)
651 wxUxThemeHandle
hTheme(win
, L
"HEADER");
654 return m_rendererNative
.DrawHeaderButton(win
, dc
, rect
, flags
, sortArrow
, params
);
658 wxCopyRectToRECT(rect
, r
);
661 if ( flags
& wxCONTROL_PRESSED
)
663 else if ( flags
& wxCONTROL_CURRENT
)
667 wxUxThemeEngine::Get()->DrawThemeBackground
670 GetHdcOf(dc
.GetTempHDC()),
677 // NOTE: Using the theme to draw HP_HEADERSORTARROW doesn't do anything.
678 // Why? If this can be fixed then draw the sort arrows using the theme
679 // and then clear those flags before calling DrawHeaderButtonContents.
681 // Add any extras that are specified in flags and params
682 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
687 wxRendererXP::DrawTreeItemButton(wxWindow
*win
,
692 wxUxThemeHandle
hTheme(win
, L
"TREEVIEW");
695 m_rendererNative
.DrawTreeItemButton(win
, dc
, rect
, flags
);
700 wxCopyRectToRECT(rect
, r
);
702 int state
= flags
& wxCONTROL_EXPANDED
? GLPS_OPENED
: GLPS_CLOSED
;
703 wxUxThemeEngine::Get()->DrawThemeBackground
706 GetHdcOf(dc
.GetTempHDC()),
715 wxRendererXP::DoDrawXPButton(int kind
,
721 wxUxThemeHandle
hTheme(win
, L
"BUTTON");
725 DoDrawButtonLike(hTheme
, kind
, dc
, rect
, flags
);
731 wxRendererXP::DoDrawButtonLike(HTHEME htheme
,
738 wxCopyRectToRECT(rect
, r
);
740 // the base state is always 1, whether it is PBS_NORMAL,
741 // {CBS,RBS}_UNCHECKEDNORMAL or CBS_NORMAL
744 // XBS_XXX is followed by XBX_XXXHOT, then XBS_XXXPRESSED and DISABLED
754 // in both RBS_ and CBS_ enums CHECKED elements are offset by 4 from base
755 // (UNCHECKED) ones and MIXED are offset by 4 again as there are all states
756 // from the above enum in between them
757 if ( flags
& wxCONTROL_CHECKED
)
758 state
+= STATES_COUNT
;
759 else if ( flags
& wxCONTROL_UNDETERMINED
)
760 state
+= 2*STATES_COUNT
;
762 if ( flags
& wxCONTROL_DISABLED
)
763 state
+= DISABLED_OFFSET
;
764 else if ( flags
& wxCONTROL_PRESSED
)
765 state
+= PRESSED_OFFSET
;
766 else if ( flags
& wxCONTROL_CURRENT
)
768 // wxCONTROL_ISDEFAULT flag is only valid for push buttons
769 else if ( part
== BP_PUSHBUTTON
&& (flags
& wxCONTROL_ISDEFAULT
) )
770 state
= PBS_DEFAULTED
;
772 wxUxThemeEngine::Get()->DrawThemeBackground
775 GetHdcOf(dc
.GetTempHDC()),
784 wxRendererXP::DrawTitleBarBitmap(wxWindow
*win
,
787 wxTitleBarButton button
,
790 wxUxThemeHandle
hTheme(win
, L
"WINDOW");
793 m_rendererNative
.DrawTitleBarBitmap(win
, dc
, rect
, button
, flags
);
800 case wxTITLEBAR_BUTTON_CLOSE
:
801 part
= WP_CLOSEBUTTON
;
804 case wxTITLEBAR_BUTTON_MAXIMIZE
:
808 case wxTITLEBAR_BUTTON_ICONIZE
:
812 case wxTITLEBAR_BUTTON_RESTORE
:
813 part
= WP_RESTOREBUTTON
;
816 case wxTITLEBAR_BUTTON_HELP
:
817 part
= WP_HELPBUTTON
;
821 wxFAIL_MSG( "unsupported title bar button" );
825 DoDrawButtonLike(hTheme
, part
, dc
, rect
, flags
);
828 // ----------------------------------------------------------------------------
830 // ----------------------------------------------------------------------------
832 // the width of the sash: this is the same as used by Explorer...
833 static const wxCoord SASH_WIDTH
= 4;
835 wxSplitterRenderParams
836 wxRendererXP::GetSplitterParams(const wxWindow
* win
)
838 if ( win
->HasFlag(wxSP_NO_XP_THEME
) )
839 return m_rendererNative
.GetSplitterParams(win
);
841 return wxSplitterRenderParams(SASH_WIDTH
, 0, false);
845 wxRendererXP::DrawSplitterBorder(wxWindow
* win
,
850 if ( win
->HasFlag(wxSP_NO_XP_THEME
) )
852 m_rendererNative
.DrawSplitterBorder(win
, dc
, rect
, flags
);
857 wxRendererXP::DrawSplitterSash(wxWindow
*win
,
861 wxOrientation orient
,
864 if ( !win
->HasFlag(wxSP_NO_XP_THEME
) )
866 dc
.SetPen(*wxTRANSPARENT_PEN
);
867 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
)));
868 if ( orient
== wxVERTICAL
)
870 dc
.DrawRectangle(position
, 0, SASH_WIDTH
, size
.y
);
874 dc
.DrawRectangle(0, position
, size
.x
, SASH_WIDTH
);
880 m_rendererNative
.DrawSplitterSash(win
, dc
, size
, position
, orient
, flags
);
883 #endif // wxUSE_UXTHEME