]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: msw/ownerdrw.cpp | |
3 | // Purpose: implementation of wxOwnerDrawn class | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 13.11.97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // Licence: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
a3b46648 UU |
12 | #ifdef __GNUG__ |
13 | #pragma implementation | |
14 | #endif | |
2bda0e17 KB |
15 | |
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
a3b46648 | 20 | #pragma hdrstop |
2bda0e17 KB |
21 | #endif |
22 | ||
23 | #ifndef WX_PRECOMP | |
0c589ad0 BM |
24 | #include "wx/window.h" |
25 | #include "wx/msw/private.h" | |
2432b92d JS |
26 | #include "wx/font.h" |
27 | #include "wx/bitmap.h" | |
28 | #include "wx/dcmemory.h" | |
2bda0e17 | 29 | #include "wx/menu.h" |
2432b92d | 30 | #include "wx/utils.h" |
2bda0e17 KB |
31 | #endif |
32 | ||
33 | #include "wx/ownerdrw.h" | |
34 | #include "wx/menuitem.h" | |
35 | ||
1b24a68e GRG |
36 | #if wxUSE_OWNER_DRAWN |
37 | ||
2432b92d | 38 | |
2bda0e17 KB |
39 | // ============================================================================ |
40 | // implementation of wxOwnerDrawn class | |
41 | // ============================================================================ | |
42 | ||
43 | // ctor | |
44 | // ---- | |
3aadbb82 | 45 | wxOwnerDrawn::wxOwnerDrawn(const wxString& str, |
2bda0e17 KB |
46 | bool bCheckable, bool bMenuItem) |
47 | : m_strName(str) | |
48 | { | |
49 | m_bCheckable = bCheckable; | |
50 | m_bOwnerDrawn = FALSE; | |
51 | m_nHeight = 0; | |
52 | m_nMarginWidth = ms_nLastMarginWidth; | |
53 | } | |
54 | ||
2432b92d | 55 | #if defined(__WXMSW__) && defined(__WIN32__) && defined(SM_CXMENUCHECK) |
c86f1403 | 56 | size_t wxOwnerDrawn::ms_nDefaultMarginWidth = GetSystemMetrics(SM_CXMENUCHECK); |
2bda0e17 | 57 | #else // # what is the reasonable default? |
c86f1403 | 58 | size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 15; |
2bda0e17 KB |
59 | #endif |
60 | ||
c86f1403 | 61 | size_t wxOwnerDrawn::ms_nLastMarginWidth = ms_nDefaultMarginWidth; |
2bda0e17 KB |
62 | |
63 | // drawing | |
64 | // ------- | |
65 | ||
66 | // get size of the item | |
c86f1403 | 67 | bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight) |
2bda0e17 KB |
68 | { |
69 | wxMemoryDC dc; | |
70 | dc.SetFont(GetFont()); | |
71 | ||
72 | // ## ugly... | |
837e5743 OK |
73 | wxChar *szStripped = new wxChar[m_strName.Len()]; |
74 | wxStripMenuCodes((wxChar *)m_strName.c_str(), szStripped); | |
2bda0e17 KB |
75 | wxString str = szStripped; |
76 | delete [] szStripped; | |
77 | ||
78 | // # without this menu items look too tightly packed (at least under Windows) | |
223d09f6 | 79 | str += wxT('W'); // 'W' is typically the widest letter |
2bda0e17 KB |
80 | |
81 | dc.GetTextExtent(str, (long *)pwidth, (long *)pheight); | |
c7527e3f JS |
82 | |
83 | // JACS: items still look too tightly packed, so adding 2 pixels. | |
84 | (*pheight) = (*pheight) + 2; | |
85 | ||
2bda0e17 KB |
86 | m_nHeight = *pheight; // remember height for use in OnDrawItem |
87 | ||
88 | return TRUE; | |
89 | } | |
90 | ||
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) | |
94 | ||
c7527e3f JS |
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 | |
97 | // embossing? | |
98 | ||
57c208c5 | 99 | #if defined(__WIN32__) && !defined(__SC__) && !defined(__TWIN32__) |
2bda0e17 KB |
100 | #define O_DRAW_NATIVE_API // comments below explain why I use it |
101 | #endif | |
102 | ||
103 | // draw the item | |
104 | bool wxOwnerDrawn::OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus st) | |
105 | { | |
106 | // we do nothing on focus change | |
107 | if ( act == wxODFocusChanged ) | |
108 | return TRUE; | |
109 | ||
110 | // wxColor <-> RGB | |
111 | #define ToRGB(col) RGB(col.Red(), col.Green(), col.Blue()) | |
112 | #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col) | |
113 | ||
114 | // set the colors | |
115 | // -------------- | |
116 | DWORD colBack, colText; | |
117 | if ( st & wxODSelected ) { | |
118 | colBack = GetSysColor(COLOR_HIGHLIGHT); | |
119 | colText = GetSysColor(COLOR_HIGHLIGHTTEXT); | |
120 | } | |
121 | else { | |
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); | |
125 | } | |
126 | ||
127 | #ifdef O_DRAW_NATIVE_API | |
128 | #define hdc (HDC)dc.GetHDC() | |
129 | COLORREF colOldText = ::SetTextColor(hdc, colText), | |
130 | colOldBack = ::SetBkColor(hdc, colBack); | |
131 | #else | |
132 | dc.SetTextForeground(wxColor(UnRGB(colText))); | |
133 | dc.SetTextBackground(wxColor(UnRGB(colBack))); | |
134 | #endif | |
135 | ||
136 | // select the font and draw the text | |
137 | // --------------------------------- | |
138 | ||
139 | // determine where to draw and leave space for a check-mark. | |
140 | int x = rc.x + GetMarginWidth(); | |
141 | ||
142 | // using native API because it reckognizes '&' | |
143 | #ifdef O_DRAW_NATIVE_API | |
144 | int nPrevMode = SetBkMode(hdc, TRANSPARENT); | |
07cf98cb VZ |
145 | HBRUSH hbr = CreateSolidBrush(colBack), |
146 | hPrevBrush = (HBRUSH)SelectObject(hdc, hbr); | |
2bda0e17 KB |
147 | |
148 | RECT rectAll = { rc.GetLeft(), rc.GetTop(), rc.GetRight(), rc.GetBottom() }; | |
149 | FillRect(hdc, &rectAll, hbr); | |
150 | ||
07cf98cb VZ |
151 | DeleteObject(hbr); |
152 | ||
2bda0e17 KB |
153 | // use default font if no font set |
154 | HFONT hfont; | |
155 | if ( m_font.Ok() ) { | |
156 | m_font.RealizeResource(); | |
157 | hfont = (HFONT)m_font.GetResourceHandle(); | |
158 | } | |
159 | else { | |
160 | hfont = (HFONT)::GetStockObject(SYSTEM_FONT); | |
161 | } | |
162 | ||
c4e7c2aa | 163 | HFONT hPrevFont = (HFONT) ::SelectObject(hdc, hfont); |
2bda0e17 | 164 | DrawState(hdc, NULL, NULL, |
837e5743 | 165 | (LPARAM)(const wxChar *)m_strName, m_strName.Length(), |
2bda0e17 KB |
166 | x, rc.y, rc.GetWidth(), rc.GetHeight(), |
167 | DST_PREFIXTEXT | ( st & wxODDisabled ? DSS_DISABLED : 0) ); | |
168 | ||
169 | (void)SelectObject(hdc, hPrevBrush); | |
170 | (void)SelectObject(hdc, hPrevFont); | |
171 | (void)SetBkMode(hdc, nPrevMode); | |
172 | #else | |
173 | dc.SetFont(GetFont()); | |
174 | dc.DrawText(m_strName, x, rc.y); | |
175 | #endif //O_DRAW_NATIVE_API | |
176 | ||
177 | // draw the bitmap | |
178 | // --------------- | |
179 | if ( IsCheckable() && !m_bmpChecked.Ok() ) { | |
180 | if ( st & wxODChecked ) { | |
181 | // using native APIs for performance and simplicity | |
5260b1c5 | 182 | #ifdef O_DRAW_NATIVE_API |
2bda0e17 KB |
183 | // what goes on: DrawFrameControl creates a b/w mask, |
184 | // then we copy it to screen to have right colors | |
185 | ||
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); | |
190 | ||
191 | // then draw a check mark into it | |
192 | RECT rect = { 0, 0, GetMarginWidth(), m_nHeight }; | |
07cf98cb VZ |
193 | if ( m_nHeight > 0 ) |
194 | { | |
2432b92d | 195 | #ifndef __SC__ |
07cf98cb | 196 | DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK); |
2432b92d | 197 | #endif |
07cf98cb | 198 | } |
2bda0e17 KB |
199 | |
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); | |
07cf98cb | 203 | |
2bda0e17 | 204 | DeleteDC(hdcMem); |
07cf98cb | 205 | DeleteObject(hbmpCheck); |
5260b1c5 | 206 | #else |
2bda0e17 | 207 | // #### to do: perhaps using Marlett font (create equiv. font under X) |
5260b1c5 JS |
208 | // wxFAIL("not implemented"); |
209 | #endif //O_DRAW_NATIVE_API | |
2bda0e17 KB |
210 | } |
211 | } | |
212 | else { | |
213 | // for uncheckable item we use only the 'checked' bitmap | |
214 | wxBitmap bmp(GetBitmap(IsCheckable() ? ((st & wxODChecked) != 0) : TRUE)); | |
215 | if ( bmp.Ok() ) { | |
216 | wxMemoryDC dcMem(&dc); | |
217 | dcMem.SelectObject(bmp); | |
218 | ||
219 | // center bitmap | |
220 | int nBmpWidth = bmp.GetWidth(), | |
221 | nBmpHeight = bmp.GetHeight(); | |
222 | ||
223 | // there should be enough place! | |
224 | wxASSERT((nBmpWidth <= rc.GetWidth()) && (nBmpHeight <= rc.GetHeight())); | |
225 | ||
ebb206a3 | 226 | //MT: blit with mask enabled. |
2bda0e17 KB |
227 | dc.Blit(rc.x + (GetMarginWidth() - nBmpWidth) / 2, |
228 | rc.y + (m_nHeight - nBmpHeight) /2, | |
229 | nBmpWidth, nBmpHeight, | |
0cdf89ab | 230 | &dcMem, 0, 0, wxCOPY, TRUE); |
2bda0e17 KB |
231 | |
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); | |
239 | #else | |
240 | // ## to write portable DrawEdge | |
241 | #endif //O_DRAW_NATIVE_API | |
242 | } | |
243 | } | |
244 | } | |
245 | ||
246 | #ifdef O_DRAW_NATIVE_API | |
247 | ::SetTextColor(hdc, colOldText); | |
248 | ::SetBkColor(hdc, colOldBack); | |
249 | ||
250 | #undef hdc | |
251 | #endif //O_DRAW_NATIVE_API | |
252 | ||
253 | return TRUE; | |
254 | } | |
255 | ||
1b24a68e GRG |
256 | |
257 | #endif // wxUSE_OWNER_DRAWN | |
258 |