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