add textonly mode feature
[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 DELETE_ICON_PATH "/usr/share/cbhm/icons/05_delete.png"
8 #define IM      "/usr/share/cbhm/icons/"
9 static const char *g_images_path[] = {
10         IM"cbhm_default_img.png",
11 };
12 #define N_IMAGES (1)
13
14 #define GRID_ITEM_SPACE_W 6
15 #define GRID_ITEM_SINGLE_W 187
16 #define GRID_ITEM_SINGLE_H 151
17 #define GRID_ITEM_W (GRID_ITEM_SINGLE_W+(GRID_ITEM_SPACE_W*2))
18 #define GRID_ITEM_H (GRID_ITEM_SINGLE_H)
19 #define GRID_IMAGE_LIMIT_W 91
20 #define GRID_IMAGE_LIMIT_H 113
21
22 // gic should live at gengrid callback functions
23 Elm_Gengrid_Item_Class gic;
24
25 typedef struct tag_griditem
26 {
27         int itype;
28         Elm_Gengrid_Item *item;
29         const char *ipathdata;
30         Eina_Strbuf *istrdata;
31         Evas_Object *delbtn;
32         Evas_Object *ilayout;
33 } griditem_t;
34
35
36 int clipdrawer_update_contents(void *data)
37 {
38         struct appdata *ad = data;
39         int i, pos;
40         char *unesc = NULL;
41
42         for (i = 0; i < HISTORY_QUEUE_MAX_ITEMS; i++)
43         {
44                 pos = get_current_history_position() - i;
45                 if (pos < 0)
46                         pos = pos+HISTORY_QUEUE_MAX_ITEMS;
47
48                 if (get_item_contents_by_pos(pos) != NULL && strlen(get_item_contents_by_pos(pos)) > 0)
49                 {
50                         unesc = clipdrawer_get_plain_string_from_escaped(get_item_contents_by_pos(pos));
51                         unesc = unesc ? unesc : "";
52                         elm_list_item_append(ad->txtlist, unesc, NULL, NULL, NULL, ad);
53                         free(unesc);
54                 }
55         }
56
57         /* FIXME : sometimes when list update, screen isn't updated */
58
59         return 0;
60 }
61
62 const char* clipdrawer_get_plain_string_from_escaped(char *escstr)
63 {
64         /* TODO : is it should be here? besides, remove dependency for entry */
65         /* NOTE : return string should be freed */
66         return elm_entry_markup_to_utf8(escstr);
67 }
68
69 static void _grid_del_response_cb(void *data, Evas_Object *obj, void *event_info)
70 {
71         Elm_Gengrid_Item *it = (Elm_Gengrid_Item *)data;
72         evas_object_del(obj);
73
74         if((int)event_info == ELM_POPUP_RESPONSE_OK)
75                 elm_gengrid_item_del(it);
76 }
77
78 static void _grid_click_delete(void *data, Evas_Object *obj, void *event_info)
79 {
80         struct appdata *ad = data;
81 }
82
83 static void
84 _grid_item_ly_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
85 {
86         struct appdata *ad = g_get_main_appdata();
87
88         Elm_Gengrid_Item *sgobj = NULL;
89         sgobj = elm_gengrid_selected_item_get(ad->hig);
90         griditem_t *ti = NULL;
91         ti = elm_gengrid_item_data_get(sgobj);
92
93         #define EDJE_DELBTN_PART_PREFIX "delbtn"
94         if (strncmp(source, EDJE_DELBTN_PART_PREFIX, strlen(EDJE_DELBTN_PART_PREFIX)))
95         {
96                 if (!sgobj || !ti)
97                 {
98                         DTRACE("ERR: cbhm can't get the selected image\n");
99                         return;
100                 }
101
102                 elm_gengrid_item_selected_set(sgobj, EINA_FALSE);
103
104                 if (ti->itype == GI_TEXT)
105                 {
106                         char *p = strdup(eina_strbuf_string_get(ti->istrdata));
107
108                         elm_selection_set(1, obj, /*ELM_SEL_FORMAT_TEXT*/1, p);
109                 }
110                 else //if (ti->itype == GI_IMAGE)
111                 {
112                         if (!clipdrawer_paste_textonly_get(ad))
113                         {
114                                 int len = strlen(ti->ipathdata);
115                                 char *p = malloc(len + 10);
116                                 snprintf(p,len+10, "file:///%s", ti->ipathdata);
117
118                                 elm_selection_set(/*secondary*/1,obj,/*ELM_SEL_FORMAT_IMAGE*/4,p);
119                         }
120                         else
121                         {
122                                 DTRACE("ERR: cbhm image paste mode is false\n");
123                         }
124                 }
125
126                 return;
127         }
128
129         if (!sgobj)
130         {
131                 DTRACE("ERR: cbhm can't get the selected item\n");
132                 return;
133         }
134
135         elm_gengrid_item_selected_set(sgobj, EINA_FALSE);
136
137         Evas_Object *popup = elm_popup_add(ad->win_main);
138         elm_popup_timeout_set(popup, 5);
139         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
140         elm_popup_desc_set(popup, "Are you sure delete this?");
141         elm_popup_buttons_add(popup, 2,
142                                                   "Yes", ELM_POPUP_RESPONSE_OK,
143                                                   "No", ELM_POPUP_RESPONSE_CANCEL,
144                                                   NULL);
145         evas_object_smart_callback_add(popup, "response", _grid_del_response_cb, sgobj);
146         evas_object_show(popup);
147 }
148
149 Evas_Object* _grid_icon_get(const void *data, Evas_Object *obj, const char *part)
150 {
151         griditem_t *ti = (griditem_t *)data;
152
153         if (!strcmp(part, "elm.swallow.icon"))
154         {
155                 if (ti->itype == GI_TEXT)
156                 {
157                         Evas_Object *layout = elm_layout_add (obj);
158                         elm_layout_theme_set(layout, "gengrid", "widestyle", "horizontal_layout");
159                         edje_object_signal_callback_add(elm_layout_edje_get(layout), 
160                                                                                         "mouse,up,1", "*", _grid_item_ly_clicked, data);
161                         Evas_Object *rect = evas_object_rectangle_add(evas_object_evas_get(obj));
162                         evas_object_resize(rect, GRID_ITEM_W, GRID_ITEM_H);
163                         evas_object_color_set(rect, 242, 233, 183, 255);
164                         evas_object_show(rect);
165                         elm_layout_content_set(layout, "elm.swallow.icon", rect);
166
167                         // FIXME: add string length check
168                         Evas_Object *ientry = elm_scrolled_entry_add(obj);
169                         evas_object_size_hint_weight_set(ientry, 0, 0);
170                         Eina_Strbuf *strent = NULL;
171                         char *strdata = eina_strbuf_string_get(ti->istrdata);
172                         int i, skipflag, strcnt;
173                         
174                         strent = eina_strbuf_new();
175                         skipflag = 0;
176                         strcnt = 0;
177                         for (i = 0; i < eina_strbuf_length_get(ti->istrdata); i++)
178                         {
179                                 switch (strdata[i])
180                                 {
181                                         case '>':
182                                                 skipflag = 0;
183                                                 break;
184                                         case '<':
185                                                 skipflag = 1;
186                                                 break;
187                                         default:
188                                                 if (!skipflag)
189                                                         strcnt++;
190                                                 break;
191                                 }
192                                 if (strcnt > 100)
193                                         break;
194                         }
195                         eina_strbuf_append_n(strent, strdata, i);
196 //                      eina_strbuf_append(strent, strdata);
197                         eina_strbuf_replace_all(strent, " absize=240x180 ", " absize=52x39 ");
198                         if (strcnt > 100)
199                                 eina_strbuf_append(strent, "...");
200
201                         elm_scrolled_entry_entry_set(ientry, eina_strbuf_string_get(strent));
202                         elm_scrolled_entry_editable_set(ientry, EINA_FALSE);
203                         elm_scrolled_entry_context_menu_disabled_set(ientry, EINA_TRUE);
204                         evas_object_show(ientry);
205                         elm_layout_content_set(layout, "elm.swallow.inner", ientry);
206
207                         eina_strbuf_free(strent);
208
209                         return layout;
210                 }
211                 else// if (ti->itype == GI_IMAGE)
212                 {
213                         Evas_Object *layout = elm_layout_add (obj);
214                         elm_layout_theme_set(layout, "gengrid", "widestyle", "horizontal_layout");
215                         edje_object_signal_callback_add(elm_layout_edje_get(layout), 
216                                                                                         "mouse,up,1", "*", _grid_item_ly_clicked, data);
217
218                         Evas_Object *sicon;
219                         sicon = evas_object_image_add(evas_object_evas_get(obj));
220                         evas_object_image_load_size_set(sicon, GRID_ITEM_SINGLE_W, GRID_ITEM_SINGLE_H);
221                         evas_object_image_file_set(sicon, ti->ipathdata, NULL);
222                         evas_object_image_fill_set(sicon, 0, 0, GRID_ITEM_SINGLE_W, GRID_ITEM_SINGLE_H);
223                         evas_object_resize(sicon, GRID_ITEM_SINGLE_W, GRID_ITEM_SINGLE_H);
224                         elm_layout_content_set(layout, "elm.swallow.icon", sicon);
225
226                         struct appdata *ad = g_get_main_appdata();
227                         
228                         if (!clipdrawer_paste_textonly_get(ad))
229                         {
230                                 edje_object_signal_emit(elm_layout_edje_get(layout), "elm,state,hide,delbtn", "elm");
231                                 Evas_Object *rect = evas_object_rectangle_add(evas_object_evas_get(obj));
232                                 evas_object_color_set(rect, 0, 0, 0, 200);
233                                 evas_object_show(rect);
234                                 elm_layout_content_set(layout, "elm.swallow.cover", rect);
235                         }
236
237                         ti->ilayout = layout;
238                         return layout;
239
240 /*
241                         Evas_Object *icon = elm_icon_add(obj);
242                         elm_icon_file_set(icon, ti->ipathdata, NULL);
243                         evas_object_size_hint_aspect_set(icon, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
244                         evas_object_show(icon);
245 */
246
247 /*
248                         Ecore_Evas *my_ee;
249                         Evas *my_e;
250                         Evas_Object *fgimg;
251                         Evas_Object *bgrect;
252                         Evas_Object *delbtn;
253                         Evas_Object *icon;
254                         my_ee = ecore_evas_buffer_new(GRID_ITEM_SINGLE_W, GRID_ITEM_SINGLE_H);
255                         my_e = ecore_evas_get(my_ee);
256
257                         bgrect = evas_object_rectangle_add(my_e);
258                         evas_object_color_set(bgrect, 255, 255, 255, 255);
259                         evas_object_resize(bgrect, GRID_ITEM_SINGLE_W, GRID_ITEM_SINGLE_H);
260                         evas_object_move(bgrect, 0, 0);
261                         evas_object_show(bgrect);
262
263 #define BORDER_SIZE 1
264                         fgimg = evas_object_image_add(my_e);
265                         evas_object_image_load_size_set(fgimg, GRID_ITEM_SINGLE_W-BORDER_SIZE*2, GRID_ITEM_SINGLE_H-BORDER_SIZE*2);
266                         evas_object_image_file_set(fgimg, ti->ipathdata, NULL);
267                         evas_object_image_fill_set(fgimg, 0, 0, GRID_ITEM_SINGLE_W-BORDER_SIZE*2, GRID_ITEM_SINGLE_H-BORDER_SIZE*2);
268                         evas_object_image_filled_set(fgimg, 1);
269                         int x,y;
270                         evas_object_image_size_get(fgimg, &x, &y);
271                         //fprintf(stderr, "## img x = %d, y = %d\n", x, y);
272                         evas_object_resize(fgimg, GRID_ITEM_SINGLE_W-BORDER_SIZE*2, GRID_ITEM_SINGLE_H-BORDER_SIZE*2);
273                         evas_object_move(fgimg, BORDER_SIZE, BORDER_SIZE);
274                         evas_object_show(fgimg);
275
276                         icon = evas_object_image_add(evas_object_evas_get(obj));
277
278                         evas_object_image_data_set(icon, NULL);
279                         evas_object_image_size_set(icon, GRID_ITEM_SINGLE_W, GRID_ITEM_SINGLE_H);
280                         evas_object_image_fill_set(icon, 0, 0, GRID_ITEM_SINGLE_W, GRID_ITEM_SINGLE_H);
281                         evas_object_image_filled_set(icon, EINA_TRUE);
282                         evas_object_image_data_copy_set(icon, (int *)ecore_evas_buffer_pixels_get(my_ee));
283                         evas_object_image_data_update_add(icon, 0, 0, GRID_ITEM_SINGLE_W, GRID_ITEM_SINGLE_H);
284
285                         evas_object_del(bgrect);
286                         evas_object_del(fgimg);
287                         ecore_evas_free(my_ee);
288
289                         return icon;
290 */
291                 }
292
293 //              return icon;
294         }
295 /*
296         else if (!strcmp(part, "elm.swallow.end"))
297         {
298                 ti->delbtn = elm_check_add(obj);
299                 elm_object_style_set(ti->delbtn, "extended/itemcheck");
300                 //evas_object_propagate_events_set(ti->delbtn, 0);
301                 elm_check_state_set(ti->delbtn, tcm);
302                 evas_object_smart_callback_add(ti->delbtn, "changed", _grid_item_check_changed, data);
303                 evas_object_show(ti->delbtn);
304                 return ti->delbtn;
305         }
306 */
307            
308         return NULL;
309 }
310
311 static void _grid_longpress(void *data, Evas_Object *obj, void *event_info)
312 {
313         struct appdata *ad = data;
314         clipdrawer_change_mode(ad);
315 }
316
317 static void _grid_click_paste(void *data, Evas_Object *obj, void *event_info)
318 {
319         struct appdata *ad = data;
320         Elm_Gengrid_Item *sgobj = NULL;
321         sgobj = elm_gengrid_selected_item_get(ad->hig);
322         griditem_t *ti = NULL;
323         ti = elm_gengrid_item_data_get(sgobj);
324
325         fprintf(stderr, "## grid_click_paste = 0x%x\n", event_info);
326 }
327
328 void _grid_del(const void *data, Evas_Object *obj)
329 {
330         griditem_t *ti = (griditem_t *)data;
331         if (ti->itype == GI_TEXT)
332                 eina_strbuf_free(ti->istrdata);
333         else
334                 eina_stringshare_del(ti->ipathdata);
335         free(ti);
336 }
337
338 // FIXME: how to remove calling g_get_main_appdata()? 
339 //        it's mainly used at 'clipdrawer_image_item'
340 int clipdrawer_add_item(char *idata, int type)
341 {
342         struct appdata *ad = g_get_main_appdata();
343         griditem_t *newgi = NULL;
344
345         newgi = malloc(sizeof(griditem_t));
346         newgi->itype = type;
347
348         fprintf(stderr, "## add - %d : %s\n", newgi->itype, idata);
349         if (type == GI_TEXT)
350         {
351                 newgi->istrdata = eina_strbuf_new();
352                 eina_strbuf_append(newgi->istrdata, idata);
353         }
354         else //if (type == GI_IMAGE)
355         {
356                 Elm_Gengrid_Item *item = elm_gengrid_first_item_get(ad->hig);
357                 griditem_t *ti = NULL;
358
359                 if (!check_regular_file(idata))
360                 {
361                         DTRACE("Error : it isn't normal file = %s\n", idata);
362                         return -1;
363                 }
364
365                 while (item)    
366                 {
367                         ti = elm_gengrid_item_data_get(item);
368                         if ((ti->itype == type) && !strcmp(ti->ipathdata, idata))
369                         {
370                                 DTRACE("Error : duplicated file path = %s\n", idata);
371                                 return -1;
372                         }
373                         item = elm_gengrid_item_next_get(item);      
374                 }
375
376                 newgi->ipathdata = eina_stringshare_add(idata);
377         }
378
379         if (ad->hicount >= HISTORY_QUEUE_MAX_ITEMS)
380         {
381                 ad->hicount--;
382                 // FIXME: add routine that is removing its elements
383                 elm_gengrid_item_del(elm_gengrid_last_item_get(ad->hig));
384         }
385
386         ad->hicount++;
387         newgi->item = elm_gengrid_item_prepend(ad->hig, &gic, newgi, NULL, NULL);
388
389         return TRUE;
390 }
391
392 static void
393 clipdrawer_ly_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
394 {
395         struct appdata *ad = data;
396
397         #define EDJE_CLOSE_PART_PREFIX "background/close"
398         if (!strncmp(source, EDJE_CLOSE_PART_PREFIX, strlen(EDJE_CLOSE_PART_PREFIX)))
399         {
400                 clipdrawer_lower_view(ad);
401         }
402 }
403
404 int clipdrawer_init(void *data)
405 {
406         struct appdata *ad = data;
407         double cdy, cdw;
408
409         ad->hicount = 0;
410         ad->pastetextonly = EINA_TRUE;
411
412         // for elm_check
413         elm_theme_extension_add(NULL, APP_EDJ_FILE);
414
415         cdy = (1.0*CLIPDRAWER_HEIGHT/SCREEN_HEIGHT)*ad->root_h;
416         cdw = (1.0*CLIPDRAWER_POS_Y/SCREEN_HEIGHT)*ad->root_h;
417
418         evas_object_resize(ad->win_main, ad->root_w, (int)cdy);
419         evas_object_move(ad->win_main, CLIPDRAWER_POS_X, (int)cdw);
420         evas_object_resize(ad->ly_main, ad->root_w, (int)cdy);
421         evas_object_move(ad->ly_main, CLIPDRAWER_POS_X, (int)cdw);
422
423         edje_object_signal_callback_add(elm_layout_edje_get(ad->ly_main), 
424                                                                         "mouse,up,1", "*", clipdrawer_ly_clicked, ad);
425
426         ad->hig = NULL;
427         ad->hig = elm_gengrid_add(ad->win_main);
428         elm_layout_content_set(ad->ly_main, "historyitems", ad->hig);
429         elm_gengrid_item_size_set(ad->hig, GRID_ITEM_W+2, GRID_ITEM_H);
430         elm_gengrid_align_set(ad->hig, 0.5, 0.5);
431         elm_gengrid_horizontal_set(ad->hig, EINA_TRUE);
432         elm_gengrid_bounce_set(ad->hig, EINA_TRUE, EINA_FALSE);
433         elm_gengrid_multi_select_set(ad->hig, EINA_FALSE);
434         evas_object_smart_callback_add(ad->hig, "selected", _grid_click_paste, ad);
435 //      evas_object_smart_callback_add(ad->hig, "longpressed", _grid_longpress, ad);
436         evas_object_size_hint_weight_set(ad->hig, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
437
438         elm_gengrid_clear(ad->hig);
439
440         gic.item_style = "default_grid";
441         gic.func.label_get = NULL;
442         gic.func.icon_get = _grid_icon_get;
443         gic.func.state_get = NULL;
444         gic.func.del = _grid_del;
445
446         int i;
447         griditem_t *newgi;
448
449         for (i = 0; i < N_IMAGES; i++)
450         {
451                 clipdrawer_add_item(g_images_path[0], GI_IMAGE);
452         }
453
454         clipdrawer_add_item("clipboard history", GI_TEXT);
455
456         evas_object_show (ad->hig);
457
458 // for debugging, calc history pos
459 /*
460    Evas_Coord x, y, w, h;
461    Evas_Coord vx, vy, vw, vh;
462
463    edje_object_part_geometry_get(elm_layout_edje_get(ad->ly_main),"imagehistory/list",&x,&y,&w,&h);
464    evas_object_geometry_get (ad->hig, &vx,&vy,&vw,&vh);
465    fprintf(stderr, "## x = %d, y = %d, w = %d, h = %d\n", x, y, w, h);
466    fprintf(stderr, "## vx = %d, vy = %d, vw = %d, vh = %d\n", vx, vy, vw, vh);
467 */
468
469 //      if (get_item_counts() != 0)
470 //              clipdrawer_update_contents(ad);
471
472         return 0;
473 }
474
475 int clipdrawer_create_view(void *data)
476 {
477         struct appdata *ad = data;
478
479         clipdrawer_init(ad);
480
481         // for debug
482         // at starting, showing app view
483         // clipdrawer_activate_view(ad);
484
485         return 0;
486 }
487
488 void clipdrawer_activate_view(void *data)
489 {
490         struct appdata *ad = data;
491         
492         if (ad->win_main)
493         {
494                 set_transient_for(ad);
495                 evas_object_show(ad->win_main);
496                 elm_win_activate(ad->win_main);
497         }
498 }
499
500 void clipdrawer_lower_view(void *data)
501 {
502         struct appdata *ad = data;
503         
504         if (ad->win_main)
505         {
506                 unset_transient_for(ad);
507                 evas_object_hide(ad->win_main);
508                 elm_win_lower(ad->win_main);
509         }
510 }
511
512 void _change_gengrid_paste_textonly_mode(void *data)
513 {
514         struct appdata *ad = data;
515
516         fprintf(stderr, "## _change_gengrid_paste_textonly_mode = %d\n", clipdrawer_paste_textonly_get(ad));
517
518         Elm_Gengrid_Item *item;
519         griditem_t *ti = NULL;
520
521         if (clipdrawer_paste_textonly_get(ad))
522         { // textonly paste mode
523                 Elm_Gengrid_Item *item = elm_gengrid_first_item_get(ad->hig);
524
525                 while (item)    
526                 {
527                         ti = elm_gengrid_item_data_get(item);
528                         if ((ti->itype == GI_IMAGE) && (ti->ilayout))
529                         {
530                                 fprintf(stderr, "## sig to hide delbtn\n");
531
532                                 edje_object_signal_emit(elm_layout_edje_get(ti->ilayout), "elm,state,hide,delbtn", "elm");
533                                 Evas_Object *rect = evas_object_rectangle_add(evas_object_evas_get(ad->hig));
534                                 evas_object_color_set(rect, 0, 0, 0, 200);
535                                 evas_object_show(rect);
536                                 elm_layout_content_set(ti->ilayout, "elm.swallow.cover", rect);
537                         }
538                         item = elm_gengrid_item_next_get(item);      
539                 }
540         }
541         else
542         { // text+image paste mode
543                 Elm_Gengrid_Item *item = elm_gengrid_first_item_get(ad->hig);
544
545                 while (item)    
546                 {
547                         ti = elm_gengrid_item_data_get(item);
548                         fprintf(stderr, "## itype = %d\n", ti->itype);
549                         fprintf(stderr, "## ilayout = 0x%x\n", ti->ilayout);
550                         if ((ti->itype == GI_IMAGE) && (ti->ilayout))
551                         {
552                                 fprintf(stderr, "## sig to show delbtn\n");
553
554                                 edje_object_signal_emit(elm_layout_edje_get(ti->ilayout), "elm,state,show,delbtn", "elm");
555                                 Evas_Object *rect = elm_layout_content_unset(ti->ilayout, "elm.swallow.cover");
556                                 evas_object_hide(rect);
557                                 evas_object_del(rect);
558                         }
559                         item = elm_gengrid_item_next_get(item);      
560                 }
561         }
562 }
563
564 void clipdrawer_paste_textonly_set(void *data, Eina_Bool textonly)
565 {
566         struct appdata *ad = data;
567         textonly = !!textonly;
568         if (ad->pastetextonly != textonly)
569                 ad->pastetextonly = textonly;
570         DTRACE("paste textonly mode = %d\n", textonly);
571
572         _change_gengrid_paste_textonly_mode(ad);
573 }
574
575 Eina_Bool clipdrawer_paste_textonly_get(void *data)
576 {
577         struct appdata *ad = data;
578         return ad->pastetextonly;
579 }