Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_ContextMenuItem.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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  * @file                FUiCtrl_ContextMenuItem.cpp
19  * @brief               This is the implementation file for the _ContextMenuItem class.
20  */
21
22 #include <FBaseSysLog.h>
23 #include <FGrp_BitmapImpl.h>
24 #include "FUi_ResourceManager.h"
25 #include "FUiCtrl_ContextMenuItem.h"
26
27
28 using namespace Tizen::Base;
29 using namespace Tizen::Graphics;
30 using namespace Tizen::Ui;
31
32 namespace Tizen { namespace Ui { namespace Controls
33 {
34
35 _ContextMenuItem::_ContextMenuItem(void)
36         : __type(CONTEXT_MENU_ITEM_DRAWING_TYPE_NONE)
37         , __actionId(-1)
38         , __divider(false)
39         , __selected(false)
40         , __parentScrollEnable(false)
41         , __textSize(0)
42         , __text(L"")
43         , __size(Dimension(0, 0))
44         , __drawRect(Rectangle(0, 0, 0, 0))
45         , __pBitmapLabel(null)
46         , __pTextLabel(null)
47         , __pDividerLineLabel1(null)
48         , __pDividerLineLabel2(null)
49 {
50         __pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_NORMAL] = null;
51         __pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_PRESSED] = null;
52         __pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_HIGHLIGHTED] = null;
53 }
54
55 _ContextMenuItem::~_ContextMenuItem(void)
56 {
57         if (__pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_NORMAL] != null)
58         {
59                 delete __pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_NORMAL];
60                 __pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_NORMAL] = null;
61         }
62
63         if (__pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_PRESSED] != null)
64         {
65                 delete __pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_PRESSED];
66                 __pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_PRESSED] = null;
67         }
68
69         if (__pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_HIGHLIGHTED] != null)
70         {
71                 delete __pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_HIGHLIGHTED];
72                 __pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_HIGHLIGHTED] = null;
73         }
74 }
75
76 _ContextMenuItem*
77 _ContextMenuItem::CreateContextMenuItemN(void)
78 {
79         ClearLastResult();
80
81         _ContextMenuItem* pItem = null;
82
83         pItem = new (std::nothrow) _ContextMenuItem();
84         if (pItem == null)
85         {
86                 SetLastResult(E_OUT_OF_MEMORY);
87                 return null;
88         }
89
90         pItem->AcquireHandle();
91
92         SetLastResult(E_SUCCESS);
93         return pItem;
94 }
95
96 void
97 _ContextMenuItem::SetBitmapLabel(_Label* pLabel)
98 {
99         __pBitmapLabel = pLabel;
100 }
101
102 void
103 _ContextMenuItem::SetTextLabel(_Label* pLabel)
104 {
105         __pTextLabel = pLabel;
106 }
107
108
109 void
110 _ContextMenuItem::SetType(ContextMenuItemDrawingType type)
111 {
112         __type = type;
113 }
114
115 ContextMenuItemDrawingType
116 _ContextMenuItem::GetType(void) const
117 {
118         return __type;
119 }
120
121 void
122 _ContextMenuItem::SetActionId(int actionId)
123 {
124         __actionId = actionId;
125 }
126
127 int
128 _ContextMenuItem::GetActionId(void) const
129 {
130         return __actionId;
131 }
132
133 void
134 _ContextMenuItem::SetDivider(bool drawDivider)
135 {
136         __divider = drawDivider;
137 }
138 bool
139 _ContextMenuItem::GetDivider(void) const
140 {
141         return __divider;
142 }
143
144 void
145 _ContextMenuItem::SetTextSize(int size)
146 {
147         __textSize = size;
148 }
149
150 result
151 _ContextMenuItem::SetText(const Tizen::Base::String& text)
152 {
153         if (text.GetLength() <= 0)
154         {
155                 return E_INVALID_ARG;
156         }
157
158         __text = text;
159
160         return E_SUCCESS;
161 }
162
163 const Tizen::Base::String&
164 _ContextMenuItem::GetText(void) const
165 {
166         return __text;
167 }
168
169 result
170 _ContextMenuItem::SetBitmap(ContextMenuItemDrawingStatus status, const Tizen::Graphics::Bitmap* pBitmap)
171 {
172         if (status < CONTEXT_MENU_ITEM_DRAWING_STATUS_NORMAL || status > CONTEXT_MENU_ITEM_DRAWING_STATUS_HIGHLIGHTED)
173         {
174                 return E_INVALID_ARG;
175         }
176
177         SysTryReturn(NID_UI_CTRL, (status != CONTEXT_MENU_ITEM_DRAWING_STATUS_NORMAL || pBitmap != null), E_INVALID_ARG,
178                                 E_INVALID_ARG,
179                                 "[E_INVALID_ARG] The normal bitmap must not be null.");
180
181         Bitmap* pClonedBitmap = _BitmapImpl::CloneN(*pBitmap);
182
183         // If bitmap is in _ContextMenuItem, delete old one.
184         if (__pBitmap[status] != null)
185         {
186                 delete __pBitmap[status];
187                 __pBitmap[status] = null;
188         }
189
190         __pBitmap[status] = pClonedBitmap;
191
192         return E_SUCCESS;
193 }
194
195 const Tizen::Graphics::Bitmap*
196 _ContextMenuItem::GetBitmap(ContextMenuItemDrawingStatus status) const
197 {
198         if (status < CONTEXT_MENU_ITEM_DRAWING_STATUS_NORMAL || status > CONTEXT_MENU_ITEM_DRAWING_STATUS_HIGHLIGHTED)
199         {
200                 return null;
201         }
202
203         return __pBitmap[status];
204 }
205
206 void
207 _ContextMenuItem::SetSize(Tizen::Graphics::Dimension size)
208 {
209         __size = size;
210 }
211
212 Tizen::Graphics::Dimension
213 _ContextMenuItem::GetSize(void) const
214 {
215         return __size;
216 }
217
218 void
219 _ContextMenuItem::SetDrawRect(Tizen::Graphics::Rectangle rect)
220 {
221         __drawRect = rect;
222
223 }
224
225 Tizen::Graphics::Rectangle
226 _ContextMenuItem::GetDrawRect(void) const
227 {
228         return __drawRect;
229 }
230
231
232 int
233 _ContextMenuItem::Release(void)
234 {
235         delete this;
236
237         return 0;
238 }
239
240 void
241 _ContextMenuItem::OnDraw(void)
242 {
243         DrawItem();
244 }
245
246
247 void
248 _ContextMenuItem::DrawItem(void)
249 {
250         if(__selected)
251         {
252                 Color selectedBgColor;
253                 GET_COLOR_CONFIG(CONTEXTMENU::ITEM_BG_PRESSED, selectedBgColor);
254                 SetBackgroundColor(selectedBgColor);
255         }
256         else
257         {
258                 SetBackgroundColor(Color(0, 0, 0, 0));
259         }
260
261         DrawItemDivider();
262 }
263
264 void
265 _ContextMenuItem::DrawItemDivider(void)
266 {
267                 int leftMargin, rightMargin;
268                 GET_SHAPE_CONFIG(CONTEXTMENU::LIST_LEFT_MARGIN,  _CONTROL_ORIENTATION_PORTRAIT, leftMargin);
269                 GET_SHAPE_CONFIG(CONTEXTMENU::LIST_RIGHT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, rightMargin);
270         if (__pDividerLineLabel1 == null)
271         {
272                 __pDividerLineLabel1 = _Label::CreateLabelN();
273                 SysTryReturnVoidResult(NID_UI_CTRL, __pDividerLineLabel1, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
274
275                 Rectangle bounds = GetBounds();
276
277                 __pDividerLineLabel1->SetBounds(Rectangle(leftMargin, bounds.height - 2, bounds.width - leftMargin - rightMargin, 1));
278
279                 Color dividerLineColor;
280                 GET_COLOR_CONFIG(CONTEXTMENU::LIST_ITEM_DIVIDER1, dividerLineColor);
281                 __pDividerLineLabel1->SetBackgroundColor(dividerLineColor);
282
283                 AttachChild(*__pDividerLineLabel1);
284         }
285
286         __pDividerLineLabel1->Invalidate();
287
288         if (__pDividerLineLabel2 == null)
289         {
290                 __pDividerLineLabel2 = _Label::CreateLabelN();
291                 SysTryReturnVoidResult(NID_UI_CTRL, __pDividerLineLabel2, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
292
293                 Rectangle bounds = GetBounds();
294
295                 __pDividerLineLabel2->SetBounds(Rectangle(leftMargin, bounds.height - 1, bounds.width - leftMargin - rightMargin, 1));
296
297                 Color dividerLineColor;
298                 GET_COLOR_CONFIG(CONTEXTMENU::LIST_ITEM_DIVIDER2, dividerLineColor);
299                 __pDividerLineLabel2->SetBackgroundColor(dividerLineColor);
300
301                 AttachChild(*__pDividerLineLabel2);
302         }
303
304         __pDividerLineLabel2->Invalidate();
305 }
306
307 _UiTouchEventDelivery
308 _ContextMenuItem::OnPreviewTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
309 {
310         return _UI_TOUCH_EVENT_DELIVERY_FORCED_YES;
311 }
312
313 _UiTouchEventDelivery
314 _ContextMenuItem::OnPreviewTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
315 {
316         return _UI_TOUCH_EVENT_DELIVERY_FORCED_YES;
317 }
318
319 _UiTouchEventDelivery
320 _ContextMenuItem::OnPreviewTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
321 {
322         return _UI_TOUCH_EVENT_DELIVERY_FORCED_YES;
323 }
324
325 bool
326 _ContextMenuItem::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
327 {
328         __selected = true;
329         __pressed = true;
330         Invalidate();
331
332         return false;
333 }
334
335 bool
336 _ContextMenuItem::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
337 {
338         Reset();
339
340         Invalidate();
341
342         return false;
343 }
344
345 bool
346 _ContextMenuItem::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
347 {
348         if (__pressed)
349         {
350                 if (__parentScrollEnable)
351                 {
352                         if (__selected)
353                         {
354                                 __selected = false;
355                                 Invalidate();
356                         }
357                 }
358                 else
359                 {
360                         int oldSelected = __selected;
361
362                         Rectangle bounds(0, 0, __size.width, __size.height);
363                         if (bounds.Contains(touchinfo.GetCurrentPosition()))
364                         {
365                                 __selected = true;
366                         }
367                         else
368                         {
369                                 __selected = false;
370                         }
371
372                         if (oldSelected != __selected)
373                         {
374                                 Invalidate();
375                         }
376                 }
377         }
378         return false;
379 }
380
381 void
382 _ContextMenuItem::OnFontChanged(Tizen::Graphics::Font* pFont)
383 {
384         String fontName = _Control::GetFont();
385
386         if (__pTextLabel != null)
387         {
388                 __pTextLabel->SetFont(fontName);
389         }
390 }
391
392 void
393 _ContextMenuItem::OnFontInfoRequested(unsigned long& style, int& size)
394 {
395         style = FONT_STYLE_PLAIN;
396         size = __textSize;
397 }
398
399 void
400 _ContextMenuItem::SetParentScrollEnable(bool enable)
401 {
402         __parentScrollEnable = enable;
403 }
404
405 bool
406 _ContextMenuItem::GetParentScrollEnable() const
407 {
408         return __parentScrollEnable;
409 }
410
411 bool
412 _ContextMenuItem::IsSelected() const
413 {
414         return __selected;
415 }
416
417 void
418 _ContextMenuItem::Reset()
419 {
420         __selected = false;
421         __pressed = false;
422 }
423
424 }}} // Tizen::Ui::Controls