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/event/nested_loop.h>
79 #include <dpl/popup/popup_manager.h>
85 struct PopupAnswerCallback
87 typedef void (PopupAnswerCallback::*MemberPtr)();
91 void (*callTranslator)(PopupAnswerCallback* callData,
92 const AnswerCallbackData& answer);
94 void Call(const AnswerCallbackData& answer)
96 callTranslator(this, answer);
100 class PopupController;
103 typedef DPL::SharedPtr<CtrlPopup> CtrlPopupPtr;
105 DECLARE_GENERIC_EVENT_3(PopupAnswerEvent,
109 DECLARE_GENERIC_EVENT_3(ShowPopupEvent,
112 DPL::Event::LoopHandle)
114 class CtrlPopup : public DPL::Event::EventSupport<PopupAnswerEvent>,
115 protected DPL::EnableSharedFromThis<CtrlPopup>
118 void SetTitle(const std::string &title);
119 void Append(PopupObject::IPopupObject *object);
122 friend class PopupController;
123 friend class DPL::SharedPtr<CtrlPopup>;
125 explicit CtrlPopup(IPopupPtr popup);
127 void EmitAnswer(const AnswerCallbackData& answer);
130 PopupAnswerCallback m_callback;
131 DPL::Event::LoopHandle m_loopHandle;
134 class PopupController :
135 public DPL::Event::Controller<DPL::TypeListDecl<ShowPopupEvent>::Type>
138 CtrlPopupPtr CreatePopup() const;
140 void setExternalCanvas(void* canvas)
144 void* getExternalCanvas() const
151 virtual void OnEventReceived(const ShowPopupEvent& event);
155 static void StaticOnAnswerReceived(const AnswerCallbackData& answer,
156 CtrlPopupPtr* popup);
159 class PopupControllerUser : DPL::Event::EventListener<PopupAnswerEvent>
161 template <class Type>
162 struct PopupAnswerCallbackCreator
164 typedef void (Type::*MemberPtr)(const AnswerCallbackData& answer);
168 PopupAnswerCallback::MemberPtr generic;
171 static PopupAnswerCallback Create(Type* callee,
174 PopupAnswerCallback callData;
176 callData.callee = callee;
179 caster.specific = callback;
180 callData.member = caster.generic;
182 callData.callTranslator =
183 &PopupAnswerCallbackCreator::MemberCallbackTranslator;
188 static void MemberCallbackTranslator(PopupAnswerCallback* callData,
189 const AnswerCallbackData& answer)
191 Type* typedThis = static_cast<Type*>(callData->callee);
193 caster.generic = callData->member;
194 MemberPtr typedCallback = caster.specific;
195 (typedThis->*typedCallback)(answer);
200 virtual void OnEventReceived(const PopupAnswerEvent& event);
201 void ListenForAnswer(CtrlPopupPtr popup);
203 template <class Type>
204 PopupAnswerCallback MakeAnswerCallback(Type* This,
205 void (Type::*callback)
206 (const AnswerCallbackData &))
208 return PopupAnswerCallbackCreator<Type>::Create(This, callback);
212 typedef DPL::Singleton<PopupController> PopupControllerSingleton;
217 #endif //WRT_SRC_POPUP_CONTROLLER_POPUP_CONTROLLER_H_