/[pcsx2_0.9.7]/branch/r3113_0.9.7_beta/3rdparty/wxWidgets/src/msw/ownerdrw.cpp
ViewVC logotype

Contents of /branch/r3113_0.9.7_beta/3rdparty/wxWidgets/src/msw/ownerdrw.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22