]>
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"
25 #include "wx/bitmap.h"
26 #include "wx/dcmemory.h"
31 #include "wx/ownerdrw.h"
32 #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 char *szStripped
= new char[m_strName
.Len()];
75 wxStripMenuCodes((char *)m_strName
.c_str(), szStripped
);
76 wxString str
= szStripped
;
79 // # without this menu items look too tightly packed (at least under Windows)
80 str
+= 'W'; // 'W' is typically the widest letter
82 dc
.GetTextExtent(str
, (long *)pwidth
, (long *)pheight
);
83 m_nHeight
= *pheight
; // remember height for use in OnDrawItem
88 // searching for this macro you'll find all the code where I'm using the native
89 // Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
90 // port this code to X. (VZ)
92 #if defined(__WIN32__) && !defined(__SC__)
93 #define O_DRAW_NATIVE_API // comments below explain why I use it
97 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus st
)
99 // we do nothing on focus change
100 if ( act
== wxODFocusChanged
)
104 #define ToRGB(col) RGB(col.Red(), col.Green(), col.Blue())
105 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
109 DWORD colBack
, colText
;
110 if ( st
& wxODSelected
) {
111 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
112 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
115 // fall back to default colors if none explicitly specified
116 colBack
= m_colBack
.Ok() ? ToRGB(m_colBack
) : GetSysColor(COLOR_WINDOW
);
117 colText
= m_colText
.Ok() ? ToRGB(m_colText
) : GetSysColor(COLOR_WINDOWTEXT
);
120 #ifdef O_DRAW_NATIVE_API
121 #define hdc (HDC)dc.GetHDC()
122 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
123 colOldBack
= ::SetBkColor(hdc
, colBack
);
125 dc
.SetTextForeground(wxColor(UnRGB(colText
)));
126 dc
.SetTextBackground(wxColor(UnRGB(colBack
)));
129 // select the font and draw the text
130 // ---------------------------------
132 // determine where to draw and leave space for a check-mark.
133 int x
= rc
.x
+ GetMarginWidth();
135 // using native API because it reckognizes '&'
136 #ifdef O_DRAW_NATIVE_API
137 int nPrevMode
= SetBkMode(hdc
, TRANSPARENT
);
138 HBRUSH hbr
= CreateSolidBrush(colBack
),
139 hPrevBrush
= (HBRUSH
) SelectObject(hdc
, hbr
);
141 RECT rectAll
= { rc
.GetLeft(), rc
.GetTop(), rc
.GetRight(), rc
.GetBottom() };
142 FillRect(hdc
, &rectAll
, hbr
);
144 // use default font if no font set
147 m_font
.RealizeResource();
148 hfont
= (HFONT
)m_font
.GetResourceHandle();
151 hfont
= (HFONT
)::GetStockObject(SYSTEM_FONT
);
154 HFONT hPrevFont
= (HFONT
) ::SelectObject(hdc
, hfont
);
155 DrawState(hdc
, NULL
, NULL
,
156 (LPARAM
)(const char *)m_strName
, m_strName
.Length(),
157 x
, rc
.y
, rc
.GetWidth(), rc
.GetHeight(),
158 DST_PREFIXTEXT
| ( st
& wxODDisabled
? DSS_DISABLED
: 0) );
160 (void)SelectObject(hdc
, hPrevBrush
);
161 (void)SelectObject(hdc
, hPrevFont
);
162 (void)SetBkMode(hdc
, nPrevMode
);
164 dc
.SetFont(GetFont());
165 dc
.DrawText(m_strName
, x
, rc
.y
);
166 #endif //O_DRAW_NATIVE_API
170 if ( IsCheckable() && !m_bmpChecked
.Ok() ) {
171 if ( st
& wxODChecked
) {
172 // using native APIs for performance and simplicity
173 #ifdef O_DRAW_NATIVE_API
174 // what goes on: DrawFrameControl creates a b/w mask,
175 // then we copy it to screen to have right colors
177 // first create a monochrome bitmap in a memory DC
178 HDC hdcMem
= CreateCompatibleDC(hdc
);
179 HBITMAP hbmpCheck
= CreateBitmap(GetMarginWidth(), m_nHeight
, 1, 1, 0);
180 SelectObject(hdcMem
, hbmpCheck
);
182 // then draw a check mark into it
183 RECT rect
= { 0, 0, GetMarginWidth(), m_nHeight
};
185 DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
188 // finally copy it to screen DC and clean up
189 BitBlt(hdc
, rc
.x
, rc
.y
, GetMarginWidth(), m_nHeight
,
190 hdcMem
, 0, 0, SRCCOPY
);
193 // #### to do: perhaps using Marlett font (create equiv. font under X)
194 // wxFAIL("not implemented");
195 #endif //O_DRAW_NATIVE_API
199 // for uncheckable item we use only the 'checked' bitmap
200 wxBitmap
bmp(GetBitmap(IsCheckable() ? ((st
& wxODChecked
) != 0) : TRUE
));
202 wxMemoryDC
dcMem(&dc
);
203 dcMem
.SelectObject(bmp
);
206 int nBmpWidth
= bmp
.GetWidth(),
207 nBmpHeight
= bmp
.GetHeight();
209 // there should be enough place!
210 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
212 dc
.Blit(rc
.x
+ (GetMarginWidth() - nBmpWidth
) / 2,
213 rc
.y
+ (m_nHeight
- nBmpHeight
) /2,
214 nBmpWidth
, nBmpHeight
,
215 &dcMem
, 0, 0, wxCOPY
);
217 if ( st
& wxODSelected
) {
218 #ifdef O_DRAW_NATIVE_API
219 RECT rectBmp
= { rc
.GetLeft(), rc
.GetTop(),
220 rc
.GetLeft() + GetMarginWidth(),
221 rc
.GetTop() + m_nHeight
};
222 SetBkColor(hdc
, colBack
);
223 DrawEdge(hdc
, &rectBmp
, EDGE_RAISED
, BF_SOFT
| BF_RECT
);
225 // ## to write portable DrawEdge
226 #endif //O_DRAW_NATIVE_API
231 #ifdef O_DRAW_NATIVE_API
232 ::SetTextColor(hdc
, colOldText
);
233 ::SetBkColor(hdc
, colOldBack
);
236 #endif //O_DRAW_NATIVE_API