Tizen 2.1 base
[framework/osp/uifw.git] / src / graphics / FGrp_TextElementImpl.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        FGrp_TextElementImpl.cpp
20  * @brief       This is the cpp file for _TextElementImpl class.
21  */
22
23 // Includes
24 #include <new>
25
26 #include "FGrp_TextElementImpl.h"
27 #include "FGrp_EnrichedTextImpl.h"
28 #include "FGrp_FontImpl.h"
29 #include "FGrp_ResUtil.h"
30 #include "FGrp_TextTextCutLinkParser.h"
31 #include "FGrp_TextTextUtility.h"
32 #include "FGrp_TextTextElement.h"
33 #include "FGrp_TextTextSimple.h"
34 #include "FGrp_TextTextCutLink.h"
35 #include "util/FGrp_Util.h"
36
37 // Usings
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Runtime;
40 using namespace Tizen::Base::Utility;
41 using namespace Tizen::Graphics;
42 using namespace Tizen::Graphics::_Text;
43
44 namespace // unnamed
45 {
46         const int DEFAULT_FONT_SIZE = 42;
47 }
48
49 namespace Tizen { namespace Graphics
50 {
51
52 _TextElementImpl::_TextElementImpl(void)
53 {
54         __strText = null;
55         __linkText = null;
56         __linkType = LINK_TYPE_NONE;
57         __pFont = null;
58         __isAuto = false;
59         __bgColorEnable = false;
60         __outlineColorEnable = false;
61         __autoLink = false;
62         __pRegisteringEnrichedText = null;
63         __count = 0;
64         __linkOffset = 0;
65         __pTextComponent = null;
66 }
67
68 _TextElementImpl::~_TextElementImpl(void)
69 {
70         if (__pRegisteringEnrichedText)
71         {
72                 int index = __pRegisteringEnrichedText->GetTextElementIndex(this);
73                 int count = __pRegisteringEnrichedText->GetTextElementCount();
74
75                 if (index >= 0 && index < count)
76                 {
77                         __pRegisteringEnrichedText->RemoveAt(index, false);
78                 }
79                 __pRegisteringEnrichedText = null;
80         }
81
82         if (__pFont)
83         {
84                 delete __pFont;
85                 __pFont = null;
86         }
87
88         if (__pTextComponent)
89         {
90                 Destroy();
91         }
92 }
93
94 result
95 _TextElementImpl::Construct(const Tizen::Base::String& text)
96 {
97         SysTryReturn(NID_GRP, __pTextComponent == null, E_OPERATION_FAILED, E_OPERATION_FAILED, "[E_OPERATION_FAILED] This instance is already constructed.");
98         SysTryReturn(NID_GRP, text.IsEmpty() == false, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
99
100         result r = E_SUCCESS;
101         Font* pFont = null;
102         __strText = text;
103
104         r = Create(__strText, __strText.GetLength(), TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
105         SysTryCatch(NID_GRP
106                         , r == E_SUCCESS
107                         , , r, "[%s] Fails to construct native text element.", GetErrorMessage(r));
108
109         __foregroundColor = GetTextColor();
110         __backgroundColor = GetBackgroundColor();
111         __outlineColor = GetOutlineColor();
112
113         pFont = new (std::nothrow) Font;
114         if (!pFont)
115         {
116                 return E_OUT_OF_MEMORY;
117         }
118
119         r = pFont->Construct(FONT_STYLE_PLAIN, DEFAULT_FONT_SIZE);
120         SysTryCatch(NID_GRP
121                         , r == E_SUCCESS
122                         , , r, "[%s] Fails to construct font.", GetErrorMessage(r));
123
124         r = SetTextObjectFont(*pFont);
125         SysTryCatch(NID_GRP
126                         , r == E_SUCCESS
127                         , , r, "[%s] Fails to set font.", GetErrorMessage(r));
128
129         this->__pFont = pFont;
130
131         return r;
132
133 CATCH:
134         delete pFont;
135         pFont = null;
136
137         return r;
138 }
139
140 result
141 _TextElementImpl::Construct(const Tizen::Base::String& text, const Tizen::Graphics::Canvas& canvas)
142 {
143         SysTryReturn(NID_GRP, __pTextComponent == null, E_OPERATION_FAILED, E_OPERATION_FAILED, "[E_OPERATION_FAILED] This instance is already constructed.");
144         SysTryReturn(NID_GRP, text.IsEmpty() == false, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
145         SysTryReturn(NID_GRP, &canvas, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
146
147         result r = E_SUCCESS;
148
149         r = this->Construct(text);
150
151         if (r != E_SUCCESS)
152         {
153                 return r;
154         }
155
156         Tizen::Graphics::Canvas& refCanvas = const_cast <Tizen::Graphics::Canvas&>(canvas);
157
158         Tizen::Graphics::Color color1 = refCanvas.GetForegroundColor();
159         r = GetLastResult();
160         SysTryReturn(NID_GRP, r == E_SUCCESS, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
161         this->SetTextColor(color1);
162
163         Tizen::Graphics::Color color2 = refCanvas.GetBackgroundColor();
164         r = GetLastResult();
165         SysTryReturn(NID_GRP, r == E_SUCCESS, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
166         this->SetBackgroundColor(color2);
167
168         return r;
169 }
170
171 result
172 _TextElementImpl::Construct(void)
173 {
174         SysTryReturn(NID_GRP, __pTextComponent == null, E_OPERATION_FAILED, E_OPERATION_FAILED, "[E_OPERATION_FAILED] This instance is already constructed.");
175
176         result r = E_SUCCESS;
177         __strText = "";
178         Font* pFont = null;
179
180         r = Create(__strText, __strText.GetLength(), TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
181         SysTryCatch(NID_GRP
182                         , r == E_SUCCESS
183                         , , r, "[%s] Fails to construct native text element.", GetErrorMessage(r));
184
185         __foregroundColor = GetTextColor();
186         __backgroundColor = GetBackgroundColor();
187         __outlineColor = GetOutlineColor();
188
189         pFont = new (std::nothrow) Font;
190         if (!pFont)
191         {
192                 return E_OUT_OF_MEMORY;
193         }
194
195         r = pFont->Construct(FONT_STYLE_PLAIN, DEFAULT_FONT_SIZE);
196         SysTryCatch(NID_GRP
197                         , r == E_SUCCESS
198                         , , r, "[%s] Fails to construct font.", GetErrorMessage(r));
199
200         r = SetTextObjectFont(*pFont);
201         SysTryCatch(NID_GRP
202                         , r == E_SUCCESS
203                         , , r, "[%s] Fails to set font.", GetErrorMessage(r));
204
205         this->__pFont = pFont;
206
207         return r;
208
209 CATCH:
210         delete pFont;
211         pFont = null;
212
213         return r;
214 }
215
216 result
217 _TextElementImpl::Construct(const Tizen::Base::String& text, Tizen::Base::Utility::LinkType linkType, const Tizen::Base::String& link)
218 {
219         SysTryReturn(NID_GRP, __pTextComponent == null, E_OPERATION_FAILED, E_OPERATION_FAILED, "[E_OPERATION_FAILED] This instance is already constructed.");
220         SysTryReturn(NID_GRP, text.IsEmpty() == false, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
221         SysTryReturn(NID_GRP, LINK_TYPE_NONE != linkType, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The link type is invalid.");
222
223         result r = E_SUCCESS;
224         Font* pFont = null;
225         _CutLinkType type = this->ConvertObjectLinkType(linkType);
226         FontStyle fontStyle = FONT_STYLE_MIN;
227
228         __strText = text;
229         __linkText = link;
230         __linkType = linkType;
231
232         r = CreateUserLink(__strText, type);
233         SysTryCatch(NID_GRP
234                         , r == E_SUCCESS
235                         , , r, "[%s] Fails to create native text element.", GetErrorMessage(r));
236
237         __foregroundColor = GetTextColor();
238         __backgroundColor = GetBackgroundColor();
239         __outlineColor = GetOutlineColor();
240
241         pFont = new (std::nothrow) Font;
242         if (!pFont)
243         {
244                 return E_OUT_OF_MEMORY;
245         }
246
247         if (linkType == LINK_TYPE_URL || linkType == LINK_TYPE_EMAIL)
248         {
249                 fontStyle = FONT_STYLE_ITALIC;
250         }
251         else if (linkType == LINK_TYPE_TEL_NUM)
252         {
253                 fontStyle = FONT_STYLE_BOLD;
254         }
255         else
256         {
257                 fontStyle = FONT_STYLE_PLAIN;
258         }
259         r = pFont->Construct(fontStyle, DEFAULT_FONT_SIZE);
260         SysTryCatch(NID_GRP
261                         , r == E_SUCCESS
262                         , , r, "[%s] Fails to construct font.", GetErrorMessage(r));
263
264         SetTextColor(Color::GetColor(COLOR_ID_BLUE));
265         pFont->SetUnderline(true);
266         r = SetTextObjectFont(*pFont);
267         SysTryCatch(NID_GRP
268                         , r == E_SUCCESS
269                         , , r, "[%s] Fails to set font.", GetErrorMessage(r));
270
271         this->__pFont = pFont;
272
273         return r;
274
275 CATCH:
276         delete pFont;
277         pFont = null;
278
279         return r;
280 }
281
282 result
283 _TextElementImpl::Construct(const Tizen::Base::String& text, unsigned long autoLink)
284 {
285         SysTryReturn(NID_GRP, __pTextComponent == null, E_OPERATION_FAILED, E_OPERATION_FAILED, "[E_OPERATION_FAILED] This instance is already constructed.");
286         SysTryReturn(NID_GRP, text.IsEmpty() == false, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
287         SysTryReturn(NID_GRP, LINK_TYPE_NONE < autoLink && autoLink < LINK_TYPE_MAX, E_INVALID_ARG, E_INVALID_ARG,
288                            "[E_INVALID_ARG] The autolink mask is invalid.");
289
290         result r = E_SUCCESS;
291         Tizen::Base::String link;
292         Font* pFont = null;
293         _CutLinkType type;
294         FontStyle fontStyle = FONT_STYLE_MIN;
295
296         __strText = text;
297         __autoLink = autoLink;
298         __isAuto = true;
299
300         r = CreateAutoLink(__strText, autoLink);
301         SysTryCatch(NID_GRP
302                         , r == E_SUCCESS
303                         , , r, "[%s] Fails to create native text element.", GetErrorMessage(r));
304
305         __foregroundColor = GetTextColor();
306         __backgroundColor = GetBackgroundColor();
307         __outlineColor = GetOutlineColor();
308
309         type = GetCutLinkType();
310
311         pFont = new (std::nothrow) Font;
312         if (!pFont)
313         {
314                 return E_OUT_OF_MEMORY;
315         }
316
317         if (type != TEXT_CUTLINK_TYPE_INVALID)
318         {
319                 int len = GetLength();
320
321                 text.SubString((GetAutolinkSrcOffset()), len, link);
322
323                 __linkText = link;
324                 __linkType = this->ConvertObjectLinkType(type);
325
326                 if (type == TEXT_CUTLINK_TYPE_URL || type == TEXT_CUTLINK_TYPE_EMAIL)
327                 {
328                         fontStyle = FONT_STYLE_ITALIC;
329                 }
330                 else if (type == TEXT_CUTLINK_TYPE_PHONE_NUMBER)
331                 {
332                         fontStyle = FONT_STYLE_BOLD;
333                 }
334                 else
335                 {
336                         fontStyle = FONT_STYLE_PLAIN;
337                 }
338
339                 r = pFont->Construct(fontStyle, DEFAULT_FONT_SIZE);
340                 SysTryCatch(NID_GRP
341                                 , r == E_SUCCESS
342                                 , , r, "[%s] Fails to construct font.", GetErrorMessage(r));
343
344                 SetTextColor(Color::GetColor(COLOR_ID_BLUE));
345                 pFont->SetUnderline(true);
346                 r = SetTextObjectFont(*pFont);
347                 SysTryCatch(NID_GRP
348                                 , r == E_SUCCESS
349                                 , , r, "[%s] Fails to set font.", GetErrorMessage(r));
350
351                 this->__pFont = pFont;
352         }
353         else
354         {
355                 r = pFont->Construct(FONT_STYLE_PLAIN, DEFAULT_FONT_SIZE);
356                 SysTryCatch(NID_GRP
357                                 , r == E_SUCCESS
358                                 , , r, "[%s] Fails to construct font.", GetErrorMessage(r));
359
360                 r = SetTextObjectFont(*pFont);
361                 SysTryCatch(NID_GRP
362                                 , r == E_SUCCESS
363                                 , , r, "[%s] Fails to set font.", GetErrorMessage(r));
364
365                 this->__pFont = pFont;
366         }
367
368         return r;
369
370 CATCH:
371         delete pFont;
372         pFont = null;
373
374         return r;
375 }
376
377 result
378 _TextElementImpl::Construct(const Tizen::Base::String& text, Tizen::Base::Utility::LinkType linkType, const Tizen::Base::String& link,
379                                                         const Tizen::Graphics::Canvas& canvas)
380 {
381         SysTryReturn(NID_GRP, __pTextComponent == null, E_OPERATION_FAILED, E_OPERATION_FAILED, "[E_OPERATION_FAILED] This instance is already constructed.");
382         SysTryReturn(NID_GRP, text.IsEmpty() == false, E_INVALID_ARG, E_INVALID_ARG
383                         , "[E_INVALID_ARG] The argument is invalid.");
384         SysTryReturn(NID_GRP, LINK_TYPE_NONE != linkType, E_INVALID_ARG, E_INVALID_ARG
385                         , "[E_INVALID_ARG] The link type is invalid.");
386         SysTryReturn(NID_GRP, &canvas, E_INVALID_ARG, E_INVALID_ARG
387                         , "[E_INVALID_ARG] The argument is invalid.");
388
389         result r = E_SUCCESS;
390
391         r = this->Construct(text, linkType, link);
392
393         if (r != E_SUCCESS)
394         {
395                 return r;
396         }
397
398         Tizen::Graphics::Canvas& refCanvas = const_cast <Tizen::Graphics::Canvas&>(canvas);
399
400         Tizen::Graphics::Color color1 = refCanvas.GetForegroundColor();
401         r = GetLastResult();
402         SysTryReturn(NID_GRP, r == E_SUCCESS, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
403         this->SetTextColor(color1);
404
405         Tizen::Graphics::Color color2 = refCanvas.GetBackgroundColor();
406         r = GetLastResult();
407         SysTryReturn(NID_GRP, r == E_SUCCESS, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
408         this->SetBackgroundColor(color2);
409
410         return r;
411 }
412
413 result
414 _TextElementImpl::Construct(const Tizen::Base::String& text, unsigned long autoLink, const Tizen::Graphics::Canvas& canvas)
415 {
416         SysTryReturn(NID_GRP, __pTextComponent == null, E_OPERATION_FAILED, E_OPERATION_FAILED
417                         , "[E_OPERATION_FAILED] This instance is already constructed.");
418         SysTryReturn(NID_GRP, text.IsEmpty() == false, E_INVALID_ARG, E_INVALID_ARG
419                         , "[E_INVALID_ARG] The argument is invalid.");
420         SysTryReturn(NID_GRP, LINK_TYPE_NONE < autoLink && autoLink < LINK_TYPE_MAX, E_INVALID_ARG, E_INVALID_ARG
421                         , "[E_INVALID_ARG] The autolink mask is invalid.");
422         SysTryReturn(NID_GRP, &canvas, E_INVALID_ARG, E_INVALID_ARG
423                         , "[E_INVALID_ARG] The argument is invalid.");
424
425         result r = E_SUCCESS;
426
427         r = this->Construct(text, autoLink);
428         if (r != E_SUCCESS)
429         {
430                 return r;
431         }
432
433         Tizen::Graphics::Canvas& refCanvas = const_cast <Tizen::Graphics::Canvas&>(canvas);
434
435         Tizen::Graphics::Color color1 = refCanvas.GetForegroundColor();
436         r = GetLastResult();
437         SysTryReturn(NID_GRP, r == E_SUCCESS, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
438         this->SetTextColor(color1);
439
440         Tizen::Graphics::Color color2 = refCanvas.GetBackgroundColor();
441         r = GetLastResult();
442         SysTryReturn(NID_GRP, r == E_SUCCESS, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
443         this->SetBackgroundColor(color2);
444
445         return r;
446 }
447
448 Tizen::Base::String&
449 _TextElementImpl::GetLink(void) const
450 {
451         return *(const_cast <String*>(&__linkText));
452 }
453
454 result
455 _TextElementImpl::SetText(const Tizen::Base::String& text)
456 {
457         SysTryReturn(NID_GRP, text.IsEmpty() == false, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
458         SysTryReturn(NID_GRP, __pTextComponent, E_OPERATION_FAILED, E_OPERATION_FAILED, "E_OPERATION_FAILED] Not Construct yet");
459
460         result r = E_SUCCESS;
461
462         int index = -1;
463         __strText = text;
464
465         if (__pRegisteringEnrichedText)
466         {
467                 index = __pRegisteringEnrichedText->GetTextElementIndex(this);
468                 r = __pRegisteringEnrichedText->RemoveFromTextObject(this);
469         }
470
471         if (__pTextComponent)
472         {
473                 Destroy();
474         }
475
476         if ((__linkType == LINK_TYPE_NONE) && (__autoLink == 0))
477         {
478                 r = Create(__strText, __strText.GetLength(), TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
479
480                 if (r == E_SUCCESS)
481                 {
482                         SetTextColor(__foregroundColor);
483                         if (__bgColorEnable == true)
484                         {
485                                 SetBackgroundColor(__backgroundColor);
486                         }
487                         if (__outlineColorEnable == true)
488                         {
489                                 SetOutlineColor(__outlineColor);
490                         }
491                         if (__pFont)
492                         {
493                                 r = SetTextObjectFont(*__pFont);
494                         }
495                 }
496                 else
497                 {
498                         return r;
499                 }
500         }
501         else
502         {
503                 if (__isAuto == true)
504                 {
505                         r = CreateAutoLink(__strText, __autoLink);
506
507                         if (r == E_SUCCESS)
508                         {
509                                 _CutLinkType type = GetCutLinkType();
510                                 __linkType = ConvertObjectLinkType(type);
511
512                                 int len = GetLength();
513                                 Tizen::Base::String link;
514                                 __strText.SubString(GetAutolinkSrcOffset(), len, link);
515                                 __linkText = link;
516
517                                 SetTextColor(__foregroundColor);
518                                 if (__bgColorEnable == true)
519                                 {
520                                         SetBackgroundColor(__backgroundColor);
521                                 }
522                                 if (__outlineColorEnable == true)
523                                 {
524                                         SetOutlineColor(__outlineColor);
525                                 }
526                                 if (__pFont)
527                                 {
528                                         r = SetTextObjectFont(*__pFont);
529                                         if (r != E_SUCCESS)
530                                         {
531                                                 return r;
532                                         }
533                                 }
534                         }
535                         else
536                         {
537                                 return r;
538                         }
539                 }
540                 else
541                 {
542                         _CutLinkType type = ConvertObjectLinkType(__linkType);
543                         r = CreateUserLink(__strText, type);
544
545                         if (r == E_SUCCESS)
546                         {
547                                 SetTextColor(__foregroundColor);
548                                 if (__bgColorEnable == true)
549                                 {
550                                         SetBackgroundColor(__backgroundColor);
551                                 }
552                                 if (__outlineColorEnable == true)
553                                 {
554                                         SetOutlineColor(__outlineColor);
555                                 }
556                                 if (__pFont)
557                                 {
558                                         r = SetTextObjectFont(*__pFont);
559                                         if (r != E_SUCCESS)
560                                         {
561                                                 return r;
562                                         }
563                                 }
564                         }
565                         else
566                         {
567                                 return r;
568                         }
569                 }
570         }
571
572         if (__pRegisteringEnrichedText)
573         {
574                 r = __pRegisteringEnrichedText->InsertAtFromTextObject(index, this);
575                 if (r != E_SUCCESS)
576                 {
577                         return r;
578                 }
579         }
580
581         return E_SUCCESS;
582 }
583
584 result
585 _TextElementImpl::SetFont(const Tizen::Graphics::Font& font)
586 {
587         SysTryReturn(NID_GRP, _Util::CheckValidity(&font), E_INVALID_ARG, E_INVALID_ARG
588                         , "[E_INVALID_ARG] The infomation of the given font is invalid.");
589         SysTryReturn(NID_GRP, __pTextComponent, E_OPERATION_FAILED, E_OPERATION_FAILED, "E_OPERATION_FAILED] Not Construct yet");
590
591         result r = E_SUCCESS;
592         TextSimple* pTextElement = null;
593
594         if (__pFont)
595         {
596                 delete __pFont;
597                 __pFont = null;
598         }
599
600         __pFont = _FontImpl::CloneN(const_cast <Font&>(font));
601
602         if (__pFont)
603         {
604                 if (__pTextComponent->GetType() == TEXT_ELEMENT_TYPE_CUTLINK)
605                 {
606                         TextCutLink* pLinkTextElement = dynamic_cast < TextCutLink* >(__pTextComponent);
607                         if (pLinkTextElement)
608                         {
609                                 pLinkTextElement->SetEditModeEnable(true);
610                         }
611                 }
612
613                 pTextElement = dynamic_cast < TextSimple* >(__pTextComponent);
614                 if (pTextElement)
615                 {
616                         pTextElement->SetFont(const_cast <Font*>(__pFont));
617                 }
618         }
619         else
620         {
621                 r = E_INVALID_ARG;
622         }
623
624         return r;
625 }
626
627 result
628 _TextElementImpl::SetTextColor(const Tizen::Graphics::Color& color)
629 {
630         SysTryReturn(NID_GRP, __pTextComponent, E_OPERATION_FAILED, E_OPERATION_FAILED, "E_OPERATION_FAILED] Not Construct yet");
631
632         __foregroundColor = color;
633
634         TextSimple* pTextElement = null;
635
636         if (__pTextComponent->GetType() == TEXT_ELEMENT_TYPE_CUTLINK)
637         {
638                 TextCutLink* pLinkTextElement = dynamic_cast < TextCutLink* >(__pTextComponent);
639                 if (pLinkTextElement)
640                 {
641                         pLinkTextElement->SetEditModeEnable(true);
642                 }
643         }
644
645         pTextElement = dynamic_cast < TextSimple* >(__pTextComponent);
646         if (pTextElement)
647         {
648                 pTextElement->SetForegroundColor(color);
649         }
650
651         return E_SUCCESS;
652 }
653
654 result
655 _TextElementImpl::SetBackgroundColor(const Tizen::Graphics::Color& color)
656 {
657         SysTryReturn(NID_GRP, __pTextComponent, E_OPERATION_FAILED, E_OPERATION_FAILED, "E_OPERATION_FAILED] Not Construct yet");
658
659         __backgroundColor = color;
660         __bgColorEnable = true;
661
662         TextSimple* pTextElement = null;
663
664         if (__pTextComponent->GetType() == TEXT_ELEMENT_TYPE_CUTLINK)
665         {
666                 TextCutLink* pLinkTextElement = dynamic_cast < TextCutLink* >(__pTextComponent);
667                 if (pLinkTextElement)
668                 {
669                         pLinkTextElement->SetEditModeEnable(true);
670                 }
671         }
672
673         pTextElement = dynamic_cast < TextSimple* >(__pTextComponent);
674         if (pTextElement)
675         {
676                 pTextElement->SetBackGroundDrawingModeEnabled(true);
677                 pTextElement->SetBackgroundColor(color);
678         }
679
680         return E_SUCCESS;
681 }
682
683 result
684 _TextElementImpl::SetOutlineColor(const Tizen::Graphics::Color& color)
685 {
686         SysTryReturn(NID_GRP, __pTextComponent, E_OPERATION_FAILED, E_OPERATION_FAILED, "E_OPERATION_FAILED] Not Construct yet");
687
688         __outlineColor = color;
689         __outlineColorEnable = true;
690
691         TextSimple* pTextElement = null;
692
693         if (__pTextComponent->GetType() == TEXT_ELEMENT_TYPE_CUTLINK)
694         {
695                 TextCutLink* pLinkTextElement = dynamic_cast < TextCutLink* >(__pTextComponent);
696                 if (pLinkTextElement)
697                 {
698                         pLinkTextElement->SetEditModeEnable(true);
699                 }
700         }
701
702         pTextElement = dynamic_cast < TextSimple* >(__pTextComponent);
703         if (pTextElement)
704         {
705                 pTextElement->SetOutlineColor(color);
706         }
707
708         return E_SUCCESS;
709 }
710
711 Tizen::Base::String&
712 _TextElementImpl::GetText(void) const
713 {
714         return *(const_cast <String*>(&__strText));
715 }
716
717 Tizen::Graphics::Color
718 _TextElementImpl::GetTextColor(void) const
719 {
720         return __foregroundColor;
721 }
722
723 Tizen::Graphics::Color
724 _TextElementImpl::GetBackgroundColor(void) const
725 {
726         return __backgroundColor;
727 }
728
729 Tizen::Graphics::Color
730 _TextElementImpl::GetOutlineColor(void) const
731 {
732         return __outlineColor;
733 }
734
735 _CutLinkType
736 _TextElementImpl::ConvertObjectLinkType(Tizen::Base::Utility::LinkType type)
737 {
738         _CutLinkType cutLinkType;
739
740         switch (type)
741         {
742         case LINK_TYPE_URL:
743                 cutLinkType = TEXT_CUTLINK_TYPE_URL;
744                 break;
745
746         case LINK_TYPE_EMAIL:
747                 cutLinkType = TEXT_CUTLINK_TYPE_EMAIL;
748                 break;
749
750         case LINK_TYPE_TEL_NUM:
751                 cutLinkType = TEXT_CUTLINK_TYPE_PHONE_NUMBER;
752                 break;
753
754         default:
755                 cutLinkType = TEXT_CUTLINK_TYPE_INVALID;
756                 break;
757         }
758         return cutLinkType;
759 }
760
761 Tizen::Base::Utility::LinkType
762 _TextElementImpl::ConvertObjectLinkType(_CutLinkType type)
763 {
764         LinkType linkType;
765
766         switch (type)
767         {
768         case TEXT_CUTLINK_TYPE_URL:
769                 linkType = LINK_TYPE_URL;
770                 break;
771
772         case TEXT_CUTLINK_TYPE_EMAIL:
773                 linkType = LINK_TYPE_EMAIL;
774                 break;
775
776         case TEXT_CUTLINK_TYPE_PHONE_NUMBER:
777                 linkType = LINK_TYPE_TEL_NUM;
778                 break;
779
780         case TEXT_CUTLINK_TYPE_STREAMING_URL:
781                 linkType = LINK_TYPE_URL;
782                 break;
783
784         default:
785                 linkType = LINK_TYPE_NONE;
786                 break;
787         }
788         return linkType;
789 }
790
791 _EnrichedTextImpl*
792 _TextElementImpl::GetRegisteringEnrichedText(void) const
793 {
794         return __pRegisteringEnrichedText;
795 }
796
797 void
798 _TextElementImpl::SetRegisteringEnrichedText(_EnrichedTextImpl* pEnrichedTextImpl)
799 {
800         __pRegisteringEnrichedText = pEnrichedTextImpl;
801 }
802
803 const Tizen::Base::String&
804 _TextElementImpl::GetLinkText(void) const
805 {
806         return __linkText;
807 }
808
809 Tizen::Base::Utility::LinkType
810 _TextElementImpl::GetLinkType(void) const
811 {
812         return __linkType;
813 }
814
815 bool
816 _TextElementImpl::IsAuto(void) const
817 {
818         return __isAuto;
819 }
820
821 result
822 _TextElementImpl::Create(const Tizen::Base::String& text, int length, TextElementSourceType sourceType)
823 {
824         TextSimple* pTextElement = null;
825
826         pTextElement = new (std::nothrow) TextSimple((wchar_t*) text.GetPointer(), length, sourceType);
827
828         if (pTextElement == null)
829         {
830                 return E_OUT_OF_MEMORY;
831         }
832
833         __pTextComponent = pTextElement;
834
835         return E_SUCCESS;
836 }
837
838 result
839 _TextElementImpl::CreateUserLink(const Tizen::Base::String& text, _CutLinkType linkType)
840 {
841         LinkType cutLinkType = LINK_TYPE_NONE;
842         int len = text.GetLength();
843         TextCutLink* pTextElement = null;
844
845         switch (linkType)
846         {
847         case TEXT_CUTLINK_TYPE_URL:
848                 cutLinkType = LINK_TYPE_URL;
849                 break;
850
851         case TEXT_CUTLINK_TYPE_EMAIL:
852                 cutLinkType = LINK_TYPE_EMAIL;
853                 break;
854
855         case TEXT_CUTLINK_TYPE_PHONE_NUMBER:
856                 cutLinkType = LINK_TYPE_TEL_NUM;
857                 break;
858
859         case TEXT_CUTLINK_TYPE_STREAMING_URL:
860                 cutLinkType = LINK_TYPE_URL;
861                 break;
862
863         default:
864                 cutLinkType = LINK_TYPE_NONE;
865                 break;
866         }
867
868         pTextElement = new (std::nothrow) TextCutLink(true, cutLinkType, (wchar_t*) text.GetPointer(), len, TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
869
870         if (pTextElement == null)
871         {
872                 return E_OUT_OF_MEMORY;
873         }
874
875         __pTextComponent = pTextElement;
876
877         return E_SUCCESS;
878 }
879
880 result
881 _TextElementImpl::CreateAutoLink(const Tizen::Base::String& text, unsigned long autoLink)
882 {
883         result r = E_SUCCESS;
884         int len = text.GetLength();
885         wchar_t* pText = (wchar_t*) text.GetPointer();
886
887         if (autoLink != 0)
888         {
889                 TextLinkInfo* pTextLinkInfo = null;
890                 TextCutLinkParser* pParser = null;
891
892                 pParser = new (std::nothrow) TextCutLinkParser;
893                 if (!pParser)
894                 {
895                         return E_OUT_OF_MEMORY;
896                 }
897
898                 pTextLinkInfo = pParser->Parse(pText, len, 0);
899                 if (!pTextLinkInfo)
900                 {
901                         delete pParser;
902                         pParser = null;
903
904                         return E_INVALID_ARG;
905                 }
906
907                 __linkOffset = pTextLinkInfo->srcOffset;
908
909                 if ((ConvertLinkType(pTextLinkInfo->linkType) & autoLink) != 0)
910                 {
911                         TextCutLink* pTextElement = null;
912
913                         pTextElement = new (std::nothrow) TextCutLink(false, pTextLinkInfo->linkType, pText + pTextLinkInfo->srcOffset
914                                                                                                                                         , pTextLinkInfo->length, TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
915
916                         if (pTextElement == null)
917                         {
918                                 if (pParser)
919                                 {
920                                         delete pParser;
921                                         pParser = null;
922                                 }
923                                 return E_OUT_OF_MEMORY;
924                         }
925
926                         __pTextComponent = pTextElement;
927                         r = E_SUCCESS;
928                 }
929                 else
930                 {
931                         r = Create(text, len, TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
932                 }
933
934                 while (pTextLinkInfo)
935                 {
936                         TextLinkInfo* pCurrent = pTextLinkInfo;
937                         pTextLinkInfo = pTextLinkInfo->pNextLinkInfo;
938                         free(pCurrent);
939                 }
940
941                 if (pParser)
942                 {
943                         delete pParser;
944                         pParser = null;
945                 }
946         }
947         else
948         {
949                 r = Create(text, len, TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
950         }
951
952         return r;
953 }
954
955 void
956 _TextElementImpl::Destroy(void)
957 {
958         if (__pTextComponent)
959         {
960                 delete __pTextComponent;
961                 __pTextComponent = null;
962         }
963 }
964
965 _CutLinkType
966 _TextElementImpl::ConvertLinkType(LinkType type) const
967 {
968         _CutLinkType linkType = TEXT_CUTLINK_TYPE_INVALID;
969
970         switch (type)
971         {
972         case LINK_TYPE_URL:
973                 linkType = TEXT_CUTLINK_TYPE_URL;
974                 break;
975
976         case LINK_TYPE_EMAIL:
977                 linkType = TEXT_CUTLINK_TYPE_EMAIL;
978                 break;
979
980         case LINK_TYPE_TEL_NUM:
981                 linkType = TEXT_CUTLINK_TYPE_PHONE_NUMBER;
982                 break;
983
984         default:
985                 linkType = TEXT_CUTLINK_TYPE_INVALID;
986                 break;
987         }
988
989         return linkType;
990 }
991
992 _CutLinkType
993 _TextElementImpl::GetCutLinkType(void) const
994 {
995         _CutLinkType linkType = TEXT_CUTLINK_TYPE_INVALID;
996         TextCutLink* pTextElement = null;
997
998         if (__pTextComponent->GetType() != TEXT_ELEMENT_TYPE_CUTLINK)
999         {
1000                 return TEXT_CUTLINK_TYPE_INVALID;
1001         }
1002
1003         pTextElement = dynamic_cast < TextCutLink* >(__pTextComponent);
1004         if (pTextElement)
1005         {
1006                 linkType = ConvertLinkType(pTextElement->GetCutLinkType());
1007         }
1008
1009         return linkType;
1010 }
1011
1012 int
1013 _TextElementImpl::GetLength(void) const
1014 {
1015         return __pTextComponent->GetTextLength();
1016 }
1017
1018 int
1019 _TextElementImpl::GetAutolinkSrcOffset(void) const
1020 {
1021         return __linkOffset;
1022 }
1023
1024 result
1025 _TextElementImpl::SetTextObjectFont(const Tizen::Graphics::Font& font)
1026 {
1027         SysTryReturn(NID_GRP, &font, E_INVALID_ARG, E_INVALID_ARG
1028                         , "[E_INVALID_ARG] The infomation of the given font is invalid.");
1029
1030         SysTryReturn(NID_GRP, _Util::CheckValidity(&font), E_INVALID_ARG, E_INVALID_ARG
1031                         , "[E_INVALID_ARG] The infomation of the given font is invalid.");
1032
1033         result r = E_SUCCESS;
1034         Font* pFont = null;
1035         TextSimple* pTextElement = null;
1036
1037         pFont = const_cast <Font*>(&font);
1038         if (!pFont)
1039         {
1040                 return E_INVALID_ARG;
1041         }
1042
1043         if (__pTextComponent->GetType() == TEXT_ELEMENT_TYPE_CUTLINK)
1044         {
1045                 TextCutLink* pLinkTextElement = dynamic_cast < TextCutLink* >(__pTextComponent);
1046                 if (pLinkTextElement)
1047                 {
1048                         pLinkTextElement->SetEditModeEnable(true);
1049                 }
1050         }
1051
1052         pTextElement = dynamic_cast < TextSimple* >(__pTextComponent);
1053         if (pTextElement)
1054         {
1055                 pTextElement->SetFont(pFont);
1056         }
1057
1058         return r;
1059 }
1060
1061 _Text::TextElement*
1062 _TextElementImpl::GetComponent(void) const
1063 {
1064         return __pTextComponent;
1065 }
1066
1067
1068 const Tizen::Graphics::Font*
1069 _TextElementImpl::GetFont(void) const
1070 {
1071         return __pFont;
1072 }
1073
1074 unsigned long
1075 _TextElementImpl::GetAutoLink(void) const
1076 {
1077         return __autoLink;
1078 }
1079
1080 bool
1081 _TextElementImpl::IsBackgoundColorEnable(void) const
1082 {
1083         return __bgColorEnable;
1084 }
1085
1086 bool
1087 _TextElementImpl::IsOutlineColorEnable(void) const
1088 {
1089         return __outlineColorEnable;
1090 }
1091
1092 _TextElementImpl*
1093 _TextElementImpl::GetInstance(TextElement& element)
1094 {
1095         return (&element != null) ? element.__pImpl : null;
1096 }
1097
1098 const _TextElementImpl*
1099 _TextElementImpl::GetInstance(const TextElement& element)
1100 {
1101         return (&element != null) ? element.__pImpl : null;
1102 }
1103
1104 bool
1105 _TextElementImpl::IsConstructed(void) const
1106 {
1107         return (this->__pTextComponent != null);
1108 }
1109
1110 }} // Tizen::Graphics