xref: /haiku/src/tests/kits/interface/look/Look.cpp (revision bab64f65bb775dc23060e276f1f1c4498ab7af6c)
1 /*
2  * Copyright 2009, Stephan Aßmus <superstippi@gmx.de>. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include <Application.h>
7 #include <Box.h>
8 #include <Button.h>
9 #include <ChannelSlider.h>
10 #include <CheckBox.h>
11 #include <ColorControl.h>
12 #include <ColumnListView.h>
13 #include <ColumnTypes.h>
14 #include <ControlLook.h>
15 #include <FilePanel.h>
16 #include <LayoutBuilder.h>
17 #include <ListView.h>
18 #include <MenuBar.h>
19 #include <MenuField.h>
20 #include <MenuItem.h>
21 #include <OptionControl.h>
22 #include <OutlineListView.h>
23 #include <PopUpMenu.h>
24 #include <RadioButton.h>
25 #include <ScrollBar.h>
26 #include <ScrollView.h>
27 #include <Slider.h>
28 #include <SpaceLayoutItem.h>
29 #include <StatusBar.h>
30 #include <StringView.h>
31 #include <TabView.h>
32 #include <TextControl.h>
33 #include <TextView.h>
34 #include <Window.h>
35 
36 
37 static const float kInset = 8.0f;
38 
39 
40 template <class ControlType>
41 void
add_controls(BGridLayout * layout,int32 & row)42 add_controls(BGridLayout* layout, int32& row)
43 {
44 	ControlType* control1 = new ControlType("Enabled", NULL);
45 	ControlType* control2 = new ControlType("Disabled", NULL);
46 	control2->SetEnabled(false);
47 	ControlType* control3 = new ControlType("Enabled", NULL);
48 	control3->SetValue(B_CONTROL_ON);
49 	ControlType* control4 = new ControlType("Disabled", NULL);
50 	control4->SetValue(B_CONTROL_ON);
51 	control4->SetEnabled(false);
52 
53 	control1->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
54 	control2->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
55 	control3->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
56 	control4->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
57 
58 	layout->AddView(control1, 0, row);
59 	layout->AddView(control2, 1, row);
60 	layout->AddView(control3, 2, row);
61 	layout->AddView(control4, 3, row);
62 
63 	row++;
64 }
65 
66 
67 #define USE_LAYOUT_ITEMS 0
68 
69 
70 void
add_menu_fields(BGridLayout * layout,int32 & row)71 add_menu_fields(BGridLayout* layout, int32& row)
72 {
73 	BPopUpMenu* menu1 = new BPopUpMenu("Selection");
74 	BMenuField* control1 = new BMenuField("Enabled", menu1);
75 	BPopUpMenu* menu2 = new BPopUpMenu("Selection");
76 	BMenuField* control2 = new BMenuField("Disabled", menu2);
77 	control2->SetEnabled(false);
78 
79 #if USE_LAYOUT_ITEMS
80 	layout->AddItem(control1->CreateLabelLayoutItem(), 0, row);
81 	layout->AddItem(control1->CreateMenuBarLayoutItem(), 1, row);
82 	layout->AddItem(control2->CreateLabelLayoutItem(), 2, row);
83 	layout->AddItem(control2->CreateMenuBarLayoutItem(), 3, row);
84 #else
85 	layout->AddView(control1, 0, row, 2);
86 	layout->AddView(control2, 2, row, 2);
87 #endif
88 
89 	row++;
90 }
91 
92 
93 void
add_text_controls(BGridLayout * layout,int32 & row)94 add_text_controls(BGridLayout* layout, int32& row)
95 {
96 	BTextControl* control1 = new BTextControl("Enabled", "Some text", NULL);
97 	BTextControl* control2 = new BTextControl("Disabled", "More text", NULL);
98 	control2->SetEnabled(false);
99 
100 #if USE_LAYOUT_ITEMS
101 	layout->AddItem(control1->CreateLabelLayoutItem(), 0, row);
102 	layout->AddItem(control1->CreateTextViewLayoutItem(), 1, row);
103 	layout->AddItem(control2->CreateLabelLayoutItem(), 2, row);
104 	layout->AddItem(control2->CreateTextViewLayoutItem(), 3, row);
105 #else
106 	layout->AddView(control1, 0, row, 2);
107 	layout->AddView(control2, 2, row, 2);
108 #endif
109 
110 	row++;
111 }
112 
113 
114 void
add_sliders(BGridLayout * layout,int32 & row)115 add_sliders(BGridLayout* layout, int32& row)
116 {
117 	BSlider* control1 = new BSlider("slider 1", "Enabled", NULL, 1, 100,
118 		B_HORIZONTAL);
119 	BSlider* control2 = new BSlider("slider 2", "Disabled", NULL, 1, 100,
120 		B_HORIZONTAL);
121 	control2->SetEnabled(false);
122 
123 	control1->SetHashMarkCount(10);
124 	control1->SetHashMarks(B_HASH_MARKS_BOTTOM);
125 	control2->SetHashMarkCount(10);
126 	control2->SetHashMarks(B_HASH_MARKS_BOTTOM);
127 
128 	layout->AddView(control1, 0, row, 2);
129 	layout->AddView(control2, 2, row, 2);
130 
131 	row++;
132 
133 	control1 = new BSlider("slider 3", "Enabled", NULL, 1, 100,
134 		B_HORIZONTAL, B_TRIANGLE_THUMB);
135 	control2 = new BSlider("slider 4", "Disabled", NULL, 1, 100,
136 		B_HORIZONTAL, B_TRIANGLE_THUMB);
137 	control2->SetEnabled(false);
138 
139 	rgb_color fillColor = (rgb_color){ 255, 115, 0, 255 };
140 
141 	control1->SetLimitLabels("Min", "Max");
142 	control1->UseFillColor(true, &fillColor);
143 	control1->SetValue(20);
144 
145 	control2->SetLimitLabels("1", "100");
146 	control2->UseFillColor(true, &fillColor);
147 	control2->SetValue(10);
148 
149 	layout->AddView(control1, 0, row, 2);
150 	layout->AddView(control2, 2, row, 2);
151 
152 	row++;
153 }
154 
155 
156 void
add_status_bars(BGridLayout * layout,int32 & row)157 add_status_bars(BGridLayout* layout, int32& row)
158 {
159 	BBox* box = new BBox(B_FANCY_BORDER, NULL);
160 	box->SetLabel("Info");
161 
162 	BGroupLayout* boxLayout = new BGroupLayout(B_VERTICAL, kInset);
163 	boxLayout->SetInsets(kInset, kInset + box->TopBorderOffset(), kInset,
164 		kInset);
165 	box->SetLayout(boxLayout);
166 
167 	BStatusBar* statusBar = new BStatusBar("status bar", "Status",
168 		"Completed");
169 	statusBar->SetMaxValue(100);
170 	statusBar->SetTo(0);
171 	statusBar->SetBarHeight(12);
172 	boxLayout->AddView(statusBar);
173 
174 	statusBar = new BStatusBar("status bar", "Progress",
175 		"Completed");
176 	statusBar->SetMaxValue(100);
177 	statusBar->SetTo(40);
178 	statusBar->SetBarHeight(12);
179 	boxLayout->AddView(statusBar);
180 
181 	statusBar = new BStatusBar("status bar", "Lifespan of capitalism",
182 		"Completed");
183 	statusBar->SetMaxValue(100);
184 	statusBar->SetTo(100);
185 	statusBar->SetBarHeight(12);
186 	boxLayout->AddView(statusBar);
187 
188 	layout->AddView(box, 0, row, 4);
189 
190 	row++;
191 }
192 
193 
194 enum {
195 	MSG_TEST_OPEN_FILE_PANEL = 'tofp',
196 	MSG_TEST_SAVE_FILE_PANEL = 'tsfp',
197 	MSG_TOGGLE_LOOK = 'tggl'
198 };
199 
200 
201 class Window : public BWindow {
202 public:
Window(BRect frame,const char * title,window_type type,uint32 flags)203 	Window(BRect frame, const char* title, window_type type, uint32 flags)
204 		: BWindow(frame, title, type, flags)
205 	{
206 		fControlLook = NULL;
207 	}
~Window()208 	~Window()
209 	{
210 		if (fControlLook != NULL)
211 			be_control_look = fControlLook;
212 	}
213 
MessageReceived(BMessage * message)214 	virtual void MessageReceived(BMessage* message)
215 	{
216 		switch (message->what) {
217 		case MSG_TEST_OPEN_FILE_PANEL:
218 			{
219 				BFilePanel* panel = new BFilePanel();
220 				panel->Show();
221 			}
222 			break;
223 		case MSG_TEST_SAVE_FILE_PANEL:
224 			{
225 				BFilePanel* panel = new BFilePanel(B_SAVE_PANEL);
226 				panel->Show();
227 			}
228 			break;
229 		case MSG_TOGGLE_LOOK:
230 			{
231 				BControlLook* temp = fControlLook;
232 				fControlLook = be_control_look;
233 				be_control_look = temp;
234 				_InvalidateChildrenAndView(ChildAt(0));
235 			}
236 			break;
237 
238 		default:
239 			BWindow::MessageReceived(message);
240 		}
241 	}
242 
243 private:
_InvalidateChildrenAndView(BView * view)244 	void _InvalidateChildrenAndView(BView* view)
245 	{
246 		for (int32 i = 0; BView* child = view->ChildAt(i); i++)
247 			_InvalidateChildrenAndView(child);
248 		view->Invalidate();
249 	}
250 
251 private:
252 	BControlLook* fControlLook;
253 };
254 
255 
256 int
main(int argc,char ** argv)257 main(int argc, char** argv)
258 {
259 	BApplication app("application/x-vnd.haiku-look");
260 
261 	BWindow* window = new Window(BRect(50, 50, 100, 100),
262 		"Look at these pretty controls!", B_TITLED_WINDOW,
263 		B_ASYNCHRONOUS_CONTROLS | B_AUTO_UPDATE_SIZE_LIMITS
264 			| B_QUIT_ON_WINDOW_CLOSE);
265 
266 	window->SetLayout(new BGroupLayout(B_HORIZONTAL));
267 
268 	// create some controls
269 
270 	// BListView
271 	BListView* listView = new BListView();
272 	for (int32 i = 0; i < 20; i++) {
273 		BString itemLabel("List item ");
274 		itemLabel << i + 1;
275 		listView->AddItem(new BStringItem(itemLabel.String()));
276 	}
277 	BScrollView* scrollView = new BScrollView("scroller", listView, 0,
278 		true, true);
279 	scrollView->SetExplicitMinSize(BSize(300, 140));
280 
281 	// BColumnListView
282 	BColumnListView* columnListView = new BColumnListView("clv", 0,
283 		B_FANCY_BORDER);
284 //		B_PLAIN_BORDER);
285 //		B_NO_BORDER);
286 
287 	columnListView->AddColumn(new BTitledColumn("Short",
288 		150, 50, 500, B_ALIGN_LEFT), 0);
289 	columnListView->AddColumn(new BTitledColumn("Medium Length",
290 		100, 50, 500, B_ALIGN_CENTER), 1);
291 	columnListView->AddColumn(new BTitledColumn("Some Long Column Name",
292 		130, 50, 500, B_ALIGN_RIGHT), 2);
293 
294 //	for (int32 i = 0; i < 20; i++) {
295 //		BString itemLabel("List Item ");
296 //		itemLabel << i + 1;
297 //		columnListView->AddItem(new BStringItem(itemLabel.String()));
298 //	}
299 
300 
301 	BGridView* controls = new BGridView(kInset, kInset);
302 	BGridLayout* layout = controls->GridLayout();
303 	controls->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED));
304 
305 	int32 row = 0;
306 	add_controls<BButton>(layout, row);
307 	add_controls<BCheckBox>(layout, row);
308 	add_controls<BRadioButton>(layout, row);
309 	add_menu_fields(layout, row);
310 	add_text_controls(layout, row);
311 	add_sliders(layout, row);
312 	add_status_bars(layout, row);
313 
314 	BColorControl* colorControl = new BColorControl(B_ORIGIN, B_CELLS_32x8,
315 		8.0f, "color control");
316 	layout->AddView(colorControl, 0, row, 4);
317 
318 	BTabView* tabView = new BTabView("tab view", B_WIDTH_FROM_WIDEST);
319 	BGroupView* content = new BGroupView(B_VERTICAL, kInset);
320 	BLayoutBuilder::Group<>(content)
321 		.Add(scrollView)
322 		.Add(columnListView)
323 		.Add(controls)
324 		.SetInsets(kInset, kInset, kInset, kInset);
325 
326 	content->SetName("Tab 1");
327 
328 	tabView->AddTab(content);
329 	BView* tab2 = new BView("Tab 2", 0);
330 	tab2->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
331 	tabView->AddTab(tab2);
332 	tabView->AddTab(new BView("Tab 3", 0));
333 
334 	BMenuBar* menuBar = new BMenuBar("menu bar");
335 	BMenu* menu = new BMenu("File");
336 	menu->AddItem(new BMenuItem("Test Open BFilePanel",
337 		new BMessage(MSG_TEST_OPEN_FILE_PANEL)));
338 	menu->AddItem(new BMenuItem("Test Save BFilePanel",
339 		new BMessage(MSG_TEST_SAVE_FILE_PANEL)));
340 	menu->AddItem(new BMenuItem("Click me!", NULL));
341 	menu->AddItem(new BMenuItem("Another option", NULL));
342 	menu->AddSeparatorItem();
343 	menu->AddItem(new BMenuItem("Quit", new BMessage(B_QUIT_REQUESTED)));
344 	menuBar->AddItem(menu);
345 	menu = new BMenu("Edit");
346 	menu->SetEnabled(false);
347 	menu->AddItem(new BMenuItem("Cut", NULL));
348 	menu->AddItem(new BMenuItem("Copy", NULL));
349 	menu->AddSeparatorItem();
350 	menu->AddItem(new BMenuItem("Past", NULL));
351 	menuBar->AddItem(menu);
352 	menu = new BMenu("One Item");
353 	menu->AddItem(new BMenuItem("Only", NULL));
354 	menuBar->AddItem(menu);
355 	menu = new BMenu("Sub Menu");
356 	BMenu* subMenu = new BMenu("Click me");
357 	subMenu->AddItem(new BMenuItem("Either", NULL));
358 	subMenu->AddItem(new BMenuItem("Or", NULL));
359 	subMenu->SetRadioMode(true);
360 	menu->AddItem(subMenu);
361 	menuBar->AddItem(menu);
362 
363 	BButton* okButton = new BButton("OK", new BMessage(B_QUIT_REQUESTED));
364 
365 	BLayoutBuilder::Group<>(window, B_VERTICAL, 0)
366 		.Add(menuBar)
367 		.AddGroup(B_VERTICAL, kInset)
368 			.SetInsets(kInset, kInset, kInset, kInset)
369 			.Add(tabView)
370 			.AddGroup(B_HORIZONTAL, kInset)
371 				.Add(new BButton("Revert", new BMessage(MSG_TOGGLE_LOOK)))
372 				.AddGlue()
373 				.Add(new BButton("Cancel", NULL))
374 				.Add(okButton);
375 
376 	window->SetDefaultButton(okButton);
377 
378 	window->Show();
379 	app.Run();
380 	return 0;
381 }
382 
383