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