1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxBitmapButton
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "bmpbuttn.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
24 #include "wx/bmpbuttn.h"
27 #include "wx/msw/private.h"
29 #if !USE_SHARED_LIBRARY
30 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton
, wxButton
)
33 #define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1)
35 bool wxBitmapButton
::Create(wxWindow
*parent
, wxWindowID id
, const wxBitmap
& bitmap
,
37 const wxSize
& size
, long style
,
38 const wxValidator
& validator
,
41 m_buttonBitmap
= bitmap
;
43 SetValidator(validator
);
45 parent
->AddChild(this);
47 m_backgroundColour
= parent
->GetBackgroundColour() ;
48 m_foregroundColour
= parent
->GetForegroundColour() ;
49 m_windowStyle
= style
;
53 if ( style
& wxBU_AUTODRAW
)
55 m_marginX
= wxDEFAULT_BUTTON_MARGIN
;
56 m_marginY
= wxDEFAULT_BUTTON_MARGIN
;
65 m_windowId
= NewControlId();
69 if ( width
== -1 && bitmap
.Ok())
70 width
= bitmap
.GetWidth() + 2*m_marginX
;
72 if ( height
== -1 && bitmap
.Ok())
73 height
= bitmap
.GetHeight() + 2*m_marginY
;
75 m_hWnd
= (WXHWND
)CreateWindowEx
80 WS_VISIBLE
| WS_TABSTOP
| WS_CHILD
| BS_OWNERDRAW
,
88 // Subclass again for purposes of dialog editing mode
91 SetFont(parent
->GetFont()) ;
93 SetSize(x
, y
, width
, height
);
98 void wxBitmapButton
::SetBitmapLabel(const wxBitmap
& bitmap
)
100 m_buttonBitmap
= bitmap
;
103 bool wxBitmapButton
::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
105 #if defined(__WIN95__)
106 long style
= GetWindowLong((HWND
) GetHWND(), GWL_STYLE
);
107 if (style
& BS_BITMAP
)
109 // Let default procedure draw the bitmap, which is defined
110 // in the Windows resource.
115 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
) item
;
117 // choose the bitmap to use depending on the buttons state
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
;
129 bitmap
= &m_buttonBitmap
;
134 // draw it on the memory DC
135 HDC hDC
= lpDIS
->hDC
;
136 HDC memDC
= ::CreateCompatibleDC(hDC
);
138 HBITMAP old
= (HBITMAP
) ::SelectObject(memDC
, (HBITMAP
) bitmap
->GetHBITMAP());
142 wxLogLastError(_T("SelectObject"));
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
;
152 int wBmp
= bitmap
->GetWidth(),
153 hBmp
= bitmap
->GetHeight();
155 // Draw the face, if auto-drawing
156 bool autoDraw
= (GetWindowStyleFlag() & wxBU_AUTODRAW
) != 0;
159 DrawFace((WXHDC
) hDC
,
160 lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
,
161 lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
,
165 // Centre the bitmap in the control area
166 int x1
= x
+ (width
- wBmp
) / 2;
167 int y1
= y
+ (height
- hBmp
) / 2;
169 if ( isSelected
&& autoDraw
)
176 wxMask
*mask
= bitmap
->GetMask();
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
183 hDC
, x1
, y1
, wBmp
, hBmp
, // dst
185 (HBITMAP
)mask
->GetMaskBitmap(), 0, 0, // mask
186 MAKEROP4(SRCCOPY
, // fg ROP
192 ok
= ::BitBlt(hDC
, x1
, y1
, wBmp
, hBmp
, // dst
199 wxLogLastError(_T("Mask/BitBlt()"));
202 if ( (state
& ODS_DISABLED
) && autoDraw
)
204 DrawButtonDisable((WXHDC
) hDC
,
205 lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
,
206 lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
,
209 else if ( (state
& ODS_FOCUS
) && autoDraw
)
211 DrawButtonFocus((WXHDC
) hDC
,
215 lpDIS
->rcItem
.bottom
,
219 ::SelectObject(memDC
, old
);
226 void wxBitmapButton
::DrawFace( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
237 ms_color
= GetSysColor(COLOR_WINDOWFRAME
) ;
238 penBorder
= CreatePen(PS_SOLID
,0,ms_color
) ;
240 ms_color
= GetSysColor(COLOR_BTNSHADOW
) ;
241 penShadow
= CreatePen(PS_SOLID
,0,ms_color
) ;
243 ms_color
= GetSysColor(COLOR_BTNHIGHLIGHT
) ;
244 penLight
= CreatePen(PS_SOLID
,0,ms_color
) ;
246 ms_color
= GetSysColor(COLOR_BTNFACE
) ;
247 brushFace
= CreateSolidBrush(ms_color
) ;
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);
258 SelectObject( (HDC
) dc
, penShadow
) ;
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) ;
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) ;
274 SelectObject( (HDC
) dc
, penLight
) ;
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) ;
280 SelectObject((HDC
) dc
,oldp
) ;
281 SelectObject((HDC
) dc
,oldb
) ;
283 DeleteObject(penBorder
);
284 DeleteObject(penLight
);
285 DeleteObject(penShadow
);
286 DeleteObject(brushFace
);
289 // VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
290 #define FOCUS_MARGIN 3
292 void wxBitmapButton
::DrawButtonFocus( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
298 rect
.bottom
= bottom
;
299 InflateRect( &rect
, - FOCUS_MARGIN
, - FOCUS_MARGIN
) ;
301 OffsetRect( &rect
, 1, 1 ) ;
302 DrawFocusRect( (HDC
) dc
, &rect
) ;
305 extern HBRUSH wxDisableButtonBrush
;
306 void wxBitmapButton
::DrawButtonDisable( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool with_marg
)
308 HBRUSH old
= (HBRUSH
) SelectObject( (HDC
) dc
, wxDisableButtonBrush
) ;
310 // VZ: what's this?? there is no such ROP AFAIK
312 DWORD dwRop
= 0xFA0089L
;
314 DWORD dwRop
= 0xFA0089UL
;
321 right
-= 2 * m_marginX
;
322 bottom
-= 2 * m_marginY
;
325 ::PatBlt( (HDC
) dc
, left
, top
, right
, bottom
, dwRop
);
327 ::SelectObject( (HDC
) dc
, old
) ;
330 void wxBitmapButton
::SetDefault()
332 wxButton
::SetDefault();