]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ownerdrw.cpp
b23fb671e2c7d4901f9f67a3be8eed8082a5b4f4
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/settings.h"
35 #include "wx/ownerdrw.h"
36 #include "wx/menuitem.h"
41 // ============================================================================
42 // implementation of wxOwnerDrawn class
43 // ============================================================================
47 wxOwnerDrawn::wxOwnerDrawn(const wxString
& str
,
48 bool bCheckable
, bool bMenuItem
)
51 m_bCheckable
= bCheckable
;
52 m_bOwnerDrawn
= FALSE
;
54 m_nMarginWidth
= ms_nLastMarginWidth
;
56 m_font
= * wxNORMAL_FONT
;
59 #if defined(__WXMSW__) && defined(__WIN32__) && defined(SM_CXMENUCHECK)
60 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= GetSystemMetrics(SM_CXMENUCHECK
);
61 #else // # what is the reasonable default?
62 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 15;
65 size_t wxOwnerDrawn::ms_nLastMarginWidth
= ms_nDefaultMarginWidth
;
70 // get size of the item
71 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth
, size_t *pheight
)
74 dc
.SetFont(GetFont());
76 wxString str
= wxStripMenuCodes(m_strName
);
78 // # without this menu items look too tightly packed (at least under Windows)
79 str
+= wxT('W'); // 'W' is typically the widest letter
81 dc
.GetTextExtent(str
, (long *)pwidth
, (long *)pheight
);
83 // JACS: items still look too tightly packed, so adding 2 pixels.
84 (*pheight
) = (*pheight
) + 2;
86 m_nHeight
= *pheight
; // remember height for use in OnDrawItem
91 // searching for this macro you'll find all the code where I'm using the native
92 // Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
93 // port this code to X. (VZ)
95 // JACS: TODO. Why does a disabled but highlighted item still
96 // get drawn embossed? How can we tell DrawState that we don't want the
99 #if defined(__WIN32__) && !defined(__SC__) && !defined(__TWIN32__)
100 #define O_DRAW_NATIVE_API // comments below explain why I use it
104 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus st
)
106 // we do nothing on focus change
107 if ( act
== wxODFocusChanged
)
111 #define ToRGB(col) RGB(col.Red(), col.Green(), col.Blue())
112 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
116 DWORD colBack
, colText
;
117 if ( st
& wxODSelected
) {
118 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
119 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
122 // fall back to default colors if none explicitly specified
123 colBack
= m_colBack
.Ok() ? ToRGB(m_colBack
) : GetSysColor(COLOR_WINDOW
);
124 colText
= m_colText
.Ok() ? ToRGB(m_colText
) : GetSysColor(COLOR_WINDOWTEXT
);
127 #ifdef O_DRAW_NATIVE_API
128 #define hdc (HDC)dc.GetHDC()
129 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
130 colOldBack
= ::SetBkColor(hdc
, colBack
);
132 dc
.SetTextForeground(wxColor(UnRGB(colText
)));
133 dc
.SetTextBackground(wxColor(UnRGB(colBack
)));
136 // select the font and draw the text
137 // ---------------------------------
139 // determine where to draw and leave space for a check-mark.
140 int x
= rc
.x
+ GetMarginWidth();
142 // using native API because it reckognizes '&'
143 #ifdef O_DRAW_NATIVE_API
144 int nPrevMode
= SetBkMode(hdc
, TRANSPARENT
);
145 HBRUSH hbr
= CreateSolidBrush(colBack
),
146 hPrevBrush
= (HBRUSH
)SelectObject(hdc
, hbr
);
148 RECT rectAll
= { rc
.GetLeft(), rc
.GetTop(), rc
.GetRight(), rc
.GetBottom() };
149 FillRect(hdc
, &rectAll
, hbr
);
153 // use default font if no font set
156 m_font
.RealizeResource();
157 hfont
= (HFONT
)m_font
.GetResourceHandle();
160 hfont
= (HFONT
)::GetStockObject(SYSTEM_FONT
);
163 HFONT hPrevFont
= (HFONT
) ::SelectObject(hdc
, hfont
);
164 DrawState(hdc
, NULL
, NULL
,
165 (LPARAM
)(const wxChar
*)m_strName
, m_strName
.Length(),
166 x
, rc
.y
, rc
.GetWidth(), rc
.GetHeight(),
167 DST_PREFIXTEXT
| ( st
& wxODDisabled
? DSS_DISABLED
: 0) );
169 (void)SelectObject(hdc
, hPrevBrush
);
170 (void)SelectObject(hdc
, hPrevFont
);
171 (void)SetBkMode(hdc
, nPrevMode
);
173 dc
.SetFont(GetFont());
174 dc
.DrawText(m_strName
, x
, rc
.y
);
175 #endif //O_DRAW_NATIVE_API
179 if ( IsCheckable() && !m_bmpChecked
.Ok() ) {
180 if ( st
& wxODChecked
) {
181 // using native APIs for performance and simplicity
182 #ifdef O_DRAW_NATIVE_API
183 // what goes on: DrawFrameControl creates a b/w mask,
184 // then we copy it to screen to have right colors
186 // first create a monochrome bitmap in a memory DC
187 HDC hdcMem
= CreateCompatibleDC(hdc
);
188 HBITMAP hbmpCheck
= CreateBitmap(GetMarginWidth(), m_nHeight
, 1, 1, 0);
189 SelectObject(hdcMem
, hbmpCheck
);
191 // then draw a check mark into it
192 RECT rect
= { 0, 0, GetMarginWidth(), m_nHeight
};
196 DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
200 // finally copy it to screen DC and clean up
201 BitBlt(hdc
, rc
.x
, rc
.y
, GetMarginWidth(), m_nHeight
,
202 hdcMem
, 0, 0, SRCCOPY
);
205 DeleteObject(hbmpCheck
);
207 // #### to do: perhaps using Marlett font (create equiv. font under X)
208 // wxFAIL("not implemented");
209 #endif //O_DRAW_NATIVE_API
213 // for uncheckable item we use only the 'checked' bitmap
214 wxBitmap
bmp(GetBitmap(IsCheckable() ? ((st
& wxODChecked
) != 0) : TRUE
));
216 wxMemoryDC
dcMem(&dc
);
217 dcMem
.SelectObject(bmp
);
220 int nBmpWidth
= bmp
.GetWidth(),
221 nBmpHeight
= bmp
.GetHeight();
223 // there should be enough place!
224 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
226 //MT: blit with mask enabled.
227 dc
.Blit(rc
.x
+ (GetMarginWidth() - nBmpWidth
) / 2,
228 rc
.y
+ (m_nHeight
- nBmpHeight
) /2,
229 nBmpWidth
, nBmpHeight
,
230 &dcMem
, 0, 0, wxCOPY
, TRUE
);
232 if ( st
& wxODSelected
) {
233 #ifdef O_DRAW_NATIVE_API
234 RECT rectBmp
= { rc
.GetLeft(), rc
.GetTop(),
235 rc
.GetLeft() + GetMarginWidth(),
236 rc
.GetTop() + m_nHeight
};
237 SetBkColor(hdc
, colBack
);
238 DrawEdge(hdc
, &rectBmp
, EDGE_RAISED
, BF_SOFT
| BF_RECT
);
240 // ## to write portable DrawEdge
241 #endif //O_DRAW_NATIVE_API
246 #ifdef O_DRAW_NATIVE_API
247 ::SetTextColor(hdc
, colOldText
);
248 ::SetBkColor(hdc
, colOldBack
);
251 #endif //O_DRAW_NATIVE_API
257 #endif // wxUSE_OWNER_DRAWN