modify license, permission and remove ^M char
[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 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        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, displayRect.x >= 0 && displayRect.y >= 0
78                         , E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
79
80         return Draw(canvasImpl, displayRect, startTextIndex, textLength, align, action);
81 }
82
83 result
84 TextCutLink::Draw(_CanvasImpl& canvasImpl, FloatRectangle& displayRect, int startTextIndex, int textLength, const TextObjectAlignment align,
85                                                 const TextObjectActionType action)
86 {
87         return TextSimple::Draw(canvasImpl, displayRect, startTextIndex, textLength, align, action);
88 }
89
90 TextElement*
91 TextCutLink::CloneN(TextComponentInfoValueType type, unsigned int value)
92 {
93         TextCutLink* pLinkElement = null;
94
95         pLinkElement = new (std::nothrow) TextCutLink(__isEditable, __linkType, _pText, _length,
96                         TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL, _pFont, _foregroundColor, _backgroundColor, _outlineColor);
97
98         SysTryReturn(NID_GRP, pLinkElement, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
99
100         TextElement::CopyMembers(pLinkElement);
101
102         pLinkElement->__isEdited = __isEdited;
103         pLinkElement->SetValue(type, value);
104
105         return pLinkElement;
106 }
107
108 TextElement*
109 TextCutLink::CopyN(int textStartIndex, int textLength)
110 {
111         const wchar_t* pText = null;
112
113         TextCutLink* pLinkElement = null;
114
115         SysTryReturn(NID_GRP, textStartIndex < _length, null, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
116
117         if (textStartIndex + textLength > _length)
118         {
119                 textLength = _length - textStartIndex;
120         }
121
122         SysTryReturn(NID_GRP, textLength >= 0, null, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
123
124         pText = &_pText[_offset + textStartIndex];
125
126         pLinkElement = new (std::nothrow) TextCutLink(__isEditable, __linkType, pText, textLength, _sourceType,
127                         _pFont, _foregroundColor, _backgroundColor, _outlineColor);
128
129         SysTryReturn(NID_GRP, pLinkElement, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
130
131         pLinkElement->_isBackgroundDrawingModeEnabled = _isBackgroundDrawingModeEnabled;
132         pLinkElement->__isEdited = __isEdited;
133
134         return pLinkElement;
135 }
136
137 result
138 TextCutLink::SetValue(TextComponentInfoValueType type, unsigned int value)
139 {
140         switch (type)
141         {
142         case SET_CUTLINK_TYPE:
143                 // fall through
144                 return E_INVALID_ARG;
145
146         case SET_CUTLINK_EDITABLE:
147                 // fall through
148                 return E_INVALID_ARG;
149
150         case SET_CUTLINK_EDITING:
151                 if (__isEditable)
152                 {
153                         __isEdited = (bool) value;
154                         return E_SUCCESS;
155                 }
156                 return E_INVALID_ARG;
157
158         default:
159                 break;
160         }
161
162         if (!__isEdited && type == SET_TEXT_OFFSET)
163         {
164                 return E_INVALID_ARG;
165         }
166
167         return TextSimple::SetValue(type, value);
168 }
169
170 unsigned int
171 TextCutLink::GetValue(TextComponentInfoValueType type) const
172 {
173         switch (type)
174         {
175         case SET_CUTLINK_TYPE:
176                 // fall through
177                 return (unsigned int) __linkType;
178
179         case SET_CUTLINK_EDITABLE:
180                 // fall through
181                 return (unsigned int) __isEditable;
182
183         case SET_CUTLINK_EDITING:
184                 // fall through
185                 return (unsigned int) __isEdited;
186
187         default:
188                 // fall through
189                 return TextSimple::GetValue(type);
190         }
191 }
192
193 void
194 TextCutLink::SetSelect(bool isSelected)
195 {
196         __isSelected = isSelected;
197 }
198
199 bool
200 TextCutLink::GetSelect(void) const
201 {
202         return __isSelected;
203 }
204
205 void
206 TextCutLink::SetUserColor(const Color& color, const Color& colorInSelect)
207 {
208         __isColorDefined = true;
209         __linkColor = color;
210         __linkSelectedColor = colorInSelect;
211 }
212
213 void
214 TextCutLink::ResetUserColor(void)
215 {
216         __isColorDefined = false;
217 }
218
219 result
220 TextCutLink::SetTextOffset(int offset)
221 {
222         SysTryReturn(NID_GRP, __isEdited == true, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
223
224         return TextSimple::SetTextOffset(offset);
225 }
226
227 result
228 TextCutLink::ChangeTextOffset(wchar_t* pText, int gap)
229 {
230         SysTryReturn(NID_GRP, __isEdited == true, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
231
232         return TextSimple::ChangeTextOffset(pText, gap);
233 }
234
235 Font*
236 TextCutLink::GetCurrentFont(void) const
237 {
238         if (__isEdited)
239         {
240                 return TextSimple::GetCurrentFont();
241         }
242
243         return __pLinkFont;
244 }
245
246 Color
247 TextCutLink::GetCurrentForegroundColor(void) const
248 {
249         if (__isEdited)
250         {
251                 return TextSimple::GetCurrentForegroundColor();
252         }
253
254         if (__isColorDefined)
255         {
256                 if (__isSelected)
257                 {
258                         return __linkSelectedColor;
259                 }
260
261                 return __linkColor;
262         }
263
264         if (__isSelected)
265         {
266                 return Color::GetColor(COLOR_ID_BLUE);
267         }
268
269         return Color::GetColor(COLOR_ID_BLUE);
270 }
271
272 Color
273 TextCutLink::GetCurrentBackgroundColor(void) const
274 {
275         if (__isEdited)
276         {
277                 return TextSimple::GetCurrentBackgroundColor();
278         }
279
280         if (__isSelected)
281         {
282                 return Color::GetColor(COLOR_ID_WHITE);
283         }
284
285         return Color::GetColor(COLOR_ID_WHITE);
286 }
287
288 Color
289 TextCutLink::GetCurrentOutlineColor(void) const
290 {
291         if (__isEdited)
292         {
293                 return TextSimple::GetCurrentOutlineColor();
294         }
295
296         return Color::GetColor(COLOR_ID_WHITE);
297 }
298
299 bool
300 TextCutLink::GetEditable(void) const
301 {
302         return __isEditable;
303 }
304
305 void
306 TextCutLink::SetEditModeEnable(bool enable)
307 {
308         __isEdited = enable;
309 }
310
311 bool
312 TextCutLink::IsEditModeEnable(void) const
313 {
314         return __isEdited;
315 }
316
317 Tizen::Base::Utility::LinkType
318 TextCutLink::GetCutLinkType(void) const
319 {
320         return __linkType;
321 }
322
323 }}} // Tizen::Graphics::_Text