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