Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrlOverlayRegion.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 /**
19  * @file                FUiCtrlOverlayRegion.cpp
20  * @brief               This is the implementation file for OverlayRegion class.
21  */
22
23 #include <FUiCtrlOverlayRegion.h>
24
25 #include <FSys_SystemInfoImpl.h>
26
27 #include "FUi_Control.h"
28 #include "FUi_ResourceManager.h"
29 #include "FUi_CoordinateSystemUtils.h"
30 #include "FUiCtrl_OverlayRegionImpl.h"
31 #include "FUiCtrl_OverlayAgent.h"
32
33 #include "FUi_EcoreEvasMgr.h"
34 #include "FUi_EcoreEvas.h"
35
36 using namespace Tizen::Ui;
37 using namespace Tizen::Ui::Controls;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Graphics;
40
41 namespace Tizen { namespace Ui { namespace Controls
42 {
43
44 OverlayRegion::OverlayRegion(void)
45         : __pOverlayRegionImpl(null)
46 {
47
48 }
49
50 OverlayRegion::~OverlayRegion(void)
51 {
52         delete __pOverlayRegionImpl;
53         __pOverlayRegionImpl = null;
54 }
55
56 Tizen::Graphics::Rectangle
57 OverlayRegion::GetBounds(void) const
58 {
59         if (__pOverlayRegionImpl == null)
60         {
61                 SysAssertf(__pOverlayRegionImpl != null, "Not yet constructed. Construct() should be called before use.");
62                 return Tizen::Graphics::Rectangle();
63         }
64
65         return __pOverlayRegionImpl->GetBounds();
66 }
67
68 void
69 OverlayRegion::GetBounds(int& x, int& y, int& width, int& height) const
70 {
71         SysAssertf(__pOverlayRegionImpl != null, "Not yet constructed. Construct() should be called before use.");
72
73         __pOverlayRegionImpl->GetBounds(x, y, width, height);
74
75         return;
76 }
77
78 result
79 OverlayRegion::SetInputBuffer(const Tizen::Base::ByteBuffer& srcBuffer, const Tizen::Graphics::Dimension& sourceDim, Tizen::Ui::Controls::OverlayRegionBufferPixelFormat srcFormat)
80 {
81         SysAssertf(__pOverlayRegionImpl != null, "Not yet constructed. Construct() should be called before use.");
82
83         result r  = __pOverlayRegionImpl->SetInputBuffer(srcBuffer, sourceDim, srcFormat, true);
84         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
85
86         return E_SUCCESS;
87 }
88
89 result
90 OverlayRegion::GetBackgroundBufferInfo(Tizen::Graphics::BufferInfo& info) const
91 {
92         SysAssertf(__pOverlayRegionImpl != null, "Not yet constructed. Construct() should be called before use.");
93
94         result r  = __pOverlayRegionImpl->GetBackgroundBufferInfo(info);
95         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
96
97         return E_SUCCESS;
98 }
99
100 result
101 OverlayRegion::Show(void)
102 {
103         SysAssertf(__pOverlayRegionImpl != null, "Not yet constructed. Construct() should be called before use.");
104
105         result r  = __pOverlayRegionImpl->Show();
106         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
107
108         return E_SUCCESS;
109 }
110
111 bool
112 OverlayRegion::EvaluateBounds(OverlayRegionEvaluationOption option, Tizen::Graphics::Rectangle& rect, bool& modified)
113 {
114         modified = false;
115
116         if (rect.width <= 0 || rect.height <= 0)
117         {
118                 SysLogException(NID_UI_CTRL, E_INVALID_ARG, "[E_INVALID_ARG] input rect size is invalid.");
119                 return false;
120         }
121
122         Tizen::Graphics::Rectangle physicalRect(_CoordinateSystemUtils::Transform(rect));
123         SysLog(NID_UI_CTRL, "org user rect width : %d Height : %d , org physicalRect[%d, %d]!", rect.width, rect.height, physicalRect.width, physicalRect.height);
124
125         int deviceLcdWidth = 0;
126         int deviceLcdHeight = 0;
127
128         SysTryReturn(NID_UI_CTRL, Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/screen.width", deviceLcdWidth) == E_SUCCESS, false, E_SYSTEM, "[E_SYSTEM] failed to get the value of ScreenWidth from SystemInfo.");
129         SysTryReturn(NID_UI_CTRL, Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/screen.height", deviceLcdHeight) == E_SUCCESS, false, E_SYSTEM, "[E_SYSTEM] failed to get the value of ScreenHeight from SystemInfo.");
130
131         const int widthUnit = GetWidthUnit();
132         const int heightUnit = GetHeightUnit();
133
134         if (widthUnit == -1 || heightUnit == -1)
135         {
136                 SetLastResult(E_SYSTEM);
137                 return false;
138         }
139
140         _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
141         int rotation = ecore_evas_rotation_get(pEcoreEvas->GetEcoreEvas());
142
143         if (rotation == 90 || rotation == 270)
144         {
145                 int temp = deviceLcdWidth;
146                 deviceLcdWidth = deviceLcdHeight;
147                 deviceLcdHeight = temp;
148
149                 SysLog(NID_UI_CTRL, "rotate[%d] : deviceLcdWidth[%d], deviceLcdHeight[%d] is swapped", rotation, deviceLcdWidth, deviceLcdHeight);
150         }
151
152         int maxW = deviceLcdWidth - (deviceLcdWidth % widthUnit);
153         int maxH = deviceLcdHeight - (deviceLcdHeight % heightUnit);
154
155         int logicalMaxW = _CoordinateSystemUtils::InverseHorizontalTransform(maxW);
156         int logicalMaxH = _CoordinateSystemUtils::InverseVerticalTransform(maxH);
157
158         SysLog(NID_UI_CTRL, "logicalMax[%d, %d], physicalMax[%d, %d]!", logicalMaxW, logicalMaxH, maxW, maxH);
159
160         int dstMinWidth = _OverlayAgent::GetDstRectMinWidth();
161         int dstMinHeight = _OverlayAgent::GetDstRectMinHeight();
162
163         SysLog(NID_UI_CTRL, "dstMinDimention[%d, %d]", dstMinWidth, dstMinHeight);
164
165         int widthReminder = physicalRect.width % widthUnit;
166         int heightReminder = physicalRect.height % heightUnit;
167
168         switch (option)
169         {
170         case OVERLAY_REGION_EVALUATION_OPTION_GREATER_THAN:
171                 {
172                         if (physicalRect.width > maxW ||  physicalRect.height > maxH)
173                         {
174                                 modified = false;
175                                 SysLogException(NID_UI_CTRL, E_UNSUPPORTED_OPTION, "[E_UNSUPPORTED_OPTION] the input rect size is already over physical maximun size.");
176                                 return false;
177                         }
178
179                         if (rect.width > logicalMaxW || rect.height > logicalMaxH)
180                         {
181                                 modified = false;
182                                 SysLogException(NID_UI_CTRL, E_UNSUPPORTED_OPTION, "[E_UNSUPPORTED_OPTION] the input rect size is already over logical maximun size.");
183                                 return false;
184                         }
185
186                         if (widthReminder != 0)
187                         {
188                                 widthReminder -= widthUnit;
189                         }
190
191                         if (heightReminder != 0)
192                         {
193                                 heightReminder -= heightUnit;
194                         }
195
196                         physicalRect.width -= widthReminder;
197                         physicalRect.height -= heightReminder;
198
199                         if (physicalRect.width < dstMinWidth)
200                         {
201                                 physicalRect.width = dstMinWidth;
202                         }
203
204                         if (physicalRect.height < dstMinHeight)
205                         {
206                                 physicalRect.height = dstMinHeight;
207                         }
208                 }
209                 break;
210         case OVERLAY_REGION_EVALUATION_OPTION_LESS_THAN:
211                 {
212                         if (physicalRect.width < dstMinWidth || physicalRect.height < dstMinHeight)
213                         {
214                                 modified = false;
215                                 SysLogException(NID_UI_CTRL, E_UNSUPPORTED_OPTION, "[E_UNSUPPORTED_OPTION] the input rect size doesn't meet the minimun size.");
216                                 return false;
217                         }
218
219                         if (physicalRect.width > maxW)
220                         {
221                                 physicalRect.width = maxW + widthReminder;
222                                 SysLog(NID_UI_CTRL, "adjusted width [%d] [%d + %d = %d]!", physicalRect.width, maxW, widthReminder, maxW + widthReminder);
223                         }
224
225                         if (physicalRect.height > maxH)
226                         {
227                                 physicalRect.height = maxH + heightReminder;
228                                 SysLog(NID_UI_CTRL, "adjusted height [%d]  [%d + %d = %d]!", physicalRect.height, maxH, heightReminder, maxH+ heightReminder);
229                         }
230
231                         physicalRect.width -= widthReminder;
232                         physicalRect.height -= heightReminder;
233                 }
234                 break;
235         default:
236                 modified = false;
237                 SysLogException(NID_UI_CTRL, E_UNSUPPORTED_OPTION, "[E_UNSUPPORTED_OPTION] option is invalid.");
238                 return false;
239         }
240
241         Rectangle orgRrect(rect);
242
243         rect = _CoordinateSystemUtils::InverseTransform(physicalRect);
244
245         if (rect != orgRrect)
246         {
247                 SysLog(NID_UI_CTRL, "org logical rect [%d, %d, %d, %d] changed to rect [%d, %d, %d, %d]",
248                                 orgRrect.x, orgRrect.y, orgRrect.width, orgRrect.height, rect.x, rect.y, rect.width, rect.height);
249                 modified = true;
250         }
251
252         SysLog(NID_UI_CTRL, "adjusted PhysicalRect [W %d, H %d]!", physicalRect.width, physicalRect.height);
253
254         SetLastResult(E_SUCCESS);
255         return true;
256 }
257
258 int
259 OverlayRegion::GetWidthUnit(void)
260 {
261         int widthUnit = _OverlayRegionImpl::GetWidthUnit();
262         SysTryReturn(NID_UI_CTRL, widthUnit != -1, -1, E_SYSTEM, "[E_SYSTEM] can't get the width Unit");
263
264         return widthUnit;
265 }
266
267 int
268 OverlayRegion::GetHeightUnit(void)
269 {
270         int heightUnit = _OverlayRegionImpl::GetHeightUnit();
271         SysTryReturn(NID_UI_CTRL, heightUnit != -1, -1, E_SYSTEM, "[E_SYSTEM] can't get the height Unit");
272
273         return heightUnit;
274 }
275
276 int
277 OverlayRegion::GetMaxCount(void)
278 {
279         int maxCount = _OverlayRegionImpl::GetMaxCount();
280         SysTryReturn(NID_UI_CTRL, maxCount != -1, -1, E_SYSTEM, "[E_SYSTEM] can't get the maximum count value");
281
282         return maxCount;
283 }
284
285 IListT< Tizen::Ui::Controls::OverlayRegionBufferPixelFormat >*
286 OverlayRegion::GetSupportedBufferPixelFormatListN(void)
287 {
288         if (!_OverlayRegionImpl::overlayRegionPixelFomatList[0])
289         {
290                 result r = _OverlayRegionImpl::SetPixelFormatList();
291                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
292         }
293
294         Tizen::Base::Collection::ArrayListT<OverlayRegionBufferPixelFormat>* pFormatList = null;
295
296         pFormatList = new (std::nothrow) ArrayListT<OverlayRegionBufferPixelFormat>();
297         SysTryReturn(NID_UI_CTRL, pFormatList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] failed to create a list instance");
298         if (pFormatList->Construct() != E_SUCCESS)
299         {
300                 SysLogException(NID_UI_CTRL, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
301                 delete pFormatList;
302                 return null;
303         }
304
305         for (int i = 1; i < OVERLAY_REGION_BUFFER_PIXEL_FORMAT_MAX; i++)
306         {
307                 if (_OverlayRegionImpl::overlayRegionPixelFomatList[i])
308                 {
309                         pFormatList->Add(static_cast<OverlayRegionBufferPixelFormat>(i));
310                 }
311         }
312
313         SysLog(NID_UI_CTRL, "format list count : %d", pFormatList->GetCount());
314
315         SetLastResult(E_SUCCESS);
316         return pFormatList;
317 }
318
319 } } } // Tizen::Ui::Controls