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