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/uxtheme.h"
40 // tmschema.h is in Win32 Platform SDK and might not be available with earlier
42 #ifndef CP_DROPDOWNBUTTON
43 #define BP_PUSHBUTTON 1
44 #define BP_RADIOBUTTON 2
46 #define RBS_UNCHECKEDNORMAL 1
47 #define RBS_CHECKEDNORMAL (RBS_UNCHECKEDNORMAL + 4)
48 #define RBS_MIXEDNORMAL (RBS_CHECKEDNORMAL + 4)
49 #define CBS_UNCHECKEDNORMAL 1
50 #define CBS_CHECKEDNORMAL (CBS_UNCHECKEDNORMAL + 4)
51 #define CBS_MIXEDNORMAL (CBS_CHECKEDNORMAL + 4)
56 #define PBS_DISABLED 4
57 #define PBS_DEFAULTED 5
59 #define CP_DROPDOWNBUTTON 1
63 #define CBXS_PRESSED 3
64 #define CBXS_DISABLED 4
71 #define HP_HEADERITEM 1
77 #define TMT_HEIGHT 2417
79 #define HP_HEADERSORTARROW 4
80 #define HSAS_SORTEDUP 1
81 #define HSAS_SORTEDDOWN 2
86 #define ETS_SELECTED 3
87 #define ETS_DISABLED 4
89 #define ETS_READONLY 6
91 #define TMT_FILLCOLOR 3802
92 #define TMT_TEXTCOLOR 3803
93 #define TMT_BORDERCOLOR 3801
94 #define TMT_EDGEFILLCOLOR 3808
97 #if defined(__WXWINCE__)
107 #define DFCS_HOT 0x1000
110 // ----------------------------------------------------------------------------
111 // methods common to wxRendererMSW and wxRendererXP
112 // ----------------------------------------------------------------------------
114 class wxRendererMSWBase
: public wxDelegateRendererNative
117 wxRendererMSWBase() { }
118 wxRendererMSWBase(wxRendererNative
& rendererNative
)
119 : wxDelegateRendererNative(rendererNative
) { }
121 void DrawFocusRect(wxWindow
* win
,
126 void DrawItemSelectionRect(wxWindow
*win
,
132 // ----------------------------------------------------------------------------
133 // wxRendererMSW: wxRendererNative implementation for "old" Win32 systems
134 // ----------------------------------------------------------------------------
136 class wxRendererMSW
: public wxRendererMSWBase
141 static wxRendererNative
& Get();
143 virtual void DrawComboBoxDropButton(wxWindow
*win
,
148 virtual void DrawCheckBox(wxWindow
*win
,
153 DoDrawButton(DFCS_BUTTONCHECK
, win
, dc
, rect
, flags
);
156 virtual void DrawPushButton(wxWindow
*win
,
161 virtual void DrawChoice(wxWindow
* win
,
166 virtual void DrawComboBox(wxWindow
* win
,
171 virtual void DrawTextCtrl(wxWindow
* win
,
176 virtual void DrawRadioBitmap(wxWindow
* win
,
181 DoDrawButton(DFCS_BUTTONRADIO
, win
, dc
, rect
, flags
);
184 virtual wxSize
GetCheckBoxSize(wxWindow
*win
);
186 virtual int GetHeaderButtonHeight(wxWindow
*win
);
189 // common part of Draw{PushButton,CheckBox,RadioBitmap}(): wraps
190 // DrawFrameControl(DFC_BUTTON)
191 void DoDrawButton(UINT kind
,
197 wxDECLARE_NO_COPY_CLASS(wxRendererMSW
);
200 // ----------------------------------------------------------------------------
201 // wxRendererXP: wxRendererNative implementation for Windows XP and later
202 // ----------------------------------------------------------------------------
206 class wxRendererXP
: public wxRendererMSWBase
209 wxRendererXP() : wxRendererMSWBase(wxRendererMSW::Get()) { }
211 static wxRendererNative
& Get();
213 virtual int DrawHeaderButton(wxWindow
*win
,
217 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
,
218 wxHeaderButtonParams
* params
= NULL
);
220 virtual void DrawTreeItemButton(wxWindow
*win
,
224 virtual void DrawSplitterBorder(wxWindow
*win
,
228 virtual void DrawSplitterSash(wxWindow
*win
,
232 wxOrientation orient
,
234 virtual void DrawComboBoxDropButton(wxWindow
*win
,
238 virtual void DrawCheckBox(wxWindow
*win
,
243 if ( !DoDrawXPButton(BP_CHECKBOX
, win
, dc
, rect
, flags
) )
244 m_rendererNative
.DrawCheckBox(win
, dc
, rect
, flags
);
247 virtual void DrawPushButton(wxWindow
*win
,
252 if ( !DoDrawXPButton(BP_PUSHBUTTON
, win
, dc
, rect
, flags
) )
253 m_rendererNative
.DrawPushButton(win
, dc
, rect
, flags
);
256 virtual void DrawRadioBitmap(wxWindow
*win
,
261 if ( !DoDrawXPButton(BP_RADIOBUTTON
, win
, dc
, rect
, flags
) )
262 m_rendererNative
.DrawRadioBitmap(win
, dc
, rect
, flags
);
265 virtual wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
268 // common part of DrawCheckBox(), DrawPushButton() and DrawRadioBitmap()
269 bool DoDrawXPButton(int kind
,
275 wxDECLARE_NO_COPY_CLASS(wxRendererXP
);
278 #endif // wxUSE_UXTHEME
281 // ============================================================================
282 // wxRendererMSWBase implementation
283 // ============================================================================
285 void wxRendererMSWBase::DrawFocusRect(wxWindow
* WXUNUSED(win
),
291 wxCopyRectToRECT(rect
, rc
);
293 ::DrawFocusRect(GetHdcOf(dc
.GetTempHDC()), &rc
);
296 void wxRendererMSWBase::DrawItemSelectionRect(wxWindow
*win
,
302 if ( flags
& wxCONTROL_SELECTED
)
304 if ( flags
& wxCONTROL_FOCUSED
)
306 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
310 brush
= wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
315 brush
= *wxTRANSPARENT_BRUSH
;
319 dc
.SetPen(*wxTRANSPARENT_PEN
);
320 dc
.DrawRectangle( rect
);
322 if ((flags
& wxCONTROL_FOCUSED
) && (flags
& wxCONTROL_CURRENT
))
323 DrawFocusRect( win
, dc
, rect
, flags
);
327 // ============================================================================
328 // wxRendererNative and wxRendererMSW implementation
329 // ============================================================================
332 wxRendererNative
& wxRendererNative::GetDefault()
335 wxUxThemeEngine
*themeEngine
= wxUxThemeEngine::Get();
336 if ( themeEngine
&& themeEngine
->IsAppThemed() )
337 return wxRendererXP::Get();
338 #endif // wxUSE_UXTHEME
340 return wxRendererMSW::Get();
344 wxRendererNative
& wxRendererMSW::Get()
346 static wxRendererMSW s_rendererMSW
;
348 return s_rendererMSW
;
352 wxRendererMSW::DrawComboBoxDropButton(wxWindow
* WXUNUSED(win
),
358 wxCopyRectToRECT(rect
, r
);
360 int style
= DFCS_SCROLLCOMBOBOX
;
361 if ( flags
& wxCONTROL_DISABLED
)
362 style
|= DFCS_INACTIVE
;
363 if ( flags
& wxCONTROL_PRESSED
)
364 style
|= DFCS_PUSHED
| DFCS_FLAT
;
366 ::DrawFrameControl(GetHdcOf(dc
.GetTempHDC()), &r
, DFC_SCROLL
, style
);
370 wxRendererMSW::DoDrawButton(UINT kind
,
371 wxWindow
* WXUNUSED(win
),
377 wxCopyRectToRECT(rect
, r
);
380 if ( flags
& wxCONTROL_CHECKED
)
381 style
|= DFCS_CHECKED
;
382 if ( flags
& wxCONTROL_DISABLED
)
383 style
|= DFCS_INACTIVE
;
384 if ( flags
& wxCONTROL_FLAT
)
386 if ( flags
& wxCONTROL_PRESSED
)
387 style
|= DFCS_PUSHED
;
388 if ( flags
& wxCONTROL_CURRENT
)
391 ::DrawFrameControl(GetHdcOf(dc
.GetTempHDC()), &r
, DFC_BUTTON
, style
);
395 wxRendererMSW::DrawPushButton(wxWindow
*win
,
397 const wxRect
& rectOrig
,
400 wxRect
rect(rectOrig
);
401 if ( flags
& wxCONTROL_ISDEFAULT
)
403 // DrawFrameControl() doesn't seem to support default buttons so we
404 // have to draw the border ourselves
405 wxDCPenChanger
pen(dc
, *wxBLACK_PEN
);
406 wxDCBrushChanger
brush(dc
, *wxTRANSPARENT_BRUSH
);
407 dc
.DrawRectangle(rect
);
411 DoDrawButton(DFCS_BUTTONPUSH
, win
, dc
, rect
, flags
);
414 wxSize
wxRendererMSW::GetCheckBoxSize(wxWindow
* WXUNUSED(win
))
416 return wxSize(::GetSystemMetrics(SM_CXMENUCHECK
),
417 ::GetSystemMetrics(SM_CYMENUCHECK
));
420 int wxRendererMSW::GetHeaderButtonHeight(wxWindow
* WXUNUSED(win
))
422 // some "reasonable" value returned in case of error, it doesn't really
423 // correspond to anything but it's better than returning 0
424 static const int DEFAULT_HEIGHT
= 20;
427 // create a temporary header window just to get its geometry
428 HWND hwndHeader
= ::CreateWindow(WC_HEADER
, NULL
, 0,
429 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
);
431 return DEFAULT_HEIGHT
;
433 wxON_BLOCK_EXIT1( ::DestroyWindow
, hwndHeader
);
435 // initialize the struct filled with the values by Header_Layout()
436 RECT parentRect
= { 0, 0, 100, 100 };
437 WINDOWPOS wp
= { 0, 0, 0, 0, 0, 0, 0 };
438 HDLAYOUT hdl
= { &parentRect
, &wp
};
440 return Header_Layout(hwndHeader
, &hdl
) ? wp
.cy
: DEFAULT_HEIGHT
;
443 // Uses the theme to draw the border and fill for something like a wxTextCtrl
444 void wxRendererMSW::DrawTextCtrl(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
451 wxUxThemeHandle
hTheme(win
, L
"EDIT");
454 wxUxThemeEngine::Get()->GetThemeColor(hTheme
, EP_EDITTEXT
,
455 ETS_NORMAL
, TMT_FILLCOLOR
, &cref
);
456 fill
= wxRGBToColour(cref
);
459 if ( flags
& wxCONTROL_DISABLED
)
460 etsState
= ETS_DISABLED
;
462 etsState
= ETS_NORMAL
;
464 wxUxThemeEngine::Get()->GetThemeColor(hTheme
, EP_EDITTEXT
,
465 etsState
, TMT_BORDERCOLOR
, &cref
);
466 bdr
= wxRGBToColour(cref
);
471 fill
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
477 dc
.DrawRectangle(rect
);
481 // Draw the equivalent of a wxComboBox
482 void wxRendererMSW::DrawComboBox(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
484 // Draw the main part of the control same as TextCtrl
485 DrawTextCtrl(win
, dc
, rect
, flags
);
487 // Draw the button inside the border, on the right side
490 br
.x
+= br
.width
- br
.height
- 1;
491 br
.width
= br
.height
;
494 DrawComboBoxDropButton(win
, dc
, br
, flags
);
498 void wxRendererMSW::DrawChoice(wxWindow
* win
, wxDC
& dc
,
499 const wxRect
& rect
, int flags
)
501 DrawComboBox(win
, dc
, rect
, flags
);
504 // ============================================================================
505 // wxRendererXP implementation
506 // ============================================================================
511 wxRendererNative
& wxRendererXP::Get()
513 static wxRendererXP s_rendererXP
;
518 // NOTE: There is no guarantee that the button drawn fills the entire rect (XP
519 // default theme, for example), so the caller should have cleared button's
520 // background before this call. This is quite likely a wxMSW-specific thing.
522 wxRendererXP::DrawComboBoxDropButton(wxWindow
* win
,
527 wxUxThemeHandle
hTheme(win
, L
"COMBOBOX");
530 m_rendererNative
.DrawComboBoxDropButton(win
, dc
, rect
, flags
);
535 wxCopyRectToRECT(rect
, r
);
538 if ( flags
& wxCONTROL_PRESSED
)
539 state
= CBXS_PRESSED
;
540 else if ( flags
& wxCONTROL_CURRENT
)
542 else if ( flags
& wxCONTROL_DISABLED
)
543 state
= CBXS_DISABLED
;
547 wxUxThemeEngine::Get()->DrawThemeBackground
550 GetHdcOf(dc
.GetTempHDC()),
560 wxRendererXP::DrawHeaderButton(wxWindow
*win
,
564 wxHeaderSortIconType sortArrow
,
565 wxHeaderButtonParams
* params
)
567 wxUxThemeHandle
hTheme(win
, L
"HEADER");
570 return m_rendererNative
.DrawHeaderButton(win
, dc
, rect
, flags
, sortArrow
, params
);
574 wxCopyRectToRECT(rect
, r
);
577 if ( flags
& wxCONTROL_PRESSED
)
579 else if ( flags
& wxCONTROL_CURRENT
)
583 wxUxThemeEngine::Get()->DrawThemeBackground
586 GetHdcOf(dc
.GetTempHDC()),
593 // NOTE: Using the theme to draw HP_HEADERSORTARROW doesn't do anything.
594 // Why? If this can be fixed then draw the sort arrows using the theme
595 // and then clear those flags before calling DrawHeaderButtonContents.
597 // Add any extras that are specified in flags and params
598 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
603 wxRendererXP::DrawTreeItemButton(wxWindow
*win
,
608 wxUxThemeHandle
hTheme(win
, L
"TREEVIEW");
611 m_rendererNative
.DrawTreeItemButton(win
, dc
, rect
, flags
);
616 wxCopyRectToRECT(rect
, r
);
618 int state
= flags
& wxCONTROL_EXPANDED
? GLPS_OPENED
: GLPS_CLOSED
;
619 wxUxThemeEngine::Get()->DrawThemeBackground
622 GetHdcOf(dc
.GetTempHDC()),
631 wxRendererXP::DoDrawXPButton(int kind
,
637 wxUxThemeHandle
hTheme(win
, L
"BUTTON");
642 wxCopyRectToRECT(rect
, r
);
644 // determine the base state depending on the button kind
653 state
= RBS_UNCHECKEDNORMAL
;
657 state
= CBS_UNCHECKEDNORMAL
;
661 wxFAIL_MSG( "unknown button kind" );
665 // XBS_XXX is followed by XBX_XXXHOT, then XBS_XXXPRESSED and DISABLED
675 // in both RBS_ and CBS_ enums CHECKED elements are offset by 4 from base
676 // (UNCHECKED) ones and MIXED are offset by 4 again as there are all states
677 // from the above enum in between them
678 if ( flags
& wxCONTROL_CHECKED
)
679 state
+= STATES_COUNT
;
680 else if ( flags
& wxCONTROL_UNDETERMINED
)
681 state
+= 2*STATES_COUNT
;
683 if ( flags
& wxCONTROL_DISABLED
)
684 state
+= DISABLED_OFFSET
;
685 else if ( flags
& wxCONTROL_PRESSED
)
686 state
+= PRESSED_OFFSET
;
687 else if ( flags
& wxCONTROL_CURRENT
)
689 // wxCONTROL_ISDEFAULT flag is only valid for push buttons
690 else if ( kind
== BP_PUSHBUTTON
&& (flags
& wxCONTROL_ISDEFAULT
) )
691 state
= PBS_DEFAULTED
;
693 wxUxThemeEngine::Get()->DrawThemeBackground
696 GetHdcOf(dc
.GetTempHDC()),
706 // ----------------------------------------------------------------------------
708 // ----------------------------------------------------------------------------
710 // the width of the sash: this is the same as used by Explorer...
711 static const wxCoord SASH_WIDTH
= 4;
713 wxSplitterRenderParams
714 wxRendererXP::GetSplitterParams(const wxWindow
* win
)
716 if ( win
->HasFlag(wxSP_NO_XP_THEME
) )
717 return m_rendererNative
.GetSplitterParams(win
);
719 return wxSplitterRenderParams(SASH_WIDTH
, 0, false);
723 wxRendererXP::DrawSplitterBorder(wxWindow
* win
,
728 if ( win
->HasFlag(wxSP_NO_XP_THEME
) )
730 m_rendererNative
.DrawSplitterBorder(win
, dc
, rect
, flags
);
735 wxRendererXP::DrawSplitterSash(wxWindow
*win
,
739 wxOrientation orient
,
742 if ( !win
->HasFlag(wxSP_NO_XP_THEME
) )
744 dc
.SetPen(*wxTRANSPARENT_PEN
);
745 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
)));
746 if ( orient
== wxVERTICAL
)
748 dc
.DrawRectangle(position
, 0, SASH_WIDTH
, size
.y
);
752 dc
.DrawRectangle(0, position
, size
.x
, SASH_WIDTH
);
758 m_rendererNative
.DrawSplitterSash(win
, dc
, size
, position
, orient
, flags
);
761 #endif // wxUSE_UXTHEME