xref: /haiku/src/kits/interface/StringView.cpp (revision d5cd5d63ff0ad395989db6cf4841a64d5b545d1d)
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:		StringView.cpp
23 //	Author:			Frans van Nispen (xlr8@tref.nl)
24 //	Description:	BStringView draw a non-editable text string
25 //------------------------------------------------------------------------------
26 
27 // Standard Includes -----------------------------------------------------------
28 #include <stdio.h>
29 #include <posix/string.h>
30 
31 // System Includes -------------------------------------------------------------
32 #include <Message.h>
33 #include <StringView.h>
34 #include <View.h>
35 #include <Window.h>
36 
37 // Project Includes ------------------------------------------------------------
38 
39 // Local Includes --------------------------------------------------------------
40 
41 // Local Defines ---------------------------------------------------------------
42 
43 // Globals ---------------------------------------------------------------------
44 
45 
46 //------------------------------------------------------------------------------
47 BStringView::BStringView(BRect frame, const char* name, const char* text,
48 						 uint32 resizeMask, uint32 flags)
49 	:	BView(frame, name, resizeMask, flags)
50 {
51 	fText = strdup(text);
52 	fAlign = B_ALIGN_LEFT;
53 }
54 //------------------------------------------------------------------------------
55 BStringView::BStringView(BMessage* data)
56 	:	BView(data)
57 {
58 	const char* text;
59 
60 	if (data->FindInt32("_aligne",(int32&)fAlign) != B_OK)
61 	{
62 		fAlign = B_ALIGN_LEFT;
63 	}
64 
65 	if (data->FindString("_text",&text) != B_OK)
66 	{
67 		text = NULL;
68 	}
69 
70 	SetText(text);
71 }
72 //------------------------------------------------------------------------------
73 BArchivable* BStringView::Instantiate(BMessage* data)
74 {
75 	if (!validate_instantiation(data,"BStringView"))
76 	{
77 		return NULL;
78 	}
79 
80 	return new BStringView(data);
81 }
82 //------------------------------------------------------------------------------
83 status_t BStringView::Archive(BMessage* data, bool deep) const
84 {
85 	BView::Archive(data, deep);
86 
87 	if (fText)
88 	{
89 		data->AddString("_text",fText);
90 	}
91 
92 	data->AddInt32("_align", fAlign);
93 
94 	return B_OK;
95 }
96 //------------------------------------------------------------------------------
97 BStringView::~BStringView()
98 {
99 	if (fText)
100 	{
101 		delete[] fText;
102 	}
103 }
104 //------------------------------------------------------------------------------
105 void BStringView::SetText(const char* text)
106 {
107 	if (fText)
108 	{
109 		delete[] fText;
110 	}
111 	fText = strdup(text);
112 	Invalidate();
113 }
114 //------------------------------------------------------------------------------
115 const char* BStringView::Text() const
116 {
117 	return fText;
118 }
119 //------------------------------------------------------------------------------
120 void BStringView::SetAlignment(alignment flag)
121 {
122 	fAlign = flag;
123 	Invalidate();
124 }
125 //------------------------------------------------------------------------------
126 alignment BStringView::Alignment() const
127 {
128 	return fAlign;
129 }
130 //------------------------------------------------------------------------------
131 void BStringView::AttachedToWindow()
132 {
133 	if (Parent())
134 	{
135 		SetViewColor(Parent()->ViewColor());
136 	}
137 }
138 //------------------------------------------------------------------------------
139 void BStringView::Draw(BRect bounds)
140 {
141 	SetLowColor(ViewColor());
142 	BFont font;
143 	GetFont(&font);
144 	font_height fh;
145 	font.GetHeight(&fh);
146 
147 	float y = Bounds().bottom - ceil(fh.descent);
148 	float x;
149 	switch (fAlign)
150 	{
151 		case B_ALIGN_RIGHT:
152 			x = Bounds().Width() - font.StringWidth(fText) - 2.0f;
153 			break;
154 
155 		case B_ALIGN_CENTER:
156 			x = (Bounds().Width() - font.StringWidth(fText))/2.0f;
157 			break;
158 
159 		default:
160 			x = 2.0f;
161 			break;
162 	}
163 
164 	DrawString( fText, BPoint(x,y) );
165 }
166 //------------------------------------------------------------------------------
167 void BStringView::ResizeToPreferred()
168 {
169 	float w, h;
170 	GetPreferredSize(&w, &h);
171 	BView::ResizeTo(w, h);
172 }
173 //------------------------------------------------------------------------------
174 void BStringView::GetPreferredSize(float* width, float* height)
175 {
176 	BFont font;
177 	GetFont(&font);
178 	font_height fh;
179 	font.GetHeight(&fh);
180 
181 	*height = ceil(fh.ascent + fh.descent + fh.leading) + 2.0f;
182 	*width = 4.0f + ceil(font.StringWidth(fText));
183 }
184 //------------------------------------------------------------------------------
185 void BStringView::MessageReceived(BMessage* msg)
186 {
187 	BView::MessageReceived(msg);
188 }
189 //------------------------------------------------------------------------------
190 void BStringView::MouseDown(BPoint pt)
191 {
192 	BView::MouseDown(pt);
193 }
194 //------------------------------------------------------------------------------
195 void BStringView::MouseUp(BPoint pt)
196 {
197 	BView::MouseUp(pt);
198 }
199 //------------------------------------------------------------------------------
200 void BStringView::MouseMoved(BPoint pt, uint32 code, const BMessage* msg)
201 {
202 	BView::MouseMoved(pt, code, msg);
203 }
204 //------------------------------------------------------------------------------
205 void BStringView::DetachedFromWindow()
206 {
207 	BView::DetachedFromWindow();
208 }
209 //------------------------------------------------------------------------------
210 void BStringView::FrameMoved(BPoint newPosition)
211 {
212 	BView::FrameMoved(newPosition);
213 }
214 //------------------------------------------------------------------------------
215 void BStringView::FrameResized(float newWidth, float newHeight)
216 {
217 	BView::FrameResized(newWidth, newHeight);
218 }
219 //------------------------------------------------------------------------------
220 BHandler* BStringView::ResolveSpecifier(BMessage* msg, int32 index,
221 										BMessage* specifier, int32 form,
222 										const char* property)
223 {
224 	return NULL;
225 }
226 //------------------------------------------------------------------------------
227 void BStringView::MakeFocus(bool state)
228 {
229 	BView::MakeFocus(state);
230 }
231 //------------------------------------------------------------------------------
232 void BStringView::AllAttached()
233 {
234 	BView::AllAttached();
235 }
236 //------------------------------------------------------------------------------
237 void BStringView::AllDetached()
238 {
239 	BView::AllDetached();
240 }
241 //------------------------------------------------------------------------------
242 status_t BStringView::GetSupportedSuites(BMessage* data)
243 {
244 	return B_OK;
245 }
246 //------------------------------------------------------------------------------
247 status_t BStringView::Perform(perform_code d, void* arg)
248 {
249 	return B_ERROR;
250 }
251 //------------------------------------------------------------------------------
252 void BStringView::_ReservedStringView1()
253 {
254 }
255 //------------------------------------------------------------------------------
256 void BStringView::_ReservedStringView2()
257 {
258 }
259 //------------------------------------------------------------------------------
260 void BStringView::_ReservedStringView3()
261 {
262 }
263 //------------------------------------------------------------------------------
264 BStringView& BStringView::operator=(const BStringView&)
265 {
266 	// Assignment not allowed
267 	return *this;
268 }
269 //------------------------------------------------------------------------------
270 
271 /*
272  * $Log $
273  *
274  * $Id  $
275  *
276  */
277 
278