[copypaste] Added null check routine.
[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 static void
8 _ctxpopup_position(Evas_Object *obj)
9 {
10    Evas_Coord cx, cy, cw, ch, x, y, mw, mh;
11    evas_object_geometry_get(ext_mod->ent, &x, &y, NULL, NULL);
12    edje_object_part_text_cursor_geometry_get(ext_mod->ent, "elm.text",
13                                              &cx, &cy, &cw, &ch);
14    evas_object_size_hint_min_get(ext_mod->popup, &mw, &mh);
15    if (cw < mw)
16      {
17         cx += (cw - mw) / 2;
18         cw = mw;
19      }
20    if (ch < mh)
21      {
22         cy += (ch - mh) / 2;
23         ch = mh;
24      }
25    evas_object_move(ext_mod->popup, x + cx, y + cy);
26    evas_object_resize(ext_mod->popup, cw, ch);
27 }
28
29 static void
30 _select(void *data, Evas_Object *obj, void *event_info)
31 {
32    ext_mod->select(data,obj,event_info);
33    evas_object_hide(obj);
34 }
35
36 static void
37 _paste(void *data, Evas_Object *obj, void *event_info)
38 {
39         ext_mod->paste(data,obj,event_info);
40         evas_object_hide(obj);
41 }
42
43 static void
44 _cut(void *data, Evas_Object *obj, void *event_info)
45 {
46         ext_mod->cut(data,obj,event_info);
47         evas_object_hide(obj);
48         elm_ctxpopup_scroller_disabled_set(ext_mod->popup, EINA_FALSE);
49 }
50
51 static void
52 _copy(void *data, Evas_Object *obj, void *event_info)
53 {
54         ext_mod->copy(data,obj,event_info);
55         evas_object_hide(obj);
56         elm_ctxpopup_scroller_disabled_set(ext_mod->popup, EINA_FALSE);
57 }
58
59 static void
60 _cancel(void *data, Evas_Object *obj, void *event_info)
61 {
62         ext_mod->cancel(data,obj,event_info);
63         evas_object_hide(obj);
64         elm_ctxpopup_scroller_disabled_set(ext_mod->popup, EINA_FALSE);
65 }
66
67 static void
68 _item_clicked(void *data, Evas_Object *obj, void *event_info)
69 {
70    Elm_Entry_Context_Menu_Item *it = data;
71    Evas_Object *obj2 = it->obj;
72
73         if (it->func) it->func(it->data, obj2, NULL);
74         evas_object_hide(obj);
75 }
76
77 // module api funcs needed
78 EAPI int
79 elm_modapi_init(void *m)
80 {
81    return 1; // succeed always
82 }
83
84 EAPI int
85 elm_modapi_shutdown(void *m)
86 {
87    return 1; // succeed always
88 }
89
90 // module fucns for the specific module type
91 EAPI void
92 obj_hook(Evas_Object *obj)
93 {
94         if(!ext_mod)
95                 {
96                         ext_mod = ELM_NEW(Elm_Entry_Extension_data);
97                 elm_entry_extension_module_data_get(obj,ext_mod);
98                 }
99 }
100
101 EAPI void
102 obj_unhook(Evas_Object *obj)
103 {
104    if(ext_mod)
105                 {
106                         free(ext_mod);
107                         ext_mod = NULL;
108         }
109 }
110
111 EAPI void
112 obj_longpress(Evas_Object *obj)
113 {               
114         if(!ext_mod) return;
115         Evas_Object *top;
116         const Eina_List *l;
117         const Elm_Entry_Context_Menu_Item *it;
118         const char *context_menu_orientation;
119
120         /*update*/
121         elm_entry_extension_module_data_get(obj,ext_mod);
122
123         if (ext_mod->popup) evas_object_del(ext_mod->popup);
124        else elm_widget_scroll_freeze_push(obj);
125         top = elm_widget_top_get(obj);
126         if(top)
127                 ext_mod->popup = elm_ctxpopup_add(top);
128         /*currently below theme not used,when guideline comes a new theme can be created if required*/
129         //elm_object_style_set(ext_mod->popup,"entry");
130         elm_ctxpopup_scroller_disabled_set(ext_mod->popup, EINA_TRUE);
131         context_menu_orientation = edje_object_data_get
132         (ext_mod->ent, "context_menu_orientation");
133         if ((context_menu_orientation) &&
134         (!strcmp(context_menu_orientation, "horizontal")))
135                 elm_ctxpopup_horizontal_set(ext_mod->popup, EINA_TRUE);
136
137         elm_widget_sub_object_add(obj, ext_mod->popup);
138         if (!ext_mod->selmode)
139                 {       
140                         if (!ext_mod->password)
141                                 elm_ctxpopup_item_add(ext_mod->popup, NULL, "Select",_select, obj );
142                         if (1) // need way to detect if someone has a selection
143                                 {
144                                         if (ext_mod->editable)
145                                                 elm_ctxpopup_item_add(ext_mod->popup, NULL, "Paste",    _paste, obj );
146                                 }
147         //              elm_ctxpopup_item_add(wd->ctxpopup, NULL, "Selectall",_select_all, obj );
148                 }
149         else
150                 {
151                           if (!ext_mod->password)
152                                 {
153                                         if (ext_mod->have_selection)
154                                                 {
155                                                         elm_ctxpopup_item_add(ext_mod->popup, NULL, "Copy",_copy, obj );
156                                                         if (ext_mod->editable)
157                                                                 elm_ctxpopup_item_add(ext_mod->popup, NULL, "Cut",_cut, obj );                                                  
158                                                 }
159                                         else
160                                                 {
161                                                         _cancel(obj,ext_mod->popup,NULL);               
162                                                         elm_ctxpopup_item_add(ext_mod->popup, NULL, "Select",_select, obj );
163                                                         if (1) // need way to detect if someone has a selection
164                                                                 {
165                                                                         if (ext_mod->editable)
166                                                                                 elm_ctxpopup_item_add(ext_mod->popup, NULL, "Paste",    _paste, obj );
167                                                                 }
168                                                 }
169                                 }
170                 }
171                 EINA_LIST_FOREACH(ext_mod->items, l, it)
172                 {
173                         elm_ctxpopup_item_add(ext_mod->popup, NULL, it->label,_item_clicked, it );
174                 }
175         if (ext_mod->popup)
176                 {
177                         _ctxpopup_position(obj);
178                         evas_object_show(ext_mod->popup);                 
179                 }
180                 ext_mod->longpress_timer = NULL;
181         }
182
183 EAPI void
184 obj_mouseup(Evas_Object *obj)
185 {
186
187         if (!obj || !ext_mod) {
188                 return;
189         }
190
191 /*update*/
192         elm_entry_extension_module_data_get(obj,ext_mod);
193    if (ext_mod->longpress_timer)
194      {    
195                 if (ext_mod->have_selection )
196                         {                               
197                                 _cancel(obj,ext_mod->popup,NULL);
198                         }
199      }     
200   else
201    {
202                 if (ext_mod->have_selection )
203                         {
204                                 obj_longpress( obj );
205                         }
206    }
207 }
208
209