This example shows how to append, insert and remove elements from an Edje box part.
It will make use of the edje_object_part_box functions.
To play with this example, use mouse left click to delete an existing rectangle from the box and right mouse click to add a new rectangle just before the clicked one. Use the keyboard keys "a" to append a rectangle, "i" to prepend, and "c" to remove all rectangles from the box.
We will store our example global information in the data structure defined below, and also set some callbacks for resizing the canvas and exiting the window:
static const char commands[] = \
"commands are:\n"
"\ti - prepend rectangle\n"
"\ta - append rectangle\n"
"\tc - remove\n"
"\tEsc - exit\n"
"\th - print help\n";
static void
{
}
static void
_on_canvas_resize(Ecore_Evas *ee)
{
int w;
int h;
}
In the main
function, we create our Ecore_Evas
, add a background to it, and finally load our Edje
file that contains a Box
part. This part is named "example/box"
in this case, and we use this name to append elements to it.
The code until now is the one that follows:
Also notice that we set the callback _bg_key_down
for "key down"
events on the background object, and that object is the one with focus.
Now we add some small rectangles to the box part, using the edje_object_part_box_append() API, and set some callbacks for "mouse down"
events on every object. These callbacks will be used to add or delete objects from the box part.
for (i = 0; i < NRECTS; i++)
{
int red = (i * 10) % 256;
if (!r)
printf("An error occurred when appending rect #%d to the box.\n", i);
}
Now let's take a look at the callbacks for key down and mouse down events:
static void
{
Ecore_Evas *ee;
ee = (Ecore_Evas *)data;
{
printf("Removing rect %p under the mouse pointer.\n", o);
}
{
printf("Inserting rect %p before the rectangle under the mouse pointer.\n", rect);
if (!r)
printf("An error occurred when appending rect %p to the box.\n", rect);
}
}
This callback for mouse down events will get left clicks and remove the object that received that left click from the box part, and then delete it. This is done with the edje_object_part_box_remove() function.
However, on right clicks it will create a new rectangle object, and add it just before the right clicked object, using edje_object_part_box_insert_before().
And this is the key down callback:
static void
{
Ecore_Evas *ee;
ee = (Ecore_Evas *)data;
if (!strcmp(ev->
key,
"h"))
{
printf(commands);
return;
}
if (!strcmp(ev->
key,
"i"))
{
printf("Inserting rect %p before the rectangle under the mouse pointer.\n", rect);
if (!r)
printf("An error occurred when appending rect %p to the box.\n", rect);
}
else if (!strcmp(ev->
key,
"a"))
{
printf("Inserting rect %p before the rectangle under the mouse pointer.\n", rect);
if (!r)
printf("An error occurred when appending rect %p to the box.\n", rect);
}
else if (!strcmp(ev->
key,
"c"))
else if (!strcmp(ev->
key,
"Escape"))
else
{
printf(
"unhandled key: %s\n", ev->
key);
printf(commands);
}
It will insert elements at the beginning of the box if "i" was pressed, using edje_object_part_box_insert_at(). It will also append objects to the box if "a" was pressed, just exactly like we did in the main
function. And will remove all objects (deleting them) if "c" was pressed.
As you can see, this example uses the "horizontal_flow"
layout for the box, where each item is put linearly in rows, in as many rows as necessary to store all of them.
The example's window should look like this picture:
The full source code follows:
#ifdef HAVE_CONFIG_H
# include "config.h"
#else
# define EINA_UNUSED
#endif
#ifndef PACKAGE_DATA_DIR
#define PACKAGE_DATA_DIR "."
#endif
#include <Ecore.h>
#define WIDTH 300
#define HEIGHT 300
#define RECTW 30
#define RECTH 30
#define NRECTS 20
static const char commands[] = \
"commands are:\n"
"\ti - prepend rectangle\n"
"\ta - append rectangle\n"
"\tc - remove\n"
"\tEsc - exit\n"
"\th - print help\n";
static void
{
}
static void
_on_canvas_resize(Ecore_Evas *ee)
{
int w;
int h;
}
static void
{
Ecore_Evas *ee;
ee = (Ecore_Evas *)data;
{
printf("Removing rect %p under the mouse pointer.\n", o);
}
{
printf("Inserting rect %p before the rectangle under the mouse pointer.\n", rect);
if (!r)
printf("An error occurred when appending rect %p to the box.\n", rect);
}
}
static void
{
Ecore_Evas *ee;
ee = (Ecore_Evas *)data;
if (!strcmp(ev->
key,
"h"))
{
printf(commands);
return;
}
if (!strcmp(ev->
key,
"i"))
{
printf("Inserting rect %p before the rectangle under the mouse pointer.\n", rect);
if (!r)
printf("An error occurred when appending rect %p to the box.\n", rect);
}
else if (!strcmp(ev->
key,
"a"))
{
printf("Inserting rect %p before the rectangle under the mouse pointer.\n", rect);
if (!r)
printf("An error occurred when appending rect %p to the box.\n", rect);
}
else if (!strcmp(ev->
key,
"c"))
else if (!strcmp(ev->
key,
"Escape"))
else
{
printf(
"unhandled key: %s\n", ev->
key);
printf(commands);
}
}
int
{
const char *edje_file = PACKAGE_DATA_DIR"/box.edj";
Ecore_Evas *ee;
int i;
return EXIT_FAILURE;
goto shutdown_ecore_evas;
if (!ee) goto shutdown_edje;
for (i = 0; i < NRECTS; i++)
{
int red = (i * 10) % 256;
if (!r)
printf("An error occurred when appending rect #%d to the box.\n", i);
}
printf(commands);
return EXIT_SUCCESS;
shutdown_edje:
shutdown_ecore_evas:
return EXIT_FAILURE;
}
To compile use this command:
* gcc -o edje-box edje-box.c -DPACKAGE_BIN_DIR=\"/Where/enlightenment/is/installed/bin\" -DPACKAGE_LIB_DIR=\"/Where/enlightenment/is/installed/lib\"
* -DPACKAGE_DATA_DIR=\"/Where/enlightenment/is/installed/share\"
* `pkg-config --cflags --libs evas ecore ecore-evas edje`
*
* edje_cc box.edc
*