Update wrt-commons_0.2.53
[framework/web/wrt-commons.git] / modules / popup / include / dpl / popup / 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 <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>
79
80 namespace DPL {
81 namespace Popup {
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 DPL::SharedPtr<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     protected DPL::EnableSharedFromThis<CtrlPopup>
115 {
116   public:
117     void SetTitle(const std::string &title);
118     void Append(PopupObject::IPopupObject *object);
119
120   private:
121     friend class PopupController;
122     friend class DPL::SharedPtr<CtrlPopup>;
123
124     explicit CtrlPopup(IPopupPtr popup);
125     ~CtrlPopup();
126     void EmitAnswer(const AnswerCallbackData& answer);
127
128     IPopupPtr m_popup;
129     PopupAnswerCallback m_callback;
130 };
131
132 class PopupController :
133     public DPL::Event::Controller<DPL::TypeListDecl<ShowPopupEventShort>::Type>
134 {
135   public:
136     CtrlPopupPtr CreatePopup() const;
137
138     void setExternalCanvas(void* canvas)
139     {
140         m_canvas = canvas;
141     }
142     void* getExternalCanvas() const
143     {
144         return m_canvas;
145     }
146     void* m_canvas;
147
148   protected:
149     virtual void OnEventReceived(const ShowPopupEventShort& event);
150     PopupController();
151
152   private:
153     static void StaticOnAnswerReceived(const AnswerCallbackData& answer,
154             CtrlPopupPtr* popup);
155 };
156
157 class PopupControllerUser : DPL::Event::EventListener<PopupAnswerEvent>
158 {
159     template <class Type>
160     struct PopupAnswerCallbackCreator
161     {
162         typedef void (Type::*MemberPtr)(const AnswerCallbackData& answer);
163         union Caster
164         {
165             MemberPtr specific;
166             PopupAnswerCallback::MemberPtr generic;
167         };
168
169         static PopupAnswerCallback Create(Type* callee,
170                 MemberPtr callback)
171         {
172             PopupAnswerCallback callData;
173
174             callData.callee = callee;
175
176             Caster caster;
177             caster.specific = callback;
178             callData.member = caster.generic;
179
180             callData.callTranslator =
181                 &PopupAnswerCallbackCreator::MemberCallbackTranslator;
182
183             return callData;
184         }
185
186         static void MemberCallbackTranslator(PopupAnswerCallback* callData,
187                 const AnswerCallbackData& answer)
188         {
189             Type* typedThis = static_cast<Type*>(callData->callee);
190             Caster caster;
191             caster.generic = callData->member;
192             MemberPtr typedCallback = caster.specific;
193             (typedThis->*typedCallback)(answer);
194         }
195     };
196
197   protected:
198     virtual void OnEventReceived(const PopupAnswerEvent& event);
199     void ListenForAnswer(CtrlPopupPtr popup);
200
201     template <class Type>
202     PopupAnswerCallback MakeAnswerCallback(Type* This,
203             void (Type::*callback)
204             (const AnswerCallbackData &))
205     {
206         return PopupAnswerCallbackCreator<Type>::Create(This, callback);
207     }
208 };
209
210 typedef DPL::Singleton<PopupController> PopupControllerSingleton;
211
212 } //namespace Popup
213 } //namespace DPL
214
215 #endif //WRT_SRC_POPUP_CONTROLLER_POPUP_CONTROLLER_H_