aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/elementary/test_win_plug.c
blob: 615e6fa8d5e893abbb723d5032c76e56b49cc106 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#include <Elementary.h>
// FIXME: use smart cb
#include "elm_notify_eo.h"

#define MAX_TRY 40

static int try_num = 0;

static void
_timer_del(void *data       EINA_UNUSED,
           Evas *e          EINA_UNUSED,
           Evas_Object     *obj,
           void *event_info EINA_UNUSED)
{
   Ecore_Timer *timer = evas_object_data_del(obj, "test-timer");
   if (!timer) return;
   ecore_timer_del(timer);
}

static Eina_Bool
cb_plug_connect(void *data)
{
   Evas_Object *obj = data;
   Ecore_Timer *timer;

   if (!obj) return ECORE_CALLBACK_CANCEL;

   try_num++;
   if (try_num > MAX_TRY) return ECORE_CALLBACK_CANCEL;

   timer= evas_object_data_get(obj, "test-timer");
   if (!timer) return ECORE_CALLBACK_CANCEL;

   if (elm_plug_connect(obj, "ello", 0, EINA_FALSE))
     {
        printf("plug connect to server[ello]\n");
        evas_object_data_del(obj, "test-timer");
        return ECORE_CALLBACK_CANCEL;
     }

   ecore_timer_interval_set(timer, 1);
   return ECORE_CALLBACK_RENEW;
}

static void
cb_plug_disconnected(void *data EINA_UNUSED,
                    Evas_Object *obj,
                    void *event_info EINA_UNUSED)
{
   Ecore_Timer *timer = evas_object_data_get(obj, "test-timer");
   if (timer)
     {
        ecore_timer_del(timer);
        evas_object_data_del(obj, "test-timer");
     }

   timer = ecore_timer_add(1, cb_plug_connect, obj);
   evas_object_data_set(obj, "test-timer", timer);
}

static void
cb_plug_resized(void *data EINA_UNUSED,
                Evas_Object *obj EINA_UNUSED,
                void *event_info)
{
   Evas_Coord_Size *size = event_info;
   printf("server image resized to %dx%d\n", size->w, size->h);
}

static void
cb_mouse_down(void *data EINA_UNUSED, Evas *evas EINA_UNUSED, Evas_Object *obj, void *event_info)
{
   Evas_Event_Mouse_Down *ev = event_info;

   if (ev->button == 1) elm_object_focus_set(obj, EINA_TRUE);
}

static void
cb_mouse_move(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj, void *event_info)
{
   Evas_Event_Mouse_Move *ev = event_info;
   Evas_Object *orig = data;
   Evas_Coord x, y;
   Evas_Map *p;
   int i, w, h;

   if (!ev->buttons) return;
   evas_object_geometry_get(obj, &x, &y, NULL, NULL);
   evas_object_move(obj,
                    x + (ev->cur.canvas.x - ev->prev.output.x),
                    y + (ev->cur.canvas.y - ev->prev.output.y));
   evas_object_image_size_get(orig, &w, &h);
   p = evas_map_new(4);
   evas_object_map_enable_set(orig, EINA_TRUE);
   evas_object_raise(orig);
   for (i = 0; i < 4; i++)
     {
        Evas_Object *hand;
        char key[32];

        snprintf(key, sizeof(key), "h-%i\n", i);
        hand = evas_object_data_get(orig, key);
        evas_object_raise(hand);
        evas_object_geometry_get(hand, &x, &y, NULL, NULL);
        x += 15;
        y += 15;
        evas_map_point_coord_set(p, i, x, y, 0);
        if (i == 0) evas_map_point_image_uv_set(p, i, 0, 0);
        else if (i == 1) evas_map_point_image_uv_set(p, i, w, 0);
        else if (i == 2) evas_map_point_image_uv_set(p, i, w, h);
        else if (i == 3) evas_map_point_image_uv_set(p, i, 0, h);
     }
   evas_object_map_set(orig, p);
   evas_map_free(p);
}

static void
create_handles(Evas_Object *obj)
{
   int i;
   Evas_Coord x, y, w, h;

   evas_object_geometry_get(obj, &x, &y, &w, &h);
   for (i = 0; i < 4; i++)
     {
        Evas_Object *hand;
        char buf[PATH_MAX];
        char key[32];

        hand = evas_object_image_filled_add(evas_object_evas_get(obj));
        evas_object_resize(hand, 31, 31);
        snprintf(buf, sizeof(buf), "%s/images/pt.png", elm_app_data_dir_get());
        evas_object_image_file_set(hand, buf, NULL);
        if (i == 0)      evas_object_move(hand, x     - 15, y     - 15);
        else if (i == 1) evas_object_move(hand, x + w - 15, y     - 15);
        else if (i == 2) evas_object_move(hand, x + w - 15, y + h - 15);
        else if (i == 3) evas_object_move(hand, x     - 15, y + h - 15);
        evas_object_event_callback_add(hand, EVAS_CALLBACK_MOUSE_MOVE, cb_mouse_move, obj);
        evas_object_show(hand);
        snprintf(key, sizeof(key), "h-%i\n", i);
        evas_object_data_set(obj, key, hand);
     }
}

static void
_notify_end(void *data EINA_UNUSED, const Efl_Event *event)
{
   efl_del(event->object);
   efl_event_callback_stop(event->object);
}

static inline void
_notify_error(Evas_Object *parent, const char *msg)
{
   Evas_Object *notif, *txt;

   printf("%s\n", msg);

   notif = elm_notify_add(parent);
   evas_object_size_hint_weight_set(notif, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
   elm_notify_align_set(notif, 0.5, 1.0);
   elm_notify_timeout_set(notif, 3.0);
   efl_event_callback_add(notif, ELM_NOTIFY_EVENT_DISMISSED, _notify_end, NULL);

   txt = elm_label_add(notif);
   elm_object_text_set(txt, msg);
   elm_object_content_set(notif, txt);

   evas_object_show(txt);
   evas_object_show(notif);
}

void
test_win_plug(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Evas_Object *win, *bg, *plug;
   char buf[PATH_MAX];

   win = elm_win_add(NULL, "window-plug", ELM_WIN_BASIC);
   elm_win_title_set(win, "Window Plug");
   elm_win_autodel_set(win, EINA_TRUE);

   bg = elm_bg_add(win);
   snprintf(buf, sizeof(buf), "%s/images/plant_01.jpg", elm_app_data_dir_get());
   elm_bg_file_set(bg, buf, NULL);
   evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
   elm_win_resize_object_add(win, bg);
   evas_object_show(bg);

   plug = elm_plug_add(win);
   evas_object_event_callback_add(elm_plug_image_object_get(plug), EVAS_CALLBACK_MOUSE_DOWN, cb_mouse_down, NULL);
   evas_object_event_callback_add(plug, EVAS_CALLBACK_DEL, _timer_del, NULL);
   if (!elm_plug_connect(plug, "ello", 0, EINA_FALSE))
     {
        _notify_error(data, "Unable to connect to the Window Socket!");
        evas_object_del(win);
        return;
     }

   evas_object_smart_callback_add(plug, "image,deleted", cb_plug_disconnected, NULL);
   evas_object_smart_callback_add(plug, "image,resized", cb_plug_resized, NULL);

   evas_object_resize(plug, 380, 500);
   evas_object_move(plug, 10, 10);
   evas_object_show(plug);

   create_handles(elm_plug_image_object_get(plug));

   evas_object_resize(win, 400, 600);
   evas_object_show(win);
}