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