Flora license update
[apps/osp/Home.git] / src / HmCustomPageMarker.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        CustomPageMarker.cpp
19  * @brief       Keeps implementation of the CustomPageMarker
20  * implements CustomPageMarker class, which contains the top bubble's bar which indicates the page number
21  */
22
23
24 #include <FBase.h>
25 #include <FGraphics.h>
26 #include <FUi.h>
27 #include <FUiAnimations.h>
28 #include "HmApplicationUtils.h"
29 #include "HmCustomPageMarker.h"
30 #include "HmTypes.h"
31
32 using namespace Tizen::Base;
33 using namespace Tizen::Base::Collection;
34 using namespace Tizen::Graphics;
35 using namespace Tizen::Ui;
36 using namespace Tizen::Ui::Animations;
37 using namespace Tizen::Ui::Controls;
38
39 CustomPageMarker::CustomPageMarker(void)
40         : __pMarkerListener(null)
41         , __bubbleCount(1)
42         , __selectedBubble(0)
43         , __pBubbleLabels(null)
44         , __pBubbleBitmaps(null)
45 {
46         //No implementation needed
47 }
48
49 CustomPageMarker::~CustomPageMarker(void)
50 {
51         if (__pBubbleLabels != null)
52         {
53                 __pBubbleLabels->RemoveAll(false); //the controls will be deleted using removeControl calls by framework
54                 delete __pBubbleLabels;
55         }
56
57         if (__pBubbleBitmaps != null)
58         {
59                 __pBubbleBitmaps->RemoveAll(true);
60                 delete __pBubbleBitmaps;
61         }
62 }
63
64 result
65 CustomPageMarker::Construct(const Rectangle& controlRect, int bubbleCount)
66 {
67         result r = E_SUCCESS;
68         int xPos = 0;
69         int yPos = Y_MARKER_POSITION;
70         int width = 0;
71         int xFactor = 0;
72         Bitmap* pUnselectedBitmap = null;
73         Bitmap* pSelectedBitmap = null;
74         __bubbleCount = bubbleCount;
75
76         r = Panel::Construct(controlRect, GROUP_STYLE_NONE);
77         TryCatch(r == E_SUCCESS, , "Panel::Construct failed with error %s", GetErrorMessage(r));
78
79         __pBubbleLabels = new (std::nothrow) ArrayList();
80         r = __pBubbleLabels->Construct();
81         TryCatch(r == E_SUCCESS, , "__pLabels->Construct(); failed with error %s", GetErrorMessage(r));
82
83         __pBubbleBitmaps = new (std::nothrow) ArrayList();
84         r = __pBubbleBitmaps->Construct();
85         pUnselectedBitmap = ApplicationUtils::GetResourceBitmapN(IDB_BUBBLE_TOP);
86
87         if (pUnselectedBitmap != null)
88         {
89                 __pBubbleBitmaps->Add(pUnselectedBitmap);
90         }
91
92         for (int bubbleCountTemp = 1; bubbleCountTemp <= __bubbleCount; bubbleCountTemp++)
93         {
94                 String bubbleName(IDB_SELECTED_BUBBLE_TOP);
95                 bubbleName.Append(bubbleCountTemp);
96                 bubbleName.Append(BITMAP_EXTENSION);
97                 pSelectedBitmap = ApplicationUtils::GetResourceBitmapN(bubbleName);
98
99                 if (pSelectedBitmap != null)
100                 {
101                         __pBubbleBitmaps->Add(pSelectedBitmap);
102                 }
103         }
104
105         width = (controlRect.width);
106         xFactor = width - ((W_MARKER_LABEL * __bubbleCount) + (X_MARKER_LABEL_OFFSET * (__bubbleCount - 1)));
107
108         xPos = xFactor / 2;
109
110         for (int bubbleCount = 1; bubbleCount <= __bubbleCount; bubbleCount++)
111         {
112                 Label* pLabel = new (std::nothrow) Label();
113                 String labelName;
114                 labelName.Append(bubbleCount);
115
116                 if (pLabel != null)
117                 {
118                         Rectangle labelRect(xPos, yPos, W_MARKER_LABEL, H_MARKER_LABEL);
119                         pLabel->Construct(labelRect, L"");
120                         pLabel->SetBackgroundBitmap(*static_cast<Bitmap*>(__pBubbleBitmaps->GetAt(0)));
121                         pLabel->SetTextColor(Color::GetColor(COLOR_ID_BLACK));
122                         pLabel->SetTextHorizontalAlignment(ALIGNMENT_CENTER);
123                         pLabel->SetTextVerticalAlignment(ALIGNMENT_MIDDLE);
124                         pLabel->SetName(labelName);
125                         pLabel->AddTouchEventListener(*this);
126                         AddControl(pLabel);
127                         __pBubbleLabels->Add(pLabel);
128                         xPos += W_MARKER_LABEL + X_MARKER_LABEL_OFFSET;
129                 }
130         }
131
132         return r;
133
134 CATCH:
135
136         if (__pBubbleLabels != null)
137         {
138                 delete __pBubbleLabels;
139                 __pBubbleLabels = null;
140         }
141         return r;
142 }
143
144 result
145 CustomPageMarker::AddBubble(void)
146 {
147         result r = E_SUCCESS;
148         Rectangle controlRect = GetBounds();
149         int width = 0;
150         int xFactor = 0;
151         int xPos = 0;
152         int yPos = Y_MARKER_POSITION;
153         Label* pLabel = null;
154         String labelName;
155         __bubbleCount++;
156
157         width = (controlRect.width);
158         xFactor = width - ((W_MARKER_LABEL * __bubbleCount) + (X_MARKER_LABEL_OFFSET * (__bubbleCount - 1)));
159
160         xPos = xFactor / 2;
161
162         for (int bubbleCount = 1; bubbleCount < __bubbleCount; bubbleCount++)
163         {
164                 String labelName;
165                 labelName.Append(bubbleCount);
166                 Label* pLabel = static_cast<Label*>(GetControl(labelName));
167
168                 if (pLabel != null)
169                 {
170                         Rectangle labelRect(xPos, yPos, W_MARKER_LABEL, H_MARKER_LABEL);
171                         pLabel->SetBounds(labelRect);
172                         xPos += (X_MARKER_LABEL_OFFSET + W_MARKER_LABEL);
173                 }
174         }
175
176         pLabel = new (std::nothrow) Label();
177         labelName.Append(__bubbleCount);
178
179         if (pLabel != null)
180         {
181                 String bubbleName(IDB_SELECTED_BUBBLE_TOP);
182                 Bitmap* pNewBubbleBitmap = null;
183                 Rectangle labelRect(xPos, yPos, W_MARKER_LABEL, H_MARKER_LABEL);
184                 pLabel->Construct(labelRect, L"");
185                 bubbleName.Append(__bubbleCount);
186                 bubbleName.Append(BITMAP_EXTENSION);
187                 pNewBubbleBitmap = ApplicationUtils::GetResourceBitmapN(bubbleName);
188
189                 if (pNewBubbleBitmap != null)
190                 {
191                         __pBubbleBitmaps->Add(pNewBubbleBitmap);
192                 }
193
194                 pLabel->SetBackgroundBitmap(*static_cast<Bitmap*>(__pBubbleBitmaps->GetAt(0)));
195                 pLabel->SetTextColor(Color::GetColor(COLOR_ID_BLACK));
196                 pLabel->SetTextHorizontalAlignment(ALIGNMENT_CENTER);
197                 pLabel->SetTextVerticalAlignment(ALIGNMENT_MIDDLE);
198                 pLabel->SetName(labelName);
199                 pLabel->AddTouchEventListener(*this);
200                 r = AddControl(pLabel);
201                 __pBubbleLabels->Add(pLabel);
202                 xPos += (W_MARKER_LABEL + X_MARKER_LABEL_OFFSET);
203         }
204         return r;
205 }
206
207 const int
208 CustomPageMarker::GetBubbleCount(void)
209 {
210         return __bubbleCount;
211 }
212
213 result
214 CustomPageMarker::RemoveBubble(int bubbleNo)
215 {
216         result r = E_SUCCESS;
217         Label* pLabel = static_cast<Label*>(__pBubbleLabels->GetAt(bubbleNo - 1));
218
219         if (pLabel != null)
220         {
221                 RemoveControl(bubbleNo - 1);
222                 __pBubbleLabels->RemoveAt(bubbleNo - 1, false);
223                 __pBubbleBitmaps->RemoveAt(bubbleNo, true);
224
225                 for (int bubbleCount = bubbleNo; bubbleCount < __bubbleCount; bubbleCount++)
226                 {
227                         Label* pLabel = static_cast<Label*>(GetControl(bubbleCount));
228
229                         if (pLabel != null)
230                         {
231                                 Point curPos = pLabel->GetPosition();
232                                 curPos.x -= (W_MARKER_LABEL + X_MARKER_LABEL_OFFSET);
233                                 pLabel->SetPosition(curPos);
234                         }
235                 }
236
237                 RequestRedraw(true);
238                 __bubbleCount--;
239         }
240         return r;
241 }
242
243 void
244 CustomPageMarker::SetPageMarkerEventListener(ICustomPageMarkerEventListener* pListner)
245 {
246         __pMarkerListener = pListner;
247         return;
248 }
249
250 void
251 CustomPageMarker::SetSelectedBubble(int bubbleNumber)
252 {
253         TryReturnVoid(bubbleNumber != __selectedBubble, "Passed page is already being shown");
254         String controlName;
255         controlName.Append(bubbleNumber);
256         Label* pLabel = null;
257
258         if (__selectedBubble != 0)
259         {
260                 String controlName;
261                 controlName.Append(__selectedBubble);
262                 Label* pLabel = static_cast<Label*>(GetControl(controlName));
263
264                 if (pLabel != null)
265                 {
266                         Bitmap* pBitmapToBeSet = null;
267                         pLabel->SetText(L"");
268
269                         pBitmapToBeSet = static_cast<Bitmap*>(__pBubbleBitmaps->GetAt(0));
270
271                         if (pBitmapToBeSet != null)
272                         {
273                                 pLabel->SetBackgroundBitmap(*pBitmapToBeSet);
274                         }
275                         pLabel->RequestRedraw(true);
276                         __selectedBubble = bubbleNumber;
277                 }
278         }
279
280         pLabel = static_cast<Label*>(GetControl(controlName));
281
282         if (pLabel != null)
283         {
284                 Bitmap* pBitmapToBeSet = null;
285                 pBitmapToBeSet = static_cast<Bitmap*>(__pBubbleBitmaps->GetAt(bubbleNumber));
286
287                 if (pBitmapToBeSet != null)
288                 {
289                         pLabel->SetBackgroundBitmap(*pBitmapToBeSet);
290                 }
291
292                 pLabel->RequestRedraw(true);
293                 __selectedBubble = bubbleNumber;
294         }
295
296         return;
297 }
298
299 void
300 CustomPageMarker::OnTouchReleased(const Control& source, const Point& currentPosition, const TouchEventInfo& touchInfo)
301 {
302         if (__pMarkerListener != null)
303         {
304                 String controlName = source.GetName();
305
306                 if (!controlName.IsEmpty())
307                 {
308                         int bubbleNumber = 0;
309                         Integer::Parse(controlName, bubbleNumber);
310                         __pMarkerListener->OnMarkerBubbleMoved(bubbleNumber);
311                 }
312         }
313         return;
314 }