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