Merge "Floating point support for animations" into tizen_2.1
[platform/framework/native/uifw.git] / src / graphics / text / FGrp_TextTextCutLink.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_TextTextCutLink.cpp
20  * @brief       This is the implementation file for TextCutLink class.
21  */
22
23 #include <new>
24 #include <FBaseSysLog.h>
25 #include "FGrp_TextCommon.h"
26 #include "FGrp_TextTextUtility.h"
27 #include "FGrp_TextTextElement.h"
28 #include "FGrp_TextTextSimple.h"
29 #include "FGrp_TextTextCutLink.h"
30 #include "FGrp_CanvasImpl.h"
31 #include "FGrp_CoordinateSystemUtils.h"
32
33 using namespace Tizen::Base::Utility;
34
35 namespace // unnamed
36 {
37         const int DEFAULT_FONT_SIZE = 42;
38 }
39
40 namespace Tizen { namespace Graphics
41 {
42
43 namespace _Text
44 {
45
46 TextCutLink::TextCutLink(bool isEditable, LinkType cutLinkType, const wchar_t* pText, int length, TextElementSourceType sourceType,
47                 Font* pFont, const Color& foregroundColor, const Color& backgroundColor, const Color&
48                 outlineColor)
49         : TextSimple(pText, length, sourceType, pFont, foregroundColor, backgroundColor, outlineColor)
50 {
51         __linkType = cutLinkType;
52         __isEditable = isEditable;
53         __isEdited = false;
54         __isSelected = false;
55         _type = TEXT_ELEMENT_TYPE_CUTLINK;
56         __isColorDefined = false;
57         __linkColor = Color::GetColor(COLOR_ID_BLUE);
58         __linkSelectedColor = Color::GetColor(COLOR_ID_BLUE);
59
60         __pLinkFont = new (std::nothrow) Font();
61         __pLinkFont->Construct(FONT_STYLE_PLAIN, DEFAULT_FONT_SIZE);
62 }
63
64 TextCutLink::~TextCutLink(void)
65 {
66         if (__pLinkFont)
67         {
68                 delete __pLinkFont;
69                 __pLinkFont = null;
70         }
71 }
72
73 result
74 TextCutLink::Draw(_CanvasImpl& canvasImpl, Rectangle& displayRect, int startTextIndex, int textLength, const TextObjectAlignment align,
75                                         const TextObjectActionType action)
76 {
77         SysTryReturn(NID_GRP
78                         , displayRect.x >= 0 && displayRect.y >= 0
79                         , E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
80
81         return TextSimple::Draw(canvasImpl, displayRect, startTextIndex, textLength, align, action);
82 }
83
84 result
85 TextCutLink::Draw(_CanvasImpl& canvasImpl, FloatRectangle& displayRect, int startTextIndex, int textLength, const TextObjectAlignment align,
86                                                 const TextObjectActionType action)
87 {
88         Rectangle intDisplayRect = _CoordinateSystemUtils::ConvertToInteger(displayRect);
89
90         return Draw(canvasImpl, intDisplayRect, startTextIndex, textLength, align, action);
91 }
92
93 TextElement*
94 TextCutLink::CloneN(TextComponentInfoValueType type, unsigned int value)
95 {
96         TextCutLink* pLinkElement = null;
97
98         pLinkElement = new (std::nothrow) TextCutLink(__isEditable, __linkType, _pText, _length,
99                         TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL, _pFont, _foregroundColor, _backgroundColor, _outlineColor);
100
101         SysTryReturn(NID_GRP
102                 , pLinkElement
103                 , null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
104
105         TextElement::CopyMembers(pLinkElement);
106
107         pLinkElement->__isEdited = __isEdited;
108         pLinkElement->SetValue(type, value);
109
110         return pLinkElement;
111 }
112
113 TextElement*
114 TextCutLink::CopyN(int textStartIndex, int textLength)
115 {
116         const wchar_t* pText = null;
117
118         TextCutLink* pLinkElement = null;
119
120         SysTryReturn(NID_GRP
121                         , textStartIndex < _length
122                         , null, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
123
124         if (textStartIndex + textLength > _length)
125         {
126                 textLength = _length - textStartIndex;
127         }
128
129         SysTryReturn(NID_GRP
130                         , textLength >= 0
131                         , null, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
132
133         pText = &_pText[_offset + textStartIndex];
134
135         pLinkElement = new (std::nothrow) TextCutLink(__isEditable, __linkType, pText, textLength, _sourceType,
136                         _pFont, _foregroundColor, _backgroundColor, _outlineColor);
137
138         SysTryReturn(NID_GRP
139                 , pLinkElement
140                 , null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
141
142         pLinkElement->_isBackgroundDrawingModeEnabled = _isBackgroundDrawingModeEnabled;
143         pLinkElement->__isEdited = __isEdited;
144
145         return pLinkElement;
146 }
147
148 result
149 TextCutLink::SetValue(TextComponentInfoValueType type, unsigned int value)
150 {
151         switch (type)
152         {
153         case SET_CUTLINK_TYPE:
154                 // fall through
155                 return E_INVALID_ARG;
156
157         case SET_CUTLINK_EDITABLE:
158                 // fall through
159                 return E_INVALID_ARG;
160
161         case SET_CUTLINK_EDITING:
162                 if (__isEditable)
163                 {
164                         __isEdited = (bool) value;
165                         return E_SUCCESS;
166                 }
167                 return E_INVALID_ARG;
168
169         default:
170                 break;
171         }
172
173         if (!__isEdited && type == SET_TEXT_OFFSET)
174         {
175                 return E_INVALID_ARG;
176         }
177
178         return TextSimple::SetValue(type, value);
179 }
180
181 unsigned int
182 TextCutLink::GetValue(TextComponentInfoValueType type) const
183 {
184         switch (type)
185         {
186         case SET_CUTLINK_TYPE:
187                 // fall through
188                 return (unsigned int) __linkType;
189
190         case SET_CUTLINK_EDITABLE:
191                 // fall through
192                 return (unsigned int) __isEditable;
193
194         case SET_CUTLINK_EDITING:
195                 // fall through
196                 return (unsigned int) __isEdited;
197
198         default:
199                 // fall through
200                 return TextSimple::GetValue(type);
201         }
202 }
203
204 void
205 TextCutLink::SetSelect(bool isSelected)
206 {
207         __isSelected = isSelected;
208 }
209
210 bool
211 TextCutLink::GetSelect(void) const
212 {
213         return __isSelected;
214 }
215
216 void
217 TextCutLink::SetUserColor(const Color& color, const Color& colorInSelect)
218 {
219         __isColorDefined = true;
220         __linkColor = color;
221         __linkSelectedColor = colorInSelect;
222 }
223
224 void
225 TextCutLink::ResetUserColor(void)
226 {
227         __isColorDefined = false;
228 }
229
230 result
231 TextCutLink::SetTextOffset(int offset)
232 {
233         SysTryReturn(NID_GRP
234                 , __isEdited == true
235                 , E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
236
237         return TextSimple::SetTextOffset(offset);
238 }
239
240 result
241 TextCutLink::ChangeTextOffset(wchar_t* pText, int gap)
242 {
243         SysTryReturn(NID_GRP
244                 , __isEdited == true
245                 , E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
246
247         return TextSimple::ChangeTextOffset(pText, gap);
248 }
249
250 Font*
251 TextCutLink::GetCurrentFont(void) const
252 {
253         if (__isEdited)
254         {
255                 return TextSimple::GetCurrentFont();
256         }
257
258         return __pLinkFont;
259 }
260
261 Color
262 TextCutLink::GetCurrentForegroundColor(void) const
263 {
264         if (__isEdited)
265         {
266                 return TextSimple::GetCurrentForegroundColor();
267         }
268
269         if (__isColorDefined)
270         {
271                 if (__isSelected)
272                 {
273                         return __linkSelectedColor;
274                 }
275
276                 return __linkColor;
277         }
278
279         if (__isSelected)
280         {
281                 return Color::GetColor(COLOR_ID_BLUE);
282         }
283
284         return Color::GetColor(COLOR_ID_BLUE);
285 }
286
287 Color
288 TextCutLink::GetCurrentBackgroundColor(void) const
289 {
290         if (__isEdited)
291         {
292                 return TextSimple::GetCurrentBackgroundColor();
293         }
294
295         if (__isSelected)
296         {
297                 return Color::GetColor(COLOR_ID_WHITE);
298         }
299
300         return Color::GetColor(COLOR_ID_WHITE);
301 }
302
303 Color
304 TextCutLink::GetCurrentOutlineColor(void) const
305 {
306         if (__isEdited)
307         {
308                 return TextSimple::GetCurrentOutlineColor();
309         }
310
311         return Color::GetColor(COLOR_ID_WHITE);
312 }
313
314 bool
315 TextCutLink::GetEditable(void) const
316 {
317         return __isEditable;
318 }
319
320 void
321 TextCutLink::SetEditModeEnable(bool enable)
322 {
323         __isEdited = enable;
324 }
325
326 bool
327 TextCutLink::IsEditModeEnable(void) const
328 {
329         return __isEdited;
330 }
331
332 Tizen::Base::Utility::LinkType
333 TextCutLink::GetCutLinkType(void) const
334 {
335         return __linkType;
336 }
337
338 }}} // Tizen::Graphics::_Text