]> git.saurik.com Git - wxWidgets.git/blob - src/msw/ownerdrw.cpp
[ 1509599 ] 'Split pickers page in widgets sample' with more icons and rebaking.
[wxWidgets.git] / src / msw / ownerdrw.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/ownerdrw.cpp
3 // Purpose: implementation of wxOwnerDrawn class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 13.11.97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/window.h"
21 #include "wx/msw/private.h"
22 #include "wx/font.h"
23 #include "wx/bitmap.h"
24 #include "wx/dcmemory.h"
25 #include "wx/menu.h"
26 #include "wx/utils.h"
27 #include "wx/settings.h"
28 #include "wx/menuitem.h"
29 #endif
30
31 #include "wx/ownerdrw.h"
32 #include "wx/fontutil.h"
33 #include "wx/module.h"
34
35 #if wxUSE_OWNER_DRAWN
36
37 #ifndef SPI_GETKEYBOARDCUES
38 #define SPI_GETKEYBOARDCUES 0x100A
39 #endif
40
41 class wxMSWSystemMenuFontModule : public wxModule
42 {
43 public:
44
45 virtual bool OnInit()
46 {
47 ms_systemMenuFont = new wxFont;
48
49 #if defined(__WXMSW__) && defined(__WIN32__) && defined(SM_CXMENUCHECK)
50 NONCLIENTMETRICS nm;
51 nm.cbSize = sizeof(NONCLIENTMETRICS);
52 SystemParametersInfo(SPI_GETNONCLIENTMETRICS,0,&nm,0);
53
54 ms_systemMenuButtonWidth = nm.iMenuHeight;
55 ms_systemMenuHeight = nm.iMenuHeight;
56
57 // create menu font
58 wxNativeFontInfo info;
59 memcpy(&info.lf, &nm.lfMenuFont, sizeof(LOGFONT));
60 ms_systemMenuFont->Create(info);
61
62 if (SystemParametersInfo(SPI_GETKEYBOARDCUES, 0, &ms_showCues, 0) == 0)
63 ms_showCues = true;
64 #endif
65
66 return true;
67 }
68
69 virtual void OnExit()
70 {
71 delete ms_systemMenuFont;
72 ms_systemMenuFont = NULL;
73 }
74
75 static wxFont* ms_systemMenuFont;
76 static int ms_systemMenuButtonWidth; // windows clean install default
77 static int ms_systemMenuHeight; // windows clean install default
78 static bool ms_showCues;
79 private:
80 DECLARE_DYNAMIC_CLASS(wxMSWSystemMenuFontModule)
81 };
82
83 // these static variables are from the wxMSWSystemMenuFontModule object
84 // and reflect the system settings returned by the Win32 API's
85 // SystemParametersInfo() call.
86
87 wxFont* wxMSWSystemMenuFontModule::ms_systemMenuFont = NULL;
88 int wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth = 18; // windows clean install default
89 int wxMSWSystemMenuFontModule::ms_systemMenuHeight = 18; // windows clean install default
90 bool wxMSWSystemMenuFontModule::ms_showCues = true;
91
92 IMPLEMENT_DYNAMIC_CLASS(wxMSWSystemMenuFontModule, wxModule)
93
94
95 // VC++ 6 gives a warning here:
96 //
97 // return type for 'OwnerDrawnSet_wxImplementation_HashTable::iterator::
98 // operator ->' is 'class wxOwnerDrawn ** ' (ie; not a UDT or reference to
99 // a UDT. Will produce errors if applied using infix notation.
100 //
101 // shut it down
102 #if defined __VISUALC__ && __VISUALC__ <= 1300
103 #if __VISUALC__ >= 1200
104 #pragma warning(push)
105 #define POP_WARNINGS
106 #endif
107 #pragma warning(disable: 4284)
108 #endif
109
110 #include "wx/hashset.h"
111 WX_DECLARE_HASH_SET(wxOwnerDrawn*, wxPointerHash, wxPointerEqual, OwnerDrawnSet);
112
113 #ifdef POP_WARNINGS
114 #pragma warning(pop)
115 #endif
116
117 // ============================================================================
118 // implementation of wxOwnerDrawn class
119 // ============================================================================
120
121 // ctor
122 // ----
123 wxOwnerDrawn::wxOwnerDrawn(const wxString& str,
124 bool bCheckable,
125 bool bMenuItem)
126 : m_strName(str)
127 {
128 if ( ms_nDefaultMarginWidth == 0 )
129 {
130 ms_nDefaultMarginWidth = ::GetSystemMetrics(SM_CXMENUCHECK) +
131 wxSystemSettings::GetMetric(wxSYS_EDGE_X);
132 ms_nLastMarginWidth = ms_nDefaultMarginWidth;
133 }
134
135 m_bCheckable = bCheckable;
136 m_bOwnerDrawn = false;
137 m_isMenuItem = bMenuItem;
138 m_nHeight = 0;
139 m_nMarginWidth = ms_nLastMarginWidth;
140 m_nMinHeight = wxMSWSystemMenuFontModule::ms_systemMenuHeight;
141 }
142
143 wxOwnerDrawn::~wxOwnerDrawn()
144 {
145 }
146
147 bool wxOwnerDrawn::IsMenuItem() const
148 {
149 return m_isMenuItem;
150 }
151
152
153 // these items will be set during the first invocation of the c'tor,
154 // because the values will be determined by checking the system settings,
155 // which is a chunk of code
156 size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 0;
157 size_t wxOwnerDrawn::ms_nLastMarginWidth = 0;
158
159
160 // drawing
161 // -------
162
163 wxFont wxOwnerDrawn::GetFontToUse() const
164 {
165 wxFont font = m_font;
166 if ( !font.Ok() )
167 {
168 if ( IsMenuItem() )
169 font = *wxMSWSystemMenuFontModule::ms_systemMenuFont;
170
171 if ( !font.Ok() )
172 font = *wxNORMAL_FONT;
173 }
174
175 return font;
176 }
177
178 // get size of the item
179 // The item size includes the menu string, the accel string,
180 // the bitmap and size for a submenu expansion arrow...
181 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight)
182 {
183 if ( IsOwnerDrawn() )
184 {
185 wxMemoryDC dc;
186
187 wxString str = wxStripMenuCodes(m_strName);
188
189 // if we have a valid accel string, then pad out
190 // the menu string so that the menu and accel string are not
191 // placed on top of each other.
192 if ( !m_strAccel.empty() )
193 {
194 str.Pad(str.length()%8);
195 str += m_strAccel;
196 }
197
198 dc.SetFont(GetFontToUse());
199
200 dc.GetTextExtent(str, (long *)pwidth, (long *)pheight);
201
202 // add space at the end of the menu for the submenu expansion arrow
203 // this will also allow offsetting the accel string from the right edge
204 *pwidth += GetMarginWidth() + 16;
205 }
206 else // don't draw the text, just the bitmap (if any)
207 {
208 *pwidth =
209 *pheight = 0;
210 }
211
212 // increase size to accommodate bigger bitmaps if necessary
213 if (m_bmpChecked.Ok())
214 {
215 // Is BMP height larger then text height?
216 size_t adjustedHeight = m_bmpChecked.GetHeight() +
217 2*wxSystemSettings::GetMetric(wxSYS_EDGE_Y);
218 if (*pheight < adjustedHeight)
219 *pheight = adjustedHeight;
220
221 const size_t widthBmp = m_bmpChecked.GetWidth();
222 if ( IsOwnerDrawn() )
223 {
224 // widen the margin to fit the bitmap if necessary
225 if ((size_t)GetMarginWidth() < widthBmp)
226 SetMarginWidth(widthBmp);
227 }
228 else // we must allocate enough space for the bitmap
229 {
230 *pwidth += widthBmp;
231 }
232 }
233
234 // add a 4-pixel separator, otherwise menus look cluttered
235 *pwidth += 4;
236
237 // make sure that this item is at least as tall as the system menu height
238 if ( *pheight < m_nMinHeight )
239 *pheight = m_nMinHeight;
240
241 // remember height for use in OnDrawItem
242 m_nHeight = *pheight;
243
244 return true;
245 }
246
247 // draw the item
248 bool wxOwnerDrawn::OnDrawItem(wxDC& dc,
249 const wxRect& rc,
250 wxODAction act,
251 wxODStatus st)
252 {
253 // we do nothing on focus change
254 if ( act == wxODFocusChanged )
255 return true;
256
257
258 // this flag determines whether or not an edge will
259 // be drawn around the bitmap. In most "windows classic"
260 // applications, a 1-pixel highlight edge is drawn around
261 // the bitmap of an item when it is selected. However,
262 // with the new "luna" theme, no edge is drawn around
263 // the bitmap because the background is white (this applies
264 // only to "non-XP style" menus w/ bitmaps --
265 // see IE 6 menus for an example)
266
267 bool draw_bitmap_edge = true;
268
269 // set the colors
270 // --------------
271 DWORD colBack, colText;
272 if ( st & wxODSelected )
273 {
274 colBack = GetSysColor(COLOR_HIGHLIGHT);
275 if (!(st & wxODDisabled))
276 {
277 colText = GetSysColor(COLOR_HIGHLIGHTTEXT);
278 }
279 else
280 {
281 colText = GetSysColor(COLOR_GRAYTEXT);
282 }
283 }
284 else
285 {
286 // fall back to default colors if none explicitly specified
287 colBack = m_colBack.Ok() ? wxColourToPalRGB(m_colBack)
288 : GetSysColor(COLOR_MENU);
289 colText = m_colText.Ok() ? wxColourToPalRGB(m_colText)
290 : GetSysColor(COLOR_MENUTEXT);
291 }
292
293 if ( IsOwnerDrawn() )
294 {
295 // don't draw an edge around the bitmap, if background is white ...
296 DWORD menu_bg_color = GetSysColor(COLOR_MENU);
297 if ( ( GetRValue( menu_bg_color ) >= 0xf0 &&
298 GetGValue( menu_bg_color ) >= 0xf0 &&
299 GetBValue( menu_bg_color ) >= 0xf0 )
300 )
301 {
302 draw_bitmap_edge = false;
303 }
304 }
305 else // edge doesn't look well with default Windows drawing
306 {
307 draw_bitmap_edge = false;
308 }
309
310
311 HDC hdc = GetHdcOf(dc);
312 COLORREF colOldText = ::SetTextColor(hdc, colText),
313 colOldBack = ::SetBkColor(hdc, colBack);
314
315 // *2, as in wxSYS_EDGE_Y
316 int margin = GetMarginWidth() + 2 * wxSystemSettings::GetMetric(wxSYS_EDGE_X);
317
318 // select the font and draw the text
319 // ---------------------------------
320
321
322 // determine where to draw and leave space for a check-mark.
323 // + 1 pixel to separate the edge from the highlight rectangle
324 int xText = rc.x + margin + 1;
325
326
327 // using native API because it recognizes '&'
328 if ( IsOwnerDrawn() )
329 {
330 int nPrevMode = SetBkMode(hdc, TRANSPARENT);
331 AutoHBRUSH hbr(colBack);
332 SelectInHDC selBrush(hdc, hbr);
333
334 RECT rectFill = { rc.GetLeft(), rc.GetTop(),
335 rc.GetRight() + 1, rc.GetBottom() + 1 };
336
337 if ( (st & wxODSelected) && m_bmpChecked.Ok() && draw_bitmap_edge )
338 {
339 // only draw the highlight under the text, not under
340 // the bitmap or checkmark
341 rectFill.left = xText;
342 }
343
344 FillRect(hdc, &rectFill, hbr);
345
346 // use default font if no font set
347 wxFont fontToUse = GetFontToUse();
348 SelectInHDC selFont(hdc, GetHfontOf(fontToUse));
349
350 wxString strMenuText = m_strName.BeforeFirst('\t');
351
352 xText += 3; // separate text from the highlight rectangle
353
354 SIZE sizeRect;
355 ::GetTextExtentPoint32(hdc, strMenuText.c_str(), strMenuText.length(), &sizeRect);
356 ::DrawState(hdc, NULL, NULL,
357 (LPARAM)strMenuText.c_str(), strMenuText.length(),
358 xText, rc.y + (int) ((rc.GetHeight()-sizeRect.cy)/2.0), // centre text vertically
359 rc.GetWidth()-margin, sizeRect.cy,
360 DST_PREFIXTEXT |
361 (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0) |
362 (((st & wxODHidePrefix) && !wxMSWSystemMenuFontModule::ms_showCues) ? 512 : 0)); // 512 == DSS_HIDEPREFIX
363
364 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
365 // as the last parameter in DrawState() (at least with Windows98). So we have
366 // to take care of right alignment ourselves.
367 if ( !m_strAccel.empty() )
368 {
369 int accel_width, accel_height;
370 dc.GetTextExtent(m_strAccel, &accel_width, &accel_height);
371 // right align accel string with right edge of menu ( offset by the
372 // margin width )
373 ::DrawState(hdc, NULL, NULL,
374 (LPARAM)m_strAccel.c_str(), m_strAccel.length(),
375 rc.GetWidth()-16-accel_width, rc.y+(int) ((rc.GetHeight()-sizeRect.cy)/2.0),
376 0, 0,
377 DST_TEXT |
378 (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0));
379 }
380
381 (void)SetBkMode(hdc, nPrevMode);
382 }
383
384
385 // draw the bitmap
386 // ---------------
387 if ( IsCheckable() && !m_bmpChecked.Ok() )
388 {
389 if ( st & wxODChecked )
390 {
391 // what goes on: DrawFrameControl creates a b/w mask,
392 // then we copy it to screen to have right colors
393
394 // first create a monochrome bitmap in a memory DC
395 HDC hdcMem = CreateCompatibleDC(hdc);
396 HBITMAP hbmpCheck = CreateBitmap(margin, m_nHeight, 1, 1, 0);
397 SelectObject(hdcMem, hbmpCheck);
398
399 // then draw a check mark into it
400 RECT rect = { 0, 0, margin, m_nHeight };
401 if ( m_nHeight > 0 )
402 {
403 ::DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
404 }
405
406 // finally copy it to screen DC and clean up
407 BitBlt(hdc, rc.x, rc.y, margin, m_nHeight, hdcMem, 0, 0, SRCCOPY);
408
409 DeleteDC(hdcMem);
410 DeleteObject(hbmpCheck);
411 }
412 }
413 else
414 {
415 wxBitmap bmp;
416
417 if ( st & wxODDisabled )
418 {
419 bmp = GetDisabledBitmap();
420 }
421
422 if ( !bmp.Ok() )
423 {
424 // for not checkable bitmaps we should always use unchecked one because
425 // their checked bitmap is not set
426 bmp = GetBitmap(!IsCheckable() || (st & wxODChecked));
427 }
428
429 if ( bmp.Ok() )
430 {
431 wxMemoryDC dcMem(&dc);
432 dcMem.SelectObject(bmp);
433
434 // center bitmap
435 int nBmpWidth = bmp.GetWidth(),
436 nBmpHeight = bmp.GetHeight();
437
438 // there should be enough space!
439 wxASSERT((nBmpWidth <= rc.GetWidth()) && (nBmpHeight <= rc.GetHeight()));
440
441 int heightDiff = m_nHeight - nBmpHeight;
442 dc.Blit(rc.x + (margin - nBmpWidth) / 2,
443 rc.y + heightDiff / 2,
444 nBmpWidth, nBmpHeight,
445 &dcMem, 0, 0, wxCOPY, true /* use mask */);
446
447 if ( ( st & wxODSelected ) && !( st & wxODDisabled ) && draw_bitmap_edge )
448 {
449 RECT rectBmp = { rc.GetLeft(), rc.GetTop(),
450 rc.GetLeft() + margin,
451 rc.GetTop() + m_nHeight };
452 SetBkColor(hdc, colBack);
453
454 DrawEdge(hdc, &rectBmp, BDR_RAISEDINNER, BF_RECT);
455 }
456 }
457 }
458
459 ::SetTextColor(hdc, colOldText);
460 ::SetBkColor(hdc, colOldBack);
461
462 return true;
463 }
464
465
466 #endif // wxUSE_OWNER_DRAWN