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