b66e0db25ef0e8625dcd19bacaa063406b235f8a
[framework/uifw/e17.git] / src / modules / wizard / e_wizard.c
1 #include "e.h"
2 #include "e_mod_main.h"
3
4 static void _e_wizard_next_eval(void);
5 static E_Popup *_e_wizard_main_new(E_Zone *zone);
6 static E_Popup *_e_wizard_extra_new(E_Zone *zone);
7 static void _e_wizard_cb_key_down(void *data, Evas *e, Evas_Object *obj, void *event);
8 static void _e_wizard_cb_next(void *data, Evas_Object *obj, const char *emission, const char *source);
9
10 static E_Popup *pop = NULL;
11 static Eina_List *pops = NULL;
12 static Evas_Object *o_bg = NULL;
13 static Evas_Object *o_content = NULL;
14 static Eina_List *pages = NULL;
15 static E_Wizard_Page *curpage = NULL;
16 static int next_ok = 1;
17 static int next_can = 0;
18 static int next_prev = 0;
19
20 EAPI int
21 e_wizard_init(void)
22 {
23    Eina_List *l;
24
25    for (l = e_manager_list(); l; l = l->next)
26      {
27         E_Manager *man;
28         Eina_List *l2;
29
30         man = l->data;
31         for (l2 = man->containers; l2; l2 = l2->next)
32           {
33              E_Container *con;
34              Eina_List *l3;
35
36              con = l2->data;
37              for (l3 = con->zones; l3; l3 = l3->next)
38                {
39                   E_Zone *zone;
40
41                   zone = l3->data;
42                   if (!pop)
43                     pop = _e_wizard_main_new(zone);
44                   else
45                     pops = eina_list_append(pops, _e_wizard_extra_new(zone));
46                }
47           }
48      }
49    return 1;
50 }
51
52 EAPI int
53 e_wizard_shutdown(void)
54 {
55    if (pop)
56      {
57         e_object_del(E_OBJECT(pop));
58         pop = NULL;
59      }
60    while (pops)
61      {
62         e_object_del(E_OBJECT(pops->data));
63         pops = eina_list_remove_list(pops, pops);
64      }
65    while (pages)
66      {
67         e_wizard_page_del(pages->data);
68      }
69    return 1;
70 }
71
72 EAPI void
73 e_wizard_go(void)
74 {
75    if (!curpage)
76      {
77         if (pages)
78           {
79              curpage = pages->data;
80              if (pages->next) next_can = 1;
81           }
82      }
83    if (curpage)
84      {
85         if ((!curpage->data) && (curpage->init)) curpage->init(curpage);
86         _e_wizard_next_eval();
87         if ((curpage->show) && (!curpage->show(curpage)))
88           {
89              e_wizard_next();
90           }
91      }
92 }
93
94 EAPI void
95 e_wizard_apply(void)
96 {
97    Eina_List *l;
98
99    for (l = pages; l; l = l->next)
100      {  
101         E_Wizard_Page *pg;
102
103         pg = l->data;
104         if (pg->apply) pg->apply(pg);
105      }
106 }
107
108 EAPI void
109 e_wizard_next(void)
110 {
111    Eina_List *l;
112
113    for (l = pages; l; l = l->next)
114      {
115         if (l->data == curpage)
116           {
117              if (l->next)
118                {
119                   if (curpage)
120                     {
121                        if (curpage->hide)
122                          curpage->hide(curpage);
123                     }
124                   curpage = l->next->data;
125                   if (!curpage->data)
126                     {
127                        if (curpage->init)
128                          curpage->init(curpage);
129                     }
130                   next_can = 1;
131                   _e_wizard_next_eval();
132                   if ((curpage->show) && (curpage->show(curpage)))
133                     {
134                        break;
135                     }
136                }
137              else
138                {
139                   /* FINISH */
140                   e_wizard_apply();
141                   e_wizard_shutdown();
142                   return;
143                }
144           }
145      }
146 }
147
148 EAPI void
149 e_wizard_page_show(Evas_Object *obj)
150 {
151    if (o_content) evas_object_del(o_content);
152    o_content = obj;
153    if (obj)
154      {
155         Evas_Coord minw, minh;
156
157         e_widget_size_min_get(obj, &minw, &minh);
158         edje_extern_object_min_size_set(obj, minw, minh);
159         edje_object_part_swallow(o_bg, "e.swallow.content", obj);
160         evas_object_show(obj);
161         e_widget_focus_set(obj, 1);
162         edje_object_signal_emit(o_bg, "e,action,page,new", "e");
163      }
164 }
165
166 EAPI E_Wizard_Page *
167 e_wizard_page_add(void *handle,
168                   int (*init)     (E_Wizard_Page *pg),
169                   int (*shutdown) (E_Wizard_Page *pg),
170                   int (*show)     (E_Wizard_Page *pg),
171                   int (*hide)     (E_Wizard_Page *pg),
172                   int (*apply)    (E_Wizard_Page *pg)
173                   )
174 {
175    E_Wizard_Page *pg;
176
177    pg = E_NEW(E_Wizard_Page, 1);
178    if (!pg) return NULL;
179
180    pg->handle = handle;
181    pg->evas = pop->evas;
182
183    pg->init = init;
184    pg->shutdown = shutdown;
185    pg->show = show;
186    pg->hide = hide;
187    pg->apply = apply;
188
189    pages = eina_list_append(pages, pg);
190
191    return pg;
192 }
193
194 EAPI void
195 e_wizard_page_del(E_Wizard_Page *pg)
196 {
197    if (pg->handle) dlclose(pg->handle);
198    pages = eina_list_remove(pages, pg);
199    free(pg);
200 }
201
202 EAPI void
203 e_wizard_button_next_enable_set(int enable)
204 {
205    next_ok = enable;
206    _e_wizard_next_eval();
207 }
208
209 EAPI void
210 e_wizard_title_set(const char *title)
211 {
212    edje_object_part_text_set(o_bg, "e.text.title", title);
213 }
214
215 EAPI void
216 e_wizard_labels_update(void)
217 {
218    edje_object_part_text_set(o_bg, "e.text.label", _("Next"));
219 }
220
221 static void
222 _e_wizard_next_eval(void)
223 {
224    int ok;
225
226    ok = next_can;
227    if (!next_ok) ok = 0;
228    if (next_prev != ok)
229      {
230         if (ok) edje_object_signal_emit(o_bg, "e,state,next,enable", "e");
231         else edje_object_signal_emit(o_bg, "e,state,next,disable", "e");
232         next_prev = ok;
233      }
234 }
235
236 static E_Popup *
237 _e_wizard_main_new(E_Zone *zone)
238 {
239    E_Popup *pop;
240    Evas_Object *o;
241    Evas_Modifier_Mask mask;
242    Eina_Bool kg;
243
244    pop = e_popup_new(zone, 0, 0, zone->w, zone->h);
245    e_popup_layer_set(pop, 255);
246    o = edje_object_add(pop->evas);
247
248    e_theme_edje_object_set(o, "base/theme/wizard", "e/wizard/main");
249    evas_object_move(o, 0, 0);
250    evas_object_resize(o, zone->w, zone->h);
251    evas_object_show(o);
252    edje_object_signal_callback_add(o, "e,action,next", "",
253                                    _e_wizard_cb_next, pop);
254    o_bg = o;
255
256    o = evas_object_rectangle_add(pop->evas);
257    mask = 0;
258    kg = evas_object_key_grab(o, "Tab", mask, ~mask, 0);
259    if (!kg)
260      fprintf(stderr,"ERROR: unable to redirect \"Tab\" key events to object %p.\n", o);
261    mask = evas_key_modifier_mask_get(pop->evas, "Shift");
262    kg = evas_object_key_grab(o, "Tab", mask, ~mask, 0);
263    if (!kg)
264      fprintf(stderr,"ERROR: unable to redirect \"Tab\" key events to object %p.\n", o);
265    mask = 0;
266    kg = evas_object_key_grab(o, "Return", mask, ~mask, 0);
267    if (!kg)
268      fprintf(stderr,"ERROR: unable to redirect \"Return\" key events to object %p.\n", o);
269    mask = 0;
270    kg = evas_object_key_grab(o, "KP_Enter", mask, ~mask, 0);
271    if (!kg)
272      fprintf(stderr,"ERROR: unable to redirect \"KP_Enter\" key events to object %p.\n", o);
273    evas_object_event_callback_add(o, EVAS_CALLBACK_KEY_DOWN,
274                                   _e_wizard_cb_key_down, pop);
275
276    /* set up next/prev buttons */
277    edje_object_part_text_set(o_bg, "e.text.title", _("Welcome to Enlightenment"));
278    edje_object_signal_emit(o_bg, "e,state,next,disable", "e");
279    e_wizard_labels_update();
280
281    e_popup_edje_bg_object_set(pop, o_bg);
282    e_popup_show(pop);
283    if (!e_grabinput_get(ecore_evas_software_x11_window_get(pop->ecore_evas),
284                         1, ecore_evas_software_x11_window_get(pop->ecore_evas)))
285      {
286         e_object_del(E_OBJECT(pop));
287         pop = NULL;
288      }
289    return pop;
290 }
291
292 static E_Popup *
293 _e_wizard_extra_new(E_Zone *zone)
294 {
295    E_Popup *pop;
296    Evas_Object *o;
297
298    pop = e_popup_new(zone, 0, 0, zone->w, zone->h);
299    e_popup_layer_set(pop, 255);
300    o = edje_object_add(pop->evas);
301    e_theme_edje_object_set(o, "base/theme/wizard", "e/wizard/extra");
302    evas_object_move(o, 0, 0);
303    evas_object_resize(o, zone->w, zone->h);
304    evas_object_show(o);
305    e_popup_edje_bg_object_set(pop, o);
306    e_popup_show(pop);
307    return pop;
308 }
309
310 static void
311 _e_wizard_cb_key_down(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event)
312 {
313    Evas_Event_Key_Down *ev;
314
315    ev = event;
316    if (!o_content) return;
317    if (!strcmp(ev->keyname, "Tab"))
318      {
319         if (evas_key_modifier_is_set(ev->modifiers, "Shift"))
320           e_widget_focus_jump(o_content, 0);
321         else
322           e_widget_focus_jump(o_content, 1);
323      }
324    else if (((!strcmp(ev->keyname, "Return")) ||
325              (!strcmp(ev->keyname, "KP_Enter")) ||
326              (!strcmp(ev->keyname, "space"))))
327      {
328         Evas_Object *o;
329
330         o = e_widget_focused_object_get(o_content);
331         if (o) e_widget_activate(o);
332      }
333 }
334
335 static void
336 _e_wizard_cb_next(void *data __UNUSED__, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
337 {
338    e_wizard_next();
339 }