tizen 2.3 release
[apps/home/ug-nfc-efl.git] / ug-nfc-setting-efl / src / ug-nfc-setting-popup.c
1 /*
2   * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3   *
4   * Licensed under the Flora License, Version 1.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7
8   *     http://floralicense.org/license/
9   *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16
17 #include "ug-nfc-setting-popup.h"
18
19 #include <utilX.h>
20
21 #define NFC_POPUP_AUTO_TIMEOUT_SEC 3.0
22 #define MOUSE_RIGHT_BTN_UP  3
23
24 static Evas_Object *_popup = NULL;
25 static UG_NFC_POPUP_USER_RESP_CB _user_response_cb;
26 static void *_user_data = NULL;
27
28 static void _remove_key_event_handler(void);
29 static void _add_key_event_handler(void);
30
31 static void _ug_nfc_setting_popup_response_cb(void *data, Evas_Object *obj, void *event_info)
32 {
33         int btn_type = (int)data;
34
35         if (_user_response_cb)
36                 _user_response_cb(_user_data, obj, (void*)btn_type);
37
38         LOGD("btn_type: %d", (int)btn_type);
39         LOGD("Popup is removed: [%p]", obj);
40         evas_object_del(_popup);
41
42         _popup = NULL;
43         _user_response_cb = NULL;
44         _user_data = NULL;
45 }
46
47 static Eina_Bool _ug_nfc_setting_popup_show_cb(void *data)
48 {
49         if (_popup)
50                 evas_object_show(_popup);
51
52         return ECORE_CALLBACK_CANCEL;
53 }
54
55 static void _popup_back_click_cb(void)
56 {
57         if (!_popup)
58                 return;
59
60         _remove_key_event_handler();
61
62         _ug_nfc_setting_popup_response_cb((void*)UG_NFC_POPUP_RESP_CANCEL, _popup, NULL);
63 }
64
65 static void _mouseup_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
66 {
67         Evas_Event_Mouse_Up *ev = event_info;
68
69         if (!ev)
70                 return;
71
72         if (ev->button == MOUSE_RIGHT_BTN_UP)
73                 _popup_back_click_cb();
74 }
75
76 static void _keydown_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
77 {
78         Evas_Event_Key_Down *ev = event_info;
79
80         if (!ev)
81                 return;
82
83         if (!strcmp(ev->keyname, KEY_BACK))
84                 _popup_back_click_cb();
85 }
86
87 static void _remove_key_event_handler(void)
88 {
89         if (!_popup)
90                 return;
91
92         evas_object_event_callback_del(_popup, EVAS_CALLBACK_MOUSE_UP, _mouseup_cb);
93         evas_object_event_callback_del(_popup, EVAS_CALLBACK_KEY_DOWN, _keydown_cb);
94 }
95
96 static void _add_key_event_handler(void)
97 {
98         if (!_popup)
99                 return;
100
101         evas_object_event_callback_add(_popup, EVAS_CALLBACK_MOUSE_UP, _mouseup_cb, NULL);
102         evas_object_event_callback_add(_popup, EVAS_CALLBACK_KEY_DOWN, _keydown_cb, NULL);
103 }
104
105 static void _ug_nfc_setting_popup_block_clicked_cb(void *data, Evas_Object *obj, void *event_info)
106 {
107         _ug_nfc_setting_popup_response_cb((void*)UG_NFC_POPUP_RESP_CANCEL, _popup, NULL);
108 }
109
110 Evas_Object *ug_nfc_setting_create_popup(void *data,
111                                     Evas_Object *parent_layout,
112                                     const char *title,
113                                     const char *description,
114                                     const char *btn1_text,
115                                     int btn1_type,
116                                     const char *btn2_text,
117                                     int btn2_type,
118                                     const char *btn3_text,
119                                     int btn3_type,
120                                     bool is_alert_type,
121                                     bool enable_timeout,
122                                     UG_NFC_POPUP_USER_RESP_CB response_cb)
123 {
124         Evas_Object *btn = NULL;
125
126         if (data == NULL)
127                 return NULL;
128
129         if (_popup) {
130                 ug_nfc_setting_close_popup(_popup);
131                 _popup = NULL;
132         }
133
134         _popup = elm_popup_add(parent_layout);
135         LOGD("Popup is created: [%p]", _popup);
136         evas_object_size_hint_weight_set(_popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
137
138         if (title)
139                 elm_object_part_text_set(_popup, "title,text", title);
140
141         elm_object_text_set(_popup, description);
142
143         if(btn1_text) {
144                 btn = elm_button_add(_popup);
145                 elm_object_style_set (btn, "popup");
146                 elm_object_text_set(btn, btn1_text);
147                 elm_object_part_content_set(_popup, "button1", btn);
148                 evas_object_smart_callback_add(btn, "clicked",
149                         _ug_nfc_setting_popup_response_cb, (void*)btn1_type);
150         }
151         if (btn2_text) {
152                 btn = elm_button_add(_popup);
153                 elm_object_style_set (btn, "popup");
154                 elm_object_text_set(btn, btn2_text);
155                 elm_object_part_content_set(_popup, "button2", btn);
156                 evas_object_smart_callback_add(btn, "clicked",
157                         _ug_nfc_setting_popup_response_cb, (void*)btn2_type);
158         }
159         if (btn3_text) {
160                 btn = elm_button_add(_popup);
161                 elm_object_style_set (btn, "popup");
162                 elm_object_text_set(btn, btn3_text);
163                 elm_object_part_content_set(_popup, "button3", btn);
164                 evas_object_smart_callback_add(btn, "clicked",
165                         _ug_nfc_setting_popup_response_cb, (void*)btn3_type);
166         }
167
168         _user_response_cb = response_cb;
169         _user_data = data;
170
171         if (is_alert_type) {
172                 evas_object_smart_callback_add(_popup, "block,clicked",
173                         _ug_nfc_setting_popup_block_clicked_cb, NULL);
174         }
175
176         if (enable_timeout) {
177                 elm_popup_timeout_set(_popup, NFC_POPUP_AUTO_TIMEOUT_SEC);
178                 evas_object_smart_callback_add(_popup, "timeout",
179                         _ug_nfc_setting_popup_response_cb, NULL);
180         }
181
182         _add_key_event_handler();
183
184         _ug_nfc_setting_popup_show_cb(NULL);
185
186         return _popup;
187 }
188
189
190 void ug_nfc_setting_close_popup(Evas_Object* popup)
191 {
192         if (NULL == popup || NULL == _popup || _popup != popup) {
193                 LOGD("NULL == popup || NULL == _popup || _popup != popup");
194         }
195
196         _ug_nfc_setting_popup_response_cb((void*)UG_NFC_POPUP_RESP_CANCEL, _popup, NULL);
197 }
198
199