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