xref: /haiku/src/kits/interface/ListItem.cpp (revision 52a380120846174213ccce9c4aab0dda17c72083)
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 //  File:           ListItem.h
4 //
5 //  Description:    BListView represents a one-dimensional list view.
6 //
7 //  Copyright 2001, Ulrich Wimboeck
8 //
9 ////////////////////////////////////////////////////////////////////////////////
10 
11 #include <Font.h>
12 #include <Message.h>
13 #include <View.h>
14 #include "MyListItem.h"
15 
16 /*----------------------------------------------------------------*/
17 /*----- BListItem class ------------------------------------------*/
18 
19 /**
20  * The constructors create a new BListItem object. The level and
21  * expanded arguments are only used if the item is added to a
22  * BOutlineListView object; see BOutlineListView::AddItem() for
23  * more information. The destructor is empty.
24  *
25  * @param outlineLevel Level of the
26  */
27 
28 BListItem::BListItem(uint32 outlineLevel /* = 0 */, bool expanded /* = true */)
29  : fWidth(0), fHeight(0), fLevel(outlineLevel), fSelected(false), fEnabled(true),
30    fExpanded(expanded), fHasSubItems(false), fVisible(true)
31 {
32 }
33 
34 BListItem::BListItem(BMessage* data)
35  : BArchivable(data), // first call the constructor of the base class
36    fWidth(0), fHeight(0), fLevel(0), fSelected(false), fEnabled(true),
37    fExpanded(true), fHasSubItems(false), fVisible(true)
38 {
39   // get the data from the message
40 
41   if (data->FindBool("_sel", &fSelected) != B_OK)
42     fSelected = false ;
43 
44   if (data->FindBool("_disable", !fEnabled) != B_OK)
45     fEnabled = true ;
46 
47   if (data->FindBool("_li_expanded", fExpanded) != B_OK)
48     fExpanded = true ;
49 
50   if (data->FindInt32("_li_outline_level", fLevel) != B_OK)
51     fLevel = 0 ;
52 }
53 
54 BListItem::~BListItem()
55 {
56   fWidth = 0 ;
57   fHeight = 0 ;
58   fLevel = 0 ;
59   fSelected = false ;
60   fEnabled = false ;
61 }
62 
63 status_t
64 BListItem::Archive(BMessage* data, bool deep /* = true */) const
65 {
66   status_t tReturn = BArchivable::Archive(data, deep) ;
67 
68   if (tReturn != B_OK)
69     return tReturn ;
70 
71   tReturn = data->AddString("class", "BListItem") ;
72 
73   if (tReturn != B_OK)
74     return tReturn ;
75 
76   if (fSelected == true)
77   {
78     tReturn = data->AddBool("_sel", fSelected) ;
79 
80     if (tReturn != B_OK)
81       return tReturn ;
82   }
83 
84   if (fEnabled == false)
85   {
86     tReturn = data->AddBool("_disable", !fEnabled) ;
87 
88     if (tReturn != B_OK)
89       return tReturn ;
90   }
91 
92   if (!fExpanded) {
93     tReturn = data->AddInt32("_li_outline_level", fExpanded) ;
94   }
95 
96   return tReturn ;
97 }
98 
99 float
100 BListItem::Height() const
101 {
102   return fHeight ;
103 }
104 
105 float
106 BListItem::Width() const
107 {
108   return fWidth ;
109 }
110 
111 //inline
112 bool
113 BListItem::IsSelected() const
114 {
115   return fSelected ;
116 }
117 
118 //inline
119 void
120 BListItem::Select()
121 {
122   fSelected = true ;
123 }
124 
125 //inline
126 void
127 BListItem::Deselect()
128 {
129   fSelected = false ;
130 }
131 
132 //inline
133 void
134 BListItem::SetEnabled(bool on)
135 {
136   fEnabled = on ;
137 }
138 
139 //inline
140 bool
141 BListItem::IsEnabled() const
142 {
143   return fEnabled ;
144 }
145 
146 //inline void
147 void BListItem::SetHeight(float height)
148 {
149   fHeight = height ;
150 }
151 
152 //inline
153 void
154 BListItem::SetWidth(float width)
155 {
156   fWidth = width ;
157 }
158 
159 void
160 BListItem::Update(BView* owner, const BFont* font)
161 {
162   // Set the width of the item to the width of the owner
163   BRect rect(owner->Frame()) ;
164   SetWidth(rect.right - rect.left) ;
165 
166   // Set the height of the item to the height of the font
167   font_height height ;
168   font->GetHeight(&height) ;
169   SetHeight(height.leading + height.ascent + height.descent);
170 }
171 
172 status_t
173 BListItem::Perform(perform_code d, void* arg)
174 {
175   status_t tReturn = B_OK ;
176   return tReturn ;
177 }
178 
179 inline uint32
180 BListItem::OutlineLevel() const
181 {
182   return fLevel ;
183 }
184