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