Ecore_Evas window size hints

On this example, we show you how to deal with Ecore_Evas window size hints, which are implemented per Evas engine.

We start by defining an initial size for our window and, after creating it, adding a background white rectangle and a text object to it, to be used to display the current window's sizes, at any given time:

#define WIDTH (300)
#define HEIGHT (300)
return EXIT_FAILURE;
/* this will give you a window with an Evas canvas under the first
* engine available */
ee = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, NULL);
if (!ee) goto error;
ecore_evas_title_set(ee, "Ecore_Evas window sizes example");
ecore_evas_callback_resize_set(ee, _canvas_resize_cb);
evas = ecore_evas_get(ee);
evas_object_color_set(bg, 255, 255, 255, 255); /* white bg */
evas_object_move(bg, 0, 0); /* at canvas' origin */
evas_object_resize(bg, WIDTH, HEIGHT); /* covers full canvas */
EAPI int ecore_evas_init(void)
Inits the Ecore_Evas system.
Definition ecore_evas.c:608
EAPI void ecore_evas_title_set(Ecore_Evas *ee, const char *t)
Sets the title of an Ecore_Evas' window.
Definition ecore_evas.c:1541
EAPI void ecore_evas_callback_delete_request_set(Ecore_Evas *ee, Ecore_Evas_Event_Cb func)
Sets a callback for Ecore_Evas delete request events.
Definition ecore_evas.c:1190
EAPI void ecore_evas_show(Ecore_Evas *ee)
Shows an Ecore_Evas' window.
Definition ecore_evas.c:1494
EAPI Evas * ecore_evas_get(const Ecore_Evas *ee)
Gets an Ecore_Evas's Evas.
Definition ecore_evas.c:1314
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:1053
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:1154
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_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_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
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_rectangle_add(Evas *e)
Adds a rectangle to the given evas.
Definition evas_object_rectangle.c:78
text = evas_object_text_add(evas);
evas_object_color_set(text, 0, 0, 0, 255);
evas_object_resize(text, 150, 50);
evas_object_text_font_set(text, "Sans", 20);
_canvas_resize_cb(ee);
printf(commands);
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition ecore_main.c:1311
EVAS_API void evas_object_text_font_set(Eo *obj, const char *font, Evas_Font_Size size)
Set the font family or filename, and size on a given text object.
Definition evas_object_text.c:2340
EVAS_API Evas_Object * evas_object_text_add(Evas *e)
Creates a new text object on the provided canvas.
Definition evas_object_text.c:366
/* to inform current window's size */
static void
_canvas_resize_cb(Ecore_Evas *ee_)
{
int w, h;
char buf[1024];
ecore_evas_geometry_get(ee_, NULL, NULL, &w, &h);
snprintf(buf, sizeof(buf), "%d x %d", w, h);
evas_object_move(text, (w - 150) / 2, (h - 50) / 2);
evas_object_resize(bg, w, h);
}
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:1376
EVAS_API void evas_object_text_text_set(Eo *obj, const char *text)
Sets the text string to be displayed by the given text object.
Definition evas_object_text.c:2356

The program has a command line interface, responding to the following keys:

static const char commands[] = \
"commands are:\n"
"\tm - impose a minumum size to the window\n"
"\tx - impose a maximum size to the window\n"
"\tb - impose a base size to the window\n"
"\ts - impose a step size (different than 1 px) to the window\n"
"\tg - get the screen geometry\n"
"\th - print help\n";

Use the 'm' key to impose a minimum size of half the initial ones on our window. Test it by trying to resize it to smaller sizes than that:

if (strcmp(ev->key, "m") == 0) /* impose a minimum size on the window */
{
min_set = !min_set;
if (min_set)
{
ecore_evas_size_min_set(ee, WIDTH / 2, HEIGHT / 2);
printf("Imposing a minimum size of %d x %d\n",
WIDTH / 2, HEIGHT / 2);
}
EAPI void ecore_evas_size_min_set(Ecore_Evas *ee, int w, int h)
Sets the minimum size of a given Ecore_Evas window.
Definition ecore_evas.c:1574
else
{
printf("Taking off minimum size restriction from the"
" window\n");
}
return;
}

