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