| 1 |
#if defined(Hiro_RadioButton) |
| 2 |
|
| 3 |
namespace hiro { |
| 4 |
|
| 5 |
static auto RadioButton_activate(GtkToggleButton*, pRadioButton* 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 pRadioButton::construct() -> void { |
| 13 |
gtkWidget = gtk_toggle_button_new(); |
| 14 |
|
| 15 |
setBordered(state().bordered); |
| 16 |
setIcon(state().icon); |
| 17 |
setOrientation(state().orientation); |
| 18 |
setText(state().text); |
| 19 |
|
| 20 |
g_signal_connect(G_OBJECT(gtkWidget), "toggled", G_CALLBACK(RadioButton_activate), (gpointer)this); |
| 21 |
|
| 22 |
pWidget::construct(); |
| 23 |
} |
| 24 |
|
| 25 |
auto pRadioButton::destruct() -> void { |
| 26 |
gtk_widget_destroy(gtkWidget); |
| 27 |
} |
| 28 |
|
| 29 |
auto pRadioButton::minimumSize() const -> Size { |
| 30 |
Size size = pFont::size(self().font(true), state().text); |
| 31 |
|
| 32 |
if(state().orientation == Orientation::Horizontal) { |
| 33 |
size.setWidth(size.width() + state().icon.width()); |
| 34 |
size.setHeight(max(size.height(), state().icon.height())); |
| 35 |
} |
| 36 |
|
| 37 |
if(state().orientation == Orientation::Vertical) { |
| 38 |
size.setWidth(max(size.width(), state().icon.width())); |
| 39 |
size.setHeight(size.height() + state().icon.height()); |
| 40 |
} |
| 41 |
|
| 42 |
return {size.width() + 24, size.height() + 12}; |
| 43 |
} |
| 44 |
|
| 45 |
auto pRadioButton::setBordered(bool bordered) -> void { |
| 46 |
gtk_button_set_relief(GTK_BUTTON(gtkWidget), bordered ? GTK_RELIEF_NORMAL : GTK_RELIEF_NONE); |
| 47 |
} |
| 48 |
|
| 49 |
auto pRadioButton::setChecked() -> void { |
| 50 |
for(auto& weak : self().group()->state.objects) { |
| 51 |
if(auto object = weak.lock()) { |
| 52 |
if(auto radioButton = dynamic_cast<mRadioButton*>(object.get())) { |
| 53 |
if(auto self = radioButton->self()) { |
| 54 |
self->lock(); |
| 55 |
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(self->gtkWidget), radioButton->state.checked); |
| 56 |
self->unlock(); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
auto pRadioButton::setGroup(sGroup group) -> void { |
| 64 |
bool first = true; |
| 65 |
for(auto& weak : group->state.objects) { |
| 66 |
if(auto object = weak.lock()) { |
| 67 |
if(auto radioButton = dynamic_cast<mRadioButton*>(object.get())) { |
| 68 |
if(auto self = radioButton->self()) { |
| 69 |
self->lock(); |
| 70 |
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(self->gtkWidget), radioButton->state.checked = first); |
| 71 |
first = false; |
| 72 |
self->unlock(); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
auto pRadioButton::setIcon(const image& icon) -> void { |
| 80 |
if(icon) { |
| 81 |
GtkImage* gtkImage = CreateImage(icon); |
| 82 |
gtk_button_set_image(GTK_BUTTON(gtkWidget), (GtkWidget*)gtkImage); |
| 83 |
} else { |
| 84 |
gtk_button_set_image(GTK_BUTTON(gtkWidget), nullptr); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
auto pRadioButton::setOrientation(Orientation orientation) -> void { |
| 89 |
switch(orientation) { |
| 90 |
case Orientation::Horizontal: gtk_button_set_image_position(GTK_BUTTON(gtkWidget), GTK_POS_LEFT); break; |
| 91 |
case Orientation::Vertical: gtk_button_set_image_position(GTK_BUTTON(gtkWidget), GTK_POS_TOP); break; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
auto pRadioButton::setText(const string& text) -> void { |
| 96 |
gtk_button_set_label(GTK_BUTTON(gtkWidget), text); |
| 97 |
setFont(self().font(true)); //gtk_button_set_label() recreates label, which destroys currently assigned font |
| 98 |
} |
| 99 |
|
| 100 |
auto pRadioButton::groupLocked() const -> bool { |
| 101 |
if(auto group = state().group) { |
| 102 |
for(auto& weak : group->state.objects) { |
| 103 |
if(auto object = weak.lock()) { |
| 104 |
if(auto self = object->self()) { |
| 105 |
if(self->locked()) return true; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
return false; |
| 110 |
} |
| 111 |
return locked(); |
| 112 |
} |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
#endif |