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