1/*
2 * Copyright 2002-2006, Stephan Aßmus <superstippi@gmx.de>
3 * All rights reserved. Distributed under the terms of the MIT license.
4 *
5 */
6
7#include "ColorPickerPanel.h"
8
9#include <stdio.h>
10
11#include <Application.h>
12#include <Catalog.h>
13#include <Locale.h>
14
15#if LIB_LAYOUT
16#  include <MBorder.h>
17#  include <HGroup.h>
18#  include <Space.h>
19#  include <MButton.h>
20#  include <VGroup.h>
21#else
22#  include <Box.h>
23#  include <Button.h>
24#endif
25
26#include "support_ui.h"
27
28#include "ColorPickerView.h"
29
30
31#undef B_TRANSLATION_CONTEXT
32#define B_TRANSLATION_CONTEXT "Icon-O-Matic-ColorPicker"
33
34
35enum {
36	MSG_CANCEL					= 'cncl',
37	MSG_DONE					= 'done',
38};
39
40// constructor
41ColorPickerPanel::ColorPickerPanel(BRect frame, rgb_color color,
42								   selected_color_mode mode,
43								   BWindow* window,
44								   BMessage* message, BHandler* target)
45	: Panel(frame, "Pick Color",
46			B_FLOATING_WINDOW_LOOK, B_FLOATING_SUBSET_WINDOW_FEEL,
47			B_ASYNCHRONOUS_CONTROLS |
48			B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_NOT_CLOSABLE),
49	  fWindow(window),
50	  fMessage(message),
51	  fTarget(target)
52{
53	SetTitle(B_TRANSLATE("Pick a color"));
54
55	fColorPickerView = new ColorPickerView("color picker", color, mode);
56
57#if LIB_LAYOUT
58	MButton* defaultButton = new MButton(B_TRANSLATE("OK"),
59		new BMessage(MSG_DONE), this);
60
61	// interface layout
62	BView* topView = new VGroup (
63		fColorPickerView,
64		new MBorder (
65			M_RAISED_BORDER, 5, "buttons",
66			new HGroup (
67				new Space(minimax(0.0, 0.0, 10000.0, 10000.0, 5.0)),
68				new MButton(B_TRANSLATE("Cancel"), new BMessage(MSG_CANCEL),
69					this),
70				new Space(minimax(5.0, 0.0, 10.0, 10000.0, 1.0)),
71				defaultButton,
72				new Space(minimax(2.0, 0.0, 2.0, 10000.0, 0.0)),
73				0
74			)
75		),
76		0
77	);
78#else // LIB_LAYOUT
79	frame = BRect(0, 0, 40, 15);
80	BButton* defaultButton = new BButton(frame, "ok button",
81								B_TRANSLATE("OK"), new BMessage(MSG_DONE),
82								B_FOLLOW_RIGHT | B_FOLLOW_TOP);
83	defaultButton->ResizeToPreferred();
84	BButton* cancelButton = new BButton(frame, "cancel button",
85								B_TRANSLATE("Cancel"), new BMessage(MSG_CANCEL),
86								B_FOLLOW_RIGHT | B_FOLLOW_TOP);
87	cancelButton->ResizeToPreferred();
88
89	frame.bottom = frame.top + (defaultButton->Frame().Height() + 16);
90	frame.right = frame.left + fColorPickerView->Frame().Width();
91	BBox* buttonBox = new BBox(frame, "button group",
92							   B_FOLLOW_LEFT_RIGHT | B_FOLLOW_BOTTOM,
93							   B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE_JUMP,
94							   B_PLAIN_BORDER);
95
96	ResizeTo(frame.Width(),
97			 fColorPickerView->Frame().Height() + frame.Height() + 1);
98
99	frame = Bounds();
100	BView* topView = new BView(frame, "bg", B_FOLLOW_ALL, 0);
101	topView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
102
103	buttonBox->MoveTo(frame.left, frame.bottom - buttonBox->Frame().Height());
104
105	defaultButton->MoveTo(frame.right - defaultButton->Frame().Width() - 10,
106						  frame.top + 8);
107	buttonBox->AddChild(defaultButton);
108
109	cancelButton->MoveTo(defaultButton->Frame().left - 10
110							- cancelButton->Frame().Width(),
111						 frame.top + 8);
112	buttonBox->AddChild(cancelButton);
113
114	topView->AddChild(fColorPickerView);
115	topView->AddChild(buttonBox);
116#endif // LIB_LAYOUT
117
118	SetDefaultButton(defaultButton);
119
120	if (fWindow)
121		AddToSubset(fWindow);
122	else
123		SetFeel(B_FLOATING_APP_WINDOW_FEEL);
124
125	AddChild(topView);
126}
127
128// destructor
129ColorPickerPanel::~ColorPickerPanel()
130{
131	delete fMessage;
132}
133
134// Cancel
135void
136ColorPickerPanel::Cancel()
137{
138	PostMessage(MSG_CANCEL);
139}
140
141// MessageReceived
142void
143ColorPickerPanel::MessageReceived(BMessage* message)
144{
145	switch (message->what) {
146		case MSG_CANCEL:
147		case MSG_DONE: {
148			BMessage msg('PSTE');
149			BLooper* looper = fTarget ? fTarget->Looper() : be_app;
150			if (fMessage)
151				msg = *fMessage;
152			if (message->what == MSG_DONE)
153				store_color_in_message(&msg, fColorPickerView->Color());
154			msg.AddRect("panel frame", Frame());
155			msg.AddInt32("panel mode", fColorPickerView->Mode());
156			msg.AddBool("begin", true);
157			looper->PostMessage(&msg, fTarget);
158			PostMessage(B_QUIT_REQUESTED);
159			break;
160		}
161		default:
162			Panel::MessageReceived(message);
163			break;
164	}
165}
166
167// #pragma mark -
168
169// SetColor
170void
171ColorPickerPanel::SetColor(rgb_color color)
172{
173	fColorPickerView->SetColor(color);
174}
175
176// SetMessage
177void
178ColorPickerPanel::SetMessage(BMessage* message)
179{
180	delete fMessage;
181	fMessage = message;
182}
183
184// SetTarget
185void
186ColorPickerPanel::SetTarget(BHandler* target)
187{
188	fTarget = target;
189}
190