]> git.saurik.com Git - wxWidgets.git/blob - src/msw/ownerdrw.cpp
adding support for wxCB_SORT
[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 #ifndef SPI_GETKEYBOARDCUES
43 #define SPI_GETKEYBOARDCUES 0x100A
44 #endif
45
46 class wxMSWSystemMenuFontModule : public wxModule
47 {
48 public:
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);
57 SystemParametersInfo(SPI_GETNONCLIENTMETRICS,0,&nm,0);
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);
66
67 if (SystemParametersInfo(SPI_GETKEYBOARDCUES, 0, &ms_showCues, 0) == 0)
68 ms_showCues = true;
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
83 static bool ms_showCues;
84 private:
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
92 wxFont* wxMSWSystemMenuFontModule::ms_systemMenuFont = NULL;
93 int wxMSWSystemMenuFontModule::ms_systemMenuButtonWidth = 18; // windows clean install default
94 int wxMSWSystemMenuFontModule::ms_systemMenuHeight = 18; // windows clean install default
95 bool wxMSWSystemMenuFontModule::ms_showCues = true;
96
97 IMPLEMENT_DYNAMIC_CLASS(wxMSWSystemMenuFontModule, wxModule)
98
99 // ============================================================================
100 // implementation of wxOwnerDrawn class
101 // ============================================================================
102
103 // ctor
104 // ----
105 wxOwnerDrawn::wxOwnerDrawn(const wxString& str,
106 bool bCheckable, bool bMenuItem)
107 : m_strName(str)
108 {
109 // get the default menu height and font from the system
110 NONCLIENTMETRICS nm;
111 nm.cbSize = sizeof (NONCLIENTMETRICS);
112 SystemParametersInfo (SPI_GETNONCLIENTMETRICS,0,&nm,0);
113 m_nMinHeight = nm.iMenuHeight;
114
115 // nm.iMenuWidth is the system default for the width of
116 // menu icons and checkmarks
117 if (ms_nDefaultMarginWidth == 0)
118 {
119 ms_nDefaultMarginWidth = ::GetSystemMetrics(SM_CXMENUCHECK) + wxSystemSettings::GetMetric(wxSYS_EDGE_X);
120 ms_nLastMarginWidth = ms_nDefaultMarginWidth;
121 }
122
123 if (wxMSWSystemMenuFontModule::ms_systemMenuFont->Ok() && bMenuItem)
124 {
125 m_font = *wxMSWSystemMenuFontModule::ms_systemMenuFont;
126 }
127 else
128 {
129 m_font = *wxNORMAL_FONT;
130 }
131
132 m_bCheckable = bCheckable;
133 m_bOwnerDrawn = false;
134 m_nHeight = 0;
135 m_nMarginWidth = ms_nLastMarginWidth;
136 m_nMinHeight = wxMSWSystemMenuFontModule::ms_systemMenuHeight;
137
138 m_bmpDisabled = wxNullBitmap;
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,
144 // which is a chunk of code
145 size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 0;
146 size_t wxOwnerDrawn::ms_nLastMarginWidth = 0;
147
148
149 // drawing
150 // -------
151
152 // get size of the item
153 // The item size includes the menu string, the accel string,
154 // the bitmap and size for a submenu expansion arrow...
155 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight)
156 {
157 wxMemoryDC dc;
158
159 wxString str = wxStripMenuCodes(m_strName);
160
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 }
169
170 if (m_font.Ok())
171 dc.SetFont(GetFont());
172
173 dc.GetTextExtent(str, (long *)pwidth, (long *)pheight);
174
175 // increase size to accommodate bigger bitmaps if necessary
176 if (m_bmpChecked.Ok())
177 {
178 // Is BMP height larger then text height?
179 size_t adjustedHeight = m_bmpChecked.GetHeight() +
180 2*wxSystemSettings::GetMetric(wxSYS_EDGE_Y);
181 if (*pheight < adjustedHeight)
182 *pheight = adjustedHeight;
183
184 // Does BMP encroach on default check menu position?
185 size_t adjustedWidth = m_bmpChecked.GetWidth();
186
187 // Do we need to widen margin to fit BMP?
188 if ((size_t)GetMarginWidth() < adjustedWidth)
189 SetMarginWidth(adjustedWidth);
190 }
191
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
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
204 // remember height for use in OnDrawItem
205 m_nHeight = *pheight;
206
207 return true;
208 }
209
210 // draw the item
211 bool wxOwnerDrawn::OnDrawItem(wxDC& dc,
212 const wxRect& rc,
213 wxODAction act,
214 wxODStatus st)
215 {
216 // we do nothing on focus change
217 if ( act == wxODFocusChanged )
218 return true;
219
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
232 // set the colors
233 // --------------
234 DWORD colBack, colText;
235 if ( st & wxODSelected ) {
236 colBack = GetSysColor(COLOR_HIGHLIGHT);
237 if (!(st & wxODDisabled))
238 {
239 colText = GetSysColor(COLOR_HIGHLIGHTTEXT);
240 }
241 else
242 {
243 colText = GetSysColor(COLOR_GRAYTEXT);
244 }
245 }
246 else {
247 // fall back to default colors if none explicitly specified
248 colBack = m_colBack.Ok() ? wxColourToPalRGB(m_colBack)
249 : GetSysColor(COLOR_MENU);
250 colText = m_colText.Ok() ? wxColourToPalRGB(m_colText)
251 : GetSysColor(COLOR_MENUTEXT);
252 }
253
254
255 // don't draw an edge around the bitmap, if background is white ...
256 DWORD menu_bg_color = GetSysColor(COLOR_MENU);
257 if ( ( GetRValue( menu_bg_color ) >= 0xf0 &&
258 GetGValue( menu_bg_color ) >= 0xf0 &&
259 GetBValue( menu_bg_color ) >= 0xf0 )
260 )
261 {
262 draw_bitmap_edge = false;
263 }
264
265
266 HDC hdc = GetHdcOf(dc);
267 COLORREF colOldText = ::SetTextColor(hdc, colText),
268 colOldBack = ::SetBkColor(hdc, colBack);
269
270 // *2, as in wxSYS_EDGE_Y
271 int margin = GetMarginWidth() + 2 * wxSystemSettings::GetMetric(wxSYS_EDGE_X);
272
273 // select the font and draw the text
274 // ---------------------------------
275
276
277 // determine where to draw and leave space for a check-mark.
278 // + 1 pixel to separate the edge from the highlight rectangle
279 int xText = rc.x + margin + 1;
280
281
282 // using native API because it reckognizes '&'
283 int nPrevMode = SetBkMode(hdc, TRANSPARENT);
284 HBRUSH hbr = CreateSolidBrush(colBack),
285 hPrevBrush = (HBRUSH)SelectObject(hdc, hbr);
286
287 RECT rectFill = { rc.GetLeft(), rc.GetTop(),
288 rc.GetRight() + 1, rc.GetBottom() + 1 };
289
290 if ( (st & wxODSelected) && m_bmpChecked.Ok() && draw_bitmap_edge ) {
291 // only draw the highlight under the text, not under
292 // the bitmap or checkmark
293 rectFill.left = xText;
294 }
295
296 FillRect(hdc, &rectFill, hbr);
297
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 }
307
308 HFONT hPrevFont = (HFONT) ::SelectObject(hdc, hfont);
309
310 wxString strMenuText = m_strName.BeforeFirst('\t');
311
312 xText += 3; // separate text from the highlight rectangle
313
314 SIZE sizeRect;
315 GetTextExtentPoint32(hdc,strMenuText.c_str(), strMenuText.Length(),&sizeRect);
316 ::DrawState(hdc, NULL, NULL,
317 (LPARAM)strMenuText.c_str(), strMenuText.length(),
318 xText, rc.y + (int) ((rc.GetHeight()-sizeRect.cy)/2.0), // centre text vertically
319 rc.GetWidth()-margin, sizeRect.cy,
320 DST_PREFIXTEXT |
321 (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0) |
322 (((st & wxODHidePrefix) && !wxMSWSystemMenuFontModule::ms_showCues) ? 512 : 0)); // 512 == DSS_HIDEPREFIX
323
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(),
335 rc.GetWidth()-16-accel_width, rc.y+(int) ((rc.GetHeight()-sizeRect.cy)/2.0),
336 0, 0,
337 DST_TEXT |
338 (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0));
339 }
340
341 (void)SelectObject(hdc, hPrevBrush);
342 (void)SelectObject(hdc, hPrevFont);
343 (void)SetBkMode(hdc, nPrevMode);
344
345 DeleteObject(hbr);
346
347 // draw the bitmap
348 // ---------------
349 if ( IsCheckable() && !m_bmpChecked.Ok() ) {
350 if ( st & wxODChecked ) {
351 // what goes on: DrawFrameControl creates a b/w mask,
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);
356 HBITMAP hbmpCheck = CreateBitmap(margin, m_nHeight, 1, 1, 0);
357 SelectObject(hdcMem, hbmpCheck);
358
359 // then draw a check mark into it
360 RECT rect = { 0, 0, margin, m_nHeight };
361 if ( m_nHeight > 0 )
362 {
363 ::DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
364 }
365
366 // finally copy it to screen DC and clean up
367 BitBlt(hdc, rc.x, rc.y, margin, m_nHeight,
368 hdcMem, 0, 0, SRCCOPY);
369
370 DeleteDC(hdcMem);
371 DeleteObject(hbmpCheck);
372 }
373 }
374 else {
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
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
400 int heightDiff = m_nHeight - nBmpHeight;
401 dc.Blit(rc.x + (margin - nBmpWidth) / 2,
402 rc.y + heightDiff / 2,
403 nBmpWidth, nBmpHeight,
404 &dcMem, 0, 0, wxCOPY, true /* use mask */);
405
406 if ( ( st & wxODSelected ) && !( st & wxODDisabled ) && draw_bitmap_edge ) {
407 RECT rectBmp = { rc.GetLeft(), rc.GetTop(),
408 rc.GetLeft() + margin,
409 rc.GetTop() + m_nHeight };
410 SetBkColor(hdc, colBack);
411
412 DrawEdge(hdc, &rectBmp, BDR_RAISEDINNER, BF_RECT);
413 }
414 }
415 }
416
417 ::SetTextColor(hdc, colOldText);
418 ::SetBkColor(hdc, colOldBack);
419
420 return true;
421 }
422
423
424 #endif // wxUSE_OWNER_DRAWN
425