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