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 // wxRendererMSW: wxRendererNative implementation for "old" Win32 systems
167 // ----------------------------------------------------------------------------
169 class WXDLLEXPORT wxRendererMSW
: public wxDelegateRendererNative
174 static wxRendererNative
& Get();
176 virtual void DrawComboBoxDropButton(wxWindow
*win
,
181 virtual void DrawCheckBox(wxWindow
*win
,
186 virtual void DrawPushButton(wxWindow
*win
,
191 virtual void DrawFocusRect(wxWindow
* win
,
196 virtual void DrawChoice(wxWindow
* win
,
201 virtual void DrawComboBox(wxWindow
* win
,
206 virtual void DrawTextCtrl(wxWindow
* win
,
211 virtual void DrawRadioButton(wxWindow
* win
,
216 virtual wxSize
GetCheckBoxSize() const;
218 virtual int GetHeaderButtonHeight(wxWindow
*win
);
221 wxDECLARE_NO_COPY_CLASS(wxRendererMSW
);
224 // ----------------------------------------------------------------------------
225 // wxRendererXP: wxRendererNative implementation for Windows XP and later
226 // ----------------------------------------------------------------------------
230 class WXDLLEXPORT wxRendererXP
: public wxDelegateRendererNative
233 wxRendererXP() : wxDelegateRendererNative(wxRendererMSW::Get()) { }
235 static wxRendererNative
& Get();
237 virtual int DrawHeaderButton(wxWindow
*win
,
241 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
,
242 wxHeaderButtonParams
* params
= NULL
);
244 virtual void DrawTreeItemButton(wxWindow
*win
,
248 virtual void DrawSplitterBorder(wxWindow
*win
,
252 virtual void DrawSplitterSash(wxWindow
*win
,
256 wxOrientation orient
,
258 virtual void DrawComboBoxDropButton(wxWindow
*win
,
262 virtual void DrawCheckBox(wxWindow
*win
,
267 virtual void DrawPushButton(wxWindow
*win
,
272 virtual void DrawItemSelectionRect(wxWindow
*win
,
278 virtual wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
280 wxDECLARE_NO_COPY_CLASS(wxRendererXP
);
283 #endif // wxUSE_UXTHEME
285 // ============================================================================
286 // wxRendererNative and wxRendererMSW implementation
287 // ============================================================================
290 wxRendererNative
& wxRendererNative::GetDefault()
293 wxUxThemeEngine
*themeEngine
= wxUxThemeEngine::Get();
294 if ( themeEngine
&& themeEngine
->IsAppThemed() )
295 return wxRendererXP::Get();
296 #endif // wxUSE_UXTHEME
298 return wxRendererMSW::Get();
302 wxRendererNative
& wxRendererMSW::Get()
304 static wxRendererMSW s_rendererMSW
;
306 return s_rendererMSW
;
310 wxRendererMSW::DrawComboBoxDropButton(wxWindow
* WXUNUSED(win
),
316 wxCopyRectToRECT(rect
, r
);
318 int style
= DFCS_SCROLLCOMBOBOX
;
319 if ( flags
& wxCONTROL_DISABLED
)
320 style
|= DFCS_INACTIVE
;
321 if ( flags
& wxCONTROL_PRESSED
)
322 style
|= DFCS_PUSHED
| DFCS_FLAT
;
324 ::DrawFrameControl(GraphicsHDC(&dc
), &r
, DFC_SCROLL
, style
);
328 wxRendererMSW::DrawCheckBox(wxWindow
* WXUNUSED(win
),
334 wxCopyRectToRECT(rect
, r
);
336 int style
= DFCS_BUTTONCHECK
;
337 if ( flags
& wxCONTROL_CHECKED
)
338 style
|= DFCS_CHECKED
;
339 if ( flags
& wxCONTROL_DISABLED
)
340 style
|= DFCS_INACTIVE
;
341 if ( flags
& wxCONTROL_FLAT
)
343 if ( flags
& wxCONTROL_PRESSED
)
344 style
|= DFCS_PUSHED
;
345 if ( flags
& wxCONTROL_CURRENT
)
348 ::DrawFrameControl(GraphicsHDC(&dc
), &r
, DFC_BUTTON
, style
);
352 wxRendererMSW::DrawPushButton(wxWindow
* WXUNUSED(win
),
354 const wxRect
& rectOrig
,
357 wxRect
rect(rectOrig
);
359 int style
= DFCS_BUTTONPUSH
;
360 if ( flags
& wxCONTROL_DISABLED
)
361 style
|= DFCS_INACTIVE
;
362 if ( flags
& wxCONTROL_PRESSED
)
363 style
|= DFCS_PUSHED
| DFCS_FLAT
;
364 if ( flags
& wxCONTROL_ISDEFAULT
)
366 // DrawFrameControl() doesn't seem to support default buttons so we
367 // have to draw the border ourselves
368 wxDCPenChanger
pen(dc
, *wxBLACK_PEN
);
369 wxDCBrushChanger
brush(dc
, *wxTRANSPARENT_BRUSH
);
370 dc
.DrawRectangle(rect
);
375 wxCopyRectToRECT(rect
, rc
);
377 ::DrawFrameControl(GraphicsHDC(&dc
), &rc
, DFC_BUTTON
, style
);
380 void wxRendererMSW::DrawFocusRect(wxWindow
* WXUNUSED(win
),
386 wxCopyRectToRECT(rect
, rc
);
388 ::DrawFocusRect(GraphicsHDC(&dc
), &rc
);
391 wxSize
wxRendererMSW::GetCheckBoxSize() const
393 return wxSize(::GetSystemMetrics(SM_CXMENUCHECK
),
394 ::GetSystemMetrics(SM_CYMENUCHECK
));
397 int wxRendererMSW::GetHeaderButtonHeight(wxWindow
* WXUNUSED(win
))
399 // some "reasonable" value returned in case of error, it doesn't really
400 // correspond to anything but it's better than returning 0
401 static const int DEFAULT_HEIGHT
= 20;
404 // create a temporary header window just to get its geometry
405 HWND hwndHeader
= ::CreateWindow(WC_HEADER
, NULL
, 0,
406 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
);
408 return DEFAULT_HEIGHT
;
410 wxON_BLOCK_EXIT1( ::DestroyWindow
, hwndHeader
);
412 // initialize the struct filled with the values by Header_Layout()
413 RECT parentRect
= { 0, 0, 100, 100 };
414 WINDOWPOS wp
= { 0, 0, 0, 0, 0, 0, 0 };
415 HDLAYOUT hdl
= { &parentRect
, &wp
};
417 return Header_Layout(hwndHeader
, &hdl
) ? wp
.cy
: DEFAULT_HEIGHT
;
420 // Uses the theme to draw the border and fill for something like a wxTextCtrl
421 void wxRendererMSW::DrawTextCtrl(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
428 wxUxThemeHandle
hTheme(win
, L
"EDIT");
431 wxUxThemeEngine::Get()->GetThemeColor(hTheme
, EP_EDITTEXT
,
432 ETS_NORMAL
, TMT_FILLCOLOR
, &cref
);
433 fill
= wxRGBToColour(cref
);
436 if ( flags
& wxCONTROL_DISABLED
)
437 etsState
= ETS_DISABLED
;
439 etsState
= ETS_NORMAL
;
441 wxUxThemeEngine::Get()->GetThemeColor(hTheme
, EP_EDITTEXT
,
442 etsState
, TMT_BORDERCOLOR
, &cref
);
443 bdr
= wxRGBToColour(cref
);
448 fill
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
454 dc
.DrawRectangle(rect
);
458 // Draw the equivallent of a wxComboBox
459 void wxRendererMSW::DrawComboBox(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
461 // Draw the main part of the control same as TextCtrl
462 DrawTextCtrl(win
, dc
, rect
, flags
);
464 // Draw the button inside the border, on the right side
467 br
.x
+= br
.width
- br
.height
- 1;
468 br
.width
= br
.height
;
471 DrawComboBoxDropButton(win
, dc
, br
, flags
);
475 void wxRendererMSW::DrawChoice(wxWindow
* win
, wxDC
& dc
,
476 const wxRect
& rect
, int flags
)
478 DrawComboBox(win
, dc
, rect
, flags
);
482 // Draw a themed radio button
483 void wxRendererMSW::DrawRadioButton(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
486 wxUxThemeHandle
hTheme(win
, L
"BUTTON");
490 // ??? m_rendererNative.DrawRadioButton(win, dc, rect, flags);
496 wxCopyRectToRECT(rect
, r
);
499 if ( flags
& wxCONTROL_CHECKED
)
500 state
= RBS_CHECKEDNORMAL
;
501 else if ( flags
& wxCONTROL_UNDETERMINED
)
502 state
= RBS_MIXEDNORMAL
;
504 state
= RBS_UNCHECKEDNORMAL
;
506 // RBS_XXX is followed by RBX_XXXGOT, then RBS_XXXPRESSED and DISABLED
507 if ( flags
& wxCONTROL_CURRENT
)
509 else if ( flags
& wxCONTROL_PRESSED
)
511 else if ( flags
& wxCONTROL_DISABLED
)
514 wxUxThemeEngine::Get()->DrawThemeBackground
526 // ============================================================================
527 // wxRendererXP implementation
528 // ============================================================================
533 wxRendererNative
& wxRendererXP::Get()
535 static wxRendererXP s_rendererXP
;
540 // NOTE: There is no guarantee that the button drawn fills the entire rect (XP
541 // default theme, for example), so the caller should have cleared button's
542 // background before this call. This is quite likely a wxMSW-specific thing.
544 wxRendererXP::DrawComboBoxDropButton(wxWindow
* win
,
549 wxUxThemeHandle
hTheme(win
, L
"COMBOBOX");
552 m_rendererNative
.DrawComboBoxDropButton(win
, dc
, rect
, flags
);
557 wxCopyRectToRECT(rect
, r
);
560 if ( flags
& wxCONTROL_PRESSED
)
561 state
= CBXS_PRESSED
;
562 else if ( flags
& wxCONTROL_CURRENT
)
564 else if ( flags
& wxCONTROL_DISABLED
)
565 state
= CBXS_DISABLED
;
569 wxUxThemeEngine::Get()->DrawThemeBackground
572 GetHdcOf(*((wxMSWDCImpl
*)dc
.GetImpl())),
582 wxRendererXP::DrawHeaderButton(wxWindow
*win
,
586 wxHeaderSortIconType sortArrow
,
587 wxHeaderButtonParams
* params
)
589 wxUxThemeHandle
hTheme(win
, L
"HEADER");
592 return m_rendererNative
.DrawHeaderButton(win
, dc
, rect
, flags
, sortArrow
, params
);
596 wxCopyRectToRECT(rect
, r
);
599 if ( flags
& wxCONTROL_PRESSED
)
601 else if ( flags
& wxCONTROL_CURRENT
)
605 wxUxThemeEngine::Get()->DrawThemeBackground
608 GetHdcOf(*((wxMSWDCImpl
*)dc
.GetImpl())),
615 // NOTE: Using the theme to draw HP_HEADERSORTARROW doesn't do anything.
616 // Why? If this can be fixed then draw the sort arrows using the theme
617 // and then clear those flags before calling DrawHeaderButtonContents.
619 // Add any extras that are specified in flags and params
620 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
625 wxRendererXP::DrawTreeItemButton(wxWindow
*win
,
630 wxUxThemeHandle
hTheme(win
, L
"TREEVIEW");
633 m_rendererNative
.DrawTreeItemButton(win
, dc
, rect
, flags
);
638 wxCopyRectToRECT(rect
, r
);
640 int state
= flags
& wxCONTROL_EXPANDED
? GLPS_OPENED
: GLPS_CLOSED
;
641 wxUxThemeEngine::Get()->DrawThemeBackground
644 GetHdcOf(*((wxMSWDCImpl
*)dc
.GetImpl())),
655 wxRendererXP::DrawCheckBox(wxWindow
*win
,
660 wxUxThemeHandle
hTheme(win
, L
"BUTTON");
663 m_rendererNative
.DrawCheckBox(win
, dc
, rect
, flags
);
668 wxCopyRectToRECT(rect
, r
);
671 if ( flags
& wxCONTROL_CHECKED
)
672 state
= CBS_CHECKEDNORMAL
;
673 else if ( flags
& wxCONTROL_UNDETERMINED
)
674 state
= CBS_MIXEDNORMAL
;
676 state
= CBS_UNCHECKEDNORMAL
;
678 // CBS_XXX is followed by CBX_XXXHOT, then CBS_XXXPRESSED and DISABLED
682 CBS_PRESSED_OFFSET
= 2,
683 CBS_DISABLED_OFFSET
= 3
686 if ( flags
& wxCONTROL_DISABLED
)
687 state
+= CBS_DISABLED_OFFSET
;
688 else if ( flags
& wxCONTROL_PRESSED
)
689 state
+= CBS_PRESSED_OFFSET
;
690 else if ( flags
& wxCONTROL_CURRENT
)
691 state
+= CBS_HOT_OFFSET
;
693 wxUxThemeEngine::Get()->DrawThemeBackground
696 GetHdcOf(*((wxMSWDCImpl
*)dc
.GetImpl())),
705 wxRendererXP::DrawPushButton(wxWindow
* win
,
710 wxUxThemeHandle
hTheme(win
, L
"BUTTON");
713 m_rendererNative
.DrawPushButton(win
, dc
, rect
, flags
);
718 wxCopyRectToRECT(rect
, r
);
721 if ( flags
& wxCONTROL_PRESSED
)
723 else if ( flags
& wxCONTROL_CURRENT
)
725 else if ( flags
& wxCONTROL_DISABLED
)
726 state
= PBS_DISABLED
;
727 else if ( flags
& wxCONTROL_ISDEFAULT
)
728 state
= PBS_DEFAULTED
;
732 wxUxThemeEngine::Get()->DrawThemeBackground
735 GetHdcOf(*((wxMSWDCImpl
*)dc
.GetImpl())),
744 wxRendererXP::DrawItemSelectionRect(wxWindow
*win
,
750 if ( flags
& wxCONTROL_SELECTED
)
752 if ( flags
& wxCONTROL_FOCUSED
)
754 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
758 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
763 brush
= *wxTRANSPARENT_BRUSH
;
767 dc
.SetPen(*wxTRANSPARENT_PEN
);
768 dc
.DrawRectangle( rect
);
770 if ((flags
& wxCONTROL_FOCUSED
) && (flags
& wxCONTROL_CURRENT
))
771 DrawFocusRect( win
, dc
, rect
, flags
);
776 // ----------------------------------------------------------------------------
778 // ----------------------------------------------------------------------------
780 // the width of the sash: this is the same as used by Explorer...
781 static const wxCoord SASH_WIDTH
= 4;
783 wxSplitterRenderParams
784 wxRendererXP::GetSplitterParams(const wxWindow
* win
)
786 if ( win
->HasFlag(wxSP_NO_XP_THEME
) )
787 return m_rendererNative
.GetSplitterParams(win
);
789 return wxSplitterRenderParams(SASH_WIDTH
, 0, false);
793 wxRendererXP::DrawSplitterBorder(wxWindow
* win
,
798 if ( win
->HasFlag(wxSP_NO_XP_THEME
) )
800 m_rendererNative
.DrawSplitterBorder(win
, dc
, rect
, flags
);
805 wxRendererXP::DrawSplitterSash(wxWindow
*win
,
809 wxOrientation orient
,
812 if ( !win
->HasFlag(wxSP_NO_XP_THEME
) )
814 dc
.SetPen(*wxTRANSPARENT_PEN
);
815 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
)));
816 if ( orient
== wxVERTICAL
)
818 dc
.DrawRectangle(position
, 0, SASH_WIDTH
, size
.y
);
822 dc
.DrawRectangle(0, position
, size
.x
, SASH_WIDTH
);
828 m_rendererNative
.DrawSplitterSash(win
, dc
, size
, position
, orient
, flags
);
831 #endif // wxUSE_UXTHEME