]> git.saurik.com Git - wxWidgets.git/blob - src/msw/ownerdrw.cpp
Start of actual filling in of wxFrame. Makefile now supports tiff
[wxWidgets.git] / src / msw / ownerdrw.cpp
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/ownerdrw.h"
35 #include "wx/menuitem.h"
36
37 #if wxUSE_OWNER_DRAWN
38
39
40 // ============================================================================
41 // implementation of wxOwnerDrawn class
42 // ============================================================================
43
44 // ctor
45 // ----
46 wxOwnerDrawn::wxOwnerDrawn(const wxString& str,
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
56 #if defined(__WXMSW__) && defined(__WIN32__) && defined(SM_CXMENUCHECK)
57 size_t wxOwnerDrawn::ms_nDefaultMarginWidth = GetSystemMetrics(SM_CXMENUCHECK);
58 #else // # what is the reasonable default?
59 size_t wxOwnerDrawn::ms_nDefaultMarginWidth = 15;
60 #endif
61
62 size_t wxOwnerDrawn::ms_nLastMarginWidth = ms_nDefaultMarginWidth;
63
64 // drawing
65 // -------
66
67 // get size of the item
68 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight)
69 {
70 wxMemoryDC dc;
71 dc.SetFont(GetFont());
72
73 // ## ugly...
74 wxChar *szStripped = new wxChar[m_strName.Len()];
75 wxStripMenuCodes((wxChar *)m_strName.c_str(), szStripped);
76 wxString str = szStripped;
77 delete [] szStripped;
78
79 // # without this menu items look too tightly packed (at least under Windows)
80 str += wxT('W'); // 'W' is typically the widest letter
81
82 dc.GetTextExtent(str, (long *)pwidth, (long *)pheight);
83
84 // JACS: items still look too tightly packed, so adding 2 pixels.
85 (*pheight) = (*pheight) + 2;
86
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
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
100 #if defined(__WIN32__) && !defined(__SC__) && !defined(__TWIN32__)
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);
146 HBRUSH hbr = CreateSolidBrush(colBack),
147 hPrevBrush = (HBRUSH)SelectObject(hdc, hbr);
148
149 RECT rectAll = { rc.GetLeft(), rc.GetTop(), rc.GetRight(), rc.GetBottom() };
150 FillRect(hdc, &rectAll, hbr);
151
152 DeleteObject(hbr);
153
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
164 HFONT hPrevFont = (HFONT) ::SelectObject(hdc, hfont);
165 DrawState(hdc, NULL, NULL,
166 (LPARAM)(const wxChar *)m_strName, m_strName.Length(),
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
183 #ifdef O_DRAW_NATIVE_API
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 };
194 if ( m_nHeight > 0 )
195 {
196 #ifndef __SC__
197 DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
198 #endif
199 }
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);
204
205 DeleteDC(hdcMem);
206 DeleteObject(hbmpCheck);
207 #else
208 // #### to do: perhaps using Marlett font (create equiv. font under X)
209 // wxFAIL("not implemented");
210 #endif //O_DRAW_NATIVE_API
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
227 //MT: blit with mask enabled.
228 dc.Blit(rc.x + (GetMarginWidth() - nBmpWidth) / 2,
229 rc.y + (m_nHeight - nBmpHeight) /2,
230 nBmpWidth, nBmpHeight,
231 &dcMem, 0, 0, wxCOPY, TRUE);
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
257
258 #endif // wxUSE_OWNER_DRAWN
259