elementary_test: change deprecated APIs
[framework/uifw/elementary.git] / src / bin / test_fileselector_entry.c
1 #include <Elementary.h>
2 #include "test.h"
3 #ifdef HAVE_CONFIG_H
4 # include "elementary_config.h"
5 #endif
6
7 #ifndef ELM_LIB_QUICKLAUNCH
8 struct _api_data
9 {
10    unsigned int state;  /* What state we are testing       */
11    void *box;           /* Use this to get box content     */
12 };
13 typedef struct _api_data api_data;
14
15 enum _api_state
16 {
17    ICON_UNSET,
18    WINDOW_TITLE_SET,
19    API_STATE_LAST
20 };
21 typedef enum _api_state api_state;
22
23 static void
24 set_api_state(api_data *api)
25 {
26    const Eina_List *items = elm_box_children_get(api->box);
27    if(!eina_list_count(items))
28      return;
29
30    /* Get first item of list of vbox children */
31    Evas_Object *fs_bt = eina_list_nth(items, 0);
32
33    /* use elm_box_children_get() to get list of children */
34    switch(api->state)
35      { /* Put all api-changes under switch */
36       case ICON_UNSET:
37          elm_object_part_content_unset(fs_bt, NULL);
38          break;
39
40       case WINDOW_TITLE_SET:
41          elm_fileselector_entry_window_title_set(fs_bt, "Custom title from API");
42          break;
43
44       case API_STATE_LAST:
45
46          break;
47       default:
48          return;
49      }
50 }
51
52 static void
53 _api_bt_clicked(void *data, Evas_Object *obj, void *event_info __UNUSED__)
54 {  /* Will add here a SWITCH command containing code to modify test-object */
55    /* in accordance a->state value. */
56    api_data *a = data;
57    char str[128];
58
59    printf("clicked event on API Button: api_state=<%d>\n", a->state);
60    set_api_state(a);
61    a->state++;
62    sprintf(str, "Next API function (%u)", a->state);
63    elm_object_text_set(obj, str);
64    elm_object_disabled_set(obj, a->state == API_STATE_LAST);
65 }
66
67 static void
68 create_dir_struct(void)
69 {
70    FILE *fp;
71    mkdir("/tmp/test_fs_bt", S_IRWXU);
72    fp = fopen("/tmp/test_fs_bt/a_file.txt", "w");
73    if (fp) fclose(fp);
74    fp = fopen("/tmp/test_fs_bt/k_file.txt", "w");
75    if (fp) fclose(fp);
76    fp = fopen("/tmp/test_fs_bt/m_file.txt", "w");
77    if (fp) fclose(fp);
78
79    mkdir("/tmp/test_fs_bt/a_subdir", S_IRWXU);
80    fp = fopen("/tmp/test_fs_bt/a_subdir/d_sub_file.txt", "w");
81    if (fp) fclose(fp);
82    fp = fopen("/tmp/test_fs_bt/a_subdir/j_sub_file.txt", "w");
83    if (fp) fclose(fp);
84 }
85
86 static void
87 _file_chosen(void            *data,
88              Evas_Object *obj __UNUSED__,
89              void            *event_info)
90 {
91    Evas_Object *entry = data;
92    char *file = event_info;
93    elm_object_text_set(entry, file);
94    printf("File chosen: %s\n", file);
95 }
96
97 static void
98 _inwin_mode_toggle(void            *data,
99                    Evas_Object *obj __UNUSED__,
100                    void *event_info __UNUSED__)
101 {
102    Evas_Object *fs_en = data;
103    Eina_Bool value = elm_fileselector_entry_inwin_mode_get(fs_en);
104    elm_fileselector_entry_inwin_mode_set(fs_en, !value);
105    printf("Inwin mode set to: %s\n", value ? "false" : "true");
106 }
107
108 static void
109 _folder_only_toggle(void            *data,
110                     Evas_Object *obj __UNUSED__,
111                     void *event_info __UNUSED__)
112 {
113    Evas_Object *fs_en = data;
114    Evas_Object *ic = elm_object_part_content_get(fs_en, "button icon");
115    Eina_Bool value = elm_fileselector_entry_folder_only_get(fs_en);
116    elm_fileselector_entry_folder_only_set(fs_en, !value);
117    printf("Folder only flag set to: %s\n", value ? "false" : "true");
118    if (!value)
119      {
120         elm_icon_standard_set(ic, "folder");
121         elm_object_text_set(fs_en, "Select a folder");
122      }
123    else
124      {
125         elm_icon_standard_set(ic, "file");
126         elm_object_text_set(fs_en, "Select a file");
127      }
128 }
129
130 static void
131 _expandable_toggle(void            *data,
132                    Evas_Object *obj __UNUSED__,
133                    void *event_info __UNUSED__)
134 {
135    Evas_Object *fs_en = data;
136    Eina_Bool value = elm_fileselector_entry_expandable_get(fs_en);
137    elm_fileselector_entry_expandable_set(fs_en, !value);
138    printf("Expandable flag set to: %s\n", value ? "false" : "true");
139 }
140
141 static void
142 _disabled_toggle(void            *data,
143                  Evas_Object *obj __UNUSED__,
144                  void *event_info __UNUSED__)
145 {
146    Evas_Object *fs_en = data;
147    Eina_Bool value = elm_object_disabled_get(fs_en);
148    elm_object_disabled_set(fs_en, !value);
149    printf("Disabled flag set to: %s\n", value ? "false" : "true");
150 }
151
152 static void
153 _cleanup_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
154 {
155    free(data);
156 }
157
158 void
159 test_fileselector_entry(void *data       __UNUSED__,
160                         Evas_Object *obj __UNUSED__,
161                         void *event_info __UNUSED__)
162 {
163    Evas_Object *win, *bg, *vbox, *hbox, *ic, *bt, *fs_en, *en, *lb, *bxx;
164    api_data *api = calloc(1, sizeof(api_data));
165
166    win = elm_win_add(NULL, "fileselector-entry", ELM_WIN_BASIC);
167    elm_win_title_set(win, "File Selector Entry");
168    elm_win_autodel_set(win, EINA_TRUE);
169    evas_object_event_callback_add(win, EVAS_CALLBACK_FREE, _cleanup_cb, api);
170
171    bg = elm_bg_add(win);
172    elm_win_resize_object_add(win, bg);
173    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
174    evas_object_show(bg);
175
176    bxx = elm_box_add(win);
177    elm_win_resize_object_add(win, bxx);
178    evas_object_size_hint_weight_set(bxx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
179    evas_object_show(bxx);
180
181    vbox = elm_box_add(win);
182    evas_object_size_hint_weight_set(vbox, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
183    api->box = vbox;
184    evas_object_show(vbox);
185
186    bt = elm_button_add(win);
187    elm_object_text_set(bt, "Next API function");
188    evas_object_smart_callback_add(bt, "clicked", _api_bt_clicked, (void *) api);
189    elm_box_pack_end(bxx, bt);
190    elm_object_disabled_set(bt, api->state == API_STATE_LAST);
191    evas_object_show(bt);
192
193    elm_box_pack_end(bxx, vbox);
194
195    create_dir_struct(); /* Create a dir struct in /tmp */
196
197    /* file selector entry */
198    ic = elm_icon_add(win);
199    elm_icon_standard_set(ic, "file");
200    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
201    fs_en = elm_fileselector_entry_add(win);
202    elm_fileselector_entry_path_set(fs_en, "/tmp/test_fs_bt");
203    elm_object_text_set(fs_en, "Select a file");
204    elm_object_part_content_set(fs_en, "button icon", ic);
205    evas_object_size_hint_weight_set(fs_en, EVAS_HINT_EXPAND, 0.0);
206    evas_object_size_hint_align_set(fs_en, EVAS_HINT_FILL, EVAS_HINT_FILL);
207
208    elm_box_pack_end(vbox, fs_en);
209    evas_object_show(fs_en);
210    evas_object_show(ic);
211
212    /* attribute setting buttons */
213    hbox = elm_box_add(win);
214    elm_box_horizontal_set(hbox, EINA_TRUE);
215    elm_box_pack_end(vbox, hbox);
216    evas_object_show(hbox);
217
218    bt = elm_button_add(win);
219    elm_object_text_set(bt, "Toggle inwin mode");
220    evas_object_smart_callback_add(bt, "clicked", _inwin_mode_toggle, fs_en);
221    elm_box_pack_end(hbox, bt);
222    evas_object_show(bt);
223
224    bt = elm_button_add(win);
225    elm_object_text_set(bt, "Toggle folder only mode");
226    evas_object_smart_callback_add(bt, "clicked", _folder_only_toggle, fs_en);
227    elm_box_pack_end(hbox, bt);
228    evas_object_show(bt);
229
230    bt = elm_button_add(win);
231    elm_object_text_set(bt, "Toggle expandable mode");
232    evas_object_smart_callback_add(bt, "clicked", _expandable_toggle, fs_en);
233    elm_box_pack_end(hbox, bt);
234    evas_object_show(bt);
235
236    lb = elm_label_add(win);
237    elm_object_text_set(lb, "Last selection:");
238    elm_box_pack_end(vbox, lb);
239    evas_object_show(lb);
240
241    hbox = elm_box_add(win);
242    elm_box_horizontal_set(hbox, EINA_TRUE);
243    elm_box_pack_end(vbox, hbox);
244    evas_object_show(hbox);
245
246    bt = elm_button_add(win);
247    elm_object_text_set(bt, "Toggle disabled");
248    evas_object_smart_callback_add(bt, "clicked", _disabled_toggle, fs_en);
249    elm_box_pack_end(hbox, bt);
250    evas_object_show(bt);
251
252    en = elm_entry_add(win);
253    elm_entry_line_wrap_set(en, ELM_WRAP_NONE);
254    elm_entry_editable_set(en, EINA_FALSE);
255    evas_object_smart_callback_add(fs_en, "file,chosen", _file_chosen, en);
256    elm_box_pack_end(vbox, en);
257    evas_object_show(en);
258
259    evas_object_resize(win, 400, 500);
260    evas_object_show(win);
261 }
262
263 #endif