Tizen 2.1 base
[sdk/ide/native-sample.git] / samples / native / partner / cpp / Sample / Tizen C++ / UiVisualElement / UiVisualElement / project / src / EventListenerForm.cpp
1 //
2 // Tizen C++ SDK
3 // Copyright (c) 2012 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://www.tizenopensource.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 #include <FUiAnimVisualElementPropertyAnimation.h>
19
20 #include "FormManager.h"
21 #include "EventListenerForm.h"
22
23 using namespace Osp::App;
24 using namespace Osp::Graphics;
25 using namespace Osp::Ui;
26 using namespace Osp::Ui::Animations;
27 using namespace Osp::Ui::Controls;
28
29 EventListenerForm::EventListenerForm(void)
30         : __pRed(null)
31         , __pYellow(null)
32         , __pButton(null)
33         , __bAttached(false)
34 {
35 }
36
37 EventListenerForm::~EventListenerForm(void)
38 {
39         SetVisualElement(null);
40         if (__pVisualElement)
41         {
42                 __pVisualElement->Destroy();
43                 __pVisualElement = null;
44         }
45 }
46
47 bool
48 EventListenerForm::Initialize()
49 {
50         Construct(FORM_STYLE_NORMAL | FORM_STYLE_HEADER | FORM_STYLE_FOOTER | FORM_STYLE_INDICATOR);
51         InitializeFooter();
52         SetFormBackEventListener(this);
53
54         return true;
55 }
56
57 result
58 EventListenerForm::OnInitializing(void)
59 {
60         result r = E_SUCCESS;
61
62         Header* pHeader = Form::GetHeader();
63         pHeader->SetStyle(HEADER_STYLE_TITLE);
64         pHeader->SetTitleText(L"Event Listener");
65
66         __pVisualElement = new (std::nothrow) VisualElement();
67         __pVisualElement->Construct();
68         __pVisualElement->SetName(L"VisualElement");
69         __pVisualElement->SetBounds(FloatRectangle(0.0f, 0.0f, 0.0f, 0.0f));
70         __pVisualElement->SetClipChildrenEnabled(false);
71         __pVisualElement->SetShowState(true);
72         SetVisualElement(__pVisualElement);
73         Rectangle clientRect = GetClientAreaBounds();
74
75         __pButton = new (std::nothrow) Button();
76         __pButton->Construct(Rectangle(50, clientRect.height - 125, clientRect.width - 100, 75));
77         __pButton->SetText(L"Detached with opacity 1.0");
78         AddControl(*__pButton);
79
80         float redSize = 250.f;
81
82         __pRed = new (std::nothrow) VisualElement();
83         __pRed->Construct();
84         __pRed->SetName(L"Red");
85         __pRed->SetBounds(FloatRectangle((clientRect.width - redSize) / 2.0f, clientRect.y + (clientRect.height - redSize) / 2.0f, redSize, redSize));
86         __pRed->SetShowState(true);
87         __pRed->SetVisualElementEventListener(this);
88         __pVisualElement->AttachChild(*__pRed);
89
90         Canvas* pCanvas = __pRed->GetCanvasN();
91         pCanvas->FillRectangle(Color::GetColor(COLOR_ID_RED), Rectangle(0, 0, 250, 250));
92         delete pCanvas;
93
94         float yellowSize = 100.f;
95
96         __pYellow = new VisualElement();
97         __pYellow->Construct();
98         __pYellow->SetName(L"Yellow");
99         __pYellow->SetBounds(FloatRectangle((clientRect.width - yellowSize) / 2.0f, clientRect.y + 50.0f, yellowSize, yellowSize));
100         __pYellow->SetShowState(true);
101         __pYellow->SetImplicitAnimationEnabled(false);
102         __pVisualElement->AttachChild(*__pYellow);
103
104         pCanvas = __pYellow->GetCanvasN();
105         pCanvas->FillRectangle(Color::GetColor(COLOR_ID_YELLOW), Rectangle(0, 0, 250, 250));
106         delete pCanvas;
107
108         __detachRect = __pYellow->GetBounds();
109         __attachRect = FloatRectangle(50.0f, 50.0f, __detachRect.width, __detachRect.height);
110
111         return r;
112 }
113
114 void
115 EventListenerForm::OnActionPerformed(const Control& source, int actionId)
116 {
117         switch(actionId)
118         {
119         case ID_FOOTER_BUTTON_ATTACH_DETACH:
120                 {
121                         if (__bAttached == true)
122                         {
123                                 __pRed->DetachChild(*__pYellow);
124                                 __pVisualElement->AttachChild(*__pYellow);
125                         }
126                         else
127                         {
128                                 __pVisualElement->DetachChild(*__pYellow);
129                                 __pRed->AttachChild(*__pYellow);
130                         }
131                 }
132                 break;
133
134         default:
135                 break;
136         }
137 }
138
139 void
140 EventListenerForm::OnFormBackRequested(Osp::Ui::Controls::Form& source)
141 {
142         Frame* pFrame = UiApp::GetInstance()->GetFrameAt(0);
143         if (pFrame != null)
144         {
145                 FormManager* pFormManager = static_cast<FormManager*>(pFrame->GetControl(L"FormManager"));
146
147                 if (pFormManager != null)
148                 {
149                                 pFormManager->SendUserEvent(FormManager::REQUEST_ID_MAIN_FORM, null);
150                 }
151         }
152 }
153
154 void
155 EventListenerForm::InitializeFooter(void)
156 {
157         Footer* pFooter = Form::GetFooter();
158
159         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
160         pFooter->SetBackButton();
161         pFooter->AddActionEventListener(*this);
162
163         FooterItem  footerItem1;
164         footerItem1.Construct(ID_FOOTER_BUTTON_ATTACH_DETACH);
165         footerItem1.SetText(L"Attach/Detach");
166
167         pFooter->AddItem(footerItem1);
168 }
169
170 void
171 EventListenerForm::OnChildAttached(VisualElement& source, VisualElement& child)
172 {
173         child.SetBounds(__attachRect);
174         child.SetOpacity(0.5f);
175
176         __pButton->SetText(L"Attached with opacity 0.5");
177         __pButton->Draw();
178
179         __bAttached = true;
180 }
181
182 void
183 EventListenerForm::OnChildDetached(VisualElement& source, VisualElement& child)
184 {
185         child.SetBounds(__detachRect);
186         child.SetOpacity(1.0f);
187
188         __pButton->SetText(L"Detached with opacity 1.0");
189         __pButton->Draw();
190
191         __bAttached = false;
192 }