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"
28 #include "wx/msw/private.h"
30 #if !USE_SHARED_LIBRARY
31 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton
, wxButton
)
34 #define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1)
36 bool wxBitmapButton
::Create(wxWindow
*parent
, wxWindowID id
, const wxBitmap
& bitmap
,
38 const wxSize
& size
, long style
,
39 const wxValidator
& validator
,
42 m_buttonBitmap
= bitmap
;
44 SetValidator(validator
);
46 parent
->AddChild(this);
48 m_backgroundColour
= parent
->GetBackgroundColour() ;
49 m_foregroundColour
= parent
->GetForegroundColour() ;
50 m_windowStyle
= style
;
54 if ( style
& wxBU_AUTODRAW
)
56 m_marginX
= wxDEFAULT_BUTTON_MARGIN
;
57 m_marginY
= wxDEFAULT_BUTTON_MARGIN
;
66 m_windowId
= NewControlId();
70 if ( width
== -1 && bitmap
.Ok())
71 width
= bitmap
.GetWidth() + 2*m_marginX
;
73 if ( height
== -1 && bitmap
.Ok())
74 height
= bitmap
.GetHeight() + 2*m_marginY
;
76 m_hWnd
= (WXHWND
)CreateWindowEx
81 WS_VISIBLE
| WS_TABSTOP
| WS_CHILD
| BS_OWNERDRAW
,
89 // Subclass again for purposes of dialog editing mode
92 SetFont(parent
->GetFont()) ;
94 SetSize(x
, y
, width
, height
);
99 void wxBitmapButton
::SetBitmapLabel(const wxBitmap
& bitmap
)
101 m_buttonBitmap
= bitmap
;
104 bool wxBitmapButton
::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
106 #if defined(__WIN95__)
107 long style
= GetWindowLong((HWND
) GetHWND(), GWL_STYLE
);
108 if (style
& BS_BITMAP
)
110 // Let default procedure draw the bitmap, which is defined
111 // in the Windows resource.
116 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
) item
;
118 // choose the bitmap to use depending on the buttons state
121 UINT state
= lpDIS
->itemState
;
122 bool isSelected
= (state
& ODS_SELECTED
) != 0;
123 if ( isSelected
&& m_buttonBitmapSelected
.Ok() )
124 bitmap
= &m_buttonBitmapSelected
;
125 else if ((state
& ODS_FOCUS
) && m_buttonBitmapFocus
.Ok())
126 bitmap
= &m_buttonBitmapFocus
;
127 else if ((state
& ODS_DISABLED
) && m_buttonBitmapDisabled
.Ok())
128 bitmap
= &m_buttonBitmapDisabled
;
130 bitmap
= &m_buttonBitmap
;
135 // draw it on the memory DC
136 HDC hDC
= lpDIS
->hDC
;
137 HDC memDC
= ::CreateCompatibleDC(hDC
);
139 HBITMAP old
= (HBITMAP
) ::SelectObject(memDC
, (HBITMAP
) bitmap
->GetHBITMAP());
143 wxLogLastError(_T("SelectObject"));
148 int x
= lpDIS
->rcItem
.left
;
149 int y
= lpDIS
->rcItem
.top
;
150 int width
= lpDIS
->rcItem
.right
- x
;
151 int height
= lpDIS
->rcItem
.bottom
- y
;
153 int wBmp
= bitmap
->GetWidth(),
154 hBmp
= bitmap
->GetHeight();
156 // Draw the face, if auto-drawing
157 bool autoDraw
= (GetWindowStyleFlag() & wxBU_AUTODRAW
) != 0;
160 DrawFace((WXHDC
) hDC
,
161 lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
,
162 lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
,
166 // Centre the bitmap in the control area
167 int x1
= x
+ (width
- wBmp
) / 2;
168 int y1
= y
+ (height
- hBmp
) / 2;
170 if ( isSelected
&& autoDraw
)
177 wxMask
*mask
= bitmap
->GetMask();
180 // the fg ROP is applied for the pixels of the mask bitmap which are 1
181 // (for a wxMask this means that this is a non transparent pixel), the
182 // bg ROP is applied for all the others
184 hDC
, x1
, y1
, wBmp
, hBmp
, // dst
186 (HBITMAP
)mask
->GetMaskBitmap(), 0, 0, // mask
187 MAKEROP4(SRCCOPY
, // fg ROP
193 ok
= ::BitBlt(hDC
, x1
, y1
, wBmp
, hBmp
, // dst
200 wxLogLastError(_T("Mask/BitBlt()"));
203 if ( (state
& ODS_DISABLED
) && autoDraw
)
205 DrawButtonDisable((WXHDC
) hDC
,
206 lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
,
207 lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
,
210 else if ( (state
& ODS_FOCUS
) && autoDraw
)
212 DrawButtonFocus((WXHDC
) hDC
,
216 lpDIS
->rcItem
.bottom
,
220 ::SelectObject(memDC
, old
);
227 void wxBitmapButton
::DrawFace( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
238 ms_color
= GetSysColor(COLOR_WINDOWFRAME
) ;
239 penBorder
= CreatePen(PS_SOLID
,0,ms_color
) ;
241 ms_color
= GetSysColor(COLOR_BTNSHADOW
) ;
242 penShadow
= CreatePen(PS_SOLID
,0,ms_color
) ;
244 ms_color
= GetSysColor(COLOR_BTNHIGHLIGHT
) ;
245 penLight
= CreatePen(PS_SOLID
,0,ms_color
) ;
247 ms_color
= GetSysColor(COLOR_BTNFACE
) ;
248 brushFace
= CreateSolidBrush(ms_color
) ;
250 oldp
= (HPEN
) SelectObject( (HDC
) dc
, GetStockObject( NULL_PEN
) ) ;
251 oldb
= (HBRUSH
) SelectObject( (HDC
) dc
, brushFace
) ;
252 Rectangle( (HDC
) dc
, left
, top
, right
, bottom
) ;
253 SelectObject( (HDC
) dc
, penBorder
) ;
254 MoveToEx((HDC
) dc
,left
+1,top
,NULL
);LineTo((HDC
) dc
,right
-1,top
);
255 MoveToEx((HDC
) dc
,left
,top
+1,NULL
);LineTo((HDC
) dc
,left
,bottom
-1);
256 MoveToEx((HDC
) dc
,left
+1,bottom
-1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
257 MoveToEx((HDC
) dc
,right
-1,top
+1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
259 SelectObject( (HDC
) dc
, penShadow
) ;
262 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
) ;
263 LineTo((HDC
) dc
, left
+1 ,top
+1) ;
264 LineTo((HDC
) dc
, right
-2 ,top
+1) ;
268 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
) ;
269 LineTo((HDC
) dc
, right
-2 ,bottom
-2) ;
270 LineTo((HDC
) dc
, right
-2 ,top
) ;
271 MoveToEx((HDC
) dc
,left
+2 ,bottom
-3 ,NULL
) ;
272 LineTo((HDC
) dc
, right
-3 ,bottom
-3) ;
273 LineTo((HDC
) dc
, right
-3 ,top
+1) ;
275 SelectObject( (HDC
) dc
, penLight
) ;
277 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
) ;
278 LineTo((HDC
) dc
, left
+1 ,top
+1) ;
279 LineTo((HDC
) dc
, right
-2 ,top
+1) ;
281 SelectObject((HDC
) dc
,oldp
) ;
282 SelectObject((HDC
) dc
,oldb
) ;
284 DeleteObject(penBorder
);
285 DeleteObject(penLight
);
286 DeleteObject(penShadow
);
287 DeleteObject(brushFace
);
290 // VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
291 #define FOCUS_MARGIN 3
293 void wxBitmapButton
::DrawButtonFocus( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
299 rect
.bottom
= bottom
;
300 InflateRect( &rect
, - FOCUS_MARGIN
, - FOCUS_MARGIN
) ;
302 OffsetRect( &rect
, 1, 1 ) ;
303 DrawFocusRect( (HDC
) dc
, &rect
) ;
306 extern HBRUSH wxDisableButtonBrush
;
307 void wxBitmapButton
::DrawButtonDisable( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool with_marg
)
309 HBRUSH old
= (HBRUSH
) SelectObject( (HDC
) dc
, wxDisableButtonBrush
) ;
311 // VZ: what's this?? there is no such ROP AFAIK
313 DWORD dwRop
= 0xFA0089L
;
315 DWORD dwRop
= 0xFA0089UL
;
322 right
-= 2 * m_marginX
;
323 bottom
-= 2 * m_marginY
;
326 ::PatBlt( (HDC
) dc
, left
, top
, right
, bottom
, dwRop
);
328 ::SelectObject( (HDC
) dc
, old
) ;
331 void wxBitmapButton
::SetDefault()
333 wxButton
::SetDefault();