]> git.saurik.com Git - wxWidgets.git/commitdiff
added support for bitmaps in wxButton to wxOSX/Cocoa
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 21 Jun 2009 03:01:53 +0000 (03:01 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 21 Jun 2009 03:01:53 +0000 (03:01 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61158 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/changes.txt
include/wx/osx/button.h
include/wx/osx/carbon/private.h
include/wx/osx/cocoa/private.h
include/wx/osx/core/private.h
interface/wx/button.h
src/osx/button_osx.cpp
src/osx/carbon/window.cpp
src/osx/cocoa/button.mm
src/osx/cocoa/window.mm

index 48baa8bd11aa33c6fc2313a38bc1aca1f61cae24..be69b64869e8e5ceccfdbee811e5e5452090c93c 100644 (file)
@@ -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Ă©).
index 8a1a8e06e6c8c97c1b5777866a320a3946b5debb..48a82144500bb2de19703bc7d0928bdf635ffdb3 100644 (file)
@@ -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_
index 7beee9fa2dff96fc251b239dac0fba2e69b42849..d8dc8223427cbfeb40f9924be904ac968b9868b2 100644 (file)
@@ -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 &notebook );
 
     void                GetBestRect( wxRect *r ) const;
index fb8f330b3db9bdab472ea32900895dd9f024b715..ec69ce31d02824ae2c5071ba8b94c2377b094ad6 100644 (file)
@@ -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 &notebook );
     void                GetBestRect( wxRect *r ) const;
     bool                IsEnabled() const;
index cc2193377b53a4e2b3dd9ba533a07aed660f74b6..41ffd3cbdb3e1f985efde3de72193b32a1667ced 100644 (file)
@@ -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 &notebook ) =0;
     virtual void        GetBestRect( wxRect *r ) const = 0;
     virtual bool        IsEnabled() const = 0;
index 764d87fca57b8b5187d840e0d7985e12573ad5ae..5669cfc7bc7615b82e9a80d1a31805a45bcb8353 100644 (file)
@@ -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
     @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
index a7c7cf869c969198d51651825a55dc56d0afe0b9..12b630c412bf7f39b5b058f3990d2e9c6c0e9478 100644 (file)
@@ -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();
index b3aab0c01498af4daffb70f6731ede54184cf0bf..6fdc1d132b497c9f44f904db631f833367faf5b4 100644 (file)
@@ -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
index 6907d26536b4791c3cb1bb5f08aab3f633ed75bf..7a3c485d01f42fb4f6cc1ee9a81753798cd02074 100644 (file)
@@ -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<NSButton *>(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 )
index 7b32645868754ebfcd303d4011a893410e9c9216..7eded58ffb057c763dc3d4c1ad009d2c426d5eb6 100644 (file)
@@ -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