add patch
[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 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         r = pTextObject->AppendElement(*pSimpleText);
105         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
106
107         r = pTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_CENTER | TEXT_OBJECT_ALIGNMENT_MIDDLE);
108         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
109
110         r = pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
111         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
112
113         r = pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
114         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
115
116         r = pTextObject->SetFont(__pFont, 0, pTextObject->GetTextLength());
117         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
118
119         if (__pTextObject != null)
120         {
121                 r = __pTextObject->RemoveAll();
122                 SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
123
124                 delete __pTextObject;
125                 __pTextObject = null;
126         }
127         __pTextObject = pTextObject;
128
129         return true;
130
131 CATCH:
132         pTextObject->RemoveAll();
133
134         delete pTextObject;
135         delete pSimpleText;
136
137         return false;
138 }
139
140 const String&
141 _DateTimeDisplayBox::GetText(void) const
142 {
143         return __text;
144 }
145
146 void
147 _DateTimeDisplayBox::DrawDisplayBox(Canvas& canvas, DateTimePickerStatus status)
148 {
149         if ((!__focusable) && (status == DATETIME_STATUS_HIGHLIGHTED))
150         {
151                 status = DATETIME_STATUS_NORMAL;
152         }
153
154         DrawBackground(canvas, status);
155         DrawText(canvas, status);
156
157         return;
158 }
159
160 void
161 _DateTimeDisplayBox::DrawBackground(Canvas& canvas, DateTimePickerStatus status)
162 {
163         result r = E_SUCCESS;
164         bool isCustomBitmap[DATETIME_STATUS_MAX];
165
166         if (__pBackgroundNormalBitmap[status] == null)
167         {
168                 return;
169         }
170
171         for (int i = 0; i < DATETIME_STATUS_MAX; i++)
172         {
173                 isCustomBitmap[i] = false;
174         }
175
176         if (__pBackgroundNormalBitmap[status] != null)
177         {
178                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pBackgroundNormalBitmap[status]))
179                 {
180                         r = canvas.DrawNinePatchedBitmap(__windowBounds, *__pBackgroundNormalBitmap[status]);
181                         SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
182                 }
183                 else
184                 {
185                         r = canvas.DrawBitmap(__windowBounds, *__pBackgroundNormalBitmap[status]);
186                         SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
187                 }
188         }
189
190         if (__boxId == DATETIME_ID_AMPM)
191         {
192                 isCustomBitmap[DATETIME_STATUS_NORMAL] = IS_CUSTOM_BITMAP(DATETIMEPICKER::AMPM_BG_NORMAL);
193                 isCustomBitmap[DATETIME_STATUS_SELECTED] = IS_CUSTOM_BITMAP(DATETIMEPICKER::AMPM_BG_PRESSED);
194                 isCustomBitmap[DATETIME_STATUS_HIGHLIGHTED] = IS_CUSTOM_BITMAP(DATETIMEPICKER::AMPM_BG_HIGHLIGHTED);
195                 isCustomBitmap[DATETIME_STATUS_DISABLED] = IS_CUSTOM_BITMAP(DATETIMEPICKER::AMPM_BG_DISABLED);
196         }
197         else
198         {
199                 isCustomBitmap[DATETIME_STATUS_NORMAL] = IS_CUSTOM_BITMAP(DATETIMEPICKER::CONTENT_BG_NORMAL);
200                 isCustomBitmap[DATETIME_STATUS_SELECTED] = IS_CUSTOM_BITMAP(DATETIMEPICKER::CONTENT_BG_PRESSED);
201                 isCustomBitmap[DATETIME_STATUS_HIGHLIGHTED] = IS_CUSTOM_BITMAP(DATETIMEPICKER::CONTENT_BG_HIGHLIGHTED);
202                 isCustomBitmap[DATETIME_STATUS_DISABLED] = IS_CUSTOM_BITMAP(DATETIMEPICKER::CONTENT_BG_DISABLED);
203         }
204
205         if (!isCustomBitmap[status] && __pBackgroundEffectBitmap[status] != null)
206         {
207                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pBackgroundEffectBitmap[status]))
208                 {
209                         r = canvas.DrawNinePatchedBitmap(__windowBounds, *__pBackgroundEffectBitmap[status]);
210                         SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
211                 }
212                 else
213                 {
214                         r = canvas.DrawBitmap(__windowBounds, *__pBackgroundEffectBitmap[status]);
215                         SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
216                 }
217         }
218
219         return;
220 }
221
222 void
223 _DateTimeDisplayBox::DrawText(Canvas& canvas, DateTimePickerStatus status)
224 {
225         SysTryReturnVoidResult(NID_UI_CTRL, (__pTextObject != null), E_SYSTEM,
226                                                    "[E_SYSTEM] A system error has occurred. The instance of TextObject is null.");
227
228         result r = E_SUCCESS;
229
230         r = __pTextObject->SetForegroundColor(__textColor[status], 0, __pTextObject->GetTextLength());
231         SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
232
233         r = __pTextObject->SetBounds(__windowBounds);
234         SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
235
236         r = __pTextObject->Draw(*_CanvasImpl::GetInstance(canvas));
237         SysTryReturnVoidResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
238
239         return;
240 }
241
242 void
243 _DateTimeDisplayBox::SetFocusable(bool focusable)
244 {
245         __focusable = focusable;
246
247         return;
248 }
249
250 void
251 _DateTimeDisplayBox::SetBackgroundBitmap(Bitmap **pBackgroundNormalBitmap, Bitmap **pBackgroundEffectBitmap)
252 {
253         GET_COLOR_CONFIG(DATETIMEPICKER::AMPM_TEXT_NORMAL, __textColor[DATETIME_STATUS_NORMAL]);
254         GET_COLOR_CONFIG(DATETIMEPICKER::AMPM_TEXT_PRESSED, __textColor[DATETIME_STATUS_SELECTED]);
255         GET_COLOR_CONFIG(DATETIMEPICKER::AMPM_TEXT_HIGHLIGHTED, __textColor[DATETIME_STATUS_HIGHLIGHTED]);
256         GET_COLOR_CONFIG(DATETIMEPICKER::AMPM_TEXT_DISABLED, __textColor[DATETIME_STATUS_DISABLED]);
257
258         for (int i = 0; i < DATETIME_STATUS_MAX; i++)
259         {
260                 __pBackgroundNormalBitmap[i] = pBackgroundNormalBitmap[i];
261                 __pBackgroundEffectBitmap[i] = pBackgroundEffectBitmap[i];
262         }
263
264         return;
265 }
266
267 void
268 _DateTimeDisplayBox::UpdateDisplayBoxBounds(FloatRectangle& bounds)
269 {
270         __windowBounds = bounds;
271
272         return;
273 }
274
275 void
276 _DateTimeDisplayBox::SetFont(Font* pFont)
277 {
278         __pFont = pFont;
279
280         return;
281 }
282
283 }}} // Tizen::Ui::Controls