modify title text
[apps/native/sample/BasicApp.git] / project / src / MainForm.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <FApp.h>
18 #include <FBase.h>
19
20 #include "AppResourceId.h"
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(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         SetFormBackEventListener(this);
107
108         // Create the Button panel
109         ButtonPanel* pButtonPanel = new (std::nothrow) ButtonPanel();
110         pButtonPanel->Initialize(rect);
111         AddControl(pButtonPanel);
112         __pPanel[0] = pButtonPanel;
113
114         // Orientation panel was created with UI Builder,
115         // so only its button events must be defined here
116         __pPanel[1] = static_cast<Panel *>(GetControl(IDC_ORIENTATIONPANEL));
117         if (__pPanel[1] != null)
118         {
119                 __pButtonOrientation = static_cast<Button *>(GetControl(IDC_BUTTON_ORIENTATION, true));
120
121                 if (__pButtonOrientation != null)
122                 {
123                         __pButtonOrientation->SetActionId(ID_ORIENTATION);
124                         __pButtonOrientation->AddActionEventListener(*this);
125                 }
126         }
127
128         // Set the current panel as selected in the header and display it on the form
129         if (pHeader)
130         {
131                 pHeader->SetItemSelected(__panelId);
132
133                 if(__panelId == 2)
134                 {
135                         if (__pPanel[2] == null)
136                         {
137                                 CreateImagePanel();
138                         }
139                         SetOrientation(ORIENTATION_AUTOMATIC);
140                 }
141
142                 if (__pPanel[0] != null)
143                 {
144                         __pPanel[0]->SetShowState(false);
145                 }
146                 if (__pPanel[1] != null)
147                 {
148                         __pPanel[1]->SetShowState(false);
149                 }
150
151                 __pPanel[__panelId]->SetShowState(true);
152         }
153
154         Invalidate(true);
155         return r;
156 }
157
158 result
159 MainForm::OnTerminating(void)
160 {
161         AppRegistry *appRegistry = Application::GetInstance()->GetAppRegistry();
162         String panelIDkey(L"AppLastPanelId");
163         String panelNamekey(L"AppLastPanelName");
164
165         Header* pHeader = GetHeader();
166         result r = appRegistry->Set(panelIDkey,pHeader->GetSelectedItemIndex());
167         if (IsFailed(r))
168         {
169                 //error condition
170         }
171
172         String panel;
173         panel.Format(50, L"Panel%d", pHeader->GetSelectedItemIndex());
174         r = appRegistry->Set(panelNamekey, panel);
175         if (IsFailed(r))
176         {
177                 //error condition
178         }
179
180         r = appRegistry->Save();
181         if (IsFailed(r))
182         {
183                 //failed to save data to registry.
184         }
185
186         return r;
187 }
188
189 void
190 MainForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
191 {
192         switch(actionId)
193         {
194         case ID_HEADER_ITEM1:
195                 {
196                         if (__pPanel[0] != null)
197                         {
198                                 __pPanel[0]->SetShowState(true);
199                         }
200                         if (__pPanel[1] != null)
201                         {
202                                 __pPanel[1]->SetShowState(false);
203                         }
204                         if (__pPanel[2] != null)
205                         {
206                                 __pPanel[2]->SetShowState(false);
207                         }
208                         SetOrientation(ORIENTATION_PORTRAIT);
209                 }
210                 break;
211
212         case ID_HEADER_ITEM2:
213                 {
214                         if (__pPanel[0] != null)
215                         {
216                                 __pPanel[0]->SetShowState(false);
217                         }
218                         if (__pPanel[1] != null)
219                         {
220                                 __pPanel[1]->SetShowState(true);
221                         }
222                         if (__pPanel[2] != null)
223                         {
224                                 __pPanel[2]->SetShowState(false);
225                         }
226                         SetOrientation(__status);
227                 }
228                 break;
229
230         case ID_HEADER_ITEM3:
231                 {
232                         if (__pPanel[2] == null)
233                         {
234                                 CreateImagePanel();
235                         }
236                         if (__pPanel[0] != null)
237                         {
238                                 __pPanel[0]->SetShowState(false);
239                         }
240                         if (__pPanel[1] != null)
241                         {
242                                 __pPanel[1]->SetShowState(false);
243                         }
244                         if (__pPanel[2] != null)
245                         {
246                                 __pPanel[2]->SetShowState(true);
247                         }
248                         SetOrientation(ORIENTATION_AUTOMATIC);
249                 }
250                 break;
251
252         case ID_ORIENTATION:
253                 {
254                         if (__pPanel[1]->GetShowState())
255                         {
256                                 OrientationStatus status = GetOrientationStatus();
257                                 if (status == ORIENTATION_STATUS_PORTRAIT)
258                                 {
259                                         __status = ORIENTATION_LANDSCAPE;
260                                 }
261                                 else if (status == ORIENTATION_STATUS_LANDSCAPE)
262                                 {
263                                         __status = ORIENTATION_PORTRAIT;
264                                 }
265                                 SetOrientation(__status);
266                         }
267                 }
268                 break;
269
270         default:
271                 break;
272         }
273
274         Invalidate(true);
275 }
276
277 void
278 MainForm::OnFormBackRequested(Form& source)
279 {
280         UiApp* pApp = UiApp::GetInstance();
281         AppAssert(pApp);
282         pApp->Terminate();
283 }