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 virtual void DrawPushButton(wxWindow
*win
,
213 virtual void DrawChoice(wxWindow
* win
,
218 virtual void DrawComboBox(wxWindow
* win
,
223 virtual void DrawTextCtrl(wxWindow
* win
,
228 virtual void DrawRadioBitmap(wxWindow
* win
,
233 virtual wxSize
GetCheckBoxSize(wxWindow
*win
);
235 virtual int GetHeaderButtonHeight(wxWindow
*win
);
238 wxDECLARE_NO_COPY_CLASS(wxRendererMSW
);
241 // ----------------------------------------------------------------------------
242 // wxRendererXP: wxRendererNative implementation for Windows XP and later
243 // ----------------------------------------------------------------------------
247 class wxRendererXP
: public wxRendererMSWBase
250 wxRendererXP() : wxRendererMSWBase(wxRendererMSW::Get()) { }
252 static wxRendererNative
& Get();
254 virtual int DrawHeaderButton(wxWindow
*win
,
258 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
,
259 wxHeaderButtonParams
* params
= NULL
);
261 virtual void DrawTreeItemButton(wxWindow
*win
,
265 virtual void DrawSplitterBorder(wxWindow
*win
,
269 virtual void DrawSplitterSash(wxWindow
*win
,
273 wxOrientation orient
,
275 virtual void DrawComboBoxDropButton(wxWindow
*win
,
279 virtual void DrawCheckBox(wxWindow
*win
,
284 virtual void DrawPushButton(wxWindow
*win
,
289 virtual wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
291 wxDECLARE_NO_COPY_CLASS(wxRendererXP
);
294 #endif // wxUSE_UXTHEME
297 // ============================================================================
298 // wxRendererMSWBase implementation
299 // ============================================================================
301 void wxRendererMSWBase::DrawFocusRect(wxWindow
* WXUNUSED(win
),
307 wxCopyRectToRECT(rect
, rc
);
309 ::DrawFocusRect(GraphicsHDC(&dc
), &rc
);
312 void wxRendererMSWBase::DrawItemSelectionRect(wxWindow
*win
,
318 if ( flags
& wxCONTROL_SELECTED
)
320 if ( flags
& wxCONTROL_FOCUSED
)
322 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
326 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
331 brush
= *wxTRANSPARENT_BRUSH
;
335 dc
.SetPen(*wxTRANSPARENT_PEN
);
336 dc
.DrawRectangle( rect
);
338 if ((flags
& wxCONTROL_FOCUSED
) && (flags
& wxCONTROL_CURRENT
))
339 DrawFocusRect( win
, dc
, rect
, flags
);
343 // ============================================================================
344 // wxRendererNative and wxRendererMSW implementation
345 // ============================================================================
348 wxRendererNative
& wxRendererNative::GetDefault()
351 wxUxThemeEngine
*themeEngine
= wxUxThemeEngine::Get();
352 if ( themeEngine
&& themeEngine
->IsAppThemed() )
353 return wxRendererXP::Get();
354 #endif // wxUSE_UXTHEME
356 return wxRendererMSW::Get();
360 wxRendererNative
& wxRendererMSW::Get()
362 static wxRendererMSW s_rendererMSW
;
364 return s_rendererMSW
;
368 wxRendererMSW::DrawComboBoxDropButton(wxWindow
* WXUNUSED(win
),
374 wxCopyRectToRECT(rect
, r
);
376 int style
= DFCS_SCROLLCOMBOBOX
;
377 if ( flags
& wxCONTROL_DISABLED
)
378 style
|= DFCS_INACTIVE
;
379 if ( flags
& wxCONTROL_PRESSED
)
380 style
|= DFCS_PUSHED
| DFCS_FLAT
;
382 ::DrawFrameControl(GraphicsHDC(&dc
), &r
, DFC_SCROLL
, style
);
386 wxRendererMSW::DrawCheckBox(wxWindow
* WXUNUSED(win
),
392 wxCopyRectToRECT(rect
, r
);
394 int style
= DFCS_BUTTONCHECK
;
395 if ( flags
& wxCONTROL_CHECKED
)
396 style
|= DFCS_CHECKED
;
397 if ( flags
& wxCONTROL_DISABLED
)
398 style
|= DFCS_INACTIVE
;
399 if ( flags
& wxCONTROL_FLAT
)
401 if ( flags
& wxCONTROL_PRESSED
)
402 style
|= DFCS_PUSHED
;
403 if ( flags
& wxCONTROL_CURRENT
)
406 ::DrawFrameControl(GraphicsHDC(&dc
), &r
, DFC_BUTTON
, style
);
410 wxRendererMSW::DrawPushButton(wxWindow
* WXUNUSED(win
),
412 const wxRect
& rectOrig
,
415 wxRect
rect(rectOrig
);
417 int style
= DFCS_BUTTONPUSH
;
418 if ( flags
& wxCONTROL_DISABLED
)
419 style
|= DFCS_INACTIVE
;
420 if ( flags
& wxCONTROL_PRESSED
)
421 style
|= DFCS_PUSHED
| DFCS_FLAT
;
422 if ( flags
& wxCONTROL_ISDEFAULT
)
424 // DrawFrameControl() doesn't seem to support default buttons so we
425 // have to draw the border ourselves
426 wxDCPenChanger
pen(dc
, *wxBLACK_PEN
);
427 wxDCBrushChanger
brush(dc
, *wxTRANSPARENT_BRUSH
);
428 dc
.DrawRectangle(rect
);
433 wxCopyRectToRECT(rect
, rc
);
435 ::DrawFrameControl(GraphicsHDC(&dc
), &rc
, DFC_BUTTON
, style
);
438 wxSize
wxRendererMSW::GetCheckBoxSize(wxWindow
* WXUNUSED(win
))
440 return wxSize(::GetSystemMetrics(SM_CXMENUCHECK
),
441 ::GetSystemMetrics(SM_CYMENUCHECK
));
444 int wxRendererMSW::GetHeaderButtonHeight(wxWindow
* WXUNUSED(win
))
446 // some "reasonable" value returned in case of error, it doesn't really
447 // correspond to anything but it's better than returning 0
448 static const int DEFAULT_HEIGHT
= 20;
451 // create a temporary header window just to get its geometry
452 HWND hwndHeader
= ::CreateWindow(WC_HEADER
, NULL
, 0,
453 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
);
455 return DEFAULT_HEIGHT
;
457 wxON_BLOCK_EXIT1( ::DestroyWindow
, hwndHeader
);
459 // initialize the struct filled with the values by Header_Layout()
460 RECT parentRect
= { 0, 0, 100, 100 };
461 WINDOWPOS wp
= { 0, 0, 0, 0, 0, 0, 0 };
462 HDLAYOUT hdl
= { &parentRect
, &wp
};
464 return Header_Layout(hwndHeader
, &hdl
) ? wp
.cy
: DEFAULT_HEIGHT
;
467 // Uses the theme to draw the border and fill for something like a wxTextCtrl
468 void wxRendererMSW::DrawTextCtrl(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
475 wxUxThemeHandle
hTheme(win
, L
"EDIT");
478 wxUxThemeEngine::Get()->GetThemeColor(hTheme
, EP_EDITTEXT
,
479 ETS_NORMAL
, TMT_FILLCOLOR
, &cref
);
480 fill
= wxRGBToColour(cref
);
483 if ( flags
& wxCONTROL_DISABLED
)
484 etsState
= ETS_DISABLED
;
486 etsState
= ETS_NORMAL
;
488 wxUxThemeEngine::Get()->GetThemeColor(hTheme
, EP_EDITTEXT
,
489 etsState
, TMT_BORDERCOLOR
, &cref
);
490 bdr
= wxRGBToColour(cref
);
495 fill
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
501 dc
.DrawRectangle(rect
);
505 // Draw the equivallent of a wxComboBox
506 void wxRendererMSW::DrawComboBox(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
508 // Draw the main part of the control same as TextCtrl
509 DrawTextCtrl(win
, dc
, rect
, flags
);
511 // Draw the button inside the border, on the right side
514 br
.x
+= br
.width
- br
.height
- 1;
515 br
.width
= br
.height
;
518 DrawComboBoxDropButton(win
, dc
, br
, flags
);
522 void wxRendererMSW::DrawChoice(wxWindow
* win
, wxDC
& dc
,
523 const wxRect
& rect
, int flags
)
525 DrawComboBox(win
, dc
, rect
, flags
);
529 // Draw a themed radio button
530 void wxRendererMSW::DrawRadioBitmap(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
533 wxUxThemeHandle
hTheme(win
, L
"BUTTON");
537 // ??? m_rendererNative.DrawRadioButton(win, dc, rect, flags);
543 wxCopyRectToRECT(rect
, r
);
546 if ( flags
& wxCONTROL_CHECKED
)
547 state
= RBS_CHECKEDNORMAL
;
548 else if ( flags
& wxCONTROL_UNDETERMINED
)
549 state
= RBS_MIXEDNORMAL
;
551 state
= RBS_UNCHECKEDNORMAL
;
553 // RBS_XXX is followed by RBX_XXXGOT, then RBS_XXXPRESSED and DISABLED
554 if ( flags
& wxCONTROL_CURRENT
)
556 else if ( flags
& wxCONTROL_PRESSED
)
558 else if ( flags
& wxCONTROL_DISABLED
)
561 wxUxThemeEngine::Get()->DrawThemeBackground
573 // ============================================================================
574 // wxRendererXP implementation
575 // ============================================================================
580 wxRendererNative
& wxRendererXP::Get()
582 static wxRendererXP s_rendererXP
;
587 // NOTE: There is no guarantee that the button drawn fills the entire rect (XP
588 // default theme, for example), so the caller should have cleared button's
589 // background before this call. This is quite likely a wxMSW-specific thing.
591 wxRendererXP::DrawComboBoxDropButton(wxWindow
* win
,
596 wxUxThemeHandle
hTheme(win
, L
"COMBOBOX");
599 m_rendererNative
.DrawComboBoxDropButton(win
, dc
, rect
, flags
);
604 wxCopyRectToRECT(rect
, r
);
607 if ( flags
& wxCONTROL_PRESSED
)
608 state
= CBXS_PRESSED
;
609 else if ( flags
& wxCONTROL_CURRENT
)
611 else if ( flags
& wxCONTROL_DISABLED
)
612 state
= CBXS_DISABLED
;
616 wxUxThemeEngine::Get()->DrawThemeBackground
619 GetHdcOf(*((wxMSWDCImpl
*)dc
.GetImpl())),
629 wxRendererXP::DrawHeaderButton(wxWindow
*win
,
633 wxHeaderSortIconType sortArrow
,
634 wxHeaderButtonParams
* params
)
636 wxUxThemeHandle
hTheme(win
, L
"HEADER");
639 return m_rendererNative
.DrawHeaderButton(win
, dc
, rect
, flags
, sortArrow
, params
);
643 wxCopyRectToRECT(rect
, r
);
646 if ( flags
& wxCONTROL_PRESSED
)
648 else if ( flags
& wxCONTROL_CURRENT
)
652 wxUxThemeEngine::Get()->DrawThemeBackground
655 GetHdcOf(*((wxMSWDCImpl
*)dc
.GetImpl())),
662 // NOTE: Using the theme to draw HP_HEADERSORTARROW doesn't do anything.
663 // Why? If this can be fixed then draw the sort arrows using the theme
664 // and then clear those flags before calling DrawHeaderButtonContents.
666 // Add any extras that are specified in flags and params
667 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
672 wxRendererXP::DrawTreeItemButton(wxWindow
*win
,
677 wxUxThemeHandle
hTheme(win
, L
"TREEVIEW");
680 m_rendererNative
.DrawTreeItemButton(win
, dc
, rect
, flags
);
685 wxCopyRectToRECT(rect
, r
);
687 int state
= flags
& wxCONTROL_EXPANDED
? GLPS_OPENED
: GLPS_CLOSED
;
688 wxUxThemeEngine::Get()->DrawThemeBackground
691 GetHdcOf(*((wxMSWDCImpl
*)dc
.GetImpl())),
702 wxRendererXP::DrawCheckBox(wxWindow
*win
,
707 wxUxThemeHandle
hTheme(win
, L
"BUTTON");
710 m_rendererNative
.DrawCheckBox(win
, dc
, rect
, flags
);
715 wxCopyRectToRECT(rect
, r
);
718 if ( flags
& wxCONTROL_CHECKED
)
719 state
= CBS_CHECKEDNORMAL
;
720 else if ( flags
& wxCONTROL_UNDETERMINED
)
721 state
= CBS_MIXEDNORMAL
;
723 state
= CBS_UNCHECKEDNORMAL
;
725 // CBS_XXX is followed by CBX_XXXHOT, then CBS_XXXPRESSED and DISABLED
729 CBS_PRESSED_OFFSET
= 2,
730 CBS_DISABLED_OFFSET
= 3
733 if ( flags
& wxCONTROL_DISABLED
)
734 state
+= CBS_DISABLED_OFFSET
;
735 else if ( flags
& wxCONTROL_PRESSED
)
736 state
+= CBS_PRESSED_OFFSET
;
737 else if ( flags
& wxCONTROL_CURRENT
)
738 state
+= CBS_HOT_OFFSET
;
740 wxUxThemeEngine::Get()->DrawThemeBackground
743 GetHdcOf(*((wxMSWDCImpl
*)dc
.GetImpl())),
752 wxRendererXP::DrawPushButton(wxWindow
* win
,
757 wxUxThemeHandle
hTheme(win
, L
"BUTTON");
760 m_rendererNative
.DrawPushButton(win
, dc
, rect
, flags
);
765 wxCopyRectToRECT(rect
, r
);
768 if ( flags
& wxCONTROL_PRESSED
)
770 else if ( flags
& wxCONTROL_CURRENT
)
772 else if ( flags
& wxCONTROL_DISABLED
)
773 state
= PBS_DISABLED
;
774 else if ( flags
& wxCONTROL_ISDEFAULT
)
775 state
= PBS_DEFAULTED
;
779 wxUxThemeEngine::Get()->DrawThemeBackground
782 GetHdcOf(*((wxMSWDCImpl
*)dc
.GetImpl())),
791 // ----------------------------------------------------------------------------
793 // ----------------------------------------------------------------------------
795 // the width of the sash: this is the same as used by Explorer...
796 static const wxCoord SASH_WIDTH
= 4;
798 wxSplitterRenderParams
799 wxRendererXP::GetSplitterParams(const wxWindow
* win
)
801 if ( win
->HasFlag(wxSP_NO_XP_THEME
) )
802 return m_rendererNative
.GetSplitterParams(win
);
804 return wxSplitterRenderParams(SASH_WIDTH
, 0, false);
808 wxRendererXP::DrawSplitterBorder(wxWindow
* win
,
813 if ( win
->HasFlag(wxSP_NO_XP_THEME
) )
815 m_rendererNative
.DrawSplitterBorder(win
, dc
, rect
, flags
);
820 wxRendererXP::DrawSplitterSash(wxWindow
*win
,
824 wxOrientation orient
,
827 if ( !win
->HasFlag(wxSP_NO_XP_THEME
) )
829 dc
.SetPen(*wxTRANSPARENT_PEN
);
830 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
)));
831 if ( orient
== wxVERTICAL
)
833 dc
.DrawRectangle(position
, 0, SASH_WIDTH
, size
.y
);
837 dc
.DrawRectangle(0, position
, size
.x
, SASH_WIDTH
);
843 m_rendererNative
.DrawSplitterSash(win
, dc
, size
, position
, orient
, flags
);
846 #endif // wxUSE_UXTHEME