[copypaste] hide copypaste ctxpopup when entry is scrolled
[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
5 Elm_Entry_Extension_data *ext_mod;
6 static int _mod_hook_count = 0;
7
8 typedef struct _Elm_Entry_Context_Menu_Item Elm_Entry_Context_Menu_Item;
9 struct _Elm_Entry_Context_Menu_Item
10 {
11    Evas_Object *obj;
12    const char *label;
13    const char *icon_file;
14    const char *icon_group;
15    Elm_Icon_Type icon_type;
16    Evas_Smart_Cb func;
17    void *data;
18 };
19
20 static void _ctxpopup_hide(Evas_Object *popup);
21 static void
22 _entry_move(void *data, Evas *e, Evas_Object *obj, void *event_info)
23 {
24    _ctxpopup_hide(ext_mod->popup);
25 }
26
27 static void
28 _ctxpopup_hide(Evas_Object *popup)
29 {
30    evas_object_hide(popup);
31    evas_object_event_callback_del(ext_mod->caller, EVAS_CALLBACK_MOVE, _entry_move);
32    ext_mod->caller = NULL;
33 }
34
35 static void
36 _ctxpopup_position(Evas_Object *obj)
37 {
38    if(!ext_mod) return;
39
40    Evas_Coord cx, cy, cw, ch, x, y, w, h;
41    if (!edje_object_part_text_selection_geometry_get(ext_mod->ent, "elm.text", &x, &y, &w, &h))
42      {
43         evas_object_geometry_get(ext_mod->ent, &x, &y, NULL, NULL);
44         edje_object_part_text_cursor_geometry_get(ext_mod->ent, "elm.text",
45                                                   &cx, &cy, &cw, &ch);
46         evas_object_size_hint_min_get(ext_mod->popup, &w, &h);
47         if (cw < w)
48           {
49              cx += (cw - w) / 2;
50              cw = w;
51           }
52         if (ch < h)
53           {
54              cy += (ch - h) / 2;
55              ch = h;
56           }
57         evas_object_move(ext_mod->popup, x + cx, y + cy);
58         evas_object_resize(ext_mod->popup, cw, ch);
59      }
60    else
61      {
62         if (ext_mod->viewport_rect.x != -1 || ext_mod->viewport_rect.y != -1
63             || ext_mod->viewport_rect.w != -1 || ext_mod->viewport_rect.h != -1)
64           {
65              Evas_Coord vx, vy, vw, vh, x2, y2;
66              x2 = x + w;
67              y2 = y + h;
68              vx = ext_mod->viewport_rect.x;
69              vy = ext_mod->viewport_rect.y;
70              vw = ext_mod->viewport_rect.w;
71              vh = ext_mod->viewport_rect.h;
72
73              if (x < vx) x = vx;
74              if (y < vy) y = vy;
75              if (x2 > vx + vw) x2 = vx + vw;
76              if (y2 > vy + vh) y2 = vy + vh;
77              w = x2 - x;
78              h = y2 - y;
79           }
80         else
81           {
82              Evas_Coord sw, sh, x2, y2;
83              x2 = x + w;
84              y2 = y + h;
85              ecore_x_window_size_get(ecore_x_window_root_first_get(), &sw, &sh);
86
87              if (x < 0) x = 0;
88              if (y < 0) y = 0;
89              if (x2 > sw) x2 = sw;
90              if (y2 > sh) y2 = sh;
91              w = x2 - x;
92              h = y2 - y;
93           }
94         cx = x + (w / 2);
95         cy = y + (h / 2);
96         evas_object_move(ext_mod->popup, cx, cy);
97      }
98 }
99
100 static void
101 _select_all(void *data, Evas_Object *obj, void *event_info)
102 {
103    if(!ext_mod) return;
104
105    ext_mod->selectall(data,obj,event_info);
106    _ctxpopup_hide(obj);
107 }
108
109 static void
110 _select(void *data, Evas_Object *obj, void *event_info)
111 {
112    if(!ext_mod) return;
113
114    ext_mod->select(data,obj,event_info);
115    _ctxpopup_hide(obj);
116 }
117
118 static void
119 _paste(void *data, Evas_Object *obj, void *event_info)
120 {
121    if(!ext_mod) return;
122
123    ext_mod->paste(data,obj,event_info);
124    _ctxpopup_hide(obj);
125 }
126
127 static void
128 _cut(void *data, Evas_Object *obj, void *event_info)
129 {
130    if(!ext_mod) return;
131
132    ext_mod->cut(data,obj,event_info);
133    _ctxpopup_hide(obj);
134    elm_object_scroll_freeze_pop(ext_mod->popup);
135 }
136
137 static void
138 _copy(void *data, Evas_Object *obj, void *event_info)
139 {
140    if(!ext_mod) return;
141
142    ext_mod->copy(data,obj,event_info);
143    _ctxpopup_hide(obj);
144    elm_object_scroll_freeze_pop(ext_mod->popup);
145 }
146
147 static void
148 _cancel(void *data, Evas_Object *obj, void *event_info)
149 {
150    if(!ext_mod) return;
151
152    ext_mod->cancel(data,obj,event_info);
153    _ctxpopup_hide(obj);
154    elm_object_scroll_freeze_pop(ext_mod->popup);
155 }
156
157 static void
158 _clipboard_menu(void *data, Evas_Object *obj, void *event_info)
159 {
160    if(!ext_mod) return;
161
162    // start for cbhm
163 #ifdef HAVE_ELEMENTARY_X
164    ecore_x_selection_secondary_set(elm_win_xwindow_get(obj), "",1);
165 #endif
166    ext_mod->cnpinit(data,obj,event_info);
167    elm_cbhm_helper_init(obj);
168    if (ext_mod->cnp_mode != ELM_CNP_MODE_MARKUP)
169      elm_cbhm_send_raw_data("show0");
170    else
171      elm_cbhm_send_raw_data("show1");
172    _ctxpopup_hide(obj);
173    // end for cbhm
174 }
175
176 static void
177 _item_clicked(void *data, Evas_Object *obj, void *event_info)
178 {
179    Elm_Entry_Context_Menu_Item *it = data;
180    Evas_Object *obj2 = it->obj;
181
182    if (it->func) it->func(it->data, obj2, NULL);
183    _ctxpopup_hide(obj);
184 }
185
186 static void
187 _ctxpopup_dismissed_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
188 {
189    if (!ext_mod) return;
190
191    elm_object_scroll_freeze_pop(ext_mod->popup);
192 }
193
194 // module api funcs needed
195 EAPI int
196 elm_modapi_init(void *m)
197 {
198    return 1; // succeed always
199 }
200
201 EAPI int
202 elm_modapi_shutdown(void *m)
203 {
204    return 1; // succeed always
205 }
206
207 // module funcs for the specific module type
208 EAPI void
209 obj_hook(Evas_Object *obj)
210 {
211    _mod_hook_count++;
212    //if(_mod_hook_count > 1) return;
213
214    if(!ext_mod)
215      {
216         ext_mod = ELM_NEW(Elm_Entry_Extension_data);
217         elm_entry_extension_module_data_get(obj,ext_mod);
218      }
219 }
220
221 EAPI void
222 obj_unhook(Evas_Object *obj)
223 {
224    _mod_hook_count--;
225    if(_mod_hook_count > 0) return;
226
227    if(ext_mod)
228      {
229         free(ext_mod);
230         ext_mod = NULL;
231      }
232 }
233
234 EAPI void
235 obj_longpress(Evas_Object *obj)
236 {
237    if(!ext_mod) return;
238
239    Evas_Object *top;
240    const Eina_List *l;
241    const Elm_Entry_Context_Menu_Item *it;
242    const char *context_menu_orientation;
243    char buf[255];
244    Evas_Object* icon;
245    Elm_Object_Item *added_item = NULL;
246
247    /*update*/
248    elm_entry_extension_module_data_get(obj,ext_mod);
249    if (ext_mod->context_menu)
250      {
251 #ifdef HAVE_ELEMENTARY_X
252         int cbhm_count = -1;
253         if (elm_cbhm_helper_init(obj))
254           cbhm_count = elm_cbhm_get_count();
255 #endif
256         if (ext_mod->popup) evas_object_del(ext_mod->popup);
257         //else elm_widget_scroll_freeze_push(obj);
258         top = elm_widget_top_get(obj);
259         if(top)
260           {
261              ext_mod->popup = elm_ctxpopup_add(top);
262              elm_object_tree_unfocusable_set(ext_mod->popup, EINA_TRUE);
263              evas_object_smart_callback_add(ext_mod->popup, "dismissed", _ctxpopup_dismissed_cb, NULL);
264           }
265         /*currently below theme not used,when guideline comes a new theme can be created if required*/
266         elm_object_style_set(ext_mod->popup,"extended/entry");
267         context_menu_orientation = edje_object_data_get
268            (ext_mod->ent, "context_menu_orientation");
269         if ((context_menu_orientation) &&
270             (!strcmp(context_menu_orientation, "horizontal")))
271           elm_ctxpopup_horizontal_set(ext_mod->popup, EINA_TRUE);
272
273         elm_widget_sub_object_add(obj, ext_mod->popup);
274         if (!ext_mod->selmode)
275           {
276              if (!ext_mod->password)
277                {
278                   if (!elm_entry_is_empty(obj))
279                     {
280                        added_item = elm_ctxpopup_item_append(ext_mod->popup, "Select", NULL, _select, obj );
281                        added_item = elm_ctxpopup_item_append(ext_mod->popup, "Select All", NULL, _select_all, obj );
282                     }
283                }
284
285 #ifdef HAVE_ELEMENTARY_X
286              if (cbhm_count)
287 #else
288              if (1) // need way to detect if someone has a selection
289 #endif
290                {
291                   if (ext_mod->editable)
292                     added_item = elm_ctxpopup_item_append(ext_mod->popup, "Paste", NULL, _paste, obj );
293                }
294              //elm_ctxpopup_item_append(wd->ctxpopup, NULL, "Selectall",_select_all, obj );
295              // start for cbhm
296 #ifdef HAVE_ELEMENTARY_X
297              if ((!ext_mod->password) && (ext_mod->editable) && (cbhm_count))
298 #else
299              if ((!ext_mod->password) && (ext_mod->editable))
300 #endif
301                {
302                   icon = elm_icon_add(ext_mod->popup);
303                   snprintf(buf, sizeof(buf), "%s/images/copypaste_icon_clipboard.png", PACKAGE_DATA_DIR);
304                   elm_icon_file_set(icon, buf, NULL);
305                   added_item = elm_ctxpopup_item_append(ext_mod->popup, NULL, icon, _clipboard_menu, obj);
306                   //elm_ctxpopup_item_append(ext_mod->popup, "More", NULL, _clipboard_menu, obj );
307                }
308              // end for cbhm
309           }
310         else
311           {
312              if (!ext_mod->password)
313                {
314                   if (ext_mod->have_selection)
315                     {
316                        added_item = elm_ctxpopup_item_append(ext_mod->popup, "Copy", NULL, _copy, obj );
317                        if (ext_mod->editable)
318                          added_item = elm_ctxpopup_item_append(ext_mod->popup, "Cut", NULL, _cut, obj );
319 #ifdef HAVE_ELEMENTARY_X
320                        if (ext_mod->editable && cbhm_count)
321 #else
322                        if (ext_mod->editable)
323 #endif
324                          added_item = elm_ctxpopup_item_append(ext_mod->popup, "Paste", NULL, _paste, obj );
325                     }
326                   else
327                     {
328                        _cancel(obj,ext_mod->popup,NULL);
329                        if (!elm_entry_is_empty(obj))
330                          {
331                             added_item = elm_ctxpopup_item_append(ext_mod->popup, "Select", NULL, _select, obj );
332                             added_item = elm_ctxpopup_item_append(ext_mod->popup, "Select All", NULL, _select_all, obj );
333                          }
334 #ifdef HAVE_ELEMENTARY_X
335                        if (cbhm_count)
336 #else
337                        if (1) // need way to detect if someone has a selection
338 #endif
339                          {
340                             if (ext_mod->editable)
341                               added_item = elm_ctxpopup_item_append(ext_mod->popup, "Paste", NULL, _paste, obj );
342                          }
343                     }
344                   // start for cbhm
345 #ifdef HAVE_ELEMENTARY_X
346                   if (ext_mod->editable && cbhm_count)
347 #else
348                   if (ext_mod->editable)
349 #endif
350                     {
351                        icon = elm_icon_add(ext_mod->popup);
352                        snprintf(buf, sizeof(buf), "%s/images/copypaste_icon_clipboard.png", PACKAGE_DATA_DIR);
353                        elm_icon_file_set(icon, buf, NULL);
354                        added_item = elm_ctxpopup_item_append(ext_mod->popup, NULL, icon, _clipboard_menu, obj);
355                        //elm_ctxpopup_item_append(ext_mod->popup, "More", NULL, _clipboard_menu, obj );
356                     }
357                   // end for cbhm
358                }
359           }
360         EINA_LIST_FOREACH(ext_mod->items, l, it)
361           {
362              added_item = elm_ctxpopup_item_append(ext_mod->popup, it->label, NULL, _item_clicked, it );
363           }
364         if (ext_mod->popup && added_item)
365           {
366              elm_object_scroll_freeze_push(ext_mod->popup);
367              _ctxpopup_position(obj);
368              evas_object_show(ext_mod->popup);
369              ext_mod->caller = obj;
370              evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _entry_move, ext_mod->popup);
371           }
372         else
373           ext_mod->caller = NULL;
374      }
375 }
376
377 EAPI void
378 obj_mouseup(Evas_Object *obj)
379 {
380    if (!obj || !ext_mod)
381      return;
382 }
383
384
385 EAPI void
386 obj_hidemenu(Evas_Object *obj)
387 {
388    if (!obj || !ext_mod || obj != ext_mod->caller)
389      return;
390
391    _ctxpopup_hide(ext_mod->popup);
392    // if (ext_mod->popup) evas_object_del(ext_mod->popup);
393 }