1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/bmpbuttn.cpp
3 // Purpose: wxBitmapButton
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 #if wxUSE_EXTENDED_RTTI
39 WX_DEFINE_FLAGS( wxBitmapButtonStyle
)
41 wxBEGIN_FLAGS( wxBitmapButtonStyle
)
42 // new style border flags, we put them first to
43 // use them for streaming out
44 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
45 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
46 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
47 wxFLAGS_MEMBER(wxBORDER_RAISED
)
48 wxFLAGS_MEMBER(wxBORDER_STATIC
)
49 wxFLAGS_MEMBER(wxBORDER_NONE
)
51 // old style border flags
52 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
53 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
54 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
55 wxFLAGS_MEMBER(wxRAISED_BORDER
)
56 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
57 wxFLAGS_MEMBER(wxBORDER
)
59 // standard window styles
60 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
61 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
62 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
63 wxFLAGS_MEMBER(wxWANTS_CHARS
)
64 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
65 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
66 wxFLAGS_MEMBER(wxVSCROLL
)
67 wxFLAGS_MEMBER(wxHSCROLL
)
69 wxFLAGS_MEMBER(wxBU_AUTODRAW
)
70 wxFLAGS_MEMBER(wxBU_LEFT
)
71 wxFLAGS_MEMBER(wxBU_RIGHT
)
72 wxFLAGS_MEMBER(wxBU_TOP
)
73 wxFLAGS_MEMBER(wxBU_BOTTOM
)
74 wxEND_FLAGS( wxBitmapButtonStyle
)
76 IMPLEMENT_DYNAMIC_CLASS_XTI(wxBitmapButton
, wxButton
,"wx/bmpbuttn.h")
78 wxBEGIN_PROPERTIES_TABLE(wxBitmapButton
)
79 wxPROPERTY_FLAGS( WindowStyle
, wxBitmapButtonStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
80 wxEND_PROPERTIES_TABLE()
82 wxBEGIN_HANDLERS_TABLE(wxBitmapButton
)
83 wxEND_HANDLERS_TABLE()
85 wxCONSTRUCTOR_5( wxBitmapButton
, wxWindow
* , Parent
, wxWindowID
, Id
, wxBitmap
, Bitmap
, wxPoint
, Position
, wxSize
, Size
)
88 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton
, wxButton
)
94 long "style" , wxBU_AUTODRAW
101 #define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1)
103 wxBitmapButtonBase::wxBitmapButtonBase()
113 bool wxBitmapButton::Create(wxWindow
*parent
, wxWindowID id
, const wxBitmap
& bitmap
,
115 const wxSize
& size
, long style
,
116 const wxValidator
& wxVALIDATOR_PARAM(validator
),
117 const wxString
& name
)
119 m_bmpNormal
= bitmap
;
123 SetValidator(validator
);
124 #endif // wxUSE_VALIDATORS
126 parent
->AddChild(this);
128 m_backgroundColour
= parent
->GetBackgroundColour();
129 m_foregroundColour
= parent
->GetForegroundColour();
130 m_windowStyle
= style
;
132 if ( style
& wxBU_AUTODRAW
)
134 m_marginX
= wxDEFAULT_BUTTON_MARGIN
;
135 m_marginY
= wxDEFAULT_BUTTON_MARGIN
;
144 m_windowId
= NewControlId();
148 if ( width
== -1 && bitmap
.Ok())
149 width
= bitmap
.GetWidth() + 2*m_marginX
;
151 if ( height
== -1 && bitmap
.Ok())
152 height
= bitmap
.GetHeight() + 2*m_marginY
;
154 long msStyle
= WS_VISIBLE
| WS_TABSTOP
| WS_CHILD
| BS_OWNERDRAW
;
156 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
157 msStyle
|= WS_CLIPSIBLINGS
;
160 if(m_windowStyle
& wxBU_LEFT
)
162 if(m_windowStyle
& wxBU_RIGHT
)
164 if(m_windowStyle
& wxBU_TOP
)
166 if(m_windowStyle
& wxBU_BOTTOM
)
167 msStyle
|= BS_BOTTOM
;
170 m_hWnd
= (WXHWND
)CreateWindowEx
183 // Subclass again for purposes of dialog editing mode
186 SetFont(parent
->GetFont());
188 SetSize(x
, y
, width
, height
);
193 // VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
194 #define FOCUS_MARGIN 3
196 bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
199 long style
= GetWindowLong((HWND
) GetHWND(), GWL_STYLE
);
200 if (style
& BS_BITMAP
)
202 // Let default procedure draw the bitmap, which is defined
203 // in the Windows resource.
208 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
) item
;
209 HDC hDC
= lpDIS
->hDC
;
210 UINT state
= lpDIS
->itemState
;
211 bool isSelected
= (state
& ODS_SELECTED
) != 0;
212 bool autoDraw
= (GetWindowStyleFlag() & wxBU_AUTODRAW
) != 0;
215 // choose the bitmap to use depending on the button state
218 if ( isSelected
&& m_bmpSelected
.Ok() )
219 bitmap
= &m_bmpSelected
;
220 else if ((state
& ODS_FOCUS
) && m_bmpFocus
.Ok())
221 bitmap
= &m_bmpFocus
;
222 else if ((state
& ODS_DISABLED
) && m_bmpDisabled
.Ok())
223 bitmap
= &m_bmpDisabled
;
225 bitmap
= &m_bmpNormal
;
230 // centre the bitmap in the control area
231 int x
= lpDIS
->rcItem
.left
;
232 int y
= lpDIS
->rcItem
.top
;
233 int width
= lpDIS
->rcItem
.right
- x
;
234 int height
= lpDIS
->rcItem
.bottom
- y
;
235 int wBmp
= bitmap
->GetWidth();
236 int hBmp
= bitmap
->GetHeight();
240 if(m_windowStyle
& wxBU_LEFT
)
241 x1
= x
+ (FOCUS_MARGIN
+1);
242 else if(m_windowStyle
& wxBU_RIGHT
)
243 x1
= x
+ (width
- wBmp
) - (FOCUS_MARGIN
+1);
245 x1
= x
+ (width
- wBmp
) / 2;
247 if(m_windowStyle
& wxBU_TOP
)
248 y1
= y
+ (FOCUS_MARGIN
+1);
249 else if(m_windowStyle
& wxBU_BOTTOM
)
250 y1
= y
+ (height
- hBmp
) - (FOCUS_MARGIN
+1);
252 y1
= y
+ (height
- hBmp
) / 2;
254 if ( isSelected
&& autoDraw
)
260 // draw the face, if auto-drawing
263 DrawFace((WXHDC
) hDC
,
264 lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
,
265 lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
,
271 dst
.SetHDC((WXHDC
) hDC
, FALSE
);
272 dst
.DrawBitmap(*bitmap
, x1
, y1
, TRUE
);
274 // draw focus / disabled state, if auto-drawing
275 if ( (state
& ODS_DISABLED
) && autoDraw
)
277 DrawButtonDisable((WXHDC
) hDC
,
278 lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
,
279 lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
,
282 else if ( (state
& ODS_FOCUS
) && autoDraw
)
284 DrawButtonFocus((WXHDC
) hDC
,
288 lpDIS
->rcItem
.bottom
,
295 // GRG Feb/2000, support for bmp buttons with Win95/98 standard LNF
297 #if defined(__WIN95__)
299 void wxBitmapButton::DrawFace( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
308 // create needed pens and brush
309 penHiLight
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DHILIGHT
));
310 penLight
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DLIGHT
));
311 penShadow
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DSHADOW
));
312 penDkShadow
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DDKSHADOW
));
313 // brushFace = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
314 // Taking the background colour fits in better with
315 // Windows XP themes.
316 brushFace
= CreateSolidBrush(m_backgroundColour
.m_pixel
);
318 // draw the rectangle
323 rect
.bottom
= bottom
;
324 FillRect((HDC
) dc
, &rect
, brushFace
);
327 oldp
= (HPEN
) SelectObject( (HDC
) dc
, sel
? penDkShadow
: penHiLight
);
329 wxDrawLine((HDC
) dc
, left
, top
, right
-1, top
);
330 wxDrawLine((HDC
) dc
, left
, top
+1, left
, bottom
-1);
332 SelectObject( (HDC
) dc
, sel
? penShadow
: penLight
);
333 wxDrawLine((HDC
) dc
, left
+1, top
+1, right
-2, top
+1);
334 wxDrawLine((HDC
) dc
, left
+1, top
+2, left
+1, bottom
-2);
336 SelectObject( (HDC
) dc
, sel
? penLight
: penShadow
);
337 wxDrawLine((HDC
) dc
, left
+1, bottom
-2, right
-1, bottom
-2);
338 wxDrawLine((HDC
) dc
, right
-2, bottom
-3, right
-2, top
);
340 SelectObject( (HDC
) dc
, sel
? penHiLight
: penDkShadow
);
341 wxDrawLine((HDC
) dc
, left
, bottom
-1, right
+2, bottom
-1);
342 wxDrawLine((HDC
) dc
, right
-1, bottom
-2, right
-1, top
-1);
344 // delete allocated resources
345 SelectObject((HDC
) dc
,oldp
);
346 DeleteObject(penHiLight
);
347 DeleteObject(penLight
);
348 DeleteObject(penShadow
);
349 DeleteObject(penDkShadow
);
350 DeleteObject(brushFace
);
355 void wxBitmapButton::DrawFace( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
363 // create needed pens and brush
364 penBorder
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_WINDOWFRAME
));
365 penShadow
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_BTNSHADOW
));
366 penLight
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_BTNHIGHLIGHT
));
367 brushFace
= CreateSolidBrush(COLOR_BTNFACE
);
369 // draw the rectangle
374 rect
.bottom
= bottom
;
375 FillRect((HDC
) dc
, &rect
, brushFace
);
378 oldp
= (HPEN
) SelectObject( (HDC
) dc
, penBorder
);
379 MoveToEx((HDC
) dc
,left
+1,top
,NULL
);LineTo((HDC
) dc
,right
-1,top
);
380 MoveToEx((HDC
) dc
,left
,top
+1,NULL
);LineTo((HDC
) dc
,left
,bottom
-1);
381 MoveToEx((HDC
) dc
,left
+1,bottom
-1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
382 MoveToEx((HDC
) dc
,right
-1,top
+1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
384 SelectObject( (HDC
) dc
, penShadow
);
387 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
);
388 LineTo((HDC
) dc
, left
+1 ,top
+1);
389 LineTo((HDC
) dc
, right
-2 ,top
+1);
393 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
);
394 LineTo((HDC
) dc
, right
-2 ,bottom
-2);
395 LineTo((HDC
) dc
, right
-2 ,top
);
397 MoveToEx((HDC
) dc
,left
+2 ,bottom
-3 ,NULL
);
398 LineTo((HDC
) dc
, right
-3 ,bottom
-3);
399 LineTo((HDC
) dc
, right
-3 ,top
+1);
401 SelectObject( (HDC
) dc
, penLight
);
403 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
);
404 LineTo((HDC
) dc
, left
+1 ,top
+1);
405 LineTo((HDC
) dc
, right
-2 ,top
+1);
408 // delete allocated resources
409 SelectObject((HDC
) dc
,oldp
);
410 DeleteObject(penBorder
);
411 DeleteObject(penLight
);
412 DeleteObject(penShadow
);
413 DeleteObject(brushFace
);
416 #endif // defined(__WIN95__)
419 void wxBitmapButton::DrawButtonFocus( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool sel
)
425 rect
.bottom
= bottom
;
426 InflateRect( &rect
, - FOCUS_MARGIN
, - FOCUS_MARGIN
);
428 // GRG: the focus rectangle should not move when the button is pushed!
431 OffsetRect( &rect, 1, 1 );
434 DrawFocusRect( (HDC
) dc
, &rect
);
437 extern HBRUSH wxDisableButtonBrush
;
438 void wxBitmapButton::DrawButtonDisable( WXHDC dc
, int left
, int top
, int right
, int bottom
, bool with_marg
)
440 HBRUSH old
= (HBRUSH
) SelectObject( (HDC
) dc
, wxDisableButtonBrush
);
442 // VZ: what's this?? there is no such ROP AFAIK
444 DWORD dwRop
= 0xFA0089L
;
446 DWORD dwRop
= 0xFA0089UL
;
453 right
-= 2 * m_marginX
;
454 bottom
-= 2 * m_marginY
;
457 ::PatBlt( (HDC
) dc
, left
, top
, right
, bottom
, dwRop
);
459 ::SelectObject( (HDC
) dc
, old
);
462 void wxBitmapButton::SetDefault()
464 wxButton::SetDefault();
467 #endif // wxUSE_BMPBUTTON