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;
if (!ee) goto error;
bg = evas_object_rectangle_add(evas);
evas_object_color_set(bg, 255, 255, 255, 255);
evas_object_move(bg, 0, 0);
evas_object_resize(bg, WIDTH, HEIGHT);
evas_object_show(bg);
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);
evas_object_show(text);
_canvas_resize_cb(ee);
fprintf(stdout, commands);
static void
_canvas_resize_cb(Ecore_Evas *ee)
{
int w, h;
char buf[1024];
snprintf(buf, sizeof(buf), "%d x %d", w, h);
evas_object_text_text_set(text, buf);
evas_object_move(text, (w - 150) / 2, (h - 50) / 2);
evas_object_resize(bg, w, h);
}
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"
"\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->keyname, "m") == 0)
{
min_set = !min_set;
if (min_set)
{
fprintf(stdout, "Imposing a minimum size of %d x %d\n",
WIDTH / 2, HEIGHT / 2);
}
else
{
fprintf(stdout, "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->keyname, "x") == 0)
{
max_set = !max_set;
if (max_set)
{
fprintf(stdout, "Imposing a maximum size of %d x %d\n",
WIDTH * 2, HEIGHT * 2);
}
else
{
fprintf(stdout, "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->keyname, "b") == 0)
{
base_set = !base_set;
if (base_set)
{
fprintf(stdout, "Imposing a base size of %d x %d\n",
WIDTH * 2, HEIGHT * 2);
}
else
{
fprintf(stdout, "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->keyname, "s") == 0)
{
step_set = !step_set;
if (step_set)
{
fprintf(stdout, "Imposing a step size of %d x %d\n", 40, 40);
}
else
{
fprintf(stdout, "Taking off step size restriction from the"
" window\n");
}
return;
}
The full example follows.
#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define __UNUSED__
#endif
#include <Ecore.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"
"\th - print help\n";
static void
_canvas_resize_cb(Ecore_Evas *ee)
{
int w, h;
char buf[1024];
snprintf(buf, sizeof(buf), "%d x %d", w, h);
evas_object_text_text_set(text, buf);
evas_object_move(text, (w - 150) / 2, (h - 50) / 2);
evas_object_resize(bg, w, h);
}
static void
_on_destroy(Ecore_Evas *ee __UNUSED__)
{
}
static void
_on_keydown(void *data __UNUSED__,
Evas *evas __UNUSED__,
Evas_Object *o __UNUSED__,
void *einfo)
{
Evas_Event_Key_Down *ev = einfo;
if (strcmp(ev->keyname, "h") == 0)
{
fprintf(stdout, commands);
return;
}
if (strcmp(ev->keyname, "m") == 0)
{
min_set = !min_set;
if (min_set)
{
fprintf(stdout, "Imposing a minimum size of %d x %d\n",
WIDTH / 2, HEIGHT / 2);
}
else
{
fprintf(stdout, "Taking off minimum size restriction from the"
" window\n");
}
return;
}
if (strcmp(ev->keyname, "x") == 0)
{
max_set = !max_set;
if (max_set)
{
fprintf(stdout, "Imposing a maximum size of %d x %d\n",
WIDTH * 2, HEIGHT * 2);
}
else
{
fprintf(stdout, "Taking off maximum size restriction from the"
" window\n");
}
return;
}
if (strcmp(ev->keyname, "b") == 0)
{
base_set = !base_set;
if (base_set)
{
fprintf(stdout, "Imposing a base size of %d x %d\n",
WIDTH * 2, HEIGHT * 2);
}
else
{
fprintf(stdout, "Taking off base size restriction from the"
" window\n");
}
return;
}
if (strcmp(ev->keyname, "s") == 0)
{
step_set = !step_set;
if (step_set)
{
fprintf(stdout, "Imposing a step size of %d x %d\n", 40, 40);
}
else
{
fprintf(stdout, "Taking off step size restriction from the"
" window\n");
}
return;
}
}
int
main(void)
{
Evas *evas;
return EXIT_FAILURE;
if (!ee) goto error;
bg = evas_object_rectangle_add(evas);
evas_object_color_set(bg, 255, 255, 255, 255);
evas_object_move(bg, 0, 0);
evas_object_resize(bg, WIDTH, HEIGHT);
evas_object_show(bg);
evas_object_focus_set(bg, EINA_TRUE);
evas_object_event_callback_add(
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);
evas_object_show(text);
_canvas_resize_cb(ee);
fprintf(stdout, commands);
return 0;
error:
fprintf(stderr, "You got to have at least one Evas engine built"
" and linked up to ecore-evas for this example to run"
" properly.\n");
return -1;
}