]>
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" |
2bda0e17 KB |
32 | #endif |
33 | ||
5100cabf | 34 | #include "wx/settings.h" |
2bda0e17 KB |
35 | #include "wx/ownerdrw.h" |
36 | #include "wx/menuitem.h" | |
d9cf726b | 37 | #include "wx/fontutil.h" |
2bda0e17 | 38 | |
1b24a68e GRG |
39 | #if wxUSE_OWNER_DRAWN |
40 | ||
2432b92d | 41 | |
2bda0e17 KB |
42 | // ============================================================================ |
43 | // implementation of wxOwnerDrawn class | |
44 | // ============================================================================ | |
45 | ||
46 | // ctor | |
47 | // ---- | |
33ac7e6f KB |
48 | wxOwnerDrawn::wxOwnerDrawn(const wxString& str, |
49 | bool bCheckable, bool WXUNUSED(bMenuItem)) | |
2bda0e17 KB |
50 | : m_strName(str) |
51 | { | |
2432b92d | 52 | #if defined(__WXMSW__) && defined(__WIN32__) && defined(SM_CXMENUCHECK) |
d9cf726b JS |
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; | |
2bda0e17 KB |
81 | #endif |
82 | ||
d9cf726b JS |
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 | ||
2bda0e17 KB |
96 | |
97 | // drawing | |
98 | // ------- | |
99 | ||
100 | // get size of the item | |
2a2a71e3 JS |
101 | // The item size includes the menu string, the accel string, |
102 | // the bitmap and size for a submenu expansion arrow... | |
c86f1403 | 103 | bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight) |
2bda0e17 KB |
104 | { |
105 | wxMemoryDC dc; | |
2bda0e17 | 106 | |
f2ab8671 | 107 | wxString str = wxStripMenuCodes(m_strName); |
2bda0e17 | 108 | |
2a2a71e3 JS |
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 | } | |
2bda0e17 | 117 | |
e7dd6ecd GT |
118 | if (m_font.Ok()) |
119 | dc.SetFont(GetFont()); | |
120 | ||
2bda0e17 | 121 | dc.GetTextExtent(str, (long *)pwidth, (long *)pheight); |
c7527e3f | 122 | |
5ac7be7a JS |
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); | |
2a2a71e3 | 132 | *pwidth += accel_width; |
5ac7be7a JS |
133 | } |
134 | ||
2a2a71e3 JS |
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 | |
4fa47e56 | 137 | *pwidth += (size_t) (GetDefaultMarginWidth() * 1.5); |
2a2a71e3 | 138 | |
d9cf726b JS |
139 | // JACS: items still look too tightly packed, so adding 5 pixels. |
140 | (*pheight) = (*pheight) + 5; | |
c7527e3f | 141 | |
e7dd6ecd GT |
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 | |
33ac7e6f | 144 | // not match the size expected by the system. This will |
e7dd6ecd GT |
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() + | |
a756f210 | 151 | wxSystemSettings::GetMetric(wxSYS_EDGE_Y); |
e7dd6ecd GT |
152 | if (*pheight < adjustedHeight) |
153 | *pheight = adjustedHeight; | |
33ac7e6f | 154 | |
e7dd6ecd GT |
155 | // Does BMP encroach on default check menu position? |
156 | size_t adjustedWidth = m_bmpChecked.GetWidth() + | |
a756f210 | 157 | (wxSystemSettings::GetMetric(wxSYS_EDGE_X) * 2); |
2a2a71e3 JS |
158 | // if (ms_nDefaultMarginWidth < adjustedWidth) |
159 | // *pwidth += adjustedWidth - ms_nDefaultMarginWidth; | |
33ac7e6f | 160 | |
e7dd6ecd | 161 | // Do we need to widen margin to fit BMP? |
d2ec53ea | 162 | if ((size_t)GetMarginWidth() != adjustedWidth) |
e7dd6ecd | 163 | SetMarginWidth(adjustedWidth); |
2a2a71e3 JS |
164 | |
165 | // add the size of the bitmap to our total size... | |
166 | *pwidth += GetMarginWidth(); | |
e7dd6ecd | 167 | } |
33ac7e6f | 168 | |
2a2a71e3 JS |
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 | ||
d9cf726b JS |
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 | ||
2bda0e17 KB |
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 | ||
c7527e3f JS |
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 | ||
57c208c5 | 191 | #if defined(__WIN32__) && !defined(__SC__) && !defined(__TWIN32__) |
2bda0e17 KB |
192 | #define O_DRAW_NATIVE_API // comments below explain why I use it |
193 | #endif | |
194 | ||
195 | // draw the item | |
6d5b2a57 VZ |
196 | bool wxOwnerDrawn::OnDrawItem(wxDC& dc, |
197 | const wxRect& rc, | |
198 | wxODAction act, | |
199 | wxODStatus st) | |
2bda0e17 KB |
200 | { |
201 | // we do nothing on focus change | |
202 | if ( act == wxODFocusChanged ) | |
203 | return TRUE; | |
204 | ||
205 | // wxColor <-> RGB | |
19193a2c | 206 | #define ToRGB(col) PALETTERGB(col.Red(), col.Green(), col.Blue()) |
2bda0e17 KB |
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); | |
5ac7be7a JS |
214 | if (!(st & wxODDisabled)) |
215 | { | |
216 | colText = GetSysColor(COLOR_HIGHLIGHTTEXT); | |
217 | } | |
218 | else | |
219 | { | |
220 | colText = GetSysColor(COLOR_GRAYTEXT); | |
221 | } | |
2bda0e17 KB |
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 | } | |
33ac7e6f | 228 | |
2bda0e17 KB |
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 | ||
d9cf726b | 241 | |
33ac7e6f | 242 | // determine where to draw and leave space for a check-mark. |
d9cf726b JS |
243 | // Add 3 pixel padding so text appears well within highlight rectangle |
244 | int x = rc.x + GetMarginWidth() + 3; | |
245 | ||
2bda0e17 | 246 | |
33ac7e6f | 247 | // using native API because it reckognizes '&' |
2bda0e17 KB |
248 | #ifdef O_DRAW_NATIVE_API |
249 | int nPrevMode = SetBkMode(hdc, TRANSPARENT); | |
07cf98cb VZ |
250 | HBRUSH hbr = CreateSolidBrush(colBack), |
251 | hPrevBrush = (HBRUSH)SelectObject(hdc, hbr); | |
2bda0e17 | 252 | |
d9cf726b JS |
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); | |
2bda0e17 KB |
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 | ||
c4e7c2aa | 273 | HFONT hPrevFont = (HFONT) ::SelectObject(hdc, hfont); |
f44b4495 | 274 | |
5ac7be7a | 275 | wxString strMenuText = m_strName.BeforeFirst('\t'); |
d9cf726b | 276 | |
2a2a71e3 JS |
277 | SIZE sizeRect; |
278 | GetTextExtentPoint32(hdc,strMenuText.c_str(), strMenuText.Length(),&sizeRect); | |
4fa47e56 | 279 | ::DrawState(hdc, NULL, NULL, |
5ac7be7a | 280 | (LPARAM)strMenuText.c_str(), strMenuText.length(), |
4fa47e56 | 281 | x, rc.y+( (int) ((rc.GetHeight()-sizeRect.cy)/2.0) )-1, // centre text vertically |
2a2a71e3 | 282 | rc.GetWidth()-GetMarginWidth(), sizeRect.cy, |
5ac7be7a JS |
283 | DST_PREFIXTEXT | |
284 | (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0)); | |
6d5b2a57 | 285 | |
136cb3c7 | 286 | if ( !m_strAccel.empty() ) |
6d5b2a57 | 287 | { |
2a2a71e3 JS |
288 | // right align accel string with right edge of menu ( offset by the margin width ) |
289 | ::SetTextAlign(hdc, TA_RIGHT); | |
4fa47e56 | 290 | ::DrawState(hdc, NULL, NULL, |
5ac7be7a | 291 | (LPARAM)m_strAccel.c_str(), m_strAccel.length(), |
4fa47e56 | 292 | rc.GetWidth()-(GetMarginWidth()), rc.y+(int) ((rc.GetHeight()-sizeRect.cy)/2.0), |
2a2a71e3 | 293 | rc.GetWidth()-GetMarginWidth(), sizeRect.cy, |
5ac7be7a JS |
294 | DST_TEXT | |
295 | (((st & wxODDisabled) && !(st & wxODSelected)) ? DSS_DISABLED : 0)); | |
2a2a71e3 | 296 | ::SetTextAlign(hdc, TA_LEFT); |
6d5b2a57 | 297 | } |
2bda0e17 KB |
298 | |
299 | (void)SelectObject(hdc, hPrevBrush); | |
300 | (void)SelectObject(hdc, hPrevFont); | |
301 | (void)SetBkMode(hdc, nPrevMode); | |
2b5f62a0 VZ |
302 | |
303 | DeleteObject(hbr); | |
2bda0e17 KB |
304 | #else |
305 | dc.SetFont(GetFont()); | |
d9cf726b | 306 | dc.DrawText(wxStripMenuCodes(m_strName), x, rc.y); |
2bda0e17 KB |
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 | |
5260b1c5 | 314 | #ifdef O_DRAW_NATIVE_API |
33ac7e6f | 315 | // what goes on: DrawFrameControl creates a b/w mask, |
2bda0e17 KB |
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 }; | |
07cf98cb VZ |
325 | if ( m_nHeight > 0 ) |
326 | { | |
f44b4495 | 327 | ::DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK); |
07cf98cb | 328 | } |
2bda0e17 KB |
329 | |
330 | // finally copy it to screen DC and clean up | |
33ac7e6f | 331 | BitBlt(hdc, rc.x, rc.y, GetMarginWidth(), m_nHeight, |
2bda0e17 | 332 | hdcMem, 0, 0, SRCCOPY); |
07cf98cb | 333 | |
2bda0e17 | 334 | DeleteDC(hdcMem); |
07cf98cb | 335 | DeleteObject(hbmpCheck); |
5260b1c5 | 336 | #else |
2bda0e17 | 337 | // #### to do: perhaps using Marlett font (create equiv. font under X) |
5260b1c5 JS |
338 | // wxFAIL("not implemented"); |
339 | #endif //O_DRAW_NATIVE_API | |
2bda0e17 KB |
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 | ||
f44b4495 | 356 | int heightDiff = m_nHeight - nBmpHeight; |
33ac7e6f KB |
357 | dc.Blit(rc.x + (GetMarginWidth() - nBmpWidth) / 2, |
358 | rc.y + heightDiff / 2, | |
359 | nBmpWidth, nBmpHeight, | |
f44b4495 | 360 | &dcMem, 0, 0, wxCOPY, TRUE /* use mask */); |
2bda0e17 KB |
361 | |
362 | if ( st & wxODSelected ) { | |
2a2a71e3 JS |
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 | |
d9cf726b JS |
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); | |
2a2a71e3 | 382 | #endif //O_DRAW_NATIVE_API |
2bda0e17 KB |
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 | ||
1b24a68e GRG |
397 | |
398 | #endif // wxUSE_OWNER_DRAWN | |
399 |