Fork for IVI: mesa fixing
[profile/ivi/uifw.git] / src / ui / FUi_OrientationAgent.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file        FUi_OrientationAgent.cpp
19  * @brief       This is the implementation file for the _OrientationAgent class.
20  */
21 #include <FBaseSysLog.h>
22 #include "FUi_OrientationAgent.h"
23 #include "FUi_ControlManager.h"
24 #include "FUi_ControlImplManager.h"
25 #include "FUi_PublicOrientationEvent.h"
26 #include "FUi_ControlImpl.h"
27 #include "FUi_Window.h"
28 #include "FUiCtrl_FormImpl.h"
29 #include "FUiCtrl_FrameImpl.h"
30 #include "FUiCtrl_Frame.h"
31 #include "FUi_EcoreEvasMgr.h"
32 #include "FUi_EcoreEvas.h"
33
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Runtime;
36 using namespace Tizen::Ui::Controls;
37 using namespace Tizen::Graphics;
38
39 namespace Tizen { namespace Ui {
40
41 _OrientationAgent*
42 _OrientationAgent::CreateInstanceN(Control& publicControl)
43 {
44         _OrientationAgent* pAgent = new (std::nothrow) _OrientationAgent(publicControl);
45         SysTryReturn(NID_UI, pAgent, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
46
47         result r = GetLastResult();
48         SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
49
50         SetLastResult(E_SUCCESS);
51
52         return pAgent;
53
54 CATCH:
55         delete pAgent;
56         return null;
57 }
58
59 _OrientationAgent::_OrientationAgent(Control& publicControl)
60         : __publicControl(publicControl)
61         , __pPublicEvent(null)
62         , __mode(ORIENTATION_PORTRAIT)
63         , __status(ORIENTATION_STATUS_PORTRAIT)
64         , __firePublicEvent(false)
65         , __statusChanged(false)
66         , __updateStatus(true)
67 {
68         _PublicOrientationEvent* pPublicEvent = _PublicOrientationEvent::CreateInstanceN(publicControl);
69
70         result r = GetLastResult();
71         SysTryReturnVoidResult(NID_UI, pPublicEvent, r, "[%s] Propagating.", GetErrorMessage(r));
72
73         __pPublicEvent = pPublicEvent;
74
75         SetLastResult(E_SUCCESS);
76 }
77
78 _OrientationAgent::~_OrientationAgent(void)
79 {
80         if (__pPublicEvent)
81         {
82                 delete __pPublicEvent;
83                 __pPublicEvent = null;
84         }
85 }
86
87 void
88 _OrientationAgent::AddListener(IOrientationEventListener& listener)
89 {
90         result r = __pPublicEvent->AddListener(listener);
91         SysTryReturnVoidResult(NID_UI, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
92
93         SetLastResult(E_SUCCESS);
94 }
95
96 void
97 _OrientationAgent::RemoveListener(IOrientationEventListener& listener)
98 {
99         result r = __pPublicEvent->RemoveListener(listener);
100         SysTryReturnVoidResult(NID_UI, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
101
102         SetLastResult(E_SUCCESS);
103 }
104
105 Orientation
106 _OrientationAgent::GetMode(void) const
107 {
108         return __mode;
109 }
110
111 OrientationStatus
112 _OrientationAgent::GetStatus(void) const
113 {
114         return __status;
115 }
116
117 void
118 _OrientationAgent::SetMode(Orientation mode)
119 {
120         SysTryReturnVoidResult(NID_UI, mode != ORIENTATION_NONE, E_INVALID_ARG, "[E_INVALID_ARG] A specified input parameter is invalid.");
121
122         SetLastResult(E_SUCCESS);
123
124         if (__mode == mode)
125         {
126                 return;
127         }
128
129         __mode = mode;
130         Update();
131 }
132
133 void
134 _OrientationAgent::Update(bool draw)
135 {
136         _ControlImplManager* pImplManager = _ControlImplManager::GetInstance();
137         SysAssert(pImplManager);
138
139         _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
140         if (!pImpl)
141         {
142                 return;
143         }
144
145         OrientationStatus status = pImplManager->GetOrientationStatus(__mode);
146
147         if (__updateStatus == true)
148         {
149                 __statusChanged = false;
150                 if (__status != status)
151                 {
152                         __statusChanged = true;
153                         __updateStatus = false;
154                 }
155         }
156
157         __status = status;
158
159         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
160         if (!pEcoreEvas)
161         {
162                 return;
163         }
164
165         pEcoreEvas->AllowSetWindowBounds(false);
166         FireEvent(status);
167         pEcoreEvas->AllowSetWindowBounds(true);
168
169         // For the form to be made by Ui-Builder
170         if ((draw == true) && (__statusChanged == true))
171         {
172                 _ControlOrientation coreOrientation =
173                         (status == ORIENTATION_STATUS_PORTRAIT || status == ORIENTATION_STATUS_PORTRAIT_REVERSE) ?
174                         _CONTROL_ORIENTATION_PORTRAIT : _CONTROL_ORIENTATION_LANDSCAPE;
175
176                 Rectangle temp;
177                 bool exist = pImpl->GetBuilderBounds(coreOrientation, temp);
178                 if (exist)
179                 {
180                         pImpl->Invalidate(true);
181                 }
182         }
183
184         // Despite not changing status, it needs to rotate screen.
185 #if !defined(MULTI_WINDOW)
186         pImplManager->RotateScreen(status);
187 #else
188         _ControlImpl* pControlImpl = _ControlImpl::GetInstance(__publicControl);
189         pImplManager->RotateScreen(pControlImpl, status);
190 #endif
191 }
192
193 void
194 _OrientationAgent::FireOrientationEvent(void)
195 {
196         if (__firePublicEvent)
197         {
198                 return;
199         }
200
201         __firePublicEvent = true;
202
203         if (__statusChanged)
204         {
205                 IEventArg* pArg = _PublicOrientationEvent::CreateOrientationEventArgN(*__pPublicEvent->GetSource(), __status);
206                 __pPublicEvent->Fire(*pArg);
207
208                 __updateStatus = true;
209         }
210 }
211
212 void
213 _OrientationAgent::FireEvent(OrientationStatus status)
214 {
215         ClearLastResult();
216
217         _ControlManager* pCoreManager = _ControlManager::GetInstance();
218         SysAssert(pCoreManager);
219
220         _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
221         if (!pImpl)
222         {
223                 return;
224         }
225
226         _ControlOrientation coreOrientation =
227                 (status == ORIENTATION_STATUS_PORTRAIT || status == ORIENTATION_STATUS_PORTRAIT_REVERSE) ?
228                 _CONTROL_ORIENTATION_PORTRAIT : _CONTROL_ORIENTATION_LANDSCAPE;
229
230         _ControlImplManager* pImplManager = _ControlImplManager::GetInstance();
231         SysAssert(pImplManager);
232
233         // Core
234         pCoreManager->SetOrientation(coreOrientation);
235         pImplManager->SetOrientationStatus(status);
236         pImpl->GetCore().ChangeLayout(coreOrientation);
237
238         // Ownee
239         int owneeCount = pImpl->GetCore().GetOwneeCount();
240         for (int i = 0; i < owneeCount; i++)
241         {
242                 _Window* pOwnee = pImpl->GetCore().GetOwnee(i);
243
244                 if (pOwnee)
245                 {
246                         pOwnee->ChangeLayout(coreOrientation);
247                 }
248         }
249
250         // Public
251         if (__firePublicEvent && __statusChanged)
252         {
253                 IEventArg* pArg = _PublicOrientationEvent::CreateOrientationEventArgN(*__pPublicEvent->GetSource(), status);
254                 __pPublicEvent->Fire(*pArg);
255
256                 __updateStatus = true;
257         }
258 }
259
260 }} // Tizen::Ui