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