]>
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"
18 #include "wx/msw/private.h"
25 #include "wx/window.h"
26 // #include "wx/msw/private.h"
28 #include "wx/bitmap.h"
29 #include "wx/dcmemory.h"
34 #include "wx/ownerdrw.h"
35 #include "wx/menuitem.h"
40 // ============================================================================
41 // implementation of wxOwnerDrawn class
42 // ============================================================================
46 wxOwnerDrawn::wxOwnerDrawn(const wxString
& str
,
47 bool bCheckable
, bool bMenuItem
)
50 m_bCheckable
= bCheckable
;
51 m_bOwnerDrawn
= FALSE
;
53 m_nMarginWidth
= ms_nLastMarginWidth
;
56 #if defined(__WXMSW__) && defined(__WIN32__) && defined(SM_CXMENUCHECK)
57 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= GetSystemMetrics(SM_CXMENUCHECK
);
58 #else // # what is the reasonable default?
59 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 15;
62 size_t wxOwnerDrawn::ms_nLastMarginWidth
= ms_nDefaultMarginWidth
;
67 // get size of the item
68 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth
, size_t *pheight
)
71 dc
.SetFont(GetFont());
74 wxChar
*szStripped
= new wxChar
[m_strName
.Len()];
75 wxStripMenuCodes((wxChar
*)m_strName
.c_str(), szStripped
);
76 wxString str
= szStripped
;
79 // # without this menu items look too tightly packed (at least under Windows)
80 str
+= wxT('W'); // 'W' is typically the widest letter
82 dc
.GetTextExtent(str
, (long *)pwidth
, (long *)pheight
);
84 // JACS: items still look too tightly packed, so adding 2 pixels.
85 (*pheight
) = (*pheight
) + 2;
87 m_nHeight
= *pheight
; // remember height for use in OnDrawItem
92 // searching for this macro you'll find all the code where I'm using the native
93 // Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
94 // port this code to X. (VZ)
96 // JACS: TODO. Why does a disabled but highlighted item still
97 // get drawn embossed? How can we tell DrawState that we don't want the
100 #if defined(__WIN32__) && !defined(__SC__) && !defined(__TWIN32__)
101 #define O_DRAW_NATIVE_API // comments below explain why I use it
105 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus st
)
107 // we do nothing on focus change
108 if ( act
== wxODFocusChanged
)
112 #define ToRGB(col) RGB(col.Red(), col.Green(), col.Blue())
113 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
117 DWORD colBack
, colText
;
118 if ( st
& wxODSelected
) {
119 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
120 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
123 // fall back to default colors if none explicitly specified
124 colBack
= m_colBack
.Ok() ? ToRGB(m_colBack
) : GetSysColor(COLOR_WINDOW
);
125 colText
= m_colText
.Ok() ? ToRGB(m_colText
) : GetSysColor(COLOR_WINDOWTEXT
);
128 #ifdef O_DRAW_NATIVE_API
129 #define hdc (HDC)dc.GetHDC()
130 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
131 colOldBack
= ::SetBkColor(hdc
, colBack
);
133 dc
.SetTextForeground(wxColor(UnRGB(colText
)));
134 dc
.SetTextBackground(wxColor(UnRGB(colBack
)));
137 // select the font and draw the text
138 // ---------------------------------
140 // determine where to draw and leave space for a check-mark.
141 int x
= rc
.x
+ GetMarginWidth();
143 // using native API because it reckognizes '&'
144 #ifdef O_DRAW_NATIVE_API
145 int nPrevMode
= SetBkMode(hdc
, TRANSPARENT
);
146 HBRUSH hbr
= CreateSolidBrush(colBack
),
147 hPrevBrush
= (HBRUSH
)SelectObject(hdc
, hbr
);
149 RECT rectAll
= { rc
.GetLeft(), rc
.GetTop(), rc
.GetRight(), rc
.GetBottom() };
150 FillRect(hdc
, &rectAll
, hbr
);
154 // use default font if no font set
157 m_font
.RealizeResource();
158 hfont
= (HFONT
)m_font
.GetResourceHandle();
161 hfont
= (HFONT
)::GetStockObject(SYSTEM_FONT
);
164 HFONT hPrevFont
= (HFONT
) ::SelectObject(hdc
, hfont
);
165 DrawState(hdc
, NULL
, NULL
,
166 (LPARAM
)(const wxChar
*)m_strName
, m_strName
.Length(),
167 x
, rc
.y
, rc
.GetWidth(), rc
.GetHeight(),
168 DST_PREFIXTEXT
| ( st
& wxODDisabled
? DSS_DISABLED
: 0) );
170 (void)SelectObject(hdc
, hPrevBrush
);
171 (void)SelectObject(hdc
, hPrevFont
);
172 (void)SetBkMode(hdc
, nPrevMode
);
174 dc
.SetFont(GetFont());
175 dc
.DrawText(m_strName
, x
, rc
.y
);
176 #endif //O_DRAW_NATIVE_API
180 if ( IsCheckable() && !m_bmpChecked
.Ok() ) {
181 if ( st
& wxODChecked
) {
182 // using native APIs for performance and simplicity
183 #ifdef O_DRAW_NATIVE_API
184 // what goes on: DrawFrameControl creates a b/w mask,
185 // then we copy it to screen to have right colors
187 // first create a monochrome bitmap in a memory DC
188 HDC hdcMem
= CreateCompatibleDC(hdc
);
189 HBITMAP hbmpCheck
= CreateBitmap(GetMarginWidth(), m_nHeight
, 1, 1, 0);
190 SelectObject(hdcMem
, hbmpCheck
);
192 // then draw a check mark into it
193 RECT rect
= { 0, 0, GetMarginWidth(), m_nHeight
};
197 DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
201 // finally copy it to screen DC and clean up
202 BitBlt(hdc
, rc
.x
, rc
.y
, GetMarginWidth(), m_nHeight
,
203 hdcMem
, 0, 0, SRCCOPY
);
206 DeleteObject(hbmpCheck
);
208 // #### to do: perhaps using Marlett font (create equiv. font under X)
209 // wxFAIL("not implemented");
210 #endif //O_DRAW_NATIVE_API
214 // for uncheckable item we use only the 'checked' bitmap
215 wxBitmap
bmp(GetBitmap(IsCheckable() ? ((st
& wxODChecked
) != 0) : TRUE
));
217 wxMemoryDC
dcMem(&dc
);
218 dcMem
.SelectObject(bmp
);
221 int nBmpWidth
= bmp
.GetWidth(),
222 nBmpHeight
= bmp
.GetHeight();
224 // there should be enough place!
225 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
227 //MT: blit with mask enabled.
228 dc
.Blit(rc
.x
+ (GetMarginWidth() - nBmpWidth
) / 2,
229 rc
.y
+ (m_nHeight
- nBmpHeight
) /2,
230 nBmpWidth
, nBmpHeight
,
231 &dcMem
, 0, 0, wxCOPY
, TRUE
);
233 if ( st
& wxODSelected
) {
234 #ifdef O_DRAW_NATIVE_API
235 RECT rectBmp
= { rc
.GetLeft(), rc
.GetTop(),
236 rc
.GetLeft() + GetMarginWidth(),
237 rc
.GetTop() + m_nHeight
};
238 SetBkColor(hdc
, colBack
);
239 DrawEdge(hdc
, &rectBmp
, EDGE_RAISED
, BF_SOFT
| BF_RECT
);
241 // ## to write portable DrawEdge
242 #endif //O_DRAW_NATIVE_API
247 #ifdef O_DRAW_NATIVE_API
248 ::SetTextColor(hdc
, colOldText
);
249 ::SetBkColor(hdc
, colOldBack
);
252 #endif //O_DRAW_NATIVE_API
258 #endif // wxUSE_OWNER_DRAWN