xref: /haiku/src/tests/kits/interface/SimpleAlertTest.cpp (revision ded5af13dcf0b71231fee57a5d9502d388c1f758)
1 /*
2  * Copyright 2014, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <Alert.h>
8 #include <Application.h>
9 #include <Button.h>
10 #include <LayoutBuilder.h>
11 #include <MenuField.h>
12 #include <Slider.h>
13 #include <String.h>
14 #include <StringView.h>
15 #include <Window.h>
16 
17 #include <stdio.h>
18 
19 
20 const uint32 kMsgShowAlert = 'shAl';
21 
22 
23 //	#pragma mark -
24 
25 
26 class Window : public BWindow {
27 public:
28 								Window();
29 
30 	virtual void				MessageReceived(BMessage* message);
31 	virtual	bool				QuitRequested();
32 
33 private:
34 			button_width		_ButtonWidth();
35 			button_spacing		_ButtonSpacing();
36 			alert_type			_AlertType();
37 
38 private:
39 			BMenuField*			fSizeField;
40 			BMenuField*			fTypeField;
41 			BMenuField*			fSpacingField;
42 			BSlider*			fCountSlider;
43 			BStringView*		fLastStringView;
44 };
45 
46 
47 Window::Window()
48 	:
49 	BWindow(BRect(100, 100, 620, 200), "Alert-Test", B_TITLED_WINDOW,
50 		B_ASYNCHRONOUS_CONTROLS | B_AUTO_UPDATE_SIZE_LIMITS)
51 {
52 	BMenu* sizeMenu = new BMenu("Button size");
53 	sizeMenu->SetLabelFromMarked(true);
54 	sizeMenu->SetRadioMode(true);
55 	sizeMenu->AddItem(new BMenuItem("As usual", NULL));
56 	sizeMenu->AddItem(new BMenuItem("From widest", NULL));
57 	sizeMenu->AddItem(new BMenuItem("From label", NULL));
58 	sizeMenu->ItemAt(0)->SetMarked(true);
59 	fSizeField = new BMenuField("Button size", sizeMenu);
60 
61 	BMenu* typeMenu = new BMenu("Alert type");
62 	typeMenu->SetLabelFromMarked(true);
63 	typeMenu->SetRadioMode(true);
64 	typeMenu->AddItem(new BMenuItem("Empty", NULL));
65 	typeMenu->AddItem(new BMenuItem("Info", NULL));
66 	typeMenu->AddItem(new BMenuItem("Idea", NULL));
67 	typeMenu->AddItem(new BMenuItem("Warning", NULL));
68 	typeMenu->AddItem(new BMenuItem("Stop", NULL));
69 	typeMenu->ItemAt(0)->SetMarked(true);
70 	fTypeField = new BMenuField("Alert type", typeMenu);
71 
72 	BMenu* spacingMenu = new BMenu("Button spacing");
73 	spacingMenu->SetLabelFromMarked(true);
74 	spacingMenu->SetRadioMode(true);
75 	spacingMenu->AddItem(new BMenuItem("Even", NULL));
76 	spacingMenu->AddItem(new BMenuItem("Offset", NULL));
77 	spacingMenu->ItemAt(0)->SetMarked(true);
78 	fSpacingField = new BMenuField("Button spacing", spacingMenu);
79 
80 	fCountSlider = new BSlider("count", "Button count", NULL, 1, 3,
81 		B_HORIZONTAL);
82 	fCountSlider->SetValue(3);
83 	fCountSlider->SetLimitLabels("1", "3");
84 	fCountSlider->SetHashMarkCount(3);
85 	fCountSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
86 
87 	fLastStringView = new BStringView("last pressed", "");
88 	fLastStringView->SetAlignment(B_ALIGN_CENTER);
89 
90 	BButton* button = new BButton("Show alert", new BMessage(kMsgShowAlert));
91 
92 	BLayoutBuilder::Group<>(this, B_VERTICAL)
93 		.Add(fSizeField)
94 		.Add(fTypeField)
95 		.Add(fSpacingField)
96 		.Add(fCountSlider)
97 		.AddGlue()
98 		.Add(fLastStringView)
99 		.Add(button)
100 		.SetInsets(B_USE_DEFAULT_SPACING);
101 
102 	CenterOnScreen();
103 	SetFlags(Flags() | B_CLOSE_ON_ESCAPE);
104 }
105 
106 
107 void
108 Window::MessageReceived(BMessage* message)
109 {
110 	switch (message->what) {
111 		case kMsgShowAlert:
112 		{
113 			int32 count = fCountSlider->Value();
114 
115 			BAlert* alert = new BAlert("Test title", "Lorem ipsum dolor sit "
116 				"amet, consectetur adipiscing elit. Suspendisse vel iaculis "
117 				"quam. Donec faucibus erat nunc, ac ullamcorper justo sodales.",
118 				"short 1", count > 1 ? "a bit longer 2" : NULL,
119 				count > 2 ? "very very long button 3" : NULL,
120 				_ButtonWidth(), _ButtonSpacing(), _AlertType());
121 			alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
122 			int result = alert->Go();
123 			if (result < 0) {
124 				fLastStringView->SetText("Canceled alert");
125 			} else {
126 				fLastStringView->SetText(BString().SetToFormat(
127 					"Pressed button %d", result + 1).String());
128 			}
129 			break;
130 		}
131 		default:
132 			BWindow::MessageReceived(message);
133 	}
134 }
135 
136 
137 bool
138 Window::QuitRequested()
139 {
140 	be_app->PostMessage(B_QUIT_REQUESTED);
141 	return true;
142 }
143 
144 
145 button_width
146 Window::_ButtonWidth()
147 {
148 	switch (fSizeField->Menu()->FindMarkedIndex()) {
149 		case 0:
150 		default:
151 			return B_WIDTH_AS_USUAL;
152 		case 1:
153 			return B_WIDTH_FROM_WIDEST;
154 		case 2:
155 			return B_WIDTH_FROM_LABEL;
156 	}
157 }
158 
159 
160 button_spacing
161 Window::_ButtonSpacing()
162 {
163 	return (button_spacing)fSpacingField->Menu()->FindMarkedIndex();
164 }
165 
166 
167 alert_type
168 Window::_AlertType()
169 {
170 	return (alert_type)fTypeField->Menu()->FindMarkedIndex();
171 }
172 
173 
174 //	#pragma mark -
175 
176 
177 class Application : public BApplication {
178 public:
179 								Application();
180 
181 	virtual	void				ReadyToRun();
182 };
183 
184 
185 Application::Application()
186 	:
187 	BApplication("application/x-vnd.haiku-AlertTest")
188 {
189 }
190 
191 
192 void
193 Application::ReadyToRun(void)
194 {
195 	BWindow* window = new Window();
196 	window->Show();
197 }
198 
199 
200 //	#pragma mark -
201 
202 
203 int
204 main(int argc, char** argv)
205 {
206 	Application app;
207 
208 	app.Run();
209 	return 0;
210 }
211