1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/ownerdrw.cpp
3 // Purpose: implementation of wxOwnerDrawn class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
22 #include "wx/window.h"
24 #include "wx/bitmap.h"
26 #include "wx/dcmemory.h"
29 #include "wx/settings.h"
30 #include "wx/menuitem.h"
31 #include "wx/module.h"
34 #include "wx/ownerdrw.h"
35 #include "wx/fontutil.h"
37 #include "wx/msw/private.h"
39 #ifndef SPI_GETKEYBOARDCUES
40 #define SPI_GETKEYBOARDCUES 0x100A
43 #ifndef DSS_HIDEPREFIX
44 #define DSS_HIDEPREFIX 0x0200
47 class wxMSWSystemMenuFontModule
: public wxModule
57 if ( ms_systemMenuFont
)
59 delete ms_systemMenuFont
;
60 ms_systemMenuFont
= NULL
;
64 static const wxFont
& GetSystemMenuFont()
66 if ( !ms_systemMenuFont
)
69 return *ms_systemMenuFont
;
72 static int GetSystemMenuHeight()
74 if ( !ms_systemMenuHeight
)
77 return ms_systemMenuHeight
;
80 static bool AlwaysShowCues()
82 if ( !ms_systemMenuHeight
)
85 return ms_alwaysShowCues
;
89 static NONCLIENTMETRICS
GetNCM()
91 WinStruct
<NONCLIENTMETRICS
> nm
;
92 if ( !::SystemParametersInfo(SPI_GETNONCLIENTMETRICS
, 0, &nm
, 0) )
95 // a new field has been added to NONCLIENTMETRICS under Vista, so
96 // the call to SystemParametersInfo() fails if we use the struct
97 // size incorporating this new value on an older system -- retry
99 nm
.cbSize
-= sizeof(int);
100 if ( !::SystemParametersInfo(SPI_GETNONCLIENTMETRICS
, 0, &nm
, 0) )
101 #endif // WINVER >= 0x0600
103 // maybe we should initialize the struct with some defaults?
104 wxLogLastError(_T("SystemParametersInfo(SPI_GETNONCLIENTMETRICS)"));
111 static void DoInitMetrics()
113 // iMenuHeight is the menu bar height and the menu items are less tall,
114 // although I don't know by how much -- below is the value for my system
115 ms_systemMenuHeight
= GetNCM().iMenuHeight
- 4;
117 wxASSERT_MSG( ms_systemMenuHeight
> 0,
118 "menu height should be positive" );
120 if ( ::SystemParametersInfo(SPI_GETKEYBOARDCUES
, 0,
121 &ms_alwaysShowCues
, 0) == 0 )
123 // if it's not supported, we must be on an old Windows version
124 // which always shows them
125 ms_alwaysShowCues
= true;
129 static void DoInitFont()
131 ms_systemMenuFont
= new wxFont(wxNativeFontInfo(GetNCM().lfMenuFont
));
134 static wxFont
* ms_systemMenuFont
;
135 static int ms_systemMenuHeight
;
136 static bool ms_alwaysShowCues
;
139 DECLARE_DYNAMIC_CLASS(wxMSWSystemMenuFontModule
)
142 // these static variables are from the wxMSWSystemMenuFontModule object
143 // and reflect the system settings returned by the Win32 API's
144 // SystemParametersInfo() call.
146 wxFont
* wxMSWSystemMenuFontModule::ms_systemMenuFont
= NULL
;
147 int wxMSWSystemMenuFontModule::ms_systemMenuHeight
= 0;
148 bool wxMSWSystemMenuFontModule::ms_alwaysShowCues
= false;
150 IMPLEMENT_DYNAMIC_CLASS(wxMSWSystemMenuFontModule
, wxModule
)
153 // VC++ 6 gives a warning here:
155 // return type for 'OwnerDrawnSet_wxImplementation_HashTable::iterator::
156 // operator ->' is 'class wxOwnerDrawn ** ' (ie; not a UDT or reference to
157 // a UDT. Will produce errors if applied using infix notation.
160 #if defined __VISUALC__ && __VISUALC__ <= 1300
161 #if __VISUALC__ >= 1200
162 #pragma warning(push)
165 #pragma warning(disable: 4284)
168 #include "wx/hashset.h"
169 WX_DECLARE_HASH_SET(wxOwnerDrawn
*, wxPointerHash
, wxPointerEqual
, OwnerDrawnSet
);
175 // ============================================================================
176 // implementation of wxOwnerDrawn class
177 // ============================================================================
181 wxOwnerDrawn::wxOwnerDrawn(const wxString
& str
,
186 if ( ms_nDefaultMarginWidth
== 0 )
188 ms_nDefaultMarginWidth
= ::GetSystemMetrics(SM_CXMENUCHECK
) +
189 wxSystemSettings::GetMetric(wxSYS_EDGE_X
);
190 ms_nLastMarginWidth
= ms_nDefaultMarginWidth
;
193 m_bCheckable
= bCheckable
;
194 m_bOwnerDrawn
= false;
195 m_isMenuItem
= bMenuItem
;
197 m_nMarginWidth
= ms_nLastMarginWidth
;
200 wxOwnerDrawn::~wxOwnerDrawn()
204 bool wxOwnerDrawn::IsMenuItem() const
210 // these items will be set during the first invocation of the ctor,
211 // because the values will be determined by checking the system settings,
212 // which is a chunk of code
213 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 0;
214 size_t wxOwnerDrawn::ms_nLastMarginWidth
= 0;
220 wxFont
wxOwnerDrawn::GetFontToUse() const
222 wxFont font
= m_font
;
226 font
= wxMSWSystemMenuFontModule::GetSystemMenuFont();
229 font
= *wxNORMAL_FONT
;
235 // get size of the item
236 // The item size includes the menu string, the accel string,
237 // the bitmap and size for a submenu expansion arrow...
238 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth
, size_t *pheight
)
240 if ( IsOwnerDrawn() )
244 wxString str
= wxStripMenuCodes(m_strName
);
246 // if we have a valid accel string, then pad out
247 // the menu string so that the menu and accel string are not
248 // placed on top of each other.
249 if ( !m_strAccel
.empty() )
251 str
.Pad(str
.length()%8
);
255 dc
.SetFont(GetFontToUse());
258 dc
.GetTextExtent(str
, &w
, &h
);
262 // add space at the end of the menu for the submenu expansion arrow
263 // this will also allow offsetting the accel string from the right edge
264 *pwidth
+= GetMarginWidth() + 16;
266 else // don't draw the text, just the bitmap (if any)
272 // increase size to accommodate bigger bitmaps if necessary
273 if (m_bmpChecked
.Ok())
275 // Is BMP height larger than text height?
276 size_t adjustedHeight
= m_bmpChecked
.GetHeight();
277 if ( *pheight
< adjustedHeight
)
278 *pheight
= adjustedHeight
;
280 const size_t widthBmp
= m_bmpChecked
.GetWidth();
281 if ( IsOwnerDrawn() )
283 // widen the margin to fit the bitmap if necessary
284 if ((size_t)GetMarginWidth() < widthBmp
)
285 SetMarginWidth(widthBmp
);
287 else // we must allocate enough space for the bitmap
293 // add a 4-pixel separator, otherwise menus look cluttered
296 // make sure that this item is at least as tall as the system menu height
297 const size_t heightStd
= wxMSWSystemMenuFontModule::GetSystemMenuHeight();
298 if ( *pheight
< heightStd
)
299 *pheight
= heightStd
;
301 // remember height for use in OnDrawItem
302 m_nHeight
= *pheight
;
308 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
,
313 // we do nothing on focus change
314 if ( act
== wxODFocusChanged
)
318 // this flag determines whether or not an edge will
319 // be drawn around the bitmap. In most "windows classic"
320 // applications, a 1-pixel highlight edge is drawn around
321 // the bitmap of an item when it is selected. However,
322 // with the new "luna" theme, no edge is drawn around
323 // the bitmap because the background is white (this applies
324 // only to "non-XP style" menus w/ bitmaps --
325 // see IE 6 menus for an example)
327 bool draw_bitmap_edge
= true;
331 DWORD colBack
, colText
;
332 if ( st
& wxODSelected
)
334 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
335 if (!(st
& wxODDisabled
))
337 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
341 colText
= GetSysColor(COLOR_GRAYTEXT
);
346 // fall back to default colors if none explicitly specified
347 colBack
= m_colBack
.Ok() ? wxColourToPalRGB(m_colBack
)
348 : GetSysColor(COLOR_MENU
);
349 colText
= m_colText
.Ok() ? wxColourToPalRGB(m_colText
)
350 : GetSysColor(COLOR_MENUTEXT
);
353 if ( IsOwnerDrawn() )
355 // don't draw an edge around the bitmap, if background is white ...
356 DWORD menu_bg_color
= GetSysColor(COLOR_MENU
);
357 if ( ( GetRValue( menu_bg_color
) >= 0xf0 &&
358 GetGValue( menu_bg_color
) >= 0xf0 &&
359 GetBValue( menu_bg_color
) >= 0xf0 )
362 draw_bitmap_edge
= false;
365 else // edge doesn't look well with default Windows drawing
367 draw_bitmap_edge
= false;
371 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
372 HDC hdc
= GetHdcOf(*impl
);
373 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
374 colOldBack
= ::SetBkColor(hdc
, colBack
);
376 // *2, as in wxSYS_EDGE_Y
377 int margin
= GetMarginWidth() + 2 * wxSystemSettings::GetMetric(wxSYS_EDGE_X
);
379 // select the font and draw the text
380 // ---------------------------------
383 // determine where to draw and leave space for a check-mark.
384 // + 1 pixel to separate the edge from the highlight rectangle
385 int xText
= rc
.x
+ margin
+ 1;
388 // using native API because it recognizes '&'
389 if ( IsOwnerDrawn() )
391 int nPrevMode
= SetBkMode(hdc
, TRANSPARENT
);
392 AutoHBRUSH
hbr(colBack
);
393 SelectInHDC
selBrush(hdc
, hbr
);
395 RECT rectFill
= { rc
.GetLeft(), rc
.GetTop(),
396 rc
.GetRight() + 1, rc
.GetBottom() + 1 };
398 if ( (st
& wxODSelected
) && m_bmpChecked
.Ok() && draw_bitmap_edge
)
400 // only draw the highlight under the text, not under
401 // the bitmap or checkmark
402 rectFill
.left
= xText
;
405 FillRect(hdc
, &rectFill
, hbr
);
407 // use default font if no font set
408 wxFont fontToUse
= GetFontToUse();
409 SelectInHDC
selFont(hdc
, GetHfontOf(fontToUse
));
411 wxString strMenuText
= m_strName
.BeforeFirst('\t');
413 xText
+= 3; // separate text from the highlight rectangle
416 ::GetTextExtentPoint32(hdc
, strMenuText
.c_str(), strMenuText
.length(), &sizeRect
);
418 int flags
= DST_PREFIXTEXT
;
419 if ( (st
& wxODDisabled
) && !(st
& wxODSelected
) )
420 flags
|= DSS_DISABLED
;
422 if ( (st
& wxODHidePrefix
) &&
423 !wxMSWSystemMenuFontModule::AlwaysShowCues() )
424 flags
|= DSS_HIDEPREFIX
;
431 (LPARAM
)strMenuText
.wx_str(),
432 strMenuText
.length(),
434 rc
.y
+ (rc
.GetHeight() - sizeRect
.cy
+ 1)/2, // centre vertically
435 rc
.GetWidth() - margin
,
440 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
441 // as the last parameter in DrawState() (at least with Windows98). So we have
442 // to take care of right alignment ourselves.
443 if ( !m_strAccel
.empty() )
445 int accel_width
, accel_height
;
446 dc
.GetTextExtent(m_strAccel
, &accel_width
, &accel_height
);
447 // right align accel string with right edge of menu ( offset by the
449 ::DrawState(hdc
, NULL
, NULL
,
450 (LPARAM
)m_strAccel
.wx_str(),
452 rc
.GetWidth()-16-accel_width
, rc
.y
+(int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0),
455 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0));
458 (void)SetBkMode(hdc
, nPrevMode
);
464 if ( IsCheckable() && !m_bmpChecked
.Ok() )
466 if ( st
& wxODChecked
)
468 // what goes on: DrawFrameControl creates a b/w mask,
469 // then we copy it to screen to have right colors
471 // first create a monochrome bitmap in a memory DC
472 HDC hdcMem
= CreateCompatibleDC(hdc
);
473 HBITMAP hbmpCheck
= CreateBitmap(margin
, m_nHeight
, 1, 1, 0);
474 SelectObject(hdcMem
, hbmpCheck
);
476 // then draw a check mark into it
477 RECT rect
= { 0, 0, margin
, m_nHeight
};
480 ::DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
483 // finally copy it to screen DC and clean up
484 BitBlt(hdc
, rc
.x
, rc
.y
, margin
, m_nHeight
, hdcMem
, 0, 0, SRCCOPY
);
487 DeleteObject(hbmpCheck
);
494 if ( st
& wxODDisabled
)
496 bmp
= GetDisabledBitmap();
501 // for not checkable bitmaps we should always use unchecked one
502 // because their checked bitmap is not set
503 bmp
= GetBitmap(!IsCheckable() || (st
& wxODChecked
));
506 if ( bmp
.Ok() && st
& wxODDisabled
)
508 // we need to grey out the bitmap as we don't have any specific
510 wxImage imgGrey
= bmp
.ConvertToImage().ConvertToGreyscale();
512 bmp
= wxBitmap(imgGrey
);
514 #endif // wxUSE_IMAGE
519 wxMemoryDC
dcMem(&dc
);
520 dcMem
.SelectObjectAsSource(bmp
);
523 int nBmpWidth
= bmp
.GetWidth(),
524 nBmpHeight
= bmp
.GetHeight();
526 // there should be enough space!
527 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
529 int heightDiff
= m_nHeight
- nBmpHeight
;
530 dc
.Blit(rc
.x
+ (margin
- nBmpWidth
) / 2,
531 rc
.y
+ heightDiff
/ 2,
532 nBmpWidth
, nBmpHeight
,
533 &dcMem
, 0, 0, wxCOPY
, true /* use mask */);
535 if ( ( st
& wxODSelected
) && !( st
& wxODDisabled
) && draw_bitmap_edge
)
537 RECT rectBmp
= { rc
.GetLeft(), rc
.GetTop(),
538 rc
.GetLeft() + margin
,
539 rc
.GetTop() + m_nHeight
};
540 SetBkColor(hdc
, colBack
);
542 DrawEdge(hdc
, &rectBmp
, BDR_RAISEDINNER
, BF_RECT
);
547 ::SetTextColor(hdc
, colOldText
);
548 ::SetBkColor(hdc
, colOldBack
);
554 #endif // wxUSE_OWNER_DRAWN