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