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"
26 #include "wx/dcmemory.h"
29 #include "wx/msw/private.h"
31 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
;
45 SetValidator(validator
);
46 #endif // wxUSE_VALIDATORS
48 parent
->AddChild(this);
50 m_backgroundColour
= parent
->GetBackgroundColour();
51 m_foregroundColour
= parent
->GetForegroundColour();
52 m_windowStyle
= style
;
56 if ( style
& wxBU_AUTODRAW
)
58 m_marginX
= wxDEFAULT_BUTTON_MARGIN
;
59 m_marginY
= wxDEFAULT_BUTTON_MARGIN
;
68 m_windowId
= NewControlId();
72 if ( width
== -1 && bitmap
.Ok())
73 width
= bitmap
.GetWidth() + 2*m_marginX
;
75 if ( height
== -1 && bitmap
.Ok())
76 height
= bitmap
.GetHeight() + 2*m_marginY
;
78 long msStyle
= WS_VISIBLE
| WS_TABSTOP
| WS_CHILD
| BS_OWNERDRAW
;
81 if(m_windowStyle
& wxBU_LEFT
)
83 if(m_windowStyle
& wxBU_RIGHT
)
85 if(m_windowStyle
& wxBU_TOP
)
87 if(m_windowStyle
& wxBU_BOTTOM
)
91 m_hWnd
= (WXHWND
)CreateWindowEx
104 // Subclass again for purposes of dialog editing mode
107 SetFont(parent
->GetFont());
109 SetSize(x
, y
, width
, height
);
114 void wxBitmapButton::SetBitmapLabel(const wxBitmap
& bitmap
)
116 m_buttonBitmap
= bitmap
;
119 // VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
120 #define FOCUS_MARGIN 3
122 bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
124 #if defined(__WIN95__)
125 long style
= GetWindowLong((HWND
) GetHWND(), GWL_STYLE
);
126 if (style
& BS_BITMAP
)
128 // Let default procedure draw the bitmap, which is defined
129 // in the Windows resource.
134 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
) item
;
135 HDC hDC
= lpDIS
->hDC
;
136 UINT state
= lpDIS
->itemState
;
137 bool isSelected
= (state
& ODS_SELECTED
) != 0;
138 bool autoDraw
= (GetWindowStyleFlag() & wxBU_AUTODRAW
) != 0;
141 // choose the bitmap to use depending on the button state
144 if ( isSelected
&& m_buttonBitmapSelected
.Ok() )
145 bitmap
= &m_buttonBitmapSelected
;
146 else if ((state
& ODS_FOCUS
) && m_buttonBitmapFocus
.Ok())
147 bitmap
= &m_buttonBitmapFocus
;
148 else if ((state
& ODS_DISABLED
) && m_buttonBitmapDisabled
.Ok())
149 bitmap
= &m_buttonBitmapDisabled
;
151 bitmap
= &m_buttonBitmap
;
156 // centre the bitmap in the control area
157 int x
= lpDIS
->rcItem
.left
;
158 int y
= lpDIS
->rcItem
.top
;
159 int width
= lpDIS
->rcItem
.right
- x
;
160 int height
= lpDIS
->rcItem
.bottom
- y
;
161 int wBmp
= bitmap
->GetWidth();
162 int hBmp
= bitmap
->GetHeight();
166 if(m_windowStyle
& wxBU_LEFT
)
167 x1
= x
+ (FOCUS_MARGIN
+1);
168 else if(m_windowStyle
& wxBU_RIGHT
)
169 x1
= x
+ (width
- wBmp
) - (FOCUS_MARGIN
+1);
171 x1
= x
+ (width
- wBmp
) / 2;
173 if(m_windowStyle
& wxBU_TOP
)
174 y1
= y
+ (FOCUS_MARGIN
+1);
175 else if(m_windowStyle
& wxBU_BOTTOM
)
176 y1
= y
+ (height
- hBmp
) - (FOCUS_MARGIN
+1);
178 y1
= y
+ (height
- hBmp
) / 2;
180 if ( isSelected
&& autoDraw
)
186 // draw the face, if auto-drawing
189 DrawFace((WXHDC
) hDC
,
190 lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
,
191 lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
,
197 dst
.SetHDC((WXHDC
) hDC
, FALSE
);
198 dst
.DrawBitmap(*bitmap
, x1
, y1
, TRUE
);
200 // draw focus / disabled state, if auto-drawing
201 if ( (state
& ODS_DISABLED
) && autoDraw
)
203 DrawButtonDisable((WXHDC
) hDC
,
204 lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
,
205 lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
,
208 else if ( (state
& ODS_FOCUS
) && autoDraw
)
210 DrawButtonFocus((WXHDC
) hDC
,
214 lpDIS
->rcItem
.bottom
,
221 // GRG Feb/2000, support for bmp buttons with Win95/98 standard LNF
223 #if defined(__WIN95__)
225 void wxBitmapButton::DrawFace( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
234 // create needed pens and brush
235 penHiLight
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DHILIGHT
));
236 penLight
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DLIGHT
));
237 penShadow
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DSHADOW
));
238 penDkShadow
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DDKSHADOW
));
239 brushFace
= CreateSolidBrush(GetSysColor(COLOR_BTNFACE
));
241 // draw the rectangle
246 rect
.bottom
= bottom
;
247 FillRect((HDC
) dc
, &rect
, brushFace
);
250 oldp
= (HPEN
) SelectObject( (HDC
) dc
, sel
? penDkShadow
: penHiLight
);
251 MoveToEx((HDC
) dc
, left
, top
, NULL
); LineTo((HDC
) dc
, right
-1, top
);
252 MoveToEx((HDC
) dc
, left
, top
+1, NULL
); LineTo((HDC
) dc
, left
, bottom
-1);
254 SelectObject( (HDC
) dc
, sel
? penShadow
: penLight
);
255 MoveToEx((HDC
) dc
, left
+1, top
+1, NULL
); LineTo((HDC
) dc
, right
-2, top
+1);
256 MoveToEx((HDC
) dc
, left
+1, top
+2, NULL
); LineTo((HDC
) dc
, left
+1, bottom
-2);
258 SelectObject( (HDC
) dc
, sel
? penLight
: penShadow
);
259 MoveToEx((HDC
) dc
, left
+1, bottom
-2, NULL
); LineTo((HDC
) dc
, right
-1, bottom
-2);
260 MoveToEx((HDC
) dc
, right
-2, bottom
-3, NULL
); LineTo((HDC
) dc
, right
-2, top
);
262 SelectObject( (HDC
) dc
, sel
? penHiLight
: penDkShadow
);
263 MoveToEx((HDC
) dc
, left
, bottom
-1, NULL
); LineTo((HDC
) dc
, right
+2, bottom
-1);
264 MoveToEx((HDC
) dc
, right
-1, bottom
-2, NULL
); LineTo((HDC
) dc
, right
-1, top
-1);
266 // delete allocated resources
267 SelectObject((HDC
) dc
,oldp
);
268 DeleteObject(penHiLight
);
269 DeleteObject(penLight
);
270 DeleteObject(penShadow
);
271 DeleteObject(penDkShadow
);
272 DeleteObject(brushFace
);
277 void wxBitmapButton::DrawFace( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
285 // create needed pens and brush
286 penBorder
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_WINDOWFRAME
));
287 penShadow
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_BTNSHADOW
));
288 penLight
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_BTNHIGHLIGHT
));
289 brushFace
= CreateSolidBrush(COLOR_BTNFACE
);
291 // draw the rectangle
296 rect
.bottom
= bottom
;
297 FillRect((HDC
) dc
, &rect
, brushFace
);
300 oldp
= (HPEN
) SelectObject( (HDC
) dc
, penBorder
);
301 MoveToEx((HDC
) dc
,left
+1,top
,NULL
);LineTo((HDC
) dc
,right
-1,top
);
302 MoveToEx((HDC
) dc
,left
,top
+1,NULL
);LineTo((HDC
) dc
,left
,bottom
-1);
303 MoveToEx((HDC
) dc
,left
+1,bottom
-1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
304 MoveToEx((HDC
) dc
,right
-1,top
+1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
306 SelectObject( (HDC
) dc
, penShadow
);
309 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
);
310 LineTo((HDC
) dc
, left
+1 ,top
+1);
311 LineTo((HDC
) dc
, right
-2 ,top
+1);
315 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
);
316 LineTo((HDC
) dc
, right
-2 ,bottom
-2);
317 LineTo((HDC
) dc
, right
-2 ,top
);
319 MoveToEx((HDC
) dc
,left
+2 ,bottom
-3 ,NULL
);
320 LineTo((HDC
) dc
, right
-3 ,bottom
-3);
321 LineTo((HDC
) dc
, right
-3 ,top
+1);
323 SelectObject( (HDC
) dc
, penLight
);
325 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
);
326 LineTo((HDC
) dc
, left
+1 ,top
+1);
327 LineTo((HDC
) dc
, right
-2 ,top
+1);
330 // delete allocated resources
331 SelectObject((HDC
) dc
,oldp
);
332 DeleteObject(penBorder
);
333 DeleteObject(penLight
);
334 DeleteObject(penShadow
);
335 DeleteObject(brushFace
);
338 #endif // defined(__WIN95__)
341 void wxBitmapButton::DrawButtonFocus( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
347 rect
.bottom
= bottom
;
348 InflateRect( &rect
, - FOCUS_MARGIN
, - FOCUS_MARGIN
);
350 // GRG: the focus rectangle should not move when the button is pushed!
353 OffsetRect( &rect, 1, 1 );
356 DrawFocusRect( (HDC
) dc
, &rect
);
359 extern HBRUSH wxDisableButtonBrush
;
360 void wxBitmapButton::DrawButtonDisable( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool with_marg
)
362 HBRUSH old
= (HBRUSH
) SelectObject( (HDC
) dc
, wxDisableButtonBrush
);
364 // VZ: what's this?? there is no such ROP AFAIK
366 DWORD dwRop
= 0xFA0089L
;
368 DWORD dwRop
= 0xFA0089UL
;
375 right
-= 2 * m_marginX
;
376 bottom
-= 2 * m_marginY
;
379 ::PatBlt( (HDC
) dc
, left
, top
, right
, bottom
, dwRop
);
381 ::SelectObject( (HDC
) dc
, old
);
384 void wxBitmapButton::SetDefault()
386 wxButton::SetDefault();