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