]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/ownerdrw.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/ownerdrw.cpp
3 // Purpose: implementation of wxOwnerDrawn class
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
16 #include "wx/window.h"
17 #include "wx/os2/private.h"
19 #include "wx/bitmap.h"
20 #include "wx/dcmemory.h"
27 #include "wx/settings.h"
28 #include "wx/ownerdrw.h"
29 #include "wx/menuitem.h"
32 // ============================================================================
33 // implementation of wxOwnerDrawn class
34 // ============================================================================
40 wxOwnerDrawn::wxOwnerDrawn( const wxString
& rsStr
,
42 bool WXUNUSED(bMenuItem
) )
45 m_bCheckable
= bCheckable
;
46 m_bOwnerDrawn
= FALSE
;
48 m_nMarginWidth
= ms_nLastMarginWidth
;
50 m_font
= *wxNORMAL_FONT
;
51 } // end of wxOwnerDrawn::wxOwnerDrawn
53 wxOwnerDrawn::~wxOwnerDrawn() { }
55 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 15;
57 size_t wxOwnerDrawn::ms_nLastMarginWidth
= ms_nDefaultMarginWidth
;
64 bool wxOwnerDrawn::OnMeasureItem( size_t* pWidth
,
69 wxString sStr
= wxStripMenuCodes(m_strName
);
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() )
77 sStr
.Pad(sStr
.Length()%8
);
80 vDC
.SetFont(GetFont());
81 vDC
.GetTextExtent( sStr
85 if (!m_strAccel
.empty())
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)
96 vDC
.GetTextExtent( m_strAccel
100 *pWidth
+= nAccelWidth
;
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
107 *pWidth
= (size_t)(*pWidth
+ GetDefaultMarginWidth() * 1.5);
110 // JACS: items still look too tightly packed, so adding 5 pixels.
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.
121 if (m_bmpChecked
.Ok())
124 // Is BMP height larger then text height?
126 size_t nAdjustedHeight
= m_bmpChecked
.GetHeight() +
127 wxSystemSettings::GetMetric(wxSYS_EDGE_Y
);
128 if (*pHeight
< nAdjustedHeight
)
129 *pHeight
= nAdjustedHeight
;
132 // Does BMP encroach on default check menu position?
134 size_t nAdjustedWidth
= m_bmpChecked
.GetWidth() +
135 (wxSystemSettings::GetMetric(wxSYS_EDGE_X
) * 2);
138 // Do we need to widen margin to fit BMP?
140 if ((size_t)GetMarginWidth() < nAdjustedWidth
)
141 SetMarginWidth(nAdjustedWidth
);
144 // Add the size of the bitmap to our total size...
146 *pWidth
+= GetMarginWidth();
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...
153 *pWidth
+= GetMarginWidth();
156 // Make sure that this item is at least as
157 // tall as the user's system settings specify
159 if (*pHeight
< m_nMinHeight
)
160 *pHeight
= m_nMinHeight
;
161 m_nHeight
= *pHeight
; // remember height for use in OnDrawItem
163 } // end of wxOwnerDrawn::OnMeasureItem
166 bool wxOwnerDrawn::OnDrawItem( wxDC
& rDC
,
172 // We do nothing on focus change
174 if (eAction
== wxODFocusChanged
)
178 // Select the font and draw the text
179 // ---------------------------------
183 HPS hPS
= rDC
.GetHPS();
187 RECTL vRect
= {rRect
.x
+ 4, rRect
.y
+ 1, rRect
.x
+ (rRect
.width
- 2), rRect
.y
+ rRect
.height
};
189 memset(&vCbnd
, 0, sizeof(CHARBUNDLE
));
192 // Use default font if no font set
196 m_font
.RealizeResource();
200 ::GpiSetCharSet(hPS
, LCID_DEFAULT
);
204 // Based on the status of the menu item, pick the right colors
206 if (eStatus
& wxODSelected
)
208 wxColour
vCol2(wxT("WHITE"));
209 vColBack
.Set( (unsigned char)0
212 ); // no dark blue in color table
215 else if (eStatus
& wxODDisabled
)
217 vRef
= (ULONG
)::WinQuerySysColor( HWND_DESKTOP
218 ,SYSCLR_MENU
// Light gray
221 vColBack
.Set( GetRValue(vRef
)
225 vRef
= (ULONG
)::WinQuerySysColor( HWND_DESKTOP
226 ,SYSCLR_MENUDISABLEDTEXT
// dark gray
229 vColText
.Set( GetRValue(vRef
)
237 // Fall back to default colors if none explicitly specified
239 vRef
= ::WinQuerySysColor( HWND_DESKTOP
240 ,SYSCLR_MENU
// we are using gray for all our window backgrounds in wxWidgets
243 vColBack
.Set( GetRValue(vRef
)
247 vRef
= ::WinQuerySysColor( HWND_DESKTOP
248 ,SYSCLR_WINDOWTEXT
// Black
251 vColText
.Set( GetRValue(vRef
)
257 rDC
.SetTextBackground(vColBack
);
258 rDC
.SetTextForeground(vColText
);
259 rDC
.SetBackgroundMode(wxTRANSPARENT
);
260 vCbnd
.lColor
= vColText
.GetPixel();
261 vCbnd
.lBackColor
= vColBack
.GetPixel();
264 ,CBB_BACK_COLOR
| CBB_COLOR
273 // Paint the background
275 ::WinFillRect(hPS
, &vRect
, vColBack
.GetPixel());
278 // Determine where to draw and leave space for a check-mark.
280 int nX
= rRect
.x
+ GetMarginWidth();
283 // Unfortunately, unlike Win32, PM has no owner drawn specific text
284 // drawing methods like ::DrawState that can cleanly handle accel
285 // mnemonics and deal, automatically, with various states, so we have
286 // to handle them ourselves. Notice Win32 can't handle \t in ownerdrawn
287 // strings either. We cannot handle mnemonics either. We display
288 // them, though, in the hope we can figure them out some day.
292 // Display main text and accel text separately to align better
294 wxString sTgt
= wxT("\t");
295 wxString sFullString
= m_strName
; // need to save the original text
301 bool bFoundMnemonic
= false;
302 bool bFoundAccel
= false;
305 // Deal with the tab, extracting the Accel text
307 nIndex
= sFullString
.Find(sTgt
.c_str());
311 sAccel
= sFullString
.Mid(nIndex
+ 1);
312 sFullString
.Remove(nIndex
);
316 // Deal with the mnemonic character
319 nIndex
= sFullString
.Find(sTgt
.c_str());
322 wxString sTmp
= sFullString
;
324 bFoundMnemonic
= true;
326 rDC
.GetTextExtent( sTmp
330 sTmp
= sFullString
[(size_t)(nIndex
+ 1)];
331 rDC
.GetTextExtent( sTmp
335 sFullString
.Replace(sTgt
.c_str(), wxEmptyString
, true);
339 // Draw the main item text sans the accel text
341 POINTL vPntStart
= {nX
, rRect
.y
+ 4};
342 ::GpiCharStringAt( rDC
.GetHPS()
344 ,sFullString
.length()
345 ,(PCH
)sFullString
.c_str()
350 // Underline the mnemonic -- still won't work, but at least it "looks" right
353 POINTL vPntEnd
= {nX
+ nWidth
+ nCharWidth
- 3, rRect
.y
+ 2}; //CharWidth is bit wide
355 vPntStart
.x
= nX
+ nWidth
- 1;
356 vPntStart
.y
= rRect
.y
+ 2; // Make it look pretty!
357 vPen
= wxPen(vColText
, 1, wxSOLID
); // Assuming we are always black
359 ::GpiMove(hPS
, &vPntStart
);
360 ::GpiLine(hPS
, &vPntEnd
);
364 // Now draw the accel text
371 rDC
.GetTextExtent( sAccel
376 // Back off the starting position from the right edge
378 vPntStart
.x
= rRect
.width
- (nWidth
+ 7);
379 vPntStart
.y
= rRect
.y
+ 4;
380 ::GpiCharStringAt( rDC
.GetHPS()
391 if (IsCheckable() && !m_bmpChecked
.Ok())
393 if (eStatus
& wxODChecked
)
396 HBITMAP hBmpCheck
= ::WinGetSysBitmap(HWND_DESKTOP
, SBMP_MENUCHECK
);
398 vRect
.xLeft
= rRect
.x
;
399 vRect
.xRight
= rRect
.x
+ GetMarginWidth();
400 vRect
.yBottom
= rRect
.y
;
401 vRect
.yTop
= rRect
.y
+ m_nHeight
- 3;
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
409 ,DBM_NORMAL
// draw normal size
416 // For uncheckable item we use only the 'checked' bitmap
418 wxBitmap
vBmp(GetBitmap(IsCheckable() ? ((eStatus
& wxODChecked
) != 0) : TRUE
));
423 wxMemoryDC
vDCMem(&rDC
);
424 wxMemoryDC
* pOldDC
= (wxMemoryDC
*)vBmp
.GetSelectedInto();
428 vBmp
.SetSelectedInto(NULL
);
430 vDCMem
.SelectObject(vBmp
);
435 int nBmpWidth
= vBmp
.GetWidth();
436 int nBmpHeight
= vBmp
.GetHeight();
439 // There should be enough space!
441 wxASSERT((nBmpWidth
<= rRect
.width
) && (nBmpHeight
<= rRect
.height
));
443 int nHeightDiff
= m_nHeight
- nBmpHeight
;
445 rDC
.Blit( rRect
.x
+ (GetMarginWidth() - nBmpWidth
) / 2
446 ,rRect
.y
+ nHeightDiff
/ 2
456 if (eStatus
& wxODSelected
)
458 POINTL vPnt1
= {rRect
.x
+ 1, rRect
.y
+ 3}; // Leave a little background border
459 POINTL vPnt2
= {rRect
.x
+ GetMarginWidth(), rRect
.y
+ m_nHeight
- 3};
463 vLine
.lColor
= vColBack
.GetPixel();
470 ::GpiMove(hPS
, &vPnt1
);
478 vBmp
.SetSelectedInto(NULL
);
482 } // end of wxOwnerDrawn::OnDrawItem
484 #endif //wxUSE_OWNER_DRAWN