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
;
76 CreateWindowEx(0, "BUTTON", "", BS_OWNERDRAW
| WS_TABSTOP
| WS_CHILD
,
77 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
78 wxGetInstance(), NULL
);
80 m_hWnd
= (WXHWND
)wx_button
;
82 // Subclass again for purposes of dialog editing mode
83 SubclassWin((WXHWND
)wx_button
);
85 SetFont(* parent
->GetFont()) ;
87 SetSize(x
, y
, width
, height
);
88 ShowWindow(wx_button
, SW_SHOW
);
93 void wxBitmapButton::SetBitmapLabel(const wxBitmap
& bitmap
)
95 m_buttonBitmap
= bitmap
;
98 bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
100 long style
= GetWindowLong((HWND
) GetHWND(), GWL_STYLE
);
101 #if defined(__WIN95__)
102 if (style
& BS_BITMAP
)
104 // Should we call Default() here?
107 // Let default procedure draw the bitmap, which is defined
108 // in the Windows resource.
113 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
) item
;
115 wxBitmap
* bitmap
= &m_buttonBitmap
;
117 UINT state
= lpDIS
->itemState
;
118 if ((state
& ODS_SELECTED
) && m_buttonBitmapSelected
.Ok())
119 bitmap
= &m_buttonBitmapSelected
;
120 else if ((state
& ODS_FOCUS
) && m_buttonBitmapFocus
.Ok())
121 bitmap
= &m_buttonBitmapFocus
;
122 else if ((state
& ODS_DISABLED
) && m_buttonBitmapDisabled
.Ok())
123 bitmap
= &m_buttonBitmapDisabled
;
128 HDC hDC
= lpDIS
->hDC
;
129 HDC memDC
= ::CreateCompatibleDC(hDC
);
131 HBITMAP old
= (HBITMAP
) ::SelectObject(memDC
, (HBITMAP
) bitmap
->GetHBITMAP());
136 RECT rect
= lpDIS
->rcItem
;
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 ::BitBlt(hDC
, x1
, y1
, bitmap
->GetWidth(), bitmap
->GetHeight(), memDC
, 0, 0, SRCCOPY
);
160 if ( (state
& ODS_DISABLED
) && (GetWindowStyleFlag() & wxBU_AUTODRAW
) )
161 DrawButtonDisable( (WXHDC
) hDC
, lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
, lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
, TRUE
) ;
162 else if ( (state
& ODS_FOCUS
) && (GetWindowStyleFlag() & wxBU_AUTODRAW
) )
163 DrawButtonFocus( (WXHDC
) hDC
, lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
, lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
, ((state
& ODS_SELECTED
) == ODS_SELECTED
));
165 ::SelectObject(memDC
, old
);
172 void wxBitmapButton::DrawFace( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
183 ms_color
= GetSysColor(COLOR_WINDOWFRAME
) ;
184 penBorder
= CreatePen(PS_SOLID
,0,ms_color
) ;
186 ms_color
= GetSysColor(COLOR_BTNSHADOW
) ;
187 penShadow
= CreatePen(PS_SOLID
,0,ms_color
) ;
189 ms_color
= GetSysColor(COLOR_BTNHIGHLIGHT
) ;
190 penLight
= CreatePen(PS_SOLID
,0,ms_color
) ;
192 ms_color
= GetSysColor(COLOR_BTNFACE
) ;
193 brushFace
= CreateSolidBrush(ms_color
) ;
195 oldp
= (HPEN
) SelectObject( (HDC
) dc
, GetStockObject( NULL_PEN
) ) ;
196 oldb
= (HBRUSH
) SelectObject( (HDC
) dc
, brushFace
) ;
197 Rectangle( (HDC
) dc
, left
, top
, right
, bottom
) ;
198 SelectObject( (HDC
) dc
, penBorder
) ;
199 MoveToEx((HDC
) dc
,left
+1,top
,NULL
);LineTo((HDC
) dc
,right
-1,top
);
200 MoveToEx((HDC
) dc
,left
,top
+1,NULL
);LineTo((HDC
) dc
,left
,bottom
-1);
201 MoveToEx((HDC
) dc
,left
+1,bottom
-1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
202 MoveToEx((HDC
) dc
,right
-1,top
+1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
204 SelectObject( (HDC
) dc
, penShadow
) ;
207 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
) ;
208 LineTo((HDC
) dc
, left
+1 ,top
+1) ;
209 LineTo((HDC
) dc
, right
-2 ,top
+1) ;
213 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
) ;
214 LineTo((HDC
) dc
, right
-2 ,bottom
-2) ;
215 LineTo((HDC
) dc
, right
-2 ,top
) ;
216 MoveToEx((HDC
) dc
,left
+2 ,bottom
-3 ,NULL
) ;
217 LineTo((HDC
) dc
, right
-3 ,bottom
-3) ;
218 LineTo((HDC
) dc
, right
-3 ,top
+1) ;
220 SelectObject( (HDC
) dc
, penLight
) ;
222 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
) ;
223 LineTo((HDC
) dc
, left
+1 ,top
+1) ;
224 LineTo((HDC
) dc
, right
-2 ,top
+1) ;
226 SelectObject((HDC
) dc
,oldp
) ;
227 SelectObject((HDC
) dc
,oldb
) ;
229 DeleteObject(penBorder
);
230 DeleteObject(penLight
);
231 DeleteObject(penShadow
);
232 DeleteObject(brushFace
);
235 #define FOCUS_MARGIN 6
237 void wxBitmapButton::DrawButtonFocus( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
243 rect
.bottom
= bottom
;
244 InflateRect( &rect
, - FOCUS_MARGIN
, - FOCUS_MARGIN
) ;
246 OffsetRect( &rect
, 1, 1 ) ;
247 DrawFocusRect( (HDC
) dc
, &rect
) ;
250 extern HBRUSH wxDisableButtonBrush
;
251 void wxBitmapButton::DrawButtonDisable( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool with_marg
)
253 HBRUSH old
= (HBRUSH
) SelectObject( (HDC
) dc
, wxDisableButtonBrush
) ;
256 ::PatBlt( (HDC
) dc
, left
+ m_marginX
, top
+ m_marginY
,
257 right
- 2 * m_marginX
, bottom
- 2 * m_marginY
,
259 else ::PatBlt( (HDC
) dc
, left
, top
, right
, bottom
, 0xfa0089ul
) ;
261 ::SelectObject( (HDC
) dc
, old
) ;