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 <String.h> 39 40 #include "TrackerSettings.h" 41 42 43 namespace BPrivate { 44 45 class Model; 46 class BPoseView; 47 class BColumn; 48 49 // Tracker-only type for truncating the size string 50 // (Used in InfoWindow.cpp) 51 const uint32 kSizeType = 'kszt'; 52 53 class 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 138 inline Model* 139 WidgetAttributeText::TargetModel() const 140 { 141 return fModel; 142 } 143 144 145 class 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 173 class ScalarAttributeText : public WidgetAttributeText { 174 public: 175 ScalarAttributeText(const Model*, const BColumn*); 176 177 int64 Value(); 178 virtual bool CheckAttributeChanged(); 179 180 virtual float PreferredWidth(const BPoseView*) const; 181 182 virtual bool CommitEditedText(BTextView*) { return false; } 183 // return true if attribute actually changed 184 protected: 185 virtual int64 ReadValue() = 0; 186 virtual int Compare(WidgetAttributeText&, BPoseView* view); 187 int64 fValue; 188 bool fValueDirty; 189 // used for lazy read, managed by ReadValue 190 }; 191 192 193 union GenericValueStruct { 194 time_t time_tt; 195 off_t off_tt; 196 197 bool boolt; 198 int8 int8t; 199 uint8 uint8t; 200 int16 int16t; 201 int16 uint16t; 202 int32 int32t; 203 int32 uint32t; 204 int64 int64t; 205 int64 uint64t; 206 207 float floatt; 208 double doublet; 209 }; 210 211 212 //! Used for displaying mime extra attributes. Supports different formats. 213 class GenericAttributeText : public StringAttributeText { 214 public: 215 GenericAttributeText(const Model* model, const BColumn* column); 216 217 virtual bool CheckAttributeChanged(); 218 virtual float PreferredWidth(const BPoseView* view) const; 219 220 virtual int Compare(WidgetAttributeText& other, BPoseView* view); 221 222 virtual void SetUpEditing(BTextView* view); 223 virtual bool CommitEditedText(BTextView* view); 224 225 virtual const char* ValueAsText(const BPoseView* view); 226 227 protected: 228 virtual bool CommitEditedTextFlavor(BTextView* view); 229 230 virtual void FitValue(BString* result, 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, const BColumn* column); 244 245 private: 246 virtual void FitValue(BString* result, const BPoseView* view); 247 }; 248 249 250 //! Used for the display-as type "checkbox" 251 class CheckboxAttributeText : public GenericAttributeText { 252 public: 253 CheckboxAttributeText(const Model* model, const BColumn* column); 254 255 virtual void SetUpEditing(BTextView* view); 256 257 private: 258 virtual void FitValue(BString* result, const BPoseView* view); 259 260 private: 261 BString fOnChar; 262 BString fOffChar; 263 }; 264 265 266 //! Used for the display-as type "rating" 267 class RatingAttributeText : public GenericAttributeText { 268 public: 269 RatingAttributeText(const Model* model, const BColumn* column); 270 271 virtual void SetUpEditing(BTextView* view); 272 273 private: 274 virtual void FitValue(BString* result, const BPoseView* view); 275 276 private: 277 int32 fCount; 278 int32 fMax; 279 }; 280 281 282 class TimeAttributeText : public ScalarAttributeText { 283 public: 284 TimeAttributeText(const Model*, const BColumn*); 285 286 protected: 287 virtual float PreferredWidth(const BPoseView*) const; 288 virtual void FitValue(BString* result, const BPoseView*); 289 virtual bool CheckSettingsChanged(); 290 291 TrackerSettings fSettings; 292 bool fLastClockIs24; 293 DateOrder fLastDateOrder; 294 FormatSeparator fLastTimeFormatSeparator; 295 }; 296 297 298 class PathAttributeText : public StringAttributeText { 299 public: 300 PathAttributeText(const Model*, const BColumn*); 301 302 protected: 303 virtual void ReadValue(BString* result); 304 }; 305 306 307 class OriginalPathAttributeText : public StringAttributeText { 308 public: 309 OriginalPathAttributeText(const Model*, const BColumn*); 310 311 protected: 312 virtual void ReadValue(BString* result); 313 }; 314 315 316 class KindAttributeText : public StringAttributeText { 317 public: 318 KindAttributeText(const Model*, const BColumn*); 319 320 protected: 321 virtual void ReadValue(BString* result); 322 }; 323 324 325 class NameAttributeText : public StringAttributeText { 326 public: 327 NameAttributeText(const Model*, const BColumn*); 328 virtual void SetUpEditing(BTextView*); 329 virtual void FitValue(BString* result, const BPoseView*); 330 virtual bool IsEditable() const; 331 332 static void SetSortFolderNamesFirst(bool); 333 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 class RealNameAttributeText : public StringAttributeText { 344 public: 345 RealNameAttributeText(const Model*, const BColumn*); 346 347 virtual void SetUpEditing(BTextView*); 348 virtual void FitValue(BString* result, const BPoseView*); 349 350 static void SetSortFolderNamesFirst(bool); 351 352 protected: 353 virtual bool CommitEditedTextFlavor(BTextView*); 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