]> git.saurik.com Git - wxWidgets.git/blame - src/msw/ownerdrw.cpp
implemented menu drawing in the GTK theme
[wxWidgets.git] / src / msw / ownerdrw.cpp
CommitLineData
2bda0e17
KB
1///////////////////////////////////////////////////////////////////////////////
2// Name: msw/ownerdrw.cpp
3// Purpose: implementation of wxOwnerDrawn class
4// Author: Vadim Zeitlin
33ac7e6f 5// Modified by:
2bda0e17
KB
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"
136cb3c7 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// ----
33ac7e6f
KB
47wxOwnerDrawn::wxOwnerDrawn(const wxString& str,
48 bool bCheckable, bool WXUNUSED(bMenuItem))
2bda0e17
KB
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 65size_t wxOwnerDrawn::ms_nLastMarginWidth = ms_nDefaultMarginWidth;
2bda0e17
KB
66
67// drawing
68// -------
69
70// get size of the item
c86f1403 71bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth, size_t *pheight)
2bda0e17
KB
72{
73 wxMemoryDC dc;
2bda0e17 74
f2ab8671 75 wxString str = wxStripMenuCodes(m_strName);
2bda0e17
KB
76
77 // # without this menu items look too tightly packed (at least under Windows)
223d09f6 78 str += wxT('W'); // 'W' is typically the widest letter
2bda0e17 79
e7dd6ecd
GT
80 if (m_font.Ok())
81 dc.SetFont(GetFont());
82
2bda0e17 83 dc.GetTextExtent(str, (long *)pwidth, (long *)pheight);
c7527e3f
JS
84
85 // JACS: items still look too tightly packed, so adding 2 pixels.
86 (*pheight) = (*pheight) + 2;
87
e7dd6ecd
GT
88 // Ray Gilbert's changes - Corrects the problem of a BMP
89 // being placed next to text in a menu item, and the BMP does
33ac7e6f 90 // not match the size expected by the system. This will
e7dd6ecd
GT
91 // resize the space so the BMP will fit. Without this, BMPs
92 // must be no larger or smaller than 16x16.
93 if (m_bmpChecked.Ok())
94 {
95 // Is BMP height larger then text height?
96 size_t adjustedHeight = m_bmpChecked.GetHeight() +
a756f210 97 wxSystemSettings::GetMetric(wxSYS_EDGE_Y);
e7dd6ecd
GT
98 if (*pheight < adjustedHeight)
99 *pheight = adjustedHeight;
33ac7e6f 100
e7dd6ecd
GT
101 // Does BMP encroach on default check menu position?
102 size_t adjustedWidth = m_bmpChecked.GetWidth() +
a756f210 103 (wxSystemSettings::GetMetric(wxSYS_EDGE_X) * 2);
e7dd6ecd
GT
104 if (ms_nDefaultMarginWidth < adjustedWidth)
105 *pwidth += adjustedWidth - ms_nDefaultMarginWidth;
33ac7e6f 106
e7dd6ecd
GT
107 // Do we need to widen margin to fit BMP?
108 if ((size_t)GetMarginWidth() < adjustedWidth)
109 SetMarginWidth(adjustedWidth);
110 }
33ac7e6f 111
2bda0e17
KB
112 m_nHeight = *pheight; // remember height for use in OnDrawItem
113
114 return TRUE;
115}
116
117// searching for this macro you'll find all the code where I'm using the native
118// Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
119// port this code to X. (VZ)
120
c7527e3f
JS
121// JACS: TODO. Why does a disabled but highlighted item still
122// get drawn embossed? How can we tell DrawState that we don't want the
123// embossing?
124
57c208c5 125#if defined(__WIN32__) && !defined(__SC__) && !defined(__TWIN32__)
2bda0e17
KB
126#define O_DRAW_NATIVE_API // comments below explain why I use it
127#endif
128
129// draw the item
6d5b2a57
VZ
130bool wxOwnerDrawn::OnDrawItem(wxDC& dc,
131 const wxRect& rc,
132 wxODAction act,
133 wxODStatus st)
2bda0e17
KB
134{
135 // we do nothing on focus change
136 if ( act == wxODFocusChanged )
137 return TRUE;
138
139 // wxColor <-> RGB
19193a2c 140 #define ToRGB(col) PALETTERGB(col.Red(), col.Green(), col.Blue())
2bda0e17
KB
141 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
142
143 // set the colors
144 // --------------
145 DWORD colBack, colText;
146 if ( st & wxODSelected ) {
147 colBack = GetSysColor(COLOR_HIGHLIGHT);
148 colText = GetSysColor(COLOR_HIGHLIGHTTEXT);
149 }
150 else {
151 // fall back to default colors if none explicitly specified
152 colBack = m_colBack.Ok() ? ToRGB(m_colBack) : GetSysColor(COLOR_WINDOW);
153 colText = m_colText.Ok() ? ToRGB(m_colText) : GetSysColor(COLOR_WINDOWTEXT);
154 }
33ac7e6f 155
2bda0e17
KB
156 #ifdef O_DRAW_NATIVE_API
157 #define hdc (HDC)dc.GetHDC()
158 COLORREF colOldText = ::SetTextColor(hdc, colText),
159 colOldBack = ::SetBkColor(hdc, colBack);
160 #else
161 dc.SetTextForeground(wxColor(UnRGB(colText)));
162 dc.SetTextBackground(wxColor(UnRGB(colBack)));
163 #endif
164
165 // select the font and draw the text
166 // ---------------------------------
167
33ac7e6f 168 // determine where to draw and leave space for a check-mark.
2bda0e17
KB
169 int x = rc.x + GetMarginWidth();
170
33ac7e6f 171 // using native API because it reckognizes '&'
2bda0e17
KB
172 #ifdef O_DRAW_NATIVE_API
173 int nPrevMode = SetBkMode(hdc, TRANSPARENT);
07cf98cb
VZ
174 HBRUSH hbr = CreateSolidBrush(colBack),
175 hPrevBrush = (HBRUSH)SelectObject(hdc, hbr);
2bda0e17
KB
176
177 RECT rectAll = { rc.GetLeft(), rc.GetTop(), rc.GetRight(), rc.GetBottom() };
178 FillRect(hdc, &rectAll, hbr);
179
07cf98cb
VZ
180 DeleteObject(hbr);
181
2bda0e17
KB
182 // use default font if no font set
183 HFONT hfont;
184 if ( m_font.Ok() ) {
185 m_font.RealizeResource();
186 hfont = (HFONT)m_font.GetResourceHandle();
187 }
188 else {
189 hfont = (HFONT)::GetStockObject(SYSTEM_FONT);
190 }
191
c4e7c2aa 192 HFONT hPrevFont = (HFONT) ::SelectObject(hdc, hfont);
dbc38199 193
dbc38199 194 DrawState(hdc, NULL, NULL,
6d5b2a57 195 (LPARAM)m_strName.c_str(), m_strName.length(),
2bda0e17 196 x, rc.y, rc.GetWidth(), rc.GetHeight(),
6d5b2a57
VZ
197 DST_PREFIXTEXT | (st & wxODDisabled ? DSS_DISABLED : 0));
198
136cb3c7 199 if ( !m_strAccel.empty() )
6d5b2a57
VZ
200 {
201 RECT r;
202 r.top = rc.GetTop();
203 r.left = rc.GetLeft();
204 r.right = rc.GetRight() - GetMarginWidth();
205 r.bottom = rc.GetBottom();
206
207 DrawText(hdc, m_strAccel, m_strAccel.length(), &r,
208 DT_SINGLELINE | DT_RIGHT | DT_VCENTER);
209 }
2bda0e17
KB
210
211 (void)SelectObject(hdc, hPrevBrush);
212 (void)SelectObject(hdc, hPrevFont);
213 (void)SetBkMode(hdc, nPrevMode);
214 #else
215 dc.SetFont(GetFont());
216 dc.DrawText(m_strName, x, rc.y);
217 #endif //O_DRAW_NATIVE_API
218
219 // draw the bitmap
220 // ---------------
221 if ( IsCheckable() && !m_bmpChecked.Ok() ) {
222 if ( st & wxODChecked ) {
223 // using native APIs for performance and simplicity
5260b1c5 224#ifdef O_DRAW_NATIVE_API
33ac7e6f 225 // what goes on: DrawFrameControl creates a b/w mask,
2bda0e17
KB
226 // then we copy it to screen to have right colors
227
228 // first create a monochrome bitmap in a memory DC
229 HDC hdcMem = CreateCompatibleDC(hdc);
230 HBITMAP hbmpCheck = CreateBitmap(GetMarginWidth(), m_nHeight, 1, 1, 0);
231 SelectObject(hdcMem, hbmpCheck);
232
233 // then draw a check mark into it
234 RECT rect = { 0, 0, GetMarginWidth(), m_nHeight };
07cf98cb
VZ
235 if ( m_nHeight > 0 )
236 {
2432b92d 237#ifndef __SC__
07cf98cb 238 DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
2432b92d 239#endif
07cf98cb 240 }
2bda0e17
KB
241
242 // finally copy it to screen DC and clean up
33ac7e6f 243 BitBlt(hdc, rc.x, rc.y, GetMarginWidth(), m_nHeight,
2bda0e17 244 hdcMem, 0, 0, SRCCOPY);
07cf98cb 245
2bda0e17 246 DeleteDC(hdcMem);
07cf98cb 247 DeleteObject(hbmpCheck);
5260b1c5 248#else
2bda0e17 249 // #### to do: perhaps using Marlett font (create equiv. font under X)
5260b1c5
JS
250// wxFAIL("not implemented");
251#endif //O_DRAW_NATIVE_API
2bda0e17
KB
252 }
253 }
254 else {
255 // for uncheckable item we use only the 'checked' bitmap
256 wxBitmap bmp(GetBitmap(IsCheckable() ? ((st & wxODChecked) != 0) : TRUE));
257 if ( bmp.Ok() ) {
258 wxMemoryDC dcMem(&dc);
259 dcMem.SelectObject(bmp);
260
261 // center bitmap
262 int nBmpWidth = bmp.GetWidth(),
263 nBmpHeight = bmp.GetHeight();
264
265 // there should be enough place!
266 wxASSERT((nBmpWidth <= rc.GetWidth()) && (nBmpHeight <= rc.GetHeight()));
267
e7dd6ecd
GT
268 int heightDiff = (m_nHeight - nBmpHeight);
269// if (heightDiff = -1)
270// heightDiff = -2;
271
ebb206a3 272 //MT: blit with mask enabled.
33ac7e6f
KB
273 dc.Blit(rc.x + (GetMarginWidth() - nBmpWidth) / 2,
274 rc.y + heightDiff / 2,
275 nBmpWidth, nBmpHeight,
0cdf89ab 276 &dcMem, 0, 0, wxCOPY, TRUE);
2bda0e17
KB
277
278 if ( st & wxODSelected ) {
279 #ifdef O_DRAW_NATIVE_API
33ac7e6f
KB
280 RECT rectBmp = { rc.GetLeft(), rc.GetTop(),
281 rc.GetLeft() + GetMarginWidth(),
2bda0e17
KB
282 rc.GetTop() + m_nHeight };
283 SetBkColor(hdc, colBack);
284 DrawEdge(hdc, &rectBmp, EDGE_RAISED, BF_SOFT | BF_RECT);
285 #else
286 // ## to write portable DrawEdge
287 #endif //O_DRAW_NATIVE_API
288 }
289 }
290 }
291
292 #ifdef O_DRAW_NATIVE_API
293 ::SetTextColor(hdc, colOldText);
294 ::SetBkColor(hdc, colOldBack);
295
296 #undef hdc
297 #endif //O_DRAW_NATIVE_API
298
299 return TRUE;
300}
301
1b24a68e
GRG
302
303#endif // wxUSE_OWNER_DRAWN
304