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"
39 #ifndef SPI_GETKEYBOARDCUES
40 #define SPI_GETKEYBOARDCUES 0x100A
43 class wxMSWSystemMenuFontModule
: public wxModule
48 WinStruct
<NONCLIENTMETRICS
> nm
;
49 ::SystemParametersInfo(SPI_GETNONCLIENTMETRICS
, 0, &nm
, 0);
51 ms_systemMenuButtonWidth
= nm
.iMenuHeight
;
53 // iMenuHeight is the menu bar height and the menu items are less tall,
54 // although I don't know by how much -- below is the value for my system
55 ms_systemMenuHeight
= nm
.iMenuHeight
- 4;
58 wxNativeFontInfo info
;
59 memcpy(&info
.lf
, &nm
.lfMenuFont
, sizeof(LOGFONT
));
60 ms_systemMenuFont
= new wxFont(info
);
62 if ( ::SystemParametersInfo(SPI_GETKEYBOARDCUES
, 0,
63 &ms_showCues
, 0) == 0 )
65 // if it's not supported, we must be on an old Windows version
66 // which always shows them
75 delete ms_systemMenuFont
;
76 ms_systemMenuFont
= NULL
;
79 static wxFont
* ms_systemMenuFont
;
80 static int ms_systemMenuButtonWidth
;
81 static int ms_systemMenuHeight
;
82 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;
94 int wxMSWSystemMenuFontModule::ms_systemMenuHeight
= 18;
95 bool wxMSWSystemMenuFontModule::ms_showCues
= true;
97 IMPLEMENT_DYNAMIC_CLASS(wxMSWSystemMenuFontModule
, wxModule
)
100 // VC++ 6 gives a warning here:
102 // return type for 'OwnerDrawnSet_wxImplementation_HashTable::iterator::
103 // operator ->' is 'class wxOwnerDrawn ** ' (ie; not a UDT or reference to
104 // a UDT. Will produce errors if applied using infix notation.
107 #if defined __VISUALC__ && __VISUALC__ <= 1300
108 #if __VISUALC__ >= 1200
109 #pragma warning(push)
112 #pragma warning(disable: 4284)
115 #include "wx/hashset.h"
116 WX_DECLARE_HASH_SET(wxOwnerDrawn
*, wxPointerHash
, wxPointerEqual
, OwnerDrawnSet
);
122 // ============================================================================
123 // implementation of wxOwnerDrawn class
124 // ============================================================================
128 wxOwnerDrawn::wxOwnerDrawn(const wxString
& str
,
133 if ( ms_nDefaultMarginWidth
== 0 )
135 ms_nDefaultMarginWidth
= ::GetSystemMetrics(SM_CXMENUCHECK
) +
136 wxSystemSettings::GetMetric(wxSYS_EDGE_X
);
137 ms_nLastMarginWidth
= ms_nDefaultMarginWidth
;
140 m_bCheckable
= bCheckable
;
141 m_bOwnerDrawn
= false;
142 m_isMenuItem
= bMenuItem
;
144 m_nMarginWidth
= ms_nLastMarginWidth
;
147 wxOwnerDrawn::~wxOwnerDrawn()
151 bool wxOwnerDrawn::IsMenuItem() const
157 // these items will be set during the first invocation of the ctor,
158 // because the values will be determined by checking the system settings,
159 // which is a chunk of code
160 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 0;
161 size_t wxOwnerDrawn::ms_nLastMarginWidth
= 0;
167 wxFont
wxOwnerDrawn::GetFontToUse() const
169 wxFont font
= m_font
;
173 font
= *wxMSWSystemMenuFontModule::ms_systemMenuFont
;
176 font
= *wxNORMAL_FONT
;
182 // get size of the item
183 // The item size includes the menu string, the accel string,
184 // the bitmap and size for a submenu expansion arrow...
185 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth
, size_t *pheight
)
187 if ( IsOwnerDrawn() )
191 wxString str
= wxStripMenuCodes(m_strName
);
193 // if we have a valid accel string, then pad out
194 // the menu string so that the menu and accel string are not
195 // placed on top of each other.
196 if ( !m_strAccel
.empty() )
198 str
.Pad(str
.length()%8
);
202 dc
.SetFont(GetFontToUse());
205 dc
.GetTextExtent(str
, &w
, &h
);
209 // add space at the end of the menu for the submenu expansion arrow
210 // this will also allow offsetting the accel string from the right edge
211 *pwidth
+= GetMarginWidth() + 16;
213 else // don't draw the text, just the bitmap (if any)
219 // increase size to accommodate bigger bitmaps if necessary
220 if (m_bmpChecked
.Ok())
222 // Is BMP height larger than text height?
223 size_t adjustedHeight
= m_bmpChecked
.GetHeight();
224 if ( *pheight
< adjustedHeight
)
225 *pheight
= adjustedHeight
;
227 const size_t widthBmp
= m_bmpChecked
.GetWidth();
228 if ( IsOwnerDrawn() )
230 // widen the margin to fit the bitmap if necessary
231 if ((size_t)GetMarginWidth() < widthBmp
)
232 SetMarginWidth(widthBmp
);
234 else // we must allocate enough space for the bitmap
240 // add a 4-pixel separator, otherwise menus look cluttered
243 // make sure that this item is at least as tall as the system menu height
244 const size_t heightStd
= wxMSWSystemMenuFontModule::ms_systemMenuHeight
;
245 if ( *pheight
< heightStd
)
246 *pheight
= heightStd
;
248 // remember height for use in OnDrawItem
249 m_nHeight
= *pheight
;
255 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
,
260 // we do nothing on focus change
261 if ( act
== wxODFocusChanged
)
265 // this flag determines whether or not an edge will
266 // be drawn around the bitmap. In most "windows classic"
267 // applications, a 1-pixel highlight edge is drawn around
268 // the bitmap of an item when it is selected. However,
269 // with the new "luna" theme, no edge is drawn around
270 // the bitmap because the background is white (this applies
271 // only to "non-XP style" menus w/ bitmaps --
272 // see IE 6 menus for an example)
274 bool draw_bitmap_edge
= true;
278 DWORD colBack
, colText
;
279 if ( st
& wxODSelected
)
281 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
282 if (!(st
& wxODDisabled
))
284 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
288 colText
= GetSysColor(COLOR_GRAYTEXT
);
293 // fall back to default colors if none explicitly specified
294 colBack
= m_colBack
.Ok() ? wxColourToPalRGB(m_colBack
)
295 : GetSysColor(COLOR_MENU
);
296 colText
= m_colText
.Ok() ? wxColourToPalRGB(m_colText
)
297 : GetSysColor(COLOR_MENUTEXT
);
300 if ( IsOwnerDrawn() )
302 // don't draw an edge around the bitmap, if background is white ...
303 DWORD menu_bg_color
= GetSysColor(COLOR_MENU
);
304 if ( ( GetRValue( menu_bg_color
) >= 0xf0 &&
305 GetGValue( menu_bg_color
) >= 0xf0 &&
306 GetBValue( menu_bg_color
) >= 0xf0 )
309 draw_bitmap_edge
= false;
312 else // edge doesn't look well with default Windows drawing
314 draw_bitmap_edge
= false;
318 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
319 HDC hdc
= GetHdcOf(*impl
);
320 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
321 colOldBack
= ::SetBkColor(hdc
, colBack
);
323 // *2, as in wxSYS_EDGE_Y
324 int margin
= GetMarginWidth() + 2 * wxSystemSettings::GetMetric(wxSYS_EDGE_X
);
326 // select the font and draw the text
327 // ---------------------------------
330 // determine where to draw and leave space for a check-mark.
331 // + 1 pixel to separate the edge from the highlight rectangle
332 int xText
= rc
.x
+ margin
+ 1;
335 // using native API because it recognizes '&'
336 if ( IsOwnerDrawn() )
338 int nPrevMode
= SetBkMode(hdc
, TRANSPARENT
);
339 AutoHBRUSH
hbr(colBack
);
340 SelectInHDC
selBrush(hdc
, hbr
);
342 RECT rectFill
= { rc
.GetLeft(), rc
.GetTop(),
343 rc
.GetRight() + 1, rc
.GetBottom() + 1 };
345 if ( (st
& wxODSelected
) && m_bmpChecked
.Ok() && draw_bitmap_edge
)
347 // only draw the highlight under the text, not under
348 // the bitmap or checkmark
349 rectFill
.left
= xText
;
352 FillRect(hdc
, &rectFill
, hbr
);
354 // use default font if no font set
355 wxFont fontToUse
= GetFontToUse();
356 SelectInHDC
selFont(hdc
, GetHfontOf(fontToUse
));
358 wxString strMenuText
= m_strName
.BeforeFirst('\t');
360 xText
+= 3; // separate text from the highlight rectangle
363 ::GetTextExtentPoint32(hdc
, strMenuText
.c_str(), strMenuText
.length(), &sizeRect
);
364 ::DrawState(hdc
, NULL
, NULL
,
365 (LPARAM
)strMenuText
.wx_str(),
366 strMenuText
.length(),
367 xText
, rc
.y
+ (int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0), // centre text vertically
368 rc
.GetWidth()-margin
, sizeRect
.cy
,
370 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0) |
371 (((st
& wxODHidePrefix
) && !wxMSWSystemMenuFontModule::ms_showCues
) ? 512 : 0)); // 512 == DSS_HIDEPREFIX
373 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
374 // as the last parameter in DrawState() (at least with Windows98). So we have
375 // to take care of right alignment ourselves.
376 if ( !m_strAccel
.empty() )
378 int accel_width
, accel_height
;
379 dc
.GetTextExtent(m_strAccel
, &accel_width
, &accel_height
);
380 // right align accel string with right edge of menu ( offset by the
382 ::DrawState(hdc
, NULL
, NULL
,
383 (LPARAM
)m_strAccel
.wx_str(),
385 rc
.GetWidth()-16-accel_width
, rc
.y
+(int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0),
388 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0));
391 (void)SetBkMode(hdc
, nPrevMode
);
397 if ( IsCheckable() && !m_bmpChecked
.Ok() )
399 if ( st
& wxODChecked
)
401 // what goes on: DrawFrameControl creates a b/w mask,
402 // then we copy it to screen to have right colors
404 // first create a monochrome bitmap in a memory DC
405 HDC hdcMem
= CreateCompatibleDC(hdc
);
406 HBITMAP hbmpCheck
= CreateBitmap(margin
, m_nHeight
, 1, 1, 0);
407 SelectObject(hdcMem
, hbmpCheck
);
409 // then draw a check mark into it
410 RECT rect
= { 0, 0, margin
, m_nHeight
};
413 ::DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
416 // finally copy it to screen DC and clean up
417 BitBlt(hdc
, rc
.x
, rc
.y
, margin
, m_nHeight
, hdcMem
, 0, 0, SRCCOPY
);
420 DeleteObject(hbmpCheck
);
427 if ( st
& wxODDisabled
)
429 bmp
= GetDisabledBitmap();
434 // for not checkable bitmaps we should always use unchecked one
435 // because their checked bitmap is not set
436 bmp
= GetBitmap(!IsCheckable() || (st
& wxODChecked
));
439 if ( bmp
.Ok() && st
& wxODDisabled
)
441 // we need to grey out the bitmap as we don't have any specific
443 wxImage imgGrey
= bmp
.ConvertToImage().ConvertToGreyscale();
445 bmp
= wxBitmap(imgGrey
);
447 #endif // wxUSE_IMAGE
452 wxMemoryDC
dcMem(&dc
);
453 dcMem
.SelectObjectAsSource(bmp
);
456 int nBmpWidth
= bmp
.GetWidth(),
457 nBmpHeight
= bmp
.GetHeight();
459 // there should be enough space!
460 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
462 int heightDiff
= m_nHeight
- nBmpHeight
;
463 dc
.Blit(rc
.x
+ (margin
- nBmpWidth
) / 2,
464 rc
.y
+ heightDiff
/ 2,
465 nBmpWidth
, nBmpHeight
,
466 &dcMem
, 0, 0, wxCOPY
, true /* use mask */);
468 if ( ( st
& wxODSelected
) && !( st
& wxODDisabled
) && draw_bitmap_edge
)
470 RECT rectBmp
= { rc
.GetLeft(), rc
.GetTop(),
471 rc
.GetLeft() + margin
,
472 rc
.GetTop() + m_nHeight
};
473 SetBkColor(hdc
, colBack
);
475 DrawEdge(hdc
, &rectBmp
, BDR_RAISEDINNER
, BF_RECT
);
480 ::SetTextColor(hdc
, colOldText
);
481 ::SetBkColor(hdc
, colOldBack
);
487 #endif // wxUSE_OWNER_DRAWN