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 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 by 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
)
95 // ============================================================================
96 // implementation of wxOwnerDrawn class
97 // ============================================================================
101 wxOwnerDrawn::wxOwnerDrawn(const wxString
& str
,
102 bool bCheckable
, bool bMenuItem
)
105 // get the default menu height and font from the system
107 nm
.cbSize
= sizeof (NONCLIENTMETRICS
);
108 SystemParametersInfo (SPI_GETNONCLIENTMETRICS
,0,&nm
,0);
109 m_nMinHeight
= nm
.iMenuHeight
;
111 // nm.iMenuWidth is the system default for the width of
112 // menu icons and checkmarks
113 if (ms_nDefaultMarginWidth
== 0)
115 ms_nDefaultMarginWidth
= ::GetSystemMetrics(SM_CXMENUCHECK
) + wxSystemSettings::GetMetric(wxSYS_EDGE_X
);
116 ms_nLastMarginWidth
= ms_nDefaultMarginWidth
;
119 if (wxMSWSystemMenuFontModule::ms_systemMenuFont
->Ok() && bMenuItem
)
121 m_font
= *wxMSWSystemMenuFontModule::ms_systemMenuFont
;
125 m_font
= *wxNORMAL_FONT
;
128 m_bCheckable
= bCheckable
;
129 m_bOwnerDrawn
= false;
131 m_nMarginWidth
= ms_nLastMarginWidth
;
132 m_nMinHeight
= wxMSWSystemMenuFontModule::ms_systemMenuHeight
;
134 m_bmpDisabled
= wxNullBitmap
;
138 // these items will be set during the first invocation of the c'tor,
139 // because the values will be determined by checking the system settings,
140 // which is a chunk of code
141 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 0;
142 size_t wxOwnerDrawn::ms_nLastMarginWidth
= 0;
148 // get size of the item
149 // The item size includes the menu string, the accel string,
150 // the bitmap and size for a submenu expansion arrow...
151 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth
, size_t *pheight
)
155 wxString str
= wxStripMenuCodes(m_strName
);
157 // if we have a valid accel string, then pad out
158 // the menu string so the menu and accel string are not
159 // placed ontop of eachother.
160 if ( !m_strAccel
.empty() )
162 str
.Pad(str
.Length()%8
);
167 dc
.SetFont(GetFont());
169 dc
.GetTextExtent(str
, (long *)pwidth
, (long *)pheight
);
171 // increase size to accommodate bigger bitmaps if necessary
172 if (m_bmpChecked
.Ok())
174 // Is BMP height larger then text height?
175 size_t adjustedHeight
= m_bmpChecked
.GetHeight() +
176 2*wxSystemSettings::GetMetric(wxSYS_EDGE_Y
);
177 if (*pheight
< adjustedHeight
)
178 *pheight
= adjustedHeight
;
180 // Does BMP encroach on default check menu position?
181 size_t adjustedWidth
= m_bmpChecked
.GetWidth();
183 // Do we need to widen margin to fit BMP?
184 if ((size_t)GetMarginWidth() < adjustedWidth
)
185 SetMarginWidth(adjustedWidth
);
188 // add space at the end of the menu for the submenu expansion arrow
189 // this will also allow offsetting the accel string from the right edge
190 *pwidth
+= GetMarginWidth() + 16;
192 // add a 4-pixel separator, otherwise menus look cluttered
195 // make sure that this item is at least as
196 // tall as the user's system settings specify
197 if (*pheight
< m_nMinHeight
)
198 *pheight
= m_nMinHeight
;
200 // remember height for use in OnDrawItem
201 m_nHeight
= *pheight
;
207 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
,
212 // we do nothing on focus change
213 if ( act
== wxODFocusChanged
)
217 // this flag determines whether or not an edge will
218 // be drawn around the bitmap. In most "windows classic"
219 // applications, a 1-pixel highlight edge is drawn around
220 // the bitmap of an item when it is selected. However,
221 // with the new "luna" theme, no edge is drawn around
222 // the bitmap because the background is white (this applies
223 // only to "non-XP style" menus w/ bitmaps --
224 // see IE 6 menus for an example)
226 bool draw_bitmap_edge
= true;
230 DWORD colBack
, colText
;
231 if ( st
& wxODSelected
) {
232 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
233 if (!(st
& wxODDisabled
))
235 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
239 colText
= GetSysColor(COLOR_GRAYTEXT
);
243 // fall back to default colors if none explicitly specified
244 colBack
= m_colBack
.Ok() ? wxColourToPalRGB(m_colBack
)
245 : GetSysColor(COLOR_MENU
);
246 colText
= m_colText
.Ok() ? wxColourToPalRGB(m_colText
)
247 : GetSysColor(COLOR_MENUTEXT
);
251 // don't draw an edge around the bitmap, if background is white ...
252 DWORD menu_bg_color
= GetSysColor(COLOR_MENU
);
253 if ( ( GetRValue( menu_bg_color
) >= 0xf0 &&
254 GetGValue( menu_bg_color
) >= 0xf0 &&
255 GetBValue( menu_bg_color
) >= 0xf0 )
258 draw_bitmap_edge
= false;
262 HDC hdc
= GetHdcOf(dc
);
263 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
264 colOldBack
= ::SetBkColor(hdc
, colBack
);
266 // *2, as in wxSYS_EDGE_Y
267 int margin
= GetMarginWidth() + 2 * wxSystemSettings::GetMetric(wxSYS_EDGE_X
);
269 // select the font and draw the text
270 // ---------------------------------
273 // determine where to draw and leave space for a check-mark.
274 // + 1 pixel to separate the edge from the highlight rectangle
275 int xText
= rc
.x
+ margin
+ 1;
278 // using native API because it reckognizes '&'
279 int nPrevMode
= SetBkMode(hdc
, TRANSPARENT
);
280 HBRUSH hbr
= CreateSolidBrush(colBack
),
281 hPrevBrush
= (HBRUSH
)SelectObject(hdc
, hbr
);
283 RECT rectFill
= { rc
.GetLeft(), rc
.GetTop(),
284 rc
.GetRight() + 1, rc
.GetBottom() + 1 };
286 if ( (st
& wxODSelected
) && m_bmpChecked
.Ok() && draw_bitmap_edge
) {
287 // only draw the highlight under the text, not under
288 // the bitmap or checkmark
289 rectFill
.left
= xText
;
292 FillRect(hdc
, &rectFill
, hbr
);
294 // use default font if no font set
297 m_font
.RealizeResource();
298 hfont
= (HFONT
)m_font
.GetResourceHandle();
301 hfont
= (HFONT
)::GetStockObject(SYSTEM_FONT
);
304 HFONT hPrevFont
= (HFONT
) ::SelectObject(hdc
, hfont
);
306 wxString strMenuText
= m_strName
.BeforeFirst('\t');
308 xText
+= 3; // separate text from the highlight rectangle
311 GetTextExtentPoint32(hdc
,strMenuText
.c_str(), strMenuText
.Length(),&sizeRect
);
312 ::DrawState(hdc
, NULL
, NULL
,
313 (LPARAM
)strMenuText
.c_str(), strMenuText
.length(),
314 xText
, rc
.y
+ (int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0), // centre text vertically
315 rc
.GetWidth()-margin
, sizeRect
.cy
,
317 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0) |
318 (((st
& wxODHidePrefix
) && !wxMSWSystemMenuFontModule::ms_showCues
) ? 512 : 0)); // 512 == DSS_HIDEPREFIX
320 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
321 // as last parameter in DrawState() (at least with Windows98). So we have
322 // to take care of right alignment ourselves.
323 if ( !m_strAccel
.empty() )
325 int accel_width
, accel_height
;
326 dc
.GetTextExtent(m_strAccel
, &accel_width
, &accel_height
);
327 // right align accel string with right edge of menu ( offset by the
329 ::DrawState(hdc
, NULL
, NULL
,
330 (LPARAM
)m_strAccel
.c_str(), m_strAccel
.length(),
331 rc
.GetWidth()-16-accel_width
, rc
.y
+(int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0),
334 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0));
337 (void)SelectObject(hdc
, hPrevBrush
);
338 (void)SelectObject(hdc
, hPrevFont
);
339 (void)SetBkMode(hdc
, nPrevMode
);
345 if ( IsCheckable() && !m_bmpChecked
.Ok() ) {
346 if ( st
& wxODChecked
) {
347 // what goes on: DrawFrameControl creates a b/w mask,
348 // then we copy it to screen to have right colors
350 // first create a monochrome bitmap in a memory DC
351 HDC hdcMem
= CreateCompatibleDC(hdc
);
352 HBITMAP hbmpCheck
= CreateBitmap(margin
, m_nHeight
, 1, 1, 0);
353 SelectObject(hdcMem
, hbmpCheck
);
355 // then draw a check mark into it
356 RECT rect
= { 0, 0, margin
, m_nHeight
};
359 ::DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
362 // finally copy it to screen DC and clean up
363 BitBlt(hdc
, rc
.x
, rc
.y
, margin
, m_nHeight
,
364 hdcMem
, 0, 0, SRCCOPY
);
367 DeleteObject(hbmpCheck
);
373 if ( st
& wxODDisabled
)
375 bmp
= GetDisabledBitmap();
380 // for not checkable bitmaps we should always use unchecked one because
381 // their checked bitmap is not set
382 bmp
= GetBitmap(!IsCheckable() || (st
& wxODChecked
));
386 wxMemoryDC
dcMem(&dc
);
387 dcMem
.SelectObject(bmp
);
390 int nBmpWidth
= bmp
.GetWidth(),
391 nBmpHeight
= bmp
.GetHeight();
393 // there should be enough place!
394 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
396 int heightDiff
= m_nHeight
- nBmpHeight
;
397 dc
.Blit(rc
.x
+ (margin
- nBmpWidth
) / 2,
398 rc
.y
+ heightDiff
/ 2,
399 nBmpWidth
, nBmpHeight
,
400 &dcMem
, 0, 0, wxCOPY
, true /* use mask */);
402 if ( ( st
& wxODSelected
) && !( st
& wxODDisabled
) && draw_bitmap_edge
) {
403 RECT rectBmp
= { rc
.GetLeft(), rc
.GetTop(),
404 rc
.GetLeft() + margin
,
405 rc
.GetTop() + m_nHeight
};
406 SetBkColor(hdc
, colBack
);
408 DrawEdge(hdc
, &rectBmp
, BDR_RAISEDINNER
, BF_RECT
);
413 ::SetTextColor(hdc
, colOldText
);
414 ::SetBkColor(hdc
, colOldBack
);
420 #endif // wxUSE_OWNER_DRAWN