1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/renderer.cpp
3 // Purpose: implementation of wxRendererNative for Windows
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
27 #include "wx/string.h"
28 #include "wx/window.h"
30 #include "wx/settings.h"
33 #include "wx/dcgraph.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
,
137 void DrawChoice(wxWindow
* win
,
142 void DrawComboBox(wxWindow
* win
,
147 virtual void DrawComboBoxDropButton(wxWindow
*win
,
152 virtual void DrawTextCtrl(wxWindow
* win
,
158 // ----------------------------------------------------------------------------
159 // wxRendererMSW: wxRendererNative implementation for "old" Win32 systems
160 // ----------------------------------------------------------------------------
162 class wxRendererMSW
: public wxRendererMSWBase
167 static wxRendererNative
& Get();
169 virtual void DrawComboBoxDropButton(wxWindow
*win
,
174 virtual void DrawCheckBox(wxWindow
*win
,
179 DoDrawButton(DFCS_BUTTONCHECK
, win
, dc
, rect
, flags
);
182 virtual void DrawPushButton(wxWindow
*win
,
187 virtual void DrawTextCtrl(wxWindow
* win
,
192 virtual void DrawRadioBitmap(wxWindow
* win
,
197 DoDrawButton(DFCS_BUTTONRADIO
, win
, dc
, rect
, flags
);
200 virtual void DrawTitleBarBitmap(wxWindow
*win
,
203 wxTitleBarButton button
,
206 virtual wxSize
GetCheckBoxSize(wxWindow
*win
);
208 virtual int GetHeaderButtonHeight(wxWindow
*win
);
210 virtual int GetHeaderButtonMargin(wxWindow
*win
);
213 // wrapper of DrawFrameControl()
214 void DoDrawFrameControl(UINT type
,
221 // common part of Draw{PushButton,CheckBox,RadioBitmap}(): wraps
222 // DrawFrameControl(DFC_BUTTON)
223 void DoDrawButton(UINT kind
,
229 DoDrawFrameControl(DFC_BUTTON
, kind
, win
, dc
, rect
, flags
);
232 wxDECLARE_NO_COPY_CLASS(wxRendererMSW
);
235 // ----------------------------------------------------------------------------
236 // wxRendererXP: wxRendererNative implementation for Windows XP and later
237 // ----------------------------------------------------------------------------
241 class wxRendererXP
: public wxRendererMSWBase
244 wxRendererXP() : wxRendererMSWBase(wxRendererMSW::Get()) { }
246 static wxRendererNative
& Get();
248 virtual int DrawHeaderButton(wxWindow
*win
,
252 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
,
253 wxHeaderButtonParams
* params
= NULL
);
255 virtual void DrawTreeItemButton(wxWindow
*win
,
259 virtual void DrawSplitterBorder(wxWindow
*win
,
263 virtual void DrawSplitterSash(wxWindow
*win
,
267 wxOrientation orient
,
269 virtual void DrawComboBoxDropButton(wxWindow
*win
,
273 virtual void DrawCheckBox(wxWindow
*win
,
278 if ( !DoDrawXPButton(BP_CHECKBOX
, win
, dc
, rect
, flags
) )
279 m_rendererNative
.DrawCheckBox(win
, dc
, rect
, flags
);
282 virtual void DrawPushButton(wxWindow
*win
,
287 if ( !DoDrawXPButton(BP_PUSHBUTTON
, win
, dc
, rect
, flags
) )
288 m_rendererNative
.DrawPushButton(win
, dc
, rect
, flags
);
291 virtual void DrawTextCtrl(wxWindow
* win
,
296 virtual void DrawRadioBitmap(wxWindow
*win
,
301 if ( !DoDrawXPButton(BP_RADIOBUTTON
, win
, dc
, rect
, flags
) )
302 m_rendererNative
.DrawRadioBitmap(win
, dc
, rect
, flags
);
305 virtual void DrawTitleBarBitmap(wxWindow
*win
,
308 wxTitleBarButton button
,
311 virtual wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
314 // wrapper around DrawThemeBackground() translating flags to NORMAL/HOT/
315 // PUSHED/DISABLED states (and so suitable for drawing anything
317 void DoDrawButtonLike(HTHEME htheme
,
323 // common part of DrawCheckBox(), DrawPushButton() and DrawRadioBitmap()
324 bool DoDrawXPButton(int kind
,
330 wxDECLARE_NO_COPY_CLASS(wxRendererXP
);
333 #endif // wxUSE_UXTHEME
336 // ============================================================================
337 // wxRendererMSWBase implementation
338 // ============================================================================
340 void wxRendererMSWBase::DrawFocusRect(wxWindow
* WXUNUSED(win
),
346 wxCopyRectToRECT(rect
, rc
);
348 ::DrawFocusRect(GetHdcOf(dc
.GetTempHDC()), &rc
);
351 void wxRendererMSWBase::DrawItemSelectionRect(wxWindow
*win
,
357 if ( flags
& wxCONTROL_SELECTED
)
359 if ( flags
& wxCONTROL_FOCUSED
)
361 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
365 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
370 brush
= *wxTRANSPARENT_BRUSH
;
374 dc
.SetPen(*wxTRANSPARENT_PEN
);
375 dc
.DrawRectangle( rect
);
377 if ((flags
& wxCONTROL_FOCUSED
) && (flags
& wxCONTROL_CURRENT
))
378 DrawFocusRect( win
, dc
, rect
, flags
);
381 void wxRendererMSWBase::DrawChoice(wxWindow
* win
,
386 DrawComboBox(win
, dc
, rect
, flags
);
389 void wxRendererMSWBase::DrawComboBox(wxWindow
* win
,
394 // Draw the main part of the control same as TextCtrl
395 DrawTextCtrl(win
, dc
, rect
, flags
);
397 // Draw the button inside the border, on the right side
400 br
.x
+= br
.width
- br
.height
- 1;
401 br
.width
= br
.height
;
404 DrawComboBoxDropButton(win
, dc
, br
, flags
);
407 // ============================================================================
408 // wxRendererNative and wxRendererMSW implementation
409 // ============================================================================
412 wxRendererNative
& wxRendererNative::GetDefault()
415 wxUxThemeEngine
*themeEngine
= wxUxThemeEngine::Get();
416 if ( themeEngine
&& themeEngine
->IsAppThemed() )
417 return wxRendererXP::Get();
418 #endif // wxUSE_UXTHEME
420 return wxRendererMSW::Get();
424 wxRendererNative
& wxRendererMSW::Get()
426 static wxRendererMSW s_rendererMSW
;
428 return s_rendererMSW
;
432 wxRendererMSW::DrawComboBoxDropButton(wxWindow
* WXUNUSED(win
),
437 wxCHECK_RET( dc
.GetImpl(), wxT("Invalid wxDC") );
439 wxRect adjustedRect
= dc
.GetImpl()->MSWApplyGDIPlusTransform(rect
);
442 wxCopyRectToRECT(adjustedRect
, r
);
444 int style
= DFCS_SCROLLCOMBOBOX
;
445 if ( flags
& wxCONTROL_DISABLED
)
446 style
|= DFCS_INACTIVE
;
447 if ( flags
& wxCONTROL_PRESSED
)
448 style
|= DFCS_PUSHED
| DFCS_FLAT
;
450 ::DrawFrameControl(GetHdcOf(dc
.GetTempHDC()), &r
, DFC_SCROLL
, style
);
454 wxRendererMSW::DoDrawFrameControl(UINT type
,
456 wxWindow
* WXUNUSED(win
),
461 wxCHECK_RET( dc
.GetImpl(), wxT("Invalid wxDC") );
463 wxRect adjustedRect
= dc
.GetImpl()->MSWApplyGDIPlusTransform(rect
);
466 wxCopyRectToRECT(adjustedRect
, r
);
469 if ( flags
& wxCONTROL_CHECKED
)
470 style
|= DFCS_CHECKED
;
471 if ( flags
& wxCONTROL_DISABLED
)
472 style
|= DFCS_INACTIVE
;
473 if ( flags
& wxCONTROL_FLAT
)
475 if ( flags
& wxCONTROL_PRESSED
)
476 style
|= DFCS_PUSHED
;
477 if ( flags
& wxCONTROL_CURRENT
)
479 if ( flags
& wxCONTROL_UNDETERMINED
)
480 // Using DFCS_BUTTON3STATE here doesn't work (as might be expected),
481 // use the following two styles to get the same look of a check box
482 // in the undetermined state.
483 style
|= DFCS_INACTIVE
| DFCS_CHECKED
;
485 ::DrawFrameControl(GetHdcOf(dc
.GetTempHDC()), &r
, type
, style
);
489 wxRendererMSW::DrawPushButton(wxWindow
*win
,
491 const wxRect
& rectOrig
,
494 wxRect
rect(rectOrig
);
495 if ( flags
& wxCONTROL_ISDEFAULT
)
497 // DrawFrameControl() doesn't seem to support default buttons so we
498 // have to draw the border ourselves
499 wxDCPenChanger
pen(dc
, *wxBLACK_PEN
);
500 wxDCBrushChanger
brush(dc
, *wxTRANSPARENT_BRUSH
);
501 dc
.DrawRectangle(rect
);
505 DoDrawButton(DFCS_BUTTONPUSH
, win
, dc
, rect
, flags
);
509 wxRendererMSW::DrawTitleBarBitmap(wxWindow
*win
,
512 wxTitleBarButton button
,
518 case wxTITLEBAR_BUTTON_CLOSE
:
519 kind
= DFCS_CAPTIONCLOSE
;
522 case wxTITLEBAR_BUTTON_MAXIMIZE
:
523 kind
= DFCS_CAPTIONMAX
;
526 case wxTITLEBAR_BUTTON_ICONIZE
:
527 kind
= DFCS_CAPTIONMIN
;
530 case wxTITLEBAR_BUTTON_RESTORE
:
531 kind
= DFCS_CAPTIONRESTORE
;
534 case wxTITLEBAR_BUTTON_HELP
:
535 kind
= DFCS_CAPTIONHELP
;
539 wxFAIL_MSG( "unsupported title bar button" );
543 DoDrawFrameControl(DFC_CAPTION
, kind
, win
, dc
, rect
, flags
);
546 wxSize
wxRendererMSW::GetCheckBoxSize(wxWindow
* WXUNUSED(win
))
548 return wxSize(::GetSystemMetrics(SM_CXMENUCHECK
),
549 ::GetSystemMetrics(SM_CYMENUCHECK
));
552 int wxRendererMSW::GetHeaderButtonHeight(wxWindow
* WXUNUSED(win
))
554 // some "reasonable" value returned in case of error, it doesn't really
555 // correspond to anything but it's better than returning 0
556 static const int DEFAULT_HEIGHT
= 20;
559 // create a temporary header window just to get its geometry
560 HWND hwndHeader
= ::CreateWindow(WC_HEADER
, NULL
, 0,
561 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
);
563 return DEFAULT_HEIGHT
;
565 wxON_BLOCK_EXIT1( ::DestroyWindow
, hwndHeader
);
567 // initialize the struct filled with the values by Header_Layout()
568 RECT parentRect
= { 0, 0, 100, 100 };
569 WINDOWPOS wp
= { 0, 0, 0, 0, 0, 0, 0 };
570 HDLAYOUT hdl
= { &parentRect
, &wp
};
572 return Header_Layout(hwndHeader
, &hdl
) ? wp
.cy
: DEFAULT_HEIGHT
;
575 int wxRendererMSW::GetHeaderButtonMargin(wxWindow
*WXUNUSED(win
))
580 // Uses the theme to draw the border and fill for something like a wxTextCtrl
581 void wxRendererMSW::DrawTextCtrl(wxWindow
* WXUNUSED(win
),
589 fill
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
595 dc
.DrawRectangle(rect
);
599 // ============================================================================
600 // wxRendererXP implementation
601 // ============================================================================
606 wxRendererNative
& wxRendererXP::Get()
608 static wxRendererXP s_rendererXP
;
613 // NOTE: There is no guarantee that the button drawn fills the entire rect (XP
614 // default theme, for example), so the caller should have cleared button's
615 // background before this call. This is quite likely a wxMSW-specific thing.
617 wxRendererXP::DrawComboBoxDropButton(wxWindow
* win
,
622 wxUxThemeHandle
hTheme(win
, L
"COMBOBOX");
625 m_rendererNative
.DrawComboBoxDropButton(win
, dc
, rect
, flags
);
629 wxCHECK_RET( dc
.GetImpl(), wxT("Invalid wxDC") );
631 wxRect adjustedRect
= dc
.GetImpl()->MSWApplyGDIPlusTransform(rect
);
634 wxCopyRectToRECT(adjustedRect
, r
);
637 if ( flags
& wxCONTROL_PRESSED
)
638 state
= CBXS_PRESSED
;
639 else if ( flags
& wxCONTROL_CURRENT
)
641 else if ( flags
& wxCONTROL_DISABLED
)
642 state
= CBXS_DISABLED
;
646 wxUxThemeEngine::Get()->DrawThemeBackground
649 GetHdcOf(dc
.GetTempHDC()),
659 wxRendererXP::DrawHeaderButton(wxWindow
*win
,
663 wxHeaderSortIconType sortArrow
,
664 wxHeaderButtonParams
* params
)
666 wxUxThemeHandle
hTheme(win
, L
"HEADER");
669 return m_rendererNative
.DrawHeaderButton(win
, dc
, rect
, flags
, sortArrow
, params
);
672 wxCHECK_MSG( dc
.GetImpl(), -1, wxT("Invalid wxDC") );
674 wxRect adjustedRect
= dc
.GetImpl()->MSWApplyGDIPlusTransform(rect
);
677 wxCopyRectToRECT(adjustedRect
, r
);
680 if ( flags
& wxCONTROL_PRESSED
)
682 else if ( flags
& wxCONTROL_CURRENT
)
686 wxUxThemeEngine::Get()->DrawThemeBackground
689 GetHdcOf(dc
.GetTempHDC()),
696 // NOTE: Using the theme to draw HP_HEADERSORTARROW doesn't do anything.
697 // Why? If this can be fixed then draw the sort arrows using the theme
698 // and then clear those flags before calling DrawHeaderButtonContents.
700 // Add any extras that are specified in flags and params
701 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
706 wxRendererXP::DrawTreeItemButton(wxWindow
*win
,
711 wxUxThemeHandle
hTheme(win
, L
"TREEVIEW");
714 m_rendererNative
.DrawTreeItemButton(win
, dc
, rect
, flags
);
718 wxCHECK_RET( dc
.GetImpl(), wxT("Invalid wxDC") );
720 wxRect adjustedRect
= dc
.GetImpl()->MSWApplyGDIPlusTransform(rect
);
723 wxCopyRectToRECT(adjustedRect
, r
);
725 int state
= flags
& wxCONTROL_EXPANDED
? GLPS_OPENED
: GLPS_CLOSED
;
726 wxUxThemeEngine::Get()->DrawThemeBackground
729 GetHdcOf(dc
.GetTempHDC()),
738 wxRendererXP::DoDrawXPButton(int kind
,
744 wxUxThemeHandle
hTheme(win
, L
"BUTTON");
748 DoDrawButtonLike(hTheme
, kind
, dc
, rect
, flags
);
754 wxRendererXP::DoDrawButtonLike(HTHEME htheme
,
760 wxCHECK_RET( dc
.GetImpl(), wxT("Invalid wxDC") );
762 wxRect adjustedRect
= dc
.GetImpl()->MSWApplyGDIPlusTransform(rect
);
765 wxCopyRectToRECT(adjustedRect
, r
);
767 // the base state is always 1, whether it is PBS_NORMAL,
768 // {CBS,RBS}_UNCHECKEDNORMAL or CBS_NORMAL
771 // XBS_XXX is followed by XBX_XXXHOT, then XBS_XXXPRESSED and DISABLED
781 // in both RBS_ and CBS_ enums CHECKED elements are offset by 4 from base
782 // (UNCHECKED) ones and MIXED are offset by 4 again as there are all states
783 // from the above enum in between them
784 if ( flags
& wxCONTROL_CHECKED
)
785 state
+= STATES_COUNT
;
786 else if ( flags
& wxCONTROL_UNDETERMINED
)
787 state
+= 2*STATES_COUNT
;
789 if ( flags
& wxCONTROL_DISABLED
)
790 state
+= DISABLED_OFFSET
;
791 else if ( flags
& wxCONTROL_PRESSED
)
792 state
+= PRESSED_OFFSET
;
793 else if ( flags
& wxCONTROL_CURRENT
)
795 // wxCONTROL_ISDEFAULT flag is only valid for push buttons
796 else if ( part
== BP_PUSHBUTTON
&& (flags
& wxCONTROL_ISDEFAULT
) )
797 state
= PBS_DEFAULTED
;
799 wxUxThemeEngine::Get()->DrawThemeBackground
802 GetHdcOf(dc
.GetTempHDC()),
811 wxRendererXP::DrawTitleBarBitmap(wxWindow
*win
,
814 wxTitleBarButton button
,
817 wxUxThemeHandle
hTheme(win
, L
"WINDOW");
820 m_rendererNative
.DrawTitleBarBitmap(win
, dc
, rect
, button
, flags
);
827 case wxTITLEBAR_BUTTON_CLOSE
:
828 part
= WP_CLOSEBUTTON
;
831 case wxTITLEBAR_BUTTON_MAXIMIZE
:
835 case wxTITLEBAR_BUTTON_ICONIZE
:
839 case wxTITLEBAR_BUTTON_RESTORE
:
840 part
= WP_RESTOREBUTTON
;
843 case wxTITLEBAR_BUTTON_HELP
:
844 part
= WP_HELPBUTTON
;
848 wxFAIL_MSG( "unsupported title bar button" );
852 DoDrawButtonLike(hTheme
, part
, dc
, rect
, flags
);
855 // Uses the theme to draw the border and fill for something like a wxTextCtrl
856 void wxRendererXP::DrawTextCtrl(wxWindow
* win
,
861 wxUxThemeHandle
hTheme(win
, L
"EDIT");
864 m_rendererNative
.DrawTextCtrl(win
,dc
,rect
,flags
);
872 wxUxThemeEngine::Get()->GetThemeColor(hTheme
, EP_EDITTEXT
,
873 ETS_NORMAL
, TMT_FILLCOLOR
, &cref
);
874 fill
= wxRGBToColour(cref
);
877 if ( flags
& wxCONTROL_DISABLED
)
878 etsState
= ETS_DISABLED
;
880 etsState
= ETS_NORMAL
;
882 wxUxThemeEngine::Get()->GetThemeColor(hTheme
, EP_EDITTEXT
,
883 etsState
, TMT_BORDERCOLOR
, &cref
);
884 bdr
= wxRGBToColour(cref
);
888 dc
.DrawRectangle(rect
);
891 // ----------------------------------------------------------------------------
893 // ----------------------------------------------------------------------------
895 // the width of the sash: this is the same as used by Explorer...
896 static const wxCoord SASH_WIDTH
= 4;
898 wxSplitterRenderParams
899 wxRendererXP::GetSplitterParams(const wxWindow
* win
)
901 if ( win
->HasFlag(wxSP_NO_XP_THEME
) )
902 return m_rendererNative
.GetSplitterParams(win
);
904 return wxSplitterRenderParams(SASH_WIDTH
, 0, false);
908 wxRendererXP::DrawSplitterBorder(wxWindow
* win
,
913 if ( win
->HasFlag(wxSP_NO_XP_THEME
) )
915 m_rendererNative
.DrawSplitterBorder(win
, dc
, rect
, flags
);
920 wxRendererXP::DrawSplitterSash(wxWindow
*win
,
924 wxOrientation orient
,
927 if ( !win
->HasFlag(wxSP_NO_XP_THEME
) )
929 dc
.SetPen(*wxTRANSPARENT_PEN
);
930 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
)));
931 if ( orient
== wxVERTICAL
)
933 dc
.DrawRectangle(position
, 0, SASH_WIDTH
, size
.y
);
937 dc
.DrawRectangle(0, position
, size
.x
, SASH_WIDTH
);
943 m_rendererNative
.DrawSplitterSash(win
, dc
, size
, position
, orient
, flags
);
946 #endif // wxUSE_UXTHEME