Prefs Example 02

This example shows how to create a simple prefs widget with Elementary, where some items properties are changed on each timer tick.

We'll create items on the .EPC file and after handle it on the .C file.

Creating items on EPC file

First we'll create prefs items on .EPC file that we'll use later on the .C file. Note that the code is similar to .EDC (edje) files.

collection
{
page
{
name: "main";
version: 1;
title: "Preferences Widget";
subtitle: "Example 02";
widget: "elm/vertical_frame";

In this part, we create a TEXTAREA item and, by default, it will become a multi-line text entry in the UI. Note that we use a regular expression to accept only characters and whitespaces in it.

items {
item {
name: "text";
type: TEXTAREA;
editable: 1;
textarea {
placeholder: "This is a editable text entry";
default: "This is DEFAULT!";
accept: "^[a-zA-Z ]*$";
}
}

Now we create a FLOAT type item, by default will become a spinner in UI, and default, min and max parameters are optional as well as in INT type.

item {
name: "floatsp";
type: FLOAT;
editable: 1;
label: "Floating...";
float {
default: 0.7;
min: 0;
max: 1;
}
}

Here we create a BOOL type item, by default will become a checkbox in UI.

item {
name: "checkb";
type: BOOL;
label: "Checkbox";
bool {
default: true;
}
}

Here we create two items, separator and save types, that we've already covered in Prefs Example 01

item {
name: "sep";
type: SEPARATOR;
}
item {
name: "save";
type: SAVE;
label: "Save";
}

In this part, we create a ACTION type item. when clicked, the action item will emit a signal to .C file and call a smart callback.

item {
name: "action";
type: ACTION;
label: "Action!";
}
}
}
}

Handling items on C File

Now we're handling the .C file and first we'll create a prefs widget.

prefs = elm_prefs_add(win);
evas_object_size_hint_weight_set(prefs, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);

In this part we add the action smart callback, that will be called when the action item be clicked.

evas_object_smart_callback_add(prefs, "action", _action_cb, notify);

Here we add a simple action item callback that sets a text to another item.

_action_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
Evas_Object *notify = data;
Elm_Prefs_Data *prefs_data;
Eina_Value value;
prefs_data = evas_object_data_get(notify, "prefs_data");
if (elm_prefs_data_value_get(prefs_data, "main:text", &type, &value))
{
eina_value_set(&value, "Action!");
elm_prefs_data_value_set(prefs_data, "main:text", type, &value);
}

Now we set the prefs to save its values back (on the user data file) automatically on every UI element changes.

elm_prefs_autosave_set(prefs, EINA_TRUE);

In this part we create the prefs data handle and set the .EPB file (.EPC compiled).

