]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ownerdrw.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/ownerdrw.cpp
3 // Purpose: implementation of wxOwnerDrawn class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
24 #include "wx/window.h"
25 #include "wx/msw/private.h"
27 #include "wx/bitmap.h"
28 #include "wx/dcmemory.h"
33 #include "wx/ownerdrw.h"
34 #include "wx/menuitem.h"
37 // ============================================================================
38 // implementation of wxOwnerDrawn class
39 // ============================================================================
43 wxOwnerDrawn::wxOwnerDrawn(const wxString
& str
,
44 bool bCheckable
, bool bMenuItem
)
47 m_bCheckable
= bCheckable
;
48 m_bOwnerDrawn
= FALSE
;
50 m_nMarginWidth
= ms_nLastMarginWidth
;
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;
59 size_t wxOwnerDrawn::ms_nLastMarginWidth
= ms_nDefaultMarginWidth
;
64 // get size of the item
65 bool wxOwnerDrawn::OnMeasureItem(size_t *pwidth
, size_t *pheight
)
68 dc
.SetFont(GetFont());
71 wxChar
*szStripped
= new wxChar
[m_strName
.Len()];
72 wxStripMenuCodes((wxChar
*)m_strName
.c_str(), szStripped
);
73 wxString str
= szStripped
;
76 // # without this menu items look too tightly packed (at least under Windows)
77 str
+= _T('W'); // 'W' is typically the widest letter
79 dc
.GetTextExtent(str
, (long *)pwidth
, (long *)pheight
);
80 m_nHeight
= *pheight
; // remember height for use in OnDrawItem
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)
89 #if defined(__WIN32__) && !defined(__SC__) && !defined(__TWIN32__)
90 #define O_DRAW_NATIVE_API // comments below explain why I use it
94 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus st
)
96 // we do nothing on focus change
97 if ( act
== wxODFocusChanged
)
101 #define ToRGB(col) RGB(col.Red(), col.Green(), col.Blue())
102 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
106 DWORD colBack
, colText
;
107 if ( st
& wxODSelected
) {
108 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
109 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
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
);
117 #ifdef O_DRAW_NATIVE_API
118 #define hdc (HDC)dc.GetHDC()
119 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
120 colOldBack
= ::SetBkColor(hdc
, colBack
);
122 dc
.SetTextForeground(wxColor(UnRGB(colText
)));
123 dc
.SetTextBackground(wxColor(UnRGB(colBack
)));
126 // select the font and draw the text
127 // ---------------------------------
129 // determine where to draw and leave space for a check-mark.
130 int x
= rc
.x
+ GetMarginWidth();
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
);
138 RECT rectAll
= { rc
.GetLeft(), rc
.GetTop(), rc
.GetRight(), rc
.GetBottom() };
139 FillRect(hdc
, &rectAll
, hbr
);
141 // use default font if no font set
144 m_font
.RealizeResource();
145 hfont
= (HFONT
)m_font
.GetResourceHandle();
148 hfont
= (HFONT
)::GetStockObject(SYSTEM_FONT
);
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) );
157 (void)SelectObject(hdc
, hPrevBrush
);
158 (void)SelectObject(hdc
, hPrevFont
);
159 (void)SetBkMode(hdc
, nPrevMode
);
161 dc
.SetFont(GetFont());
162 dc
.DrawText(m_strName
, x
, rc
.y
);
163 #endif //O_DRAW_NATIVE_API
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
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
);
179 // then draw a check mark into it
180 RECT rect
= { 0, 0, GetMarginWidth(), m_nHeight
};
182 DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
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
);
190 // #### to do: perhaps using Marlett font (create equiv. font under X)
191 // wxFAIL("not implemented");
192 #endif //O_DRAW_NATIVE_API
196 // for uncheckable item we use only the 'checked' bitmap
197 wxBitmap
bmp(GetBitmap(IsCheckable() ? ((st
& wxODChecked
) != 0) : TRUE
));
199 wxMemoryDC
dcMem(&dc
);
200 dcMem
.SelectObject(bmp
);
203 int nBmpWidth
= bmp
.GetWidth(),
204 nBmpHeight
= bmp
.GetHeight();
206 // there should be enough place!
207 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
209 dc
.Blit(rc
.x
+ (GetMarginWidth() - nBmpWidth
) / 2,
210 rc
.y
+ (m_nHeight
- nBmpHeight
) /2,
211 nBmpWidth
, nBmpHeight
,
212 &dcMem
, 0, 0, wxCOPY
);
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
);
222 // ## to write portable DrawEdge
223 #endif //O_DRAW_NATIVE_API
228 #ifdef O_DRAW_NATIVE_API
229 ::SetTextColor(hdc
, colOldText
);
230 ::SetBkColor(hdc
, colOldBack
);
233 #endif //O_DRAW_NATIVE_API