add restricting img history item to 10
[framework/uifw/cbhm.git] / src / clipdrawer.c
1 #include "common.h"
2 #include "cbhm_main.h"
3 #include "storage.h"
4 #include "xcnphandler.h"
5 #include "clipdrawer.h"
6
7 #define IM      "/mnt/ums/Images/Photo/"
8 static const char *g_images_path[] = {
9         IM"1_photo.jpg",
10 /*
11         IM"2_photo.jpg",
12         IM"3_photo.jpg",
13         IM"4_photo.jpg",
14         IM"5_photo.jpg",
15         IM"6_photo.jpg",
16 */
17 };
18 #define N_IMAGES (1)
19
20 // gic should live at gengrid callback functions
21 Elm_Gengrid_Item_Class gic;
22
23 typedef struct tag_gridimgitem
24 {
25         Elm_Gengrid_Item *item;
26         const char *path;
27 } gridimgitem_t;
28
29 static void _list_click( void *data, Evas_Object *obj, void *event_info )
30 {
31         struct appdata *ad = data;
32     Elm_List_Item *it = (Elm_List_Item *) elm_list_selected_item_get(obj);
33         if (it == NULL)
34                 return;
35
36     elm_list_item_selected_set(it, 0);
37
38         char *p = NULL;
39         int cplen;
40
41         char *cpdata = NULL;
42         cpdata = elm_list_item_label_get(it);
43         if (cpdata == NULL)
44                 return;
45         cplen = strlen(cpdata);
46         p = malloc(cplen + 1);
47         snprintf(p, cplen+1, "%s", cpdata);
48         elm_selection_set(1, obj, /*ELM_SEL_FORMAT_TEXT*/1, p);
49 //      elm_selection_set(1, obj, /*ELM_SEL_FORMAT_MARKUP*/2, p);
50
51 //      clipdrawer_lower_view(ad);
52 }
53
54 int clipdrawer_update_contents(void *data)
55 {
56         struct appdata *ad = data;
57         int i, pos;
58
59         elm_list_clear(ad->txtlist);
60         for (i = 0; i < HISTORY_QUEUE_NUMBER; i++)
61         {
62                 pos = get_current_history_position()+i;
63                 if (pos > HISTORY_QUEUE_NUMBER-1)
64                         pos = pos-HISTORY_QUEUE_NUMBER;
65                 if (get_item_contents_by_pos(pos) != NULL && strlen(get_item_contents_by_pos(pos)) > 0)
66                 {
67                         elm_list_item_append(ad->txtlist, get_item_contents_by_pos(pos), NULL, NULL, NULL, ad);
68                 }
69         }
70         elm_list_go(ad->txtlist);
71
72         /* FIXME : sometimes when list update, screen isn't updated */
73
74         return 0;
75 }
76
77 const char* clipdrawer_get_plain_string_from_escaped(char *escstr)
78 {
79         /* TODO : is it should be here? besides, remove dependency for entry */
80         /* NOTE : return string should be freed */
81         return elm_entry_markup_to_utf8(escstr);
82 }
83
84 Evas_Object* grid_icon_get(const void *data, Evas_Object *obj, const char *part)
85 {
86         gridimgitem_t *ti = (gridimgitem_t *)data;
87         if (!strcmp(part, "elm.swallow.icon"))
88         {
89                 Evas_Object *icon = elm_icon_add(obj);
90                 elm_icon_file_set(icon, ti->path, NULL);
91                 evas_object_size_hint_aspect_set(icon, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
92                 evas_object_show(icon);
93                 return icon;
94         }
95         return NULL;
96 }
97
98 static void grid_selected(void *data, Evas_Object *obj, void *event_info)
99 {
100         struct appdata *ad = data;
101         char *file, *p;
102         int len;
103         Elm_Gengrid_Item *sgobj = NULL;
104         sgobj = elm_gengrid_selected_item_get(ad->imggrid);
105         gridimgitem_t *ti = NULL;
106         ti = elm_gengrid_item_data_get(sgobj);
107
108         if (!sgobj || !ti)
109         {
110                 DTRACE("ERR: cbhm can't get the selected image\n");
111                 return;
112         }
113         len = strlen(ti->path);
114         p = malloc(len + 10);
115         snprintf(p,len+10, "file:///%s", ti->path);
116
117         elm_selection_set(/*secondary*/1,obj,/*ELM_SEL_FORMAT_IMAGE*/4,p);
118
119 //      clipdrawer_lower_view(ad);
120
121         elm_gengrid_item_selected_set(sgobj, EINA_FALSE);
122 }
123
124 void grid_del(const void *data, Evas_Object *obj)
125 {
126         gridimgitem_t *ti = (gridimgitem_t *)data;
127         eina_stringshare_del(ti->path);
128         free(ti);
129 }
130
131 // FIXME: how to remove main_ad? 
132 //        it's mainly used at 'clipdrawer_add_image_item'
133 int clipdrawer_add_image_item(char *imagepath)
134 {
135         struct appdata *ad = g_get_main_appdata();
136         gridimgitem_t *newgenimg = NULL;
137         char* filepath = NULL;
138         Eina_List *igl = NULL;
139         unsigned int igl_counter = 0;
140         filepath = &imagepath[7]; // skip 'file://'
141
142         igl = elm_gengrid_items_get(ad->imggrid);
143         igl_counter = eina_list_count(igl);
144         if (igl_counter >= 10)
145         {
146                 elm_gengrid_item_del(eina_list_nth(igl, 0));
147         }
148
149         newgenimg = malloc(sizeof(gridimgitem_t));
150         newgenimg->path = eina_stringshare_add(filepath);
151         newgenimg->item = elm_gengrid_item_append(ad->imggrid, &gic, newgenimg, NULL, NULL);
152         
153         return TRUE;
154 }
155
156 static void
157 clipdrawer_ly_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
158 {
159         struct appdata *ad = data;
160
161         #define EDJE_CLOSE_PART_PREFIX "closebutton/"
162         if (!strncmp(source, EDJE_CLOSE_PART_PREFIX, strlen(EDJE_CLOSE_PART_PREFIX)))
163         {
164                 clipdrawer_lower_view(ad);
165         }
166 }
167
168 int clipdrawer_init(void *data)
169 {
170         struct appdata *ad = data;
171
172         evas_object_resize(ad->win_main, CLIPDRAWER_WIDTH, CLIPDRAWER_HEIGHT);
173         evas_object_move(ad->win_main, CLIPDRAWER_POS_X, CLIPDRAWER_POS_Y);
174         evas_object_resize(ad->ly_main, CLIPDRAWER_WIDTH, CLIPDRAWER_HEIGHT);
175         evas_object_move(ad->ly_main, CLIPDRAWER_POS_X, CLIPDRAWER_POS_Y);
176
177         edje_object_signal_callback_add(elm_layout_edje_get(ad->ly_main), "mouse,up,1", "*", clipdrawer_ly_clicked, ad);
178
179         ad->imggrid = NULL;
180         ad->imggrid = elm_gengrid_add(ad->win_main);
181         elm_layout_content_set(ad->ly_main, "imagehistory/list", ad->imggrid);
182         elm_gengrid_item_size_set(ad->imggrid, 100, 100+3);
183         elm_gengrid_align_set(ad->imggrid, 0.0, 0.0);
184         elm_gengrid_horizontal_set(ad->imggrid, EINA_TRUE);
185         elm_gengrid_bounce_set(ad->imggrid, EINA_TRUE, EINA_FALSE);
186         elm_gengrid_multi_select_set(ad->imggrid, EINA_FALSE);
187         evas_object_smart_callback_add(ad->imggrid, "selected", grid_selected, ad);
188         evas_object_size_hint_weight_set(ad->imggrid, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
189
190         elm_gengrid_clear(ad->imggrid);
191
192         gic.item_style = "default_grid";
193         gic.func.label_get = NULL;
194         gic.func.icon_get = grid_icon_get;
195         gic.func.state_get = NULL;
196         gic.func.del = grid_del;
197
198         int i;
199         gridimgitem_t *newgenimg;
200
201         for (i = 0; i < N_IMAGES; i++)
202         {
203                 newgenimg = malloc(sizeof(gridimgitem_t));
204                 newgenimg->path = eina_stringshare_add(g_images_path[i]);
205                 newgenimg->item = elm_gengrid_item_append(ad->imggrid, &gic, newgenimg, NULL, NULL);
206 //              evas_object_data_set(newgenimg->item, "URI", g_images_path[i]);
207         }
208
209         evas_object_show (ad->imggrid);
210
211 // for debugging, calc image history pos
212 /*
213    Evas_Coord x, y, w, h;
214    Evas_Coord vx, vy, vw, vh;
215
216    edje_object_part_geometry_get(elm_layout_edje_get(ad->ly_main),"imagehistory/list",&x,&y,&w,&h);
217    evas_object_geometry_get (ad->imggrid, &vx,&vy,&vw,&vh);
218    fprintf(stderr, "## x = %d, y = %d, w = %d, h = %d\n", x, y, w, h);
219    fprintf(stderr, "## vx = %d, vy = %d, vw = %d, vh = %d\n", vx, vy, vw, vh);
220 */
221
222         ad->txtlist = elm_list_add(ad->win_main);
223         elm_layout_content_set(ad->ly_main, "texthistory/list", ad->txtlist);
224         elm_theme_extension_add(NULL, APP_EDJ_FILE);
225         elm_object_style_set(ad->txtlist, "extended/historylist");
226         evas_object_size_hint_weight_set(ad->txtlist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
227         evas_object_smart_callback_add(ad->txtlist, "selected", _list_click, ad);
228         elm_list_item_append(ad->txtlist, "default", NULL, NULL, NULL, ad);
229
230         elm_list_go(ad->txtlist);
231
232         if (get_item_counts() != 0)
233                 clipdrawer_update_contents(ad);
234
235         return 0;
236 }
237
238 int clipdrawer_create_view(void *data)
239 {
240         struct appdata *ad = data;
241
242         clipdrawer_init(ad);
243
244         // for debug
245         // at starting, showing app view
246         //clipdrawer_activate_view(ad);
247
248         return 0;
249 }
250
251 void clipdrawer_activate_view(void *data)
252 {
253         struct appdata *ad = data;
254         
255         if (ad->win_main)
256         {
257                 evas_object_show(ad->win_main);
258                 elm_win_activate(ad->win_main);
259         }
260 }
261
262 void clipdrawer_lower_view(void *data)
263 {
264         struct appdata *ad = data;
265         
266         if (ad->win_main)
267         {
268                 evas_object_hide(ad->win_main);
269                 elm_win_lower(ad->win_main);
270         }
271 }