Evas text object example

In this example, we illustrate how to use text objects in various manners.

We place, in the canvas, a text object along with a border image to delimit its geometry. After we instantiate the text object, we set lots of properties on it to the initial ones from a preset list, which has the following declaration:

/* init values one is going to cycle through while running this
* example */
struct text_preset_data init_data =
{
.font = {"DejaVu", "Courier", "Utopia"},
.text = {BLACK, WHITE, GREY, RED, GREEN, BLUE},
.shadow = {WHITE, BLUE, GREEN, RED},
.outline = {WHITE, RED, GREEN, BLUE},
.glow = {WHITE, BLUE, GREEN, RED},
.glow2 = {WHITE, RED, BLUE, GREEN}
};

Then, we set the text string itself, on it, with evas_object_text_text_set(). We set an explicit size of 30 points for our font, as you could see, what we check back with the getter evas_object_text_font_get().

Look at how it translates to code:

d.text = evas_object_text_add(d.evas);
/* let the pre-set things be enforced */
d.text, d.t_data.text_ptr->r, d.t_data.text_ptr->g,
d.t_data.text_ptr->b, d.t_data.text_ptr->a);
d.text, d.t_data.glow_ptr->r, d.t_data.glow_ptr->g,
d.t_data.glow_ptr->b, d.t_data.glow_ptr->a);
d.text, d.t_data.glow2_ptr->r, d.t_data.glow2_ptr->g,
d.t_data.glow2_ptr->b, d.t_data.glow2_ptr->a);
d.text, d.t_data.outline_ptr->r, d.t_data.outline_ptr->g,
d.t_data.outline_ptr->b, d.t_data.outline_ptr->a);
d.text, d.t_data.shadow_ptr->r, d.t_data.shadow_ptr->g,
d.t_data.shadow_ptr->b, d.t_data.shadow_ptr->a);
evas_object_text_font_set(d.text, *d.t_data.font_ptr, 30);
evas_object_text_text_set(d.text, "sample text");
evas_object_resize(d.text, (3 * WIDTH) / 4, HEIGHT / 4);
evas_object_move(d.text, WIDTH / 8, (3 * HEIGHT) / 8);
evas_object_text_font_get(d.text, &font, &size);
printf("Adding text object with font %s, size %d\n", font, size);
/* this is a border around the text object above, here just to
* emphasize its geometry */
d.border = evas_object_image_filled_add(d.evas);
evas_object_image_file_set(d.border, border_img_path, NULL);
evas_object_image_border_set(d.border, 3, 3, 3, 3);
evas_object_resize(d.border, ((3 * WIDTH) / 4) + 3, (HEIGHT / 4) + 3);
evas_object_move(d.border, (WIDTH / 8) - 3, ((3 * HEIGHT) / 8) - 3);
evas_object_show(d.border);
printf("%s", commands);
return 0;
error:
fprintf(stderr, "error: Requires at least one Evas engine built and linked"
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_begin(void)
Runs the application main loop.
Definition: ecore_main.c:1311
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 void evas_object_image_border_set(Evas_Object *obj, int l, int r, int t, int b)
Dimensions of this image's border, a region that does not scale with the center area.
Definition: evas_image_legacy.c:117
EVAS_API void evas_object_image_border_center_fill_set(Evas_Object *obj, Evas_Border_Fill_Mode fill)
Specifies how the center part of the object (not the borders) should be drawn when EFL is rendering i...
Definition: evas_image_legacy.c:145
EVAS_API void evas_object_image_file_set(Evas_Object *obj, const char *file, const char *key)
Set the source file from where an image object must fetch the real image data (it may be an Eet file,...
Definition: evas_image_legacy.c:194
EVAS_API Evas_Object * evas_object_image_filled_add(Evas *eo_e)
Creates a new image object that automatically scales its bound image to the object's area,...
Definition: evas_image_legacy.c:35
@ EVAS_BORDER_FILL_NONE
Image's center region is not to be rendered.
Definition: Evas_Legacy.h:5721
EVAS_API void evas_object_text_outline_color_set(Evas_Text *obj, int r, int g, int b, int a)
Controls the outline color for the given text object.
Definition: evas_text_eo.legacy.c:39
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
EVAS_API void evas_object_text_font_get(const Eo *obj, const char **font, Evas_Font_Size *size)
Retrieve the font family and size in use on a given text object.
Definition: evas_object_text.c:2349
EVAS_API void evas_object_text_style_set(Evas_Text *obj, Evas_Text_Style_Type style)
Controls the style to apply on the given text object.
Definition: evas_text_eo.legacy.c:63
EVAS_API void evas_object_text_shadow_color_set(Evas_Text *obj, int r, int g, int b, int a)
Controls the shadow color for the given text object.
Definition: evas_text_eo.legacy.c:3
EVAS_API void evas_object_text_glow2_color_set(Evas_Text *obj, int r, int g, int b, int a)
Sets the 'glow 2' color for the given text object.
Definition: evas_text_eo.legacy.c:51
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
EVAS_API void evas_object_text_glow_color_set(Evas_Text *obj, int r, int g, int b, int a)
Sets the glow color for the given text object.
Definition: evas_text_eo.legacy.c:75
@ EVAS_TEXT_STYLE_PLAIN
plain, standard text
Definition: Evas_Legacy.h:6365

Like in other Evas examples, one interacts with it by means of key commands:

static const char *commands = \
"commands are:\n"
"\tt - change text's current style\n"
"\tz - change text's font size\n"
"\tf - change text's font family\n"
"\tb - change text's base color\n"
"\ts - change text's \'shadow\' color\n"
"\to - change text's \'outline\' color\n"
"\tw - change text's \'glow\' color\n"
"\tg - change text's \'glow 2\' color\n"
"\th - print help\n";

Use the 't' key to exercise the evas_object_text_style_set() function on the text – it will cycle through all styles on Evas_Text_Style_Type (note we start on EVAS_TEXT_STYLE_PLAIN, thus with no effects on it) and, with other keys, you'll be able to set properties applicable to individual styles on the text object.

The 'z' key will change the text's size, keeping the font family for it. Use 'f' to change the font, keeping the last size set. There are three font families the example will cycle through:

The 'b' command shows us that evas_object_color_set(), on a given text object, will change the text's base color. Experiment with it, which will cycle through the colors in the .text list in init_data.

The 's', 'o', 'w' and 'g' keys will make the text object to cycle to the preset values on colors for shadow, outline, glow and 'glow 2' effects, respectively. Naturally, they will only take effect on the text styles which resemble them.

The full example follows.

#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define PACKAGE_EXAMPLES_DIR "."
#endif
#include <Ecore.h>
#include <Ecore_Evas.h>
#include <stdio.h>
#include <errno.h>
#define WIDTH (320)
#define HEIGHT (240)
#define GREY {190, 190, 190, 255}
#define BLACK {0, 0, 0, 255}
#define WHITE {255, 255, 255, 255}
#define RED {255, 0, 0, 255}
#define GREEN {0, 255, 0, 255}
#define BLUE {0, 0, 255, 255}
#define POINTER_CYCLE(_ptr, _array) \
do \
{ \
if ((unsigned int)(((unsigned char *)(_ptr)) - ((unsigned char *)(_array))) >= \
sizeof(_array)) \
_ptr = _array; \
} \
while(0)
static const char *commands = \
"commands are:\n"
"\tt - change text's current style\n"
"\tz - change text's font size\n"
"\tf - change text's font family\n"
"\tb - change text's base color\n"
"\ts - change text's \'shadow\' color\n"
"\to - change text's \'outline\' color\n"
"\tw - change text's \'glow\' color\n"
"\tg - change text's \'glow 2\' color\n"
"\th - print help\n";
static const char *border_img_path = PACKAGE_EXAMPLES_DIR "/red.png";
struct color_tuple
{
int r, g, b, a;
};
struct text_preset_data
{
const char **font_ptr;
const char *font[3];
struct color_tuple *text_ptr;
struct color_tuple text[6];
struct color_tuple *shadow_ptr;
struct color_tuple shadow[4];
struct color_tuple *outline_ptr;
struct color_tuple outline[4];
struct color_tuple *glow_ptr;
struct color_tuple glow[4];
struct color_tuple *glow2_ptr;
struct color_tuple glow2[4];
};
struct test_data
{
Ecore_Evas *ee;
Evas *evas;
struct text_preset_data t_data;
Evas_Object *text, *bg, *border;
};
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);
}
static const char *
_text_style_type_to_str(Evas_Text_Style_Type mode)
{
switch (mode)
{
return "plain";
return "shadow";
return "outline";
return "soft outline";
return "glow";
return "outline shadow";
return "far shadow";
return "outline soft shadow";
return "soft shadow";
return "far soft shadow";
default:
return "invalid";
}
}
static void
_on_keydown(void *data EINA_UNUSED,
void *einfo)
{
Evas_Event_Key_Down *ev = einfo;
if (strcmp(ev->key, "h") == 0) /* print help */
{
printf("%s", commands);
return;
}
if (strcmp(ev->key, "t") == 0) /* change text's current style */
{
type = (type + 1) % 10;
printf("Changing text's style to \'%s\'\n",
_text_style_type_to_str(type));
return;
}
if (strcmp(ev->key, "f") == 0) /* change text's font */
{
int sz;
(d.t_data.font_ptr)++;
evas_object_text_font_get(d.text, NULL, &sz);
POINTER_CYCLE(d.t_data.font_ptr, d.t_data.font);
evas_object_text_font_set(d.text, *d.t_data.font_ptr, sz);
printf("Changing text's font to %s\n",
*d.t_data.font_ptr);
return;
}
if (strcmp(ev->key, "b") == 0) /* change text's base color */
{
(d.t_data.text_ptr)++;
POINTER_CYCLE(d.t_data.text_ptr, d.t_data.text);
d.text, d.t_data.text_ptr->r, d.t_data.text_ptr->g,
d.t_data.text_ptr->b, d.t_data.text_ptr->a);
printf("Changing base color for text to (%d, %d, %d, %d)\n",
d.t_data.text_ptr->r, d.t_data.text_ptr->g,
d.t_data.text_ptr->b, d.t_data.text_ptr->a);
return;
}
if (strcmp(ev->key, "g") == 0) /* change text's glow 2 color */
{
(d.t_data.glow2_ptr)++;
POINTER_CYCLE(d.t_data.glow2_ptr, d.t_data.glow2);
d.text, d.t_data.glow2_ptr->r, d.t_data.glow2_ptr->g,
d.t_data.glow2_ptr->b, d.t_data.glow2_ptr->a);
printf("Changing glow 2 color for text to (%d, %d, %d, %d)\n",
d.t_data.glow2_ptr->r, d.t_data.glow2_ptr->g,
d.t_data.glow2_ptr->b, d.t_data.glow2_ptr->a);
return;
}
if (strcmp(ev->key, "w") == 0) /* change text's glow color */
{
(d.t_data.glow_ptr)++;
POINTER_CYCLE(d.t_data.glow_ptr, d.t_data.glow);
d.text, d.t_data.glow_ptr->r, d.t_data.glow_ptr->g,
d.t_data.glow_ptr->b, d.t_data.glow_ptr->a);
printf("Changing glow color for text to (%d, %d, %d, %d)\n",
d.t_data.glow_ptr->r, d.t_data.glow_ptr->g,
d.t_data.glow_ptr->b, d.t_data.glow_ptr->a);
return;
}
if (strcmp(ev->key, "o") == 0) /* change text's outline color */
{
(d.t_data.outline_ptr)++;
POINTER_CYCLE(d.t_data.outline_ptr, d.t_data.outline);
d.text, d.t_data.outline_ptr->r, d.t_data.outline_ptr->g,
d.t_data.outline_ptr->b, d.t_data.outline_ptr->a);
printf("Changing outline color for text to (%d, %d, %d, %d)\n",
d.t_data.outline_ptr->r, d.t_data.outline_ptr->g,
d.t_data.outline_ptr->b, d.t_data.outline_ptr->a);
return;
}
if (strcmp(ev->key, "s") == 0) /* change text's shadow color */
{
(d.t_data.shadow_ptr)++;
POINTER_CYCLE(d.t_data.shadow_ptr, d.t_data.shadow);
d.text, d.t_data.shadow_ptr->r, d.t_data.shadow_ptr->g,
d.t_data.shadow_ptr->b, d.t_data.shadow_ptr->a);
printf("Changing shadow color for text to (%d, %d, %d, %d)\n",
d.t_data.shadow_ptr->r, d.t_data.shadow_ptr->g,
d.t_data.shadow_ptr->b, d.t_data.shadow_ptr->a);
return;
}
if (strcmp(ev->key, "z") == 0) /* change text's font size */
{
const char *font;
int size;
evas_object_text_font_get(d.text, &font, &size);
size = (size + 10) % 50;
if (!size) size = 10;
evas_object_text_font_set(d.text, font, size);
printf("Changing text's font size to %d\n", size);
return;
}
}
int
main(void)
{
int size;
const char *font;
return EXIT_FAILURE;
/* init values one is going to cycle through while running this
* example */
struct text_preset_data init_data =
{
.font = {"DejaVu", "Courier", "Utopia"},
.text = {BLACK, WHITE, GREY, RED, GREEN, BLUE},
.shadow = {WHITE, BLUE, GREEN, RED},
.outline = {WHITE, RED, GREEN, BLUE},
.glow = {WHITE, BLUE, GREEN, RED},
.glow2 = {WHITE, RED, BLUE, GREEN}
};
d.t_data = init_data;
d.t_data.font_ptr = d.t_data.font;
d.t_data.text_ptr = d.t_data.text;
d.t_data.glow_ptr = d.t_data.glow;
d.t_data.glow2_ptr = d.t_data.glow2;
d.t_data.outline_ptr = d.t_data.outline;
d.t_data.shadow_ptr = d.t_data.shadow;
/* this will give you a window with an Evas canvas under the first
* engine available */
d.ee = ecore_evas_new(NULL, 10, 10, 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 */
d.bg, EVAS_CALLBACK_KEY_DOWN, _on_keydown, NULL);
d.text = evas_object_text_add(d.evas);
/* let the pre-set things be enforced */
d.text, d.t_data.text_ptr->r, d.t_data.text_ptr->g,
d.t_data.text_ptr->b, d.t_data.text_ptr->a);
d.text, d.t_data.glow_ptr->r, d.t_data.glow_ptr->g,
d.t_data.glow_ptr->b, d.t_data.glow_ptr->a);
d.text, d.t_data.glow2_ptr->r, d.t_data.glow2_ptr->g,
d.t_data.glow2_ptr->b, d.t_data.glow2_ptr->a);
d.text, d.t_data.outline_ptr->r, d.t_data.outline_ptr->g,
d.t_data.outline_ptr->b, d.t_data.outline_ptr->a);
d.text, d.t_data.shadow_ptr->r, d.t_data.shadow_ptr->g,
d.t_data.shadow_ptr->b, d.t_data.shadow_ptr->a);
evas_object_text_font_set(d.text, *d.t_data.font_ptr, 30);
evas_object_text_text_set(d.text, "sample text");
evas_object_resize(d.text, (3 * WIDTH) / 4, HEIGHT / 4);
evas_object_move(d.text, WIDTH / 8, (3 * HEIGHT) / 8);
evas_object_text_font_get(d.text, &font, &size);
printf("Adding text object with font %s, size %d\n", font, size);
/* this is a border around the text object above, here just to
* emphasize its geometry */
d.border = evas_object_image_filled_add(d.evas);
evas_object_image_file_set(d.border, border_img_path, NULL);
evas_object_image_border_set(d.border, 3, 3, 3, 3);
evas_object_resize(d.border, ((3 * WIDTH) / 4) + 3, (HEIGHT / 4) + 3);
evas_object_move(d.border, (WIDTH / 8) - 3, ((3 * HEIGHT) / 8) - 3);
evas_object_show(d.border);
printf("%s", 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_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
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_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_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
Efl_Canvas_Object Evas_Object
An Evas Object handle.
Definition: Evas_Common.h:185
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
EVAS_API Evas_Object * evas_object_rectangle_add(Evas *e)
Adds a rectangle to the given evas.
Definition: evas_object_rectangle.c:78
Evas_Text_Style_Type
Types of styles to be applied on text objects.
Definition: Evas_Legacy.h:6364
EVAS_API Evas_Text_Style_Type evas_object_text_style_get(const Evas_Text *obj)
Controls the style to apply on the given text object.
Definition: evas_text_eo.legacy.c:69
@ EVAS_TEXT_STYLE_SOFT_OUTLINE
text with a soft outline
Definition: Evas_Legacy.h:6368
@ EVAS_TEXT_STYLE_FAR_SHADOW
text with (far) shadow underneath
Definition: Evas_Legacy.h:6371
@ EVAS_TEXT_STYLE_OUTLINE_SHADOW
text with both outline and shadow effects
Definition: Evas_Legacy.h:6370
@ EVAS_TEXT_STYLE_SOFT_SHADOW
text with (soft) shadow underneath
Definition: Evas_Legacy.h:6373
@ EVAS_TEXT_STYLE_OUTLINE
text with an outline
Definition: Evas_Legacy.h:6367
@ EVAS_TEXT_STYLE_OUTLINE_SOFT_SHADOW
text with outline and soft shadow effects combined
Definition: Evas_Legacy.h:6372
@ EVAS_TEXT_STYLE_FAR_SOFT_SHADOW
text with (far soft) shadow underneath
Definition: Evas_Legacy.h:6374
@ EVAS_TEXT_STYLE_GLOW
text with a glow effect
Definition: Evas_Legacy.h:6369
@ EVAS_TEXT_STYLE_SHADOW
text with shadow underneath
Definition: Evas_Legacy.h:6366
Key press event.
Definition: Evas_Legacy.h:314
const char * key
The logical key : (eg shift+1 == exclamation)
Definition: Evas_Legacy.h:320