1 /* 2 * Copyright 2004-2020, Haiku, Inc. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Adrien Destugues, pulkomandy@pulkomandy.tk 7 * Michael Wilber 8 */ 9 10 11 #include "IconView.h" 12 13 #include <new> 14 #include <stdio.h> 15 #include <string.h> 16 17 #include <Entry.h> 18 #include <IconUtils.h> 19 #include <Node.h> 20 #include <NodeInfo.h> 21 22 23 using std::nothrow; 24 25 26 IconView::IconView(icon_size iconSize) 27 : 28 BView("IconView", B_WILL_DRAW), 29 fIconSize(iconSize), 30 fIconBitmap(new BBitmap(BRect(iconSize), B_RGBA32)), 31 fDrawIcon(false) 32 { 33 SetViewUIColor(B_PANEL_BACKGROUND_COLOR); 34 } 35 36 37 IconView::~IconView() 38 { 39 delete fIconBitmap; 40 fIconBitmap = NULL; 41 } 42 43 44 status_t 45 IconView::SetIcon(const BPath& path, icon_size iconSize) 46 { 47 fDrawIcon = false; 48 49 if (iconSize != fIconSize) { 50 BBitmap* bitmap = new BBitmap(BRect(iconSize), B_RGBA32); 51 if (bitmap == NULL) 52 return B_NO_MEMORY; 53 54 delete fIconBitmap; 55 fIconBitmap = bitmap; 56 fIconSize = iconSize; 57 } 58 59 status_t status = fIconBitmap->InitCheck(); 60 if (status != B_OK) 61 return status; 62 63 BEntry entry(path.Path()); 64 BNode node(&entry); 65 BNodeInfo info(&node); 66 67 status = info.GetTrackerIcon(fIconBitmap, fIconSize); 68 if (status != B_OK) 69 return status; 70 71 if (!fIconBitmap->IsValid()) 72 return fIconBitmap->InitCheck(); 73 74 _SetSize(); 75 76 fDrawIcon = true; 77 Invalidate(); 78 return B_OK; 79 } 80 81 82 status_t 83 IconView::SetIcon(const uint8_t* data, size_t size, icon_size iconSize) 84 { 85 fDrawIcon = false; 86 87 if (iconSize != fIconSize) { 88 BBitmap* bitmap = new BBitmap(BRect(iconSize), B_RGBA32); 89 if (bitmap == NULL) 90 return B_NO_MEMORY; 91 92 delete fIconBitmap; 93 fIconBitmap = bitmap; 94 fIconSize = iconSize; 95 } 96 97 status_t status = fIconBitmap->InitCheck(); 98 if (status != B_OK) 99 return status; 100 101 status = BIconUtils::GetVectorIcon(data, size, fIconBitmap); 102 if (status != B_OK) 103 return status; 104 105 if (!fIconBitmap->IsValid()) 106 return fIconBitmap->InitCheck(); 107 108 _SetSize(); 109 110 fDrawIcon = true; 111 Invalidate(); 112 return B_OK; 113 } 114 115 116 void 117 IconView::DrawIcon(bool draw) 118 { 119 if (draw == fDrawIcon) 120 return; 121 122 fDrawIcon = draw; 123 Invalidate(); 124 } 125 126 127 void 128 IconView::Draw(BRect area) 129 { 130 if (fDrawIcon && fIconBitmap != NULL) { 131 SetDrawingMode(B_OP_ALPHA); 132 SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY); 133 DrawBitmap(fIconBitmap); 134 SetDrawingMode(B_OP_COPY); 135 } else 136 BView::Draw(area); 137 } 138 139 140 status_t 141 IconView::InitCheck() const 142 { 143 if (fIconBitmap == NULL) 144 return B_NO_MEMORY; 145 146 return fIconBitmap->InitCheck(); 147 } 148 149 150 void 151 IconView::_SetSize() 152 { 153 SetExplicitMinSize(BSize(fIconSize, fIconSize)); 154 SetExplicitMaxSize(BSize(fIconSize, fIconSize)); 155 SetExplicitPreferredSize(BSize(fIconSize, fIconSize)); 156 } 157