From 524819ebcb58b339e252f7be70cae1a048a078d6 Mon Sep 17 00:00:00 2001 From: onefang Date: Sun, 20 Nov 2011 20:19:03 +0000 Subject: [PATCH] Adding the source for the signal test stuff raster and I where using, as another example. git-svn-id: http://svn.enlightenment.org/svn/e/trunk/edje@65460 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33 --- src/examples/sigtest.c | 202 +++++++++++++++++++++++++++++++ src/examples/sigtest.edc | 303 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 505 insertions(+) create mode 100644 src/examples/sigtest.c create mode 100644 src/examples/sigtest.edc diff --git a/src/examples/sigtest.c b/src/examples/sigtest.c new file mode 100644 index 0000000..8596cd6 --- /dev/null +++ b/src/examples/sigtest.c @@ -0,0 +1,202 @@ +#ifdef HAVE_CONFIG_H +#include "config.h" +#else +#define PACKAGE_EXAMPLES_DIR "." +#define __UNUSED__ +#endif + +#include +#include +#include +#include + +#define WIDTH (300) +#define HEIGHT (300) + +static const char *border_img_path = PACKAGE_EXAMPLES_DIR "/red.png"; +static const char *edje_file_path = PACKAGE_EXAMPLES_DIR "/sigtest.edj"; + +static Ecore_Evas *ee; +static Evas_Object *edje_obj; + +static const char commands[] = \ + "commands are:\n" + "\te - change te edje base\n" + "\tl - change to lua base\n" + "\tm - send message\n" + "\ts - send signal\n" + "\th - print help\n"; + +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, "e") == 0) + { + if (!edje_object_file_set(edje_obj, edje_file_path, "plain/edje/group")) + { + int err = edje_object_load_error_get(edje_obj); + const char *errmsg = edje_load_error_str(err); + + fprintf(stderr, "Could not load 'plain/edje/group' from sigtest.edj: %s\n", errmsg); + } + else + fprintf(stdout, "Loaded Edje object bound to group 'plain/edje/group' from" + " file sigtest.edj with success!\n"); + return; + } + + if (strcmp(ev->keyname, "l") == 0) + { + if (!edje_object_file_set(edje_obj, edje_file_path, "lua_base")) + { + int err = edje_object_load_error_get(edje_obj); + const char *errmsg = edje_load_error_str(err); + + fprintf(stderr, "Could not load 'lua_base' from sigtest.edj: %s\n", errmsg); + } + else + fprintf(stdout, "Loaded Edje object bound to group 'lua_base' from" + " file sigtest.edj with success!\n"); + return; + } + + if (strcmp(ev->keyname, "m") == 0) + { + Edje_Message_String *msg = malloc(sizeof(*msg)); + + fprintf(stdout, "\n"); + msg->str = strdup("C message text"); + edje_object_message_send(edje_obj, EDJE_MESSAGE_STRING, 2, msg); + free(msg); + fprintf(stdout, "C message sent\n"); + return; + } + + if (strcmp(ev->keyname, "s") == 0) + { + fprintf(stdout, "\n"); + edje_object_signal_emit(edje_obj, "C signal 1", "Csource"); + edje_object_signal_emit(edje_obj, "bubbles_lua:C signal 2", "Csource"); + fprintf(stdout, "C signal sent\n"); + return; + } +} + +static void +_on_message(void *data, Evas_Object *obj, Edje_Message_Type type, int id, void *msg) +{ + fprintf(stdout, "C::message type=%d id=%d\n", type, id); +} + +static void +_on_signal(void *data, Evas_Object *obj __UNUSED__, const char *emission, const char *source) +{ + fprintf(stdout, "C::signal sig=|%s| src=|%s|\n", emission, source); +} + +static void +_on_delete(Ecore_Evas *ee __UNUSED__) +{ + ecore_main_loop_quit(); +} + +int +main(void) +{ + Evas_Object *border, *bg; + Evas *evas; + + if (!ecore_evas_init()) + return EXIT_FAILURE; + + if (!edje_init()) + 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_callback_delete_request_set(ee, _on_delete); + ecore_evas_title_set(ee, "Signals and wessages tester"); + ecore_evas_show(ee); + + evas = ecore_evas_get(ee); + + bg = evas_object_rectangle_add(evas); + 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 */ + evas_object_show(bg); + ecore_evas_object_associate(ee, bg, ECORE_EVAS_OBJECT_ASSOCIATE_BASE); + + evas_object_focus_set(bg, EINA_TRUE); + evas_object_event_callback_add( + bg, EVAS_CALLBACK_KEY_DOWN, _on_keydown, NULL); + + edje_obj = edje_object_add(evas); + + edje_object_message_handler_set(edje_obj, _on_message, NULL); + edje_object_signal_callback_add(edje_obj, "*", "*", _on_signal, NULL); + + if (!edje_object_file_set(edje_obj, edje_file_path, "lua_base")) + { + int err = edje_object_load_error_get(edje_obj); + const char *errmsg = edje_load_error_str(err); + fprintf(stderr, "Could not load 'lua_base' from sigtest.edj: %s\n", + errmsg); + + evas_object_del(edje_obj); + goto error_edj; + } + + fprintf(stdout, "Loaded Edje object bound to group 'lua_base' from" + " file sigtest.edj with success!\n"); + + evas_object_move(edje_obj, 20, 20); + evas_object_resize(edje_obj, WIDTH - 40, HEIGHT - 40); + evas_object_show(edje_obj); + + /* this is a border around the Edje object above, here just to + * emphasize its geometry */ + border = evas_object_image_filled_add(evas); + evas_object_image_file_set(border, border_img_path, NULL); + evas_object_image_border_set(border, 2, 2, 2, 2); + evas_object_image_border_center_fill_set(border, EVAS_BORDER_FILL_NONE); + + evas_object_resize(border, WIDTH - 40 + 4, HEIGHT - 40 + 4); + evas_object_move(border, 20 - 2, 20 - 2); + evas_object_show(border); + + ecore_main_loop_begin(); + + ecore_evas_free(ee); + ecore_evas_shutdown(); + edje_shutdown(); + 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"); + ecore_evas_shutdown(); + return -1; + +error_edj: + fprintf(stderr, "Failed to load sigtest.edj!\n"); + + ecore_evas_shutdown(); + return -2; +} diff --git a/src/examples/sigtest.edc b/src/examples/sigtest.edc new file mode 100644 index 0000000..ef2a854 --- /dev/null +++ b/src/examples/sigtest.edc @@ -0,0 +1,303 @@ +fonts { + font: "Vera.ttf" "default"; +} + +images { + image: "bubble.png" COMP; +} + +collections { + group { + name: "lua_base"; + lua_script_only: 1; + lua_script { + --// stick object private/local vars here + local D; + local text_geom; + + --// Functions to print tables. + local print_table, print_table_start; + + function print_table_start(table, space, name) + print(space .. name .. ": "); + print(space .. "{"); + print_table(table, space .. " "); + print(space .. "}"); + end + + function print_table(table, space) + for k, v in pairs(table) do + if type(v) == "table" then + print_table_start(v, space, k); + elseif type(v) == "string" then + print(space .. k .. ': "' .. v .. '";') + else + print(space .. k .. ": " .. v .. ";") + end + end + end + + + --// init object here + D = {}; --// data is empty table to start + edje_geom = edje.geom(); + + D.edje = edje.edje(); + D.edje:file("plain/edje/group"); + D.edje:move(0, 0); + D.edje:resize(edje_geom.w, edje_geom.h); + D.edje:show(); + + edje.text_class("test_text_class", "Sans:style=Bold", 10); + + --// send some random edje message + edje.messagesend(7, "none" ); + edje.messagesend(7, "sig", "lua message signal", "luaSource"); + edje.messagesend(7, "str", "hello world"); + edje.messagesend(7, "int", 987); + edje.messagesend(7, "float", 987.321); + edje.messagesend(7, "strset", {"hello", "there", "world"}); + edje.messagesend(7, "intset", {1, 2, 3}); + edje.messagesend(7, "floatset", {1.1, 2.2, 3.3}); + edje.messagesend(7, "strint", "hello world", 7); + edje.messagesend(7, "strfloat", "hello world", 7.654); + edje.messagesend(7, "strintset","hello world", {1, 2, 3}); + --// and a signal + edje.emit("lua signal", "luaSource"); + + + function move (x, y) + print("lua::move x=" .. x .. " x=" .. y); + D.edje:move(0, 0); + end + + function resize (w, h) + print("lua::resize w=" .. w .. " h=" .. h); + D.edje:resize(w, h); + end + + function message (id, type, ...) + print("lua::message id=" .. id .. " type=" .. type); + --// handle your message type here. check id + type then use the + --// vararg appropriately. they are the same as the params passed + --// to edje:messagesend() (if any are passed at all). Any array + --// arguments are passed as a single table. + + if ("none" == type) then + print("lua::message no args"); + elseif ("strset" == type) then + strs = ... ; + print_table_start(strs, "", "lua::message strings"); + elseif ("intset" == type) then + ints = ... ; + print_table_start(ints, "", "lua::message ints"); + elseif ("floatset" == type) then + floats = ... ; + print_table_start(floats, "", "lua::message floats"); + elseif ("strintset" == type) then + str, ints = ... ; + print("lua::message " .. str); + print_table_start(ints, "", "lua::message ints"); + elseif ("strfloatset" == type) then + str, floats = ... ; + print("lua::message " .. str); + print_table_start(floats, "", "lua::message floats"); + else + print("lua::message " .. ... ); + end + end + + function signal (sig, src) + print("lua::signal sig=|" .. sig .. "| src=" .. src .. "|"); + end + } + } + + // The group name NEEDS a / in it, + // or the part below that tries to swallow it won't work. + // Leaving just the lua part visible. + group { + name: "bubbles/lua"; + lua_script_only: 1; + lua_script { + local bubbles = { }; + local bubbleCols = 8; + local bubbleRows = 6; + + --// Functions to print tables. + local print_table, print_table_start; + + function print_table_start(table, space, name) + print(space .. name .. ": "); + print(space .. "{"); + print_table(table, space .. " "); + print(space .. "}"); + end + + function print_table(table, space) + for k, v in pairs(table) do + if type(v) == "table" then + print_table_start(v, space, k); + elseif type(v) == "string" then + print(space .. k .. ': "' .. v .. '";') + else + print(space .. k .. ": " .. v .. ";") + end + end + end + + + for i = 1, bubbleRows do + row = { }; + for j = 1, bubbleCols do + image = edje.image(); + image:image("bubble.png"); + image:show(); + table.insert(row, image); + end + table.insert(bubbles, row); + end + + function resize (w, h) + for i = 1, bubbleRows do + for j = 1, bubbleCols do + w1 = w / bubbleCols; + h1 = h / bubbleRows; + bubbles[i][j]:geom((j - 1) * w1, (i - 1) * h1, w1, h1); + if ((1 == i) or (1 == j) or (bubbleRows == i) or (bubbleCols == j)) then + bubbles[i][j]:color(0, 255, 0, 200); + else + bubbles[i][j]:color(math.random(200) + 55, 0, math.random(255) + 55, 200); + end + end + end + end + + function message (id, type, ...) + print("bubbles::message id=" .. id .. " type=" .. type); + --// handle your message type here. check id + type then use the + --// vararg appropriately. they are the same as the params passed + --// to edje:messagesend() (if any are passed at all). Any array + --// arguments are passed as a single table. + + if ("none" == type) then + print("bubbles::message no args"); + elseif ("strset" == type) then + strs = ... ; + print_table_start(strs, "", "bubbles::message strings"); + elseif ("intset" == type) then + ints = ... ; + print_table_start(ints, "", "bubbles::message ints"); + elseif ("floatset" == type) then + floats = ... ; + print_table_start(floats, "", "bubbles::message floats"); + elseif ("strintset" == type) then + str, ints = ... ; + print("bubbles::message " .. str); + print_table_start(ints, "", "bubbles::message ints"); + elseif ("strfloatset" == type) then + str, floats = ... ; + print("bubbles::message " .. str); + print_table_start(floats, "", "bubbles::message floats"); + else + print("bubbles::message " .. ... ); + end + end + + function signal (sig, src) + print("bubbles::signal sig=|" .. sig .. "| src=|" .. src .. "|"); + end + } + } + + group { + name: "plain/edje/group"; + parts { + part { + name: "background"; + type: RECT; + mouse_events: 0; + description { + state: "default" 0.0; + color: 0 0 0 255; + } + } + + // A lua group embedded in an edje group. + part { + name: "bubbles_lua"; + type: GROUP; + source: "bubbles/lua"; + mouse_events: 0; + description { state: "default" 0.0; visible: 1; } + } + + part { + name: "some_text"; + type: TEXT; + mouse_events: 0; + description { + state: "default" 0; + visible: 1; + text + { + text: "This is test text."; + text_class: "test_text_class"; + } + } + } + + program { name: "show_signals"; + signal: "*"; + source: "*"; + script + { + new buf[128]; + + snprintf(buf, 128, "edje::signal sig=%s sig=%s", sig, src); + set_text(PART:"some_text", buf); + } + } + + script { + public global_str0; + public global_str1; + public global_str2; + public str_idx; + + public set_text_string() { + new tmp[1024]; + new idx; + idx = get_int(str_idx); + + if (idx == 0) + get_str(global_str0, tmp, 1024); + else if (idx == 1) + get_str(global_str1, tmp, 1024); + else if (idx == 2) + get_str(global_str2, tmp, 1024); + else return; + + set_text(PART:"some_text", tmp); + send_message(MSG_STRING, 1, tmp); + } + + public message(Msg_Type:type, id, ...) { + if (type == MSG_STRING) { + new text[64]; + new buf[128]; + + getsarg(3, text, 64); + snprintf(buf, 128, "embryo::message |%s|", text); + set_text(PART:"some_text", buf); + } + } + + + } + + } + } + +} + -- 2.7.4