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