Merge "[cnp] fixed S1-2415 issue("search" is not working)"
[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 __UNUSED__, void *event_info __UNUSED__)
92 {
93    if (evas_pointer_button_down_mask_get(e))
94      _ctxpopup_hide(data);
95    else
96      _ctxpopup_position(data);
97 }
98
99 static void
100 _entry_resize_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
101 {
102    _ctxpopup_hide(data);
103 }
104
105 static void
106 _ctxpopup_hide(Evas_Object *popup)
107 {
108    evas_object_hide(popup);
109    evas_object_event_callback_del(ext_mod->caller, EVAS_CALLBACK_DEL, _entry_del_cb);
110    evas_object_event_callback_del(ext_mod->caller, EVAS_CALLBACK_HIDE, _entry_hide_cb);
111    evas_object_event_callback_del(ext_mod->caller, EVAS_CALLBACK_MOVE, _entry_move_cb);
112    evas_object_event_callback_del(ext_mod->caller, EVAS_CALLBACK_RESIZE, _entry_resize_cb);
113 }
114
115 static void
116 _ctxpopup_position(Evas_Object *obj)
117 {
118    if(!ext_mod) return;
119
120    Evas_Coord cx, cy, cw, ch, x, y, w, h;
121    if (!edje_object_part_text_selection_geometry_get(ext_mod->ent, "elm.text", &x, &y, &w, &h))
122      {
123         evas_object_geometry_get(ext_mod->ent, &x, &y, NULL, NULL);
124         edje_object_part_text_cursor_geometry_get(ext_mod->ent, "elm.text",
125                                                   &cx, &cy, &cw, &ch);
126         evas_object_size_hint_min_get(ext_mod->popup, &w, &h);
127         if (cw < w)
128           {
129              cx += (cw - w) / 2;
130              cw = w;
131           }
132         if (ch < h)
133           {
134              cy += (ch - h) / 2;
135              ch = h;
136           }
137         evas_object_move(ext_mod->popup, x + cx, y + cy);
138         evas_object_resize(ext_mod->popup, cw, ch);
139      }
140    else
141      {
142         if (ext_mod->viewport_rect.x != -1 || ext_mod->viewport_rect.y != -1
143             || ext_mod->viewport_rect.w != -1 || ext_mod->viewport_rect.h != -1)
144           {
145              Evas_Coord vx, vy, vw, vh, x2, y2;
146              x2 = x + w;
147              y2 = y + h;
148              vx = ext_mod->viewport_rect.x;
149              vy = ext_mod->viewport_rect.y;
150              vw = ext_mod->viewport_rect.w;
151              vh = ext_mod->viewport_rect.h;
152
153              if (x < vx) x = vx;
154              if (y < vy) y = vy;
155              if (x2 > vx + vw) x2 = vx + vw;
156              if (y2 > vy + vh) y2 = vy + vh;
157              w = x2 - x;
158              h = y2 - y;
159           }
160         else
161           {
162              Evas_Coord sw, sh, x2, y2;
163              x2 = x + w;
164              y2 = y + h;
165              ecore_x_window_size_get(ecore_x_window_root_first_get(), &sw, &sh);
166
167              if (x < 0) x = 0;
168              if (y < 0) y = 0;
169              if (x2 > sw) x2 = sw;
170              if (y2 > sh) y2 = sh;
171              w = x2 - x;
172              h = y2 - y;
173           }
174         cx = x + (w / 2);
175         cy = y + (h / 2);
176         evas_object_move(ext_mod->popup, cx, cy);
177      }
178 }
179
180 static void
181 _select_all(void *data, Evas_Object *obj, void *event_info)
182 {
183    if(!ext_mod) return;
184
185    ext_mod->selectall(data,obj,event_info);
186    _ctxpopup_hide(obj);
187 }
188
189 static void
190 _select(void *data, Evas_Object *obj, void *event_info)
191 {
192    if(!ext_mod) return;
193
194    ext_mod->select(data,obj,event_info);
195    _ctxpopup_hide(obj);
196 }
197
198 static void
199 _paste(void *data, Evas_Object *obj, void *event_info)
200 {
201    if(!ext_mod) return;
202
203    ext_mod->paste(data,obj,event_info);
204    _ctxpopup_hide(obj);
205 }
206
207 static void
208 _cut(void *data, Evas_Object *obj, void *event_info)
209 {
210    if(!ext_mod) return;
211
212    ext_mod->cut(data,obj,event_info);
213    _ctxpopup_hide(obj);
214    elm_object_scroll_freeze_pop(ext_mod->popup);
215 }
216
217 static void
218 _copy(void *data, Evas_Object *obj, void *event_info)
219 {
220    if(!ext_mod) return;
221
222    ext_mod->copy(data,obj,event_info);
223    _ctxpopup_hide(obj);
224    elm_object_scroll_freeze_pop(ext_mod->popup);
225 }
226
227 static void
228 _cancel(void *data, Evas_Object *obj, void *event_info)
229 {
230    if(!ext_mod) return;
231
232    ext_mod->cancel(data,obj,event_info);
233    _ctxpopup_hide(obj);
234    elm_object_scroll_freeze_pop(ext_mod->popup);
235 }
236
237 static void
238 _search_menu(void *data, Evas_Object *obj, void *event_info)
239 {
240    if(!ext_mod) return;
241
242    int ret;
243    bundle *b = bundle_create();
244    if (!b)
245      {
246         //printf("bundle_create() failed\n");
247         return;
248      }
249
250    appsvc_set_operation(b, APPSVC_OPERATION_SEARCH);
251    if (ext_mod->selmode)
252      {
253         const char *selection = elm_entry_selection_get(ext_mod->caller);
254         if (selection)
255           {
256              char *str = _remove_tags(selection);
257              if (str)
258                {
259                   appsvc_add_data(b, APPSVC_DATA_KEYWORD, str);
260                   free(str);
261                }
262              else
263                appsvc_add_data(b, APPSVC_DATA_KEYWORD, selection);
264           }
265      }
266    appsvc_run_service(b, 0, NULL, NULL);
267    bundle_free(b);
268    _ctxpopup_hide(obj);
269 }
270
271 static void
272 _clipboard_menu(void *data, Evas_Object *obj, void *event_info)
273 {
274    if(!ext_mod) return;
275
276    // start for cbhm
277 #ifdef HAVE_ELEMENTARY_X
278    ecore_x_selection_secondary_set(elm_win_xwindow_get(obj), "",1);
279 #endif
280    ext_mod->cnpinit(data,obj,event_info);
281    if (ext_mod->cnp_mode != ELM_CNP_MODE_MARKUP)
282      _cbhm_msg_send(obj, "show0");
283    else
284      _cbhm_msg_send(obj, "show1");
285    _ctxpopup_hide(obj);
286    // end for cbhm
287 }
288
289 static void
290 _item_clicked(void *data, Evas_Object *obj, void *event_info)
291 {
292    Elm_Entry_Context_Menu_Item *it = data;
293    Evas_Object *obj2 = it->obj;
294
295    if (it->func) it->func(it->data, obj2, NULL);
296    _ctxpopup_hide(obj);
297 }
298
299 static void
300 _ctxpopup_dismissed_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
301 {
302    if (!ext_mod) return;
303
304    elm_object_scroll_freeze_pop(ext_mod->popup);
305 }
306
307 // module api funcs needed
308 EAPI int
309 elm_modapi_init(void *m)
310 {
311    return 1; // succeed always
312 }
313
314 EAPI int
315 elm_modapi_shutdown(void *m)
316 {
317    return 1; // succeed always
318 }
319
320 // module funcs for the specific module type
321 EAPI void
322 obj_hook(Evas_Object *obj)
323 {
324    _mod_hook_count++;
325    //if(_mod_hook_count > 1) return;
326
327    if(!ext_mod)
328      {
329         ext_mod = ELM_NEW(Elm_Entry_Extension_data);
330         elm_entry_extension_module_data_get(obj,ext_mod);
331      }
332 }
333
334 EAPI void
335 obj_unhook(Evas_Object *obj)
336 {
337    _mod_hook_count--;
338    if(_mod_hook_count > 0) return;
339
340    if(ext_mod)
341      {
342         free(ext_mod);
343         ext_mod = NULL;
344      }
345 }
346
347 EAPI void
348 obj_longpress(Evas_Object *obj)
349 {
350    if(!ext_mod) return;
351
352    Evas_Object *top;
353    const Eina_List *l;
354    const Elm_Entry_Context_Menu_Item *it;
355    const char *context_menu_orientation;
356    char buf[255];
357    Evas_Object* icon;
358    Elm_Object_Item *added_item = NULL;
359
360    /*update*/
361    elm_entry_extension_module_data_get(obj,ext_mod);
362    if (ext_mod->context_menu)
363      {
364 #ifdef HAVE_ELEMENTARY_X
365         int cbhm_count = _cbhm_item_count_get(obj);
366 #endif
367         if (ext_mod->popup) evas_object_del(ext_mod->popup);
368         //else elm_widget_scroll_freeze_push(obj);
369         top = elm_widget_top_get(obj);
370         if(top)
371           {
372              ext_mod->popup = elm_ctxpopup_add(top);
373              elm_object_focus_allow_set(ext_mod->popup, EINA_FALSE);
374              evas_object_smart_callback_add(ext_mod->popup, "dismissed", _ctxpopup_dismissed_cb, NULL);
375              evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL, _entry_del_cb, ext_mod->popup);
376              evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE, _entry_hide_cb, ext_mod->popup);
377           }
378         /*currently below theme not used,when guideline comes a new theme can be created if required*/
379         elm_object_style_set(ext_mod->popup,"extended/entry");
380         context_menu_orientation = edje_object_data_get
381            (ext_mod->ent, "context_menu_orientation");
382         if ((context_menu_orientation) &&
383             (!strcmp(context_menu_orientation, "horizontal")))
384           elm_ctxpopup_horizontal_set(ext_mod->popup, EINA_TRUE);
385
386         if (!ext_mod->selmode)
387           {
388              if (!ext_mod->password)
389                {
390                   if (!elm_entry_is_empty(obj))
391                     {
392                        added_item = elm_ctxpopup_item_append(ext_mod->popup, S_SELECT, NULL, _select, obj );
393                        added_item = elm_ctxpopup_item_append(ext_mod->popup, S_SELECT_ALL, NULL, _select_all, obj );
394                     }
395                }
396
397 #ifdef HAVE_ELEMENTARY_X
398              if (cbhm_count)
399 #else
400              if (1) // need way to detect if someone has a selection
401 #endif
402                {
403                   if (ext_mod->editable)
404                     added_item = elm_ctxpopup_item_append(ext_mod->popup, S_PASTE, NULL, _paste, obj );
405                }
406              //elm_ctxpopup_item_append(wd->ctxpopup, NULL, "Selectall",_select_all, obj );
407              // start for cbhm
408 #ifdef HAVE_ELEMENTARY_X
409              if ((!ext_mod->password) && (ext_mod->editable) && (cbhm_count))
410 #else
411              if ((!ext_mod->password) && (ext_mod->editable))
412 #endif
413                {
414                   added_item = elm_ctxpopup_item_append(ext_mod->popup, S_CLIPBOARD, NULL, _clipboard_menu, obj);  // Clipboard
415                   //elm_ctxpopup_item_append(ext_mod->popup, "More", NULL, _clipboard_menu, obj );
416                }
417              // end for cbhm
418              icon = elm_icon_add(ext_mod->popup);
419              snprintf(buf, sizeof(buf), "%s/images/copy&paste_icon_search.png", PACKAGE_DATA_DIR);
420              elm_icon_file_set(icon, buf, NULL);
421              added_item = elm_ctxpopup_item_append(ext_mod->popup, NULL, icon, _search_menu, obj);  // Search
422           }
423         else
424           {
425              if (!ext_mod->password)
426                {
427                   if (ext_mod->have_selection)
428                     {
429                        added_item = elm_ctxpopup_item_append(ext_mod->popup, S_COPY, NULL, _copy, obj );
430                        if (ext_mod->editable)
431                          added_item = elm_ctxpopup_item_append(ext_mod->popup, S_CUT, NULL, _cut, obj );
432 #ifdef HAVE_ELEMENTARY_X
433                        if (ext_mod->editable && cbhm_count)
434 #else
435                        if (ext_mod->editable)
436 #endif
437                          added_item = elm_ctxpopup_item_append(ext_mod->popup, S_PASTE, NULL, _paste, obj );
438                     }
439                   else
440                     {
441                        _cancel(obj,ext_mod->popup,NULL);
442                        if (!elm_entry_is_empty(obj))
443                          {
444                             added_item = elm_ctxpopup_item_append(ext_mod->popup, S_SELECT, NULL, _select, obj );
445                             added_item = elm_ctxpopup_item_append(ext_mod->popup, S_SELECT_ALL, NULL, _select_all, obj );
446                          }
447 #ifdef HAVE_ELEMENTARY_X
448                        if (cbhm_count)
449 #else
450                        if (1) // need way to detect if someone has a selection
451 #endif
452                          {
453                             if (ext_mod->editable)
454                               added_item = elm_ctxpopup_item_append(ext_mod->popup, S_PASTE, NULL, _paste, obj );
455                          }
456                     }
457                   // start for cbhm
458 #ifdef HAVE_ELEMENTARY_X
459                   if (ext_mod->editable && cbhm_count)
460 #else
461                   if (ext_mod->editable)
462 #endif
463                     {
464                        added_item = elm_ctxpopup_item_append(ext_mod->popup, S_CLIPBOARD, NULL, _clipboard_menu, obj);  // Clipboard
465                        //elm_ctxpopup_item_append(ext_mod->popup, "More", NULL, _clipboard_menu, obj );
466                     }
467                   // end for cbhm
468                   icon = elm_icon_add(ext_mod->popup);
469                   snprintf(buf, sizeof(buf), "%s/images/copy&paste_icon_search.png", PACKAGE_DATA_DIR);
470                   elm_icon_file_set(icon, buf, NULL);
471                   added_item = elm_ctxpopup_item_append(ext_mod->popup, NULL, icon, _search_menu, obj);  // Search
472                }
473           }
474         EINA_LIST_FOREACH(ext_mod->items, l, it)
475           {
476              added_item = elm_ctxpopup_item_append(ext_mod->popup, it->label, NULL, _item_clicked, it );
477           }
478         if (ext_mod->popup && added_item)
479           {
480              elm_object_scroll_freeze_push(ext_mod->popup);
481              _ctxpopup_position(obj);
482              evas_object_show(ext_mod->popup);
483              ext_mod->caller = obj;
484              evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _entry_move_cb, ext_mod->popup);
485              evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE, _entry_resize_cb, ext_mod->popup);
486           }
487         else
488           ext_mod->caller = NULL;
489      }
490 }
491
492 EAPI void
493 obj_mouseup(Evas_Object *obj)
494 {
495    if (!obj || !ext_mod)
496      return;
497 }
498
499
500 EAPI void
501 obj_hidemenu(Evas_Object *obj)
502 {
503    if (!obj || !ext_mod || obj != ext_mod->caller)
504      return;
505
506    _ctxpopup_hide(ext_mod->popup);
507    // if (ext_mod->popup) evas_object_del(ext_mod->popup);
508 }