]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ownerdrw.cpp
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 license
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"
42 // ============================================================================
43 // implementation of wxOwnerDrawn class
44 // ============================================================================
48 wxOwnerDrawn::wxOwnerDrawn(const wxString
& str
,
49 bool bCheckable
, bool WXUNUSED(bMenuItem
))
52 #if defined(__WXMSW__) && defined(__WIN32__) && defined(SM_CXMENUCHECK)
53 // get the default menu height and font from the system
55 nm
.cbSize
= sizeof (NONCLIENTMETRICS
);
56 SystemParametersInfo (SPI_GETNONCLIENTMETRICS
,0,&nm
,0);
57 m_nMinHeight
= nm
.iMenuHeight
;
59 // nm.iMenuWidth is the system default for the width of
60 // menu icons and checkmarks
61 if (ms_nDefaultMarginWidth
== 0)
63 ms_nDefaultMarginWidth
= nm
.iMenuWidth
;
64 ms_nLastMarginWidth
= nm
.iMenuWidth
;
67 wxNativeFontInfo info
;
68 memcpy(&info
.lf
, &nm
.lfMenuFont
, sizeof(LOGFONT
));
71 // windows clean install default
74 if (ms_nDefaultMarginWidth
== 0)
76 ms_nDefaultMarginWidth
= 18;
77 ms_nLastMarginWidth
= 18;
80 m_font
= *wxNORMAL_FONT
;
83 m_bCheckable
= bCheckable
;
84 m_bOwnerDrawn
= FALSE
;
86 m_nMarginWidth
= ms_nLastMarginWidth
;
90 // these items will be set during the first invocation of the c'tor,
91 // because the values will be determined by checking the system settings,
92 // which is a chunk of code
93 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 0;
94 size_t wxOwnerDrawn::ms_nLastMarginWidth
= 0;
100 // get size of the item
101 // The item size includes the menu string, the accel string,
102 // the bitmap and size for a submenu expansion arrow...
103 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth
, size_t *pheight
)
107 wxString str
= wxStripMenuCodes(m_strName
);
109 // if we have a valid accel string, then pad out
110 // the menu string so the menu and accel string are not
111 // placed ontop of eachother.
112 if ( !m_strAccel
.empty() )
114 str
.Pad(str
.Length()%8
);
119 dc
.SetFont(GetFont());
121 dc
.GetTextExtent(str
, (long *)pwidth
, (long *)pheight
);
123 if (!m_strAccel
.IsEmpty())
125 // measure the accelerator string, and add it's width to
126 // the total item width, plus 16 (Accelerators are right justified,
127 // with the right edge of the text rectangle 16 pixels left of
128 // the right edge of the menu)
130 int accel_width
, accel_height
;
131 dc
.GetTextExtent(m_strAccel
, &accel_width
, &accel_height
);
132 *pwidth
+= accel_width
;
135 // add space at the end of the menu for the submenu expansion arrow
136 // this will also allow offsetting the accel string from the right edge
137 *pwidth
+= (size_t) (GetDefaultMarginWidth() * 1.5);
139 // JACS: items still look too tightly packed, so adding 5 pixels.
140 (*pheight
) = (*pheight
) + 5;
142 // Ray Gilbert's changes - Corrects the problem of a BMP
143 // being placed next to text in a menu item, and the BMP does
144 // not match the size expected by the system. This will
145 // resize the space so the BMP will fit. Without this, BMPs
146 // must be no larger or smaller than 16x16.
147 if (m_bmpChecked
.Ok())
149 // Is BMP height larger then text height?
150 size_t adjustedHeight
= m_bmpChecked
.GetHeight() +
151 wxSystemSettings::GetMetric(wxSYS_EDGE_Y
);
152 if (*pheight
< adjustedHeight
)
153 *pheight
= adjustedHeight
;
155 // Does BMP encroach on default check menu position?
156 size_t adjustedWidth
= m_bmpChecked
.GetWidth() +
157 (wxSystemSettings::GetMetric(wxSYS_EDGE_X
) * 2);
158 // if (ms_nDefaultMarginWidth < adjustedWidth)
159 // *pwidth += adjustedWidth - ms_nDefaultMarginWidth;
161 // Do we need to widen margin to fit BMP?
162 if ((size_t)GetMarginWidth() != adjustedWidth
)
163 SetMarginWidth(adjustedWidth
);
165 // add the size of the bitmap to our total size...
166 *pwidth
+= GetMarginWidth();
169 // add the size of the bitmap to our total size - even if we don't have
170 // a bitmap we leave room for one...
171 *pwidth
+= GetMarginWidth();
173 // make sure that this item is at least as
174 // tall as the user's system settings specify
175 if (*pheight
< m_nMinHeight
)
176 *pheight
= m_nMinHeight
;
178 m_nHeight
= *pheight
; // remember height for use in OnDrawItem
183 // searching for this macro you'll find all the code where I'm using the native
184 // Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
185 // port this code to X. (VZ)
187 // JACS: TODO. Why does a disabled but highlighted item still
188 // get drawn embossed? How can we tell DrawState that we don't want the
191 #if defined(__WIN32__) && !defined(__SC__) && !defined(__TWIN32__)
192 #define O_DRAW_NATIVE_API // comments below explain why I use it
196 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
,
201 // we do nothing on focus change
202 if ( act
== wxODFocusChanged
)
206 #define ToRGB(col) PALETTERGB(col.Red(), col.Green(), col.Blue())
207 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
211 DWORD colBack
, colText
;
212 if ( st
& wxODSelected
) {
213 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
214 if (!(st
& wxODDisabled
))
216 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
220 colText
= GetSysColor(COLOR_GRAYTEXT
);
224 // fall back to default colors if none explicitly specified
225 colBack
= m_colBack
.Ok() ? ToRGB(m_colBack
) : GetSysColor(COLOR_WINDOW
);
226 colText
= m_colText
.Ok() ? ToRGB(m_colText
) : GetSysColor(COLOR_WINDOWTEXT
);
229 #ifdef O_DRAW_NATIVE_API
230 #define hdc (HDC)dc.GetHDC()
231 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
232 colOldBack
= ::SetBkColor(hdc
, colBack
);
234 dc
.SetTextForeground(wxColor(UnRGB(colText
)));
235 dc
.SetTextBackground(wxColor(UnRGB(colBack
)));
238 // select the font and draw the text
239 // ---------------------------------
242 // determine where to draw and leave space for a check-mark.
243 // Add 3 pixel padding so text appears well within highlight rectangle
244 int x
= rc
.x
+ GetMarginWidth() + 3;
247 // using native API because it reckognizes '&'
248 #ifdef O_DRAW_NATIVE_API
249 int nPrevMode
= SetBkMode(hdc
, TRANSPARENT
);
250 HBRUSH hbr
= CreateSolidBrush(colBack
),
251 hPrevBrush
= (HBRUSH
)SelectObject(hdc
, hbr
);
253 RECT rectFill
= { rc
.GetLeft(), rc
.GetTop(), rc
.GetRight()+1, rc
.GetBottom() };
255 if ( st
& wxODSelected
&& m_bmpChecked
.Ok()) {
256 // only draw the highlight under the text, not under
257 // the bitmap or checkmark; leave a 1-pixel gap.
258 rectFill
.left
= GetMarginWidth() + 1;
261 FillRect(hdc
, &rectFill
, hbr
);
263 // use default font if no font set
266 m_font
.RealizeResource();
267 hfont
= (HFONT
)m_font
.GetResourceHandle();
270 hfont
= (HFONT
)::GetStockObject(SYSTEM_FONT
);
273 HFONT hPrevFont
= (HFONT
) ::SelectObject(hdc
, hfont
);
275 wxString strMenuText
= m_strName
.BeforeFirst('\t');
278 GetTextExtentPoint32(hdc
,strMenuText
.c_str(), strMenuText
.Length(),&sizeRect
);
279 ::DrawState(hdc
, NULL
, NULL
,
280 (LPARAM
)strMenuText
.c_str(), strMenuText
.length(),
281 x
, rc
.y
+( (int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0) )-1, // centre text vertically
282 rc
.GetWidth()-GetMarginWidth(), sizeRect
.cy
,
284 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0));
286 if ( !m_strAccel
.empty() )
288 // right align accel string with right edge of menu ( offset by the margin width )
289 ::SetTextAlign(hdc
, TA_RIGHT
);
290 ::DrawState(hdc
, NULL
, NULL
,
291 (LPARAM
)m_strAccel
.c_str(), m_strAccel
.length(),
292 rc
.GetWidth()-(GetMarginWidth()), rc
.y
+(int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0),
293 rc
.GetWidth()-GetMarginWidth(), sizeRect
.cy
,
295 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0));
296 ::SetTextAlign(hdc
, TA_LEFT
);
299 (void)SelectObject(hdc
, hPrevBrush
);
300 (void)SelectObject(hdc
, hPrevFont
);
301 (void)SetBkMode(hdc
, nPrevMode
);
305 dc
.SetFont(GetFont());
306 dc
.DrawText(wxStripMenuCodes(m_strName
), x
, rc
.y
);
307 #endif //O_DRAW_NATIVE_API
311 if ( IsCheckable() && !m_bmpChecked
.Ok() ) {
312 if ( st
& wxODChecked
) {
313 // using native APIs for performance and simplicity
314 #ifdef O_DRAW_NATIVE_API
315 // what goes on: DrawFrameControl creates a b/w mask,
316 // then we copy it to screen to have right colors
318 // first create a monochrome bitmap in a memory DC
319 HDC hdcMem
= CreateCompatibleDC(hdc
);
320 HBITMAP hbmpCheck
= CreateBitmap(GetMarginWidth(), m_nHeight
, 1, 1, 0);
321 SelectObject(hdcMem
, hbmpCheck
);
323 // then draw a check mark into it
324 RECT rect
= { 0, 0, GetMarginWidth(), m_nHeight
};
327 ::DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
330 // finally copy it to screen DC and clean up
331 BitBlt(hdc
, rc
.x
, rc
.y
, GetMarginWidth(), m_nHeight
,
332 hdcMem
, 0, 0, SRCCOPY
);
335 DeleteObject(hbmpCheck
);
337 // #### to do: perhaps using Marlett font (create equiv. font under X)
338 // wxFAIL("not implemented");
339 #endif //O_DRAW_NATIVE_API
343 // for uncheckable item we use only the 'checked' bitmap
344 wxBitmap
bmp(GetBitmap(IsCheckable() ? ((st
& wxODChecked
) != 0) : TRUE
));
346 wxMemoryDC
dcMem(&dc
);
347 dcMem
.SelectObject(bmp
);
350 int nBmpWidth
= bmp
.GetWidth(),
351 nBmpHeight
= bmp
.GetHeight();
353 // there should be enough place!
354 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
356 int heightDiff
= m_nHeight
- nBmpHeight
;
357 dc
.Blit(rc
.x
+ (GetMarginWidth() - nBmpWidth
) / 2,
358 rc
.y
+ heightDiff
/ 2,
359 nBmpWidth
, nBmpHeight
,
360 &dcMem
, 0, 0, wxCOPY
, TRUE
/* use mask */);
362 if ( st
& wxODSelected
) {
363 #ifdef O_DRAW_NATIVE_API
364 RECT rectBmp
= { rc
.GetLeft(), rc
.GetTop(),
365 rc
.GetLeft() + GetMarginWidth(),
366 rc
.GetTop() + m_nHeight
-1 };
367 SetBkColor(hdc
, colBack
);
368 DrawEdge(hdc
, &rectBmp
, EDGE_RAISED
, BF_SOFT
| BF_RECT
);
373 x2
= x1
+ GetMarginWidth() - 1;
374 y2
= y1
+ m_nHeight
- 1;
376 dc
.SetPen(*wxWHITE_PEN
);
377 dc
.DrawLine(x1
, y1
, x2
, y1
);
378 dc
.DrawLine(x1
, y1
, x1
, y2
);
379 dc
.SetPen(*wxGREY_PEN
);
380 dc
.DrawLine(x1
, y2
-1, x2
, y2
-1);
381 dc
.DrawLine(x2
, y1
, x2
, y2
);
382 #endif //O_DRAW_NATIVE_API
387 #ifdef O_DRAW_NATIVE_API
388 ::SetTextColor(hdc
, colOldText
);
389 ::SetBkColor(hdc
, colOldBack
);
392 #endif //O_DRAW_NATIVE_API
398 #endif // wxUSE_OWNER_DRAWN