1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/bmpbuttn.cpp
3 // Purpose: wxBitmapButton
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "bmpbuttn.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/bmpbuttn.h"
28 #include "wx/dcmemory.h"
31 #include "wx/msw/private.h"
33 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton
, wxButton
)
35 #define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1)
37 bool wxBitmapButton::Create(wxWindow
*parent
, wxWindowID id
, const wxBitmap
& bitmap
,
39 const wxSize
& size
, long style
,
40 const wxValidator
& validator
,
47 SetValidator(validator
);
48 #endif // wxUSE_VALIDATORS
50 parent
->AddChild(this);
52 m_backgroundColour
= parent
->GetBackgroundColour();
53 m_foregroundColour
= parent
->GetForegroundColour();
54 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 // VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
118 #define FOCUS_MARGIN 3
120 bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
122 #if defined(__WIN95__)
123 long style
= GetWindowLong((HWND
) GetHWND(), GWL_STYLE
);
124 if (style
& BS_BITMAP
)
126 // Let default procedure draw the bitmap, which is defined
127 // in the Windows resource.
132 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
) item
;
133 HDC hDC
= lpDIS
->hDC
;
134 UINT state
= lpDIS
->itemState
;
135 bool isSelected
= (state
& ODS_SELECTED
) != 0;
136 bool autoDraw
= (GetWindowStyleFlag() & wxBU_AUTODRAW
) != 0;
139 // choose the bitmap to use depending on the button state
142 if ( isSelected
&& m_bmpSelected
.Ok() )
143 bitmap
= &m_bmpSelected
;
144 else if ((state
& ODS_FOCUS
) && m_bmpFocus
.Ok())
145 bitmap
= &m_bmpFocus
;
146 else if ((state
& ODS_DISABLED
) && m_bmpDisabled
.Ok())
147 bitmap
= &m_bmpDisabled
;
149 bitmap
= &m_bmpNormal
;
154 // centre the bitmap in the control area
155 int x
= lpDIS
->rcItem
.left
;
156 int y
= lpDIS
->rcItem
.top
;
157 int width
= lpDIS
->rcItem
.right
- x
;
158 int height
= lpDIS
->rcItem
.bottom
- y
;
159 int wBmp
= bitmap
->GetWidth();
160 int hBmp
= bitmap
->GetHeight();
164 if(m_windowStyle
& wxBU_LEFT
)
165 x1
= x
+ (FOCUS_MARGIN
+1);
166 else if(m_windowStyle
& wxBU_RIGHT
)
167 x1
= x
+ (width
- wBmp
) - (FOCUS_MARGIN
+1);
169 x1
= x
+ (width
- wBmp
) / 2;
171 if(m_windowStyle
& wxBU_TOP
)
172 y1
= y
+ (FOCUS_MARGIN
+1);
173 else if(m_windowStyle
& wxBU_BOTTOM
)
174 y1
= y
+ (height
- hBmp
) - (FOCUS_MARGIN
+1);
176 y1
= y
+ (height
- hBmp
) / 2;
178 if ( isSelected
&& autoDraw
)
184 // draw the face, if auto-drawing
187 DrawFace((WXHDC
) hDC
,
188 lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
,
189 lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
,
195 dst
.SetHDC((WXHDC
) hDC
, FALSE
);
196 dst
.DrawBitmap(*bitmap
, x1
, y1
, TRUE
);
198 // draw focus / disabled state, if auto-drawing
199 if ( (state
& ODS_DISABLED
) && autoDraw
)
201 DrawButtonDisable((WXHDC
) hDC
,
202 lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
,
203 lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
,
206 else if ( (state
& ODS_FOCUS
) && autoDraw
)
208 DrawButtonFocus((WXHDC
) hDC
,
212 lpDIS
->rcItem
.bottom
,
219 // GRG Feb/2000, support for bmp buttons with Win95/98 standard LNF
221 #if defined(__WIN95__)
223 void wxBitmapButton::DrawFace( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
232 // create needed pens and brush
233 penHiLight
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DHILIGHT
));
234 penLight
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DLIGHT
));
235 penShadow
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DSHADOW
));
236 penDkShadow
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DDKSHADOW
));
237 // brushFace = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
238 // Taking the background colour fits in better with
239 // Windows XP themes.
240 brushFace
= CreateSolidBrush(m_backgroundColour
.m_pixel
);
242 // draw the rectangle
247 rect
.bottom
= bottom
;
248 FillRect((HDC
) dc
, &rect
, brushFace
);
251 oldp
= (HPEN
) SelectObject( (HDC
) dc
, sel
? penDkShadow
: penHiLight
);
252 MoveToEx((HDC
) dc
, left
, top
, NULL
); LineTo((HDC
) dc
, right
-1, top
);
253 MoveToEx((HDC
) dc
, left
, top
+1, NULL
); LineTo((HDC
) dc
, left
, bottom
-1);
255 SelectObject( (HDC
) dc
, sel
? penShadow
: penLight
);
256 MoveToEx((HDC
) dc
, left
+1, top
+1, NULL
); LineTo((HDC
) dc
, right
-2, top
+1);
257 MoveToEx((HDC
) dc
, left
+1, top
+2, NULL
); LineTo((HDC
) dc
, left
+1, bottom
-2);
259 SelectObject( (HDC
) dc
, sel
? penLight
: penShadow
);
260 MoveToEx((HDC
) dc
, left
+1, bottom
-2, NULL
); LineTo((HDC
) dc
, right
-1, bottom
-2);
261 MoveToEx((HDC
) dc
, right
-2, bottom
-3, NULL
); LineTo((HDC
) dc
, right
-2, top
);
263 SelectObject( (HDC
) dc
, sel
? penHiLight
: penDkShadow
);
264 MoveToEx((HDC
) dc
, left
, bottom
-1, NULL
); LineTo((HDC
) dc
, right
+2, bottom
-1);
265 MoveToEx((HDC
) dc
, right
-1, bottom
-2, NULL
); LineTo((HDC
) dc
, right
-1, top
-1);
267 // delete allocated resources
268 SelectObject((HDC
) dc
,oldp
);
269 DeleteObject(penHiLight
);
270 DeleteObject(penLight
);
271 DeleteObject(penShadow
);
272 DeleteObject(penDkShadow
);
273 DeleteObject(brushFace
);
278 void wxBitmapButton::DrawFace( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
286 // create needed pens and brush
287 penBorder
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_WINDOWFRAME
));
288 penShadow
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_BTNSHADOW
));
289 penLight
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_BTNHIGHLIGHT
));
290 brushFace
= CreateSolidBrush(COLOR_BTNFACE
);
292 // draw the rectangle
297 rect
.bottom
= bottom
;
298 FillRect((HDC
) dc
, &rect
, brushFace
);
301 oldp
= (HPEN
) SelectObject( (HDC
) dc
, penBorder
);
302 MoveToEx((HDC
) dc
,left
+1,top
,NULL
);LineTo((HDC
) dc
,right
-1,top
);
303 MoveToEx((HDC
) dc
,left
,top
+1,NULL
);LineTo((HDC
) dc
,left
,bottom
-1);
304 MoveToEx((HDC
) dc
,left
+1,bottom
-1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
305 MoveToEx((HDC
) dc
,right
-1,top
+1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
307 SelectObject( (HDC
) dc
, penShadow
);
310 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
);
311 LineTo((HDC
) dc
, left
+1 ,top
+1);
312 LineTo((HDC
) dc
, right
-2 ,top
+1);
316 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
);
317 LineTo((HDC
) dc
, right
-2 ,bottom
-2);
318 LineTo((HDC
) dc
, right
-2 ,top
);
320 MoveToEx((HDC
) dc
,left
+2 ,bottom
-3 ,NULL
);
321 LineTo((HDC
) dc
, right
-3 ,bottom
-3);
322 LineTo((HDC
) dc
, right
-3 ,top
+1);
324 SelectObject( (HDC
) dc
, penLight
);
326 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
);
327 LineTo((HDC
) dc
, left
+1 ,top
+1);
328 LineTo((HDC
) dc
, right
-2 ,top
+1);
331 // delete allocated resources
332 SelectObject((HDC
) dc
,oldp
);
333 DeleteObject(penBorder
);
334 DeleteObject(penLight
);
335 DeleteObject(penShadow
);
336 DeleteObject(brushFace
);
339 #endif // defined(__WIN95__)
342 void wxBitmapButton::DrawButtonFocus( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
348 rect
.bottom
= bottom
;
349 InflateRect( &rect
, - FOCUS_MARGIN
, - FOCUS_MARGIN
);
351 // GRG: the focus rectangle should not move when the button is pushed!
354 OffsetRect( &rect, 1, 1 );
357 DrawFocusRect( (HDC
) dc
, &rect
);
360 extern HBRUSH wxDisableButtonBrush
;
361 void wxBitmapButton::DrawButtonDisable( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool with_marg
)
363 HBRUSH old
= (HBRUSH
) SelectObject( (HDC
) dc
, wxDisableButtonBrush
);
365 // VZ: what's this?? there is no such ROP AFAIK
367 DWORD dwRop
= 0xFA0089L
;
369 DWORD dwRop
= 0xFA0089UL
;
376 right
-= 2 * m_marginX
;
377 bottom
-= 2 * m_marginY
;
380 ::PatBlt( (HDC
) dc
, left
, top
, right
, bottom
, dwRop
);
382 ::SelectObject( (HDC
) dc
, old
);
385 void wxBitmapButton::SetDefault()
387 wxButton::SetDefault();
390 #endif // wxUSE_BMPBUTTON