Merge "[elm_datetime.h] Datetime default minimum year is changed to 1902 and document...
[framework/uifw/elementary.git] / src / lib / elc_fileselector_entry.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 typedef struct _Widget_Data Widget_Data;
5
6 struct _Widget_Data
7 {
8    Evas_Object *edje;
9    Evas_Object *button;
10    Evas_Object *entry;
11    char *path;
12 };
13
14 static const char *widtype = NULL;
15
16 static const char SIG_CHANGED[] = "changed";
17 static const char SIG_ACTIVATED[] = "activated";
18 static const char SIG_PRESS[] = "press";
19 static const char SIG_LONGPRESSED[] = "longpressed";
20 static const char SIG_CLICKED[] = "clicked";
21 static const char SIG_CLICKED_DOUBLE[] = "clicked,double";
22 static const char SIG_FOCUSED[] = "focused";
23 static const char SIG_UNFOCUSED[] = "unfocused";
24 static const char SIG_SELECTION_PASTE[] = "selection,paste";
25 static const char SIG_SELECTION_COPY[] = "selection,copy";
26 static const char SIG_SELECTION_CUT[] = "selection,cut";
27 static const char SIG_UNPRESSED[] = "unpressed";
28 static const char SIG_FILE_CHOSEN[] = "file,chosen";
29 static const Evas_Smart_Cb_Description _signals[] =
30 {
31   {SIG_CHANGED, ""},
32   {SIG_ACTIVATED, ""},
33   {SIG_PRESS, ""},
34   {SIG_LONGPRESSED, ""},
35   {SIG_CLICKED, ""},
36   {SIG_CLICKED_DOUBLE, ""},
37   {SIG_FOCUSED, ""},
38   {SIG_UNFOCUSED, ""},
39   {SIG_SELECTION_PASTE, ""},
40   {SIG_SELECTION_COPY, ""},
41   {SIG_SELECTION_CUT, ""},
42   {SIG_UNPRESSED, ""},
43   {SIG_FILE_CHOSEN, "s"},
44   {NULL, NULL}
45 };
46
47 #define SIG_FWD(name)                                                    \
48 static void                                                              \
49 _##name##_fwd(void *data, Evas_Object *obj __UNUSED__, void *event_info) \
50 {                                                                        \
51    evas_object_smart_callback_call(data, SIG_##name, event_info);        \
52 }
53 SIG_FWD(CHANGED)
54 SIG_FWD(PRESS)
55 SIG_FWD(LONGPRESSED)
56 SIG_FWD(CLICKED)
57 SIG_FWD(CLICKED_DOUBLE)
58 SIG_FWD(FOCUSED)
59 SIG_FWD(UNFOCUSED)
60 SIG_FWD(SELECTION_PASTE)
61 SIG_FWD(SELECTION_COPY)
62 SIG_FWD(SELECTION_CUT)
63 SIG_FWD(UNPRESSED)
64 #undef SIG_FWD
65
66 static void _del_pre_hook(Evas_Object *obj);
67 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
68 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
69
70 static void
71 _FILE_CHOSEN_fwd(void *data, Evas_Object *obj __UNUSED__, void *event_info)
72 {
73    Widget_Data *wd = elm_widget_data_get(data);
74    const char *file = event_info;
75    char *s;
76    
77    s = elm_entry_utf8_to_markup(file);
78    if (!s) return;
79    elm_object_text_set(wd->entry, s);
80    free(s);
81    evas_object_smart_callback_call(data, SIG_FILE_CHOSEN, event_info);
82 }
83
84 static void
85 _ACTIVATED_fwd(void *data, Evas_Object *obj __UNUSED__, void *event_info)
86 {
87    Widget_Data *wd = elm_widget_data_get(data);
88    const char *file = elm_object_text_get(wd->entry);
89    elm_fileselector_button_path_set(wd->button, file);
90    evas_object_smart_callback_call(data, SIG_ACTIVATED, event_info);
91 }
92
93 static void
94 _del_pre_hook(Evas_Object *obj)
95 {
96    Widget_Data *wd = elm_widget_data_get(obj);
97    if (!wd) return;
98    evas_object_event_callback_del_full
99       (wd->button, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
100    evas_object_event_callback_del_full
101       (wd->entry, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
102 }
103
104 static void
105 _del_hook(Evas_Object *obj)
106 {
107    Widget_Data *wd = elm_widget_data_get(obj);
108    if (wd->path) free(wd->path);
109    free(wd);
110 }
111
112 static void
113 _sizing_eval(Evas_Object *obj)
114 {
115    Widget_Data *wd = elm_widget_data_get(obj);
116    Evas_Coord minw = -1, minh = -1;
117    if (!wd) return;
118    edje_object_size_min_calc(wd->edje, &minw, &minh);
119    evas_object_size_hint_min_set(obj, minw, minh);
120    evas_object_size_hint_max_set(obj, -1, -1);
121 }
122
123 static Eina_Bool
124 _elm_fileselector_entry_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
125 {
126    Widget_Data *wd = elm_widget_data_get(obj);
127
128    if (!wd)
129      return EINA_FALSE;
130
131    Evas_Object *chain[2];
132
133    /* Direction */
134    if (dir == ELM_FOCUS_PREVIOUS)
135      {
136         chain[0] = wd->button;
137         chain[1] = wd->entry;
138      }
139    else if (dir == ELM_FOCUS_NEXT)
140      {
141         chain[0] = wd->entry;
142         chain[1] = wd->button;
143      }
144    else
145      return EINA_FALSE;
146
147    unsigned char i = elm_widget_focus_get(chain[1]);
148
149    if (elm_widget_focus_next_get(chain[i], dir, next))
150      return EINA_TRUE;
151
152    i = !i;
153
154    Evas_Object *to_focus;
155    if (elm_widget_focus_next_get(chain[i], dir, &to_focus))
156      {
157         *next = to_focus;
158         return !!i;
159      }
160
161    return EINA_FALSE;
162 }
163
164 static void
165 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
166 {
167    Widget_Data *wd = elm_widget_data_get(obj);
168    if (!wd) return;
169    elm_widget_mirrored_set(wd->button, rtl);
170    edje_object_mirrored_set(wd->edje, rtl);
171 }
172
173 static void
174 _theme_hook(Evas_Object *obj)
175 {
176    Widget_Data *wd = elm_widget_data_get(obj);
177    const char *style = elm_widget_style_get(obj);
178    char buf[1024];
179
180    if (!wd) return;
181    _elm_widget_mirrored_reload(obj);
182    _mirrored_set(obj, elm_widget_mirrored_get(obj));
183
184    _elm_theme_object_set(obj, wd->edje, "fileselector_entry", "base", style);
185    if (elm_object_disabled_get(obj))
186       edje_object_signal_emit(wd->edje, "elm,state,disabled", "elm");
187
188    if (!style) style = "default";
189    snprintf(buf, sizeof(buf), "fileselector_entry/%s", style);
190    elm_widget_style_set(wd->button, buf);
191    elm_widget_style_set(wd->entry, buf);
192
193    edje_object_part_swallow(obj, "elm.swallow.button", wd->button);
194    edje_object_part_swallow(obj, "elm.swallow.entry", wd->entry);
195
196    edje_object_message_signal_process(wd->edje);
197    edje_object_scale_set
198      (wd->edje, elm_widget_scale_get(obj) * _elm_config->scale);
199    _sizing_eval(obj);
200 }
201
202 static void
203 _disable_hook(Evas_Object *obj)
204 {
205    Widget_Data *wd = elm_widget_data_get(obj);
206    Eina_Bool val = elm_widget_disabled_get(obj);
207    if (!wd) return;
208    if (val)
209      edje_object_signal_emit(wd->edje, "elm,state,disabled", "elm");
210    else
211      edje_object_signal_emit(wd->edje, "elm,state,enabled", "elm");
212
213    elm_widget_disabled_set(wd->button, val);
214    elm_widget_disabled_set(wd->entry, val);
215 }
216
217 static void
218 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
219 {
220    _sizing_eval(data);
221 }
222
223 static void
224 _elm_fileselector_entry_button_label_set(Evas_Object *obj, const char *item, const char *label)
225 {
226    ELM_CHECK_WIDTYPE(obj, widtype);
227    Widget_Data *wd = elm_widget_data_get(obj);
228    if (item && strcmp(item, "default")) return;
229    if (!wd) return;
230    elm_object_text_set(wd->button, label);
231 }
232
233 static const char *
234 _elm_fileselector_entry_button_label_get(const Evas_Object *obj, const char *item)
235 {
236    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
237    Widget_Data *wd = elm_widget_data_get(obj);
238    if (item && strcmp(item, "default")) return NULL;
239    if (!wd) return NULL;
240    return elm_object_text_get(wd->button);
241 }
242
243 static void
244 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
245 {
246    ELM_CHECK_WIDTYPE(obj, widtype);
247    Widget_Data *wd = elm_widget_data_get(obj);
248    if (!wd) return;
249    if (part && strcmp(part, "button icon")) return;
250    elm_object_part_content_set(wd->button, NULL, content);
251 }
252
253 static Evas_Object *
254 _content_get_hook(const Evas_Object *obj, const char *part)
255 {
256    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
257    Widget_Data *wd = elm_widget_data_get(obj);
258    if (!wd) return NULL;
259    if (part && strcmp(part, "button icon")) return NULL;
260    return elm_object_part_content_get(wd->button, NULL);
261 }
262
263 static Evas_Object *
264 _content_unset_hook(Evas_Object *obj, const char *part)
265 {
266    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
267    Widget_Data *wd = elm_widget_data_get(obj);
268    if (!wd) return NULL;
269    if (part && strcmp(part, "button icon")) return NULL;
270    return elm_object_part_content_unset(wd->button, NULL);
271 }
272
273 EAPI Evas_Object *
274 elm_fileselector_entry_add(Evas_Object *parent)
275 {
276    Evas_Object *obj;
277    Evas *e;
278    Widget_Data *wd;
279
280    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
281
282    ELM_SET_WIDTYPE(widtype, "fileselector_entry");
283    elm_widget_type_set(obj, "fileselector_entry");
284    elm_widget_sub_object_add(parent, obj);
285    elm_widget_data_set(obj, wd);
286    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
287    elm_widget_del_hook_set(obj, _del_hook);
288    elm_widget_disable_hook_set(obj, _disable_hook);
289    elm_widget_focus_next_hook_set(obj, _elm_fileselector_entry_focus_next_hook);
290    elm_widget_can_focus_set(obj, EINA_FALSE);
291    elm_widget_theme_hook_set(obj, _theme_hook);
292    elm_widget_text_set_hook_set(obj, _elm_fileselector_entry_button_label_set);
293    elm_widget_text_get_hook_set(obj, _elm_fileselector_entry_button_label_get);
294    elm_widget_content_set_hook_set(obj, _content_set_hook);
295    elm_widget_content_get_hook_set(obj, _content_get_hook);
296    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
297
298    wd->edje = edje_object_add(e);
299    _elm_theme_object_set(obj, wd->edje, "fileselector_entry", "base", "default");
300    elm_widget_resize_object_set(obj, wd->edje);
301
302    wd->button = elm_fileselector_button_add(obj);
303    elm_widget_mirrored_automatic_set(wd->button, EINA_FALSE);
304    ELM_SET_WIDTYPE(widtype, "fileselector_entry");
305    elm_widget_style_set(wd->button, "fileselector_entry/default");
306    edje_object_part_swallow(wd->edje, "elm.swallow.button", wd->button);
307    elm_widget_sub_object_add(obj, wd->button);
308    evas_object_event_callback_add
309       (wd->button, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
310    elm_fileselector_button_expandable_set(wd->button,
311                                           _elm_config->fileselector_expand_enable);
312
313 #define SIG_FWD(name)                                                   \
314    evas_object_smart_callback_add(wd->button, SIG_##name, _##name##_fwd, obj)
315    SIG_FWD(CLICKED);
316    SIG_FWD(UNPRESSED);
317    SIG_FWD(FILE_CHOSEN);
318 #undef SIG_FWD
319
320    wd->entry = elm_entry_add(obj);
321    elm_entry_scrollable_set(wd->entry, EINA_TRUE);
322    elm_widget_mirrored_automatic_set(wd->entry, EINA_FALSE);
323    elm_widget_style_set(wd->entry, "fileselector_entry/default");
324    elm_entry_single_line_set(wd->entry, EINA_TRUE);
325    elm_entry_editable_set(wd->entry, EINA_TRUE);
326    edje_object_part_swallow(wd->edje, "elm.swallow.entry", wd->entry);
327    elm_widget_sub_object_add(obj, wd->entry);
328    evas_object_event_callback_add
329       (wd->entry, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
330
331 #define SIG_FWD(name)                                                   \
332    evas_object_smart_callback_add(wd->entry, SIG_##name, _##name##_fwd, obj)
333    SIG_FWD(CHANGED);
334    SIG_FWD(ACTIVATED);
335    SIG_FWD(PRESS);
336    SIG_FWD(LONGPRESSED);
337    SIG_FWD(CLICKED);
338    SIG_FWD(CLICKED_DOUBLE);
339    SIG_FWD(FOCUSED);
340    SIG_FWD(UNFOCUSED);
341    SIG_FWD(SELECTION_PASTE);
342    SIG_FWD(SELECTION_COPY);
343    SIG_FWD(SELECTION_CUT);
344 #undef SIG_FWD
345
346    _mirrored_set(obj, elm_widget_mirrored_get(obj));
347    _sizing_eval(obj);
348
349    // TODO: convert Elementary to subclassing of Evas_Smart_Class
350    // TODO: and save some bytes, making descriptions per-class and not instance!
351    evas_object_smart_callbacks_descriptions_set(obj, _signals);
352    return obj;
353 }
354
355 EAPI void
356 elm_fileselector_entry_selected_set(Evas_Object *obj, const char *path)
357 {
358    ELM_CHECK_WIDTYPE(obj, widtype);
359    Widget_Data *wd = elm_widget_data_get(obj);
360    if (!wd) return;
361    elm_fileselector_button_path_set(wd->button, path);
362 }
363
364 EAPI const char *
365 elm_fileselector_entry_selected_get(const Evas_Object *obj)
366 {
367    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
368    Widget_Data *wd = elm_widget_data_get(obj);
369    if (!wd) return NULL;
370    return elm_fileselector_button_path_get(wd->button);
371 }
372
373 EAPI void
374 elm_fileselector_entry_window_title_set(Evas_Object *obj, const char *title)
375 {
376    ELM_CHECK_WIDTYPE(obj, widtype);
377    Widget_Data *wd = elm_widget_data_get(obj);
378    if (!wd) return;
379    elm_fileselector_button_window_title_set(wd->button, title);
380 }
381
382 EAPI const char *
383 elm_fileselector_entry_window_title_get(const Evas_Object *obj)
384 {
385    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
386    Widget_Data *wd = elm_widget_data_get(obj);
387    if (!wd) return NULL;
388    return elm_fileselector_button_window_title_get(wd->button);
389 }
390
391 EAPI void
392 elm_fileselector_entry_window_size_set(Evas_Object *obj, Evas_Coord width, Evas_Coord height)
393 {
394    ELM_CHECK_WIDTYPE(obj, widtype);
395    Widget_Data *wd = elm_widget_data_get(obj);
396    if (!wd) return;
397    elm_fileselector_button_window_size_set(wd->button, width, height);
398 }
399
400 EAPI void
401 elm_fileselector_entry_window_size_get(const Evas_Object *obj, Evas_Coord *width, Evas_Coord *height)
402 {
403    ELM_CHECK_WIDTYPE(obj, widtype);
404    Widget_Data *wd = elm_widget_data_get(obj);
405    if (!wd) return;
406    elm_fileselector_button_window_size_get(wd->button, width, height);
407 }
408
409 EAPI void
410 elm_fileselector_entry_path_set(Evas_Object *obj, const char *path)
411 {
412    ELM_CHECK_WIDTYPE(obj, widtype);
413    Widget_Data *wd = elm_widget_data_get(obj);
414    char *s;
415    
416    if (!wd) return;
417    elm_fileselector_button_path_set(wd->button, path);
418    s = elm_entry_utf8_to_markup(path);
419    if (s)
420      {
421         elm_object_text_set(wd->entry, s);
422         free(s);
423      }
424 }
425
426 EAPI const char *
427 elm_fileselector_entry_path_get(const Evas_Object *obj)
428 {
429    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
430    Widget_Data *wd = elm_widget_data_get(obj);
431    
432    if (!wd) return NULL;
433    if (wd->path) free(wd->path);
434    wd->path = elm_entry_markup_to_utf8(elm_object_text_get(wd->entry));
435    return wd->path;
436 }
437
438 EAPI void
439 elm_fileselector_entry_expandable_set(Evas_Object *obj, Eina_Bool value)
440 {
441    ELM_CHECK_WIDTYPE(obj, widtype);
442    Widget_Data *wd = elm_widget_data_get(obj);
443    if (!wd) return;
444    elm_fileselector_button_expandable_set(wd->button, value);
445 }
446
447 EAPI Eina_Bool
448 elm_fileselector_entry_expandable_get(const Evas_Object *obj)
449 {
450    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
451    Widget_Data *wd = elm_widget_data_get(obj);
452    if (!wd) return EINA_FALSE;
453    return elm_fileselector_button_expandable_get(wd->button);
454 }
455
456 EAPI void
457 elm_fileselector_entry_folder_only_set(Evas_Object *obj, Eina_Bool value)
458 {
459    ELM_CHECK_WIDTYPE(obj, widtype);
460    Widget_Data *wd = elm_widget_data_get(obj);
461    if (!wd) return;
462    elm_fileselector_button_folder_only_set(wd->button, value);
463 }
464
465 EAPI Eina_Bool
466 elm_fileselector_entry_folder_only_get(const Evas_Object *obj)
467 {
468    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
469    Widget_Data *wd = elm_widget_data_get(obj);
470    if (!wd) return EINA_FALSE;
471    return elm_fileselector_button_folder_only_get(wd->button);
472 }
473
474 EAPI void
475 elm_fileselector_entry_is_save_set(Evas_Object *obj, Eina_Bool value)
476 {
477    ELM_CHECK_WIDTYPE(obj, widtype);
478    Widget_Data *wd = elm_widget_data_get(obj);
479    if (!wd) return;
480    elm_fileselector_button_is_save_set(wd->button, value);
481 }
482
483 EAPI Eina_Bool
484 elm_fileselector_entry_is_save_get(const Evas_Object *obj)
485 {
486    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
487    Widget_Data *wd = elm_widget_data_get(obj);
488    if (!wd) return EINA_FALSE;
489    return elm_fileselector_button_is_save_get(wd->button);
490 }
491
492 EAPI void
493 elm_fileselector_entry_inwin_mode_set(Evas_Object *obj, Eina_Bool value)
494 {
495    ELM_CHECK_WIDTYPE(obj, widtype);
496    Widget_Data *wd = elm_widget_data_get(obj);
497    if (!wd) return;
498    elm_fileselector_button_inwin_mode_set(wd->button, value);
499 }
500
501 EAPI Eina_Bool
502 elm_fileselector_entry_inwin_mode_get(const Evas_Object *obj)
503 {
504    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
505    Widget_Data *wd = elm_widget_data_get(obj);
506    if (!wd) return EINA_FALSE;
507    return elm_fileselector_button_inwin_mode_get(wd->button);
508 }