1/*
2 * Copyright 2007, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5
6#include "BoxTest.h"
7
8#include <stdio.h>
9
10#include <Box.h>
11#include <Button.h>
12#include <Message.h>
13
14#include "CheckBox.h"
15#include "GroupView.h"
16#include "RadioButton.h"
17#include "TestView.h"
18
19
20// messages
21enum {
22	MSG_BORDER_STYLE_CHANGED	= 'bstc',
23	MSG_LABEL_CHANGED			= 'lbch',
24	MSG_LONG_LABEL_CHANGED		= 'llch',
25	MSG_CHILD_CHANGED			= 'chch'
26};
27
28
29// BorderStyleRadioButton
30class BoxTest::BorderStyleRadioButton : public LabeledRadioButton {
31public:
32	BorderStyleRadioButton(const char* label, border_style style)
33		: LabeledRadioButton(label),
34		  fBorderStyle(style)
35	{
36	}
37
38	border_style	fBorderStyle;
39};
40
41
42// LabelRadioButton
43class BoxTest::LabelRadioButton : public LabeledRadioButton {
44public:
45	LabelRadioButton(const char* label, const char* boxLabel,
46		bool labelView = false)
47		: LabeledRadioButton(label),
48		  fLabel(boxLabel),
49		  fLabelView(labelView)
50	{
51	}
52
53	const char*	fLabel;
54	bool		fLabelView;
55};
56
57
58// constructor
59BoxTest::BoxTest()
60	: Test("Box", NULL),
61	  fBox(new BBox("test box")),
62	  fChild(NULL),
63	  fBorderStyleRadioGroup(NULL),
64	  fLabelRadioGroup(NULL),
65	  fLongLabelCheckBox(NULL),
66	  fChildCheckBox(NULL)
67{
68	SetView(fBox);
69}
70
71
72// destructor
73BoxTest::~BoxTest()
74{
75	delete fBorderStyleRadioGroup;
76	delete fLabelRadioGroup;
77}
78
79
80// CreateTest
81Test*
82BoxTest::CreateTest()
83{
84	return new BoxTest;
85}
86
87
88// ActivateTest
89void
90BoxTest::ActivateTest(View* controls)
91{
92	// BBox sets its background color to that of its parent in
93	// AttachedToWindow(). Override.
94	rgb_color background = ui_color(B_PANEL_BACKGROUND_COLOR);
95	fBox->SetViewColor(background);
96	fBox->SetLowColor(background);
97
98	GroupView* group = new GroupView(B_VERTICAL);
99	group->SetFrame(controls->Bounds());
100	group->SetSpacing(0, 8);
101	controls->AddChild(group);
102
103	// the radio button group for selecting the border style
104
105	fBorderStyleRadioGroup = new RadioButtonGroup(
106		new BMessage(MSG_BORDER_STYLE_CHANGED), this);
107
108	// no border
109	LabeledRadioButton* button = new BorderStyleRadioButton("no border",
110		B_NO_BORDER);
111	group->AddChild(button);
112	fBorderStyleRadioGroup->AddButton(button->GetRadioButton());
113
114	// plain border
115	button = new BorderStyleRadioButton("plain border", B_PLAIN_BORDER);
116	group->AddChild(button);
117	fBorderStyleRadioGroup->AddButton(button->GetRadioButton());
118
119	// fancy border
120	button = new BorderStyleRadioButton("fancy border", B_FANCY_BORDER);
121	group->AddChild(button);
122	fBorderStyleRadioGroup->AddButton(button->GetRadioButton());
123
124	// default to no border
125	fBorderStyleRadioGroup->SelectButton((int32)0);
126
127	// spacing
128	group->AddChild(new VStrut(10));
129
130	// the radio button group for selecting the label
131
132	fLabelRadioGroup = new RadioButtonGroup(new BMessage(MSG_LABEL_CHANGED),
133		this);
134
135	// no label
136	button = new LabelRadioButton("No label", NULL);
137	group->AddChild(button);
138	fLabelRadioGroup->AddButton(button->GetRadioButton());
139
140	// label string
141	button = new LabelRadioButton("Label string", "");
142	group->AddChild(button);
143	fLabelRadioGroup->AddButton(button->GetRadioButton());
144
145	// label view
146	button = new LabelRadioButton("Label view", NULL, true);
147	group->AddChild(button);
148	fLabelRadioGroup->AddButton(button->GetRadioButton());
149
150	// default to no border
151	fLabelRadioGroup->SelectButton((int32)0);
152
153	// spacing
154	group->AddChild(new VStrut(10));
155
156	// long label
157	fLongLabelCheckBox = new LabeledCheckBox("Long label",
158		new BMessage(MSG_LONG_LABEL_CHANGED), this);
159	group->AddChild(fLongLabelCheckBox);
160
161	// child
162	fChildCheckBox = new LabeledCheckBox("Child",
163		new BMessage(MSG_CHILD_CHANGED), this);
164	group->AddChild(fChildCheckBox);
165
166
167	// glue
168	group->AddChild(new Glue());
169}
170
171
172// DectivateTest
173void
174BoxTest::DectivateTest()
175{
176}
177
178
179// MessageReceived
180void
181BoxTest::MessageReceived(BMessage* message)
182{
183	switch (message->what) {
184		case MSG_BORDER_STYLE_CHANGED:
185			_UpdateBorderStyle();
186			break;
187		case MSG_LABEL_CHANGED:
188			_UpdateLabel();
189			break;
190		case MSG_LONG_LABEL_CHANGED:
191			_UpdateLongLabel();
192			break;
193		case MSG_CHILD_CHANGED:
194			_UpdateChild();
195			break;
196		default:
197			Test::MessageReceived(message);
198			break;
199	}
200}
201
202
203// _UpdateBorderStyle
204void
205BoxTest::_UpdateBorderStyle()
206{
207	if (fBorderStyleRadioGroup) {
208		// We need to get the parent of the actually selected button, since
209		// that is the labeled radio button we've derived our
210		// BorderStyleRadioButton from.
211		AbstractButton* selectedButton
212			= fBorderStyleRadioGroup->SelectedButton();
213		View* parent = (selectedButton ? selectedButton->Parent() : NULL);
214		BorderStyleRadioButton* button = dynamic_cast<BorderStyleRadioButton*>(
215			parent);
216		if (button)
217			fBox->SetBorder(button->fBorderStyle);
218	}
219}
220
221
222// _UpdateLabel
223void
224BoxTest::_UpdateLabel()
225{
226	if (fLabelRadioGroup) {
227		// We need to get the parent of the actually selected button, since
228		// that is the labeled radio button we've derived our
229		// BorderStyleRadioButton from.
230		AbstractButton* selectedButton = fLabelRadioGroup->SelectedButton();
231		View* parent = (selectedButton ? selectedButton->Parent() : NULL);
232		LabelRadioButton* button = dynamic_cast<LabelRadioButton*>(parent);
233		if (button) {
234			if (button->fLabelView)
235				fBox->SetLabel(new BButton("", NULL));
236			else
237				fBox->SetLabel(button->fLabel);
238
239			_UpdateLongLabel();
240		}
241	}
242}
243
244
245// _UpdateLongLabel
246void
247BoxTest::_UpdateLongLabel()
248{
249	if (!fLongLabelCheckBox)
250		return;
251
252	const char* label = (fLongLabelCheckBox->IsSelected()
253		? "Quite Long Label for a BBox"
254		: "Label");
255
256	if (BView* labelView = fBox->LabelView()) {
257		if (BButton* button = dynamic_cast<BButton*>(labelView))
258			button->SetLabel(label);
259	} else if (fBox->Label())
260		fBox->SetLabel(label);
261}
262
263
264// _UpdateChild
265void
266BoxTest::_UpdateChild()
267{
268	if (!fChildCheckBox || fChildCheckBox->IsSelected() == (fChild != NULL))
269		return;
270
271	if (fChild) {
272		fBox->RemoveChild(fChild);
273		fChild = NULL;
274	} else {
275		fChild = new TestView(BSize(20, 10), BSize(350, 200), BSize(100, 70));
276		fBox->AddChild(fChild);
277	}
278}
279