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"
24 #include "wx/dcmemory.h"
29 #include "wx/settings.h"
30 #include "wx/ownerdrw.h"
31 #include "wx/menuitem.h"
32 #include "wx/fontutil.h"
33 #include "wx/module.h"
37 #ifndef SPI_GETKEYBOARDCUES
38 #define SPI_GETKEYBOARDCUES 0x100A
41 class wxMSWSystemMenuFontModule
: public wxModule
47 ms_systemMenuFont
= new wxFont
;
49 #if defined(__WXMSW__) && defined(__WIN32__) && defined(SM_CXMENUCHECK)
51 nm
.cbSize
= sizeof(NONCLIENTMETRICS
);
52 SystemParametersInfo(SPI_GETNONCLIENTMETRICS
,0,&nm
,0);
54 ms_systemMenuButtonWidth
= nm
.iMenuHeight
;
55 ms_systemMenuHeight
= nm
.iMenuHeight
;
58 wxNativeFontInfo info
;
59 memcpy(&info
.lf
, &nm
.lfMenuFont
, sizeof(LOGFONT
));
60 ms_systemMenuFont
->Create(info
);
62 if (SystemParametersInfo(SPI_GETKEYBOARDCUES
, 0, &ms_showCues
, 0) == 0)
71 delete ms_systemMenuFont
;
72 ms_systemMenuFont
= NULL
;
75 static wxFont
* ms_systemMenuFont
;
76 static int ms_systemMenuButtonWidth
; // windows clean install default
77 static int ms_systemMenuHeight
; // windows clean install default
78 static bool ms_showCues
;
80 DECLARE_DYNAMIC_CLASS(wxMSWSystemMenuFontModule
)
83 // these static variables are from the wxMSWSystemMenuFontModule object
84 // and reflect the system settings returned by the Win32 API's
85 // SystemParametersInfo() call.
87 wxFont
* wxMSWSystemMenuFontModule::ms_systemMenuFont
= NULL
;
88 int wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth
= 18; // windows clean install default
89 int wxMSWSystemMenuFontModule::ms_systemMenuHeight
= 18; // windows clean install default
90 bool wxMSWSystemMenuFontModule::ms_showCues
= true;
92 IMPLEMENT_DYNAMIC_CLASS(wxMSWSystemMenuFontModule
, wxModule
)
95 // VC++ 6 gives a warning here:
97 // return type for 'OwnerDrawnSet_wxImplementation_HashTable::iterator::
98 // operator ->' is 'class wxOwnerDrawn ** ' (ie; not a UDT or reference to
99 // a UDT. Will produce errors if applied using infix notation.
102 #if defined __VISUALC__ && __VISUALC__ <= 1300
103 #if __VISUALC__ >= 1200
104 #pragma warning(push)
107 #pragma warning(disable: 4284)
110 #include "wx/hashset.h"
111 WX_DECLARE_HASH_SET(wxOwnerDrawn
*, wxPointerHash
, wxPointerEqual
, OwnerDrawnSet
);
117 // ============================================================================
118 // implementation of wxOwnerDrawn class
119 // ============================================================================
123 wxOwnerDrawn::wxOwnerDrawn(const wxString
& str
,
128 if ( ms_nDefaultMarginWidth
== 0 )
130 ms_nDefaultMarginWidth
= ::GetSystemMetrics(SM_CXMENUCHECK
) +
131 wxSystemSettings::GetMetric(wxSYS_EDGE_X
);
132 ms_nLastMarginWidth
= ms_nDefaultMarginWidth
;
135 m_bCheckable
= bCheckable
;
136 m_bOwnerDrawn
= false;
137 m_isMenuItem
= bMenuItem
;
139 m_nMarginWidth
= ms_nLastMarginWidth
;
140 m_nMinHeight
= wxMSWSystemMenuFontModule::ms_systemMenuHeight
;
143 wxOwnerDrawn::~wxOwnerDrawn()
147 bool wxOwnerDrawn::IsMenuItem() const
153 // these items will be set during the first invocation of the c'tor,
154 // because the values will be determined by checking the system settings,
155 // which is a chunk of code
156 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 0;
157 size_t wxOwnerDrawn::ms_nLastMarginWidth
= 0;
163 wxFont
wxOwnerDrawn::GetFontToUse() const
165 wxFont font
= m_font
;
169 font
= *wxMSWSystemMenuFontModule::ms_systemMenuFont
;
172 font
= *wxNORMAL_FONT
;
178 // get size of the item
179 // The item size includes the menu string, the accel string,
180 // the bitmap and size for a submenu expansion arrow...
181 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth
, size_t *pheight
)
183 if ( IsOwnerDrawn() )
187 wxString str
= wxStripMenuCodes(m_strName
);
189 // if we have a valid accel string, then pad out
190 // the menu string so that the menu and accel string are not
191 // placed on top of each other.
192 if ( !m_strAccel
.empty() )
194 str
.Pad(str
.length()%8
);
198 dc
.SetFont(GetFontToUse());
200 dc
.GetTextExtent(str
, (long *)pwidth
, (long *)pheight
);
202 // add space at the end of the menu for the submenu expansion arrow
203 // this will also allow offsetting the accel string from the right edge
204 *pwidth
+= GetMarginWidth() + 16;
206 else // don't draw the text, just the bitmap (if any)
212 // increase size to accommodate bigger bitmaps if necessary
213 if (m_bmpChecked
.Ok())
215 // Is BMP height larger then text height?
216 size_t adjustedHeight
= m_bmpChecked
.GetHeight() +
217 2*wxSystemSettings::GetMetric(wxSYS_EDGE_Y
);
218 if (*pheight
< adjustedHeight
)
219 *pheight
= adjustedHeight
;
221 const size_t widthBmp
= m_bmpChecked
.GetWidth();
222 if ( IsOwnerDrawn() )
224 // widen the margin to fit the bitmap if necessary
225 if ((size_t)GetMarginWidth() < widthBmp
)
226 SetMarginWidth(widthBmp
);
228 else // we must allocate enough space for the bitmap
234 // add a 4-pixel separator, otherwise menus look cluttered
237 // make sure that this item is at least as tall as the system menu height
238 if ( *pheight
< m_nMinHeight
)
239 *pheight
= m_nMinHeight
;
241 // remember height for use in OnDrawItem
242 m_nHeight
= *pheight
;
248 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
,
253 // we do nothing on focus change
254 if ( act
== wxODFocusChanged
)
258 // this flag determines whether or not an edge will
259 // be drawn around the bitmap. In most "windows classic"
260 // applications, a 1-pixel highlight edge is drawn around
261 // the bitmap of an item when it is selected. However,
262 // with the new "luna" theme, no edge is drawn around
263 // the bitmap because the background is white (this applies
264 // only to "non-XP style" menus w/ bitmaps --
265 // see IE 6 menus for an example)
267 bool draw_bitmap_edge
= true;
271 DWORD colBack
, colText
;
272 if ( st
& wxODSelected
)
274 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
275 if (!(st
& wxODDisabled
))
277 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
281 colText
= GetSysColor(COLOR_GRAYTEXT
);
286 // fall back to default colors if none explicitly specified
287 colBack
= m_colBack
.Ok() ? wxColourToPalRGB(m_colBack
)
288 : GetSysColor(COLOR_MENU
);
289 colText
= m_colText
.Ok() ? wxColourToPalRGB(m_colText
)
290 : GetSysColor(COLOR_MENUTEXT
);
293 if ( IsOwnerDrawn() )
295 // don't draw an edge around the bitmap, if background is white ...
296 DWORD menu_bg_color
= GetSysColor(COLOR_MENU
);
297 if ( ( GetRValue( menu_bg_color
) >= 0xf0 &&
298 GetGValue( menu_bg_color
) >= 0xf0 &&
299 GetBValue( menu_bg_color
) >= 0xf0 )
302 draw_bitmap_edge
= false;
305 else // edge doesn't look well with default Windows drawing
307 draw_bitmap_edge
= false;
311 HDC hdc
= GetHdcOf(dc
);
312 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
313 colOldBack
= ::SetBkColor(hdc
, colBack
);
315 // *2, as in wxSYS_EDGE_Y
316 int margin
= GetMarginWidth() + 2 * wxSystemSettings::GetMetric(wxSYS_EDGE_X
);
318 // select the font and draw the text
319 // ---------------------------------
322 // determine where to draw and leave space for a check-mark.
323 // + 1 pixel to separate the edge from the highlight rectangle
324 int xText
= rc
.x
+ margin
+ 1;
327 // using native API because it recognizes '&'
328 if ( IsOwnerDrawn() )
330 int nPrevMode
= SetBkMode(hdc
, TRANSPARENT
);
331 AutoHBRUSH
hbr(colBack
);
332 SelectInHDC
selBrush(hdc
, hbr
);
334 RECT rectFill
= { rc
.GetLeft(), rc
.GetTop(),
335 rc
.GetRight() + 1, rc
.GetBottom() + 1 };
337 if ( (st
& wxODSelected
) && m_bmpChecked
.Ok() && draw_bitmap_edge
)
339 // only draw the highlight under the text, not under
340 // the bitmap or checkmark
341 rectFill
.left
= xText
;
344 FillRect(hdc
, &rectFill
, hbr
);
346 // use default font if no font set
347 wxFont fontToUse
= GetFontToUse();
348 SelectInHDC
selFont(hdc
, GetHfontOf(fontToUse
));
350 wxString strMenuText
= m_strName
.BeforeFirst('\t');
352 xText
+= 3; // separate text from the highlight rectangle
355 ::GetTextExtentPoint32(hdc
, strMenuText
.c_str(), strMenuText
.length(), &sizeRect
);
356 ::DrawState(hdc
, NULL
, NULL
,
357 (LPARAM
)strMenuText
.c_str(), strMenuText
.length(),
358 xText
, rc
.y
+ (int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0), // centre text vertically
359 rc
.GetWidth()-margin
, sizeRect
.cy
,
361 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0) |
362 (((st
& wxODHidePrefix
) && !wxMSWSystemMenuFontModule::ms_showCues
) ? 512 : 0)); // 512 == DSS_HIDEPREFIX
364 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
365 // as the last parameter in DrawState() (at least with Windows98). So we have
366 // to take care of right alignment ourselves.
367 if ( !m_strAccel
.empty() )
369 int accel_width
, accel_height
;
370 dc
.GetTextExtent(m_strAccel
, &accel_width
, &accel_height
);
371 // right align accel string with right edge of menu ( offset by the
373 ::DrawState(hdc
, NULL
, NULL
,
374 (LPARAM
)m_strAccel
.c_str(), m_strAccel
.length(),
375 rc
.GetWidth()-16-accel_width
, rc
.y
+(int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0),
378 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0));
381 (void)SetBkMode(hdc
, nPrevMode
);
387 if ( IsCheckable() && !m_bmpChecked
.Ok() )
389 if ( st
& wxODChecked
)
391 // what goes on: DrawFrameControl creates a b/w mask,
392 // then we copy it to screen to have right colors
394 // first create a monochrome bitmap in a memory DC
395 HDC hdcMem
= CreateCompatibleDC(hdc
);
396 HBITMAP hbmpCheck
= CreateBitmap(margin
, m_nHeight
, 1, 1, 0);
397 SelectObject(hdcMem
, hbmpCheck
);
399 // then draw a check mark into it
400 RECT rect
= { 0, 0, margin
, m_nHeight
};
403 ::DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
406 // finally copy it to screen DC and clean up
407 BitBlt(hdc
, rc
.x
, rc
.y
, margin
, m_nHeight
, hdcMem
, 0, 0, SRCCOPY
);
410 DeleteObject(hbmpCheck
);
417 if ( st
& wxODDisabled
)
419 bmp
= GetDisabledBitmap();
424 // for not checkable bitmaps we should always use unchecked one because
425 // their checked bitmap is not set
426 bmp
= GetBitmap(!IsCheckable() || (st
& wxODChecked
));
431 wxMemoryDC
dcMem(&dc
);
432 dcMem
.SelectObject(bmp
);
435 int nBmpWidth
= bmp
.GetWidth(),
436 nBmpHeight
= bmp
.GetHeight();
438 // there should be enough space!
439 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
441 int heightDiff
= m_nHeight
- nBmpHeight
;
442 dc
.Blit(rc
.x
+ (margin
- nBmpWidth
) / 2,
443 rc
.y
+ heightDiff
/ 2,
444 nBmpWidth
, nBmpHeight
,
445 &dcMem
, 0, 0, wxCOPY
, true /* use mask */);
447 if ( ( st
& wxODSelected
) && !( st
& wxODDisabled
) && draw_bitmap_edge
)
449 RECT rectBmp
= { rc
.GetLeft(), rc
.GetTop(),
450 rc
.GetLeft() + margin
,
451 rc
.GetTop() + m_nHeight
};
452 SetBkColor(hdc
, colBack
);
454 DrawEdge(hdc
, &rectBmp
, BDR_RAISEDINNER
, BF_RECT
);
459 ::SetTextColor(hdc
, colOldText
);
460 ::SetBkColor(hdc
, colOldBack
);
466 #endif // wxUSE_OWNER_DRAWN