xref: /haiku/src/kits/tracker/WidgetAttributeText.h (revision dd2a1e350b303b855a50fd64e6cb55618be1ae6a)
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 #ifndef _TEXT_WIDGET_ATTRIBUTE_H
35 #define _TEXT_WIDGET_ATTRIBUTE_H
36 
37 
38 #include <DateFormat.h>
39 #include <String.h>
40 
41 #include "TrackerSettings.h"
42 
43 
44 namespace BPrivate {
45 
46 class Model;
47 class BPoseView;
48 class BColumn;
49 
50 // Tracker-only type for truncating the size string
51 // (Used in InfoWindow.cpp)
52 const uint32 kSizeType = 'kszt';
53 
54 class WidgetAttributeText {
55 	// each of subclasses knows how to retrieve a specific attribute
56 	// from a model that is passed in and knows how to display the
57 	// corresponding text, fitted into a specified width using a given
58 	// view
59 	// It is being asked for the string value by the TextWidget object
60 public:
61 	WidgetAttributeText(const Model*, const BColumn*);
62 	virtual ~WidgetAttributeText();
63 
64 	virtual bool CheckAttributeChanged() = 0;
65 		// returns true if attribute value changed
66 
67 	bool CheckViewChanged(const BPoseView*);
68 		// returns true if fitted text changed, either because value
69 		// changed or because width/view changed
70 
71 	virtual bool CheckSettingsChanged();
72 		// override if the text rendering depends on a setting
73 
74 	const char* FittingText(const BPoseView*);
75 		// returns text, recalculating if not yet calculated
76 
77 	virtual int Compare(WidgetAttributeText&, BPoseView* view) = 0;
78 		// override to define a compare of two different attributes for
79 		// sorting
80 
81 	static WidgetAttributeText* NewWidgetText(const Model*,
82 		const BColumn*, const BPoseView*);
83 		// WidgetAttributeText factory
84 		// call this to make the right WidgetAttributeText type for a
85 		// given column
86 
87 	float Width(const BPoseView*);
88 		// respects the width of the corresponding column
89 	float CurrentWidth() const;
90 		// return the item width we got during our last fitting attempt
91 
92 	virtual void SetupEditing(BTextView*);
93 		// set up the passed textView for the specifics of a given
94 		// attribute editing
95 	virtual bool CommitEditedText(BTextView*) = 0;
96 		// return true if attribute actually changed
97 
98 	virtual float PreferredWidth(const BPoseView*) const = 0;
99 
100 	static status_t AttrAsString(const Model* model, BString* result,
101 		const char* attrName, int32 attrType, float width,
102 		BView* view, int64* value = 0);
103 
104 	Model* TargetModel() const;
105 
106 	virtual bool IsEditable() const;
107 
108 	void SetDirty(bool);
109 
110 protected:
111 	// generic fitting routines used by the different attributes
112 	static float TruncString(BString* result, const char* src,
113 		int32 length, const BPoseView*, float width,
114 		uint32 truncMode = B_TRUNCATE_MIDDLE);
115 
116 	static float TruncTime(BString* result, int64 src,
117 		const BPoseView* view, float width);
118 
119 	static float TruncFileSize(BString* result, int64 src,
120 		const BPoseView* view, float width);
121 
122 	virtual void FitValue(BString* result, const BPoseView*) = 0;
123 		// override FitValue to do a specific text fitting for a given
124 		// attribute
125 
126 	mutable Model* fModel;
127 	const BColumn* fColumn;
128 	// TODO: make these int32 only
129 	float fOldWidth;
130 	float fTruncatedWidth;
131 	bool fDirty;
132 		// if true, need to recalculate text next time we try to use it
133 	bool fValueIsDefined;
134 	BString fText;
135 		// holds the truncated text, fit to the parameters passed in
136 		// in the last FittingText call
137 };
138 
139 inline Model*
140 WidgetAttributeText::TargetModel() const
141 {
142 	return fModel;
143 }
144 
145 
146 class StringAttributeText : public WidgetAttributeText {
147 public:
148 	StringAttributeText(const Model*, const BColumn*);
149 
150 	virtual const char* ValueAsText(const BPoseView* view);
151 		// returns the untrucated text that corresponds to
152 		// the attribute value
153 
154 	virtual bool CheckAttributeChanged();
155 
156 	virtual float PreferredWidth(const BPoseView*) const;
157 
158 	virtual bool CommitEditedText(BTextView*);
159 
160 protected:
161 	virtual bool CommitEditedTextFlavor(BTextView*) { return false; }
162 
163 	virtual void FitValue(BString* result, const BPoseView*);
164 	virtual void ReadValue(BString* result) = 0;
165 
166 	virtual int Compare(WidgetAttributeText &, BPoseView* view);
167 
168 	BString fFullValueText;
169 	bool fValueDirty;
170 		// used for lazy read, managed by ReadValue
171 };
172 
173 
174 class ScalarAttributeText : public WidgetAttributeText {
175 public:
176 	ScalarAttributeText(const Model*, const BColumn*);
177 
178 	int64 Value();
179 	virtual bool CheckAttributeChanged();
180 
181 	virtual float PreferredWidth(const BPoseView*) const;
182 
183 	virtual bool CommitEditedText(BTextView*) { return false; }
184 		// return true if attribute actually changed
185 protected:
186 	virtual int64 ReadValue() = 0;
187 	virtual int Compare(WidgetAttributeText&, BPoseView* view);
188 	int64 fValue;
189 	bool fValueDirty;
190 		// used for lazy read, managed by ReadValue
191 };
192 
193 
194 union GenericValueStruct {
195 	time_t	time_tt;
196 	off_t	off_tt;
197 
198 	bool	boolt;
199 	int8	int8t;
200 	uint8	uint8t;
201 	int16	int16t;
202 	int16	uint16t;
203 	int32	int32t;
204 	int32	uint32t;
205 	int64	int64t;
206 	int64	uint64t;
207 
208 	float	floatt;
209 	double	doublet;
210 };
211 
212 
213 //! Used for displaying mime extra attributes. Supports different formats.
214 class GenericAttributeText : public StringAttributeText {
215 public:
216 	GenericAttributeText(const Model* model, const BColumn* column);
217 
218 	virtual bool CheckAttributeChanged();
219 	virtual float PreferredWidth(const BPoseView* view) const;
220 
221 	virtual int Compare(WidgetAttributeText& other, 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 
228 protected:
229 	virtual bool CommitEditedTextFlavor(BTextView* view);
230 
231 	virtual void FitValue(BString* result, const BPoseView* view);
232 	virtual void ReadValue(BString* result);
233 
234 protected:
235 	// TODO: split this up into a scalar flavor and string flavor
236 	// to save memory
237 	GenericValueStruct fValue;
238 };
239 
240 
241 //! Used for the display-as type "duration"
242 class DurationAttributeText : public GenericAttributeText {
243 public:
244 	DurationAttributeText(const Model* model, const BColumn* column);
245 
246 private:
247 	virtual void FitValue(BString* result, const BPoseView* view);
248 };
249 
250 
251 //! Used for the display-as type "checkbox"
252 class CheckboxAttributeText : public GenericAttributeText {
253 public:
254 	CheckboxAttributeText(const Model* model, const BColumn* column);
255 
256 	virtual void SetupEditing(BTextView* view);
257 
258 private:
259 	virtual void FitValue(BString* result, const BPoseView* view);
260 
261 private:
262 	BString fOnChar;
263 	BString fOffChar;
264 };
265 
266 
267 //! Used for the display-as type "rating"
268 class RatingAttributeText : public GenericAttributeText {
269 public:
270 	RatingAttributeText(const Model* model, const BColumn* column);
271 
272 	virtual void SetupEditing(BTextView* view);
273 
274 private:
275 	virtual void FitValue(BString* result, const BPoseView* view);
276 
277 private:
278 	int32 fCount;
279 	int32 fMax;
280 };
281 
282 
283 class TimeAttributeText : public ScalarAttributeText {
284 public:
285 	TimeAttributeText(const Model*, const BColumn*);
286 
287 protected:
288 	virtual float PreferredWidth(const BPoseView*) const;
289 	virtual void FitValue(BString* ratingString, const BPoseView* view);
290 	virtual bool CheckSettingsChanged();
291 
292 	TrackerSettings fSettings;
293 	bool fLastClockIs24;
294 	DateOrder fLastDateOrder;
295 	FormatSeparator fLastTimeFormatSeparator;
296 };
297 
298 
299 class PathAttributeText : public StringAttributeText {
300 public:
301 	PathAttributeText(const Model*, const BColumn*);
302 
303 protected:
304 	virtual void ReadValue(BString* result);
305 };
306 
307 
308 class OriginalPathAttributeText : public StringAttributeText {
309 public:
310 	OriginalPathAttributeText(const Model*, const BColumn*);
311 
312 protected:
313 	virtual void ReadValue(BString* result);
314 };
315 
316 
317 class KindAttributeText : public StringAttributeText {
318 public:
319 	KindAttributeText(const Model*, const BColumn*);
320 
321 protected:
322 	virtual void ReadValue(BString* result);
323 };
324 
325 
326 class NameAttributeText : public StringAttributeText {
327 public:
328 	NameAttributeText(const Model*, const BColumn*);
329 	virtual void SetupEditing(BTextView*);
330 	virtual void FitValue(BString* result, const BPoseView*);
331 	virtual bool IsEditable() const;
332 
333 	static void SetSortFolderNamesFirst(bool);
334 
335 protected:
336 	virtual bool CommitEditedTextFlavor(BTextView*);
337 	virtual int Compare(WidgetAttributeText&, BPoseView* view);
338 	virtual void ReadValue(BString* result);
339 
340 	static bool sSortFolderNamesFirst;
341 };
342 
343 
344 class RealNameAttributeText : public NameAttributeText {
345 public:
346 	RealNameAttributeText(const Model*, const BColumn*);
347 
348 	virtual void SetupEditing(BTextView*);
349 	virtual void FitValue(BString* result, const BPoseView*);
350 
351 	static void SetSortFolderNamesFirst(bool);
352 
353 protected:
354 	virtual int Compare(WidgetAttributeText&, BPoseView* view);
355 	virtual void ReadValue(BString* result);
356 
357 	static bool sSortFolderNamesFirst;
358 };
359 
360 
361 #ifdef OWNER_GROUP_ATTRIBUTES
362 class OwnerAttributeText : public StringAttributeText {
363 public:
364 	OwnerAttributeText(const Model*, const BColumn*);
365 
366 protected:
367 	virtual void ReadValue(BString* result);
368 };
369 
370 
371 class GroupAttributeText : public StringAttributeText {
372 public:
373 	GroupAttributeText(const Model*, const BColumn*);
374 
375 protected:
376 	virtual void ReadValue(BString* result);
377 };
378 #endif	// OWNER_GROUP_ATTRIBUTES
379 
380 
381 class ModeAttributeText : public StringAttributeText {
382 public:
383 	ModeAttributeText(const Model*, const BColumn*);
384 
385 protected:
386 	virtual void ReadValue(BString* result);
387 };
388 
389 
390 const int64 kUnknownSize = -1;
391 
392 
393 class SizeAttributeText : public ScalarAttributeText {
394 public:
395 	SizeAttributeText(const Model*, const BColumn*);
396 
397 protected:
398 	virtual void FitValue(BString* result, const BPoseView*);
399 	virtual int64 ReadValue();
400 	virtual float PreferredWidth(const BPoseView*) const;
401 };
402 
403 
404 class CreationTimeAttributeText : public TimeAttributeText {
405 public:
406 	CreationTimeAttributeText(const Model*, const BColumn*);
407 
408 protected:
409 	virtual int64 ReadValue();
410 };
411 
412 
413 class ModificationTimeAttributeText : public TimeAttributeText {
414 public:
415 	ModificationTimeAttributeText(const Model*, const BColumn*);
416 
417 protected:
418 	virtual int64 ReadValue();
419 };
420 
421 
422 class OpenWithRelationAttributeText : public ScalarAttributeText {
423 public:
424 	OpenWithRelationAttributeText(const Model*, const BColumn*,
425 		const BPoseView*);
426 
427 protected:
428 	virtual void FitValue(BString* result, const BPoseView*);
429 	virtual int64 ReadValue();
430 	virtual float PreferredWidth(const BPoseView*) const;
431 
432 	const BPoseView* fPoseView;
433 	BString fRelationText;
434 };
435 
436 
437 class VersionAttributeText : public StringAttributeText {
438 public:
439 	VersionAttributeText(const Model*, const BColumn*, bool appVersion);
440 
441 protected:
442 	virtual void ReadValue(BString* result);
443 
444 private:
445 	bool fAppVersion;
446 };
447 
448 
449 class AppShortVersionAttributeText : public VersionAttributeText {
450 public:
451 	AppShortVersionAttributeText(const Model* model,
452 		const BColumn* column)
453 		:
454 		VersionAttributeText(model, column, true)
455 	{
456 	}
457 };
458 
459 
460 class SystemShortVersionAttributeText : public VersionAttributeText {
461 public:
462 	SystemShortVersionAttributeText(const Model* model,
463 		const BColumn* column)
464 		:
465 		VersionAttributeText(model, column, false)
466 	{
467 	}
468 };
469 
470 } // namespace BPrivate
471 
472 
473 extern status_t TimeFormat(BString &string, int32 index,
474 	FormatSeparator format, DateOrder order, bool clockIs24Hour);
475 
476 using namespace BPrivate;
477 
478 
479 #endif	// _TEXT_WIDGET_ATTRIBUTE_H
480