]> git.saurik.com Git - wxWidgets.git/blame - src/msw/ownerdrw.cpp
Pass events to pushed event handlers.
[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
a3b46648
UU
12#ifdef __GNUG__
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"
2bda0e17 38
1b24a68e
GRG
39#if wxUSE_OWNER_DRAWN
40
2432b92d 41
2bda0e17
KB
42// ============================================================================
43// implementation of wxOwnerDrawn class
44// ============================================================================
45
46// ctor
47// ----
33ac7e6f 48wxOwnerDrawn::wxOwnerDrawn(const wxString& str,
19b42379 49 bool bCheckable, bool bMenuItem)
2bda0e17
KB
50 : m_strName(str)
51{
2432b92d 52#if defined(__WXMSW__) && defined(__WIN32__) && defined(SM_CXMENUCHECK)
d9cf726b
JS
53 // get the default menu height and font from the system
54 NONCLIENTMETRICS nm;
55 nm.cbSize = sizeof (NONCLIENTMETRICS);
56 SystemParametersInfo (SPI_GETNONCLIENTMETRICS,0,&nm,0);
57 m_nMinHeight = nm.iMenuHeight;
58
59 // nm.iMenuWidth is the system default for the width of
60 // menu icons and checkmarks
61 if (ms_nDefaultMarginWidth == 0)
62 {
63 ms_nDefaultMarginWidth = nm.iMenuWidth;
64 ms_nLastMarginWidth = nm.iMenuWidth;
65 }
66
19b42379
JS
67 if (bMenuItem)
68 {
69 static wxFont menu_font;
70 if (!menu_font.Ok())
71 {
72 // create menu font
73 wxNativeFontInfo info;
74 memcpy(&info.lf, &nm.lfMenuFont, sizeof(LOGFONT));
75 menu_font.Create(info);
76 }
77
78 m_font = menu_font;
79 }
80 else
81 {
82 m_font = *wxNORMAL_FONT;
83 }
d9cf726b
JS
84#else
85 // windows clean install default
86 m_nMinHeight = 18;
87
88 if (ms_nDefaultMarginWidth == 0)
89 {
90 ms_nDefaultMarginWidth = 18;
91 ms_nLastMarginWidth = 18;
92 }
93 if (wxNORMAL_FONT)
94 m_font = *wxNORMAL_FONT;
2bda0e17
KB
95#endif
96
d9cf726b
JS
97 m_bCheckable = bCheckable;
98 m_bOwnerDrawn = FALSE;
99 m_nHeight = 0;
100 m_nMarginWidth = ms_nLastMarginWidth;
101}
102
103
104// these items will be set during the first invocation of the c'tor,
105// because the values will be determined by checking the system settings,
106// which is a chunk of code
107size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 0;
108size_t wxOwnerDrawn::ms_nLastMarginWidth = 0;
109
2bda0e17
KB
110
111// drawing
112// -------
113
114// get size of the item
2a2a71e3
JS
115// The item size includes the menu string, the accel string,
116// the bitmap and size for a submenu expansion arrow...
c86f1403 117bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight)
2bda0e17
KB
118{
119 wxMemoryDC dc;
2bda0e17 120
f2ab8671 121 wxString str = wxStripMenuCodes(m_strName);
2bda0e17 122
2a2a71e3
JS
123 // if we have a valid accel string, then pad out
124 // the menu string so the menu and accel string are not
125 // placed ontop of eachother.
126 if ( !m_strAccel.empty() )
127 {
128 str.Pad(str.Length()%8);
129 str += m_strAccel;
130 }
2bda0e17 131
e7dd6ecd
GT
132 if (m_font.Ok())
133 dc.SetFont(GetFont());
134
2bda0e17 135 dc.GetTextExtent(str, (long *)pwidth, (long *)pheight);
c7527e3f 136
5ac7be7a
JS
137 if (!m_strAccel.IsEmpty())
138 {
139 // measure the accelerator string, and add it's width to
140 // the total item width, plus 16 (Accelerators are right justified,
141 // with the right edge of the text rectangle 16 pixels left of
142 // the right edge of the menu)
143
144 int accel_width, accel_height;
145 dc.GetTextExtent(m_strAccel, &accel_width, &accel_height);
2a2a71e3 146 *pwidth += accel_width;
5ac7be7a
JS
147 }
148
2a2a71e3
JS
149 // add space at the end of the menu for the submenu expansion arrow
150 // this will also allow offsetting the accel string from the right edge
4fa47e56 151 *pwidth += (size_t) (GetDefaultMarginWidth() * 1.5);
2a2a71e3 152
d9cf726b
JS
153 // JACS: items still look too tightly packed, so adding 5 pixels.
154 (*pheight) = (*pheight) + 5;
c7527e3f 155
e7dd6ecd
GT
156 // Ray Gilbert's changes - Corrects the problem of a BMP
157 // being placed next to text in a menu item, and the BMP does
33ac7e6f 158 // not match the size expected by the system. This will
e7dd6ecd
GT
159 // resize the space so the BMP will fit. Without this, BMPs
160 // must be no larger or smaller than 16x16.
161 if (m_bmpChecked.Ok())
162 {
163 // Is BMP height larger then text height?
164 size_t adjustedHeight = m_bmpChecked.GetHeight() +
a756f210 165 wxSystemSettings::GetMetric(wxSYS_EDGE_Y);
e7dd6ecd
GT
166 if (*pheight < adjustedHeight)
167 *pheight = adjustedHeight;
33ac7e6f 168
e7dd6ecd
GT
169 // Does BMP encroach on default check menu position?
170 size_t adjustedWidth = m_bmpChecked.GetWidth() +
a756f210 171 (wxSystemSettings::GetMetric(wxSYS_EDGE_X) * 2);
2a2a71e3
JS
172// if (ms_nDefaultMarginWidth < adjustedWidth)
173// *pwidth += adjustedWidth - ms_nDefaultMarginWidth;
33ac7e6f 174
e7dd6ecd 175 // Do we need to widen margin to fit BMP?
d2ec53ea 176 if ((size_t)GetMarginWidth() != adjustedWidth)
e7dd6ecd 177 SetMarginWidth(adjustedWidth);
2a2a71e3
JS
178
179 // add the size of the bitmap to our total size...
180 *pwidth += GetMarginWidth();
e7dd6ecd 181 }
33ac7e6f 182
2a2a71e3
JS
183 // add the size of the bitmap to our total size - even if we don't have
184 // a bitmap we leave room for one...
185 *pwidth += GetMarginWidth();
186
d9cf726b
JS
187 // make sure that this item is at least as
188 // tall as the user's system settings specify
189 if (*pheight < m_nMinHeight)
190 *pheight = m_nMinHeight;
191
2bda0e17
KB
192 m_nHeight = *pheight; // remember height for use in OnDrawItem
193
194 return TRUE;
195}
196
197// searching for this macro you'll find all the code where I'm using the native
198// Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
199// port this code to X. (VZ)
200
c7527e3f
JS
201// JACS: TODO. Why does a disabled but highlighted item still
202// get drawn embossed? How can we tell DrawState that we don't want the
203// embossing?
204
2bdf7154 205#if defined(__WIN32__) && !defined(__SYMANTEC__) && !defined(__TWIN32__)
2bda0e17
KB
206#define O_DRAW_NATIVE_API // comments below explain why I use it
207#endif
208
209// draw the item
6d5b2a57
VZ
210bool wxOwnerDrawn::OnDrawItem(wxDC& dc,
211 const wxRect& rc,
212 wxODAction act,
213 wxODStatus st)
2bda0e17
KB
214{
215 // we do nothing on focus change
216 if ( act == wxODFocusChanged )
217 return TRUE;
218
219 // wxColor <-> RGB
19193a2c 220 #define ToRGB(col) PALETTERGB(col.Red(), col.Green(), col.Blue())
2bda0e17
KB
221 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
222
223 // set the colors
224 // --------------
225 DWORD colBack, colText;
226 if ( st & wxODSelected ) {
227 colBack = GetSysColor(COLOR_HIGHLIGHT);
5ac7be7a
JS
228 if (!(st & wxODDisabled))
229 {
230 colText = GetSysColor(COLOR_HIGHLIGHTTEXT);
231 }
232 else
233 {
234 colText = GetSysColor(COLOR_GRAYTEXT);
235 }
2bda0e17
KB
236 }
237 else {
238 // fall back to default colors if none explicitly specified
239 colBack = m_colBack.Ok() ? ToRGB(m_colBack) : GetSysColor(COLOR_WINDOW);
240 colText = m_colText.Ok() ? ToRGB(m_colText) : GetSysColor(COLOR_WINDOWTEXT);
241 }
33ac7e6f 242
2bda0e17
KB
243 #ifdef O_DRAW_NATIVE_API
244 #define hdc (HDC)dc.GetHDC()
245 COLORREF colOldText = ::SetTextColor(hdc, colText),
246 colOldBack = ::SetBkColor(hdc, colBack);
247 #else
248 dc.SetTextForeground(wxColor(UnRGB(colText)));
249 dc.SetTextBackground(wxColor(UnRGB(colBack)));
250 #endif
251
252 // select the font and draw the text
253 // ---------------------------------
254
d9cf726b 255
33ac7e6f 256 // determine where to draw and leave space for a check-mark.
d9cf726b
JS
257 // Add 3 pixel padding so text appears well within highlight rectangle
258 int x = rc.x + GetMarginWidth() + 3;
259
2bda0e17 260
33ac7e6f 261 // using native API because it reckognizes '&'
2bda0e17
KB
262 #ifdef O_DRAW_NATIVE_API
263 int nPrevMode = SetBkMode(hdc, TRANSPARENT);
07cf98cb
VZ
264 HBRUSH hbr = CreateSolidBrush(colBack),
265 hPrevBrush = (HBRUSH)SelectObject(hdc, hbr);
2bda0e17 266
d9cf726b
JS
267 RECT rectFill = { rc.GetLeft(), rc.GetTop(), rc.GetRight()+1, rc.GetBottom() };
268
269 if ( st & wxODSelected && m_bmpChecked.Ok()) {
270 // only draw the highlight under the text, not under
271 // the bitmap or checkmark; leave a 1-pixel gap.
272 rectFill.left = GetMarginWidth() + 1;
273 }
274
275 FillRect(hdc, &rectFill, hbr);
2bda0e17
KB
276
277 // use default font if no font set
278 HFONT hfont;
279 if ( m_font.Ok() ) {
280 m_font.RealizeResource();
281 hfont = (HFONT)m_font.GetResourceHandle();
282 }
283 else {
284 hfont = (HFONT)::GetStockObject(SYSTEM_FONT);
285 }
286
c4e7c2aa 287 HFONT hPrevFont = (HFONT) ::SelectObject(hdc, hfont);
f44b4495 288
5ac7be7a 289 wxString strMenuText = m_strName.BeforeFirst('\t');
d9cf726b 290
2a2a71e3
JS
291 SIZE sizeRect;
292 GetTextExtentPoint32(hdc,strMenuText.c_str(), strMenuText.Length(),&sizeRect);
4fa47e56 293 ::DrawState(hdc, NULL, NULL,
5ac7be7a 294 (LPARAM)strMenuText.c_str(), strMenuText.length(),
4fa47e56 295 x, rc.y+( (int) ((rc.GetHeight()-sizeRect.cy)/2.0) )-1, // centre text vertically
2a2a71e3 296 rc.GetWidth()-GetMarginWidth(), sizeRect.cy,
5ac7be7a
JS
297 DST_PREFIXTEXT |
298 (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0));
6d5b2a57 299
136cb3c7 300 if ( !m_strAccel.empty() )
6d5b2a57 301 {
2a2a71e3
JS
302 // right align accel string with right edge of menu ( offset by the margin width )
303 ::SetTextAlign(hdc, TA_RIGHT);
4fa47e56 304 ::DrawState(hdc, NULL, NULL,
5ac7be7a 305 (LPARAM)m_strAccel.c_str(), m_strAccel.length(),
4fa47e56 306 rc.GetWidth()-(GetMarginWidth()), rc.y+(int) ((rc.GetHeight()-sizeRect.cy)/2.0),
2a2a71e3 307 rc.GetWidth()-GetMarginWidth(), sizeRect.cy,
5ac7be7a
JS
308 DST_TEXT |
309 (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0));
2a2a71e3 310 ::SetTextAlign(hdc, TA_LEFT);
6d5b2a57 311 }
2bda0e17
KB
312
313 (void)SelectObject(hdc, hPrevBrush);
314 (void)SelectObject(hdc, hPrevFont);
315 (void)SetBkMode(hdc, nPrevMode);
2b5f62a0
VZ
316
317 DeleteObject(hbr);
2bda0e17
KB
318 #else
319 dc.SetFont(GetFont());
d9cf726b 320 dc.DrawText(wxStripMenuCodes(m_strName), x, rc.y);
2bda0e17
KB
321 #endif //O_DRAW_NATIVE_API
322
323 // draw the bitmap
324 // ---------------
325 if ( IsCheckable() && !m_bmpChecked.Ok() ) {
326 if ( st & wxODChecked ) {
327 // using native APIs for performance and simplicity
5260b1c5 328#ifdef O_DRAW_NATIVE_API
33ac7e6f 329 // what goes on: DrawFrameControl creates a b/w mask,
2bda0e17
KB
330 // then we copy it to screen to have right colors
331
332 // first create a monochrome bitmap in a memory DC
333 HDC hdcMem = CreateCompatibleDC(hdc);
334 HBITMAP hbmpCheck = CreateBitmap(GetMarginWidth(), m_nHeight, 1, 1, 0);
335 SelectObject(hdcMem, hbmpCheck);
336
337 // then draw a check mark into it
338 RECT rect = { 0, 0, GetMarginWidth(), m_nHeight };
07cf98cb
VZ
339 if ( m_nHeight > 0 )
340 {
f44b4495 341 ::DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
07cf98cb 342 }
2bda0e17
KB
343
344 // finally copy it to screen DC and clean up
33ac7e6f 345 BitBlt(hdc, rc.x, rc.y, GetMarginWidth(), m_nHeight,
2bda0e17 346 hdcMem, 0, 0, SRCCOPY);
07cf98cb 347
2bda0e17 348 DeleteDC(hdcMem);
07cf98cb 349 DeleteObject(hbmpCheck);
5260b1c5 350#else
2bda0e17 351 // #### to do: perhaps using Marlett font (create equiv. font under X)
5260b1c5
JS
352// wxFAIL("not implemented");
353#endif //O_DRAW_NATIVE_API
2bda0e17
KB
354 }
355 }
356 else {
357 // for uncheckable item we use only the 'checked' bitmap
358 wxBitmap bmp(GetBitmap(IsCheckable() ? ((st & wxODChecked) != 0) : TRUE));
359 if ( bmp.Ok() ) {
360 wxMemoryDC dcMem(&dc);
361 dcMem.SelectObject(bmp);
362
363 // center bitmap
364 int nBmpWidth = bmp.GetWidth(),
365 nBmpHeight = bmp.GetHeight();
366
367 // there should be enough place!
368 wxASSERT((nBmpWidth <= rc.GetWidth()) && (nBmpHeight <= rc.GetHeight()));
369
f44b4495 370 int heightDiff = m_nHeight - nBmpHeight;
33ac7e6f
KB
371 dc.Blit(rc.x + (GetMarginWidth() - nBmpWidth) / 2,
372 rc.y + heightDiff / 2,
373 nBmpWidth, nBmpHeight,
f44b4495 374 &dcMem, 0, 0, wxCOPY, TRUE /* use mask */);
2bda0e17
KB
375
376 if ( st & wxODSelected ) {
2a2a71e3
JS
377 #ifdef O_DRAW_NATIVE_API
378 RECT rectBmp = { rc.GetLeft(), rc.GetTop(),
379 rc.GetLeft() + GetMarginWidth(),
380 rc.GetTop() + m_nHeight-1 };
381 SetBkColor(hdc, colBack);
382 DrawEdge(hdc, &rectBmp, EDGE_RAISED, BF_SOFT | BF_RECT);
383 #else
d9cf726b
JS
384 int x1, y1, x2, y2;
385 x1 = rc.x;
386 y1 = rc.y;
387 x2 = x1 + GetMarginWidth() - 1;
388 y2 = y1 + m_nHeight - 1;
389
390 dc.SetPen(*wxWHITE_PEN);
391 dc.DrawLine(x1, y1, x2, y1);
392 dc.DrawLine(x1, y1, x1, y2);
393 dc.SetPen(*wxGREY_PEN);
394 dc.DrawLine(x1, y2-1, x2, y2-1);
395 dc.DrawLine(x2, y1, x2, y2);
2a2a71e3 396 #endif //O_DRAW_NATIVE_API
2bda0e17
KB
397 }
398 }
399 }
400
401 #ifdef O_DRAW_NATIVE_API
402 ::SetTextColor(hdc, colOldText);
403 ::SetBkColor(hdc, colOldBack);
404
405 #undef hdc
406 #endif //O_DRAW_NATIVE_API
407
408 return TRUE;
409}
410
1b24a68e
GRG
411
412#endif // wxUSE_OWNER_DRAWN
413