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
)
440 if ( flags
& wxCONTROL_UNDETERMINED
)
441 // Using DFCS_BUTTON3STATE here doesn't work (as might be expected),
442 // use the following two styles to get the same look of a check box
443 // in the undetermined state.
444 style
|= DFCS_INACTIVE
| DFCS_CHECKED
;
446 ::DrawFrameControl(GetHdcOf(dc
.GetTempHDC()), &r
, type
, style
);
450 wxRendererMSW::DrawPushButton(wxWindow
*win
,
452 const wxRect
& rectOrig
,
455 wxRect
rect(rectOrig
);
456 if ( flags
& wxCONTROL_ISDEFAULT
)
458 // DrawFrameControl() doesn't seem to support default buttons so we
459 // have to draw the border ourselves
460 wxDCPenChanger
pen(dc
, *wxBLACK_PEN
);
461 wxDCBrushChanger
brush(dc
, *wxTRANSPARENT_BRUSH
);
462 dc
.DrawRectangle(rect
);
466 DoDrawButton(DFCS_BUTTONPUSH
, win
, dc
, rect
, flags
);
470 wxRendererMSW::DrawTitleBarBitmap(wxWindow
*win
,
473 wxTitleBarButton button
,
479 case wxTITLEBAR_BUTTON_CLOSE
:
480 kind
= DFCS_CAPTIONCLOSE
;
483 case wxTITLEBAR_BUTTON_MAXIMIZE
:
484 kind
= DFCS_CAPTIONMAX
;
487 case wxTITLEBAR_BUTTON_ICONIZE
:
488 kind
= DFCS_CAPTIONMIN
;
491 case wxTITLEBAR_BUTTON_RESTORE
:
492 kind
= DFCS_CAPTIONRESTORE
;
495 case wxTITLEBAR_BUTTON_HELP
:
496 kind
= DFCS_CAPTIONHELP
;
500 wxFAIL_MSG( "unsupported title bar button" );
504 DoDrawFrameControl(DFC_CAPTION
, kind
, win
, dc
, rect
, flags
);
507 wxSize
wxRendererMSW::GetCheckBoxSize(wxWindow
* WXUNUSED(win
))
509 return wxSize(::GetSystemMetrics(SM_CXMENUCHECK
),
510 ::GetSystemMetrics(SM_CYMENUCHECK
));
513 int wxRendererMSW::GetHeaderButtonHeight(wxWindow
* WXUNUSED(win
))
515 // some "reasonable" value returned in case of error, it doesn't really
516 // correspond to anything but it's better than returning 0
517 static const int DEFAULT_HEIGHT
= 20;
520 // create a temporary header window just to get its geometry
521 HWND hwndHeader
= ::CreateWindow(WC_HEADER
, NULL
, 0,
522 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
);
524 return DEFAULT_HEIGHT
;
526 wxON_BLOCK_EXIT1( ::DestroyWindow
, hwndHeader
);
528 // initialize the struct filled with the values by Header_Layout()
529 RECT parentRect
= { 0, 0, 100, 100 };
530 WINDOWPOS wp
= { 0, 0, 0, 0, 0, 0, 0 };
531 HDLAYOUT hdl
= { &parentRect
, &wp
};
533 return Header_Layout(hwndHeader
, &hdl
) ? wp
.cy
: DEFAULT_HEIGHT
;
536 int wxRendererMSW::GetHeaderButtonMargin(wxWindow
*WXUNUSED(win
))
541 // Uses the theme to draw the border and fill for something like a wxTextCtrl
542 void wxRendererMSW::DrawTextCtrl(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
549 wxUxThemeHandle
hTheme(win
, L
"EDIT");
552 wxUxThemeEngine::Get()->GetThemeColor(hTheme
, EP_EDITTEXT
,
553 ETS_NORMAL
, TMT_FILLCOLOR
, &cref
);
554 fill
= wxRGBToColour(cref
);
557 if ( flags
& wxCONTROL_DISABLED
)
558 etsState
= ETS_DISABLED
;
560 etsState
= ETS_NORMAL
;
562 wxUxThemeEngine::Get()->GetThemeColor(hTheme
, EP_EDITTEXT
,
563 etsState
, TMT_BORDERCOLOR
, &cref
);
564 bdr
= wxRGBToColour(cref
);
569 fill
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
575 dc
.DrawRectangle(rect
);
579 // Draw the equivalent of a wxComboBox
580 void wxRendererMSW::DrawComboBox(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
582 // Draw the main part of the control same as TextCtrl
583 DrawTextCtrl(win
, dc
, rect
, flags
);
585 // Draw the button inside the border, on the right side
588 br
.x
+= br
.width
- br
.height
- 1;
589 br
.width
= br
.height
;
592 DrawComboBoxDropButton(win
, dc
, br
, flags
);
596 void wxRendererMSW::DrawChoice(wxWindow
* win
, wxDC
& dc
,
597 const wxRect
& rect
, int flags
)
599 DrawComboBox(win
, dc
, rect
, flags
);
602 // ============================================================================
603 // wxRendererXP implementation
604 // ============================================================================
609 wxRendererNative
& wxRendererXP::Get()
611 static wxRendererXP s_rendererXP
;
616 // NOTE: There is no guarantee that the button drawn fills the entire rect (XP
617 // default theme, for example), so the caller should have cleared button's
618 // background before this call. This is quite likely a wxMSW-specific thing.
620 wxRendererXP::DrawComboBoxDropButton(wxWindow
* win
,
625 wxUxThemeHandle
hTheme(win
, L
"COMBOBOX");
628 m_rendererNative
.DrawComboBoxDropButton(win
, dc
, rect
, flags
);
632 wxCHECK_RET( dc
.GetImpl(), wxT("Invalid wxDC") );
634 wxRect adjustedRect
= dc
.GetImpl()->MSWApplyGDIPlusTransform(rect
);
637 wxCopyRectToRECT(adjustedRect
, r
);
640 if ( flags
& wxCONTROL_PRESSED
)
641 state
= CBXS_PRESSED
;
642 else if ( flags
& wxCONTROL_CURRENT
)
644 else if ( flags
& wxCONTROL_DISABLED
)
645 state
= CBXS_DISABLED
;
649 wxUxThemeEngine::Get()->DrawThemeBackground
652 GetHdcOf(dc
.GetTempHDC()),
662 wxRendererXP::DrawHeaderButton(wxWindow
*win
,
666 wxHeaderSortIconType sortArrow
,
667 wxHeaderButtonParams
* params
)
669 wxUxThemeHandle
hTheme(win
, L
"HEADER");
672 return m_rendererNative
.DrawHeaderButton(win
, dc
, rect
, flags
, sortArrow
, params
);
675 wxCHECK_MSG( dc
.GetImpl(), -1, wxT("Invalid wxDC") );
677 wxRect adjustedRect
= dc
.GetImpl()->MSWApplyGDIPlusTransform(rect
);
680 wxCopyRectToRECT(adjustedRect
, r
);
683 if ( flags
& wxCONTROL_PRESSED
)
685 else if ( flags
& wxCONTROL_CURRENT
)
689 wxUxThemeEngine::Get()->DrawThemeBackground
692 GetHdcOf(dc
.GetTempHDC()),
699 // NOTE: Using the theme to draw HP_HEADERSORTARROW doesn't do anything.
700 // Why? If this can be fixed then draw the sort arrows using the theme
701 // and then clear those flags before calling DrawHeaderButtonContents.
703 // Add any extras that are specified in flags and params
704 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
709 wxRendererXP::DrawTreeItemButton(wxWindow
*win
,
714 wxUxThemeHandle
hTheme(win
, L
"TREEVIEW");
717 m_rendererNative
.DrawTreeItemButton(win
, dc
, rect
, flags
);
721 wxCHECK_RET( dc
.GetImpl(), wxT("Invalid wxDC") );
723 wxRect adjustedRect
= dc
.GetImpl()->MSWApplyGDIPlusTransform(rect
);
726 wxCopyRectToRECT(adjustedRect
, r
);
728 int state
= flags
& wxCONTROL_EXPANDED
? GLPS_OPENED
: GLPS_CLOSED
;
729 wxUxThemeEngine::Get()->DrawThemeBackground
732 GetHdcOf(dc
.GetTempHDC()),
741 wxRendererXP::DoDrawXPButton(int kind
,
747 wxUxThemeHandle
hTheme(win
, L
"BUTTON");
751 DoDrawButtonLike(hTheme
, kind
, dc
, rect
, flags
);
757 wxRendererXP::DoDrawButtonLike(HTHEME htheme
,
763 wxCHECK_RET( dc
.GetImpl(), wxT("Invalid wxDC") );
765 wxRect adjustedRect
= dc
.GetImpl()->MSWApplyGDIPlusTransform(rect
);
768 wxCopyRectToRECT(adjustedRect
, r
);
770 // the base state is always 1, whether it is PBS_NORMAL,
771 // {CBS,RBS}_UNCHECKEDNORMAL or CBS_NORMAL
774 // XBS_XXX is followed by XBX_XXXHOT, then XBS_XXXPRESSED and DISABLED
784 // in both RBS_ and CBS_ enums CHECKED elements are offset by 4 from base
785 // (UNCHECKED) ones and MIXED are offset by 4 again as there are all states
786 // from the above enum in between them
787 if ( flags
& wxCONTROL_CHECKED
)
788 state
+= STATES_COUNT
;
789 else if ( flags
& wxCONTROL_UNDETERMINED
)
790 state
+= 2*STATES_COUNT
;
792 if ( flags
& wxCONTROL_DISABLED
)
793 state
+= DISABLED_OFFSET
;
794 else if ( flags
& wxCONTROL_PRESSED
)
795 state
+= PRESSED_OFFSET
;
796 else if ( flags
& wxCONTROL_CURRENT
)
798 // wxCONTROL_ISDEFAULT flag is only valid for push buttons
799 else if ( part
== BP_PUSHBUTTON
&& (flags
& wxCONTROL_ISDEFAULT
) )
800 state
= PBS_DEFAULTED
;
802 wxUxThemeEngine::Get()->DrawThemeBackground
805 GetHdcOf(dc
.GetTempHDC()),
814 wxRendererXP::DrawTitleBarBitmap(wxWindow
*win
,
817 wxTitleBarButton button
,
820 wxUxThemeHandle
hTheme(win
, L
"WINDOW");
823 m_rendererNative
.DrawTitleBarBitmap(win
, dc
, rect
, button
, flags
);
830 case wxTITLEBAR_BUTTON_CLOSE
:
831 part
= WP_CLOSEBUTTON
;
834 case wxTITLEBAR_BUTTON_MAXIMIZE
:
838 case wxTITLEBAR_BUTTON_ICONIZE
:
842 case wxTITLEBAR_BUTTON_RESTORE
:
843 part
= WP_RESTOREBUTTON
;
846 case wxTITLEBAR_BUTTON_HELP
:
847 part
= WP_HELPBUTTON
;
851 wxFAIL_MSG( "unsupported title bar button" );
855 DoDrawButtonLike(hTheme
, part
, dc
, rect
, flags
);
858 // ----------------------------------------------------------------------------
860 // ----------------------------------------------------------------------------
862 // the width of the sash: this is the same as used by Explorer...
863 static const wxCoord SASH_WIDTH
= 4;
865 wxSplitterRenderParams
866 wxRendererXP::GetSplitterParams(const wxWindow
* win
)
868 if ( win
->HasFlag(wxSP_NO_XP_THEME
) )
869 return m_rendererNative
.GetSplitterParams(win
);
871 return wxSplitterRenderParams(SASH_WIDTH
, 0, false);
875 wxRendererXP::DrawSplitterBorder(wxWindow
* win
,
880 if ( win
->HasFlag(wxSP_NO_XP_THEME
) )
882 m_rendererNative
.DrawSplitterBorder(win
, dc
, rect
, flags
);
887 wxRendererXP::DrawSplitterSash(wxWindow
*win
,
891 wxOrientation orient
,
894 if ( !win
->HasFlag(wxSP_NO_XP_THEME
) )
896 dc
.SetPen(*wxTRANSPARENT_PEN
);
897 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
)));
898 if ( orient
== wxVERTICAL
)
900 dc
.DrawRectangle(position
, 0, SASH_WIDTH
, size
.y
);
904 dc
.DrawRectangle(0, position
, size
.x
, SASH_WIDTH
);
910 m_rendererNative
.DrawSplitterSash(win
, dc
, size
, position
, orient
, flags
);
913 #endif // wxUSE_UXTHEME