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 virtual 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 virtual bool IsEditable() const; 333 334 static void SetSortFolderNamesFirst(bool); 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 StringAttributeText { 345 public: 346 RealNameAttributeText(const Model *, 347 const BColumn *); 348 virtual void SetUpEditing(BTextView *); 349 virtual void FitValue(BString *result, const BPoseView *); 350 351 static void SetSortFolderNamesFirst(bool); 352 353 protected: 354 virtual bool CommitEditedTextFlavor(BTextView *); 355 virtual int Compare(WidgetAttributeText &, BPoseView *view); 356 virtual void ReadValue(BString *result); 357 358 static bool sSortFolderNamesFirst; 359 }; 360 361 362 #ifdef OWNER_GROUP_ATTRIBUTES 363 364 class OwnerAttributeText : public StringAttributeText { 365 public: 366 OwnerAttributeText(const Model *, const BColumn *); 367 368 protected: 369 virtual void ReadValue(BString *result); 370 }; 371 372 373 class GroupAttributeText : public StringAttributeText { 374 public: 375 GroupAttributeText(const Model *, const BColumn *); 376 377 protected: 378 virtual void ReadValue(BString *result); 379 }; 380 381 #endif /* OWNER_GROUP_ATTRIBUTES */ 382 383 class ModeAttributeText : public StringAttributeText { 384 public: 385 ModeAttributeText(const Model *, const BColumn *); 386 387 protected: 388 virtual void ReadValue(BString *result); 389 }; 390 391 const int64 kUnknownSize = -1; 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 protected: 408 virtual int64 ReadValue(); 409 }; 410 411 412 class ModificationTimeAttributeText : public TimeAttributeText { 413 public: 414 ModificationTimeAttributeText(const Model *, const BColumn *); 415 416 protected: 417 virtual int64 ReadValue(); 418 }; 419 420 421 class OpenWithRelationAttributeText : public ScalarAttributeText { 422 public: 423 OpenWithRelationAttributeText(const Model *, const BColumn *, 424 const BPoseView *); 425 426 protected: 427 virtual void FitValue(BString *result, const BPoseView *); 428 virtual int64 ReadValue(); 429 virtual float PreferredWidth(const BPoseView *) const; 430 431 const BPoseView *fPoseView; 432 BString fRelationText; 433 }; 434 435 436 class VersionAttributeText : public StringAttributeText { 437 public: 438 VersionAttributeText(const Model *, const BColumn *, bool appVersion); 439 440 protected: 441 virtual void ReadValue(BString *result); 442 private: 443 bool fAppVersion; 444 }; 445 446 447 class AppShortVersionAttributeText : public VersionAttributeText { 448 public: 449 AppShortVersionAttributeText(const Model *model, const BColumn *column) 450 : VersionAttributeText(model, column, true) 451 { 452 } 453 }; 454 455 456 class SystemShortVersionAttributeText : public VersionAttributeText { 457 public: 458 SystemShortVersionAttributeText(const Model *model, const BColumn *column) 459 : VersionAttributeText(model, column, false) 460 { 461 } 462 }; 463 464 } // namespace BPrivate 465 466 467 extern status_t TimeFormat(BString &string, int32 index, FormatSeparator format, 468 DateOrder order, bool clockIs24Hour); 469 470 using namespace BPrivate; 471 472 #endif /* __TEXT_WIDGET_ATTRIBUTE__ */ 473