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