Fix invalid licenses
[platform/framework/web/crosswalk-tizen.git] / tests / popup / popup.cc
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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
18 #include "popup/popup.h"
19
20 #include <efl_assist.h>
21
22 #include "runtime/native_window.h"
23 #include "runtime/popup_string.h"
24 #include "common/logger.h"
25
26 namespace wrt {
27
28 namespace {
29
30 const char* kContentTitle = "title,text";
31 const char* kContentButton1 = "button1";
32 const char* kContentButton2 = "button2";
33
34 const char* kStyleDefault = "default";
35 const char* kStyleLabel = "default";
36 const char* kStyleButton = "popup";
37 const char* kStyleEditPw = "editfield/password/popup";
38
39 const char* kSignalEdit = "elm,action,hide,search_icon";
40
41 const char* kStateActivated = "activated";
42 const char* kStateClicked = "clicked";
43
44 const double kMaxPopupHeight = 0.80;
45 const double kMaxScrollerHeight = 0.80;
46
47 static void ButtonClickedCallback(void* data,
48                                   Evas_Object* obj, void* /*eventInfo*/) {
49   Popup* popup = static_cast<Popup*>(data);
50   if (!popup) {
51     LOGGER(ERROR) << "Fail to get Popup instance";
52     return;
53   }
54   popup->Result(popup->IsPositiveButton(obj));
55   popup->Hide();
56 }
57
58 // caution: not Evas_Object* but Popup*
59 static Evas_Object* AddButton(Popup* popup,
60                               const char* str_id, const char* content) {
61   Evas_Object* btn = elm_button_add(popup->popup());
62   elm_object_style_set(btn, kStyleButton);
63   elm_object_domain_translatable_part_text_set(btn, 0,
64                                                popup_string::kTextDomainWrt,
65                                                str_id);
66   elm_object_part_content_set(popup->popup(), content, btn);
67   evas_object_smart_callback_add(btn, kStateClicked,
68                                  ButtonClickedCallback, popup);
69   return btn;
70 }
71
72 static Evas_Object* AddEntry(Evas_Object* parent, Popup::EntryType type) {
73   Evas_Object* entry = elm_entry_add(parent);
74   elm_object_style_set(entry, kStyleEditPw);
75   elm_entry_single_line_set(entry, EINA_TRUE);
76   elm_entry_scrollable_set(entry, EINA_TRUE);
77   evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
78   evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
79   elm_entry_prediction_allow_set(entry, EINA_FALSE);
80   elm_object_signal_emit(entry, kSignalEdit, "");
81   elm_entry_autocapital_type_set(entry, ELM_AUTOCAPITAL_TYPE_NONE);
82
83   if (type == Popup::EntryType::Edit) {
84     evas_object_smart_callback_add(entry, kStateActivated,
85                                    [](void*, Evas_Object* obj, void*) {
86                                      elm_object_focus_set(obj, EINA_TRUE);
87                                    }, NULL);
88   } else {
89     elm_entry_password_set(entry, EINA_TRUE);
90     elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_PASSWORD);
91   }
92
93   return entry;
94 }
95
96 static Evas_Object* AddEntrySet(Evas_Object* parent,
97                                 const char* str_id, Popup::EntryType type) {
98   // a grid for entry
99   Evas_Object* entry_grid = elm_grid_add(parent);
100   evas_object_size_hint_weight_set(entry_grid, EVAS_HINT_EXPAND,
101                                    EVAS_HINT_EXPAND);
102   evas_object_size_hint_align_set(entry_grid, EVAS_HINT_FILL, EVAS_HINT_FILL);
103   evas_object_show(entry_grid);
104
105   // label for the entry
106   Evas_Object* entry_label = elm_label_add(entry_grid);
107   elm_object_style_set(entry_label, kStyleLabel);
108   elm_object_domain_translatable_part_text_set(entry_label, 0,
109                                                popup_string::kTextDomainWrt,
110                                                str_id);
111   evas_object_color_set(entry_label, 0, 0, 0, 255);
112   evas_object_size_hint_weight_set(entry_label,
113                                    EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
114   evas_object_size_hint_align_set(entry_label, EVAS_HINT_FILL, EVAS_HINT_FILL);
115   elm_grid_pack(entry_grid, entry_label, 0, 0, 30, 100);
116   evas_object_show(entry_label);
117
118   // entry
119   Evas_Object* entry = AddEntry(entry_grid, type);
120   evas_object_show(entry);
121   elm_grid_pack(entry_grid, entry, 30, 0, 40, 100);
122   elm_box_pack_end(parent, entry_grid);
123
124   return entry;
125 }
126
127 static Evas_Object* AddCheckBox(Evas_Object* parent) {
128   Evas_Object* check = elm_check_add(parent);
129   elm_object_style_set(check, kStyleDefault);
130   elm_object_style_set(check, "multiline");
131   evas_object_size_hint_align_set(check, 0.0, 0.0);
132   evas_object_color_set(check, 0, 0, 0, 255);
133   elm_check_state_set(check, EINA_TRUE);
134   return check;
135 }
136
137 }  // namespace
138
139 Popup* Popup::CreatePopup(Evas_Object* window) {
140   Evas_Object* popup = elm_popup_add(window);
141   elm_object_style_set(popup, kStyleDefault);
142
143   Evas_Object* grid = elm_grid_add(popup);
144   evas_object_size_hint_weight_set(grid, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
145   evas_object_size_hint_align_set(grid, EVAS_HINT_FILL, EVAS_HINT_FILL);
146   elm_object_part_content_set(popup, "default", grid);
147   evas_object_show(grid);
148
149   Evas_Object* box = elm_box_add(grid);
150   elm_box_padding_set(box, 0, 10);
151   evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
152   evas_object_size_hint_align_set(box, EVAS_HINT_FILL, EVAS_HINT_FILL);
153   elm_grid_pack(grid, box, 3, 3, 94, 94);
154   evas_object_show(box);
155
156   evas_object_event_callback_add(popup, EVAS_CALLBACK_RESIZE, NULL, NULL);
157
158   return new Popup(popup, grid, box);
159 }
160
161 void Popup::SetButtonType(ButtonType type) {
162   enable_button_ = true;
163   switch (type) {
164     case ButtonType::OkButton:
165     button1_ = AddButton(this, popup_string::kPopupButtonOk,
166                          kContentButton1);
167     break;
168     case ButtonType::OkCancelButton:
169     button1_ = AddButton(this, popup_string::kPopupButtonCancel,
170                          kContentButton1);
171     button2_ = AddButton(this, popup_string::kPopupButtonOk,
172                          kContentButton2);
173     case ButtonType::LoginCancelButton:
174     button1_ = AddButton(this, popup_string::kPopupButtonCancel,
175                          kContentButton1);
176     button2_ = AddButton(this, popup_string::kPopupButtonLogin,
177                          kContentButton2);
178     break;
179     case ButtonType::AllowDenyButton:
180     button1_ = AddButton(this, popup_string::kPopupButtonDeny,
181                          kContentButton1);
182     button2_ = AddButton(this, popup_string::kPopupButtonAllow,
183                          kContentButton2);
184     break;
185   }
186 }
187
188 bool Popup::IsPositiveButton(Evas_Object* button) {
189   if (button == NULL || button1_ == NULL)
190     return false;
191   else
192     return button == button2_;
193 }
194
195 bool Popup::GetButtonResult() const {
196   return result_button_;
197 }
198
199 void Popup::SetFirstEntry(const std::string& str_id, EntryType type) {
200   enable_entry_ = true;
201   entry1_ = AddEntrySet(box_, str_id.c_str(), type);
202 }
203
204 // suppose that it is called after SetFirstEntry()
205 void Popup::SetSecondEntry(const std::string& str_id, EntryType type) {
206   if (!enable_entry_ || !entry1_) {
207     LOGGER(ERROR) << "SetFirstEntry() is not called yet";
208     return;
209   }
210   entry2_ = AddEntrySet(box_, str_id.c_str(), type);
211 }
212
213 std::string Popup::GetFirstEntryResult() const {
214   return result_entry1_;
215 }
216
217 std::string Popup::GetSecondEntryResult() const {
218   return result_entry2_;
219 }
220
221 void Popup::SetCheckBox(const std::string& str_id) {
222   enable_check_box_ = true;
223   check_box_ = AddCheckBox(box_);
224   if (!str_id.empty()) {
225     elm_object_domain_translatable_part_text_set(check_box_, 0,
226                                                  popup_string::kTextDomainWrt,
227                                                  str_id.c_str());
228   }
229   elm_box_pack_end(box_, check_box_);
230   evas_object_show(check_box_);
231 }
232
233 bool Popup::GetCheckBoxResult() const {
234   return result_check_box_;
235 }
236
237 void Popup::SetTitle(const std::string& str_id) {
238   elm_object_domain_translatable_part_text_set(popup_, kContentTitle,
239                                                popup_string::kTextDomainWrt,
240                                                str_id.c_str());
241 }
242
243 void Popup::SetBody(const std::string& str_id) {
244   Evas_Object* label = elm_label_add(box_);
245   elm_object_style_set(label, kStyleLabel);
246   elm_label_line_wrap_set(label, ELM_WRAP_MIXED);
247   elm_object_domain_translatable_part_text_set(label, 0,
248                                                popup_string::kTextDomainWrt,
249                                                str_id.c_str());
250   evas_object_color_set(label, 0, 0, 0, 255);
251   evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
252   evas_object_size_hint_align_set(label, EVAS_HINT_FILL, EVAS_HINT_FILL);
253   elm_box_pack_end(box_, label);
254   evas_object_show(label);
255 }
256
257 void Popup::SetResultHandler(std::function<void
258     (Popup* popup, void* user_data)> handler, void* user_data) {
259   handler_ = handler;
260   user_data_ = user_data;
261 }
262
263 void Popup::Show() {
264   evas_object_show(popup_);
265 }
266
267 void Popup::Hide() {
268   evas_object_hide(popup_);
269   evas_object_del(popup_);
270   ecore_idler_add([](void* popup) {
271       Popup* obj = static_cast<Popup*>(popup);
272       delete obj;
273       return EINA_FALSE;
274     }, this);
275 }
276
277 void Popup::Result(bool is_positive) {
278   if (enable_button_) {
279     result_button_ = is_positive;
280   }
281   if (enable_entry_ && !!entry1_) {
282     result_entry1_ = elm_entry_entry_get(entry1_);
283     if (!!entry2_) {
284       result_entry2_ = elm_entry_entry_get(entry2_);
285     }
286   }
287   if (enable_check_box_) {
288     result_check_box_ = elm_check_state_get(check_box_);
289   }
290
291   handler_(this, user_data_);
292 }
293
294 Popup::Popup(Evas_Object* popup, Evas_Object* grid, Evas_Object* box)
295   : popup_(popup), grid_(grid), box_(box) {}
296
297 Popup::~Popup() {}
298
299 }  // namespace wrt