mask corrections
[wxWidgets.git] / src / msw / bmpbuttn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: bmpbuttn.cpp
3 // Purpose: wxBitmapButton
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "bmpbuttn.h"
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/bmpbuttn.h"
25 #endif
26
27 #include "wx/msw/private.h"
28
29 #if !USE_SHARED_LIBRARY
30 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
31 #endif
32
33 #define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1)
34
35 bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id, const wxBitmap& bitmap,
36 const wxPoint& pos,
37 const wxSize& size, long style,
38 const wxValidator& validator,
39 const wxString& name)
40 {
41 m_buttonBitmap = bitmap;
42 SetName(name);
43 SetValidator(validator);
44
45 parent->AddChild(this);
46
47 m_backgroundColour = parent->GetBackgroundColour() ;
48 m_foregroundColour = parent->GetForegroundColour() ;
49 m_windowStyle = style;
50 m_marginX = 0;
51 m_marginY = 0;
52
53 if ( style & wxBU_AUTODRAW )
54 {
55 m_marginX = wxDEFAULT_BUTTON_MARGIN;
56 m_marginY = wxDEFAULT_BUTTON_MARGIN;
57 }
58
59 int x = pos.x;
60 int y = pos.y;
61 int width = size.x;
62 int height = size.y;
63
64 if (id == -1)
65 m_windowId = NewControlId();
66 else
67 m_windowId = id;
68
69 if ( width == -1 && bitmap.Ok())
70 width = bitmap.GetWidth() + 2*m_marginX;
71
72 if ( height == -1 && bitmap.Ok())
73 height = bitmap.GetHeight() + 2*m_marginY;
74
75 m_hWnd = (WXHWND)CreateWindowEx
76 (
77 0,
78 wxT("BUTTON"),
79 wxT(""),
80 WS_VISIBLE | WS_TABSTOP | WS_CHILD | BS_OWNERDRAW ,
81 0, 0, 0, 0,
82 GetWinHwnd(parent),
83 (HMENU)m_windowId,
84 wxGetInstance(),
85 NULL
86 );
87
88 // Subclass again for purposes of dialog editing mode
89 SubclassWin(m_hWnd);
90
91 SetFont(parent->GetFont()) ;
92
93 SetSize(x, y, width, height);
94
95 return TRUE;
96 }
97
98 void wxBitmapButton::SetBitmapLabel(const wxBitmap& bitmap)
99 {
100 m_buttonBitmap = bitmap;
101 }
102
103 bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT *item)
104 {
105 #if defined(__WIN95__)
106 long style = GetWindowLong((HWND) GetHWND(), GWL_STYLE);
107 if (style & BS_BITMAP)
108 {
109 // Let default procedure draw the bitmap, which is defined
110 // in the Windows resource.
111 return FALSE;
112 }
113 #endif
114
115 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
116
117 // choose the bitmap to use depending on the buttons state
118 wxBitmap* bitmap;
119
120 UINT state = lpDIS->itemState;
121 bool isSelected = (state & ODS_SELECTED) != 0;
122 if ( isSelected && m_buttonBitmapSelected.Ok() )
123 bitmap = &m_buttonBitmapSelected;
124 else if ((state & ODS_FOCUS) && m_buttonBitmapFocus.Ok())
125 bitmap = &m_buttonBitmapFocus;
126 else if ((state & ODS_DISABLED) && m_buttonBitmapDisabled.Ok())
127 bitmap = &m_buttonBitmapDisabled;
128 else
129 bitmap = &m_buttonBitmap;
130
131 if ( !bitmap->Ok() )
132 return FALSE;
133
134 // draw it on the memory DC
135 HDC hDC = lpDIS->hDC;
136 HDC memDC = ::CreateCompatibleDC(hDC);
137
138 HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP());
139
140 if (!old)
141 {
142 wxLogLastError(_T("SelectObject"));
143
144 return FALSE;
145 }
146
147 int x = lpDIS->rcItem.left;
148 int y = lpDIS->rcItem.top;
149 int width = lpDIS->rcItem.right - x;
150 int height = lpDIS->rcItem.bottom - y;
151
152 int wBmp = bitmap->GetWidth(),
153 hBmp = bitmap->GetHeight();
154
155 // Draw the face, if auto-drawing
156 bool autoDraw = (GetWindowStyleFlag() & wxBU_AUTODRAW) != 0;
157 if ( autoDraw )
158 {
159 DrawFace((WXHDC) hDC,
160 lpDIS->rcItem.left, lpDIS->rcItem.top,
161 lpDIS->rcItem.right, lpDIS->rcItem.bottom,
162 isSelected);
163 }
164
165 // Centre the bitmap in the control area
166 int x1 = x + (width - wBmp) / 2;
167 int y1 = y + (height - hBmp) / 2;
168
169 if ( isSelected && autoDraw )
170 {
171 x1++;
172 y1++;
173 }
174
175 BOOL ok;
176 wxMask *mask = bitmap->GetMask();
177 if ( mask )
178 {
179 // the fg ROP is applied for the pixels of the mask bitmap which are 1
180 // (for a wxMask this means that this is a non transparent pixel), the
181 // bg ROP is applied for all the others
182 ok = ::MaskBlt(
183 hDC, x1, y1, wBmp, hBmp, // dst
184 memDC, 0, 0, // src
185 (HBITMAP)mask->GetMaskBitmap(), 0, 0, // mask
186 MAKEROP4(SRCCOPY, // fg ROP
187 SRCPAINT) // bg ROP
188 );
189 }
190 else
191 {
192 ok = ::BitBlt(hDC, x1, y1, wBmp, hBmp, // dst
193 memDC, 0, 0, // src
194 SRCCOPY); // ROP
195 }
196
197 if ( !ok )
198 {
199 wxLogLastError(_T("Mask/BitBlt()"));
200 }
201
202 if ( (state & ODS_DISABLED) && autoDraw )
203 {
204 DrawButtonDisable((WXHDC) hDC,
205 lpDIS->rcItem.left, lpDIS->rcItem.top,
206 lpDIS->rcItem.right, lpDIS->rcItem.bottom,
207 TRUE);
208 }
209 else if ( (state & ODS_FOCUS) && autoDraw )
210 {
211 DrawButtonFocus((WXHDC) hDC,
212 lpDIS->rcItem.left,
213 lpDIS->rcItem.top,
214 lpDIS->rcItem.right,
215 lpDIS->rcItem.bottom,
216 isSelected);
217 }
218
219 ::SelectObject(memDC, old);
220
221 ::DeleteDC(memDC);
222
223 return TRUE;
224 }
225
226 void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, int right, int bottom, bool sel )
227 {
228 HPEN oldp;
229 HBRUSH oldb ;
230
231 HPEN penBorder;
232 HPEN penLight;
233 HPEN penShadow;
234 HBRUSH brushFace;
235 COLORREF ms_color;
236
237 ms_color = GetSysColor(COLOR_WINDOWFRAME) ;
238 penBorder = CreatePen(PS_SOLID,0,ms_color) ;
239
240 ms_color = GetSysColor(COLOR_BTNSHADOW) ;
241 penShadow = CreatePen(PS_SOLID,0,ms_color) ;
242
243 ms_color = GetSysColor(COLOR_BTNHIGHLIGHT) ;
244 penLight = CreatePen(PS_SOLID,0,ms_color) ;
245
246 ms_color = GetSysColor(COLOR_BTNFACE) ;
247 brushFace = CreateSolidBrush(ms_color) ;
248
249 oldp = (HPEN) SelectObject( (HDC) dc, GetStockObject( NULL_PEN ) ) ;
250 oldb = (HBRUSH) SelectObject( (HDC) dc, brushFace ) ;
251 Rectangle( (HDC) dc, left, top, right, bottom ) ;
252 SelectObject( (HDC) dc, penBorder) ;
253 MoveToEx((HDC) dc,left+1,top,NULL);LineTo((HDC) dc,right-1,top);
254 MoveToEx((HDC) dc,left,top+1,NULL);LineTo((HDC) dc,left,bottom-1);
255 MoveToEx((HDC) dc,left+1,bottom-1,NULL);LineTo((HDC) dc,right-1,bottom-1);
256 MoveToEx((HDC) dc,right-1,top+1,NULL);LineTo((HDC) dc,right-1,bottom-1);
257
258 SelectObject( (HDC) dc, penShadow) ;
259 if (sel)
260 {
261 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL) ;
262 LineTo((HDC) dc, left+1 ,top+1) ;
263 LineTo((HDC) dc, right-2 ,top+1) ;
264 }
265 else
266 {
267 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL) ;
268 LineTo((HDC) dc, right-2 ,bottom-2) ;
269 LineTo((HDC) dc, right-2 ,top) ;
270 MoveToEx((HDC) dc,left+2 ,bottom-3 ,NULL) ;
271 LineTo((HDC) dc, right-3 ,bottom-3) ;
272 LineTo((HDC) dc, right-3 ,top+1) ;
273
274 SelectObject( (HDC) dc, penLight) ;
275
276 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL) ;
277 LineTo((HDC) dc, left+1 ,top+1) ;
278 LineTo((HDC) dc, right-2 ,top+1) ;
279 }
280 SelectObject((HDC) dc,oldp) ;
281 SelectObject((HDC) dc,oldb) ;
282
283 DeleteObject(penBorder);
284 DeleteObject(penLight);
285 DeleteObject(penShadow);
286 DeleteObject(brushFace);
287 }
288
289 // VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
290 #define FOCUS_MARGIN 3
291
292 void wxBitmapButton::DrawButtonFocus( WXHDC dc, int left, int top, int right, int bottom, bool sel )
293 {
294 RECT rect;
295 rect.left = left;
296 rect.top = top;
297 rect.right = right;
298 rect.bottom = bottom;
299 InflateRect( &rect, - FOCUS_MARGIN, - FOCUS_MARGIN ) ;
300 if ( sel )
301 OffsetRect( &rect, 1, 1 ) ;
302 DrawFocusRect( (HDC) dc, &rect ) ;
303 }
304
305 extern HBRUSH wxDisableButtonBrush;
306 void wxBitmapButton::DrawButtonDisable( WXHDC dc, int left, int top, int right, int bottom, bool with_marg )
307 {
308 HBRUSH old = (HBRUSH) SelectObject( (HDC) dc, wxDisableButtonBrush ) ;
309
310 // VZ: what's this?? there is no such ROP AFAIK
311 #ifdef __SALFORDC__
312 DWORD dwRop = 0xFA0089L;
313 #else
314 DWORD dwRop = 0xFA0089UL;
315 #endif
316
317 if ( with_marg )
318 {
319 left += m_marginX;
320 top += m_marginY;
321 right -= 2 * m_marginX;
322 bottom -= 2 * m_marginY;
323 }
324
325 ::PatBlt( (HDC) dc, left, top, right, bottom, dwRop);
326
327 ::SelectObject( (HDC) dc, old ) ;
328 }
329
330 void wxBitmapButton::SetDefault()
331 {
332 wxButton::SetDefault();
333 }