Fixed N_SE-38563, N_SE-38552
[apps/osp/Settings.git] / src / StFontSizeForm.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                StFontSizeForm.cpp
19  * @brief               This is the implementation file for FontSizeForm class.
20  */
21
22 #include "StFontSizeForm.h"
23 #include "StResourceManager.h"
24 #include "StTypes.h"
25
26 using namespace Tizen::App;
27 using namespace Tizen::Base;
28 using namespace Tizen::Base::Collection;
29 using namespace Tizen::Graphics;
30 using namespace Tizen::System;
31 using namespace Tizen::Ui;
32 using namespace Tizen::Ui::Controls;
33 using namespace Tizen::Ui::Scenes;
34
35 static const int ID_GROUP_FONT_SIZE = 0;
36 static const int ID_GROUP_FONT_ITEM_COUNT = 5;
37 static const int ID_ITEM_FONT_SIZE_GIANT = 0;
38 static const int ID_ITEM_FONT_SIZE_HUGE = 1;
39 static const int ID_ITEM_FONT_SIZE_LARGE = 2;
40 static const int ID_ITEM_FONT_SIZE_NORMAL = 3;
41 static const int ID_ITEM_FONT_SIZE_SMALL = 4;
42
43 static const int ID_GROUP_FONT_TEXT = 1;
44 static const int ID_GROUP_FONT_TEXT_LABEL = -1;
45 static const int ID_GROUP_FONT_TEXT_ITEM_COUNT = 0;
46
47 static const int ID_GROUP_COUNT = 2;
48 static const int ID_GROUP_MAX_ITEM_COUNT = ID_GROUP_FONT_ITEM_COUNT;
49
50 static const int H_GROUP_INDEX_HELP_TEXT_GAP = 20;
51
52 static const int DIVIDE_BT_TWO = 2;
53
54 FontSizeForm::FontSizeForm(void)
55         : __isAppControlRequest(false)
56         , __categoryCheck(L"")
57 {
58 }
59
60 FontSizeForm::~FontSizeForm(void)
61 {
62 }
63
64 void
65 FontSizeForm::CreateFooter(void)
66 {
67         Footer* pFooter = GetFooter();
68         AppAssert(pFooter);
69
70         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
71         pFooter->SetBackButton();
72         pFooter->AddActionEventListener(*this);
73
74         SetFormBackEventListener(this);
75 }
76
77 result
78 FontSizeForm::OnInitializing(void)
79 {
80         CreateHeader(ResourceManager::GetString(L"IDS_ST_BODY_FONT_SIZE"));
81         CreateFooter();
82         CreateTableView();
83
84         AppLogDebug("ENTER");
85
86         return E_SUCCESS;
87 }
88
89 result
90 FontSizeForm::OnTerminating(void)
91 {
92         __pTableView = null;
93
94         SetFormBackEventListener(null);
95         return E_SUCCESS;
96 }
97
98 void
99 FontSizeForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
100 {
101         int itemSelect = 0;
102         String fontReturnValue;
103
104         if (pArgs != null)
105         {
106                 __categoryCheck = static_cast<String*>(pArgs->GetAt(0))->GetPointer();
107
108                 if ((__categoryCheck == L"category:FontType") || (__categoryCheck == L"category:FontSize"))
109                 {
110                         __isAppControlRequest = true;
111                 }
112                 delete pArgs;
113         }
114
115         __pTableView->UpdateTableView();
116
117         if (SettingInfo::GetValue(SETTING_INFO_KEY_FONT_SIZE, fontReturnValue) == E_SUCCESS)
118         {
119                 if (fontReturnValue.Equals(L"giant", false))
120                 {
121                         itemSelect = ID_ITEM_FONT_SIZE_GIANT;
122                 }
123                 else if (fontReturnValue.Equals(L"huge", false))
124                 {
125                         itemSelect = ID_ITEM_FONT_SIZE_HUGE;
126                 }
127                 else if (fontReturnValue.Equals(L"large", false))
128                 {
129                         itemSelect = ID_ITEM_FONT_SIZE_LARGE;
130                 }
131                 else if (fontReturnValue.Equals(L"medium", false))
132                 {
133                         itemSelect = ID_ITEM_FONT_SIZE_NORMAL;
134                 }
135                 else
136                 {
137                         itemSelect = ID_ITEM_FONT_SIZE_SMALL;
138                 }
139                 __pTableView->SetItemChecked(ID_GROUP_FONT_SIZE, itemSelect, true);
140         }
141 }
142
143 void
144 FontSizeForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
145 {
146 }
147
148 void
149 FontSizeForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
150 {
151         SceneManager* pSceneManager = SceneManager::GetInstance();
152         AppAssert(pSceneManager);
153
154         if (__isAppControlRequest == true)
155         {
156                 UiApp* pApp = UiApp::GetInstance();
157                 AppAssert(pApp);
158                 AppControlFontResult();
159                 pApp->Terminate();
160         }
161         else
162         {
163                 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
164         }
165 }
166
167 int
168 FontSizeForm::GetGroupCount(void)
169 {
170         AppLogDebug("ENTER");
171         return ID_GROUP_COUNT;
172 }
173
174 int
175 FontSizeForm::GetItemCount(int groupIndex)
176 {
177         int itemCount = 0;
178
179         if (groupIndex != ID_GROUP_FONT_TEXT)
180         {
181                 itemCount = ID_GROUP_MAX_ITEM_COUNT;
182         }
183
184         AppLogDebug("GetItemCount %d", itemCount);
185
186         return itemCount;
187 }
188
189 TableViewGroupItem*
190 FontSizeForm::CreateGroupItem(int groupIndex, int itemWidth)
191 {
192         AppLogDebug("ENTER");
193
194         int itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
195         int yItemOffset = H_GROUP_INDEX_HELP_TEXT_TOP_GAP;
196         LabelTextStyle style = LABEL_TEXT_STYLE_NORMAL;
197         Rectangle itemMainRectangle;
198         int fontSize = GetFontSize();
199         String groupText;
200         Label* pLabel = null;
201
202         TableViewGroupItem* pItem = new (std::nothrow) TableViewGroupItem();
203
204         switch (groupIndex)
205         {
206         case ID_GROUP_FONT_SIZE:
207                 {
208                         yItemOffset = H_GROUP_INDEX_NO_HELP_TEXT_GAP;
209                         itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
210                 }
211                 break;
212
213         case ID_GROUP_FONT_TEXT:
214                 {
215                         yItemOffset = H_GROUP_INDEX_HELP_TEXT_TOP_GAP / DIVIDE_BT_TWO ;
216                         groupText = ResourceManager::GetString(L"IDS_ST_BODY_CHANGING_THE_FONT_SIZE_IN_ACCESSIBILITY_SETTINGS_WILL_OVERRIDE_THE_FONT_SIZE_IN_EACH_APPLICATION");
217                         itemHeight = GetHeightForStringArea(groupText, itemWidth, fontSize) + H_GROUP_INDEX_HELP_TEXT_GAP;
218                 }
219                 break;
220
221         default:
222                 {
223                         yItemOffset = H_GROUP_INDEX_NO_HELP_TEXT_GAP;
224                         itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
225                 }
226                 break;
227         }
228
229         itemMainRectangle.x = X_GROUP_ITEM_DEFAULT_LABEL;
230         itemMainRectangle.y = yItemOffset;
231         itemMainRectangle.width = itemWidth;
232         itemMainRectangle.height = itemHeight;
233
234         RelativeLayout relativeLayout;
235         relativeLayout.Construct();
236
237         pItem->Construct(relativeLayout, Dimension(itemWidth, itemHeight));
238         pItem->SetBackgroundColor(COLOR_BG_GROUP_INDEX_DEFAULT);
239
240         pLabel = new (std::nothrow) Label();
241         pLabel->Construct(itemMainRectangle, groupText);
242         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
243         pLabel->SetTextVerticalAlignment(ALIGNMENT_TOP);
244         pLabel->SetTextConfig(fontSize, style);
245         pLabel->SetTextColor(COLOR_HELP_TEXT_TYPE_01);
246
247         pItem->AddControl(pLabel);
248         pItem->SetEnabled(false);
249         relativeLayout.SetMargin(*pLabel, itemMainRectangle.x, 0, 0, 0);
250         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
251         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
252
253         return pItem;
254 }
255
256 TableViewItem*
257 FontSizeForm::CreateItem(int groupIndex, int itemIndex, int itemWidth)
258 {
259         AppLogDebug("group[%d] index[%d]", groupIndex, itemIndex);
260
261         Rectangle itemMainRectangle;
262         TableViewAnnexStyle style = TABLE_VIEW_ANNEX_STYLE_RADIO;
263         String itemMainText;
264         String fontReturnValue;
265         Label* pLabel = null;
266         int fontSize = FONT_MAIN_TEXT_SIZE_NORMAL;
267         TableViewItem* pItem = new (std::nothrow) TableViewItem();
268
269         if (SettingInfo::GetValue(SETTING_INFO_KEY_FONT_SIZE, fontReturnValue) == E_SUCCESS)
270         {
271                 AppLogDebug("GetValue Fail [%s]", GetErrorMessage(GetLastResult()));
272         }
273
274         switch(itemIndex)
275         {
276         case ID_ITEM_FONT_SIZE_GIANT:
277                 {
278                         fontSize = FONT_MAIN_TEXT_SIZE_GIANT;
279                         itemMainText = ResourceManager::GetString(L"IDS_EMAIL_POP_GIANT_M_TEXTSIZE");
280
281                         if (fontReturnValue.Equals(L"giant", false))
282                         {
283                                 __pTableView->SetItemChecked(groupIndex, itemIndex, true);
284                         }
285                 }
286                 break;
287
288         case ID_ITEM_FONT_SIZE_HUGE:
289                 {
290                         fontSize = FONT_MAIN_TEXT_SIZE_HUGE;
291                         itemMainText = ResourceManager::GetString(L"IDS_EMAIL_OPT_HUGE_M_TEXTSIZE");
292
293                         if (fontReturnValue.Equals(L"huge", false))
294                         {
295                                 __pTableView->SetItemChecked(groupIndex, itemIndex, true);
296                         }
297                 }
298                 break;
299
300         case ID_ITEM_FONT_SIZE_LARGE:
301                 {
302                         fontSize = FONT_MAIN_TEXT_SIZE_LARGE;
303                         itemMainText = ResourceManager::GetString(L"IDS_ST_BODY_TEXTSTYLE_LARGE");
304
305                         if (fontReturnValue.Equals(L"large", false))
306                         {
307                                 __pTableView->SetItemChecked(groupIndex, itemIndex, true);
308                         }
309                 }
310                 break;
311
312         case ID_ITEM_FONT_SIZE_NORMAL:
313                 {
314                         fontSize = FONT_MAIN_TEXT_SIZE_NORMAL;
315                         itemMainText = ResourceManager::GetString(L"IDS_ST_BODY_FONTTYPE_NORMAL");
316
317                         if (fontReturnValue.Equals(L"medium", false))
318                         {
319                                 __pTableView->SetItemChecked(groupIndex, itemIndex, true);
320                         }
321                 }
322                 break;
323
324         case ID_ITEM_FONT_SIZE_SMALL:
325                 {
326                         fontSize = FONT_MAIN_TEXT_SIZE_SMALL;
327                         itemMainText = ResourceManager::GetString(L"IDS_ST_BODY_SMALL_M_TEXTSIZE");
328
329                         if (fontReturnValue.Equals(L"small", false))
330                         {
331                                 __pTableView->SetItemChecked(groupIndex, itemIndex, true);
332                         }
333                 }
334                 break;
335
336         default:
337                 break;
338         }
339
340         ItemTypeOneLine(itemMainRectangle);
341         itemMainRectangle.height = itemMainRectangle.height + H_GROUP_INDEX_HELP_TEXT_GAP;
342
343         RelativeLayout relativeLayout;
344         relativeLayout.Construct();
345
346         pItem->Construct(relativeLayout, Dimension(itemWidth, itemMainRectangle.height), style);
347         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
348
349         pLabel = new (std::nothrow) Label();
350         pLabel->Construct(itemMainRectangle, itemMainText);
351         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
352
353         pLabel->SetTextConfig(fontSize, LABEL_TEXT_STYLE_NORMAL);
354         pLabel->SetTextColor(COLOR_MAIN_TEXT);
355
356         pItem->AddControl(pLabel);
357         relativeLayout.SetMargin(*pLabel, RELATIVE_LAYOUT_LEFT_MARGIN, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
358         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
359         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
360
361         return pItem;
362 }
363
364 bool
365 FontSizeForm::DeleteGroupItem(int groupIndex, TableViewGroupItem* pItem)
366 {
367         AppLogDebug("ENTER");
368
369         delete pItem;
370         pItem = null;
371
372         return true;
373 }
374
375 bool
376 FontSizeForm::DeleteItem(int groupIndex, int itemIndex, TableViewItem* pItem)
377 {
378         AppLogDebug("ENTER");
379
380         delete pItem;
381         pItem = null;
382
383         return true;
384 }
385
386 void
387 FontSizeForm::OnGroupedTableViewItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
388 {
389         AppLogDebug("group[%d] index[%d]", groupIndex, itemIndex);
390
391         String fontSize(L"medium");
392
393         switch (itemIndex)
394         {
395         case ID_ITEM_FONT_SIZE_GIANT:
396                 {
397                         fontSize = L"giant";
398                 }
399                 break;
400
401         case ID_ITEM_FONT_SIZE_HUGE:
402                 {
403                         fontSize = L"huge";
404                 }
405                 break;
406
407         case ID_ITEM_FONT_SIZE_LARGE:
408                 {
409                         fontSize = L"large";
410                 }
411                 break;
412
413         case ID_ITEM_FONT_SIZE_NORMAL:
414                 {
415                         fontSize = L"medium";
416                 }
417                 break;
418
419         case ID_ITEM_FONT_SIZE_SMALL:
420                 {
421                         fontSize = L"small";
422                 }
423                 break;
424
425         default:
426                 break;
427         }
428
429         int itemCount = tableView.GetItemCountAt(ID_GROUP_FONT_SIZE);
430         for (int count = 0; count < itemCount; count++)
431         {
432                 int localGroup = ID_GROUP_FONT_SIZE;
433                 int localItem = count;
434
435                 if (count == itemIndex)
436                 {
437                         tableView.SetItemChecked(localGroup, localItem, true);
438                         SettingInfo::SetValue(SETTING_INFO_KEY_FONT_SIZE, fontSize);
439                 }
440                 else
441                 {
442                         tableView.SetItemChecked(localGroup, localItem, false);
443                 }
444         }
445         RefreshAllItem();
446         __pTableView->ScrollToItem(groupIndex, itemIndex);
447 }
448
449 int
450 FontSizeForm::GetDefaultGroupItemHeight(void)
451 {
452         return H_GROUP_INDEX_NO_TITLE_DEFAULT;
453 }
454
455 int
456 FontSizeForm::GetDefaultItemHeight(void)
457 {
458         return H_GROUP_ITEM_DEFAULT;
459 }
460
461 void
462 FontSizeForm::UpdateGroupItem(int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem)
463 {
464         if (groupIndex != ID_GROUP_FONT_TEXT)
465         {
466                 AppLogDebug("groupIndex is not ID_GROUP_FONT_TEXT");
467                 return;
468         }
469
470         Label* pLabel = static_cast<Label*>(pItem->GetControl(0));
471         if (pLabel == null)
472         {
473                 AppLogDebug("pLabel is null");
474                 return;
475         }
476
477         Rectangle clientRect = GetClientAreaBounds();
478         Rectangle itemRect = pItem->GetBounds();
479         String groupText = pLabel->GetText();
480         int fontSize = GetFontSize();
481         int itemHeight = GetHeightForStringArea(groupText, clientRect.width, fontSize) + H_GROUP_INDEX_HELP_TEXT_GAP;
482
483         itemRect.height = itemHeight + H_GROUP_INDEX_HELP_TEXT_TOP_GAP;
484         pLabel->SetTextConfig(fontSize, LABEL_TEXT_STYLE_NORMAL);
485         pLabel->SetBounds(clientRect.x, H_GROUP_INDEX_HELP_TEXT_TOP_GAP, clientRect.width, itemHeight);
486         pItem->SetBounds(itemRect);
487         Invalidate(true);
488 }
489
490 void
491 FontSizeForm::UpdateItem(int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
492 {
493 }
494
495 void
496 FontSizeForm::OnGroupedTableViewContextItemActivationStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewContextItem* pContextItem, bool activated)
497 {
498 }
499
500 void
501 FontSizeForm::OnGroupedTableViewGroupItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
502 {
503 }
504
505 void
506 FontSizeForm::RefreshAllItem(void)
507 {
508         int groupCount = __pTableView->GetGroupCount();
509
510         for (int count = 0; count < groupCount; count++)
511         {
512                 RefreshGroupAt(count);
513 //              RefreshGroupItemAt(count);
514         }
515 }
516
517 void
518 FontSizeForm::RefreshGroupAt(int groupIndex)
519 {
520         __pTableView->RefreshItem(groupIndex, -1, TABLE_VIEW_REFRESH_TYPE_ITEM_MODIFY);
521 }
522
523 void
524 FontSizeForm::RefreshGroupItemAt(int groupIndex)
525 {
526         int itemCount = __pTableView->GetItemCountAt(groupIndex);
527
528         for (int count = 0; count < itemCount; count++)
529         {
530                 __pTableView->RefreshItem(groupIndex, count, TABLE_VIEW_REFRESH_TYPE_ITEM_MODIFY);
531         }
532 }
533
534 void
535 FontSizeForm::AppControlFontResult(void)
536 {
537         RequestId reqId = 0;
538         String value;
539         AppControlProviderManager* pAppManager = AppControlProviderManager::GetInstance();
540
541         HashMap* hashMap = new (std::nothrow) HashMap();
542         hashMap->Construct();
543
544         if (__categoryCheck == L"category:FontType")
545         {
546                 SettingInfo::GetValue(SETTING_INFO_KEY_FONT_TYPE, value);
547                 hashMap->Add(*(new (std::nothrow) String(L"category")), *(new (std::nothrow) String(L"FontType")));
548                 hashMap->Add(*(new (std::nothrow) String(L"type")), *(new (std::nothrow) String(value)));
549         }
550         else if (__categoryCheck == L"category:FontSize")
551         {
552                 SettingInfo::GetValue(SETTING_INFO_KEY_FONT_SIZE, value);
553                 hashMap->Add(*(new (std::nothrow) String(L"category")), *(new (std::nothrow) String(L"FontSize")));
554                 hashMap->Add(*(new (std::nothrow) String(L"size")), *(new (std::nothrow) String(value)));
555         }
556
557         pAppManager->SendAppControlResult(reqId, APP_CTRL_RESULT_SUCCEEDED, hashMap);
558
559         hashMap->RemoveAll();
560         delete hashMap;
561 }
562
563 void
564 FontSizeForm::OnOrientationChanged(const Tizen::Ui::Control& source, Tizen::Ui::OrientationStatus orientationStatus)
565 {
566         __pTableView->RefreshItem(ID_GROUP_FONT_TEXT, ID_GROUP_FONT_TEXT_LABEL, TABLE_VIEW_REFRESH_TYPE_ITEM_MODIFY);
567         Invalidate(true);
568 }