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