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
);
201 // wrapper of DrawFrameControl()
202 void DoDrawFrameControl(UINT type
,
209 // common part of Draw{PushButton,CheckBox,RadioBitmap}(): wraps
210 // DrawFrameControl(DFC_BUTTON)
211 void DoDrawButton(UINT kind
,
217 DoDrawFrameControl(DFC_BUTTON
, kind
, win
, dc
, rect
, flags
);
220 wxDECLARE_NO_COPY_CLASS(wxRendererMSW
);
223 // ----------------------------------------------------------------------------
224 // wxRendererXP: wxRendererNative implementation for Windows XP and later
225 // ----------------------------------------------------------------------------
229 class wxRendererXP
: public wxRendererMSWBase
232 wxRendererXP() : wxRendererMSWBase(wxRendererMSW::Get()) { }
234 static wxRendererNative
& Get();
236 virtual int DrawHeaderButton(wxWindow
*win
,
240 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
,
241 wxHeaderButtonParams
* params
= NULL
);
243 virtual void DrawTreeItemButton(wxWindow
*win
,
247 virtual void DrawSplitterBorder(wxWindow
*win
,
251 virtual void DrawSplitterSash(wxWindow
*win
,
255 wxOrientation orient
,
257 virtual void DrawComboBoxDropButton(wxWindow
*win
,
261 virtual void DrawCheckBox(wxWindow
*win
,
266 if ( !DoDrawXPButton(BP_CHECKBOX
, win
, dc
, rect
, flags
) )
267 m_rendererNative
.DrawCheckBox(win
, dc
, rect
, flags
);
270 virtual void DrawPushButton(wxWindow
*win
,
275 if ( !DoDrawXPButton(BP_PUSHBUTTON
, win
, dc
, rect
, flags
) )
276 m_rendererNative
.DrawPushButton(win
, dc
, rect
, flags
);
279 virtual void DrawRadioBitmap(wxWindow
*win
,
284 if ( !DoDrawXPButton(BP_RADIOBUTTON
, win
, dc
, rect
, flags
) )
285 m_rendererNative
.DrawRadioBitmap(win
, dc
, rect
, flags
);
288 virtual void DrawTitleBarBitmap(wxWindow
*win
,
291 wxTitleBarButton button
,
294 virtual wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
297 // wrapper around DrawThemeBackground() translating flags to NORMAL/HOT/
298 // PUSHED/DISABLED states (and so suitable for drawing anything
300 void DoDrawButtonLike(HTHEME htheme
,
306 // common part of DrawCheckBox(), DrawPushButton() and DrawRadioBitmap()
307 bool DoDrawXPButton(int kind
,
313 wxDECLARE_NO_COPY_CLASS(wxRendererXP
);
316 #endif // wxUSE_UXTHEME
319 // ============================================================================
320 // wxRendererMSWBase implementation
321 // ============================================================================
323 void wxRendererMSWBase::DrawFocusRect(wxWindow
* WXUNUSED(win
),
329 wxCopyRectToRECT(rect
, rc
);
331 ::DrawFocusRect(GetHdcOf(dc
.GetTempHDC()), &rc
);
334 void wxRendererMSWBase::DrawItemSelectionRect(wxWindow
*win
,
340 if ( flags
& wxCONTROL_SELECTED
)
342 if ( flags
& wxCONTROL_FOCUSED
)
344 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
348 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
353 brush
= *wxTRANSPARENT_BRUSH
;
357 dc
.SetPen(*wxTRANSPARENT_PEN
);
358 dc
.DrawRectangle( rect
);
360 if ((flags
& wxCONTROL_FOCUSED
) && (flags
& wxCONTROL_CURRENT
))
361 DrawFocusRect( win
, dc
, rect
, flags
);
365 // ============================================================================
366 // wxRendererNative and wxRendererMSW implementation
367 // ============================================================================
370 wxRendererNative
& wxRendererNative::GetDefault()
373 wxUxThemeEngine
*themeEngine
= wxUxThemeEngine::Get();
374 if ( themeEngine
&& themeEngine
->IsAppThemed() )
375 return wxRendererXP::Get();
376 #endif // wxUSE_UXTHEME
378 return wxRendererMSW::Get();
382 wxRendererNative
& wxRendererMSW::Get()
384 static wxRendererMSW s_rendererMSW
;
386 return s_rendererMSW
;
390 wxRendererMSW::DrawComboBoxDropButton(wxWindow
* WXUNUSED(win
),
396 wxCopyRectToRECT(rect
, r
);
398 int style
= DFCS_SCROLLCOMBOBOX
;
399 if ( flags
& wxCONTROL_DISABLED
)
400 style
|= DFCS_INACTIVE
;
401 if ( flags
& wxCONTROL_PRESSED
)
402 style
|= DFCS_PUSHED
| DFCS_FLAT
;
404 ::DrawFrameControl(GetHdcOf(dc
.GetTempHDC()), &r
, DFC_SCROLL
, style
);
408 wxRendererMSW::DoDrawFrameControl(UINT type
,
410 wxWindow
* WXUNUSED(win
),
416 wxCopyRectToRECT(rect
, r
);
419 if ( flags
& wxCONTROL_CHECKED
)
420 style
|= DFCS_CHECKED
;
421 if ( flags
& wxCONTROL_DISABLED
)
422 style
|= DFCS_INACTIVE
;
423 if ( flags
& wxCONTROL_FLAT
)
425 if ( flags
& wxCONTROL_PRESSED
)
426 style
|= DFCS_PUSHED
;
427 if ( flags
& wxCONTROL_CURRENT
)
430 ::DrawFrameControl(GetHdcOf(dc
.GetTempHDC()), &r
, type
, style
);
434 wxRendererMSW::DrawPushButton(wxWindow
*win
,
436 const wxRect
& rectOrig
,
439 wxRect
rect(rectOrig
);
440 if ( flags
& wxCONTROL_ISDEFAULT
)
442 // DrawFrameControl() doesn't seem to support default buttons so we
443 // have to draw the border ourselves
444 wxDCPenChanger
pen(dc
, *wxBLACK_PEN
);
445 wxDCBrushChanger
brush(dc
, *wxTRANSPARENT_BRUSH
);
446 dc
.DrawRectangle(rect
);
450 DoDrawButton(DFCS_BUTTONPUSH
, win
, dc
, rect
, flags
);
454 wxRendererMSW::DrawTitleBarBitmap(wxWindow
*win
,
457 wxTitleBarButton button
,
463 case wxTITLEBAR_BUTTON_CLOSE
:
464 kind
= DFCS_CAPTIONCLOSE
;
467 case wxTITLEBAR_BUTTON_MAXIMIZE
:
468 kind
= DFCS_CAPTIONMAX
;
471 case wxTITLEBAR_BUTTON_ICONIZE
:
472 kind
= DFCS_CAPTIONMIN
;
475 case wxTITLEBAR_BUTTON_RESTORE
:
476 kind
= DFCS_CAPTIONRESTORE
;
479 case wxTITLEBAR_BUTTON_HELP
:
480 kind
= DFCS_CAPTIONHELP
;
484 wxFAIL_MSG( "unsupported title bar button" );
488 DoDrawFrameControl(DFC_CAPTION
, kind
, win
, dc
, rect
, flags
);
491 wxSize
wxRendererMSW::GetCheckBoxSize(wxWindow
* WXUNUSED(win
))
493 return wxSize(::GetSystemMetrics(SM_CXMENUCHECK
),
494 ::GetSystemMetrics(SM_CYMENUCHECK
));
497 int wxRendererMSW::GetHeaderButtonHeight(wxWindow
* WXUNUSED(win
))
499 // some "reasonable" value returned in case of error, it doesn't really
500 // correspond to anything but it's better than returning 0
501 static const int DEFAULT_HEIGHT
= 20;
504 // create a temporary header window just to get its geometry
505 HWND hwndHeader
= ::CreateWindow(WC_HEADER
, NULL
, 0,
506 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
);
508 return DEFAULT_HEIGHT
;
510 wxON_BLOCK_EXIT1( ::DestroyWindow
, hwndHeader
);
512 // initialize the struct filled with the values by Header_Layout()
513 RECT parentRect
= { 0, 0, 100, 100 };
514 WINDOWPOS wp
= { 0, 0, 0, 0, 0, 0, 0 };
515 HDLAYOUT hdl
= { &parentRect
, &wp
};
517 return Header_Layout(hwndHeader
, &hdl
) ? wp
.cy
: DEFAULT_HEIGHT
;
520 // Uses the theme to draw the border and fill for something like a wxTextCtrl
521 void wxRendererMSW::DrawTextCtrl(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
528 wxUxThemeHandle
hTheme(win
, L
"EDIT");
531 wxUxThemeEngine::Get()->GetThemeColor(hTheme
, EP_EDITTEXT
,
532 ETS_NORMAL
, TMT_FILLCOLOR
, &cref
);
533 fill
= wxRGBToColour(cref
);
536 if ( flags
& wxCONTROL_DISABLED
)
537 etsState
= ETS_DISABLED
;
539 etsState
= ETS_NORMAL
;
541 wxUxThemeEngine::Get()->GetThemeColor(hTheme
, EP_EDITTEXT
,
542 etsState
, TMT_BORDERCOLOR
, &cref
);
543 bdr
= wxRGBToColour(cref
);
548 fill
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
554 dc
.DrawRectangle(rect
);
558 // Draw the equivalent of a wxComboBox
559 void wxRendererMSW::DrawComboBox(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
561 // Draw the main part of the control same as TextCtrl
562 DrawTextCtrl(win
, dc
, rect
, flags
);
564 // Draw the button inside the border, on the right side
567 br
.x
+= br
.width
- br
.height
- 1;
568 br
.width
= br
.height
;
571 DrawComboBoxDropButton(win
, dc
, br
, flags
);
575 void wxRendererMSW::DrawChoice(wxWindow
* win
, wxDC
& dc
,
576 const wxRect
& rect
, int flags
)
578 DrawComboBox(win
, dc
, rect
, flags
);
581 // ============================================================================
582 // wxRendererXP implementation
583 // ============================================================================
588 wxRendererNative
& wxRendererXP::Get()
590 static wxRendererXP s_rendererXP
;
595 // NOTE: There is no guarantee that the button drawn fills the entire rect (XP
596 // default theme, for example), so the caller should have cleared button's
597 // background before this call. This is quite likely a wxMSW-specific thing.
599 wxRendererXP::DrawComboBoxDropButton(wxWindow
* win
,
604 wxUxThemeHandle
hTheme(win
, L
"COMBOBOX");
607 m_rendererNative
.DrawComboBoxDropButton(win
, dc
, rect
, flags
);
612 wxCopyRectToRECT(rect
, r
);
615 if ( flags
& wxCONTROL_PRESSED
)
616 state
= CBXS_PRESSED
;
617 else if ( flags
& wxCONTROL_CURRENT
)
619 else if ( flags
& wxCONTROL_DISABLED
)
620 state
= CBXS_DISABLED
;
624 wxUxThemeEngine::Get()->DrawThemeBackground
627 GetHdcOf(dc
.GetTempHDC()),
637 wxRendererXP::DrawHeaderButton(wxWindow
*win
,
641 wxHeaderSortIconType sortArrow
,
642 wxHeaderButtonParams
* params
)
644 wxUxThemeHandle
hTheme(win
, L
"HEADER");
647 return m_rendererNative
.DrawHeaderButton(win
, dc
, rect
, flags
, sortArrow
, params
);
651 wxCopyRectToRECT(rect
, r
);
654 if ( flags
& wxCONTROL_PRESSED
)
656 else if ( flags
& wxCONTROL_CURRENT
)
660 wxUxThemeEngine::Get()->DrawThemeBackground
663 GetHdcOf(dc
.GetTempHDC()),
670 // NOTE: Using the theme to draw HP_HEADERSORTARROW doesn't do anything.
671 // Why? If this can be fixed then draw the sort arrows using the theme
672 // and then clear those flags before calling DrawHeaderButtonContents.
674 // Add any extras that are specified in flags and params
675 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
680 wxRendererXP::DrawTreeItemButton(wxWindow
*win
,
685 wxUxThemeHandle
hTheme(win
, L
"TREEVIEW");
688 m_rendererNative
.DrawTreeItemButton(win
, dc
, rect
, flags
);
693 wxCopyRectToRECT(rect
, r
);
695 int state
= flags
& wxCONTROL_EXPANDED
? GLPS_OPENED
: GLPS_CLOSED
;
696 wxUxThemeEngine::Get()->DrawThemeBackground
699 GetHdcOf(dc
.GetTempHDC()),
708 wxRendererXP::DoDrawXPButton(int kind
,
714 wxUxThemeHandle
hTheme(win
, L
"BUTTON");
718 DoDrawButtonLike(hTheme
, kind
, dc
, rect
, flags
);
724 wxRendererXP::DoDrawButtonLike(HTHEME htheme
,
731 wxCopyRectToRECT(rect
, r
);
733 // the base state is always 1, whether it is PBS_NORMAL,
734 // {CBS,RBS}_UNCHECKEDNORMAL or CBS_NORMAL
737 // XBS_XXX is followed by XBX_XXXHOT, then XBS_XXXPRESSED and DISABLED
747 // in both RBS_ and CBS_ enums CHECKED elements are offset by 4 from base
748 // (UNCHECKED) ones and MIXED are offset by 4 again as there are all states
749 // from the above enum in between them
750 if ( flags
& wxCONTROL_CHECKED
)
751 state
+= STATES_COUNT
;
752 else if ( flags
& wxCONTROL_UNDETERMINED
)
753 state
+= 2*STATES_COUNT
;
755 if ( flags
& wxCONTROL_DISABLED
)
756 state
+= DISABLED_OFFSET
;
757 else if ( flags
& wxCONTROL_PRESSED
)
758 state
+= PRESSED_OFFSET
;
759 else if ( flags
& wxCONTROL_CURRENT
)
761 // wxCONTROL_ISDEFAULT flag is only valid for push buttons
762 else if ( part
== BP_PUSHBUTTON
&& (flags
& wxCONTROL_ISDEFAULT
) )
763 state
= PBS_DEFAULTED
;
765 wxUxThemeEngine::Get()->DrawThemeBackground
768 GetHdcOf(dc
.GetTempHDC()),
777 wxRendererXP::DrawTitleBarBitmap(wxWindow
*win
,
780 wxTitleBarButton button
,
783 wxUxThemeHandle
hTheme(win
, L
"WINDOW");
786 m_rendererNative
.DrawTitleBarBitmap(win
, dc
, rect
, button
, flags
);
793 case wxTITLEBAR_BUTTON_CLOSE
:
794 part
= WP_CLOSEBUTTON
;
797 case wxTITLEBAR_BUTTON_MAXIMIZE
:
801 case wxTITLEBAR_BUTTON_ICONIZE
:
805 case wxTITLEBAR_BUTTON_RESTORE
:
806 part
= WP_RESTOREBUTTON
;
809 case wxTITLEBAR_BUTTON_HELP
:
810 part
= WP_HELPBUTTON
;
814 wxFAIL_MSG( "unsupported title bar button" );
818 DoDrawButtonLike(hTheme
, part
, dc
, rect
, flags
);
821 // ----------------------------------------------------------------------------
823 // ----------------------------------------------------------------------------
825 // the width of the sash: this is the same as used by Explorer...
826 static const wxCoord SASH_WIDTH
= 4;
828 wxSplitterRenderParams
829 wxRendererXP::GetSplitterParams(const wxWindow
* win
)
831 if ( win
->HasFlag(wxSP_NO_XP_THEME
) )
832 return m_rendererNative
.GetSplitterParams(win
);
834 return wxSplitterRenderParams(SASH_WIDTH
, 0, false);
838 wxRendererXP::DrawSplitterBorder(wxWindow
* win
,
843 if ( win
->HasFlag(wxSP_NO_XP_THEME
) )
845 m_rendererNative
.DrawSplitterBorder(win
, dc
, rect
, flags
);
850 wxRendererXP::DrawSplitterSash(wxWindow
*win
,
854 wxOrientation orient
,
857 if ( !win
->HasFlag(wxSP_NO_XP_THEME
) )
859 dc
.SetPen(*wxTRANSPARENT_PEN
);
860 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
)));
861 if ( orient
== wxVERTICAL
)
863 dc
.DrawRectangle(position
, 0, SASH_WIDTH
, size
.y
);
867 dc
.DrawRectangle(0, position
, size
.x
, SASH_WIDTH
);
873 m_rendererNative
.DrawSplitterSash(win
, dc
, size
, position
, orient
, flags
);
876 #endif // wxUSE_UXTHEME