From e5d05b907c823c87f7d4f92e535c15a7ee0124f2 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 21 Jun 2009 03:01:53 +0000 Subject: [PATCH] added support for bitmaps in wxButton to wxOSX/Cocoa git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61158 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/osx/button.h | 26 +++++++++++-------- include/wx/osx/carbon/private.h | 2 ++ include/wx/osx/cocoa/private.h | 2 ++ include/wx/osx/core/private.h | 2 ++ interface/wx/button.h | 10 +++++--- src/osx/button_osx.cpp | 22 ++++++++++++++++ src/osx/carbon/window.cpp | 10 ++++++++ src/osx/cocoa/button.mm | 32 +++++++++++++++++++++-- src/osx/cocoa/window.mm | 45 +++++++++++++++++++++++++++++++++ 10 files changed, 135 insertions(+), 17 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 48baa8bd11..be69b64869 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -349,6 +349,7 @@ All: All (GUI): +- Added support for showing bitmaps in wxButton. - wxWindow::SetAutoLayout() now works for all windows, not just panels. - Support wxListCtrl columns, items and image lists in XRC (Kinaou HervĂ©). - Added support for wxFileCtrl to XRC (Kinaou HervĂ©). diff --git a/include/wx/osx/button.h b/include/wx/osx/button.h index 8a1a8e06e6..48a8214450 100644 --- a/include/wx/osx/button.h +++ b/include/wx/osx/button.h @@ -9,16 +9,14 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#ifndef _WX_BUTTON_H_ -#define _WX_BUTTON_H_ +#ifndef _WX_OSX_BUTTON_H_ +#define _WX_OSX_BUTTON_H_ #include "wx/control.h" #include "wx/gdicmn.h" -WXDLLIMPEXP_DATA_CORE(extern const char) wxButtonNameStr[]; - // Pushbutton -class WXDLLIMPEXP_CORE wxButton: public wxButtonBase +class WXDLLIMPEXP_CORE wxButton : public wxButtonBase { public: wxButton() {} @@ -50,16 +48,23 @@ public: virtual void Command(wxCommandEvent& event); // osx specific event handling common for all osx-ports - + virtual bool OSXHandleClicked( double timestampsec ); protected: virtual wxSize DoGetBestSize() const ; +#if wxOSX_USE_COCOA + virtual wxBitmap DoGetBitmap(State which) const; + virtual void DoSetBitmap(const wxBitmap& bitmap, State which); + virtual void DoSetBitmapPosition(wxDirection dir); +#endif // wxOSX_USE_COCOA + DECLARE_DYNAMIC_CLASS(wxButton) }; -class WXDLLIMPEXP_CORE wxDisclosureTriangle: public wxControl +// OS X specific class, not part of public wx API +class WXDLLIMPEXP_CORE wxDisclosureTriangle : public wxControl { public: wxDisclosureTriangle(wxWindow *parent, @@ -87,13 +92,12 @@ public: bool IsOpen() const; // osx specific event handling common for all osx-ports - + virtual bool OSXHandleClicked( double timestampsec ); protected: virtual wxSize DoGetBestSize() const ; - + }; -#endif - // _WX_BUTTON_H_ +#endif // _WX_OSX_BUTTON_H_ diff --git a/include/wx/osx/carbon/private.h b/include/wx/osx/carbon/private.h index 7beee9fa2d..d8dc822342 100644 --- a/include/wx/osx/carbon/private.h +++ b/include/wx/osx/carbon/private.h @@ -335,7 +335,9 @@ public : wxInt32 GetValue() const; void SetValue( wxInt32 v ); + wxBitmap GetBitmap() const; void SetBitmap( const wxBitmap& bitmap ); + void SetBitmapPosition( wxDirection dir ); void SetupTabs( const wxNotebook ¬ebook ); void GetBestRect( wxRect *r ) const; diff --git a/include/wx/osx/cocoa/private.h b/include/wx/osx/cocoa/private.h index fb8f330b3d..ec69ce31d0 100644 --- a/include/wx/osx/cocoa/private.h +++ b/include/wx/osx/cocoa/private.h @@ -106,7 +106,9 @@ public : wxInt32 GetValue() const; void SetValue( wxInt32 v ); + wxBitmap GetBitmap() const; void SetBitmap( const wxBitmap& bitmap ); + void SetBitmapPosition( wxDirection dir ); void SetupTabs( const wxNotebook ¬ebook ); void GetBestRect( wxRect *r ) const; bool IsEnabled() const; diff --git a/include/wx/osx/core/private.h b/include/wx/osx/core/private.h index cc2193377b..41ffd3cbdb 100644 --- a/include/wx/osx/core/private.h +++ b/include/wx/osx/core/private.h @@ -235,7 +235,9 @@ public : virtual wxInt32 GetValue() const = 0; virtual void SetValue( wxInt32 v ) = 0; + virtual wxBitmap GetBitmap() const = 0; virtual void SetBitmap( const wxBitmap& bitmap ) = 0; + virtual void SetBitmapPosition( wxDirection dir ) = 0; virtual void SetupTabs( const wxNotebook ¬ebook ) =0; virtual void GetBestRect( wxRect *r ) const = 0; virtual bool IsEnabled() const = 0; diff --git a/interface/wx/button.h b/interface/wx/button.h index 764d87fca5..5669cfc7bc 100644 --- a/interface/wx/button.h +++ b/interface/wx/button.h @@ -34,8 +34,8 @@ bitmap but uses a standard id would display a label too. @style{wxBORDER_NONE} Creates a button without border. This is currently implemented in MSW, - GTK2 and OSX ports but in the latter only applies to buttons with - bitmaps and using bitmap of one of the standard sizes only, namely + GTK2 and OSX/Carbon ports but in the latter only applies to buttons + with bitmaps and using bitmap of one of the standard sizes only, namely 128*128, 48*48, 24*24 or 16*16. In all the other cases wxBORDER_NONE is ignored under OSX. @endStyleTable @@ -54,14 +54,16 @@ @endEventTable - Since version 2.9.1 wxButton supports showing both text and an image, see + Since version 2.9.1 wxButton supports showing both text and an image + (currently only when using wxMSW, wxGTK or wxOSX/Cocoa ports), see SetBitmap() and SetBitmapLabel(), SetBitmapDisabled() &c methods. In the previous wxWidgets versions this functionality was only available in (the now trivial) wxBitmapButton class which was only capable of showing an image without text. A button may have either a single image for all states or different images - for the following states: + for the following states (different images are not currently supported + under OS X where the normal image is used for all states): @li @b normal: the default state @li @b disabled: bitmap shown when the button is disabled. @li @b pressed: bitmap shown when the button is pushed (e.g. while the user diff --git a/src/osx/button_osx.cpp b/src/osx/button_osx.cpp index a7c7cf869c..12b630c412 100644 --- a/src/osx/button_osx.cpp +++ b/src/osx/button_osx.cpp @@ -67,6 +67,28 @@ 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(); +} + +void wxButton::DoSetBitmap(const wxBitmap& bitmap, State which) +{ + if ( which == State_Normal ) + m_peer->SetBitmap(bitmap); +} + +void wxButton::DoSetBitmapPosition(wxDirection dir) +{ + m_peer->SetBitmapPosition(dir); +} + +#endif // wxOSX_USE_COCOA + wxWindow *wxButton::SetDefault() { wxWindow *btnOldDefault = wxButtonBase::SetDefault(); diff --git a/src/osx/carbon/window.cpp b/src/osx/carbon/window.cpp index b3aab0c014..6fdc1d132b 100644 --- a/src/osx/carbon/window.cpp +++ b/src/osx/carbon/window.cpp @@ -1467,11 +1467,21 @@ wxMacControl* wxMacControl::GetReferenceFromNativeControl(ControlRef control) return NULL; } +wxBitmap wxMacControl::GetBitmap() const +{ + return wxNullBitmap; +} + void wxMacControl::SetBitmap( const wxBitmap& WXUNUSED(bmp) ) { // implemented in the respective subclasses } +void wxMacControl::SetBitmapPosition( wxDirection WXUNUSED(dir) ) +{ + // implemented in the same subclasses that implement SetBitmap() +} + void wxMacControl::SetScrollThumb( wxInt32 WXUNUSED(pos), wxInt32 WXUNUSED(viewsize) ) { // implemented in respective subclass diff --git a/src/osx/cocoa/button.mm b/src/osx/cocoa/button.mm index 6907d26536..7a3c485d01 100644 --- a/src/osx/cocoa/button.mm +++ b/src/osx/cocoa/button.mm @@ -164,6 +164,35 @@ wxSize wxButton::GetDefaultSize() @end +namespace +{ + +class wxButtonCocoaImpl : public wxWidgetCocoaImpl +{ +public: + wxButtonCocoaImpl(wxWindowMac *wxpeer, wxNSButton *v) + : wxWidgetCocoaImpl(wxpeer, v) + { + } + + virtual void SetBitmap(const wxBitmap& bitmap) + { + [GetNSButton() setBezelStyle:bitmap.IsOk() ? NSRegularSquareBezelStyle + : NSRoundedBezelStyle]; + + wxWidgetCocoaImpl::SetBitmap(bitmap); + } + +private: + NSButton *GetNSButton() const + { + wxASSERT( [m_osxView isKindOfClass:[NSButton class]] ); + + return static_cast(m_osxView); + } +}; + +} // anonymous namespace wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent), @@ -187,8 +216,7 @@ wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer, } [v setButtonType:NSMomentaryPushInButton]; - wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v ); - return c; + return new wxButtonCocoaImpl( wxpeer, v ); } void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault ) diff --git a/src/osx/cocoa/window.mm b/src/osx/cocoa/window.mm index 7b32645868..7eded58ffb 100644 --- a/src/osx/cocoa/window.mm +++ b/src/osx/cocoa/window.mm @@ -1387,6 +1387,19 @@ wxInt32 wxWidgetCocoaImpl::GetMaximum() const return 0; } +wxBitmap wxWidgetCocoaImpl::GetBitmap() const +{ + wxBitmap bmp; + + // TODO: how to create a wxBitmap from NSImage? +#if 0 + if ( [m_osxView respondsToSelector:@selector(image:)] ) + bmp = [m_osxView image]; +#endif + + return bmp; +} + void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap ) { if ( [m_osxView respondsToSelector:@selector(setImage:)] ) @@ -1395,6 +1408,38 @@ void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap ) } } +void wxWidgetCocoaImpl::SetBitmapPosition( wxDirection dir ) +{ + if ( [m_osxView respondsToSelector:@selector(setImagePosition:)] ) + { + NSCellImagePosition pos; + switch ( dir ) + { + case wxLEFT: + pos = NSImageLeft; + break; + + case wxRIGHT: + pos = NSImageRight; + break; + + case wxTOP: + pos = NSImageAbove; + break; + + case wxBOTTOM: + pos = NSImageBelow; + break; + + default: + wxFAIL_MSG( "invalid image position" ); + pos = NSNoImage; + } + + [m_osxView setImagePosition:pos]; + } +} + void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& WXUNUSED(notebook)) { // implementation in subclass -- 2.47.2