e977bb43bc07c6f50e55ecfed2db34daf2a4b8ab
[apps/native/sample/DynamicBoxViewer.git] / project / src / ViewForm.cpp
1
2 #include <unique_ptr.h>
3 #include <FShellLiveboxView.h>
4 #include <FShellLiveboxManager.h>
5 #include <FShellLiveboxProviderInfo.h>
6 #include <FShellLiveboxSizeInfo.h>
7 #include "ViewForm.h"
8
9 using namespace Tizen::Base;
10 using namespace Tizen::Base::Collection;
11 using namespace Tizen::Graphics;
12 using namespace Tizen::Ui;
13 using namespace Tizen::Ui::Controls;
14 using namespace Tizen::Ui::Scenes;
15 using namespace Tizen::Shell;
16
17 const int CONTEXT_MENU_X = 100;
18 const int CONTEXT_MENU_Y = 200;
19 const int VIEW_X = 0;
20 const int VIEW_Y = 50;
21 const int VIEW_WIDTH = 172;
22 const int VIEW_HEIGHT = 172;
23
24 ViewForm::ViewForm(void)
25         : __pLiveboxView(null)
26         , __pContextMenu(null)
27         , __pSizeInfoList(null)
28 {
29 }
30
31 ViewForm::~ViewForm(void)
32 {
33 }
34
35 bool
36 ViewForm::Initialize()
37 {
38         Form::Construct(L"IDF_VIEW_FORM");
39
40         return true;
41 }
42
43 result
44 ViewForm::OnInitializing(void)
45 {
46         result r = E_SUCCESS;
47
48         // TODO: Add your initialization code here
49         SetFormBackEventListener(this);
50         Footer* pFooter = GetFooter();
51         pFooter->AddActionEventListener(*this);
52         return r;
53 }
54
55 result
56 ViewForm::OnTerminating(void)
57 {
58         result r = E_SUCCESS;
59
60         // TODO: Add your termination code here
61         delete __pContextMenu;
62         if (__pSizeInfoList)
63         {
64                 __pSizeInfoList->RemoveAll(true);
65                 delete __pSizeInfoList;
66         }
67
68         return r;
69 }
70
71 void
72 ViewForm::CreateLiveboxView(const Tizen::Base::String& appId, const Tizen::Base::String& providerName)
73 {
74         __pLiveboxView = new LiveboxView();
75         if (__pLiveboxView)
76         {
77                 __pLiveboxView->Construct(appId, providerName, Rectangle(VIEW_X, VIEW_Y, VIEW_WIDTH, VIEW_HEIGHT));
78                 AddControl(*__pLiveboxView);
79         }
80
81         __pContextMenu = new (std::nothrow) ContextMenu();
82         __pContextMenu->Construct(Point(CONTEXT_MENU_X, CONTEXT_MENU_Y), CONTEXT_MENU_STYLE_LIST, CONTEXT_MENU_ANCHOR_DIRECTION_DOWNWARD);
83
84         std::unique_ptr<LiveboxProviderInfo> pProviderInfo(LiveboxManager::GetInstance()->GetLiveboxProviderInfoN(appId, providerName ));
85         TryReturnVoid(pProviderInfo.get(), "pProviderInfo is null!!");
86
87         __pSizeInfoList = pProviderInfo->GetSizeInfosN();
88         if (__pSizeInfoList)
89         {
90                 for(int i = 0; i < __pSizeInfoList->GetCount(); i ++)
91                 {
92                         LiveboxSizeInfo* pSizeInfo = dynamic_cast<LiveboxSizeInfo*>(__pSizeInfoList->GetAt(i) );
93                         if (!pSizeInfo)
94                         {
95                                 AppLog("pSizeInfo is null");
96                                 continue;
97                         }
98                         AppLog("(%d, %d)", pSizeInfo->GetSize().width, pSizeInfo->GetSize().height);
99
100                         String size;
101                         size.Format(255, L"%dx%d", pSizeInfo->GetSize().width, pSizeInfo->GetSize().height);
102
103                         __pContextMenu->AddItem(size, i);
104                 }
105         }
106
107         Rectangle formBounds = GetBounds();
108         Rectangle contextMenuBounds = __pContextMenu->GetBounds();
109         Point anchor = __pContextMenu->GetAnchorPosition();
110         anchor.y = formBounds.height - contextMenuBounds.height;
111
112         AppLog("(%d %d %d %d) (%d %d)", contextMenuBounds.x, contextMenuBounds.y, contextMenuBounds.width, contextMenuBounds.height, anchor.x, anchor.y);
113
114         __pContextMenu->SetAnchorPosition(anchor);
115
116         __pContextMenu->AddActionEventListener(*this);
117 }
118
119 void
120 ViewForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
121 {
122         AppLog("(%d)", actionId);
123
124         if (actionId == ID_FOOTER_ITEM1)
125         {
126                 if (__pContextMenu && (__pContextMenu->GetItemCount() > 0))
127                 {
128                         __pContextMenu->SetShowState(true);
129                         __pContextMenu->Show();
130                 }
131         }
132         else
133         {
134                 LiveboxSizeInfo* pSizeInfo = null;
135                 if (__pSizeInfoList)
136                 {
137                         pSizeInfo = dynamic_cast<LiveboxSizeInfo*>(__pSizeInfoList->GetAt(actionId) );
138                         TryReturnVoid(pSizeInfo, "pSizeInfo is null!!");
139                 }
140                 
141                 if (__pLiveboxView)
142                 {
143                         __pLiveboxView->SetSize(pSizeInfo->GetSize());
144                 }
145         }
146 }
147
148 void
149 ViewForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
150 {
151         SceneManager* pSceneManager = SceneManager::GetInstance();
152         AppAssert(pSceneManager);
153         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
154 }
155
156 void
157 ViewForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
158 {
159         if (pArgs && (pArgs->GetCount() > 0))
160         {
161                 String* pAppId = dynamic_cast<String*>(pArgs->GetAt(0));
162                 String* pLiveboxProviderName = dynamic_cast<String*>(pArgs->GetAt(1));
163                 if (pAppId != null && pLiveboxProviderName != null)
164                 {
165                         AppLog("%ls", pAppId->GetPointer());
166
167                         Header* pHeader = GetHeader();
168                         pHeader->SetTitleText(*pAppId);
169
170                         CreateLiveboxView(*pAppId, *pLiveboxProviderName);
171                 }
172                 
173                 pArgs->RemoveAll(true);
174                 delete pArgs;
175         }
176 }
177
178 void
179 ViewForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
180 {
181         if (__pLiveboxView)
182         {
183                 RemoveControl(*__pLiveboxView);
184                 __pLiveboxView = null;
185         }
186 }
187