]>
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/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
)
75 wxString str
= wxStripMenuCodes(m_strName
);
77 // # without this menu items look too tightly packed (at least under Windows)
78 str
+= wxT('W'); // 'W' is typically the widest letter
81 dc
.SetFont(GetFont());
83 dc
.GetTextExtent(str
, (long *)pwidth
, (long *)pheight
);
85 // JACS: items still look too tightly packed, so adding 2 pixels.
86 (*pheight
) = (*pheight
) + 2;
88 // Ray Gilbert's changes - Corrects the problem of a BMP
89 // being placed next to text in a menu item, and the BMP does
90 // not match the size expected by the system. This will
91 // resize the space so the BMP will fit. Without this, BMPs
92 // must be no larger or smaller than 16x16.
93 if (m_bmpChecked
.Ok())
95 // Is BMP height larger then text height?
96 size_t adjustedHeight
= m_bmpChecked
.GetHeight() +
97 wxSystemSettings::GetSystemMetric(wxSYS_EDGE_Y
);
98 if (*pheight
< adjustedHeight
)
99 *pheight
= adjustedHeight
;
101 // Does BMP encroach on default check menu position?
102 size_t adjustedWidth
= m_bmpChecked
.GetWidth() +
103 (wxSystemSettings::GetSystemMetric(wxSYS_EDGE_X
) * 2);
104 if (ms_nDefaultMarginWidth
< adjustedWidth
)
105 *pwidth
+= adjustedWidth
- ms_nDefaultMarginWidth
;
107 // Do we need to widen margin to fit BMP?
108 if ((size_t)GetMarginWidth() < adjustedWidth
)
109 SetMarginWidth(adjustedWidth
);
112 m_nHeight
= *pheight
; // remember height for use in OnDrawItem
117 // searching for this macro you'll find all the code where I'm using the native
118 // Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
119 // port this code to X. (VZ)
121 // JACS: TODO. Why does a disabled but highlighted item still
122 // get drawn embossed? How can we tell DrawState that we don't want the
125 #if defined(__WIN32__) && !defined(__SC__) && !defined(__TWIN32__)
126 #define O_DRAW_NATIVE_API // comments below explain why I use it
130 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus st
)
132 // we do nothing on focus change
133 if ( act
== wxODFocusChanged
)
137 #define ToRGB(col) RGB(col.Red(), col.Green(), col.Blue())
138 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
142 DWORD colBack
, colText
;
143 if ( st
& wxODSelected
) {
144 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
145 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
148 // fall back to default colors if none explicitly specified
149 colBack
= m_colBack
.Ok() ? ToRGB(m_colBack
) : GetSysColor(COLOR_WINDOW
);
150 colText
= m_colText
.Ok() ? ToRGB(m_colText
) : GetSysColor(COLOR_WINDOWTEXT
);
153 #ifdef O_DRAW_NATIVE_API
154 #define hdc (HDC)dc.GetHDC()
155 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
156 colOldBack
= ::SetBkColor(hdc
, colBack
);
158 dc
.SetTextForeground(wxColor(UnRGB(colText
)));
159 dc
.SetTextBackground(wxColor(UnRGB(colBack
)));
162 // select the font and draw the text
163 // ---------------------------------
165 // determine where to draw and leave space for a check-mark.
166 int x
= rc
.x
+ GetMarginWidth();
168 // using native API because it reckognizes '&'
169 #ifdef O_DRAW_NATIVE_API
170 int nPrevMode
= SetBkMode(hdc
, TRANSPARENT
);
171 HBRUSH hbr
= CreateSolidBrush(colBack
),
172 hPrevBrush
= (HBRUSH
)SelectObject(hdc
, hbr
);
174 RECT rectAll
= { rc
.GetLeft(), rc
.GetTop(), rc
.GetRight(), rc
.GetBottom() };
175 FillRect(hdc
, &rectAll
, hbr
);
179 // use default font if no font set
182 m_font
.RealizeResource();
183 hfont
= (HFONT
)m_font
.GetResourceHandle();
186 hfont
= (HFONT
)::GetStockObject(SYSTEM_FONT
);
189 HFONT hPrevFont
= (HFONT
) ::SelectObject(hdc
, hfont
);
190 DrawState(hdc
, NULL
, NULL
,
191 (LPARAM
)(const wxChar
*)m_strName
, m_strName
.Length(),
192 x
, rc
.y
, rc
.GetWidth(), rc
.GetHeight(),
193 DST_PREFIXTEXT
| ( st
& wxODDisabled
? DSS_DISABLED
: 0) );
195 (void)SelectObject(hdc
, hPrevBrush
);
196 (void)SelectObject(hdc
, hPrevFont
);
197 (void)SetBkMode(hdc
, nPrevMode
);
199 dc
.SetFont(GetFont());
200 dc
.DrawText(m_strName
, x
, rc
.y
);
201 #endif //O_DRAW_NATIVE_API
205 if ( IsCheckable() && !m_bmpChecked
.Ok() ) {
206 if ( st
& wxODChecked
) {
207 // using native APIs for performance and simplicity
208 #ifdef O_DRAW_NATIVE_API
209 // what goes on: DrawFrameControl creates a b/w mask,
210 // then we copy it to screen to have right colors
212 // first create a monochrome bitmap in a memory DC
213 HDC hdcMem
= CreateCompatibleDC(hdc
);
214 HBITMAP hbmpCheck
= CreateBitmap(GetMarginWidth(), m_nHeight
, 1, 1, 0);
215 SelectObject(hdcMem
, hbmpCheck
);
217 // then draw a check mark into it
218 RECT rect
= { 0, 0, GetMarginWidth(), m_nHeight
};
222 DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
226 // finally copy it to screen DC and clean up
227 BitBlt(hdc
, rc
.x
, rc
.y
, GetMarginWidth(), m_nHeight
,
228 hdcMem
, 0, 0, SRCCOPY
);
231 DeleteObject(hbmpCheck
);
233 // #### to do: perhaps using Marlett font (create equiv. font under X)
234 // wxFAIL("not implemented");
235 #endif //O_DRAW_NATIVE_API
239 // for uncheckable item we use only the 'checked' bitmap
240 wxBitmap
bmp(GetBitmap(IsCheckable() ? ((st
& wxODChecked
) != 0) : TRUE
));
242 wxMemoryDC
dcMem(&dc
);
243 dcMem
.SelectObject(bmp
);
246 int nBmpWidth
= bmp
.GetWidth(),
247 nBmpHeight
= bmp
.GetHeight();
249 // there should be enough place!
250 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
252 int heightDiff
= (m_nHeight
- nBmpHeight
);
253 // if (heightDiff = -1)
256 //MT: blit with mask enabled.
257 dc
.Blit(rc
.x
+ (GetMarginWidth() - nBmpWidth
) / 2,
258 rc
.y
+ heightDiff
/ 2,
259 nBmpWidth
, nBmpHeight
,
260 &dcMem
, 0, 0, wxCOPY
, TRUE
);
262 if ( st
& wxODSelected
) {
263 #ifdef O_DRAW_NATIVE_API
264 RECT rectBmp
= { rc
.GetLeft(), rc
.GetTop(),
265 rc
.GetLeft() + GetMarginWidth(),
266 rc
.GetTop() + m_nHeight
};
267 SetBkColor(hdc
, colBack
);
268 DrawEdge(hdc
, &rectBmp
, EDGE_RAISED
, BF_SOFT
| BF_RECT
);
270 // ## to write portable DrawEdge
271 #endif //O_DRAW_NATIVE_API
276 #ifdef O_DRAW_NATIVE_API
277 ::SetTextColor(hdc
, colOldText
);
278 ::SetBkColor(hdc
, colOldBack
);
281 #endif //O_DRAW_NATIVE_API
287 #endif // wxUSE_OWNER_DRAWN