2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
18 * @file CtSearchListItem.cpp
19 * @brief This is the implementation file for the SearchListItem class.
22 #include "CtContactsApp.h"
23 #include "CtSearchListItem.h"
25 using namespace Tizen::Base;
26 using namespace Tizen::Graphics;
27 using namespace Tizen::Ui::Controls;
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;
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;
41 SearchListItem::SearchListItem(const Tizen::Base::String& name, Tizen::Graphics::Bitmap* pThumbnail, const Tizen::Base::String& searchedText)
43 , __searchedText(searchedText)
44 , __pThumbnail(pThumbnail)
48 SearchListItem::~SearchListItem(void)
52 Tizen::Graphics::EnrichedText*
53 SearchListItem::GetDisplayEnrichedTextN(const Tizen::Graphics::Rectangle& rect, const Tizen::Base::String& text, Tizen::Ui::Controls::ListItemDrawingStatus itemStatus, bool isSearchingTextVisible)
55 int searchedTextIndex = 0;
56 String preSearchedText;
58 String postSearchedText;
59 String displayNameText;
60 EnrichedText* pEnrichedText = null;
61 TextElement* pSearchedTextElement = null;
62 TextElement* pPreNameTextElement = null;
63 TextElement* pPostNameTextElement = null;
65 Color searchedTextColor;
68 ContactsApp *pContactsApp = static_cast<ContactsApp*>(ContactsApp::GetInstance());
70 font.Construct(FONT_STYLE_PLAIN, pContactsApp->GetFontSize());
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);
78 if (itemStatus == LIST_ITEM_DRAWING_STATUS_NORMAL)
80 textColor = COLOR_TEXT_NORMAL;
81 searchedTextColor = COLOR_SEARCHED_TEXT;
83 else if (itemStatus == LIST_ITEM_DRAWING_STATUS_PRESSED)
85 textColor = COLOR_TEXT_PRESSED;
86 searchedTextColor = COLOR_TEXT_PRESSED;
89 if (isSearchingTextVisible == false)
91 if (text.GetLength() > 0)
93 pSearchedTextElement = new (std::nothrow) TextElement();
94 pSearchedTextElement->Construct(text);
95 pSearchedTextElement->SetTextColor(textColor);
96 pSearchedTextElement->SetFont(font);
97 pEnrichedText->Add(*pSearchedTextElement);
100 return pEnrichedText;
103 text.ToLowerCase(displayNameText);
104 displayNameText.IndexOf(__searchedText, 0, searchedTextIndex);
106 if (searchedTextIndex == -1)
108 preSearchedText = text;
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);
117 if (preSearchedText.GetLength() > 0)
119 pPreNameTextElement = new (std::nothrow) TextElement();
120 pPreNameTextElement->Construct(preSearchedText);
121 pPreNameTextElement->SetTextColor(textColor);
122 pPreNameTextElement->SetFont(font);
124 pEnrichedText->Add(*pPreNameTextElement);
127 if (searchedText.GetLength() > 0)
129 pSearchedTextElement = new (std::nothrow) TextElement();
130 pSearchedTextElement->Construct(searchedText);
131 pSearchedTextElement->SetTextColor(searchedTextColor);
132 pSearchedTextElement->SetFont(font);
134 pEnrichedText->Add(*pSearchedTextElement);
137 if (postSearchedText.GetLength() > 0)
139 pPostNameTextElement = new (std::nothrow) TextElement();
140 pPostNameTextElement->Construct(postSearchedText);
141 pPostNameTextElement->SetTextColor(textColor);
142 pPostNameTextElement->SetFont(font);
144 pEnrichedText->Add(*pPostNameTextElement);
147 return pEnrichedText;
151 SearchListItem::OnDraw(Tizen::Graphics::Canvas& canvas, const Tizen::Graphics::Rectangle& rect, Tizen::Ui::Controls::ListItemDrawingStatus itemStatus)
153 result r = E_SUCCESS;
155 EnrichedText* pEnrichedText = null;
156 Rectangle enrichedTextBounds = rect;
159 ContactsApp *pContactsApp = static_cast<ContactsApp*>(ContactsApp::GetInstance());
161 font.Construct(FONT_STYLE_PLAIN, pContactsApp->GetFontSize());
162 canvas.SetFont(font);
164 enrichedTextBounds.height = H_TEXT + pContactsApp->GetFontHeightOffset();
165 pEnrichedText = GetDisplayEnrichedTextN(enrichedTextBounds, __name, itemStatus);
167 r = canvas.DrawText(Point(rect.x + X_TEXT, Y_TEXT), *pEnrichedText);
168 TryCatch(r == E_SUCCESS, ret = false, "Unable to draw text.");
170 if (__pThumbnail != null)
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.");
177 if (pEnrichedText != null)
179 pEnrichedText->RemoveAllTextElements(true);
180 delete pEnrichedText;