xref: /haiku/src/kits/interface/StringItem.cpp (revision 7749d0bb0c358a3279b1b9cc76d8376e900130a5)
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 <Message.h>
18 #include <View.h>
19 
20 
21 BStringItem::BStringItem(const char* text, uint32 level, bool expanded)
22 	: BListItem(level, expanded),
23 	fText(NULL),
24 	fBaselineOffset(0)
25 {
26 	SetText(text);
27 }
28 
29 
30 BStringItem::BStringItem(BMessage* archive)
31 	: BListItem(archive),
32 	fText(NULL),
33 	fBaselineOffset(0)
34 {
35 	const char* string;
36 	if (archive->FindString("_label", &string) == B_OK)
37 		SetText(string);
38 }
39 
40 
41 BStringItem::~BStringItem()
42 {
43 	free(fText);
44 }
45 
46 
47 BArchivable*
48 BStringItem::Instantiate(BMessage* archive)
49 {
50 	if (validate_instantiation(archive, "BStringItem"))
51 		return new BStringItem(archive);
52 
53 	return NULL;
54 }
55 
56 
57 status_t
58 BStringItem::Archive(BMessage *archive, bool deep) const
59 {
60 	status_t status = BListItem::Archive(archive);
61 
62 	if (status == B_OK && fText != NULL)
63 		status = archive->AddString("_label", fText);
64 
65 	return status;
66 }
67 
68 
69 void
70 BStringItem::DrawItem(BView *owner, BRect frame, bool complete)
71 {
72 	if (fText == NULL)
73 		return;
74 
75 	rgb_color highColor = owner->HighColor();
76 	rgb_color lowColor = owner->LowColor();
77 
78 	if (IsSelected() || complete) {
79 		if (IsSelected()) {
80 			owner->SetHighColor(tint_color(lowColor, B_DARKEN_2_TINT));
81 			owner->SetLowColor(owner->HighColor());
82 		} else
83 			owner->SetHighColor(lowColor);
84 
85 		owner->FillRect(frame);
86 	}
87 
88 	owner->MovePenTo(frame.left, frame.top + fBaselineOffset);
89 
90 	rgb_color black = {0, 0, 0, 255};
91 
92 	if (!IsEnabled())
93 		owner->SetHighColor(tint_color(black, B_LIGHTEN_2_TINT));
94 	else
95 		owner->SetHighColor(black);
96 
97 	owner->DrawString(fText);
98 
99 	owner->SetHighColor(highColor);
100 	owner->SetLowColor(lowColor);
101 }
102 
103 
104 void
105 BStringItem::SetText(const char *text)
106 {
107 	free(fText);
108 	fText = NULL;
109 
110 	if (text)
111 		fText = strdup(text);
112 }
113 
114 
115 const char *
116 BStringItem::Text() const
117 {
118 	return fText;
119 }
120 
121 
122 void
123 BStringItem::Update(BView *owner, const BFont *font)
124 {
125 	if (fText)
126 		SetWidth(font->StringWidth(fText));
127 
128 	font_height fheight;
129 	font->GetHeight(&fheight);
130 
131 	fBaselineOffset = 2 + ceilf(fheight.ascent + fheight.leading / 2);
132 
133 	SetHeight(ceilf(fheight.ascent) + ceilf(fheight.descent)
134 		+ ceilf(fheight.leading) + 4);
135 }
136 
137 
138 status_t
139 BStringItem::Perform(perform_code d, void *arg)
140 {
141 	return BListItem::Perform(d, arg);
142 }
143 
144 
145 float
146 BStringItem::BaselineOffset() const
147 {
148 	return fBaselineOffset;
149 }
150 
151 
152 void BStringItem::_ReservedStringItem1() {}
153 void BStringItem::_ReservedStringItem2() {}
154 
155 
156 BStringItem::BStringItem(const BStringItem &)
157 {
158 }
159 
160 
161 BStringItem	&
162 BStringItem::operator=(const BStringItem &)
163 {
164 	return *this;
165 }
166