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_H 35#define _TEXT_WIDGET_ATTRIBUTE_H 36 37 38#include <DateFormat.h> 39#include <String.h> 40 41#include "TrackerSettings.h" 42 43 44namespace BPrivate { 45 46class Model; 47class BPoseView; 48class BColumn; 49 50// Tracker-only type for truncating the size string 51// (Used in InfoWindow.cpp) 52const uint32 kSizeType = 'kszt'; 53 54class 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 60public: 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 110protected: 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 139inline Model* 140WidgetAttributeText::TargetModel() const 141{ 142 return fModel; 143} 144 145 146class StringAttributeText : public WidgetAttributeText { 147public: 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 160protected: 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 174class ScalarAttributeText : public WidgetAttributeText { 175public: 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 185protected: 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 194union 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. 214class GenericAttributeText : public StringAttributeText { 215public: 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 228protected: 229 virtual bool CommitEditedTextFlavor(BTextView* view); 230 231 virtual void FitValue(BString* result, const BPoseView* view); 232 virtual void ReadValue(BString* result); 233 234protected: 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" 242class DurationAttributeText : public GenericAttributeText { 243public: 244 DurationAttributeText(const Model* model, const BColumn* column); 245 246private: 247 virtual void FitValue(BString* result, const BPoseView* view); 248}; 249 250 251//! Used for the display-as type "checkbox" 252class CheckboxAttributeText : public GenericAttributeText { 253public: 254 CheckboxAttributeText(const Model* model, const BColumn* column); 255 256 virtual void SetupEditing(BTextView* view); 257 258private: 259 virtual void FitValue(BString* result, const BPoseView* view); 260 261private: 262 BString fOnChar; 263 BString fOffChar; 264}; 265 266 267//! Used for the display-as type "rating" 268class RatingAttributeText : public GenericAttributeText { 269public: 270 RatingAttributeText(const Model* model, const BColumn* column); 271 272 virtual void SetupEditing(BTextView* view); 273 274private: 275 virtual void FitValue(BString* result, const BPoseView* view); 276 277private: 278 int32 fCount; 279 int32 fMax; 280}; 281 282 283class TimeAttributeText : public ScalarAttributeText { 284public: 285 TimeAttributeText(const Model*, const BColumn*); 286 287protected: 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 299class PathAttributeText : public StringAttributeText { 300public: 301 PathAttributeText(const Model*, const BColumn*); 302 303protected: 304 virtual void ReadValue(BString* result); 305}; 306 307 308class OriginalPathAttributeText : public StringAttributeText { 309public: 310 OriginalPathAttributeText(const Model*, const BColumn*); 311 312protected: 313 virtual void ReadValue(BString* result); 314}; 315 316 317class KindAttributeText : public StringAttributeText { 318public: 319 KindAttributeText(const Model*, const BColumn*); 320 321protected: 322 virtual void ReadValue(BString* result); 323}; 324 325 326class NameAttributeText : public StringAttributeText { 327public: 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 335protected: 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 344class RealNameAttributeText : public NameAttributeText { 345public: 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 353protected: 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 362class OwnerAttributeText : public StringAttributeText { 363public: 364 OwnerAttributeText(const Model*, const BColumn*); 365 366protected: 367 virtual void ReadValue(BString* result); 368}; 369 370 371class GroupAttributeText : public StringAttributeText { 372public: 373 GroupAttributeText(const Model*, const BColumn*); 374 375protected: 376 virtual void ReadValue(BString* result); 377}; 378#endif // OWNER_GROUP_ATTRIBUTES 379 380 381class ModeAttributeText : public StringAttributeText { 382public: 383 ModeAttributeText(const Model*, const BColumn*); 384 385protected: 386 virtual void ReadValue(BString* result); 387}; 388 389 390const int64 kUnknownSize = -1; 391 392 393class SizeAttributeText : public ScalarAttributeText { 394public: 395 SizeAttributeText(const Model*, const BColumn*); 396 397protected: 398 virtual void FitValue(BString* result, const BPoseView*); 399 virtual int64 ReadValue(); 400 virtual float PreferredWidth(const BPoseView*) const; 401}; 402 403 404class CreationTimeAttributeText : public TimeAttributeText { 405public: 406 CreationTimeAttributeText(const Model*, const BColumn*); 407 408protected: 409 virtual int64 ReadValue(); 410}; 411 412 413class ModificationTimeAttributeText : public TimeAttributeText { 414public: 415 ModificationTimeAttributeText(const Model*, const BColumn*); 416 417protected: 418 virtual int64 ReadValue(); 419}; 420 421 422class OpenWithRelationAttributeText : public ScalarAttributeText { 423public: 424 OpenWithRelationAttributeText(const Model*, const BColumn*, 425 const BPoseView*); 426 427protected: 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 437class VersionAttributeText : public StringAttributeText { 438public: 439 VersionAttributeText(const Model*, const BColumn*, bool appVersion); 440 441protected: 442 virtual void ReadValue(BString* result); 443 444private: 445 bool fAppVersion; 446}; 447 448 449class AppShortVersionAttributeText : public VersionAttributeText { 450public: 451 AppShortVersionAttributeText(const Model* model, 452 const BColumn* column) 453 : 454 VersionAttributeText(model, column, true) 455 { 456 } 457}; 458 459 460class SystemShortVersionAttributeText : public VersionAttributeText { 461public: 462 SystemShortVersionAttributeText(const Model* model, 463 const BColumn* column) 464 : 465 VersionAttributeText(model, column, false) 466 { 467 } 468}; 469 470} // namespace BPrivate 471 472 473extern status_t TimeFormat(BString &string, int32 index, 474 FormatSeparator format, DateOrder order, bool clockIs24Hour); 475 476using namespace BPrivate; 477 478 479#endif // _TEXT_WIDGET_ATTRIBUTE_H 480