Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_DateTimeDisplayBox.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_DateTimeDisplayBox.cpp
19  * @brief               This is the implementation file for the _DateTimeDisplayBox class.
20  */
21
22 #include <FBaseErrorDefine.h>
23 #include <FBaseSysLog.h>
24 #include <FGrpCanvas.h>
25 #include <FGrp_TextCommon.h>
26 #include <FGrp_TextTextObject.h>
27 #include <FGrp_TextTextSimple.h>
28 #include <FGrp_CanvasImpl.h>
29 #include <FGrp_BitmapImpl.h>
30 #include "FUi_ResourceManager.h"
31 #include "FUiAnim_VisualElement.h"
32 #include "FUiCtrl_DateTimeDisplayBox.h"
33 #include "FUiCtrl_DateTimeDefine.h"
34
35 using namespace Tizen::Base;
36 using namespace Tizen::Graphics;
37 using namespace Tizen::Graphics::_Text;
38
39 namespace Tizen { namespace Ui { namespace Controls
40 {
41
42 _DateTimeDisplayBox::_DateTimeDisplayBox(const Rectangle& bounds, int boxId)
43         : __windowBounds(bounds)
44         , __boxId(boxId)
45         , __pTextObject(null)
46         , __text()
47         , __focusable(true)
48         , __pFont(null)
49         , __pBackgroundEffectBitmap(null)
50         , __pBackgroundPressedEffectBitmap(null)
51         , __pBackgroundDisabledEffectBitmap(null)
52 {
53         for (int i = 0; i < DATETIME_STATUS_MAX; i++)
54         {
55                 __pBackgroundNormalBitmap[i] = null;
56         }
57
58         for (int i = 0; i < DATETIME_STATUS_MAX; i++)
59         {
60                 __backgroundColor[i] = Color();
61         }
62
63         GET_COLOR_CONFIG(DATETIMEPICKER::TEXT_NORMAL, __textColor[DATETIME_STATUS_NORMAL]);
64         GET_COLOR_CONFIG(DATETIMEPICKER::TEXT_HIGHLIGHTED, __textColor[DATETIME_STATUS_FOCUSED]);
65         GET_COLOR_CONFIG(DATETIMEPICKER::TEXT_PRESSED, __textColor[DATETIME_STATUS_SELECTED]);
66         GET_COLOR_CONFIG(DATETIMEPICKER::TEXT_DISABLED, __textColor[DATETIME_STATUS_DISABLED]);
67 }
68
69 _DateTimeDisplayBox::~_DateTimeDisplayBox(void)
70 {
71         if (__pTextObject != null)
72         {
73                 __pTextObject->RemoveAll();
74                 delete __pTextObject;
75                 __pTextObject = null;
76         }
77
78         for (int i = 0; i < DATETIME_STATUS_MAX; i++)
79         {
80                 delete __pBackgroundNormalBitmap[i];
81                 __pBackgroundNormalBitmap[i] = null;
82         }
83 }
84
85 Rectangle
86 _DateTimeDisplayBox::GetDisplayBoxBounds(void) const
87 {
88         return __windowBounds;
89 }
90
91 int
92 _DateTimeDisplayBox::GetDisplayBoxId(void) const
93 {
94         return __boxId;
95 }
96
97 bool
98 _DateTimeDisplayBox::SetText(const String& text)
99 {
100         result r = E_SUCCESS;
101         TextSimple* pSimpleText = null;
102
103         TextObject * pTextObject = new (std::nothrow) TextObject;
104         SysTryReturn(NID_UI_CTRL, (pTextObject != null), false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
105
106         __text.Clear();
107         __text = text;
108         wchar_t* pString = const_cast <wchar_t*>(__text.GetPointer());
109
110         r = pTextObject->Construct();
111         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
112
113         pSimpleText = new (std::nothrow)TextSimple(pString, __text.GetLength(), TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
114         SysTryCatch(NID_UI_CTRL, (pSimpleText != null), , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
115
116         r = pTextObject->AppendElement(*pSimpleText);
117         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
118
119         r = pTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_CENTER | TEXT_OBJECT_ALIGNMENT_MIDDLE);
120         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
121
122         r = pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
123         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
124
125         r = pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
126         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
127
128         r = pTextObject->SetFont(__pFont, 0, pTextObject->GetTextLength());
129         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
130
131         if (__pTextObject != null)
132         {
133                 r = __pTextObject->RemoveAll();
134                 SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
135
136                 delete __pTextObject;
137                 __pTextObject = null;
138         }
139         __pTextObject = pTextObject;
140
141         return true;
142
143 CATCH:
144         pTextObject->RemoveAll();
145
146         delete pTextObject;
147         delete pSimpleText;
148
149         return false;
150 }
151
152 const String&
153 _DateTimeDisplayBox::GetText(void) const
154 {
155         return __text;
156 }
157
158 void
159 _DateTimeDisplayBox::DrawDisplayBox(Canvas& canvas, DateTimePickerStatus status)
160 {
161         if ((__focusable == false) && (status == DATETIME_STATUS_FOCUSED))
162         {
163                 status = DATETIME_STATUS_NORMAL;
164         }
165
166         DrawBackground(canvas, status);
167         DrawText(canvas, status);
168
169         return;
170 }
171
172 void
173 _DateTimeDisplayBox::DrawBackground(Canvas& canvas, DateTimePickerStatus status)
174 {
175         result r = E_SUCCESS;
176
177         if (__pBackgroundNormalBitmap[status] == null)
178         {
179                 return;
180         }
181
182         if (__pBackgroundNormalBitmap[status]->IsNinePatchedBitmap() == true)
183         {
184                 r = canvas.DrawNinePatchedBitmap(__windowBounds, *__pBackgroundNormalBitmap[status]);
185                 SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
186         }
187         else
188         {
189                 r = canvas.DrawBitmap(__windowBounds, *__pBackgroundNormalBitmap[status]);
190                 SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
191         }
192
193         if (status == DATETIME_STATUS_FOCUSED && __pBackgroundPressedEffectBitmap != null )
194         {
195                 if (__pBackgroundEffectBitmap->IsNinePatchedBitmap() == true)
196                 {
197                         r = canvas.DrawNinePatchedBitmap(__windowBounds, *__pBackgroundPressedEffectBitmap);
198                         SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
199                 }
200                 else
201                 {
202                         r = canvas.DrawBitmap(__windowBounds, *__pBackgroundPressedEffectBitmap);
203                         SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
204                 }
205         }
206         else if (status == DATETIME_STATUS_DISABLED && __pBackgroundDisabledEffectBitmap != null)
207         {
208                 if (__pBackgroundDisabledEffectBitmap->IsNinePatchedBitmap() == true)
209                 {
210                         r = canvas.DrawNinePatchedBitmap(__windowBounds, *__pBackgroundDisabledEffectBitmap);
211                         SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
212                 }
213                 else
214                 {
215                         r = canvas.DrawBitmap(__windowBounds, *__pBackgroundDisabledEffectBitmap);
216                         SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
217                 }
218         }
219         else if (__pBackgroundEffectBitmap != null)
220         {
221                 if (__pBackgroundEffectBitmap->IsNinePatchedBitmap() == true)
222                 {
223                         r = canvas.DrawNinePatchedBitmap(__windowBounds, *__pBackgroundEffectBitmap);
224                         SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
225                 }
226                 else
227                 {
228                         r = canvas.DrawBitmap(__windowBounds, *__pBackgroundEffectBitmap);
229                         SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
230                 }
231         }
232
233         return;
234 }
235
236 void
237 _DateTimeDisplayBox::DrawText(Canvas& canvas, DateTimePickerStatus status)
238 {
239         SysTryReturnVoidResult(NID_UI_CTRL, (__pTextObject != null), E_SYSTEM,
240                                                    "[E_SYSTEM] A system error has occurred. The instance of TextObject is null.");
241
242         result r = E_SUCCESS;
243
244         r = __pTextObject->SetForegroundColor(__textColor[status], 0, __pTextObject->GetTextLength());
245         SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
246
247         r = __pTextObject->SetBounds(__windowBounds);
248         SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
249
250         r = __pTextObject->Draw(*_CanvasImpl::GetInstance(canvas));
251         SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
252
253         return;
254 }
255
256 void
257 _DateTimeDisplayBox::SetFocusable(bool focusable)
258 {
259         __focusable = focusable;
260
261         return;
262 }
263
264 void
265 _DateTimeDisplayBox::SetBackgroundBitmap(Bitmap* pNormalBitmap, Bitmap* pEffectBitmap, Bitmap* pPressedEffectBitmap, Bitmap* pDisabledEffectBitmap)
266 {
267         result r = E_SUCCESS;
268
269         GET_COLOR_CONFIG(DATETIMEPICKER::AMPM_BG_NORMAL, __backgroundColor[DATETIME_STATUS_NORMAL]);
270         GET_COLOR_CONFIG(DATETIMEPICKER::AMPM_BG_PRESSED, __backgroundColor[DATETIME_STATUS_SELECTED]);
271         GET_COLOR_CONFIG(DATETIMEPICKER::AMPM_BG_HIGHLIGHTED, __backgroundColor[DATETIME_STATUS_FOCUSED]);
272         GET_COLOR_CONFIG(DATETIMEPICKER::AMPM_BG_DISABLED, __backgroundColor[DATETIME_STATUS_DISABLED]);
273
274         GET_COLOR_CONFIG(DATETIMEPICKER::AMPM_TEXT_NORMAL, __textColor[DATETIME_STATUS_NORMAL]);
275         GET_COLOR_CONFIG(DATETIMEPICKER::AMPM_TEXT_PRESSED, __textColor[DATETIME_STATUS_SELECTED]);
276         GET_COLOR_CONFIG(DATETIMEPICKER::AMPM_TEXT_HIGHLIGHTED, __textColor[DATETIME_STATUS_FOCUSED]);
277         GET_COLOR_CONFIG(DATETIMEPICKER::AMPM_TEXT_DISABLED, __textColor[DATETIME_STATUS_DISABLED]);
278
279         for (int i = 0; i < DATETIME_STATUS_MAX; i++)
280         {
281                 __pBackgroundNormalBitmap[i] = _BitmapImpl::GetColorReplacedBitmapN(*pNormalBitmap, Color::GetColor(COLOR_ID_MAGENTA), __backgroundColor[i]);
282                 r = GetLastResult();
283                 SysTryCatch(NID_UI_CTRL, (__pBackgroundNormalBitmap[i] != null), , r, "[%s] Propagating.", GetErrorMessage(r));
284         }
285
286         __pBackgroundEffectBitmap = pEffectBitmap;
287         __pBackgroundPressedEffectBitmap = pPressedEffectBitmap;
288         __pBackgroundDisabledEffectBitmap = pDisabledEffectBitmap;
289
290         return;
291
292 CATCH:
293         for (int i = 0; i < DATETIME_STATUS_MAX; i++)
294         {
295                 delete __pBackgroundNormalBitmap[i];
296                 __pBackgroundNormalBitmap[i] = null;
297         }
298
299         return;
300 }
301
302 void
303 _DateTimeDisplayBox::UpdateDisplayBoxBounds(Rectangle& bounds)
304 {
305         __windowBounds = bounds;
306
307         return;
308 }
309
310 void
311 _DateTimeDisplayBox::SetFont(Font* pFont)
312 {
313         __pFont = pFont;
314
315         return;
316 }
317
318 }}} // Tizen::Ui::Controls