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