]> git.saurik.com Git - wxWidgets.git/blobdiff - src/osx/cocoa/button.mm
implement wxGTK wxBitmapButton in terms of wxButton
[wxWidgets.git] / src / osx / cocoa / button.mm
index 789b07d0863be42eb511ac99cfe3862327a0c852..9efc79667e75c0326f85764f91126737b88ea5f2 100644 (file)
@@ -26,7 +26,7 @@
 wxSize wxButton::DoGetBestSize() const
 {
     if ( GetId() == wxID_HELP )
-        return wxSize( 20 , 20 ) ;
+        return wxSize( 23 , 23 ) ;
 
     wxSize sz = GetDefaultSize() ;
 
@@ -34,7 +34,7 @@ wxSize wxButton::DoGetBestSize() const
     {
         case wxWINDOW_VARIANT_NORMAL:
         case wxWINDOW_VARIANT_LARGE:
-            sz.y = 20 ;
+            sz.y = 23 ;
             break;
 
         case wxWINDOW_VARIANT_SMALL:
@@ -49,6 +49,21 @@ wxSize wxButton::DoGetBestSize() const
             break;
     }
 
+    wxRect r ;
+        
+    m_peer->GetBestRect(&r);
+
+    if ( r.GetWidth() == 0 && r.GetHeight() == 0 )
+    {
+    }
+    sz.x = r.GetWidth();
+    sz.y = r.GetHeight();
+
+    int wBtn = 96;
+    
+    if ((wBtn > sz.x) || ( GetWindowStyle() & wxBU_EXACTFIT))
+        sz.x = wBtn;
+
 #if wxOSX_USE_CARBON
     Rect    bestsize = { 0 , 0 , 0 , 0 } ;
     m_peer->GetBestRect( &bestsize ) ;
@@ -108,40 +123,16 @@ wxSize wxButton::GetDefaultSize()
 
 @implementation wxNSButton
 
-- (id)initWithFrame:(NSRect)frame
-{
-    [super initWithFrame:frame];
-    m_impl = NULL;
-    [self setTarget: self];
-    [self setAction: @selector(clickedAction:)];
-    return self;
-}
-
-- (void) clickedAction: (id) sender
++ (void)initialize
 {
-    if ( m_impl )
+    static BOOL initialized = NO;
+    if (!initialized) 
     {
-        wxButton* wxpeer = (wxButton*) m_impl->GetWXPeer();
-        if ( wxpeer )
-            wxpeer->HandleClicked(0);
+        initialized = YES;
+        wxOSXCocoaClassAddWXMethods( self );
     }
 }
 
-- (void)setImplementation: (wxWidgetImpl *) theImplementation
-{
-    m_impl = theImplementation;
-}
-
-- (wxWidgetImpl*) implementation
-{
-    return m_impl;
-}
-
-- (BOOL) isFlipped
-{
-    return YES;
-}
-
 - (int) intValue
 {
     switch ( [self state] )
@@ -175,18 +166,15 @@ wxSize wxButton::GetDefaultSize()
 
 
 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer, 
-                                    wxWindowMac* parent
+                                    wxWindowMac* WXUNUSED(parent)
                                     wxWindowID id, 
-                                    const wxString& label,
+                                    const wxString& WXUNUSED(label),
                                     const wxPoint& pos, 
                                     const wxSize& size,
-                                    long style
-                                    long extraStyle
+                                    long WXUNUSED(style)
+                                    long WXUNUSED(extraStyle)
 {
-    NSView* sv = (wxpeer->GetParent()->GetHandle() );
-    
-    NSRect r = wxToNSRect( sv, wxRect( pos, size) );
-    // Rect bounds = wxMacGetBoundsForControl( wxpeer, pos , size ) ;
+    NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
     wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
     
     if ( id == wxID_HELP )
@@ -199,9 +187,7 @@ wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
     }
     
     [v setButtonType:NSMomentaryPushInButton];
-    [sv addSubview:v];
     wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
-    [v setImplementation:c];
     return c;
 /*
     OSStatus err;
@@ -256,12 +242,162 @@ wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
 
 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
 { 
-    [m_osxView setKeyEquivalent: isDefault ? @"\r" : nil ];
-//    SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) isDefault ) ;
+    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() 
 {
 }
 
-// TODO for the disclosure button : NSDisclosureBezelStyle and the button type to NSOnOffButton.
+//
+// wxDisclosureButton implementation
+//
+
+@interface wxDisclosureNSButton : NSButton
+{
+
+    BOOL isOpen;
+}
+
+- (void) updateImage;
+
+- (void) toggle;
+
++ (NSImage *)rotateImage: (NSImage *)image;
+
+@end
+
+static const char * disc_triangle_xpm[] = {
+"10 9 4 1",
+"   c None",
+".  c #737373",
+"+  c #989898",
+"-  c #c6c6c6",
+" .-       ",
+" ..+-     ",
+" ....+    ",
+" ......-  ",
+" .......- ",
+" ......-  ",
+" ....+    ",
+" ..+-     ",
+" .-       ",
+};
+
+@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
+{
+    static wxBitmap trianglebm(disc_triangle_xpm);
+    if ( downArray.get() == NULL )
+    {
+        downArray.reset( [wxDisclosureNSButton rotateImage:trianglebm.GetNSImage()] );
+    }
+    
+    if ( isOpen )
+        [self setImage:(NSImage*)downArray.get()];
+    else
+        [self setImage:trianglebm.GetNSImage()];
+}
+
++ (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, 
+                                    wxWindowMac* WXUNUSED(parent), 
+                                    wxWindowID WXUNUSED(id), 
+                                    const wxString& label,
+                                    const wxPoint& pos, 
+                                    const wxSize& size,
+                                    long WXUNUSED(style), 
+                                    long WXUNUSED(extraStyle)) 
+{
+    NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
+    wxDisclosureNSButton* v = [[wxDisclosureNSButton alloc] initWithFrame:r];
+    [v setTitle:wxCFStringRef( label).AsNSString()];
+    wxDisclosureTriangleCocoaImpl* c = new wxDisclosureTriangleCocoaImpl( wxpeer, v );
+    return c;
+}