Fixed N_SE-38563, N_SE-38552
[apps/osp/Settings.git] / src / StBaseForm.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 /**
18  * @file                StBaseForm.cpp
19  * @brief               This is the implementation file for BaseForm class.
20  */
21
22 #include <FSystem.h>
23 #include "StBaseForm.h"
24 #include "StTypes.h"
25
26 using namespace Tizen::App;
27 using namespace Tizen::Base;
28 using namespace Tizen::Graphics;
29 using namespace Tizen::System;
30 using namespace Tizen::Ui;
31 using namespace Tizen::Ui::Controls;
32
33 static const int W_LARGE_SIZE_TEXT_GAP = 110;
34 static const int W_HUGE_SIZE_PORTRAIT_TEXT_GAP = 90;
35 static const int W_HUGE_SIZE_LANDSCAPE_TEXT_GAP = 220;
36 static const int LINE_COUNT_DEFAULT = 1;
37
38 static const int BASE_FORM_DEFAULT_MARGIN = 32;
39
40 BaseForm::BaseForm(void)
41         : __pTableView(null)
42 {
43 }
44
45 BaseForm::~BaseForm(void)
46 {
47 }
48
49 bool
50 BaseForm::Initialize(void)
51 {
52         RelativeLayout relativeLayout;
53         relativeLayout.Construct();
54         Construct(relativeLayout, FORM_STYLE_NORMAL | FORM_STYLE_INDICATOR | FORM_STYLE_HEADER | FORM_STYLE_FOOTER);
55
56         SetFooter();
57         SetOrientationAutoMode();
58         return true;
59 }
60
61 void
62 BaseForm::SetOrientationAutoMode(void)
63 {
64         this->SetOrientation(ORIENTATION_AUTOMATIC_FOUR_DIRECTION);
65         this->AddOrientationEventListener(*this);
66 }
67
68 void
69 BaseForm::CreateHeader(const Tizen::Base::String& textTitle)
70 {
71         Header* pHeader = GetHeader();
72
73         pHeader->SetStyle(HEADER_STYLE_TITLE);
74         pHeader->SetTitleText(textTitle);
75 }
76
77 void
78 BaseForm::CreateTableView(void)
79 {
80         if (__pTableView != null)
81         {
82                 AppLogDebug("__pTableView is not null");
83                 return;
84         }
85         Rectangle bounds = GetClientAreaBounds();
86         bounds.x = X_GROUP_DEFAULT;
87         bounds.y = Y_GROUP_DEFAULT;
88
89         __pTableView = new (std::nothrow) GroupedTableView();
90
91         __pTableView->Construct(bounds, false, TABLE_VIEW_SCROLL_BAR_STYLE_FADE_OUT);
92         __pTableView->SetItemProvider(this);
93
94         AddControl(__pTableView);
95         __pTableView->SetGroupedLookEnabled(true);
96         __pTableView->AddGroupedTableViewItemEventListener(*this);
97
98         RelativeLayout* pRelativeLayout = dynamic_cast<RelativeLayout*>(this->GetLayoutN());
99         if (pRelativeLayout != null)
100         {
101                 pRelativeLayout->SetHorizontalFitPolicy(*__pTableView, FIT_POLICY_PARENT);
102                 pRelativeLayout->SetRelation(*__pTableView, this, RECT_EDGE_RELATION_TOP_TO_TOP);
103                 pRelativeLayout->SetRelation(*__pTableView, this, RECT_EDGE_RELATION_BOTTOM_TO_BOTTOM);
104                 delete pRelativeLayout;
105         }
106 }
107
108 bool
109 BaseForm::SetFooter(void)
110 {
111         Footer* pFooter = GetFooter();
112
113         pFooter->SetStyle(FOOTER_STYLE_SEGMENTED_TEXT);
114         pFooter->SetBackButton();
115         pFooter->AddActionEventListener(*this);
116
117         SetFormBackEventListener(this);
118
119         return true;
120 }
121
122 void
123 BaseForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
124 {
125         AppLogDebug("ENTER");
126 }
127
128 void
129 BaseForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
130 {
131         AppLogDebug("ENTER");
132
133         UiApp* pApp = UiApp::GetInstance();
134         AppAssert(pApp);
135         pApp->Terminate();
136 }
137
138 void
139 BaseForm::ShowMessageBox(const Tizen::Base::String& title, const Tizen::Base::String& text)
140 {
141         MessageBox messageBox;
142         int result = E_SUCCESS;
143
144         messageBox.Construct(title, text, MSGBOX_STYLE_NONE, MESSAGEBOX_DISPLAY_TIME_2_SEC);
145         messageBox.ShowAndWait(result);
146 }
147
148 void
149 BaseForm::ItemTypeOneLine(Tizen::Graphics::Rectangle& main)
150 {
151         Rectangle clientRect = GetClientAreaBounds();
152
153         main.x = X_ITEM_TYPE_1_LABEL;
154         main.y = Y_ITEM_TYPE_1_LABEL;
155         main.width = (clientRect.width - TWO_LINE_ITEM_WIDTH_GAP);
156         main.height = H_ITEM_TYPE_1_LABEL;
157 }
158
159 void
160 BaseForm::ItemTypeTwoLine(Tizen::Graphics::Rectangle& main, Tizen::Graphics::Rectangle& sub, int fontSize)
161 {
162         Rectangle clientRect = GetClientAreaBounds();
163
164         if (fontSize >= FONT_MAIN_TEXT_SIZE_LARGE)
165         {
166                 main.x = X_ITEM_TYPE_2_LABEL_MAIN;
167                 main.y = Y_ITEM_TYPE_2_LABEL_MAIN;
168                 main.width = (clientRect.width - TWO_LINE_ITEM_WIDTH_GAP);
169                 main.height = H_ITEM_TYPE_1_LABEL;
170
171                 sub.x = X_ITEM_TYPE_2_LABEL_SUB;
172                 sub.y = H_ITEM_TYPE_1_LABEL;
173                 sub.width = (clientRect.width - TWO_LINE_ITEM_WIDTH_GAP);
174                 sub.height = H_ITEM_TYPE_2_LABEL_SUB;
175         }
176         else
177         {
178                 main.x = X_ITEM_TYPE_2_LABEL_MAIN;
179                 main.y = Y_ITEM_TYPE_2_LABEL_MAIN;
180                 main.width = (clientRect.width - TWO_LINE_ITEM_WIDTH_GAP);
181                 main.height = H_ITEM_TYPE_2_LABEL_MAIN;
182
183                 sub.x = X_ITEM_TYPE_2_LABEL_SUB;
184                 sub.y = Y_ITEM_TYPE_2_LABEL_SUB;
185                 sub.width = (clientRect.width - TWO_LINE_ITEM_WIDTH_GAP);
186                 sub.height = H_ITEM_TYPE_2_LABEL_SUB;
187         }
188 }
189
190 void
191 BaseForm::ItemTypeIconAndOneLine(Tizen::Graphics::Rectangle& icon, Tizen::Graphics::Rectangle& main)
192 {
193         Rectangle clientRect = GetClientAreaBounds();
194
195         icon.x = X_ITEM_TYPE_3_ICON;
196         icon.y = Y_ITEM_TYPE_3_ICON;
197         icon.width = W_ITEM_TYPE_3_ICON;
198         icon.height = H_ITEM_TYPE_3_ICON;
199
200         main.x = X_ITEM_TYPE_3_LABEL;
201         main.y = Y_ITEM_TYPE_3_LABEL;
202         main.width = (clientRect.width - ICON_AND_ONE_LINE_ITEM_WIDTH_GAP);
203         main.height = H_ITEM_TYPE_3_LABEL;
204 }
205
206 void
207 BaseForm::ItemTypeIconAndTwoLine(Tizen::Graphics::Rectangle& icon, Tizen::Graphics::Rectangle& main, Tizen::Graphics::Rectangle& sub, int fontSize)
208 {
209         Rectangle clientRect = GetClientAreaBounds();
210
211         icon.x = X_ITEM_TYPE_4_ICON;
212         icon.y = Y_ITEM_TYPE_4_ICON;
213         icon.width = W_ITEM_TYPE_4_ICON;
214         icon.height = H_ITEM_TYPE_4_ICON;
215
216         if (fontSize > FONT_MAIN_TEXT_SIZE_LARGE)
217         {
218                 main.x = X_ITEM_TYPE_4_LABEL_MAIN;
219                 main.y = Y_ITEM_TYPE_4_LABEL_MAIN;
220                 main.width = (clientRect.width - X_ITEM_TYPE_4_LABEL_MAIN);
221                 main.height = H_ITEM_TYPE_1_LABEL;
222
223                 sub.x = X_ITEM_TYPE_4_LABEL_SUB;
224                 sub.y = H_ITEM_TYPE_1_LABEL;
225                 sub.width =  (clientRect.width - X_ITEM_TYPE_4_LABEL_MAIN);
226                 sub.height = H_ITEM_TYPE_4_LABEL_SUB;
227         }
228         else
229         {
230                 main.x = X_ITEM_TYPE_4_LABEL_MAIN;
231                 main.y = Y_ITEM_TYPE_4_LABEL_MAIN;
232                 main.width = (clientRect.width - X_ITEM_TYPE_4_LABEL_MAIN);
233                 main.height = H_ITEM_TYPE_4_LABEL_MAIN;
234
235                 sub.x = X_ITEM_TYPE_4_LABEL_SUB;
236                 sub.y = Y_ITEM_TYPE_4_LABEL_SUB;
237                 sub.width = main.width;
238                 sub.height = H_ITEM_TYPE_4_LABEL_SUB;
239         }
240 }
241
242 int
243 BaseForm::GetFontSize(void)
244 {
245         int fontSize = FONT_MAIN_TEXT_SIZE_NORMAL;
246         String fontSizeValue;
247
248         if (SettingInfo::GetValue(SETTING_INFO_KEY_FONT_SIZE, fontSizeValue) == E_SUCCESS)
249         {
250                 if (fontSizeValue.Equals(L"giant", false))
251                 {
252                         fontSize = FONT_MAIN_TEXT_SIZE_GIANT;
253                 }
254                 else if (fontSizeValue.Equals(L"huge", false))
255                 {
256                         fontSize = FONT_MAIN_TEXT_SIZE_HUGE;
257                 }
258                 else if (fontSizeValue.Equals(L"large", false))
259                 {
260                         fontSize = FONT_MAIN_TEXT_SIZE_LARGE;
261                 }
262                 else if (fontSizeValue.Equals(L"medium", false))
263                 {
264                         fontSize = FONT_MAIN_TEXT_SIZE_NORMAL;
265                 }
266                 else
267                 {
268                         fontSize = FONT_MAIN_TEXT_SIZE_SMALL;
269                 }
270         }
271         return fontSize;
272 }
273
274 void
275 BaseForm::OnOrientationChanged(const Tizen::Ui::Control& source, Tizen::Ui::OrientationStatus orientationStatus)
276 {
277         Invalidate(true);
278 }
279
280 int
281 BaseForm::GetHeightForStringArea(const Tizen::Base::String source, int width, int fontSize) const
282 {
283         Font font;
284         Dimension dim;
285         String temp;
286
287         int lineCount = LINE_COUNT_DEFAULT;
288         int margin = BASE_FORM_DEFAULT_MARGIN + RELATIVE_LAYOUT_RIGHT_MARGIN;
289         if (fontSize > FONT_MAIN_TEXT_SIZE_LARGE)
290         {
291                 OrientationStatus orientationStatus = GetOrientationStatus();
292                 if (((orientationStatus == ORIENTATION_STATUS_PORTRAIT)
293                         || (orientationStatus == ORIENTATION_STATUS_PORTRAIT_REVERSE))
294                         && (fontSize == FONT_MAIN_TEXT_SIZE_HUGE))
295                 {
296                         width -= W_HUGE_SIZE_PORTRAIT_TEXT_GAP;
297                 }
298                 else if (((orientationStatus == ORIENTATION_STATUS_LANDSCAPE)
299                                 || (orientationStatus == ORIENTATION_STATUS_LANDSCAPE_REVERSE))
300                                 && (fontSize == FONT_MAIN_TEXT_SIZE_HUGE))
301                 {
302                         width -= W_HUGE_SIZE_LANDSCAPE_TEXT_GAP;
303                 }
304                 width -= W_LARGE_SIZE_TEXT_GAP;
305         }
306         int boudwidth = width - margin;
307         font.Construct(FONT_STYLE_PLAIN, fontSize);
308
309         for (int i = 0; i < source.GetLength(); i++)
310         {
311                 temp.Append(source[i]);
312                 font.GetTextExtent(temp, temp.GetLength(), dim);
313                 if (dim.width > boudwidth)
314                 {
315                         temp.Clear();
316                         temp.Append(source[i]);
317                         lineCount++;
318                 }
319         }
320
321         return dim.height * lineCount;
322 }