854857b4806187504fc8ddf69cf62ca08d748a1b
[framework/uifw/elementary.git] / src / modules / ctxpopup_copypasteshareUI / 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
7 typedef struct _Elm_Entry_Context_Menu_Item Elm_Entry_Context_Menu_Item;
8 struct _Elm_Entry_Context_Menu_Item
9 {
10    Evas_Object *obj;
11    const char *label;
12    const char *icon_file;
13    const char *icon_group;
14    Elm_Icon_Type icon_type;
15    Evas_Smart_Cb func;
16    void *data;
17 };
18
19 static void
20 _ctxpopup_position(Evas_Object *obj)
21 {
22    Evas_Coord cx, cy, cw, ch, x, y, mw, mh;
23    evas_object_geometry_get(ext_mod->ent, &x, &y, NULL, NULL);
24    edje_object_part_text_cursor_geometry_get(ext_mod->ent, "elm.text",
25                                              &cx, &cy, &cw, &ch);
26    evas_object_size_hint_min_get(ext_mod->popup, &mw, &mh);
27    if (cw < mw)
28      {
29         cx += (cw - mw) / 2;
30         cw = mw;
31      }
32    if (ch < mh)
33      {
34         cy += (ch - mh) / 2;
35         ch = mh;
36      }
37    evas_object_move(ext_mod->popup, x + cx, y + cy);
38    evas_object_resize(ext_mod->popup, cw, ch);
39 }
40
41 static void
42 _select_all(void *data, Evas_Object *obj, void *event_info)
43 {
44    ext_mod->selectall(data,obj,event_info);
45    evas_object_hide(obj);
46 }
47
48 static void
49 _select(void *data, Evas_Object *obj, void *event_info)
50 {
51    ext_mod->select(data,obj,event_info);
52    evas_object_hide(obj);
53 }
54
55 static void
56 _paste(void *data, Evas_Object *obj, void *event_info)
57 {
58         ext_mod->paste(data,obj,event_info);
59         evas_object_hide(obj);
60 }
61
62 static void
63 _cut(void *data, Evas_Object *obj, void *event_info)
64 {
65         ext_mod->cut(data,obj,event_info);
66         evas_object_hide(obj);
67         elm_object_scroll_freeze_pop(ext_mod->popup);
68 }
69
70 static void
71 _copy(void *data, Evas_Object *obj, void *event_info)
72 {
73         ext_mod->copy(data,obj,event_info);
74         evas_object_hide(obj);
75         elm_object_scroll_freeze_pop(ext_mod->popup);
76 }
77
78 static void
79 _share(void *data, Evas_Object *obj, void *event_info)
80 {
81         char *str = NULL;
82 #ifdef HAVE_ELEMENTARY_X
83         Ecore_X_Atom atom;
84 #endif
85         str = elm_entry_markup_to_utf8(elm_entry_selection_get(data));
86         if (!str) return;
87 #ifdef HAVE_ELEMENTARY_X
88         atom = ecore_x_atom_get("_TEXT_SHARE_TEST");
89         if (atom)
90           {
91              ecore_x_window_prop_string_set(ecore_x_window_root_first_get(), atom, str);
92              ecore_x_client_message32_send(ecore_x_window_root_first_get(),
93                                            atom, ECORE_X_EVENT_MASK_WINDOW_CONFIGURE,
94                                            0, 0, 0, 0, 0);
95           }
96 #endif
97         free(str);
98
99         evas_object_hide(obj);
100         elm_ctxpopup_scroller_disabled_set(ext_mod->popup, EINA_FALSE);
101 }
102
103 static void
104 _cancel(void *data, Evas_Object *obj, void *event_info)
105 {
106         ext_mod->cancel(data,obj,event_info);
107         evas_object_hide(obj);
108         elm_object_scroll_freeze_pop(ext_mod->popup);
109 }
110
111 static void
112 _clipboard_menu(void *data, Evas_Object *obj, void *event_info)
113 {
114         // start for cbhm
115 #ifdef HAVE_ELEMENTARY_X
116         ecore_x_selection_secondary_set(elm_win_xwindow_get(obj), "",1);
117 #endif
118         ext_mod->cnpinit(data,obj,event_info);
119         elm_cbhm_helper_init(obj);
120         if (ext_mod->cnp_mode != ELM_CNP_MODE_MARKUP)
121                 elm_cbhm_send_raw_data("show0");
122         else
123                 elm_cbhm_send_raw_data("show1");
124         evas_object_hide(obj);
125         // end for cbhm
126 }
127
128 static void
129 _item_clicked(void *data, Evas_Object *obj, void *event_info)
130 {
131    Elm_Entry_Context_Menu_Item *it = data;
132    Evas_Object *obj2 = it->obj;
133
134         if (it->func) it->func(it->data, obj2, NULL);
135         evas_object_hide(obj);
136 }
137
138 // module api funcs needed
139 EAPI int
140 elm_modapi_init(void *m)
141 {
142    return 1; // succeed always
143 }
144
145 EAPI int
146 elm_modapi_shutdown(void *m)
147 {
148    return 1; // succeed always
149 }
150
151 // module fucns for the specific module type
152 EAPI void
153 obj_hook(Evas_Object *obj)
154 {
155         if(!ext_mod)
156                 {
157                         ext_mod = ELM_NEW(Elm_Entry_Extension_data);
158                 elm_entry_extension_module_data_get(obj,ext_mod);
159                 }
160 }
161
162 EAPI void
163 obj_unhook(Evas_Object *obj)
164 {
165    if(ext_mod)
166                 {
167                         free(ext_mod);
168                         ext_mod = NULL;
169         }
170 }
171
172 EAPI void
173 obj_longpress(Evas_Object *obj)
174 {
175         if(!ext_mod) return;
176         Evas_Object *top;
177         const Eina_List *l;
178         const Elm_Entry_Context_Menu_Item *it;
179         const char *context_menu_orientation;
180         char buf[255];
181         Evas_Object* icon;
182
183         /*update*/
184         elm_entry_extension_module_data_get(obj,ext_mod);
185         if (ext_mod->context_menu)
186         {
187                 if (ext_mod->popup) evas_object_del(ext_mod->popup);
188 //              else elm_widget_scroll_freeze_push(obj);
189                 top = elm_widget_top_get(obj);
190                 if(top)
191                         ext_mod->popup = elm_ctxpopup_add(top);
192                 /*currently below theme not used,when guideline comes a new theme can be created if required*/
193                 elm_object_style_set(ext_mod->popup,"extended/entry");
194                 context_menu_orientation = edje_object_data_get
195                 (ext_mod->ent, "context_menu_orientation");
196                 if ((context_menu_orientation) &&
197                 (!strcmp(context_menu_orientation, "horizontal")))
198                 elm_ctxpopup_horizontal_set(ext_mod->popup, EINA_TRUE);
199
200                 elm_widget_sub_object_add(obj, ext_mod->popup);
201                 if (!ext_mod->selmode)
202                 {
203                         if (!ext_mod->password)
204                         {
205                                 if (!elm_entry_is_empty(obj))
206                                 {
207                                         elm_ctxpopup_item_append(ext_mod->popup, "Select", NULL, _select, obj );
208                                         elm_ctxpopup_item_append(ext_mod->popup, "Select All", NULL, _select_all, obj );
209                                 }
210                         }
211                         if (1) // need way to detect if someone has a selection
212                                 {
213                                         if (ext_mod->editable)
214                                                 elm_ctxpopup_item_append(ext_mod->popup, "Paste", NULL, _paste, obj );
215                                 }
216         // start for cbhm
217                         if ((!ext_mod->password) && (ext_mod->editable))
218                         {
219                                 icon = elm_icon_add(ext_mod->popup);
220                                 snprintf(buf, sizeof(buf), "%s/images/copypaste_icon_clipboard.png", PACKAGE_DATA_DIR);
221                                 elm_icon_file_set(icon, buf, NULL);
222                                 elm_ctxpopup_item_append(ext_mod->popup, NULL, icon, _clipboard_menu, obj);
223                         }
224         // end for cbhm
225                 }
226                 else
227                 {
228                           if (!ext_mod->password)
229                                 {
230                                         if (ext_mod->have_selection)
231                                                 {
232                                                         elm_ctxpopup_item_append(ext_mod->popup, "Copy", NULL, _copy, obj );
233                                                         if (ext_mod->editable)
234                                                                 elm_ctxpopup_item_append(ext_mod->popup, "Cut", NULL, _cut, obj );
235                                                         if (ext_mod->editable)
236                                                                 elm_ctxpopup_item_append(ext_mod->popup, "Paste", NULL, _paste, obj );
237                                                         icon = elm_icon_add(ext_mod->popup);
238                                                         snprintf(buf, sizeof(buf), "%s/images/copypaste_icon_share.png", PACKAGE_DATA_DIR);
239                                                         elm_icon_file_set(icon, buf, NULL);
240                                                         elm_ctxpopup_item_append(ext_mod->popup, NULL, icon, _share, obj);
241                                                 }
242                                         else
243                                                 {
244                                                         _cancel(obj,ext_mod->popup,NULL);
245                                                         if (!elm_entry_is_empty(obj))
246                                                         {
247                                                                 elm_ctxpopup_item_append(ext_mod->popup, "Select", NULL, _select, obj );
248                                                                 elm_ctxpopup_item_append(ext_mod->popup, "Select All", NULL, _select_all, obj );
249                                                         }
250                                                         if (1) // need way to detect if someone has a selection
251                                                                 {
252                                                                         if (ext_mod->editable)
253                                                                                 elm_ctxpopup_item_append(ext_mod->popup, "Paste", NULL, _paste, obj );
254                                                                 }
255                                                 }
256         // start for cbhm
257                                         if (ext_mod->editable)
258                                         {
259                                                 icon = elm_icon_add(ext_mod->popup);
260                                                 snprintf(buf, sizeof(buf), "%s/images/copypaste_icon_clipboard.png", PACKAGE_DATA_DIR);
261                                                 elm_icon_file_set(icon, buf, NULL);
262                                                 elm_ctxpopup_item_append(ext_mod->popup, NULL, icon, _clipboard_menu, obj);
263                                         }
264         // end for cbhm
265                                 }
266                 }
267                 EINA_LIST_FOREACH(ext_mod->items, l, it)
268                 {
269                         elm_ctxpopup_item_append(ext_mod->popup, it->label, NULL, _item_clicked, it );
270                 }
271                 if (ext_mod->popup)
272                         {
273                                 elm_object_scroll_freeze_push(ext_mod->popup);
274                                 _ctxpopup_position(obj);
275                                 evas_object_show(ext_mod->popup);
276                         }
277                 }
278         }
279
280 EAPI void
281 obj_mouseup(Evas_Object *obj)
282 {
283         if (!obj || !ext_mod) {
284                 return;
285         }
286
287 /*update*/
288         elm_entry_extension_module_data_get(obj,ext_mod);
289 }
290
291
292 EAPI void
293 obj_hidemenu(Evas_Object *obj)
294 {
295         if (!obj || !ext_mod) {
296                 return;
297         }
298
299         evas_object_hide(ext_mod->popup);
300 //      if (ext_mod->popup) evas_object_del(ext_mod->popup);
301 }
302
303