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