Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_mobile / wrt-popup / wrt / popup-bin / renderer / popup_controller.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file    popup_controller.h
18  * @author  Lukasz Wrzosek (l.wrzosek@samsung.com)
19  * @version 1.0
20  * @bref    Header file for popup controller
21  */
22
23 /**
24  * To display a popup from a given class:
25  *
26  * class ABC
27  * {
28  *    void AskUser()
29  *    {
30  *    }
31  *
32  *    void DoSomeLogicWithAnswer()
33  *    {
34  *    }
35  * };
36  *
37  * ... update the class to something simmilar:
38  *
39  * class ABC : Popup::PopupControllerUser
40  * {
41  *    void AskUser() {
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);
54  *    }
55  *
56  *    void DoSomeLogicWithAnswer(Popup::LabelId answer) {
57  *        if (answer == 1)
58  *            ;//name1 pressed
59  *        else if (answer == 2)
60  *            ;//name2 pressed
61  *    }
62  * };
63  **/
64
65 #ifndef WRT_SRC_POPUP_CONTROLLER_POPUP_CONTROLLER_H_
66 #define WRT_SRC_POPUP_CONTROLLER_POPUP_CONTROLLER_H_
67
68 #include <memory>
69 #include <dpl/singleton.h>
70 #include <dpl/event/controller.h>
71 #include <dpl/event/event_listener.h>
72 #include <dpl/generic_event.h>
73 #include <dpl/mutex.h>
74 #include <dpl/exception.h>
75 #include <dpl/noncopyable.h>
76 #include <dpl/log/log.h>
77 #include "popup_manager.h"
78
79 namespace Wrt {
80 namespace Popup {
81 namespace Renderer {
82 typedef int LabelId;
83
84 struct PopupAnswerCallback
85 {
86     typedef void (PopupAnswerCallback::*MemberPtr)();
87
88     void* callee;
89     MemberPtr member;
90     void (*callTranslator)(PopupAnswerCallback* callData,
91                            const AnswerCallbackData& answer);
92
93     void Call(const AnswerCallbackData& answer)
94     {
95         callTranslator(this, answer);
96     }
97 };
98
99 class PopupController;
100 class CtrlPopup;
101
102 typedef std::shared_ptr<CtrlPopup> CtrlPopupPtr;
103
104 DECLARE_GENERIC_EVENT_3(PopupAnswerEvent,
105                         CtrlPopupPtr,
106                         PopupAnswerCallback,
107                         AnswerCallbackData)
108
109 DECLARE_GENERIC_EVENT_2(ShowPopupEventShort,
110                         CtrlPopupPtr,
111                         PopupAnswerCallback)
112
113 class CtrlPopup : public DPL::Event::EventSupport<PopupAnswerEvent>,
114     public std::enable_shared_from_this<CtrlPopup>
115 {
116   public:
117     void SetTitle(const std::string &title);
118     void Append(PopupObject::IPopupObject *object);
119
120     ~CtrlPopup();
121
122   private:
123     friend class PopupController;
124     friend class std::shared_ptr<CtrlPopup>;
125
126     explicit CtrlPopup(IPopupPtr popup);
127     void EmitAnswer(const AnswerCallbackData& answer);
128
129     IPopupPtr m_popup;
130     PopupAnswerCallback m_callback;
131 };
132
133 class PopupController :
134     public DPL::Event::Controller<DPL::TypeListDecl<ShowPopupEventShort>::Type>
135 {
136   public:
137     CtrlPopupPtr CreatePopup() const;
138
139     void setExternalCanvas(void* canvas)
140     {
141         m_canvas = canvas;
142     }
143     void* getExternalCanvas() const
144     {
145         return m_canvas;
146     }
147     void* m_canvas;
148
149   protected:
150     virtual void OnEventReceived(const ShowPopupEventShort& event);
151     PopupController();
152
153   private:
154     static void StaticOnAnswerReceived(const AnswerCallbackData& answer,
155                                        CtrlPopupPtr* popup);
156 };
157
158 class PopupControllerUser : DPL::Event::EventListener<PopupAnswerEvent>
159 {
160     template <class Type>
161     struct PopupAnswerCallbackCreator
162     {
163         typedef void (Type::*MemberPtr)(const AnswerCallbackData& answer);
164         union Caster
165         {
166             MemberPtr specific;
167             PopupAnswerCallback::MemberPtr generic;
168         };
169
170         static PopupAnswerCallback Create(Type* callee,
171                                           MemberPtr callback)
172         {
173             PopupAnswerCallback callData;
174
175             callData.callee = callee;
176
177             Caster caster;
178             caster.specific = callback;
179             callData.member = caster.generic;
180
181             callData.callTranslator =
182                 &PopupAnswerCallbackCreator::MemberCallbackTranslator;
183
184             return callData;
185         }
186
187         static void MemberCallbackTranslator(PopupAnswerCallback* callData,
188                                              const AnswerCallbackData& answer)
189         {
190             Type* typedThis = static_cast<Type*>(callData->callee);
191             Caster caster;
192             caster.generic = callData->member;
193             MemberPtr typedCallback = caster.specific;
194             (typedThis->*typedCallback)(answer);
195         }
196     };
197
198   protected:
199     virtual void OnEventReceived(const PopupAnswerEvent& event);
200     void ListenForAnswer(CtrlPopupPtr popup);
201
202     template <class Type>
203     PopupAnswerCallback MakeAnswerCallback(Type* This,
204                                            void (Type::*callback)
205                                            (const AnswerCallbackData &))
206     {
207         return PopupAnswerCallbackCreator<Type>::Create(This, callback);
208     }
209 };
210
211 typedef DPL::Singleton<PopupController> PopupControllerSingleton;
212 }
213 } //namespace Popup
214 } //namespace Wrt
215
216 #endif //WRT_SRC_POPUP_CONTROLLER_POPUP_CONTROLLER_H_