]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ownerdrw.cpp
4baece74673ccc6d2874b56636c8213197475141
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"
42 // ============================================================================
43 // implementation of wxOwnerDrawn class
44 // ============================================================================
48 wxOwnerDrawn::wxOwnerDrawn(const wxString
& str
,
49 bool bCheckable
, bool 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
;
69 static wxFont menu_font
;
73 wxNativeFontInfo info
;
74 memcpy(&info
.lf
, &nm
.lfMenuFont
, sizeof(LOGFONT
));
75 menu_font
.Create(info
);
82 m_font
= *wxNORMAL_FONT
;
85 // windows clean install default
88 if (ms_nDefaultMarginWidth
== 0)
90 ms_nDefaultMarginWidth
= 18;
91 ms_nLastMarginWidth
= 18;
94 m_font
= *wxNORMAL_FONT
;
97 m_bCheckable
= bCheckable
;
98 m_bOwnerDrawn
= FALSE
;
100 m_nMarginWidth
= ms_nLastMarginWidth
;
104 // these items will be set during the first invocation of the c'tor,
105 // because the values will be determined by checking the system settings,
106 // which is a chunk of code
107 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 0;
108 size_t wxOwnerDrawn::ms_nLastMarginWidth
= 0;
114 // get size of the item
115 // The item size includes the menu string, the accel string,
116 // the bitmap and size for a submenu expansion arrow...
117 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth
, size_t *pheight
)
121 wxString str
= wxStripMenuCodes(m_strName
);
123 // if we have a valid accel string, then pad out
124 // the menu string so the menu and accel string are not
125 // placed ontop of eachother.
126 if ( !m_strAccel
.empty() )
128 str
.Pad(str
.Length()%8
);
133 dc
.SetFont(GetFont());
135 dc
.GetTextExtent(str
, (long *)pwidth
, (long *)pheight
);
137 if (!m_strAccel
.IsEmpty())
139 // measure the accelerator string, and add it's width to
140 // the total item width, plus 16 (Accelerators are right justified,
141 // with the right edge of the text rectangle 16 pixels left of
142 // the right edge of the menu)
144 int accel_width
, accel_height
;
145 dc
.GetTextExtent(m_strAccel
, &accel_width
, &accel_height
);
146 *pwidth
+= accel_width
;
149 // add space at the end of the menu for the submenu expansion arrow
150 // this will also allow offsetting the accel string from the right edge
151 *pwidth
+= (size_t) (GetDefaultMarginWidth() * 1.5);
153 // JACS: items still look too tightly packed, so adding 5 pixels.
154 (*pheight
) = (*pheight
) + 5;
156 // Ray Gilbert's changes - Corrects the problem of a BMP
157 // being placed next to text in a menu item, and the BMP does
158 // not match the size expected by the system. This will
159 // resize the space so the BMP will fit. Without this, BMPs
160 // must be no larger or smaller than 16x16.
161 if (m_bmpChecked
.Ok())
163 // Is BMP height larger then text height?
164 size_t adjustedHeight
= m_bmpChecked
.GetHeight() +
165 wxSystemSettings::GetMetric(wxSYS_EDGE_Y
);
166 if (*pheight
< adjustedHeight
)
167 *pheight
= adjustedHeight
;
169 // Does BMP encroach on default check menu position?
170 size_t adjustedWidth
= m_bmpChecked
.GetWidth() +
171 (wxSystemSettings::GetMetric(wxSYS_EDGE_X
) * 2);
172 // if (ms_nDefaultMarginWidth < adjustedWidth)
173 // *pwidth += adjustedWidth - ms_nDefaultMarginWidth;
175 // Do we need to widen margin to fit BMP?
176 if ((size_t)GetMarginWidth() != adjustedWidth
)
177 SetMarginWidth(adjustedWidth
);
179 // add the size of the bitmap to our total size...
180 *pwidth
+= GetMarginWidth();
183 // add the size of the bitmap to our total size - even if we don't have
184 // a bitmap we leave room for one...
185 *pwidth
+= GetMarginWidth();
187 // make sure that this item is at least as
188 // tall as the user's system settings specify
189 if (*pheight
< m_nMinHeight
)
190 *pheight
= m_nMinHeight
;
192 m_nHeight
= *pheight
; // remember height for use in OnDrawItem
197 // searching for this macro you'll find all the code where I'm using the native
198 // Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
199 // port this code to X. (VZ)
201 // JACS: TODO. Why does a disabled but highlighted item still
202 // get drawn embossed? How can we tell DrawState that we don't want the
205 #if defined(__WIN32__) && !defined(__SYMANTEC__) && !defined(__TWIN32__)
206 #define O_DRAW_NATIVE_API // comments below explain why I use it
210 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
,
215 // we do nothing on focus change
216 if ( act
== wxODFocusChanged
)
220 #define ToRGB(col) PALETTERGB(col.Red(), col.Green(), col.Blue())
221 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
225 DWORD colBack
, colText
;
226 if ( st
& wxODSelected
) {
227 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
228 if (!(st
& wxODDisabled
))
230 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
234 colText
= GetSysColor(COLOR_GRAYTEXT
);
238 // fall back to default colors if none explicitly specified
239 colBack
= m_colBack
.Ok() ? ToRGB(m_colBack
) : GetSysColor(COLOR_WINDOW
);
240 colText
= m_colText
.Ok() ? ToRGB(m_colText
) : GetSysColor(COLOR_WINDOWTEXT
);
243 #ifdef O_DRAW_NATIVE_API
244 #define hdc (HDC)dc.GetHDC()
245 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
246 colOldBack
= ::SetBkColor(hdc
, colBack
);
248 dc
.SetTextForeground(wxColor(UnRGB(colText
)));
249 dc
.SetTextBackground(wxColor(UnRGB(colBack
)));
252 // select the font and draw the text
253 // ---------------------------------
256 // determine where to draw and leave space for a check-mark.
257 // Add 3 pixel padding so text appears well within highlight rectangle
258 int x
= rc
.x
+ GetMarginWidth() + 3;
261 // using native API because it reckognizes '&'
262 #ifdef O_DRAW_NATIVE_API
263 int nPrevMode
= SetBkMode(hdc
, TRANSPARENT
);
264 HBRUSH hbr
= CreateSolidBrush(colBack
),
265 hPrevBrush
= (HBRUSH
)SelectObject(hdc
, hbr
);
267 RECT rectFill
= { rc
.GetLeft(), rc
.GetTop(), rc
.GetRight()+1, rc
.GetBottom() };
269 if ( st
& wxODSelected
&& m_bmpChecked
.Ok()) {
270 // only draw the highlight under the text, not under
271 // the bitmap or checkmark; leave a 1-pixel gap.
272 rectFill
.left
= GetMarginWidth() + 1;
275 FillRect(hdc
, &rectFill
, hbr
);
277 // use default font if no font set
280 m_font
.RealizeResource();
281 hfont
= (HFONT
)m_font
.GetResourceHandle();
284 hfont
= (HFONT
)::GetStockObject(SYSTEM_FONT
);
287 HFONT hPrevFont
= (HFONT
) ::SelectObject(hdc
, hfont
);
289 wxString strMenuText
= m_strName
.BeforeFirst('\t');
292 GetTextExtentPoint32(hdc
,strMenuText
.c_str(), strMenuText
.Length(),&sizeRect
);
293 ::DrawState(hdc
, NULL
, NULL
,
294 (LPARAM
)strMenuText
.c_str(), strMenuText
.length(),
295 x
, rc
.y
+( (int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0) )-1, // centre text vertically
296 rc
.GetWidth()-GetMarginWidth(), sizeRect
.cy
,
298 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0));
300 if ( !m_strAccel
.empty() )
302 // right align accel string with right edge of menu ( offset by the margin width )
303 ::SetTextAlign(hdc
, TA_RIGHT
);
304 ::DrawState(hdc
, NULL
, NULL
,
305 (LPARAM
)m_strAccel
.c_str(), m_strAccel
.length(),
306 rc
.GetWidth()-(GetMarginWidth()), rc
.y
+(int) ((rc
.GetHeight()-sizeRect
.cy
)/2.0),
307 rc
.GetWidth()-GetMarginWidth(), sizeRect
.cy
,
309 (((st
& wxODDisabled
) && !(st
& wxODSelected
)) ? DSS_DISABLED
: 0));
310 ::SetTextAlign(hdc
, TA_LEFT
);
313 (void)SelectObject(hdc
, hPrevBrush
);
314 (void)SelectObject(hdc
, hPrevFont
);
315 (void)SetBkMode(hdc
, nPrevMode
);
319 dc
.SetFont(GetFont());
320 dc
.DrawText(wxStripMenuCodes(m_strName
), x
, rc
.y
);
321 #endif //O_DRAW_NATIVE_API
325 if ( IsCheckable() && !m_bmpChecked
.Ok() ) {
326 if ( st
& wxODChecked
) {
327 // using native APIs for performance and simplicity
328 #ifdef O_DRAW_NATIVE_API
329 // what goes on: DrawFrameControl creates a b/w mask,
330 // then we copy it to screen to have right colors
332 // first create a monochrome bitmap in a memory DC
333 HDC hdcMem
= CreateCompatibleDC(hdc
);
334 HBITMAP hbmpCheck
= CreateBitmap(GetMarginWidth(), m_nHeight
, 1, 1, 0);
335 SelectObject(hdcMem
, hbmpCheck
);
337 // then draw a check mark into it
338 RECT rect
= { 0, 0, GetMarginWidth(), m_nHeight
};
341 ::DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
344 // finally copy it to screen DC and clean up
345 BitBlt(hdc
, rc
.x
, rc
.y
, GetMarginWidth(), m_nHeight
,
346 hdcMem
, 0, 0, SRCCOPY
);
349 DeleteObject(hbmpCheck
);
351 // #### to do: perhaps using Marlett font (create equiv. font under X)
352 // wxFAIL("not implemented");
353 #endif //O_DRAW_NATIVE_API
357 // for uncheckable item we use only the 'checked' bitmap
358 wxBitmap
bmp(GetBitmap(IsCheckable() ? ((st
& wxODChecked
) != 0) : TRUE
));
360 wxMemoryDC
dcMem(&dc
);
361 dcMem
.SelectObject(bmp
);
364 int nBmpWidth
= bmp
.GetWidth(),
365 nBmpHeight
= bmp
.GetHeight();
367 // there should be enough place!
368 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
370 int heightDiff
= m_nHeight
- nBmpHeight
;
371 dc
.Blit(rc
.x
+ (GetMarginWidth() - nBmpWidth
) / 2,
372 rc
.y
+ heightDiff
/ 2,
373 nBmpWidth
, nBmpHeight
,
374 &dcMem
, 0, 0, wxCOPY
, TRUE
/* use mask */);
376 if ( st
& wxODSelected
) {
377 #ifdef O_DRAW_NATIVE_API
378 RECT rectBmp
= { rc
.GetLeft(), rc
.GetTop(),
379 rc
.GetLeft() + GetMarginWidth(),
380 rc
.GetTop() + m_nHeight
-1 };
381 SetBkColor(hdc
, colBack
);
382 DrawEdge(hdc
, &rectBmp
, EDGE_RAISED
, BF_SOFT
| BF_RECT
);
387 x2
= x1
+ GetMarginWidth() - 1;
388 y2
= y1
+ m_nHeight
- 1;
390 dc
.SetPen(*wxWHITE_PEN
);
391 dc
.DrawLine(x1
, y1
, x2
, y1
);
392 dc
.DrawLine(x1
, y1
, x1
, y2
);
393 dc
.SetPen(*wxGREY_PEN
);
394 dc
.DrawLine(x1
, y2
-1, x2
, y2
-1);
395 dc
.DrawLine(x2
, y1
, x2
, y2
);
396 #endif //O_DRAW_NATIVE_API
401 #ifdef O_DRAW_NATIVE_API
402 ::SetTextColor(hdc
, colOldText
);
403 ::SetBkColor(hdc
, colOldBack
);
406 #endif //O_DRAW_NATIVE_API
412 #endif // wxUSE_OWNER_DRAWN