4a0c8ee72234dddf4f5c7b6c4cf40450084ea42c
[platform/framework/native/uifw.git] / src / ui / controls / FUiCtrl_InputPadPresenter.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_InputPadPresenter.cpp
20  * @brief               This is the implementation file for the _InputPadPresenter class.
21  */
22
23 #include <FGrp_CanvasImpl.h>
24 #include <FGrp_TextTextSimple.h>
25 #include <FGrp_BitmapImpl.h>
26
27 #include "FUi_ResourceManager.h"
28 #include "FUiAnim_VisualElement.h"
29 #include "FUiCtrl_InputPad.h"
30 #include "FUiCtrl_InputPadPresenter.h"
31 #include "FUiCtrl_DateTimeUtils.h"
32 #include "FUi_CoordinateSystemUtils.h"
33
34 using namespace Tizen::Ui::Controls;
35 using namespace Tizen::Graphics;
36 using namespace Tizen::Graphics::_Text;
37 using namespace Tizen::Base;
38
39 namespace Tizen { namespace Ui { namespace Controls
40 {
41
42 _InputPadPresenter::_InputPadPresenter(_InputPad* pInputPad)
43         : __pInputPad(pInputPad)
44         , __pInputPadEventListener(null)
45         , __pFont(null)
46         , __pKeypadBackgroundBitmap(null)
47         , __inputPadStyle(INPUTPAD_STYLE_NORMAL)
48         , __buttonWidth(0.0f)
49         , __buttonHeight(0.0f)
50         , __startX(0.0f)
51         , __startY(0.0f)
52         , __marginX(0.0f)
53         , __marginY(0.0f)
54         , __pressedIndex(-1)
55         , __isLayoutChanged(false)
56 {
57         for (int i = 0; i < DATETIME_STATUS_MAX; i++)
58         {
59                 __pKeypadButtonNormalBitmap[i] = null;
60                 __pKeypadButtonEffectBitmap[i] = null;
61         }
62 }
63
64 _InputPadPresenter::~_InputPadPresenter(void)
65 {
66         delete __pFont;
67         __pFont = null;
68
69         delete __pKeypadBackgroundBitmap;
70         __pKeypadBackgroundBitmap = null;
71
72         for (int i = 0; i < DATETIME_STATUS_MAX; i++)
73         {
74                 delete __pKeypadButtonNormalBitmap[i];
75                 __pKeypadButtonNormalBitmap[i] = null;
76
77                 delete __pKeypadButtonEffectBitmap[i];
78                 __pKeypadButtonEffectBitmap[i] = null;
79         }
80 }
81
82 result
83 _InputPadPresenter::Construct(const FloatRectangle& bounds)
84 {
85         result r = E_SUCCESS;
86
87         r = LoadProperties(__pInputPad->GetOrientation());
88         SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), E_SYSTEM,
89                                            "A system error has occurred. Failed to load the properties for input pad.");
90
91         r = LoadResource();
92         SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), E_SYSTEM,
93                                            "A system error has occurred. Failed to load the resource for input pad.");
94
95         r = InitializeTextObjects();
96         SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), E_SYSTEM,
97                                            "A system error has occurred. Failed to initialize the text objects for input pad.");
98
99         return r;
100 }
101
102 result
103 _InputPadPresenter::LoadProperties(_ControlOrientation orientation)
104 {
105         float marginTop = 0.0f;
106
107         GET_SHAPE_CONFIG(INPUTPAD::BUTTON_WIDTH, orientation, __buttonWidth);
108         GET_SHAPE_CONFIG(INPUTPAD::BUTTON_HEIGHT, orientation, __buttonHeight);
109         GET_SHAPE_CONFIG(INPUTPAD::BUTTON_HORIZONTAL_MARGIN, orientation, __marginX);
110         GET_SHAPE_CONFIG(INPUTPAD::BUTTON_VERTICAL_MARGIN, orientation, __marginY);
111         GET_SHAPE_CONFIG(INPUTPAD::TOP_MARGIN, orientation, marginTop);
112
113         __startX = (__pInputPad->GetBounds().width - (__buttonWidth + __marginX) * INPUTPAD_BUTTON_COLUMN_MAX + __marginX) / 2;
114         __startY = marginTop;
115
116         return E_SUCCESS;
117 }
118
119 result
120 _InputPadPresenter::LoadResource(void)
121 {
122         result r = E_SUCCESS;
123
124         Color buttonColor[DATETIME_STATUS_MAX];
125         Color replacementColor = Color::GetColor(COLOR_ID_MAGENTA);
126
127         Bitmap* pButtonBgNormalBitmap[DATETIME_STATUS_MAX];
128         bool isCustomBitmap[DATETIME_STATUS_MAX];
129
130         for (int i = 0; i < DATETIME_STATUS_MAX; i++)
131         {
132                 buttonColor[i] = Color();
133                 pButtonBgNormalBitmap[i] = null;
134                 isCustomBitmap[i] = false;
135         }
136
137         GET_COLOR_CONFIG(INPUTPAD::BUTTON_BG_NORMAL, buttonColor[DATETIME_STATUS_NORMAL]);
138         GET_COLOR_CONFIG(INPUTPAD::BUTTON_BG_PRESSED, buttonColor[DATETIME_STATUS_PRESSED]);
139         GET_COLOR_CONFIG(INPUTPAD::BUTTON_BG_HIGHLIGHTED, buttonColor[DATETIME_STATUS_HIGHLIGHTED]);
140         GET_COLOR_CONFIG(INPUTPAD::BUTTON_BG_DISABLED, buttonColor[DATETIME_STATUS_DISABLED]);
141
142         r = GET_BITMAP_CONFIG_N(INPUTPAD::BG_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, __pKeypadBackgroundBitmap);
143         SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
144
145         r = GET_BITMAP_CONFIG_N(INPUTPAD::BUTTON_BG_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, pButtonBgNormalBitmap[DATETIME_STATUS_NORMAL]);
146         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
147
148         r = GET_BITMAP_CONFIG_N(INPUTPAD::BUTTON_BG_PRESSED, BITMAP_PIXEL_FORMAT_ARGB8888, pButtonBgNormalBitmap[DATETIME_STATUS_PRESSED]);
149         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
150
151         r = GET_BITMAP_CONFIG_N(INPUTPAD::BUTTON_BG_HIGHLIGHTED, BITMAP_PIXEL_FORMAT_ARGB8888, pButtonBgNormalBitmap[DATETIME_STATUS_HIGHLIGHTED]);
152         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
153
154         r = GET_BITMAP_CONFIG_N(INPUTPAD::BUTTON_BG_DISABLED, BITMAP_PIXEL_FORMAT_ARGB8888, pButtonBgNormalBitmap[DATETIME_STATUS_DISABLED]);
155         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
156
157         isCustomBitmap[DATETIME_STATUS_NORMAL] = IS_CUSTOM_BITMAP(INPUTPAD::BUTTON_BG_NORMAL);
158         isCustomBitmap[DATETIME_STATUS_PRESSED] = IS_CUSTOM_BITMAP(INPUTPAD::BUTTON_BG_PRESSED);
159         isCustomBitmap[DATETIME_STATUS_HIGHLIGHTED] = IS_CUSTOM_BITMAP(INPUTPAD::BUTTON_BG_HIGHLIGHTED);
160         isCustomBitmap[DATETIME_STATUS_DISABLED] = IS_CUSTOM_BITMAP(INPUTPAD::BUTTON_BG_DISABLED);
161
162         for (int i = 0; i < DATETIME_STATUS_MAX; i++)
163         {
164                 if (isCustomBitmap[i])
165                 {
166                         __pKeypadButtonNormalBitmap[i] = pButtonBgNormalBitmap[i];
167                 }
168                 else
169                 {
170                         __pKeypadButtonNormalBitmap[i] = _BitmapImpl::GetColorReplacedBitmapN(*pButtonBgNormalBitmap[i], replacementColor, buttonColor[i]);
171                         SysTryCatch(NID_UI_CTRL, (__pKeypadButtonNormalBitmap[i] != null), , r, "[%s] Propagating.", GetErrorMessage(r));
172                 }
173         }
174
175         if (!isCustomBitmap[DATETIME_STATUS_NORMAL])
176         {
177                 r = GET_BITMAP_CONFIG_N(INPUTPAD::BUTTON_BG_EFFECT_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, __pKeypadButtonEffectBitmap[DATETIME_STATUS_NORMAL]);
178                 SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
179         }
180
181         if (!isCustomBitmap[DATETIME_STATUS_PRESSED])
182         {
183                 r = GET_BITMAP_CONFIG_N(INPUTPAD::BUTTON_BG_EFFECT_PRESSED, BITMAP_PIXEL_FORMAT_ARGB8888, __pKeypadButtonEffectBitmap[DATETIME_STATUS_PRESSED]);
184                 SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
185         }
186
187         if (!isCustomBitmap[DATETIME_STATUS_HIGHLIGHTED])
188         {
189                 r = GET_BITMAP_CONFIG_N(INPUTPAD::BUTTON_BG_EFFECT_HIGHLIGHTED, BITMAP_PIXEL_FORMAT_ARGB8888, __pKeypadButtonEffectBitmap[DATETIME_STATUS_HIGHLIGHTED]);
190                 SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
191         }
192
193         if (!isCustomBitmap[DATETIME_STATUS_DISABLED])
194         {
195                 r = GET_BITMAP_CONFIG_N(INPUTPAD::BUTTON_BG_EFFECT_DISABLED, BITMAP_PIXEL_FORMAT_ARGB8888, __pKeypadButtonEffectBitmap[DATETIME_STATUS_DISABLED]);
196                 SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
197         }
198
199         for (int i = 0; i < DATETIME_STATUS_MAX; i++)
200         {
201                 delete pButtonBgNormalBitmap[i];
202         }
203
204         return r;
205
206 CATCH:
207         delete __pKeypadBackgroundBitmap;
208         __pKeypadBackgroundBitmap = null;
209
210         for (int i = 0; i < DATETIME_STATUS_MAX; i++)
211         {
212                 delete pButtonBgNormalBitmap[i];
213
214                 delete __pKeypadButtonNormalBitmap[i];
215                 __pKeypadButtonNormalBitmap[i] = null;
216
217                 delete __pKeypadButtonEffectBitmap[i];
218                 __pKeypadButtonEffectBitmap[i] = null;
219         }
220
221         return r;
222 }
223
224 result
225 _InputPadPresenter::InitializeTextObjects(void)
226 {
227         result r = E_SUCCESS;
228
229         // Create TextObject
230         r = __textObject.Construct();
231         SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
232
233         __textObject.SetAlignment(TEXT_OBJECT_ALIGNMENT_CENTER | TEXT_OBJECT_ALIGNMENT_MIDDLE);
234         __textObject.SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
235         __textObject.SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
236
237         r = __guideTextObject.Construct();
238         SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
239
240         __guideTextObject.SetAlignment(TEXT_OBJECT_ALIGNMENT_RIGHT | TEXT_OBJECT_ALIGNMENT_TOP);
241         __guideTextObject.SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
242         __guideTextObject.SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
243
244         return r;
245 }
246
247 void
248 _InputPadPresenter::SetInputPadEventListener(const _IInputPadEventListener& listener)
249 {
250         __pInputPadEventListener = const_cast <_IInputPadEventListener*>(&listener);
251
252         return;
253 }
254
255 result
256 _InputPadPresenter::SetInputPadStyle(const _InputPadStyle style)
257 {
258         if (style != __inputPadStyle || __isLayoutChanged == true)
259         {
260                 __isLayoutChanged = false;
261
262                 __inputPadStyle = style;
263         }
264         __pressedIndex = -1;
265
266         return E_SUCCESS;
267 }
268
269 _InputPadStyle
270 _InputPadPresenter::GetInputPadStyle(void) const
271 {
272         return __inputPadStyle;
273 }
274
275 FloatRectangle
276 _InputPadPresenter::GetButtonBounds(int index) const
277 {
278         FloatRectangle buttonBounds;
279
280         buttonBounds.x = __startX + (index % INPUTPAD_BUTTON_COLUMN_MAX) * (__buttonWidth + __marginX);
281         buttonBounds.y = __startY + (index / INPUTPAD_BUTTON_COLUMN_MAX) * (__buttonHeight + __marginY);
282         buttonBounds.width = __buttonWidth;
283         buttonBounds.height = __buttonHeight;
284
285         return buttonBounds;
286 }
287
288 result
289 _InputPadPresenter::Draw(void)
290 {
291         result r = E_SUCCESS;
292         FloatRectangle bgBounds;
293
294         Canvas* pCanvas = __pInputPad->GetCanvasN();
295         r = GetLastResult();
296         SysTryReturnResult(NID_UI_CTRL, (pCanvas != null), r, "Propagating.");
297
298         bgBounds = pCanvas->GetBoundsF();
299
300         if(__pKeypadBackgroundBitmap != null)
301         {
302                 r = pCanvas->DrawNinePatchedBitmap(bgBounds, *__pKeypadBackgroundBitmap);
303                 SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
304         }
305
306         r = DrawAllButtons(*pCanvas);
307
308         delete pCanvas;
309         pCanvas = null;
310
311         return r;
312
313 CATCH:
314         delete pCanvas;
315         pCanvas = null;
316
317         return r;
318 }
319
320 void
321 _InputPadPresenter::EnableLayoutChangedFlag(void)
322 {
323         __isLayoutChanged = true;
324
325         return;
326 }
327
328 void
329 _InputPadPresenter::SetFont(Font* pFont)
330 {
331         if (__pFont != null)
332         {
333                 delete __pFont;
334                 __pFont = null;
335         }
336
337         __pFont = _FontImpl::CloneN(*pFont);
338         SysTryReturnVoidResult(NID_UI_CTRL, __pFont != null, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
339
340         return;
341 }
342
343 result
344 _InputPadPresenter::DrawAllButtons(Canvas& canvas)
345 {
346         result r = E_SUCCESS;
347
348         for (int i = 0; i < INPUTPAD_BUTTON_MAX; i++)
349         {
350                 r |= DrawButton(canvas, i);
351         }
352
353         if (r != E_SUCCESS)
354         {
355                 return E_SYSTEM;
356         }
357
358         return E_SUCCESS;
359 }
360
361 result
362 _InputPadPresenter::DrawButton(Canvas& canvas, int index)
363 {
364         result r = E_SUCCESS;
365         Color textColor;
366         DateTimePickerStatus status;
367         bool isCustomBitmap[DATETIME_STATUS_MAX];
368
369         FloatRectangle buttonBounds = GetButtonBounds(index);
370         int returnValue = GetReturnValue(index);
371
372         // Do not Draw Extra Buttons in case of Numeric Keypad
373         if (returnValue < 0)
374         {
375                 return E_SUCCESS;
376         }
377
378         if (__pInputPad->IsEnabled() == false)
379         {
380                 status = DATETIME_STATUS_DISABLED;
381                 GET_COLOR_CONFIG(INPUTPAD::TEXT_DISABLED, textColor);
382         }
383         else if (__pressedIndex == index)
384         {
385                 status = DATETIME_STATUS_PRESSED;
386                 GET_COLOR_CONFIG(INPUTPAD::TEXT_PRESSED, textColor);
387         }
388         else
389         {
390                 status = DATETIME_STATUS_NORMAL;
391                 GET_COLOR_CONFIG(INPUTPAD::TEXT_NORMAL, textColor);
392         }
393
394         isCustomBitmap[DATETIME_STATUS_NORMAL] = IS_CUSTOM_BITMAP(INPUTPAD::BUTTON_BG_NORMAL);
395         isCustomBitmap[DATETIME_STATUS_PRESSED] = IS_CUSTOM_BITMAP(INPUTPAD::BUTTON_BG_PRESSED);
396         isCustomBitmap[DATETIME_STATUS_HIGHLIGHTED] = IS_CUSTOM_BITMAP(INPUTPAD::BUTTON_BG_HIGHLIGHTED);
397         isCustomBitmap[DATETIME_STATUS_DISABLED] = IS_CUSTOM_BITMAP(INPUTPAD::BUTTON_BG_DISABLED);
398
399         if (isCustomBitmap[status] == false && __pKeypadButtonEffectBitmap[status] != null)
400         {
401                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pKeypadButtonEffectBitmap[status]))
402                 {
403                         r = canvas.DrawNinePatchedBitmap(buttonBounds, *__pKeypadButtonEffectBitmap[status]);
404                         SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
405                 }
406                 else
407                 {
408                         r = canvas.DrawBitmap(buttonBounds, *__pKeypadButtonEffectBitmap[status]);
409                         SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
410                 }
411         }
412
413         if (__pKeypadButtonNormalBitmap[status] != null)
414         {
415                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pKeypadButtonNormalBitmap[status]))
416                 {
417                         r = canvas.DrawNinePatchedBitmap(buttonBounds, *__pKeypadButtonNormalBitmap[status]);
418                         SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
419                 }
420                 else
421                 {
422                         r = canvas.DrawBitmap(buttonBounds, *__pKeypadButtonNormalBitmap[status]);
423                         SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
424                 }
425         }
426
427         // Draw text
428         if (__inputPadStyle == INPUTPAD_STYLE_ALPHA)
429         {
430                 // Calculate text bounds
431                 int textCorrectionY = 0;
432                 GET_SHAPE_CONFIG(INPUTPAD::TEXT_CORRECTION_Y, __pInputPad->GetOrientation(), textCorrectionY);
433
434                 buttonBounds.y += textCorrectionY;
435
436                 _DateTimeUtils dateTimeUtils;
437                 String text = dateTimeUtils.GetMonthString(returnValue);
438
439                 r = __textObject.RemoveAll();
440                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
441
442                 TextSimple* pSimpleText = new (std::nothrow)TextSimple((wchar_t*)text.GetPointer(), text.GetLength(),
443                                                                                                                                 TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
444                 SysTryReturnResult(NID_UI_CTRL, (pSimpleText != null), E_OUT_OF_MEMORY, "Memory allocation failed.");
445
446                 r = __textObject.AppendElement(*pSimpleText);
447                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
448
449                 r = __textObject.SetForegroundColor(textColor, 0, __textObject.GetTextLength());
450                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
451
452                 int fontSize = 0;
453                 GET_SHAPE_CONFIG(INPUTPAD::ALPHA_FONT_SIZE, __pInputPad->GetOrientation(), fontSize);
454
455                 r = _FontImpl::GetInstance(*__pFont)->SetStyle(FONT_STYLE_BOLD);
456                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
457
458                 r = _FontImpl::GetInstance(*__pFont)->SetSize(fontSize);
459                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
460
461                 r = __textObject.SetFont(__pFont, 0, __textObject.GetTextLength());
462                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
463
464                 r = __textObject.SetBounds(buttonBounds);
465                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
466
467                 r = __textObject.Draw(*_CanvasImpl::GetInstance(canvas));
468                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
469
470                 // Draw guide text
471                 int guideTextCorrectionX = 0;
472                 GET_SHAPE_CONFIG(INPUTPAD::GUIDE_CORRECTION_X, __pInputPad->GetOrientation(), guideTextCorrectionX);
473
474                 buttonBounds.width -= guideTextCorrectionX;
475
476                 Color guideTextColor;
477                 GET_COLOR_CONFIG(INPUTPAD::GUIDE_TEXT_NORMAL, guideTextColor);
478
479                 String guideText = Integer::ToString(returnValue);
480
481                 r = __guideTextObject.RemoveAll();
482                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
483
484                 TextSimple* pSimpleTextForGuideText = new (std::nothrow)TextSimple((wchar_t*)guideText.GetPointer(),
485                                                                                                                                                    guideText.GetLength(), TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
486                 SysTryReturnResult(NID_UI_CTRL, (pSimpleTextForGuideText != null), E_OUT_OF_MEMORY, "Memory allocation failed.");
487
488                 r = __guideTextObject.AppendElement(*pSimpleTextForGuideText);
489                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
490
491                 r = __guideTextObject.SetForegroundColor(guideTextColor, 0, __guideTextObject.GetTextLength());
492                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
493
494                 GET_SHAPE_CONFIG(INPUTPAD::GUIDE_FONT_SIZE, __pInputPad->GetOrientation(), fontSize);
495
496                 r = _FontImpl::GetInstance(*__pFont)->SetStyle(FONT_STYLE_PLAIN);
497                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
498
499                 r = _FontImpl::GetInstance(*__pFont)->SetSize(fontSize);
500                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
501
502                 r = __guideTextObject.SetFont(__pFont, 0, __guideTextObject.GetTextLength());
503                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
504
505                 r = __guideTextObject.SetBounds(buttonBounds);
506                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
507
508                 r = __guideTextObject.Draw(*_CanvasImpl::GetInstance(canvas));
509                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
510         }
511         else if (returnValue >= 0)
512         {
513                 String text = Integer::ToString(returnValue);
514
515                 r = __textObject.RemoveAll();
516                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
517
518                 TextSimple* pSimpleText = new (std::nothrow)TextSimple((wchar_t*)text.GetPointer(),
519                                                                                                                            text.GetLength(), TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
520                 SysTryReturnResult(NID_UI_CTRL, (pSimpleText != null), E_OUT_OF_MEMORY, "Memory allocation failed.");
521
522                 r = __textObject.AppendElement(*pSimpleText);
523                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
524                 result r = E_SUCCESS;
525
526                 r = __textObject.SetForegroundColor(textColor, 0, __textObject.GetTextLength());
527                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
528
529                 int fontSize = 0;
530
531                 GET_SHAPE_CONFIG(INPUTPAD::NUMERIC_FONT_SIZE, __pInputPad->GetOrientation(), fontSize);
532
533                 r = _FontImpl::GetInstance(*__pFont)->SetStyle(FONT_STYLE_BOLD);
534                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
535
536                 r = _FontImpl::GetInstance(*__pFont)->SetSize(fontSize);
537                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
538
539                 r = __textObject.SetFont(__pFont, 0, __textObject.GetTextLength());
540                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
541
542                 r = __textObject.SetBounds(buttonBounds);
543                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
544
545                 r = __textObject.Draw(*_CanvasImpl::GetInstance(canvas));
546                 SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), r, "Propagating.");
547         }
548
549         return E_SUCCESS;
550 }
551
552 result
553 _InputPadPresenter::RefreshButton(int index)
554 {
555         Canvas* pCanvas = __pInputPad->GetCanvasN();
556         SysTryReturnResult(NID_UI_CTRL, (pCanvas != null), GetLastResult(), "Propagating.");
557
558         result r = DrawButton(*pCanvas, index);
559         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to draw the keypad button.");
560
561         delete pCanvas;
562         pCanvas = null;
563
564         if (__pInputPad->GetVisualElement() != null)
565         {
566                 __pInputPad->GetVisualElement()->SetFlushNeeded();
567         }
568
569         return r;
570
571 CATCH:
572         delete pCanvas;
573         pCanvas = null;
574
575         return r;
576 }
577
578 int
579 _InputPadPresenter::GetIndexFromTouchedPosition(const FloatPoint& point) const
580 {
581         float correctionValue = 0.0f;
582
583         GET_FIXED_VALUE_CONFIG(INPUTPAD::BUTTON_TOUCH_CORRECTION_Y, __pInputPad->GetOrientation(), correctionValue);
584
585         if (point.x < (__startX - correctionValue) || point.y < (__startY - correctionValue))
586         {
587                 return -1;
588         }
589
590         int row = _CoordinateSystemUtils::ConvertToInteger((point.y - __startY + __marginY + correctionValue) / (__buttonHeight + __marginY));
591         int col = _CoordinateSystemUtils::ConvertToInteger((point.x - __startX + __marginX) / (__buttonWidth + __marginX));
592         if (row >= INPUTPAD_BUTTON_ROW_MAX || col >= INPUTPAD_BUTTON_COLUMN_MAX)
593         {
594                 return -1;
595         }
596
597         return row * INPUTPAD_BUTTON_COLUMN_MAX + col;
598 }
599
600 int
601 _InputPadPresenter::GetReturnValue(int index) const
602 {
603         if (__inputPadStyle == INPUTPAD_STYLE_NORMAL)
604         {
605                 if (index >= INPUTPAD_NUMBER_1 && index <= INPUTPAD_NUMBER_9)
606                 {
607                         return index + 1;
608                 }
609                 else if (index == INPUTPAD_NUMBER_0)
610                 {
611                         return 0;
612                 }
613         }
614         else if (__inputPadStyle == INPUTPAD_STYLE_ALPHA)
615         {
616                 return index + 1;
617         }
618
619         return -1;
620 }
621
622 bool
623 _InputPadPresenter::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
624 {
625         if (&source != __pInputPad)
626         {
627                 return false;
628         }
629
630         __pressedIndex = GetIndexFromTouchedPosition(touchinfo.GetCurrentPosition());
631         if (__pressedIndex >= 0)
632         {
633                 RefreshButton(__pressedIndex);
634                 if (__pInputPadEventListener != null)
635                 {
636                         __pInputPadEventListener->OnInputPadValueChanged(source, GetReturnValue(__pressedIndex));
637                 }
638
639                 return true;
640         }
641
642         return false;
643 }
644
645 bool
646 _InputPadPresenter::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
647 {
648         if (&source != __pInputPad)
649         {
650                 return false;
651         }
652
653         if (__pressedIndex >= 0)
654         {
655                 int refreshIndex = __pressedIndex;
656                 __pressedIndex = -1;
657                 RefreshButton(refreshIndex);
658
659                 return true;
660         }
661
662         return false;
663 }
664
665 bool
666 _InputPadPresenter::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
667 {
668         return OnTouchReleased(source, touchinfo);
669 }
670
671 }}} // Tizen::Ui::Controls