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