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/private/metrics.h"
39 #include "wx/msw/dc.h"
41 #ifndef SPI_GETKEYBOARDCUES
42 #define SPI_GETKEYBOARDCUES 0x100A
45 #ifndef DSS_HIDEPREFIX
46 #define DSS_HIDEPREFIX 0x0200
49 class wxMSWSystemMenuFontModule
: public wxModule
59 if ( ms_systemMenuFont
)
61 delete ms_systemMenuFont
;
62 ms_systemMenuFont
= NULL
;
66 static const wxFont
& GetSystemMenuFont()
68 if ( !ms_systemMenuFont
)
71 return *ms_systemMenuFont
;
74 static int GetSystemMenuHeight()
76 if ( !ms_systemMenuHeight
)
79 return ms_systemMenuHeight
;
82 static bool AlwaysShowCues()
84 if ( !ms_systemMenuHeight
)
87 return ms_alwaysShowCues
;
91 static void DoInitMetrics()
93 // iMenuHeight is the menu bar height and the menu items are less tall,
94 // although I don't know by how much -- below is the value for my system
95 ms_systemMenuHeight
= wxMSWImpl::GetNonClientMetrics().iMenuHeight
- 4;
97 wxASSERT_MSG( ms_systemMenuHeight
> 0,
98 "menu height should be positive" );
100 if ( ::SystemParametersInfo(SPI_GETKEYBOARDCUES
, 0,
101 &ms_alwaysShowCues
, 0) == 0 )
103 // if it's not supported, we must be on an old Windows version
104 // which always shows them
105 ms_alwaysShowCues
= true;
109 static void DoInitFont()
111 ms_systemMenuFont
= new
112 wxFont(wxNativeFontInfo(wxMSWImpl::GetNonClientMetrics().lfMenuFont
));
115 static wxFont
* ms_systemMenuFont
;
116 static int ms_systemMenuHeight
;
117 static bool ms_alwaysShowCues
;
120 DECLARE_DYNAMIC_CLASS(wxMSWSystemMenuFontModule
)
123 // these static variables are from the wxMSWSystemMenuFontModule object
124 // and reflect the system settings returned by the Win32 API's
125 // SystemParametersInfo() call.
127 wxFont
* wxMSWSystemMenuFontModule::ms_systemMenuFont
= NULL
;
128 int wxMSWSystemMenuFontModule::ms_systemMenuHeight
= 0;
129 bool wxMSWSystemMenuFontModule::ms_alwaysShowCues
= false;
131 IMPLEMENT_DYNAMIC_CLASS(wxMSWSystemMenuFontModule
, wxModule
)
134 // VC++ 6 gives a warning here:
136 // return type for 'OwnerDrawnSet_wxImplementation_HashTable::iterator::
137 // operator ->' is 'class wxOwnerDrawn ** ' (ie; not a UDT or reference to
138 // a UDT. Will produce errors if applied using infix notation.
141 #if defined __VISUALC__ && __VISUALC__ <= 1300
142 #if __VISUALC__ >= 1200
143 #pragma warning(push)
146 #pragma warning(disable: 4284)
149 #include "wx/hashset.h"
150 WX_DECLARE_HASH_SET(wxOwnerDrawn
*, wxPointerHash
, wxPointerEqual
, OwnerDrawnSet
);
156 // ============================================================================
157 // implementation of wxOwnerDrawn class
158 // ============================================================================
162 wxOwnerDrawn::wxOwnerDrawn(const wxString
& str
,
167 if ( ms_nDefaultMarginWidth
== 0 )
169 ms_nDefaultMarginWidth
= ::GetSystemMetrics(SM_CXMENUCHECK
) +
170 wxSystemSettings::GetMetric(wxSYS_EDGE_X
);
171 ms_nLastMarginWidth
= ms_nDefaultMarginWidth
;
174 m_bCheckable
= bCheckable
;
175 m_bOwnerDrawn
= false;
176 m_isMenuItem
= bMenuItem
;
178 m_nMarginWidth
= ms_nLastMarginWidth
;
181 wxOwnerDrawn::~wxOwnerDrawn()
185 bool wxOwnerDrawn::IsMenuItem() const
191 // these items will be set during the first invocation of the ctor,
192 // because the values will be determined by checking the system settings,
193 // which is a chunk of code
194 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 0;
195 size_t wxOwnerDrawn::ms_nLastMarginWidth
= 0;
201 wxFont
wxOwnerDrawn::GetFontToUse() const
203 wxFont font
= m_font
;
207 font
= wxMSWSystemMenuFontModule::GetSystemMenuFont();
210 font
= *wxNORMAL_FONT
;
216 // get size of the item
217 // The item size includes the menu string, the accel string,
218 // the bitmap and size for a submenu expansion arrow...
219 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth
, size_t *pheight
)
221 if ( IsOwnerDrawn() )
225 wxString str
= wxStripMenuCodes(m_strName
);
227 // if we have a valid accel string, then pad out
228 // the menu string so that the menu and accel string are not
229 // placed on top of each other.
230 if ( !m_strAccel
.empty() )
232 str
.Pad(str
.length()%8
);
236 dc
.SetFont(GetFontToUse());
239 dc
.GetTextExtent(str
, &w
, &h
);
243 // add space at the end of the menu for the submenu expansion arrow
244 // this will also allow offsetting the accel string from the right edge
245 *pwidth
+= GetMarginWidth() + 16;
247 else // don't draw the text, just the bitmap (if any)
253 // increase size to accommodate bigger bitmaps if necessary
254 if (m_bmpChecked
.Ok())
256 // Is BMP height larger than text height?
257 size_t adjustedHeight
= m_bmpChecked
.GetHeight();
258 if ( *pheight
< adjustedHeight
)
259 *pheight
= adjustedHeight
;
261 const size_t widthBmp
= m_bmpChecked
.GetWidth();
262 if ( IsOwnerDrawn() )
264 // widen the margin to fit the bitmap if necessary
265 if ((size_t)GetMarginWidth() < widthBmp
)
266 SetMarginWidth(widthBmp
);
268 else // we must allocate enough space for the bitmap
274 // add a 4-pixel separator, otherwise menus look cluttered
277 // make sure that this item is at least as tall as the system menu height
278 const size_t heightStd
= wxMSWSystemMenuFontModule::GetSystemMenuHeight();
279 if ( *pheight
< heightStd
)
280 *pheight
= heightStd
;
282 // remember height for use in OnDrawItem
283 m_nHeight
= *pheight
;
289 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
,
294 // we do nothing on focus change
295 if ( act
== wxODFocusChanged
)
299 // this flag determines whether or not an edge will
300 // be drawn around the bitmap. In most "windows classic"
301 // applications, a 1-pixel highlight edge is drawn around
302 // the bitmap of an item when it is selected. However,
303 // with the new "luna" theme, no edge is drawn around
304 // the bitmap because the background is white (this applies
305 // only to "non-XP style" menus w/ bitmaps --
306 // see IE 6 menus for an example)
308 bool draw_bitmap_edge
= true;
312 DWORD colBack
, colText
;
313 if ( st
& wxODSelected
)
315 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
316 if (!(st
& wxODDisabled
))
318 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
322 colText
= GetSysColor(COLOR_GRAYTEXT
);
327 // fall back to default colors if none explicitly specified
328 colBack
= m_colBack
.Ok() ? wxColourToPalRGB(m_colBack
)
329 : GetSysColor(COLOR_MENU
);
330 colText
= m_colText
.Ok() ? wxColourToPalRGB(m_colText
)
331 : GetSysColor(COLOR_MENUTEXT
);
334 if ( IsOwnerDrawn() )
336 // don't draw an edge around the bitmap, if background is white ...
337 DWORD menu_bg_color
= GetSysColor(COLOR_MENU
);
338 if ( ( GetRValue( menu_bg_color
) >= 0xf0 &&
339 GetGValue( menu_bg_color
) >= 0xf0 &&
340 GetBValue( menu_bg_color
) >= 0xf0 )
343 draw_bitmap_edge
= false;
346 else // edge doesn't look well with default Windows drawing
348 draw_bitmap_edge
= false;
352 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
353 HDC hdc
= GetHdcOf(*impl
);
354 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
355 colOldBack
= ::SetBkColor(hdc
, colBack
);
357 // *2, as in wxSYS_EDGE_Y
358 int margin
= GetMarginWidth() + 2 * wxSystemSettings::GetMetric(wxSYS_EDGE_X
);
360 // select the font and draw the text
361 // ---------------------------------
364 // determine where to draw and leave space for a check-mark.
365 // + 1 pixel to separate the edge from the highlight rectangle
366 int xText
= rc
.x
+ margin
+ 1;
369 // using native API because it recognizes '&'
370 if ( IsOwnerDrawn() )
372 int nPrevMode
= SetBkMode(hdc
, TRANSPARENT
);
373 AutoHBRUSH
hbr(colBack
);
374 SelectInHDC
selBrush(hdc
, hbr
);
376 RECT rectFill
= { rc
.GetLeft(), rc
.GetTop(),
377 rc
.GetRight() + 1, rc
.GetBottom() + 1 };
379 if ( (st
& wxODSelected
) && m_bmpChecked
.Ok() && draw_bitmap_edge
)
381 // only draw the highlight under the text, not under
382 // the bitmap or checkmark
383 rectFill
.left
= xText
;
386 FillRect(hdc
, &rectFill
, hbr
);
388 // use default font if no font set
389 wxFont fontToUse
= GetFontToUse();
390 SelectInHDC
selFont(hdc
, GetHfontOf(fontToUse
));
392 wxString strMenuText
= m_strName
.BeforeFirst('\t');
394 xText
+= 3; // separate text from the highlight rectangle
397 ::GetTextExtentPoint32(hdc
, strMenuText
.c_str(), strMenuText
.length(), &sizeRect
);
399 int flags
= DST_PREFIXTEXT
;
400 if ( (st
& wxODDisabled
) && !(st
& wxODSelected
) )
401 flags
|= DSS_DISABLED
;
403 if ( (st
& wxODHidePrefix
) &&
404 !wxMSWSystemMenuFontModule::AlwaysShowCues() )
405 flags
|= DSS_HIDEPREFIX
;
412 (LPARAM
)strMenuText
.wx_str(),
413 strMenuText
.length(),
415 rc
.y
+ (rc
.GetHeight() - sizeRect
.cy
+ 1)/2, // centre vertically
416 rc
.GetWidth() - margin
,
421 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
422 // as the last parameter in DrawState() (at least with Windows98). So we have
423 // to take care of right alignment ourselves.
424 if ( !m_strAccel
.empty() )
426 int accel_width
, accel_height
;
427 dc
.GetTextExtent(m_strAccel
, &accel_width
, &accel_height
);
428 // right align accel string with right edge of menu ( offset by the
430 ::DrawState(hdc
, NULL
, NULL
,
431 (LPARAM
)m_strAccel
.wx_str(),
433 rc
.GetWidth()-16-accel_width
, rc
.y
+(int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0),
436 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0));
439 (void)SetBkMode(hdc
, nPrevMode
);
445 if ( IsCheckable() && !m_bmpChecked
.Ok() )
447 if ( st
& wxODChecked
)
449 // what goes on: DrawFrameControl creates a b/w mask,
450 // then we copy it to screen to have right colors
452 // first create a monochrome bitmap in a memory DC
453 HDC hdcMem
= CreateCompatibleDC(hdc
);
454 HBITMAP hbmpCheck
= CreateBitmap(margin
, m_nHeight
, 1, 1, 0);
455 SelectObject(hdcMem
, hbmpCheck
);
457 // then draw a check mark into it
458 RECT rect
= { 0, 0, margin
, m_nHeight
};
461 ::DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
464 // finally copy it to screen DC and clean up
465 BitBlt(hdc
, rc
.x
, rc
.y
, margin
, m_nHeight
, hdcMem
, 0, 0, SRCCOPY
);
468 DeleteObject(hbmpCheck
);
475 if ( st
& wxODDisabled
)
477 bmp
= GetDisabledBitmap();
482 // for not checkable bitmaps we should always use unchecked one
483 // because their checked bitmap is not set
484 bmp
= GetBitmap(!IsCheckable() || (st
& wxODChecked
));
487 if ( bmp
.Ok() && st
& wxODDisabled
)
489 // we need to grey out the bitmap as we don't have any specific
491 wxImage imgGrey
= bmp
.ConvertToImage().ConvertToGreyscale();
493 bmp
= wxBitmap(imgGrey
);
495 #endif // wxUSE_IMAGE
500 wxMemoryDC
dcMem(&dc
);
501 dcMem
.SelectObjectAsSource(bmp
);
504 int nBmpWidth
= bmp
.GetWidth(),
505 nBmpHeight
= bmp
.GetHeight();
507 // there should be enough space!
508 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
510 int heightDiff
= m_nHeight
- nBmpHeight
;
511 dc
.Blit(rc
.x
+ (margin
- nBmpWidth
) / 2,
512 rc
.y
+ heightDiff
/ 2,
513 nBmpWidth
, nBmpHeight
,
514 &dcMem
, 0, 0, wxCOPY
, true /* use mask */);
516 if ( ( st
& wxODSelected
) && !( st
& wxODDisabled
) && draw_bitmap_edge
)
518 RECT rectBmp
= { rc
.GetLeft(), rc
.GetTop(),
519 rc
.GetLeft() + margin
,
520 rc
.GetTop() + m_nHeight
};
521 SetBkColor(hdc
, colBack
);
523 DrawEdge(hdc
, &rectBmp
, BDR_RAISEDINNER
, BF_RECT
);
528 ::SetTextColor(hdc
, colOldText
);
529 ::SetBkColor(hdc
, colOldBack
);
535 #endif // wxUSE_OWNER_DRAWN