]>
Commit | Line | Data |
---|---|---|
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 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | #include "wx/msw/private.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WX_PRECOMP | |
25 | #include "wx/window.h" | |
26 | // #include "wx/msw/private.h" | |
27 | #include "wx/font.h" | |
28 | #include "wx/bitmap.h" | |
29 | #include "wx/dcmemory.h" | |
30 | #include "wx/menu.h" | |
31 | #include "wx/utils.h" | |
32 | #endif | |
33 | ||
34 | #include "wx/settings.h" | |
35 | #include "wx/ownerdrw.h" | |
36 | #include "wx/menuitem.h" | |
37 | ||
38 | #if wxUSE_OWNER_DRAWN | |
39 | ||
40 | ||
41 | // ============================================================================ | |
42 | // implementation of wxOwnerDrawn class | |
43 | // ============================================================================ | |
44 | ||
45 | // ctor | |
46 | // ---- | |
47 | wxOwnerDrawn::wxOwnerDrawn(const wxString& str, | |
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; | |
55 | if (wxNORMAL_FONT) | |
56 | m_font = * wxNORMAL_FONT; | |
57 | } | |
58 | ||
59 | #if defined(__WXMSW__) && defined(__WIN32__) && defined(SM_CXMENUCHECK) | |
60 | size_t wxOwnerDrawn::ms_nDefaultMarginWidth = GetSystemMetrics(SM_CXMENUCHECK); | |
61 | #else // # what is the reasonable default? | |
62 | size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 15; | |
63 | #endif | |
64 | ||
65 | size_t wxOwnerDrawn::ms_nLastMarginWidth = ms_nDefaultMarginWidth; | |
66 | ||
67 | // drawing | |
68 | // ------- | |
69 | ||
70 | // get size of the item | |
71 | bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight) | |
72 | { | |
73 | wxMemoryDC dc; | |
74 | dc.SetFont(GetFont()); | |
75 | ||
76 | // ## ugly... | |
77 | wxChar *szStripped = new wxChar[m_strName.Len()]; | |
78 | wxStripMenuCodes((wxChar *)m_strName.c_str(), szStripped); | |
79 | wxString str = szStripped; | |
80 | delete [] szStripped; | |
81 | ||
82 | // # without this menu items look too tightly packed (at least under Windows) | |
83 | str += wxT('W'); // 'W' is typically the widest letter | |
84 | ||
85 | dc.GetTextExtent(str, (long *)pwidth, (long *)pheight); | |
86 | ||
87 | // JACS: items still look too tightly packed, so adding 2 pixels. | |
88 | (*pheight) = (*pheight) + 2; | |
89 | ||
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 | ||
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 | ||
103 | #if defined(__WIN32__) && !defined(__SC__) && !defined(__TWIN32__) | |
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); | |
149 | HBRUSH hbr = CreateSolidBrush(colBack), | |
150 | hPrevBrush = (HBRUSH)SelectObject(hdc, hbr); | |
151 | ||
152 | RECT rectAll = { rc.GetLeft(), rc.GetTop(), rc.GetRight(), rc.GetBottom() }; | |
153 | FillRect(hdc, &rectAll, hbr); | |
154 | ||
155 | DeleteObject(hbr); | |
156 | ||
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 | ||
167 | HFONT hPrevFont = (HFONT) ::SelectObject(hdc, hfont); | |
168 | DrawState(hdc, NULL, NULL, | |
169 | (LPARAM)(const wxChar *)m_strName, m_strName.Length(), | |
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 | |
186 | #ifdef O_DRAW_NATIVE_API | |
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 }; | |
197 | if ( m_nHeight > 0 ) | |
198 | { | |
199 | #ifndef __SC__ | |
200 | DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK); | |
201 | #endif | |
202 | } | |
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); | |
207 | ||
208 | DeleteDC(hdcMem); | |
209 | DeleteObject(hbmpCheck); | |
210 | #else | |
211 | // #### to do: perhaps using Marlett font (create equiv. font under X) | |
212 | // wxFAIL("not implemented"); | |
213 | #endif //O_DRAW_NATIVE_API | |
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 | ||
230 | //MT: blit with mask enabled. | |
231 | dc.Blit(rc.x + (GetMarginWidth() - nBmpWidth) / 2, | |
232 | rc.y + (m_nHeight - nBmpHeight) /2, | |
233 | nBmpWidth, nBmpHeight, | |
234 | &dcMem, 0, 0, wxCOPY, TRUE); | |
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 | ||
260 | ||
261 | #endif // wxUSE_OWNER_DRAWN | |
262 |