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