Table Example with C++ binding - Heterogeneous

For our second example we'll create a table with 4 rectangles in it. Since our rectangles are of different sizes our table won't be homogeneous.

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

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.

And we also set the autohide state for win, 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.

::elm::win win(elm_win_util_standard_add("table", "Table"));
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.

Now we construct the elm table and for this we use the C++ method below, passing windows as it's parent.

::elm::table table(efl::eo::parent = win);

We then add table as a resize_object to win informing that when the size of the win changes so should the table's size and make it visible. The last configuration for table is to set homogeneous as false.

win.resize_object_add(table);
table.visible_set(true);
table.homogeneous_set(false);

For each cell of this table we are going to create a unique evas::rectangle, each with diferent colors and sizes.

Let's see a snip of the code on how we constructed our rectangles and setted the colors.

evas::rectangle rect(efl::eo::parent = win);
rect.color_set( 255, 0, 0, 255);

evas::rectangle rect2(efl::eo::parent = win);
rect2.color_set( 0, 255, 0, 255);

evas::rectangle rect3(efl::eo::parent = win);
rect3.color_set( 0, 0, 255, 255);

evas::rectangle rect4(efl::eo::parent = win);
rect4.color_set( 255, 255, 0, 255);

For each rectangle we also setted the size_hint_min that hints for an object's minimum size. This is not a size enforcement in any way, it's just a hint that should be used whenever appropriate.

rect.size_hint_min_set( 100, 50);

rect2.size_hint_min_set(50, 100);

rect3.size_hint_min_set(50, 50);

rect4.size_hint_min_set(50, 50);

When using pack in our table we are adding a child to a packing location of the table. The parameters are:

pack (evas::object subobj, int column, int row, int colspan, int rowspan)

Note
All positioning inside the table is relative to rows and columns, so a value of 0 for column and row, means the top left cell of the table. And for example, value of 2 for colspan and rowspan indicates that the subobj will occuppy two column and two rows, thus occuppying 4 cells in total.

So for each rectangle we are setting a specific location and how many cells it's occupying, better seem below:

table.pack(rect, 0, 0, 2, 1);

table.pack(rect2, 0, 1, 1, 2);

table.pack(rect3, 1, 1, 1, 1);

table.pack(rect4, 1, 2, 1, 1);

Finally we just have to make our window visible. Then run the elm mainloop, starting to handle events and drawing operations.

Full code for this example: table_cxx_example_02.cc .

Our example will look like this:

table_cxx_example_02.png