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