| 1 |
@implementation CocoaButton : NSButton |
| 2 |
|
| 3 |
-(id) initWith:(phoenix::Button&)buttonReference { |
| 4 |
if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) { |
| 5 |
button = &buttonReference; |
| 6 |
[self setTarget:self]; |
| 7 |
[self setAction:@selector(activate:)]; |
| 8 |
//NSRoundedBezelStyle has a fixed height; which breaks both icons and larger/smaller text |
| 9 |
[self setBezelStyle:NSRegularSquareBezelStyle]; |
| 10 |
} |
| 11 |
return self; |
| 12 |
} |
| 13 |
|
| 14 |
-(IBAction) activate:(id)sender { |
| 15 |
if(button->onActivate) button->onActivate(); |
| 16 |
} |
| 17 |
|
| 18 |
@end |
| 19 |
|
| 20 |
namespace phoenix { |
| 21 |
|
| 22 |
Size pButton::minimumSize() { |
| 23 |
Size size = Font::size(button.font(), button.state.text); |
| 24 |
|
| 25 |
if(button.state.orientation == Orientation::Horizontal) { |
| 26 |
size.width += button.state.image.width; |
| 27 |
size.height = max(button.state.image.height, size.height); |
| 28 |
} |
| 29 |
|
| 30 |
if(button.state.orientation == Orientation::Vertical) { |
| 31 |
size.width = max(button.state.image.width, size.width); |
| 32 |
size.height += button.state.image.height; |
| 33 |
} |
| 34 |
|
| 35 |
return {size.width + 20, size.height + 4}; |
| 36 |
} |
| 37 |
|
| 38 |
void pButton::setGeometry(Geometry geometry) { |
| 39 |
pWidget::setGeometry({ |
| 40 |
geometry.x - 2, geometry.y - 2, |
| 41 |
geometry.width + 4, geometry.height + 4 |
| 42 |
}); |
| 43 |
} |
| 44 |
|
| 45 |
void pButton::setImage(const image& image, Orientation orientation) { |
| 46 |
@autoreleasepool { |
| 47 |
if(image.empty()) { |
| 48 |
[cocoaView setImage:nil]; |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
[cocoaView setImage:NSMakeImage(image)]; |
| 53 |
|
| 54 |
if(orientation == Orientation::Horizontal) [cocoaView setImagePosition:NSImageLeft]; |
| 55 |
if(orientation == Orientation::Vertical ) [cocoaView setImagePosition:NSImageAbove]; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
void pButton::setText(string text) { |
| 60 |
@autoreleasepool { |
| 61 |
[cocoaView setTitle:[NSString stringWithUTF8String:text]]; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
void pButton::constructor() { |
| 66 |
@autoreleasepool { |
| 67 |
cocoaView = cocoaButton = [[CocoaButton alloc] initWith:button]; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
void pButton::destructor() { |
| 72 |
@autoreleasepool { |
| 73 |
[cocoaView release]; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
} |