From 17ad51edc74e86c6f914b0a5342d5603ac9d4344 Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Wed, 10 Jun 2009 04:06:05 +0000 Subject: [PATCH] changing disclosure triangle to allow for label git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@60956 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/osx/cocoa/button.mm | 123 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 118 insertions(+), 5 deletions(-) diff --git a/src/osx/cocoa/button.mm b/src/osx/cocoa/button.mm index 750218176b..54becf5d1a 100644 --- a/src/osx/cocoa/button.mm +++ b/src/osx/cocoa/button.mm @@ -252,6 +252,122 @@ 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 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, wxWindowMac* WXUNUSED(parent), wxWindowID WXUNUSED(id), @@ -262,11 +378,8 @@ wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer, long WXUNUSED(extraStyle)) { 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 setImagePosition:NSImageRight]; - wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v ); + wxDisclosureTriangleCocoaImpl* c = new wxDisclosureTriangleCocoaImpl( wxpeer, v ); return c; } -- 2.45.2