]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: msw/ownerdrw.cpp | |
3 | // Purpose: implementation of wxOwnerDrawn class | |
4 | // Author: Vadim Zeitlin | |
33ac7e6f | 5 | // Modified by: |
2bda0e17 KB |
6 | // Created: 13.11.97 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
14f355c2 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
a3b46648 UU |
13 | #pragma implementation |
14 | #endif | |
2bda0e17 KB |
15 | |
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
ff0ea71c | 18 | #include "wx/msw/private.h" |
2bda0e17 KB |
19 | |
20 | #ifdef __BORLANDC__ | |
a3b46648 | 21 | #pragma hdrstop |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | #ifndef WX_PRECOMP | |
0c589ad0 | 25 | #include "wx/window.h" |
136cb3c7 | 26 | #include "wx/msw/private.h" |
2432b92d JS |
27 | #include "wx/font.h" |
28 | #include "wx/bitmap.h" | |
29 | #include "wx/dcmemory.h" | |
2bda0e17 | 30 | #include "wx/menu.h" |
2432b92d | 31 | #include "wx/utils.h" |
2bda0e17 KB |
32 | #endif |
33 | ||
5100cabf | 34 | #include "wx/settings.h" |
2bda0e17 KB |
35 | #include "wx/ownerdrw.h" |
36 | #include "wx/menuitem.h" | |
d9cf726b | 37 | #include "wx/fontutil.h" |
e87fb0ee | 38 | #include "wx/module.h" |
2bda0e17 | 39 | |
1b24a68e GRG |
40 | #if wxUSE_OWNER_DRAWN |
41 | ||
cfbf444a JS |
42 | class wxMSWSystemMenuFontModule : public wxModule |
43 | { | |
44 | public: | |
45 | ||
46 | virtual bool OnInit() | |
47 | { | |
48 | ms_systemMenuFont = new wxFont; | |
49 | ||
50 | #if defined(__WXMSW__) && defined(__WIN32__) && defined(SM_CXMENUCHECK) | |
51 | NONCLIENTMETRICS nm; | |
52 | nm.cbSize = sizeof(NONCLIENTMETRICS); | |
10403254 | 53 | SystemParametersInfo(SPI_GETNONCLIENTMETRICS,0,&nm,0); |
cfbf444a JS |
54 | |
55 | ms_systemMenuButtonWidth = nm.iMenuHeight; | |
56 | ms_systemMenuHeight = nm.iMenuHeight; | |
57 | ||
58 | // create menu font | |
59 | wxNativeFontInfo info; | |
60 | memcpy(&info.lf, &nm.lfMenuFont, sizeof(LOGFONT)); | |
61 | ms_systemMenuFont->Create(info); | |
62 | #endif | |
63 | ||
64 | return true; | |
65 | } | |
66 | ||
67 | virtual void OnExit() | |
68 | { | |
69 | delete ms_systemMenuFont; | |
70 | ms_systemMenuFont = NULL; | |
71 | } | |
72 | ||
73 | static wxFont* ms_systemMenuFont; | |
74 | static int ms_systemMenuButtonWidth; // windows clean install default | |
75 | static int ms_systemMenuHeight; // windows clean install default | |
76 | private: | |
77 | DECLARE_DYNAMIC_CLASS(wxMSWSystemMenuFontModule) | |
78 | }; | |
79 | ||
80 | // these static variables are by the wxMSWSystemMenuFontModule object | |
81 | // and reflect the system settings returned by the Win32 API's | |
82 | // SystemParametersInfo() call. | |
83 | ||
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 | |
87 | ||
88 | IMPLEMENT_DYNAMIC_CLASS(wxMSWSystemMenuFontModule, wxModule) | |
2432b92d | 89 | |
2bda0e17 KB |
90 | // ============================================================================ |
91 | // implementation of wxOwnerDrawn class | |
92 | // ============================================================================ | |
93 | ||
94 | // ctor | |
95 | // ---- | |
33ac7e6f | 96 | wxOwnerDrawn::wxOwnerDrawn(const wxString& str, |
19b42379 | 97 | bool bCheckable, bool bMenuItem) |
2bda0e17 KB |
98 | : m_strName(str) |
99 | { | |
d9cf726b JS |
100 | // get the default menu height and font from the system |
101 | NONCLIENTMETRICS nm; | |
102 | nm.cbSize = sizeof (NONCLIENTMETRICS); | |
10403254 | 103 | SystemParametersInfo (SPI_GETNONCLIENTMETRICS,0,&nm,0); |
d9cf726b | 104 | m_nMinHeight = nm.iMenuHeight; |
10403254 | 105 | |
d9cf726b JS |
106 | // nm.iMenuWidth is the system default for the width of |
107 | // menu icons and checkmarks | |
108 | if (ms_nDefaultMarginWidth == 0) | |
109 | { | |
cfbf444a JS |
110 | ms_nDefaultMarginWidth = wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth; |
111 | ms_nLastMarginWidth = wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth; | |
d9cf726b JS |
112 | } |
113 | ||
cfbf444a | 114 | if (wxMSWSystemMenuFontModule::ms_systemMenuFont->Ok() && bMenuItem) |
19b42379 | 115 | { |
cfbf444a | 116 | m_font = *wxMSWSystemMenuFontModule::ms_systemMenuFont; |
19b42379 JS |
117 | } |
118 | else | |
119 | { | |
120 | m_font = *wxNORMAL_FONT; | |
121 | } | |
2bda0e17 | 122 | |
d9cf726b | 123 | m_bCheckable = bCheckable; |
078cf5cb | 124 | m_bOwnerDrawn = false; |
d9cf726b JS |
125 | m_nHeight = 0; |
126 | m_nMarginWidth = ms_nLastMarginWidth; | |
cfbf444a | 127 | m_nMinHeight = wxMSWSystemMenuFontModule::ms_systemMenuHeight; |
10403254 VZ |
128 | |
129 | m_bmpDisabled = wxNullBitmap; | |
d9cf726b JS |
130 | } |
131 | ||
132 | ||
133 | // these items will be set during the first invocation of the c'tor, | |
134 | // because the values will be determined by checking the system settings, | |
10403254 | 135 | // which is a chunk of code |
d9cf726b JS |
136 | size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 0; |
137 | size_t wxOwnerDrawn::ms_nLastMarginWidth = 0; | |
138 | ||
2bda0e17 KB |
139 | |
140 | // drawing | |
141 | // ------- | |
142 | ||
143 | // get size of the item | |
2a2a71e3 JS |
144 | // The item size includes the menu string, the accel string, |
145 | // the bitmap and size for a submenu expansion arrow... | |
c86f1403 | 146 | bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight) |
2bda0e17 KB |
147 | { |
148 | wxMemoryDC dc; | |
2bda0e17 | 149 | |
f2ab8671 | 150 | wxString str = wxStripMenuCodes(m_strName); |
2bda0e17 | 151 | |
2a2a71e3 JS |
152 | // if we have a valid accel string, then pad out |
153 | // the menu string so the menu and accel string are not | |
154 | // placed ontop of eachother. | |
155 | if ( !m_strAccel.empty() ) | |
156 | { | |
157 | str.Pad(str.Length()%8); | |
158 | str += m_strAccel; | |
159 | } | |
2bda0e17 | 160 | |
e7dd6ecd GT |
161 | if (m_font.Ok()) |
162 | dc.SetFont(GetFont()); | |
163 | ||
2bda0e17 | 164 | dc.GetTextExtent(str, (long *)pwidth, (long *)pheight); |
c7527e3f | 165 | |
2a2a71e3 JS |
166 | // add space at the end of the menu for the submenu expansion arrow |
167 | // this will also allow offsetting the accel string from the right edge | |
7230716d | 168 | *pwidth += GetDefaultMarginWidth() + 16; |
2a2a71e3 | 169 | |
34621cc5 | 170 | // increase size to accommodate bigger bitmaps if necessary |
e7dd6ecd GT |
171 | if (m_bmpChecked.Ok()) |
172 | { | |
173 | // Is BMP height larger then text height? | |
174 | size_t adjustedHeight = m_bmpChecked.GetHeight() + | |
7230716d | 175 | 2*wxSystemSettings::GetMetric(wxSYS_EDGE_Y); |
e7dd6ecd GT |
176 | if (*pheight < adjustedHeight) |
177 | *pheight = adjustedHeight; | |
33ac7e6f | 178 | |
e7dd6ecd | 179 | // Does BMP encroach on default check menu position? |
7230716d | 180 | size_t adjustedWidth = m_bmpChecked.GetWidth(); |
33ac7e6f | 181 | |
e7dd6ecd | 182 | // Do we need to widen margin to fit BMP? |
7230716d | 183 | if ((size_t)GetMarginWidth() < adjustedWidth) |
e7dd6ecd GT |
184 | SetMarginWidth(adjustedWidth); |
185 | } | |
33ac7e6f | 186 | |
d9cf726b JS |
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; | |
191 | ||
7230716d VZ |
192 | // remember height for use in OnDrawItem |
193 | m_nHeight = *pheight; | |
2bda0e17 | 194 | |
078cf5cb | 195 | return true; |
2bda0e17 KB |
196 | } |
197 | ||
2bda0e17 | 198 | // draw the item |
6d5b2a57 VZ |
199 | bool wxOwnerDrawn::OnDrawItem(wxDC& dc, |
200 | const wxRect& rc, | |
201 | wxODAction act, | |
202 | wxODStatus st) | |
2bda0e17 KB |
203 | { |
204 | // we do nothing on focus change | |
205 | if ( act == wxODFocusChanged ) | |
078cf5cb | 206 | return true; |
2bda0e17 | 207 | |
c0cb30dc JS |
208 | |
209 | // this flag determines whether or not an edge will | |
210 | // be drawn around the bitmap. In most "windows classic" | |
211 | // applications, a 1-pixel highlight edge is drawn around | |
212 | // the bitmap of an item when it is selected. However, | |
213 | // with the new "luna" theme, no edge is drawn around | |
214 | // the bitmap because the background is white (this applies | |
215 | // only to "non-XP style" menus w/ bitmaps -- | |
216 | // see IE 6 menus for an example) | |
217 | ||
218 | bool draw_bitmap_edge = true; | |
219 | ||
2bda0e17 KB |
220 | // set the colors |
221 | // -------------- | |
222 | DWORD colBack, colText; | |
223 | if ( st & wxODSelected ) { | |
224 | colBack = GetSysColor(COLOR_HIGHLIGHT); | |
5ac7be7a JS |
225 | if (!(st & wxODDisabled)) |
226 | { | |
227 | colText = GetSysColor(COLOR_HIGHLIGHTTEXT); | |
228 | } | |
229 | else | |
230 | { | |
231 | colText = GetSysColor(COLOR_GRAYTEXT); | |
232 | } | |
2bda0e17 KB |
233 | } |
234 | else { | |
235 | // fall back to default colors if none explicitly specified | |
7230716d VZ |
236 | colBack = m_colBack.Ok() ? wxColourToPalRGB(m_colBack) |
237 | : GetSysColor(COLOR_WINDOW); | |
238 | colText = m_colText.Ok() ? wxColourToPalRGB(m_colText) | |
239 | : GetSysColor(COLOR_WINDOWTEXT); | |
2bda0e17 | 240 | } |
33ac7e6f | 241 | |
c0cb30dc | 242 | |
cb0bcdd6 | 243 | // don't draw an edge around the bitmap, if background is white ... |
c0cb30dc | 244 | DWORD menu_bg_color = GetSysColor(COLOR_MENU); |
cb0bcdd6 VZ |
245 | if ( ( GetRValue( menu_bg_color ) >= 0xf0 && |
246 | GetGValue( menu_bg_color ) >= 0xf0 && | |
247 | GetBValue( menu_bg_color ) >= 0xf0 ) | |
248 | // ... or if the menu item is disabled | |
249 | || ( st & wxODDisabled ) | |
250 | ) | |
c0cb30dc JS |
251 | { |
252 | draw_bitmap_edge = false; | |
253 | } | |
254 | ||
255 | ||
7230716d VZ |
256 | HDC hdc = GetHdcOf(dc); |
257 | COLORREF colOldText = ::SetTextColor(hdc, colText), | |
258 | colOldBack = ::SetBkColor(hdc, colBack); | |
259 | ||
260 | int margin = GetMarginWidth() + wxSystemSettings::GetMetric(wxSYS_EDGE_X); | |
2bda0e17 KB |
261 | |
262 | // select the font and draw the text | |
263 | // --------------------------------- | |
264 | ||
d9cf726b | 265 | |
33ac7e6f | 266 | // determine where to draw and leave space for a check-mark. |
7230716d | 267 | int xText = rc.x + margin; |
d9cf726b | 268 | |
2bda0e17 | 269 | |
33ac7e6f | 270 | // using native API because it reckognizes '&' |
7230716d VZ |
271 | int nPrevMode = SetBkMode(hdc, TRANSPARENT); |
272 | HBRUSH hbr = CreateSolidBrush(colBack), | |
273 | hPrevBrush = (HBRUSH)SelectObject(hdc, hbr); | |
d9cf726b | 274 | |
7230716d VZ |
275 | RECT rectFill = { rc.GetLeft(), rc.GetTop(), |
276 | rc.GetRight() + 1, rc.GetBottom() + 1 }; | |
d9cf726b | 277 | |
7230716d VZ |
278 | if ( (st & wxODSelected) && m_bmpChecked.Ok() ) { |
279 | // only draw the highlight under the text, not under | |
280 | // the bitmap or checkmark | |
281 | rectFill.left = xText; | |
282 | } | |
2bda0e17 | 283 | |
7230716d | 284 | FillRect(hdc, &rectFill, hbr); |
2bda0e17 | 285 | |
7230716d VZ |
286 | // use default font if no font set |
287 | HFONT hfont; | |
288 | if ( m_font.Ok() ) { | |
289 | m_font.RealizeResource(); | |
290 | hfont = (HFONT)m_font.GetResourceHandle(); | |
291 | } | |
292 | else { | |
293 | hfont = (HFONT)::GetStockObject(SYSTEM_FONT); | |
294 | } | |
f44b4495 | 295 | |
7230716d | 296 | HFONT hPrevFont = (HFONT) ::SelectObject(hdc, hfont); |
d9cf726b | 297 | |
7230716d | 298 | wxString strMenuText = m_strName.BeforeFirst('\t'); |
6d5b2a57 | 299 | |
7230716d VZ |
300 | SIZE sizeRect; |
301 | GetTextExtentPoint32(hdc,strMenuText.c_str(), strMenuText.Length(),&sizeRect); | |
302 | ::DrawState(hdc, NULL, NULL, | |
303 | (LPARAM)strMenuText.c_str(), strMenuText.length(), | |
304 | xText, rc.y+( (int) ((rc.GetHeight()-sizeRect.cy)/2.0) )-1, // centre text vertically | |
305 | rc.GetWidth()-margin, sizeRect.cy, | |
306 | DST_PREFIXTEXT | | |
307 | (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0)); | |
10403254 | 308 | |
7230716d VZ |
309 | // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO |
310 | // as last parameter in DrawState() (at least with Windows98). So we have | |
311 | // to take care of right alignment ourselves. | |
312 | if ( !m_strAccel.empty() ) | |
313 | { | |
314 | int accel_width, accel_height; | |
315 | dc.GetTextExtent(m_strAccel, &accel_width, &accel_height); | |
316 | // right align accel string with right edge of menu ( offset by the | |
317 | // margin width ) | |
318 | ::DrawState(hdc, NULL, NULL, | |
319 | (LPARAM)m_strAccel.c_str(), m_strAccel.length(), | |
320 | rc.GetWidth()-margin-accel_width, rc.y+(int) ((rc.GetHeight()-sizeRect.cy)/2.0), | |
321 | rc.GetWidth()-margin-accel_width, sizeRect.cy, | |
322 | DST_TEXT | | |
323 | (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0)); | |
324 | } | |
2bda0e17 | 325 | |
7230716d VZ |
326 | (void)SelectObject(hdc, hPrevBrush); |
327 | (void)SelectObject(hdc, hPrevFont); | |
328 | (void)SetBkMode(hdc, nPrevMode); | |
2b5f62a0 | 329 | |
7230716d | 330 | DeleteObject(hbr); |
2bda0e17 KB |
331 | |
332 | // draw the bitmap | |
333 | // --------------- | |
334 | if ( IsCheckable() && !m_bmpChecked.Ok() ) { | |
335 | if ( st & wxODChecked ) { | |
33ac7e6f | 336 | // what goes on: DrawFrameControl creates a b/w mask, |
2bda0e17 KB |
337 | // then we copy it to screen to have right colors |
338 | ||
339 | // first create a monochrome bitmap in a memory DC | |
340 | HDC hdcMem = CreateCompatibleDC(hdc); | |
7230716d | 341 | HBITMAP hbmpCheck = CreateBitmap(margin, m_nHeight, 1, 1, 0); |
2bda0e17 KB |
342 | SelectObject(hdcMem, hbmpCheck); |
343 | ||
344 | // then draw a check mark into it | |
7230716d | 345 | RECT rect = { 0, 0, margin, m_nHeight }; |
07cf98cb VZ |
346 | if ( m_nHeight > 0 ) |
347 | { | |
f44b4495 | 348 | ::DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK); |
07cf98cb | 349 | } |
2bda0e17 KB |
350 | |
351 | // finally copy it to screen DC and clean up | |
7230716d | 352 | BitBlt(hdc, rc.x, rc.y, margin, m_nHeight, |
2bda0e17 | 353 | hdcMem, 0, 0, SRCCOPY); |
07cf98cb | 354 | |
2bda0e17 | 355 | DeleteDC(hdcMem); |
07cf98cb | 356 | DeleteObject(hbmpCheck); |
2bda0e17 KB |
357 | } |
358 | } | |
359 | else { | |
cb0bcdd6 VZ |
360 | wxBitmap bmp; |
361 | ||
362 | if ( st & wxODDisabled ) | |
363 | { | |
364 | bmp = GetDisabledBitmap(); | |
365 | } | |
366 | ||
367 | if ( !bmp.Ok() ) | |
368 | { | |
369 | // for not checkable bitmaps we should always use unchecked one because | |
370 | // their checked bitmap is not set | |
371 | bmp = GetBitmap(!IsCheckable() || (st & wxODChecked)); | |
372 | } | |
373 | ||
2bda0e17 KB |
374 | if ( bmp.Ok() ) { |
375 | wxMemoryDC dcMem(&dc); | |
376 | dcMem.SelectObject(bmp); | |
377 | ||
378 | // center bitmap | |
379 | int nBmpWidth = bmp.GetWidth(), | |
380 | nBmpHeight = bmp.GetHeight(); | |
381 | ||
382 | // there should be enough place! | |
383 | wxASSERT((nBmpWidth <= rc.GetWidth()) && (nBmpHeight <= rc.GetHeight())); | |
384 | ||
f44b4495 | 385 | int heightDiff = m_nHeight - nBmpHeight; |
7230716d | 386 | dc.Blit(rc.x + (margin - nBmpWidth) / 2, |
33ac7e6f KB |
387 | rc.y + heightDiff / 2, |
388 | nBmpWidth, nBmpHeight, | |
078cf5cb | 389 | &dcMem, 0, 0, wxCOPY, true /* use mask */); |
2bda0e17 | 390 | |
c0cb30dc | 391 | if ( st & wxODSelected && draw_bitmap_edge ) { |
2a2a71e3 | 392 | RECT rectBmp = { rc.GetLeft(), rc.GetTop(), |
7230716d VZ |
393 | rc.GetLeft() + margin, |
394 | rc.GetTop() + m_nHeight }; | |
2a2a71e3 | 395 | SetBkColor(hdc, colBack); |
7230716d | 396 | |
c0cb30dc | 397 | DrawEdge(hdc, &rectBmp, BDR_RAISEDOUTER, BF_SOFT | BF_RECT); |
2bda0e17 KB |
398 | } |
399 | } | |
400 | } | |
401 | ||
7230716d VZ |
402 | ::SetTextColor(hdc, colOldText); |
403 | ::SetBkColor(hdc, colOldBack); | |
2bda0e17 | 404 | |
078cf5cb | 405 | return true; |
2bda0e17 KB |
406 | } |
407 | ||
1b24a68e GRG |
408 | |
409 | #endif // wxUSE_OWNER_DRAWN | |
410 |