1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18 #include "wx/msw/private.h"
25 #include "wx/window.h"
26 #include "wx/msw/private.h"
28 #include "wx/bitmap.h"
29 #include "wx/dcmemory.h"
34 #include "wx/settings.h"
35 #include "wx/ownerdrw.h"
36 #include "wx/menuitem.h"
37 #include "wx/fontutil.h"
38 #include "wx/module.h"
42 #ifndef SPI_GETKEYBOARDCUES
43 #define SPI_GETKEYBOARDCUES 0x100A
46 class wxMSWSystemMenuFontModule
: public wxModule
52 ms_systemMenuFont
= new wxFont
;
54 #if defined(__WXMSW__) && defined(__WIN32__) && defined(SM_CXMENUCHECK)
56 nm
.cbSize
= sizeof(NONCLIENTMETRICS
);
57 SystemParametersInfo(SPI_GETNONCLIENTMETRICS
,0,&nm
,0);
59 ms_systemMenuButtonWidth
= nm
.iMenuHeight
;
60 ms_systemMenuHeight
= nm
.iMenuHeight
;
63 wxNativeFontInfo info
;
64 memcpy(&info
.lf
, &nm
.lfMenuFont
, sizeof(LOGFONT
));
65 ms_systemMenuFont
->Create(info
);
67 if (SystemParametersInfo(SPI_GETKEYBOARDCUES
, 0, &ms_showCues
, 0) == 0)
76 delete ms_systemMenuFont
;
77 ms_systemMenuFont
= NULL
;
80 static wxFont
* ms_systemMenuFont
;
81 static int ms_systemMenuButtonWidth
; // windows clean install default
82 static int ms_systemMenuHeight
; // windows clean install default
83 static bool ms_showCues
;
85 DECLARE_DYNAMIC_CLASS(wxMSWSystemMenuFontModule
)
88 // these static variables are from the wxMSWSystemMenuFontModule object
89 // and reflect the system settings returned by the Win32 API's
90 // SystemParametersInfo() call.
92 wxFont
* wxMSWSystemMenuFontModule::ms_systemMenuFont
= NULL
;
93 int wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth
= 18; // windows clean install default
94 int wxMSWSystemMenuFontModule::ms_systemMenuHeight
= 18; // windows clean install default
95 bool wxMSWSystemMenuFontModule::ms_showCues
= true;
97 IMPLEMENT_DYNAMIC_CLASS(wxMSWSystemMenuFontModule
, wxModule
)
100 // temporary hack to implement wxOwnerDrawn::IsMenuItem() without breaking
101 // backwards compatibility
102 #if wxCHECK_VERSION(2, 7, 0)
103 #pragma warning "TODO: remove gs_menuItems hack"
106 #include "wx/hashset.h"
107 WX_DECLARE_HASH_SET(wxOwnerDrawn
*, wxPointerHash
, wxPointerEqual
, OwnerDrawnSet
);
109 static OwnerDrawnSet gs_menuItems
;
111 // ============================================================================
112 // implementation of wxOwnerDrawn class
113 // ============================================================================
117 wxOwnerDrawn::wxOwnerDrawn(const wxString
& str
,
122 if (ms_nDefaultMarginWidth
== 0)
124 ms_nDefaultMarginWidth
= ::GetSystemMetrics(SM_CXMENUCHECK
) +
125 wxSystemSettings::GetMetric(wxSYS_EDGE_X
);
126 ms_nLastMarginWidth
= ms_nDefaultMarginWidth
;
129 m_bCheckable
= bCheckable
;
130 m_bOwnerDrawn
= false;
132 m_nMarginWidth
= ms_nLastMarginWidth
;
133 m_nMinHeight
= wxMSWSystemMenuFontModule::ms_systemMenuHeight
;
135 m_bmpDisabled
= wxNullBitmap
;
137 // TODO: we can't add new m_isMenuItem field in 2.6, so we use this hack
138 // with the map, but do add m_isMenuItem in 2.7
141 gs_menuItems
.insert(this);
145 wxOwnerDrawn::~wxOwnerDrawn()
147 // TODO: remove this in 2.7
148 gs_menuItems
.erase(this);
151 bool wxOwnerDrawn::IsMenuItem() const
153 // TODO: in 2.7, replace this with simple "return m_isMenuItem"
154 return gs_menuItems
.count(this) == 1;
158 // these items will be set during the first invocation of the c'tor,
159 // because the values will be determined by checking the system settings,
160 // which is a chunk of code
161 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 0;
162 size_t wxOwnerDrawn::ms_nLastMarginWidth
= 0;
168 wxFont
wxOwnerDrawn::GetFontToUse() const
170 wxFont font
= m_font
;
174 font
= *wxMSWSystemMenuFontModule::ms_systemMenuFont
;
177 font
= *wxNORMAL_FONT
;
183 // get size of the item
184 // The item size includes the menu string, the accel string,
185 // the bitmap and size for a submenu expansion arrow...
186 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth
, size_t *pheight
)
188 if ( IsOwnerDrawn() )
192 wxString str
= wxStripMenuCodes(m_strName
);
194 // if we have a valid accel string, then pad out
195 // the menu string so that the menu and accel string are not
196 // placed on top of each other.
197 if ( !m_strAccel
.empty() )
199 str
.Pad(str
.Length()%8
);
203 dc
.SetFont(GetFontToUse());
205 dc
.GetTextExtent(str
, (long *)pwidth
, (long *)pheight
);
207 // add space at the end of the menu for the submenu expansion arrow
208 // this will also allow offsetting the accel string from the right edge
209 *pwidth
+= GetMarginWidth() + 16;
211 else // don't draw the text, just the bitmap (if any)
217 // increase size to accommodate bigger bitmaps if necessary
218 if (m_bmpChecked
.Ok())
220 // Is BMP height larger then text height?
221 size_t adjustedHeight
= m_bmpChecked
.GetHeight() +
222 2*wxSystemSettings::GetMetric(wxSYS_EDGE_Y
);
223 if (*pheight
< adjustedHeight
)
224 *pheight
= adjustedHeight
;
226 const size_t widthBmp
= m_bmpChecked
.GetWidth();
227 if ( IsOwnerDrawn() )
229 // widen the margin to fit the bitmap if necessary
230 if ((size_t)GetMarginWidth() < widthBmp
)
231 SetMarginWidth(widthBmp
);
233 else // we must allocate enough space for the bitmap
239 // add a 4-pixel separator, otherwise menus look cluttered
242 // make sure that this item is at least as tall as the system menu height
243 if ( *pheight
< m_nMinHeight
)
244 *pheight
= m_nMinHeight
;
246 // remember height for use in OnDrawItem
247 m_nHeight
= *pheight
;
253 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
,
258 // we do nothing on focus change
259 if ( act
== wxODFocusChanged
)
263 // this flag determines whether or not an edge will
264 // be drawn around the bitmap. In most "windows classic"
265 // applications, a 1-pixel highlight edge is drawn around
266 // the bitmap of an item when it is selected. However,
267 // with the new "luna" theme, no edge is drawn around
268 // the bitmap because the background is white (this applies
269 // only to "non-XP style" menus w/ bitmaps --
270 // see IE 6 menus for an example)
272 bool draw_bitmap_edge
= true;
276 DWORD colBack
, colText
;
277 if ( st
& wxODSelected
) {
278 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
279 if (!(st
& wxODDisabled
))
281 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
285 colText
= GetSysColor(COLOR_GRAYTEXT
);
289 // fall back to default colors if none explicitly specified
290 colBack
= m_colBack
.Ok() ? wxColourToPalRGB(m_colBack
)
291 : GetSysColor(COLOR_MENU
);
292 colText
= m_colText
.Ok() ? wxColourToPalRGB(m_colText
)
293 : GetSysColor(COLOR_MENUTEXT
);
297 // don't draw an edge around the bitmap, if background is white ...
298 DWORD menu_bg_color
= GetSysColor(COLOR_MENU
);
299 if ( ( GetRValue( menu_bg_color
) >= 0xf0 &&
300 GetGValue( menu_bg_color
) >= 0xf0 &&
301 GetBValue( menu_bg_color
) >= 0xf0 )
304 draw_bitmap_edge
= false;
308 HDC hdc
= GetHdcOf(dc
);
309 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
310 colOldBack
= ::SetBkColor(hdc
, colBack
);
312 // *2, as in wxSYS_EDGE_Y
313 int margin
= GetMarginWidth() + 2 * wxSystemSettings::GetMetric(wxSYS_EDGE_X
);
315 // select the font and draw the text
316 // ---------------------------------
319 // determine where to draw and leave space for a check-mark.
320 // + 1 pixel to separate the edge from the highlight rectangle
321 int xText
= rc
.x
+ margin
+ 1;
324 // using native API because it recognizes '&'
325 if ( IsOwnerDrawn() )
327 int nPrevMode
= SetBkMode(hdc
, TRANSPARENT
);
328 AutoHBRUSH
hbr(colBack
);
329 SelectInHDC
selBrush(hdc
, hbr
);
331 RECT rectFill
= { rc
.GetLeft(), rc
.GetTop(),
332 rc
.GetRight() + 1, rc
.GetBottom() + 1 };
334 if ( (st
& wxODSelected
) && m_bmpChecked
.Ok() && draw_bitmap_edge
) {
335 // only draw the highlight under the text, not under
336 // the bitmap or checkmark
337 rectFill
.left
= xText
;
340 FillRect(hdc
, &rectFill
, hbr
);
342 // use default font if no font set
343 wxFont fontToUse
= GetFontToUse();
344 SelectInHDC
selFont(hdc
, GetHfontOf(fontToUse
));
346 wxString strMenuText
= m_strName
.BeforeFirst('\t');
348 xText
+= 3; // separate text from the highlight rectangle
351 ::GetTextExtentPoint32(hdc
, strMenuText
.c_str(), strMenuText
.Length(), &sizeRect
);
352 ::DrawState(hdc
, NULL
, NULL
,
353 (LPARAM
)strMenuText
.c_str(), strMenuText
.length(),
354 xText
, rc
.y
+ (int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0), // centre text vertically
355 rc
.GetWidth()-margin
, sizeRect
.cy
,
357 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0) |
358 (((st
& wxODHidePrefix
) && !wxMSWSystemMenuFontModule::ms_showCues
) ? 512 : 0)); // 512 == DSS_HIDEPREFIX
360 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
361 // as the last parameter in DrawState() (at least with Windows98). So we have
362 // to take care of right alignment ourselves.
363 if ( !m_strAccel
.empty() )
365 int accel_width
, accel_height
;
366 dc
.GetTextExtent(m_strAccel
, &accel_width
, &accel_height
);
367 // right align accel string with right edge of menu ( offset by the
369 ::DrawState(hdc
, NULL
, NULL
,
370 (LPARAM
)m_strAccel
.c_str(), m_strAccel
.length(),
371 rc
.GetWidth()-16-accel_width
, rc
.y
+(int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0),
374 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0));
377 (void)SetBkMode(hdc
, nPrevMode
);
383 if ( IsCheckable() && !m_bmpChecked
.Ok() ) {
384 if ( st
& wxODChecked
) {
385 // what goes on: DrawFrameControl creates a b/w mask,
386 // then we copy it to screen to have right colors
388 // first create a monochrome bitmap in a memory DC
389 HDC hdcMem
= CreateCompatibleDC(hdc
);
390 HBITMAP hbmpCheck
= CreateBitmap(margin
, m_nHeight
, 1, 1, 0);
391 SelectObject(hdcMem
, hbmpCheck
);
393 // then draw a check mark into it
394 RECT rect
= { 0, 0, margin
, m_nHeight
};
397 ::DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
400 // finally copy it to screen DC and clean up
401 BitBlt(hdc
, rc
.x
, rc
.y
, margin
, m_nHeight
,
402 hdcMem
, 0, 0, SRCCOPY
);
405 DeleteObject(hbmpCheck
);
411 if ( st
& wxODDisabled
)
413 bmp
= GetDisabledBitmap();
418 // for not checkable bitmaps we should always use unchecked one because
419 // their checked bitmap is not set
420 bmp
= GetBitmap(!IsCheckable() || (st
& wxODChecked
));
424 wxMemoryDC
dcMem(&dc
);
425 dcMem
.SelectObject(bmp
);
428 int nBmpWidth
= bmp
.GetWidth(),
429 nBmpHeight
= bmp
.GetHeight();
431 // there should be enough space!
432 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
434 int heightDiff
= m_nHeight
- nBmpHeight
;
435 dc
.Blit(rc
.x
+ (margin
- nBmpWidth
) / 2,
436 rc
.y
+ heightDiff
/ 2,
437 nBmpWidth
, nBmpHeight
,
438 &dcMem
, 0, 0, wxCOPY
, true /* use mask */);
440 if ( ( st
& wxODSelected
) && !( st
& wxODDisabled
) && draw_bitmap_edge
) {
441 RECT rectBmp
= { rc
.GetLeft(), rc
.GetTop(),
442 rc
.GetLeft() + margin
,
443 rc
.GetTop() + m_nHeight
};
444 SetBkColor(hdc
, colBack
);
446 DrawEdge(hdc
, &rectBmp
, BDR_RAISEDINNER
, BF_RECT
);
451 ::SetTextColor(hdc
, colOldText
);
452 ::SetBkColor(hdc
, colOldBack
);
458 #endif // wxUSE_OWNER_DRAWN