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 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 class GenericAttributeText : public StringAttributeText { 210 // used for displaying mime extra attributes 211 // supports different formats 212 public: 213 GenericAttributeText(const Model *model, const BColumn *column); 214 virtual bool CheckAttributeChanged(); 215 216 virtual float PreferredWidth(const BPoseView *view) const; 217 218 virtual int Compare(WidgetAttributeText &, BPoseView *view); 219 220 virtual void SetUpEditing(BTextView *); 221 virtual bool CommitEditedText(BTextView *); 222 223 virtual const char *ValueAsText(const BPoseView *view); 224 225 private: 226 virtual bool CommitEditedTextFlavor(BTextView *); 227 228 virtual void FitValue(BString *result, const BPoseView *); 229 virtual void ReadValue(BString *result); 230 231 // TODO: 232 // split this up into a scalar flavor and string flavor 233 // to save memory 234 GenericValueStruct fValue; 235 }; 236 237 238 class TimeAttributeText : public ScalarAttributeText { 239 public: 240 TimeAttributeText(const Model *, const BColumn *); 241 protected: 242 virtual float PreferredWidth(const BPoseView *) const; 243 virtual void FitValue(BString *result, const BPoseView *); 244 virtual bool CheckSettingsChanged(); 245 246 TrackerSettings fSettings; 247 bool fLastClockIs24; 248 DateOrder fLastDateOrder; 249 FormatSeparator fLastTimeFormatSeparator; 250 }; 251 252 253 class PathAttributeText : public StringAttributeText { 254 public: 255 PathAttributeText(const Model *, const BColumn *); 256 protected: 257 virtual void ReadValue(BString *result); 258 }; 259 260 261 class OriginalPathAttributeText : public StringAttributeText { 262 public: 263 OriginalPathAttributeText(const Model *, const BColumn *); 264 protected: 265 virtual void ReadValue(BString *result); 266 }; 267 268 269 class KindAttributeText : public StringAttributeText { 270 public: 271 KindAttributeText(const Model *, const BColumn *); 272 protected: 273 virtual void ReadValue(BString *result); 274 }; 275 276 277 class NameAttributeText : public StringAttributeText { 278 public: 279 NameAttributeText(const Model *, const BColumn *); 280 virtual void SetUpEditing(BTextView *); 281 virtual void FitValue(BString *result, const BPoseView *); 282 283 static void SetSortFolderNamesFirst(bool); 284 protected: 285 virtual bool CommitEditedTextFlavor(BTextView *); 286 virtual int Compare(WidgetAttributeText &, BPoseView *view); 287 virtual void ReadValue(BString *result); 288 289 static bool sSortFolderNamesFirst; 290 }; 291 292 293 #ifdef OWNER_GROUP_ATTRIBUTES 294 295 class OwnerAttributeText : public StringAttributeText { 296 public: 297 OwnerAttributeText(const Model *, const BColumn *); 298 299 protected: 300 virtual void ReadValue(BString *result); 301 }; 302 303 304 class GroupAttributeText : public StringAttributeText { 305 public: 306 GroupAttributeText(const Model *, const BColumn *); 307 308 protected: 309 virtual void ReadValue(BString *result); 310 }; 311 312 #endif /* OWNER_GROUP_ATTRIBUTES */ 313 314 class ModeAttributeText : public StringAttributeText { 315 public: 316 ModeAttributeText(const Model *, const BColumn *); 317 318 protected: 319 virtual void ReadValue(BString *result); 320 }; 321 322 const int64 kUnknownSize = -1; 323 324 class SizeAttributeText : public ScalarAttributeText { 325 public: 326 SizeAttributeText(const Model *, const BColumn *); 327 328 protected: 329 virtual void FitValue(BString *result, const BPoseView *); 330 virtual int64 ReadValue(); 331 virtual float PreferredWidth(const BPoseView *) const; 332 }; 333 334 335 class CreationTimeAttributeText : public TimeAttributeText { 336 public: 337 CreationTimeAttributeText(const Model *, const BColumn *); 338 protected: 339 virtual int64 ReadValue(); 340 }; 341 342 343 class ModificationTimeAttributeText : public TimeAttributeText { 344 public: 345 ModificationTimeAttributeText(const Model *, const BColumn *); 346 347 protected: 348 virtual int64 ReadValue(); 349 }; 350 351 352 class OpenWithRelationAttributeText : public ScalarAttributeText { 353 public: 354 OpenWithRelationAttributeText(const Model *, const BColumn *, 355 const BPoseView *); 356 357 protected: 358 virtual void FitValue(BString *result, const BPoseView *); 359 virtual int64 ReadValue(); 360 virtual float PreferredWidth(const BPoseView *) const; 361 362 const BPoseView *fPoseView; 363 BString fRelationText; 364 }; 365 366 367 class VersionAttributeText : public StringAttributeText { 368 public: 369 VersionAttributeText(const Model *, const BColumn *, bool appVersion); 370 371 protected: 372 virtual void ReadValue(BString *result); 373 private: 374 bool fAppVersion; 375 }; 376 377 378 class AppShortVersionAttributeText : public VersionAttributeText { 379 public: 380 AppShortVersionAttributeText(const Model *model, const BColumn *column) 381 : VersionAttributeText(model, column, true) 382 { 383 } 384 }; 385 386 387 class SystemShortVersionAttributeText : public VersionAttributeText { 388 public: 389 SystemShortVersionAttributeText(const Model *model, const BColumn *column) 390 : VersionAttributeText(model, column, false) 391 { 392 } 393 }; 394 395 } // namespace BPrivate 396 397 398 extern status_t TimeFormat(BString &string, int32 index, FormatSeparator format, 399 DateOrder order, bool clockIs24Hour); 400 401 using namespace BPrivate; 402 403 #endif /* __TEXT_WIDGET_ATTRIBUTE__ */ 404