2 * Copyright (c) 2011 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.
17 * @file popup_controller.h
18 * @author Lukasz Wrzosek (l.wrzosek@samsung.com)
20 * @bref Header file for popup controller
24 * To display a popup from a given class:
32 * void DoSomeLogicWithAnswer()
37 * ... update the class to something simmilar:
39 **class ABC : Popup::PopupControllerUser
42 * using namespace Popup;
43 * CtrlPopupPtr popup =
44 * PopupControllerSingletion::Instance().CreatePopup();
45 * popup->SetTitle("Title");
46 * popup->SetContent("Content");
47 * popup->AddButton("name1", 1);
48 * popup->AddButton("name2", 2);
49 * ListenForAnswer(popup);
50 * ShowPopupEvent event(popup,
51 * MakeAnswerCallback(this,
52 * &ABC::DoSomeLogicWithAnswer));
53 * CONTROLLER_POST_EVENT(PopupController, event);
56 * void DoSomeLogicWithAnswer(Popup::LabelId answer) {
59 * else if (answer == 2)
65 #ifndef WRT_SRC_POPUP_CONTROLLER_POPUP_CONTROLLER_H_
66 #define WRT_SRC_POPUP_CONTROLLER_POPUP_CONTROLLER_H_
68 #include <dpl/singleton.h>
69 #include <dpl/event/controller.h>
70 #include <dpl/event/event_listener.h>
71 #include <dpl/generic_event.h>
72 #include <dpl/mutex.h>
73 #include <dpl/exception.h>
74 #include <dpl/shared_ptr.h>
75 #include <dpl/enable_shared_from_this.h>
76 #include <dpl/noncopyable.h>
77 #include <dpl/log/log.h>
78 #include <dpl/popup/popup_manager.h>
84 struct PopupAnswerCallback
86 typedef void (PopupAnswerCallback::*MemberPtr)();
90 void (*callTranslator)(PopupAnswerCallback* callData,
91 const AnswerCallbackData& answer);
93 void Call(const AnswerCallbackData& answer)
95 callTranslator(this, answer);
99 class PopupController;
102 typedef DPL::SharedPtr<CtrlPopup> CtrlPopupPtr;
104 DECLARE_GENERIC_EVENT_3(PopupAnswerEvent,
109 DECLARE_GENERIC_EVENT_2(ShowPopupEventShort,
113 class CtrlPopup : public DPL::Event::EventSupport<PopupAnswerEvent>,
114 protected DPL::EnableSharedFromThis<CtrlPopup>
117 void SetTitle(const std::string &title);
118 void Append(PopupObject::IPopupObject *object);
121 friend class PopupController;
122 friend class DPL::SharedPtr<CtrlPopup>;
124 explicit CtrlPopup(IPopupPtr popup);
126 void EmitAnswer(const AnswerCallbackData& answer);
129 PopupAnswerCallback m_callback;
132 class PopupController :
133 public DPL::Event::Controller<DPL::TypeListDecl<ShowPopupEventShort>::Type>
136 CtrlPopupPtr CreatePopup() const;
138 void setExternalCanvas(void* canvas)
142 void* getExternalCanvas() const
149 virtual void OnEventReceived(const ShowPopupEventShort& event);
153 static void StaticOnAnswerReceived(const AnswerCallbackData& answer,
154 CtrlPopupPtr* popup);
157 class PopupControllerUser : DPL::Event::EventListener<PopupAnswerEvent>
159 template <class Type>
160 struct PopupAnswerCallbackCreator
162 typedef void (Type::*MemberPtr)(const AnswerCallbackData& answer);
166 PopupAnswerCallback::MemberPtr generic;
169 static PopupAnswerCallback Create(Type* callee,
172 PopupAnswerCallback callData;
174 callData.callee = callee;
177 caster.specific = callback;
178 callData.member = caster.generic;
180 callData.callTranslator =
181 &PopupAnswerCallbackCreator::MemberCallbackTranslator;
186 static void MemberCallbackTranslator(PopupAnswerCallback* callData,
187 const AnswerCallbackData& answer)
189 Type* typedThis = static_cast<Type*>(callData->callee);
191 caster.generic = callData->member;
192 MemberPtr typedCallback = caster.specific;
193 (typedThis->*typedCallback)(answer);
198 virtual void OnEventReceived(const PopupAnswerEvent& event);
199 void ListenForAnswer(CtrlPopupPtr popup);
201 template <class Type>
202 PopupAnswerCallback MakeAnswerCallback(Type* This,
203 void (Type::*callback)
204 (const AnswerCallbackData &))
206 return PopupAnswerCallbackCreator<Type>::Create(This, callback);
210 typedef DPL::Singleton<PopupController> PopupControllerSingleton;
215 #endif //WRT_SRC_POPUP_CONTROLLER_POPUP_CONTROLLER_H_