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 // License: 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/dc.h"
39 #include "wx/msw/uxtheme.h"
41 #if wxUSE_GRAPHICS_CONTEXT
42 // TODO remove this dependency (gdiplus needs the macros)
44 #define max(a,b) (((a) > (b)) ? (a) : (b))
48 #define min(a,b) (((a) < (b)) ? (a) : (b))
51 #include "wx/dcgraph.h"
53 using namespace Gdiplus
;
54 #endif // wxUSE_GRAPHICS_CONTEXT
56 // tmschema.h is in Win32 Platform SDK and might not be available with earlier
58 #ifndef CP_DROPDOWNBUTTON
59 #define BP_PUSHBUTTON 1
60 #define BP_RADIOBUTTON 2
62 #define RBS_UNCHECKEDNORMAL 1
63 #define RBS_CHECKEDNORMAL (RBS_UNCHECKEDNORMAL + 4)
64 #define RBS_MIXEDNORMAL (RBS_CHECKEDNORMAL + 4)
65 #define CBS_UNCHECKEDNORMAL 1
66 #define CBS_CHECKEDNORMAL (CBS_UNCHECKEDNORMAL + 4)
67 #define CBS_MIXEDNORMAL (CBS_CHECKEDNORMAL + 4)
72 #define PBS_DISABLED 4
73 #define PBS_DEFAULTED 5
75 #define CP_DROPDOWNBUTTON 1
79 #define CBXS_PRESSED 3
80 #define CBXS_DISABLED 4
87 #define HP_HEADERITEM 1
93 #define TMT_HEIGHT 2417
95 #define HP_HEADERSORTARROW 4
96 #define HSAS_SORTEDUP 1
97 #define HSAS_SORTEDDOWN 2
102 #define ETS_SELECTED 3
103 #define ETS_DISABLED 4
104 #define ETS_FOCUSED 5
105 #define ETS_READONLY 6
107 #define TMT_FILLCOLOR 3802
108 #define TMT_TEXTCOLOR 3803
109 #define TMT_BORDERCOLOR 3801
110 #define TMT_EDGEFILLCOLOR 3808
114 // ----------------------------------------------------------------------------
115 // If the DC is a wxGCDC then pull out the HDC from the GraphicsContext when
116 // it is needed, and handle the Release when done.
121 GraphicsHDC(wxDC
* dc
)
123 #if wxUSE_GRAPHICS_CONTEXT
125 wxGCDC
* gcdc
= wxDynamicCast(dc
, wxGCDC
);
127 m_graphics
= (Graphics
*)gcdc
->GetGraphicsContext()->GetNativeContext();
128 m_hdc
= m_graphics
->GetHDC();
132 m_hdc
= GetHdcOf(*((wxMSWDCImpl
*)dc
->GetImpl()));
137 #if wxUSE_GRAPHICS_CONTEXT
139 m_graphics
->ReleaseHDC(m_hdc
);
143 operator HDC() const { return m_hdc
; }
147 #if wxUSE_GRAPHICS_CONTEXT
148 Graphics
* m_graphics
;
152 #if defined(__WXWINCE__)
162 #define DFCS_HOT 0x1000
165 // ----------------------------------------------------------------------------
166 // methods common to wxRendererMSW and wxRendererXP
167 // ----------------------------------------------------------------------------
169 class wxRendererMSWBase
: public wxDelegateRendererNative
172 wxRendererMSWBase() { }
173 wxRendererMSWBase(wxRendererNative
& rendererNative
)
174 : wxDelegateRendererNative(rendererNative
) { }
176 void DrawFocusRect(wxWindow
* win
,
181 void DrawItemSelectionRect(wxWindow
*win
,
187 // ----------------------------------------------------------------------------
188 // wxRendererMSW: wxRendererNative implementation for "old" Win32 systems
189 // ----------------------------------------------------------------------------
191 class wxRendererMSW
: public wxRendererMSWBase
196 static wxRendererNative
& Get();
198 virtual void DrawComboBoxDropButton(wxWindow
*win
,
203 virtual void DrawCheckBox(wxWindow
*win
,
208 DoDrawButton(DFCS_BUTTONCHECK
, win
, dc
, rect
, flags
);
211 virtual void DrawPushButton(wxWindow
*win
,
216 virtual void DrawChoice(wxWindow
* win
,
221 virtual void DrawComboBox(wxWindow
* win
,
226 virtual void DrawTextCtrl(wxWindow
* win
,
231 virtual void DrawRadioBitmap(wxWindow
* win
,
236 DoDrawButton(DFCS_BUTTONRADIO
, win
, dc
, rect
, flags
);
239 virtual wxSize
GetCheckBoxSize(wxWindow
*win
);
241 virtual int GetHeaderButtonHeight(wxWindow
*win
);
244 // common part of Draw{PushButton,CheckBox,RadioBitmap}(): wraps
245 // DrawFrameControl(DFC_BUTTON)
246 void DoDrawButton(UINT kind
,
252 wxDECLARE_NO_COPY_CLASS(wxRendererMSW
);
255 // ----------------------------------------------------------------------------
256 // wxRendererXP: wxRendererNative implementation for Windows XP and later
257 // ----------------------------------------------------------------------------
261 class wxRendererXP
: public wxRendererMSWBase
264 wxRendererXP() : wxRendererMSWBase(wxRendererMSW::Get()) { }
266 static wxRendererNative
& Get();
268 virtual int DrawHeaderButton(wxWindow
*win
,
272 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
,
273 wxHeaderButtonParams
* params
= NULL
);
275 virtual void DrawTreeItemButton(wxWindow
*win
,
279 virtual void DrawSplitterBorder(wxWindow
*win
,
283 virtual void DrawSplitterSash(wxWindow
*win
,
287 wxOrientation orient
,
289 virtual void DrawComboBoxDropButton(wxWindow
*win
,
293 virtual void DrawCheckBox(wxWindow
*win
,
298 virtual void DrawPushButton(wxWindow
*win
,
303 virtual void DrawRadioBitmap(wxWindow
*win
,
308 virtual wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
311 wxDECLARE_NO_COPY_CLASS(wxRendererXP
);
314 #endif // wxUSE_UXTHEME
317 // ============================================================================
318 // wxRendererMSWBase implementation
319 // ============================================================================
321 void wxRendererMSWBase::DrawFocusRect(wxWindow
* WXUNUSED(win
),
327 wxCopyRectToRECT(rect
, rc
);
329 ::DrawFocusRect(GraphicsHDC(&dc
), &rc
);
332 void wxRendererMSWBase::DrawItemSelectionRect(wxWindow
*win
,
338 if ( flags
& wxCONTROL_SELECTED
)
340 if ( flags
& wxCONTROL_FOCUSED
)
342 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
346 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
351 brush
= *wxTRANSPARENT_BRUSH
;
355 dc
.SetPen(*wxTRANSPARENT_PEN
);
356 dc
.DrawRectangle( rect
);
358 if ((flags
& wxCONTROL_FOCUSED
) && (flags
& wxCONTROL_CURRENT
))
359 DrawFocusRect( win
, dc
, rect
, flags
);
363 // ============================================================================
364 // wxRendererNative and wxRendererMSW implementation
365 // ============================================================================
368 wxRendererNative
& wxRendererNative::GetDefault()
371 wxUxThemeEngine
*themeEngine
= wxUxThemeEngine::Get();
372 if ( themeEngine
&& themeEngine
->IsAppThemed() )
373 return wxRendererXP::Get();
374 #endif // wxUSE_UXTHEME
376 return wxRendererMSW::Get();
380 wxRendererNative
& wxRendererMSW::Get()
382 static wxRendererMSW s_rendererMSW
;
384 return s_rendererMSW
;
388 wxRendererMSW::DrawComboBoxDropButton(wxWindow
* WXUNUSED(win
),
394 wxCopyRectToRECT(rect
, r
);
396 int style
= DFCS_SCROLLCOMBOBOX
;
397 if ( flags
& wxCONTROL_DISABLED
)
398 style
|= DFCS_INACTIVE
;
399 if ( flags
& wxCONTROL_PRESSED
)
400 style
|= DFCS_PUSHED
| DFCS_FLAT
;
402 ::DrawFrameControl(GraphicsHDC(&dc
), &r
, DFC_SCROLL
, style
);
406 wxRendererMSW::DoDrawButton(UINT kind
,
407 wxWindow
* WXUNUSED(win
),
413 wxCopyRectToRECT(rect
, r
);
416 if ( flags
& wxCONTROL_CHECKED
)
417 style
|= DFCS_CHECKED
;
418 if ( flags
& wxCONTROL_DISABLED
)
419 style
|= DFCS_INACTIVE
;
420 if ( flags
& wxCONTROL_FLAT
)
422 if ( flags
& wxCONTROL_PRESSED
)
423 style
|= DFCS_PUSHED
;
424 if ( flags
& wxCONTROL_CURRENT
)
427 ::DrawFrameControl(GraphicsHDC(&dc
), &r
, DFC_BUTTON
, style
);
431 wxRendererMSW::DrawPushButton(wxWindow
*win
,
433 const wxRect
& rectOrig
,
436 wxRect
rect(rectOrig
);
437 if ( flags
& wxCONTROL_ISDEFAULT
)
439 // DrawFrameControl() doesn't seem to support default buttons so we
440 // have to draw the border ourselves
441 wxDCPenChanger
pen(dc
, *wxBLACK_PEN
);
442 wxDCBrushChanger
brush(dc
, *wxTRANSPARENT_BRUSH
);
443 dc
.DrawRectangle(rect
);
447 DoDrawButton(DFCS_BUTTONPUSH
, win
, dc
, rect
, flags
);
450 wxSize
wxRendererMSW::GetCheckBoxSize(wxWindow
* WXUNUSED(win
))
452 return wxSize(::GetSystemMetrics(SM_CXMENUCHECK
),
453 ::GetSystemMetrics(SM_CYMENUCHECK
));
456 int wxRendererMSW::GetHeaderButtonHeight(wxWindow
* WXUNUSED(win
))
458 // some "reasonable" value returned in case of error, it doesn't really
459 // correspond to anything but it's better than returning 0
460 static const int DEFAULT_HEIGHT
= 20;
463 // create a temporary header window just to get its geometry
464 HWND hwndHeader
= ::CreateWindow(WC_HEADER
, NULL
, 0,
465 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
);
467 return DEFAULT_HEIGHT
;
469 wxON_BLOCK_EXIT1( ::DestroyWindow
, hwndHeader
);
471 // initialize the struct filled with the values by Header_Layout()
472 RECT parentRect
= { 0, 0, 100, 100 };
473 WINDOWPOS wp
= { 0, 0, 0, 0, 0, 0, 0 };
474 HDLAYOUT hdl
= { &parentRect
, &wp
};
476 return Header_Layout(hwndHeader
, &hdl
) ? wp
.cy
: DEFAULT_HEIGHT
;
479 // Uses the theme to draw the border and fill for something like a wxTextCtrl
480 void wxRendererMSW::DrawTextCtrl(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
487 wxUxThemeHandle
hTheme(win
, L
"EDIT");
490 wxUxThemeEngine::Get()->GetThemeColor(hTheme
, EP_EDITTEXT
,
491 ETS_NORMAL
, TMT_FILLCOLOR
, &cref
);
492 fill
= wxRGBToColour(cref
);
495 if ( flags
& wxCONTROL_DISABLED
)
496 etsState
= ETS_DISABLED
;
498 etsState
= ETS_NORMAL
;
500 wxUxThemeEngine::Get()->GetThemeColor(hTheme
, EP_EDITTEXT
,
501 etsState
, TMT_BORDERCOLOR
, &cref
);
502 bdr
= wxRGBToColour(cref
);
507 fill
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
513 dc
.DrawRectangle(rect
);
517 // Draw the equivalent of a wxComboBox
518 void wxRendererMSW::DrawComboBox(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
520 // Draw the main part of the control same as TextCtrl
521 DrawTextCtrl(win
, dc
, rect
, flags
);
523 // Draw the button inside the border, on the right side
526 br
.x
+= br
.width
- br
.height
- 1;
527 br
.width
= br
.height
;
530 DrawComboBoxDropButton(win
, dc
, br
, flags
);
534 void wxRendererMSW::DrawChoice(wxWindow
* win
, wxDC
& dc
,
535 const wxRect
& rect
, int flags
)
537 DrawComboBox(win
, dc
, rect
, flags
);
540 // ============================================================================
541 // wxRendererXP implementation
542 // ============================================================================
547 wxRendererNative
& wxRendererXP::Get()
549 static wxRendererXP s_rendererXP
;
554 // NOTE: There is no guarantee that the button drawn fills the entire rect (XP
555 // default theme, for example), so the caller should have cleared button's
556 // background before this call. This is quite likely a wxMSW-specific thing.
558 wxRendererXP::DrawComboBoxDropButton(wxWindow
* win
,
563 wxUxThemeHandle
hTheme(win
, L
"COMBOBOX");
566 m_rendererNative
.DrawComboBoxDropButton(win
, dc
, rect
, flags
);
571 wxCopyRectToRECT(rect
, r
);
574 if ( flags
& wxCONTROL_PRESSED
)
575 state
= CBXS_PRESSED
;
576 else if ( flags
& wxCONTROL_CURRENT
)
578 else if ( flags
& wxCONTROL_DISABLED
)
579 state
= CBXS_DISABLED
;
583 wxUxThemeEngine::Get()->DrawThemeBackground
586 GetHdcOf(*((wxMSWDCImpl
*)dc
.GetImpl())),
596 wxRendererXP::DrawHeaderButton(wxWindow
*win
,
600 wxHeaderSortIconType sortArrow
,
601 wxHeaderButtonParams
* params
)
603 wxUxThemeHandle
hTheme(win
, L
"HEADER");
606 return m_rendererNative
.DrawHeaderButton(win
, dc
, rect
, flags
, sortArrow
, params
);
610 wxCopyRectToRECT(rect
, r
);
613 if ( flags
& wxCONTROL_PRESSED
)
615 else if ( flags
& wxCONTROL_CURRENT
)
619 wxUxThemeEngine::Get()->DrawThemeBackground
622 GetHdcOf(*((wxMSWDCImpl
*)dc
.GetImpl())),
629 // NOTE: Using the theme to draw HP_HEADERSORTARROW doesn't do anything.
630 // Why? If this can be fixed then draw the sort arrows using the theme
631 // and then clear those flags before calling DrawHeaderButtonContents.
633 // Add any extras that are specified in flags and params
634 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
639 wxRendererXP::DrawTreeItemButton(wxWindow
*win
,
644 wxUxThemeHandle
hTheme(win
, L
"TREEVIEW");
647 m_rendererNative
.DrawTreeItemButton(win
, dc
, rect
, flags
);
652 wxCopyRectToRECT(rect
, r
);
654 int state
= flags
& wxCONTROL_EXPANDED
? GLPS_OPENED
: GLPS_CLOSED
;
655 wxUxThemeEngine::Get()->DrawThemeBackground
658 GetHdcOf(*((wxMSWDCImpl
*)dc
.GetImpl())),
667 wxRendererXP::DrawCheckBox(wxWindow
*win
,
672 wxUxThemeHandle
hTheme(win
, L
"BUTTON");
675 m_rendererNative
.DrawCheckBox(win
, dc
, rect
, flags
);
680 wxCopyRectToRECT(rect
, r
);
683 if ( flags
& wxCONTROL_CHECKED
)
684 state
= CBS_CHECKEDNORMAL
;
685 else if ( flags
& wxCONTROL_UNDETERMINED
)
686 state
= CBS_MIXEDNORMAL
;
688 state
= CBS_UNCHECKEDNORMAL
;
690 // CBS_XXX is followed by CBX_XXXHOT, then CBS_XXXPRESSED and DISABLED
694 CBS_PRESSED_OFFSET
= 2,
695 CBS_DISABLED_OFFSET
= 3
698 if ( flags
& wxCONTROL_DISABLED
)
699 state
+= CBS_DISABLED_OFFSET
;
700 else if ( flags
& wxCONTROL_PRESSED
)
701 state
+= CBS_PRESSED_OFFSET
;
702 else if ( flags
& wxCONTROL_CURRENT
)
703 state
+= CBS_HOT_OFFSET
;
705 wxUxThemeEngine::Get()->DrawThemeBackground
708 GetHdcOf(*((wxMSWDCImpl
*)dc
.GetImpl())),
716 void wxRendererXP::DrawRadioBitmap(wxWindow
* win
,
721 wxUxThemeHandle
hTheme(win
, L
"BUTTON");
724 m_rendererNative
.DrawRadioBitmap(win
, dc
, rect
, flags
);
729 wxCopyRectToRECT(rect
, r
);
732 if ( flags
& wxCONTROL_CHECKED
)
733 state
= RBS_CHECKEDNORMAL
;
734 else if ( flags
& wxCONTROL_UNDETERMINED
)
735 state
= RBS_MIXEDNORMAL
;
737 state
= RBS_UNCHECKEDNORMAL
;
739 // RBS_XXX is followed by RBX_XXXGOT, then RBS_XXXPRESSED and DISABLED
740 if ( flags
& wxCONTROL_CURRENT
)
742 else if ( flags
& wxCONTROL_PRESSED
)
744 else if ( flags
& wxCONTROL_DISABLED
)
747 wxUxThemeEngine::Get()->DrawThemeBackground
759 wxRendererXP::DrawPushButton(wxWindow
* win
,
764 wxUxThemeHandle
hTheme(win
, L
"BUTTON");
767 m_rendererNative
.DrawPushButton(win
, dc
, rect
, flags
);
772 wxCopyRectToRECT(rect
, r
);
775 if ( flags
& wxCONTROL_PRESSED
)
777 else if ( flags
& wxCONTROL_CURRENT
)
779 else if ( flags
& wxCONTROL_DISABLED
)
780 state
= PBS_DISABLED
;
781 else if ( flags
& wxCONTROL_ISDEFAULT
)
782 state
= PBS_DEFAULTED
;
786 wxUxThemeEngine::Get()->DrawThemeBackground
789 GetHdcOf(*((wxMSWDCImpl
*)dc
.GetImpl())),
798 // ----------------------------------------------------------------------------
800 // ----------------------------------------------------------------------------
802 // the width of the sash: this is the same as used by Explorer...
803 static const wxCoord SASH_WIDTH
= 4;
805 wxSplitterRenderParams
806 wxRendererXP::GetSplitterParams(const wxWindow
* win
)
808 if ( win
->HasFlag(wxSP_NO_XP_THEME
) )
809 return m_rendererNative
.GetSplitterParams(win
);
811 return wxSplitterRenderParams(SASH_WIDTH
, 0, false);
815 wxRendererXP::DrawSplitterBorder(wxWindow
* win
,
820 if ( win
->HasFlag(wxSP_NO_XP_THEME
) )
822 m_rendererNative
.DrawSplitterBorder(win
, dc
, rect
, flags
);
827 wxRendererXP::DrawSplitterSash(wxWindow
*win
,
831 wxOrientation orient
,
834 if ( !win
->HasFlag(wxSP_NO_XP_THEME
) )
836 dc
.SetPen(*wxTRANSPARENT_PEN
);
837 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
)));
838 if ( orient
== wxVERTICAL
)
840 dc
.DrawRectangle(position
, 0, SASH_WIDTH
, size
.y
);
844 dc
.DrawRectangle(0, position
, size
.x
, SASH_WIDTH
);
850 m_rendererNative
.DrawSplitterSash(win
, dc
, size
, position
, orient
, flags
);
853 #endif // wxUSE_UXTHEME