45a895628bf56e12e2eaafdbf63ba8c9ee0995d0
[framework/uifw/elementary.git] / src / modules / ctxpopup_copypasteUI / copypaste.c
1 #include <Elementary.h>
2 #include "elm_module_priv.h"
3 #include "elm_priv.h"
4 #include <appsvc/appsvc.h>
5 #include "cbhm_helper.h"
6
7 #define MULTI_(id) dgettext("sys_string", #id)
8 #define S_SELECT MULTI_(IDS_COM_SK_SELECT)
9 #define S_SELECT_ALL MULTI_(IDS_COM_BODY_SELECT_ALL)
10 #define S_COPY MULTI_(IDS_COM_BODY_COPY)
11 #define S_CUT MULTI_(IDS_COM_BODY_CUT)
12 #define S_PASTE MULTI_(IDS_COM_BODY_PASTE)
13 #define S_CLIPBOARD MULTI_(IDS_COM_BODY_CLIPBOARD)
14
15
16 Elm_Entry_Extension_data *ext_mod;
17 static int _mod_hook_count = 0;
18
19 typedef struct _Elm_Entry_Context_Menu_Item Elm_Entry_Context_Menu_Item;
20 struct _Elm_Entry_Context_Menu_Item
21 {
22    Evas_Object *obj;
23    const char *label;
24    const char *icon_file;
25    const char *icon_group;
26    Elm_Icon_Type icon_type;
27    Evas_Smart_Cb func;
28    void *data;
29 };
30
31 static void _ctxpopup_hide(Evas_Object *popup);
32 static void _ctxpopup_position(Evas_Object *obj);
33
34 static char *
35 _remove_tags(const char *str)
36 {
37    char *ret;
38    if (!str)
39      return NULL;
40
41    Eina_Strbuf *buf = eina_strbuf_new();
42    if (!buf)
43      return NULL;
44
45    if (!eina_strbuf_append(buf, str))
46      return NULL;
47
48    eina_strbuf_replace_all(buf, "<br>", " ");
49    eina_strbuf_replace_all(buf, "<br/>", " ");
50    eina_strbuf_replace_all(buf, "<ps>", " ");
51    eina_strbuf_replace_all(buf, "<ps/>", " ");
52
53    while (EINA_TRUE)
54      {
55         const char *temp = eina_strbuf_string_get(buf);
56
57         char *startTag = NULL;
58         char *endTag = NULL;
59
60         startTag = strstr(temp, "<");
61         if (startTag)
62           endTag = strstr(startTag, ">");
63         else
64           break;
65         if (!endTag || startTag > endTag)
66           break;
67
68         size_t sindex = startTag - temp;
69         size_t eindex = endTag - temp + 1;
70         if (!eina_strbuf_remove(buf, sindex, eindex))
71           break;
72      }
73    ret = eina_strbuf_string_steal(buf);
74    eina_strbuf_free(buf);
75    return ret;
76 }
77
78 static void
79 _entry_del_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
80 {
81    evas_object_del(data);
82 }
83
84 static void
85 _entry_hide_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
86 {
87    evas_object_hide(data);
88 }
89
90 static void
91 _entry_move_cb(void *data, Evas *e, Evas_Object *obj, void *event_info __UNUSED__)
92 {
93    if (evas_pointer_button_down_mask_get(e))
94      _ctxpopup_hide(data);
95    else
96      {
97         /*update*/
98         elm_entry_extension_module_data_get(obj, ext_mod);
99         _ctxpopup_position(data);
100      }
101 }
102
103 static void
104 _entry_resize_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
105 {
106    _ctxpopup_hide(data);
107 }
108
109 static void
110 _ctxpopup_hide(Evas_Object *popup)
111 {
112    evas_object_hide(popup);
113    evas_object_event_callback_del(ext_mod->caller, EVAS_CALLBACK_DEL, _entry_del_cb);
114    evas_object_event_callback_del(ext_mod->caller, EVAS_CALLBACK_HIDE, _entry_hide_cb);
115    evas_object_event_callback_del(ext_mod->caller, EVAS_CALLBACK_MOVE, _entry_move_cb);
116    evas_object_event_callback_del(ext_mod->caller, EVAS_CALLBACK_RESIZE, _entry_resize_cb);
117 }
118
119 static void
120 _ctxpopup_position(Evas_Object *obj)
121 {
122    if(!ext_mod) return;
123
124    Evas_Coord cx, cy, cw, ch, x, y, w, h;
125    elm_ctxpopup_direction_priority_set(ext_mod->popup, ELM_CTXPOPUP_DIRECTION_UP, ELM_CTXPOPUP_DIRECTION_DOWN, ELM_CTXPOPUP_DIRECTION_LEFT, ELM_CTXPOPUP_DIRECTION_RIGHT);
126    if (!edje_object_part_text_selection_geometry_get(ext_mod->ent, "elm.text", &x, &y, &w, &h))
127      {
128         evas_object_geometry_get(ext_mod->ent, &x, &y, NULL, NULL);
129         edje_object_part_text_cursor_geometry_get(ext_mod->ent, "elm.text",
130                                                   &cx, &cy, &cw, &ch);
131         evas_object_size_hint_min_get(ext_mod->popup, &w, &h);
132         if (cw < w)
133           {
134              cx += (cw - w) / 2;
135              cw = w;
136           }
137         if (ch < h)
138           {
139              cy += (ch - h) / 2;
140              ch = h;
141           }
142         evas_object_move(ext_mod->popup, x + cx, y + cy);
143         evas_object_resize(ext_mod->popup, cw, ch);
144      }
145    else
146      {
147         if (ext_mod->viewport_rect.x != -1 || ext_mod->viewport_rect.y != -1
148             || ext_mod->viewport_rect.w != -1 || ext_mod->viewport_rect.h != -1)
149           {
150              Evas_Coord vx, vy, vw, vh, x2, y2;
151              x2 = x + w;
152              y2 = y + h;
153              vx = ext_mod->viewport_rect.x;
154              vy = ext_mod->viewport_rect.y;
155              vw = ext_mod->viewport_rect.w;
156              vh = ext_mod->viewport_rect.h;
157
158              if (x < vx) x = vx;
159              if (y < vy) y = vy;
160              if (x2 > vx + vw) x2 = vx + vw;
161              if (y2 > vy + vh) y2 = vy + vh;
162              w = x2 - x;
163              h = y2 - y;
164           }
165         else
166           {
167              Evas_Coord sw, sh, x2, y2;
168              x2 = x + w;
169              y2 = y + h;
170              ecore_x_window_size_get(ecore_x_window_root_first_get(), &sw, &sh);
171
172              if (x < 0) x = 0;
173              if (y < 0) y = 0;
174              if (x2 > sw) x2 = sw;
175              if (y2 > sh) y2 = sh;
176              w = x2 - x;
177              h = y2 - y;
178           }
179         cx = x + (w / 2);
180         cy = y + (h / 2);
181         Elm_Ctxpopup_Direction dir = elm_ctxpopup_direction_get(ext_mod->popup);
182         if (dir != ELM_CTXPOPUP_DIRECTION_UNKNOWN)
183           {
184              if (dir == ELM_CTXPOPUP_DIRECTION_UP)
185                cy = y + (h / 5);
186              else if (dir == ELM_CTXPOPUP_DIRECTION_DOWN)
187                {
188                   elm_ctxpopup_direction_priority_set(ext_mod->popup, ELM_CTXPOPUP_DIRECTION_DOWN, ELM_CTXPOPUP_DIRECTION_UP, ELM_CTXPOPUP_DIRECTION_LEFT, ELM_CTXPOPUP_DIRECTION_RIGHT);
189                   cy = y + h;
190                }
191           }
192         evas_object_move(ext_mod->popup, cx, cy);
193      }
194 }
195
196 static void
197 _select_all(void *data, Evas_Object *obj, void *event_info)
198 {
199    if(!ext_mod) return;
200
201    ext_mod->selectall(data,obj,event_info);
202    _ctxpopup_hide(obj);
203 }
204
205 static void
206 _select(void *data, Evas_Object *obj, void *event_info)
207 {
208    if(!ext_mod) return;
209
210    ext_mod->select(data,obj,event_info);
211    _ctxpopup_hide(obj);
212 }
213
214 static void
215 _paste(void *data, Evas_Object *obj, void *event_info)
216 {
217    if(!ext_mod) return;
218
219    ext_mod->paste(data,obj,event_info);
220    _ctxpopup_hide(obj);
221 }
222
223 static void
224 _cut(void *data, Evas_Object *obj, void *event_info)
225 {
226    if(!ext_mod) return;
227
228    ext_mod->cut(data,obj,event_info);
229    _ctxpopup_hide(obj);
230    //elm_object_scroll_freeze_pop(ext_mod->popup);
231 }
232
233 static void
234 _copy(void *data, Evas_Object *obj, void *event_info)
235 {
236    if(!ext_mod) return;
237
238    ext_mod->copy(data,obj,event_info);
239    _ctxpopup_hide(obj);
240    //elm_object_scroll_freeze_pop(ext_mod->popup);
241 }
242
243 static void
244 _cancel(void *data, Evas_Object *obj, void *event_info)
245 {
246    if(!ext_mod) return;
247
248    ext_mod->cancel(data,obj,event_info);
249    _ctxpopup_hide(obj);
250    //elm_object_scroll_freeze_pop(ext_mod->popup);
251 }
252
253 static void
254 _search_menu(void *data, Evas_Object *obj, void *event_info)
255 {
256    if(!ext_mod) return;
257
258    int ret;
259    bundle *b = bundle_create();
260    if (!b)
261      {
262         //printf("bundle_create() failed\n");
263         return;
264      }
265
266    appsvc_set_operation(b, APPSVC_OPERATION_SEARCH);
267    if (ext_mod->selmode)
268      {
269         const char *selection = elm_entry_selection_get(ext_mod->caller);
270         if (selection)
271           {
272              char *str = _remove_tags(selection);
273              if (str)
274                {
275                   appsvc_add_data(b, APPSVC_DATA_KEYWORD, str);
276                   free(str);
277                }
278              else
279                appsvc_add_data(b, APPSVC_DATA_KEYWORD, selection);
280           }
281      }
282    appsvc_run_service(b, 0, NULL, NULL);
283    bundle_free(b);
284    _ctxpopup_hide(obj);
285 }
286
287 static void
288 _clipboard_menu(void *data, Evas_Object *obj, void *event_info)
289 {
290    if(!ext_mod) return;
291
292    // start for cbhm
293 #ifdef HAVE_ELEMENTARY_X
294    ecore_x_selection_secondary_set(elm_win_xwindow_get(obj), "",1);
295 #endif
296    ext_mod->cnpinit(data,obj,event_info);
297    if (ext_mod->cnp_mode != ELM_CNP_MODE_MARKUP)
298      _cbhm_msg_send(obj, "show0");
299    else
300      _cbhm_msg_send(obj, "show1");
301    _ctxpopup_hide(obj);
302    // end for cbhm
303 }
304
305 static void
306 _item_clicked(void *data, Evas_Object *obj, void *event_info)
307 {
308    Elm_Entry_Context_Menu_Item *it = data;
309    Evas_Object *obj2 = it->obj;
310
311    if (it->func) it->func(it->data, obj2, NULL);
312    _ctxpopup_hide(obj);
313 }
314
315 static void
316 _ctxpopup_dismissed_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
317 {
318    if (!ext_mod) return;
319
320    //elm_object_scroll_freeze_pop(ext_mod->popup);
321 }
322
323 // module api funcs needed
324 EAPI int
325 elm_modapi_init(void *m)
326 {
327    return 1; // succeed always
328 }
329
330 EAPI int
331 elm_modapi_shutdown(void *m)
332 {
333    return 1; // succeed always
334 }
335
336 // module funcs for the specific module type
337 EAPI void
338 obj_hook(Evas_Object *obj)
339 {
340    _mod_hook_count++;
341    //if(_mod_hook_count > 1) return;
342
343    if(!ext_mod)
344      {
345         ext_mod = ELM_NEW(Elm_Entry_Extension_data);
346         elm_entry_extension_module_data_get(obj,ext_mod);
347      }
348 }
349
350 EAPI void
351 obj_unhook(Evas_Object *obj)
352 {
353    _mod_hook_count--;
354    if(_mod_hook_count > 0) return;
355
356    if(ext_mod)
357      {
358         free(ext_mod);
359         ext_mod = NULL;
360      }
361 }
362
363 EAPI void
364 obj_longpress(Evas_Object *obj)
365 {
366    if(!ext_mod) return;
367
368    Evas_Object *top;
369    const Eina_List *l;
370    const Elm_Entry_Context_Menu_Item *it;
371    const char *context_menu_orientation;
372    char buf[255];
373    Evas_Object* icon;
374    Elm_Object_Item *added_item = NULL;
375
376    /*update*/
377    elm_entry_extension_module_data_get(obj,ext_mod);
378    if (ext_mod->context_menu)
379      {
380 #ifdef HAVE_ELEMENTARY_X
381         int cbhm_count = _cbhm_item_count_get(obj);
382 #endif
383         if (ext_mod->popup) evas_object_del(ext_mod->popup);
384         //else elm_widget_scroll_freeze_push(obj);
385         top = elm_widget_top_get(obj);
386         if(top)
387           {
388              ext_mod->popup = elm_ctxpopup_add(top);
389              elm_object_tree_focus_allow_set(ext_mod->popup, EINA_FALSE);
390              evas_object_smart_callback_add(ext_mod->popup, "dismissed", _ctxpopup_dismissed_cb, NULL);
391              evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL, _entry_del_cb, ext_mod->popup);
392              evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE, _entry_hide_cb, ext_mod->popup);
393           }
394         /*currently below theme not used,when guideline comes a new theme can be created if required*/
395         //elm_object_style_set(ext_mod->popup,"extended/entry");
396         context_menu_orientation = edje_object_data_get
397            (ext_mod->ent, "context_menu_orientation");
398         if ((context_menu_orientation) &&
399             (!strcmp(context_menu_orientation, "horizontal")))
400           elm_ctxpopup_horizontal_set(ext_mod->popup, EINA_TRUE);
401
402         if (!ext_mod->selmode)
403           {
404              if (!ext_mod->password)
405                {
406                   if (!elm_entry_is_empty(obj))
407                     {
408                        added_item = elm_ctxpopup_item_append(ext_mod->popup, S_SELECT, NULL, _select, obj );
409                        added_item = elm_ctxpopup_item_append(ext_mod->popup, S_SELECT_ALL, NULL, _select_all, obj );
410                     }
411                }
412
413 #ifdef HAVE_ELEMENTARY_X
414              if (cbhm_count)
415 #else
416              if (1) // need way to detect if someone has a selection
417 #endif
418                {
419                   if (ext_mod->editable)
420                     added_item = elm_ctxpopup_item_append(ext_mod->popup, S_PASTE, NULL, _paste, obj );
421                }
422              //elm_ctxpopup_item_append(wd->ctxpopup, NULL, "Selectall",_select_all, obj );
423              // start for cbhm
424 #ifdef HAVE_ELEMENTARY_X
425              if ((!ext_mod->password) && (ext_mod->editable) && (cbhm_count))
426 #else
427              if ((!ext_mod->password) && (ext_mod->editable))
428 #endif
429                {
430                   added_item = elm_ctxpopup_item_append(ext_mod->popup, S_CLIPBOARD, NULL, _clipboard_menu, obj);  // Clipboard
431                   //elm_ctxpopup_item_append(ext_mod->popup, "More", NULL, _clipboard_menu, obj );
432                }
433              // end for cbhm
434
435              const char *entry_str;
436              char *str;
437              entry_str = edje_object_part_text_get(ext_mod->ent, "elm.text");
438              str = _remove_tags(entry_str);
439              if (strcmp(str, "") != 0)
440                {
441                   icon = elm_icon_add(ext_mod->popup);
442                   snprintf(buf, sizeof(buf), "%s/images/copy&paste_icon_search.png", PACKAGE_DATA_DIR);
443                   elm_icon_file_set(icon, buf, NULL);
444                   added_item = elm_ctxpopup_item_append(ext_mod->popup, NULL, icon, _search_menu, obj);  // Search
445                }
446           }
447         else
448           {
449              if (!ext_mod->password)
450                {
451                   if (ext_mod->have_selection)
452                     {
453                        added_item = elm_ctxpopup_item_append(ext_mod->popup, S_COPY, NULL, _copy, obj );
454                        if (ext_mod->editable)
455                          added_item = elm_ctxpopup_item_append(ext_mod->popup, S_CUT, NULL, _cut, obj );
456 #ifdef HAVE_ELEMENTARY_X
457                        if (ext_mod->editable && cbhm_count)
458 #else
459                        if (ext_mod->editable)
460 #endif
461                          added_item = elm_ctxpopup_item_append(ext_mod->popup, S_PASTE, NULL, _paste, obj );
462                     }
463                   else
464                     {
465                        _cancel(obj,ext_mod->popup,NULL);
466                        if (!elm_entry_is_empty(obj))
467                          {
468                             added_item = elm_ctxpopup_item_append(ext_mod->popup, S_SELECT, NULL, _select, obj );
469                             added_item = elm_ctxpopup_item_append(ext_mod->popup, S_SELECT_ALL, NULL, _select_all, obj );
470                          }
471 #ifdef HAVE_ELEMENTARY_X
472                        if (cbhm_count)
473 #else
474                        if (1) // need way to detect if someone has a selection
475 #endif
476                          {
477                             if (ext_mod->editable)
478                               added_item = elm_ctxpopup_item_append(ext_mod->popup, S_PASTE, NULL, _paste, obj );
479                          }
480                     }
481                   // start for cbhm
482 #ifdef HAVE_ELEMENTARY_X
483                   if (ext_mod->editable && cbhm_count)
484 #else
485                   if (ext_mod->editable)
486 #endif
487                     {
488                        added_item = elm_ctxpopup_item_append(ext_mod->popup, S_CLIPBOARD, NULL, _clipboard_menu, obj);  // Clipboard
489                        //elm_ctxpopup_item_append(ext_mod->popup, "More", NULL, _clipboard_menu, obj );
490                     }
491                   // end for cbhm
492
493                   const char *entry_str;
494                   char *str;
495                   entry_str = edje_object_part_text_get(ext_mod->ent, "elm.text");
496                   str = _remove_tags(entry_str);
497                   if (strcmp(str, "") != 0)
498                     {
499                        icon = elm_icon_add(ext_mod->popup);
500                        snprintf(buf, sizeof(buf), "%s/images/copy&paste_icon_search.png", PACKAGE_DATA_DIR);
501                        elm_icon_file_set(icon, buf, NULL);
502                        added_item = elm_ctxpopup_item_append(ext_mod->popup, NULL, icon, _search_menu, obj);  // Search
503                     }
504               }
505           }
506         EINA_LIST_FOREACH(ext_mod->items, l, it)
507           {
508              added_item = elm_ctxpopup_item_append(ext_mod->popup, it->label, NULL, _item_clicked, it );
509           }
510         if (ext_mod->popup && added_item)
511           {
512              //elm_object_scroll_freeze_push(ext_mod->popup);
513              _ctxpopup_position(obj);
514              evas_object_show(ext_mod->popup);
515              _ctxpopup_position(obj);
516              ext_mod->caller = obj;
517              evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _entry_move_cb, ext_mod->popup);
518              evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE, _entry_resize_cb, ext_mod->popup);
519           }
520         else
521           ext_mod->caller = NULL;
522      }
523 }
524
525 EAPI void
526 obj_mouseup(Evas_Object *obj)
527 {
528    if (!obj || !ext_mod)
529      return;
530 }
531
532
533 EAPI void
534 obj_hidemenu(Evas_Object *obj)
535 {
536    if (!obj || !ext_mod || obj != ext_mod->caller)
537      return;
538
539    _ctxpopup_hide(ext_mod->popup);
540    // if (ext_mod->popup) evas_object_del(ext_mod->popup);
541 }