Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_wearable / wrt-popup / wrt / popup-bin / renderer / popup_manager.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_manager.h
18  * @author      Lukasz Wrzosek (l.wrzosek@samsung.com)
19  * @version     1.0
20  * @brief       This is popup_manager declaration file
21  */
22
23 #ifndef WRT_SRC_POPUP_POPUP_MANAGER_H_
24 #define WRT_SRC_POPUP_POPUP_MANAGER_H_
25
26 #include <memory>
27 #include <dpl/assert.h>
28 #include <dpl/noncopyable.h>
29 #include <dpl/singleton.h>
30 #include <dpl/log/log.h>
31 #include "popup.h"
32 #include "popup_renderer.h"
33
34 namespace Wrt {
35 namespace Popup {
36 namespace Renderer {
37 class PopupManager : DPL::Noncopyable
38 {
39     template <class ArgType>
40     struct TemplatedPopupCallback
41     {
42         typedef void (*Type)(const AnswerCallbackData& answer, ArgType* data);
43     };
44
45   public:
46     PopupManager() : m_initialized(false) {}
47     ~PopupManager()
48     {
49         if (m_initialized) {
50             LogError("Destroyed without Deinitialize");
51         }
52     }
53     void Initialize (PopupRendererPtr creator);
54     void Deinitialize();
55     void SetPopupRenderer (PopupRendererPtr creator);
56     IPopupPtr CreatePopup();
57
58     template <class ArgType>
59     void RunAsyncWithArgType(
60         IPopupPtr popup,
61         typename TemplatedPopupCallback<ArgType>::Type
62         callback,
63         ArgType* argument)
64     {
65         Assert(callback);
66         WrapCbAndArg<ArgType>* wrapped =
67             new WrapCbAndArg<ArgType>(callback, argument);
68         popup->Show(&CallbackArgTypeTranslator<ArgType>, wrapped);
69     }
70
71     void Show(IPopupPtr popup,
72               IPopup::PopupCallbackType callback,
73               void* userdata)
74     {
75         popup->Show(callback, userdata);
76     }
77
78     void setExternalCanvas(void* externalCanvas)
79     {
80         AssertMsg(m_initialized, "Manger should be initialized");
81         m_popupRenderer->setExternalCanvas(externalCanvas);
82     }
83
84   private:
85     template <class ArgType>
86     struct WrapCbAndArg
87     {
88         WrapCbAndArg(typename TemplatedPopupCallback<ArgType>::Type cb,
89                      ArgType* arg) :
90             callback(cb),
91             argument(arg)
92         {}
93
94         typename TemplatedPopupCallback<ArgType>::Type callback;
95         ArgType* argument;
96     };
97
98     template <class ArgType>
99     static void CallbackArgTypeTranslator(const AnswerCallbackData & answer,
100                                           void* data)
101     {
102         WrapCbAndArg<ArgType>* wrapped =
103             static_cast< WrapCbAndArg<ArgType>* >(data);
104         wrapped->callback(answer, wrapped->argument);
105         delete wrapped;
106     }
107
108     bool m_initialized;
109     PopupRendererPtr m_popupRenderer;
110 };
111
112 typedef DPL::Singleton<PopupManager> PopupManagerSingleton;
113 }
114 } // namespace Popup
115 } // namespace Wrt
116
117 #endif //WRT_SRC_POPUP_POPUP_MANAGER_H_