]> git.saurik.com Git - wxWidgets.git/blob - src/msw/ownerdrw.cpp
Misc small mods
[wxWidgets.git] / src / msw / ownerdrw.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/ownerdrw.cpp
3 // Purpose: implementation of wxOwnerDrawn class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 13.11.97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/window.h"
25 #include "wx/msw/private.h"
26 #include "wx/font.h"
27 #include "wx/bitmap.h"
28 #include "wx/dcmemory.h"
29 #include "wx/menu.h"
30 #include "wx/utils.h"
31 #endif
32
33 #include "wx/ownerdrw.h"
34 #include "wx/menuitem.h"
35
36 #if wxUSE_OWNER_DRAWN
37
38
39 // ============================================================================
40 // implementation of wxOwnerDrawn class
41 // ============================================================================
42
43 // ctor
44 // ----
45 wxOwnerDrawn::wxOwnerDrawn(const wxString& str,
46 bool bCheckable, bool bMenuItem)
47 : m_strName(str)
48 {
49 m_bCheckable = bCheckable;
50 m_bOwnerDrawn = FALSE;
51 m_nHeight = 0;
52 m_nMarginWidth = ms_nLastMarginWidth;
53 }
54
55 #if defined(__WXMSW__) && defined(__WIN32__) && defined(SM_CXMENUCHECK)
56 size_t wxOwnerDrawn::ms_nDefaultMarginWidth = GetSystemMetrics(SM_CXMENUCHECK);
57 #else // # what is the reasonable default?
58 size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 15;
59 #endif
60
61 size_t wxOwnerDrawn::ms_nLastMarginWidth = ms_nDefaultMarginWidth;
62
63 // drawing
64 // -------
65
66 // get size of the item
67 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight)
68 {
69 wxMemoryDC dc;
70 dc.SetFont(GetFont());
71
72 // ## ugly...
73 wxChar *szStripped = new wxChar[m_strName.Len()];
74 wxStripMenuCodes((wxChar *)m_strName.c_str(), szStripped);
75 wxString str = szStripped;
76 delete [] szStripped;
77
78 // # without this menu items look too tightly packed (at least under Windows)
79 str += wxT('W'); // 'W' is typically the widest letter
80
81 dc.GetTextExtent(str, (long *)pwidth, (long *)pheight);
82
83 // JACS: items still look too tightly packed, so adding 2 pixels.
84 (*pheight) = (*pheight) + 2;
85
86 m_nHeight = *pheight; // remember height for use in OnDrawItem
87
88 return TRUE;
89 }
90
91 // searching for this macro you'll find all the code where I'm using the native
92 // Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
93 // port this code to X. (VZ)
94
95 // JACS: TODO. Why does a disabled but highlighted item still
96 // get drawn embossed? How can we tell DrawState that we don't want the
97 // embossing?
98
99 #if defined(__WIN32__) && !defined(__SC__) && !defined(__TWIN32__)
100 #define O_DRAW_NATIVE_API // comments below explain why I use it
101 #endif
102
103 // draw the item
104 bool wxOwnerDrawn::OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus st)
105 {
106 // we do nothing on focus change
107 if ( act == wxODFocusChanged )
108 return TRUE;
109
110 // wxColor <-> RGB
111 #define ToRGB(col) RGB(col.Red(), col.Green(), col.Blue())
112 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
113
114 // set the colors
115 // --------------
116 DWORD colBack, colText;
117 if ( st & wxODSelected ) {
118 colBack = GetSysColor(COLOR_HIGHLIGHT);
119 colText = GetSysColor(COLOR_HIGHLIGHTTEXT);
120 }
121 else {
122 // fall back to default colors if none explicitly specified
123 colBack = m_colBack.Ok() ? ToRGB(m_colBack) : GetSysColor(COLOR_WINDOW);
124 colText = m_colText.Ok() ? ToRGB(m_colText) : GetSysColor(COLOR_WINDOWTEXT);
125 }
126
127 #ifdef O_DRAW_NATIVE_API
128 #define hdc (HDC)dc.GetHDC()
129 COLORREF colOldText = ::SetTextColor(hdc, colText),
130 colOldBack = ::SetBkColor(hdc, colBack);
131 #else
132 dc.SetTextForeground(wxColor(UnRGB(colText)));
133 dc.SetTextBackground(wxColor(UnRGB(colBack)));
134 #endif
135
136 // select the font and draw the text
137 // ---------------------------------
138
139 // determine where to draw and leave space for a check-mark.
140 int x = rc.x + GetMarginWidth();
141
142 // using native API because it reckognizes '&'
143 #ifdef O_DRAW_NATIVE_API
144 int nPrevMode = SetBkMode(hdc, TRANSPARENT);
145 HBRUSH hbr = CreateSolidBrush(colBack),
146 hPrevBrush = (HBRUSH)SelectObject(hdc, hbr);
147
148 RECT rectAll = { rc.GetLeft(), rc.GetTop(), rc.GetRight(), rc.GetBottom() };
149 FillRect(hdc, &rectAll, hbr);
150
151 DeleteObject(hbr);
152
153 // use default font if no font set
154 HFONT hfont;
155 if ( m_font.Ok() ) {
156 m_font.RealizeResource();
157 hfont = (HFONT)m_font.GetResourceHandle();
158 }
159 else {
160 hfont = (HFONT)::GetStockObject(SYSTEM_FONT);
161 }
162
163 HFONT hPrevFont = (HFONT) ::SelectObject(hdc, hfont);
164 DrawState(hdc, NULL, NULL,
165 (LPARAM)(const wxChar *)m_strName, m_strName.Length(),
166 x, rc.y, rc.GetWidth(), rc.GetHeight(),
167 DST_PREFIXTEXT | ( st & wxODDisabled ? DSS_DISABLED : 0) );
168
169 (void)SelectObject(hdc, hPrevBrush);
170 (void)SelectObject(hdc, hPrevFont);
171 (void)SetBkMode(hdc, nPrevMode);
172 #else
173 dc.SetFont(GetFont());
174 dc.DrawText(m_strName, x, rc.y);
175 #endif //O_DRAW_NATIVE_API
176
177 // draw the bitmap
178 // ---------------
179 if ( IsCheckable() && !m_bmpChecked.Ok() ) {
180 if ( st & wxODChecked ) {
181 // using native APIs for performance and simplicity
182 #ifdef O_DRAW_NATIVE_API
183 // what goes on: DrawFrameControl creates a b/w mask,
184 // then we copy it to screen to have right colors
185
186 // first create a monochrome bitmap in a memory DC
187 HDC hdcMem = CreateCompatibleDC(hdc);
188 HBITMAP hbmpCheck = CreateBitmap(GetMarginWidth(), m_nHeight, 1, 1, 0);
189 SelectObject(hdcMem, hbmpCheck);
190
191 // then draw a check mark into it
192 RECT rect = { 0, 0, GetMarginWidth(), m_nHeight };
193 if ( m_nHeight > 0 )
194 {
195 #ifndef __SC__
196 DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
197 #endif
198 }
199
200 // finally copy it to screen DC and clean up
201 BitBlt(hdc, rc.x, rc.y, GetMarginWidth(), m_nHeight,
202 hdcMem, 0, 0, SRCCOPY);
203
204 DeleteDC(hdcMem);
205 DeleteObject(hbmpCheck);
206 #else
207 // #### to do: perhaps using Marlett font (create equiv. font under X)
208 // wxFAIL("not implemented");
209 #endif //O_DRAW_NATIVE_API
210 }
211 }
212 else {
213 // for uncheckable item we use only the 'checked' bitmap
214 wxBitmap bmp(GetBitmap(IsCheckable() ? ((st & wxODChecked) != 0) : TRUE));
215 if ( bmp.Ok() ) {
216 wxMemoryDC dcMem(&dc);
217 dcMem.SelectObject(bmp);
218
219 // center bitmap
220 int nBmpWidth = bmp.GetWidth(),
221 nBmpHeight = bmp.GetHeight();
222
223 // there should be enough place!
224 wxASSERT((nBmpWidth <= rc.GetWidth()) && (nBmpHeight <= rc.GetHeight()));
225
226 //MT: blit with mask enabled.
227 dc.Blit(rc.x + (GetMarginWidth() - nBmpWidth) / 2,
228 rc.y + (m_nHeight - nBmpHeight) /2,
229 nBmpWidth, nBmpHeight,
230 &dcMem, 0, 0, wxCOPY, TRUE);
231
232 if ( st & wxODSelected ) {
233 #ifdef O_DRAW_NATIVE_API
234 RECT rectBmp = { rc.GetLeft(), rc.GetTop(),
235 rc.GetLeft() + GetMarginWidth(),
236 rc.GetTop() + m_nHeight };
237 SetBkColor(hdc, colBack);
238 DrawEdge(hdc, &rectBmp, EDGE_RAISED, BF_SOFT | BF_RECT);
239 #else
240 // ## to write portable DrawEdge
241 #endif //O_DRAW_NATIVE_API
242 }
243 }
244 }
245
246 #ifdef O_DRAW_NATIVE_API
247 ::SetTextColor(hdc, colOldText);
248 ::SetBkColor(hdc, colOldBack);
249
250 #undef hdc
251 #endif //O_DRAW_NATIVE_API
252
253 return TRUE;
254 }
255
256
257 #endif // wxUSE_OWNER_DRAWN
258