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