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