]> git.saurik.com Git - wxWidgets.git/blame - src/msw/ownerdrw.cpp
Prevent Cocoa from waiting indefinitely when stopping the event loop
[wxWidgets.git] / src / msw / ownerdrw.cpp
CommitLineData
2bda0e17
KB
1///////////////////////////////////////////////////////////////////////////////
2// Name: msw/ownerdrw.cpp
3// Purpose: implementation of wxOwnerDrawn class
4// Author: Vadim Zeitlin
33ac7e6f 5// Modified by:
2bda0e17
KB
6// Created: 13.11.97
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
6c9a19aa 9// Licence: wxWindows licence
2bda0e17
KB
10///////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
a3b46648
UU
13#pragma implementation
14#endif
2bda0e17
KB
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
ff0ea71c 18#include "wx/msw/private.h"
2bda0e17
KB
19
20#ifdef __BORLANDC__
a3b46648 21#pragma hdrstop
2bda0e17
KB
22#endif
23
24#ifndef WX_PRECOMP
0c589ad0 25 #include "wx/window.h"
136cb3c7 26 #include "wx/msw/private.h"
2432b92d
JS
27 #include "wx/font.h"
28 #include "wx/bitmap.h"
29 #include "wx/dcmemory.h"
2bda0e17 30 #include "wx/menu.h"
2432b92d 31 #include "wx/utils.h"
2bda0e17
KB
32#endif
33
5100cabf 34#include "wx/settings.h"
2bda0e17
KB
35#include "wx/ownerdrw.h"
36#include "wx/menuitem.h"
d9cf726b 37#include "wx/fontutil.h"
e87fb0ee 38#include "wx/module.h"
2bda0e17 39
1b24a68e
GRG
40#if wxUSE_OWNER_DRAWN
41
cfbf444a
JS
42class wxMSWSystemMenuFontModule : public wxModule
43{
44public:
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);
10403254 53 SystemParametersInfo(SPI_GETNONCLIENTMETRICS,0,&nm,0);
cfbf444a
JS
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#endif
63
64 return true;
65 }
66
67 virtual void OnExit()
68 {
69 delete ms_systemMenuFont;
70 ms_systemMenuFont = NULL;
71 }
72
73 static wxFont* ms_systemMenuFont;
74 static int ms_systemMenuButtonWidth; // windows clean install default
75 static int ms_systemMenuHeight; // windows clean install default
76private:
77 DECLARE_DYNAMIC_CLASS(wxMSWSystemMenuFontModule)
78};
79
80// these static variables are by the wxMSWSystemMenuFontModule object
81// and reflect the system settings returned by the Win32 API's
82// SystemParametersInfo() call.
83
84wxFont* wxMSWSystemMenuFontModule::ms_systemMenuFont = NULL;
85int wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth = 18; // windows clean install default
86int wxMSWSystemMenuFontModule::ms_systemMenuHeight = 18; // windows clean install default
87
88IMPLEMENT_DYNAMIC_CLASS(wxMSWSystemMenuFontModule, wxModule)
2432b92d 89
2bda0e17
KB
90// ============================================================================
91// implementation of wxOwnerDrawn class
92// ============================================================================
93
94// ctor
95// ----
33ac7e6f 96wxOwnerDrawn::wxOwnerDrawn(const wxString& str,
19b42379 97 bool bCheckable, bool bMenuItem)
2bda0e17
KB
98 : m_strName(str)
99{
d9cf726b
JS
100 // get the default menu height and font from the system
101 NONCLIENTMETRICS nm;
102 nm.cbSize = sizeof (NONCLIENTMETRICS);
10403254 103 SystemParametersInfo (SPI_GETNONCLIENTMETRICS,0,&nm,0);
d9cf726b 104 m_nMinHeight = nm.iMenuHeight;
10403254 105
d9cf726b
JS
106 // nm.iMenuWidth is the system default for the width of
107 // menu icons and checkmarks
108 if (ms_nDefaultMarginWidth == 0)
109 {
cfbf444a
JS
110 ms_nDefaultMarginWidth = wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth;
111 ms_nLastMarginWidth = wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth;
d9cf726b
JS
112 }
113
cfbf444a 114 if (wxMSWSystemMenuFontModule::ms_systemMenuFont->Ok() && bMenuItem)
19b42379 115 {
cfbf444a 116 m_font = *wxMSWSystemMenuFontModule::ms_systemMenuFont;
19b42379
JS
117 }
118 else
119 {
120 m_font = *wxNORMAL_FONT;
121 }
2bda0e17 122
d9cf726b
JS
123 m_bCheckable = bCheckable;
124 m_bOwnerDrawn = FALSE;
125 m_nHeight = 0;
126 m_nMarginWidth = ms_nLastMarginWidth;
cfbf444a 127 m_nMinHeight = wxMSWSystemMenuFontModule::ms_systemMenuHeight;
10403254
VZ
128
129 m_bmpDisabled = wxNullBitmap;
d9cf726b
JS
130}
131
132
133// these items will be set during the first invocation of the c'tor,
134// because the values will be determined by checking the system settings,
10403254 135// which is a chunk of code
d9cf726b
JS
136size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 0;
137size_t wxOwnerDrawn::ms_nLastMarginWidth = 0;
138
2bda0e17
KB
139
140// drawing
141// -------
142
143// get size of the item
2a2a71e3
JS
144// The item size includes the menu string, the accel string,
145// the bitmap and size for a submenu expansion arrow...
c86f1403 146bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight)
2bda0e17
KB
147{
148 wxMemoryDC dc;
2bda0e17 149
f2ab8671 150 wxString str = wxStripMenuCodes(m_strName);
2bda0e17 151
2a2a71e3
JS
152 // if we have a valid accel string, then pad out
153 // the menu string so the menu and accel string are not
154 // placed ontop of eachother.
155 if ( !m_strAccel.empty() )
156 {
157 str.Pad(str.Length()%8);
158 str += m_strAccel;
159 }
2bda0e17 160
e7dd6ecd
GT
161 if (m_font.Ok())
162 dc.SetFont(GetFont());
163
2bda0e17 164 dc.GetTextExtent(str, (long *)pwidth, (long *)pheight);
c7527e3f 165
2a2a71e3
JS
166 // add space at the end of the menu for the submenu expansion arrow
167 // this will also allow offsetting the accel string from the right edge
10403254 168 *pwidth += (size_t) (GetDefaultMarginWidth() * 1.5) + 16;
2a2a71e3 169
d9cf726b
JS
170 // JACS: items still look too tightly packed, so adding 5 pixels.
171 (*pheight) = (*pheight) + 5;
c7527e3f 172
e7dd6ecd
GT
173 // Ray Gilbert's changes - Corrects the problem of a BMP
174 // being placed next to text in a menu item, and the BMP does
33ac7e6f 175 // not match the size expected by the system. This will
e7dd6ecd
GT
176 // resize the space so the BMP will fit. Without this, BMPs
177 // must be no larger or smaller than 16x16.
178 if (m_bmpChecked.Ok())
179 {
180 // Is BMP height larger then text height?
181 size_t adjustedHeight = m_bmpChecked.GetHeight() +
a756f210 182 wxSystemSettings::GetMetric(wxSYS_EDGE_Y);
e7dd6ecd
GT
183 if (*pheight < adjustedHeight)
184 *pheight = adjustedHeight;
33ac7e6f 185
e7dd6ecd
GT
186 // Does BMP encroach on default check menu position?
187 size_t adjustedWidth = m_bmpChecked.GetWidth() +
a756f210 188 (wxSystemSettings::GetMetric(wxSYS_EDGE_X) * 2);
33ac7e6f 189
e7dd6ecd 190 // Do we need to widen margin to fit BMP?
d2ec53ea 191 if ((size_t)GetMarginWidth() != adjustedWidth)
e7dd6ecd
GT
192 SetMarginWidth(adjustedWidth);
193 }
33ac7e6f 194
2a2a71e3
JS
195 // add the size of the bitmap to our total size - even if we don't have
196 // a bitmap we leave room for one...
197 *pwidth += GetMarginWidth();
10403254 198
d9cf726b
JS
199 // make sure that this item is at least as
200 // tall as the user's system settings specify
201 if (*pheight < m_nMinHeight)
202 *pheight = m_nMinHeight;
203
2bda0e17
KB
204 m_nHeight = *pheight; // remember height for use in OnDrawItem
205
206 return TRUE;
207}
208
209// searching for this macro you'll find all the code where I'm using the native
210// Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
211// port this code to X. (VZ)
212
b39dbf34 213#if defined(__WIN32__) && !defined(__SYMANTEC__)
2bda0e17
KB
214#define O_DRAW_NATIVE_API // comments below explain why I use it
215#endif
216
217// draw the item
6d5b2a57
VZ
218bool wxOwnerDrawn::OnDrawItem(wxDC& dc,
219 const wxRect& rc,
220 wxODAction act,
221 wxODStatus st)
2bda0e17
KB
222{
223 // we do nothing on focus change
224 if ( act == wxODFocusChanged )
225 return TRUE;
226
227 // wxColor <-> RGB
19193a2c 228 #define ToRGB(col) PALETTERGB(col.Red(), col.Green(), col.Blue())
2bda0e17
KB
229 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
230
c0cb30dc
JS
231
232 // this flag determines whether or not an edge will
233 // be drawn around the bitmap. In most "windows classic"
234 // applications, a 1-pixel highlight edge is drawn around
235 // the bitmap of an item when it is selected. However,
236 // with the new "luna" theme, no edge is drawn around
237 // the bitmap because the background is white (this applies
238 // only to "non-XP style" menus w/ bitmaps --
239 // see IE 6 menus for an example)
240
241 bool draw_bitmap_edge = true;
242
2bda0e17
KB
243 // set the colors
244 // --------------
245 DWORD colBack, colText;
246 if ( st & wxODSelected ) {
247 colBack = GetSysColor(COLOR_HIGHLIGHT);
5ac7be7a
JS
248 if (!(st & wxODDisabled))
249 {
250 colText = GetSysColor(COLOR_HIGHLIGHTTEXT);
251 }
252 else
253 {
254 colText = GetSysColor(COLOR_GRAYTEXT);
255 }
2bda0e17
KB
256 }
257 else {
258 // fall back to default colors if none explicitly specified
259 colBack = m_colBack.Ok() ? ToRGB(m_colBack) : GetSysColor(COLOR_WINDOW);
260 colText = m_colText.Ok() ? ToRGB(m_colText) : GetSysColor(COLOR_WINDOWTEXT);
261 }
33ac7e6f 262
c0cb30dc 263
cb0bcdd6 264 // don't draw an edge around the bitmap, if background is white ...
c0cb30dc 265 DWORD menu_bg_color = GetSysColor(COLOR_MENU);
cb0bcdd6
VZ
266 if ( ( GetRValue( menu_bg_color ) >= 0xf0 &&
267 GetGValue( menu_bg_color ) >= 0xf0 &&
268 GetBValue( menu_bg_color ) >= 0xf0 )
269 // ... or if the menu item is disabled
270 || ( st & wxODDisabled )
271 )
c0cb30dc
JS
272 {
273 draw_bitmap_edge = false;
274 }
275
276
2bda0e17
KB
277 #ifdef O_DRAW_NATIVE_API
278 #define hdc (HDC)dc.GetHDC()
279 COLORREF colOldText = ::SetTextColor(hdc, colText),
280 colOldBack = ::SetBkColor(hdc, colBack);
281 #else
282 dc.SetTextForeground(wxColor(UnRGB(colText)));
283 dc.SetTextBackground(wxColor(UnRGB(colBack)));
284 #endif
285
286 // select the font and draw the text
287 // ---------------------------------
288
d9cf726b 289
33ac7e6f 290 // determine where to draw and leave space for a check-mark.
d9cf726b
JS
291 // Add 3 pixel padding so text appears well within highlight rectangle
292 int x = rc.x + GetMarginWidth() + 3;
293
2bda0e17 294
33ac7e6f 295 // using native API because it reckognizes '&'
2bda0e17
KB
296 #ifdef O_DRAW_NATIVE_API
297 int nPrevMode = SetBkMode(hdc, TRANSPARENT);
07cf98cb
VZ
298 HBRUSH hbr = CreateSolidBrush(colBack),
299 hPrevBrush = (HBRUSH)SelectObject(hdc, hbr);
2bda0e17 300
d9cf726b
JS
301 RECT rectFill = { rc.GetLeft(), rc.GetTop(), rc.GetRight()+1, rc.GetBottom() };
302
c0cb30dc 303 if ( st & wxODSelected && m_bmpChecked.Ok() && draw_bitmap_edge) {
d9cf726b
JS
304 // only draw the highlight under the text, not under
305 // the bitmap or checkmark; leave a 1-pixel gap.
306 rectFill.left = GetMarginWidth() + 1;
307 }
308
309 FillRect(hdc, &rectFill, hbr);
2bda0e17
KB
310
311 // use default font if no font set
312 HFONT hfont;
313 if ( m_font.Ok() ) {
314 m_font.RealizeResource();
315 hfont = (HFONT)m_font.GetResourceHandle();
316 }
317 else {
318 hfont = (HFONT)::GetStockObject(SYSTEM_FONT);
319 }
320
c4e7c2aa 321 HFONT hPrevFont = (HFONT) ::SelectObject(hdc, hfont);
f44b4495 322
5ac7be7a 323 wxString strMenuText = m_strName.BeforeFirst('\t');
d9cf726b 324
2a2a71e3
JS
325 SIZE sizeRect;
326 GetTextExtentPoint32(hdc,strMenuText.c_str(), strMenuText.Length(),&sizeRect);
4fa47e56 327 ::DrawState(hdc, NULL, NULL,
5ac7be7a 328 (LPARAM)strMenuText.c_str(), strMenuText.length(),
4fa47e56 329 x, rc.y+( (int) ((rc.GetHeight()-sizeRect.cy)/2.0) )-1, // centre text vertically
2a2a71e3 330 rc.GetWidth()-GetMarginWidth(), sizeRect.cy,
5ac7be7a
JS
331 DST_PREFIXTEXT |
332 (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0));
6d5b2a57 333
10403254 334 /* Right alignment does not work with DSS_DISABLED (Windows98) - why?
136cb3c7 335 if ( !m_strAccel.empty() )
6d5b2a57 336 {
2a2a71e3
JS
337 // right align accel string with right edge of menu ( offset by the margin width )
338 ::SetTextAlign(hdc, TA_RIGHT);
4fa47e56 339 ::DrawState(hdc, NULL, NULL,
5ac7be7a 340 (LPARAM)m_strAccel.c_str(), m_strAccel.length(),
4fa47e56 341 rc.GetWidth()-(GetMarginWidth()), rc.y+(int) ((rc.GetHeight()-sizeRect.cy)/2.0),
2a2a71e3 342 rc.GetWidth()-GetMarginWidth(), sizeRect.cy,
5ac7be7a
JS
343 DST_TEXT |
344 (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0));
2a2a71e3 345 ::SetTextAlign(hdc, TA_LEFT);
6d5b2a57 346 }
10403254
VZ
347 */
348
349 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO as last parameter
350 // in DrawState() (at least with Windows98). So we have to take care of right alignment ourselves.
351 if ( !m_strAccel.empty() )
352 {
353 int accel_width, accel_height;
354 dc.GetTextExtent(m_strAccel, &accel_width, &accel_height);
355 // right align accel string with right edge of menu ( offset by the margin width )
356 ::DrawState(hdc, NULL, NULL,
357 (LPARAM)m_strAccel.c_str(), m_strAccel.length(),
358 rc.GetWidth()-GetMarginWidth()-accel_width, rc.y+(int) ((rc.GetHeight()-sizeRect.cy)/2.0),
359 rc.GetWidth()-GetMarginWidth()-accel_width, sizeRect.cy,
360 DST_TEXT |
361 (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0));
362 }
2bda0e17
KB
363
364 (void)SelectObject(hdc, hPrevBrush);
365 (void)SelectObject(hdc, hPrevFont);
366 (void)SetBkMode(hdc, nPrevMode);
2b5f62a0
VZ
367
368 DeleteObject(hbr);
2bda0e17
KB
369 #else
370 dc.SetFont(GetFont());
d9cf726b 371 dc.DrawText(wxStripMenuCodes(m_strName), x, rc.y);
2bda0e17
KB
372 #endif //O_DRAW_NATIVE_API
373
374 // draw the bitmap
375 // ---------------
376 if ( IsCheckable() && !m_bmpChecked.Ok() ) {
377 if ( st & wxODChecked ) {
378 // using native APIs for performance and simplicity
5260b1c5 379#ifdef O_DRAW_NATIVE_API
33ac7e6f 380 // what goes on: DrawFrameControl creates a b/w mask,
2bda0e17
KB
381 // then we copy it to screen to have right colors
382
383 // first create a monochrome bitmap in a memory DC
384 HDC hdcMem = CreateCompatibleDC(hdc);
385 HBITMAP hbmpCheck = CreateBitmap(GetMarginWidth(), m_nHeight, 1, 1, 0);
386 SelectObject(hdcMem, hbmpCheck);
387
388 // then draw a check mark into it
389 RECT rect = { 0, 0, GetMarginWidth(), m_nHeight };
07cf98cb
VZ
390 if ( m_nHeight > 0 )
391 {
f44b4495 392 ::DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
07cf98cb 393 }
2bda0e17
KB
394
395 // finally copy it to screen DC and clean up
33ac7e6f 396 BitBlt(hdc, rc.x, rc.y, GetMarginWidth(), m_nHeight,
2bda0e17 397 hdcMem, 0, 0, SRCCOPY);
07cf98cb 398
2bda0e17 399 DeleteDC(hdcMem);
07cf98cb 400 DeleteObject(hbmpCheck);
5260b1c5 401#else
2bda0e17 402 // #### to do: perhaps using Marlett font (create equiv. font under X)
5260b1c5
JS
403// wxFAIL("not implemented");
404#endif //O_DRAW_NATIVE_API
2bda0e17
KB
405 }
406 }
407 else {
cb0bcdd6
VZ
408 wxBitmap bmp;
409
410 if ( st & wxODDisabled )
411 {
412 bmp = GetDisabledBitmap();
413 }
414
415 if ( !bmp.Ok() )
416 {
417 // for not checkable bitmaps we should always use unchecked one because
418 // their checked bitmap is not set
419 bmp = GetBitmap(!IsCheckable() || (st & wxODChecked));
420 }
421
2bda0e17
KB
422 if ( bmp.Ok() ) {
423 wxMemoryDC dcMem(&dc);
424 dcMem.SelectObject(bmp);
425
426 // center bitmap
427 int nBmpWidth = bmp.GetWidth(),
428 nBmpHeight = bmp.GetHeight();
429
430 // there should be enough place!
431 wxASSERT((nBmpWidth <= rc.GetWidth()) && (nBmpHeight <= rc.GetHeight()));
432
f44b4495 433 int heightDiff = m_nHeight - nBmpHeight;
33ac7e6f
KB
434 dc.Blit(rc.x + (GetMarginWidth() - nBmpWidth) / 2,
435 rc.y + heightDiff / 2,
436 nBmpWidth, nBmpHeight,
f44b4495 437 &dcMem, 0, 0, wxCOPY, TRUE /* use mask */);
2bda0e17 438
c0cb30dc 439 if ( st & wxODSelected && draw_bitmap_edge ) {
2a2a71e3
JS
440 #ifdef O_DRAW_NATIVE_API
441 RECT rectBmp = { rc.GetLeft(), rc.GetTop(),
442 rc.GetLeft() + GetMarginWidth(),
443 rc.GetTop() + m_nHeight-1 };
444 SetBkColor(hdc, colBack);
c0cb30dc 445 DrawEdge(hdc, &rectBmp, BDR_RAISEDOUTER, BF_SOFT | BF_RECT);
2a2a71e3 446 #else
d9cf726b
JS
447 int x1, y1, x2, y2;
448 x1 = rc.x;
449 y1 = rc.y;
450 x2 = x1 + GetMarginWidth() - 1;
451 y2 = y1 + m_nHeight - 1;
452
453 dc.SetPen(*wxWHITE_PEN);
454 dc.DrawLine(x1, y1, x2, y1);
455 dc.DrawLine(x1, y1, x1, y2);
456 dc.SetPen(*wxGREY_PEN);
457 dc.DrawLine(x1, y2-1, x2, y2-1);
458 dc.DrawLine(x2, y1, x2, y2);
2a2a71e3 459 #endif //O_DRAW_NATIVE_API
2bda0e17
KB
460 }
461 }
462 }
463
464 #ifdef O_DRAW_NATIVE_API
465 ::SetTextColor(hdc, colOldText);
466 ::SetBkColor(hdc, colOldBack);
467
468 #undef hdc
469 #endif //O_DRAW_NATIVE_API
470
471 return TRUE;
472}
473
1b24a68e
GRG
474
475#endif // wxUSE_OWNER_DRAWN
476