Table Smart Object example

This example will arrange rectangles of different sizes(and colors) in a table.

While it's possible to create the same layout we are doing here by positioning each rectangle independently, using a table makes it a lot easier, since the table will control layout of all the objects, allowing you to move, resize or hide the entire table.

We'll start with creating the table, setting it to EVAS_OBJECT_TABLE_HOMOGENEOUS_NONE to have maximum flexibility and setting its padding to 0:

table = evas_object_table_add(d.evas);
evas_object_resize(table, WIDTH, HEIGHT);
EVAS_API void evas_object_show(Evas_Object *eo_obj)
Makes the given Evas object visible.
Definition: evas_object_main.c:1814
EVAS_API void evas_object_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
Changes the size of the given Evas object.
Definition: evas_object_main.c:1236
EVAS_API Evas_Object * evas_object_table_add(Evas *evas)
Create a new table.
Definition: evas_object_table.c:969
EVAS_API void evas_object_table_homogeneous_set(Evas_Table *obj, Evas_Object_Table_Homogeneous_Mode homogeneous)
Set how this table should layout children.
Definition: evas_table_eo.legacy.c:3
EVAS_API void evas_object_table_padding_set(Evas_Table *obj, int horizontal, int vertical)
Control the padding between cells.
Definition: evas_table_eo.legacy.c:27
@ EVAS_OBJECT_TABLE_HOMOGENEOUS_NONE
No mode specified.
Definition: evas_table_eo.h:24

We then create each rectangle and add it to the table:

rect = evas_object_rectangle_add(d.evas);
evas_object_color_set(rect, 255, 0, 0, 255);
evas_object_table_pack(table, rect, 1, 1, 2, 1);
EVAS_API void evas_object_color_set(Evas_Object *obj, int r, int g, int b, int a)
Sets the general/main color of the given Evas object to the given one.
Definition: evas_object_main.c:2024
EVAS_API void evas_object_size_hint_min_set(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
Sets the hints for an object's minimum size.
Definition: evas_object_main.c:2611
EVAS_API Evas_Object * evas_object_rectangle_add(Evas *e)
Adds a rectangle to the given evas.
Definition: evas_object_rectangle.c:78
EVAS_API Eina_Bool evas_object_table_pack(Evas_Table *obj, Efl_Canvas_Object *child, unsigned short col, unsigned short row, unsigned short colspan, unsigned short rowspan)
Add a new child to a table object or set its current packing.
Definition: evas_table_eo.legacy.c:87
rect = evas_object_rectangle_add(d.evas);
evas_object_color_set(rect, 0, 255, 0, 255);
evas_object_table_pack(table, rect, 1, 2, 1, 2);
rect = evas_object_rectangle_add(d.evas);
evas_object_color_set(rect, 0, 0, 255, 255);
evas_object_table_pack(table, rect, 2, 2, 1, 1);
rect = evas_object_rectangle_add(d.evas);
evas_object_color_set(rect, 255, 255, 0, 255);
evas_object_table_pack(table, rect, 2, 3, 1, 1);
Note
Each rectangle has a different minimum size based on how many rows and columns it will occupy.

The full source for this example follow:

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <Ecore.h>
#include <Ecore_Evas.h>
#include <stdlib.h>
#define WIDTH 100
#define HEIGHT 150
struct test_data
{
Ecore_Evas *ee;
Evas *evas;
};
static struct test_data d = {0};
static void
_on_destroy(Ecore_Evas *ee EINA_UNUSED)
{
}
/* Keep the example's window size in sync with the background image's size */
static void
_canvas_resize_cb(Ecore_Evas *ee)
{
int w, h;
ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
evas_object_resize(d.bg, w, h);
}
int
main(void)
{
Evas_Object *table, *rect;
return EXIT_FAILURE;
/* this will give you a window with an Evas canvas under the first
* engine available */
d.ee = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, NULL);
if (!d.ee)
goto error;
ecore_evas_callback_destroy_set(d.ee, _on_destroy);
ecore_evas_callback_resize_set(d.ee, _canvas_resize_cb);
/* the canvas pointer, de facto */
d.evas = ecore_evas_get(d.ee);
d.bg = evas_object_rectangle_add(d.evas);
evas_object_color_set(d.bg, 255, 255, 255, 255); /* white bg */
evas_object_move(d.bg, 0, 0); /* at canvas' origin */
evas_object_resize(d.bg, WIDTH, HEIGHT); /* covers full canvas */
table = evas_object_table_add(d.evas);
evas_object_resize(table, WIDTH, HEIGHT);
rect = evas_object_rectangle_add(d.evas);
evas_object_color_set(rect, 255, 0, 0, 255);
evas_object_table_pack(table, rect, 1, 1, 2, 1);
rect = evas_object_rectangle_add(d.evas);
evas_object_color_set(rect, 0, 255, 0, 255);
evas_object_table_pack(table, rect, 1, 2, 1, 2);
rect = evas_object_rectangle_add(d.evas);
evas_object_color_set(rect, 0, 0, 255, 255);
evas_object_table_pack(table, rect, 2, 2, 1, 1);
rect = evas_object_rectangle_add(d.evas);
evas_object_color_set(rect, 255, 255, 0, 255);
evas_object_table_pack(table, rect, 2, 3, 1, 1);
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;
}
Evas wrapper functions.
EAPI int ecore_evas_init(void)
Inits the Ecore_Evas system.
Definition: ecore_evas.c:602
EAPI void ecore_evas_callback_destroy_set(Ecore_Evas *ee, Ecore_Evas_Event_Cb func)
Sets a callback for Ecore_Evas destroy events.
Definition: ecore_evas.c:1185
EAPI void ecore_evas_show(Ecore_Evas *ee)
Shows an Ecore_Evas' window.
Definition: ecore_evas.c:1480
EAPI Evas * ecore_evas_get(const Ecore_Evas *ee)
Gets an Ecore_Evas's Evas.
Definition: ecore_evas.c:1300
EAPI void ecore_evas_geometry_get(const Ecore_Evas *ee, int *x, int *y, int *w, int *h)
Gets the geometry of an Ecore_Evas.
Definition: ecore_evas.c:1362
EAPI Ecore_Evas * ecore_evas_new(const char *engine_name, int x, int y, int w, int h, const char *extra_options)
Creates a new Ecore_Evas based on engine name and common parameters.
Definition: ecore_evas.c:1039
EAPI void ecore_evas_callback_resize_set(Ecore_Evas *ee, Ecore_Evas_Event_Cb func)
Sets a callback for Ecore_Evas resize events.
Definition: ecore_evas.c:1140
EAPI int ecore_evas_shutdown(void)
Shuts down the Ecore_Evas system.
Definition: ecore_evas.c:666
EAPI void ecore_evas_free(Ecore_Evas *ee)
Frees an Ecore_Evas.
Definition: ecore_evas.c:1083
void ecore_main_loop_quit(void)
Quits the main loop once all the events currently on the queue have been processed.
Definition: ecore_main.c:1321
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition: ecore_main.c:1311
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339
Eo Evas
An opaque handle to an Evas canvas.
Definition: Evas_Common.h:163
EVAS_API void evas_object_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
Move the given Evas object to the given location inside its canvas' viewport.
Definition: evas_object_main.c:1171
Efl_Canvas_Object Evas_Object
An Evas Object handle.
Definition: Evas_Common.h:185