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