]> git.saurik.com Git - wxWidgets.git/blobdiff - src/osx/cocoa/button.mm
implement support for per-state bitmaps in wxMSW wxButton
[wxWidgets.git] / src / osx / cocoa / button.mm
index ee7070142cc58d4c41ed3dcdb3d30513d7e2c719..54becf5d1a2beb4e2d7a6c9e786a0ed10084a457 100644 (file)
@@ -26,7 +26,7 @@
 wxSize wxButton::DoGetBestSize() const
 {
     if ( GetId() == wxID_HELP )
 wxSize wxButton::DoGetBestSize() const
 {
     if ( GetId() == wxID_HELP )
-        return wxSize( 20 , 20 ) ;
+        return wxSize( 23 , 23 ) ;
 
     wxSize sz = GetDefaultSize() ;
 
 
     wxSize sz = GetDefaultSize() ;
 
@@ -123,24 +123,13 @@ wxSize wxButton::GetDefaultSize()
 
 @implementation wxNSButton
 
 
 @implementation wxNSButton
 
-- (id)initWithFrame:(NSRect)frame
++ (void)initialize
 {
 {
-    [super initWithFrame:frame];
-    impl = NULL;
-    [self setTarget: self];
-    [self setAction: @selector(clickedAction:)];
-    return self;
-}
-
-WXCOCOAIMPL_COMMON_IMPLEMENTATION
-
-- (void) clickedAction: (id) sender
-{
-    if ( impl )
+    static BOOL initialized = NO;
+    if (!initialized) 
     {
     {
-        wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
-        if ( wxpeer )
-            wxpeer->OSXHandleClicked(0);
+        initialized = YES;
+        wxOSXCocoaClassAddWXMethods( self );
     }
 }
 
     }
 }
 
@@ -177,13 +166,13 @@ WXCOCOAIMPL_COMMON_IMPLEMENTATION
 
 
 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer, 
 
 
 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer, 
-                                    wxWindowMac* parent
+                                    wxWindowMac* WXUNUSED(parent)
                                     wxWindowID id, 
                                     wxWindowID id, 
-                                    const wxString& label,
+                                    const wxString& WXUNUSED(label),
                                     const wxPoint& pos, 
                                     const wxSize& size,
                                     const wxPoint& pos, 
                                     const wxSize& size,
-                                    long style
-                                    long extraStyle
+                                    long WXUNUSED(style)
+                                    long WXUNUSED(extraStyle)
 {
     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
     wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
 {
     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
     wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
@@ -199,7 +188,6 @@ wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
     
     [v setButtonType:NSMomentaryPushInButton];
     wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
     
     [v setButtonType:NSMomentaryPushInButton];
     wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
-    [v setImplementation:c];
     return c;
 /*
     OSStatus err;
     return c;
 /*
     OSStatus err;
@@ -254,30 +242,144 @@ wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
 
 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
 { 
 
 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
 { 
-    if ( [m_osxView isKindOfClass:[NSButton class]] )
-        [(NSButton*)m_osxView setKeyEquivalent: isDefault ? @"\r" : nil ];
+    if ( isDefault && [m_osxView isKindOfClass:[NSButton class]] )
+        // NOTE: setKeyEquivalent: nil will trigger an assert
+        // instead do not call in that case.
+        [(NSButton*)m_osxView setKeyEquivalent: @"\r" ];
 }
 
 void wxWidgetCocoaImpl::PerformClick() 
 {
 }
 
 }
 
 void wxWidgetCocoaImpl::PerformClick() 
 {
 }
 
+//
+// wxDisclosureButton implementation
+//
+
+@interface wxDisclosureNSButton : NSButton
+{
+
+    BOOL isOpen;
+}
+
+- (void) updateImage;
+
+- (void) toggle;
+
++ (NSImage *)rotateImage: (NSImage *)image;
+
+@end
+
+@implementation wxDisclosureNSButton
+
++ (void)initialize
+{
+    static BOOL initialized = NO;
+    if (!initialized) 
+    {
+        initialized = YES;
+        wxOSXCocoaClassAddWXMethods( self );
+    }
+}
+
+- (id) initWithFrame:(NSRect) frame
+{
+    self = [super initWithFrame:frame];
+    [self setBezelStyle:NSSmallSquareBezelStyle];
+    isOpen = NO;
+    [self setImagePosition:NSImageLeft];
+    [self updateImage];
+    return self;
+}
+
+- (int) intValue
+{
+    return isOpen ? 1 : 0;
+}
+
+- (void) setIntValue: (int) v
+{
+    isOpen = ( v != 0 );
+    [self updateImage];
+}
+
+- (void) toggle
+{
+    isOpen = !isOpen;
+    [self updateImage];
+}
+
+wxCFRef<NSImage*> downArray ;
+
+- (void) updateImage
+{
+    if ( downArray.get() == NULL )
+    {
+        downArray.reset( [wxDisclosureNSButton rotateImage:[NSImage imageNamed:NSImageNameRightFacingTriangleTemplate]] );
+    }
+    
+    if ( isOpen )
+        [self setImage:(NSImage*)downArray.get()];
+    else
+        [self setImage:[NSImage imageNamed:NSImageNameRightFacingTriangleTemplate]];
+}
+
++ (NSImage *)rotateImage: (NSImage *)image
+{
+    NSSize imageSize = [image size];
+    NSSize newImageSize = NSMakeSize(imageSize.height, imageSize.width);
+    NSImage* newImage = [[NSImage alloc] initWithSize: newImageSize];
+    [newImage lockFocus];
+    
+    NSAffineTransform* tm = [NSAffineTransform transform];
+    [tm translateXBy:newImageSize.width/2 yBy:newImageSize.height/2];
+    [tm rotateByDegrees:-90];
+    [tm translateXBy:-newImageSize.width/2 yBy:-newImageSize.height/2];
+    [tm concat];
+    
+    
+    [image drawInRect:NSMakeRect(0,0,newImageSize.width, newImageSize.height)
+        fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
+    
+    [newImage unlockFocus];
+    return newImage;
+}
+
+@end
+
+class wxDisclosureTriangleCocoaImpl : public wxWidgetCocoaImpl
+{
+public :
+    wxDisclosureTriangleCocoaImpl(wxWindowMac* peer , WXWidget w) :
+        wxWidgetCocoaImpl(peer, w)
+    {
+    }
+    
+    ~wxDisclosureTriangleCocoaImpl()
+    {
+    }
+
+    virtual void controlAction(WXWidget slf, void* _cmd, void *sender)
+    {
+        wxDisclosureNSButton* db = (wxDisclosureNSButton*)m_osxView;
+        [db toggle];
+        wxWidgetCocoaImpl::controlAction(slf, _cmd, sender );
+    }
+};
+
 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer, 
 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer, 
-                                    wxWindowMac* parent
-                                    wxWindowID id
+                                    wxWindowMac* WXUNUSED(parent)
+                                    wxWindowID WXUNUSED(id)
                                     const wxString& label,
                                     const wxPoint& pos, 
                                     const wxSize& size,
                                     const wxString& label,
                                     const wxPoint& pos, 
                                     const wxSize& size,
-                                    long style
-                                    long extraStyle
+                                    long WXUNUSED(style)
+                                    long WXUNUSED(extraStyle)
 {
     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
 {
     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
-    wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
-    [v setBezelStyle:NSDisclosureBezelStyle];
-    [v setButtonType:NSOnOffButton];
+    wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
     [v setTitle:wxCFStringRef( label).AsNSString()];
     [v setTitle:wxCFStringRef( label).AsNSString()];
-    [v setImagePosition:NSImageRight];
-    wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
-    [v setImplementation:c];
+    wxDisclosureTriangleCocoaImpl* c = new wxDisclosureTriangleCocoaImpl( wxpeer, v );
     return c;
 }
     return c;
 }