Hoversel example with C++ Binding

In this example we'll create a hoversel with 3 items, one with a label but no icon and two with both a label and an icon. Every item that is clicked will be deleted, but everytime the hoversel is activated we will also add an item. In addition our first item will print all items when clicked and our third item will clear all items in the hoversel.

The first part consists of including the headers. We'll include Elementary.hh, Eina.hh and Evas.hh, that are C++ bindings that are needed in this example.

#include <Elementary.hh>
#include <Eina.hh>
#include <Evas.hh>

Before our main code we'll need the following callbacks:

static void _print_items(void *data, Evas_Object *obj, void *event_info);

static void _free(void *data, Evas_Object *obj, void *event_info);

Starting the main code and initializing Eina C++ Lybrary, always initiate Eina when included.

EAPI_MAIN int
elm_main (int argc, char *argv[])
{
efl::eina::eina_init eina_init;

Now let's 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:

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

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("Hoversel");
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.

Next we'll create a red evas::rectangle to use as the icon of our hoversel, for thus using the C++ method, setting the color and making it visible.

And now we create our hoversel and set some of it's properties. We set win as its parent, set it to be vertical and give it a label and content, that will work as icon:

Next we will add callbacks to be called for the first and third:

We also set a pair of callbacks to be called whenever any item is selected or when the hoversel is activated, for this we'll use Lambda type function, add_item is called when the hoversel is activated and adds an item to the hoversel. Note that since we allocate memory for the item we need to know when the item dies so we can free that memory.

See also
For more on Lambda check here

Finishing with hoversel we set its size, position and make it visible.

In our second hoversel we'll add a button and for this we need create it using C++ method, set a text, add a callback for when button is clicked. This callback is type Lambda, it will clear hoversel when clicked.

Concluding our button options, we will set the size, position and visibility.

Now we set the size for the window, making it visible in the end:

Finally we just have to start the elm mainloop, starting to handle events and drawing operations.

Our example will look like this:

hoversel_cxx_example_01.png