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