xref: /haiku/src/kits/interface/StringItem.cpp (revision 8c6d549022e6da7ee267e3b4e5e230d446aef5ad)
1 /*
2  * Copyright 2001-2009, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Ulrich Wimboeck
7  *		Marc Flerackers (mflerackers@androme.be)
8  *		Rene Gollent
9  */
10 
11 
12 #include <StringItem.h>
13 
14 #include <stdlib.h>
15 #include <string.h>
16 
17 #include <ControlLook.h>
18 #include <Message.h>
19 #include <View.h>
20 
21 
22 BStringItem::BStringItem(const char* text, uint32 level, bool expanded)
23 	: BListItem(level, expanded),
24 	fText(NULL),
25 	fBaselineOffset(0)
26 {
27 	SetText(text);
28 }
29 
30 
31 BStringItem::BStringItem(BMessage* archive)
32 	: BListItem(archive),
33 	fText(NULL),
34 	fBaselineOffset(0)
35 {
36 	const char* string;
37 	if (archive->FindString("_label", &string) == B_OK)
38 		SetText(string);
39 }
40 
41 
42 BStringItem::~BStringItem()
43 {
44 	free(fText);
45 }
46 
47 
48 BArchivable*
49 BStringItem::Instantiate(BMessage* archive)
50 {
51 	if (validate_instantiation(archive, "BStringItem"))
52 		return new BStringItem(archive);
53 
54 	return NULL;
55 }
56 
57 
58 status_t
59 BStringItem::Archive(BMessage *archive, bool deep) const
60 {
61 	status_t status = BListItem::Archive(archive);
62 
63 	if (status == B_OK && fText != NULL)
64 		status = archive->AddString("_label", fText);
65 
66 	return status;
67 }
68 
69 
70 void
71 BStringItem::DrawItem(BView *owner, BRect frame, bool complete)
72 {
73 	if (fText == NULL)
74 		return;
75 
76 	rgb_color highColor = owner->HighColor();
77 	rgb_color lowColor = owner->LowColor();
78 
79 	if (IsSelected() || complete) {
80 		rgb_color color;
81 		if (IsSelected())
82 			color = ui_color(B_LIST_SELECTED_BACKGROUND_COLOR);
83 		else
84 			color = owner->ViewColor();
85 
86 		owner->SetLowColor(color);
87 		owner->SetHighColor(color);
88 		owner->FillRect(frame);
89 	} else
90 		owner->SetLowColor(owner->ViewColor());
91 
92 	owner->MovePenTo(frame.left + be_control_look->DefaultLabelSpacing(),
93 		frame.top + fBaselineOffset);
94 
95 	if (!IsEnabled()) {
96 		rgb_color textColor = ui_color(B_LIST_ITEM_TEXT_COLOR);
97 		if (textColor.red + textColor.green + textColor.blue > 128 * 3)
98 			owner->SetHighColor(tint_color(textColor, B_DARKEN_2_TINT));
99 		else
100 			owner->SetHighColor(tint_color(textColor, B_LIGHTEN_2_TINT));
101 	} else {
102 		if (IsSelected())
103 			owner->SetHighColor(ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR));
104 		else
105 			owner->SetHighColor(ui_color(B_LIST_ITEM_TEXT_COLOR));
106 	}
107 
108 	owner->DrawString(fText);
109 
110 	owner->SetHighColor(highColor);
111 	owner->SetLowColor(lowColor);
112 }
113 
114 
115 void
116 BStringItem::SetText(const char *text)
117 {
118 	free(fText);
119 	fText = NULL;
120 
121 	if (text)
122 		fText = strdup(text);
123 }
124 
125 
126 const char*
127 BStringItem::Text() const
128 {
129 	return fText;
130 }
131 
132 
133 void
134 BStringItem::Update(BView *owner, const BFont *font)
135 {
136 	if (fText != NULL) {
137 		SetWidth(font->StringWidth(fText)
138 			+ be_control_look->DefaultLabelSpacing());
139 	}
140 
141 	font_height fheight;
142 	font->GetHeight(&fheight);
143 
144 	fBaselineOffset = 2 + ceilf(fheight.ascent + fheight.leading / 2);
145 
146 	SetHeight(ceilf(fheight.ascent) + ceilf(fheight.descent)
147 		+ ceilf(fheight.leading) + 4);
148 }
149 
150 
151 status_t
152 BStringItem::Perform(perform_code d, void *arg)
153 {
154 	return BListItem::Perform(d, arg);
155 }
156 
157 
158 float
159 BStringItem::BaselineOffset() const
160 {
161 	return fBaselineOffset;
162 }
163 
164 
165 void BStringItem::_ReservedStringItem1() {}
166 void BStringItem::_ReservedStringItem2() {}
167 
168 
169 BStringItem::BStringItem(const BStringItem &)
170 {
171 }
172 
173 
174 BStringItem	&
175 BStringItem::operator=(const BStringItem &)
176 {
177 	return *this;
178 }
179