Debian Codegraph @

hiro/cocoa/widget/radio-label.cpp

raw
1 #if defined(Hiro_RadioLabel)
2
3 @implementation CocoaRadioLabel
4
5 -(id) initWith:(hiro::mRadioLabel&)radioLabelReference {
6 if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) {
7 radioLabel = &radioLabelReference;
8
9 [self setTarget:self];
10 [self setAction:@selector(activate:)];
11 [self setButtonType:NSButtonTypeRadio];
12 }
13 return self;
14 }
15
16 -(IBAction) activate:(id)sender {
17 radioLabel->setChecked();
18 radioLabel->doActivate();
19 }
20
21 @end
22
23 namespace hiro {
24
25 auto pRadioLabel::construct() -> void {
26 cocoaView = cocoaRadioLabel = [[CocoaRadioLabel alloc] initWith:self()];
27 pWidget::construct();
28
29 setGroup(state().group);
30 setText(state().text);
31 }
32
33 auto pRadioLabel::destruct() -> void {
34 [cocoaView removeFromSuperview];
35 }
36
37 auto pRadioLabel::minimumSize() const -> Size {
38 Size size = pFont::size(self().font(true), state().text);
39 return {size.width() + 22, size.height() + 2};
40 }
41
42 auto pRadioLabel::setChecked() -> void {
43 setGroup(state().group);
44 }
45
46 auto pRadioLabel::setGeometry(Geometry geometry) -> void {
47 pWidget::setGeometry({
48 geometry.x() - 1, geometry.y(),
49 geometry.width() + 2, geometry.height()
50 });
51 //buttonType:NSRadioButton does not set initial icon via programmatically calling setState:NSOnState.
52 //I can only get the icon to show as checked initially by setting the state on geometry resizes.
53 //adjusting the initWithFrame:NSMakeRect did not help.
54 if(state().checked) setChecked();
55 }
56
57 auto pRadioLabel::setGroup(sGroup group) -> void {
58 if(!group) return;
59 for(auto& weak : group->state.objects) {
60 if(auto object = weak.lock()) {
61 if(auto self = object->self()) {
62 if(auto p = dynamic_cast<pRadioLabel*>(self)) {
63 auto state = p->state().checked ? NSControlStateValueOn : NSControlStateValueOff;
64 [(CocoaRadioLabel*)p->cocoaView setState:state];
65 }
66 }
67 }
68 }
69 }
70
71 auto pRadioLabel::setText(const string& text) -> void {
72 [(CocoaRadioLabel*)cocoaView setTitle:[NSString stringWithUTF8String:text]];
73 }
74
75 }
76
77 #endif