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