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