action_menu: draw menu area using table object
[profile/tv/apps/native/air_mediahub.git] / src / util / util.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 <stdbool.h>
18 #include <Elementary.h>
19 #include <app_debug.h>
20
21 #include "util/util.h"
22
23 void util_set_alpha_color(Evas_Object *obj, int alpha)
24 {
25         int r, g, b, a;
26
27         evas_object_color_get(obj, &r, &g, &b, &a);
28
29         /* evas should use premultiplied alpha */
30         r = r * alpha / 255;
31         g = g * alpha / 255;
32         b = b * alpha / 255;
33
34         evas_object_color_set(obj, r, g, b, alpha);
35 }
36
37 Evas_Object *util_add_box(Evas_Object *base)
38 {
39         Evas_Object *box;
40
41         if (!base)
42                 return NULL;
43
44         box = elm_box_add(base);
45         if (!box) {
46                 _ERR("failed to create box object");
47                 return NULL;
48         }
49
50         evas_object_size_hint_align_set(box, 0.0, EVAS_HINT_FILL);
51         evas_object_size_hint_weight_set(box, 0.0, EVAS_HINT_EXPAND);
52         elm_box_horizontal_set(box, EINA_TRUE);
53
54         return box;
55 }
56
57 Evas_Object *util_add_gengrid(Evas_Object *base, int item_size_x,
58                         int item_size_y, Eina_Bool horizontal)
59 {
60         Evas_Object *grid;
61
62         if (!base)
63                 return NULL;
64
65         grid = elm_gengrid_add(base);
66         if (!grid) {
67                 _ERR("failed to create gengrid object");
68                 return NULL;
69         }
70
71         evas_object_size_hint_weight_set(grid,
72                         EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
73         evas_object_size_hint_align_set(grid, EVAS_HINT_FILL, EVAS_HINT_FILL);
74
75         elm_gengrid_select_mode_set(grid, ELM_OBJECT_SELECT_MODE_ALWAYS);
76         elm_gengrid_multi_select_set(grid, EINA_FALSE);
77
78         elm_gengrid_horizontal_set(grid, horizontal);
79
80         elm_gengrid_align_set(grid, 0.0, 0.0);
81
82         elm_gengrid_item_size_set(grid,
83                         elm_config_scale_get() * item_size_x,
84                         elm_config_scale_get() * item_size_y);
85
86         elm_scroller_policy_set(grid, ELM_SCROLLER_POLICY_OFF,
87                         ELM_SCROLLER_POLICY_OFF);
88
89         return grid;
90 }
91
92 Evas_Object *util_add_genlist(Evas_Object *base)
93 {
94         Evas_Object *list;
95
96         list = elm_genlist_add(base);
97         if (!list) {
98                 _ERR("failed to adding genlist");
99                 return NULL;
100         }
101
102         evas_object_size_hint_weight_set(list,
103                         EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
104
105         elm_genlist_homogeneous_set(list, EINA_TRUE);
106         elm_genlist_select_mode_set(list, ELM_OBJECT_SELECT_MODE_ALWAYS);
107         elm_genlist_multi_select_set(list, EINA_FALSE);
108
109         return list;
110 }
111
112 Evas_Object *util_add_image(Evas_Object *base, const char *file)
113 {
114         Evas_Object *image;
115
116         if (!base || !file)
117                 return NULL;
118
119         image = elm_image_add(base);
120         if (!image) {
121                 _ERR("failed to create image object");
122                 return NULL;
123         }
124
125         elm_image_file_set(image, file, NULL);
126         elm_image_aspect_fixed_set(image, EINA_FALSE);
127
128         return image;
129 }
130
131 Evas_Object *util_add_scroller(Evas_Object *base)
132 {
133         Evas_Object *scr;
134
135         if (!base)
136                 return NULL;
137
138         scr = elm_scroller_add(base);
139         if (!scr) {
140                 _ERR("failed to create scroller object");
141                 return NULL;
142         }
143
144         elm_scroller_policy_set(scr, ELM_SCROLLER_POLICY_OFF,
145                         ELM_SCROLLER_POLICY_OFF);
146
147         return scr;
148 }
149
150 Evas_Object *util_add_table(Evas_Object *base, int padding_x, int padding_y)
151 {
152         Evas_Object *table;
153
154         if (!base)
155                 return NULL;
156
157         table = elm_table_add(base);
158         if (!table) {
159                 _ERR("failed to create table object");
160                 return NULL;
161         }
162
163         elm_table_padding_set(table, padding_x, padding_y);
164
165         return table;
166 }
167
168 void util_time_string(char *str, int size, unsigned int ms, bool full)
169 {
170         int sec;
171         int h, m, s;
172
173         sec = ms / 1000;
174
175         h = sec / 3600;
176         m = (sec % 3600) / 60;
177         s = sec % 60;
178
179         if (full) {
180                 snprintf(str, size, "%02d:%02d:%02d", h, m, s);
181         } else {
182                 if (h)
183                         snprintf(str, size, "%d:%02d:%02d", h, m, s);
184                 else
185                         snprintf(str, size, "%d:%02d", m, s);
186         }
187 }
188
189 void util_up_string(char *str)
190 {
191         while (*str) {
192                 *str = toupper(*str);
193                 str++;
194         }
195 }
196
197 int util_get_media_index(Eina_List *list, void *data)
198 {
199         Eina_List *l;
200         int index;
201         void *obj;
202
203         index = 0;
204         EINA_LIST_FOREACH(list, l, obj) {
205                 if (data == obj)
206                         return index;
207
208                 index++;
209         }
210
211         return -1;
212 }
213
214 int util_get_media_index_from_id(Eina_List *list, const char *id)
215 {
216         Eina_List *l;
217         app_media *am;
218         app_media_info *info;
219         int index;
220
221         index = 0;
222         EINA_LIST_FOREACH(list, l, am) {
223                 info = app_media_get_info(am);
224                 if (!info)
225                         continue;
226
227                 if (!strcmp(id, info->media_id))
228                         return index;
229
230                 index++;
231         }
232
233         return -1;
234 }
235
236 app_media *util_find_media_info(Eina_List *list, const char *id)
237 {
238         Eina_List *l;
239         app_media *am;
240         app_media_info *info;
241
242         EINA_LIST_FOREACH(list, l, am) {
243                 info = app_media_get_info(am);
244                 if (!info)
245                         continue;
246
247                 if (!strcmp(info->media_id, id))
248                         return am;
249         }
250
251         return NULL;
252 }