]> git.saurik.com Git - wxWidgets.git/blame - src/msw/ownerdrw.cpp
Make sure text part of combobox is enabled/disabled too
[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"
173 return gs_menuItems.count(this) == 1;
d9cf726b
JS
174}
175
176
177// these items will be set during the first invocation of the c'tor,
178// because the values will be determined by checking the system settings,
10403254 179// which is a chunk of code
d9cf726b
JS
180size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 0;
181size_t wxOwnerDrawn::ms_nLastMarginWidth = 0;
182
2bda0e17
KB
183
184// drawing
185// -------
186
810ca882
VZ
187wxFont wxOwnerDrawn::GetFontToUse() const
188{
189 wxFont font = m_font;
190 if ( !font.Ok() )
191 {
192 if ( IsMenuItem() )
193 font = *wxMSWSystemMenuFontModule::ms_systemMenuFont;
194
195 if ( !font.Ok() )
196 font = *wxNORMAL_FONT;
197 }
198
199 return font;
200}
201
2bda0e17 202// get size of the item
2a2a71e3
JS
203// The item size includes the menu string, the accel string,
204// the bitmap and size for a submenu expansion arrow...
c86f1403 205bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight)
2bda0e17 206{
810ca882
VZ
207 if ( IsOwnerDrawn() )
208 {
209 wxMemoryDC dc;
2bda0e17 210
810ca882 211 wxString str = wxStripMenuCodes(m_strName);
2bda0e17 212
810ca882
VZ
213 // if we have a valid accel string, then pad out
214 // the menu string so that the menu and accel string are not
215 // placed on top of each other.
216 if ( !m_strAccel.empty() )
217 {
218 str.Pad(str.Length()%8);
219 str += m_strAccel;
220 }
2bda0e17 221
810ca882 222 dc.SetFont(GetFontToUse());
e7dd6ecd 223
810ca882 224 dc.GetTextExtent(str, (long *)pwidth, (long *)pheight);
c7527e3f 225
810ca882
VZ
226 // add space at the end of the menu for the submenu expansion arrow
227 // this will also allow offsetting the accel string from the right edge
228 *pwidth += GetMarginWidth() + 16;
229 }
230 else // don't draw the text, just the bitmap (if any)
231 {
232 *pwidth =
233 *pheight = 0;
234 }
33ac7e6f 235
810ca882
VZ
236 // increase size to accommodate bigger bitmaps if necessary
237 if (m_bmpChecked.Ok())
238 {
239 // Is BMP height larger then text height?
240 size_t adjustedHeight = m_bmpChecked.GetHeight() +
241 2*wxSystemSettings::GetMetric(wxSYS_EDGE_Y);
242 if (*pheight < adjustedHeight)
243 *pheight = adjustedHeight;
244
245 const size_t widthBmp = m_bmpChecked.GetWidth();
246 if ( IsOwnerDrawn() )
247 {
248 // widen the margin to fit the bitmap if necessary
249 if ((size_t)GetMarginWidth() < widthBmp)
250 SetMarginWidth(widthBmp);
251 }
252 else // we must allocate enough space for the bitmap
253 {
254 *pwidth += widthBmp;
255 }
256 }
51d2fa37 257
810ca882
VZ
258 // add a 4-pixel separator, otherwise menus look cluttered
259 *pwidth += 4;
51d2fa37 260
810ca882
VZ
261 // make sure that this item is at least as tall as the system menu height
262 if ( *pheight < m_nMinHeight )
263 *pheight = m_nMinHeight;
d9cf726b 264
810ca882
VZ
265 // remember height for use in OnDrawItem
266 m_nHeight = *pheight;
2bda0e17 267
810ca882 268 return true;
2bda0e17
KB
269}
270
2bda0e17 271// draw the item
6d5b2a57
VZ
272bool wxOwnerDrawn::OnDrawItem(wxDC& dc,
273 const wxRect& rc,
274 wxODAction act,
275 wxODStatus st)
2bda0e17
KB
276{
277 // we do nothing on focus change
278 if ( act == wxODFocusChanged )
078cf5cb 279 return true;
2bda0e17 280
c0cb30dc
JS
281
282 // this flag determines whether or not an edge will
283 // be drawn around the bitmap. In most "windows classic"
284 // applications, a 1-pixel highlight edge is drawn around
285 // the bitmap of an item when it is selected. However,
286 // with the new "luna" theme, no edge is drawn around
287 // the bitmap because the background is white (this applies
288 // only to "non-XP style" menus w/ bitmaps --
289 // see IE 6 menus for an example)
290
291 bool draw_bitmap_edge = true;
292
2bda0e17
KB
293 // set the colors
294 // --------------
295 DWORD colBack, colText;
296 if ( st & wxODSelected ) {
297 colBack = GetSysColor(COLOR_HIGHLIGHT);
5ac7be7a
JS
298 if (!(st & wxODDisabled))
299 {
300 colText = GetSysColor(COLOR_HIGHLIGHTTEXT);
301 }
302 else
303 {
304 colText = GetSysColor(COLOR_GRAYTEXT);
305 }
2bda0e17
KB
306 }
307 else {
308 // fall back to default colors if none explicitly specified
7230716d 309 colBack = m_colBack.Ok() ? wxColourToPalRGB(m_colBack)
51d2fa37 310 : GetSysColor(COLOR_MENU);
7230716d 311 colText = m_colText.Ok() ? wxColourToPalRGB(m_colText)
51d2fa37 312 : GetSysColor(COLOR_MENUTEXT);
2bda0e17 313 }
33ac7e6f 314
39dcd515
VZ
315 if ( IsOwnerDrawn() )
316 {
317 // don't draw an edge around the bitmap, if background is white ...
318 DWORD menu_bg_color = GetSysColor(COLOR_MENU);
319 if ( ( GetRValue( menu_bg_color ) >= 0xf0 &&
320 GetGValue( menu_bg_color ) >= 0xf0 &&
321 GetBValue( menu_bg_color ) >= 0xf0 )
322 )
323 {
324 draw_bitmap_edge = false;
325 }
326 }
327 else // edge doesn't look well with default Windows drawing
c0cb30dc 328 {
39dcd515 329 draw_bitmap_edge = false;
c0cb30dc
JS
330 }
331
332
7230716d
VZ
333 HDC hdc = GetHdcOf(dc);
334 COLORREF colOldText = ::SetTextColor(hdc, colText),
335 colOldBack = ::SetBkColor(hdc, colBack);
336
51d2fa37
VZ
337 // *2, as in wxSYS_EDGE_Y
338 int margin = GetMarginWidth() + 2 * wxSystemSettings::GetMetric(wxSYS_EDGE_X);
2bda0e17
KB
339
340 // select the font and draw the text
341 // ---------------------------------
342
d9cf726b 343
33ac7e6f 344 // determine where to draw and leave space for a check-mark.
51d2fa37
VZ
345 // + 1 pixel to separate the edge from the highlight rectangle
346 int xText = rc.x + margin + 1;
d9cf726b 347
2bda0e17 348
3103e8a9 349 // using native API because it recognizes '&'
810ca882
VZ
350 if ( IsOwnerDrawn() )
351 {
352 int nPrevMode = SetBkMode(hdc, TRANSPARENT);
353 AutoHBRUSH hbr(colBack);
354 SelectInHDC selBrush(hdc, hbr);
d9cf726b 355
810ca882
VZ
356 RECT rectFill = { rc.GetLeft(), rc.GetTop(),
357 rc.GetRight() + 1, rc.GetBottom() + 1 };
d9cf726b 358
810ca882
VZ
359 if ( (st & wxODSelected) && m_bmpChecked.Ok() && draw_bitmap_edge ) {
360 // only draw the highlight under the text, not under
361 // the bitmap or checkmark
362 rectFill.left = xText;
363 }
2bda0e17 364
810ca882 365 FillRect(hdc, &rectFill, hbr);
2bda0e17 366
810ca882
VZ
367 // use default font if no font set
368 wxFont fontToUse = GetFontToUse();
369 SelectInHDC selFont(hdc, GetHfontOf(fontToUse));
f44b4495 370
810ca882 371 wxString strMenuText = m_strName.BeforeFirst('\t');
d9cf726b 372
810ca882 373 xText += 3; // separate text from the highlight rectangle
6d5b2a57 374
810ca882
VZ
375 SIZE sizeRect;
376 ::GetTextExtentPoint32(hdc, strMenuText.c_str(), strMenuText.Length(), &sizeRect);
377 ::DrawState(hdc, NULL, NULL,
378 (LPARAM)strMenuText.c_str(), strMenuText.length(),
379 xText, rc.y + (int) ((rc.GetHeight()-sizeRect.cy)/2.0), // centre text vertically
380 rc.GetWidth()-margin, sizeRect.cy,
381 DST_PREFIXTEXT |
382 (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0) |
383 (((st & wxODHidePrefix) && !wxMSWSystemMenuFontModule::ms_showCues) ? 512 : 0)); // 512 == DSS_HIDEPREFIX
51d2fa37 384
810ca882
VZ
385 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
386 // as the last parameter in DrawState() (at least with Windows98). So we have
387 // to take care of right alignment ourselves.
388 if ( !m_strAccel.empty() )
389 {
390 int accel_width, accel_height;
391 dc.GetTextExtent(m_strAccel, &accel_width, &accel_height);
392 // right align accel string with right edge of menu ( offset by the
393 // margin width )
394 ::DrawState(hdc, NULL, NULL,
395 (LPARAM)m_strAccel.c_str(), m_strAccel.length(),
396 rc.GetWidth()-16-accel_width, rc.y+(int) ((rc.GetHeight()-sizeRect.cy)/2.0),
397 0, 0,
398 DST_TEXT |
399 (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0));
400 }
10403254 401
810ca882 402 (void)SetBkMode(hdc, nPrevMode);
7230716d 403 }
2bda0e17 404
2bda0e17
KB
405
406 // draw the bitmap
407 // ---------------
408 if ( IsCheckable() && !m_bmpChecked.Ok() ) {
409 if ( st & wxODChecked ) {
33ac7e6f 410 // what goes on: DrawFrameControl creates a b/w mask,
2bda0e17
KB
411 // then we copy it to screen to have right colors
412
413 // first create a monochrome bitmap in a memory DC
414 HDC hdcMem = CreateCompatibleDC(hdc);
7230716d 415 HBITMAP hbmpCheck = CreateBitmap(margin, m_nHeight, 1, 1, 0);
2bda0e17
KB
416 SelectObject(hdcMem, hbmpCheck);
417
418 // then draw a check mark into it
7230716d 419 RECT rect = { 0, 0, margin, m_nHeight };
07cf98cb
VZ
420 if ( m_nHeight > 0 )
421 {
f44b4495 422 ::DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
07cf98cb 423 }
2bda0e17
KB
424
425 // finally copy it to screen DC and clean up
7230716d 426 BitBlt(hdc, rc.x, rc.y, margin, m_nHeight,
2bda0e17 427 hdcMem, 0, 0, SRCCOPY);
07cf98cb 428
2bda0e17 429 DeleteDC(hdcMem);
07cf98cb 430 DeleteObject(hbmpCheck);
2bda0e17
KB
431 }
432 }
433 else {
cb0bcdd6
VZ
434 wxBitmap bmp;
435
436 if ( st & wxODDisabled )
437 {
438 bmp = GetDisabledBitmap();
439 }
440
441 if ( !bmp.Ok() )
442 {
443 // for not checkable bitmaps we should always use unchecked one because
444 // their checked bitmap is not set
445 bmp = GetBitmap(!IsCheckable() || (st & wxODChecked));
446 }
447
2bda0e17
KB
448 if ( bmp.Ok() ) {
449 wxMemoryDC dcMem(&dc);
450 dcMem.SelectObject(bmp);
451
452 // center bitmap
453 int nBmpWidth = bmp.GetWidth(),
454 nBmpHeight = bmp.GetHeight();
455
3103e8a9 456 // there should be enough space!
2bda0e17
KB
457 wxASSERT((nBmpWidth <= rc.GetWidth()) && (nBmpHeight <= rc.GetHeight()));
458
f44b4495 459 int heightDiff = m_nHeight - nBmpHeight;
7230716d 460 dc.Blit(rc.x + (margin - nBmpWidth) / 2,
33ac7e6f
KB
461 rc.y + heightDiff / 2,
462 nBmpWidth, nBmpHeight,
078cf5cb 463 &dcMem, 0, 0, wxCOPY, true /* use mask */);
2bda0e17 464
51d2fa37 465 if ( ( st & wxODSelected ) && !( st & wxODDisabled ) && draw_bitmap_edge ) {
2a2a71e3 466 RECT rectBmp = { rc.GetLeft(), rc.GetTop(),
7230716d
VZ
467 rc.GetLeft() + margin,
468 rc.GetTop() + m_nHeight };
2a2a71e3 469 SetBkColor(hdc, colBack);
7230716d 470
51d2fa37 471 DrawEdge(hdc, &rectBmp, BDR_RAISEDINNER, BF_RECT);
2bda0e17
KB
472 }
473 }
474 }
475
7230716d
VZ
476 ::SetTextColor(hdc, colOldText);
477 ::SetBkColor(hdc, colOldBack);
2bda0e17 478
078cf5cb 479 return true;
2bda0e17
KB
480}
481
1b24a68e
GRG
482
483#endif // wxUSE_OWNER_DRAWN
484