| 1 |
#if defined(Hiro_CheckButton) |
| 2 |
|
| 3 |
@implementation CocoaCheckButton |
| 4 |
|
| 5 |
-(id) initWith:(hiro::mCheckButton&)checkButtonReference { |
| 6 |
if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) { |
| 7 |
checkButton = &checkButtonReference; |
| 8 |
|
| 9 |
[self setTarget:self]; |
| 10 |
[self setAction:@selector(activate:)]; |
| 11 |
[self setBezelStyle:NSBezelStyleRegularSquare]; |
| 12 |
[self setButtonType:NSButtonTypeOnOff]; |
| 13 |
} |
| 14 |
return self; |
| 15 |
} |
| 16 |
|
| 17 |
-(IBAction) activate:(id)sender { |
| 18 |
checkButton->state.checked = [self state] != NSControlStateValueOff; |
| 19 |
checkButton->doToggle(); |
| 20 |
} |
| 21 |
|
| 22 |
@end |
| 23 |
|
| 24 |
namespace hiro { |
| 25 |
|
| 26 |
auto pCheckButton::construct() -> void { |
| 27 |
cocoaView = cocoaCheckButton = [[CocoaCheckButton alloc] initWith:self()]; |
| 28 |
pWidget::construct(); |
| 29 |
|
| 30 |
setBordered(state().bordered); |
| 31 |
setChecked(state().checked); |
| 32 |
setIcon(state().icon); |
| 33 |
setOrientation(state().orientation); |
| 34 |
setText(state().text); |
| 35 |
} |
| 36 |
|
| 37 |
auto pCheckButton::destruct() -> void { |
| 38 |
[cocoaView removeFromSuperview]; |
| 39 |
} |
| 40 |
|
| 41 |
auto pCheckButton::minimumSize() const -> Size { |
| 42 |
Size size = pFont::size(self().font(true), state().text); |
| 43 |
|
| 44 |
if(state().orientation == Orientation::Horizontal) { |
| 45 |
size.setWidth(size.width() + state().icon.width()); |
| 46 |
size.setHeight(max(size.height(), state().icon.height())); |
| 47 |
} |
| 48 |
|
| 49 |
if(state().orientation == Orientation::Vertical) { |
| 50 |
size.setWidth(max(size.width(), state().icon.width())); |
| 51 |
size.setHeight(size.height() + state().icon.height()); |
| 52 |
} |
| 53 |
|
| 54 |
return {size.width() + (state().text ? 20 : 8), size.height() + 8}; |
| 55 |
} |
| 56 |
|
| 57 |
auto pCheckButton::setBordered(bool bordered) -> void { |
| 58 |
} |
| 59 |
|
| 60 |
auto pCheckButton::setChecked(bool checked) -> void { |
| 61 |
[(CocoaCheckButton*)cocoaView setState:checked ? NSControlStateValueOn : NSControlStateValueOff]; |
| 62 |
} |
| 63 |
|
| 64 |
auto pCheckButton::setGeometry(Geometry geometry) -> void { |
| 65 |
pWidget::setGeometry({ |
| 66 |
geometry.x() - 2, geometry.y() - 2, |
| 67 |
geometry.width() + 4, geometry.height() + 4 |
| 68 |
}); |
| 69 |
} |
| 70 |
|
| 71 |
auto pCheckButton::setIcon(const multiFactorImage& icon) -> void { |
| 72 |
[(CocoaCheckButton*)cocoaView setImage:NSMakeImage(icon)]; |
| 73 |
} |
| 74 |
|
| 75 |
auto pCheckButton::setOrientation(Orientation orientation) -> void { |
| 76 |
if(orientation == Orientation::Horizontal) [(CocoaCheckButton*)cocoaView setImagePosition:NSImageLeft]; |
| 77 |
if(orientation == Orientation::Vertical ) [(CocoaCheckButton*)cocoaView setImagePosition:NSImageAbove]; |
| 78 |
} |
| 79 |
|
| 80 |
auto pCheckButton::setText(const string& text) -> void { |
| 81 |
[(CocoaCheckButton*)cocoaView setTitle:[NSString stringWithUTF8String:text]]; |
| 82 |
} |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
#endif |