set elm_image_fill_outside_set in utils
[profile/tv/apps/native/air_home.git] / src / utils.c
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <Elementary.h>
18 #include <app_debug.h>
19 #include <stdbool.h>
20 #include <app.h>
21 #include <inputmgr.h>
22
23 #include "utils.h"
24 #include "defs.h"
25
26 struct icon_info _icon_info[] = {
27         {
28                 IMAGE_USER_DEFAULT,
29                 IMAGE_USER_DEFAULT_FOCUS,
30                 IMAGE_USER_CURRENT_DEFAULT,
31                 IMAGE_USER_CURRENT_DEFAULT_FOCUS
32         },
33         {
34                 IMAGE_USER_DEFAULT_02,
35                 IMAGE_USER_DEFAULT_02_FOCUS,
36                 IMAGE_USER_CURRENT_DEFAULT_02,
37                 IMAGE_USER_CURRENT_DEFAULT_02_FOCUS
38         },
39         {
40                 IMAGE_USER_DEFAULT_03,
41                 IMAGE_USER_DEFAULT_03_FOCUS,
42                 IMAGE_USER_CURRENT_DEFAULT_03,
43                 IMAGE_USER_CURRENT_DEFAULT_03_FOCUS
44         },
45         {
46                 IMAGE_USER_DEFAULT_04,
47                 IMAGE_USER_DEFAULT_04_FOCUS,
48                 IMAGE_USER_CURRENT_DEFAULT_04,
49                 IMAGE_USER_CURRENT_DEFAULT_04_FOCUS
50         },
51         {
52                 IMAGE_USER_DEFAULT_05,
53                 IMAGE_USER_DEFAULT_05_FOCUS,
54                 IMAGE_USER_CURRENT_DEFAULT_05,
55                 IMAGE_USER_CURRENT_DEFAULT_05_FOCUS
56         },
57         {
58                 IMAGE_USER_DEFAULT_06,
59                 IMAGE_USER_DEFAULT_06_FOCUS,
60                 IMAGE_USER_CURRENT_DEFAULT_06,
61                 IMAGE_USER_CURRENT_DEFAULT_06_FOCUS
62         },
63         {
64                 IMAGE_USER_DEFAULT_07,
65                 IMAGE_USER_DEFAULT_07_FOCUS,
66                 IMAGE_USER_CURRENT_DEFAULT_07,
67                 IMAGE_USER_CURRENT_DEFAULT_07_FOCUS
68         },
69         {
70                 IMAGE_USER_ADD,
71                 IMAGE_USER_ADD_FOCUS,
72                 ICON_ADD,
73                 ICON_ADD
74         }
75 };
76
77 struct icon_info *utils_get_icon_info(void)
78 {
79         return _icon_info;
80 }
81
82 const char *utils_get_focus_photo_from_photo(const char *photo)
83 {
84         int i;
85
86         for (i = 0; i < MAX_ITEM_COUNT; i++) {
87                 if (!strcmp(photo, _icon_info[i].photo_file))
88                         return _icon_info[i].focus_photo_file;
89         }
90
91         return photo;
92 }
93
94 const char *utils_get_focus_icon_from_icon(const char *icon)
95 {
96         int i;
97
98         for (i = 0; i < MAX_ITEM_COUNT; i++) {
99                 if (!strcmp(icon, _icon_info[i].icon_file))
100                         return _icon_info[i].focus_icon_file;
101         }
102
103         return icon;
104 }
105
106 const char *utils_get_icon_from_photo(const char *photo)
107 {
108         int i;
109
110         for (i = 0; i < MAX_ITEM_COUNT; i++) {
111                 if (!strcmp(photo, _icon_info[i].photo_file))
112                         return _icon_info[i].icon_file;
113         }
114
115         return photo;
116 }
117
118 const char *utils_get_photo_from_icon(const char *icon)
119 {
120         int i;
121
122         for (i = 0; i < MAX_ITEM_COUNT; i++) {
123                 if (!strcmp(icon, _icon_info[i].icon_file))
124                         return _icon_info[i].photo_file;
125         }
126
127         return icon;
128 }
129
130 Evas_Object *utils_add_layout(Evas_Object *base, const char *group,
131                 bool focus_allow, const char *part)
132 {
133         Evas_Object *ly;
134
135         if (!base || !group) {
136                 _ERR("Invalid argument");
137                 return NULL;
138         }
139
140         ly = elm_layout_add(base);
141         if (!ly) {
142                 _ERR("failed to add layout");
143                 return false;
144         }
145         elm_layout_file_set(ly, EDJEFILE, group);
146
147         if (focus_allow)
148                 elm_object_focus_allow_set(ly, EINA_TRUE);
149         if (part)
150                 elm_object_part_content_set(base, part, ly);
151
152         return ly;
153 }
154
155 Evas_Object *utils_add_icon(Evas_Object *base, const char *file,
156                 const char *part)
157 {
158         Evas_Object *ic;
159
160         if (!base || !file) {
161                 _ERR("Invalid argument");
162                 return NULL;
163         }
164
165         ic = elm_image_add(base);
166         if (!ic) {
167                 _ERR("failed to add icon");
168                 return NULL;
169         }
170
171         elm_image_file_set(ic, file, NULL);
172
173         if (part)
174                 elm_object_part_content_set(base, part, ic);
175         elm_image_fill_outside_set(ic, EINA_TRUE);
176
177         evas_object_show(ic);
178
179         return ic;
180 }
181
182 Evas_Object *utils_add_label(Evas_Object *base, char *text,
183                 const char *style, const char *part)
184 {
185         Evas_Object *lbl;
186         const char *s;
187
188         if (!base || !text) {
189                 _ERR("Invalid argument");
190                 return NULL;
191         }
192
193         lbl = elm_label_add(base);
194         if (!lbl) {
195                 _ERR("failed to add label");
196                 return NULL;
197         }
198
199         if (style)
200                 elm_object_style_set(lbl, style);
201
202         s = edje_object_data_get(elm_layout_edje_get(base), TITLE_WIDTH);
203         if (s)
204                 elm_label_wrap_width_set(lbl, atoi(s));
205
206         elm_label_ellipsis_set(lbl, EINA_TRUE);
207         elm_object_text_set(lbl, text);
208
209         if (part)
210                 elm_object_part_content_set(base, part, lbl);
211
212         evas_object_show(lbl);
213
214         return lbl;
215 }
216
217 Evas_Object *utils_add_bg(Evas_Object *base, int r, int g, int b, int a,
218                 const char *part)
219 {
220         Evas_Object *bg;
221
222         if (!base) {
223                 _ERR("Invalid argument");
224                 return NULL;
225         }
226
227         bg = evas_object_rectangle_add(evas_object_evas_get(base));
228         if (!bg) {
229                 _ERR("failed to add label");
230                 return NULL;
231         }
232
233         evas_object_color_set(bg, r, g, b, a);
234
235         if (part)
236                 elm_object_part_content_set(base, part, bg);
237
238         evas_object_show(bg);
239
240         return bg;
241 }
242
243 Evas_Object *utils_add_scroller(Evas_Object *base)
244 {
245         Evas_Object *scr;
246
247         scr = elm_scroller_add(base);
248         if (!scr) {
249                 _ERR("failed to add scroller");
250                 return NULL;
251         }
252
253         elm_scroller_policy_set(scr, ELM_SCROLLER_POLICY_OFF,
254                         ELM_SCROLLER_POLICY_OFF);
255         evas_object_show(scr);
256
257         return scr;
258 }
259
260 Evas_Object *utils_add_box(Evas_Object *base, bool horizon)
261 {
262         Evas_Object *box;
263
264         box = elm_box_add(base);
265         if (!box) {
266                 _ERR("failed to add box");
267                 return NULL;
268         }
269
270         if (horizon)
271                 elm_box_horizontal_set(box, EINA_TRUE);
272
273         evas_object_show(box);
274
275         return box;
276 }
277
278 Evas_Object *utils_add_table(Evas_Object *base, bool homo, const char *part)
279 {
280         Evas_Object *table;
281
282         table = elm_table_add(base);
283         if (!table) {
284                 _ERR("failed to add table");
285                 return NULL;
286         }
287
288         if (homo)
289                 elm_table_homogeneous_set(table, EINA_TRUE);
290
291         if (part)
292                 elm_object_part_content_set(base, part, table);
293
294         return table;
295 }
296
297 Evas_Object *utils_add_button(Evas_Object *base, char *text, const char *part)
298 {
299         Evas_Object *btn;
300
301         btn = elm_button_add(base);
302         if (!btn) {
303                 _ERR("failed to add btn");
304                 return NULL;
305         }
306
307         if (part)
308                 elm_object_part_content_set(base, part, btn);
309         if (text)
310                 elm_object_text_set(btn, text);
311         evas_object_show(btn);
312
313         return btn;
314 }
315
316 static void _entry_focused(int id, void *data, Evas_Object *obj,
317                 Elm_Object_Item *item)
318 {
319         elm_object_signal_emit(data, SIG_FOCUS, SRC_PROG);
320 }
321
322 static void _entry_unfocused(int id, void *data, Evas_Object *obj,
323                 Elm_Object_Item *item)
324 {
325         elm_object_signal_emit(data, SIG_UNFOCUS, SRC_PROG);
326 }
327
328 static input_handler entry_handle = {
329         .focused = _entry_focused,
330         .unfocused = _entry_unfocused
331 };
332
333 Evas_Object *utils_add_entry(Evas_Object *base, char *text, bool password,
334                 const char *part)
335 {
336         Evas_Object *ly, *entry;
337
338         ly = utils_add_layout(base, GRP_USER_EDIT_ENTRY, false, part);
339         if (!ly) {
340                 _ERR("failed to add entry layout");
341                 return NULL;
342         }
343
344         entry = elm_entry_add(ly);
345         if (!entry) {
346                 _ERR("failed to add entry");
347                 return NULL;
348         }
349
350         if (password) {
351                 elm_entry_password_set(entry, EINA_TRUE);
352                 elm_config_password_show_last_set(EINA_FALSE);
353         }
354
355         elm_object_style_set(entry, STYLE_INPUT);
356         elm_entry_single_line_set(entry, EINA_TRUE);
357         elm_entry_input_panel_language_set(entry,
358                         ELM_INPUT_PANEL_LANG_ALPHABET);
359         elm_entry_cursor_end_set(entry);
360         elm_entry_scrollable_set(entry, EINA_TRUE);
361         elm_scroller_policy_set(entry, ELM_SCROLLER_POLICY_OFF,
362                         ELM_SCROLLER_POLICY_OFF);
363         if (text)
364                 elm_entry_entry_set(entry, text);
365         elm_object_part_content_set(ly, PART_USER_EDIT_ENTRY, entry);
366
367         inputmgr_add_callback(entry, 0, &entry_handle, ly);
368         evas_object_show(entry);
369
370         return entry;
371 }
372
373 Evas_Object *utils_add_popup(Evas_Object *base, char *title, char *message)
374 {
375         Evas_Object *popup;
376
377         if (!base) {
378                 _ERR("Invalid argument");
379                 return NULL;
380         }
381
382         popup = elm_popup_add(base);
383         if (!popup) {
384                 _ERR("failed to add popup");
385                 return NULL;
386         }
387
388         if (title)
389                 elm_object_part_text_set(popup, PART_TITLE_TEXT, title);
390         if (message)
391                 elm_object_text_set(popup, message);
392
393         elm_popup_orient_set(popup, ELM_POPUP_ORIENT_CENTER);
394         evas_object_show(popup);
395
396         return popup;
397 }
398
399 bool utils_launch_app(const char *pkg)
400 {
401         app_control_h app_control;
402         int r;
403
404         if (!pkg) {
405                 _ERR("Invalid argument");
406                 return false;
407         }
408
409         app_control_create(&app_control);
410         app_control_set_operation(app_control, APP_CONTROL_OPERATION_DEFAULT);
411         app_control_set_app_id(app_control, pkg);
412
413         r = app_control_send_launch_request(app_control, NULL, NULL);
414         if (r != APP_CONTROL_ERROR_NONE) {
415                 _ERR("failed to launch pkg");
416                 app_control_destroy(app_control);
417                 return false;
418         }
419
420         app_control_destroy(app_control);
421
422         return true;
423 }