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/dcgraph.h"
35 #include "wx/scopeguard.h"
36 #include "wx/splitter.h"
37 #include "wx/renderer.h"
38 #include "wx/msw/private.h"
39 #include "wx/msw/uxtheme.h"
41 // tmschema.h is in Win32 Platform SDK and might not be available with earlier
43 #ifndef CP_DROPDOWNBUTTON
44 #define BP_PUSHBUTTON 1
45 #define BP_RADIOBUTTON 2
47 #define RBS_UNCHECKEDNORMAL 1
48 #define RBS_CHECKEDNORMAL (RBS_UNCHECKEDNORMAL + 4)
49 #define RBS_MIXEDNORMAL (RBS_CHECKEDNORMAL + 4)
50 #define CBS_UNCHECKEDNORMAL 1
51 #define CBS_CHECKEDNORMAL (CBS_UNCHECKEDNORMAL + 4)
52 #define CBS_MIXEDNORMAL (CBS_CHECKEDNORMAL + 4)
57 #define PBS_DISABLED 4
58 #define PBS_DEFAULTED 5
60 #define CP_DROPDOWNBUTTON 1
64 #define CBXS_PRESSED 3
65 #define CBXS_DISABLED 4
72 #define HP_HEADERITEM 1
78 #define TMT_HEIGHT 2417
80 #define HP_HEADERSORTARROW 4
81 #define HSAS_SORTEDUP 1
82 #define HSAS_SORTEDDOWN 2
87 #define ETS_SELECTED 3
88 #define ETS_DISABLED 4
90 #define ETS_READONLY 6
92 #define TMT_FILLCOLOR 3802
93 #define TMT_TEXTCOLOR 3803
94 #define TMT_BORDERCOLOR 3801
95 #define TMT_EDGEFILLCOLOR 3808
97 #define WP_MINBUTTON 15
98 #define WP_MAXBUTTON 17
99 #define WP_CLOSEBUTTON 18
100 #define WP_RESTOREBUTTON 21
101 #define WP_HELPBUTTON 23
104 #if defined(__WXWINCE__)
114 #define DFCS_HOT 0x1000
117 // ----------------------------------------------------------------------------
118 // methods common to wxRendererMSW and wxRendererXP
119 // ----------------------------------------------------------------------------
121 class wxRendererMSWBase
: public wxDelegateRendererNative
124 wxRendererMSWBase() { }
125 wxRendererMSWBase(wxRendererNative
& rendererNative
)
126 : wxDelegateRendererNative(rendererNative
) { }
128 void DrawFocusRect(wxWindow
* win
,
133 void DrawItemSelectionRect(wxWindow
*win
,
139 // ----------------------------------------------------------------------------
140 // wxRendererMSW: wxRendererNative implementation for "old" Win32 systems
141 // ----------------------------------------------------------------------------
143 class wxRendererMSW
: public wxRendererMSWBase
148 static wxRendererNative
& Get();
150 virtual void DrawComboBoxDropButton(wxWindow
*win
,
155 virtual void DrawCheckBox(wxWindow
*win
,
160 DoDrawButton(DFCS_BUTTONCHECK
, win
, dc
, rect
, flags
);
163 virtual void DrawPushButton(wxWindow
*win
,
168 virtual void DrawChoice(wxWindow
* win
,
173 virtual void DrawComboBox(wxWindow
* win
,
178 virtual void DrawTextCtrl(wxWindow
* win
,
183 virtual void DrawRadioBitmap(wxWindow
* win
,
188 DoDrawButton(DFCS_BUTTONRADIO
, win
, dc
, rect
, flags
);
191 virtual void DrawTitleBarBitmap(wxWindow
*win
,
194 wxTitleBarButton button
,
197 virtual wxSize
GetCheckBoxSize(wxWindow
*win
);
199 virtual int GetHeaderButtonHeight(wxWindow
*win
);
201 virtual int GetHeaderButtonMargin(wxWindow
*win
);
204 // wrapper of DrawFrameControl()
205 void DoDrawFrameControl(UINT type
,
212 // common part of Draw{PushButton,CheckBox,RadioBitmap}(): wraps
213 // DrawFrameControl(DFC_BUTTON)
214 void DoDrawButton(UINT kind
,
220 DoDrawFrameControl(DFC_BUTTON
, kind
, win
, dc
, rect
, flags
);
223 wxDECLARE_NO_COPY_CLASS(wxRendererMSW
);
226 // ----------------------------------------------------------------------------
227 // wxRendererXP: wxRendererNative implementation for Windows XP and later
228 // ----------------------------------------------------------------------------
232 class wxRendererXP
: public wxRendererMSWBase
235 wxRendererXP() : wxRendererMSWBase(wxRendererMSW::Get()) { }
237 static wxRendererNative
& Get();
239 virtual int DrawHeaderButton(wxWindow
*win
,
243 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
,
244 wxHeaderButtonParams
* params
= NULL
);
246 virtual void DrawTreeItemButton(wxWindow
*win
,
250 virtual void DrawSplitterBorder(wxWindow
*win
,
254 virtual void DrawSplitterSash(wxWindow
*win
,
258 wxOrientation orient
,
260 virtual void DrawComboBoxDropButton(wxWindow
*win
,
264 virtual void DrawCheckBox(wxWindow
*win
,
269 if ( !DoDrawXPButton(BP_CHECKBOX
, win
, dc
, rect
, flags
) )
270 m_rendererNative
.DrawCheckBox(win
, dc
, rect
, flags
);
273 virtual void DrawPushButton(wxWindow
*win
,
278 if ( !DoDrawXPButton(BP_PUSHBUTTON
, win
, dc
, rect
, flags
) )
279 m_rendererNative
.DrawPushButton(win
, dc
, rect
, flags
);
282 virtual void DrawRadioBitmap(wxWindow
*win
,
287 if ( !DoDrawXPButton(BP_RADIOBUTTON
, win
, dc
, rect
, flags
) )
288 m_rendererNative
.DrawRadioBitmap(win
, dc
, rect
, flags
);
291 virtual void DrawTitleBarBitmap(wxWindow
*win
,
294 wxTitleBarButton button
,
297 virtual wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
300 // wrapper around DrawThemeBackground() translating flags to NORMAL/HOT/
301 // PUSHED/DISABLED states (and so suitable for drawing anything
303 void DoDrawButtonLike(HTHEME htheme
,
309 // common part of DrawCheckBox(), DrawPushButton() and DrawRadioBitmap()
310 bool DoDrawXPButton(int kind
,
316 wxDECLARE_NO_COPY_CLASS(wxRendererXP
);
319 #endif // wxUSE_UXTHEME
322 // ============================================================================
323 // wxRendererMSWBase implementation
324 // ============================================================================
326 void wxRendererMSWBase::DrawFocusRect(wxWindow
* WXUNUSED(win
),
332 wxCopyRectToRECT(rect
, rc
);
334 ::DrawFocusRect(GetHdcOf(dc
.GetTempHDC()), &rc
);
337 void wxRendererMSWBase::DrawItemSelectionRect(wxWindow
*win
,
343 if ( flags
& wxCONTROL_SELECTED
)
345 if ( flags
& wxCONTROL_FOCUSED
)
347 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
351 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
356 brush
= *wxTRANSPARENT_BRUSH
;
360 dc
.SetPen(*wxTRANSPARENT_PEN
);
361 dc
.DrawRectangle( rect
);
363 if ((flags
& wxCONTROL_FOCUSED
) && (flags
& wxCONTROL_CURRENT
))
364 DrawFocusRect( win
, dc
, rect
, flags
);
368 // ============================================================================
369 // wxRendererNative and wxRendererMSW implementation
370 // ============================================================================
373 wxRendererNative
& wxRendererNative::GetDefault()
376 wxUxThemeEngine
*themeEngine
= wxUxThemeEngine::Get();
377 if ( themeEngine
&& themeEngine
->IsAppThemed() )
378 return wxRendererXP::Get();
379 #endif // wxUSE_UXTHEME
381 return wxRendererMSW::Get();
385 wxRendererNative
& wxRendererMSW::Get()
387 static wxRendererMSW s_rendererMSW
;
389 return s_rendererMSW
;
393 wxRendererMSW::DrawComboBoxDropButton(wxWindow
* WXUNUSED(win
),
398 wxCHECK_RET( dc
.GetImpl(), wxT("Invalid wxDC") );
400 wxRect adjustedRect
= dc
.GetImpl()->MSWApplyGDIPlusTransform(rect
);
403 wxCopyRectToRECT(adjustedRect
, r
);
405 int style
= DFCS_SCROLLCOMBOBOX
;
406 if ( flags
& wxCONTROL_DISABLED
)
407 style
|= DFCS_INACTIVE
;
408 if ( flags
& wxCONTROL_PRESSED
)
409 style
|= DFCS_PUSHED
| DFCS_FLAT
;
411 ::DrawFrameControl(GetHdcOf(dc
.GetTempHDC()), &r
, DFC_SCROLL
, style
);
415 wxRendererMSW::DoDrawFrameControl(UINT type
,
417 wxWindow
* WXUNUSED(win
),
422 wxCHECK_RET( dc
.GetImpl(), wxT("Invalid wxDC") );
424 wxRect adjustedRect
= dc
.GetImpl()->MSWApplyGDIPlusTransform(rect
);
427 wxCopyRectToRECT(adjustedRect
, r
);
430 if ( flags
& wxCONTROL_CHECKED
)
431 style
|= DFCS_CHECKED
;
432 if ( flags
& wxCONTROL_DISABLED
)
433 style
|= DFCS_INACTIVE
;
434 if ( flags
& wxCONTROL_FLAT
)
436 if ( flags
& wxCONTROL_PRESSED
)
437 style
|= DFCS_PUSHED
;
438 if ( flags
& wxCONTROL_CURRENT
)
441 ::DrawFrameControl(GetHdcOf(dc
.GetTempHDC()), &r
, type
, style
);
445 wxRendererMSW::DrawPushButton(wxWindow
*win
,
447 const wxRect
& rectOrig
,
450 wxRect
rect(rectOrig
);
451 if ( flags
& wxCONTROL_ISDEFAULT
)
453 // DrawFrameControl() doesn't seem to support default buttons so we
454 // have to draw the border ourselves
455 wxDCPenChanger
pen(dc
, *wxBLACK_PEN
);
456 wxDCBrushChanger
brush(dc
, *wxTRANSPARENT_BRUSH
);
457 dc
.DrawRectangle(rect
);
461 DoDrawButton(DFCS_BUTTONPUSH
, win
, dc
, rect
, flags
);
465 wxRendererMSW::DrawTitleBarBitmap(wxWindow
*win
,
468 wxTitleBarButton button
,
474 case wxTITLEBAR_BUTTON_CLOSE
:
475 kind
= DFCS_CAPTIONCLOSE
;
478 case wxTITLEBAR_BUTTON_MAXIMIZE
:
479 kind
= DFCS_CAPTIONMAX
;
482 case wxTITLEBAR_BUTTON_ICONIZE
:
483 kind
= DFCS_CAPTIONMIN
;
486 case wxTITLEBAR_BUTTON_RESTORE
:
487 kind
= DFCS_CAPTIONRESTORE
;
490 case wxTITLEBAR_BUTTON_HELP
:
491 kind
= DFCS_CAPTIONHELP
;
495 wxFAIL_MSG( "unsupported title bar button" );
499 DoDrawFrameControl(DFC_CAPTION
, kind
, win
, dc
, rect
, flags
);
502 wxSize
wxRendererMSW::GetCheckBoxSize(wxWindow
* WXUNUSED(win
))
504 return wxSize(::GetSystemMetrics(SM_CXMENUCHECK
),
505 ::GetSystemMetrics(SM_CYMENUCHECK
));
508 int wxRendererMSW::GetHeaderButtonHeight(wxWindow
* WXUNUSED(win
))
510 // some "reasonable" value returned in case of error, it doesn't really
511 // correspond to anything but it's better than returning 0
512 static const int DEFAULT_HEIGHT
= 20;
515 // create a temporary header window just to get its geometry
516 HWND hwndHeader
= ::CreateWindow(WC_HEADER
, NULL
, 0,
517 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
);
519 return DEFAULT_HEIGHT
;
521 wxON_BLOCK_EXIT1( ::DestroyWindow
, hwndHeader
);
523 // initialize the struct filled with the values by Header_Layout()
524 RECT parentRect
= { 0, 0, 100, 100 };
525 WINDOWPOS wp
= { 0, 0, 0, 0, 0, 0, 0 };
526 HDLAYOUT hdl
= { &parentRect
, &wp
};
528 return Header_Layout(hwndHeader
, &hdl
) ? wp
.cy
: DEFAULT_HEIGHT
;
531 int wxRendererMSW::GetHeaderButtonMargin(wxWindow
*WXUNUSED(win
))
536 // Uses the theme to draw the border and fill for something like a wxTextCtrl
537 void wxRendererMSW::DrawTextCtrl(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
544 wxUxThemeHandle
hTheme(win
, L
"EDIT");
547 wxUxThemeEngine::Get()->GetThemeColor(hTheme
, EP_EDITTEXT
,
548 ETS_NORMAL
, TMT_FILLCOLOR
, &cref
);
549 fill
= wxRGBToColour(cref
);
552 if ( flags
& wxCONTROL_DISABLED
)
553 etsState
= ETS_DISABLED
;
555 etsState
= ETS_NORMAL
;
557 wxUxThemeEngine::Get()->GetThemeColor(hTheme
, EP_EDITTEXT
,
558 etsState
, TMT_BORDERCOLOR
, &cref
);
559 bdr
= wxRGBToColour(cref
);
564 fill
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
570 dc
.DrawRectangle(rect
);
574 // Draw the equivalent of a wxComboBox
575 void wxRendererMSW::DrawComboBox(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
577 // Draw the main part of the control same as TextCtrl
578 DrawTextCtrl(win
, dc
, rect
, flags
);
580 // Draw the button inside the border, on the right side
583 br
.x
+= br
.width
- br
.height
- 1;
584 br
.width
= br
.height
;
587 DrawComboBoxDropButton(win
, dc
, br
, flags
);
591 void wxRendererMSW::DrawChoice(wxWindow
* win
, wxDC
& dc
,
592 const wxRect
& rect
, int flags
)
594 DrawComboBox(win
, dc
, rect
, flags
);
597 // ============================================================================
598 // wxRendererXP implementation
599 // ============================================================================
604 wxRendererNative
& wxRendererXP::Get()
606 static wxRendererXP s_rendererXP
;
611 // NOTE: There is no guarantee that the button drawn fills the entire rect (XP
612 // default theme, for example), so the caller should have cleared button's
613 // background before this call. This is quite likely a wxMSW-specific thing.
615 wxRendererXP::DrawComboBoxDropButton(wxWindow
* win
,
620 wxUxThemeHandle
hTheme(win
, L
"COMBOBOX");
623 m_rendererNative
.DrawComboBoxDropButton(win
, dc
, rect
, flags
);
627 wxCHECK_RET( dc
.GetImpl(), wxT("Invalid wxDC") );
629 wxRect adjustedRect
= dc
.GetImpl()->MSWApplyGDIPlusTransform(rect
);
632 wxCopyRectToRECT(adjustedRect
, r
);
635 if ( flags
& wxCONTROL_PRESSED
)
636 state
= CBXS_PRESSED
;
637 else if ( flags
& wxCONTROL_CURRENT
)
639 else if ( flags
& wxCONTROL_DISABLED
)
640 state
= CBXS_DISABLED
;
644 wxUxThemeEngine::Get()->DrawThemeBackground
647 GetHdcOf(dc
.GetTempHDC()),
657 wxRendererXP::DrawHeaderButton(wxWindow
*win
,
661 wxHeaderSortIconType sortArrow
,
662 wxHeaderButtonParams
* params
)
664 wxUxThemeHandle
hTheme(win
, L
"HEADER");
667 return m_rendererNative
.DrawHeaderButton(win
, dc
, rect
, flags
, sortArrow
, params
);
670 wxCHECK_MSG( dc
.GetImpl(), -1, wxT("Invalid wxDC") );
672 wxRect adjustedRect
= dc
.GetImpl()->MSWApplyGDIPlusTransform(rect
);
675 wxCopyRectToRECT(adjustedRect
, r
);
678 if ( flags
& wxCONTROL_PRESSED
)
680 else if ( flags
& wxCONTROL_CURRENT
)
684 wxUxThemeEngine::Get()->DrawThemeBackground
687 GetHdcOf(dc
.GetTempHDC()),
694 // NOTE: Using the theme to draw HP_HEADERSORTARROW doesn't do anything.
695 // Why? If this can be fixed then draw the sort arrows using the theme
696 // and then clear those flags before calling DrawHeaderButtonContents.
698 // Add any extras that are specified in flags and params
699 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
704 wxRendererXP::DrawTreeItemButton(wxWindow
*win
,
709 wxUxThemeHandle
hTheme(win
, L
"TREEVIEW");
712 m_rendererNative
.DrawTreeItemButton(win
, dc
, rect
, flags
);
716 wxCHECK_RET( dc
.GetImpl(), wxT("Invalid wxDC") );
718 wxRect adjustedRect
= dc
.GetImpl()->MSWApplyGDIPlusTransform(rect
);
721 wxCopyRectToRECT(adjustedRect
, r
);
723 int state
= flags
& wxCONTROL_EXPANDED
? GLPS_OPENED
: GLPS_CLOSED
;
724 wxUxThemeEngine::Get()->DrawThemeBackground
727 GetHdcOf(dc
.GetTempHDC()),
736 wxRendererXP::DoDrawXPButton(int kind
,
742 wxUxThemeHandle
hTheme(win
, L
"BUTTON");
746 DoDrawButtonLike(hTheme
, kind
, dc
, rect
, flags
);
752 wxRendererXP::DoDrawButtonLike(HTHEME htheme
,
758 wxCHECK_RET( dc
.GetImpl(), wxT("Invalid wxDC") );
760 wxRect adjustedRect
= dc
.GetImpl()->MSWApplyGDIPlusTransform(rect
);
763 wxCopyRectToRECT(adjustedRect
, r
);
765 // the base state is always 1, whether it is PBS_NORMAL,
766 // {CBS,RBS}_UNCHECKEDNORMAL or CBS_NORMAL
769 // XBS_XXX is followed by XBX_XXXHOT, then XBS_XXXPRESSED and DISABLED
779 // in both RBS_ and CBS_ enums CHECKED elements are offset by 4 from base
780 // (UNCHECKED) ones and MIXED are offset by 4 again as there are all states
781 // from the above enum in between them
782 if ( flags
& wxCONTROL_CHECKED
)
783 state
+= STATES_COUNT
;
784 else if ( flags
& wxCONTROL_UNDETERMINED
)
785 state
+= 2*STATES_COUNT
;
787 if ( flags
& wxCONTROL_DISABLED
)
788 state
+= DISABLED_OFFSET
;
789 else if ( flags
& wxCONTROL_PRESSED
)
790 state
+= PRESSED_OFFSET
;
791 else if ( flags
& wxCONTROL_CURRENT
)
793 // wxCONTROL_ISDEFAULT flag is only valid for push buttons
794 else if ( part
== BP_PUSHBUTTON
&& (flags
& wxCONTROL_ISDEFAULT
) )
795 state
= PBS_DEFAULTED
;
797 wxUxThemeEngine::Get()->DrawThemeBackground
800 GetHdcOf(dc
.GetTempHDC()),
809 wxRendererXP::DrawTitleBarBitmap(wxWindow
*win
,
812 wxTitleBarButton button
,
815 wxUxThemeHandle
hTheme(win
, L
"WINDOW");
818 m_rendererNative
.DrawTitleBarBitmap(win
, dc
, rect
, button
, flags
);
825 case wxTITLEBAR_BUTTON_CLOSE
:
826 part
= WP_CLOSEBUTTON
;
829 case wxTITLEBAR_BUTTON_MAXIMIZE
:
833 case wxTITLEBAR_BUTTON_ICONIZE
:
837 case wxTITLEBAR_BUTTON_RESTORE
:
838 part
= WP_RESTOREBUTTON
;
841 case wxTITLEBAR_BUTTON_HELP
:
842 part
= WP_HELPBUTTON
;
846 wxFAIL_MSG( "unsupported title bar button" );
850 DoDrawButtonLike(hTheme
, part
, dc
, rect
, flags
);
853 // ----------------------------------------------------------------------------
855 // ----------------------------------------------------------------------------
857 // the width of the sash: this is the same as used by Explorer...
858 static const wxCoord SASH_WIDTH
= 4;
860 wxSplitterRenderParams
861 wxRendererXP::GetSplitterParams(const wxWindow
* win
)
863 if ( win
->HasFlag(wxSP_NO_XP_THEME
) )
864 return m_rendererNative
.GetSplitterParams(win
);
866 return wxSplitterRenderParams(SASH_WIDTH
, 0, false);
870 wxRendererXP::DrawSplitterBorder(wxWindow
* win
,
875 if ( win
->HasFlag(wxSP_NO_XP_THEME
) )
877 m_rendererNative
.DrawSplitterBorder(win
, dc
, rect
, flags
);
882 wxRendererXP::DrawSplitterSash(wxWindow
*win
,
886 wxOrientation orient
,
889 if ( !win
->HasFlag(wxSP_NO_XP_THEME
) )
891 dc
.SetPen(*wxTRANSPARENT_PEN
);
892 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
)));
893 if ( orient
== wxVERTICAL
)
895 dc
.DrawRectangle(position
, 0, SASH_WIDTH
, size
.y
);
899 dc
.DrawRectangle(0, position
, size
.x
, SASH_WIDTH
);
905 m_rendererNative
.DrawSplitterSash(win
, dc
, size
, position
, orient
, flags
);
908 #endif // wxUSE_UXTHEME