Debian Codegraph @

hiro/cocoa/widget/radio-button.cpp

raw
1 #if defined(Hiro_RadioButton)
2
3 @implementation CocoaRadioButton
4
5 -(id) initWith:(hiro::mRadioButton&)radioButtonReference {
6 if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) {
7 radioButton = &radioButtonReference;
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 bool wasChecked = radioButton->state.checked;
19 radioButton->setChecked();
20 if(!wasChecked) radioButton->doActivate();
21 }
22
23 @end
24
25 namespace hiro {
26
27 auto pRadioButton::construct() -> void {
28 cocoaView = cocoaRadioButton = [[CocoaRadioButton alloc] initWith:self()];
29 pWidget::construct();
30
31 setBordered(state().bordered);
32 if(state().checked) setChecked();
33 setIcon(state().icon);
34 setOrientation(state().orientation);
35 setText(state().text);
36 }
37
38 auto pRadioButton::destruct() -> void {
39 [cocoaView removeFromSuperview];
40 }
41
42 auto pRadioButton::minimumSize() const -> Size {
43 Size size = pFont::size(self().font(true), state().text);
44
45 if(state().orientation == Orientation::Horizontal) {
46 size.setWidth(size.width() + state().icon.width());
47 size.setHeight(max(size.height(), state().icon.height()));
48 }
49
50 if(state().orientation == Orientation::Vertical) {
51 size.setWidth(max(size.width(), state().icon.width()));
52 size.setHeight(size.height() + state().icon.height());
53 }
54
55 return {size.width() + (state().text ? 20 : 8), size.height() + 8};
56 }
57
58 auto pRadioButton::setBordered(bool bordered) -> void {
59 }
60
61 auto pRadioButton::setChecked() -> void {
62 if(auto group = state().group) {
63 for(auto& weak : group->state.objects) {
64 if(auto object = weak.lock()) {
65 if(auto self = object->self()) {
66 if(auto p = dynamic_cast<pRadioButton*>(self)) {
67 auto state = this == p ? NSControlStateValueOn : NSControlStateValueOff;
68 [(CocoaRadioButton*)p->cocoaView setState:state];
69 }
70 }
71 }
72 }
73 }
74 }
75
76 auto pRadioButton::setGeometry(Geometry geometry) -> void {
77 pWidget::setGeometry({
78 geometry.x() - 2, geometry.y() - 2,
79 geometry.width() + 4, geometry.height() + 4
80 });
81 }
82
83 auto pRadioButton::setGroup(sGroup group) -> void {
84 }
85
86 auto pRadioButton::setIcon(const multiFactorImage& icon) -> void {
87 [(CocoaRadioButton*)cocoaView setImage:NSMakeImage(icon)];
88 }
89
90 auto pRadioButton::setOrientation(Orientation orientation) -> void {
91 if(orientation == Orientation::Horizontal) [(CocoaRadioButton*)cocoaView setImagePosition:NSImageLeft];
92 if(orientation == Orientation::Vertical ) [(CocoaRadioButton*)cocoaView setImagePosition:NSImageAbove];
93 }
94
95 auto pRadioButton::setText(const string& text) -> void {
96 [(CocoaRadioButton*)cocoaView setTitle:[NSString stringWithUTF8String:text]];
97 }
98
99 }
100
101 #endif