implementing rollover and pressed image for bitmapbutton on osx_cocoa
[wxWidgets.git] / src / osx / bmpbuttn_osx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/bmpbuttn_osx.cpp
3 // Purpose: wxBitmapButton
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id: bmpbuttn.cpp 54820 2008-07-29 20:04:11Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_BMPBUTTON
15
16 #include "wx/bmpbuttn.h"
17 #include "wx/image.h"
18
19 #ifndef WX_PRECOMP
20 #include "wx/dcmemory.h"
21 #endif
22
23 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
24
25 BEGIN_EVENT_TABLE(wxBitmapButton, wxButton)
26 EVT_ENTER_WINDOW(wxBitmapButton::OnEnterWindow)
27 EVT_LEAVE_WINDOW(wxBitmapButton::OnLeaveWindow)
28 END_EVENT_TABLE()
29
30 #include "wx/osx/private.h"
31
32 //---------------------------------------------------------------------------
33
34 bool wxBitmapButton::Create( wxWindow *parent,
35 wxWindowID id,
36 const wxBitmap& bitmap,
37 const wxPoint& pos,
38 const wxSize& size,
39 long style,
40 const wxValidator& validator,
41 const wxString& name )
42 {
43 m_macIsUserPane = false;
44
45 if ( !wxBitmapButtonBase::Create(parent, id, pos, size, style,
46 validator, name) )
47 return false;
48
49 if ( style & wxBU_AUTODRAW )
50 {
51 m_marginX =
52 m_marginY = wxDEFAULT_BUTTON_MARGIN;
53 }
54 else
55 {
56 m_marginX =
57 m_marginY = 0;
58 }
59
60 m_bitmaps[State_Normal] = bitmap;
61
62 m_peer = wxWidgetImpl::CreateBitmapButton( this, parent, id, bitmap, pos, size, style, GetExtraStyle() );
63
64 MacPostControlCreate( pos, size );
65
66 return true;
67 }
68
69 void wxBitmapButton::DoSetBitmap(const wxBitmap& bitmap, State which)
70 {
71 wxBitmapButtonBase::DoSetBitmap(bitmap, which);
72
73 // we don't support any other states currently
74 if ( which == State_Normal )
75 {
76 m_peer->SetBitmap( bitmap );
77 }
78 else if ( which == State_Pressed )
79 {
80 wxBitmapButtonImpl* bi = dynamic_cast<wxBitmapButtonImpl*> (m_peer);
81 if ( bi )
82 bi->SetPressedBitmap(bitmap);
83 }
84 }
85
86 wxSize wxBitmapButton::DoGetBestSize() const
87 {
88 wxSize best(m_marginX, m_marginY);
89
90 best *= 2;
91
92 if ( GetBitmapLabel().IsOk() )
93 {
94 best += GetBitmapLabel().GetSize();
95 }
96
97 return best;
98 }
99
100 void wxBitmapButton::OnEnterWindow( wxMouseEvent& WXUNUSED(event))
101 {
102 if ( DoGetBitmap( State_Current ).IsOk() )
103 m_peer->SetBitmap( DoGetBitmap( State_Current ) );
104 }
105
106 void wxBitmapButton::OnLeaveWindow( wxMouseEvent& WXUNUSED(event))
107 {
108 if ( DoGetBitmap( State_Current ).IsOk() )
109 m_peer->SetBitmap( DoGetBitmap( State_Normal ) );
110 }
111
112 #endif // wxUSE_BMPBUTTON