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 wxBitmap
* bitmap
= &m_buttonBitmap
;
119 UINT state
= lpDIS
->itemState
;
120 if ((state
& ODS_SELECTED
) && m_buttonBitmapSelected
.Ok())
121 bitmap
= &m_buttonBitmapSelected
;
122 else if ((state
& ODS_FOCUS
) && m_buttonBitmapFocus
.Ok())
123 bitmap
= &m_buttonBitmapFocus
;
124 else if ((state
& ODS_DISABLED
) && m_buttonBitmapDisabled
.Ok())
125 bitmap
= &m_buttonBitmapDisabled
;
130 HDC hDC
= lpDIS
->hDC
;
131 HDC memDC
= ::CreateCompatibleDC(hDC
);
133 HBITMAP old
= (HBITMAP
) ::SelectObject(memDC
, (HBITMAP
) bitmap
->GetHBITMAP());
138 int x
= lpDIS
->rcItem
.left
;
139 int y
= lpDIS
->rcItem
.top
;
140 int width
= lpDIS
->rcItem
.right
- x
;
141 int height
= lpDIS
->rcItem
.bottom
- y
;
143 // Draw the face, if auto-drawing
144 if ( GetWindowStyleFlag() & wxBU_AUTODRAW
)
145 DrawFace((WXHDC
) hDC
, lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
, lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
,
146 ((state
& ODS_SELECTED
) == ODS_SELECTED
));
148 // Centre the bitmap in the control area
149 int x1
= (int) (x
+ ((width
- bitmap
->GetWidth()) / 2));
150 int y1
= (int) (y
+ ((height
- bitmap
->GetHeight()) / 2));
152 if ( (state
& ODS_SELECTED
) && (GetWindowStyleFlag() & wxBU_AUTODRAW
) )
158 wxMask
*mask
= bitmap
->GetMask();
161 ::MaskBlt(hDC
, x1
, y1
, bitmap
->GetWidth(), bitmap
->GetHeight(), // dst
163 (HBITMAP
)mask
->GetMaskBitmap(), 0, 0, // mask
164 MAKEROP4(SRCPAINT
, SRCCOPY
));
168 ::BitBlt(hDC
, x1
, y1
, bitmap
->GetWidth(), bitmap
->GetHeight(), // dst
173 if ( (state
& ODS_DISABLED
) && (GetWindowStyleFlag() & wxBU_AUTODRAW
) )
174 DrawButtonDisable( (WXHDC
) hDC
, lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
, lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
, TRUE
) ;
175 else if ( (state
& ODS_FOCUS
) && (GetWindowStyleFlag() & wxBU_AUTODRAW
) )
176 DrawButtonFocus( (WXHDC
) hDC
, lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
, lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
, ((state
& ODS_SELECTED
) == ODS_SELECTED
));
178 ::SelectObject(memDC
, old
);
185 void wxBitmapButton::DrawFace( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
196 ms_color
= GetSysColor(COLOR_WINDOWFRAME
) ;
197 penBorder
= CreatePen(PS_SOLID
,0,ms_color
) ;
199 ms_color
= GetSysColor(COLOR_BTNSHADOW
) ;
200 penShadow
= CreatePen(PS_SOLID
,0,ms_color
) ;
202 ms_color
= GetSysColor(COLOR_BTNHIGHLIGHT
) ;
203 penLight
= CreatePen(PS_SOLID
,0,ms_color
) ;
205 ms_color
= GetSysColor(COLOR_BTNFACE
) ;
206 brushFace
= CreateSolidBrush(ms_color
) ;
208 oldp
= (HPEN
) SelectObject( (HDC
) dc
, GetStockObject( NULL_PEN
) ) ;
209 oldb
= (HBRUSH
) SelectObject( (HDC
) dc
, brushFace
) ;
210 Rectangle( (HDC
) dc
, left
, top
, right
, bottom
) ;
211 SelectObject( (HDC
) dc
, penBorder
) ;
212 MoveToEx((HDC
) dc
,left
+1,top
,NULL
);LineTo((HDC
) dc
,right
-1,top
);
213 MoveToEx((HDC
) dc
,left
,top
+1,NULL
);LineTo((HDC
) dc
,left
,bottom
-1);
214 MoveToEx((HDC
) dc
,left
+1,bottom
-1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
215 MoveToEx((HDC
) dc
,right
-1,top
+1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
217 SelectObject( (HDC
) dc
, penShadow
) ;
220 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
) ;
221 LineTo((HDC
) dc
, left
+1 ,top
+1) ;
222 LineTo((HDC
) dc
, right
-2 ,top
+1) ;
226 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
) ;
227 LineTo((HDC
) dc
, right
-2 ,bottom
-2) ;
228 LineTo((HDC
) dc
, right
-2 ,top
) ;
229 MoveToEx((HDC
) dc
,left
+2 ,bottom
-3 ,NULL
) ;
230 LineTo((HDC
) dc
, right
-3 ,bottom
-3) ;
231 LineTo((HDC
) dc
, right
-3 ,top
+1) ;
233 SelectObject( (HDC
) dc
, penLight
) ;
235 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
) ;
236 LineTo((HDC
) dc
, left
+1 ,top
+1) ;
237 LineTo((HDC
) dc
, right
-2 ,top
+1) ;
239 SelectObject((HDC
) dc
,oldp
) ;
240 SelectObject((HDC
) dc
,oldb
) ;
242 DeleteObject(penBorder
);
243 DeleteObject(penLight
);
244 DeleteObject(penShadow
);
245 DeleteObject(brushFace
);
248 // VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
249 #define FOCUS_MARGIN 3
251 void wxBitmapButton::DrawButtonFocus( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
257 rect
.bottom
= bottom
;
258 InflateRect( &rect
, - FOCUS_MARGIN
, - FOCUS_MARGIN
) ;
260 OffsetRect( &rect
, 1, 1 ) ;
261 DrawFocusRect( (HDC
) dc
, &rect
) ;
264 extern HBRUSH wxDisableButtonBrush
;
265 void wxBitmapButton::DrawButtonDisable( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool with_marg
)
267 HBRUSH old
= (HBRUSH
) SelectObject( (HDC
) dc
, wxDisableButtonBrush
) ;
270 ::PatBlt( (HDC
) dc
, left
+ m_marginX
, top
+ m_marginY
,
271 right
- 2 * m_marginX
, bottom
- 2 * m_marginY
,
277 else ::PatBlt( (HDC
) dc
, left
, top
, right
, bottom
,
283 ::SelectObject( (HDC
) dc
, old
) ;
286 void wxBitmapButton::SetDefault()
288 wxButton::SetDefault();