]>
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"
34 #include "wx/msw/private.h"
36 // ============================================================================
37 // implementation of wxOwnerDrawn class
38 // ============================================================================
42 wxOwnerDrawn::wxOwnerDrawn(const wxString
& str
,
43 bool bCheckable
, bool bMenuItem
)
46 m_bCheckable
= bCheckable
;
47 m_bOwnerDrawn
= FALSE
;
49 m_nMarginWidth
= ms_nLastMarginWidth
;
52 #if defined(__WXMSW__) && defined(__WIN32__) && defined(SM_CXMENUCHECK)
53 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= GetSystemMetrics(SM_CXMENUCHECK
);
54 #else // # what is the reasonable default?
55 size_t wxOwnerDrawn::ms_nDefaultMarginWidth
= 15;
58 size_t wxOwnerDrawn::ms_nLastMarginWidth
= ms_nDefaultMarginWidth
;
63 // get size of the item
64 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth
, size_t *pheight
)
67 dc
.SetFont(GetFont());
70 char *szStripped
= new char[m_strName
.Len()];
71 wxStripMenuCodes((char *)m_strName
.c_str(), szStripped
);
72 wxString str
= szStripped
;
75 // # without this menu items look too tightly packed (at least under Windows)
76 str
+= 'W'; // 'W' is typically the widest letter
78 dc
.GetTextExtent(str
, (long *)pwidth
, (long *)pheight
);
79 m_nHeight
= *pheight
; // remember height for use in OnDrawItem
84 // searching for this macro you'll find all the code where I'm using the native
85 // Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
86 // port this code to X. (VZ)
88 #if defined(__WIN32__) && !defined(__SC__) && !defined(__TWIN32__)
89 #define O_DRAW_NATIVE_API // comments below explain why I use it
93 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus st
)
95 // we do nothing on focus change
96 if ( act
== wxODFocusChanged
)
100 #define ToRGB(col) RGB(col.Red(), col.Green(), col.Blue())
101 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
105 DWORD colBack
, colText
;
106 if ( st
& wxODSelected
) {
107 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
108 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
111 // fall back to default colors if none explicitly specified
112 colBack
= m_colBack
.Ok() ? ToRGB(m_colBack
) : GetSysColor(COLOR_WINDOW
);
113 colText
= m_colText
.Ok() ? ToRGB(m_colText
) : GetSysColor(COLOR_WINDOWTEXT
);
116 #ifdef O_DRAW_NATIVE_API
117 #define hdc (HDC)dc.GetHDC()
118 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
119 colOldBack
= ::SetBkColor(hdc
, colBack
);
121 dc
.SetTextForeground(wxColor(UnRGB(colText
)));
122 dc
.SetTextBackground(wxColor(UnRGB(colBack
)));
125 // select the font and draw the text
126 // ---------------------------------
128 // determine where to draw and leave space for a check-mark.
129 int x
= rc
.x
+ GetMarginWidth();
131 // using native API because it reckognizes '&'
132 #ifdef O_DRAW_NATIVE_API
133 int nPrevMode
= SetBkMode(hdc
, TRANSPARENT
);
134 HBRUSH hbr
= CreateSolidBrush(colBack
),
135 hPrevBrush
= (HBRUSH
) SelectObject(hdc
, hbr
);
137 RECT rectAll
= { rc
.GetLeft(), rc
.GetTop(), rc
.GetRight(), rc
.GetBottom() };
138 FillRect(hdc
, &rectAll
, hbr
);
140 // use default font if no font set
143 m_font
.RealizeResource();
144 hfont
= (HFONT
)m_font
.GetResourceHandle();
147 hfont
= (HFONT
)::GetStockObject(SYSTEM_FONT
);
150 HFONT hPrevFont
= (HFONT
) ::SelectObject(hdc
, hfont
);
151 DrawState(hdc
, NULL
, NULL
,
152 (LPARAM
)(const char *)m_strName
, m_strName
.Length(),
153 x
, rc
.y
, rc
.GetWidth(), rc
.GetHeight(),
154 DST_PREFIXTEXT
| ( st
& wxODDisabled
? DSS_DISABLED
: 0) );
156 (void)SelectObject(hdc
, hPrevBrush
);
157 (void)SelectObject(hdc
, hPrevFont
);
158 (void)SetBkMode(hdc
, nPrevMode
);
160 dc
.SetFont(GetFont());
161 dc
.DrawText(m_strName
, x
, rc
.y
);
162 #endif //O_DRAW_NATIVE_API
166 if ( IsCheckable() && !m_bmpChecked
.Ok() ) {
167 if ( st
& wxODChecked
) {
168 // using native APIs for performance and simplicity
169 #ifdef O_DRAW_NATIVE_API
170 // what goes on: DrawFrameControl creates a b/w mask,
171 // then we copy it to screen to have right colors
173 // first create a monochrome bitmap in a memory DC
174 HDC hdcMem
= CreateCompatibleDC(hdc
);
175 HBITMAP hbmpCheck
= CreateBitmap(GetMarginWidth(), m_nHeight
, 1, 1, 0);
176 SelectObject(hdcMem
, hbmpCheck
);
178 // then draw a check mark into it
179 RECT rect
= { 0, 0, GetMarginWidth(), m_nHeight
};
181 DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
184 // finally copy it to screen DC and clean up
185 BitBlt(hdc
, rc
.x
, rc
.y
, GetMarginWidth(), m_nHeight
,
186 hdcMem
, 0, 0, SRCCOPY
);
189 // #### to do: perhaps using Marlett font (create equiv. font under X)
190 // wxFAIL("not implemented");
191 #endif //O_DRAW_NATIVE_API
195 // for uncheckable item we use only the 'checked' bitmap
196 wxBitmap
bmp(GetBitmap(IsCheckable() ? ((st
& wxODChecked
) != 0) : TRUE
));
198 wxMemoryDC
dcMem(&dc
);
199 dcMem
.SelectObject(bmp
);
202 int nBmpWidth
= bmp
.GetWidth(),
203 nBmpHeight
= bmp
.GetHeight();
205 // there should be enough place!
206 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
208 dc
.Blit(rc
.x
+ (GetMarginWidth() - nBmpWidth
) / 2,
209 rc
.y
+ (m_nHeight
- nBmpHeight
) /2,
210 nBmpWidth
, nBmpHeight
,
211 &dcMem
, 0, 0, wxCOPY
);
213 if ( st
& wxODSelected
) {
214 #ifdef O_DRAW_NATIVE_API
215 RECT rectBmp
= { rc
.GetLeft(), rc
.GetTop(),
216 rc
.GetLeft() + GetMarginWidth(),
217 rc
.GetTop() + m_nHeight
};
218 SetBkColor(hdc
, colBack
);
219 DrawEdge(hdc
, &rectBmp
, EDGE_RAISED
, BF_SOFT
| BF_RECT
);
221 // ## to write portable DrawEdge
222 #endif //O_DRAW_NATIVE_API
227 #ifdef O_DRAW_NATIVE_API
228 ::SetTextColor(hdc
, colOldText
);
229 ::SetBkColor(hdc
, colOldBack
);
232 #endif //O_DRAW_NATIVE_API