Initialize Tizen 2.3
[apps/osp/Contacts.git] / src / CtSearchListItem.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file        CtSearchListItem.cpp
19  * @brief       This is the implementation file for the SearchListItem class.
20  */
21
22 #include "CtContactsApp.h"
23 #include "CtSearchListItem.h"
24
25 using namespace Tizen::Base;
26 using namespace Tizen::Graphics;
27 using namespace Tizen::Ui::Controls;
28
29 static const int W_THUMBNAIL_AREA = 134;
30 static const int Y_THUMBNAIL = 2;
31 static const int W_THUMBNAIL = 108;
32 static const int H_THUMBNAIL = 108;
33 static const int X_TEXT = 26;
34 static const int H_TEXT = 80;
35 static const int Y_TEXT = 16;
36
37 static const unsigned int COLOR_TEXT_NORMAL = Color32<0, 0, 0>::Value;
38 static const unsigned int COLOR_TEXT_PRESSED = Color32<255, 255, 255>::Value;
39 static const unsigned int COLOR_SEARCHED_TEXT = Color32<59, 115, 182>::Value;
40
41 SearchListItem::SearchListItem(const Tizen::Base::String& name, Tizen::Graphics::Bitmap* pThumbnail, const Tizen::Base::String& searchedText)
42 : __name(name)
43 , __searchedText(searchedText)
44 , __pThumbnail(pThumbnail)
45 {
46 }
47
48 SearchListItem::~SearchListItem(void)
49 {
50 }
51
52 Tizen::Graphics::EnrichedText*
53 SearchListItem::GetDisplayEnrichedTextN(const Tizen::Graphics::Rectangle& rect, const Tizen::Base::String& text, Tizen::Ui::Controls::ListItemDrawingStatus itemStatus, bool isSearchingTextVisible)
54 {
55         int searchedTextIndex = 0;
56         String preSearchedText;
57         String searchedText;
58         String postSearchedText;
59         String displayNameText;
60         EnrichedText* pEnrichedText = null;
61         TextElement* pSearchedTextElement = null;
62         TextElement* pPreNameTextElement = null;
63         TextElement* pPostNameTextElement = null;
64         Color textColor;
65         Color searchedTextColor;
66         Font font;
67
68         ContactsApp *pContactsApp = static_cast<ContactsApp*>(ContactsApp::GetInstance());
69
70         font.Construct(FONT_STYLE_PLAIN, pContactsApp->GetFontSize());
71
72         pEnrichedText = new (std::nothrow) EnrichedText();
73         pEnrichedText->Construct(Dimension(rect.width - (rect.x + X_TEXT + W_THUMBNAIL_AREA), rect.height));
74         pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_MIDDLE);
75         pEnrichedText->SetTextWrapStyle(TEXT_WRAP_CHARACTER_WRAP);
76         pEnrichedText->SetTextAbbreviationEnabled(true);
77
78         if (itemStatus == LIST_ITEM_DRAWING_STATUS_NORMAL)
79         {
80                 textColor = COLOR_TEXT_NORMAL;
81                 searchedTextColor = COLOR_SEARCHED_TEXT;
82         }
83         else if (itemStatus == LIST_ITEM_DRAWING_STATUS_PRESSED)
84         {
85                 textColor = COLOR_TEXT_PRESSED;
86                 searchedTextColor = COLOR_TEXT_PRESSED;
87         }
88
89         if (isSearchingTextVisible == false)
90         {
91                 if (text.GetLength() > 0)
92                 {
93                         pSearchedTextElement = new (std::nothrow) TextElement();
94                         pSearchedTextElement->Construct(text);
95                         pSearchedTextElement->SetTextColor(textColor);
96                         pSearchedTextElement->SetFont(font);
97                         pEnrichedText->Add(*pSearchedTextElement);
98                 }
99
100                 return pEnrichedText;
101         }
102
103         text.ToLowerCase(displayNameText);
104         displayNameText.IndexOf(__searchedText, 0, searchedTextIndex);
105
106         if (searchedTextIndex == -1)
107         {
108                 preSearchedText = text;
109         }
110         else
111         {
112                 text.SubString(0, searchedTextIndex, preSearchedText);
113                 text.SubString(searchedTextIndex, __searchedText.GetLength(), searchedText);
114                 text.SubString(searchedTextIndex + __searchedText.GetLength(), text.GetLength() - (searchedTextIndex + __searchedText.GetLength()), postSearchedText);
115         }
116
117         if (preSearchedText.GetLength() > 0)
118         {
119                 pPreNameTextElement = new (std::nothrow) TextElement();
120                 pPreNameTextElement->Construct(preSearchedText);
121                 pPreNameTextElement->SetTextColor(textColor);
122                 pPreNameTextElement->SetFont(font);
123
124                 pEnrichedText->Add(*pPreNameTextElement);
125         }
126
127         if (searchedText.GetLength() > 0)
128         {
129                 pSearchedTextElement = new (std::nothrow) TextElement();
130                 pSearchedTextElement->Construct(searchedText);
131                 pSearchedTextElement->SetTextColor(searchedTextColor);
132                 pSearchedTextElement->SetFont(font);
133
134                 pEnrichedText->Add(*pSearchedTextElement);
135         }
136
137         if (postSearchedText.GetLength() > 0)
138         {
139                 pPostNameTextElement = new (std::nothrow) TextElement();
140                 pPostNameTextElement->Construct(postSearchedText);
141                 pPostNameTextElement->SetTextColor(textColor);
142                 pPostNameTextElement->SetFont(font);
143
144                 pEnrichedText->Add(*pPostNameTextElement);
145         }
146
147         return pEnrichedText;
148 }
149
150 bool
151 SearchListItem::OnDraw(Tizen::Graphics::Canvas& canvas, const Tizen::Graphics::Rectangle& rect, Tizen::Ui::Controls::ListItemDrawingStatus itemStatus)
152 {
153         result r = E_SUCCESS;
154         bool ret = true;
155         EnrichedText* pEnrichedText = null;
156         Rectangle enrichedTextBounds = rect;
157         Font font;
158
159         ContactsApp *pContactsApp = static_cast<ContactsApp*>(ContactsApp::GetInstance());
160
161         font.Construct(FONT_STYLE_PLAIN, pContactsApp->GetFontSize());
162         canvas.SetFont(font);
163
164         enrichedTextBounds.height = H_TEXT + pContactsApp->GetFontHeightOffset();
165         pEnrichedText = GetDisplayEnrichedTextN(enrichedTextBounds, __name, itemStatus);
166
167         r = canvas.DrawText(Point(rect.x + X_TEXT, Y_TEXT), *pEnrichedText);
168         TryCatch(r == E_SUCCESS, ret = false, "Unable to draw text.");
169
170         if (__pThumbnail != null)
171         {
172                 r = canvas.DrawBitmap(Rectangle(rect.width - W_THUMBNAIL_AREA, (pContactsApp->GetListItemSingleLineHeight() - H_THUMBNAIL)/2 , W_THUMBNAIL, H_THUMBNAIL), *__pThumbnail);
173                 TryCatch(r == E_SUCCESS, ret = false, "Unable to draw a thumbnail.");
174         }
175
176 CATCH:
177         if (pEnrichedText != null)
178         {
179                 pEnrichedText->RemoveAllTextElements(true);
180                 delete pEnrichedText;
181         }
182
183         return ret;
184 }