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
Evas.hh, that are C++ bindings that are needed in this example.
Before our main code we'll need the following callbacks:
_print_items:callback for our first item which prints all items in the hoversel.
_free:callback that frees the allocated memory.
Starting the main code and initializing Eina C++ Lybrary, always initiate Eina when included.
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_QUIT_NONE:Never quit the application automatically;
ELM_POLICY_QUIT_LAST_WINDOW_CLOSED:quit when the application's last window is closed;
ELM_POLICY_QUIT_LAST_WINDOW_HIDDEN: quit when the application's last window is 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().
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:
name- The name of the window;
title- The title for the window.
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.
autodeland autohide are not mutually exclusive. The window will be destructed if both autodel and autohide is set to
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.
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: