xref: /haiku/src/kits/interface/ListItem.cpp (revision 43802569fe9b2e0b79bbe7a7c7d003fa2073f6ed)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2002, OpenBeOS
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		ListItem.cpp
23 //	Author:			Ulrich Wimboeck
24 //					Marc Flerackers (mflerackers@androme.be)
25 //	Description:	BListView represents a one-dimensional list view.
26 //------------------------------------------------------------------------------
27 
28 // Standard Includes -----------------------------------------------------------
29 
30 // System Includes -------------------------------------------------------------
31 #include <ListItem.h>
32 #include <View.h>
33 #include <Message.h>
34 #include <Errors.h>
35 
36 // Project Includes ------------------------------------------------------------
37 
38 // Local Includes --------------------------------------------------------------
39 
40 // Local Defines ---------------------------------------------------------------
41 
42 // Globals ---------------------------------------------------------------------
43 
44 //------------------------------------------------------------------------------
45 BListItem::BListItem(uint32 level, bool expanded)
46 	:	fWidth(0),
47 		fHeight(0),
48 		fLevel(level),
49 		fSelected(false),
50 		fEnabled(true),
51 		fExpanded(expanded),
52 		fHasSubitems(false),
53 		fVisible(true)
54 {
55 }
56 //------------------------------------------------------------------------------
57 BListItem::BListItem(BMessage *data)
58 	:	BArchivable(data),
59 		fWidth(0),
60 		fHeight(0),
61 		fLevel(0),
62 		fSelected(false),
63 		fEnabled(true),
64 		fExpanded(false),
65 		fHasSubitems(false),
66 		fVisible(true)
67 {
68 	data->FindBool("_sel", &fSelected);
69 
70 	if (data->FindBool("_disable", &fEnabled) != B_OK)
71 		fEnabled = true;
72 	else
73 		fEnabled = false;
74 
75 	data->FindBool("_li_expanded", &fExpanded);
76 	data->FindInt32("_li_outline_level", (int32*)&fLevel);
77 }
78 //------------------------------------------------------------------------------
79 BListItem::~BListItem()
80 {
81 
82 }
83 //------------------------------------------------------------------------------
84 status_t BListItem::Archive(BMessage *archive, bool deep) const
85 {
86 	BArchivable::Archive(archive, deep);
87 
88 	if (fSelected)
89 		archive->AddBool("_sel", true);
90 
91 	if (!fEnabled)
92 		archive->AddBool("_disable", true);
93 
94 	if (fExpanded)
95 		archive->AddBool("_li_expanded", true);
96 
97 	if (fLevel != 0)
98 		archive->AddInt32("_li_outline_level", fLevel);
99 
100 	return B_OK;
101 }
102 //------------------------------------------------------------------------------
103 float BListItem::Height() const
104 {
105 	return fHeight;
106 }
107 //------------------------------------------------------------------------------
108 float BListItem::Width() const
109 {
110 	return fWidth;
111 }
112 //------------------------------------------------------------------------------
113 bool BListItem::IsSelected() const
114 {
115 	return fSelected;
116 }
117 //------------------------------------------------------------------------------
118 void BListItem::Select()
119 {
120 	fSelected = true;
121 }
122 //------------------------------------------------------------------------------
123 void BListItem::Deselect()
124 {
125 	fSelected = false;
126 }
127 //------------------------------------------------------------------------------
128 void BListItem::SetEnabled(bool on)
129 {
130 	fEnabled = on;
131 }
132 //------------------------------------------------------------------------------
133 bool BListItem::IsEnabled() const
134 {
135 	return fEnabled;
136 }
137 //------------------------------------------------------------------------------
138 void BListItem::SetHeight(float height)
139 {
140 	fHeight = height;
141 }
142 //------------------------------------------------------------------------------
143 void BListItem::SetWidth(float width)
144 {
145 	fWidth = width;
146 }
147 //------------------------------------------------------------------------------
148 void BListItem::Update(BView *owner, const BFont *font)
149 {
150 	font_height fh;
151 	font->GetHeight(&fh);
152 
153 	SetWidth(owner->Bounds().Width());
154 	SetHeight(fh.ascent + fh.descent + fh.leading);
155 }
156 //------------------------------------------------------------------------------
157 status_t BListItem::Perform(perform_code d, void *arg)
158 {
159 	return BArchivable::Perform(d, arg);
160 }
161 //------------------------------------------------------------------------------
162 void BListItem::SetExpanded(bool expanded)
163 {
164 	fExpanded = expanded;
165 }
166 //------------------------------------------------------------------------------
167 bool BListItem::IsExpanded() const
168 {
169 	return fExpanded;
170 }
171 //------------------------------------------------------------------------------
172 uint32 BListItem::OutlineLevel() const
173 {
174 	return fLevel;
175 }
176 //------------------------------------------------------------------------------
177 bool BListItem::HasSubitems() const
178 {
179 	return fHasSubitems;
180 }
181 //------------------------------------------------------------------------------
182 void BListItem::_ReservedListItem1() {}
183 void BListItem::_ReservedListItem2() {}
184 //------------------------------------------------------------------------------
185 BListItem::BListItem(const BListItem &item)
186 {
187 }
188 //------------------------------------------------------------------------------
189 BListItem &BListItem::operator=(const BListItem &)
190 {
191 	return *this;
192 }
193 //------------------------------------------------------------------------------
194 bool BListItem::IsItemVisible() const
195 {
196 	return fVisible;
197 }
198 //------------------------------------------------------------------------------
199 void BListItem::SetItemVisible(bool visible)
200 {
201 	fVisible = visible;
202 }
203 //------------------------------------------------------------------------------
204 
205 
206 
207 //------------------------------------------------------------------------------
208 BStringItem::BStringItem(const char *text, uint32 level, bool expanded)
209 	:	BListItem(level, expanded),
210 		fText(NULL),
211 		fBaselineOffset(0)
212 {
213 	SetText(text);
214 }
215 //------------------------------------------------------------------------------
216 BStringItem::BStringItem(BMessage *archive)
217 	:	BListItem(archive),
218 		fText(NULL),
219 		fBaselineOffset(0)
220 {
221 	const char *string;
222 
223 	if (archive->FindString("_label", &string) == B_OK)
224 		SetText(string);
225 }
226 //------------------------------------------------------------------------------
227 BStringItem::~BStringItem()
228 {
229 	if (fText)
230 		free(fText);
231 }
232 //------------------------------------------------------------------------------
233 BArchivable	*BStringItem::Instantiate(BMessage *archive)
234 {
235 	if (validate_instantiation(archive, "BStringItem"))
236 		return new BStringItem(archive);
237 	else
238 		return NULL;
239 }
240 //------------------------------------------------------------------------------
241 status_t BStringItem::Archive(BMessage *archive, bool deep) const
242 {
243 	BListItem::Archive(archive);
244 
245 	if (fText)
246 		archive->AddString("_label", fText);
247 
248 	return B_OK;
249 }
250 //------------------------------------------------------------------------------
251 void BStringItem::DrawItem(BView *owner, BRect frame, bool complete)
252 {
253 	if (fText == NULL)
254 		return;
255 
256 	rgb_color highColor = owner->HighColor();
257 	rgb_color lowColor = owner->LowColor();
258 
259 	if (IsSelected() || complete)
260 	{
261 		if (IsSelected())
262 			owner->SetHighColor(tint_color(lowColor, B_DARKEN_2_TINT));
263 		else
264 			owner->SetHighColor(lowColor);
265 
266 		owner->FillRect(frame);
267 	}
268 
269 	owner->MovePenTo(frame.left, frame.top + fBaselineOffset);
270 
271 	rgb_color black = {0, 0, 0, 255};
272 
273 	if (!IsEnabled())
274 		owner->SetHighColor(tint_color(black, B_LIGHTEN_2_TINT));
275 	else
276 		owner->SetHighColor(black);
277 
278 	owner->DrawString(fText);
279 
280 	owner->SetHighColor(highColor);
281 	owner->SetLowColor(lowColor);
282 }
283 //------------------------------------------------------------------------------
284 void BStringItem::SetText(const char *text)
285 {
286 	if (fText)
287 	{
288 		free(fText);
289 		fText = NULL;
290 	}
291 
292 	if (text)
293 		fText = strdup(text);
294 }
295 //------------------------------------------------------------------------------
296 const char *BStringItem::Text() const
297 {
298 	return fText;
299 }
300 //------------------------------------------------------------------------------
301 void BStringItem::Update(BView *owner, const BFont *font)
302 {
303 	if (fText)
304 		SetWidth(owner->StringWidth(fText));
305 
306 	font_height fheight;
307 
308 	font->GetHeight(&fheight);
309 
310 	fBaselineOffset = fheight.ascent + fheight.leading;
311 	SetHeight((float)ceil(fheight.ascent + fheight.descent +
312 		fheight.leading) + 4);
313 }
314 //------------------------------------------------------------------------------
315 status_t BStringItem::Perform(perform_code d, void *arg)
316 {
317 	return B_ERROR;
318 }
319 //------------------------------------------------------------------------------
320 void BStringItem::_ReservedStringItem1() {}
321 void BStringItem::_ReservedStringItem2() {}
322 //------------------------------------------------------------------------------
323 BStringItem::BStringItem(const BStringItem &)
324 {
325 }
326 //------------------------------------------------------------------------------
327 BStringItem	&BStringItem::operator=(const BStringItem &)
328 {
329 	return *this;
330 }
331 //------------------------------------------------------------------------------
332 
333 /*
334  * $Log $
335  *
336  * $Id  $
337  *
338  */
339