]> git.saurik.com Git - wxWidgets.git/blame - src/os2/ownerdrw.cpp
Fixed GetIcon to keep up with return type change on all other platforms.
[wxWidgets.git] / src / os2 / ownerdrw.cpp
CommitLineData
63415778
DW
1///////////////////////////////////////////////////////////////////////////////
2// Name: msw/ownerdrw.cpp
3// Purpose: implementation of wxOwnerDrawn class
4// Author: David Webster
fb46a9a6 5// Modified by:
63415778
DW
6// Created: 10/12/99
7// RCS-ID: $Id$
8// Copyright: (c) David Webster
65571936 9// Licence: wxWindows licence
63415778
DW
10///////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifndef WX_PRECOMP
16 #include "wx/window.h"
2b5f62a0 17 #include "wx/os2/private.h"
63415778
DW
18 #include "wx/font.h"
19 #include "wx/bitmap.h"
20 #include "wx/dcmemory.h"
21 #include "wx/menu.h"
22 #include "wx/utils.h"
23#endif
24
7e99520b
DW
25#if wxUSE_OWNER_DRAWN
26
50528a1d 27#include "wx/settings.h"
63415778
DW
28#include "wx/ownerdrw.h"
29#include "wx/menuitem.h"
30
31
32// ============================================================================
33// implementation of wxOwnerDrawn class
34// ============================================================================
35
402e2f7c 36//
63415778
DW
37// ctor
38// ----
402e2f7c 39//
6670f564
WS
40wxOwnerDrawn::wxOwnerDrawn( const wxString& rsStr,
41 bool bCheckable,
42 bool WXUNUSED(bMenuItem) )
402e2f7c 43: m_strName(rsStr)
63415778 44{
402e2f7c
DW
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
63415778 52
490120b4
SN
53wxOwnerDrawn::~wxOwnerDrawn() { }
54
402e2f7c 55size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 15;
63415778
DW
56
57size_t wxOwnerDrawn::ms_nLastMarginWidth = ms_nDefaultMarginWidth;
58
402e2f7c
DW
59//
60// Drawing
63415778 61// -------
402e2f7c 62//
63415778 63
6670f564
WS
64bool wxOwnerDrawn::OnMeasureItem( size_t* pWidth,
65 size_t* pHeight )
63415778 66{
6670f564 67 wxMemoryDC vDC;
402e2f7c 68
6670f564 69 wxString sStr = wxStripMenuCodes(m_strName);
402e2f7c 70
57ff8a87
DW
71 //
72 // If we have a valid accel string, then pad out
d6922577
JS
73 // the menu string so that the menu and accel string are not
74 // placed on top of each other.
57ff8a87
DW
75 if (!m_strAccel.empty() )
76 {
77 sStr.Pad(sStr.Length()%8);
78 sStr += m_strAccel;
79 }
80 vDC.SetFont(GetFont());
402e2f7c
DW
81 vDC.GetTextExtent( sStr
82 ,(long *)pWidth
83 ,(long *)pHeight
84 );
6670f564 85 if (!m_strAccel.empty())
57ff8a87
DW
86 {
87 //
d6922577 88 // Measure the accelerator string, and add its width to
57ff8a87
DW
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 //
d6922577
JS
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
57ff8a87 106 //
9923c37d 107 *pWidth = (size_t)(*pWidth + GetDefaultMarginWidth() * 1.5);
57ff8a87
DW
108
109 //
110 // JACS: items still look too tightly packed, so adding 5 pixels.
111 //
112 (*pHeight) += 5;
402e2f7c 113
57ff8a87
DW
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 if (*pHeight < m_nMinHeight)
160 *pHeight = m_nMinHeight;
161 m_nHeight = *pHeight; // remember height for use in OnDrawItem
6670f564 162 return true;
402e2f7c 163} // end of wxOwnerDrawn::OnMeasureItem
63415778 164
63415778 165// draw the item
6670f564
WS
166bool wxOwnerDrawn::OnDrawItem( wxDC& rDC,
167 const wxRect& rRect,
168 wxODAction eAction,
169 wxODStatus eStatus )
63415778 170{
402e2f7c 171 //
23122f8c 172 // We do nothing on focus change
7172b423 173 //
23122f8c 174 if (eAction == wxODFocusChanged )
6670f564 175 return true;
7172b423 176
402e2f7c 177 //
7172b423
DW
178 // Select the font and draw the text
179 // ---------------------------------
402e2f7c 180 //
402e2f7c 181
7172b423 182 CHARBUNDLE vCbnd;
402e2f7c 183 HPS hPS= rDC.GetHPS();
7172b423
DW
184 wxColour vColBack;
185 wxColour vColText;
186 COLORREF vRef;
5afb9458 187 RECTL vRect = {rRect.x + 4, rRect.y + 1, rRect.x + (rRect.width - 2), rRect.y + rRect.height};
402e2f7c 188
ad7f3189
DW
189 memset(&vCbnd, 0, sizeof(CHARBUNDLE));
190
7172b423
DW
191 //
192 // Use default font if no font set
193 //
194 if (m_font.Ok())
195 {
196 m_font.RealizeResource();
197 }
198 else
199 {
200 ::GpiSetCharSet(hPS, LCID_DEFAULT);
201 }
5afb9458
DW
202
203 //
d6922577 204 // Based on the status of the menu item, pick the right colors
5afb9458 205 //
402e2f7c
DW
206 if (eStatus & wxODSelected)
207 {
0fba44b4 208 wxColour vCol2(wxT("WHITE"));
5afb9458
DW
209 vColBack.Set( (unsigned char)0
210 ,(unsigned char)0
211 ,(unsigned char)160
212 ); // no dark blue in color table
213 vColText = vCol2;
402e2f7c
DW
214 }
215 else if (eStatus & wxODDisabled)
216 {
7172b423
DW
217 vRef = (ULONG)::WinQuerySysColor( HWND_DESKTOP
218 ,SYSCLR_MENU // Light gray
219 ,0L
220 );
221 vColBack.Set( GetRValue(vRef)
222 ,GetGValue(vRef)
223 ,GetBValue(vRef)
224 );
225 vRef = (ULONG)::WinQuerySysColor( HWND_DESKTOP
226 ,SYSCLR_MENUDISABLEDTEXT // dark gray
227 ,0L
228 );
229 vColText.Set( GetRValue(vRef)
230 ,GetGValue(vRef)
231 ,GetBValue(vRef)
232 );
402e2f7c
DW
233 }
234 else
235 {
236 //
237 // Fall back to default colors if none explicitly specified
238 //
7172b423 239 vRef = ::WinQuerySysColor( HWND_DESKTOP
77ffb593 240 ,SYSCLR_MENU // we are using gray for all our window backgrounds in wxWidgets
7172b423
DW
241 ,0L
242 );
243 vColBack.Set( GetRValue(vRef)
244 ,GetGValue(vRef)
245 ,GetBValue(vRef)
246 );
247 vRef = ::WinQuerySysColor( HWND_DESKTOP
248 ,SYSCLR_WINDOWTEXT // Black
249 ,0L
250 );
251 vColText.Set( GetRValue(vRef)
252 ,GetGValue(vRef)
253 ,GetBValue(vRef)
254 );
402e2f7c 255 }
ad7f3189 256
5afb9458
DW
257 rDC.SetTextBackground(vColBack);
258 rDC.SetTextForeground(vColText);
259 rDC.SetBackgroundMode(wxTRANSPARENT);
ad7f3189
DW
260 vCbnd.lColor = vColText.GetPixel();
261 vCbnd.lBackColor = vColBack.GetPixel();
262 ::GpiSetAttrs( hPS
263 ,PRIM_CHAR
264 ,CBB_BACK_COLOR | CBB_COLOR
265 ,0
266 ,&vCbnd
267 );
268 ::GpiSetBackMix( hPS
269 ,BM_LEAVEALONE
270 );
7172b423 271
5afb9458
DW
272 //
273 // Paint the background
274 //
275 ::WinFillRect(hPS, &vRect, vColBack.GetPixel());
402e2f7c 276
402e2f7c
DW
277 //
278 // Determine where to draw and leave space for a check-mark.
279 //
280 int nX = rRect.x + GetMarginWidth();
281
402e2f7c
DW
282 //
283 // Unfortunately, unlike Win32, PM has no owner drawn specific text
284 // drawing methods like ::DrawState that can cleanly handle accel
d6922577 285 // mnemonics and deal, automatically, with various states, so we have
402e2f7c 286 // to handle them ourselves. Notice Win32 can't handle \t in ownerdrawn
d6922577
JS
287 // strings either. We cannot handle mnemonics either. We display
288 // them, though, in the hope we can figure them out some day.
5afb9458 289 //
402e2f7c 290
23122f8c 291 //
d6922577 292 // Display main text and accel text separately to align better
23122f8c 293 //
6670f564
WS
294 wxString sTgt = wxT("\t");
295 wxString sFullString = m_strName; // need to save the original text
296 wxString sAccel;
297 int nIndex;
298 size_t nWidth;
299 size_t nCharWidth;
300 size_t nHeight;
301 bool bFoundMnemonic = false;
302 bool bFoundAccel = false;
23122f8c 303
5afb9458
DW
304 //
305 // Deal with the tab, extracting the Accel text
306 //
307 nIndex = sFullString.Find(sTgt.c_str());
23122f8c 308 if (nIndex != -1)
5afb9458 309 {
6670f564 310 bFoundAccel = true;
5afb9458
DW
311 sAccel = sFullString.Mid(nIndex + 1);
312 sFullString.Remove(nIndex);
313 }
314
315 //
d6922577 316 // Deal with the mnemonic character
5afb9458 317 //
0fba44b4 318 sTgt = wxT("~");
5afb9458 319 nIndex = sFullString.Find(sTgt.c_str());
23122f8c 320 if (nIndex != -1)
5afb9458 321 {
6670f564 322 wxString sTmp = sFullString;
5afb9458 323
6670f564 324 bFoundMnemonic = true;
5afb9458
DW
325 sTmp.Remove(nIndex);
326 rDC.GetTextExtent( sTmp
327 ,(long *)&nWidth
328 ,(long *)&nHeight
329 );
9923c37d 330 sTmp = sFullString[(size_t)(nIndex + 1)];
5afb9458
DW
331 rDC.GetTextExtent( sTmp
332 ,(long *)&nCharWidth
333 ,(long *)&nHeight
334 );
6670f564 335 sFullString.Replace(sTgt.c_str(), wxEmptyString, true);
5afb9458
DW
336 }
337
338 //
339 // Draw the main item text sans the accel text
ad7f3189
DW
340 //
341 POINTL vPntStart = {nX, rRect.y + 4};
342 ::GpiCharStringAt( rDC.GetHPS()
343 ,&vPntStart
344 ,sFullString.length()
345 ,(PCH)sFullString.c_str()
346 );
d6922577 347 if (bFoundMnemonic)
5afb9458
DW
348 {
349 //
d6922577 350 // Underline the mnemonic -- still won't work, but at least it "looks" right
5afb9458
DW
351 //
352 wxPen vPen;
5afb9458
DW
353 POINTL vPntEnd = {nX + nWidth + nCharWidth - 3, rRect.y + 2}; //CharWidth is bit wide
354
ad7f3189
DW
355 vPntStart.x = nX + nWidth - 1;
356 vPntStart.y = rRect.y + 2; // Make it look pretty!
5afb9458
DW
357 vPen = wxPen(vColText, 1, wxSOLID); // Assuming we are always black
358 rDC.SetPen(vPen);
359 ::GpiMove(hPS, &vPntStart);
360 ::GpiLine(hPS, &vPntEnd);
361 }
23122f8c 362
5afb9458
DW
363 //
364 // Now draw the accel text
365 //
366 if (bFoundAccel)
367 {
368 size_t nWidth;
369 size_t nHeight;
7172b423 370
5afb9458
DW
371 rDC.GetTextExtent( sAccel
372 ,(long *)&nWidth
373 ,(long *)&nHeight
374 );
375 //
376 // Back off the starting position from the right edge
377 //
ad7f3189
DW
378 vPntStart.x = rRect.width - (nWidth + 7);
379 vPntStart.y = rRect.y + 4;
380 ::GpiCharStringAt( rDC.GetHPS()
381 ,&vPntStart
382 ,sAccel.length()
383 ,(PCH)sAccel.c_str()
384 );
5afb9458 385 }
402e2f7c
DW
386
387 //
388 // Draw the bitmap
389 // ---------------
390 //
391 if (IsCheckable() && !m_bmpChecked.Ok())
392 {
393 if (eStatus & wxODChecked)
394 {
395 RECTL vRect;
396 HBITMAP hBmpCheck = ::WinGetSysBitmap(HWND_DESKTOP, SBMP_MENUCHECK);
397
398 vRect.xLeft = rRect.x;
399 vRect.xRight = rRect.x + GetMarginWidth();
400 vRect.yBottom = rRect.y;
1159a76f 401 vRect.yTop = rRect.y + m_nHeight - 3;
402e2f7c
DW
402
403 ::WinDrawBitmap( hPS // PS for this menuitem
404 ,hBmpCheck // system checkmark
405 ,NULL // draw the whole bitmap
406 ,(PPOINTL)&vRect // destination -- bottom left corner of the menuitem area
407 ,0L // ignored
408 ,0L // draw a bitmap
409 ,DBM_NORMAL // draw normal size
410 );
411 }
63415778 412 }
402e2f7c
DW
413 else
414 {
415 //
416 // For uncheckable item we use only the 'checked' bitmap
417 //
6670f564 418 wxBitmap vBmp(GetBitmap(IsCheckable() ? ((eStatus & wxODChecked) != 0) : TRUE));
402e2f7c
DW
419
420 if (vBmp.Ok())
421 {
ad7f3189 422
402e2f7c 423 wxMemoryDC vDCMem(&rDC);
ad7f3189 424 wxMemoryDC* pOldDC = (wxMemoryDC*)vBmp.GetSelectedInto();
402e2f7c 425
ad7f3189
DW
426 if(pOldDC != NULL)
427 {
428 vBmp.SetSelectedInto(NULL);
429 }
402e2f7c
DW
430 vDCMem.SelectObject(vBmp);
431
432 //
433 // Center bitmap
434 //
435 int nBmpWidth = vBmp.GetWidth();
436 int nBmpHeight = vBmp.GetHeight();
437
438 //
439 // There should be enough space!
440 //
441 wxASSERT((nBmpWidth <= rRect.width) && (nBmpHeight <= rRect.height));
442
b63b737d
DW
443 int nHeightDiff = m_nHeight - nBmpHeight;
444
402e2f7c 445 rDC.Blit( rRect.x + (GetMarginWidth() - nBmpWidth) / 2
b63b737d 446 ,rRect.y + nHeightDiff / 2
402e2f7c
DW
447 ,nBmpWidth
448 ,nBmpHeight
449 ,&vDCMem
450 ,0
451 ,0
452 ,wxCOPY
6670f564 453 ,true
402e2f7c
DW
454 );
455
456 if (eStatus & wxODSelected)
457 {
2b7cd532 458 POINTL vPnt1 = {rRect.x + 1, rRect.y + 3}; // Leave a little background border
1159a76f 459 POINTL vPnt2 = {rRect.x + GetMarginWidth(), rRect.y + m_nHeight - 3};
2b7cd532 460
402e2f7c
DW
461 LINEBUNDLE vLine;
462
5afb9458 463 vLine.lColor = vColBack.GetPixel();
402e2f7c
DW
464 ::GpiSetAttrs( hPS
465 ,PRIM_LINE
466 ,LBB_COLOR
467 ,0
468 ,&vLine
469 );
1159a76f 470 ::GpiMove(hPS, &vPnt1);
402e2f7c
DW
471 ::GpiBox( hPS
472 ,DRO_OUTLINE
1159a76f 473 ,&vPnt2
402e2f7c
DW
474 ,0L
475 ,0L
476 );
477 }
ad7f3189 478 vBmp.SetSelectedInto(NULL);
402e2f7c 479 }
63415778 480 }
6670f564 481 return true;
23122f8c 482} // end of wxOwnerDrawn::OnDrawItem
63415778 483
7e99520b 484#endif //wxUSE_OWNER_DRAWN