update for beta release
[framework/uifw/e17.git] / src / modules / fileman / e_fwin_nav.c
1 #include <e.h>
2 #include "e_mod_main.h"
3
4 typedef struct _Instance Instance;
5
6 struct _Instance
7 {
8   E_Gadcon_Client *gcc;
9
10   E_Toolbar *tbar;
11
12   Evas_Object *o_base, *o_box, *o_fm, *o_scroll;
13
14   // buttons
15   Eina_List *l_buttons;
16   Eina_List *history, *current;
17   int ignore_dir;
18
19   const char *theme;
20
21   Ecore_Idle_Enterer *idler;
22 };
23
24
25 /* local function protos */
26 static E_Gadcon_Client *_gc_init(E_Gadcon *gc, const char *name, const char *id, const char *style);
27 static void _gc_shutdown(E_Gadcon_Client *gcc);
28 static void _gc_orient(E_Gadcon_Client *gcc, E_Gadcon_Orient orient);
29 static const char *_gc_label(E_Gadcon_Client_Class *client_class);
30 static Evas_Object *_gc_icon(E_Gadcon_Client_Class *client_class, Evas *evas);
31 static const char *_gc_id_new(E_Gadcon_Client_Class *client_class);
32 static void _cb_mouse_down(void *data, Evas *e, Evas_Object *obj, void *event_info);
33 static void _cb_key_down(void *data, Evas *e, Evas_Object *obj, void *event_info);
34 static void _cb_back_click(void *data, Evas_Object *obj, const char *emission, const char *source);
35 static void _cb_forward_click(void *data, Evas_Object *obj, const char *emission, const char *source);
36 static void _cb_up_click(void *data, Evas_Object *obj, const char *emission, const char *source);
37 static void _cb_refresh_click(void *data, Evas_Object *obj, const char *emission, const char *source);
38 static void _cb_favorites_click(void *data, Evas_Object *obj, const char *emission, const char *source);
39 static void _cb_changed(void *data, Evas_Object *obj, void *event_info);
40 static void _cb_dir_changed(void *data, Evas_Object *obj, void *event_info);
41 static void _cb_button_click(void *data, Evas_Object *obj, const char *emission, const char *source);
42 static void _cb_scroll_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info);
43 static void _box_button_append(Instance *inst, const char *label, void (*func)(void *data, Evas_Object *obj, const char *emission, const char *source));
44
45 static Eina_List *instances = NULL;
46 static const char *_nav_mod_dir = NULL;
47
48 /* local gadcon functions */
49 static const E_Gadcon_Client_Class _gc_class =
50 {
51    GADCON_CLIENT_CLASS_VERSION, "efm_navigation",
52      {
53         _gc_init, _gc_shutdown, _gc_orient, _gc_label, _gc_icon, _gc_id_new, NULL,
54           e_gadcon_site_is_efm_toolbar
55      }, E_GADCON_CLIENT_STYLE_PLAIN
56 };
57
58 Eina_Bool
59 e_fwin_nav_init(void)
60 {
61    e_gadcon_provider_register(&_gc_class);
62
63    return EINA_TRUE;
64 }
65
66 Eina_Bool
67 e_fwin_nav_shutdown(void)
68 {
69    e_gadcon_provider_unregister(&_gc_class);
70
71    return EINA_TRUE;
72 }
73
74 static void _cb_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
75 {
76    Instance *inst;
77    int w, h;
78
79    inst = data;
80    evas_object_geometry_get(inst->gcc->gadcon->o_container, 0, 0, &w, &h);
81    e_gadcon_client_min_size_set(inst->gcc, w, h);
82    e_gadcon_client_aspect_set(inst->gcc, w, h);
83 }
84
85 static Eina_Bool
86 _cb_initial_dir(void *data)
87 {
88    Instance *inst = data;
89
90    _cb_dir_changed(inst, NULL, NULL);
91
92    return ECORE_CALLBACK_CANCEL;
93 }
94
95 static E_Gadcon_Client *
96 _gc_init(E_Gadcon *gc, const char *name, const char *id, const char *style)
97 {
98    Instance *inst = NULL;
99    char buf[PATH_MAX];
100    int w, h;
101    E_Toolbar *tbar;
102    Eina_List *l;
103    Evas_Object *o_fm;
104
105    tbar = e_gadcon_toolbar_get(gc);
106    if (!tbar) return NULL;
107
108    o_fm = e_toolbar_fm2_get(tbar);
109    if (!o_fm) return NULL;
110
111    /* make sure only one instance exists in toolbar */
112    EINA_LIST_FOREACH(instances, l, inst)
113      if (inst->tbar == tbar) return NULL;
114
115    inst = E_NEW(Instance, 1);
116    if (!inst) return NULL;
117
118    inst->tbar = tbar;
119    inst->o_fm = o_fm;
120
121    snprintf(buf, sizeof(buf), "%s/e-module-efm_nav.edj", _nav_mod_dir);
122    inst->theme = eina_stringshare_add(buf);
123
124    inst->o_base = edje_object_add(gc->evas);
125    e_theme_edje_object_set(inst->o_base, "base/theme/modules/efm_navigation",
126                            "modules/efm_navigation/main");
127
128    edje_object_signal_callback_add(inst->o_base, "e,action,back,click", "",
129                                    _cb_back_click, inst);
130    edje_object_signal_callback_add(inst->o_base, "e,action,forward,click", "",
131                                    _cb_forward_click, inst);
132    edje_object_signal_callback_add(inst->o_base, "e,action,up,click", "",
133                                    _cb_up_click, inst);
134    edje_object_signal_callback_add(inst->o_base, "e,action,refresh,click", "",
135                                    _cb_refresh_click, inst);
136    edje_object_signal_callback_add(inst->o_base, "e,action,favorites,click", "",
137                                    _cb_favorites_click, inst);
138    evas_object_show(inst->o_base);
139
140    inst->o_scroll = e_scrollframe_add(gc->evas);
141    e_scrollframe_custom_theme_set(inst->o_scroll,
142                                        "base/theme/modules/efm_navigation",
143                                   "modules/efm_navigation/pathbar_scrollframe");
144
145    e_scrollframe_single_dir_set(inst->o_scroll, 1);
146    e_scrollframe_policy_set(inst->o_scroll, E_SCROLLFRAME_POLICY_AUTO,
147                             E_SCROLLFRAME_POLICY_OFF);
148    e_scrollframe_thumbscroll_force(inst->o_scroll, 1);
149    evas_object_show(inst->o_scroll);
150
151    inst->o_box = e_box_add(gc->evas);
152    e_box_orientation_set(inst->o_box, 1);
153    e_box_homogenous_set(inst->o_box, 0);
154    e_scrollframe_child_set(inst->o_scroll, inst->o_box);
155    evas_object_show(inst->o_box);
156
157    evas_object_event_callback_add(inst->o_scroll, EVAS_CALLBACK_RESIZE,
158                                   _cb_scroll_resize, inst);
159    
160    edje_object_part_swallow(inst->o_base, "e.swallow.pathbar", inst->o_scroll);
161
162    inst->gcc = e_gadcon_client_new(gc, name, id, style, inst->o_base);
163    inst->gcc->data = inst;
164
165    /* add the hooks to get signals from efm */
166    evas_object_event_callback_add(inst->o_fm, EVAS_CALLBACK_KEY_DOWN,
167                                   _cb_key_down, inst);
168    evas_object_smart_callback_add(inst->o_fm, "changed",
169                                   _cb_changed, inst);
170    evas_object_smart_callback_add(inst->o_fm, "dir_changed",
171                                   _cb_dir_changed, inst);
172
173    evas_object_event_callback_add(inst->o_base,
174                                   EVAS_CALLBACK_MOUSE_DOWN,
175                                   _cb_mouse_down, inst);
176    if (!inst->gcc->resizable)
177      {
178         evas_object_geometry_get(inst->gcc->gadcon->o_container, 0, 0, &w, &h);
179         e_gadcon_client_min_size_set(inst->gcc, w, h);
180         e_gadcon_client_aspect_set(inst->gcc, w, h);
181         evas_object_event_callback_add(inst->gcc->gadcon->o_container,
182                                        EVAS_CALLBACK_RESIZE,
183                                        _cb_resize, inst);
184      }
185
186    edje_object_signal_emit(inst->o_base, "e,state,back,disabled", "e");
187    edje_object_signal_emit(inst->o_base, "e,state,forward,disabled", "e");
188    edje_object_message_signal_process(inst->o_base);
189
190    instances = eina_list_append(instances, inst);
191
192    inst->idler = ecore_idle_enterer_add(_cb_initial_dir, inst);
193
194    return inst->gcc;
195 }
196
197 static void
198 _gc_shutdown(E_Gadcon_Client *gcc)
199 {
200    Instance *inst = NULL;
201    const char *s;
202    Evas_Object *btn;
203
204    inst = gcc->data;
205    if (!inst) return;
206    instances = eina_list_remove(instances, inst);
207
208    evas_object_event_callback_del_full(inst->o_fm,
209                                        EVAS_CALLBACK_KEY_DOWN,
210                                        _cb_key_down, inst);
211
212    evas_object_smart_callback_del(inst->o_fm, "changed", _cb_changed);
213    evas_object_smart_callback_del(inst->o_fm, "dir_changed", _cb_dir_changed);
214
215    EINA_LIST_FREE(inst->history, s)
216      eina_stringshare_del(s);
217
218    if (gcc->gadcon->o_container)
219      evas_object_event_callback_del_full(gcc->gadcon->o_container,
220                                          EVAS_CALLBACK_RESIZE,
221                                          _cb_resize, inst);
222
223    EINA_LIST_FREE(inst->l_buttons, btn)
224      {
225         e_box_unpack(btn);
226         evas_object_del(btn);
227      }
228
229    if (inst->o_base) evas_object_del(inst->o_base);
230    if (inst->o_box) evas_object_del(inst->o_box);
231    if (inst->o_scroll) evas_object_del(inst->o_scroll);
232
233    eina_stringshare_del(inst->theme);
234
235    E_FREE(inst);
236 }
237
238 static void
239 _gc_orient(E_Gadcon_Client *gcc, E_Gadcon_Orient orient)
240 {
241    Instance *inst;
242
243    inst = gcc->data;
244    switch (orient)
245      {
246       case E_GADCON_ORIENT_TOP:
247       case E_GADCON_ORIENT_BOTTOM:
248          /* e_gadcon_client_aspect_set(gcc, 16 * 4, 16); */
249         break;
250       case E_GADCON_ORIENT_LEFT:
251       case E_GADCON_ORIENT_RIGHT:
252         /* e_gadcon_client_aspect_set(gcc, 16, 16 * 4); */
253         break;
254       default:
255         break;
256      }
257    /* e_gadcon_client_min_size_set(gcc, 16, 16); */
258 }
259
260 static const char *
261 _gc_label(E_Gadcon_Client_Class *client_class __UNUSED__)
262 {
263    return _("EFM Navigation");
264 }
265
266 static Evas_Object *
267 _gc_icon(E_Gadcon_Client_Class *client_class __UNUSED__, Evas *evas)
268 {
269    return e_util_icon_theme_icon_add("system-file-manager", 48, evas);
270 }
271
272 static const char *
273 _gc_id_new(E_Gadcon_Client_Class *client_class __UNUSED__)
274 {
275    char buf[PATH_MAX];
276
277    snprintf(buf, sizeof(buf), "%s.%d", _gc_class.name,
278             (eina_list_count(instances) + 1));
279    return strdup(buf);
280 }
281
282 /* local functions */
283 static void
284 _cb_key_down(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info)
285 {
286    Instance *inst;
287    Evas_Event_Key_Down *ev;
288
289    inst = data;
290    ev = event_info;
291    if (evas_key_modifier_is_set(ev->modifiers, "Alt"))
292      {
293         if (!strcmp(ev->key, "Left"))
294           _cb_back_click(inst, obj, "e,action,click", "e");
295         else if (!strcmp(ev->key, "Right"))
296           _cb_forward_click(inst, obj, "e,action,click", "e");
297         else if (!strcmp(ev->key, "Up"))
298           _cb_up_click(inst, obj, "e,action,click", "e");
299      }
300    else if (evas_key_modifier_is_set(ev->modifiers, "Control"))
301      {
302         if (!strcmp(ev->key, "r"))
303           _cb_refresh_click(inst, obj, "e,action,click", "e");
304      }
305 }
306
307 static void
308 _cb_mouse_down(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
309 {
310    Instance *inst;
311    Evas_Event_Mouse_Down *ev;
312    E_Menu *m;
313    E_Zone *zone;
314    int x, y;
315
316    inst = data;
317    ev = event_info;
318    if ((ev->button != 3) || (inst->gcc->menu)) return;
319    zone = e_util_zone_current_get(e_manager_current_get());
320
321    m = e_menu_new();
322    m = e_gadcon_client_util_menu_items_append(inst->gcc, m, 0);
323    ecore_x_pointer_xy_get(zone->container->win, &x, &y);
324    e_menu_activate_mouse(m, zone, x, y, 1, 1,
325                          E_MENU_POP_DIRECTION_AUTO, ev->timestamp);
326 }
327
328 static void
329 _cb_back_click(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
330 {
331    Instance *inst;
332
333    inst = data;
334
335    if ((!inst->current) || (inst->current == eina_list_last(inst->history))) return;
336    inst->current = eina_list_next(inst->current);
337
338    inst->ignore_dir = 1;
339    e_fm2_path_set(inst->o_fm, eina_list_data_get(inst->current), "/");
340 }
341
342 static void
343 _cb_forward_click(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
344 {
345    Instance *inst;
346
347    inst = data;
348
349    if ((!inst->current) || (inst->current == inst->history)) return;
350    inst->current = eina_list_prev(inst->current);
351
352    inst->ignore_dir = 1;
353    e_fm2_path_set(inst->o_fm, eina_list_data_get(inst->current), "/");
354 }
355
356 static void
357 _cb_refresh_click(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
358 {
359    Instance *inst;
360
361    inst = data;
362
363    // Don't destroy forward history when refreshing
364    inst->ignore_dir = 1;
365    e_fm2_path_set(inst->o_fm, NULL, e_fm2_real_path_get(inst->o_fm));
366 }
367
368 static void
369 _cb_up_click(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
370 {
371    Instance *inst;
372    char *p, *t;
373
374    inst = data;
375
376    t = strdup(e_fm2_real_path_get(inst->o_fm));
377    p = strrchr(t, '/');
378    if (p)
379      {
380         *p = 0;
381         p = t;
382         if (p[0] == 0) p = "/";
383         e_fm2_path_set(inst->o_fm, NULL, p);
384      }
385    else
386      {
387         edje_object_signal_emit(inst->o_base, "e,state,up,disabled", "e");
388      }
389
390    free(t);
391 }
392
393 static void
394 _cb_favorites_click(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
395 {
396    Instance *inst;
397
398    inst = data;
399
400    e_fm2_path_set(inst->o_fm, "favorites", "/");
401 }
402
403 static void
404 _cb_changed(void *data, Evas_Object *obj __UNUSED__, void *event_info)
405 {
406    Instance *inst;
407
408    inst = data;
409    inst->tbar = event_info;
410 }
411
412 static void
413 _cb_button_click(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
414 {
415    Instance *inst = data;
416    Eina_List *l;
417    Evas_Object *btn;
418    char path[PATH_MAX] = "";
419
420    EINA_LIST_FOREACH(inst->l_buttons, l, btn)
421      {
422         strcat(path, edje_object_part_text_get(btn, "e.text.label"));
423         if (btn == obj) break;
424         strcat(path, "/");
425      }
426    e_fm2_path_set(inst->o_fm, "/", path);
427 }
428
429 static void
430 _cb_scroll_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
431 {
432    Instance *inst = data;
433    Evas_Coord mw = 0, mh = 0;
434    
435    evas_object_geometry_get(inst->o_box, NULL, NULL, &mw, NULL);
436    evas_object_geometry_get(inst->o_scroll, NULL, NULL, NULL, &mh);
437    evas_object_resize(inst->o_box, mw, mh);
438 }
439
440 static void
441 _box_button_append(Instance *inst, const char *label, void (*func)(void *data, Evas_Object *obj, const char *emission, const char *source))
442 {
443    Evas_Object *o;
444    Evas_Coord mw = 0, mh = 0;
445
446    if (!inst || !label || !*label || !func)
447      return;
448
449    o = edje_object_add(evas_object_evas_get(inst->o_box));
450
451    e_theme_edje_object_set(o, "base/theme/modules/efm_navigation",
452                            "modules/efm_navigation/pathbar_button");
453
454    edje_object_signal_callback_add(o, "e,action,click", "", func, inst);
455    edje_object_part_text_set(o, "e.text.label", label);
456    edje_object_size_min_calc(o, &mw, &mh);
457    e_box_pack_end(inst->o_box, o);
458    evas_object_show(o);
459    e_box_pack_options_set(o, 1, 0, 0, 0, 0.5, 0.5, mw, mh, 9999, 9999);
460    e_box_size_min_get(inst->o_box, &mw, NULL);
461    evas_object_geometry_get(inst->o_scroll, NULL, NULL, NULL, &mh);
462    evas_object_resize(inst->o_box, mw, mh);
463
464    inst->l_buttons = eina_list_append(inst->l_buttons, o);
465 }
466
467 static void
468 _cb_dir_changed(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
469 {
470    Instance *inst;
471    const char *realpath;
472    char *path, *dir, *p;
473    Eina_List *l, *ll, *sel = NULL;
474    Evas_Object *btn;
475    int mw, sw, changed = 0;
476
477    inst = data;
478
479    if (!(realpath = e_fm2_real_path_get(inst->o_fm))) return;
480
481    /* update pathbar */
482    if (!inst->l_buttons)
483      _box_button_append(inst, "/", _cb_button_click);
484    
485    sel = inst->l_buttons;
486    l = eina_list_next(sel);
487    p = path = ecore_file_realpath(realpath);
488
489    while (p)
490      {
491         dir = strsep(&p, "/");
492
493         if (!(*dir)) continue;
494
495         if (l && (btn = eina_list_data_get(l)))
496           {
497              if (strcmp(dir, edje_object_part_text_get(btn, "e.text.label")))
498                {
499                   changed = 1;
500
501                   while (l)
502                     {
503                        e_box_unpack(btn);
504                        evas_object_del(btn);
505                        ll = l;
506                        l = eina_list_next(l);
507                        btn = eina_list_data_get(l);
508
509                        inst->l_buttons =
510                          eina_list_remove_list(inst->l_buttons, ll);
511                     }
512                }
513              else
514                {
515                   if (!p) sel = l;
516                   l = eina_list_next(l);
517                   continue;
518                }
519           }
520
521         _box_button_append(inst, dir, _cb_button_click);
522         if (!p) sel = eina_list_last(inst->l_buttons);
523         changed = 1;
524      }
525
526    free(path);
527
528    if (changed)
529      {
530         evas_object_geometry_get(inst->o_box, NULL, NULL, &mw, NULL);
531         edje_object_size_min_calc(e_scrollframe_edje_object_get(inst->o_scroll), &sw, NULL);
532
533         evas_object_size_hint_max_set(inst->o_scroll, mw + sw, 32);
534      }
535
536    EINA_LIST_FOREACH(inst->l_buttons, l, btn)
537      if (l == sel)
538        edje_object_signal_emit(btn, "e,state,selected", "e");
539      else
540        edje_object_signal_emit(btn, "e,state,default", "e");
541
542    /* scroll to selected button */
543    if (sel)
544      {
545         Evas_Coord x, y, w, h, xx, yy, ww = 1;
546         btn = eina_list_data_get(sel);
547         evas_object_geometry_get(btn, &x, &y, &w, &h);
548
549         /* show buttons around selected */
550         if (sel->next)
551           {
552              btn = eina_list_data_get(sel->next);
553              evas_object_geometry_get(btn, NULL, NULL, &ww, NULL);
554              w += ww;
555           }
556         if (sel->prev)
557           {
558              btn = eina_list_data_get(sel->prev);
559              evas_object_geometry_get(btn, NULL, NULL, &ww, NULL);
560              x -= ww;
561              w += ww;
562           }
563
564         evas_object_geometry_get(inst->o_box, &xx, &yy, NULL, NULL);
565         e_scrollframe_child_region_show(inst->o_scroll, x - xx, y - yy, w, h);
566      }
567
568    /* update history */
569    if ((!inst->ignore_dir) && (eina_list_data_get(inst->current) != realpath))
570      {
571         if (inst->current)
572           {
573              while (inst->history != inst->current)
574                {
575                   eina_stringshare_del(eina_list_data_get(inst->history));
576                   inst->history =
577                     eina_list_remove_list(inst->history, inst->history);
578                }
579           }
580         inst->history =
581           eina_list_prepend(inst->history, eina_stringshare_ref(realpath));
582         inst->current = inst->history;
583      }
584    inst->ignore_dir = 0;
585
586    if (!strcmp(realpath, "/"))
587      edje_object_signal_emit(inst->o_base, "e,state,up,disabled", "e");
588    else
589      edje_object_signal_emit(inst->o_base, "e,state,up,enabled", "e");
590
591    if ((!inst->history) || (eina_list_last(inst->history) == inst->current))
592      edje_object_signal_emit(inst->o_base, "e,state,back,disabled", "e");
593    else
594      edje_object_signal_emit(inst->o_base, "e,state,back,enabled", "e");
595
596    if ((!inst->history) || (inst->history == inst->current))
597      edje_object_signal_emit(inst->o_base, "e,state,forward,disabled", "e");
598    else
599      edje_object_signal_emit(inst->o_base, "e,state,forward,enabled", "e");
600 }