xref: /haiku/src/kits/interface/StringItem.cpp (revision 7f99a36bc71a8ea1d7a8c242ba9339e50530c9a8)
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(ui_color(B_MENU_SELECTED_BACKGROUND_COLOR));
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 	if (!IsEnabled()) {
91 		rgb_color textColor = ui_color(B_MENU_ITEM_TEXT_COLOR);
92 		if (textColor.red + textColor.green + textColor.blue > 128 * 3)
93 			owner->SetHighColor(tint_color(textColor, B_DARKEN_2_TINT));
94 		else
95 			owner->SetHighColor(tint_color(textColor, B_LIGHTEN_2_TINT));
96 	} else {
97 		if (IsSelected())
98 			owner->SetHighColor(ui_color(B_MENU_SELECTED_ITEM_TEXT_COLOR));
99 		else
100 			owner->SetHighColor(ui_color(B_MENU_ITEM_TEXT_COLOR));
101 	}
102 
103 	owner->DrawString(fText);
104 
105 	owner->SetHighColor(highColor);
106 	owner->SetLowColor(lowColor);
107 }
108 
109 
110 void
111 BStringItem::SetText(const char *text)
112 {
113 	free(fText);
114 	fText = NULL;
115 
116 	if (text)
117 		fText = strdup(text);
118 }
119 
120 
121 const char *
122 BStringItem::Text() const
123 {
124 	return fText;
125 }
126 
127 
128 void
129 BStringItem::Update(BView *owner, const BFont *font)
130 {
131 	if (fText)
132 		SetWidth(font->StringWidth(fText));
133 
134 	font_height fheight;
135 	font->GetHeight(&fheight);
136 
137 	fBaselineOffset = 2 + ceilf(fheight.ascent + fheight.leading / 2);
138 
139 	SetHeight(ceilf(fheight.ascent) + ceilf(fheight.descent)
140 		+ ceilf(fheight.leading) + 4);
141 }
142 
143 
144 status_t
145 BStringItem::Perform(perform_code d, void *arg)
146 {
147 	return BListItem::Perform(d, arg);
148 }
149 
150 
151 float
152 BStringItem::BaselineOffset() const
153 {
154 	return fBaselineOffset;
155 }
156 
157 
158 void BStringItem::_ReservedStringItem1() {}
159 void BStringItem::_ReservedStringItem2() {}
160 
161 
162 BStringItem::BStringItem(const BStringItem &)
163 {
164 }
165 
166 
167 BStringItem	&
168 BStringItem::operator=(const BStringItem &)
169 {
170 	return *this;
171 }
172