Changed indicator bg color.
[platform/framework/native/uifw.git] / src / ui / controls / FUiCtrl_ListViewContextItem.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FUiCtrl_ListViewContextItem.cpp
20  * @brief   This is the implementation file for _ListViewContextItem class.
21  *
22  * This file contains the implementation of _ListViewContextItem class.
23  */
24
25 #include <FBaseSysLog.h>
26 #include "FUi_Math.h"
27 #include "FUi_ResourceManager.h"
28 #include "FUiCtrl_ListViewContextItem.h"
29
30 #ifdef MEMORY_LEAK_CHECK
31 #include "mem_leak_check.h"
32 #endif
33
34 using namespace Tizen::Base;
35 using namespace Tizen::Graphics;
36
37 namespace Tizen { namespace Ui { namespace Controls
38 {
39
40 _ListViewContextItem::_ListViewContextItem(float itemHeight)
41         : _TableViewItem(itemHeight)
42         , __itemWidth(0.0f)
43         , __elementYPosition(0.0f)
44 {
45         GET_SHAPE_CONFIG(LISTVIEW::CONTEXTITEM_LEFT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __margin);
46         GET_SHAPE_CONFIG(LISTVIEW::CONTEXTITEM_ELEMENT_SPACING, _CONTROL_ORIENTATION_PORTRAIT, __elementSpacing);
47         GET_SHAPE_CONFIG(LISTVIEW::CONTEXTITEM_ELEMENT_HEIGHT, _CONTROL_ORIENTATION_PORTRAIT, __elementHeight);
48
49         SetBackgroundColor(Color(0, 0, 0, 0));
50 }
51
52 _ListViewContextItem::~_ListViewContextItem(void)
53 {
54         for (int i = __elements.size() - 1; i >= 0; i--)
55         {
56                 _ListViewContextItemElement element = __elements.at(i);
57
58                 DetachChild(*(element.pButton));
59
60                 delete element.pButton;
61                 element.pButton = null;
62
63                 __elements.pop_back();
64         }
65
66         __elements.clear();
67 }
68
69 _ListViewContextItem*
70 _ListViewContextItem::CreateListViewContextItemN(float itemHeight)
71 {
72         _ListViewContextItem* pItem = new (std::nothrow) _ListViewContextItem(itemHeight);
73         SysTryReturn(NID_UI_CTRL, (pItem != null), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
74
75         pItem->AcquireHandle();
76
77         result r = pItem->Initialize();
78         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r = E_SYSTEM, "[E_SYSTEM] Unable to create TableView Item");
79
80         return pItem;
81
82 CATCH:
83         delete pItem;
84
85         return null;
86 }
87
88 bool
89 _ListViewContextItem::AddElement(int elementId, const String& text, bool enable)
90 {
91         if (text.IsEmpty() || HasElement(elementId))
92         {
93                 return false;
94         }
95
96         _ListViewContextItemElement contextElement;
97         memset(&contextElement, 0, sizeof(_ListViewContextItemElement));
98
99         contextElement.elementId = elementId;
100         contextElement.pButton = _Button::CreateButtonN();
101         SysTryReturn(NID_UI_CTRL, (contextElement.pButton != null), false, E_OUT_OF_MEMORY, "E_OUT_OF_MEMORY] The memory is insufficient.");
102
103         __elements.push_back(contextElement);
104
105         AdjustElementLayout(GetBoundsF());
106
107         contextElement.pButton->SetActionId(elementId);
108         contextElement.pButton->SetEnableState(enable);
109         contextElement.pButton->SetText(text);
110
111         AttachChild(*(contextElement.pButton));
112
113         return true;
114 }
115
116 bool
117 _ListViewContextItem::AddElement(int elementId, const Bitmap* pNormalBitmap, const Bitmap* pPressedBitmap,
118                 const Bitmap* pHighlightedBitmap, bool enable)
119 {
120         if ((pNormalBitmap == null) || (pPressedBitmap == null) || HasElement(elementId))
121         {
122                 return false;
123         }
124
125         _ListViewContextItemElement contextElement;
126         memset(&contextElement, 0, sizeof(_ListViewContextItemElement));
127
128         contextElement.elementId = elementId;
129         contextElement.pButton = _Button::CreateButtonN();
130         SysTryReturn(NID_UI_CTRL, (contextElement.pButton != null), false, E_OUT_OF_MEMORY, "E_OUT_OF_MEMORY] The memory is insufficient.");
131
132         __elements.push_back(contextElement);
133
134         AdjustElementLayout(GetBoundsF());
135
136         contextElement.pButton->SetActionId(elementId);
137         contextElement.pButton->SetEnableState(enable);
138         contextElement.pButton->SetBackgroundBitmap(_BUTTON_STATUS_NORMAL, *pNormalBitmap);
139         contextElement.pButton->SetBackgroundBitmap(_BUTTON_STATUS_PRESSED, *pPressedBitmap);
140
141         if (pHighlightedBitmap != null)
142         {
143                 contextElement.pButton->SetBackgroundBitmap(_BUTTON_STATUS_HIGHLIGHTED, *pHighlightedBitmap);
144         }
145
146         AttachChild(*(contextElement.pButton));
147
148         return true;
149 }
150
151 bool
152 _ListViewContextItem::AddElement(int elementId, const String& text, const Bitmap* pNormalBitmap, const Bitmap* pPressedBitmap,
153                 const Bitmap* pHighlightedBitmap, bool enable)
154 {
155         bool ret = AddElement(elementId, pNormalBitmap, pPressedBitmap, pHighlightedBitmap, enable);
156
157         _ListViewContextItemElement* pElement = GetElement(elementId);
158
159         if ((ret) && (pElement != null))
160         {
161                 pElement->pButton->SetMargin(0.0f, 0.0f, 0.0f, 0.0f);
162                 pElement->pButton->SetText(text);
163
164                 return true;
165         }
166
167         return false;
168 }
169
170 bool
171 _ListViewContextItem::SetElementTextColor(int elementId, ListViewContextItemElementDrawingStatus status, const Color& textColor)
172 {
173         result r = E_SYSTEM;
174         _ListViewContextItemElement* pElement = GetElement(elementId);
175
176         if (pElement != null)
177         {
178                 r = pElement->pButton->SetTextColor(GetButtonDrawingStatus(status), textColor);
179         }
180
181         return (r == E_SUCCESS);
182 }
183
184 Color
185 _ListViewContextItem::GetElementTextColor(int elementId, ListViewContextItemElementDrawingStatus status) const
186 {
187         _ListViewContextItemElement* pElement = GetElement(elementId);
188
189         if (pElement != null)
190         {
191                 return pElement->pButton->GetTextColor(GetButtonDrawingStatus(status));
192         }
193
194         return Color(0);
195 }
196
197 bool
198 _ListViewContextItem::SetElementBackgroundColor(int elementId, ListViewContextItemElementDrawingStatus status, const Color& backgroundColor)
199 {
200         result r = E_SYSTEM;
201         _ListViewContextItemElement* pElement = GetElement(elementId);
202
203         if (pElement != null)
204         {
205                 r = pElement->pButton->SetColor(GetButtonDrawingStatus(status), backgroundColor);
206         }
207
208         return (r == E_SUCCESS);
209 }
210
211 Color
212 _ListViewContextItem::GetElementBackgroundColor(int elementId, ListViewContextItemElementDrawingStatus status) const
213 {
214         _ListViewContextItemElement* pElement = GetElement(elementId);
215
216         if (pElement != null)
217         {
218                 return pElement->pButton->GetColor(GetButtonDrawingStatus(status));
219         }
220
221         return Color(0);
222 }
223
224 _ButtonStatus
225 _ListViewContextItem::GetButtonDrawingStatus(ListViewContextItemElementDrawingStatus status) const
226 {
227         _ButtonStatus buttonStatus;
228
229         switch (status)
230         {
231         case LISTVIEW_CONTEXTITEM_ELEMENT_STATUS_NORMAL:
232                 buttonStatus = _BUTTON_STATUS_NORMAL;
233                 break;
234
235         case LISTVIEW_CONTEXTITEM_ELEMENT_STATUS_PRESSED:
236                 buttonStatus = _BUTTON_STATUS_PRESSED;
237                 break;
238
239         case LISTVIEW_CONTEXTITEM_ELEMENT_STATUS_HIGHLIGHTED:
240                 buttonStatus = _BUTTON_STATUS_HIGHLIGHTED;
241                 break;
242
243         default:
244                 buttonStatus = _BUTTON_STATUS_NORMAL;
245                 break;
246         }
247
248         return buttonStatus;
249 }
250
251 void
252 _ListViewContextItem::AdjustElementLayout(FloatRectangle bounds)
253 {
254         int elementCount = __elements.size();
255         FloatRectangle elementBounds;
256
257         if ((elementCount <= 0) || (bounds.width <= 0.0f) || (bounds.height <= 0.0f))
258         {
259                 return;
260         }
261
262         // Calculate horizontal value
263         if (elementCount == 1)
264         {
265                 float margin = 0.0f;
266                 GET_SHAPE_CONFIG(LISTVIEW::CONTEXTITEM_LEFT_MARGIN_ONE, _CONTROL_ORIENTATION_PORTRAIT, margin);
267
268                 elementBounds.x = __margin + margin;
269                 elementBounds.width = (bounds.width - ((__margin + margin) * 2.0f));
270         }
271         else
272         {
273                 elementBounds.x = __margin;
274                 elementBounds.width = ((bounds.width - __margin * 2.0f) - (__elementSpacing * (elementCount - 1))) / elementCount;
275         }
276
277         // Calculate vertical value
278         elementBounds.y = (bounds.height - __elementHeight) / 2.0f;
279         elementBounds.height = __elementHeight;
280         __elementYPosition = elementBounds.y;
281
282         _LinkedList <_ListViewContextItemElement>::_Iterator iter;
283
284         for (iter = __elements.begin(); iter != __elements.end(); iter++)
285         {
286                 if (iter->pButton != null)
287                 {
288                         iter->pButton->SetBounds(elementBounds);
289                 }
290
291                 elementBounds.x += (elementBounds.width + __elementSpacing);
292         }
293 }
294
295 _ListViewContextItemElement*
296 _ListViewContextItem::GetElement(int elementId) const
297 {
298         _LinkedList <_ListViewContextItemElement>::_Iterator iter;
299
300         for (iter = __elements.begin(); iter != __elements.end(); iter++)
301         {
302                 if (iter->elementId == elementId)
303                 {
304                         return &(*iter);
305                 }
306         }
307
308         return null;
309 }
310
311 bool
312 _ListViewContextItem::HasElement(int elementId)
313 {
314         _ListViewContextItemElement* pElement = GetElement(elementId);
315
316         return (pElement != null);
317 }
318
319 int
320 _ListViewContextItem::GetElementIdFromPosition(const FloatPoint& position) const
321 {
322         _LinkedList <_ListViewContextItemElement>::_ReverseIterator reverseIter;
323
324         for (reverseIter = __elements.rbegin(); reverseIter != __elements.rend(); reverseIter++)
325         {
326                 if ((*reverseIter).pButton->GetBoundsF().Contains(position))
327                 {
328                         return (*reverseIter).elementId;
329                 }
330         }
331
332         return -1;
333 }
334
335 void
336 _ListViewContextItem::AddContextItemEventListener(_IActionEventListener& listener)
337 {
338         if (__elements.size() > 0)
339         {
340                 _LinkedList <_ListViewContextItemElement>::_Iterator iter;
341
342                 for (iter = __elements.begin(); iter != __elements.end(); iter++)
343                 {
344                         iter->pButton->AddActionEventListener(listener);
345                 }
346         }
347 }
348
349 void
350 _ListViewContextItem::RemoveContextItemEventListener(_IActionEventListener& listener)
351 {
352         if (__elements.size() > 0)
353         {
354                 _LinkedList <_ListViewContextItemElement>::_Iterator iter;
355
356                 for (iter = __elements.begin(); iter != __elements.end(); iter++)
357                 {
358                         iter->pButton->RemoveActionEventListener(listener);
359                 }
360         }
361 }
362
363 void
364 _ListViewContextItem::AdjustItemBounds(FloatRectangle& bounds)
365 {
366         if (!_FloatCompare(bounds.width, __itemWidth) || !_FloatCompare(GetSizeF().height, bounds.height))
367         {
368                 AdjustElementLayout(FloatRectangle(0.0f, 0.0f, bounds.width, bounds.height));
369                 __itemWidth = bounds.width;
370         }
371 }
372
373 result
374 _ListViewContextItem::OnBoundsChanging(const FloatRectangle& bounds)
375 {
376         float elementYPosition = (bounds.height - __elementHeight) / 2.0f;
377         if (!_FloatCompare(GetSizeF().height, bounds.height) || !_FloatCompare(__elementYPosition, elementYPosition))
378         {
379                 AdjustElementLayout(FloatRectangle(0.0f, 0.0f, __itemWidth, bounds.height));
380         }
381
382         return E_SUCCESS;
383 }
384
385 void
386 _ListViewContextItem::OnFontChanged(Font* pFont)
387 {
388         String fontName = GetFont();
389
390         _LinkedList <_ListViewContextItemElement>::_Iterator iter;
391
392         for (iter = __elements.begin(); iter != __elements.end(); iter++)
393         {
394                 iter->pButton->SetFont(fontName);
395         }
396 }
397
398 }}} // Tizen::Ui::Controls