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 ///////////////////////////////////////////////////////////////////////////////
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
);
69 delete ms_systemMenuFont
;
70 ms_systemMenuFont
= NULL
;
73 static wxFont
* ms_systemMenuFont
;
74 static int ms_systemMenuButtonWidth
; // windows clean install default
75 static int ms_systemMenuHeight
; // windows clean install default
77 DECLARE_DYNAMIC_CLASS(wxMSWSystemMenuFontModule
)
80 // these static variables are by the wxMSWSystemMenuFontModule object
81 // and reflect the system settings returned by the Win32 API's
82 // SystemParametersInfo() call.
84 wxFont
* wxMSWSystemMenuFontModule::ms_systemMenuFont
= NULL
;
85 int wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth
= 18; // windows clean install default
86 int wxMSWSystemMenuFontModule::ms_systemMenuHeight
= 18; // windows clean install default
88 IMPLEMENT_DYNAMIC_CLASS(wxMSWSystemMenuFontModule
, wxModule
)
90 // ============================================================================
91 // implementation of wxOwnerDrawn class
92 // ============================================================================
96 wxOwnerDrawn::wxOwnerDrawn(const wxString
& str
,
97 bool bCheckable
, bool bMenuItem
)
100 // get the default menu height and font from the system
102 nm
.cbSize
= sizeof (NONCLIENTMETRICS
);
103 SystemParametersInfo (SPI_GETNONCLIENTMETRICS
,0,&nm
,0);
104 m_nMinHeight
= nm
.iMenuHeight
;
106 // nm.iMenuWidth is the system default for the width of
107 // menu icons and checkmarks
108 if (ms_nDefaultMarginWidth
== 0)
110 ms_nDefaultMarginWidth
= wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth
;
111 ms_nLastMarginWidth
= wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth
;
114 if (wxMSWSystemMenuFontModule::ms_systemMenuFont
->Ok() && bMenuItem
)
116 m_font
= *wxMSWSystemMenuFontModule::ms_systemMenuFont
;
120 m_font
= *wxNORMAL_FONT
;
123 m_bCheckable
= bCheckable
;
124 m_bOwnerDrawn
= FALSE
;
126 m_nMarginWidth
= ms_nLastMarginWidth
;
127 m_nMinHeight
= wxMSWSystemMenuFontModule::ms_systemMenuHeight
;
131 // these items will be set during the first invocation of the c'tor,
132 // because the values will be determined by checking the system settings,
133 // which is a chunk of code
134 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 0;
135 size_t wxOwnerDrawn::ms_nLastMarginWidth
= 0;
141 // get size of the item
142 // The item size includes the menu string, the accel string,
143 // the bitmap and size for a submenu expansion arrow...
144 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth
, size_t *pheight
)
148 wxString str
= wxStripMenuCodes(m_strName
);
150 // if we have a valid accel string, then pad out
151 // the menu string so the menu and accel string are not
152 // placed ontop of eachother.
153 if ( !m_strAccel
.empty() )
155 str
.Pad(str
.Length()%8
);
160 dc
.SetFont(GetFont());
162 dc
.GetTextExtent(str
, (long *)pwidth
, (long *)pheight
);
164 if (!m_strAccel
.IsEmpty())
166 // measure the accelerator string, and add it's width to
167 // the total item width, plus 16 (Accelerators are right justified,
168 // with the right edge of the text rectangle 16 pixels left of
169 // the right edge of the menu)
171 int accel_width
, accel_height
;
172 dc
.GetTextExtent(m_strAccel
, &accel_width
, &accel_height
);
173 *pwidth
+= accel_width
;
176 // add space at the end of the menu for the submenu expansion arrow
177 // this will also allow offsetting the accel string from the right edge
178 *pwidth
+= (size_t) (GetDefaultMarginWidth() * 1.5);
180 // JACS: items still look too tightly packed, so adding 5 pixels.
181 (*pheight
) = (*pheight
) + 5;
183 // Ray Gilbert's changes - Corrects the problem of a BMP
184 // being placed next to text in a menu item, and the BMP does
185 // not match the size expected by the system. This will
186 // resize the space so the BMP will fit. Without this, BMPs
187 // must be no larger or smaller than 16x16.
188 if (m_bmpChecked
.Ok())
190 // Is BMP height larger then text height?
191 size_t adjustedHeight
= m_bmpChecked
.GetHeight() +
192 wxSystemSettings::GetMetric(wxSYS_EDGE_Y
);
193 if (*pheight
< adjustedHeight
)
194 *pheight
= adjustedHeight
;
196 // Does BMP encroach on default check menu position?
197 size_t adjustedWidth
= m_bmpChecked
.GetWidth() +
198 (wxSystemSettings::GetMetric(wxSYS_EDGE_X
) * 2);
199 // if (ms_nDefaultMarginWidth < adjustedWidth)
200 // *pwidth += adjustedWidth - ms_nDefaultMarginWidth;
202 // Do we need to widen margin to fit BMP?
203 if ((size_t)GetMarginWidth() != adjustedWidth
)
204 SetMarginWidth(adjustedWidth
);
206 // add the size of the bitmap to our total size...
207 *pwidth
+= GetMarginWidth();
210 // add the size of the bitmap to our total size - even if we don't have
211 // a bitmap we leave room for one...
212 *pwidth
+= GetMarginWidth();
214 // make sure that this item is at least as
215 // tall as the user's system settings specify
216 if (*pheight
< m_nMinHeight
)
217 *pheight
= m_nMinHeight
;
219 m_nHeight
= *pheight
; // remember height for use in OnDrawItem
224 // searching for this macro you'll find all the code where I'm using the native
225 // Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
226 // port this code to X. (VZ)
228 #if defined(__WIN32__) && !defined(__SYMANTEC__)
229 #define O_DRAW_NATIVE_API // comments below explain why I use it
233 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
,
238 // we do nothing on focus change
239 if ( act
== wxODFocusChanged
)
243 #define ToRGB(col) PALETTERGB(col.Red(), col.Green(), col.Blue())
244 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
247 // this flag determines whether or not an edge will
248 // be drawn around the bitmap. In most "windows classic"
249 // applications, a 1-pixel highlight edge is drawn around
250 // the bitmap of an item when it is selected. However,
251 // with the new "luna" theme, no edge is drawn around
252 // the bitmap because the background is white (this applies
253 // only to "non-XP style" menus w/ bitmaps --
254 // see IE 6 menus for an example)
256 bool draw_bitmap_edge
= true;
260 DWORD colBack
, colText
;
261 if ( st
& wxODSelected
) {
262 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
263 if (!(st
& wxODDisabled
))
265 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
269 colText
= GetSysColor(COLOR_GRAYTEXT
);
273 // fall back to default colors if none explicitly specified
274 colBack
= m_colBack
.Ok() ? ToRGB(m_colBack
) : GetSysColor(COLOR_WINDOW
);
275 colText
= m_colText
.Ok() ? ToRGB(m_colText
) : GetSysColor(COLOR_WINDOWTEXT
);
279 // if background is white, don't draw an edge around the bitmap
280 DWORD menu_bg_color
= GetSysColor(COLOR_MENU
);
281 if (GetRValue(menu_bg_color
) >= 0xf0 &&
282 GetGValue(menu_bg_color
) >= 0xf0 &&
283 GetBValue(menu_bg_color
) >= 0xf0)
285 draw_bitmap_edge
= false;
289 #ifdef O_DRAW_NATIVE_API
290 #define hdc (HDC)dc.GetHDC()
291 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
292 colOldBack
= ::SetBkColor(hdc
, colBack
);
294 dc
.SetTextForeground(wxColor(UnRGB(colText
)));
295 dc
.SetTextBackground(wxColor(UnRGB(colBack
)));
298 // select the font and draw the text
299 // ---------------------------------
302 // determine where to draw and leave space for a check-mark.
303 // Add 3 pixel padding so text appears well within highlight rectangle
304 int x
= rc
.x
+ GetMarginWidth() + 3;
307 // using native API because it reckognizes '&'
308 #ifdef O_DRAW_NATIVE_API
309 int nPrevMode
= SetBkMode(hdc
, TRANSPARENT
);
310 HBRUSH hbr
= CreateSolidBrush(colBack
),
311 hPrevBrush
= (HBRUSH
)SelectObject(hdc
, hbr
);
313 RECT rectFill
= { rc
.GetLeft(), rc
.GetTop(), rc
.GetRight()+1, rc
.GetBottom() };
315 if ( st
& wxODSelected
&& m_bmpChecked
.Ok() && draw_bitmap_edge
) {
316 // only draw the highlight under the text, not under
317 // the bitmap or checkmark; leave a 1-pixel gap.
318 rectFill
.left
= GetMarginWidth() + 1;
321 FillRect(hdc
, &rectFill
, hbr
);
323 // use default font if no font set
326 m_font
.RealizeResource();
327 hfont
= (HFONT
)m_font
.GetResourceHandle();
330 hfont
= (HFONT
)::GetStockObject(SYSTEM_FONT
);
333 HFONT hPrevFont
= (HFONT
) ::SelectObject(hdc
, hfont
);
335 wxString strMenuText
= m_strName
.BeforeFirst('\t');
338 GetTextExtentPoint32(hdc
,strMenuText
.c_str(), strMenuText
.Length(),&sizeRect
);
339 ::DrawState(hdc
, NULL
, NULL
,
340 (LPARAM
)strMenuText
.c_str(), strMenuText
.length(),
341 x
, rc
.y
+( (int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0) )-1, // centre text vertically
342 rc
.GetWidth()-GetMarginWidth(), sizeRect
.cy
,
344 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0));
346 if ( !m_strAccel
.empty() )
348 // right align accel string with right edge of menu ( offset by the margin width )
349 ::SetTextAlign(hdc
, TA_RIGHT
);
350 ::DrawState(hdc
, NULL
, NULL
,
351 (LPARAM
)m_strAccel
.c_str(), m_strAccel
.length(),
352 rc
.GetWidth()-(GetMarginWidth()), rc
.y
+(int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0),
353 rc
.GetWidth()-GetMarginWidth(), sizeRect
.cy
,
355 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0));
356 ::SetTextAlign(hdc
, TA_LEFT
);
359 (void)SelectObject(hdc
, hPrevBrush
);
360 (void)SelectObject(hdc
, hPrevFont
);
361 (void)SetBkMode(hdc
, nPrevMode
);
365 dc
.SetFont(GetFont());
366 dc
.DrawText(wxStripMenuCodes(m_strName
), x
, rc
.y
);
367 #endif //O_DRAW_NATIVE_API
371 if ( IsCheckable() && !m_bmpChecked
.Ok() ) {
372 if ( st
& wxODChecked
) {
373 // using native APIs for performance and simplicity
374 #ifdef O_DRAW_NATIVE_API
375 // what goes on: DrawFrameControl creates a b/w mask,
376 // then we copy it to screen to have right colors
378 // first create a monochrome bitmap in a memory DC
379 HDC hdcMem
= CreateCompatibleDC(hdc
);
380 HBITMAP hbmpCheck
= CreateBitmap(GetMarginWidth(), m_nHeight
, 1, 1, 0);
381 SelectObject(hdcMem
, hbmpCheck
);
383 // then draw a check mark into it
384 RECT rect
= { 0, 0, GetMarginWidth(), m_nHeight
};
387 ::DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
390 // finally copy it to screen DC and clean up
391 BitBlt(hdc
, rc
.x
, rc
.y
, GetMarginWidth(), m_nHeight
,
392 hdcMem
, 0, 0, SRCCOPY
);
395 DeleteObject(hbmpCheck
);
397 // #### to do: perhaps using Marlett font (create equiv. font under X)
398 // wxFAIL("not implemented");
399 #endif //O_DRAW_NATIVE_API
403 // for uncheckable item we use only the 'checked' bitmap
404 wxBitmap
bmp(GetBitmap(IsCheckable() ? ((st
& wxODChecked
) != 0) : TRUE
));
406 wxMemoryDC
dcMem(&dc
);
407 dcMem
.SelectObject(bmp
);
410 int nBmpWidth
= bmp
.GetWidth(),
411 nBmpHeight
= bmp
.GetHeight();
413 // there should be enough place!
414 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
416 int heightDiff
= m_nHeight
- nBmpHeight
;
417 dc
.Blit(rc
.x
+ (GetMarginWidth() - nBmpWidth
) / 2,
418 rc
.y
+ heightDiff
/ 2,
419 nBmpWidth
, nBmpHeight
,
420 &dcMem
, 0, 0, wxCOPY
, TRUE
/* use mask */);
422 if ( st
& wxODSelected
&& draw_bitmap_edge
) {
423 #ifdef O_DRAW_NATIVE_API
424 RECT rectBmp
= { rc
.GetLeft(), rc
.GetTop(),
425 rc
.GetLeft() + GetMarginWidth(),
426 rc
.GetTop() + m_nHeight
-1 };
427 SetBkColor(hdc
, colBack
);
428 DrawEdge(hdc
, &rectBmp
, BDR_RAISEDOUTER
, BF_SOFT
| BF_RECT
);
433 x2
= x1
+ GetMarginWidth() - 1;
434 y2
= y1
+ m_nHeight
- 1;
436 dc
.SetPen(*wxWHITE_PEN
);
437 dc
.DrawLine(x1
, y1
, x2
, y1
);
438 dc
.DrawLine(x1
, y1
, x1
, y2
);
439 dc
.SetPen(*wxGREY_PEN
);
440 dc
.DrawLine(x1
, y2
-1, x2
, y2
-1);
441 dc
.DrawLine(x2
, y1
, x2
, y2
);
442 #endif //O_DRAW_NATIVE_API
447 #ifdef O_DRAW_NATIVE_API
448 ::SetTextColor(hdc
, colOldText
);
449 ::SetBkColor(hdc
, colOldBack
);
452 #endif //O_DRAW_NATIVE_API
458 #endif // wxUSE_OWNER_DRAWN