Elm_Prefs_Data *prefs_data;
prefs_data = elm_prefs_data_new("./prefs_example_02.cfg", NULL,
elm_prefs_file_set(prefs, "prefs_example_02.epb", NULL);
elm_prefs_data_set(prefs, prefs_data);

Here we just create a notify widget to appear when the items properties are changed.

label = elm_label_add(win);
elm_object_text_set(label, "Editable, Visible and Disable! Just Saying...");
evas_object_size_hint_weight_set(label, 0.0, 0.0);
evas_object_size_hint_align_set(label, 0.5, 0.5);
notify = elm_notify_add(win);
elm_notify_align_set(notify, 0.5, 1);
elm_notify_timeout_set(notify, 2);
elm_object_content_set(notify, label);
evas_object_size_hint_weight_set(notify, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);

Now we add a timer to change text editable, spinners visibility and checkbox enable/disable properties on each 5.0 seconds and show the notify.

evas_object_data_set(notify, "prefs", prefs);
evas_object_data_set(notify, "prefs_data", prefs_data);
evas_object_smart_callback_add(prefs, "action", _action_cb, notify);
evas_object_resize(win, 320, 320);
ecore_timer_add(5.0, _elm_prefs_items_change, notify);
_elm_prefs_items_change(void *data)
{
Evas_Object *prefs, *notify = data;
Elm_Prefs_Data *prefs_data;
Eina_Value value;
prefs = evas_object_data_get(notify, "prefs");
prefs_data = evas_object_data_get(notify, "prefs_data");
visible = !visible;
elm_prefs_item_visible_set(prefs, "main:floatsp", visible);
disabled = !disabled;
elm_prefs_item_disabled_set(prefs, "main:checkb", disabled);
editable = !editable;
elm_prefs_item_editable_set(prefs, "main:text", editable);
if (elm_prefs_data_value_get(prefs_data, "main:text", &type, &value))
{
eina_value_set(&value, editable ? "This is a editable text entry" :
"This is a non-editable text entry");
elm_prefs_data_value_set(prefs_data, "main:text", type, &value);
}
}

Here we finish the example. The full source code can be found on prefs_example_02.c and prefs_example_02.epc

Elm_Prefs_Item_Type
Elm_Prefs_Item_Type
Elm Prefs item types.
Definition: elm_prefs_data.h:50
evas_object_data_get
void * evas_object_data_get(const Evas_Object *obj, const char *key)
Return an attached data pointer on an Evas object by its given string key.
Definition: evas_data.c:12
elm_prefs_file_set
Eina_Bool elm_prefs_file_set(Eo *obj, const char *file, const char *page)
Set file and page to populate a given prefs widget's interface.
Definition: elm_prefs.c:1794
elm_label_add
EAPI Evas_Object * elm_label_add(Evas_Object *parent)
Add a new label to the parent.
Definition: elm_label.c:413
EINA_UNUSED
#define EINA_UNUSED
Definition: eina_types.h:321
EVAS_HINT_EXPAND
#define EVAS_HINT_EXPAND
Use with evas_object_size_hint_weight_set(), evas_object_size_hint_weight_get(), evas_object_size_hin...
Definition: Evas_Common.h:292
elm_prefs_data_value_get
EAPI Eina_Bool elm_prefs_data_value_get(const Elm_Prefs_Data *prefs_data, const char *path, Elm_Prefs_Item_Type *type, Eina_Value *value)
Get one value of a given prefs data handle (by key).
Definition: elm_prefs_data.c:748
ecore_timer_add
Ecore_Timer * ecore_timer_add(double in, Ecore_Task_Cb func, const void *data)
Creates a timer to call the given function in the given period of time.
Definition: ecore_timer.c:178
evas_object_smart_callback_add
void evas_object_smart_callback_add(Evas_Object *eo_obj, const char *event, Evas_Smart_Cb func, const void *data)
Add (register) a callback function to the smart event specified by event on the smart object obj.
Definition: evas_object_smart.c:980
Evas_Object
Efl_Canvas_Object Evas_Object
Definition: Evas_Common.h:180
elm_prefs_data_new
EAPI Elm_Prefs_Data * elm_prefs_data_new(const char *data_file, const char *key, Eet_File_Mode mode)
Create a new prefs data handle.
Definition: elm_prefs_data.c:329
ECORE_CALLBACK_RENEW
#define ECORE_CALLBACK_RENEW
Return value to keep a callback.
Definition: Ecore_Common.h:153
eina_value_set
static Eina_Bool eina_value_set(Eina_Value *value,...)
Sets the generic value.
elm_prefs_add
EAPI Evas_Object * elm_prefs_add(Evas_Object *parent)
Add a new prefs widget.
Definition: elm_prefs.c:478
elm_notify_add
EAPI Evas_Object * elm_notify_add(Evas_Object *parent)
Add a new notify to the parent.
Definition: elm_notify.c:478
Elm_Prefs_Data
struct _Elm_Prefs_Data Elm_Prefs_Data
Definition: elm_prefs_data.h:89
evas_object_show
void evas_object_show(Evas_Object *eo_obj)
Makes the given Evas object visible.
Definition: evas_object_main.c:1853
EINA_TRUE
#define EINA_TRUE
Definition: eina_types.h:508
evas_object_data_set
void evas_object_data_set(Evas_Object *eo_obj, const char *key, const void *data)
Set an attached data pointer to an object with a given string key.
Definition: evas_data.c:5
elm_prefs_data_value_set
EAPI Eina_Bool elm_prefs_data_value_set(Elm_Prefs_Data *prefs_data, const char *path, const Elm_Prefs_Item_Type type, const Eina_Value *value)
Set (or delete) one value of a given prefs data handle.
Definition: elm_prefs_data.c:660
_Eina_Value
Definition: eina_value.h:661
elm_win_resize_object_add
void elm_win_resize_object_add(Eo *obj, Evas_Object *subobj)
Add subobj as a resize object of window obj.
Definition: efl_ui_win.c:8899
EET_FILE_MODE_READ_WRITE
File is for both read and write.
Definition: Eet.h:477