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