Fork for IVI: mesa fixing
[profile/ivi/uifw.git] / src / ui / controls / FUiCtrl_EditDateImpl.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 /**
19  * @file                FUiCtrl_EditDateImpl.cpp
20  * @brief               This is the implementation file for the _EditDateImpl class.
21  */
22
23 #include <FBaseSysLog.h>
24 #include "FUi_UiBuilder.h"
25 #include "FUi_ResourceManager.h"
26 #include "FUi_ResourceSizeInfo.h"
27 #include "FUiCtrl_EditDateImpl.h"
28 #include "FUiCtrl_DateTimeDefine.h"
29
30 using namespace Tizen::Graphics;
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Runtime;
33 using namespace Tizen::Ui;
34
35 namespace Tizen { namespace Ui { namespace Controls
36 {
37 _EditDateImpl::EditDateSizeInfo::EditDateSizeInfo(void)
38         : __isTitle(false)
39 {
40 }
41
42 void
43 _EditDateImpl::EditDateSizeInfo::SetTitleStyle(bool titleStyle)
44 {
45         __isTitle = titleStyle;
46         return;
47 }
48
49 Dimension
50 _EditDateImpl::EditDateSizeInfo::GetDefaultMinimumSize(_ControlOrientation orientation) const
51 {
52         result r = E_SUCCESS;
53         Tizen::Graphics::Dimension dimension(0, 0);
54
55         r = GET_DIMENSION_CONFIG(EDITDATE::DEFAULT_SIZE, orientation, dimension);
56
57         SysTryReturn(NID_UI, r == E_SUCCESS, dimension, r, "[%s] Propagating.", GetErrorMessage(r));
58         return dimension;
59 }
60
61 Dimension
62 _EditDateImpl::EditDateSizeInfo::GetDefaultMaximumSize(_ControlOrientation orientation) const
63 {
64         result r = E_SUCCESS;
65         Tizen::Graphics::Dimension dimension(0, 0);
66
67         r = GET_DIMENSION_CONFIG(EDITDATE::DEFAULT_SIZE, orientation, dimension);
68
69         SysTryReturn(NID_UI, r == E_SUCCESS, dimension, r, "[%s] Propagating.", GetErrorMessage(r));
70         return dimension;
71 }
72
73 _EditDateImpl::_EditDateImpl(EditDate* pPublic, _EditDate* pCore)
74         : _ControlImpl(pPublic, pCore)
75         , __pPublicDateChangeEvent(null)
76 {
77         ClearLastResult();
78 }
79
80 _EditDateImpl::~_EditDateImpl(void)
81 {
82         if (__pPublicDateChangeEvent)
83         {
84                 delete __pPublicDateChangeEvent;
85                 __pPublicDateChangeEvent = null;
86         }
87
88         ClearLastResult();
89 }
90
91 _EditDateImpl*
92 _EditDateImpl::GetInstance(EditDate& editDate)
93 {
94         return static_cast<_EditDateImpl*>(editDate._pControlImpl);
95 }
96
97 const _EditDateImpl*
98 _EditDateImpl::GetInstance(const EditDate& editDate)
99 {
100         return static_cast<const _EditDateImpl*>(editDate._pControlImpl);
101 }
102
103 _EditDateImpl*
104 _EditDateImpl::CreateEditDateImplN(EditDate* pControl, const Point& point, const String& title)
105 {
106         result r = E_SUCCESS;
107         Rectangle defaultSize;
108         bool isTitle = false;
109
110         _EditDate* pCore = _EditDate::CreateEditDateN(title);
111         SysTryReturn(NID_UI_CTRL, pCore, null, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
112
113         _EditDateImpl* pImpl = new (std::nothrow) _EditDateImpl(pControl, pCore);
114
115         r = CheckConstruction(pCore, pImpl);
116         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
117
118         r = pCore->AddDateChangeEventListener(*pImpl);
119         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
120
121         GET_SHAPE_CONFIG(EDITDATE::WIDTH, pCore->GetOrientation(), defaultSize.width);
122
123         GET_SHAPE_CONFIG(EDITDATE::HEIGHT, pCore->GetOrientation(), defaultSize.height);
124
125         defaultSize.x = point.x;
126         defaultSize.y = point.y;
127
128         r = pImpl->InitializeBoundsProperties(FUNC_SIZE_INFO(EditDate)(isTitle), defaultSize, pCore->GetOrientation());
129         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
130
131         ClearLastResult();
132
133         return pImpl;
134
135 CATCH:
136         delete pImpl;
137         return null;
138 }
139
140 const char*
141 _EditDateImpl::GetPublicClassName(void) const
142 {
143         return "Tizen::Ui::Controls::EditDate";
144 }
145
146 const EditDate&
147 _EditDateImpl::GetPublic(void) const
148 {
149         return static_cast <const EditDate&>(_ControlImpl::GetPublic());
150 }
151
152 EditDate&
153 _EditDateImpl::GetPublic(void)
154 {
155         return static_cast <EditDate&>(_ControlImpl::GetPublic());
156 }
157
158 const _EditDate&
159 _EditDateImpl::GetCore(void) const
160 {
161         return static_cast <const _EditDate&>(_ControlImpl::GetCore());
162 }
163
164 _EditDate&
165 _EditDateImpl::GetCore(void)
166 {
167         return static_cast <_EditDate&>(_ControlImpl::GetCore());
168 }
169
170 result
171 _EditDateImpl::AddDateChangeEventListener(IDateChangeEventListener& listener)
172 {
173         ClearLastResult();
174
175         if (__pPublicDateChangeEvent == null)
176         {
177                 __pPublicDateChangeEvent = new (std::nothrow) _PublicDateTimeChangeEvent(GetPublic());
178
179                 SysTryReturn(NID_UI_CTRL, __pPublicDateChangeEvent,
180                                         E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
181         }
182
183         result r = __pPublicDateChangeEvent->AddListener(listener);
184         SysTryReturnResult(NID_UI_CTRL, r != E_OBJ_ALREADY_EXIST, E_SYSTEM, "The IDateChangeEventListener instance already exists.");
185
186         return r;
187 }
188
189 result
190 _EditDateImpl::RemoveDateChangeEventListener(IDateChangeEventListener& listener)
191 {
192         ClearLastResult();
193
194         result r = E_OBJ_NOT_FOUND;
195
196         if (__pPublicDateChangeEvent)
197         {
198                 r = __pPublicDateChangeEvent->RemoveListener(listener);
199         }
200
201         SysTryReturnResult(NID_UI_CTRL, r != E_OBJ_NOT_FOUND, E_SYSTEM, "IDateChangeEventListener instance is not found.");
202
203         return r;
204 }
205
206 DateTime
207 _EditDateImpl::GetDate(void) const
208 {
209         ClearLastResult();
210
211         Variant date = GetCore().GetPropertyDate();
212
213         return date.ToDateTime();
214 }
215
216 int
217 _EditDateImpl::GetDay(void) const
218 {
219         ClearLastResult();
220
221         Variant day = GetCore().GetPropertyDay();
222
223         return day.ToInt();
224 }
225
226 int
227 _EditDateImpl::GetMonth(void) const
228 {
229         ClearLastResult();
230
231         Variant month = GetCore().GetPropertyMonth();
232
233         return month.ToInt();
234 }
235
236 int
237 _EditDateImpl::GetYear(void) const
238 {
239         ClearLastResult();
240
241         Variant year = GetCore().GetPropertyYear();
242
243         return year.ToInt();
244 }
245
246 void
247 _EditDateImpl::SetDate(const DateTime& date)
248 {
249         ClearLastResult();
250
251         GetCore().SetPropertyDate(Variant(date));
252         return;
253 }
254
255 void
256 _EditDateImpl::SetCurrentDate(void)
257 {
258         ClearLastResult();
259
260         GetCore().SetCurrentDate();
261         return;
262 }
263
264 result
265 _EditDateImpl::SetYear(int year)
266 {
267         ClearLastResult();
268         SysTryReturn(NID_UI_CTRL, (year >= DATETIME_YEAR_MIN && year <= DATETIME_YEAR_MAX), E_INVALID_ARG, E_INVALID_ARG,
269                         "[E_INVALID_ARG] Invalid argument is used. year (%d).", year);
270
271         result r = GetCore().SetPropertyYear(Variant(year));
272
273         SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
274
275         return r;
276 }
277
278 result
279 _EditDateImpl::SetMonth(int month)
280 {
281         ClearLastResult();
282         SysTryReturn(NID_UI_CTRL, (month >= DATETIME_MONTH_MIN && month <= DATETIME_MONTH_MAX), E_INVALID_ARG, E_INVALID_ARG,
283                         "[E_INVALID_ARG] Invalid argument is used. month (%d).", month);
284
285         result r = GetCore().SetPropertyMonth(Variant(month));
286
287         SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
288
289         return r;
290 }
291
292 result
293 _EditDateImpl::SetDay(int day)
294 {
295         ClearLastResult();
296         SysTryReturn(NID_UI_CTRL, (day >= DATETIME_DAY_MIN && day <= DATETIME_DAY_MAX), E_INVALID_ARG, E_INVALID_ARG,
297                         "[E_INVALID_ARG] Invalid argument is used. day (%d).", day);
298
299         result r = GetCore().SetPropertyDay(Variant(day));
300
301         SysTryLog(NID_UI_CTRL, r == E_SUCCESS, "[%s] Propagating.", GetErrorMessage(r));
302
303         return r;
304 }
305
306 void
307 _EditDateImpl::SetDatePickerEnabled(bool enable)
308 {
309         ClearLastResult();
310
311         GetCore().SetPropertyDatePickerEnabled(Variant(enable));
312         return;
313 }
314
315 bool
316 _EditDateImpl::IsDatePickerEnabled(void) const
317 {
318         ClearLastResult();
319
320         Variant enable = GetCore().GetPropertyDatePickerEnabled();
321
322         return enable.ToBool();
323 }
324
325 result
326 _EditDateImpl::SetYearRange(int minYear, int maxYear)
327 {
328         ClearLastResult();
329
330         result r = E_SUCCESS;
331
332         SysTryReturn(NID_UI_CTRL, (maxYear >= DATETIME_YEAR_MIN && maxYear <= DATETIME_YEAR_MAX), E_INVALID_ARG, E_INVALID_ARG,
333                         "[E_INVALID_ARG] maxYear (%d) must be greater than or equal to %d and less than or equal to %d.",
334                         maxYear, DATETIME_YEAR_MIN, DATETIME_YEAR_MAX);
335
336         SysTryReturn(NID_UI_CTRL, (minYear >= DATETIME_YEAR_MIN && minYear <= DATETIME_YEAR_MAX), E_INVALID_ARG, E_INVALID_ARG,
337                         "[E_INVALID_ARG] minYear (%d) must be greater than or equal to %d and less than or equal to %d.",
338                         minYear, DATETIME_YEAR_MIN, DATETIME_YEAR_MAX);
339
340         SysTryReturn(NID_UI_CTRL, (maxYear >= minYear), E_INVALID_ARG, E_INVALID_ARG,
341                         "[E_INVALID_ARG] maxYear (%d) must be greater than or equal to minYear (%d).", maxYear, minYear);
342
343         Variant oldMinYear = GetCore().GetPropertyMinYearRange();
344
345         r = GetCore().SetPropertyMinYearRange(Variant(minYear));
346         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
347
348         r = GetCore().SetPropertyMaxYearRange(Variant(maxYear));
349         if (r != E_SUCCESS)
350         {
351                 GetCore().SetPropertyMinYearRange(oldMinYear);
352                 SysLogException(NID_UI_CTRL, r, "[%s] Propagating", GetErrorMessage(r));
353         }
354
355         return r;
356 }
357
358 result
359 _EditDateImpl::GetYearRange(int& minYear, int& maxYear) const
360 {
361         ClearLastResult();
362
363         Variant tempMinYear = GetCore().GetPropertyMinYearRange();
364         Variant tempMaxYear = GetCore().GetPropertyMaxYearRange();
365
366         minYear = tempMinYear.ToInt();
367         maxYear = tempMaxYear.ToInt();
368
369         return GetLastResult();
370 }
371
372 void
373 _EditDateImpl::OnDateTimeChanged(const _Control& source, int year, int month, int day, int hour, int minute)
374 {
375         ClearLastResult();
376
377         if (__pPublicDateChangeEvent == null)
378         {
379                 return;
380         }
381
382         _PublicDateTimeChangeEventArg* pEventArg = new (std::nothrow) _PublicDateTimeChangeEventArg(DATE_PUBLIC_CHANGE_SAVED);
383         SysTryReturnVoidResult(NID_UI_CTRL, pEventArg != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
384
385         pEventArg->SetDate(year, month, day);
386         pEventArg->SetTime(hour, minute);
387         __pPublicDateChangeEvent->Fire(*pEventArg);
388
389         return;
390 }
391
392 void
393 _EditDateImpl::OnDateTimeChangeCanceled(const _Control& source)
394 {
395         ClearLastResult();
396
397         if (__pPublicDateChangeEvent == null)
398         {
399                 return;
400         }
401
402         _PublicDateTimeChangeEventArg* pEventArg = new (std::nothrow) _PublicDateTimeChangeEventArg(DATE_PUBLIC_CHANGE_CANCELED);
403         SysTryReturnVoidResult(NID_UI_CTRL, pEventArg != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
404
405         __pPublicDateChangeEvent->Fire(*pEventArg);
406
407         return;
408 }
409
410 class _EditDateMaker
411         : public _UiBuilderControlMaker
412 {
413 public:
414         _EditDateMaker(_UiBuilder* uibuilder)
415                 : _UiBuilderControlMaker(uibuilder){}
416         virtual ~_EditDateMaker(void){}
417         static _UiBuilderControlMaker* GetInstance(_UiBuilder* pUiBuilder)
418         {
419                 _EditDateMaker* pEditDateMaker = new (std::nothrow) _EditDateMaker(pUiBuilder);
420                 return pEditDateMaker;
421         }
422
423 protected:
424         virtual Control* Make(_UiBuilderControl* pControl)
425         {
426                 result r = E_SUCCESS;
427                 Point point;
428                 Rectangle rect;
429                 Tizen::Base::String elementStringOne;
430                 Tizen::Base::String elementStringTwo;
431                 bool titleEnable = false;
432
433                 _UiBuilderControlLayout* pControlProperty = null;
434                 EditDate* pEditDate = null;
435
436                 int tempMin = 0;
437                 int tempMax = 0;
438                 int temp = 0;
439
440                 GetProperty(pControl, &pControlProperty);
441                 if (pControlProperty == null)
442                 {
443                         return null;
444                 }
445
446                 rect = pControlProperty->GetRect();
447                 point.x = rect.x;
448                 point.y = rect.y;
449                 if (pControl->GetElement(L"titleText", elementStringOne))
450                 {
451                         titleEnable = true;
452                 }
453                 pEditDate = new (std::nothrow) EditDate;
454                 if (pEditDate == null)
455                 {
456                         return null;
457                 }
458
459                 if (titleEnable)
460                 {
461                         r = pEditDate->Construct(point, elementStringOne);
462                 }
463                 else
464                 {
465                         r = pEditDate->Construct(point);
466                 }
467
468                 if (r != E_SUCCESS)
469                 {
470                         delete pEditDate;
471                         pEditDate = null;
472                         return null;
473                 }
474
475                 if (pControl->GetElement(L"minYearRange", elementStringOne) && pControl->GetElement(L"maxYearRange", elementStringTwo))
476                 {
477                         Base::Integer::Parse(elementStringOne, tempMin);
478                         Base::Integer::Parse(elementStringTwo, tempMax);
479
480                         if (tempMin > tempMax)
481                         {
482                                 temp = tempMin;
483                                 tempMin = tempMax;
484                                 tempMax = tempMin;
485                         }
486
487                         r = pEditDate->SetYearRange(tempMin, tempMax);
488                 }
489
490                 return pEditDate;
491         }
492 }; // _EditDateMaker
493
494 _EditDateRegister::_EditDateRegister(void)
495 {
496         _UiBuilderControlTableManager* pUiBuilderControlTableManager = _UiBuilderControlTableManager::GetInstance();
497         pUiBuilderControlTableManager->RegisterControl(L"EditDate", _EditDateMaker::GetInstance);
498 }
499
500 _EditDateRegister::~_EditDateRegister(void)
501 {
502         _UiBuilderControlTableManager* pUiBuilderControlTableManager = _UiBuilderControlTableManager::GetInstance();
503         pUiBuilderControlTableManager->UnregisterControl(L"EditDate");
504 }
505 static _EditDateRegister EditDateRegisterToUiBuilder;
506 }}} // Tizen::Ui::Controls