In this simple example, we'll explain how to format the labels displaying month and year, and also set weekday names.
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.
Now we will jump to the actual code and later explain the function to make this tutorial more didactical.
We must 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
Now let's create the calendar with the C++ binding method, passing our window object as parent.
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.
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.
To format month and year labels, we need to create a callback function to create a string given the selected time, declared under a
struct tm .
struct tm , declared on
time.h, is a structure composed by nine integers:
tm_sec seconds [0,59]
tm_min minutes [0,59]
tm_hour hour [0,23]
tm_mday day of month [1,31]
tm_mon month of year [0,11]
tm_year years since 1900
tm_wday day of week [0,6] (Sunday = 0)
tm_yday day of year [0,365]
tm_isdst daylight savings flag
For our function
_format_month_year , only stuff that matters are
tm_year. But we don't need to access it directly, since there are nice functions to format date and time, as
We will get abbreviated month (b) and year (y) (check strftime manpage for more) in our example:
We need to alloc the string to be returned, and calendar widget will free it when it's not needed, what is done by
So let's register our callback to calendar object:
To set weekday names, we should declare them as an array of strings:
And then set them to calendar:
Finally we just have to make the calendar and window visible and then start the elm mainloop, starting to handle events and drawing operations.
Our example will look like this:
See the full source code calendar_cxx_example_02.cc here.