2015-12-30 06:41:46 +00:00
|
|
|
#if defined(Hiro_RadioButton)
|
|
|
|
|
2013-03-15 13:11:33 +00:00
|
|
|
@implementation CocoaRadioButton : NSButton
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
-(id) initWith:(hiro::mRadioButton&)radioButtonReference {
|
2013-03-15 13:11:33 +00:00
|
|
|
if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) {
|
|
|
|
radioButton = &radioButtonReference;
|
|
|
|
|
|
|
|
[self setTarget:self];
|
|
|
|
[self setAction:@selector(activate:)];
|
2013-11-28 10:29:01 +00:00
|
|
|
[self setBezelStyle:NSRegularSquareBezelStyle];
|
|
|
|
[self setButtonType:NSOnOffButton];
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2013-03-21 12:59:01 +00:00
|
|
|
-(IBAction) activate:(id)sender {
|
2013-11-28 10:29:01 +00:00
|
|
|
bool wasChecked = radioButton->state.checked;
|
2013-03-15 13:11:33 +00:00
|
|
|
radioButton->setChecked();
|
2015-12-30 06:41:46 +00:00
|
|
|
if(!wasChecked) radioButton->doActivate();
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
namespace hiro {
|
|
|
|
|
|
|
|
auto pRadioButton::construct() -> void {
|
|
|
|
@autoreleasepool {
|
|
|
|
cocoaView = cocoaRadioButton = [[CocoaRadioButton alloc] initWith:self()];
|
|
|
|
pWidget::construct();
|
|
|
|
|
|
|
|
setBordered(state().bordered);
|
|
|
|
if(state().checked) setChecked();
|
2016-01-07 08:14:33 +00:00
|
|
|
setIcon(state().icon);
|
2015-12-30 06:41:46 +00:00
|
|
|
setOrientation(state().orientation);
|
|
|
|
setText(state().text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pRadioButton::destruct() -> void {
|
|
|
|
@autoreleasepool {
|
2016-01-07 08:14:33 +00:00
|
|
|
[cocoaView removeFromSuperview];
|
2015-12-30 06:41:46 +00:00
|
|
|
[cocoaView release];
|
|
|
|
}
|
|
|
|
}
|
2013-03-15 13:11:33 +00:00
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
auto pRadioButton::minimumSize() const -> Size {
|
|
|
|
Size size = pFont::size(self().font(true), state().text);
|
2013-11-28 10:29:01 +00:00
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
if(state().orientation == Orientation::Horizontal) {
|
2016-01-07 08:14:33 +00:00
|
|
|
size.setWidth(size.width() + state().icon.width());
|
|
|
|
size.setHeight(max(size.height(), state().icon.height()));
|
2013-11-28 10:29:01 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
if(state().orientation == Orientation::Vertical) {
|
2016-01-07 08:14:33 +00:00
|
|
|
size.setWidth(max(size.width(), state().icon.width()));
|
|
|
|
size.setHeight(size.height() + state().icon.height());
|
2013-11-28 10:29:01 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 08:14:33 +00:00
|
|
|
return {size.width() + (state().text ? 20 : 8), size.height() + 8};
|
2015-12-30 06:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto pRadioButton::setBordered(bool bordered) -> void {
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
auto pRadioButton::setChecked() -> void {
|
2013-03-15 13:11:33 +00:00
|
|
|
@autoreleasepool {
|
2015-12-30 06:41:46 +00:00
|
|
|
if(auto group = state().group) {
|
|
|
|
for(auto& weak : group->state.objects) {
|
|
|
|
if(auto object = weak.acquire()) {
|
|
|
|
if(auto self = object->self()) {
|
|
|
|
if(auto p = dynamic_cast<pRadioButton*>(self)) {
|
|
|
|
auto state = this == p ? NSOnState : NSOffState;
|
|
|
|
[p->cocoaView setState:state];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
auto pRadioButton::setGeometry(Geometry geometry) -> void {
|
2013-04-09 13:31:46 +00:00
|
|
|
pWidget::setGeometry({
|
2015-12-30 06:41:46 +00:00
|
|
|
geometry.x() - 2, geometry.y() - 2,
|
|
|
|
geometry.width() + 4, geometry.height() + 4
|
2013-04-09 13:31:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
auto pRadioButton::setGroup(sGroup group) -> void {
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 08:14:33 +00:00
|
|
|
auto pRadioButton::setIcon(const image& icon) -> void {
|
2013-11-28 10:29:01 +00:00
|
|
|
@autoreleasepool {
|
2016-01-07 08:14:33 +00:00
|
|
|
[cocoaView setImage:NSMakeImage(icon)];
|
2013-11-28 10:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
auto pRadioButton::setOrientation(Orientation orientation) -> void {
|
2013-03-15 13:11:33 +00:00
|
|
|
@autoreleasepool {
|
2015-12-30 06:41:46 +00:00
|
|
|
if(orientation == Orientation::Horizontal) [cocoaView setImagePosition:NSImageLeft];
|
|
|
|
if(orientation == Orientation::Vertical ) [cocoaView setImagePosition:NSImageAbove];
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
auto pRadioButton::setText(const string& text) -> void {
|
2013-03-15 13:11:33 +00:00
|
|
|
@autoreleasepool {
|
2015-12-30 06:41:46 +00:00
|
|
|
[cocoaView setTitle:[NSString stringWithUTF8String:text]];
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
#endif
|