]>
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 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/ownerdrw.h"
28 #include "wx/menuitem.h"
32 // ============================================================================
33 // implementation of wxOwnerDrawn class
34 // ============================================================================
38 wxOwnerDrawn::wxOwnerDrawn(const wxTString
& str
,
39 bool bCheckable
, bool bMenuItem
)
42 m_bCheckable
= bCheckable
;
43 m_bOwnerDrawn
= FALSE
;
45 m_nMarginWidth
= ms_nLastMarginWidth
;
48 #if defined(__WINDOWS__) && defined(__WIN32__)
49 uint
wxOwnerDrawn::ms_nDefaultMarginWidth
= GetSystemMetrics(SM_CXMENUCHECK
);
50 #else // # what is the reasonable default?
51 uint
wxOwnerDrawn::ms_nDefaultMarginWidth
= 15;
54 uint
wxOwnerDrawn::ms_nLastMarginWidth
= ms_nDefaultMarginWidth
;
59 // get size of the item
60 bool wxOwnerDrawn::OnMeasureItem(uint
*pwidth
, uint
*pheight
)
63 dc
.SetFont(GetFont());
66 char *szStripped
= new char[m_strName
.Len()];
67 wxStripMenuCodes((char *)m_strName
.c_str(), szStripped
);
68 wxString str
= szStripped
;
71 // # without this menu items look too tightly packed (at least under Windows)
72 str
+= 'W'; // 'W' is typically the widest letter
74 dc
.GetTextExtent(str
, (long *)pwidth
, (long *)pheight
);
75 m_nHeight
= *pheight
; // remember height for use in OnDrawItem
80 // searching for this macro you'll find all the code where I'm using the native
81 // Win32 GDI functions and not wxWindows ones. Might help to whoever decides to
82 // port this code to X. (VZ)
85 #define O_DRAW_NATIVE_API // comments below explain why I use it
89 bool wxOwnerDrawn::OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus st
)
91 // we do nothing on focus change
92 if ( act
== wxODFocusChanged
)
96 #define ToRGB(col) RGB(col.Red(), col.Green(), col.Blue())
97 #define UnRGB(col) GetRValue(col), GetGValue(col), GetBValue(col)
101 DWORD colBack
, colText
;
102 if ( st
& wxODSelected
) {
103 colBack
= GetSysColor(COLOR_HIGHLIGHT
);
104 colText
= GetSysColor(COLOR_HIGHLIGHTTEXT
);
107 // fall back to default colors if none explicitly specified
108 colBack
= m_colBack
.Ok() ? ToRGB(m_colBack
) : GetSysColor(COLOR_WINDOW
);
109 colText
= m_colText
.Ok() ? ToRGB(m_colText
) : GetSysColor(COLOR_WINDOWTEXT
);
112 #ifdef O_DRAW_NATIVE_API
113 #define hdc (HDC)dc.GetHDC()
114 COLORREF colOldText
= ::SetTextColor(hdc
, colText
),
115 colOldBack
= ::SetBkColor(hdc
, colBack
);
117 dc
.SetTextForeground(wxColor(UnRGB(colText
)));
118 dc
.SetTextBackground(wxColor(UnRGB(colBack
)));
121 // select the font and draw the text
122 // ---------------------------------
124 // determine where to draw and leave space for a check-mark.
125 int x
= rc
.x
+ GetMarginWidth();
127 // using native API because it reckognizes '&'
128 #ifdef O_DRAW_NATIVE_API
129 int nPrevMode
= SetBkMode(hdc
, TRANSPARENT
);
130 HBRUSH hbr
= CreateSolidBrush(colBack
),
131 hPrevBrush
= SelectObject(hdc
, hbr
);
133 RECT rectAll
= { rc
.GetLeft(), rc
.GetTop(), rc
.GetRight(), rc
.GetBottom() };
134 FillRect(hdc
, &rectAll
, hbr
);
136 // use default font if no font set
139 m_font
.RealizeResource();
140 hfont
= (HFONT
)m_font
.GetResourceHandle();
143 hfont
= (HFONT
)::GetStockObject(SYSTEM_FONT
);
146 HFONT hPrevFont
= ::SelectObject(hdc
, hfont
);
147 DrawState(hdc
, NULL
, NULL
,
148 (LPARAM
)(const char *)m_strName
, m_strName
.Length(),
149 x
, rc
.y
, rc
.GetWidth(), rc
.GetHeight(),
150 DST_PREFIXTEXT
| ( st
& wxODDisabled
? DSS_DISABLED
: 0) );
152 (void)SelectObject(hdc
, hPrevBrush
);
153 (void)SelectObject(hdc
, hPrevFont
);
154 (void)SetBkMode(hdc
, nPrevMode
);
156 dc
.SetFont(GetFont());
157 dc
.DrawText(m_strName
, x
, rc
.y
);
158 #endif //O_DRAW_NATIVE_API
162 if ( IsCheckable() && !m_bmpChecked
.Ok() ) {
163 if ( st
& wxODChecked
) {
164 // using native APIs for performance and simplicity
165 #ifdef O_DRAW_NATIVE_API
166 // what goes on: DrawFrameControl creates a b/w mask,
167 // then we copy it to screen to have right colors
169 // first create a monochrome bitmap in a memory DC
170 HDC hdcMem
= CreateCompatibleDC(hdc
);
171 HBITMAP hbmpCheck
= CreateBitmap(GetMarginWidth(), m_nHeight
, 1, 1, 0);
172 SelectObject(hdcMem
, hbmpCheck
);
174 // then draw a check mark into it
175 RECT rect
= { 0, 0, GetMarginWidth(), m_nHeight
};
176 DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
178 // finally copy it to screen DC and clean up
179 BitBlt(hdc
, rc
.x
, rc
.y
, GetMarginWidth(), m_nHeight
,
180 hdcMem
, 0, 0, SRCCOPY
);
183 // #### to do: perhaps using Marlett font (create equiv. font under X)
184 // wxFAIL("not implemented");
185 #endif //O_DRAW_NATIVE_API
189 // for uncheckable item we use only the 'checked' bitmap
190 wxBitmap
bmp(GetBitmap(IsCheckable() ? ((st
& wxODChecked
) != 0) : TRUE
));
192 wxMemoryDC
dcMem(&dc
);
193 dcMem
.SelectObject(bmp
);
196 int nBmpWidth
= bmp
.GetWidth(),
197 nBmpHeight
= bmp
.GetHeight();
199 // there should be enough place!
200 wxASSERT((nBmpWidth
<= rc
.GetWidth()) && (nBmpHeight
<= rc
.GetHeight()));
202 dc
.Blit(rc
.x
+ (GetMarginWidth() - nBmpWidth
) / 2,
203 rc
.y
+ (m_nHeight
- nBmpHeight
) /2,
204 nBmpWidth
, nBmpHeight
,
205 &dcMem
, 0, 0, wxCOPY
);
207 if ( st
& wxODSelected
) {
208 #ifdef O_DRAW_NATIVE_API
209 RECT rectBmp
= { rc
.GetLeft(), rc
.GetTop(),
210 rc
.GetLeft() + GetMarginWidth(),
211 rc
.GetTop() + m_nHeight
};
212 SetBkColor(hdc
, colBack
);
213 DrawEdge(hdc
, &rectBmp
, EDGE_RAISED
, BF_SOFT
| BF_RECT
);
215 // ## to write portable DrawEdge
216 #endif //O_DRAW_NATIVE_API
221 #ifdef O_DRAW_NATIVE_API
222 ::SetTextColor(hdc
, colOldText
);
223 ::SetBkColor(hdc
, colOldBack
);
226 #endif //O_DRAW_NATIVE_API