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