sync with master
[apps/native/sample/BasicApp.git] / project / src / MainForm.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://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 #include <FApp.h>
19 #include <FBase.h>
20
21 #include "ButtonPanel.h"
22 #include "ImagePanel.h"
23 #include "MainForm.h"
24
25 using namespace Tizen::App;
26 using namespace Tizen::Base;
27 using namespace Tizen::Ui;
28 using namespace Tizen::Ui::Controls;
29 using namespace Tizen::Graphics;
30
31 MainForm::MainForm(void)
32         : __pButtonOrientation(null)
33         , __status(ORIENTATION_PORTRAIT)
34         , __panelId(0)
35 {
36         __pPanel[0] = null;
37         __pPanel[1] = null;
38         __pPanel[2] = null;
39 }
40
41 MainForm::~MainForm(void)
42 {
43 }
44
45 result
46 MainForm::Initialize(int panelId)
47 {
48         result r = Form::Construct(L"IDF_MAINFORM");
49
50         if (panelId >= 0 && panelId <= 2)
51         {
52                 __panelId = panelId;
53         }
54         else
55         {
56                 __panelId = 0;
57         }
58
59         return r;
60 }
61
62 result
63 MainForm::CreateImagePanel(void)
64 {
65         ImagePanel* pImagePanel = new (std::nothrow) ImagePanel();
66         pImagePanel->Initialize(Rectangle(0, 0, GetClientAreaBounds().width, GetClientAreaBounds().height));
67         AddOrientationEventListener(*pImagePanel);
68         AddControl(*pImagePanel);
69         __pPanel[2] = pImagePanel;
70 }
71
72 result
73 MainForm::OnInitializing(void)
74 {
75         result r = E_SUCCESS;
76
77         Rectangle clientRect = GetClientAreaBounds();
78         Rectangle rect(0, 0, clientRect.width, clientRect.height);
79
80         // Create header
81         Header* pHeader = GetHeader();
82         if (pHeader != null)
83         {
84                 pHeader->SetStyle(HEADER_STYLE_TAB);
85
86                 HeaderItem headerItem1;
87                 headerItem1.Construct(ID_HEADER_ITEM1);
88                 headerItem1.SetText(L"BUTTON");
89                 pHeader->AddItem(headerItem1);
90
91                 HeaderItem headerItem2;
92                 headerItem2.Construct(ID_HEADER_ITEM2);
93                 headerItem2.SetText(L"ORIENTATION");
94                 pHeader->AddItem(headerItem2);
95
96                 HeaderItem headerItem3;
97                 headerItem3.Construct(ID_HEADER_ITEM3);
98                 headerItem3.SetText(L"IMAGE");
99                 pHeader->AddItem(headerItem3);
100
101                 pHeader->AddActionEventListener(*this);
102         }
103
104         // Create footer
105         Footer * pFooter = GetFooter();
106         if (pFooter != null)
107         {
108                 pFooter->SetStyle(FOOTER_STYLE_BUTTON_ICON);
109                 pFooter->SetBackButton();
110                 SetFormBackEventListener(this);
111         }
112
113         // Create the Button panel
114         ButtonPanel* pButtonPanel = new (std::nothrow) ButtonPanel();
115         pButtonPanel->Initialize(rect);
116         AddControl(*pButtonPanel);
117         __pPanel[0] = pButtonPanel;
118
119         // Orientation panel was created with UI Builder,
120         // so only its button events must be defined here
121         __pPanel[1] = static_cast<Panel *>(GetControl(L"IDC_ORIENTATIONPANEL"));
122         if (__pPanel[1] != null)
123         {
124                 __pButtonOrientation = static_cast<Button *>(GetControl(L"IDC_BUTTON_ORIENTATION", true));
125
126                 if (__pButtonOrientation != null)
127                 {
128                         __pButtonOrientation->SetActionId(ID_ORIENTATION);
129                         __pButtonOrientation->AddActionEventListener(*this);
130                 }
131         }
132
133         // Set the current panel as selected in the header and display it on the form
134         if (pHeader)
135         {
136                 pHeader->SetItemSelected(__panelId);
137
138                 if(__panelId == 2)
139                 {
140                         if (__pPanel[2] == null)
141                         {
142                                 CreateImagePanel();
143                         }
144                         SetOrientation(ORIENTATION_AUTOMATIC);
145                 }
146
147                 if (__pPanel[0] != null)
148                 {
149                         __pPanel[0]->SetShowState(false);
150                 }
151                 if (__pPanel[1] != null)
152                 {
153                         __pPanel[1]->SetShowState(false);
154                 }
155
156                 __pPanel[__panelId]->SetShowState(true);
157         }
158
159         Invalidate(true);
160         return r;
161 }
162
163 result
164 MainForm::OnTerminating(void)
165 {
166         AppRegistry *appRegistry = Application::GetInstance()->GetAppRegistry();
167         String panelIDkey(L"AppLastPanelId");
168         String panelNamekey(L"AppLastPanelName");
169
170         Header* pHeader = GetHeader();
171         result r = appRegistry->Set(panelIDkey,pHeader->GetSelectedItemIndex());
172         if (IsFailed(r))
173         {
174                 //error condition
175         }
176
177         String panel;
178         panel.Format(50, L"Panel%d", pHeader->GetSelectedItemIndex());
179         r = appRegistry->Set(panelNamekey, panel);
180         if (IsFailed(r))
181         {
182                 //error condition
183         }
184
185         r = appRegistry->Save();
186         if (IsFailed(r))
187         {
188                 //failed to save data to registry.
189         }
190
191         return r;
192 }
193
194 void
195 MainForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
196 {
197         switch(actionId)
198         {
199         case ID_HEADER_ITEM1:
200                 {
201                         if (__pPanel[0] != null)
202                         {
203                                 __pPanel[0]->SetShowState(true);
204                         }
205                         if (__pPanel[1] != null)
206                         {
207                                 __pPanel[1]->SetShowState(false);
208                         }
209                         if (__pPanel[2] != null)
210                         {
211                                 __pPanel[2]->SetShowState(false);
212                         }
213                         SetOrientation(ORIENTATION_PORTRAIT);
214                 }
215                 break;
216
217         case ID_HEADER_ITEM2:
218                 {
219                         if (__pPanel[0] != null)
220                         {
221                                 __pPanel[0]->SetShowState(false);
222                         }
223                         if (__pPanel[1] != null)
224                         {
225                                 __pPanel[1]->SetShowState(true);
226                         }
227                         if (__pPanel[2] != null)
228                         {
229                                 __pPanel[2]->SetShowState(false);
230                         }
231                         SetOrientation(__status);
232                 }
233                 break;
234
235         case ID_HEADER_ITEM3:
236                 {
237                         if (__pPanel[2] == null)
238                         {
239                                 CreateImagePanel();
240                         }
241                         if (__pPanel[0] != null)
242                         {
243                                 __pPanel[0]->SetShowState(false);
244                         }
245                         if (__pPanel[1] != null)
246                         {
247                                 __pPanel[1]->SetShowState(false);
248                         }
249                         if (__pPanel[2] != null)
250                         {
251                                 __pPanel[2]->SetShowState(true);
252                         }
253                         SetOrientation(ORIENTATION_AUTOMATIC);
254                 }
255                 break;
256
257         case ID_ORIENTATION:
258                 {
259                         if (__pPanel[1]->GetShowState())
260                         {
261                                 OrientationStatus status = GetOrientationStatus();
262                                 if (status == ORIENTATION_STATUS_PORTRAIT)
263                                 {
264                                         __status = ORIENTATION_LANDSCAPE;
265                                 }
266                                 else if (status == ORIENTATION_STATUS_LANDSCAPE)
267                                 {
268                                         __status = ORIENTATION_PORTRAIT;
269                                 }
270                                 SetOrientation(__status);
271                         }
272                 }
273                 break;
274
275         default:
276                 break;
277         }
278
279         Invalidate(true);
280 }
281
282 void
283 MainForm::OnFormBackRequested(Form& source)
284 {
285         UiApp* pApp = UiApp::GetInstance();
286         AppAssert(pApp);
287         pApp->Terminate();
288 }