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"
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
);
68 delete ms_systemMenuFont
;
69 ms_systemMenuFont
= NULL
;
72 static wxFont
* ms_systemMenuFont
;
73 static int ms_systemMenuButtonWidth
; // windows clean install default
74 static int ms_systemMenuHeight
; // windows clean install default
76 DECLARE_DYNAMIC_CLASS(wxMSWSystemMenuFontModule
)
79 // these static variables are by the wxMSWSystemMenuFontModule object
80 // and reflect the system settings returned by the Win32 API's
81 // SystemParametersInfo() call.
83 wxFont
* wxMSWSystemMenuFontModule::ms_systemMenuFont
= NULL
;
84 int wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth
= 18; // windows clean install default
85 int wxMSWSystemMenuFontModule::ms_systemMenuHeight
= 18; // windows clean install default
87 IMPLEMENT_DYNAMIC_CLASS(wxMSWSystemMenuFontModule
, wxModule
)
89 // ============================================================================
90 // implementation of wxOwnerDrawn class
91 // ============================================================================
95 wxOwnerDrawn::wxOwnerDrawn(const wxString
& str
,
96 bool bCheckable
, bool bMenuItem
)
99 // get the default menu height and font from the system
101 nm
.cbSize
= sizeof (NONCLIENTMETRICS
);
102 SystemParametersInfo (SPI_GETNONCLIENTMETRICS
,0,&nm
,0);
103 m_nMinHeight
= nm
.iMenuHeight
;
105 // nm.iMenuWidth is the system default for the width of
106 // menu icons and checkmarks
107 if (ms_nDefaultMarginWidth
== 0)
109 ms_nDefaultMarginWidth
= wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth
;
110 ms_nLastMarginWidth
= wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth
;
113 if (wxMSWSystemMenuFontModule::ms_systemMenuFont
->Ok() && bMenuItem
)
115 m_font
= *wxMSWSystemMenuFontModule::ms_systemMenuFont
;
119 m_font
= *wxNORMAL_FONT
;
122 m_bCheckable
= bCheckable
;
123 m_bOwnerDrawn
= FALSE
;
125 m_nMarginWidth
= ms_nLastMarginWidth
;
126 m_nMinHeight
= wxMSWSystemMenuFontModule::ms_systemMenuHeight
;
130 // these items will be set during the first invocation of the c'tor,
131 // because the values will be determined by checking the system settings,
132 // which is a chunk of code
133 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 0;
134 size_t wxOwnerDrawn::ms_nLastMarginWidth
= 0;
140 // get size of the item
141 // The item size includes the menu string, the accel string,
142 // the bitmap and size for a submenu expansion arrow...
143 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth
, size_t *pheight
)
147 wxString str
= wxStripMenuCodes(m_strName
);
149 // if we have a valid accel string, then pad out
150 // the menu string so the menu and accel string are not
151 // placed ontop of eachother.
152 if ( !m_strAccel
.empty() )
154 str
.Pad(str
.Length()%8
);
159 dc
.SetFont(GetFont());
161 dc
.GetTextExtent(str
, (long *)pwidth
, (long *)pheight
);
163 if (!m_strAccel
.IsEmpty())
165 // measure the accelerator string, and add it's width to
166 // the total item width, plus 16 (Accelerators are right justified,
167 // with the right edge of the text rectangle 16 pixels left of
168 // the right edge of the menu)
170 int accel_width
, accel_height
;
171 dc
.GetTextExtent(m_strAccel
, &accel_width
, &accel_height
);
172 *pwidth
+= accel_width
;
175 // add space at the end of the menu for the submenu expansion arrow
176 // this will also allow offsetting the accel string from the right edge
177 *pwidth
+= (size_t) (GetDefaultMarginWidth() * 1.5);
179 // JACS: items still look too tightly packed, so adding 5 pixels.
180 (*pheight
) = (*pheight
) + 5;
182 // Ray Gilbert's changes - Corrects the problem of a BMP
183 // being placed next to text in a menu item, and the BMP does
184 // not match the size expected by the system. This will
185 // resize the space so the BMP will fit. Without this, BMPs
186 // must be no larger or smaller than 16x16.
187 if (m_bmpChecked
.Ok())
189 // Is BMP height larger then text height?
190 size_t adjustedHeight
= m_bmpChecked
.GetHeight() +
191 wxSystemSettings::GetMetric(wxSYS_EDGE_Y
);
192 if (*pheight
< adjustedHeight
)
193 *pheight
= adjustedHeight
;
195 // Does BMP encroach on default check menu position?
196 size_t adjustedWidth
= m_bmpChecked
.GetWidth() +
197 (wxSystemSettings::GetMetric(wxSYS_EDGE_X
) * 2);
198 // if (ms_nDefaultMarginWidth < adjustedWidth)
199 // *pwidth += adjustedWidth - ms_nDefaultMarginWidth;
201 // Do we need to widen margin to fit BMP?
202 if ((size_t)GetMarginWidth() != adjustedWidth
)
203 SetMarginWidth(adjustedWidth
);
205 // add the size of the bitmap to our total size...
206 *pwidth
+= GetMarginWidth();
209 // add the size of the bitmap to our total size - even if we don't have
210 // a bitmap we leave room for one...
211 *pwidth
+= GetMarginWidth();
213 // make sure that this item is at least as
214 // tall as the user's system settings specify
215 if (*pheight
< m_nMinHeight
)
216 *pheight
= m_nMinHeight
;
218 m_nHeight
= *pheight
; // remember height for use in OnDrawItem
223 // searching for this macro you'll find all the code where I'm using the native
224 // Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
225 // port this code to X. (VZ)
227 // JACS: TODO. Why does a disabled but highlighted item still
228 // get drawn embossed? How can we tell DrawState that we don't want the
231 #if defined(__WIN32__) && !defined(__SYMANTEC__) && !defined(__TWIN32__)
232 #define O_DRAW_NATIVE_API // comments below explain why I use it
236 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
,
241 // we do nothing on focus change
242 if ( act
== wxODFocusChanged
)
246 #define ToRGB(col) PALETTERGB(col.Red(), col.Green(), col.Blue())
247 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
251 DWORD colBack
, colText
;
252 if ( st
& wxODSelected
) {
253 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
254 if (!(st
& wxODDisabled
))
256 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
260 colText
= GetSysColor(COLOR_GRAYTEXT
);
264 // fall back to default colors if none explicitly specified
265 colBack
= m_colBack
.Ok() ? ToRGB(m_colBack
) : GetSysColor(COLOR_WINDOW
);
266 colText
= m_colText
.Ok() ? ToRGB(m_colText
) : GetSysColor(COLOR_WINDOWTEXT
);
269 #ifdef O_DRAW_NATIVE_API
270 #define hdc (HDC)dc.GetHDC()
271 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
272 colOldBack
= ::SetBkColor(hdc
, colBack
);
274 dc
.SetTextForeground(wxColor(UnRGB(colText
)));
275 dc
.SetTextBackground(wxColor(UnRGB(colBack
)));
278 // select the font and draw the text
279 // ---------------------------------
282 // determine where to draw and leave space for a check-mark.
283 // Add 3 pixel padding so text appears well within highlight rectangle
284 int x
= rc
.x
+ GetMarginWidth() + 3;
287 // using native API because it reckognizes '&'
288 #ifdef O_DRAW_NATIVE_API
289 int nPrevMode
= SetBkMode(hdc
, TRANSPARENT
);
290 HBRUSH hbr
= CreateSolidBrush(colBack
),
291 hPrevBrush
= (HBRUSH
)SelectObject(hdc
, hbr
);
293 RECT rectFill
= { rc
.GetLeft(), rc
.GetTop(), rc
.GetRight()+1, rc
.GetBottom() };
295 if ( st
& wxODSelected
&& m_bmpChecked
.Ok()) {
296 // only draw the highlight under the text, not under
297 // the bitmap or checkmark; leave a 1-pixel gap.
298 rectFill
.left
= GetMarginWidth() + 1;
301 FillRect(hdc
, &rectFill
, hbr
);
303 // use default font if no font set
306 m_font
.RealizeResource();
307 hfont
= (HFONT
)m_font
.GetResourceHandle();
310 hfont
= (HFONT
)::GetStockObject(SYSTEM_FONT
);
313 HFONT hPrevFont
= (HFONT
) ::SelectObject(hdc
, hfont
);
315 wxString strMenuText
= m_strName
.BeforeFirst('\t');
318 GetTextExtentPoint32(hdc
,strMenuText
.c_str(), strMenuText
.Length(),&sizeRect
);
319 ::DrawState(hdc
, NULL
, NULL
,
320 (LPARAM
)strMenuText
.c_str(), strMenuText
.length(),
321 x
, rc
.y
+( (int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0) )-1, // centre text vertically
322 rc
.GetWidth()-GetMarginWidth(), sizeRect
.cy
,
324 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0));
326 if ( !m_strAccel
.empty() )
328 // right align accel string with right edge of menu ( offset by the margin width )
329 ::SetTextAlign(hdc
, TA_RIGHT
);
330 ::DrawState(hdc
, NULL
, NULL
,
331 (LPARAM
)m_strAccel
.c_str(), m_strAccel
.length(),
332 rc
.GetWidth()-(GetMarginWidth()), rc
.y
+(int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0),
333 rc
.GetWidth()-GetMarginWidth(), sizeRect
.cy
,
335 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0));
336 ::SetTextAlign(hdc
, TA_LEFT
);
339 (void)SelectObject(hdc
, hPrevBrush
);
340 (void)SelectObject(hdc
, hPrevFont
);
341 (void)SetBkMode(hdc
, nPrevMode
);
345 dc
.SetFont(GetFont());
346 dc
.DrawText(wxStripMenuCodes(m_strName
), x
, rc
.y
);
347 #endif //O_DRAW_NATIVE_API
351 if ( IsCheckable() && !m_bmpChecked
.Ok() ) {
352 if ( st
& wxODChecked
) {
353 // using native APIs for performance and simplicity
354 #ifdef O_DRAW_NATIVE_API
355 // what goes on: DrawFrameControl creates a b/w mask,
356 // then we copy it to screen to have right colors
358 // first create a monochrome bitmap in a memory DC
359 HDC hdcMem
= CreateCompatibleDC(hdc
);
360 HBITMAP hbmpCheck
= CreateBitmap(GetMarginWidth(), m_nHeight
, 1, 1, 0);
361 SelectObject(hdcMem
, hbmpCheck
);
363 // then draw a check mark into it
364 RECT rect
= { 0, 0, GetMarginWidth(), m_nHeight
};
367 ::DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
370 // finally copy it to screen DC and clean up
371 BitBlt(hdc
, rc
.x
, rc
.y
, GetMarginWidth(), m_nHeight
,
372 hdcMem
, 0, 0, SRCCOPY
);
375 DeleteObject(hbmpCheck
);
377 // #### to do: perhaps using Marlett font (create equiv. font under X)
378 // wxFAIL("not implemented");
379 #endif //O_DRAW_NATIVE_API
383 // for uncheckable item we use only the 'checked' bitmap
384 wxBitmap
bmp(GetBitmap(IsCheckable() ? ((st
& wxODChecked
) != 0) : TRUE
));
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
+ (GetMarginWidth() - nBmpWidth
) / 2,
398 rc
.y
+ heightDiff
/ 2,
399 nBmpWidth
, nBmpHeight
,
400 &dcMem
, 0, 0, wxCOPY
, TRUE
/* use mask */);
402 if ( st
& wxODSelected
) {
403 #ifdef O_DRAW_NATIVE_API
404 RECT rectBmp
= { rc
.GetLeft(), rc
.GetTop(),
405 rc
.GetLeft() + GetMarginWidth(),
406 rc
.GetTop() + m_nHeight
-1 };
407 SetBkColor(hdc
, colBack
);
408 DrawEdge(hdc
, &rectBmp
, EDGE_RAISED
, BF_SOFT
| BF_RECT
);
413 x2
= x1
+ GetMarginWidth() - 1;
414 y2
= y1
+ m_nHeight
- 1;
416 dc
.SetPen(*wxWHITE_PEN
);
417 dc
.DrawLine(x1
, y1
, x2
, y1
);
418 dc
.DrawLine(x1
, y1
, x1
, y2
);
419 dc
.SetPen(*wxGREY_PEN
);
420 dc
.DrawLine(x1
, y2
-1, x2
, y2
-1);
421 dc
.DrawLine(x2
, y1
, x2
, y2
);
422 #endif //O_DRAW_NATIVE_API
427 #ifdef O_DRAW_NATIVE_API
428 ::SetTextColor(hdc
, colOldText
);
429 ::SetBkColor(hdc
, colOldBack
);
432 #endif //O_DRAW_NATIVE_API
438 #endif // wxUSE_OWNER_DRAWN