]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ownerdrw.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/ownerdrw.cpp
3 // Purpose: implementation of wxOwnerDrawn class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
24 #include "wx/window.h"
25 #include "wx/msw/private.h"
27 #include "wx/bitmap.h"
28 #include "wx/dcmemory.h"
33 #include "wx/ownerdrw.h"
34 #include "wx/menuitem.h"
37 // ============================================================================
38 // implementation of wxOwnerDrawn class
39 // ============================================================================
43 wxOwnerDrawn::wxOwnerDrawn(const wxString
& str
,
44 bool bCheckable
, bool bMenuItem
)
47 m_bCheckable
= bCheckable
;
48 m_bOwnerDrawn
= FALSE
;
50 m_nMarginWidth
= ms_nLastMarginWidth
;
53 #if defined(__WXMSW__) && defined(__WIN32__) && defined(SM_CXMENUCHECK)
54 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= GetSystemMetrics(SM_CXMENUCHECK
);
55 #else // # what is the reasonable default?
56 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 15;
59 size_t wxOwnerDrawn::ms_nLastMarginWidth
= ms_nDefaultMarginWidth
;
64 // get size of the item
65 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth
, size_t *pheight
)
68 dc
.SetFont(GetFont());
71 wxChar
*szStripped
= new wxChar
[m_strName
.Len()];
72 wxStripMenuCodes((wxChar
*)m_strName
.c_str(), szStripped
);
73 wxString str
= szStripped
;
76 // # without this menu items look too tightly packed (at least under Windows)
77 str
+= _T('W'); // 'W' is typically the widest letter
79 dc
.GetTextExtent(str
, (long *)pwidth
, (long *)pheight
);
81 // JACS: items still look too tightly packed, so adding 2 pixels.
82 (*pheight
) = (*pheight
) + 2;
84 m_nHeight
= *pheight
; // remember height for use in OnDrawItem
89 // searching for this macro you'll find all the code where I'm using the native
90 // Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
91 // port this code to X. (VZ)
93 // JACS: TODO. Why does a disabled but highlighted item still
94 // get drawn embossed? How can we tell DrawState that we don't want the
97 #if defined(__WIN32__) && !defined(__SC__) && !defined(__TWIN32__)
98 #define O_DRAW_NATIVE_API // comments below explain why I use it
102 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus st
)
104 // we do nothing on focus change
105 if ( act
== wxODFocusChanged
)
109 #define ToRGB(col) RGB(col.Red(), col.Green(), col.Blue())
110 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
114 DWORD colBack
, colText
;
115 if ( st
& wxODSelected
) {
116 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
117 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
120 // fall back to default colors if none explicitly specified
121 colBack
= m_colBack
.Ok() ? ToRGB(m_colBack
) : GetSysColor(COLOR_WINDOW
);
122 colText
= m_colText
.Ok() ? ToRGB(m_colText
) : GetSysColor(COLOR_WINDOWTEXT
);
125 #ifdef O_DRAW_NATIVE_API
126 #define hdc (HDC)dc.GetHDC()
127 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
128 colOldBack
= ::SetBkColor(hdc
, colBack
);
130 dc
.SetTextForeground(wxColor(UnRGB(colText
)));
131 dc
.SetTextBackground(wxColor(UnRGB(colBack
)));
134 // select the font and draw the text
135 // ---------------------------------
137 // determine where to draw and leave space for a check-mark.
138 int x
= rc
.x
+ GetMarginWidth();
140 // using native API because it reckognizes '&'
141 #ifdef O_DRAW_NATIVE_API
142 int nPrevMode
= SetBkMode(hdc
, TRANSPARENT
);
143 HBRUSH hbr
= CreateSolidBrush(colBack
),
144 hPrevBrush
= (HBRUSH
) SelectObject(hdc
, hbr
);
146 RECT rectAll
= { rc
.GetLeft(), rc
.GetTop(), rc
.GetRight(), rc
.GetBottom() };
147 FillRect(hdc
, &rectAll
, hbr
);
149 // use default font if no font set
152 m_font
.RealizeResource();
153 hfont
= (HFONT
)m_font
.GetResourceHandle();
156 hfont
= (HFONT
)::GetStockObject(SYSTEM_FONT
);
159 HFONT hPrevFont
= (HFONT
) ::SelectObject(hdc
, hfont
);
160 DrawState(hdc
, NULL
, NULL
,
161 (LPARAM
)(const wxChar
*)m_strName
, m_strName
.Length(),
162 x
, rc
.y
, rc
.GetWidth(), rc
.GetHeight(),
163 DST_PREFIXTEXT
| ( st
& wxODDisabled
? DSS_DISABLED
: 0) );
165 (void)SelectObject(hdc
, hPrevBrush
);
166 (void)SelectObject(hdc
, hPrevFont
);
167 (void)SetBkMode(hdc
, nPrevMode
);
169 dc
.SetFont(GetFont());
170 dc
.DrawText(m_strName
, x
, rc
.y
);
171 #endif //O_DRAW_NATIVE_API
175 if ( IsCheckable() && !m_bmpChecked
.Ok() ) {
176 if ( st
& wxODChecked
) {
177 // using native APIs for performance and simplicity
178 #ifdef O_DRAW_NATIVE_API
179 // what goes on: DrawFrameControl creates a b/w mask,
180 // then we copy it to screen to have right colors
182 // first create a monochrome bitmap in a memory DC
183 HDC hdcMem
= CreateCompatibleDC(hdc
);
184 HBITMAP hbmpCheck
= CreateBitmap(GetMarginWidth(), m_nHeight
, 1, 1, 0);
185 SelectObject(hdcMem
, hbmpCheck
);
187 // then draw a check mark into it
188 RECT rect
= { 0, 0, GetMarginWidth(), m_nHeight
};
190 DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
193 // finally copy it to screen DC and clean up
194 BitBlt(hdc
, rc
.x
, rc
.y
, GetMarginWidth(), m_nHeight
,
195 hdcMem
, 0, 0, SRCCOPY
);
198 // #### to do: perhaps using Marlett font (create equiv. font under X)
199 // wxFAIL("not implemented");
200 #endif //O_DRAW_NATIVE_API
204 // for uncheckable item we use only the 'checked' bitmap
205 wxBitmap
bmp(GetBitmap(IsCheckable() ? ((st
& wxODChecked
) != 0) : TRUE
));
207 wxMemoryDC
dcMem(&dc
);
208 dcMem
.SelectObject(bmp
);
211 int nBmpWidth
= bmp
.GetWidth(),
212 nBmpHeight
= bmp
.GetHeight();
214 // there should be enough place!
215 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
217 //MT: blit with mask enabled.
218 dc
.Blit(rc
.x
+ (GetMarginWidth() - nBmpWidth
) / 2,
219 rc
.y
+ (m_nHeight
- nBmpHeight
) /2,
220 nBmpWidth
, nBmpHeight
,
221 &dcMem
, 0, 0, wxCOPY
,true);
223 if ( st
& wxODSelected
) {
224 #ifdef O_DRAW_NATIVE_API
225 RECT rectBmp
= { rc
.GetLeft(), rc
.GetTop(),
226 rc
.GetLeft() + GetMarginWidth(),
227 rc
.GetTop() + m_nHeight
};
228 SetBkColor(hdc
, colBack
);
229 DrawEdge(hdc
, &rectBmp
, EDGE_RAISED
, BF_SOFT
| BF_RECT
);
231 // ## to write portable DrawEdge
232 #endif //O_DRAW_NATIVE_API
237 #ifdef O_DRAW_NATIVE_API
238 ::SetTextColor(hdc
, colOldText
);
239 ::SetBkColor(hdc
, colOldBack
);
242 #endif //O_DRAW_NATIVE_API