]>
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
)
74 dc
.SetFont(GetFont());
77 wxChar
*szStripped
= new wxChar
[m_strName
.Len()];
78 wxStripMenuCodes((wxChar
*)m_strName
.c_str(), szStripped
);
79 wxString str
= szStripped
;
82 // # without this menu items look too tightly packed (at least under Windows)
83 str
+= wxT('W'); // 'W' is typically the widest letter
85 dc
.GetTextExtent(str
, (long *)pwidth
, (long *)pheight
);
87 // JACS: items still look too tightly packed, so adding 2 pixels.
88 (*pheight
) = (*pheight
) + 2;
90 m_nHeight
= *pheight
; // remember height for use in OnDrawItem
95 // searching for this macro you'll find all the code where I'm using the native
96 // Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
97 // port this code to X. (VZ)
99 // JACS: TODO. Why does a disabled but highlighted item still
100 // get drawn embossed? How can we tell DrawState that we don't want the
103 #if defined(__WIN32__) && !defined(__SC__) && !defined(__TWIN32__)
104 #define O_DRAW_NATIVE_API // comments below explain why I use it
108 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus st
)
110 // we do nothing on focus change
111 if ( act
== wxODFocusChanged
)
115 #define ToRGB(col) RGB(col.Red(), col.Green(), col.Blue())
116 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
120 DWORD colBack
, colText
;
121 if ( st
& wxODSelected
) {
122 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
123 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
126 // fall back to default colors if none explicitly specified
127 colBack
= m_colBack
.Ok() ? ToRGB(m_colBack
) : GetSysColor(COLOR_WINDOW
);
128 colText
= m_colText
.Ok() ? ToRGB(m_colText
) : GetSysColor(COLOR_WINDOWTEXT
);
131 #ifdef O_DRAW_NATIVE_API
132 #define hdc (HDC)dc.GetHDC()
133 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
134 colOldBack
= ::SetBkColor(hdc
, colBack
);
136 dc
.SetTextForeground(wxColor(UnRGB(colText
)));
137 dc
.SetTextBackground(wxColor(UnRGB(colBack
)));
140 // select the font and draw the text
141 // ---------------------------------
143 // determine where to draw and leave space for a check-mark.
144 int x
= rc
.x
+ GetMarginWidth();
146 // using native API because it reckognizes '&'
147 #ifdef O_DRAW_NATIVE_API
148 int nPrevMode
= SetBkMode(hdc
, TRANSPARENT
);
149 HBRUSH hbr
= CreateSolidBrush(colBack
),
150 hPrevBrush
= (HBRUSH
)SelectObject(hdc
, hbr
);
152 RECT rectAll
= { rc
.GetLeft(), rc
.GetTop(), rc
.GetRight(), rc
.GetBottom() };
153 FillRect(hdc
, &rectAll
, hbr
);
157 // use default font if no font set
160 m_font
.RealizeResource();
161 hfont
= (HFONT
)m_font
.GetResourceHandle();
164 hfont
= (HFONT
)::GetStockObject(SYSTEM_FONT
);
167 HFONT hPrevFont
= (HFONT
) ::SelectObject(hdc
, hfont
);
168 DrawState(hdc
, NULL
, NULL
,
169 (LPARAM
)(const wxChar
*)m_strName
, m_strName
.Length(),
170 x
, rc
.y
, rc
.GetWidth(), rc
.GetHeight(),
171 DST_PREFIXTEXT
| ( st
& wxODDisabled
? DSS_DISABLED
: 0) );
173 (void)SelectObject(hdc
, hPrevBrush
);
174 (void)SelectObject(hdc
, hPrevFont
);
175 (void)SetBkMode(hdc
, nPrevMode
);
177 dc
.SetFont(GetFont());
178 dc
.DrawText(m_strName
, x
, rc
.y
);
179 #endif //O_DRAW_NATIVE_API
183 if ( IsCheckable() && !m_bmpChecked
.Ok() ) {
184 if ( st
& wxODChecked
) {
185 // using native APIs for performance and simplicity
186 #ifdef O_DRAW_NATIVE_API
187 // what goes on: DrawFrameControl creates a b/w mask,
188 // then we copy it to screen to have right colors
190 // first create a monochrome bitmap in a memory DC
191 HDC hdcMem
= CreateCompatibleDC(hdc
);
192 HBITMAP hbmpCheck
= CreateBitmap(GetMarginWidth(), m_nHeight
, 1, 1, 0);
193 SelectObject(hdcMem
, hbmpCheck
);
195 // then draw a check mark into it
196 RECT rect
= { 0, 0, GetMarginWidth(), m_nHeight
};
200 DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
204 // finally copy it to screen DC and clean up
205 BitBlt(hdc
, rc
.x
, rc
.y
, GetMarginWidth(), m_nHeight
,
206 hdcMem
, 0, 0, SRCCOPY
);
209 DeleteObject(hbmpCheck
);
211 // #### to do: perhaps using Marlett font (create equiv. font under X)
212 // wxFAIL("not implemented");
213 #endif //O_DRAW_NATIVE_API
217 // for uncheckable item we use only the 'checked' bitmap
218 wxBitmap
bmp(GetBitmap(IsCheckable() ? ((st
& wxODChecked
) != 0) : TRUE
));
220 wxMemoryDC
dcMem(&dc
);
221 dcMem
.SelectObject(bmp
);
224 int nBmpWidth
= bmp
.GetWidth(),
225 nBmpHeight
= bmp
.GetHeight();
227 // there should be enough place!
228 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
230 //MT: blit with mask enabled.
231 dc
.Blit(rc
.x
+ (GetMarginWidth() - nBmpWidth
) / 2,
232 rc
.y
+ (m_nHeight
- nBmpHeight
) /2,
233 nBmpWidth
, nBmpHeight
,
234 &dcMem
, 0, 0, wxCOPY
, TRUE
);
236 if ( st
& wxODSelected
) {
237 #ifdef O_DRAW_NATIVE_API
238 RECT rectBmp
= { rc
.GetLeft(), rc
.GetTop(),
239 rc
.GetLeft() + GetMarginWidth(),
240 rc
.GetTop() + m_nHeight
};
241 SetBkColor(hdc
, colBack
);
242 DrawEdge(hdc
, &rectBmp
, EDGE_RAISED
, BF_SOFT
| BF_RECT
);
244 // ## to write portable DrawEdge
245 #endif //O_DRAW_NATIVE_API
250 #ifdef O_DRAW_NATIVE_API
251 ::SetTextColor(hdc
, colOldText
);
252 ::SetBkColor(hdc
, colOldBack
);
255 #endif //O_DRAW_NATIVE_API
261 #endif // wxUSE_OWNER_DRAWN