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"
20 #include "wx/window.h"
21 #include "wx/msw/private.h"
23 #include "wx/bitmap.h"
25 #include "wx/dcmemory.h"
28 #include "wx/settings.h"
29 #include "wx/menuitem.h"
30 #include "wx/module.h"
33 #include "wx/ownerdrw.h"
34 #include "wx/fontutil.h"
38 #ifndef SPI_GETKEYBOARDCUES
39 #define SPI_GETKEYBOARDCUES 0x100A
42 class wxMSWSystemMenuFontModule
: public wxModule
48 ms_systemMenuFont
= new wxFont
;
50 #if defined(__WXMSW__) && defined(__WIN32__) && defined(SM_CXMENUCHECK)
52 nm
.cbSize
= sizeof(NONCLIENTMETRICS
);
53 SystemParametersInfo(SPI_GETNONCLIENTMETRICS
,0,&nm
,0);
55 ms_systemMenuButtonWidth
= nm
.iMenuHeight
;
56 ms_systemMenuHeight
= nm
.iMenuHeight
;
59 wxNativeFontInfo info
;
60 memcpy(&info
.lf
, &nm
.lfMenuFont
, sizeof(LOGFONT
));
61 ms_systemMenuFont
->Create(info
);
63 if (SystemParametersInfo(SPI_GETKEYBOARDCUES
, 0, &ms_showCues
, 0) == 0)
72 delete ms_systemMenuFont
;
73 ms_systemMenuFont
= NULL
;
76 static wxFont
* ms_systemMenuFont
;
77 static int ms_systemMenuButtonWidth
; // windows clean install default
78 static int ms_systemMenuHeight
; // windows clean install default
79 static bool ms_showCues
;
81 DECLARE_DYNAMIC_CLASS(wxMSWSystemMenuFontModule
)
84 // these static variables are from the wxMSWSystemMenuFontModule object
85 // and reflect the system settings returned by the Win32 API's
86 // SystemParametersInfo() call.
88 wxFont
* wxMSWSystemMenuFontModule::ms_systemMenuFont
= NULL
;
89 int wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth
= 18; // windows clean install default
90 int wxMSWSystemMenuFontModule::ms_systemMenuHeight
= 18; // windows clean install default
91 bool wxMSWSystemMenuFontModule::ms_showCues
= true;
93 IMPLEMENT_DYNAMIC_CLASS(wxMSWSystemMenuFontModule
, wxModule
)
96 // VC++ 6 gives a warning here:
98 // return type for 'OwnerDrawnSet_wxImplementation_HashTable::iterator::
99 // operator ->' is 'class wxOwnerDrawn ** ' (ie; not a UDT or reference to
100 // a UDT. Will produce errors if applied using infix notation.
103 #if defined __VISUALC__ && __VISUALC__ <= 1300
104 #if __VISUALC__ >= 1200
105 #pragma warning(push)
108 #pragma warning(disable: 4284)
111 #include "wx/hashset.h"
112 WX_DECLARE_HASH_SET(wxOwnerDrawn
*, wxPointerHash
, wxPointerEqual
, OwnerDrawnSet
);
118 // ============================================================================
119 // implementation of wxOwnerDrawn class
120 // ============================================================================
124 wxOwnerDrawn::wxOwnerDrawn(const wxString
& str
,
129 if ( ms_nDefaultMarginWidth
== 0 )
131 ms_nDefaultMarginWidth
= ::GetSystemMetrics(SM_CXMENUCHECK
) +
132 wxSystemSettings::GetMetric(wxSYS_EDGE_X
);
133 ms_nLastMarginWidth
= ms_nDefaultMarginWidth
;
136 m_bCheckable
= bCheckable
;
137 m_bOwnerDrawn
= false;
138 m_isMenuItem
= bMenuItem
;
140 m_nMarginWidth
= ms_nLastMarginWidth
;
141 m_nMinHeight
= wxMSWSystemMenuFontModule::ms_systemMenuHeight
;
144 wxOwnerDrawn::~wxOwnerDrawn()
148 bool wxOwnerDrawn::IsMenuItem() const
154 // these items will be set during the first invocation of the c'tor,
155 // because the values will be determined by checking the system settings,
156 // which is a chunk of code
157 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 0;
158 size_t wxOwnerDrawn::ms_nLastMarginWidth
= 0;
164 wxFont
wxOwnerDrawn::GetFontToUse() const
166 wxFont font
= m_font
;
170 font
= *wxMSWSystemMenuFontModule::ms_systemMenuFont
;
173 font
= *wxNORMAL_FONT
;
179 // get size of the item
180 // The item size includes the menu string, the accel string,
181 // the bitmap and size for a submenu expansion arrow...
182 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth
, size_t *pheight
)
184 if ( IsOwnerDrawn() )
188 wxString str
= wxStripMenuCodes(m_strName
);
190 // if we have a valid accel string, then pad out
191 // the menu string so that the menu and accel string are not
192 // placed on top of each other.
193 if ( !m_strAccel
.empty() )
195 str
.Pad(str
.length()%8
);
199 dc
.SetFont(GetFontToUse());
202 dc
.GetTextExtent(str
, &w
, &h
);
206 // add space at the end of the menu for the submenu expansion arrow
207 // this will also allow offsetting the accel string from the right edge
208 *pwidth
+= GetMarginWidth() + 16;
210 else // don't draw the text, just the bitmap (if any)
216 // increase size to accommodate bigger bitmaps if necessary
217 if (m_bmpChecked
.Ok())
219 // Is BMP height larger then text height?
220 size_t adjustedHeight
= m_bmpChecked
.GetHeight() +
221 2*wxSystemSettings::GetMetric(wxSYS_EDGE_Y
);
222 if (*pheight
< adjustedHeight
)
223 *pheight
= adjustedHeight
;
225 const size_t widthBmp
= m_bmpChecked
.GetWidth();
226 if ( IsOwnerDrawn() )
228 // widen the margin to fit the bitmap if necessary
229 if ((size_t)GetMarginWidth() < widthBmp
)
230 SetMarginWidth(widthBmp
);
232 else // we must allocate enough space for the bitmap
238 // add a 4-pixel separator, otherwise menus look cluttered
241 // make sure that this item is at least as tall as the system menu height
242 if ( *pheight
< m_nMinHeight
)
243 *pheight
= m_nMinHeight
;
245 // remember height for use in OnDrawItem
246 m_nHeight
= *pheight
;
252 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
,
257 // we do nothing on focus change
258 if ( act
== wxODFocusChanged
)
262 // this flag determines whether or not an edge will
263 // be drawn around the bitmap. In most "windows classic"
264 // applications, a 1-pixel highlight edge is drawn around
265 // the bitmap of an item when it is selected. However,
266 // with the new "luna" theme, no edge is drawn around
267 // the bitmap because the background is white (this applies
268 // only to "non-XP style" menus w/ bitmaps --
269 // see IE 6 menus for an example)
271 bool draw_bitmap_edge
= true;
275 DWORD colBack
, colText
;
276 if ( st
& wxODSelected
)
278 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
279 if (!(st
& wxODDisabled
))
281 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
285 colText
= GetSysColor(COLOR_GRAYTEXT
);
290 // fall back to default colors if none explicitly specified
291 colBack
= m_colBack
.Ok() ? wxColourToPalRGB(m_colBack
)
292 : GetSysColor(COLOR_MENU
);
293 colText
= m_colText
.Ok() ? wxColourToPalRGB(m_colText
)
294 : GetSysColor(COLOR_MENUTEXT
);
297 if ( IsOwnerDrawn() )
299 // don't draw an edge around the bitmap, if background is white ...
300 DWORD menu_bg_color
= GetSysColor(COLOR_MENU
);
301 if ( ( GetRValue( menu_bg_color
) >= 0xf0 &&
302 GetGValue( menu_bg_color
) >= 0xf0 &&
303 GetBValue( menu_bg_color
) >= 0xf0 )
306 draw_bitmap_edge
= false;
309 else // edge doesn't look well with default Windows drawing
311 draw_bitmap_edge
= false;
315 HDC hdc
= GetHdcOf(dc
);
316 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
317 colOldBack
= ::SetBkColor(hdc
, colBack
);
319 // *2, as in wxSYS_EDGE_Y
320 int margin
= GetMarginWidth() + 2 * wxSystemSettings::GetMetric(wxSYS_EDGE_X
);
322 // select the font and draw the text
323 // ---------------------------------
326 // determine where to draw and leave space for a check-mark.
327 // + 1 pixel to separate the edge from the highlight rectangle
328 int xText
= rc
.x
+ margin
+ 1;
331 // using native API because it recognizes '&'
332 if ( IsOwnerDrawn() )
334 int nPrevMode
= SetBkMode(hdc
, TRANSPARENT
);
335 AutoHBRUSH
hbr(colBack
);
336 SelectInHDC
selBrush(hdc
, hbr
);
338 RECT rectFill
= { rc
.GetLeft(), rc
.GetTop(),
339 rc
.GetRight() + 1, rc
.GetBottom() + 1 };
341 if ( (st
& wxODSelected
) && m_bmpChecked
.Ok() && draw_bitmap_edge
)
343 // only draw the highlight under the text, not under
344 // the bitmap or checkmark
345 rectFill
.left
= xText
;
348 FillRect(hdc
, &rectFill
, hbr
);
350 // use default font if no font set
351 wxFont fontToUse
= GetFontToUse();
352 SelectInHDC
selFont(hdc
, GetHfontOf(fontToUse
));
354 wxString strMenuText
= m_strName
.BeforeFirst('\t');
356 xText
+= 3; // separate text from the highlight rectangle
359 ::GetTextExtentPoint32(hdc
, strMenuText
.c_str(), strMenuText
.length(), &sizeRect
);
360 ::DrawState(hdc
, NULL
, NULL
,
361 (LPARAM
)strMenuText
.wx_str(),
362 strMenuText
.length(),
363 xText
, rc
.y
+ (int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0), // centre text vertically
364 rc
.GetWidth()-margin
, sizeRect
.cy
,
366 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0) |
367 (((st
& wxODHidePrefix
) && !wxMSWSystemMenuFontModule::ms_showCues
) ? 512 : 0)); // 512 == DSS_HIDEPREFIX
369 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
370 // as the last parameter in DrawState() (at least with Windows98). So we have
371 // to take care of right alignment ourselves.
372 if ( !m_strAccel
.empty() )
374 int accel_width
, accel_height
;
375 dc
.GetTextExtent(m_strAccel
, &accel_width
, &accel_height
);
376 // right align accel string with right edge of menu ( offset by the
378 ::DrawState(hdc
, NULL
, NULL
,
379 (LPARAM
)m_strAccel
.wx_str(),
381 rc
.GetWidth()-16-accel_width
, rc
.y
+(int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0),
384 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0));
387 (void)SetBkMode(hdc
, nPrevMode
);
393 if ( IsCheckable() && !m_bmpChecked
.Ok() )
395 if ( st
& wxODChecked
)
397 // what goes on: DrawFrameControl creates a b/w mask,
398 // then we copy it to screen to have right colors
400 // first create a monochrome bitmap in a memory DC
401 HDC hdcMem
= CreateCompatibleDC(hdc
);
402 HBITMAP hbmpCheck
= CreateBitmap(margin
, m_nHeight
, 1, 1, 0);
403 SelectObject(hdcMem
, hbmpCheck
);
405 // then draw a check mark into it
406 RECT rect
= { 0, 0, margin
, m_nHeight
};
409 ::DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
412 // finally copy it to screen DC and clean up
413 BitBlt(hdc
, rc
.x
, rc
.y
, margin
, m_nHeight
, hdcMem
, 0, 0, SRCCOPY
);
416 DeleteObject(hbmpCheck
);
423 if ( st
& wxODDisabled
)
425 bmp
= GetDisabledBitmap();
430 // for not checkable bitmaps we should always use unchecked one
431 // because their checked bitmap is not set
432 bmp
= GetBitmap(!IsCheckable() || (st
& wxODChecked
));
435 if ( bmp
.Ok() && st
& wxODDisabled
)
437 // we need to grey out the bitmap as we don't have any specific
439 wxImage imgGrey
= bmp
.ConvertToImage().ConvertToGreyscale();
441 bmp
= wxBitmap(imgGrey
);
443 #endif // wxUSE_IMAGE
448 wxMemoryDC
dcMem(&dc
);
449 dcMem
.SelectObjectAsSource(bmp
);
452 int nBmpWidth
= bmp
.GetWidth(),
453 nBmpHeight
= bmp
.GetHeight();
455 // there should be enough space!
456 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
458 int heightDiff
= m_nHeight
- nBmpHeight
;
459 dc
.Blit(rc
.x
+ (margin
- nBmpWidth
) / 2,
460 rc
.y
+ heightDiff
/ 2,
461 nBmpWidth
, nBmpHeight
,
462 &dcMem
, 0, 0, wxCOPY
, true /* use mask */);
464 if ( ( st
& wxODSelected
) && !( st
& wxODDisabled
) && draw_bitmap_edge
)
466 RECT rectBmp
= { rc
.GetLeft(), rc
.GetTop(),
467 rc
.GetLeft() + margin
,
468 rc
.GetTop() + m_nHeight
};
469 SetBkColor(hdc
, colBack
);
471 DrawEdge(hdc
, &rectBmp
, BDR_RAISEDINNER
, BF_RECT
);
476 ::SetTextColor(hdc
, colOldText
);
477 ::SetBkColor(hdc
, colOldBack
);
483 #endif // wxUSE_OWNER_DRAWN