Radio example with C++ Binding

In this example we will create 4 radios, and add them to the same 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 first part consists of including the headers. In this case we are only working with the Elementary C++ binding and thus we need only to include him.

#include <Elementary.hh>

Attention
If necessary the C and/or the C++ headers should be include here as well.

Now we need to actually start the code and set the elm_policy, which defines for a given policy group/identifier a new policy's value, respectively. In this example the only policy we need to set a value for is ELM_POLICY_QUIT, possibles values for it are:

EAPI_MAIN int
elm_main (int argc, char *argv[])
{
elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_HIDDEN);

As you can see, the policy we chose was to quit when the last win is hidden as opose to examples with the C bindings where we perpetually set it to quit when last win was closed. This changed was necessary because in C++ binding as the elm mainloop stop running all object are destroyed, references are unreferenced and events are stopped at ELM_MAIN().

See also
For more details consult elm_policy_set

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

static int val = 1;

Next step is creating an Elementary window, in this example we use the C++ binding method with the elm_win_util_standard_add that is a elm_win_legacy function, better explained below. And then we set the autohide state for it.

elm_win_util_standard_add (const char *name, const char *tittle) Adds a window object with standard setup.

Parameters:

This creates a window but also puts in a standard background with elm_bg_add(), as well as setting the window title to title. The window type created is of type ELM_WIN_BASIC, with the NULL as the parent widget. Returns the created object or NULL on failure.

The autohide works similarly to autodel, automatically handling "delete,request" signals when set to true, with the difference that it will hide the window, instead of destroying it.

It is specially designed to work together with ELM_POLICY_QUIT_LAST_WINDOW_HIDDEN which allows exiting Elementary's main loop when all the windows are hidden.

win.title_set("Radio");
win.autohide_set(true);

Note
autodel and autohide are not mutually exclusive. The window will be destructed if both autodel and autohide is set to EINA_TRUE or true.

A box arranges objects in a linear fashion, governed by a layout function that defines the details of this arrangement. The box will use an internal function to set the layout to a single row, vertical by default.

Now let's create the box with the C++ binding method, passing our window object as parent and then setting box's layout as horizontal.

::elm::box bx(efl::eo::parent = win);
bx.horizontal_set(true);

To better understand, the function size_hint_weight_set for C++ bindings originated from C bindings function evas_object_size_hint_weight_set, that is EFL Evas type function. With this function we set the hints for an object's weight. The parameters are:

This is not a size enforcement in any way, it's just a hint that should be used whenever appropriate. This is a hint on how a container object should resize a given child within its area.

Containers may adhere to the simpler logic of just expanding the child object's dimensions to fit its own (see the EVAS_HINT_EXPAND helper weight macro in the EFL Evas Documentation) or the complete one of taking each child's weight hint as real weights to how much of its size to allocate for them in each axis. A container is supposed to, after normalizing the weights of its children (with weight hints), distribute the space it has to layout them by those factors – most weighted children get larger in this process than the least ones.

bx.size_hint_weight_set(EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);

Note
Default weight hint values are 0.0, for both axis.

Now we add the box as a resize_object to win informing that when the size of the win changes so should the box's size. And finally we make it visible.

Radio is a widget that allows for one or more options to be displayed and have the user choose only one of them. It contains an indicator, an optional label and an optional icon object. While it's possible to have a group of only one radio they, are normally used in groups of 2 or more.

We will create the box with the C++ binding method, passing our window object as parent and then setting box's layout as horizontal.

And now we create a radio with the C++ binding method, passing our window object as parent. Since this is the first radio 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 text, then 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 standard home icon, the icon will be created with the same method and setting the icon as content of radio.

When using the elm::box the packing method of the subobj - radio in this case - should be defined. There are four possible methods:

In this and most examples we use pack_end by choice and practicality.

The function size_hint_weight_set works with radio the same way as with box, as above.

The function size_hint_align_set for C++ bindings originated from C bindings function evas_object_size_hint_align_set, that is EFL Evas type function. With this function we set the hints for an object's alignment. The parameters are:

These are hints on how to align an object inside the boundaries of a container/manager. Accepted values are in the 0.0 to 1.0 range, with the special value EVAS_HINT_FILL used to specify "justify" or "fill" by some users. In this case, maximum size hints should be enforced with higher priority, if they are set. Also, any padding hint set on objects should add up to the alignment space on the final scene composition.

For the horizontal component, 0.0 means to the left, 1.0 means to the right. Analogously, for the vertical component, 0.0 to the top, 1.0 means to the bottom.

This is not a size enforcement in any way, it's just a hint that should be used whenever appropriate.

Note
Default alignment hint values are 0.5, for both axis.

To end the settings of radio we'll make it visible and with our lambda type function we output the current value of val. In this example we are using std::bind to bind the parameters of our lambda function to return as std::function object to cb_val which was declare as auto. Now we just have to add cb_val as changed radio callback of our radio.

See also
To learn more consult Lambda Functions with Elementary - C++11.

The creation of our second radio is almost identical, using the same method we create radio2 passing win as parent. We also set the text, then state value of this radio to 2 and the value pointer to val. This radio will be added in the same group as the first radio.

Then we set the standard file icon, the icon will be created with the same method and then set the icon as content of radio.

As before, we set packing method of radio2 in the box, the weight, alignment and visibility of radio2. Then add cb_val as callback when the radio changes.

For our third and fourth radios we'll omit the icon and set the value to 3 and 4, respectively, we'll also add them to the group of the first radio:

Finally we just have to make our window visible and set it's size, then run the elm mainloop, starting to handle events and drawing operations.

The full code for this example can be found at radio_cxx_example_01.cc

The example will look like this:

radio_cxx_example_01.png