================================================================
[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
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_ctxpopup_scroller_disabled_set(ext_mod->popup, EINA_FALSE);
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_ctxpopup_scroller_disabled_set(ext_mod->popup, EINA_FALSE);
76 }
77
78 static void
79 _cancel(void *data, Evas_Object *obj, void *event_info)
80 {
81         ext_mod->cancel(data,obj,event_info);
82         evas_object_hide(obj);
83         elm_ctxpopup_scroller_disabled_set(ext_mod->popup, EINA_FALSE);
84 }
85
86 static void
87 _clipboard_menu(void *data, Evas_Object *obj, void *event_info)
88 {
89         // start for cbhm
90         ecore_x_selection_secondary_set(elm_win_xwindow_get(obj), "",1);
91         ext_mod->cnpinit(data,obj,event_info);
92         elm_cbhm_helper_init(obj);
93         elm_cbhm_send_raw_data("show");
94         evas_object_hide(obj);
95         // end for cbhm
96 }
97
98 static void
99 _item_clicked(void *data, Evas_Object *obj, void *event_info)
100 {
101    Elm_Entry_Context_Menu_Item *it = data;
102    Evas_Object *obj2 = it->obj;
103
104         if (it->func) it->func(it->data, obj2, NULL);
105         evas_object_hide(obj);
106 }
107
108 // module api funcs needed
109 EAPI int
110 elm_modapi_init(void *m)
111 {
112    return 1; // succeed always
113 }
114
115 EAPI int
116 elm_modapi_shutdown(void *m)
117 {
118    return 1; // succeed always
119 }
120
121 // module fucns for the specific module type
122 EAPI void
123 obj_hook(Evas_Object *obj)
124 {
125         if(!ext_mod)
126                 {
127                         ext_mod = ELM_NEW(Elm_Entry_Extension_data);
128                 elm_entry_extension_module_data_get(obj,ext_mod);
129                 }
130 }
131
132 EAPI void
133 obj_unhook(Evas_Object *obj)
134 {
135    if(ext_mod)
136                 {
137                         free(ext_mod);
138                         ext_mod = NULL;
139         }
140 }
141
142 EAPI void
143 obj_longpress(Evas_Object *obj)
144 {       
145         if(!ext_mod) return;
146         Evas_Object *top;
147         const Eina_List *l;
148         const Elm_Entry_Context_Menu_Item *it;
149         const char *context_menu_orientation;
150
151         /*update*/
152         elm_entry_extension_module_data_get(obj,ext_mod);
153         if (ext_mod->context_menu)
154         {
155                 if (ext_mod->popup) evas_object_del(ext_mod->popup);
156 //              else elm_widget_scroll_freeze_push(obj);
157                 top = elm_widget_top_get(obj);
158                 if(top)
159                         ext_mod->popup = elm_ctxpopup_add(top);
160                 /*currently below theme not used,when guideline comes a new theme can be created if required*/
161                 elm_object_style_set(ext_mod->popup,"extended/entry");
162                 elm_ctxpopup_scroller_disabled_set(ext_mod->popup, EINA_TRUE);
163                 context_menu_orientation = edje_object_data_get
164                 (ext_mod->ent, "context_menu_orientation");
165                 if ((context_menu_orientation) &&
166                 (!strcmp(context_menu_orientation, "horizontal")))
167                 elm_ctxpopup_horizontal_set(ext_mod->popup, EINA_TRUE);
168                 elm_ctxpopup_screen_dimmed_disabled_set(ext_mod->popup, EINA_TRUE);
169
170                 elm_widget_sub_object_add(obj, ext_mod->popup);
171                 if (!ext_mod->selmode)
172                 {       
173                         if (!ext_mod->password)
174                         {
175                                 elm_ctxpopup_item_add(ext_mod->popup, NULL, "Select",_select, obj );
176                                 elm_ctxpopup_item_add(ext_mod->popup, NULL, "Select All",_select_all, obj );
177                         }
178                         if (1) // need way to detect if someone has a selection
179                                 {
180                                         if (ext_mod->editable)
181                                                 elm_ctxpopup_item_add(ext_mod->popup, NULL, "Paste",_paste, obj );
182                                 }
183         //              elm_ctxpopup_item_add(wd->ctxpopup, NULL, "Selectall",_select_all, obj );
184         // start for cbhm
185                         if (!ext_mod->password)
186                                 elm_ctxpopup_item_add(ext_mod->popup, NULL, "More", _clipboard_menu, obj );
187         // end for cbhm
188                 }
189                 else
190                 {
191                           if (!ext_mod->password)
192                                 {
193                                         if (ext_mod->have_selection)
194                                                 {
195                                                         elm_ctxpopup_item_add(ext_mod->popup, NULL, "Copy",_copy, obj );
196                                                         if (ext_mod->editable)
197                                                                 elm_ctxpopup_item_add(ext_mod->popup, NULL, "Cut",_cut, obj );                                                  
198                                                         if (ext_mod->editable)
199                                                                 elm_ctxpopup_item_add(ext_mod->popup, NULL, "Paste",_paste, obj );
200                                                 }
201                                         else
202                                                 {
203                                                         _cancel(obj,ext_mod->popup,NULL);               
204                                                         elm_ctxpopup_item_add(ext_mod->popup, NULL, "Select",_select, obj );
205                                                         elm_ctxpopup_item_add(ext_mod->popup, NULL, "Select All",_select_all, obj );
206                                                         if (1) // need way to detect if someone has a selection
207                                                                 {
208                                                                         if (ext_mod->editable)
209                                                                                 elm_ctxpopup_item_add(ext_mod->popup, NULL, "Paste",_paste, obj );
210                                                                 }
211                                                 }
212         // start for cbhm
213                                                         elm_ctxpopup_item_add(ext_mod->popup, NULL, "More",_clipboard_menu, obj );
214         // end for cbhm
215                                 }
216                 }
217                 EINA_LIST_FOREACH(ext_mod->items, l, it)
218                 {
219                         elm_ctxpopup_item_add(ext_mod->popup, NULL, it->label,_item_clicked, it );
220                 }
221                 if (ext_mod->popup)
222                         {
223                                 _ctxpopup_position(obj);
224                                 evas_object_show(ext_mod->popup);                 
225                         }
226                 }
227         ext_mod->longpress_timer = NULL;
228         }
229
230 EAPI void
231 obj_mouseup(Evas_Object *obj)
232 {
233         if (!obj || !ext_mod) {
234                 return;
235         }
236
237 /*update*/
238         elm_entry_extension_module_data_get(obj,ext_mod);
239    if (ext_mod->longpress_timer)
240      {    
241                 if (ext_mod->have_selection )
242                         {                               
243                                 _cancel(obj,ext_mod->popup,NULL);
244                         }
245      }     
246 }
247
248
249 EAPI void
250 obj_hidemenu(Evas_Object *obj)
251 {
252         if (!obj || !ext_mod) {
253                 return;
254         }
255
256         evas_object_hide(ext_mod->popup);
257 //      if (ext_mod->popup) evas_object_del(ext_mod->popup);
258 }
259
260