]> git.saurik.com Git - wxWidgets.git/blame - src/msw/ownerdrw.cpp
Fixed wxToolBar for WinCE so normal bitmaps can be used;
[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>
65571936 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
8ee64db5
JS
42#ifndef SPI_GETKEYBOARDCUES
43#define SPI_GETKEYBOARDCUES 0x100A
44#endif
45
cfbf444a
JS
46class wxMSWSystemMenuFontModule : public wxModule
47{
48public:
49
50 virtual bool OnInit()
51 {
52 ms_systemMenuFont = new wxFont;
53
54#if defined(__WXMSW__) && defined(__WIN32__) && defined(SM_CXMENUCHECK)
55 NONCLIENTMETRICS nm;
56 nm.cbSize = sizeof(NONCLIENTMETRICS);
10403254 57 SystemParametersInfo(SPI_GETNONCLIENTMETRICS,0,&nm,0);
cfbf444a
JS
58
59 ms_systemMenuButtonWidth = nm.iMenuHeight;
60 ms_systemMenuHeight = nm.iMenuHeight;
61
62 // create menu font
63 wxNativeFontInfo info;
64 memcpy(&info.lf, &nm.lfMenuFont, sizeof(LOGFONT));
65 ms_systemMenuFont->Create(info);
51d2fa37
VZ
66
67 if (SystemParametersInfo(SPI_GETKEYBOARDCUES, 0, &ms_showCues, 0) == 0)
68 ms_showCues = true;
cfbf444a
JS
69#endif
70
71 return true;
72 }
73
74 virtual void OnExit()
75 {
76 delete ms_systemMenuFont;
77 ms_systemMenuFont = NULL;
78 }
79
80 static wxFont* ms_systemMenuFont;
81 static int ms_systemMenuButtonWidth; // windows clean install default
82 static int ms_systemMenuHeight; // windows clean install default
51d2fa37 83 static bool ms_showCues;
cfbf444a
JS
84private:
85 DECLARE_DYNAMIC_CLASS(wxMSWSystemMenuFontModule)
86};
87
88// these static variables are by the wxMSWSystemMenuFontModule object
89// and reflect the system settings returned by the Win32 API's
90// SystemParametersInfo() call.
91
92wxFont* wxMSWSystemMenuFontModule::ms_systemMenuFont = NULL;
93int wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth = 18; // windows clean install default
94int wxMSWSystemMenuFontModule::ms_systemMenuHeight = 18; // windows clean install default
51d2fa37 95bool wxMSWSystemMenuFontModule::ms_showCues = true;
cfbf444a
JS
96
97IMPLEMENT_DYNAMIC_CLASS(wxMSWSystemMenuFontModule, wxModule)
2432b92d 98
2bda0e17
KB
99// ============================================================================
100// implementation of wxOwnerDrawn class
101// ============================================================================
102
103// ctor
104// ----
33ac7e6f 105wxOwnerDrawn::wxOwnerDrawn(const wxString& str,
19b42379 106 bool bCheckable, bool bMenuItem)
2bda0e17
KB
107 : m_strName(str)
108{
d9cf726b
JS
109 // get the default menu height and font from the system
110 NONCLIENTMETRICS nm;
111 nm.cbSize = sizeof (NONCLIENTMETRICS);
10403254 112 SystemParametersInfo (SPI_GETNONCLIENTMETRICS,0,&nm,0);
d9cf726b 113 m_nMinHeight = nm.iMenuHeight;
10403254 114
d9cf726b
JS
115 // nm.iMenuWidth is the system default for the width of
116 // menu icons and checkmarks
117 if (ms_nDefaultMarginWidth == 0)
118 {
271fa250
JS
119 ms_nDefaultMarginWidth = ::GetSystemMetrics(SM_CXMENUCHECK) + wxSystemSettings::GetMetric(wxSYS_EDGE_X);
120 ms_nLastMarginWidth = ms_nDefaultMarginWidth;
d9cf726b
JS
121 }
122
cfbf444a 123 if (wxMSWSystemMenuFontModule::ms_systemMenuFont->Ok() && bMenuItem)
19b42379 124 {
cfbf444a 125 m_font = *wxMSWSystemMenuFontModule::ms_systemMenuFont;
19b42379
JS
126 }
127 else
128 {
129 m_font = *wxNORMAL_FONT;
130 }
2bda0e17 131
d9cf726b 132 m_bCheckable = bCheckable;
078cf5cb 133 m_bOwnerDrawn = false;
d9cf726b
JS
134 m_nHeight = 0;
135 m_nMarginWidth = ms_nLastMarginWidth;
cfbf444a 136 m_nMinHeight = wxMSWSystemMenuFontModule::ms_systemMenuHeight;
10403254
VZ
137
138 m_bmpDisabled = wxNullBitmap;
d9cf726b
JS
139}
140
141
142// these items will be set during the first invocation of the c'tor,
143// because the values will be determined by checking the system settings,
10403254 144// which is a chunk of code
d9cf726b
JS
145size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 0;
146size_t wxOwnerDrawn::ms_nLastMarginWidth = 0;
147
2bda0e17
KB
148
149// drawing
150// -------
151
152// get size of the item
2a2a71e3
JS
153// The item size includes the menu string, the accel string,
154// the bitmap and size for a submenu expansion arrow...
c86f1403 155bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight)
2bda0e17
KB
156{
157 wxMemoryDC dc;
2bda0e17 158
f2ab8671 159 wxString str = wxStripMenuCodes(m_strName);
2bda0e17 160
2a2a71e3
JS
161 // if we have a valid accel string, then pad out
162 // the menu string so the menu and accel string are not
163 // placed ontop of eachother.
164 if ( !m_strAccel.empty() )
165 {
166 str.Pad(str.Length()%8);
167 str += m_strAccel;
168 }
2bda0e17 169
e7dd6ecd
GT
170 if (m_font.Ok())
171 dc.SetFont(GetFont());
172
2bda0e17 173 dc.GetTextExtent(str, (long *)pwidth, (long *)pheight);
c7527e3f 174
34621cc5 175 // increase size to accommodate bigger bitmaps if necessary
e7dd6ecd
GT
176 if (m_bmpChecked.Ok())
177 {
178 // Is BMP height larger then text height?
179 size_t adjustedHeight = m_bmpChecked.GetHeight() +
7230716d 180 2*wxSystemSettings::GetMetric(wxSYS_EDGE_Y);
e7dd6ecd
GT
181 if (*pheight < adjustedHeight)
182 *pheight = adjustedHeight;
33ac7e6f 183
e7dd6ecd 184 // Does BMP encroach on default check menu position?
7230716d 185 size_t adjustedWidth = m_bmpChecked.GetWidth();
33ac7e6f 186
e7dd6ecd 187 // Do we need to widen margin to fit BMP?
7230716d 188 if ((size_t)GetMarginWidth() < adjustedWidth)
e7dd6ecd
GT
189 SetMarginWidth(adjustedWidth);
190 }
33ac7e6f 191
51d2fa37
VZ
192 // add space at the end of the menu for the submenu expansion arrow
193 // this will also allow offsetting the accel string from the right edge
194 *pwidth += GetMarginWidth() + 16;
195
196 // add a 4-pixel separator, otherwise menus look cluttered
197 *pwidth += 4;
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
7230716d
VZ
204 // remember height for use in OnDrawItem
205 m_nHeight = *pheight;
2bda0e17 206
078cf5cb 207 return true;
2bda0e17
KB
208}
209
2bda0e17 210// draw the item
6d5b2a57
VZ
211bool wxOwnerDrawn::OnDrawItem(wxDC& dc,
212 const wxRect& rc,
213 wxODAction act,
214 wxODStatus st)
2bda0e17
KB
215{
216 // we do nothing on focus change
217 if ( act == wxODFocusChanged )
078cf5cb 218 return true;
2bda0e17 219
c0cb30dc
JS
220
221 // this flag determines whether or not an edge will
222 // be drawn around the bitmap. In most "windows classic"
223 // applications, a 1-pixel highlight edge is drawn around
224 // the bitmap of an item when it is selected. However,
225 // with the new "luna" theme, no edge is drawn around
226 // the bitmap because the background is white (this applies
227 // only to "non-XP style" menus w/ bitmaps --
228 // see IE 6 menus for an example)
229
230 bool draw_bitmap_edge = true;
231
2bda0e17
KB
232 // set the colors
233 // --------------
234 DWORD colBack, colText;
235 if ( st & wxODSelected ) {
236 colBack = GetSysColor(COLOR_HIGHLIGHT);
5ac7be7a
JS
237 if (!(st & wxODDisabled))
238 {
239 colText = GetSysColor(COLOR_HIGHLIGHTTEXT);
240 }
241 else
242 {
243 colText = GetSysColor(COLOR_GRAYTEXT);
244 }
2bda0e17
KB
245 }
246 else {
247 // fall back to default colors if none explicitly specified
7230716d 248 colBack = m_colBack.Ok() ? wxColourToPalRGB(m_colBack)
51d2fa37 249 : GetSysColor(COLOR_MENU);
7230716d 250 colText = m_colText.Ok() ? wxColourToPalRGB(m_colText)
51d2fa37 251 : GetSysColor(COLOR_MENUTEXT);
2bda0e17 252 }
33ac7e6f 253
c0cb30dc 254
cb0bcdd6 255 // don't draw an edge around the bitmap, if background is white ...
c0cb30dc 256 DWORD menu_bg_color = GetSysColor(COLOR_MENU);
cb0bcdd6
VZ
257 if ( ( GetRValue( menu_bg_color ) >= 0xf0 &&
258 GetGValue( menu_bg_color ) >= 0xf0 &&
259 GetBValue( menu_bg_color ) >= 0xf0 )
cb0bcdd6 260 )
c0cb30dc
JS
261 {
262 draw_bitmap_edge = false;
263 }
264
265
7230716d
VZ
266 HDC hdc = GetHdcOf(dc);
267 COLORREF colOldText = ::SetTextColor(hdc, colText),
268 colOldBack = ::SetBkColor(hdc, colBack);
269
51d2fa37
VZ
270 // *2, as in wxSYS_EDGE_Y
271 int margin = GetMarginWidth() + 2 * wxSystemSettings::GetMetric(wxSYS_EDGE_X);
2bda0e17
KB
272
273 // select the font and draw the text
274 // ---------------------------------
275
d9cf726b 276
33ac7e6f 277 // determine where to draw and leave space for a check-mark.
51d2fa37
VZ
278 // + 1 pixel to separate the edge from the highlight rectangle
279 int xText = rc.x + margin + 1;
d9cf726b 280
2bda0e17 281
33ac7e6f 282 // using native API because it reckognizes '&'
7230716d
VZ
283 int nPrevMode = SetBkMode(hdc, TRANSPARENT);
284 HBRUSH hbr = CreateSolidBrush(colBack),
285 hPrevBrush = (HBRUSH)SelectObject(hdc, hbr);
d9cf726b 286
7230716d
VZ
287 RECT rectFill = { rc.GetLeft(), rc.GetTop(),
288 rc.GetRight() + 1, rc.GetBottom() + 1 };
d9cf726b 289
51d2fa37 290 if ( (st & wxODSelected) && m_bmpChecked.Ok() && draw_bitmap_edge ) {
7230716d
VZ
291 // only draw the highlight under the text, not under
292 // the bitmap or checkmark
293 rectFill.left = xText;
294 }
2bda0e17 295
7230716d 296 FillRect(hdc, &rectFill, hbr);
2bda0e17 297
7230716d
VZ
298 // use default font if no font set
299 HFONT hfont;
300 if ( m_font.Ok() ) {
301 m_font.RealizeResource();
302 hfont = (HFONT)m_font.GetResourceHandle();
303 }
304 else {
305 hfont = (HFONT)::GetStockObject(SYSTEM_FONT);
306 }
f44b4495 307
7230716d 308 HFONT hPrevFont = (HFONT) ::SelectObject(hdc, hfont);
d9cf726b 309
7230716d 310 wxString strMenuText = m_strName.BeforeFirst('\t');
6d5b2a57 311
51d2fa37
VZ
312 xText += 3; // separate text from the highlight rectangle
313
7230716d
VZ
314 SIZE sizeRect;
315 GetTextExtentPoint32(hdc,strMenuText.c_str(), strMenuText.Length(),&sizeRect);
316 ::DrawState(hdc, NULL, NULL,
317 (LPARAM)strMenuText.c_str(), strMenuText.length(),
51d2fa37 318 xText, rc.y + (int) ((rc.GetHeight()-sizeRect.cy)/2.0), // centre text vertically
7230716d
VZ
319 rc.GetWidth()-margin, sizeRect.cy,
320 DST_PREFIXTEXT |
51d2fa37
VZ
321 (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0) |
322 (((st & wxODHidePrefix) && !wxMSWSystemMenuFontModule::ms_showCues) ? 512 : 0)); // 512 == DSS_HIDEPREFIX
10403254 323
7230716d
VZ
324 // ::SetTextAlign(hdc, TA_RIGHT) doesn't work with DSS_DISABLED or DSS_MONO
325 // as last parameter in DrawState() (at least with Windows98). So we have
326 // to take care of right alignment ourselves.
327 if ( !m_strAccel.empty() )
328 {
329 int accel_width, accel_height;
330 dc.GetTextExtent(m_strAccel, &accel_width, &accel_height);
331 // right align accel string with right edge of menu ( offset by the
332 // margin width )
333 ::DrawState(hdc, NULL, NULL,
334 (LPARAM)m_strAccel.c_str(), m_strAccel.length(),
51d2fa37
VZ
335 rc.GetWidth()-16-accel_width, rc.y+(int) ((rc.GetHeight()-sizeRect.cy)/2.0),
336 0, 0,
7230716d
VZ
337 DST_TEXT |
338 (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0));
339 }
2bda0e17 340
7230716d
VZ
341 (void)SelectObject(hdc, hPrevBrush);
342 (void)SelectObject(hdc, hPrevFont);
343 (void)SetBkMode(hdc, nPrevMode);
2b5f62a0 344
7230716d 345 DeleteObject(hbr);
2bda0e17
KB
346
347 // draw the bitmap
348 // ---------------
349 if ( IsCheckable() && !m_bmpChecked.Ok() ) {
350 if ( st & wxODChecked ) {
33ac7e6f 351 // what goes on: DrawFrameControl creates a b/w mask,
2bda0e17
KB
352 // then we copy it to screen to have right colors
353
354 // first create a monochrome bitmap in a memory DC
355 HDC hdcMem = CreateCompatibleDC(hdc);
7230716d 356 HBITMAP hbmpCheck = CreateBitmap(margin, m_nHeight, 1, 1, 0);
2bda0e17
KB
357 SelectObject(hdcMem, hbmpCheck);
358
359 // then draw a check mark into it
7230716d 360 RECT rect = { 0, 0, margin, m_nHeight };
07cf98cb
VZ
361 if ( m_nHeight > 0 )
362 {
f44b4495 363 ::DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
07cf98cb 364 }
2bda0e17
KB
365
366 // finally copy it to screen DC and clean up
7230716d 367 BitBlt(hdc, rc.x, rc.y, margin, m_nHeight,
2bda0e17 368 hdcMem, 0, 0, SRCCOPY);
07cf98cb 369
2bda0e17 370 DeleteDC(hdcMem);
07cf98cb 371 DeleteObject(hbmpCheck);
2bda0e17
KB
372 }
373 }
374 else {
cb0bcdd6
VZ
375 wxBitmap bmp;
376
377 if ( st & wxODDisabled )
378 {
379 bmp = GetDisabledBitmap();
380 }
381
382 if ( !bmp.Ok() )
383 {
384 // for not checkable bitmaps we should always use unchecked one because
385 // their checked bitmap is not set
386 bmp = GetBitmap(!IsCheckable() || (st & wxODChecked));
387 }
388
2bda0e17
KB
389 if ( bmp.Ok() ) {
390 wxMemoryDC dcMem(&dc);
391 dcMem.SelectObject(bmp);
392
393 // center bitmap
394 int nBmpWidth = bmp.GetWidth(),
395 nBmpHeight = bmp.GetHeight();
396
397 // there should be enough place!
398 wxASSERT((nBmpWidth <= rc.GetWidth()) && (nBmpHeight <= rc.GetHeight()));
399
f44b4495 400 int heightDiff = m_nHeight - nBmpHeight;
7230716d 401 dc.Blit(rc.x + (margin - nBmpWidth) / 2,
33ac7e6f
KB
402 rc.y + heightDiff / 2,
403 nBmpWidth, nBmpHeight,
078cf5cb 404 &dcMem, 0, 0, wxCOPY, true /* use mask */);
2bda0e17 405
51d2fa37 406 if ( ( st & wxODSelected ) && !( st & wxODDisabled ) && draw_bitmap_edge ) {
2a2a71e3 407 RECT rectBmp = { rc.GetLeft(), rc.GetTop(),
7230716d
VZ
408 rc.GetLeft() + margin,
409 rc.GetTop() + m_nHeight };
2a2a71e3 410 SetBkColor(hdc, colBack);
7230716d 411
51d2fa37 412 DrawEdge(hdc, &rectBmp, BDR_RAISEDINNER, BF_RECT);
2bda0e17
KB
413 }
414 }
415 }
416
7230716d
VZ
417 ::SetTextColor(hdc, colOldText);
418 ::SetBkColor(hdc, colOldBack);
2bda0e17 419
078cf5cb 420 return true;
2bda0e17
KB
421}
422
1b24a68e
GRG
423
424#endif // wxUSE_OWNER_DRAWN
425