Git init
[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, "KP_Enter", mask, ~mask, 0);
73    if (!kg)
74       fprintf(stderr,"ERROR: unable to redirect \"KP_Enter\" key events to object %p.\n", o);
75    mask = 0;
76    kg = evas_object_key_grab(o, "space", mask, ~mask, 0);
77    if (!kg)
78       fprintf(stderr,"ERROR: unable to redirect \"space\" 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, void (*func) (void *data, E_Dialog *dia), 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    e_util_icon_theme_set(dia->icon_object, icon);
169    edje_extern_object_min_size_set(dia->icon_object, size * e_scale, size * e_scale);
170    edje_object_part_swallow(dia->bg_object, "e.swallow.icon", dia->icon_object);
171    edje_object_signal_emit(dia->bg_object, "e,state,icon", "e");
172    evas_object_show(dia->icon_object);
173 }
174
175 EAPI void 
176 e_dialog_border_icon_set(E_Dialog *dia, const char *icon) 
177 {
178    E_Border *border;
179    
180    border = dia->win->border;
181    if (!border) return;
182    if (border->internal_icon)
183      {
184         eina_stringshare_del(border->internal_icon);
185         border->internal_icon = NULL;
186      }
187    if (icon)
188      border->internal_icon = eina_stringshare_add(icon);
189 }
190
191 EAPI void
192 e_dialog_content_set(E_Dialog *dia, Evas_Object *obj, Evas_Coord minw, Evas_Coord minh)
193 {
194    dia->content_object = obj;
195    e_widget_on_focus_hook_set(obj, _e_dialog_cb_wid_on_focus, dia);
196    edje_extern_object_min_size_set(obj, minw, minh);
197    edje_object_part_swallow(dia->bg_object, "e.swallow.content", obj);
198    evas_object_show(obj);
199 }
200
201 EAPI void
202 e_dialog_resizable_set(E_Dialog *dia, int resizable)
203 {
204    dia->resizable = resizable;
205    if (dia->win)
206      {
207         if (resizable)
208           {
209              e_win_size_max_set(dia->win, 99999, 99999);
210              e_util_win_auto_resize_fill(dia->win);
211              edje_object_signal_emit(dia->bg_object, "e,state,resizeble", "e");
212           }
213         else
214           {
215              e_win_resize(dia->win, dia->min_w, dia->min_h);
216              e_win_size_max_set(dia->win, dia->min_w, dia->min_h);
217              edje_object_signal_emit(dia->bg_object, "e,state,no_resizeble", "e");
218           }
219      }
220 }
221
222 EAPI void
223 e_dialog_show(E_Dialog *dia)
224 {
225    Evas_Coord mw, mh;
226    Evas_Object *o;
227    
228    o = dia->text_object;
229    if (o)
230      {
231         edje_object_size_min_calc(o, &mw, &mh);
232         edje_extern_object_min_size_set(o, mw, mh);
233         edje_object_part_swallow(dia->bg_object, "e.swallow.content", o);
234      }
235
236    o = dia->box_object;
237    e_widget_size_min_get(o, &mw, &mh);
238    edje_extern_object_min_size_set(o, mw, mh);
239    edje_object_part_swallow(dia->bg_object, "e.swallow.buttons", o);
240    
241    edje_object_size_min_calc(dia->bg_object, &mw, &mh);
242    e_win_resize(dia->win, mw, mh);
243    e_win_size_min_set(dia->win, mw, mh);
244    dia->min_w = mw;
245    dia->min_h = mh;
246    if (!dia->resizable) e_win_size_max_set(dia->win, mw, mh);
247    else
248      {
249         e_win_size_max_set(dia->win, 99999, 99999);
250         e_util_win_auto_resize_fill(dia->win);
251      }
252    e_win_show(dia->win);
253    
254    if (!e_widget_focus_get(dia->box_object))
255      e_widget_focus_set(dia->box_object, 1);
256 }
257
258 /* local subsystem functions */
259 static void
260 _e_dialog_free(E_Dialog *dia)
261 {
262    if (dia->buttons) eina_list_free(dia->buttons);
263    if (dia->text_object) evas_object_del(dia->text_object);
264    if (dia->icon_object) evas_object_del(dia->icon_object);
265    if (dia->box_object) evas_object_del(dia->box_object);
266    if (dia->bg_object) evas_object_del(dia->bg_object);
267    if (dia->content_object) evas_object_del(dia->content_object);
268    if (dia->event_object) evas_object_del(dia->event_object);
269    e_object_del(E_OBJECT(dia->win));
270    free(dia);
271 }
272
273 static void
274 _e_dialog_del_func_cb(void *data __UNUSED__, E_Dialog *dia)
275 {
276    e_util_defer_object_del(E_OBJECT(dia));
277 }
278
279 static void
280 _e_dialog_cb_key_down(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event)
281 {
282    Evas_Event_Key_Down *ev;
283    E_Dialog *dia;
284
285    ev = event;
286    dia = data;
287    if (!strcmp(ev->keyname, "Tab"))
288      {
289         if (evas_key_modifier_is_set(evas_key_modifier_get(e_win_evas_get(dia->win)), "Shift"))
290           {
291              if (e_widget_focus_get(dia->box_object))
292                {
293                   if (!e_widget_focus_jump(dia->box_object, 0))
294                     {
295                        if (dia->text_object)
296                          e_widget_focus_set(dia->box_object, 0);
297                        else
298                          {
299                             e_widget_focus_set(dia->content_object, 0);
300                             if (!e_widget_focus_get(dia->content_object))
301                               e_widget_focus_set(dia->box_object, 0);
302                          }
303                     }
304                }
305              else
306                {
307                   if (!e_widget_focus_jump(dia->content_object, 0))
308                     e_widget_focus_set(dia->box_object, 0);
309                }
310           }
311         else
312           {
313              if (e_widget_focus_get(dia->box_object))
314                {
315                   if (!e_widget_focus_jump(dia->box_object, 1))
316                     {
317                        if (dia->text_object)
318                          e_widget_focus_set(dia->box_object, 1);
319                        else
320                          {
321                             e_widget_focus_set(dia->content_object, 1);
322                             if (!e_widget_focus_get(dia->content_object))
323                               e_widget_focus_set(dia->box_object, 1);
324                          }
325                     }
326                }
327              else
328                {
329                   if (!e_widget_focus_jump(dia->content_object, 1))
330                     e_widget_focus_set(dia->box_object, 1);
331                }
332           }
333      }
334    else if (((!strcmp(ev->keyname, "Return")) || 
335              (!strcmp(ev->keyname, "KP_Enter")) || 
336              (!strcmp(ev->keyname, "space"))))
337      {
338         Evas_Object *o = NULL;
339         
340         if ((dia->content_object) && (e_widget_focus_get(dia->content_object)))
341           o = e_widget_focused_object_get(dia->content_object);
342         else
343           o = e_widget_focused_object_get(dia->box_object);
344         if (o) e_widget_activate(o);
345      }
346 }
347
348 static void
349 _e_dialog_cb_delete(E_Win *win)
350 {
351    E_Dialog *dia;
352    
353    dia = win->data;
354    e_object_del(E_OBJECT(dia));
355 }
356
357 static void
358 _e_dialog_cb_resize(E_Win *win)
359 {
360    E_Dialog *dia;
361    
362    dia = win->data;
363    evas_object_resize(dia->bg_object, dia->win->w, dia->win->h);
364 }
365
366 static void
367 _e_dialog_cb_wid_on_focus(void *data, Evas_Object *obj)
368 {
369    E_Dialog *dia;
370    
371    dia = data;
372    if (obj == dia->content_object)
373      e_widget_focused_object_clear(dia->box_object);
374    else if (dia->content_object)
375      e_widget_focused_object_clear(dia->content_object);
376 }
377