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