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