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 "IconValueView.h"
10
11#include <stdio.h>
12#include <string.h>
13
14#include <Bitmap.h>
15#include <Region.h>
16
17#include "PropertyItemView.h"
18
19// constructor
20IconValueView::IconValueView(IconProperty* property)
21	: PropertyEditorView(),
22	  fProperty(property),
23	  fIcon(NULL)
24{
25	SetFlags(Flags() | B_NAVIGABLE_JUMP);
26}
27
28// destructor
29IconValueView::~IconValueView()
30{
31	delete fIcon;
32}
33
34// Draw
35void
36IconValueView::Draw(BRect updateRect)
37{
38	BRect r;
39	BRegion originalClippingRegion;
40	GetClippingRegion(&originalClippingRegion);
41	if (fIcon) {
42		BRect b(Bounds());
43		// layout icon in the center
44		r = fIcon->Bounds();
45		r.OffsetTo(floorf(b.left + b.Width() / 2.0 - r.Width() / 2.0),
46				   floorf(b.top + b.Height() / 2.0 - r.Height() / 2.0));
47		if (fIcon->ColorSpace() == B_RGBA32 || fIcon->ColorSpace() == B_RGBA32_BIG) {
48			// set up transparent drawing and let
49			// the base class draw the entire background
50			SetHighColor(255, 255, 255, 255);
51			SetDrawingMode(B_OP_ALPHA);
52			SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
53		} else {
54			// constrain clipping region to exclude bitmap
55			BRegion region = originalClippingRegion;
56			region.Exclude(r);
57			ConstrainClippingRegion(&region);
58		}
59	}
60	// draw surrouing area (and possibly background for bitmap)
61	PropertyEditorView::Draw(updateRect);
62
63	ConstrainClippingRegion(&originalClippingRegion);
64	if (fIcon) {
65		DrawBitmap(fIcon, r.LeftTop());
66	}
67}
68
69// SetEnabled
70void
71IconValueView::SetEnabled(bool enabled)
72{
73	// TODO: gray out icon...
74}
75
76// AdoptProperty
77bool
78IconValueView::AdoptProperty(Property* property)
79{
80	IconProperty* p = dynamic_cast<IconProperty*>(property);
81	if (p) {
82		SetIcon(p->Icon(), p->Width(), p->Height(), p->Format());
83		Invalidate();
84
85		fProperty = p;
86		return true;
87	}
88	return false;
89}
90
91// GetProperty
92Property*
93IconValueView::GetProperty() const
94{
95	return fProperty;
96}
97
98// #pragma mark -
99
100// SetIcon
101status_t
102IconValueView::SetIcon(const unsigned char* bitsFromQuickRes,
103					   uint32 width, uint32 height, color_space format)
104{
105	status_t status = B_BAD_VALUE;
106	if (bitsFromQuickRes && width > 0 && height > 0) {
107		delete fIcon;
108		fIcon = new BBitmap(BRect(0.0, 0.0, width - 1.0, height - 1.0), format);
109		status = fIcon ? fIcon->InitCheck() : B_ERROR;
110		if (status >= B_OK) {
111			// It doesn't look right to copy BitsLength() bytes, but bitmaps
112			// exported from QuickRes or WonderBrush still contain their padding,
113			// so it is alright.
114			memcpy(fIcon->Bits(), bitsFromQuickRes, fIcon->BitsLength());
115		} else {
116			delete fIcon;
117			fIcon = NULL;
118			printf("IconValueView::SetIcon() - error allocating bitmap: %s\n", strerror(status));
119		}
120	}
121	return status;
122}
123
124
125