Tizen 2.1 release
[platform/core/uifw/e17.git] / src / bin / e_dialog.c
1 #include "e.h"
2
3 /* local subsystem functions */
4 static void _e_dialog_free(E_Dialog *dia);
5 static void _e_dialog_del_func_cb(void *data, E_Dialog *dia);
6 static void _e_dialog_cb_delete(E_Win *win);
7 static void _e_dialog_cb_resize(E_Win *win);
8 static void _e_dialog_cb_key_down(void *data, Evas *e, Evas_Object *obj, void *event);
9 static void _e_dialog_cb_wid_on_focus(void *data, Evas_Object *obj);
10
11 /* local subsystem globals */
12
13 /* externally accessible functions */
14
15 static E_Dialog *
16 _e_dialog_internal_new(E_Container *con, const char *name, const char *class, int dialog)
17 {
18    E_Dialog *dia;
19    E_Manager *man;
20    Evas_Object *o;
21    Evas_Modifier_Mask mask;
22    Eina_Bool kg;
23
24    if (!con)
25      {
26         man = e_manager_current_get();
27         if (!man) return NULL;
28         con = e_container_current_get(man);
29         if (!con) con = e_container_number_get(man, 0);
30         if (!con) return NULL;
31      }
32    dia = E_OBJECT_ALLOC(E_Dialog, E_DIALOG_TYPE, _e_dialog_free);
33    if (!dia) return NULL;
34    dia->win = e_win_new(con);
35    if (!dia->win)
36      {
37         free(dia);
38         return NULL;
39      }
40    e_win_delete_callback_set(dia->win, _e_dialog_cb_delete);
41    e_win_resize_callback_set(dia->win, _e_dialog_cb_resize);
42    dia->win->data = dia;
43    if (dialog) e_win_dialog_set(dia->win, 1);
44    e_win_name_class_set(dia->win, name, class);
45    o = edje_object_add(e_win_evas_get(dia->win));
46    dia->bg_object = o;
47    e_theme_edje_object_set(o, "base/theme/dialog",
48                            "e/widgets/dialog/main");
49    evas_object_move(o, 0, 0);
50    evas_object_show(o);
51
52    o = e_widget_list_add(e_win_evas_get(dia->win), 1, 1);
53    e_widget_on_focus_hook_set(o, _e_dialog_cb_wid_on_focus, dia);
54    dia->box_object = o;
55    edje_object_part_swallow(dia->bg_object, "e.swallow.buttons", o);
56
57    o = evas_object_rectangle_add(e_win_evas_get(dia->win));
58    dia->event_object = o;
59    mask = 0;
60    kg = evas_object_key_grab(o, "Tab", mask, ~mask, 0);
61    if (!kg)
62      fprintf(stderr, "ERROR: unable to redirect \"Tab\" key events to object %p.\n", o);
63    mask = evas_key_modifier_mask_get(e_win_evas_get(dia->win), "Shift");
64    kg = evas_object_key_grab(o, "Tab", mask, ~mask, 0);
65    if (!kg)
66      fprintf(stderr, "ERROR: unable to redirect \"Tab\" key events to object %p.\n", o);
67    mask = 0;
68    kg = evas_object_key_grab(o, "Return", mask, ~mask, 0);
69    if (!kg)
70      fprintf(stderr, "ERROR: unable to redirect \"Return\" key events to object %p.\n", o);
71    mask = 0;
72    kg = evas_object_key_grab(o, "Escape", mask, ~mask, 0);
73    if (!kg)
74      fprintf(stderr, "ERROR: unable to redirect \"Escape\" key events to object %p.\n", o);
75    mask = 0;
76    kg = evas_object_key_grab(o, "KP_Enter", mask, ~mask, 0);
77    if (!kg)
78      fprintf(stderr, "ERROR: unable to redirect \"KP_Enter\" key events to object %p.\n", o);
79
80    evas_object_event_callback_add(o, EVAS_CALLBACK_KEY_DOWN, _e_dialog_cb_key_down, dia);
81
82    return dia;
83 }
84
85 EAPI E_Dialog *
86 e_dialog_new(E_Container *con, const char *name, const char *class)
87 {
88    return _e_dialog_internal_new(con, name, class, 1);
89 }
90
91 EAPI E_Dialog *
92 e_dialog_normal_win_new(E_Container *con, const char *name, const char *class)
93 {
94    return _e_dialog_internal_new(con, name, class, 0);
95 }
96
97 EAPI void
98 e_dialog_button_add(E_Dialog *dia, const char *label, const char *icon, E_Dialog_Cb func, void *data)
99 {
100    Evas_Object *o;
101
102    if (!func) func = _e_dialog_del_func_cb;
103    o = e_widget_button_add(e_win_evas_get(dia->win), label, icon, (void (*)(void *, void *))func, data, dia);
104    e_widget_list_object_append(dia->box_object, o, 1, 0, 0.5);
105    dia->buttons = eina_list_append(dia->buttons, o);
106 }
107
108 EAPI int
109 e_dialog_button_focus_num(E_Dialog *dia, int button)
110 {
111    Evas_Object *o;
112
113    o = eina_list_nth(dia->buttons, button);
114    if (o) e_widget_focus_steal(o);
115    return 1;
116 }
117
118 EAPI int
119 e_dialog_button_disable_num_set(E_Dialog *dia, int button, int disabled)
120 {
121    Evas_Object *o;
122
123    o = eina_list_nth(dia->buttons, button);
124    if (o) e_widget_disabled_set(o, disabled);
125    return 1;
126 }
127
128 EAPI int
129 e_dialog_button_disable_num_get(E_Dialog *dia, int button)
130 {
131    Evas_Object *o;
132    int ret = 0;
133
134    o = eina_list_nth(dia->buttons, button);
135    if (o) ret = e_widget_disabled_get(o);
136    return ret;
137 }
138
139 EAPI void
140 e_dialog_title_set(E_Dialog *dia, const char *title)
141 {
142    e_win_title_set(dia->win, title);
143 }
144
145 EAPI void
146 e_dialog_text_set(E_Dialog *dia, const char *text)
147 {
148    if (!dia->text_object)
149      {
150         Evas_Object *o;
151
152         o = edje_object_add(e_win_evas_get(dia->win));
153         dia->text_object = o;
154         e_theme_edje_object_set(o, "base/theme/dialog",
155                                 "e/widgets/dialog/text");
156         edje_object_part_swallow(dia->bg_object, "e.swallow.content", o);
157         evas_object_show(o);
158      }
159    edje_object_part_text_set(dia->text_object, "e.textblock.message", text);
160 }
161
162 EAPI void
163 e_dialog_icon_set(E_Dialog *dia, const char *icon, Evas_Coord size)
164 {
165    if (!icon) return;
166
167    dia->icon_object = e_icon_add(e_win_evas_get(dia->win));
168    if (!e_util_icon_theme_set(dia->icon_object, icon))
169      e_icon_file_edje_set(dia->icon_object, icon, "icon");
170    edje_extern_object_min_size_set(dia->icon_object, size * e_scale, size * e_scale);
171    edje_object_part_swallow(dia->bg_object, "e.swallow.icon", dia->icon_object);
172    edje_object_signal_emit(dia->bg_object, "e,state,icon", "e");
173    evas_object_show(dia->icon_object);
174 }
175
176 EAPI void
177 e_dialog_border_icon_set(E_Dialog *dia, const char *icon)
178 {
179    E_Border *border;
180
181    border = dia->win->border;
182    if (!border) return;
183    if (border->internal_icon)
184      {
185         eina_stringshare_del(border->internal_icon);
186         border->internal_icon = NULL;
187      }
188    if (icon)
189      border->internal_icon = eina_stringshare_add(icon);
190 }
191
192 EAPI void
193 e_dialog_content_set(E_Dialog *dia, Evas_Object *obj, Evas_Coord minw, Evas_Coord minh)
194 {
195    int mw, mh;
196    dia->content_object = obj;
197    e_widget_on_focus_hook_set(obj, _e_dialog_cb_wid_on_focus, dia);
198    edje_extern_object_min_size_set(obj, minw, minh);
199    edje_object_part_swallow(dia->bg_object, "e.swallow.content", obj);
200    edje_object_size_min_calc(dia->bg_object, &mw, &mh);
201    e_win_resize(dia->win, mw, mh);
202    e_win_size_min_set(dia->win, mw, mh);
203    dia->min_w = mw;
204    dia->min_h = mh;
205    evas_object_show(obj);
206 }
207
208 EAPI void
209 e_dialog_resizable_set(E_Dialog *dia, int resizable)
210 {
211    dia->resizable = resizable;
212    if (dia->win)
213      {
214         if (resizable)
215           {
216              e_win_size_max_set(dia->win, 99999, 99999);
217              e_util_win_auto_resize_fill(dia->win);
218              edje_object_signal_emit(dia->bg_object, "e,state,resizeble", "e");
219           }
220         else
221           {
222              e_win_resize(dia->win, dia->min_w, dia->min_h);
223              e_win_size_max_set(dia->win, dia->min_w, dia->min_h);
224              edje_object_signal_emit(dia->bg_object, "e,state,no_resizeble", "e");
225           }
226      }
227 }
228
229 EAPI void
230 e_dialog_parent_set(E_Dialog *dia, E_Win *win)
231 {
232    if (!dia) return;
233    if (win)
234      ecore_x_icccm_transient_for_set(dia->win->evas_win, win->evas_win);
235    else
236      ecore_x_icccm_transient_for_unset(dia->win->evas_win);
237    ecore_evas_modal_set(dia->win->ecore_evas, !!win);
238 }
239
240 EAPI void
241 e_dialog_show(E_Dialog *dia)
242 {
243    Evas_Coord mw, mh;
244    Evas_Object *o;
245
246    o = dia->text_object;
247    if (o)
248      {
249         edje_object_size_min_calc(o, &mw, &mh);
250         edje_extern_object_min_size_set(o, mw, mh);
251         edje_object_part_swallow(dia->bg_object, "e.swallow.content", o);
252      }
253
254    o = dia->box_object;
255    e_widget_size_min_get(o, &mw, &mh);
256    edje_extern_object_min_size_set(o, mw, mh);
257    edje_object_part_swallow(dia->bg_object, "e.swallow.buttons", o);
258
259    edje_object_size_min_calc(dia->bg_object, &mw, &mh);
260    e_win_resize(dia->win, mw, mh);
261    e_win_size_min_set(dia->win, mw, mh);
262    dia->min_w = mw;
263    dia->min_h = mh;
264    if (!dia->resizable) e_win_size_max_set(dia->win, mw, mh);
265    else
266      {
267         e_win_size_max_set(dia->win, 99999, 99999);
268         e_util_win_auto_resize_fill(dia->win);
269      }
270    e_win_show(dia->win);
271
272    if (!e_widget_focus_get(dia->box_object))
273      e_widget_focus_set(dia->box_object, 1);
274 }
275
276 /* local subsystem functions */
277 static void
278 _e_dialog_free(E_Dialog *dia)
279 {
280    if (dia->buttons) eina_list_free(dia->buttons);
281    if (dia->text_object) evas_object_del(dia->text_object);
282    if (dia->icon_object) evas_object_del(dia->icon_object);
283    if (dia->box_object) evas_object_del(dia->box_object);
284    if (dia->bg_object) evas_object_del(dia->bg_object);
285    if (dia->content_object) evas_object_del(dia->content_object);
286    if (dia->event_object) evas_object_del(dia->event_object);
287    e_object_del(E_OBJECT(dia->win));
288    free(dia);
289 }
290
291 static void
292 _e_dialog_del_func_cb(void *data __UNUSED__, E_Dialog *dia)
293 {
294    e_util_defer_object_del(E_OBJECT(dia));
295 }
296
297 static void
298 _e_dialog_cb_key_down(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event)
299 {
300    Evas_Event_Key_Down *ev;
301    E_Dialog *dia;
302
303    ev = event;
304    dia = data;
305    if (!strcmp(ev->keyname, "Tab"))
306      {
307         if (evas_key_modifier_is_set(evas_key_modifier_get(e_win_evas_get(dia->win)), "Shift"))
308           {
309              if (e_widget_focus_get(dia->box_object))
310                {
311                   if (!e_widget_focus_jump(dia->box_object, 0))
312                     {
313                        if (dia->text_object)
314                          e_widget_focus_set(dia->box_object, 0);
315                        else
316                          {
317                             e_widget_focus_set(dia->content_object, 0);
318                             if (!e_widget_focus_get(dia->content_object))
319                               e_widget_focus_set(dia->box_object, 0);
320                          }
321                     }
322                }
323              else
324                {
325                   if (!e_widget_focus_jump(dia->content_object, 0))
326                     e_widget_focus_set(dia->box_object, 0);
327                }
328           }
329         else
330           {
331              if (e_widget_focus_get(dia->box_object))
332                {
333                   if (!e_widget_focus_jump(dia->box_object, 1))
334                     {
335                        if (dia->text_object)
336                          e_widget_focus_set(dia->box_object, 1);
337                        else
338                          {
339                             e_widget_focus_set(dia->content_object, 1);
340                             if (!e_widget_focus_get(dia->content_object))
341                               e_widget_focus_set(dia->box_object, 1);
342                          }
343                     }
344                }
345              else
346                {
347                   if (!e_widget_focus_jump(dia->content_object, 1))
348                     e_widget_focus_set(dia->box_object, 1);
349                }
350           }
351      }
352    else if (((!strcmp(ev->keyname, "Return")) ||
353              (!strcmp(ev->keyname, "KP_Enter"))))
354      {
355         Evas_Object *o = NULL;
356
357         if ((dia->content_object) && (e_widget_focus_get(dia->content_object)))
358           o = e_widget_focused_object_get(dia->content_object);
359         else
360           o = e_widget_focused_object_get(dia->box_object);
361         if (o) e_widget_activate(o);
362      }
363    else if (!strcmp(ev->keyname, "Escape"))
364      e_object_del(E_OBJECT(dia));
365 }
366
367 static void
368 _e_dialog_cb_delete(E_Win *win)
369 {
370    E_Dialog *dia;
371
372    dia = win->data;
373    e_object_del(E_OBJECT(dia));
374 }
375
376 static void
377 _e_dialog_cb_resize(E_Win *win)
378 {
379    E_Dialog *dia;
380
381    dia = win->data;
382    evas_object_resize(dia->bg_object, dia->win->w, dia->win->h);
383 }
384
385 static void
386 _e_dialog_cb_wid_on_focus(void *data, Evas_Object *obj)
387 {
388    E_Dialog *dia;
389
390    dia = data;
391    if (obj == dia->content_object)
392      e_widget_focused_object_clear(dia->box_object);
393    else if (dia->content_object)
394      e_widget_focused_object_clear(dia->content_object);
395 }
396