| 1 |
#if defined(Hiro_RadioLabel) |
| 2 |
|
| 3 |
namespace hiro { |
| 4 |
|
| 5 |
static auto RadioLabel_activate(GtkToggleButton*, pRadioLabel* p) -> void { |
| 6 |
if(p->groupLocked()) return; |
| 7 |
bool wasChecked = p->state().checked; |
| 8 |
p->self().setChecked(); |
| 9 |
if(!wasChecked) p->self().doActivate(); |
| 10 |
} |
| 11 |
|
| 12 |
auto pRadioLabel::construct() -> void { |
| 13 |
gtkWidget = gtk_radio_button_new_with_label(nullptr, ""); |
| 14 |
gtkToggleButton = GTK_TOGGLE_BUTTON(gtkWidget); |
| 15 |
gtkRadioButton = GTK_RADIO_BUTTON(gtkWidget); |
| 16 |
|
| 17 |
setGroup(state().group); |
| 18 |
setText(state().text); |
| 19 |
|
| 20 |
g_signal_connect(G_OBJECT(gtkWidget), "toggled", G_CALLBACK(RadioLabel_activate), (gpointer)this); |
| 21 |
|
| 22 |
pWidget::construct(); |
| 23 |
} |
| 24 |
|
| 25 |
auto pRadioLabel::destruct() -> void { |
| 26 |
gtk_widget_destroy(gtkWidget); |
| 27 |
} |
| 28 |
|
| 29 |
auto pRadioLabel::minimumSize() const -> Size { |
| 30 |
Size size = pFont::size(self().font(true), state().text); |
| 31 |
return {size.width() + 28, size.height() + 4}; |
| 32 |
} |
| 33 |
|
| 34 |
auto pRadioLabel::setChecked() -> void { |
| 35 |
auto lock = acquire(); |
| 36 |
gtk_toggle_button_set_active(gtkToggleButton, true); |
| 37 |
} |
| 38 |
|
| 39 |
auto pRadioLabel::setGroup(sGroup group) -> void { |
| 40 |
maybe<GtkRadioButton*> gtkRadioButton; |
| 41 |
for(auto& weak : group->state.objects) { |
| 42 |
if(auto object = weak.lock()) { |
| 43 |
if(auto radioLabel = dynamic_cast<mRadioLabel*>(object.get())) { |
| 44 |
if(auto self = radioLabel->self()) { |
| 45 |
auto lock = self->acquire(); |
| 46 |
gtk_radio_button_set_group(self->gtkRadioButton, nullptr); |
| 47 |
if(!gtkRadioButton) { |
| 48 |
gtkRadioButton = self->gtkRadioButton; |
| 49 |
gtk_toggle_button_set_active(self->gtkToggleButton, radioLabel->state.checked = true); |
| 50 |
} else { |
| 51 |
gtk_radio_button_set_group(self->gtkRadioButton, gtk_radio_button_get_group(*gtkRadioButton)); |
| 52 |
gtk_toggle_button_set_active(self->gtkToggleButton, radioLabel->state.checked = false); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
auto pRadioLabel::setText(const string& text) -> void { |
| 61 |
gtk_button_set_label(GTK_BUTTON(gtkWidget), text); |
| 62 |
setFont(self().font(true)); //gtk_button_set_label() recreates label, which destroys currently assigned font |
| 63 |
} |
| 64 |
|
| 65 |
auto pRadioLabel::groupLocked() const -> bool { |
| 66 |
if(auto group = state().group) { |
| 67 |
for(auto& weak : group->state.objects) { |
| 68 |
if(auto object = weak.lock()) { |
| 69 |
if(auto self = object->self()) { |
| 70 |
if(self->locked()) return true; |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
return false; |
| 75 |
} |
| 76 |
return locked(); |
| 77 |
} |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
#endif |