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