The 'x' key will, in turn, set a maximum size on our window – to two times our initial size. Test it by trying to resize the window to bigger sizes than that:

if (strcmp(ev->key, "x") == 0) /* impose a maximum size on the window */
{
max_set = !max_set;
if (max_set)
{
ecore_evas_size_max_set(ee, WIDTH * 2, HEIGHT * 2);
printf("Imposing a maximum size of %d x %d\n",
WIDTH * 2, HEIGHT * 2);
}
EAPI void ecore_evas_size_max_set(Ecore_Evas *ee, int w, int h)
Sets the maximum size of a given Ecore_Evas window.
Definition ecore_evas.c:1610
else
{
printf("Taking off maximum size restriction from the"
" window\n");
}
return;
}

Window base sizes will override any minimum sizes set, so try it with the 'b' key. It will set a base size of two times the initial one:

if (strcmp(ev->key, "b") == 0) /* impose a base size on the window */
{
base_set = !base_set;
if (base_set)
{
ecore_evas_size_base_set(ee, WIDTH * 2, HEIGHT * 2);
printf("Imposing a base size of %d x %d\n",
WIDTH * 2, HEIGHT * 2);
}
EAPI void ecore_evas_size_base_set(Ecore_Evas *ee, int w, int h)
Sets the base size for a given Ecore_Evas window.
Definition ecore_evas.c:1646
else
{
printf("Taking off base size restriction from the"
" window\n");
}
return;
}

Finally, there's a key to impose a "step size" on our window, of 40 pixels. With than on ('s' key), you'll see the window will always be bound to multiples of that size, for dimensions on both axis:

if (strcmp(ev->key, "s") == 0) /* impose a step size on the window */
{
step_set = !step_set;
if (step_set)
{
printf("Imposing a step size of %d x %d\n", 40, 40);
}
EAPI void ecore_evas_size_step_set(Ecore_Evas *ee, int w, int h)
Sets the "size step" for a given Ecore_Evas window.
Definition ecore_evas.c:1680
else
{
printf("Taking off step size restriction from the"
" window\n");
}
return;
}

The full example follows.

