xref: /haiku/src/tests/kits/game/file_game_sound_test/FileSoundWindow.cpp (revision d5cd5d63ff0ad395989db6cf4841a64d5b545d1d)
1 /**App window for the FileSoundWindow test
2 	@author Tim de Jong
3 	@date 21/09/2002
4 	@version 1.0
5  */
6 
7 #include "FileSoundWindow.h"
8 #include <Application.h>
9 #include <Box.h>
10 #include <TextControl.h>
11 #include <Button.h>
12 #include <CheckBox.h>
13 #include <Alert.h>
14 #include <Path.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 
18 FileSoundWindow::FileSoundWindow(BRect windowBounds)
19 					:BWindow(windowBounds,"BFileGameSound test",B_TITLED_WINDOW,B_NOT_ZOOMABLE|B_NOT_RESIZABLE)
20 {
21 	//init private vars
22 	loop = false;
23 	preload = false;
24 	playing = false;
25 	paused = false;
26 	rampTime = 250000; // 250 ms default
27 	fileSound = 0;
28 	//make openPanel and let it send its messages to this window
29 	openPanel = new BFilePanel();
30 	openPanel -> SetTarget(this);
31 	//get appBounds
32 	BRect appBounds = Bounds();
33 	//make a cosmetic box
34 	BBox *box = new BBox(appBounds);
35 	//textcontrol to display the chosen file
36 	BRect textBounds(appBounds.left + 10, appBounds.top + 10, appBounds.right - 70, appBounds.top + 20);
37 	textControl = new BTextControl(textBounds,"filechosen","Play File:","No file chosen yet.", NULL);
38 	textControl -> SetEnabled(false);
39 
40 	float x1 = textControl -> Bounds().left;
41 	float x2 = textControl -> Bounds().right;
42 	float dividerX = 0.20 * (x2 - x1);
43 	textControl -> SetDivider(dividerX);
44 
45 	box -> AddChild(textControl);
46 	//button to choose file
47 	BRect browseBounds(textBounds.right + 5,textBounds.top, appBounds.right - 5, textBounds.bottom);
48 	BMessage *browseMessage = new BMessage(BROWSE_MSG);
49 	BButton *browseButton = new BButton(browseBounds,"browseButton","Browse" B_UTF8_ELLIPSIS,browseMessage);
50 	box -> AddChild(browseButton);
51 	//make play button
52 	BRect playBounds(textBounds.left,textBounds.bottom + 15, textBounds.left + 50, textBounds.bottom + 25);
53 	BMessage *playMessage = new BMessage(PLAY_MSG);
54 	playButton = new BButton(playBounds,"playbutton","Play", playMessage);
55 	box -> AddChild(playButton);
56 	//make pause button
57 	BRect pauseBounds(playBounds.right + 10,playBounds.top, playBounds.right + 60, playBounds.bottom);
58 	BMessage *pauseMessage = new BMessage(PAUSE_MSG);
59 	pauseButton = new BButton(pauseBounds,"pausebutton","Pause",pauseMessage);
60 	box -> AddChild(pauseButton);
61 	//make textcontrol to enter delay for pausing/resuming
62 	BRect delayBounds(pauseBounds.right + 10, pauseBounds.top,pauseBounds.right + 150, pauseBounds.bottom);
63 	delayControl = new BTextControl(delayBounds,"delay","Ramp time (ms)","250", new BMessage(DELAY_MSG));
64 	delayControl -> SetDivider(90);
65 	delayControl -> SetModificationMessage(new BMessage(DELAY_MSG));
66 	box -> AddChild(delayControl);
67 	//make loop checkbox
68 	BRect loopBounds(playBounds.left,playBounds.bottom + 20, playBounds.left + 150, playBounds.bottom + 30);
69 	BMessage *loopMessage = new BMessage(LOOP_MSG);
70 	loopCheck = new BCheckBox(loopBounds,"loopcheckbox","Loop the sound file",loopMessage);
71 	box -> AddChild(loopCheck);
72 	//make preload checkbox
73 	BRect preloadBounds(loopBounds.right + 10,loopBounds.top, loopBounds.right + 150, loopBounds.bottom);
74 	BMessage *preloadMessage = new BMessage(PRELOAD_MSG);
75 	preloadCheck = new BCheckBox(preloadBounds,"loopcheckbox","Preload the sound file",preloadMessage);
76 	box -> AddChild(preloadCheck);
77 	//add background box to window
78 	AddChild(box);
79 }
80 
81 FileSoundWindow::~FileSoundWindow()
82 {
83 }
84 
85 void FileSoundWindow::MessageReceived(BMessage *message)
86 {
87 	switch (message -> what)
88 	{
89 		case BROWSE_MSG:
90 			openPanel -> Show();
91 		break;
92 
93 		case B_REFS_RECEIVED:
94 		{
95 			message->FindRef("refs", 0, &fileref);
96 			BPath path(new BEntry(&fileref));
97 			textControl -> SetText(path.Path());
98 		}
99 		break;
100 
101 		case PLAY_MSG:
102 		{
103 			if (!playing)
104 			{
105 				fileSound = new BFileGameSound(&fileref,loop);
106 				status_t error = fileSound -> InitCheck();
107 				if (error != B_OK)
108 				{
109 					delete fileSound;
110 					fileSound = 0;
111 					if (error == B_NO_MEMORY)
112 					{
113 						BAlert *alert = new BAlert("alert","Not enough memory.","Ok");
114 						alert -> Go();
115 					}
116 					else if (error == B_ERROR)
117 					{
118 						BAlert *alert = new BAlert("alert","Unable to create a sound player.","Ok");
119 						alert -> Go();
120 					}
121 					else
122 					{
123 						BAlert *alert = new BAlert("alert","Other kind of error!","Ok");
124 						alert -> Go();
125 					}
126 					break;
127 				}
128 				paused = false;
129 				pauseButton -> SetLabel("Pause");
130 				//preload sound file?
131 				if (preload)
132 				{
133 					status_t preloadError = fileSound -> Preload();
134 					if (preloadError != B_OK)
135 					{
136 						BAlert *alert = new BAlert("alert","Port errors. Unable to communicate with the streaming sound port.","Ok");
137 						alert -> Go();
138 					}
139 				}
140 				//play it!
141 				status_t playerror = fileSound -> StartPlaying();
142 				if (playerror != B_OK)
143 				{
144 					if (playerror == EALREADY)
145 					{
146 						BAlert *alert = new BAlert("alert","Sound is already playing","Ok");
147 						alert -> Go();
148 					}
149 					else
150 					{
151 						BAlert *alert = new BAlert("alert","Error playing sound","Ok");
152 						alert -> Go();
153 					}
154 				}
155 				else
156 				{
157 					playButton -> SetLabel("Stop");
158 					playing = true;
159 				}
160 			}
161 			else
162 			{
163 				//stop it!
164 				status_t stoperror = fileSound -> StopPlaying();
165 				if (stoperror != B_OK)
166 				{
167 					if (stoperror == EALREADY)
168 					{
169 						BAlert *alert = new BAlert("alert","Sound is already stopped","Ok");
170 						alert -> Go();
171 					}
172 					else
173 					{
174 						BAlert *alert = new BAlert("alert","Error stopping sound","Ok");
175 						alert -> Go();
176 					}
177 				}
178 				else
179 				{
180 					playButton -> SetLabel("Play");
181 					playing = false;
182 				}
183 				delete fileSound;
184 				fileSound = 0;
185 			}
186 		}
187 		break;
188 
189 		case PAUSE_MSG:
190 		{
191 			if (!fileSound)
192 				break;
193 
194 			int32 pausedValue = fileSound -> IsPaused();
195 			if (pausedValue != BFileGameSound::B_PAUSE_IN_PROGRESS)
196 			{
197 				status_t error;
198 				char *label;
199 				if (paused)
200 				{
201 					paused = false;
202 					label = "Pause";
203 					error = fileSound ->SetPaused(paused, rampTime);
204 				}
205 				else
206 				{
207 					paused = true;
208 					label = "Resume";
209 					error = fileSound ->SetPaused(paused, rampTime);
210 				}
211 
212 				if (error != B_OK)
213 				{
214 					if (error == EALREADY)
215 					{
216 						BAlert *alert = new BAlert("alert","Already in requested state","Ok");
217 						alert -> Go();
218 					}
219 					else
220 					{
221 						BAlert *alert = new BAlert("alert","Error!","Ok");
222 						alert -> Go();
223 					}
224 				}
225 				else
226 				{
227 					pauseButton -> SetLabel(label);
228 				}
229 			}
230 		}
231 		break;
232 
233 		case LOOP_MSG:
234 		{
235 			int32 checkValue = loopCheck -> Value();
236 			if (checkValue == B_CONTROL_ON)
237 				loop = true;
238 			else loop = false;
239 		}
240 		break;
241 
242 		case PRELOAD_MSG:
243 		{
244 			int32 checkValue = preloadCheck -> Value();
245 			if (checkValue == B_CONTROL_ON)
246 				preload = true;
247 			else preload = false;
248 		}
249 		break;
250 
251 		case DELAY_MSG:
252 		{
253 			//set delay time for resuming/pausing
254 			rampTime = atol(delayControl -> Text()) * 1000;
255 		}
256 		break;
257 	}
258 
259 }
260 
261 bool FileSoundWindow::QuitRequested()
262 {
263 
264 	delete openPanel;
265 	if (fileSound != 0 && fileSound -> IsPlaying())
266 		fileSound -> StopPlaying();
267 	delete fileSound;
268 	be_app->PostMessage(B_QUIT_REQUESTED);
269 	return(true);
270 }
271