2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "runtime/popup.h"
20 #include <efl_assist.h>
22 #include "runtime/native_window.h"
23 #include "runtime/popup_string.h"
24 #include "common/logger.h"
30 const char* kContentTitle = "title,text";
31 const char* kContentButton1 = "button1";
32 const char* kContentButton2 = "button2";
34 const char* kStyleDefault = "default";
35 const char* kStyleLabel = "default";
36 const char* kStyleButton = "popup";
37 const char* kStyleEditPw = "editfield/password/popup";
39 const char* kSignalEdit = "elm,action,hide,search_icon";
41 const char* kStateActivated = "activated";
42 const char* kStateClicked = "clicked";
44 const double kMaxPopupHeight = 0.80;
45 const double kMaxScrollerHeight = 0.80;
47 static void ButtonClickedCallback(void* data,
48 Evas_Object* obj, void* /*eventInfo*/) {
49 Popup* popup = static_cast<Popup*>(data);
51 LOGGER(ERROR) << "Fail to get Popup instance";
54 popup->Result(popup->IsPositiveButton(obj));
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,
66 elm_object_part_content_set(popup->popup(), content, btn);
67 evas_object_smart_callback_add(btn, kStateClicked,
68 ButtonClickedCallback, popup);
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);
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);
89 elm_entry_password_set(entry, EINA_TRUE);
90 elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_PASSWORD);
96 static Evas_Object* AddEntrySet(Evas_Object* parent,
97 const char* str_id, Popup::EntryType type) {
99 Evas_Object* entry_grid = elm_grid_add(parent);
100 evas_object_size_hint_weight_set(entry_grid, EVAS_HINT_EXPAND,
102 evas_object_size_hint_align_set(entry_grid, EVAS_HINT_FILL, EVAS_HINT_FILL);
103 evas_object_show(entry_grid);
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,
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);
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);
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);
139 Popup* Popup::CreatePopup(NativeWindow* window) {
140 Evas_Object* popup = elm_popup_add(window->evas_object());
141 elm_object_style_set(popup, kStyleDefault);
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);
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);
156 evas_object_event_callback_add(popup, EVAS_CALLBACK_RESIZE, NULL, NULL);
158 return new Popup(popup, grid, box);
161 void Popup::SetButtonType(ButtonType type) {
162 enable_button_ = true;
164 case ButtonType::OkButton:
165 button1_ = AddButton(this, popup_string::kPopupButtonOk,
168 case ButtonType::OkCancelButton:
169 button1_ = AddButton(this, popup_string::kPopupButtonCancel,
171 button2_ = AddButton(this, popup_string::kPopupButtonOk,
173 case ButtonType::LoginCancelButton:
174 button1_ = AddButton(this, popup_string::kPopupButtonCancel,
176 button2_ = AddButton(this, popup_string::kPopupButtonLogin,
179 case ButtonType::AllowDenyButton:
180 button1_ = AddButton(this, popup_string::kPopupButtonDeny,
182 button2_ = AddButton(this, popup_string::kPopupButtonAllow,
188 bool Popup::IsPositiveButton(Evas_Object* button) {
189 if (button == NULL || button1_ == NULL)
192 return button == button2_;
195 bool Popup::GetButtonResult() const {
196 return result_button_;
199 void Popup::SetFirstEntry(const std::string& str_id, EntryType type) {
200 enable_entry_ = true;
201 entry1_ = AddEntrySet(box_, str_id.c_str(), type);
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";
210 entry2_ = AddEntrySet(box_, str_id.c_str(), type);
213 std::string Popup::GetFirstEntryResult() const {
214 return result_entry1_;
217 std::string Popup::GetSecondEntryResult() const {
218 return result_entry2_;
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,
229 elm_box_pack_end(box_, check_box_);
230 evas_object_show(check_box_);
233 bool Popup::GetCheckBoxResult() const {
234 return result_check_box_;
237 void Popup::SetTitle(const std::string& str_id) {
238 elm_object_domain_translatable_part_text_set(popup_, kContentTitle,
239 popup_string::kTextDomainWrt,
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,
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);
257 void Popup::SetResultHandler(std::function<void
258 (Popup* popup, void* user_data)> handler, void* user_data) {
260 user_data_ = user_data;
264 evas_object_show(popup_);
268 evas_object_hide(popup_);
269 evas_object_del(popup_);
270 ecore_idler_add([](void* popup) {
271 Popup* obj = static_cast<Popup*>(popup);
277 void Popup::Result(bool is_positive) {
278 if (enable_button_) {
279 result_button_ = is_positive;
281 if (enable_entry_ && !!entry1_) {
282 result_entry1_ = elm_entry_entry_get(entry1_);
284 result_entry2_ = elm_entry_entry_get(entry2_);
287 if (enable_check_box_) {
288 result_check_box_ = elm_check_state_get(check_box_);
291 handler_(this, user_data_);
294 Popup::Popup(Evas_Object* popup, Evas_Object* grid, Evas_Object* box)
295 : popup_(popup), grid_(grid), box_(box) {}