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