1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/ownerdrw.cpp
3 // Purpose: implementation of wxOwnerDrawn class
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
20 #include "wx/window.h"
21 #include "wx/msw/private.h"
23 #include "wx/bitmap.h"
24 #include "wx/dcmemory.h"
31 #include "wx/ownerdrw.h"
32 #include "wx/menuitem.h"
35 // ============================================================================
36 // implementation of wxOwnerDrawn class
37 // ============================================================================
43 wxOwnerDrawn
::wxOwnerDrawn(
50 m_bCheckable
= bCheckable
;
51 m_bOwnerDrawn
= FALSE
;
53 m_nMarginWidth
= ms_nLastMarginWidth
;
55 m_font
= *wxNORMAL_FONT
;
56 } // end of wxOwnerDrawn::wxOwnerDrawn
58 size_t wxOwnerDrawn
::ms_nDefaultMarginWidth
= 15;
60 size_t wxOwnerDrawn
::ms_nLastMarginWidth
= ms_nDefaultMarginWidth
;
67 bool wxOwnerDrawn
::OnMeasureItem(
74 vDC
.SetFont(GetFont());
76 wxString sStr
= wxStripMenuCodes(m_strName
);
81 nIndex
= sStr
.Find(sTgt
.c_str());
85 nIndex
= sStr
.Find(sTgt
.c_str());
87 sStr
.Replace(sTgt
.c_str(), "", TRUE
);
89 vDC
.GetTextExtent( sStr
94 (*pHeight
) = (*pHeight
) + 2;
95 m_nHeight
= *pHeight
; // remember height for use in OnDrawItem
97 } // end of wxOwnerDrawn::OnMeasureItem
100 bool wxOwnerDrawn
::OnDrawItem(
102 , const wxRect
& rRect
108 // We do nothing on focus change
109 if (eAction
== wxODFocusChanged
)
112 // WxWinGdi_CColour <-> RGB
114 #define ToRGB(col) OS2RGB(col.Red(), col.Green(), col.Blue())
115 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
117 CHARBUNDLE vCbndText
;
118 CHARBUNDLE vCbndBack
;
119 HPS hPS
= rDC
.GetHPS();
123 if (eStatus
& wxODSelected
)
125 lColBack
= (ULONG
)::WinQuerySysColor( HWND_DESKTOP
126 ,SYSCLR_MENUHILITEBGND
// Light gray
129 lColText
= (ULONG
)::WinQuerySysColor( HWND_DESKTOP
130 ,SYSCLR_MENUTEXT
// Black
134 else if (eStatus
& wxODDisabled
)
136 lColBack
= (ULONG
)::WinQuerySysColor( HWND_DESKTOP
137 ,SYSCLR_MENU
// Light gray
140 lColText
= (ULONG
)::WinQuerySysColor( HWND_DESKTOP
141 ,SYSCLR_MENUDISABLEDTEXT
// dark gray
148 // Fall back to default colors if none explicitly specified
150 lColBack
= m_colBack
.Ok() ?
ToRGB(m_colBack
) : ::WinQuerySysColor( HWND_DESKTOP
151 ,SYSCLR_MENU
// we are using gray for all our window backgrounds in wxWindows
154 lColText
= m_colText
.Ok() ?
ToRGB(m_colText
) : ::WinQuerySysColor( HWND_DESKTOP
155 ,SYSCLR_WINDOWTEXT
// Black
159 vCbndText
.lColor
= (LONG
)lColText
;
160 vCbndBack
.lColor
= (LONG
)lColBack
;
177 // Determine where to draw and leave space for a check-mark.
179 int nX
= rRect
.x
+ GetMarginWidth();
182 // Select the font and draw the text
183 // ---------------------------------
187 // Use default font if no font set
191 m_font
.RealizeResource();
195 ::GpiSetCharSet(hPS
, LCID_DEFAULT
);
199 // Unfortunately, unlike Win32, PM has no owner drawn specific text
200 // drawing methods like ::DrawState that can cleanly handle accel
201 // pneumonics and deal, automatically, with various states, so we have
202 // to handle them ourselves. Notice Win32 can't handle \t in ownerdrawn
206 // Manually replace the tab with spaces
208 wxString sTgt
= "\t";
209 wxString sReplace
= " ";
212 nIndex
= m_strName
.Find(sTgt
.c_str());
214 m_strName
.Replace(sTgt
.c_str(), sReplace
.c_str(), TRUE
);
216 nIndex
= m_strName
.Find(sTgt
.c_str());
218 m_strName
.Replace(sTgt
.c_str(), "", TRUE
);
220 rDC
.DrawText( m_strName
230 if (IsCheckable() && !m_bmpChecked
.Ok())
232 if (eStatus
& wxODChecked
)
235 HBITMAP hBmpCheck
= ::WinGetSysBitmap(HWND_DESKTOP
, SBMP_MENUCHECK
);
237 vRect
.xLeft
= rRect
.x
;
238 vRect
.xRight
= rRect
.x
+ GetMarginWidth();
239 vRect
.yBottom
= rRect
.y
;
240 vRect
.yTop
= rRect
.y
+ m_nHeight
;
242 ::WinDrawBitmap( hPS
// PS for this menuitem
243 ,hBmpCheck
// system checkmark
244 ,NULL
// draw the whole bitmap
245 ,(PPOINTL
)&vRect
// destination -- bottom left corner of the menuitem area
248 ,DBM_NORMAL
// draw normal size
255 // For uncheckable item we use only the 'checked' bitmap
257 wxBitmap
vBmp(GetBitmap(IsCheckable() ?
((eStatus
& wxODChecked
) != 0) : TRUE
));
261 wxMemoryDC
vDCMem(&rDC
);
263 vDCMem
.SelectObject(vBmp
);
268 int nBmpWidth
= vBmp
.GetWidth();
269 int nBmpHeight
= vBmp
.GetHeight();
272 // There should be enough space!
274 wxASSERT((nBmpWidth
<= rRect
.width
) && (nBmpHeight
<= rRect
.height
));
277 //MT: blit with mask enabled.
279 rDC
.Blit( rRect
.x
+ (GetMarginWidth() - nBmpWidth
) / 2
280 ,rRect
.y
+ (m_nHeight
- nBmpHeight
) /2
290 if (eStatus
& wxODSelected
)
292 RECT vRectBmp
= { rRect
.x
294 ,rRect
.x
+ GetMarginWidth()
299 vLine
.lColor
= lColBack
;
317 } // end of wxOwnerDrawn::OnDrawItem
319 #endif //wxUSE_OWNER_DRAWN