Debian Codegraph @

phoenix/gtk/widget/radio-button.cpp

raw
1 namespace phoenix {
2
3 static void RadioButton_activate(GtkToggleButton* toggleButton, RadioButton* self) {
4 self->p.onActivate();
5 }
6
7 Size pRadioButton::minimumSize() {
8 Size size = pFont::size(widget.state.font, radioButton.state.text);
9
10 if(radioButton.state.orientation == Orientation::Horizontal) {
11 size.width += radioButton.state.image.width;
12 size.height = max(radioButton.state.image.height, size.height);
13 }
14
15 if(radioButton.state.orientation == Orientation::Vertical) {
16 size.width = max(radioButton.state.image.width, size.width);
17 size.height += radioButton.state.image.height;
18 }
19
20 return {size.width + 24, size.height + 12};
21 }
22
23 void pRadioButton::setChecked() {
24 parent().locked = true;
25 for(auto& item : radioButton.state.group) {
26 bool checked = &item == &radioButton;
27 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(item.p.gtkWidget), item.state.checked = checked);
28 }
29 parent().locked = false;
30 }
31
32 void pRadioButton::setGroup(const group<RadioButton>& group) {
33 parent().locked = true;
34 for(auto& item : radioButton.state.group) {
35 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(item.p.gtkWidget), item.state.checked);
36 }
37 parent().locked = false;
38 }
39
40 void pRadioButton::setImage(const image& image, Orientation orientation) {
41 if(image.empty() == false) {
42 GtkImage* gtkImage = CreateImage(image);
43 gtk_button_set_image(GTK_BUTTON(gtkWidget), (GtkWidget*)gtkImage);
44 } else {
45 gtk_button_set_image(GTK_BUTTON(gtkWidget), nullptr);
46 }
47 switch(orientation) {
48 case Orientation::Horizontal: gtk_button_set_image_position(GTK_BUTTON(gtkWidget), GTK_POS_LEFT); break;
49 case Orientation::Vertical: gtk_button_set_image_position(GTK_BUTTON(gtkWidget), GTK_POS_TOP); break;
50 }
51 }
52
53 void pRadioButton::setText(string text) {
54 gtk_button_set_label(GTK_BUTTON(gtkWidget), text);
55 setFont(widget.state.font);
56 }
57
58 void pRadioButton::constructor() {
59 gtkWidget = gtk_toggle_button_new();
60 g_signal_connect(G_OBJECT(gtkWidget), "toggled", G_CALLBACK(RadioButton_activate), (gpointer)&radioButton);
61
62 setGroup(radioButton.state.group);
63 setText(radioButton.state.text);
64 }
65
66 void pRadioButton::destructor() {
67 gtk_widget_destroy(gtkWidget);
68 }
69
70 void pRadioButton::orphan() {
71 destructor();
72 constructor();
73 }
74
75 void pRadioButton::onActivate() {
76 if(parent().locked) return;
77 bool wasChecked = radioButton.state.checked;
78 setChecked();
79 if(!wasChecked) {
80 if(radioButton.onActivate) radioButton.onActivate();
81 }
82 }
83
84 pRadioButton& pRadioButton::parent() {
85 if(radioButton.state.group.size()) return radioButton.state.group.first().p;
86 return *this;
87 }
88
89 }