tizen 2.4 release
[framework/uifw/elementary.git] / src / bin / test_win_plug.c
1 #ifdef HAVE_CONFIG_H
2 # include "elementary_config.h"
3 #endif
4 #include <Elementary.h>
5
6 #define MAX_TRY 40
7
8 static int try_num = 0;
9
10 static void
11 _timer_del(void *data       EINA_UNUSED,
12            Evas *e          EINA_UNUSED,
13            Evas_Object     *obj,
14            void *event_info EINA_UNUSED)
15 {
16    Ecore_Timer *timer = evas_object_data_del(obj, "test-timer");
17    if (!timer) return;
18    ecore_timer_del(timer);
19 }
20
21 static Eina_Bool
22 cb_plug_connect(void *data)
23 {
24    Evas_Object *obj = data;
25    Ecore_Timer *timer;
26
27    if (!obj) return ECORE_CALLBACK_CANCEL;
28
29    try_num++;
30    if (try_num > MAX_TRY) return ECORE_CALLBACK_CANCEL;
31
32    timer= evas_object_data_get(obj, "test-timer");
33    if (!timer) return ECORE_CALLBACK_CANCEL;
34
35    if (elm_plug_connect(obj, "ello", 0, EINA_FALSE))
36      {
37         printf("plug connect to server[ello]\n");
38         evas_object_data_del(obj, "test-timer");
39         return ECORE_CALLBACK_CANCEL;
40      }
41
42    ecore_timer_interval_set(timer, 1);
43    return ECORE_CALLBACK_RENEW;
44 }
45
46 static void
47 cb_plug_disconnected(void *data EINA_UNUSED,
48                     Evas_Object *obj,
49                     void *event_info EINA_UNUSED)
50 {
51    Ecore_Timer *timer = evas_object_data_get(obj, "test-timer");
52    if (timer)
53      {
54         ecore_timer_del(timer);
55         evas_object_data_del(obj, "test-timer");
56      }
57
58    timer = ecore_timer_add(1, cb_plug_connect, obj);
59    evas_object_data_set(obj, "test-timer", timer);
60 }
61
62 static void
63 cb_plug_resized(void *data EINA_UNUSED,
64                 Evas_Object *obj EINA_UNUSED,
65                 void *event_info)
66 {
67    Evas_Coord_Size *size = event_info;
68    printf("server image resized to %dx%d\n", size->w, size->h);
69 }
70
71 static void
72 cb_mouse_down(void *data EINA_UNUSED, Evas *evas EINA_UNUSED, Evas_Object *obj, void *event_info)
73 {
74    Evas_Event_Mouse_Down *ev = event_info;
75
76    if (ev->button == 1) elm_object_focus_set(obj, EINA_TRUE);
77 }
78
79 static void
80 cb_mouse_move(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj, void *event_info)
81 {
82    Evas_Event_Mouse_Move *ev = event_info;
83    Evas_Object *orig = data;
84    Evas_Coord x, y;
85    Evas_Map *p;
86    int i, w, h;
87
88    if (!ev->buttons) return;
89    evas_object_geometry_get(obj, &x, &y, NULL, NULL);
90    evas_object_move(obj,
91                     x + (ev->cur.canvas.x - ev->prev.output.x),
92                     y + (ev->cur.canvas.y - ev->prev.output.y));
93    evas_object_image_size_get(orig, &w, &h);
94    p = evas_map_new(4);
95    evas_object_map_enable_set(orig, EINA_TRUE);
96    evas_object_raise(orig);
97    for (i = 0; i < 4; i++)
98      {
99         Evas_Object *hand;
100         char key[32];
101
102         snprintf(key, sizeof(key), "h-%i\n", i);
103         hand = evas_object_data_get(orig, key);
104         evas_object_raise(hand);
105         evas_object_geometry_get(hand, &x, &y, NULL, NULL);
106         x += 15;
107         y += 15;
108         evas_map_point_coord_set(p, i, x, y, 0);
109         if (i == 0) evas_map_point_image_uv_set(p, i, 0, 0);
110         else if (i == 1) evas_map_point_image_uv_set(p, i, w, 0);
111         else if (i == 2) evas_map_point_image_uv_set(p, i, w, h);
112         else if (i == 3) evas_map_point_image_uv_set(p, i, 0, h);
113      }
114    evas_object_map_set(orig, p);
115    evas_map_free(p);
116 }
117
118 static void
119 create_handles(Evas_Object *obj)
120 {
121    int i;
122    Evas_Coord x, y, w, h;
123
124    evas_object_geometry_get(obj, &x, &y, &w, &h);
125    for (i = 0; i < 4; i++)
126      {
127         Evas_Object *hand;
128         char buf[PATH_MAX];
129         char key[32];
130
131         hand = evas_object_image_filled_add(evas_object_evas_get(obj));
132         evas_object_resize(hand, 31, 31);
133         snprintf(buf, sizeof(buf), "%s/images/pt.png", elm_app_data_dir_get());
134         evas_object_image_file_set(hand, buf, NULL);
135         if (i == 0)      evas_object_move(hand, x     - 15, y     - 15);
136         else if (i == 1) evas_object_move(hand, x + w - 15, y     - 15);
137         else if (i == 2) evas_object_move(hand, x + w - 15, y + h - 15);
138         else if (i == 3) evas_object_move(hand, x     - 15, y + h - 15);
139         evas_object_event_callback_add(hand, EVAS_CALLBACK_MOUSE_MOVE, cb_mouse_move, obj);
140         evas_object_show(hand);
141         snprintf(key, sizeof(key), "h-%i\n", i);
142         evas_object_data_set(obj, key, hand);
143      }
144 }
145
146 void
147 test_win_plug(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
148 {
149    Evas_Object *win, *bg, *plug;
150    char buf[PATH_MAX];
151
152    win = elm_win_add(NULL, "window-plug", ELM_WIN_BASIC);
153    elm_win_title_set(win, "Window Plug");
154    elm_win_autodel_set(win, EINA_TRUE);
155
156    bg = elm_bg_add(win);
157    snprintf(buf, sizeof(buf), "%s/images/plant_01.jpg", elm_app_data_dir_get());
158    elm_bg_file_set(bg, buf, NULL);
159    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
160    elm_win_resize_object_add(win, bg);
161    evas_object_show(bg);
162
163    plug = elm_plug_add(win);
164    evas_object_event_callback_add(elm_plug_image_object_get(plug), EVAS_CALLBACK_MOUSE_DOWN, cb_mouse_down, NULL);
165    evas_object_event_callback_add(plug, EVAS_CALLBACK_DEL, _timer_del, NULL);
166    if (!elm_plug_connect(plug, "ello", 0, EINA_FALSE))
167      {
168         printf("Cannot connect plug\n");
169         return;
170      }
171
172    evas_object_smart_callback_add(plug, "image,deleted", cb_plug_disconnected, NULL);
173    evas_object_smart_callback_add(plug, "image,resized", cb_plug_resized, NULL);
174
175    evas_object_resize(plug, 380, 500);
176    evas_object_move(plug, 10, 10);
177    evas_object_show(plug);
178
179    create_handles(elm_plug_image_object_get(plug));
180
181    evas_object_resize(win, 400, 600);
182    evas_object_show(win);
183 }