dbg.h file is removed.
[profile/tv/apps/native/source.git] / src / util / util.cpp
1 /*
2  * Copyright (c) 2014 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 <Evas.h>
18 #include "def.h"
19
20 #include "SourceInfo.h"
21 #include "util.h"
22 #include "source_mgr.h"
23
24 Evas_Object *util_add_btn(Evas_Object *parent, const char *group)
25 {
26         Evas_Object *btn;
27
28         if (!parent || !group)
29                 return NULL;
30
31         btn = elm_button_add(parent);
32         if (!btn)
33                 return NULL;
34
35         elm_object_style_set(btn, group);
36
37         evas_object_size_hint_weight_set(btn,
38                         EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
39         evas_object_show(btn);
40         return btn;
41 }
42
43 Evas_Object *util_add_layout(Evas_Object *parent, const char *group)
44 {
45         Evas_Object *layout;
46
47         if (!parent || !group)
48                 return NULL;
49
50         layout = elm_layout_add(parent);
51         if (!layout)
52                 return NULL;
53
54         elm_layout_file_set(layout, EDJ_FILE, group);
55
56         evas_object_size_hint_weight_set(layout,
57                         EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
58
59         return layout;
60 }
61
62 Evas_Object *util_add_grid(Evas_Object *parent, int w, int h)
63 {
64         Evas_Object *grid;
65
66         if (!parent)
67                 return NULL;
68
69         grid = elm_gengrid_add(parent);
70         if (!grid)
71                 return NULL;
72
73         elm_scroller_bounce_set(grid, EINA_FALSE, EINA_FALSE);
74         elm_gengrid_horizontal_set(grid, EINA_FALSE);
75         elm_gengrid_align_set(grid, 0.0, 0.0);
76         elm_gengrid_select_mode_set(grid, ELM_OBJECT_SELECT_MODE_ALWAYS);
77         elm_gengrid_multi_select_set(grid, EINA_FALSE);
78
79         elm_gengrid_item_size_set(grid, elm_config_scale_get() * h,
80                         elm_config_scale_get() * w);
81
82         evas_object_size_hint_weight_set(grid,
83                         EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
84         evas_object_size_hint_align_set(grid,
85                         EVAS_HINT_FILL, EVAS_HINT_FILL);
86
87         return grid;
88 }
89
90 void util_dim_item(Elm_Object_Item *it)
91 {
92         Evas_Object *img;
93
94         if (!it)
95                 return;
96
97         elm_object_item_disabled_set(it, EINA_TRUE);
98
99         img = elm_object_item_part_content_get(it, PART_ITEM_ICO);
100         if (!img)
101                 return;
102
103         evas_object_color_set(img, ITEM_ICO_DIM_R,
104                         ITEM_ICO_DIM_G, ITEM_ICO_DIM_B, ITEM_ICO_DIM_A);
105 }
106
107 static Evas_Object *_util_grid_content_get(void *data,
108                 Evas_Object *obj, const char *part)
109 {
110         const char *path;
111         CSourceInfo *si;
112         Evas_Object *img;
113
114         if (!part || !data || !obj)
115                 return NULL;
116
117         si = (CSourceInfo *)data;
118
119         if (!strcmp(part, PART_ITEM_ICO)) {
120                 path = si->IconPath();
121                 if (!path)
122                         return NULL;
123
124                 img = elm_image_add(obj);
125                 if (!img)
126                         return NULL;
127
128                 elm_image_file_set(img, path, NULL);
129                 evas_object_show(img);
130                 return img;
131         }
132
133         return NULL;
134 }
135
136 static char *_util_grid_text_get(void *data, Evas_Object *obj, const char *part)
137 {
138         char *ret;
139         const char *cret;
140         CSourceInfo *si;
141
142         if (!part || !data)
143                 return NULL;
144
145         ret = NULL;
146         si = (CSourceInfo *)data;
147
148         if (!strcmp(part, PART_ITEM_NAME)) {
149                 cret = si->Name();
150                 ret = strdup(cret ? cret : NO_NAME);
151         } else if (!strcmp(part, PART_ITEM_TYPE)) {
152                 cret = si->TypeName();
153                 ret = strdup(cret ? cret : NO_TYPE);
154         }
155
156         return ret;
157 }
158
159 static void _util_grid_item_del(void *data, Evas_Object *obj)
160 {
161         Ecore_Timer *timer;
162         Elm_Object_Item *it;
163         CSourceInfo *si;
164
165         if (!data)
166                 return;
167
168         si = (CSourceInfo *)data;
169         it = (Elm_Object_Item *)si->Data();
170
171         timer = (Ecore_Timer*)elm_object_item_data_get(it);
172         if (!timer)
173                 return;
174
175         ecore_timer_del(timer);
176 }
177
178 static void _util_grid_sel(void *data, Evas_Object *obj, void *event_info)
179 {
180         CSourceInfo *si;
181
182         if (!data)
183                 return;
184
185         si = (CSourceInfo *)data;
186
187         if (!si->SwitchTo()) {
188                 _ERR("Switch to source failed.");
189                 return;
190         }
191         elm_gengrid_item_selected_set((Elm_Object_Item*)event_info, EINA_FALSE);
192 }
193
194
195 void util_add_item(Evas_Object *grid, CSourceInfo *si)
196 {
197         Elm_Object_Item *it;
198         Elm_Gengrid_Item_Class *ic;
199
200         if (!grid || !si) {
201                 _ERR("Parameter is NULL.");
202                 return;
203         }
204
205         ic = elm_gengrid_item_class_new();
206         if (!ic) {
207                 _ERR("Create item class failed.");
208                 return;
209         }
210
211         ic->item_style = "source";
212         ic->func.text_get = _util_grid_text_get;
213         ic->func.content_get = _util_grid_content_get;
214         ic->func.state_get = NULL;
215         ic->func.del = _util_grid_item_del;
216
217         it = elm_gengrid_item_append(grid, ic, si, _util_grid_sel, si);
218
219         si->SetData(it);
220
221         elm_gengrid_item_class_free(ic);
222 }