]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/os2/ownerdrw.cpp | |
3 | // Purpose: implementation of wxOwnerDrawn class | |
4 | // Author: David Webster | |
5 | // Modified by: | |
6 | // Created: 10/12/99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) David Webster | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #if wxUSE_OWNER_DRAWN | |
16 | ||
17 | #ifndef WX_PRECOMP | |
18 | #include "wx/window.h" | |
19 | #include "wx/os2/private.h" | |
20 | #include "wx/font.h" | |
21 | #include "wx/bitmap.h" | |
22 | #include "wx/dcmemory.h" | |
23 | #include "wx/menu.h" | |
24 | #include "wx/utils.h" | |
25 | #include "wx/settings.h" | |
26 | #include "wx/menuitem.h" | |
27 | #endif | |
28 | ||
29 | #include "wx/ownerdrw.h" | |
30 | #include "wx/os2/dcclient.h" | |
31 | ||
32 | // ============================================================================ | |
33 | // implementation of wxOwnerDrawn class | |
34 | // ============================================================================ | |
35 | ||
36 | // | |
37 | // ctor | |
38 | // ---- | |
39 | // | |
40 | wxOwnerDrawn::wxOwnerDrawn( const wxString& rsStr, | |
41 | bool bCheckable, | |
42 | bool WXUNUSED(bMenuItem) ) | |
43 | : m_strName(rsStr) | |
44 | { | |
45 | m_bCheckable = bCheckable; | |
46 | m_bOwnerDrawn = false; | |
47 | m_nHeight = 0; | |
48 | m_nMarginWidth = ms_nLastMarginWidth; | |
49 | if (wxNORMAL_FONT) | |
50 | m_font = *wxNORMAL_FONT; | |
51 | } // end of wxOwnerDrawn::wxOwnerDrawn | |
52 | ||
53 | wxOwnerDrawn::~wxOwnerDrawn() { } | |
54 | ||
55 | size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 15; | |
56 | ||
57 | size_t wxOwnerDrawn::ms_nLastMarginWidth = ms_nDefaultMarginWidth; | |
58 | ||
59 | // | |
60 | // Drawing | |
61 | // ------- | |
62 | // | |
63 | ||
64 | bool wxOwnerDrawn::OnMeasureItem( size_t* pWidth, | |
65 | size_t* pHeight ) | |
66 | { | |
67 | wxMemoryDC vDC; | |
68 | ||
69 | wxString sStr = wxStripMenuCodes(m_strName); | |
70 | ||
71 | // | |
72 | // If we have a valid accel string, then pad out | |
73 | // the menu string so that the menu and accel string are not | |
74 | // placed on top of each other. | |
75 | if (!m_strAccel.empty() ) | |
76 | { | |
77 | sStr.Pad(sStr.length()%8); | |
78 | sStr += m_strAccel; | |
79 | } | |
80 | vDC.SetFont(GetFont()); | |
81 | vDC.GetTextExtent( sStr | |
82 | ,(wxCoord *)pWidth | |
83 | ,(wxCoord *)pHeight | |
84 | ); | |
85 | if (!m_strAccel.empty()) | |
86 | { | |
87 | // | |
88 | // Measure the accelerator string, and add its width to | |
89 | // the total item width, plus 16 (Accelerators are right justified, | |
90 | // with the right edge of the text rectangle 16 pixels left of | |
91 | // the right edge of the menu) | |
92 | // | |
93 | int nAccelWidth; | |
94 | int nAccelHeight; | |
95 | ||
96 | vDC.GetTextExtent( m_strAccel | |
97 | ,&nAccelWidth | |
98 | ,&nAccelHeight | |
99 | ); | |
100 | *pWidth += nAccelWidth; | |
101 | } | |
102 | ||
103 | // | |
104 | // Add space at the end of the menu for the submenu expansion arrow. | |
105 | // This will also allow offsetting the accel string from the right edge | |
106 | // | |
107 | *pWidth = (size_t)(*pWidth + GetDefaultMarginWidth() * 1.5); | |
108 | ||
109 | // | |
110 | // JACS: items still look too tightly packed, so adding 5 pixels. | |
111 | // | |
112 | (*pHeight) += 5; | |
113 | ||
114 | // | |
115 | // Ray Gilbert's changes - Corrects the problem of a BMP | |
116 | // being placed next to text in a menu item, and the BMP does | |
117 | // not match the size expected by the system. This will | |
118 | // resize the space so the BMP will fit. Without this, BMPs | |
119 | // must be no larger or smaller than 16x16. | |
120 | // | |
121 | if (m_bmpChecked.Ok()) | |
122 | { | |
123 | // | |
124 | // Is BMP height larger then text height? | |
125 | // | |
126 | size_t nAdjustedHeight = m_bmpChecked.GetHeight() + | |
127 | wxSystemSettings::GetMetric(wxSYS_EDGE_Y); | |
128 | if (*pHeight < nAdjustedHeight) | |
129 | *pHeight = nAdjustedHeight; | |
130 | ||
131 | // | |
132 | // Does BMP encroach on default check menu position? | |
133 | // | |
134 | size_t nAdjustedWidth = m_bmpChecked.GetWidth() + | |
135 | (wxSystemSettings::GetMetric(wxSYS_EDGE_X) * 2); | |
136 | ||
137 | // | |
138 | // Do we need to widen margin to fit BMP? | |
139 | // | |
140 | if ((size_t)GetMarginWidth() < nAdjustedWidth) | |
141 | SetMarginWidth(nAdjustedWidth); | |
142 | ||
143 | // | |
144 | // Add the size of the bitmap to our total size... | |
145 | // | |
146 | *pWidth += GetMarginWidth(); | |
147 | } | |
148 | ||
149 | // | |
150 | // Add the size of the bitmap to our total size - even if we don't have | |
151 | // a bitmap we leave room for one... | |
152 | // | |
153 | *pWidth += GetMarginWidth(); | |
154 | ||
155 | // | |
156 | // Make sure that this item is at least as | |
157 | // tall as the user's system settings specify | |
158 | // | |
159 | const size_t heightStd = 6; // FIXME: get value from the system | |
160 | if ( *pHeight < heightStd ) | |
161 | *pHeight = heightStd; | |
162 | m_nHeight = *pHeight; // remember height for use in OnDrawItem | |
163 | return true; | |
164 | } // end of wxOwnerDrawn::OnMeasureItem | |
165 | ||
166 | // draw the item | |
167 | bool wxOwnerDrawn::OnDrawItem( wxDC& rDC, | |
168 | const wxRect& rRect, | |
169 | wxODAction eAction, | |
170 | wxODStatus eStatus ) | |
171 | { | |
172 | // | |
173 | // We do nothing on focus change | |
174 | // | |
175 | if (eAction == wxODFocusChanged ) | |
176 | return true; | |
177 | ||
178 | // | |
179 | // Select the font and draw the text | |
180 | // --------------------------------- | |
181 | // | |
182 | ||
183 | CHARBUNDLE vCbnd; | |
184 | wxPMDCImpl *impl = (wxPMDCImpl*) rDC.GetImpl(); | |
185 | HPS hPS= impl->GetHPS(); | |
186 | wxColour vColBack; | |
187 | wxColour vColText; | |
188 | COLORREF vRef; | |
189 | RECTL vRect = {rRect.x + 4, rRect.y + 1, rRect.x + (rRect.width - 2), rRect.y + rRect.height}; | |
190 | ||
191 | memset(&vCbnd, 0, sizeof(CHARBUNDLE)); | |
192 | ||
193 | // | |
194 | // Use default font if no font set | |
195 | // | |
196 | if (m_font.Ok()) | |
197 | { | |
198 | m_font.RealizeResource(); | |
199 | } | |
200 | else | |
201 | { | |
202 | ::GpiSetCharSet(hPS, LCID_DEFAULT); | |
203 | } | |
204 | ||
205 | // | |
206 | // Based on the status of the menu item, pick the right colors | |
207 | // | |
208 | if (eStatus & wxODSelected) | |
209 | { | |
210 | vRef = (ULONG)::WinQuerySysColor( HWND_DESKTOP | |
211 | ,SYSCLR_MENUHILITEBGND | |
212 | ,0L | |
213 | ); | |
214 | vColBack.Set( GetRValue(vRef) | |
215 | ,GetGValue(vRef) | |
216 | ,GetBValue(vRef) | |
217 | ); | |
218 | vRef = (ULONG)::WinQuerySysColor( HWND_DESKTOP | |
219 | ,SYSCLR_MENUHILITE | |
220 | ,0L | |
221 | ); | |
222 | vColText.Set( GetRValue(vRef) | |
223 | ,GetGValue(vRef) | |
224 | ,GetBValue(vRef) | |
225 | ); | |
226 | } | |
227 | else if (eStatus & wxODDisabled) | |
228 | { | |
229 | vRef = (ULONG)::WinQuerySysColor( HWND_DESKTOP | |
230 | ,SYSCLR_MENU // Light gray | |
231 | ,0L | |
232 | ); | |
233 | vColBack.Set( GetRValue(vRef) | |
234 | ,GetGValue(vRef) | |
235 | ,GetBValue(vRef) | |
236 | ); | |
237 | vRef = (ULONG)::WinQuerySysColor( HWND_DESKTOP | |
238 | ,SYSCLR_MENUDISABLEDTEXT // dark gray | |
239 | ,0L | |
240 | ); | |
241 | vColText.Set( GetRValue(vRef) | |
242 | ,GetGValue(vRef) | |
243 | ,GetBValue(vRef) | |
244 | ); | |
245 | } | |
246 | else | |
247 | { | |
248 | // | |
249 | // Fall back to default colors if none explicitly specified | |
250 | // | |
251 | vRef = ::WinQuerySysColor( HWND_DESKTOP | |
252 | ,SYSCLR_MENU // we are using gray for all our window backgrounds in wxWidgets | |
253 | ,0L | |
254 | ); | |
255 | vColBack.Set( GetRValue(vRef) | |
256 | ,GetGValue(vRef) | |
257 | ,GetBValue(vRef) | |
258 | ); | |
259 | vRef = ::WinQuerySysColor( HWND_DESKTOP | |
260 | ,SYSCLR_WINDOWTEXT // Black | |
261 | ,0L | |
262 | ); | |
263 | vColText.Set( GetRValue(vRef) | |
264 | ,GetGValue(vRef) | |
265 | ,GetBValue(vRef) | |
266 | ); | |
267 | } | |
268 | ||
269 | rDC.SetTextBackground(vColBack); | |
270 | rDC.SetTextForeground(vColText); | |
271 | rDC.SetBackgroundMode(wxTRANSPARENT); | |
272 | vCbnd.lColor = vColText.GetPixel(); | |
273 | vCbnd.lBackColor = vColBack.GetPixel(); | |
274 | ::GpiSetAttrs( hPS | |
275 | ,PRIM_CHAR | |
276 | ,CBB_BACK_COLOR | CBB_COLOR | |
277 | ,0 | |
278 | ,&vCbnd | |
279 | ); | |
280 | ::GpiSetBackMix( hPS | |
281 | ,BM_LEAVEALONE | |
282 | ); | |
283 | ||
284 | // | |
285 | // Paint the background | |
286 | // | |
287 | ::WinFillRect(hPS, &vRect, vColBack.GetPixel()); | |
288 | ||
289 | // | |
290 | // Determine where to draw and leave space for a check-mark. | |
291 | // | |
292 | int nX = rRect.x + GetMarginWidth(); | |
293 | ||
294 | // | |
295 | // Unfortunately, unlike Win32, PM has no owner drawn specific text | |
296 | // drawing methods like ::DrawState that can cleanly handle accel | |
297 | // mnemonics and deal, automatically, with various states, so we have | |
298 | // to handle them ourselves. Notice Win32 can't handle \t in ownerdrawn | |
299 | // strings either. We cannot handle mnemonics either. We display | |
300 | // them, though, in the hope we can figure them out some day. | |
301 | // | |
302 | ||
303 | // | |
304 | // Display main text and accel text separately to align better | |
305 | // | |
306 | wxString sTgt = wxT("\t"); | |
307 | wxString sFullString = m_strName; // need to save the original text | |
308 | wxString sAccel; | |
309 | int nIndex; | |
310 | size_t nWidth; | |
311 | size_t nCharWidth; | |
312 | size_t nHeight; | |
313 | bool bFoundMnemonic = false; | |
314 | bool bFoundAccel = false; | |
315 | ||
316 | // | |
317 | // Deal with the tab, extracting the Accel text | |
318 | // | |
319 | nIndex = sFullString.Find(sTgt); | |
320 | if (nIndex != -1) | |
321 | { | |
322 | bFoundAccel = true; | |
323 | sAccel = sFullString.Mid(nIndex + 1); | |
324 | sFullString.Remove(nIndex); | |
325 | } | |
326 | ||
327 | // | |
328 | // Deal with the mnemonic character | |
329 | // | |
330 | sTgt = wxT("~"); | |
331 | nIndex = sFullString.Find(sTgt); | |
332 | if (nIndex != -1) | |
333 | { | |
334 | wxString sTmp = sFullString; | |
335 | ||
336 | bFoundMnemonic = true; | |
337 | sTmp.Remove(nIndex); | |
338 | rDC.GetTextExtent( sTmp | |
339 | ,(wxCoord *)&nWidth | |
340 | ,(wxCoord *)&nHeight | |
341 | ); | |
342 | sTmp = sFullString[(size_t)(nIndex + 1)]; | |
343 | rDC.GetTextExtent( sTmp | |
344 | ,(wxCoord *)&nCharWidth | |
345 | ,(wxCoord *)&nHeight | |
346 | ); | |
347 | sFullString.Replace(sTgt.c_str(), wxEmptyString, true); | |
348 | } | |
349 | ||
350 | // | |
351 | // Draw the main item text sans the accel text | |
352 | // | |
353 | POINTL vPntStart = {nX, rRect.y + 4}; | |
354 | ::GpiCharStringAt( impl->GetHPS() | |
355 | ,&vPntStart | |
356 | ,sFullString.length() | |
357 | ,sFullString.char_str() | |
358 | ); | |
359 | if (bFoundMnemonic) | |
360 | { | |
361 | // | |
362 | // Underline the mnemonic -- still won't work, but at least it "looks" right | |
363 | // | |
364 | wxPen vPen; | |
365 | POINTL vPntEnd = {nX + nWidth + nCharWidth - 3, rRect.y + 2}; //CharWidth is bit wide | |
366 | ||
367 | vPntStart.x = nX + nWidth - 1; | |
368 | vPntStart.y = rRect.y + 2; // Make it look pretty! | |
369 | vPen = wxPen(vColText, 1, wxSOLID); // Assuming we are always black | |
370 | rDC.SetPen(vPen); | |
371 | ::GpiMove(hPS, &vPntStart); | |
372 | ::GpiLine(hPS, &vPntEnd); | |
373 | } | |
374 | ||
375 | // | |
376 | // Now draw the accel text | |
377 | // | |
378 | if (bFoundAccel) | |
379 | { | |
380 | size_t nWidth; | |
381 | size_t nHeight; | |
382 | ||
383 | rDC.GetTextExtent( sAccel | |
384 | ,(wxCoord *)&nWidth | |
385 | ,(wxCoord *)&nHeight | |
386 | ); | |
387 | // | |
388 | // Back off the starting position from the right edge | |
389 | // | |
390 | vPntStart.x = rRect.width - (nWidth + 7); | |
391 | vPntStart.y = rRect.y + 4; | |
392 | ::GpiCharStringAt( impl->GetHPS() | |
393 | ,&vPntStart | |
394 | ,sAccel.length() | |
395 | ,sAccel.char_str() | |
396 | ); | |
397 | } | |
398 | ||
399 | // | |
400 | // Draw the bitmap | |
401 | // --------------- | |
402 | // | |
403 | if (IsCheckable() && !m_bmpChecked.Ok()) | |
404 | { | |
405 | if (eStatus & wxODChecked) | |
406 | { | |
407 | RECTL vRect; | |
408 | HBITMAP hBmpCheck = ::WinGetSysBitmap(HWND_DESKTOP, SBMP_MENUCHECK); | |
409 | ||
410 | vRect.xLeft = rRect.x; | |
411 | vRect.xRight = rRect.x + GetMarginWidth(); | |
412 | vRect.yBottom = rRect.y; | |
413 | vRect.yTop = rRect.y + m_nHeight - 3; | |
414 | ||
415 | ::WinDrawBitmap( hPS // PS for this menuitem | |
416 | ,hBmpCheck // system checkmark | |
417 | ,NULL // draw the whole bitmap | |
418 | ,(PPOINTL)&vRect // destination -- bottom left corner of the menuitem area | |
419 | ,0L // ignored | |
420 | ,0L // draw a bitmap | |
421 | ,DBM_NORMAL // draw normal size | |
422 | ); | |
423 | } | |
424 | } | |
425 | else | |
426 | { | |
427 | // | |
428 | // For uncheckable item we use only the 'checked' bitmap | |
429 | // | |
430 | wxBitmap vBmp(GetBitmap(IsCheckable() ? ((eStatus & wxODChecked) != 0) : TRUE)); | |
431 | ||
432 | if (vBmp.Ok()) | |
433 | { | |
434 | ||
435 | wxMemoryDC vDCMem(&rDC); | |
436 | wxMemoryDC* pOldDC = (wxMemoryDC*)vBmp.GetSelectedInto(); | |
437 | ||
438 | if(pOldDC != NULL) | |
439 | { | |
440 | vBmp.SetSelectedInto(NULL); | |
441 | } | |
442 | vDCMem.SelectObject(vBmp); | |
443 | ||
444 | // | |
445 | // Center bitmap | |
446 | // | |
447 | int nBmpWidth = vBmp.GetWidth(); | |
448 | int nBmpHeight = vBmp.GetHeight(); | |
449 | ||
450 | // | |
451 | // There should be enough space! | |
452 | // | |
453 | wxASSERT((nBmpWidth <= rRect.width) && (nBmpHeight <= rRect.height)); | |
454 | ||
455 | int nHeightDiff = m_nHeight - nBmpHeight; | |
456 | ||
457 | rDC.Blit( rRect.x + (GetMarginWidth() - nBmpWidth) / 2 | |
458 | ,rRect.y + nHeightDiff / 2 | |
459 | ,nBmpWidth | |
460 | ,nBmpHeight | |
461 | ,&vDCMem | |
462 | ,0 | |
463 | ,0 | |
464 | ,wxCOPY | |
465 | ,true | |
466 | ); | |
467 | ||
468 | if (eStatus & wxODSelected) | |
469 | { | |
470 | POINTL vPnt1 = {rRect.x + 1, rRect.y + 3}; // Leave a little background border | |
471 | POINTL vPnt2 = {rRect.x + GetMarginWidth(), rRect.y + m_nHeight - 3}; | |
472 | ||
473 | LINEBUNDLE vLine; | |
474 | ||
475 | vLine.lColor = vColBack.GetPixel(); | |
476 | ::GpiSetAttrs( hPS | |
477 | ,PRIM_LINE | |
478 | ,LBB_COLOR | |
479 | ,0 | |
480 | ,&vLine | |
481 | ); | |
482 | ::GpiMove(hPS, &vPnt1); | |
483 | ::GpiBox( hPS | |
484 | ,DRO_OUTLINE | |
485 | ,&vPnt2 | |
486 | ,0L | |
487 | ,0L | |
488 | ); | |
489 | } | |
490 | vBmp.SetSelectedInto(NULL); | |
491 | } | |
492 | } | |
493 | return true; | |
494 | } // end of wxOwnerDrawn::OnDrawItem | |
495 | ||
496 | #endif //wxUSE_OWNER_DRAWN |