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