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