Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_DimmingLayer.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        FUiCtrl_DimmingLayer.cpp
20  * @brief       This is the implementation file for _DimmingLayer class.
21  *
22  * This file contains the implementation of _DimmingLayer class.
23  */
24
25
26 #include <FGrpFloatRectangle.h>
27
28 #include "FUi_Control.h"
29 #include "FUi_ControlManager.h"
30 #include "FUi_Window.h"
31 #include "FUiCtrl_DimmingLayer.h"
32 #include "FUiAnim_ControlVisualElement.h"
33 #include "FUiAnim_RootVisualElement.h"
34
35 using namespace Tizen::Ui::Controls;
36 using namespace Tizen::Graphics;
37 using namespace Tizen::Ui::Animations;
38 using namespace Tizen::Ui;
39
40 namespace Tizen
41 {
42 namespace Ui
43 {
44 namespace Controls
45 {
46
47 _DimmingLayer::_DimmingLayer(void)
48         : __enabled(false)
49         , __pControl(null)
50         , __pDimmingElement(null)
51         , __oldControlRenderOperation(VisualElement::RENDER_OPERATION_BLEND)
52 {
53
54 }
55
56 _DimmingLayer::~_DimmingLayer(void)
57 {
58         if (__pDimmingElement)
59         {
60                 __pDimmingElement->Destroy();
61         }
62         __pDimmingElement = null;
63
64         if (__pControl)
65         {
66                 VisualElement* pControlVisualElement = __pControl->GetVisualElement();
67                 if (pControlVisualElement)
68                 {
69                         pControlVisualElement->SetRenderOperation(__oldControlRenderOperation);
70                 }
71         }
72 }
73
74 result
75 _DimmingLayer::Construct(_Control& control)
76 {
77         result r = E_SUCCESS;
78         VisualElement* pControlVisualElement = null;
79
80         __pControl = &control;
81
82         if (!__pDimmingElement)
83         {
84                 __pDimmingElement = new (std::nothrow) _ControlVisualElement;
85                 SysTryReturnResult(NID_UI, __pDimmingElement, E_OUT_OF_MEMORY, "Memory allocation failed.");
86         }
87
88         r = __pDimmingElement->ConstructControlVisualElement();
89         SysTryCatch(NID_UI, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
90
91         __pDimmingElement->SetImplicitAnimationEnabled(true);
92         __pDimmingElement->SetName("DimmingLayer");
93
94         r = __pDimmingElement->SetSurfaceOpaque(false);
95         SysTryCatch(NID_UI, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
96
97         __pDimmingElement->SetOpacity(0.65f);
98
99         r = __pDimmingElement->SetBackgroundColor(_Colorf());
100         SysTryCatch(NID_UI, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
101
102         pControlVisualElement = __pControl->GetVisualElement();
103         if (pControlVisualElement)
104         {
105                 __oldControlRenderOperation = pControlVisualElement->GetRenderOperation();
106                 pControlVisualElement->SetRenderOperation(VisualElement::RENDER_OPERATION_BLEND);
107         }
108
109         r = Rearrange();
110         SysTryCatch(NID_UI, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
111
112         return r;
113
114 CATCH:
115         __pDimmingElement->Destroy();
116         return E_SYSTEM;
117 }
118
119 result
120 _DimmingLayer::SetOpacity(float opacity)
121 {
122         SysTryReturnResult(NID_UI, __pDimmingElement, E_SYSTEM, "A system error has been occurred.");
123
124         result r = E_SUCCESS;
125
126         __pDimmingElement->SetOpacity(opacity);
127
128         if (opacity >= 1.0f)
129         {
130                 __pDimmingElement->SetSurfaceOpaque(true);
131         }
132         else
133         {
134                 __pDimmingElement->SetSurfaceOpaque(false);
135         }
136
137         SysTryReturnResult(NID_UI, !IsFailed(r), E_SYSTEM, "A system error has been occurred.");
138
139         return r;
140 }
141
142 float
143 _DimmingLayer::GetOpacity(void) const
144 {
145         SysTryReturnResult(NID_UI, __pDimmingElement, E_SYSTEM, "A system error has been occurred.");
146         return __pDimmingElement->GetOpacity();
147 }
148
149 result
150 _DimmingLayer::SetDimmingEnabled(bool enabled)
151 {
152         SysTryReturnResult(NID_UI, __pDimmingElement, E_SYSTEM, "A system error has been occurred.");
153
154         result r = E_SUCCESS;
155
156         _VisualElement* pControlVisualElement = __pControl->GetVisualElement();
157         SysTryReturnResult(NID_UI, pControlVisualElement, E_SYSTEM, "A system error has been occurred.");
158
159         __pDimmingElement->SetShowState(enabled);
160         if (enabled)
161         {
162 #if !defined(MULTI_WINDOW)
163                 VisualElement* pParent = pControlVisualElement->GetParent();
164 #else
165                 _Window* pWindow = __pControl->GetRootWindow();
166                 _RootVisualElement* pParent = pWindow->GetRootVisualElement();
167 #endif
168                 if (pParent)
169                 {
170                         pParent->InsertChild(*__pDimmingElement, pControlVisualElement, false);
171                 }
172         }
173         else
174         {
175 #if !defined(MULTI_WINDOW)
176                 VisualElement* pParent = pControlVisualElement->GetParent();
177 #else
178                 _Window* pWindow = __pControl->GetRootWindow();
179                 _RootVisualElement* pParent = pWindow->GetRootVisualElement();
180 #endif
181                 if (pParent)
182                 {
183                         pParent->DetachChild(*__pDimmingElement);
184                 }
185         }
186
187         __enabled = enabled;
188
189         return r;
190 }
191
192 bool
193 _DimmingLayer::GetDimmingEnabled(void) const
194 {
195         return __enabled;
196 }
197
198 result
199 _DimmingLayer::Rearrange(void)
200 {
201         SysTryReturnResult(NID_UI, __pDimmingElement, E_SYSTEM, "A system error has been occurred.");
202         SysTryReturnResult(NID_UI, __pControl, E_SYSTEM, "A system error has been occurred.");
203
204         result r = E_SUCCESS;
205
206         Dimension size = _ControlManager::GetInstance()->GetScreenSize();
207         if (__pControl->GetOrientation() == _CONTROL_ORIENTATION_PORTRAIT)
208         {
209                 __pDimmingElement->SetBounds(Tizen::Graphics::FloatRectangle(0.0f, 0.0f, (float)size.width, (float)size.height));
210         }
211         else
212         {
213                 __pDimmingElement->SetBounds(Tizen::Graphics::FloatRectangle(0.0f, 0.0f, (float)size.height, (float)size.width));
214         }
215
216         return r;
217 }
218
219 }   // Tizen::Ui::Controls
220 }   // Tizen::Ui
221 }   // Osp