]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/ownerdrw.cpp
Patch [ 588732 ] makes wxRB_GROUP/radiobuttons working
[wxWidgets.git] / src / msw / ownerdrw.cpp
... / ...
CommitLineData
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 license
10///////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18#include "wx/msw/private.h"
19
20#ifdef __BORLANDC__
21#pragma hdrstop
22#endif
23
24#ifndef WX_PRECOMP
25 #include "wx/window.h"
26 #include "wx/msw/private.h"
27 #include "wx/font.h"
28 #include "wx/bitmap.h"
29 #include "wx/dcmemory.h"
30 #include "wx/menu.h"
31 #include "wx/utils.h"
32#endif
33
34#include "wx/settings.h"
35#include "wx/ownerdrw.h"
36#include "wx/menuitem.h"
37#include "wx/fontutil.h"
38
39#if wxUSE_OWNER_DRAWN
40
41
42// ============================================================================
43// implementation of wxOwnerDrawn class
44// ============================================================================
45
46// ctor
47// ----
48wxOwnerDrawn::wxOwnerDrawn(const wxString& str,
49 bool bCheckable, bool WXUNUSED(bMenuItem))
50 : m_strName(str)
51{
52#if defined(__WXMSW__) && defined(__WIN32__) && defined(SM_CXMENUCHECK)
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
67 wxNativeFontInfo info;
68 memcpy(&info.lf, &nm.lfMenuFont, sizeof(LOGFONT));
69 m_font.Create(info);
70#else
71 // windows clean install default
72 m_nMinHeight = 18;
73
74 if (ms_nDefaultMarginWidth == 0)
75 {
76 ms_nDefaultMarginWidth = 18;
77 ms_nLastMarginWidth = 18;
78 }
79 if (wxNORMAL_FONT)
80 m_font = *wxNORMAL_FONT;
81#endif
82
83 m_bCheckable = bCheckable;
84 m_bOwnerDrawn = FALSE;
85 m_nHeight = 0;
86 m_nMarginWidth = ms_nLastMarginWidth;
87}
88
89
90// these items will be set during the first invocation of the c'tor,
91// because the values will be determined by checking the system settings,
92// which is a chunk of code
93size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 0;
94size_t wxOwnerDrawn::ms_nLastMarginWidth = 0;
95
96
97// drawing
98// -------
99
100// get size of the item
101bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight)
102{
103 wxMemoryDC dc;
104
105 wxString str = wxStripMenuCodes(m_strName);
106
107 // # without this menu items look too tightly packed (at least under Windows)
108 str += wxT('W'); // 'W' is typically the widest letter
109
110 if (m_font.Ok())
111 dc.SetFont(GetFont());
112
113 dc.GetTextExtent(str, (long *)pwidth, (long *)pheight);
114
115 // JACS: items still look too tightly packed, so adding 5 pixels.
116 (*pheight) = (*pheight) + 5;
117
118 // Ray Gilbert's changes - Corrects the problem of a BMP
119 // being placed next to text in a menu item, and the BMP does
120 // not match the size expected by the system. This will
121 // resize the space so the BMP will fit. Without this, BMPs
122 // must be no larger or smaller than 16x16.
123 if (m_bmpChecked.Ok())
124 {
125 // Is BMP height larger then text height?
126 size_t adjustedHeight = m_bmpChecked.GetHeight() +
127 wxSystemSettings::GetMetric(wxSYS_EDGE_Y);
128 if (*pheight < adjustedHeight)
129 *pheight = adjustedHeight;
130
131 // Does BMP encroach on default check menu position?
132 size_t adjustedWidth = m_bmpChecked.GetWidth() +
133 (wxSystemSettings::GetMetric(wxSYS_EDGE_X) * 2);
134 if (ms_nDefaultMarginWidth < adjustedWidth)
135 *pwidth += adjustedWidth - ms_nDefaultMarginWidth;
136
137 // Do we need to widen margin to fit BMP?
138 if ((size_t)GetMarginWidth() < adjustedWidth)
139 SetMarginWidth(adjustedWidth);
140 }
141
142 // make sure that this item is at least as
143 // tall as the user's system settings specify
144 if (*pheight < m_nMinHeight)
145 *pheight = m_nMinHeight;
146
147 m_nHeight = *pheight; // remember height for use in OnDrawItem
148
149 return TRUE;
150}
151
152// searching for this macro you'll find all the code where I'm using the native
153// Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
154// port this code to X. (VZ)
155
156// JACS: TODO. Why does a disabled but highlighted item still
157// get drawn embossed? How can we tell DrawState that we don't want the
158// embossing?
159
160#if defined(__WIN32__) && !defined(__SC__) && !defined(__TWIN32__)
161#define O_DRAW_NATIVE_API // comments below explain why I use it
162#endif
163
164// draw the item
165bool wxOwnerDrawn::OnDrawItem(wxDC& dc,
166 const wxRect& rc,
167 wxODAction act,
168 wxODStatus st)
169{
170 // we do nothing on focus change
171 if ( act == wxODFocusChanged )
172 return TRUE;
173
174 // wxColor <-> RGB
175 #define ToRGB(col) PALETTERGB(col.Red(), col.Green(), col.Blue())
176 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
177
178 // set the colors
179 // --------------
180 DWORD colBack, colText;
181 if ( st & wxODSelected ) {
182 colBack = GetSysColor(COLOR_HIGHLIGHT);
183 colText = GetSysColor(COLOR_HIGHLIGHTTEXT);
184 }
185 else {
186 // fall back to default colors if none explicitly specified
187 colBack = m_colBack.Ok() ? ToRGB(m_colBack) : GetSysColor(COLOR_WINDOW);
188 colText = m_colText.Ok() ? ToRGB(m_colText) : GetSysColor(COLOR_WINDOWTEXT);
189 }
190
191 #ifdef O_DRAW_NATIVE_API
192 #define hdc (HDC)dc.GetHDC()
193 COLORREF colOldText = ::SetTextColor(hdc, colText),
194 colOldBack = ::SetBkColor(hdc, colBack);
195 #else
196 dc.SetTextForeground(wxColor(UnRGB(colText)));
197 dc.SetTextBackground(wxColor(UnRGB(colBack)));
198 #endif
199
200 // select the font and draw the text
201 // ---------------------------------
202
203
204 // determine where to draw and leave space for a check-mark.
205 // Add 3 pixel padding so text appears well within highlight rectangle
206 int x = rc.x + GetMarginWidth() + 3;
207
208
209 // using native API because it reckognizes '&'
210 #ifdef O_DRAW_NATIVE_API
211 int nPrevMode = SetBkMode(hdc, TRANSPARENT);
212 HBRUSH hbr = CreateSolidBrush(colBack),
213 hPrevBrush = (HBRUSH)SelectObject(hdc, hbr);
214
215 RECT rectFill = { rc.GetLeft(), rc.GetTop(), rc.GetRight()+1, rc.GetBottom() };
216
217 if ( st & wxODSelected && m_bmpChecked.Ok()) {
218 // only draw the highlight under the text, not under
219 // the bitmap or checkmark; leave a 1-pixel gap.
220 rectFill.left = GetMarginWidth() + 1;
221 }
222
223 FillRect(hdc, &rectFill, hbr);
224
225 DeleteObject(hbr);
226
227 // use default font if no font set
228 HFONT hfont;
229 if ( m_font.Ok() ) {
230 m_font.RealizeResource();
231 hfont = (HFONT)m_font.GetResourceHandle();
232 }
233 else {
234 hfont = (HFONT)::GetStockObject(SYSTEM_FONT);
235 }
236
237 HFONT hPrevFont = (HFONT) ::SelectObject(hdc, hfont);
238
239 wxString strStrippedName = wxStripMenuCodes(m_strName);
240
241 ::DrawState(hdc, NULL, NULL,
242 (LPARAM)strStrippedName.c_str(), strStrippedName.length(),
243 x, rc.y + 1, rc.GetWidth(), rc.GetHeight(),
244 DST_PREFIXTEXT | (st & wxODDisabled ? DSS_DISABLED : 0));
245
246 if ( !m_strAccel.empty() )
247 {
248 RECT r;
249 r.top = rc.GetTop() + 1;
250 r.left = rc.GetLeft();
251 r.right = rc.GetRight() - 16;
252 r.bottom = rc.GetBottom();
253
254 DrawText(hdc, m_strAccel, m_strAccel.length(), &r,
255 DT_SINGLELINE | DT_RIGHT);
256 }
257
258 (void)SelectObject(hdc, hPrevBrush);
259 (void)SelectObject(hdc, hPrevFont);
260 (void)SetBkMode(hdc, nPrevMode);
261 #else
262 dc.SetFont(GetFont());
263 dc.DrawText(wxStripMenuCodes(m_strName), x, rc.y);
264 #endif //O_DRAW_NATIVE_API
265
266 // draw the bitmap
267 // ---------------
268 if ( IsCheckable() && !m_bmpChecked.Ok() ) {
269 if ( st & wxODChecked ) {
270 // using native APIs for performance and simplicity
271#ifdef O_DRAW_NATIVE_API
272 // what goes on: DrawFrameControl creates a b/w mask,
273 // then we copy it to screen to have right colors
274
275 // first create a monochrome bitmap in a memory DC
276 HDC hdcMem = CreateCompatibleDC(hdc);
277 HBITMAP hbmpCheck = CreateBitmap(GetMarginWidth(), m_nHeight, 1, 1, 0);
278 SelectObject(hdcMem, hbmpCheck);
279
280 // then draw a check mark into it
281 RECT rect = { 0, 0, GetMarginWidth(), m_nHeight };
282 if ( m_nHeight > 0 )
283 {
284 ::DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
285 }
286
287 // finally copy it to screen DC and clean up
288 BitBlt(hdc, rc.x, rc.y, GetMarginWidth(), m_nHeight,
289 hdcMem, 0, 0, SRCCOPY);
290
291 DeleteDC(hdcMem);
292 DeleteObject(hbmpCheck);
293#else
294 // #### to do: perhaps using Marlett font (create equiv. font under X)
295// wxFAIL("not implemented");
296#endif //O_DRAW_NATIVE_API
297 }
298 }
299 else {
300 // for uncheckable item we use only the 'checked' bitmap
301 wxBitmap bmp(GetBitmap(IsCheckable() ? ((st & wxODChecked) != 0) : TRUE));
302 if ( bmp.Ok() ) {
303 wxMemoryDC dcMem(&dc);
304 dcMem.SelectObject(bmp);
305
306 // center bitmap
307 int nBmpWidth = bmp.GetWidth(),
308 nBmpHeight = bmp.GetHeight();
309
310 // there should be enough place!
311 wxASSERT((nBmpWidth <= rc.GetWidth()) && (nBmpHeight <= rc.GetHeight()));
312
313 int heightDiff = m_nHeight - nBmpHeight;
314 dc.Blit(rc.x + (GetMarginWidth() - nBmpWidth) / 2,
315 rc.y + heightDiff / 2,
316 nBmpWidth, nBmpHeight,
317 &dcMem, 0, 0, wxCOPY, TRUE /* use mask */);
318
319 if ( st & wxODSelected ) {
320
321 int x1, y1, x2, y2;
322 x1 = rc.x;
323 y1 = rc.y;
324 x2 = x1 + GetMarginWidth() - 1;
325 y2 = y1 + m_nHeight - 1;
326
327 dc.SetPen(*wxWHITE_PEN);
328 dc.DrawLine(x1, y1, x2, y1);
329 dc.DrawLine(x1, y1, x1, y2);
330 dc.SetPen(*wxGREY_PEN);
331 dc.DrawLine(x1, y2-1, x2, y2-1);
332 dc.DrawLine(x2, y1, x2, y2);
333 }
334 }
335 }
336
337 #ifdef O_DRAW_NATIVE_API
338 ::SetTextColor(hdc, colOldText);
339 ::SetBkColor(hdc, colOldBack);
340
341 #undef hdc
342 #endif //O_DRAW_NATIVE_API
343
344 return TRUE;
345}
346
347
348#endif // wxUSE_OWNER_DRAWN
349