xref: /haiku/src/kits/shared/IconView.cpp (revision e81a954787e50e56a7f06f72705b7859b6ab06d1)
1 // Author: Michael Wilber
2 // Copyright (C) Haiku, uses the MIT license
3 
4 
5 #include "IconView.h"
6 
7 #include <new>
8 #include <stdio.h>
9 #include <string.h>
10 
11 #include <Entry.h>
12 #include <Node.h>
13 #include <NodeInfo.h>
14 
15 
16 using std::nothrow;
17 
18 
19 IconView::IconView(icon_size iconSize)
20 	:
21 	BView("IconView", B_WILL_DRAW),
22 	fIconSize(iconSize),
23 	fIconBitmap(new BBitmap(BRect(iconSize), B_RGBA32)),
24 	fDrawIcon(false)
25 {
26 	SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
27 }
28 
29 
30 IconView::~IconView()
31 {
32 	delete fIconBitmap;
33 	fIconBitmap = NULL;
34 }
35 
36 
37 status_t
38 IconView::SetIcon(const BPath& path, icon_size iconSize)
39 {
40 	fDrawIcon = false;
41 
42 	if (iconSize != fIconSize) {
43 		BBitmap* bitmap = new BBitmap(BRect(iconSize), B_RGBA32);
44 		if (bitmap == NULL)
45 			return B_NO_MEMORY;
46 
47 		delete fIconBitmap;
48 		fIconBitmap = bitmap;
49 		fIconSize = iconSize;
50 	}
51 
52 	status_t status = fIconBitmap->InitCheck();
53 	if (status != B_OK)
54 		return status;
55 
56 	BEntry entry(path.Path());
57 	BNode node(&entry);
58 	BNodeInfo info(&node);
59 
60 	status = info.GetTrackerIcon(fIconBitmap, fIconSize);
61 	if (status != B_OK)
62 		return status;
63 
64 	if (!fIconBitmap->IsValid())
65 		return fIconBitmap->InitCheck();
66 
67 	_SetSize();
68 
69 	fDrawIcon = true;
70 	Invalidate();
71 	return B_OK;
72 }
73 
74 
75 void
76 IconView::DrawIcon(bool draw)
77 {
78 	if (draw == fDrawIcon)
79 		return;
80 
81 	fDrawIcon = draw;
82 	Invalidate();
83 }
84 
85 
86 void
87 IconView::Draw(BRect area)
88 {
89 	if (fDrawIcon && fIconBitmap != NULL) {
90 		SetDrawingMode(B_OP_ALPHA);
91 		SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
92 		DrawBitmap(fIconBitmap);
93 		SetDrawingMode(B_OP_COPY);
94 	} else
95 		BView::Draw(area);
96 }
97 
98 
99 status_t
100 IconView::InitCheck() const
101 {
102 	if (fIconBitmap == NULL)
103 		return B_NO_MEMORY;
104 
105 	return fIconBitmap->InitCheck();
106 }
107 
108 
109 void
110 IconView::_SetSize()
111 {
112 	SetExplicitMinSize(BSize(fIconSize, fIconSize));
113 	SetExplicitMaxSize(BSize(fIconSize, fIconSize));
114 	SetExplicitPreferredSize(BSize(fIconSize, fIconSize));
115 }
116