From: Stefan Csomor Date: Fri, 9 Oct 2009 12:24:35 +0000 (+0000) Subject: moving bitmapbutton functionality up to button for OSX X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/b38dc31ffa6eb372727a7b0e65001a1c3d107fcb moving bitmapbutton functionality up to button for OSX git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@62347 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/bmpbuttn.h b/include/wx/bmpbuttn.h index 73ebf694a0..f34ae50935 100644 --- a/include/wx/bmpbuttn.h +++ b/include/wx/bmpbuttn.h @@ -18,10 +18,10 @@ #include "wx/button.h" -// FIXME: right now only wxMSW and wxGTK implement bitmap support in wxButton +// FIXME: right now only wxMSW, wxGTK and wxOSX implement bitmap support in wxButton // itself, this shouldn't be used for the other platforms neither // when all of them do it -#if (defined(__WXMSW__) || defined(__WXGTK20__)) && !defined(__WXUNIVERSAL__) +#if (defined(__WXMSW__) || defined(__WXGTK20__) || defined(__WXOSX__)) && !defined(__WXUNIVERSAL__) #define wxHAS_BUTTON_BITMAP #endif diff --git a/include/wx/osx/bmpbuttn.h b/include/wx/osx/bmpbuttn.h index 94c97608b5..c6edbf4e6b 100644 --- a/include/wx/osx/bmpbuttn.h +++ b/include/wx/osx/bmpbuttn.h @@ -40,15 +40,10 @@ public: const wxString& name = wxButtonNameStr); protected: - void OnEnterWindow( wxMouseEvent& event); - void OnLeaveWindow( wxMouseEvent& event); virtual wxSize DoGetBestSize() const; - virtual void DoSetBitmap(const wxBitmap& bitmap, State which); - DECLARE_DYNAMIC_CLASS(wxBitmapButton) - DECLARE_EVENT_TABLE() }; #endif // _WX_OSX_BMPBUTTN_H_ diff --git a/include/wx/osx/button.h b/include/wx/osx/button.h index 48a8214450..8959d9b142 100644 --- a/include/wx/osx/button.h +++ b/include/wx/osx/button.h @@ -54,13 +54,30 @@ public: protected: virtual wxSize DoGetBestSize() const ; -#if wxOSX_USE_COCOA + void OnEnterWindow( wxMouseEvent& event); + void OnLeaveWindow( wxMouseEvent& event); + virtual wxBitmap DoGetBitmap(State which) const; virtual void DoSetBitmap(const wxBitmap& bitmap, State which); virtual void DoSetBitmapPosition(wxDirection dir); -#endif // wxOSX_USE_COCOA + virtual void DoSetBitmapMargins(int x, int y) + { + m_marginX = x; + m_marginY = y; + } + + // the margins around the bitmap + int m_marginX; + int m_marginY; + + // the bitmaps for the different state of the buttons, all of them may be + // invalid and the button only shows a bitmap at all if State_Normal bitmap + // is valid + wxBitmap m_bitmaps[State_Max]; + DECLARE_DYNAMIC_CLASS(wxButton) + DECLARE_EVENT_TABLE() }; // OS X specific class, not part of public wx API @@ -97,7 +114,6 @@ public: protected: virtual wxSize DoGetBestSize() const ; - }; #endif // _WX_OSX_BUTTON_H_ diff --git a/include/wx/osx/core/private.h b/include/wx/osx/core/private.h index a011a3d5a9..2ea1bd40a7 100644 --- a/include/wx/osx/core/private.h +++ b/include/wx/osx/core/private.h @@ -588,14 +588,14 @@ public : }; // -// common interface bitmapbuttons +// common interface for buttons // -class wxBitmapButtonImpl +class wxButtonImpl { public : - wxBitmapButtonImpl(){} - virtual ~wxBitmapButtonImpl(){} + wxButtonImpl(){} + virtual ~wxButtonImpl(){} virtual void SetPressedBitmap( const wxBitmap& bitmap ) = 0; } ; diff --git a/src/osx/bmpbuttn_osx.cpp b/src/osx/bmpbuttn_osx.cpp index 6264a9baa8..9a1cb174ca 100644 --- a/src/osx/bmpbuttn_osx.cpp +++ b/src/osx/bmpbuttn_osx.cpp @@ -22,10 +22,6 @@ IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton) -BEGIN_EVENT_TABLE(wxBitmapButton, wxButton) - EVT_ENTER_WINDOW(wxBitmapButton::OnEnterWindow) - EVT_LEAVE_WINDOW(wxBitmapButton::OnLeaveWindow) -END_EVENT_TABLE() #include "wx/osx/private.h" @@ -66,23 +62,6 @@ bool wxBitmapButton::Create( wxWindow *parent, return true; } -void wxBitmapButton::DoSetBitmap(const wxBitmap& bitmap, State which) -{ - wxBitmapButtonBase::DoSetBitmap(bitmap, which); - - // we don't support any other states currently - if ( which == State_Normal ) - { - m_peer->SetBitmap( bitmap ); - } - else if ( which == State_Pressed ) - { - wxBitmapButtonImpl* bi = dynamic_cast (m_peer); - if ( bi ) - bi->SetPressedBitmap(bitmap); - } -} - wxSize wxBitmapButton::DoGetBestSize() const { wxSize best(m_marginX, m_marginY); @@ -97,16 +76,4 @@ wxSize wxBitmapButton::DoGetBestSize() const return best; } -void wxBitmapButton::OnEnterWindow( wxMouseEvent& WXUNUSED(event)) -{ - if ( DoGetBitmap( State_Current ).IsOk() ) - m_peer->SetBitmap( DoGetBitmap( State_Current ) ); -} - -void wxBitmapButton::OnLeaveWindow( wxMouseEvent& WXUNUSED(event)) -{ - if ( DoGetBitmap( State_Current ).IsOk() ) - m_peer->SetBitmap( DoGetBitmap( State_Normal ) ); -} - #endif // wxUSE_BMPBUTTON diff --git a/src/osx/button_osx.cpp b/src/osx/button_osx.cpp index 52208f4ed2..b61d296384 100644 --- a/src/osx/button_osx.cpp +++ b/src/osx/button_osx.cpp @@ -25,6 +25,11 @@ IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl) +BEGIN_EVENT_TABLE(wxButton, wxControl) + EVT_ENTER_WINDOW(wxButton::OnEnterWindow) + EVT_LEAVE_WINDOW(wxButton::OnLeaveWindow) +END_EVENT_TABLE() + bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& lbl, @@ -34,6 +39,9 @@ bool wxButton::Create(wxWindow *parent, const wxValidator& validator, const wxString& name) { + m_marginX = + m_marginY = 0; + // FIXME: this hack is needed because we're called from // wxBitmapButton::Create() with this style and we currently use a // different wxWidgetImpl method (CreateBitmapButton() rather than @@ -79,19 +87,23 @@ void wxButton::SetLabel(const wxString& label) wxButtonBase::SetLabel(label); } -// there is no support for button bitmaps in wxOSX/Carbon so there is no need -// for these methods there -#if wxOSX_USE_COCOA - wxBitmap wxButton::DoGetBitmap(State which) const { - return which == State_Normal ? m_peer->GetBitmap() : wxBitmap(); + return m_bitmaps[which]; } void wxButton::DoSetBitmap(const wxBitmap& bitmap, State which) { + m_bitmaps[which] = bitmap; + if ( which == State_Normal ) m_peer->SetBitmap(bitmap); + else if ( which == State_Pressed ) + { + wxButtonImpl* bi = dynamic_cast (m_peer); + if ( bi ) + bi->SetPressedBitmap(bitmap); + } } void wxButton::DoSetBitmapPosition(wxDirection dir) @@ -99,8 +111,6 @@ void wxButton::DoSetBitmapPosition(wxDirection dir) m_peer->SetBitmapPosition(dir); } -#endif // wxOSX_USE_COCOA - wxWindow *wxButton::SetDefault() { wxWindow *btnOldDefault = wxButtonBase::SetDefault(); @@ -121,6 +131,18 @@ void wxButton::Command (wxCommandEvent & WXUNUSED(event)) // ProcessCommand(event); } +void wxButton::OnEnterWindow( wxMouseEvent& WXUNUSED(event)) +{ + if ( DoGetBitmap( State_Current ).IsOk() ) + m_peer->SetBitmap( DoGetBitmap( State_Current ) ); +} + +void wxButton::OnLeaveWindow( wxMouseEvent& WXUNUSED(event)) +{ + if ( DoGetBitmap( State_Current ).IsOk() ) + m_peer->SetBitmap( DoGetBitmap( State_Normal ) ); +} + bool wxButton::OSXHandleClicked( double WXUNUSED(timestampsec) ) { wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId); diff --git a/src/osx/carbon/bmpbuttn.cpp b/src/osx/carbon/bmpbuttn.cpp index 55147f94e4..5862d0b1b1 100644 --- a/src/osx/carbon/bmpbuttn.cpp +++ b/src/osx/carbon/bmpbuttn.cpp @@ -27,7 +27,7 @@ namespace // define a derived class to override SetBitmap() and also to provide // InitButtonContentInfo() helper used by CreateBitmapButton() -class wxMacBitmapButton : public wxMacControl, public wxBitmapButtonImpl +class wxMacBitmapButton : public wxMacControl, public wxButtonImpl { public: wxMacBitmapButton(wxWindowMac* peer, const wxBitmap& bitmap, int style) diff --git a/src/osx/cocoa/bmpbuttn.mm b/src/osx/cocoa/bmpbuttn.mm index ccfc8c9881..bc9a26325d 100644 --- a/src/osx/cocoa/bmpbuttn.mm +++ b/src/osx/cocoa/bmpbuttn.mm @@ -9,69 +9,4 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#include "wx/wxprec.h" - -#if wxUSE_BMPBUTTON - -#include "wx/bmpbuttn.h" -#include "wx/image.h" - -#ifndef WX_PRECOMP - #include "wx/dcmemory.h" -#endif - -#include "wx/osx/private.h" - -class wxBitmapButtonCocoaImpl : public wxWidgetCocoaImpl, public wxBitmapButtonImpl -{ - public : - wxBitmapButtonCocoaImpl( wxWindowMac* peer , WXWidget w) : wxWidgetCocoaImpl(peer,w) - { - } - - void SetPressedBitmap( const wxBitmap& bitmap ) - { - wxNSButton* button = (wxNSButton*) m_osxView; - [button setAlternateImage: bitmap.GetNSImage()]; - [button setButtonType:NSMomentaryChangeButton]; - } -} ; - -wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer, - wxWindowMac* WXUNUSED(parent), - wxWindowID WXUNUSED(id), - const wxBitmap& bitmap, - const wxPoint& pos, - const wxSize& size, - long style, - long WXUNUSED(extraStyle)) -{ - NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; - wxNSButton* v = [[wxNSButton alloc] initWithFrame:r]; - - // trying to get as close as possible to flags - if ( style & wxBORDER_NONE ) - { - [v setBezelStyle:NSShadowlessSquareBezelStyle]; - [v setBordered:NO]; - } - else - { - // see trac #11128 for a thorough discussion - if ( (style & wxBORDER_MASK) == wxBORDER_RAISED ) - [v setBezelStyle:NSRegularSquareBezelStyle]; - else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN ) - [v setBezelStyle:NSSmallSquareBezelStyle]; - else - [v setBezelStyle:NSShadowlessSquareBezelStyle]; - } - - if (bitmap.Ok()) - [v setImage:bitmap.GetNSImage() ]; - - [v setButtonType:NSMomentaryPushInButton]; - wxWidgetCocoaImpl* c = new wxBitmapButtonCocoaImpl( wxpeer, v ); - return c; -} - -#endif +// everything is done in button.mm now \ No newline at end of file diff --git a/src/osx/cocoa/button.mm b/src/osx/cocoa/button.mm index f83027bf6b..9dbcbee2da 100644 --- a/src/osx/cocoa/button.mm +++ b/src/osx/cocoa/button.mm @@ -97,7 +97,7 @@ wxSize wxButton::GetDefaultSize() namespace { -class wxButtonCocoaImpl : public wxWidgetCocoaImpl +class wxButtonCocoaImpl : public wxWidgetCocoaImpl, public wxButtonImpl { public: wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v) @@ -107,12 +107,20 @@ public: virtual void SetBitmap(const wxBitmap& bitmap) { - [GetNSButton() setBezelStyle:bitmap.IsOk() ? NSRegularSquareBezelStyle - : NSRoundedBezelStyle]; + // switch bezel style for plain pushbuttons + if ( bitmap.IsOk() && [GetNSButton() bezelStyle] == NSRoundedBezelStyle ) + [GetNSButton() setBezelStyle:NSRegularSquareBezelStyle ]; wxWidgetCocoaImpl::SetBitmap(bitmap); } + void SetPressedBitmap( const wxBitmap& bitmap ) + { + NSButton* button = GetNSButton(); + [button setAlternateImage: bitmap.GetNSImage()]; + [button setButtonType:NSMomentaryChangeButton]; + } + private: NSButton *GetNSButton() const { @@ -163,6 +171,47 @@ void wxWidgetCocoaImpl::PerformClick() [(NSControl*)m_osxView performClick:nil]; } +#if wxUSE_BMPBUTTON + +wxWidgetImplType* wxWidgetImpl::CreateBitmapButton( wxWindowMac* wxpeer, + wxWindowMac* WXUNUSED(parent), + wxWindowID WXUNUSED(id), + const wxBitmap& bitmap, + const wxPoint& pos, + const wxSize& size, + long style, + long WXUNUSED(extraStyle)) +{ + NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; + wxNSButton* v = [[wxNSButton alloc] initWithFrame:r]; + + // trying to get as close as possible to flags + if ( style & wxBORDER_NONE ) + { + [v setBezelStyle:NSShadowlessSquareBezelStyle]; + [v setBordered:NO]; + } + else + { + // see trac #11128 for a thorough discussion + if ( (style & wxBORDER_MASK) == wxBORDER_RAISED ) + [v setBezelStyle:NSRegularSquareBezelStyle]; + else if ( (style & wxBORDER_MASK) == wxBORDER_SUNKEN ) + [v setBezelStyle:NSSmallSquareBezelStyle]; + else + [v setBezelStyle:NSShadowlessSquareBezelStyle]; + } + + if (bitmap.Ok()) + [v setImage:bitmap.GetNSImage() ]; + + [v setButtonType:NSMomentaryPushInButton]; + wxWidgetCocoaImpl* c = new wxButtonCocoaImpl( wxpeer, v ); + return c; +} + +#endif + // // wxDisclosureButton implementation //