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 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton
, wxButton
)
32 #define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1)
34 bool wxBitmapButton::Create(wxWindow
*parent
, wxWindowID id
, const wxBitmap
& bitmap
,
36 const wxSize
& size
, long style
,
37 const wxValidator
& validator
,
40 m_buttonBitmap
= bitmap
;
42 SetValidator(validator
);
44 parent
->AddChild(this);
46 m_backgroundColour
= parent
->GetBackgroundColour() ;
47 m_foregroundColour
= parent
->GetForegroundColour() ;
48 m_windowStyle
= style
;
52 if ( style
& wxBU_AUTODRAW
)
54 m_marginX
= wxDEFAULT_BUTTON_MARGIN
;
55 m_marginY
= wxDEFAULT_BUTTON_MARGIN
;
64 m_windowId
= NewControlId();
68 if ( width
== -1 && bitmap
.Ok())
69 width
= bitmap
.GetWidth() + 2*m_marginX
;
71 if ( height
== -1 && bitmap
.Ok())
72 height
= bitmap
.GetHeight() + 2*m_marginY
;
74 m_hWnd
= (WXHWND
)CreateWindowEx
79 WS_VISIBLE
| WS_TABSTOP
| WS_CHILD
| BS_OWNERDRAW
,
87 // Subclass again for purposes of dialog editing mode
90 SetFont(parent
->GetFont()) ;
92 SetSize(x
, y
, width
, height
);
97 void wxBitmapButton::SetBitmapLabel(const wxBitmap
& bitmap
)
99 m_buttonBitmap
= bitmap
;
102 bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
104 #if defined(__WIN95__)
105 long style
= GetWindowLong((HWND
) GetHWND(), GWL_STYLE
);
106 if (style
& BS_BITMAP
)
108 // Let default procedure draw the bitmap, which is defined
109 // in the Windows resource.
114 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
) item
;
116 // choose the bitmap to use depending on the buttons state
119 UINT state
= lpDIS
->itemState
;
120 bool isSelected
= (state
& ODS_SELECTED
) != 0;
121 if ( isSelected
&& m_buttonBitmapSelected
.Ok() )
122 bitmap
= &m_buttonBitmapSelected
;
123 else if ((state
& ODS_FOCUS
) && m_buttonBitmapFocus
.Ok())
124 bitmap
= &m_buttonBitmapFocus
;
125 else if ((state
& ODS_DISABLED
) && m_buttonBitmapDisabled
.Ok())
126 bitmap
= &m_buttonBitmapDisabled
;
128 bitmap
= &m_buttonBitmap
;
133 // draw it on the memory DC
134 HDC hDC
= lpDIS
->hDC
;
135 HDC memDC
= ::CreateCompatibleDC(hDC
);
137 HBITMAP old
= (HBITMAP
) ::SelectObject(memDC
, (HBITMAP
) bitmap
->GetHBITMAP());
141 wxLogLastError(_T("SelectObject"));
146 int x
= lpDIS
->rcItem
.left
;
147 int y
= lpDIS
->rcItem
.top
;
148 int width
= lpDIS
->rcItem
.right
- x
;
149 int height
= lpDIS
->rcItem
.bottom
- y
;
151 int wBmp
= bitmap
->GetWidth(),
152 hBmp
= bitmap
->GetHeight();
154 // Draw the face, if auto-drawing
155 bool autoDraw
= (GetWindowStyleFlag() & wxBU_AUTODRAW
) != 0;
158 DrawFace((WXHDC
) hDC
,
159 lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
,
160 lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
,
164 // Centre the bitmap in the control area
165 int x1
= x
+ (width
- wBmp
) / 2;
166 int y1
= y
+ (height
- hBmp
) / 2;
168 if ( isSelected
&& autoDraw
)
175 wxMask
*mask
= bitmap
->GetMask();
178 // the fg ROP is applied for the pixels of the mask bitmap which are 1
179 // (for a wxMask this means that this is a non transparent pixel), the
180 // bg ROP is applied for all the others
182 wxColour colBg
= GetBackgroundColour();
183 HBRUSH hbrBackground
=
184 ::CreateSolidBrush(RGB(colBg
.Red(), colBg
.Green(), colBg
.Blue()));
185 HBRUSH hbrOld
= (HBRUSH
)::SelectObject(hDC
, hbrBackground
);
188 hDC
, x1
, y1
, wBmp
, hBmp
, // dst
190 (HBITMAP
)mask
->GetMaskBitmap(), 0, 0, // mask
191 MAKEROP4(SRCCOPY
, // fg ROP
195 ::SelectObject(hDC
, hbrOld
);
196 ::DeleteObject(hbrBackground
);
200 ok
= ::BitBlt(hDC
, x1
, y1
, wBmp
, hBmp
, // dst
207 wxLogLastError(_T("Mask/BitBlt()"));
210 if ( (state
& ODS_DISABLED
) && autoDraw
)
212 DrawButtonDisable((WXHDC
) hDC
,
213 lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
,
214 lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
,
217 else if ( (state
& ODS_FOCUS
) && autoDraw
)
219 DrawButtonFocus((WXHDC
) hDC
,
223 lpDIS
->rcItem
.bottom
,
227 ::SelectObject(memDC
, old
);
234 void wxBitmapButton::DrawFace( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
245 ms_color
= GetSysColor(COLOR_WINDOWFRAME
) ;
246 penBorder
= CreatePen(PS_SOLID
,0,ms_color
) ;
248 ms_color
= GetSysColor(COLOR_BTNSHADOW
) ;
249 penShadow
= CreatePen(PS_SOLID
,0,ms_color
) ;
251 ms_color
= GetSysColor(COLOR_BTNHIGHLIGHT
) ;
252 penLight
= CreatePen(PS_SOLID
,0,ms_color
) ;
254 ms_color
= GetSysColor(COLOR_BTNFACE
) ;
255 brushFace
= CreateSolidBrush(ms_color
) ;
257 oldp
= (HPEN
) SelectObject( (HDC
) dc
, GetStockObject( NULL_PEN
) ) ;
258 oldb
= (HBRUSH
) SelectObject( (HDC
) dc
, brushFace
) ;
259 Rectangle( (HDC
) dc
, left
, top
, right
, bottom
) ;
260 SelectObject( (HDC
) dc
, penBorder
) ;
261 MoveToEx((HDC
) dc
,left
+1,top
,NULL
);LineTo((HDC
) dc
,right
-1,top
);
262 MoveToEx((HDC
) dc
,left
,top
+1,NULL
);LineTo((HDC
) dc
,left
,bottom
-1);
263 MoveToEx((HDC
) dc
,left
+1,bottom
-1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
264 MoveToEx((HDC
) dc
,right
-1,top
+1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
266 SelectObject( (HDC
) dc
, penShadow
) ;
269 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
) ;
270 LineTo((HDC
) dc
, left
+1 ,top
+1) ;
271 LineTo((HDC
) dc
, right
-2 ,top
+1) ;
275 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
) ;
276 LineTo((HDC
) dc
, right
-2 ,bottom
-2) ;
277 LineTo((HDC
) dc
, right
-2 ,top
) ;
278 MoveToEx((HDC
) dc
,left
+2 ,bottom
-3 ,NULL
) ;
279 LineTo((HDC
) dc
, right
-3 ,bottom
-3) ;
280 LineTo((HDC
) dc
, right
-3 ,top
+1) ;
282 SelectObject( (HDC
) dc
, penLight
) ;
284 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
) ;
285 LineTo((HDC
) dc
, left
+1 ,top
+1) ;
286 LineTo((HDC
) dc
, right
-2 ,top
+1) ;
288 SelectObject((HDC
) dc
,oldp
) ;
289 SelectObject((HDC
) dc
,oldb
) ;
291 DeleteObject(penBorder
);
292 DeleteObject(penLight
);
293 DeleteObject(penShadow
);
294 DeleteObject(brushFace
);
297 // VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
298 #define FOCUS_MARGIN 3
300 void wxBitmapButton::DrawButtonFocus( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
306 rect
.bottom
= bottom
;
307 InflateRect( &rect
, - FOCUS_MARGIN
, - FOCUS_MARGIN
) ;
309 OffsetRect( &rect
, 1, 1 ) ;
310 DrawFocusRect( (HDC
) dc
, &rect
) ;
313 extern HBRUSH wxDisableButtonBrush
;
314 void wxBitmapButton::DrawButtonDisable( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool with_marg
)
316 HBRUSH old
= (HBRUSH
) SelectObject( (HDC
) dc
, wxDisableButtonBrush
) ;
318 // VZ: what's this?? there is no such ROP AFAIK
320 DWORD dwRop
= 0xFA0089L
;
322 DWORD dwRop
= 0xFA0089UL
;
329 right
-= 2 * m_marginX
;
330 bottom
-= 2 * m_marginY
;
333 ::PatBlt( (HDC
) dc
, left
, top
, right
, bottom
, dwRop
);
335 ::SelectObject( (HDC
) dc
, old
) ;
338 void wxBitmapButton::SetDefault()
340 wxButton::SetDefault();