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 "ColorValueView.h"
10
11#include <stdio.h>
12
13#include <Message.h>
14#include <String.h>
15#include <Window.h>
16
17#include "support_ui.h"
18
19#include "PropertyItemView.h"
20#include "SwatchValueView.h"
21
22enum {
23	MSG_VALUE_CHANGED	= 'vchd',
24};
25
26// constructor
27ColorValueView::ColorValueView(ColorProperty* property)
28	: PropertyEditorView(),
29	  fProperty(property)
30{
31	fSwatchView = new SwatchValueView("swatch property view",
32									  NULL, this,/*new BMessage(MSG_SET_COLOR), this,*/
33									  fProperty->Value());
34	fSwatchView->SetDroppedMessage(new BMessage(MSG_VALUE_CHANGED));
35	AddChild(fSwatchView);
36}
37
38// destructor
39ColorValueView::~ColorValueView()
40{
41}
42
43// Draw
44void
45ColorValueView::Draw(BRect updateRect)
46{
47	BRect b(Bounds());
48	if (fSwatchView->IsFocus()) {
49		SetHighColor(ui_color(B_KEYBOARD_NAVIGATION_COLOR));
50		StrokeRect(b);
51		b.InsetBy(1.0, 1.0);
52		updateRect = updateRect & b;
53	}
54	FillRect(b, B_SOLID_LOW);
55}
56
57// FrameResized
58void
59ColorValueView::FrameResized(float width, float height)
60{
61	BRect b(Bounds());
62	b.InsetBy(2.0, 2.0);
63	b.left = floorf(b.left + (b.Width() / 2.0) - b.Height() / 2.0);
64	b.right = b.left + b.Height();
65
66	fSwatchView->MoveTo(b.LeftTop());
67	fSwatchView->ResizeTo(b.Width(), b.Height());
68}
69
70// MakeFocus
71void
72ColorValueView::MakeFocus(bool focused)
73{
74	fSwatchView->MakeFocus(focused);
75}
76
77// MessageReceived
78void
79ColorValueView::MessageReceived(BMessage* message)
80{
81	switch (message->what) {
82		case B_PASTE:
83			fSwatchView->MessageReceived(message);
84			break;
85//		case MSG_SET_COLOR:
86//			if (BWindow* window = Window())
87//				window->PostMessage(message, window);
88//			break;
89		case MSG_VALUE_CHANGED: {
90			rgb_color c;
91			if (restore_color_from_message(message, c) >= B_OK
92				&& fProperty->SetValue(c)) {
93				ValueChanged();
94			}
95			break;
96		}
97		default:
98			PropertyEditorView::MessageReceived(message);
99	}
100}
101
102// SetEnabled
103void
104ColorValueView::SetEnabled(bool enabled)
105{
106//	if (fEnabled != enabled) {
107//		fEnabled = enabled;
108//		Invalidate();
109//	}
110}
111
112// IsFocused
113bool
114ColorValueView::IsFocused() const
115{
116	return fSwatchView->IsFocus();
117}
118
119// AdoptProperty
120bool
121ColorValueView::AdoptProperty(Property* property)
122{
123	ColorProperty* p = dynamic_cast<ColorProperty*>(property);
124	if (p) {
125		rgb_color ownColor = fProperty->Value();
126		rgb_color color = p->Value();
127		if (ownColor != color) {
128			fSwatchView->SetColor(color);
129		}
130		fProperty = p;
131		return true;
132	}
133	return false;
134}
135
136// GetProperty
137Property*
138ColorValueView::GetProperty() const
139{
140	return fProperty;
141}
142
143
144
145