Radio example

In this example we will create 4 radio buttons, three of them in a group and another one not in the group. We will also have the radios in the group change the value of a variable directly and have then print it when the value changes. The fourth button is in the example just to make clear that radios outside the group don't affect the group.

We'll start with the usual includes:

#include <Elementary.h>

And move right to declaring a static variable(the one whose value the radios will change):

static int val = 1;

We now need to have a window and all that good stuff to be able to place our radios in:

static void _cb(void *data, Evas_Object *obj, void *event_info);
EAPI_MAIN int
elm_main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
{
Evas_Object *win, *bx, *radio, *group, *ic;
elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
win = elm_win_util_standard_add("radio", "Radio");
elm_win_autodel_set(win, EINA_TRUE);
bx = elm_box_add(win);
elm_box_horizontal_set(bx, EINA_TRUE);
evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
elm_win_resize_object_add(win, bx);
evas_object_show(bx);
EVAS_HINT_EXPAND
#define EVAS_HINT_EXPAND
Use with evas_object_size_hint_weight_set(), evas_object_size_hint_weight_get(), evas_object_size_hin...
Definition: Evas_Common.h:297
EINA_TRUE
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition: eina_types.h:539
EINA_UNUSED
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339
elm_box_add
Evas_Object * elm_box_add(Evas_Object *parent)
Add a new box to the parent.
Definition: elm_box.c:363
elm_box_horizontal_set
void elm_box_horizontal_set(Elm_Box *obj, Eina_Bool horizontal)
Set the horizontal orientation.
Definition: elm_box_eo.legacy.c:27
elm_policy_set
Eina_Bool elm_policy_set(unsigned int policy, int value)
Set a new policy's value (for a given policy group/identifier).
Definition: elm_main.c:1380
ELM_POLICY_QUIT_LAST_WINDOW_CLOSED
@ ELM_POLICY_QUIT_LAST_WINDOW_CLOSED
quit when the application's last window is closed
Definition: elm_general.h:248
ELM_POLICY_QUIT
@ ELM_POLICY_QUIT
under which circumstances the application should quit automatically.
Definition: elm_general.h:227
elm_win_resize_object_add
void elm_win_resize_object_add(Eo *obj, Evas_Object *subobj)
Add subobj as a resize object of window obj.
Definition: efl_ui_win.c:8992
elm_win_autodel_set
void elm_win_autodel_set(Eo *obj, Eina_Bool autodel)
Set the window's autodel state.
Definition: efl_ui_win.c:6189
elm_win_util_standard_add
Evas_Object * elm_win_util_standard_add(const char *name, const char *title)
Adds a window object with standard setup.
Definition: efl_ui_win.c:9576
evas_object_show
EVAS_API void evas_object_show(Evas_Object *eo_obj)
Makes the given Evas object visible.
Definition: evas_object_main.c:1814
evas_object_size_hint_weight_set
EVAS_API void evas_object_size_hint_weight_set(Evas_Object *obj, double x, double y)
Sets the hints for an object's weight.
Definition: evas_object_main.c:2638
Evas_Object
Efl_Canvas_Object Evas_Object
An Evas Object handle.
Definition: Evas_Common.h:185

And now we create a radio button, since this is the first button in our group we set the group to be the radio(so we can set the other radios in the same group). We also set the state value of this radio to 1 and the value pointer to `val`, since val is `1` this has the additional effect of setting the radio value to `1`. For this radio we choose the default home icon:

group = radio = elm_radio_add(win);
elm_object_text_set(radio, "Radio 1");
elm_radio_state_value_set(radio, 1);
elm_radio_value_pointer_set(radio, &val);
ic = elm_icon_add(win);
elm_icon_standard_set(ic, "home");
elm_object_part_content_set(radio, "icon", ic);
elm_box_pack_end(bx, radio);
evas_object_size_hint_weight_set(radio, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(radio, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_show(radio);
EVAS_HINT_FILL
#define EVAS_HINT_FILL
Use with evas_object_size_hint_align_set(), evas_object_size_hint_align_get(), evas_object_size_hint_...
Definition: Evas_Common.h:298
elm_box_pack_end
void elm_box_pack_end(Elm_Box *obj, Efl_Canvas_Object *subobj)
Add an object at the end of the pack list.
Definition: elm_box_eo.legacy.c:57
elm_object_part_content_set
void elm_object_part_content_set(Evas_Object *obj, const char *part, Evas_Object *content)
Set the content on part of a given container widget.
Definition: elm_main.c:1562
elm_icon_add
Evas_Object * elm_icon_add(Evas_Object *parent)
Add a new icon object to the parent.
Definition: elm_icon.c:613
elm_icon_standard_set
Eina_Bool elm_icon_standard_set(Evas_Object *obj, const char *name)
Set the icon by icon standards names.
Definition: elm_icon.c:885
elm_radio_add
Evas_Object * elm_radio_add(Evas_Object *parent)
Add a new radio to the parent.
Definition: efl_ui_radio.c:401
elm_radio_state_value_set
void elm_radio_state_value_set(Efl_Ui_Radio *obj, int value)
Set the integer value that this radio object represents.
Definition: efl_ui_radio_eo.legacy.c:3
elm_radio_value_pointer_set
void elm_radio_value_pointer_set(Efl_Ui_Radio *obj, int *valuep)
Set a convenience pointer to an integer, which changes when radio group value changes.
Definition: efl_ui_radio.c:428
evas_object_size_hint_align_set
EVAS_API void evas_object_size_hint_align_set(Evas_Object *obj, double x, double y)
Sets the hints for an object's alignment.
Definition: evas_object_main.c:2650

To check that our radio buttons are working we'll add a callback to the "changed" signal of the radio:

evas_object_smart_callback_add(radio, "changed", _cb, NULL);
evas_object_smart_callback_add
EVAS_API void evas_object_smart_callback_add(Evas_Object *eo_obj, const char *event, Evas_Smart_Cb func, const void *data)
Add (register) a callback function to the smart event specified by event on the smart object obj.
Definition: evas_object_smart.c:1040

The creation of our second radio button is almost identical, the 2 differences worth noting are, the value of this radio 2 and that we add this radio to the group of the first radio:

radio = elm_radio_add(win);
elm_object_text_set(radio, "Radio 2");
elm_radio_state_value_set(radio, 2);
elm_radio_value_pointer_set(radio, &val);
elm_radio_group_add(radio, group);
ic = elm_icon_add(win);
elm_icon_standard_set(ic, "file");
elm_object_part_content_set(radio, "icon", ic);
elm_box_pack_end(bx, radio);
evas_object_size_hint_weight_set(radio, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(radio, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_show(radio);
evas_object_smart_callback_add(radio, "changed", _cb, NULL);
elm_radio_group_add
void elm_radio_group_add(Efl_Ui_Radio *obj, Efl_Ui_Radio *group)
Add this radio to a group of other radio objects.
Definition: efl_ui_radio.c:465

For our third callback we'll omit the icon and set the value to 3, we'll also add it to the group of the first radio:

radio = elm_radio_add(win);
elm_object_text_set(radio, "Radio 3");
elm_radio_state_value_set(radio, 3);
elm_radio_value_pointer_set(radio, &val);
elm_radio_group_add(radio, group);
elm_box_pack_end(bx, radio);
evas_object_size_hint_weight_set(radio, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(radio, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_show(radio);
evas_object_smart_callback_add(radio, "changed", _cb, NULL);

Our fourth callback has a value of 4, no icon and most relevantly is not a member of the same group as the other radios:

radio = elm_radio_add(win);
elm_object_text_set(radio, "Radio 4");
elm_radio_state_value_set(radio, 4);
elm_radio_value_pointer_set(radio, &val);
elm_radio_group_add(radio, group);
elm_box_pack_end(bx, radio);
evas_object_size_hint_weight_set(radio, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(radio, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_show(radio);

We finally run the main loop:

evas_object_smart_callback_add(radio, "changed", _cb, NULL);
evas_object_show(win);
elm_run();
return 0;
}
ELM_MAIN()
ELM_MAIN
#define ELM_MAIN()
macro to be used after the elm_main() function
Definition: elm_general.h:556
elm_run
void elm_run(void)
Run Elementary's main loop.
Definition: elm_main.c:1357

And the last detail in our example is the callback that prints `val` so that we can see that the radios are indeed changing its value:

static void
_cb(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
printf("val is now: %d\n", val);
}

The example will look like this: