1/*
2 * Copyright 2006, Haiku.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Stephan Aßmus <superstippi@gmx.de>
7 */
8
9#include "IconView.h"
10
11#include <Bitmap.h>
12
13#include "ui_defines.h"
14
15#include "IconRenderer.h"
16
17// constructor
18IconView::IconView(BRect frame, const char* name)
19	: BView(frame, name, B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW),
20	  fBitmap(new BBitmap(frame.OffsetToCopy(B_ORIGIN), 0, B_RGB32)),
21	  fIcon(NULL),
22	  fRenderer(new IconRenderer(fBitmap)),
23	  fDirtyIconArea(fBitmap->Bounds()),
24
25	  fScale((frame.Width() + 1.0) / 64.0)
26{
27	fRenderer->SetScale(fScale);
28#if __HAIKU__
29	BSize size(frame.Width(), frame.Height());
30	SetExplicitMinSize(size);
31	SetExplicitMaxSize(size);
32#endif
33}
34
35// destructor
36IconView::~IconView()
37{
38	SetIcon(NULL);
39	delete fRenderer;
40	delete fBitmap;
41}
42
43// #pragma mark -
44
45// AttachedToWindow
46void
47IconView::AttachedToWindow()
48{
49	SetViewColor(B_TRANSPARENT_COLOR);
50
51	rgb_color lc = LowColor();
52	fRenderer->SetBackground(agg::rgba8(lc.red, lc.green, lc.blue, 255));
53}
54
55// Draw
56void
57IconView::Draw(BRect updateRect)
58{
59	if (fDirtyIconArea.IsValid()) {
60		fRenderer->Render(fDirtyIconArea);
61		fDirtyIconArea.Set(LONG_MAX, LONG_MAX, LONG_MIN, LONG_MIN);
62	}
63
64	// icon
65	DrawBitmap(fBitmap, B_ORIGIN);
66}
67
68// #pragma mark -
69
70// AreaInvalidated
71void
72IconView::AreaInvalidated(const BRect& area)
73{
74	BRect scaledArea(area);
75	scaledArea.left *= fScale;
76	scaledArea.top *= fScale;
77	scaledArea.right *= fScale;
78	scaledArea.bottom *= fScale;
79
80	if (fDirtyIconArea.Contains(scaledArea))
81		return;
82
83	fDirtyIconArea = fDirtyIconArea | scaledArea;
84
85	Invalidate(scaledArea);
86}
87
88
89// #pragma mark -
90
91// SetIcon
92void
93IconView::SetIcon(Icon* icon)
94{
95	if (fIcon == icon)
96		return;
97
98	if (fIcon)
99		fIcon->RemoveListener(this);
100
101	fIcon = icon;
102	fRenderer->SetIcon(icon);
103
104	if (fIcon)
105		fIcon->AddListener(this);
106}
107
108// SetIconBGColor
109void
110IconView::SetIconBGColor(const rgb_color& color)
111{
112	SetLowColor(color);
113
114	fRenderer->SetBackground(
115		agg::rgba8(color.red, color.green, color.blue, 255));
116
117	fDirtyIconArea = fBitmap->Bounds();
118	Invalidate();
119}
120