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 BSize; 67 68 class IconView : public BControl { 69 public: 70 IconView(const char* name, 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 BSize MaxSize(); 80 virtual BSize MinSize(); 81 virtual BSize PreferredSize(); 82 83 virtual void MouseDown(BPoint where); 84 virtual void MouseUp(BPoint where); 85 virtual void MouseMoved(BPoint where, uint32 transit, const BMessage* dragMessage); 86 virtual void KeyDown(const char* bytes, int32 numBytes); 87 88 virtual void MakeFocus(bool focus = true); 89 90 void SetTo(const entry_ref& file, const char* fileType = NULL); 91 void SetTo(const BMimeType& type); 92 void SetTo(::Icon* icon); 93 void Unset(); 94 void Update(); 95 96 void SetIconSize(int32 size); 97 void ShowIconHeap(bool show); 98 void ShowEmptyFrame(bool show); 99 status_t SetTarget(const BMessenger& target); 100 void SetModificationMessage(BMessage* message); 101 status_t Invoke(BMessage* message = NULL); 102 103 ::Icon* Icon(); 104 int32 IconSize() const { return fIconSize; } 105 icon_source IconSource() const { return fSource; } 106 status_t GetRef(entry_ref& ref) const; 107 status_t GetMimeType(BMimeType& type) const; 108 109 protected: 110 virtual bool AcceptsDrag(const BMessage* message); 111 virtual BRect BitmapRect() const; 112 113 private: 114 void _AddOrEditIcon(); 115 void _SetIcon(BBitmap* large, BBitmap* mini, const uint8* data, size_t size, 116 bool force = false); 117 void _SetIcon(entry_ref* ref); 118 void _RemoveIcon(); 119 void _DeleteIcons(); 120 void _StartWatching(); 121 void _StopWatching(); 122 123 BMessenger fTarget; 124 BMessage* fModificationMessage; 125 int32 fIconSize; 126 BBitmap* fIcon; 127 BBitmap* fHeapIcon; 128 129 bool fHasRef; 130 bool fHasType; 131 entry_ref fRef; 132 BMimeType fType; 133 icon_source fSource; 134 ::Icon* fIconData; 135 136 BPoint fDragPoint; 137 bool fTracking; 138 bool fDragging; 139 bool fDropTarget; 140 bool fShowEmptyFrame; 141 }; 142 143 static const uint32 kMsgIconInvoked = 'iciv'; 144 static const uint32 kMsgRemoveIcon = 'icrm'; 145 static const uint32 kMsgAddIcon = 'icad'; 146 static const uint32 kMsgEditIcon = 'iced'; 147 148 extern status_t icon_for_type(const BMimeType& type, uint8** _data, size_t* _size, 149 icon_source* _source = NULL); 150 extern status_t icon_for_type(const BMimeType& type, BBitmap& bitmap, 151 icon_size size, icon_source* _source = NULL); 152 153 #endif // ICON_VIEW_H 154