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
)
176 // no MaskBlt() under Win16
178 wxMask
*mask
= bitmap
->GetMask();
181 // the fg ROP is applied for the pixels of the mask bitmap which are 1
182 // (for a wxMask this means that this is a non transparent pixel), the
183 // bg ROP is applied for all the others
185 wxColour colBg
= GetBackgroundColour();
186 HBRUSH hbrBackground
=
187 ::CreateSolidBrush(RGB(colBg
.Red(), colBg
.Green(), colBg
.Blue()));
188 HBRUSH hbrOld
= (HBRUSH
)::SelectObject(hDC
, hbrBackground
);
191 hDC
, x1
, y1
, wBmp
, hBmp
, // dst
193 (HBITMAP
)mask
->GetMaskBitmap(), 0, 0, // mask
194 MAKEROP4(SRCCOPY
, // fg ROP
198 ::SelectObject(hDC
, hbrOld
);
199 ::DeleteObject(hbrBackground
);
204 ok
= ::BitBlt(hDC
, x1
, y1
, wBmp
, hBmp
, // dst
211 wxLogLastError(_T("Mask/BitBlt()"));
214 if ( (state
& ODS_DISABLED
) && autoDraw
)
216 DrawButtonDisable((WXHDC
) hDC
,
217 lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
,
218 lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
,
221 else if ( (state
& ODS_FOCUS
) && autoDraw
)
223 DrawButtonFocus((WXHDC
) hDC
,
227 lpDIS
->rcItem
.bottom
,
231 ::SelectObject(memDC
, old
);
238 void wxBitmapButton::DrawFace( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
249 ms_color
= GetSysColor(COLOR_WINDOWFRAME
) ;
250 penBorder
= CreatePen(PS_SOLID
,0,ms_color
) ;
252 ms_color
= GetSysColor(COLOR_BTNSHADOW
) ;
253 penShadow
= CreatePen(PS_SOLID
,0,ms_color
) ;
255 ms_color
= GetSysColor(COLOR_BTNHIGHLIGHT
) ;
256 penLight
= CreatePen(PS_SOLID
,0,ms_color
) ;
258 ms_color
= GetSysColor(COLOR_BTNFACE
) ;
259 brushFace
= CreateSolidBrush(ms_color
) ;
261 oldp
= (HPEN
) SelectObject( (HDC
) dc
, GetStockObject( NULL_PEN
) ) ;
262 oldb
= (HBRUSH
) SelectObject( (HDC
) dc
, brushFace
) ;
263 Rectangle( (HDC
) dc
, left
, top
, right
, bottom
) ;
264 SelectObject( (HDC
) dc
, penBorder
) ;
265 MoveToEx((HDC
) dc
,left
+1,top
,NULL
);LineTo((HDC
) dc
,right
-1,top
);
266 MoveToEx((HDC
) dc
,left
,top
+1,NULL
);LineTo((HDC
) dc
,left
,bottom
-1);
267 MoveToEx((HDC
) dc
,left
+1,bottom
-1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
268 MoveToEx((HDC
) dc
,right
-1,top
+1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
270 SelectObject( (HDC
) dc
, penShadow
) ;
273 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
) ;
274 LineTo((HDC
) dc
, left
+1 ,top
+1) ;
275 LineTo((HDC
) dc
, right
-2 ,top
+1) ;
279 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
) ;
280 LineTo((HDC
) dc
, right
-2 ,bottom
-2) ;
281 LineTo((HDC
) dc
, right
-2 ,top
) ;
282 MoveToEx((HDC
) dc
,left
+2 ,bottom
-3 ,NULL
) ;
283 LineTo((HDC
) dc
, right
-3 ,bottom
-3) ;
284 LineTo((HDC
) dc
, right
-3 ,top
+1) ;
286 SelectObject( (HDC
) dc
, penLight
) ;
288 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
) ;
289 LineTo((HDC
) dc
, left
+1 ,top
+1) ;
290 LineTo((HDC
) dc
, right
-2 ,top
+1) ;
292 SelectObject((HDC
) dc
,oldp
) ;
293 SelectObject((HDC
) dc
,oldb
) ;
295 DeleteObject(penBorder
);
296 DeleteObject(penLight
);
297 DeleteObject(penShadow
);
298 DeleteObject(brushFace
);
301 // VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
302 #define FOCUS_MARGIN 3
304 void wxBitmapButton::DrawButtonFocus( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
310 rect
.bottom
= bottom
;
311 InflateRect( &rect
, - FOCUS_MARGIN
, - FOCUS_MARGIN
) ;
313 OffsetRect( &rect
, 1, 1 ) ;
314 DrawFocusRect( (HDC
) dc
, &rect
) ;
317 extern HBRUSH wxDisableButtonBrush
;
318 void wxBitmapButton::DrawButtonDisable( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool with_marg
)
320 HBRUSH old
= (HBRUSH
) SelectObject( (HDC
) dc
, wxDisableButtonBrush
) ;
322 // VZ: what's this?? there is no such ROP AFAIK
324 DWORD dwRop
= 0xFA0089L
;
326 DWORD dwRop
= 0xFA0089UL
;
333 right
-= 2 * m_marginX
;
334 bottom
-= 2 * m_marginY
;
337 ::PatBlt( (HDC
) dc
, left
, top
, right
, bottom
, dwRop
);
339 ::SelectObject( (HDC
) dc
, old
) ;
342 void wxBitmapButton::SetDefault()
344 wxButton::SetDefault();