Refactor owner-drawing code.
[wxWidgets.git] / src / msw / ownerdrw.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/ownerdrw.cpp
3 // Purpose: implementation of wxOwnerDrawn class
4 // Author: Vadim Zeitlin
5 // Modified by: Marcin Malich
6 // Created: 13.11.97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_OWNER_DRAWN
20
21 #include "wx/ownerdrw.h"
22 #include "wx/msw/dc.h"
23 #include "wx/msw/private.h"
24 #include "wx/msw/private/dc.h"
25
26 #ifndef DSS_HIDEPREFIX
27 #define DSS_HIDEPREFIX 0x0200
28 #endif
29
30 // ----------------------------------------------------------------------------
31 // constants for base class
32 // ----------------------------------------------------------------------------
33
34 int wxOwnerDrawnBase::ms_defaultMargin = 3;
35
36 // ============================================================================
37 // implementation of wxOwnerDrawn class
38 // ============================================================================
39
40 // draw the item
41 bool wxOwnerDrawn::OnDrawItem(wxDC& dc, const wxRect& rc,
42 wxODAction, wxODStatus stat)
43 {
44 // we do nothing if item isn't ownerdrawn
45 if ( !IsOwnerDrawn() )
46 return true;
47
48 // set the font and colors
49 wxFont font;
50 GetFontToUse(font);
51
52 wxColour colText, colBack;
53 GetColourToUse(stat, colText, colBack);
54
55 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
56 HDC hdc = GetHdcOf(*impl);
57
58 SelectInHDC selFont(hdc, GetHfontOf(font));
59
60 wxMSWImpl::wxTextColoursChanger textCol(hdc, colText, colBack);
61 wxMSWImpl::wxBkModeChanger bkMode(hdc, wxBRUSHSTYLE_TRANSPARENT);
62
63
64 AutoHBRUSH hbr(wxColourToPalRGB(colBack));
65 SelectInHDC selBrush(hdc, hbr);
66
67 RECT rectFill;
68 wxCopyRectToRECT(rc, rectFill);
69 ::FillRect(hdc, &rectFill, hbr);
70
71 // using native API because it recognizes '&'
72
73 wxString text = GetName();
74
75 SIZE sizeRect;
76 ::GetTextExtentPoint32(hdc, text.c_str(), text.length(), &sizeRect);
77
78 int flags = DST_PREFIXTEXT;
79 if ( (stat & wxODDisabled) && !(stat & wxODSelected) )
80 flags |= DSS_DISABLED;
81
82 if ( (stat & wxODHidePrefix) )
83 flags |= DSS_HIDEPREFIX;
84
85 int x = rc.x + GetMarginWidth();
86 int y = rc.y + (rc.GetHeight() - sizeRect.cy) / 2;
87 int cx = rc.GetWidth() - GetMarginWidth();
88 int cy = sizeRect.cy;
89
90 ::DrawState(hdc, NULL, NULL, (LPARAM)text.wx_str(),
91 text.length(), x, y, cx, cy, flags);
92
93 return true;
94 }
95
96 // ----------------------------------------------------------------------------
97 // global helper functions implemented here
98 // ----------------------------------------------------------------------------
99
100 BOOL wxDrawStateBitmap(HDC hDC, HBITMAP hBitmap, int x, int y, UINT uState)
101 {
102 // determine size of bitmap image
103 BITMAP bmp;
104 if ( !::GetObject(hBitmap, sizeof(BITMAP), &bmp) )
105 return FALSE;
106
107 BOOL result;
108
109 switch ( uState )
110 {
111 case wxDSB_NORMAL:
112 case wxDSB_SELECTED:
113 {
114 // uses image list functions to draw
115 // - normal bitmap with support transparency
116 // (image list internally create mask etc.)
117 // - blend bitmap with the background colour
118 // (like default selected items)
119 HIMAGELIST hIml = ::ImageList_Create(bmp.bmWidth, bmp.bmHeight,
120 ILC_COLOR32 | ILC_MASK, 1, 1);
121 ::ImageList_Add(hIml, hBitmap, NULL);
122 UINT fStyle = uState == wxDSB_SELECTED ? ILD_SELECTED : ILD_NORMAL;
123 result = ::ImageList_Draw(hIml, 0, hDC, x, y, fStyle);
124 ::ImageList_Destroy(hIml);
125 }
126 break;
127
128 case wxDSB_DISABLED:
129 result = ::DrawState(hDC, NULL, NULL, (LPARAM)hBitmap, 0, x, y,
130 bmp.bmWidth, bmp.bmHeight,
131 DST_BITMAP | DSS_DISABLED);
132 break;
133
134 default:
135 wxFAIL_MSG( wxT("DrawStateBitmap: unknown wxDSBStates value") );
136 result = FALSE;
137 }
138
139 return result;
140 }
141
142 #endif // wxUSE_OWNER_DRAWN