]> git.saurik.com Git - wxWidgets.git/blame - src/msw/ownerdrw.cpp
don't use wxSubwindows with a single element to store the dummy radio item, store...
[wxWidgets.git] / src / msw / ownerdrw.cpp
CommitLineData
2bda0e17 1///////////////////////////////////////////////////////////////////////////////
f38924e8 2// Name: src/msw/ownerdrw.cpp
2bda0e17
KB
3// Purpose: implementation of wxOwnerDrawn class
4// Author: Vadim Zeitlin
33ac7e6f 5// Modified by:
2bda0e17
KB
6// Created: 13.11.97
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10///////////////////////////////////////////////////////////////////////////////
11
2bda0e17
KB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
f38924e8 16 #pragma hdrstop
2bda0e17
KB
17#endif
18
19#ifndef WX_PRECOMP
f38924e8
WS
20 #include "wx/window.h"
21 #include "wx/msw/private.h"
22 #include "wx/font.h"
23 #include "wx/bitmap.h"
4d1f8403 24 #include "wx/image.h"
f38924e8
WS
25 #include "wx/dcmemory.h"
26 #include "wx/menu.h"
27 #include "wx/utils.h"
9eddec69 28 #include "wx/settings.h"
25466131 29 #include "wx/menuitem.h"
02761f6c 30 #include "wx/module.h"
2bda0e17
KB
31#endif
32
33#include "wx/ownerdrw.h"
d9cf726b 34#include "wx/fontutil.h"
2bda0e17 35
1b24a68e
GRG
36#if wxUSE_OWNER_DRAWN
37
8ee64db5
JS
38#ifndef SPI_GETKEYBOARDCUES
39#define SPI_GETKEYBOARDCUES 0x100A
40#endif
41
cfbf444a
JS
42class wxMSWSystemMenuFontModule : public wxModule
43{
44public:
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);
10403254 53 SystemParametersInfo(SPI_GETNONCLIENTMETRICS,0,&nm,0);
cfbf444a
JS
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);
51d2fa37
VZ
62
63 if (SystemParametersInfo(SPI_GETKEYBOARDCUES, 0, &ms_showCues, 0) == 0)
64 ms_showCues = true;
cfbf444a
JS
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
51d2fa37 79 static bool ms_showCues;
cfbf444a
JS
80private:
81 DECLARE_DYNAMIC_CLASS(wxMSWSystemMenuFontModule)
82};
83
3103e8a9 84// these static variables are from the wxMSWSystemMenuFontModule object
cfbf444a
JS
85// and reflect the system settings returned by the Win32 API's
86// SystemParametersInfo() call.
87
88wxFont* wxMSWSystemMenuFontModule::ms_systemMenuFont = NULL;
89int wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth = 18; // windows clean install default
90int wxMSWSystemMenuFontModule::ms_systemMenuHeight = 18; // windows clean install default
51d2fa37 91bool wxMSWSystemMenuFontModule::ms_showCues = true;
cfbf444a
JS
92
93IMPLEMENT_DYNAMIC_CLASS(wxMSWSystemMenuFontModule, wxModule)
2432b92d 94
810ca882 95
9ba4b498
VZ
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
8d7eaf91
MW
103#if defined __VISUALC__ && __VISUALC__ <= 1300
104 #if __VISUALC__ >= 1200
9ba4b498 105 #pragma warning(push)
9ba4b498
VZ
106 #define POP_WARNINGS
107 #endif
8d7eaf91 108 #pragma warning(disable: 4284)
9ba4b498
VZ
109#endif
110
810ca882 111#include "wx/hashset.h"
3f5c62f9 112WX_DECLARE_HASH_SET(wxOwnerDrawn*, wxPointerHash, wxPointerEqual, OwnerDrawnSet);
810ca882 113
9ba4b498
VZ
114#ifdef POP_WARNINGS
115 #pragma warning(pop)
116#endif
117
2bda0e17
KB
118// ============================================================================
119// implementation of wxOwnerDrawn class
120// ============================================================================
121
122// ctor
123// ----
33ac7e6f 124wxOwnerDrawn::wxOwnerDrawn(const wxString& str,
810ca882
VZ
125 bool bCheckable,
126 bool bMenuItem)
2bda0e17
KB
127 : m_strName(str)
128{
4e47fd5a 129 if ( ms_nDefaultMarginWidth == 0 )
d9cf726b 130 {
810ca882
VZ
131 ms_nDefaultMarginWidth = ::GetSystemMetrics(SM_CXMENUCHECK) +
132 wxSystemSettings::GetMetric(wxSYS_EDGE_X);
271fa250 133 ms_nLastMarginWidth = ms_nDefaultMarginWidth;
d9cf726b
JS
134 }
135
d9cf726b 136 m_bCheckable = bCheckable;
078cf5cb 137 m_bOwnerDrawn = false;
4e47fd5a 138 m_isMenuItem = bMenuItem;
d9cf726b
JS
139 m_nHeight = 0;
140 m_nMarginWidth = ms_nLastMarginWidth;
cfbf444a 141 m_nMinHeight = wxMSWSystemMenuFontModule::ms_systemMenuHeight;
810ca882
VZ
142}
143
144wxOwnerDrawn::~wxOwnerDrawn()
145{
810ca882
VZ
146}
147
148bool wxOwnerDrawn::IsMenuItem() const
149{
4e47fd5a 150 return m_isMenuItem;
d9cf726b
JS
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,
10403254 156// which is a chunk of code
d9cf726b
JS
157size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 0;
158size_t wxOwnerDrawn::ms_nLastMarginWidth = 0;
159
2bda0e17
KB
160
161// drawing
162// -------
163
810ca882
VZ
164wxFont 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
2bda0e17 179// get size of the item
2a2a71e3
JS
180// The item size includes the menu string, the accel string,
181// the bitmap and size for a submenu expansion arrow...
c86f1403 182bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight)
2bda0e17 183{
810ca882
VZ
184 if ( IsOwnerDrawn() )
185 {
186 wxMemoryDC dc;
2bda0e17 187
810ca882 188 wxString str = wxStripMenuCodes(m_strName);
2bda0e17 189
810ca882
VZ
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 {
f38924e8 195 str.Pad(str.length()%8);
810ca882
VZ
196 str += m_strAccel;
197 }
2bda0e17 198
810ca882 199 dc.SetFont(GetFontToUse());
e7dd6ecd 200
36866abc
PC
201 wxCoord w, h;
202 dc.GetTextExtent(str, &w, &h);
203 *pwidth = w;
204 *pheight = h;
c7527e3f 205
810ca882
VZ
206 // add space at the end of the menu for the submenu expansion arrow
207 // this will also allow offsetting the accel string from the right edge
208 *pwidth += GetMarginWidth() + 16;
209 }
210 else // don't draw the text, just the bitmap (if any)
211 {
212 *pwidth =
213 *pheight = 0;
214 }
33ac7e6f 215
810ca882
VZ
216 // increase size to accommodate bigger bitmaps if necessary
217 if (m_bmpChecked.Ok())
218 {
219 // Is BMP height larger then text height?
220 size_t adjustedHeight = m_bmpChecked.GetHeight() +
221 2*wxSystemSettings::GetMetric(wxSYS_EDGE_Y);
222 if (*pheight < adjustedHeight)
223 *pheight = adjustedHeight;
224
225 const size_t widthBmp = m_bmpChecked.GetWidth();
226 if ( IsOwnerDrawn() )
227 {
228 // widen the margin to fit the bitmap if necessary
229 if ((size_t)GetMarginWidth() < widthBmp)
230 SetMarginWidth(widthBmp);
231 }
232 else // we must allocate enough space for the bitmap
233 {
234 *pwidth += widthBmp;
235 }
236 }
51d2fa37 237
810ca882
VZ
238 // add a 4-pixel separator, otherwise menus look cluttered
239 *pwidth += 4;
51d2fa37 240
810ca882
VZ
241 // make sure that this item is at least as tall as the system menu height
242 if ( *pheight < m_nMinHeight )
243 *pheight = m_nMinHeight;
d9cf726b 244
810ca882
VZ
245 // remember height for use in OnDrawItem
246 m_nHeight = *pheight;
2bda0e17 247
810ca882 248 return true;
2bda0e17
KB
249}
250
2bda0e17 251// draw the item
6d5b2a57
VZ
252bool wxOwnerDrawn::OnDrawItem(wxDC& dc,
253 const wxRect& rc,
254 wxODAction act,
255 wxODStatus st)
2bda0e17 256{
92218ce6
WS
257 // we do nothing on focus change
258 if ( act == wxODFocusChanged )
259 return true;
2bda0e17 260
c0cb30dc 261
92218ce6
WS
262 // this flag determines whether or not an edge will
263 // be drawn around the bitmap. In most "windows classic"
264 // applications, a 1-pixel highlight edge is drawn around
265 // the bitmap of an item when it is selected. However,
266 // with the new "luna" theme, no edge is drawn around
267 // the bitmap because the background is white (this applies
268 // only to "non-XP style" menus w/ bitmaps --
269 // see IE 6 menus for an example)
c0cb30dc 270
92218ce6 271 bool draw_bitmap_edge = true;
c0cb30dc 272
92218ce6
WS
273 // set the colors
274 // --------------
275 DWORD colBack, colText;
276 if ( st & wxODSelected )
5ac7be7a 277 {
92218ce6
WS
278 colBack = GetSysColor(COLOR_HIGHLIGHT);
279 if (!(st & wxODDisabled))
280 {
281 colText = GetSysColor(COLOR_HIGHLIGHTTEXT);
282 }
283 else
284 {
285 colText = GetSysColor(COLOR_GRAYTEXT);
286 }
5ac7be7a 287 }
92218ce6 288 else
5ac7be7a 289 {
92218ce6
WS
290 // fall back to default colors if none explicitly specified
291 colBack = m_colBack.Ok() ? wxColourToPalRGB(m_colBack)
292 : GetSysColor(COLOR_MENU);
293 colText = m_colText.Ok() ? wxColourToPalRGB(m_colText)
294 : GetSysColor(COLOR_MENUTEXT);
295 }
296
297 if ( IsOwnerDrawn() )
298 {
299 // don't draw an edge around the bitmap, if background is white ...
300 DWORD menu_bg_color = GetSysColor(COLOR_MENU);
301 if ( ( GetRValue( menu_bg_color ) >= 0xf0 &&
302 GetGValue( menu_bg_color ) >= 0xf0 &&
303 GetBValue( menu_bg_color ) >= 0xf0 )
304 )
305 {
306 draw_bitmap_edge = false;
307 }
5ac7be7a 308 }
92218ce6 309 else // edge doesn't look well with default Windows drawing
39dcd515
VZ
310 {
311 draw_bitmap_edge = false;
312 }
c0cb30dc 313
7230716d 314
888dde65
RR
315 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
316 HDC hdc = GetHdcOf(*impl);
92218ce6
WS
317 COLORREF colOldText = ::SetTextColor(hdc, colText),
318 colOldBack = ::SetBkColor(hdc, colBack);
2bda0e17 319
92218ce6
WS
320 // *2, as in wxSYS_EDGE_Y
321 int margin = GetMarginWidth() + 2 * wxSystemSettings::GetMetric(wxSYS_EDGE_X);
2bda0e17 322
92218ce6
WS
323 // select the font and draw the text
324 // ---------------------------------
d9cf726b 325
d9cf726b 326
92218ce6
WS
327 // determine where to draw and leave space for a check-mark.
328 // + 1 pixel to separate the edge from the highlight rectangle
329 int xText = rc.x + margin + 1;
2bda0e17 330
d9cf726b 331
92218ce6
WS
332 // using native API because it recognizes '&'
333 if ( IsOwnerDrawn() )
334 {
335 int nPrevMode = SetBkMode(hdc, TRANSPARENT);
336 AutoHBRUSH hbr(colBack);
337 SelectInHDC selBrush(hdc, hbr);
d9cf726b 338
92218ce6
WS
339 RECT rectFill = { rc.GetLeft(), rc.GetTop(),
340 rc.GetRight() + 1, rc.GetBottom() + 1 };
2bda0e17 341
92218ce6
WS
342 if ( (st & wxODSelected) && m_bmpChecked.Ok() && draw_bitmap_edge )
343 {
344 // only draw the highlight under the text, not under
345 // the bitmap or checkmark
346 rectFill.left = xText;
347 }
2bda0e17 348
92218ce6 349 FillRect(hdc, &rectFill, hbr);
f44b4495 350
92218ce6
WS
351 // use default font if no font set
352 wxFont fontToUse = GetFontToUse();
353 SelectInHDC selFont(hdc, GetHfontOf(fontToUse));
d9cf726b 354
92218ce6 355 wxString strMenuText = m_strName.BeforeFirst('\t');
6d5b2a57 356
92218ce6 357 xText += 3; // separate text from the highlight rectangle
51d2fa37 358
92218ce6 359 SIZE sizeRect;
f38924e8 360 ::GetTextExtentPoint32(hdc, strMenuText.c_str(), strMenuText.length(), &sizeRect);
810ca882 361 ::DrawState(hdc, NULL, NULL,
c9f78968
VS
362 (LPARAM)strMenuText.wx_str(),
363 strMenuText.length(),
92218ce6
WS
364 xText, rc.y + (int) ((rc.GetHeight()-sizeRect.cy)/2.0), // centre text vertically
365 rc.GetWidth()-margin, sizeRect.cy,
366 DST_PREFIXTEXT |
367 (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0) |
368 (((st & wxODHidePrefix) && !wxMSWSystemMenuFontModule::ms_showCues) ? 512 : 0)); // 512 == DSS_HIDEPREFIX
369
370 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
371 // as the last parameter in DrawState() (at least with Windows98). So we have
372 // to take care of right alignment ourselves.
373 if ( !m_strAccel.empty() )
374 {
375 int accel_width, accel_height;
376 dc.GetTextExtent(m_strAccel, &accel_width, &accel_height);
377 // right align accel string with right edge of menu ( offset by the
378 // margin width )
379 ::DrawState(hdc, NULL, NULL,
c9f78968
VS
380 (LPARAM)m_strAccel.wx_str(),
381 m_strAccel.length(),
92218ce6
WS
382 rc.GetWidth()-16-accel_width, rc.y+(int) ((rc.GetHeight()-sizeRect.cy)/2.0),
383 0, 0,
384 DST_TEXT |
385 (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0));
386 }
07cf98cb 387
92218ce6 388 (void)SetBkMode(hdc, nPrevMode);
2bda0e17 389 }
cb0bcdd6 390
cb0bcdd6 391
92218ce6
WS
392 // draw the bitmap
393 // ---------------
394 if ( IsCheckable() && !m_bmpChecked.Ok() )
cb0bcdd6 395 {
92218ce6
WS
396 if ( st & wxODChecked )
397 {
398 // what goes on: DrawFrameControl creates a b/w mask,
399 // then we copy it to screen to have right colors
400
401 // first create a monochrome bitmap in a memory DC
402 HDC hdcMem = CreateCompatibleDC(hdc);
403 HBITMAP hbmpCheck = CreateBitmap(margin, m_nHeight, 1, 1, 0);
404 SelectObject(hdcMem, hbmpCheck);
405
406 // then draw a check mark into it
407 RECT rect = { 0, 0, margin, m_nHeight };
408 if ( m_nHeight > 0 )
409 {
410 ::DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
411 }
412
413 // finally copy it to screen DC and clean up
414 BitBlt(hdc, rc.x, rc.y, margin, m_nHeight, hdcMem, 0, 0, SRCCOPY);
415
416 DeleteDC(hdcMem);
417 DeleteObject(hbmpCheck);
418 }
cb0bcdd6 419 }
92218ce6
WS
420 else
421 {
422 wxBitmap bmp;
cb0bcdd6 423
92218ce6
WS
424 if ( st & wxODDisabled )
425 {
426 bmp = GetDisabledBitmap();
427 }
2bda0e17 428
92218ce6
WS
429 if ( !bmp.Ok() )
430 {
0d3997fd
VZ
431 // for not checkable bitmaps we should always use unchecked one
432 // because their checked bitmap is not set
92218ce6 433 bmp = GetBitmap(!IsCheckable() || (st & wxODChecked));
0d3997fd
VZ
434
435#if wxUSE_IMAGE
436 if ( bmp.Ok() && st & wxODDisabled )
437 {
438 // we need to grey out the bitmap as we don't have any specific
439 // disabled bitmap
440 wxImage imgGrey = bmp.ConvertToImage().ConvertToGreyscale();
441 if ( imgGrey.Ok() )
442 bmp = wxBitmap(imgGrey);
443 }
444#endif // wxUSE_IMAGE
92218ce6 445 }
7230716d 446
92218ce6
WS
447 if ( bmp.Ok() )
448 {
449 wxMemoryDC dcMem(&dc);
5d7eebb6 450 dcMem.SelectObjectAsSource(bmp);
92218ce6
WS
451
452 // center bitmap
453 int nBmpWidth = bmp.GetWidth(),
454 nBmpHeight = bmp.GetHeight();
455
456 // there should be enough space!
457 wxASSERT((nBmpWidth <= rc.GetWidth()) && (nBmpHeight <= rc.GetHeight()));
458
459 int heightDiff = m_nHeight - nBmpHeight;
460 dc.Blit(rc.x + (margin - nBmpWidth) / 2,
461 rc.y + heightDiff / 2,
462 nBmpWidth, nBmpHeight,
463 &dcMem, 0, 0, wxCOPY, true /* use mask */);
464
465 if ( ( st & wxODSelected ) && !( st & wxODDisabled ) && draw_bitmap_edge )
466 {
467 RECT rectBmp = { rc.GetLeft(), rc.GetTop(),
468 rc.GetLeft() + margin,
469 rc.GetTop() + m_nHeight };
470 SetBkColor(hdc, colBack);
471
472 DrawEdge(hdc, &rectBmp, BDR_RAISEDINNER, BF_RECT);
473 }
474 }
2bda0e17 475 }
2bda0e17 476
92218ce6
WS
477 ::SetTextColor(hdc, colOldText);
478 ::SetBkColor(hdc, colOldBack);
2bda0e17 479
92218ce6 480 return true;
2bda0e17
KB
481}
482
1b24a68e
GRG
483
484#endif // wxUSE_OWNER_DRAWN