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