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
;
80 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
81 msStyle
|= WS_CLIPSIBLINGS
;
84 if(m_windowStyle
& wxBU_LEFT
)
86 if(m_windowStyle
& wxBU_RIGHT
)
88 if(m_windowStyle
& wxBU_TOP
)
90 if(m_windowStyle
& wxBU_BOTTOM
)
94 m_hWnd
= (WXHWND
)CreateWindowEx
107 // Subclass again for purposes of dialog editing mode
110 SetFont(parent
->GetFont());
112 SetSize(x
, y
, width
, height
);
117 void wxBitmapButton::SetBitmapLabel(const wxBitmap
& bitmap
)
119 m_buttonBitmap
= bitmap
;
122 // VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
123 #define FOCUS_MARGIN 3
125 bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
127 #if defined(__WIN95__)
128 long style
= GetWindowLong((HWND
) GetHWND(), GWL_STYLE
);
129 if (style
& BS_BITMAP
)
131 // Let default procedure draw the bitmap, which is defined
132 // in the Windows resource.
137 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
) item
;
138 HDC hDC
= lpDIS
->hDC
;
139 UINT state
= lpDIS
->itemState
;
140 bool isSelected
= (state
& ODS_SELECTED
) != 0;
141 bool autoDraw
= (GetWindowStyleFlag() & wxBU_AUTODRAW
) != 0;
144 // choose the bitmap to use depending on the button state
147 if ( isSelected
&& m_buttonBitmapSelected
.Ok() )
148 bitmap
= &m_buttonBitmapSelected
;
149 else if ((state
& ODS_FOCUS
) && m_buttonBitmapFocus
.Ok())
150 bitmap
= &m_buttonBitmapFocus
;
151 else if ((state
& ODS_DISABLED
) && m_buttonBitmapDisabled
.Ok())
152 bitmap
= &m_buttonBitmapDisabled
;
154 bitmap
= &m_buttonBitmap
;
159 // centre the bitmap in the control area
160 int x
= lpDIS
->rcItem
.left
;
161 int y
= lpDIS
->rcItem
.top
;
162 int width
= lpDIS
->rcItem
.right
- x
;
163 int height
= lpDIS
->rcItem
.bottom
- y
;
164 int wBmp
= bitmap
->GetWidth();
165 int hBmp
= bitmap
->GetHeight();
169 if(m_windowStyle
& wxBU_LEFT
)
170 x1
= x
+ (FOCUS_MARGIN
+1);
171 else if(m_windowStyle
& wxBU_RIGHT
)
172 x1
= x
+ (width
- wBmp
) - (FOCUS_MARGIN
+1);
174 x1
= x
+ (width
- wBmp
) / 2;
176 if(m_windowStyle
& wxBU_TOP
)
177 y1
= y
+ (FOCUS_MARGIN
+1);
178 else if(m_windowStyle
& wxBU_BOTTOM
)
179 y1
= y
+ (height
- hBmp
) - (FOCUS_MARGIN
+1);
181 y1
= y
+ (height
- hBmp
) / 2;
183 if ( isSelected
&& autoDraw
)
189 // draw the face, if auto-drawing
192 DrawFace((WXHDC
) hDC
,
193 lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
,
194 lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
,
200 dst
.SetHDC((WXHDC
) hDC
, FALSE
);
201 dst
.DrawBitmap(*bitmap
, x1
, y1
, TRUE
);
203 // draw focus / disabled state, if auto-drawing
204 if ( (state
& ODS_DISABLED
) && autoDraw
)
206 DrawButtonDisable((WXHDC
) hDC
,
207 lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
,
208 lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
,
211 else if ( (state
& ODS_FOCUS
) && autoDraw
)
213 DrawButtonFocus((WXHDC
) hDC
,
217 lpDIS
->rcItem
.bottom
,
224 // GRG Feb/2000, support for bmp buttons with Win95/98 standard LNF
226 #if defined(__WIN95__)
228 void wxBitmapButton::DrawFace( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
237 // create needed pens and brush
238 penHiLight
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DHILIGHT
));
239 penLight
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DLIGHT
));
240 penShadow
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DSHADOW
));
241 penDkShadow
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DDKSHADOW
));
242 brushFace
= CreateSolidBrush(GetSysColor(COLOR_BTNFACE
));
244 // draw the rectangle
249 rect
.bottom
= bottom
;
250 FillRect((HDC
) dc
, &rect
, brushFace
);
253 oldp
= (HPEN
) SelectObject( (HDC
) dc
, sel
? penDkShadow
: penHiLight
);
254 MoveToEx((HDC
) dc
, left
, top
, NULL
); LineTo((HDC
) dc
, right
-1, top
);
255 MoveToEx((HDC
) dc
, left
, top
+1, NULL
); LineTo((HDC
) dc
, left
, bottom
-1);
257 SelectObject( (HDC
) dc
, sel
? penShadow
: penLight
);
258 MoveToEx((HDC
) dc
, left
+1, top
+1, NULL
); LineTo((HDC
) dc
, right
-2, top
+1);
259 MoveToEx((HDC
) dc
, left
+1, top
+2, NULL
); LineTo((HDC
) dc
, left
+1, bottom
-2);
261 SelectObject( (HDC
) dc
, sel
? penLight
: penShadow
);
262 MoveToEx((HDC
) dc
, left
+1, bottom
-2, NULL
); LineTo((HDC
) dc
, right
-1, bottom
-2);
263 MoveToEx((HDC
) dc
, right
-2, bottom
-3, NULL
); LineTo((HDC
) dc
, right
-2, top
);
265 SelectObject( (HDC
) dc
, sel
? penHiLight
: penDkShadow
);
266 MoveToEx((HDC
) dc
, left
, bottom
-1, NULL
); LineTo((HDC
) dc
, right
+2, bottom
-1);
267 MoveToEx((HDC
) dc
, right
-1, bottom
-2, NULL
); LineTo((HDC
) dc
, right
-1, top
-1);
269 // delete allocated resources
270 SelectObject((HDC
) dc
,oldp
);
271 DeleteObject(penHiLight
);
272 DeleteObject(penLight
);
273 DeleteObject(penShadow
);
274 DeleteObject(penDkShadow
);
275 DeleteObject(brushFace
);
280 void wxBitmapButton::DrawFace( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
288 // create needed pens and brush
289 penBorder
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_WINDOWFRAME
));
290 penShadow
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_BTNSHADOW
));
291 penLight
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_BTNHIGHLIGHT
));
292 brushFace
= CreateSolidBrush(COLOR_BTNFACE
);
294 // draw the rectangle
299 rect
.bottom
= bottom
;
300 FillRect((HDC
) dc
, &rect
, brushFace
);
303 oldp
= (HPEN
) SelectObject( (HDC
) dc
, penBorder
);
304 MoveToEx((HDC
) dc
,left
+1,top
,NULL
);LineTo((HDC
) dc
,right
-1,top
);
305 MoveToEx((HDC
) dc
,left
,top
+1,NULL
);LineTo((HDC
) dc
,left
,bottom
-1);
306 MoveToEx((HDC
) dc
,left
+1,bottom
-1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
307 MoveToEx((HDC
) dc
,right
-1,top
+1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
309 SelectObject( (HDC
) dc
, penShadow
);
312 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
);
313 LineTo((HDC
) dc
, left
+1 ,top
+1);
314 LineTo((HDC
) dc
, right
-2 ,top
+1);
318 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
);
319 LineTo((HDC
) dc
, right
-2 ,bottom
-2);
320 LineTo((HDC
) dc
, right
-2 ,top
);
322 MoveToEx((HDC
) dc
,left
+2 ,bottom
-3 ,NULL
);
323 LineTo((HDC
) dc
, right
-3 ,bottom
-3);
324 LineTo((HDC
) dc
, right
-3 ,top
+1);
326 SelectObject( (HDC
) dc
, penLight
);
328 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
);
329 LineTo((HDC
) dc
, left
+1 ,top
+1);
330 LineTo((HDC
) dc
, right
-2 ,top
+1);
333 // delete allocated resources
334 SelectObject((HDC
) dc
,oldp
);
335 DeleteObject(penBorder
);
336 DeleteObject(penLight
);
337 DeleteObject(penShadow
);
338 DeleteObject(brushFace
);
341 #endif // defined(__WIN95__)
344 void wxBitmapButton::DrawButtonFocus( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
350 rect
.bottom
= bottom
;
351 InflateRect( &rect
, - FOCUS_MARGIN
, - FOCUS_MARGIN
);
353 // GRG: the focus rectangle should not move when the button is pushed!
356 OffsetRect( &rect, 1, 1 );
359 DrawFocusRect( (HDC
) dc
, &rect
);
362 extern HBRUSH wxDisableButtonBrush
;
363 void wxBitmapButton::DrawButtonDisable( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool with_marg
)
365 HBRUSH old
= (HBRUSH
) SelectObject( (HDC
) dc
, wxDisableButtonBrush
);
367 // VZ: what's this?? there is no such ROP AFAIK
369 DWORD dwRop
= 0xFA0089L
;
371 DWORD dwRop
= 0xFA0089UL
;
378 right
-= 2 * m_marginX
;
379 bottom
-= 2 * m_marginY
;
382 ::PatBlt( (HDC
) dc
, left
, top
, right
, bottom
, dwRop
);
384 ::SelectObject( (HDC
) dc
, old
);
387 void wxBitmapButton::SetDefault()
389 wxButton::SetDefault();