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 if ( !DoDrawXPButton(BP_CHECKBOX
, win
, dc
, rect
, flags
) )
299 m_rendererNative
.DrawCheckBox(win
, dc
, rect
, flags
);
302 virtual void DrawPushButton(wxWindow
*win
,
307 if ( !DoDrawXPButton(BP_PUSHBUTTON
, win
, dc
, rect
, flags
) )
308 m_rendererNative
.DrawPushButton(win
, dc
, rect
, flags
);
311 virtual void DrawRadioBitmap(wxWindow
*win
,
316 if ( !DoDrawXPButton(BP_RADIOBUTTON
, win
, dc
, rect
, flags
) )
317 m_rendererNative
.DrawRadioBitmap(win
, dc
, rect
, flags
);
320 virtual wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
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(GraphicsHDC(&dc
), &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
);
382 // ============================================================================
383 // wxRendererNative and wxRendererMSW implementation
384 // ============================================================================
387 wxRendererNative
& wxRendererNative::GetDefault()
390 wxUxThemeEngine
*themeEngine
= wxUxThemeEngine::Get();
391 if ( themeEngine
&& themeEngine
->IsAppThemed() )
392 return wxRendererXP::Get();
393 #endif // wxUSE_UXTHEME
395 return wxRendererMSW::Get();
399 wxRendererNative
& wxRendererMSW::Get()
401 static wxRendererMSW s_rendererMSW
;
403 return s_rendererMSW
;
407 wxRendererMSW::DrawComboBoxDropButton(wxWindow
* WXUNUSED(win
),
413 wxCopyRectToRECT(rect
, r
);
415 int style
= DFCS_SCROLLCOMBOBOX
;
416 if ( flags
& wxCONTROL_DISABLED
)
417 style
|= DFCS_INACTIVE
;
418 if ( flags
& wxCONTROL_PRESSED
)
419 style
|= DFCS_PUSHED
| DFCS_FLAT
;
421 ::DrawFrameControl(GraphicsHDC(&dc
), &r
, DFC_SCROLL
, style
);
425 wxRendererMSW::DoDrawButton(UINT kind
,
426 wxWindow
* WXUNUSED(win
),
432 wxCopyRectToRECT(rect
, r
);
435 if ( flags
& wxCONTROL_CHECKED
)
436 style
|= DFCS_CHECKED
;
437 if ( flags
& wxCONTROL_DISABLED
)
438 style
|= DFCS_INACTIVE
;
439 if ( flags
& wxCONTROL_FLAT
)
441 if ( flags
& wxCONTROL_PRESSED
)
442 style
|= DFCS_PUSHED
;
443 if ( flags
& wxCONTROL_CURRENT
)
446 ::DrawFrameControl(GraphicsHDC(&dc
), &r
, DFC_BUTTON
, 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
);
469 wxSize
wxRendererMSW::GetCheckBoxSize(wxWindow
* WXUNUSED(win
))
471 return wxSize(::GetSystemMetrics(SM_CXMENUCHECK
),
472 ::GetSystemMetrics(SM_CYMENUCHECK
));
475 int wxRendererMSW::GetHeaderButtonHeight(wxWindow
* WXUNUSED(win
))
477 // some "reasonable" value returned in case of error, it doesn't really
478 // correspond to anything but it's better than returning 0
479 static const int DEFAULT_HEIGHT
= 20;
482 // create a temporary header window just to get its geometry
483 HWND hwndHeader
= ::CreateWindow(WC_HEADER
, NULL
, 0,
484 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
);
486 return DEFAULT_HEIGHT
;
488 wxON_BLOCK_EXIT1( ::DestroyWindow
, hwndHeader
);
490 // initialize the struct filled with the values by Header_Layout()
491 RECT parentRect
= { 0, 0, 100, 100 };
492 WINDOWPOS wp
= { 0, 0, 0, 0, 0, 0, 0 };
493 HDLAYOUT hdl
= { &parentRect
, &wp
};
495 return Header_Layout(hwndHeader
, &hdl
) ? wp
.cy
: DEFAULT_HEIGHT
;
498 // Uses the theme to draw the border and fill for something like a wxTextCtrl
499 void wxRendererMSW::DrawTextCtrl(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
506 wxUxThemeHandle
hTheme(win
, L
"EDIT");
509 wxUxThemeEngine::Get()->GetThemeColor(hTheme
, EP_EDITTEXT
,
510 ETS_NORMAL
, TMT_FILLCOLOR
, &cref
);
511 fill
= wxRGBToColour(cref
);
514 if ( flags
& wxCONTROL_DISABLED
)
515 etsState
= ETS_DISABLED
;
517 etsState
= ETS_NORMAL
;
519 wxUxThemeEngine::Get()->GetThemeColor(hTheme
, EP_EDITTEXT
,
520 etsState
, TMT_BORDERCOLOR
, &cref
);
521 bdr
= wxRGBToColour(cref
);
526 fill
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
532 dc
.DrawRectangle(rect
);
536 // Draw the equivalent of a wxComboBox
537 void wxRendererMSW::DrawComboBox(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
539 // Draw the main part of the control same as TextCtrl
540 DrawTextCtrl(win
, dc
, rect
, flags
);
542 // Draw the button inside the border, on the right side
545 br
.x
+= br
.width
- br
.height
- 1;
546 br
.width
= br
.height
;
549 DrawComboBoxDropButton(win
, dc
, br
, flags
);
553 void wxRendererMSW::DrawChoice(wxWindow
* win
, wxDC
& dc
,
554 const wxRect
& rect
, int flags
)
556 DrawComboBox(win
, dc
, rect
, flags
);
559 // ============================================================================
560 // wxRendererXP implementation
561 // ============================================================================
566 wxRendererNative
& wxRendererXP::Get()
568 static wxRendererXP s_rendererXP
;
573 // NOTE: There is no guarantee that the button drawn fills the entire rect (XP
574 // default theme, for example), so the caller should have cleared button's
575 // background before this call. This is quite likely a wxMSW-specific thing.
577 wxRendererXP::DrawComboBoxDropButton(wxWindow
* win
,
582 wxUxThemeHandle
hTheme(win
, L
"COMBOBOX");
585 m_rendererNative
.DrawComboBoxDropButton(win
, dc
, rect
, flags
);
590 wxCopyRectToRECT(rect
, r
);
593 if ( flags
& wxCONTROL_PRESSED
)
594 state
= CBXS_PRESSED
;
595 else if ( flags
& wxCONTROL_CURRENT
)
597 else if ( flags
& wxCONTROL_DISABLED
)
598 state
= CBXS_DISABLED
;
602 wxUxThemeEngine::Get()->DrawThemeBackground
605 GetHdcOf(*((wxMSWDCImpl
*)dc
.GetImpl())),
615 wxRendererXP::DrawHeaderButton(wxWindow
*win
,
619 wxHeaderSortIconType sortArrow
,
620 wxHeaderButtonParams
* params
)
622 wxUxThemeHandle
hTheme(win
, L
"HEADER");
625 return m_rendererNative
.DrawHeaderButton(win
, dc
, rect
, flags
, sortArrow
, params
);
629 wxCopyRectToRECT(rect
, r
);
632 if ( flags
& wxCONTROL_PRESSED
)
634 else if ( flags
& wxCONTROL_CURRENT
)
638 wxUxThemeEngine::Get()->DrawThemeBackground
641 GetHdcOf(*((wxMSWDCImpl
*)dc
.GetImpl())),
648 // NOTE: Using the theme to draw HP_HEADERSORTARROW doesn't do anything.
649 // Why? If this can be fixed then draw the sort arrows using the theme
650 // and then clear those flags before calling DrawHeaderButtonContents.
652 // Add any extras that are specified in flags and params
653 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
658 wxRendererXP::DrawTreeItemButton(wxWindow
*win
,
663 wxUxThemeHandle
hTheme(win
, L
"TREEVIEW");
666 m_rendererNative
.DrawTreeItemButton(win
, dc
, rect
, flags
);
671 wxCopyRectToRECT(rect
, r
);
673 int state
= flags
& wxCONTROL_EXPANDED
? GLPS_OPENED
: GLPS_CLOSED
;
674 wxUxThemeEngine::Get()->DrawThemeBackground
677 GetHdcOf(*((wxMSWDCImpl
*)dc
.GetImpl())),
686 wxRendererXP::DoDrawXPButton(int kind
,
692 wxUxThemeHandle
hTheme(win
, L
"BUTTON");
697 wxCopyRectToRECT(rect
, r
);
699 // determine the base state depending on the button kind
708 state
= RBS_UNCHECKEDNORMAL
;
712 state
= CBS_UNCHECKEDNORMAL
;
716 wxFAIL_MSG( "unknown button kind" );
720 // XBS_XXX is followed by XBX_XXXHOT, then XBS_XXXPRESSED and DISABLED
730 // in both RBS_ and CBS_ enums CHECKED elements are offset by 4 from base
731 // (UNCHECKED) ones and MIXED are offset by 4 again as there are all states
732 // from the above enum in between them
733 if ( flags
& wxCONTROL_CHECKED
)
734 state
+= STATES_COUNT
;
735 else if ( flags
& wxCONTROL_UNDETERMINED
)
736 state
+= 2*STATES_COUNT
;
738 if ( flags
& wxCONTROL_DISABLED
)
739 state
+= DISABLED_OFFSET
;
740 else if ( flags
& wxCONTROL_PRESSED
)
741 state
+= PRESSED_OFFSET
;
742 else if ( flags
& wxCONTROL_CURRENT
)
744 // wxCONTROL_ISDEFAULT flag is only valid for push buttons
745 else if ( kind
== BP_PUSHBUTTON
&& (flags
& wxCONTROL_ISDEFAULT
) )
746 state
= PBS_DEFAULTED
;
748 wxUxThemeEngine::Get()->DrawThemeBackground
751 GetHdcOf(*((wxMSWDCImpl
*)dc
.GetImpl())),
761 // ----------------------------------------------------------------------------
763 // ----------------------------------------------------------------------------
765 // the width of the sash: this is the same as used by Explorer...
766 static const wxCoord SASH_WIDTH
= 4;
768 wxSplitterRenderParams
769 wxRendererXP::GetSplitterParams(const wxWindow
* win
)
771 if ( win
->HasFlag(wxSP_NO_XP_THEME
) )
772 return m_rendererNative
.GetSplitterParams(win
);
774 return wxSplitterRenderParams(SASH_WIDTH
, 0, false);
778 wxRendererXP::DrawSplitterBorder(wxWindow
* win
,
783 if ( win
->HasFlag(wxSP_NO_XP_THEME
) )
785 m_rendererNative
.DrawSplitterBorder(win
, dc
, rect
, flags
);
790 wxRendererXP::DrawSplitterSash(wxWindow
*win
,
794 wxOrientation orient
,
797 if ( !win
->HasFlag(wxSP_NO_XP_THEME
) )
799 dc
.SetPen(*wxTRANSPARENT_PEN
);
800 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
)));
801 if ( orient
== wxVERTICAL
)
803 dc
.DrawRectangle(position
, 0, SASH_WIDTH
, size
.y
);
807 dc
.DrawRectangle(0, position
, size
.x
, SASH_WIDTH
);
813 m_rendererNative
.DrawSplitterSash(win
, dc
, size
, position
, orient
, flags
);
816 #endif // wxUSE_UXTHEME