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