Calendar - Signal callback and getters with C++ binding.

Most of setters explained on previous examples have associated getters. That's the subject of this example. We'll add a callback to display all calendar information every time user interacts with the calendar. To be more didatical we'll start with the basics.

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>

If necessary the C and/or the C++ headers should be included 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: function to make this tutorial more didactical.

elm_main (int argc, char *argv[])

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("Calendar Getters Example");

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 let's create the calendar with the C++ binding method, passing our window object as parent.

::elm::calendar cal(efl::eo::parent = win);

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.

cal.size_hint_weight_set(EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);

Default weight hint values are 0.0, for both axis.

Now we add the calendar as a resize-object to win informing that when the size of the win changes so should the calendar's size.

Let's check our callback function, type lambda:

auto print_cal_info = std::bind([] (::elm::calendar obj)
int year_min, year_max;
bool sel_enabled;
const char **wds;
struct tm sel_time;
double interval;

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

To get selected day, we need to call selected_time_get(), but to assure nothing wrong happened, we must check for function return. It'll return EINA_FALSE if fail. Otherwise we can use time set to our structure stime.

if (!obj.selected_time_get(&sel_time))

Next we'll get information from calendar and place on declared vars:

interval = obj.interval_get();
obj.min_max_year_get(&year_min, &year_max);
sel_enabled = (obj.select_mode_get() != ELM_CALENDAR_SELECT_MODE_NONE);
wds = obj.weekdays_names_get();

The only tricky part is that last line gets an array of strings (char arrays), one for each weekday.

Then we can simple print that with std::cout and finish the lambda function:

std::cout << "Day: " << sel_time.tm_mday << ", Mon: " << sel_time.tm_mon << ", Year " <<
sel_time.tm_year + 1900 << ", WeekDay: " << sel_time.tm_wday << "<br>" << std::endl;
std::cout << "Interval: " << interval << ", Year_Min: " << year_min << ", Year_Max " <<
year_max << ", Sel Enabled: " << sel_enabled << "<br>" << std::endl;
std::cout << "Weekdays: " << wds[0] << ", " << wds[1] << ", " << wds[2] << ", " << wds[3] <<
", " << wds[4] << ", " << wds[5] << ", " << wds[6] << "<br>" << std::endl << std::endl;
} , std::placeholders::_1 );

struct tm is declared on time.h. You can check ctime manpage to read about it.

To register this callback, that will be called every time user selects a day or goes to next or previous month, just add a callback for signal changed.


Finally we just have to make calendar and window visibles and then start the elm mainloop, starting to handle events and drawing operations.

Our example will look like this:


See the full source code here.