1 /* 2 * Copyright 2006, Axel Dörfler, axeld@pinc-software.de. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef ICON_VIEW_H 6 #define ICON_VIEW_H 7 8 9 #include <Control.h> 10 #include <Entry.h> 11 #include <Messenger.h> 12 #include <Mime.h> 13 #include <String.h> 14 15 16 enum icon_source { 17 kNoIcon = 0, 18 kOwnIcon, 19 kApplicationIcon, 20 kSupertypeIcon 21 }; 22 23 24 class Icon { 25 public: 26 Icon(); 27 Icon(const Icon& source); 28 ~Icon(); 29 30 void SetTo(const BAppFileInfo& info, const char* type = NULL); 31 void SetTo(const entry_ref& ref, const char* type = NULL); 32 void SetTo(const BMimeType& type, icon_source* _source = NULL); 33 status_t CopyTo(BAppFileInfo& info, const char* type = NULL, 34 bool force = false) const; 35 status_t CopyTo(const entry_ref& ref, const char* type = NULL, 36 bool force = false) const; 37 status_t CopyTo(BMimeType& type, bool force = false) const; 38 status_t CopyTo(BMessage& message) const; 39 40 void SetData(const uint8* data, size_t size); 41 void SetLarge(const BBitmap* large); 42 void SetMini(const BBitmap* large); 43 void Unset(); 44 45 bool HasData() const; 46 status_t GetData(icon_size which, BBitmap** _bitmap) const; 47 status_t GetData(uint8** _data, size_t* _size) const; 48 49 status_t GetIcon(BBitmap* bitmap) const; 50 51 Icon& operator=(const Icon& source); 52 53 void AdoptLarge(BBitmap *large); 54 void AdoptMini(BBitmap *mini); 55 void AdoptData(uint8* data, size_t size); 56 57 static BBitmap* AllocateBitmap(int32 size, int32 space = -1); 58 59 private: 60 BBitmap* fLarge; 61 BBitmap* fMini; 62 uint8* fData; 63 size_t fSize; 64 }; 65 66 class IconView : public BControl { 67 public: 68 IconView(BRect rect, const char* name, 69 uint32 resizeMode = B_FOLLOW_LEFT | B_FOLLOW_TOP, 70 uint32 flags = B_NAVIGABLE); 71 virtual ~IconView(); 72 73 virtual void AttachedToWindow(); 74 virtual void DetachedFromWindow(); 75 virtual void MessageReceived(BMessage* message); 76 virtual void Draw(BRect updateRect); 77 virtual void GetPreferredSize(float* _width, float* _height); 78 79 virtual void MouseDown(BPoint where); 80 virtual void MouseUp(BPoint where); 81 virtual void MouseMoved(BPoint where, uint32 transit, const BMessage* dragMessage); 82 virtual void KeyDown(const char* bytes, int32 numBytes); 83 84 virtual void MakeFocus(bool focus = true); 85 86 void SetTo(const entry_ref& file, const char* fileType = NULL); 87 void SetTo(const BMimeType& type); 88 void SetTo(::Icon* icon); 89 void Unset(); 90 void Update(); 91 92 void SetIconSize(int32 size); 93 void ShowIconHeap(bool show); 94 void ShowEmptyFrame(bool show); 95 void SetTarget(const BMessenger& target); 96 void Invoke(); 97 98 ::Icon* Icon(); 99 int32 IconSize() const { return fIconSize; } 100 icon_source IconSource() const { return fSource; } 101 status_t GetRef(entry_ref& ref) const; 102 status_t GetMimeType(BMimeType& type) const; 103 104 protected: 105 virtual bool AcceptsDrag(const BMessage* message); 106 virtual BRect BitmapRect() const; 107 108 private: 109 void _AddOrEditIcon(); 110 void _SetIcon(BBitmap* large, BBitmap* mini, const uint8* data, size_t size, 111 bool force = false); 112 void _SetIcon(entry_ref* ref); 113 void _RemoveIcon(); 114 void _DeleteIcons(); 115 void _StartWatching(); 116 void _StopWatching(); 117 118 BMessenger fTarget; 119 int32 fIconSize; 120 BBitmap* fIcon; 121 BBitmap* fHeapIcon; 122 123 bool fHasRef; 124 bool fHasType; 125 entry_ref fRef; 126 BMimeType fType; 127 icon_source fSource; 128 ::Icon* fIconData; 129 130 BPoint fDragPoint; 131 bool fTracking; 132 bool fDragging; 133 bool fDropTarget; 134 bool fShowEmptyFrame; 135 }; 136 137 static const uint32 kMsgIconInvoked = 'iciv'; 138 static const uint32 kMsgRemoveIcon = 'icrm'; 139 static const uint32 kMsgAddIcon = 'icad'; 140 static const uint32 kMsgEditIcon = 'iced'; 141 142 extern status_t icon_for_type(const BMimeType& type, uint8** _data, size_t* _size, 143 icon_source* _source = NULL); 144 extern status_t icon_for_type(const BMimeType& type, BBitmap& bitmap, 145 icon_size size, icon_source* _source = NULL); 146 147 #endif // ICON_VIEW_H 148