]> git.saurik.com Git - wxWidgets.git/blob - src/msw/ownerdrw.cpp
Applied patch [ 731719 ] Owner draw font leak/overuse
[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 licence
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 #include "wx/msw/private.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/window.h"
26 #include "wx/msw/private.h"
27 #include "wx/font.h"
28 #include "wx/bitmap.h"
29 #include "wx/dcmemory.h"
30 #include "wx/menu.h"
31 #include "wx/utils.h"
32 #endif
33
34 #include "wx/settings.h"
35 #include "wx/ownerdrw.h"
36 #include "wx/menuitem.h"
37 #include "wx/fontutil.h"
38
39 #if wxUSE_OWNER_DRAWN
40
41
42 // ============================================================================
43 // implementation of wxOwnerDrawn class
44 // ============================================================================
45
46 // ctor
47 // ----
48 wxOwnerDrawn::wxOwnerDrawn(const wxString& str,
49 bool bCheckable, bool bMenuItem)
50 : m_strName(str)
51 {
52 #if defined(__WXMSW__) && defined(__WIN32__) && defined(SM_CXMENUCHECK)
53 // get the default menu height and font from the system
54 NONCLIENTMETRICS nm;
55 nm.cbSize = sizeof (NONCLIENTMETRICS);
56 SystemParametersInfo (SPI_GETNONCLIENTMETRICS,0,&nm,0);
57 m_nMinHeight = nm.iMenuHeight;
58
59 // nm.iMenuWidth is the system default for the width of
60 // menu icons and checkmarks
61 if (ms_nDefaultMarginWidth == 0)
62 {
63 ms_nDefaultMarginWidth = nm.iMenuWidth;
64 ms_nLastMarginWidth = nm.iMenuWidth;
65 }
66
67 if (bMenuItem)
68 {
69 static wxFont menu_font;
70 if (!menu_font.Ok())
71 {
72 // create menu font
73 wxNativeFontInfo info;
74 memcpy(&info.lf, &nm.lfMenuFont, sizeof(LOGFONT));
75 menu_font.Create(info);
76 }
77
78 m_font = menu_font;
79 }
80 else
81 {
82 m_font = *wxNORMAL_FONT;
83 }
84 #else
85 // windows clean install default
86 m_nMinHeight = 18;
87
88 if (ms_nDefaultMarginWidth == 0)
89 {
90 ms_nDefaultMarginWidth = 18;
91 ms_nLastMarginWidth = 18;
92 }
93 if (wxNORMAL_FONT)
94 m_font = *wxNORMAL_FONT;
95 #endif
96
97 m_bCheckable = bCheckable;
98 m_bOwnerDrawn = FALSE;
99 m_nHeight = 0;
100 m_nMarginWidth = ms_nLastMarginWidth;
101 }
102
103
104 // these items will be set during the first invocation of the c'tor,
105 // because the values will be determined by checking the system settings,
106 // which is a chunk of code
107 size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 0;
108 size_t wxOwnerDrawn::ms_nLastMarginWidth = 0;
109
110
111 // drawing
112 // -------
113
114 // get size of the item
115 // The item size includes the menu string, the accel string,
116 // the bitmap and size for a submenu expansion arrow...
117 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight)
118 {
119 wxMemoryDC dc;
120
121 wxString str = wxStripMenuCodes(m_strName);
122
123 // if we have a valid accel string, then pad out
124 // the menu string so the menu and accel string are not
125 // placed ontop of eachother.
126 if ( !m_strAccel.empty() )
127 {
128 str.Pad(str.Length()%8);
129 str += m_strAccel;
130 }
131
132 if (m_font.Ok())
133 dc.SetFont(GetFont());
134
135 dc.GetTextExtent(str, (long *)pwidth, (long *)pheight);
136
137 if (!m_strAccel.IsEmpty())
138 {
139 // measure the accelerator string, and add it's width to
140 // the total item width, plus 16 (Accelerators are right justified,
141 // with the right edge of the text rectangle 16 pixels left of
142 // the right edge of the menu)
143
144 int accel_width, accel_height;
145 dc.GetTextExtent(m_strAccel, &accel_width, &accel_height);
146 *pwidth += accel_width;
147 }
148
149 // add space at the end of the menu for the submenu expansion arrow
150 // this will also allow offsetting the accel string from the right edge
151 *pwidth += (size_t) (GetDefaultMarginWidth() * 1.5);
152
153 // JACS: items still look too tightly packed, so adding 5 pixels.
154 (*pheight) = (*pheight) + 5;
155
156 // Ray Gilbert's changes - Corrects the problem of a BMP
157 // being placed next to text in a menu item, and the BMP does
158 // not match the size expected by the system. This will
159 // resize the space so the BMP will fit. Without this, BMPs
160 // must be no larger or smaller than 16x16.
161 if (m_bmpChecked.Ok())
162 {
163 // Is BMP height larger then text height?
164 size_t adjustedHeight = m_bmpChecked.GetHeight() +
165 wxSystemSettings::GetMetric(wxSYS_EDGE_Y);
166 if (*pheight < adjustedHeight)
167 *pheight = adjustedHeight;
168
169 // Does BMP encroach on default check menu position?
170 size_t adjustedWidth = m_bmpChecked.GetWidth() +
171 (wxSystemSettings::GetMetric(wxSYS_EDGE_X) * 2);
172 // if (ms_nDefaultMarginWidth < adjustedWidth)
173 // *pwidth += adjustedWidth - ms_nDefaultMarginWidth;
174
175 // Do we need to widen margin to fit BMP?
176 if ((size_t)GetMarginWidth() != adjustedWidth)
177 SetMarginWidth(adjustedWidth);
178
179 // add the size of the bitmap to our total size...
180 *pwidth += GetMarginWidth();
181 }
182
183 // add the size of the bitmap to our total size - even if we don't have
184 // a bitmap we leave room for one...
185 *pwidth += GetMarginWidth();
186
187 // make sure that this item is at least as
188 // tall as the user's system settings specify
189 if (*pheight < m_nMinHeight)
190 *pheight = m_nMinHeight;
191
192 m_nHeight = *pheight; // remember height for use in OnDrawItem
193
194 return TRUE;
195 }
196
197 // searching for this macro you'll find all the code where I'm using the native
198 // Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
199 // port this code to X. (VZ)
200
201 // JACS: TODO. Why does a disabled but highlighted item still
202 // get drawn embossed? How can we tell DrawState that we don't want the
203 // embossing?
204
205 #if defined(__WIN32__) && !defined(__SYMANTEC__) && !defined(__TWIN32__)
206 #define O_DRAW_NATIVE_API // comments below explain why I use it
207 #endif
208
209 // draw the item
210 bool wxOwnerDrawn::OnDrawItem(wxDC& dc,
211 const wxRect& rc,
212 wxODAction act,
213 wxODStatus st)
214 {
215 // we do nothing on focus change
216 if ( act == wxODFocusChanged )
217 return TRUE;
218
219 // wxColor <-> RGB
220 #define ToRGB(col) PALETTERGB(col.Red(), col.Green(), col.Blue())
221 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
222
223 // set the colors
224 // --------------
225 DWORD colBack, colText;
226 if ( st & wxODSelected ) {
227 colBack = GetSysColor(COLOR_HIGHLIGHT);
228 if (!(st & wxODDisabled))
229 {
230 colText = GetSysColor(COLOR_HIGHLIGHTTEXT);
231 }
232 else
233 {
234 colText = GetSysColor(COLOR_GRAYTEXT);
235 }
236 }
237 else {
238 // fall back to default colors if none explicitly specified
239 colBack = m_colBack.Ok() ? ToRGB(m_colBack) : GetSysColor(COLOR_WINDOW);
240 colText = m_colText.Ok() ? ToRGB(m_colText) : GetSysColor(COLOR_WINDOWTEXT);
241 }
242
243 #ifdef O_DRAW_NATIVE_API
244 #define hdc (HDC)dc.GetHDC()
245 COLORREF colOldText = ::SetTextColor(hdc, colText),
246 colOldBack = ::SetBkColor(hdc, colBack);
247 #else
248 dc.SetTextForeground(wxColor(UnRGB(colText)));
249 dc.SetTextBackground(wxColor(UnRGB(colBack)));
250 #endif
251
252 // select the font and draw the text
253 // ---------------------------------
254
255
256 // determine where to draw and leave space for a check-mark.
257 // Add 3 pixel padding so text appears well within highlight rectangle
258 int x = rc.x + GetMarginWidth() + 3;
259
260
261 // using native API because it reckognizes '&'
262 #ifdef O_DRAW_NATIVE_API
263 int nPrevMode = SetBkMode(hdc, TRANSPARENT);
264 HBRUSH hbr = CreateSolidBrush(colBack),
265 hPrevBrush = (HBRUSH)SelectObject(hdc, hbr);
266
267 RECT rectFill = { rc.GetLeft(), rc.GetTop(), rc.GetRight()+1, rc.GetBottom() };
268
269 if ( st & wxODSelected && m_bmpChecked.Ok()) {
270 // only draw the highlight under the text, not under
271 // the bitmap or checkmark; leave a 1-pixel gap.
272 rectFill.left = GetMarginWidth() + 1;
273 }
274
275 FillRect(hdc, &rectFill, hbr);
276
277 // use default font if no font set
278 HFONT hfont;
279 if ( m_font.Ok() ) {
280 m_font.RealizeResource();
281 hfont = (HFONT)m_font.GetResourceHandle();
282 }
283 else {
284 hfont = (HFONT)::GetStockObject(SYSTEM_FONT);
285 }
286
287 HFONT hPrevFont = (HFONT) ::SelectObject(hdc, hfont);
288
289 wxString strMenuText = m_strName.BeforeFirst('\t');
290
291 SIZE sizeRect;
292 GetTextExtentPoint32(hdc,strMenuText.c_str(), strMenuText.Length(),&sizeRect);
293 ::DrawState(hdc, NULL, NULL,
294 (LPARAM)strMenuText.c_str(), strMenuText.length(),
295 x, rc.y+( (int) ((rc.GetHeight()-sizeRect.cy)/2.0) )-1, // centre text vertically
296 rc.GetWidth()-GetMarginWidth(), sizeRect.cy,
297 DST_PREFIXTEXT |
298 (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0));
299
300 if ( !m_strAccel.empty() )
301 {
302 // right align accel string with right edge of menu ( offset by the margin width )
303 ::SetTextAlign(hdc, TA_RIGHT);
304 ::DrawState(hdc, NULL, NULL,
305 (LPARAM)m_strAccel.c_str(), m_strAccel.length(),
306 rc.GetWidth()-(GetMarginWidth()), rc.y+(int) ((rc.GetHeight()-sizeRect.cy)/2.0),
307 rc.GetWidth()-GetMarginWidth(), sizeRect.cy,
308 DST_TEXT |
309 (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0));
310 ::SetTextAlign(hdc, TA_LEFT);
311 }
312
313 (void)SelectObject(hdc, hPrevBrush);
314 (void)SelectObject(hdc, hPrevFont);
315 (void)SetBkMode(hdc, nPrevMode);
316
317 DeleteObject(hbr);
318 #else
319 dc.SetFont(GetFont());
320 dc.DrawText(wxStripMenuCodes(m_strName), x, rc.y);
321 #endif //O_DRAW_NATIVE_API
322
323 // draw the bitmap
324 // ---------------
325 if ( IsCheckable() && !m_bmpChecked.Ok() ) {
326 if ( st & wxODChecked ) {
327 // using native APIs for performance and simplicity
328 #ifdef O_DRAW_NATIVE_API
329 // what goes on: DrawFrameControl creates a b/w mask,
330 // then we copy it to screen to have right colors
331
332 // first create a monochrome bitmap in a memory DC
333 HDC hdcMem = CreateCompatibleDC(hdc);
334 HBITMAP hbmpCheck = CreateBitmap(GetMarginWidth(), m_nHeight, 1, 1, 0);
335 SelectObject(hdcMem, hbmpCheck);
336
337 // then draw a check mark into it
338 RECT rect = { 0, 0, GetMarginWidth(), m_nHeight };
339 if ( m_nHeight > 0 )
340 {
341 ::DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
342 }
343
344 // finally copy it to screen DC and clean up
345 BitBlt(hdc, rc.x, rc.y, GetMarginWidth(), m_nHeight,
346 hdcMem, 0, 0, SRCCOPY);
347
348 DeleteDC(hdcMem);
349 DeleteObject(hbmpCheck);
350 #else
351 // #### to do: perhaps using Marlett font (create equiv. font under X)
352 // wxFAIL("not implemented");
353 #endif //O_DRAW_NATIVE_API
354 }
355 }
356 else {
357 // for uncheckable item we use only the 'checked' bitmap
358 wxBitmap bmp(GetBitmap(IsCheckable() ? ((st & wxODChecked) != 0) : TRUE));
359 if ( bmp.Ok() ) {
360 wxMemoryDC dcMem(&dc);
361 dcMem.SelectObject(bmp);
362
363 // center bitmap
364 int nBmpWidth = bmp.GetWidth(),
365 nBmpHeight = bmp.GetHeight();
366
367 // there should be enough place!
368 wxASSERT((nBmpWidth <= rc.GetWidth()) && (nBmpHeight <= rc.GetHeight()));
369
370 int heightDiff = m_nHeight - nBmpHeight;
371 dc.Blit(rc.x + (GetMarginWidth() - nBmpWidth) / 2,
372 rc.y + heightDiff / 2,
373 nBmpWidth, nBmpHeight,
374 &dcMem, 0, 0, wxCOPY, TRUE /* use mask */);
375
376 if ( st & wxODSelected ) {
377 #ifdef O_DRAW_NATIVE_API
378 RECT rectBmp = { rc.GetLeft(), rc.GetTop(),
379 rc.GetLeft() + GetMarginWidth(),
380 rc.GetTop() + m_nHeight-1 };
381 SetBkColor(hdc, colBack);
382 DrawEdge(hdc, &rectBmp, EDGE_RAISED, BF_SOFT | BF_RECT);
383 #else
384 int x1, y1, x2, y2;
385 x1 = rc.x;
386 y1 = rc.y;
387 x2 = x1 + GetMarginWidth() - 1;
388 y2 = y1 + m_nHeight - 1;
389
390 dc.SetPen(*wxWHITE_PEN);
391 dc.DrawLine(x1, y1, x2, y1);
392 dc.DrawLine(x1, y1, x1, y2);
393 dc.SetPen(*wxGREY_PEN);
394 dc.DrawLine(x1, y2-1, x2, y2-1);
395 dc.DrawLine(x2, y1, x2, y2);
396 #endif //O_DRAW_NATIVE_API
397 }
398 }
399 }
400
401 #ifdef O_DRAW_NATIVE_API
402 ::SetTextColor(hdc, colOldText);
403 ::SetBkColor(hdc, colOldBack);
404
405 #undef hdc
406 #endif //O_DRAW_NATIVE_API
407
408 return TRUE;
409 }
410
411
412 #endif // wxUSE_OWNER_DRAWN
413