xref: /haiku/src/kits/tracker/WidgetAttributeText.h (revision a906d0a031e721e2f2ec9d95274103e74a3a774f)
1 /*
2 Open Tracker License
3 
4 Terms and Conditions
5 
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
28 
29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
32 All rights reserved.
33 */
34 
35 #ifndef __TEXT_WIDGET_ATTRIBUTE__
36 #define __TEXT_WIDGET_ATTRIBUTE__
37 
38 #include <String.h>
39 
40 #include "TrackerSettings.h"
41 
42 namespace BPrivate {
43 
44 class Model;
45 class BPoseView;
46 class BColumn;
47 
48 // Tracker-only type for truncating the size string
49 // (Used in InfoWindow.cpp)
50 const uint32 kSizeType = 'kszt';
51 
52 class WidgetAttributeText {
53 	// each of subclasses knows how to retrieve a specific attribute
54 	// from a model that is passed in and knows how to display the
55 	// corresponding text, fitted into a specified width using a given
56 	// view
57 	// It is being asked for the string value by the TextWidget object
58 	public:
59 		WidgetAttributeText(const Model *, const BColumn *);
60 		virtual ~WidgetAttributeText();
61 
62 		virtual bool CheckAttributeChanged() = 0;
63 			// returns true if attribute value changed
64 
65 		bool CheckViewChanged(const BPoseView *);
66 			// returns true if fitted text changed, either because value
67 			// changed or because width/view changed
68 
69 		virtual bool CheckSettingsChanged();
70 			// override if the text rendering depends on a setting
71 
72 		const char *FittingText(const BPoseView *);
73 			// returns text, recalculating if not yet calculated
74 
75 		virtual int Compare(WidgetAttributeText &, BPoseView *view) = 0;
76 			// override to define a compare of two different attributes for
77 			// sorting
78 
79 		static WidgetAttributeText *NewWidgetText(const Model *, const BColumn *,
80 			const BPoseView *);
81 			// WidgetAttributeText factory
82 			// call this to make the right WidgetAttributeText type for a
83 			// given column
84 
85 		float Width(const BPoseView *);
86 			// respects the width of the corresponding column
87 		float CurrentWidth() const;
88 			// return the item width we got during our last fitting attempt
89 
90 		virtual void SetUpEditing(BTextView *);
91 			// set up the passed textView for the specifics of a given
92 			// attribute editing
93 		virtual bool CommitEditedText(BTextView *) = 0;
94 			// return true if attribute actually changed
95 
96 		virtual float PreferredWidth(const BPoseView *) const = 0;
97 
98 		static status_t AttrAsString(const Model *model, BString *result,
99 			const char *attrName, int32 attrType, float width,
100 			BView *view, int64 *value = 0);
101 
102 		Model *TargetModel() const;
103 
104 		bool IsEditable() const;
105 
106 		void SetDirty(bool);
107 
108 	protected:
109 		// generic fitting routines used by the different attributes
110 		static float TruncString(BString *result, const char *src,
111 			int32 length, const BPoseView *, float width,
112 			uint32 truncMode = B_TRUNCATE_MIDDLE);
113 
114 		static float TruncTime(BString *result, int64 src,
115 			const BPoseView *view, float width);
116 
117 		static float TruncFileSize(BString *result, int64 src,
118 			const BPoseView *view, float width);
119 
120 		virtual void FitValue(BString *result, const BPoseView *) = 0;
121 			// override FitValue to do a specific text fitting for a given
122 			// attribute
123 
124 		mutable Model *fModel;
125 		const BColumn *fColumn;
126 		float fOldWidth;			// ToDo: make these int32 only
127 		float fTruncatedWidth;
128 		bool fDirty;
129 			// if true, need to recalculate text next time we try to use it
130 	 	bool fValueIsDefined;
131 	 	BString fText;
132 			// holds the truncated text, fit to the parameters passed in
133 			// in the last FittingText call
134 };
135 
136 inline Model *
137 WidgetAttributeText::TargetModel() const
138 {
139 	return fModel;
140 }
141 
142 
143 class StringAttributeText : public WidgetAttributeText {
144 	public:
145 		StringAttributeText(const Model *, const BColumn *);
146 
147 		virtual const char *ValueAsText(const BPoseView *view);
148 			// returns the untrucated text that corresponds to the attribute
149 			// value
150 
151 		virtual bool CheckAttributeChanged();
152 
153 		virtual float PreferredWidth(const BPoseView *) const;
154 
155 		virtual bool CommitEditedText(BTextView *);
156 
157 	protected:
158 		virtual bool CommitEditedTextFlavor(BTextView *) { return false; }
159 
160 		virtual void FitValue(BString *result, const BPoseView *);
161 		virtual void ReadValue(BString *result) = 0;
162 
163 		virtual int Compare(WidgetAttributeText &, BPoseView *view);
164 
165 		BString fFullValueText;
166 		bool fValueDirty;
167 			// used for lazy read, managed by ReadValue
168 };
169 
170 
171 class ScalarAttributeText : public WidgetAttributeText {
172 	public:
173 		ScalarAttributeText(const Model *, const BColumn *);
174 		int64 Value();
175 		virtual bool CheckAttributeChanged();
176 
177 		virtual float PreferredWidth(const BPoseView *) const;
178 
179 		virtual bool CommitEditedText(BTextView *) { return false; }
180 			// return true if attribute actually changed
181 	protected:
182 		virtual int64 ReadValue() = 0;
183 		virtual int Compare(WidgetAttributeText &, BPoseView *view);
184 		int64 fValue;
185 		bool fValueDirty;
186 			// used for lazy read, managed by ReadValue
187 };
188 
189 
190 union GenericValueStruct {
191 	time_t time_tt;
192 	off_t off_tt;
193 
194 	bool boolt;
195 	int8 int8t;
196 	uint8 uint8t;
197 	int16 int16t;
198 	int16 uint16t;
199 	int32 int32t;
200 	int32 uint32t;
201 	int64 int64t;
202 	int64 uint64t;
203 
204 	float floatt;
205 	double doublet;
206 };
207 
208 
209 //! Used for displaying mime extra attributes. Supports different formats.
210 class GenericAttributeText : public StringAttributeText {
211 public:
212 								GenericAttributeText(const Model* model,
213 									const BColumn* column);
214 
215 	virtual	bool				CheckAttributeChanged();
216 	virtual	float				PreferredWidth(const BPoseView* view) const;
217 
218 	virtual	int					Compare(WidgetAttributeText& other,
219 									BPoseView* view);
220 
221 	virtual	void				SetUpEditing(BTextView* view);
222 	virtual	bool				CommitEditedText(BTextView* view);
223 
224 	virtual	const char*			ValueAsText(const BPoseView* view);
225 
226 protected:
227 	virtual	bool				CommitEditedTextFlavor(BTextView* view);
228 
229 	virtual	void				FitValue(BString* result,
230 									const BPoseView* view);
231 	virtual void				ReadValue(BString* result);
232 
233 protected:
234 	// TODO: split this up into a scalar flavor and string flavor
235 	// to save memory
236 			GenericValueStruct	fValue;
237 };
238 
239 
240 //! Used for the display-as type "duration"
241 class DurationAttributeText : public GenericAttributeText {
242 public:
243 								DurationAttributeText(const Model* model,
244 									const BColumn* column);
245 
246 private:
247 	virtual	void				FitValue(BString* result,
248 									const BPoseView* view);
249 };
250 
251 
252 //! Used for the display-as type "checkbox"
253 class CheckboxAttributeText : public GenericAttributeText {
254 public:
255 								CheckboxAttributeText(const Model* model,
256 									const BColumn* column);
257 
258 	virtual	void				SetUpEditing(BTextView* view);
259 
260 private:
261 	virtual	void				FitValue(BString* result,
262 									const BPoseView* view);
263 
264 private:
265 			BString				fOnChar;
266 			BString				fOffChar;
267 };
268 
269 
270 //! Used for the display-as type "rating"
271 class RatingAttributeText : public GenericAttributeText {
272 public:
273 								RatingAttributeText(const Model* model,
274 									const BColumn* column);
275 
276 	virtual	void				SetUpEditing(BTextView* view);
277 
278 private:
279 	virtual	void				FitValue(BString* result,
280 									const BPoseView* view);
281 
282 private:
283 			int32				fCount;
284 			int32				fMax;
285 };
286 
287 
288 class TimeAttributeText : public ScalarAttributeText {
289 	public:
290 		TimeAttributeText(const Model *, const BColumn *);
291 	protected:
292 		virtual float PreferredWidth(const BPoseView *) const;
293 		virtual void FitValue(BString *result, const BPoseView *);
294 		virtual bool CheckSettingsChanged();
295 
296 		TrackerSettings fSettings;
297 		bool fLastClockIs24;
298 		DateOrder fLastDateOrder;
299 		FormatSeparator fLastTimeFormatSeparator;
300 };
301 
302 
303 class PathAttributeText : public StringAttributeText {
304 	public:
305 		PathAttributeText(const Model *, const BColumn *);
306 	protected:
307 		virtual void ReadValue(BString *result);
308 };
309 
310 
311 class OriginalPathAttributeText : public StringAttributeText {
312 	public:
313 		OriginalPathAttributeText(const Model *, const BColumn *);
314 	protected:
315 		virtual void ReadValue(BString *result);
316 };
317 
318 
319 class KindAttributeText : public StringAttributeText {
320 	public:
321 		KindAttributeText(const Model *, const BColumn *);
322 	protected:
323 		virtual void ReadValue(BString *result);
324 };
325 
326 
327 class NameAttributeText : public StringAttributeText {
328 	public:
329 		NameAttributeText(const Model *, const BColumn *);
330 		virtual void SetUpEditing(BTextView *);
331 		virtual void FitValue(BString *result, const BPoseView *);
332 
333 		static void SetSortFolderNamesFirst(bool);
334 	protected:
335 		virtual bool CommitEditedTextFlavor(BTextView *);
336 		virtual int Compare(WidgetAttributeText &, BPoseView *view);
337 		virtual void ReadValue(BString *result);
338 
339 		static bool sSortFolderNamesFirst;
340 };
341 
342 
343 #ifdef OWNER_GROUP_ATTRIBUTES
344 
345 class OwnerAttributeText : public StringAttributeText {
346 	public:
347 		OwnerAttributeText(const Model *, const BColumn *);
348 
349 	protected:
350 		virtual void ReadValue(BString *result);
351 };
352 
353 
354 class GroupAttributeText : public StringAttributeText {
355 	public:
356 		GroupAttributeText(const Model *, const BColumn *);
357 
358 	protected:
359 		virtual void ReadValue(BString *result);
360 };
361 
362 #endif  /* OWNER_GROUP_ATTRIBUTES */
363 
364 class ModeAttributeText : public StringAttributeText {
365 	public:
366 		ModeAttributeText(const Model *, const BColumn *);
367 
368 	protected:
369 		virtual void ReadValue(BString *result);
370 };
371 
372 const int64 kUnknownSize = -1;
373 
374 class SizeAttributeText : public ScalarAttributeText {
375 	public:
376 		SizeAttributeText(const Model *, const BColumn *);
377 
378 	protected:
379 		virtual void FitValue(BString *result, const BPoseView *);
380 		virtual int64 ReadValue();
381 		virtual float PreferredWidth(const BPoseView *) const;
382 };
383 
384 
385 class CreationTimeAttributeText : public TimeAttributeText {
386 	public:
387 		CreationTimeAttributeText(const Model *, const BColumn *);
388 	protected:
389 		virtual int64 ReadValue();
390 };
391 
392 
393 class ModificationTimeAttributeText : public TimeAttributeText {
394 	public:
395 		ModificationTimeAttributeText(const Model *, const BColumn *);
396 
397 	protected:
398 		virtual int64 ReadValue();
399 };
400 
401 
402 class OpenWithRelationAttributeText : public ScalarAttributeText {
403 	public:
404 		OpenWithRelationAttributeText(const Model *, const BColumn *,
405 			const BPoseView *);
406 
407 	protected:
408 		virtual void FitValue(BString *result, const BPoseView *);
409 		virtual int64 ReadValue();
410 		virtual float PreferredWidth(const BPoseView *) const;
411 
412 		const BPoseView *fPoseView;
413 		BString fRelationText;
414 };
415 
416 
417 class VersionAttributeText : public StringAttributeText {
418 	public:
419 		VersionAttributeText(const Model *, const BColumn *, bool appVersion);
420 
421 	protected:
422 		virtual void ReadValue(BString *result);
423 	private:
424 		bool fAppVersion;
425 };
426 
427 
428 class AppShortVersionAttributeText : public VersionAttributeText {
429 	public:
430 		AppShortVersionAttributeText(const Model *model, const BColumn *column)
431 			:	VersionAttributeText(model, column, true)
432 		{
433 		}
434 };
435 
436 
437 class SystemShortVersionAttributeText : public VersionAttributeText {
438 	public:
439 		SystemShortVersionAttributeText(const Model *model, const BColumn *column)
440 			:	VersionAttributeText(model, column, false)
441 		{
442 		}
443 };
444 
445 } // namespace BPrivate
446 
447 
448 extern status_t TimeFormat(BString &string, int32 index, FormatSeparator format,
449 	DateOrder order, bool clockIs24Hour);
450 
451 using namespace BPrivate;
452 
453 #endif	/* __TEXT_WIDGET_ATTRIBUTE__ */
454