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