In this example, we illustrate how to create and handle Evas smart objects.
A smart object is one that provides custom functions to handle clipping, hiding, moving, resizing, color setting and more on child elements, automatically, for the smart object's user. They could be as simple as a group of objects that move together (see Clipped Smart Object) or implementations of whole complex UI widgets, providing some intelligence (thus the name) and extension to simple Evas objects.
Here, we create one as an example. What it does is to control (at maximum) 2 child objects, with regard to their geometries and colors. There can be a "left" child and a "right" one. The former will always occupy the top left quadrant of the smart object's area, while the latter will occupy the bottom right. The smart object will also contain an internal decorative border object, which will also be controlled by it, naturally.
Here is where we add it to the canvas:
d.smt = evas_smart_example_add(d.evas);
evas_object_move(d.smt, WIDTH / 4, HEIGHT / 4);
evas_object_resize(d.smt, WIDTH / 2, HEIGHT / 2);
The magic starts to happen in the evas_smart_example_add()
function, which is one in the example smart object's defined interface. These should be the functions you would export to the users of your smart object. We made three for this one:
evas_smart_example_add()
: add a new instance of the example smart object to a canvas
evas_smart_example_remove()
: remove a given child of the smart object from it
evas_smart_example_set_left()
: set the left child of the smart object
evas_smart_example_set_right()
: set the right child of the smart object
The object's creation takes place as:
evas_smart_example_add(
Evas *evas)
{
}
Smart objects are defined by smart classes, which are structs defining their interfaces, or smart functions (see Evas_Smart_Class, the base class for any smart object). As you see, one has to use the evas_object_smart_add() function to instantiate smart objects. Its second parameter is what matters – an Evas_Smart struct, which contains all the smart class definitions (smart functions, smart callbacks, and the like). Note, however, that _evas_smart_example_smart_class_new()
seems not to be defined in our example's code. That's because it came from a very handy helper macro:
What it does is to subclass a given existing smart class, thus specializing it. This is very common and useful in Evas. There is a built-in smart object, the "clipped smart object", which implements a behavior mostly desired by many other smart object implementors: it will clip its children to its area and move them along with it, on evas_object_move() calls. Then, our example smart object will get that behavior for free.
The first argument to the macro,
#define _evas_smart_example_type "Evas_Smart_Example"
will define the new smart class' name. The second tells the macro what is the prefix of the function it will be declaring with a _smart_set_user()
suffix. On this function, we may override/extend any desired method from our parent smart class:
static void
{
sc->
add = _evas_smart_example_smart_add;
sc->
del = _evas_smart_example_smart_del;
sc->
resize = _evas_smart_example_smart_resize;
sc->
calculate = _evas_smart_example_smart_calculate;
}
The first function pointer's code will take place at an example smart object's creation time:
static void
{
_evas_smart_example_parent_sc->add(o);
priv->border = evas_object_image_filled_add(evas_object_evas_get(o));
evas_object_image_file_set(priv->border, border_img_path, NULL);
evas_object_image_border_set(priv->border, 3, 3, 3, 3);
evas_object_image_border_center_fill_set(
priv->border, EVAS_BORDER_FILL_NONE);
}
The EVAS_SMART_DATA_ALLOC macro will take care of allocating our smart object data, which will be available on other contexts for us (mainly in our interface functions):
typedef struct _Evas_Smart_Example_Data Evas_Smart_Example_Data;
struct _Evas_Smart_Example_Data
{
int child_count;
};
See that, as we're inheriting from the clipped smart object's class, we must have their data struct as our first member. Other data of interest for us is a child members array and the border object's handle. The latter is what is created in the last mentioned function. Note how to tell Evas the border will be managed by our smart object from that time on: evas_object_smart_member_add(priv->border, o);
. The counterpart of this function is exemplified on the smart object's interface function to remove children:
{
long idx;
EVAS_SMART_EXAMPLE_DATA_GET_OR_RETURN_VAL(o, priv, NULL);
if (priv->children[0] != child && priv->children[1] != child)
{
fprintf(stderr, "You are trying to remove something not belonging to"
" the example smart object!\n");
return NULL;
}
idx--;
_evas_smart_example_remove_do(priv, child, idx);
o, EVT_CHILDREN_NUMBER_CHANGED, (void *)(uintptr_t)priv->child_count);
return child;
}
At the end of that function we make use of an constant defined by the EVAS_SMART_SUBCLASS_NEW: _evas_smart_example_parent_sc
. It has the same prefix we passed to the macro, as you can see, and it holds a pointer to our parent smart class. Then, we can call the specialized method, itself, after our code. The del
, hide
, show
and resize
specializations are straightforward, we let the reader take a look at them below to check their behavior. What's interesting is the calculate
one:
static void
{
EVAS_SMART_EXAMPLE_DATA_GET_OR_RETURN(o, priv);
evas_object_resize(priv->border, w, h);
evas_object_move(priv->border, x, y);
if (priv->children[0])
{
evas_object_move(priv->children[0], x + 3, y + 3);
evas_object_resize(priv->children[0], (w / 2) - 3, (h / 2) - 3);
}
if (priv->children[1])
{
evas_object_move(priv->children[1], x + (w / 2), y + (h / 2));
evas_object_resize(priv->children[1], (w / 2) - 3, (h / 2) - 3);
}
}
This code will take place whenever the smart object itself is flagged "dirty", i.e., must be recalculated for rendering (that could come from changes on its clipper, resizing, moving, etc). There, we make sure the decorative border lies on the edges of the smart object and the children, if any, lie on their respective quadrants.
After instantiating our smart object, we do some checks to exemplify some of the API on smart objects:
printf("Adding smart object of type \"%s\" to the canvas: %s.\n",
_evas_smart_example_type, ret ? "success" : "failure");
printf("Checking if clipped smart object's clipper is a "
"\"static\" one: %s\n",
evas_object_static_clip_get(d.clipper) ? "yes" : "no");
The evas_object_smart_type_check() one will assure we have the string naming our smart class really set to the live object. The evas_object_smart_clipped_clipper_get() exemplifies usage of "static clippers" – clipped smart objects have their global clippers flagged static.
Other important things we also exemplify here are smart callbacks and smart callback introspection:
#define EVT_CHILDREN_NUMBER_CHANGED "children,changed"
{
{EVT_CHILDREN_NUMBER_CHANGED, "i"},
{NULL, NULL}
};
Here we declare our array of smart callback descriptions, which has one element only, in this case. That callback will take place, as the name indicates, whenever the number of member objects in our smart object example instance changes. That global array variable must be the last argument to EVAS_SMART_SUBCLASS_NEW, so that it's registered as the smart class's callbacks description.
After we instantiate the smart object, we take a look on those descriptions and register a callback on that unique smart event:
for (; *descriptions; descriptions++)
{
printf("We've found a smart callback on the smart object!"
"\n\tname: %s\n\ttype: %s\n", (*descriptions)->name,
(*descriptions)->type);
if (strcmp((*descriptions)->type, "i")) continue;
d.smt, (*descriptions)->name,
_on_example_smart_object_child_num_change, NULL);
}
The code of the callback will just print how many member objects we have, which is an integer argument of the callback itself, as flagged by its description:
_on_example_smart_object_child_num_change(
void *data
EINA_UNUSED,
void *event_info)
{
printf("Number of child members on our example smart"
" object changed to %llu\n", (unsigned long long)(uintptr_t)event_info);
}
One of the points at which we issue that callback is inside the evas_smart_example_remove()
, code that was already shown.
As in other examples, to interact with this one there's a command line interface. A help string can be asked for with the 'h' key:
static const char *commands = \
"commands are:\n"
"\tl - insert child rectangle on the left\n"
"\tr - insert child rectangle on the right\n"
"\tw - remove and delete all members from the smart object\n"
"\tright arrow - move smart object to the right\n"
"\tleft arrow - move smart object to the left\n"
"\tup arrow - move smart object up\n"
"\tdown arrow - move smart object down\n"
"\td - decrease smart object's size\n"
"\ti - increase smart object's size\n"
"\tc - change smart object's clipper color\n"
"\t. - rotate object to the right\n"
"\t, - rotate object to the left\n"
"\th - print help\n"
"\tq - quit\n"
;
Use 'l' and 'r' keys, to create new rectangles and place them on the left (evas_smart_example_set_left()
) or right (evas_smart_example_set_right()
) spots of our smart object, respectively. The 'w' command will remove all member objects from the smart object and delete them. The keyboard arrows will move the smart object along the canvas. See how it takes any child objects with it during its movement. The 'd' and 'i' keys will increase or decrease the smart object's size – see how it affects the children's sizes, too. Finally, 'c' will change the color of the smart object's clipper (which is the exact internal clipper coming from a clipped smart object):
printf("Checking if clipped smart object's clipper is a "
"\"static\" one: %s\n",
evas_object_static_clip_get(d.clipper) ? "yes" : "no");
d.clipper, clipper_colors[cur_color].r, clipper_colors[cur_color].g,
clipper_colors[cur_color].b, clipper_colors[cur_color].a);
"Real life" examples of smart objects are Edje and Emotion objects: they both have independent libraries implementing their behavior. The full example follows.
#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define PACKAGE_EXAMPLES_DIR "."
#endif
#include <Ecore.h>
#include <stdio.h>
#include <errno.h>
#include "evas-common.h"
#define WIDTH (320)
#define HEIGHT (240)
static const char *commands = \
"commands are:\n"
"\tl - insert child rectangle on the left\n"
"\tr - insert child rectangle on the right\n"
"\tw - remove and delete all members from the smart object\n"
"\tright arrow - move smart object to the right\n"
"\tleft arrow - move smart object to the left\n"
"\tup arrow - move smart object up\n"
"\tdown arrow - move smart object down\n"
"\td - decrease smart object's size\n"
"\ti - increase smart object's size\n"
"\tc - change smart object's clipper color\n"
"\t. - rotate object to the right\n"
"\t, - rotate object to the left\n"
"\th - print help\n"
"\tq - quit\n"
;
#define WHITE {255, 255, 255, 255}
#define RED {255, 0, 0, 255}
#define GREEN {0, 255, 0, 255}
#define BLUE {0, 0, 255, 255}
struct test_data
{
Ecore_Evas *ee;
};
struct color_tuple
{
int r, g, b, a;
} clipper_colors[4] = {WHITE, RED, GREEN, BLUE};
int cur_color = 0;
int cur_angle = 0;
static const char *
_index_to_color(int i)
{
switch (i)
{
case 0:
return "WHITE (default)";
case 1:
return "RED";
case 2:
return "GREEN";
case 3:
return "BLUE";
default:
return "other";
}
}
static struct test_data d = {0};
static const char *border_img_path = PACKAGE_EXAMPLES_DIR EVAS_IMAGE_FOLDER "/red.png";
#define _evas_smart_example_type "Evas_Smart_Example"
#define EVT_CHILDREN_NUMBER_CHANGED "children,changed"
{
{EVT_CHILDREN_NUMBER_CHANGED, "i"},
{NULL, NULL}
};
typedef struct _Evas_Smart_Example_Data Evas_Smart_Example_Data;
struct _Evas_Smart_Example_Data
{
int child_count;
};
#define EVAS_SMART_EXAMPLE_DATA_GET(o, ptr) \
Evas_Smart_Example_Data * ptr = evas_object_smart_data_get(o)
#define EVAS_SMART_EXAMPLE_DATA_GET_OR_RETURN(o, ptr) \
EVAS_SMART_EXAMPLE_DATA_GET(o, ptr); \
if (!ptr) \
{ \
fprintf(stderr, "No widget data for object %p (%s)!", \
o, evas_object_type_get(o)); \
fflush(stderr); \
abort(); \
return; \
}
#define EVAS_SMART_EXAMPLE_DATA_GET_OR_RETURN_VAL(o, ptr, val) \
EVAS_SMART_EXAMPLE_DATA_GET(o, ptr); \
if (!ptr) \
{ \
fprintf(stderr, "No widget data for object %p (%s)!", \
o, evas_object_type_get(o)); \
fflush(stderr); \
abort(); \
return val; \
}
static void
{
}
static void
_canvas_resize_cb(Ecore_Evas *ee)
{
int w, h;
evas_object_resize(d.bg, w, h);
}
static void
_on_child_del(void *data,
{
long idx;
EVAS_SMART_EXAMPLE_DATA_GET(example_smart, priv);
idx--;
priv->children[idx] = NULL;
}
static void
_evas_smart_example_child_callbacks_unregister(
Evas_Object *obj)
{
}
static void
_evas_smart_example_child_callbacks_register(
Evas_Object *o,
long idx)
{
}
static void
{
_evas_smart_example_parent_sc->add(o);
priv->border = evas_object_image_filled_add(evas_object_evas_get(o));
evas_object_image_file_set(priv->border, border_img_path, NULL);
evas_object_image_border_set(priv->border, 3, 3, 3, 3);
evas_object_image_border_center_fill_set(
priv->border, EVAS_BORDER_FILL_NONE);
}
static void
{
EVAS_SMART_EXAMPLE_DATA_GET(o, priv);
if (priv->children[0])
{
_evas_smart_example_child_callbacks_unregister(priv->children[0]);
priv->children[0] = NULL;
}
if (priv->children[1])
{
_evas_smart_example_child_callbacks_unregister(priv->children[1]);
priv->children[1] = NULL;
}
_evas_smart_example_parent_sc->del(o);
}
static void
{
if ((ow == w) && (oh == h)) return;
}
static void
{
EVAS_SMART_EXAMPLE_DATA_GET_OR_RETURN(o, priv);
evas_object_resize(priv->border, w, h);
evas_object_move(priv->border, x, y);
if (priv->children[0])
{
evas_object_move(priv->children[0], x + 3, y + 3);
evas_object_resize(priv->children[0], (w / 2) - 3, (h / 2) - 3);
}
if (priv->children[1])
{
evas_object_move(priv->children[1], x + (w / 2), y + (h / 2));
evas_object_resize(priv->children[1], (w / 2) - 3, (h / 2) - 3);
}
}
static void
{
sc->
add = _evas_smart_example_smart_add;
sc->
del = _evas_smart_example_smart_del;
sc->
resize = _evas_smart_example_smart_resize;
sc->
calculate = _evas_smart_example_smart_calculate;
}
evas_smart_example_add(
Evas *evas)
{
}
static void
_evas_smart_example_remove_do(Evas_Smart_Example_Data *priv,
int idx)
{
priv->children[idx] = NULL;
priv->child_count--;
_evas_smart_example_child_callbacks_unregister(child);
}
{
long idx;
EVAS_SMART_EXAMPLE_DATA_GET_OR_RETURN_VAL(o, priv, NULL);
if (priv->children[0] != child && priv->children[1] != child)
{
fprintf(stderr, "You are trying to remove something not belonging to"
" the example smart object!\n");
return NULL;
}
idx--;
_evas_smart_example_remove_do(priv, child, idx);
o, EVT_CHILDREN_NUMBER_CHANGED, (void *)(uintptr_t)priv->child_count);
return child;
}
{
EVAS_SMART_EXAMPLE_DATA_GET_OR_RETURN_VAL(o, priv, NULL);
if (!child)
return NULL;
if (priv->children[1] == child)
{
fprintf(stderr, "You mustn't place a child on both slots of"
" the example smart object!\n");
return NULL;
}
if (priv->children[0])
{
if (priv->children[0] != child)
{
ret = priv->children[0];
_evas_smart_example_remove_do(priv, priv->children[0], 0);
}
else return child;
}
priv->children[0] = child;
_evas_smart_example_child_callbacks_register(o, child, 0);
priv->child_count++;
if (!ret)
{
o, EVT_CHILDREN_NUMBER_CHANGED, (void *)(uintptr_t)priv->child_count);
}
return ret;
}
{
EVAS_SMART_EXAMPLE_DATA_GET_OR_RETURN_VAL(o, priv, NULL);
if (!child)
return NULL;
if (priv->children[0] == child)
{
fprintf(stderr, "You mustn't place a child on both slots of"
" the example smart object!\n");
return NULL;
}
if (priv->children[1])
{
if (priv->children[1] != child)
{
ret = priv->children[1];
_evas_smart_example_remove_do(priv, priv->children[1], 1);
}
else return child;
}
priv->children[1] = child;
_evas_smart_example_child_callbacks_register(o, child, 1);
priv->child_count++;
if (!ret)
{
o, EVT_CHILDREN_NUMBER_CHANGED, (void *)(uintptr_t)priv->child_count);
}
return ret;
}
static void
_map_update(void)
{
Evas_Map *m;
evas_object_map_set(d.smt, m);
evas_object_map_enable_set(d.smt,
EINA_TRUE);
}
static void
void *einfo)
{
if (strcmp(ev->
key,
"q") == 0)
{
_on_destroy(NULL);
return;
}
if (strcmp(ev->
key,
"h") == 0)
{
puts(commands);
return;
}
if (strcmp(ev->
key,
"w") == 0)
{
if (d.rects[0])
{
evas_smart_example_remove(d.smt, d.rects[0]);
}
if (d.rects[1])
{
evas_smart_example_remove(d.smt, d.rects[1]);
}
memset(d.rects, 0, sizeof(d.rects));
printf("Deleting all members of the smart object.\n");
return;
}
if (strcmp(ev->
key,
"l") == 0)
{
rect, rand() % 255, rand() % 255, rand() % 255, 255);
prev = evas_smart_example_set_left(d.smt, rect);
d.rects[0] = rect;
printf("Setting smart object's left spot with a new rectangle.\n");
printf("Checking its new smart object parent: %s\n",
"Failure!");
if (prev)
{
int r, g, b;
printf("Deleting previous left child,"
" which had colors (%d, %d, %d)\n", r, g, b);
}
return;
}
if (strcmp(ev->
key,
"r") == 0)
{
rect, rand() % 255, rand() % 255, rand() % 255, 255);
prev = evas_smart_example_set_right(d.smt, rect);
d.rects[1] = rect;
printf("Setting smart object's right spot with a new rectangle.\n");
printf("Checking its new smart object parent: %s\n",
"Failure!");
if (prev)
{
int r, g, b;
printf("Deleting previous right child,"
" which had colors (%d, %d, %d)\n", r, g, b);
}
return;
}
if (strcmp(ev->
key,
"Right") == 0 || strcmp(ev->
key,
"Left") == 0 ||
strcmp(ev->
key,
"Up") == 0 || strcmp(ev->
key,
"Down") == 0)
{
{
case 'R':
x += 20;
break;
case 'L':
x -= 20;
break;
case 'U':
y -= 20;
break;
case 'D':
y += 20;
break;
}
evas_object_move(d.smt, x, y);
_map_update();
return;
}
if (strcmp(ev->
key,
"i") == 0)
{
w *= 1.1;
h *= 1.1;
evas_object_resize(d.smt, w, h);
_map_update();
return;
}
if (strcmp(ev->
key,
"d") == 0)
{
w *= 0.9;
h *= 0.9;
evas_object_resize(d.smt, w, h);
_map_update();
return;
}
if (strcmp(ev->
key,
"c") == 0)
{
cur_color = (cur_color + 1) % 4;
d.clipper, clipper_colors[cur_color].r, clipper_colors[cur_color].g,
clipper_colors[cur_color].b, clipper_colors[cur_color].a);
fprintf(stderr, "Changing clipper's color to %s\n",
_index_to_color(cur_color));
return;
}
if (strcmp(ev->
key,
"period") == 0)
{
cur_angle = (cur_angle + 30) % 360;
_map_update();
return;
}
if (strcmp(ev->
key,
"comma") == 0)
{
cur_angle = (cur_angle - 30) % 360;
_map_update();
return;
}
fprintf(stderr,
"Invalid key: '%s'\n", ev->
key);
}
static void
_on_example_smart_object_child_num_change(
void *data
EINA_UNUSED,
void *event_info)
{
printf("Number of child members on our example smart"
" object changed to %llu\n", (unsigned long long)(uintptr_t)event_info);
}
int
main(void)
{
unsigned int count;
srand(time(NULL));
return EXIT_FAILURE;
if (!d.ee)
goto error;
evas_object_move(d.bg, 0, 0);
evas_object_resize(d.bg, WIDTH, HEIGHT);
d.smt = evas_smart_example_add(d.evas);
evas_object_move(d.smt, WIDTH / 4, HEIGHT / 4);
evas_object_resize(d.smt, WIDTH / 2, HEIGHT / 2);
printf("Adding smart object of type \"%s\" to the canvas: %s.\n",
_evas_smart_example_type, ret ? "success" : "failure");
printf("Checking if clipped smart object's clipper is a "
"\"static\" one: %s\n",
evas_object_static_clip_get(d.clipper) ? "yes" : "no");
d.clipper, clipper_colors[cur_color].r, clipper_colors[cur_color].g,
clipper_colors[cur_color].b, clipper_colors[cur_color].a);
d.smt, &descriptions, &count, NULL, NULL);
for (; *descriptions; descriptions++)
{
printf("We've found a smart callback on the smart object!"
"\n\tname: %s\n\ttype: %s\n", (*descriptions)->name,
(*descriptions)->type);
if (strcmp((*descriptions)->type, "i")) continue;
d.smt, (*descriptions)->name,
_on_example_smart_object_child_num_change, NULL);
}
puts(commands);
return 0;
error:
fprintf(stderr, "error: Requires at least one Evas engine built and linked"
" to ecore-evas for this example to run properly.\n");
return -1;
}