]> git.saurik.com Git - wxWidgets.git/blob - src/msw/ownerdrw.cpp
more native look for wxCheckListBox (especially under XP) (modified patch 1690068)
[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:
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 #ifndef WX_PRECOMP
20 #include "wx/window.h"
21 #include "wx/msw/private.h"
22 #include "wx/font.h"
23 #include "wx/bitmap.h"
24 #include "wx/image.h"
25 #include "wx/dcmemory.h"
26 #include "wx/menu.h"
27 #include "wx/utils.h"
28 #include "wx/settings.h"
29 #include "wx/menuitem.h"
30 #include "wx/module.h"
31 #endif
32
33 #include "wx/ownerdrw.h"
34 #include "wx/fontutil.h"
35
36 #if wxUSE_OWNER_DRAWN
37
38 #ifndef SPI_GETKEYBOARDCUES
39 #define SPI_GETKEYBOARDCUES 0x100A
40 #endif
41
42 class wxMSWSystemMenuFontModule : public wxModule
43 {
44 public:
45
46 virtual bool OnInit()
47 {
48 ms_systemMenuFont = new wxFont;
49
50 #if defined(__WXMSW__) && defined(__WIN32__) && defined(SM_CXMENUCHECK)
51 NONCLIENTMETRICS nm;
52 nm.cbSize = sizeof(NONCLIENTMETRICS);
53 SystemParametersInfo(SPI_GETNONCLIENTMETRICS,0,&nm,0);
54
55 ms_systemMenuButtonWidth = nm.iMenuHeight;
56 ms_systemMenuHeight = nm.iMenuHeight;
57
58 // create menu font
59 wxNativeFontInfo info;
60 memcpy(&info.lf, &nm.lfMenuFont, sizeof(LOGFONT));
61 ms_systemMenuFont->Create(info);
62
63 if (SystemParametersInfo(SPI_GETKEYBOARDCUES, 0, &ms_showCues, 0) == 0)
64 ms_showCues = true;
65 #endif
66
67 return true;
68 }
69
70 virtual void OnExit()
71 {
72 delete ms_systemMenuFont;
73 ms_systemMenuFont = NULL;
74 }
75
76 static wxFont* ms_systemMenuFont;
77 static int ms_systemMenuButtonWidth; // windows clean install default
78 static int ms_systemMenuHeight; // windows clean install default
79 static bool ms_showCues;
80 private:
81 DECLARE_DYNAMIC_CLASS(wxMSWSystemMenuFontModule)
82 };
83
84 // these static variables are from the wxMSWSystemMenuFontModule object
85 // and reflect the system settings returned by the Win32 API's
86 // SystemParametersInfo() call.
87
88 wxFont* wxMSWSystemMenuFontModule::ms_systemMenuFont = NULL;
89 int wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth = 18; // windows clean install default
90 int wxMSWSystemMenuFontModule::ms_systemMenuHeight = 18; // windows clean install default
91 bool wxMSWSystemMenuFontModule::ms_showCues = true;
92
93 IMPLEMENT_DYNAMIC_CLASS(wxMSWSystemMenuFontModule, wxModule)
94
95
96 // VC++ 6 gives a warning here:
97 //
98 // return type for 'OwnerDrawnSet_wxImplementation_HashTable::iterator::
99 // operator ->' is 'class wxOwnerDrawn ** ' (ie; not a UDT or reference to
100 // a UDT. Will produce errors if applied using infix notation.
101 //
102 // shut it down
103 #if defined __VISUALC__ && __VISUALC__ <= 1300
104 #if __VISUALC__ >= 1200
105 #pragma warning(push)
106 #define POP_WARNINGS
107 #endif
108 #pragma warning(disable: 4284)
109 #endif
110
111 #include "wx/hashset.h"
112 WX_DECLARE_HASH_SET(wxOwnerDrawn*, wxPointerHash, wxPointerEqual, OwnerDrawnSet);
113
114 #ifdef POP_WARNINGS
115 #pragma warning(pop)
116 #endif
117
118 // ============================================================================
119 // implementation of wxOwnerDrawn class
120 // ============================================================================
121
122 // ctor
123 // ----
124 wxOwnerDrawn::wxOwnerDrawn(const wxString& str,
125 bool bCheckable,
126 bool bMenuItem)
127 : m_strName(str)
128 {
129 if ( ms_nDefaultMarginWidth == 0 )
130 {
131 ms_nDefaultMarginWidth = ::GetSystemMetrics(SM_CXMENUCHECK) +
132 wxSystemSettings::GetMetric(wxSYS_EDGE_X);
133 ms_nLastMarginWidth = ms_nDefaultMarginWidth;
134 }
135
136 m_bCheckable = bCheckable;
137 m_bOwnerDrawn = false;
138 m_isMenuItem = bMenuItem;
139 m_nHeight = 0;
140 m_nMarginWidth = ms_nLastMarginWidth;
141 m_nMinHeight = wxMSWSystemMenuFontModule::ms_systemMenuHeight;
142 }
143
144 wxOwnerDrawn::~wxOwnerDrawn()
145 {
146 }
147
148 bool wxOwnerDrawn::IsMenuItem() const
149 {
150 return m_isMenuItem;
151 }
152
153
154 // these items will be set during the first invocation of the c'tor,
155 // because the values will be determined by checking the system settings,
156 // which is a chunk of code
157 size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 0;
158 size_t wxOwnerDrawn::ms_nLastMarginWidth = 0;
159
160
161 // drawing
162 // -------
163
164 wxFont wxOwnerDrawn::GetFontToUse() const
165 {
166 wxFont font = m_font;
167 if ( !font.Ok() )
168 {
169 if ( IsMenuItem() )
170 font = *wxMSWSystemMenuFontModule::ms_systemMenuFont;
171
172 if ( !font.Ok() )
173 font = *wxNORMAL_FONT;
174 }
175
176 return font;
177 }
178
179 // get size of the item
180 // The item size includes the menu string, the accel string,
181 // the bitmap and size for a submenu expansion arrow...
182 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight)
183 {
184 if ( IsOwnerDrawn() )
185 {
186 wxMemoryDC dc;
187
188 wxString str = wxStripMenuCodes(m_strName);
189
190 // if we have a valid accel string, then pad out
191 // the menu string so that the menu and accel string are not
192 // placed on top of each other.
193 if ( !m_strAccel.empty() )
194 {
195 str.Pad(str.length()%8);
196 str += m_strAccel;
197 }
198
199 dc.SetFont(GetFontToUse());
200
201 dc.GetTextExtent(str, (long *)pwidth, (long *)pheight);
202
203 // add space at the end of the menu for the submenu expansion arrow
204 // this will also allow offsetting the accel string from the right edge
205 *pwidth += GetMarginWidth() + 16;
206 }
207 else // don't draw the text, just the bitmap (if any)
208 {
209 *pwidth =
210 *pheight = 0;
211 }
212
213 // increase size to accommodate bigger bitmaps if necessary
214 if (m_bmpChecked.Ok())
215 {
216 // Is BMP height larger then text height?
217 size_t adjustedHeight = m_bmpChecked.GetHeight() +
218 2*wxSystemSettings::GetMetric(wxSYS_EDGE_Y);
219 if (*pheight < adjustedHeight)
220 *pheight = adjustedHeight;
221
222 const size_t widthBmp = m_bmpChecked.GetWidth();
223 if ( IsOwnerDrawn() )
224 {
225 // widen the margin to fit the bitmap if necessary
226 if ((size_t)GetMarginWidth() < widthBmp)
227 SetMarginWidth(widthBmp);
228 }
229 else // we must allocate enough space for the bitmap
230 {
231 *pwidth += widthBmp;
232 }
233 }
234
235 // add a 4-pixel separator, otherwise menus look cluttered
236 *pwidth += 4;
237
238 // make sure that this item is at least as tall as the system menu height
239 if ( *pheight < m_nMinHeight )
240 *pheight = m_nMinHeight;
241
242 // remember height for use in OnDrawItem
243 m_nHeight = *pheight;
244
245 return true;
246 }
247
248 // draw the item
249 bool wxOwnerDrawn::OnDrawItem(wxDC& dc,
250 const wxRect& rc,
251 wxODAction act,
252 wxODStatus st)
253 {
254 // we do nothing on focus change
255 if ( act == wxODFocusChanged )
256 return true;
257
258
259 // this flag determines whether or not an edge will
260 // be drawn around the bitmap. In most "windows classic"
261 // applications, a 1-pixel highlight edge is drawn around
262 // the bitmap of an item when it is selected. However,
263 // with the new "luna" theme, no edge is drawn around
264 // the bitmap because the background is white (this applies
265 // only to "non-XP style" menus w/ bitmaps --
266 // see IE 6 menus for an example)
267
268 bool draw_bitmap_edge = true;
269
270 // set the colors
271 // --------------
272 DWORD colBack, colText;
273 if ( st & wxODSelected )
274 {
275 colBack = GetSysColor(COLOR_HIGHLIGHT);
276 if (!(st & wxODDisabled))
277 {
278 colText = GetSysColor(COLOR_HIGHLIGHTTEXT);
279 }
280 else
281 {
282 colText = GetSysColor(COLOR_GRAYTEXT);
283 }
284 }
285 else
286 {
287 // fall back to default colors if none explicitly specified
288 colBack = m_colBack.Ok() ? wxColourToPalRGB(m_colBack)
289 : GetSysColor(COLOR_MENU);
290 colText = m_colText.Ok() ? wxColourToPalRGB(m_colText)
291 : GetSysColor(COLOR_MENUTEXT);
292 }
293
294 if ( IsOwnerDrawn() )
295 {
296 // don't draw an edge around the bitmap, if background is white ...
297 DWORD menu_bg_color = GetSysColor(COLOR_MENU);
298 if ( ( GetRValue( menu_bg_color ) >= 0xf0 &&
299 GetGValue( menu_bg_color ) >= 0xf0 &&
300 GetBValue( menu_bg_color ) >= 0xf0 )
301 )
302 {
303 draw_bitmap_edge = false;
304 }
305 }
306 else // edge doesn't look well with default Windows drawing
307 {
308 draw_bitmap_edge = false;
309 }
310
311
312 HDC hdc = GetHdcOf(dc);
313 COLORREF colOldText = ::SetTextColor(hdc, colText),
314 colOldBack = ::SetBkColor(hdc, colBack);
315
316 // *2, as in wxSYS_EDGE_Y
317 int margin = GetMarginWidth() + 2 * wxSystemSettings::GetMetric(wxSYS_EDGE_X);
318
319 // select the font and draw the text
320 // ---------------------------------
321
322
323 // determine where to draw and leave space for a check-mark.
324 // + 1 pixel to separate the edge from the highlight rectangle
325 int xText = rc.x + margin + 1;
326
327
328 // using native API because it recognizes '&'
329 if ( IsOwnerDrawn() )
330 {
331 int nPrevMode = SetBkMode(hdc, TRANSPARENT);
332 AutoHBRUSH hbr(colBack);
333 SelectInHDC selBrush(hdc, hbr);
334
335 RECT rectFill = { rc.GetLeft(), rc.GetTop(),
336 rc.GetRight() + 1, rc.GetBottom() + 1 };
337
338 if ( (st & wxODSelected) && m_bmpChecked.Ok() && draw_bitmap_edge )
339 {
340 // only draw the highlight under the text, not under
341 // the bitmap or checkmark
342 rectFill.left = xText;
343 }
344
345 FillRect(hdc, &rectFill, hbr);
346
347 // use default font if no font set
348 wxFont fontToUse = GetFontToUse();
349 SelectInHDC selFont(hdc, GetHfontOf(fontToUse));
350
351 wxString strMenuText = m_strName.BeforeFirst('\t');
352
353 xText += 3; // separate text from the highlight rectangle
354
355 SIZE sizeRect;
356 ::GetTextExtentPoint32(hdc, strMenuText.c_str(), strMenuText.length(), &sizeRect);
357 ::DrawState(hdc, NULL, NULL,
358 (LPARAM)strMenuText.wx_str(),
359 strMenuText.length(),
360 xText, rc.y + (int) ((rc.GetHeight()-sizeRect.cy)/2.0), // centre text vertically
361 rc.GetWidth()-margin, sizeRect.cy,
362 DST_PREFIXTEXT |
363 (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0) |
364 (((st & wxODHidePrefix) && !wxMSWSystemMenuFontModule::ms_showCues) ? 512 : 0)); // 512 == DSS_HIDEPREFIX
365
366 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
367 // as the last parameter in DrawState() (at least with Windows98). So we have
368 // to take care of right alignment ourselves.
369 if ( !m_strAccel.empty() )
370 {
371 int accel_width, accel_height;
372 dc.GetTextExtent(m_strAccel, &accel_width, &accel_height);
373 // right align accel string with right edge of menu ( offset by the
374 // margin width )
375 ::DrawState(hdc, NULL, NULL,
376 (LPARAM)m_strAccel.wx_str(),
377 m_strAccel.length(),
378 rc.GetWidth()-16-accel_width, rc.y+(int) ((rc.GetHeight()-sizeRect.cy)/2.0),
379 0, 0,
380 DST_TEXT |
381 (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0));
382 }
383
384 (void)SetBkMode(hdc, nPrevMode);
385 }
386
387
388 // draw the bitmap
389 // ---------------
390 if ( IsCheckable() && !m_bmpChecked.Ok() )
391 {
392 if ( st & wxODChecked )
393 {
394 // what goes on: DrawFrameControl creates a b/w mask,
395 // then we copy it to screen to have right colors
396
397 // first create a monochrome bitmap in a memory DC
398 HDC hdcMem = CreateCompatibleDC(hdc);
399 HBITMAP hbmpCheck = CreateBitmap(margin, m_nHeight, 1, 1, 0);
400 SelectObject(hdcMem, hbmpCheck);
401
402 // then draw a check mark into it
403 RECT rect = { 0, 0, margin, m_nHeight };
404 if ( m_nHeight > 0 )
405 {
406 ::DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
407 }
408
409 // finally copy it to screen DC and clean up
410 BitBlt(hdc, rc.x, rc.y, margin, m_nHeight, hdcMem, 0, 0, SRCCOPY);
411
412 DeleteDC(hdcMem);
413 DeleteObject(hbmpCheck);
414 }
415 }
416 else
417 {
418 wxBitmap bmp;
419
420 if ( st & wxODDisabled )
421 {
422 bmp = GetDisabledBitmap();
423 }
424
425 if ( !bmp.Ok() )
426 {
427 // for not checkable bitmaps we should always use unchecked one
428 // because their checked bitmap is not set
429 bmp = GetBitmap(!IsCheckable() || (st & wxODChecked));
430
431 #if wxUSE_IMAGE
432 if ( bmp.Ok() && st & wxODDisabled )
433 {
434 // we need to grey out the bitmap as we don't have any specific
435 // disabled bitmap
436 wxImage imgGrey = bmp.ConvertToImage().ConvertToGreyscale();
437 if ( imgGrey.Ok() )
438 bmp = wxBitmap(imgGrey);
439 }
440 #endif // wxUSE_IMAGE
441 }
442
443 if ( bmp.Ok() )
444 {
445 wxMemoryDC dcMem(&dc);
446 dcMem.SelectObjectAsSource(bmp);
447
448 // center bitmap
449 int nBmpWidth = bmp.GetWidth(),
450 nBmpHeight = bmp.GetHeight();
451
452 // there should be enough space!
453 wxASSERT((nBmpWidth <= rc.GetWidth()) && (nBmpHeight <= rc.GetHeight()));
454
455 int heightDiff = m_nHeight - nBmpHeight;
456 dc.Blit(rc.x + (margin - nBmpWidth) / 2,
457 rc.y + heightDiff / 2,
458 nBmpWidth, nBmpHeight,
459 &dcMem, 0, 0, wxCOPY, true /* use mask */);
460
461 if ( ( st & wxODSelected ) && !( st & wxODDisabled ) && draw_bitmap_edge )
462 {
463 RECT rectBmp = { rc.GetLeft(), rc.GetTop(),
464 rc.GetLeft() + margin,
465 rc.GetTop() + m_nHeight };
466 SetBkColor(hdc, colBack);
467
468 DrawEdge(hdc, &rectBmp, BDR_RAISEDINNER, BF_RECT);
469 }
470 }
471 }
472
473 ::SetTextColor(hdc, colOldText);
474 ::SetBkColor(hdc, colOldBack);
475
476 return true;
477 }
478
479
480 #endif // wxUSE_OWNER_DRAWN