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