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