Applied latest source code
[apps/native/preloaded/Clock.git] / src / ClkCityListCustomItem.cpp
1 //
2 //  Tizen Native SDK
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.1 (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    ClkCityListCustomItem
20  * @brief       This header file contains the declarations of the CityListCustomItem
21  *
22  */
23
24 #include <FUiControls.h>
25 #include "ClkCityListCustomItem.h"
26
27 using namespace Tizen::Base;
28 using namespace Tizen::Base::Collection;
29 using namespace Tizen::Base::Utility;
30 using namespace Tizen::Graphics;
31 using namespace Tizen::Ui::Controls;
32
33 static const int IDA_FORMAT_STRING = 101;
34 static const int IDA_FORMAT_SUB_STRING = 102;
35 static const int IDA_FORMAT_RIGHT_SUB_STRING = 103;
36
37 CityListCustomItem::CityListCustomItem(void)
38 :__rightSubText(""),
39  __subText(""),
40  __text("")
41
42 {
43
44 }
45
46 CityListCustomItem::~CityListCustomItem(void)
47 {
48
49 }
50
51 result
52 CityListCustomItem::Construct(int width, int height, bool searchBarModeOn)
53 {
54         result r = E_FAILURE;
55         __width = width;
56         __height = height;
57         searchBarModeInput = searchBarModeOn;
58         const Dimension dim(width, height);
59         r = CustomItem::Construct(dim, LIST_ANNEX_STYLE_NORMAL);
60         return r;
61
62 }
63
64 void
65 CityListCustomItem::SetText(String& text)
66 {
67         __text = text;
68 }
69
70 String
71 CityListCustomItem::GetText(void)
72 {
73         return __text;
74 }
75
76 void
77 CityListCustomItem::SetSubText(String& text)
78 {
79         __subText.Clear();
80         __subText.Append(text);
81 }
82
83 String
84 CityListCustomItem::GetSubText(void)
85 {
86         return __subText;
87 }
88
89 void
90 CityListCustomItem::SetRightSubText(String& text)
91 {
92         __rightSubText.Clear();
93         __rightSubText.Append(text);
94 }
95
96 String
97 CityListCustomItem::GetRightSubText(void)
98 {
99         return __rightSubText;
100 }
101
102 result
103 CityListCustomItem::Make(String& searchText, int mainTextFont)
104 {
105         result r = E_SUCCESS;
106         Color pressedTextColor = Color(255, 255, 255);
107         int textSize = mainTextFont;
108         int xMargin = 26;
109         int yMargin = 22;
110         int width_Element = __width - 60;
111         int width_text = width_Element - 200;
112         int width_rightSubText = 200;
113         int stringHeight = mainTextFont + 18;
114         int subStringHeight = 42;
115         int subStringFont = 32;
116         EnrichedText text;
117         EnrichedText subText;
118         EnrichedText rightSubText;
119         TextElement* textElement = null;
120         TextElement* subTextElement = null;
121         TextElement* rightSubTextElement = null;
122         int compareValue = -1;
123         Font textFont;
124         Font subTextFont;
125         String* token = NULL;
126         int index = 0;
127         int count = 0;
128         ArrayList tokens;
129         int length;
130         String* pStr = null;
131         Color textColor = Color(0, 0, 0);
132         Color redColor = Color(59, 115, 182);
133         String strSearchText = L"";
134         String str = L"";
135
136         text.Construct(Dimension(width_text, stringHeight));
137         subText.Construct(Dimension(width_Element, subStringHeight));
138         rightSubText.Construct(Dimension(width_Element,subStringHeight));
139
140         tokens.Construct();
141
142         if (searchText == L"")
143         {
144                 token = new String();
145
146                 if (token == null)
147                 {
148                         return E_FAILURE;
149                 }
150
151                 *token = __text;
152                 tokens.Add(token);
153         }
154         else
155         {
156                 length = __text.GetLength();
157
158                 strSearchText = searchText;
159                 strSearchText.ToLowerCase();
160
161                 while (length > 0)
162                 {
163                         token = new String();
164
165                         if (token == null)
166                         {
167                                 return E_FAILURE;
168                         }
169
170                         str = __text;
171                         str.ToLowerCase();
172
173                         r = str.IndexOf(strSearchText, 0, index);
174
175                         if(r == E_OBJ_NOT_FOUND)
176                         {
177                                 *token = __text;
178                                 tokens.Add(token);
179                                 __text.Remove(0, __text.GetLength());
180                         }
181                         else
182                         {
183
184                                 if(index == 0)
185                                 {
186                                         __text.SubString(0, searchText.GetLength(), *token);
187                                         __text.Remove(0,searchText.GetLength());
188                                         tokens.Add(token);
189
190                                 }
191                                 else
192                                 {
193                                         __text.SubString(0, index, *token);
194                                         tokens.Add(token);
195                                         __text.Remove(0, index);
196                                 }
197
198                         }
199                         length = __text.GetLength();
200                 }
201         }
202
203         textFont.Construct(FONT_STYLE_BOLD, textSize);
204
205         length = tokens.GetCount();
206         while (count < length)
207         {
208                 pStr = static_cast< String* > (tokens.GetAt(count));
209
210                 textElement = new (std::nothrow) TextElement();
211                 textElement->Construct();
212
213                 textElement->SetFont(textFont);
214                 textElement->SetText(*pStr);
215
216
217                 //compareValue = String::Compare(searchText, *pStr);
218                 compareValue = pStr->Equals(searchText, false);
219
220                 if (compareValue == 1)
221                 {
222                         textElement->SetTextColor(redColor);
223                 }
224                 else
225                 {
226                         textElement->SetTextColor(textColor);
227                 }
228
229                 text.Add(*textElement);
230                 count++;
231         }
232
233         AddElement(Rectangle(xMargin, yMargin, width_text, stringHeight), IDA_FORMAT_STRING, text);
234
235         text.RemoveAll(true);
236         tokens.RemoveAll(true);
237
238         if (searchText == L"")
239         {
240                 token = new String();
241
242                 if (token == null)
243                 {
244                         return E_FAILURE;
245                 }
246
247                 *token = __subText;
248                 tokens.Add(token);
249         }
250         else
251         {
252                 length = __subText.GetLength();
253
254                 strSearchText = searchText;
255                 strSearchText.ToLowerCase();
256
257                 while (length > 0)
258                 {
259                         token = new String();
260
261                         if (token == null)
262                         {
263                                 return E_FAILURE;
264                         }
265
266                         str = __subText;
267                         str.ToLowerCase();
268
269                         r = str.IndexOf(strSearchText, 0, index);
270
271                         if(r == E_OBJ_NOT_FOUND)
272                         {
273                                 *token = __subText;
274                                 tokens.Add(token);
275                                 __subText.Remove(0, __subText.GetLength());
276                         }
277                         else
278                         {
279
280                                 if(index == 0)
281                                 {
282                                         __subText.SubString(0, searchText.GetLength(), *token);
283                                         __subText.Remove(0,searchText.GetLength());
284                                         tokens.Add(token);
285
286                                 }
287                                 else
288                                 {
289                                         __subText.SubString(0, index, *token);
290                                         tokens.Add(token);
291                                         __subText.Remove(0, index);
292                                 }
293
294                         }
295                         length = __subText.GetLength();
296                 }
297         }
298
299         subTextFont.Construct(FONT_STYLE_BOLD, subStringFont);
300         count = 0;
301
302         if (searchBarModeInput == true)
303                 textColor = Color(0,0,0);
304         else
305                 textColor = Color(128,128,128);
306
307         length = tokens.GetCount();
308
309         rightSubText.SetHorizontalAlignment(TEXT_ALIGNMENT_RIGHT);
310
311         while (count < length)
312         {
313                 pStr = static_cast< String* > (tokens.GetAt(count));
314
315                 subTextElement = new (std::nothrow) TextElement();
316                 subTextElement->Construct();
317
318                 subTextElement->SetFont(subTextFont);
319                 subTextElement->SetText(*pStr);
320
321                 //compareValue = String::Compare(searchText, *pStr);
322
323                 compareValue = pStr->Equals(searchText, false);
324
325                 if (compareValue == 1)
326                 {
327                         subTextElement->SetTextColor(redColor);
328                 }
329                 else
330                 {
331                         subTextElement->SetTextColor(textColor);
332                 }
333
334                 subText.Add(*subTextElement);
335
336                 count++;
337         }
338
339         AddElement(Rectangle(xMargin, yMargin + stringHeight, width_Element, subStringHeight), IDA_FORMAT_SUB_STRING, subText);
340
341         subText.RemoveAll(true);
342         tokens.RemoveAll(true);
343
344         if(searchText == L"")
345         {
346                 token = new String();
347
348                 if (token == null)
349                 {
350                         return E_FAILURE;
351                 }
352
353                 *token = __rightSubText;
354                 tokens.Add(token);
355         }
356         else
357         {
358                 tokens.Add(new String("GMT"));
359                 __rightSubText.Remove(0, 3);
360                 length = __rightSubText.GetLength();
361
362                 while (length > 0)
363                 {
364                         token = new String();
365
366                         if (token == null)
367                         {
368                                 return E_FAILURE;
369                         }
370
371                         r = __rightSubText.IndexOf(searchText, 0, index);
372
373                         if (r == E_OBJ_NOT_FOUND)
374                         {
375                                 *token = __rightSubText;
376                                 tokens.Add(token);
377                                 __rightSubText.Remove(0, __rightSubText.GetLength());
378                         }
379                         else
380                         {
381
382                                 if (index == 0)
383                                 {
384                                         delete token;
385                                         token = null;
386                                 }
387                                 else
388                                 {
389                                         __rightSubText.SubString(0, index, *token);
390                                         tokens.Add(token);
391                                         __rightSubText.Remove(0, index);
392                                 }
393
394                                 __rightSubText.Remove(0,searchText.GetLength());
395                                 token = new String();
396
397                                 if (token == null)
398                                 {
399                                         return E_FAILURE;
400                                 }
401
402                                 *token = searchText;
403                                 tokens.Add(token);
404                         }
405                         length = __rightSubText.GetLength();
406
407                 }
408         }
409
410         count = 0;
411
412         if (searchBarModeInput == true)
413                 textColor = Color(0,0,0);
414         else
415                 textColor = Color(128,128,128);
416
417         length = tokens.GetCount();
418
419         rightSubText.SetHorizontalAlignment(TEXT_ALIGNMENT_RIGHT);
420
421         while (count < length)
422         {
423                 pStr = static_cast< String* > (tokens.GetAt(count));
424
425                 rightSubTextElement = new (std::nothrow) TextElement();
426                 rightSubTextElement->Construct();
427
428                 rightSubTextElement->SetFont(subTextFont);
429                 rightSubTextElement->SetText(*pStr);
430
431                 compareValue = String::Compare(searchText, *pStr);
432
433                 if (compareValue == 0 && count > 0)
434                 {
435                         rightSubTextElement->SetTextColor(redColor);
436                 }
437                 else
438                 {
439                         rightSubTextElement->SetTextColor(textColor);
440                 }
441
442                 rightSubText.Add(*rightSubTextElement);
443
444                 count++;
445         }
446
447         AddElement(Rectangle(xMargin + width_text - 18 /*+ 16*/, yMargin, width_rightSubText, subStringHeight), IDA_FORMAT_RIGHT_SUB_STRING, rightSubText);
448
449         rightSubText.RemoveAll(true);
450         tokens.RemoveAll(true);
451
452         return r;
453 }