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