xref: /haiku/src/kits/interface/ListItem.cpp (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2005, Haiku, Inc.
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:	BListItem is the base class for BListView's items,
26 //					BStringItem is a subclass of BListItem which draws a string.
27 //------------------------------------------------------------------------------
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include <ListItem.h>
32 #include <Message.h>
33 #include <View.h>
34 
35 
36 BListItem::BListItem(uint32 level, bool expanded)
37 	:	fWidth(0),
38 		fHeight(0),
39 		fLevel(level),
40 		fSelected(false),
41 		fEnabled(true),
42 		fExpanded(expanded),
43 		fHasSubitems(false),
44 		fVisible(true)
45 {
46 }
47 
48 
49 BListItem::BListItem(BMessage *data)
50 	:	BArchivable(data),
51 		fWidth(0),
52 		fHeight(0),
53 		fLevel(0),
54 		fSelected(false),
55 		fEnabled(true),
56 		fExpanded(false),
57 		fHasSubitems(false),
58 		fVisible(true)
59 {
60 	data->FindBool("_sel", &fSelected);
61 
62 	if (data->FindBool("_disable", &fEnabled) != B_OK)
63 		fEnabled = true;
64 	else
65 		fEnabled = false;
66 
67 	data->FindBool("_li_expanded", &fExpanded);
68 	data->FindInt32("_li_outline_level", (int32*)&fLevel);
69 }
70 
71 
72 BListItem::~BListItem()
73 {
74 }
75 
76 
77 status_t
78 BListItem::Archive(BMessage *archive, bool deep) const
79 {
80 	status_t status = BArchivable::Archive(archive, deep);
81 
82 	if (status == B_OK && fSelected)
83 		status = archive->AddBool("_sel", true);
84 
85 	if (status == B_OK && !fEnabled)
86 		status = archive->AddBool("_disable", true);
87 
88 	if (status == B_OK && fExpanded)
89 		status = archive->AddBool("_li_expanded", true);
90 
91 	if (status == B_OK && fLevel != 0)
92 		status = archive->AddInt32("_li_outline_level", fLevel);
93 
94 	return status;
95 }
96 
97 
98 float
99 BListItem::Height() const
100 {
101 	return fHeight;
102 }
103 
104 
105 float
106 BListItem::Width() const
107 {
108 	return fWidth;
109 }
110 
111 
112 bool
113 BListItem::IsSelected() const
114 {
115 	return fSelected;
116 }
117 
118 
119 void
120 BListItem::Select()
121 {
122 	fSelected = true;
123 }
124 
125 
126 void
127 BListItem::Deselect()
128 {
129 	fSelected = false;
130 }
131 
132 
133 void
134 BListItem::SetEnabled(bool on)
135 {
136 	fEnabled = on;
137 }
138 
139 
140 bool
141 BListItem::IsEnabled() const
142 {
143 	return fEnabled;
144 }
145 
146 
147 void
148 BListItem::SetHeight(float height)
149 {
150 	fHeight = height;
151 }
152 
153 
154 void
155 BListItem::SetWidth(float width)
156 {
157 	fWidth = width;
158 }
159 
160 
161 void
162 BListItem::Update(BView *owner, const BFont *font)
163 {
164 	font_height fh;
165 	font->GetHeight(&fh);
166 
167 	SetWidth(owner->Bounds().Width());
168 	SetHeight(fh.ascent + fh.descent + fh.leading);
169 }
170 
171 
172 status_t
173 BListItem::Perform(perform_code d, void *arg)
174 {
175 	return BArchivable::Perform(d, arg);
176 }
177 
178 
179 void
180 BListItem::SetExpanded(bool expanded)
181 {
182 	fExpanded = expanded;
183 }
184 
185 
186 bool
187 BListItem::IsExpanded() const
188 {
189 	return fExpanded;
190 }
191 
192 
193 uint32
194 BListItem::OutlineLevel() const
195 {
196 	return fLevel;
197 }
198 
199 
200 bool
201 BListItem::HasSubitems() const
202 {
203 	return fHasSubitems;
204 }
205 
206 
207 void BListItem::_ReservedListItem1() {}
208 void BListItem::_ReservedListItem2() {}
209 
210 
211 BListItem::BListItem(const BListItem &item)
212 {
213 }
214 
215 
216 BListItem &
217 BListItem::operator=(const BListItem &)
218 {
219 	return *this;
220 }
221 
222 
223 bool
224 BListItem::IsItemVisible() const
225 {
226 	return fVisible;
227 }
228 
229 
230 void
231 BListItem::SetItemVisible(bool visible)
232 {
233 	fVisible = visible;
234 }
235 
236 
237 // BStringItem
238 BStringItem::BStringItem(const char *text, uint32 level, bool expanded)
239 	:	BListItem(level, expanded),
240 		fText(NULL),
241 		fBaselineOffset(0)
242 {
243 	SetText(text);
244 }
245 
246 
247 BStringItem::BStringItem(BMessage *archive)
248 	:	BListItem(archive),
249 		fText(NULL),
250 		fBaselineOffset(0)
251 {
252 	const char *string;
253 
254 	if (archive->FindString("_label", &string) == B_OK)
255 		SetText(string);
256 }
257 
258 
259 BStringItem::~BStringItem()
260 {
261 	free(fText);
262 }
263 
264 
265 BArchivable	*
266 BStringItem::Instantiate(BMessage *archive)
267 {
268 	if (validate_instantiation(archive, "BStringItem"))
269 		return new BStringItem(archive);
270 	else
271 		return NULL;
272 }
273 
274 
275 status_t
276 BStringItem::Archive(BMessage *archive, bool deep) const
277 {
278 	status_t status = BListItem::Archive(archive);
279 
280 	if (status == B_OK && fText != NULL)
281 		status = archive->AddString("_label", fText);
282 
283 	return status;
284 }
285 
286 
287 void
288 BStringItem::DrawItem(BView *owner, BRect frame, bool complete)
289 {
290 	if (fText == NULL)
291 		return;
292 
293 	rgb_color highColor = owner->HighColor();
294 	rgb_color lowColor = owner->LowColor();
295 
296 	if (IsSelected() || complete) {
297 		if (IsSelected()) {
298 			owner->SetHighColor(tint_color(lowColor, B_DARKEN_2_TINT));
299 			owner->SetLowColor(owner->HighColor());
300 		} else
301 			owner->SetHighColor(lowColor);
302 
303 		owner->FillRect(frame);
304 	}
305 
306 	owner->MovePenTo(frame.left, frame.top + fBaselineOffset);
307 
308 	rgb_color black = {0, 0, 0, 255};
309 
310 	if (!IsEnabled())
311 		owner->SetHighColor(tint_color(black, B_LIGHTEN_2_TINT));
312 	else
313 		owner->SetHighColor(black);
314 
315 	owner->DrawString(fText);
316 
317 	owner->SetHighColor(highColor);
318 	owner->SetLowColor(lowColor);
319 }
320 
321 
322 void
323 BStringItem::SetText(const char *text)
324 {
325 	free(fText);
326 	fText = NULL;
327 
328 	if (text)
329 		fText = strdup(text);
330 }
331 
332 
333 const char *
334 BStringItem::Text() const
335 {
336 	return fText;
337 }
338 
339 
340 void
341 BStringItem::Update(BView *owner, const BFont *font)
342 {
343 	if (fText)
344 		SetWidth(owner->StringWidth(fText));
345 
346 	font_height fheight;
347 	font->GetHeight(&fheight);
348 
349 	fBaselineOffset = fheight.ascent + fheight.leading;
350 	SetHeight((float)ceil(fheight.ascent + fheight.descent +
351 		fheight.leading) + 4);
352 }
353 
354 
355 status_t
356 BStringItem::Perform(perform_code d, void *arg)
357 {
358 	return BListItem::Perform(d, arg);
359 }
360 
361 
362 void BStringItem::_ReservedStringItem1() {}
363 void BStringItem::_ReservedStringItem2() {}
364 
365 
366 BStringItem::BStringItem(const BStringItem &)
367 {
368 }
369 
370 
371 BStringItem	&
372 BStringItem::operator=(const BStringItem &)
373 {
374 	return *this;
375 }
376