#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define EINA_UNUSED
#endif
#include <Ecore.h>
#include <Ecore_Evas.h>
#define WIDTH (300)
#define HEIGHT (300)
static Ecore_Evas *ee;
static Evas_Object *text, *bg;
static Eina_Bool min_set = EINA_FALSE;
static Eina_Bool max_set = EINA_FALSE;
static Eina_Bool base_set = EINA_FALSE;
static Eina_Bool step_set = EINA_FALSE;
static const char commands[] = \
"commands are:\n"
"\tm - impose a minumum size to the window\n"
"\tx - impose a maximum size to the window\n"
"\tb - impose a base size to the window\n"
"\ts - impose a step size (different than 1 px) to the window\n"
"\tg - get the screen geometry\n"
"\th - print help\n";
/* to inform current window's size */
static void
_canvas_resize_cb(Ecore_Evas *ee_)
{
int w, h;
char buf[1024];
ecore_evas_geometry_get(ee_, NULL, NULL, &w, &h);
snprintf(buf, sizeof(buf), "%d x %d", w, h);
evas_object_move(text, (w - 150) / 2, (h - 50) / 2);
evas_object_resize(bg, w, h);
}
static void
_on_destroy(Ecore_Evas *ee_ EINA_UNUSED)
{
}
static void
_on_keydown(void *data EINA_UNUSED,
Evas *evas EINA_UNUSED,
Evas_Object *o EINA_UNUSED,
void *einfo)
{
Evas_Event_Key_Down *ev = einfo;
if (strcmp(ev->key, "h") == 0) /* print help */
{
printf(commands);
return;
}
if (strcmp(ev->key, "m") == 0) /* impose a minimum size on the window */
{
min_set = !min_set;
if (min_set)
{
ecore_evas_size_min_set(ee, WIDTH / 2, HEIGHT / 2);
printf("Imposing a minimum size of %d x %d\n",
WIDTH / 2, HEIGHT / 2);
}
else
{
printf("Taking off minimum size restriction from the"
" window\n");
}
return;
}
if (strcmp(ev->key, "x") == 0) /* impose a maximum size on the window */
{
max_set = !max_set;
if (max_set)
{
ecore_evas_size_max_set(ee, WIDTH * 2, HEIGHT * 2);
printf("Imposing a maximum size of %d x %d\n",
WIDTH * 2, HEIGHT * 2);
}
else
{
printf("Taking off maximum size restriction from the"
" window\n");
}
return;
}
if (strcmp(ev->key, "b") == 0) /* impose a base size on the window */
{
base_set = !base_set;
if (base_set)
{
ecore_evas_size_base_set(ee, WIDTH * 2, HEIGHT * 2);
printf("Imposing a base size of %d x %d\n",
WIDTH * 2, HEIGHT * 2);
}
else
{
printf("Taking off base size restriction from the"
" window\n");
}
return;
}
if (strcmp(ev->key, "s") == 0) /* impose a step size on the window */
{
step_set = !step_set;
if (step_set)
{
printf("Imposing a step size of %d x %d\n", 40, 40);
}
else
{
printf("Taking off step size restriction from the"
" window\n");
}
return;
}
if (strcmp(ev->key, "g") == 0) /* get screen geometry */
{
int x, y, w, h;
ecore_evas_screen_geometry_get(ee, &x, &y, &w, &h);
printf("screen geometry: %d,%d, %dx%d\n", x, y, w, h);
return;
}
}
int
main(void)
{
Evas *evas;
return EXIT_FAILURE;
/* this will give you a window with an Evas canvas under the first
* engine available */
ee = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, NULL);
if (!ee) goto error;
ecore_evas_title_set(ee, "Ecore_Evas window sizes example");
ecore_evas_callback_resize_set(ee, _canvas_resize_cb);
evas = ecore_evas_get(ee);
evas_object_color_set(bg, 255, 255, 255, 255); /* white bg */
evas_object_move(bg, 0, 0); /* at canvas' origin */
evas_object_resize(bg, WIDTH, HEIGHT); /* covers full canvas */
bg, EVAS_CALLBACK_KEY_DOWN, _on_keydown, NULL);
text = evas_object_text_add(evas);
evas_object_color_set(text, 0, 0, 0, 255);
evas_object_resize(text, 150, 50);
evas_object_text_font_set(text, "Sans", 20);
_canvas_resize_cb(ee);
printf(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;
}
Evas wrapper functions.
@ EVAS_CALLBACK_KEY_DOWN
Key Press Event.
Definition Evas_Common.h:430
EAPI int ecore_evas_shutdown(void)
Shuts down the Ecore_Evas system.
Definition ecore_evas.c:672
EAPI void ecore_evas_free(Ecore_Evas *ee)
Frees an Ecore_Evas.
Definition ecore_evas.c:1097
EAPI void ecore_evas_screen_geometry_get(const Ecore_Evas *ee, int *x, int *y, int *w, int *h)
Gets geometry of screen associated with this Ecore_Evas.
Definition ecore_evas.c:2752
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
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition eina_types.h:539
#define EINA_FALSE
boolean value FALSE (numerical value 0)
Definition eina_types.h:533
unsigned char Eina_Bool
Type to mimic a boolean.
Definition eina_types.h:527
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition eina_types.h:339
EVAS_API void evas_object_event_callback_add(Evas_Object *eo_obj, Evas_Callback_Type type, Evas_Object_Event_Cb func, const void *data)
Add (register) a callback function to a given Evas object event.
Definition evas_callbacks.c:478
EVAS_API void evas_object_focus_set(Efl_Canvas_Object *obj, Eina_Bool focus)
Indicates that this object is the keyboard event receiver on its canvas.
Definition efl_canvas_object_eo.legacy.c:39
Key press event.
Definition Evas_Legacy.h:314
const char * key
The logical key : (eg shift+1 == exclamation)
Definition Evas_Legacy.h:320