xref: /haiku/src/tests/libs/alm/FriendLayout.cpp (revision 4bd0c1066b227cec4b79883bdef697c7a27f2e90)
1 /*
2  * Copyright 2007-2008, Christof Lutteroth, lutteroth@cs.auckland.ac.nz
3  * Copyright 2007-2008, James Kim, jkim202@ec.auckland.ac.nz
4  * Copyright 2010, Clemens Zeidler <haiku@clemens-zeidler.de>
5  * Copyright 2012, Haiku, Inc.
6  * Distributed under the terms of the MIT License.
7  */
8 
9 #include <Application.h>
10 #include <Button.h>
11 #include <LayoutBuilder.h>
12 #include <List.h>
13 #include <Message.h>
14 #include <StringView.h>
15 #include <Window.h>
16 
17 // include this for ALM
18 #include "ALMLayout.h"
19 #include "ALMLayoutBuilder.h"
20 #include "LinearProgrammingTypes.h"
21 
22 
23 class FriendWindow : public BWindow {
24 public:
25 	FriendWindow(BRect frame)
26 		:
27 		BWindow(frame, "ALM Friend Test", B_TITLED_WINDOW,
28 			B_QUIT_ON_WINDOW_CLOSE | B_AUTO_UPDATE_SIZE_LIMITS),
29 		fLayout2(NULL),
30 		fBoom(NULL),
31 		fLeft(NULL),
32 		fTop(NULL),
33 		fRight(NULL),
34 		fBottom(NULL)
35 	{
36 		BButton* button1 = _MakeButton("friends!");
37 		BButton* button2 = _MakeButton("friends!");
38 		BButton* button3 = _MakeButton("friends!");
39 
40 		BButton* button4 = _MakeButton("friends!");
41 		BButton* button5 = _MakeButton("friends!");
42 		BButton* button6 = _MakeButton("friends!");
43 
44 		BALMLayout* layout1 = new BALMLayout(10, 10);
45 		BView* almView1 = _MakeALMView(layout1);
46 
47 		BReference<XTab> xTabs[2];
48 		layout1->AddXTabs(xTabs, 2);
49 
50 		BALM::BALMLayoutBuilder(layout1)
51 			.Add(button1, layout1->Left(), layout1->Top(),
52 				xTabs[0], layout1->Bottom())
53 			.StartingAt(button1)
54 				.AddToRight(button2, xTabs[1])
55 				.AddToRight(button3, layout1->Right());
56 
57 		fLayout2 = new BALMLayout(10, 10, layout1);
58 		BView* almView2 = _MakeALMView(fLayout2);
59 
60 		BALM::BALMLayoutBuilder(fLayout2)
61 			.Add(button4, fLayout2->Left(), fLayout2->Top(), xTabs[0])
62 			.StartingAt(button4)
63 				.AddBelow(button5, NULL, xTabs[1], fLayout2->Right())
64 				.AddBelow(button6, fLayout2->Bottom(), xTabs[0]);
65 
66 		fLeft = fLayout2->Left();
67 		fBottom = fLayout2->BottomOf(button5);
68 		fTop = fLayout2->BottomOf(button4);
69 		fRight = xTabs[1];
70 
71 		layout1->AreaFor(button2)->SetContentAspectRatio(1.0f);
72 		fLayout2->Solver()->AddConstraint(-1.0f, layout1->Left(), 1.0f, xTabs[0],
73 			LinearProgramming::kLE, 90.0f);
74 
75 		BButton* archiveButton = new BButton("clone", new BMessage('arcv'));
76 		archiveButton->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED,
77 				B_SIZE_UNSET));
78 		BLayoutBuilder::Group<>(this, B_VERTICAL)
79 			.Add(almView1->GetLayout())
80 			.Add(almView2->GetLayout())
81 			.Add(archiveButton);
82 	}
83 
84 
85 	void MessageReceived(BMessage* message)
86 	{
87 		switch (message->what) {
88 			case 'BOOM':
89 				if (!fBoom) {
90 					fBoom = _MakeButton("BOOM");
91 					fLayout2->AddView(fBoom, fLeft, fTop,
92 						fRight, fBottom);
93 				} else {
94 					if (fBoom->IsHidden(fBoom))
95 						fBoom->Show();
96 					else
97 						fBoom->Hide();
98 				}
99 				break;
100 			case 'arcv': {
101 				BView* view = GetLayout()->View();
102 				BMessage archive;
103 				status_t err = view->Archive(&archive, true);
104 				BWindow* window = new BWindow(BRect(30, 30, 400, 400),
105 					"ALM Friend Test Clone", B_TITLED_WINDOW,
106 					B_QUIT_ON_WINDOW_CLOSE | B_AUTO_UPDATE_SIZE_LIMITS);
107 				window->SetLayout(new BGroupLayout(B_VERTICAL));
108 				BView* clone;
109 				if (err == B_OK)
110 					err = BUnarchiver::InstantiateObject(&archive, clone);
111 				if (err != B_OK)
112 					window->AddChild(new BStringView("", "An error occurred!"));
113 				else {
114 					window->AddChild(clone);
115 				}
116 				window->Show();
117 
118 				break;
119 			}
120 			default:
121 				BWindow::MessageReceived(message);
122 		}
123 	}
124 
125 private:
126 	BButton* _MakeButton(const char* label)
127 	{
128 		BButton* button = new BButton(label, new BMessage('BOOM'));
129 		button->SetExplicitMinSize(BSize(10, 50));
130 		button->SetExplicitMaxSize(BSize(500, 500));
131 		button->SetExplicitAlignment(BAlignment(B_ALIGN_USE_FULL_WIDTH,
132 			B_ALIGN_USE_FULL_HEIGHT));
133 		return button;
134 	}
135 
136 	BView* _MakeALMView(BALMLayout* layout)
137 	{
138 		BView* view = new BView(NULL, 0, layout);
139 		view->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
140 		return view;
141 	}
142 
143 	BALMLayout* fLayout2;
144 	BButton*	fBoom;
145 	XTab*		fLeft;
146 	YTab*		fTop;
147 	XTab*		fRight;
148 	YTab*		fBottom;
149 };
150 
151 
152 class Friend : public BApplication {
153 public:
154 	Friend()
155 		:
156 		BApplication("application/x-vnd.haiku.Friend")
157 	{
158 		BRect frameRect;
159 		frameRect.Set(100, 100, 300, 300);
160 		FriendWindow* window = new FriendWindow(frameRect);
161 		window->Show();
162 	}
163 };
164 
165 
166 int
167 main()
168 {
169 	Friend app;
170 	app.Run();
171 	return 0;
172 }
173 
174