Menu Example with C++ Binding

This example shows how to create a menu with regular items, object items, submenus and how to delete items from a menu.

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>

Starting the main code and initializing Eina C++ Lybrary, always initiate Eina when included. We'll also initialize a couple of pointers.

EAPI_MAIN int
elm_main (int argc, char *argv[])
{
efl::eina::eina_init eina_init;
elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_HIDDEN);
win.title_set("Menu");
win.autohide_set(true);
evas::rectangle rect(efl::eo::parent = win);
win.resize_object_add(rect);
rect.size_hint_min_set(0, 0);
rect.color_set( 0, 0, 0, 0);
rect.visible_set(true);
::elm::widget_item no_parent(nullptr);
::elm::menu menu(efl::eo::parent = win);
menu.item_add(no_parent, nullptr, "first item", nullptr, nullptr);
menu_it = menu.item_add(no_parent, "mail-reply-all", "second item", nullptr, nullptr);

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:

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.

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 evas::rectangle to use as the icon of our menu for thus using the C++ method, adding our rect as a resize-object to win informing that when the size of the win changes so should the box's size.

We'll also set, for rect, the hint for it's minimum size, it's color and making it visible.

Creating the menu using the C++ method, setting it's parent and adding an item to this menu. We are going to add more items, but these icons are going to have a parent, which will put them in a sub-menu.

We'll add a button to a menu_item, where this button will delete the first item of our sub-menu when clicked, we'll do this using elm_object_item_content_set().

Now, for the callback that will be used in this button we're use lambda type function and then add as clicked callback to button.

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

We now add a separator and three more regular items:

We now add another item, however this time it won't go the sub-menu and it'll be disabled:

To make sure that our menu is shown whenever the window is clicked, we use the following callback, also lambda:

Finally. we just make menu visible, set a size for our window making it visible and then start the elm mainloop, starting to handle events and drawing operations.

Our example will look like this:

menu_cxx_example_01.png