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.
5 #include "popup/popup.h"
7 #include <efl_assist.h>
9 #include "runtime/native_window.h"
10 #include "runtime/popup_string.h"
11 #include "common/logger.h"
17 const char* kEdjPath = "/usr/share/edje/wrt/wrt.edj";
18 const char* kEdcGroupName = "PopupCommon";
20 const char* kContentTitle = "title,text";
21 const char* kContentButton1 = "button1";
22 const char* kContentButton2 = "button2";
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";
30 const char* kSignalEdit = "elm,action,hide,search_icon";
32 const char* kStateActivated = "activated";
33 const char* kStateClicked = "clicked";
35 const double kMaxPopupHeight = 0.80;
36 const double kMaxScrollerHeight = 0.80;
38 static void ButtonClickedCallback(void* data,
39 Evas_Object* obj, void* /*eventInfo*/) {
40 Popup* popup = static_cast<Popup*>(data);
42 LOGGER(ERROR) << "Fail to get Popup instance";
45 popup->Result(popup->IsPositiveButton(obj));
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,
57 elm_object_part_content_set(popup->popup(), content, btn);
58 evas_object_smart_callback_add(btn, kStateClicked,
59 ButtonClickedCallback, popup);
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);
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);
80 elm_entry_password_set(entry, EINA_TRUE);
81 elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_PASSWORD);
87 static Evas_Object* AddEntrySet(Evas_Object* parent,
88 const char* str_id, Popup::EntryType type) {
90 Evas_Object* entry_grid = elm_grid_add(parent);
91 evas_object_size_hint_weight_set(entry_grid, EVAS_HINT_EXPAND,
93 evas_object_size_hint_align_set(entry_grid, EVAS_HINT_FILL, EVAS_HINT_FILL);
94 evas_object_show(entry_grid);
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,
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);
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);
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);
130 Popup* Popup::CreatePopup(Evas_Object* window) {
131 Evas_Object* popup = elm_popup_add(window);
132 elm_object_style_set(popup, kStyleDefault);
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);
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);
147 evas_object_event_callback_add(popup, EVAS_CALLBACK_RESIZE, NULL, NULL);
149 return new Popup(popup, grid, box);
152 void Popup::SetButtonType(ButtonType type) {
153 enable_button_ = true;
155 case ButtonType::OkButton:
156 button1_ = AddButton(this, popup_string::kPopupButtonOk,
159 case ButtonType::OkCancelButton:
160 button1_ = AddButton(this, popup_string::kPopupButtonCancel,
162 button2_ = AddButton(this, popup_string::kPopupButtonOk,
164 case ButtonType::LoginCancelButton:
165 button1_ = AddButton(this, popup_string::kPopupButtonCancel,
167 button2_ = AddButton(this, popup_string::kPopupButtonLogin,
170 case ButtonType::AllowDenyButton:
171 button1_ = AddButton(this, popup_string::kPopupButtonDeny,
173 button2_ = AddButton(this, popup_string::kPopupButtonAllow,
179 bool Popup::IsPositiveButton(Evas_Object* button) {
180 if (button == NULL || button1_ == NULL)
183 return button == button2_;
186 bool Popup::GetButtonResult() const {
187 return result_button_;
190 void Popup::SetFirstEntry(const std::string& str_id, EntryType type) {
191 enable_entry_ = true;
192 entry1_ = AddEntrySet(box_, str_id.c_str(), type);
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";
201 entry2_ = AddEntrySet(box_, str_id.c_str(), type);
204 std::string Popup::GetFirstEntryResult() const {
205 return result_entry1_;
208 std::string Popup::GetSecondEntryResult() const {
209 return result_entry2_;
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,
220 elm_box_pack_end(box_, check_box_);
221 evas_object_show(check_box_);
224 bool Popup::GetCheckBoxResult() const {
225 return result_check_box_;
228 void Popup::SetTitle(const std::string& str_id) {
229 elm_object_domain_translatable_part_text_set(popup_, kContentTitle,
230 popup_string::kTextDomainWrt,
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,
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);
248 void Popup::SetResultHandler(std::function<void
249 (Popup* popup, void* user_data)> handler, void* user_data) {
251 user_data_ = user_data;
255 evas_object_show(popup_);
259 evas_object_hide(popup_);
260 evas_object_del(popup_);
261 ecore_idler_add([](void* popup) {
262 Popup* obj = static_cast<Popup*>(popup);
268 void Popup::Result(bool is_positive) {
269 if (enable_button_) {
270 result_button_ = is_positive;
272 if (enable_entry_ && !!entry1_) {
273 result_entry1_ = elm_entry_entry_get(entry1_);
275 result_entry2_ = elm_entry_entry_get(entry2_);
278 if (enable_check_box_) {
279 result_check_box_ = elm_check_state_get(check_box_);
282 handler_(this, user_data_);
285 Popup::Popup(Evas_Object* popup, Evas_Object* grid, Evas_Object* box)
286 : popup_(popup), grid_(grid), box_(box) {}