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 by 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
)
99 // ============================================================================
100 // implementation of wxOwnerDrawn class
101 // ============================================================================
105 wxOwnerDrawn::wxOwnerDrawn(const wxString
& str
,
106 bool bCheckable
, bool bMenuItem
)
109 // get the default menu height and font from the system
111 nm
.cbSize
= sizeof (NONCLIENTMETRICS
);
112 SystemParametersInfo (SPI_GETNONCLIENTMETRICS
,0,&nm
,0);
113 m_nMinHeight
= nm
.iMenuHeight
;
115 // nm.iMenuWidth is the system default for the width of
116 // menu icons and checkmarks
117 if (ms_nDefaultMarginWidth
== 0)
119 ms_nDefaultMarginWidth
= ::GetSystemMetrics(SM_CXMENUCHECK
) + wxSystemSettings::GetMetric(wxSYS_EDGE_X
);
120 ms_nLastMarginWidth
= ms_nDefaultMarginWidth
;
123 if (wxMSWSystemMenuFontModule::ms_systemMenuFont
->Ok() && bMenuItem
)
125 m_font
= *wxMSWSystemMenuFontModule::ms_systemMenuFont
;
129 m_font
= *wxNORMAL_FONT
;
132 m_bCheckable
= bCheckable
;
133 m_bOwnerDrawn
= false;
135 m_nMarginWidth
= ms_nLastMarginWidth
;
136 m_nMinHeight
= wxMSWSystemMenuFontModule::ms_systemMenuHeight
;
138 m_bmpDisabled
= wxNullBitmap
;
142 // these items will be set during the first invocation of the c'tor,
143 // because the values will be determined by checking the system settings,
144 // which is a chunk of code
145 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 0;
146 size_t wxOwnerDrawn::ms_nLastMarginWidth
= 0;
152 // get size of the item
153 // The item size includes the menu string, the accel string,
154 // the bitmap and size for a submenu expansion arrow...
155 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth
, size_t *pheight
)
159 wxString str
= wxStripMenuCodes(m_strName
);
161 // if we have a valid accel string, then pad out
162 // the menu string so the menu and accel string are not
163 // placed ontop of eachother.
164 if ( !m_strAccel
.empty() )
166 str
.Pad(str
.Length()%8
);
171 dc
.SetFont(GetFont());
173 dc
.GetTextExtent(str
, (long *)pwidth
, (long *)pheight
);
175 // increase size to accommodate bigger bitmaps if necessary
176 if (m_bmpChecked
.Ok())
178 // Is BMP height larger then text height?
179 size_t adjustedHeight
= m_bmpChecked
.GetHeight() +
180 2*wxSystemSettings::GetMetric(wxSYS_EDGE_Y
);
181 if (*pheight
< adjustedHeight
)
182 *pheight
= adjustedHeight
;
184 // Does BMP encroach on default check menu position?
185 size_t adjustedWidth
= m_bmpChecked
.GetWidth();
187 // Do we need to widen margin to fit BMP?
188 if ((size_t)GetMarginWidth() < adjustedWidth
)
189 SetMarginWidth(adjustedWidth
);
192 // add space at the end of the menu for the submenu expansion arrow
193 // this will also allow offsetting the accel string from the right edge
194 *pwidth
+= GetMarginWidth() + 16;
196 // add a 4-pixel separator, otherwise menus look cluttered
199 // make sure that this item is at least as
200 // tall as the user's system settings specify
201 if (*pheight
< m_nMinHeight
)
202 *pheight
= m_nMinHeight
;
204 // remember height for use in OnDrawItem
205 m_nHeight
= *pheight
;
211 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
,
216 // we do nothing on focus change
217 if ( act
== wxODFocusChanged
)
221 // this flag determines whether or not an edge will
222 // be drawn around the bitmap. In most "windows classic"
223 // applications, a 1-pixel highlight edge is drawn around
224 // the bitmap of an item when it is selected. However,
225 // with the new "luna" theme, no edge is drawn around
226 // the bitmap because the background is white (this applies
227 // only to "non-XP style" menus w/ bitmaps --
228 // see IE 6 menus for an example)
230 bool draw_bitmap_edge
= true;
234 DWORD colBack
, colText
;
235 if ( st
& wxODSelected
) {
236 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
237 if (!(st
& wxODDisabled
))
239 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
243 colText
= GetSysColor(COLOR_GRAYTEXT
);
247 // fall back to default colors if none explicitly specified
248 colBack
= m_colBack
.Ok() ? wxColourToPalRGB(m_colBack
)
249 : GetSysColor(COLOR_MENU
);
250 colText
= m_colText
.Ok() ? wxColourToPalRGB(m_colText
)
251 : GetSysColor(COLOR_MENUTEXT
);
255 // don't draw an edge around the bitmap, if background is white ...
256 DWORD menu_bg_color
= GetSysColor(COLOR_MENU
);
257 if ( ( GetRValue( menu_bg_color
) >= 0xf0 &&
258 GetGValue( menu_bg_color
) >= 0xf0 &&
259 GetBValue( menu_bg_color
) >= 0xf0 )
262 draw_bitmap_edge
= false;
266 HDC hdc
= GetHdcOf(dc
);
267 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
268 colOldBack
= ::SetBkColor(hdc
, colBack
);
270 // *2, as in wxSYS_EDGE_Y
271 int margin
= GetMarginWidth() + 2 * wxSystemSettings::GetMetric(wxSYS_EDGE_X
);
273 // select the font and draw the text
274 // ---------------------------------
277 // determine where to draw and leave space for a check-mark.
278 // + 1 pixel to separate the edge from the highlight rectangle
279 int xText
= rc
.x
+ margin
+ 1;
282 // using native API because it reckognizes '&'
283 int nPrevMode
= SetBkMode(hdc
, TRANSPARENT
);
284 HBRUSH hbr
= CreateSolidBrush(colBack
),
285 hPrevBrush
= (HBRUSH
)SelectObject(hdc
, hbr
);
287 RECT rectFill
= { rc
.GetLeft(), rc
.GetTop(),
288 rc
.GetRight() + 1, rc
.GetBottom() + 1 };
290 if ( (st
& wxODSelected
) && m_bmpChecked
.Ok() && draw_bitmap_edge
) {
291 // only draw the highlight under the text, not under
292 // the bitmap or checkmark
293 rectFill
.left
= xText
;
296 FillRect(hdc
, &rectFill
, hbr
);
298 // use default font if no font set
301 m_font
.RealizeResource();
302 hfont
= (HFONT
)m_font
.GetResourceHandle();
305 hfont
= (HFONT
)::GetStockObject(SYSTEM_FONT
);
308 HFONT hPrevFont
= (HFONT
) ::SelectObject(hdc
, hfont
);
310 wxString strMenuText
= m_strName
.BeforeFirst('\t');
312 xText
+= 3; // separate text from the highlight rectangle
315 ::GetTextExtentPoint32(hdc
, strMenuText
.c_str(), strMenuText
.Length(), &sizeRect
);
316 ::DrawState(hdc
, NULL
, NULL
,
317 (LPARAM
)strMenuText
.c_str(), strMenuText
.length(),
318 xText
, rc
.y
+ (int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0), // centre text vertically
319 rc
.GetWidth()-margin
, sizeRect
.cy
,
321 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0) |
322 (((st
& wxODHidePrefix
) && !wxMSWSystemMenuFontModule::ms_showCues
) ? 512 : 0)); // 512 == DSS_HIDEPREFIX
324 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
325 // as last parameter in DrawState() (at least with Windows98). So we have
326 // to take care of right alignment ourselves.
327 if ( !m_strAccel
.empty() )
329 int accel_width
, accel_height
;
330 dc
.GetTextExtent(m_strAccel
, &accel_width
, &accel_height
);
331 // right align accel string with right edge of menu ( offset by the
333 ::DrawState(hdc
, NULL
, NULL
,
334 (LPARAM
)m_strAccel
.c_str(), m_strAccel
.length(),
335 rc
.GetWidth()-16-accel_width
, rc
.y
+(int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0),
338 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0));
341 (void)SelectObject(hdc
, hPrevBrush
);
342 (void)SelectObject(hdc
, hPrevFont
);
343 (void)SetBkMode(hdc
, nPrevMode
);
349 if ( IsCheckable() && !m_bmpChecked
.Ok() ) {
350 if ( st
& wxODChecked
) {
351 // what goes on: DrawFrameControl creates a b/w mask,
352 // then we copy it to screen to have right colors
354 // first create a monochrome bitmap in a memory DC
355 HDC hdcMem
= CreateCompatibleDC(hdc
);
356 HBITMAP hbmpCheck
= CreateBitmap(margin
, m_nHeight
, 1, 1, 0);
357 SelectObject(hdcMem
, hbmpCheck
);
359 // then draw a check mark into it
360 RECT rect
= { 0, 0, margin
, m_nHeight
};
363 ::DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
366 // finally copy it to screen DC and clean up
367 BitBlt(hdc
, rc
.x
, rc
.y
, margin
, m_nHeight
,
368 hdcMem
, 0, 0, SRCCOPY
);
371 DeleteObject(hbmpCheck
);
377 if ( st
& wxODDisabled
)
379 bmp
= GetDisabledBitmap();
384 // for not checkable bitmaps we should always use unchecked one because
385 // their checked bitmap is not set
386 bmp
= GetBitmap(!IsCheckable() || (st
& wxODChecked
));
390 wxMemoryDC
dcMem(&dc
);
391 dcMem
.SelectObject(bmp
);
394 int nBmpWidth
= bmp
.GetWidth(),
395 nBmpHeight
= bmp
.GetHeight();
397 // there should be enough place!
398 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
400 int heightDiff
= m_nHeight
- nBmpHeight
;
401 dc
.Blit(rc
.x
+ (margin
- nBmpWidth
) / 2,
402 rc
.y
+ heightDiff
/ 2,
403 nBmpWidth
, nBmpHeight
,
404 &dcMem
, 0, 0, wxCOPY
, true /* use mask */);
406 if ( ( st
& wxODSelected
) && !( st
& wxODDisabled
) && draw_bitmap_edge
) {
407 RECT rectBmp
= { rc
.GetLeft(), rc
.GetTop(),
408 rc
.GetLeft() + margin
,
409 rc
.GetTop() + m_nHeight
};
410 SetBkColor(hdc
, colBack
);
412 DrawEdge(hdc
, &rectBmp
, BDR_RAISEDINNER
, BF_RECT
);
417 ::SetTextColor(hdc
, colOldText
);
418 ::SetBkColor(hdc
, colOldBack
);
424 #endif // wxUSE_OWNER_DRAWN