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"
38 #include "wx/msw/dc.h"
40 #ifndef SPI_GETKEYBOARDCUES
41 #define SPI_GETKEYBOARDCUES 0x100A
44 #ifndef DSS_HIDEPREFIX
45 #define DSS_HIDEPREFIX 0x0200
48 class wxMSWSystemMenuFontModule
: public wxModule
58 if ( ms_systemMenuFont
)
60 delete ms_systemMenuFont
;
61 ms_systemMenuFont
= NULL
;
65 static const wxFont
& GetSystemMenuFont()
67 if ( !ms_systemMenuFont
)
70 return *ms_systemMenuFont
;
73 static int GetSystemMenuHeight()
75 if ( !ms_systemMenuHeight
)
78 return ms_systemMenuHeight
;
81 static bool AlwaysShowCues()
83 if ( !ms_systemMenuHeight
)
86 return ms_alwaysShowCues
;
90 static NONCLIENTMETRICS
GetNCM()
92 WinStruct
<NONCLIENTMETRICS
> nm
;
93 if ( !::SystemParametersInfo(SPI_GETNONCLIENTMETRICS
, 0, &nm
, 0) )
96 // a new field has been added to NONCLIENTMETRICS under Vista, so
97 // the call to SystemParametersInfo() fails if we use the struct
98 // size incorporating this new value on an older system -- retry
100 nm
.cbSize
-= sizeof(int);
101 if ( !::SystemParametersInfo(SPI_GETNONCLIENTMETRICS
, 0, &nm
, 0) )
102 #endif // WINVER >= 0x0600
104 // maybe we should initialize the struct with some defaults?
105 wxLogLastError(_T("SystemParametersInfo(SPI_GETNONCLIENTMETRICS)"));
112 static void DoInitMetrics()
114 // iMenuHeight is the menu bar height and the menu items are less tall,
115 // although I don't know by how much -- below is the value for my system
116 ms_systemMenuHeight
= GetNCM().iMenuHeight
- 4;
118 wxASSERT_MSG( ms_systemMenuHeight
> 0,
119 "menu height should be positive" );
121 if ( ::SystemParametersInfo(SPI_GETKEYBOARDCUES
, 0,
122 &ms_alwaysShowCues
, 0) == 0 )
124 // if it's not supported, we must be on an old Windows version
125 // which always shows them
126 ms_alwaysShowCues
= true;
130 static void DoInitFont()
132 ms_systemMenuFont
= new wxFont(wxNativeFontInfo(GetNCM().lfMenuFont
));
135 static wxFont
* ms_systemMenuFont
;
136 static int ms_systemMenuHeight
;
137 static bool ms_alwaysShowCues
;
140 DECLARE_DYNAMIC_CLASS(wxMSWSystemMenuFontModule
)
143 // these static variables are from the wxMSWSystemMenuFontModule object
144 // and reflect the system settings returned by the Win32 API's
145 // SystemParametersInfo() call.
147 wxFont
* wxMSWSystemMenuFontModule::ms_systemMenuFont
= NULL
;
148 int wxMSWSystemMenuFontModule::ms_systemMenuHeight
= 0;
149 bool wxMSWSystemMenuFontModule::ms_alwaysShowCues
= false;
151 IMPLEMENT_DYNAMIC_CLASS(wxMSWSystemMenuFontModule
, wxModule
)
154 // VC++ 6 gives a warning here:
156 // return type for 'OwnerDrawnSet_wxImplementation_HashTable::iterator::
157 // operator ->' is 'class wxOwnerDrawn ** ' (ie; not a UDT or reference to
158 // a UDT. Will produce errors if applied using infix notation.
161 #if defined __VISUALC__ && __VISUALC__ <= 1300
162 #if __VISUALC__ >= 1200
163 #pragma warning(push)
166 #pragma warning(disable: 4284)
169 #include "wx/hashset.h"
170 WX_DECLARE_HASH_SET(wxOwnerDrawn
*, wxPointerHash
, wxPointerEqual
, OwnerDrawnSet
);
176 // ============================================================================
177 // implementation of wxOwnerDrawn class
178 // ============================================================================
182 wxOwnerDrawn::wxOwnerDrawn(const wxString
& str
,
187 if ( ms_nDefaultMarginWidth
== 0 )
189 ms_nDefaultMarginWidth
= ::GetSystemMetrics(SM_CXMENUCHECK
) +
190 wxSystemSettings::GetMetric(wxSYS_EDGE_X
);
191 ms_nLastMarginWidth
= ms_nDefaultMarginWidth
;
194 m_bCheckable
= bCheckable
;
195 m_bOwnerDrawn
= false;
196 m_isMenuItem
= bMenuItem
;
198 m_nMarginWidth
= ms_nLastMarginWidth
;
201 wxOwnerDrawn::~wxOwnerDrawn()
205 bool wxOwnerDrawn::IsMenuItem() const
211 // these items will be set during the first invocation of the ctor,
212 // because the values will be determined by checking the system settings,
213 // which is a chunk of code
214 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 0;
215 size_t wxOwnerDrawn::ms_nLastMarginWidth
= 0;
221 wxFont
wxOwnerDrawn::GetFontToUse() const
223 wxFont font
= m_font
;
227 font
= wxMSWSystemMenuFontModule::GetSystemMenuFont();
230 font
= *wxNORMAL_FONT
;
236 // get size of the item
237 // The item size includes the menu string, the accel string,
238 // the bitmap and size for a submenu expansion arrow...
239 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth
, size_t *pheight
)
241 if ( IsOwnerDrawn() )
245 wxString str
= wxStripMenuCodes(m_strName
);
247 // if we have a valid accel string, then pad out
248 // the menu string so that the menu and accel string are not
249 // placed on top of each other.
250 if ( !m_strAccel
.empty() )
252 str
.Pad(str
.length()%8
);
256 dc
.SetFont(GetFontToUse());
259 dc
.GetTextExtent(str
, &w
, &h
);
263 // add space at the end of the menu for the submenu expansion arrow
264 // this will also allow offsetting the accel string from the right edge
265 *pwidth
+= GetMarginWidth() + 16;
267 else // don't draw the text, just the bitmap (if any)
273 // increase size to accommodate bigger bitmaps if necessary
274 if (m_bmpChecked
.Ok())
276 // Is BMP height larger than text height?
277 size_t adjustedHeight
= m_bmpChecked
.GetHeight();
278 if ( *pheight
< adjustedHeight
)
279 *pheight
= adjustedHeight
;
281 const size_t widthBmp
= m_bmpChecked
.GetWidth();
282 if ( IsOwnerDrawn() )
284 // widen the margin to fit the bitmap if necessary
285 if ((size_t)GetMarginWidth() < widthBmp
)
286 SetMarginWidth(widthBmp
);
288 else // we must allocate enough space for the bitmap
294 // add a 4-pixel separator, otherwise menus look cluttered
297 // make sure that this item is at least as tall as the system menu height
298 const size_t heightStd
= wxMSWSystemMenuFontModule::GetSystemMenuHeight();
299 if ( *pheight
< heightStd
)
300 *pheight
= heightStd
;
302 // remember height for use in OnDrawItem
303 m_nHeight
= *pheight
;
309 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
,
314 // we do nothing on focus change
315 if ( act
== wxODFocusChanged
)
319 // this flag determines whether or not an edge will
320 // be drawn around the bitmap. In most "windows classic"
321 // applications, a 1-pixel highlight edge is drawn around
322 // the bitmap of an item when it is selected. However,
323 // with the new "luna" theme, no edge is drawn around
324 // the bitmap because the background is white (this applies
325 // only to "non-XP style" menus w/ bitmaps --
326 // see IE 6 menus for an example)
328 bool draw_bitmap_edge
= true;
332 DWORD colBack
, colText
;
333 if ( st
& wxODSelected
)
335 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
336 if (!(st
& wxODDisabled
))
338 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
342 colText
= GetSysColor(COLOR_GRAYTEXT
);
347 // fall back to default colors if none explicitly specified
348 colBack
= m_colBack
.Ok() ? wxColourToPalRGB(m_colBack
)
349 : GetSysColor(COLOR_MENU
);
350 colText
= m_colText
.Ok() ? wxColourToPalRGB(m_colText
)
351 : GetSysColor(COLOR_MENUTEXT
);
354 if ( IsOwnerDrawn() )
356 // don't draw an edge around the bitmap, if background is white ...
357 DWORD menu_bg_color
= GetSysColor(COLOR_MENU
);
358 if ( ( GetRValue( menu_bg_color
) >= 0xf0 &&
359 GetGValue( menu_bg_color
) >= 0xf0 &&
360 GetBValue( menu_bg_color
) >= 0xf0 )
363 draw_bitmap_edge
= false;
366 else // edge doesn't look well with default Windows drawing
368 draw_bitmap_edge
= false;
372 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
373 HDC hdc
= GetHdcOf(*impl
);
374 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
375 colOldBack
= ::SetBkColor(hdc
, colBack
);
377 // *2, as in wxSYS_EDGE_Y
378 int margin
= GetMarginWidth() + 2 * wxSystemSettings::GetMetric(wxSYS_EDGE_X
);
380 // select the font and draw the text
381 // ---------------------------------
384 // determine where to draw and leave space for a check-mark.
385 // + 1 pixel to separate the edge from the highlight rectangle
386 int xText
= rc
.x
+ margin
+ 1;
389 // using native API because it recognizes '&'
390 if ( IsOwnerDrawn() )
392 int nPrevMode
= SetBkMode(hdc
, TRANSPARENT
);
393 AutoHBRUSH
hbr(colBack
);
394 SelectInHDC
selBrush(hdc
, hbr
);
396 RECT rectFill
= { rc
.GetLeft(), rc
.GetTop(),
397 rc
.GetRight() + 1, rc
.GetBottom() + 1 };
399 if ( (st
& wxODSelected
) && m_bmpChecked
.Ok() && draw_bitmap_edge
)
401 // only draw the highlight under the text, not under
402 // the bitmap or checkmark
403 rectFill
.left
= xText
;
406 FillRect(hdc
, &rectFill
, hbr
);
408 // use default font if no font set
409 wxFont fontToUse
= GetFontToUse();
410 SelectInHDC
selFont(hdc
, GetHfontOf(fontToUse
));
412 wxString strMenuText
= m_strName
.BeforeFirst('\t');
414 xText
+= 3; // separate text from the highlight rectangle
417 ::GetTextExtentPoint32(hdc
, strMenuText
.c_str(), strMenuText
.length(), &sizeRect
);
419 int flags
= DST_PREFIXTEXT
;
420 if ( (st
& wxODDisabled
) && !(st
& wxODSelected
) )
421 flags
|= DSS_DISABLED
;
423 if ( (st
& wxODHidePrefix
) &&
424 !wxMSWSystemMenuFontModule::AlwaysShowCues() )
425 flags
|= DSS_HIDEPREFIX
;
432 (LPARAM
)strMenuText
.wx_str(),
433 strMenuText
.length(),
435 rc
.y
+ (rc
.GetHeight() - sizeRect
.cy
+ 1)/2, // centre vertically
436 rc
.GetWidth() - margin
,
441 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
442 // as the last parameter in DrawState() (at least with Windows98). So we have
443 // to take care of right alignment ourselves.
444 if ( !m_strAccel
.empty() )
446 int accel_width
, accel_height
;
447 dc
.GetTextExtent(m_strAccel
, &accel_width
, &accel_height
);
448 // right align accel string with right edge of menu ( offset by the
450 ::DrawState(hdc
, NULL
, NULL
,
451 (LPARAM
)m_strAccel
.wx_str(),
453 rc
.GetWidth()-16-accel_width
, rc
.y
+(int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0),
456 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0));
459 (void)SetBkMode(hdc
, nPrevMode
);
465 if ( IsCheckable() && !m_bmpChecked
.Ok() )
467 if ( st
& wxODChecked
)
469 // what goes on: DrawFrameControl creates a b/w mask,
470 // then we copy it to screen to have right colors
472 // first create a monochrome bitmap in a memory DC
473 HDC hdcMem
= CreateCompatibleDC(hdc
);
474 HBITMAP hbmpCheck
= CreateBitmap(margin
, m_nHeight
, 1, 1, 0);
475 SelectObject(hdcMem
, hbmpCheck
);
477 // then draw a check mark into it
478 RECT rect
= { 0, 0, margin
, m_nHeight
};
481 ::DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
484 // finally copy it to screen DC and clean up
485 BitBlt(hdc
, rc
.x
, rc
.y
, margin
, m_nHeight
, hdcMem
, 0, 0, SRCCOPY
);
488 DeleteObject(hbmpCheck
);
495 if ( st
& wxODDisabled
)
497 bmp
= GetDisabledBitmap();
502 // for not checkable bitmaps we should always use unchecked one
503 // because their checked bitmap is not set
504 bmp
= GetBitmap(!IsCheckable() || (st
& wxODChecked
));
507 if ( bmp
.Ok() && st
& wxODDisabled
)
509 // we need to grey out the bitmap as we don't have any specific
511 wxImage imgGrey
= bmp
.ConvertToImage().ConvertToGreyscale();
513 bmp
= wxBitmap(imgGrey
);
515 #endif // wxUSE_IMAGE
520 wxMemoryDC
dcMem(&dc
);
521 dcMem
.SelectObjectAsSource(bmp
);
524 int nBmpWidth
= bmp
.GetWidth(),
525 nBmpHeight
= bmp
.GetHeight();
527 // there should be enough space!
528 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
530 int heightDiff
= m_nHeight
- nBmpHeight
;
531 dc
.Blit(rc
.x
+ (margin
- nBmpWidth
) / 2,
532 rc
.y
+ heightDiff
/ 2,
533 nBmpWidth
, nBmpHeight
,
534 &dcMem
, 0, 0, wxCOPY
, true /* use mask */);
536 if ( ( st
& wxODSelected
) && !( st
& wxODDisabled
) && draw_bitmap_edge
)
538 RECT rectBmp
= { rc
.GetLeft(), rc
.GetTop(),
539 rc
.GetLeft() + margin
,
540 rc
.GetTop() + m_nHeight
};
541 SetBkColor(hdc
, colBack
);
543 DrawEdge(hdc
, &rectBmp
, BDR_RAISEDINNER
, BF_RECT
);
548 ::SetTextColor(hdc
, colOldText
);
549 ::SetBkColor(hdc
, colOldBack
);
555 #endif // wxUSE_OWNER_DRAWN