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