xref: /haiku/src/apps/mediaplayer/settings/SettingsWindow.cpp (revision 746cac055adc6ac3308c7bc2d29040fb95689cc9)
1 /*
2  * Copyright 2008, Haiku. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Fredrik Modéen <fredrik@modeen.se>
7  */
8 
9 #include "SettingsWindow.h"
10 
11 #include <stdio.h>
12 
13 #include <Box.h>
14 #include <CheckBox.h>
15 #include <StringView.h>
16 #include <RadioButton.h>
17 #include <View.h>
18 #include <Button.h>
19 #include <String.h>
20 
21 #ifdef __HAIKU__
22 #	include <GridLayoutBuilder.h>
23 #	include <GroupLayout.h>
24 #	include <GroupLayoutBuilder.h>
25 #	include <SpaceLayoutItem.h>
26 #endif
27 
28 enum {
29 	M_AUTOSTART = 0x3000,
30 	M_CLOSE_WINDOW_MOVIE,
31 	M_CLOSE_WINDOW_SOUNDS,
32 	M_LOOP_MOVIE,
33 	M_LOOP_SOUND,
34 	M_USE_OVERLAYS,
35 	M_SCALE_BILINEAR,
36 	M_START_FULL_VOLUME,
37 	M_START_HALF_VOLUME,
38 	M_START_MUTE_VOLUME,
39 
40 	M_SETTINGS_SAVE,
41 	M_SETTINGS_CANCEL,
42 	M_SETTINGS_REVERT
43 };
44 
45 #define SPACE 10
46 #define SPACEING 7
47 #define BUTTONHEIGHT 20
48 
49 SettingsWindow::SettingsWindow(BRect frame)
50  	: BWindow(frame, "MediaPlayer Settings", B_FLOATING_WINDOW_LOOK,
51  		B_FLOATING_APP_WINDOW_FEEL,
52  		B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE | B_NOT_RESIZABLE
53 #ifdef __HAIKU__
54  			| B_AUTO_UPDATE_SIZE_LIMITS)
55 #else
56  			)
57 #endif
58 {
59 #ifdef __HAIKU__
60 
61 	BBox* settingsBox = new BBox(B_PLAIN_BORDER, NULL);
62 	BGroupLayout* settingsLayout = new BGroupLayout(B_VERTICAL, 5);
63 	settingsBox->SetLayout(settingsLayout);
64 	BBox* buttonBox = new BBox(B_PLAIN_BORDER, NULL);
65 	BGroupLayout* buttonLayout = new BGroupLayout(B_HORIZONTAL, 5);
66 	buttonBox->SetLayout(buttonLayout);
67 
68 	BStringView* playModeLabel = new BStringView("stringViewPlayMode",
69 		"Play mode");
70 	BStringView* viewOptionsLabel = new BStringView("stringViewViewOpions",
71 		"View options");
72 	BStringView* bgMoviesModeLabel = new BStringView("stringViewPlayBackg",
73 		"Play background clips at");
74 	BAlignment alignment(B_ALIGN_LEFT, B_ALIGN_VERTICAL_CENTER);
75 	playModeLabel->SetExplicitAlignment(alignment);
76 	playModeLabel->SetFont(be_bold_font);
77 	viewOptionsLabel->SetExplicitAlignment(alignment);
78 	viewOptionsLabel->SetFont(be_bold_font);
79 	bgMoviesModeLabel->SetExplicitAlignment(alignment);
80 	bgMoviesModeLabel->SetFont(be_bold_font);
81 
82 	fAutostartCB = new BCheckBox("chkboxAutostart",
83 		"Automatically start playing", new BMessage(M_AUTOSTART));
84 
85 	fCloseWindowMoviesCB = new BCheckBox("chkBoxCloseWindowMovies",
86 		"Close window when done playing movies",
87 		new BMessage(M_CLOSE_WINDOW_MOVIE));
88 	fCloseWindowSoundsCB = new BCheckBox("chkBoxCloseWindowSounds",
89 		"Close window when done playing sounds",
90 		new BMessage(M_CLOSE_WINDOW_SOUNDS));
91 
92 	fLoopMoviesCB = new BCheckBox("chkBoxLoopMovie",
93 		"Loop movies by default", new BMessage(M_LOOP_MOVIE));
94 	fLoopSoundsCB = new BCheckBox("chkBoxLoopSounds",
95 		"Loop sounds by default", new BMessage(M_LOOP_SOUND));
96 
97 	fUseOverlaysCB = new BCheckBox("chkBoxUseOverlays",
98 		"Use hardware video overlays if available",
99 		new BMessage(M_USE_OVERLAYS));
100 	fScaleBilinearCB = new BCheckBox("chkBoxScaleBilinear",
101 		"Scale movies smoothly (non-overlay mode)",
102 		new BMessage(M_SCALE_BILINEAR));
103 
104 	fFullVolumeBGMoviesRB = new BRadioButton("rdbtnfullvolume",
105 		"Full volume", new BMessage(M_START_FULL_VOLUME));
106 
107 	fHalfVolumeBGMoviesRB = new BRadioButton("rdbtnhalfvolume",
108 		"Low volume", new BMessage(M_START_HALF_VOLUME));
109 
110 	fMutedVolumeBGMoviesRB = new BRadioButton("rdbtnfullvolume",
111 		"Muted", new BMessage(M_START_MUTE_VOLUME));
112 
113 	fRevertB = new BButton("revert", "Revert",
114 		new BMessage(M_SETTINGS_REVERT));
115 
116 	BButton* cancelButton = new BButton("cancel", "Cancel",
117 		new BMessage(M_SETTINGS_CANCEL));
118 
119 	BButton* okButton = new BButton("ok", "Ok",
120 		new BMessage(M_SETTINGS_SAVE));
121 	okButton->MakeDefault(true);
122 
123 
124 	// Build the layout
125 	SetLayout(new BGroupLayout(B_HORIZONTAL));
126 
127 	AddChild(BGroupLayoutBuilder(B_VERTICAL, 0)
128 		.Add(BGroupLayoutBuilder(settingsLayout)
129 			.Add(playModeLabel)
130 			.Add(BGroupLayoutBuilder(B_HORIZONTAL, 0)
131 				.Add(BSpaceLayoutItem::CreateHorizontalStrut(10))
132 				.Add(BGroupLayoutBuilder(B_VERTICAL, 0)
133 					.Add(fAutostartCB)
134 					.Add(BGridLayoutBuilder(5, 0)
135 						.Add(BSpaceLayoutItem::CreateHorizontalStrut(10), 0, 0)
136 						.Add(fCloseWindowMoviesCB, 1, 0)
137 						.Add(BSpaceLayoutItem::CreateHorizontalStrut(10), 0, 1)
138 						.Add(fCloseWindowSoundsCB, 1, 1)
139 					)
140 					.Add(fLoopMoviesCB)
141 					.Add(fLoopSoundsCB)
142 				)
143 			)
144 			.Add(BSpaceLayoutItem::CreateVerticalStrut(5))
145 
146 			.Add(viewOptionsLabel)
147 			.Add(BGroupLayoutBuilder(B_HORIZONTAL, 0)
148 				.Add(BSpaceLayoutItem::CreateHorizontalStrut(10))
149 				.Add(BGroupLayoutBuilder(B_VERTICAL, 0)
150 					.Add(fUseOverlaysCB)
151 					.Add(fScaleBilinearCB)
152 				)
153 			)
154 			.Add(BSpaceLayoutItem::CreateVerticalStrut(5))
155 
156 			.Add(bgMoviesModeLabel)
157 			.Add(BGroupLayoutBuilder(B_HORIZONTAL, 0)
158 				.Add(BSpaceLayoutItem::CreateHorizontalStrut(10))
159 				.Add(BGroupLayoutBuilder(B_VERTICAL, 0)
160 					.Add(fFullVolumeBGMoviesRB)
161 					.Add(fHalfVolumeBGMoviesRB)
162 					.Add(fMutedVolumeBGMoviesRB)
163 				)
164 			)
165 			.Add(BSpaceLayoutItem::CreateVerticalStrut(5))
166 
167 			.SetInsets(5, 5, 15, 5)
168 		)
169 		.Add(BGroupLayoutBuilder(buttonLayout)
170 			.Add(fRevertB)
171 			.AddGlue()
172 			.Add(cancelButton)
173 			.Add(okButton)
174 			.SetInsets(5, 5, 5, 5)
175 		)
176 	);
177 
178 #else
179 
180 	frame = Bounds();
181 	BView* view = new BView(frame,"SettingsView",B_FOLLOW_ALL_SIDES,B_WILL_DRAW);
182 	view->SetViewColor(216, 216, 216);
183 
184 	BRect btnRect(140.00, frame.bottom - (SPACE + BUTTONHEIGHT), 205.00,
185 		frame.bottom-SPACE);
186 	BButton* btn = new BButton(btnRect, "btnCancel", "Cancel",
187 		new BMessage(M_SETTINGS_CANCEL));
188 	view->AddChild(btn);
189 
190 	btnRect.OffsetBy(btnRect.Width() + SPACE, 0);
191 	btn = new BButton(btnRect, "btnOK", "OK", new BMessage(M_SETTINGS_SAVE));
192 	view->AddChild(btn);
193 
194 	BRect rectBox(frame.left + SPACE, frame.top + SPACE, frame.right - SPACE,
195 		btnRect.top- SPACE);
196 	BBox* bbox = new BBox(rectBox, "box1", B_FOLLOW_ALL_SIDES,B_WILL_DRAW | B_NAVIGABLE,
197 		B_FANCY_BORDER);
198 	bbox->SetLabel("MediaPlayer Settings");
199 
200 	BFont font;
201 	font_height fh1;
202 	font.GetHeight(&fh1);
203 
204 	BString str("Play Mode:");
205 	BRect rect(rectBox.left, rectBox.top + SPACE, rectBox.right - (12*2),
206 		rectBox.top + fh1.leading + fh1.ascent + 10);
207 	bbox->AddChild(new BStringView(rect, "stringViewPlayMode", str.String()));
208 
209 	rect.OffsetBy(0, rect.Height());
210 	bbox->AddChild(fAutostartCB = new BCheckBox(rect, "chkboxAutostart",
211 		"Automatically start playing", new BMessage(M_AUTOSTART)));
212 
213 	rect.OffsetBy(SPACE, rect.Height() + SPACEING);
214 	bbox->AddChild(fCloseWindowMoviesCB = new BCheckBox(rect, "chkBoxCloseWindowMovies",
215 		"Close window when done playing movies", new BMessage(M_CLOSE_WINDOW_MOVIE)));
216 
217 	rect.OffsetBy(0, rect.Height() + SPACEING);
218 	bbox->AddChild(fCloseWindowSoundsCB = new BCheckBox(rect, "chkBoxCloseWindowSounds",
219 		"Close window when done playing sounds", new BMessage(M_CLOSE_WINDOW_SOUNDS)));
220 
221 	rect.OffsetBy(-SPACE, rect.Height() + SPACEING);
222 	bbox->AddChild(fLoopMoviesCB = new BCheckBox(rect, "chkBoxLoopMovie", "Loop movies by default",
223 		new BMessage(M_LOOP_MOVIE)));
224 
225 	rect.OffsetBy(0, rect.Height() + SPACEING);
226 	bbox->AddChild(fLoopSoundsCB = new BCheckBox(rect, "chkBoxLoopSounds", "Loop sounds by default",
227 		new BMessage(M_LOOP_SOUND)));
228 
229 	rect.OffsetBy(0, rect.Height() + SPACE + SPACEING);
230 	bbox->AddChild(new BStringView(rect, "stringViewPlayBackg",
231 		"Play backgrounds clips at:"));
232 
233 	rect.OffsetBy(SPACE, rect.Height() + SPACEING);
234 	fFullVolumeBGMoviesRB = new BRadioButton(rect, "rdbtnfullvolume",
235 		"Full Volume", new BMessage(M_START_FULL_VOLUME));
236 	bbox->AddChild(fFullVolumeBGMoviesRB);
237 
238 	rect.OffsetBy(0, rect.Height() + SPACEING);
239 	fHalfVolumeBGMoviesRB = new BRadioButton(rect, "rdbtnhalfvolume",
240 		"Low Volume", new BMessage(M_START_HALF_VOLUME));
241 	bbox->AddChild(fHalfVolumeBGMoviesRB);
242 
243 	rect.OffsetBy(0, rect.Height() + SPACEING);
244 	fMutedVolumeBGMoviesRB = new BRadioButton(rect, "rdbtnfullvolume", "Muted",
245 		new BMessage(M_START_MUTE_VOLUME));
246 	bbox->AddChild(fMutedVolumeBGMoviesRB);
247 
248 	view->AddChild(bbox);
249 	AddChild(view);
250 #endif
251 
252 	// disable currently unsupported features
253 	fLoopMoviesCB->SetEnabled(false);
254 	fLoopSoundsCB->SetEnabled(false);
255 }
256 
257 
258 SettingsWindow::~SettingsWindow()
259 {
260 }
261 
262 
263 void
264 SettingsWindow::Show()
265 {
266 	Settings::Default()->LoadSettings(fLastSettings);
267 	fSettings = fLastSettings;
268 	AdoptSettings();
269 
270 	BWindow::Show();
271 }
272 
273 
274 bool
275 SettingsWindow::QuitRequested()
276 {
277 	Hide();
278 	return false;
279 }
280 
281 
282 void
283 SettingsWindow::MessageReceived(BMessage* message)
284 {
285 	switch (message->what) {
286 		case M_AUTOSTART:
287 		case M_CLOSE_WINDOW_MOVIE:
288 		case M_CLOSE_WINDOW_SOUNDS:
289 		case M_LOOP_MOVIE:
290 		case M_LOOP_SOUND:
291 		case M_USE_OVERLAYS:
292 		case M_SCALE_BILINEAR:
293 		case M_START_FULL_VOLUME:
294 		case M_START_HALF_VOLUME:
295 		case M_START_MUTE_VOLUME:
296 			ApplySettings();
297 			break;
298 
299 		case B_KEY_DOWN:
300 			int32 index;
301 			if (message->FindInt32("key", &index) == B_OK && index == 1)
302 				PostMessage(B_QUIT_REQUESTED);
303 			break;
304 
305 		case M_SETTINGS_REVERT:
306 			Revert();
307 			break;
308 
309 		case M_SETTINGS_CANCEL:
310 			Revert();
311 			// fall through
312 		case M_SETTINGS_SAVE:
313 			PostMessage(B_QUIT_REQUESTED);
314 			break;
315 
316 		default:
317 			BWindow::MessageReceived(message);
318 			break;
319 	}
320 }
321 
322 
323 void
324 SettingsWindow::AdoptSettings()
325 {
326 	fAutostartCB->SetValue(fSettings.autostart);
327 	fCloseWindowMoviesCB->SetValue(fSettings.closeWhenDonePlayingMovie);
328 	fCloseWindowSoundsCB->SetValue(fSettings.closeWhenDonePlayingSound);
329 	fLoopMoviesCB->SetValue(fSettings.loopMovie);
330 	fLoopSoundsCB->SetValue(fSettings.loopSound);
331 
332 	fUseOverlaysCB->SetValue(fSettings.useOverlays);
333 	fScaleBilinearCB->SetValue(fSettings.scaleBilinear);
334 
335 	fFullVolumeBGMoviesRB->SetValue(fSettings.backgroundMovieVolumeMode
336 		== mpSettings::BG_MOVIES_FULL_VOLUME);
337 	fHalfVolumeBGMoviesRB->SetValue(fSettings.backgroundMovieVolumeMode
338 		== mpSettings::BG_MOVIES_HALF_VLUME);
339 	fMutedVolumeBGMoviesRB->SetValue(fSettings.backgroundMovieVolumeMode
340 		== mpSettings::BG_MOVIES_MUTED);
341 
342 	fRevertB->SetEnabled(IsRevertable());
343 }
344 
345 
346 void
347 SettingsWindow::ApplySettings()
348 {
349 	fSettings.autostart = fAutostartCB->Value() == B_CONTROL_ON;
350 	fSettings.closeWhenDonePlayingMovie
351 		= fCloseWindowMoviesCB->Value() == B_CONTROL_ON;
352 	fSettings.closeWhenDonePlayingSound
353 		= fCloseWindowSoundsCB->Value() == B_CONTROL_ON;
354 	fSettings.loopMovie = fLoopMoviesCB->Value() == B_CONTROL_ON;
355 	fSettings.loopSound = fLoopSoundsCB->Value() == B_CONTROL_ON;
356 
357 	fSettings.useOverlays = fUseOverlaysCB->Value() == B_CONTROL_ON;
358 	fSettings.scaleBilinear = fScaleBilinearCB->Value() == B_CONTROL_ON;
359 
360 	if (fFullVolumeBGMoviesRB->Value() == B_CONTROL_ON) {
361 		fSettings.backgroundMovieVolumeMode
362 			= mpSettings::BG_MOVIES_FULL_VOLUME;
363 	} else if (fHalfVolumeBGMoviesRB->Value() == B_CONTROL_ON) {
364 		fSettings.backgroundMovieVolumeMode
365 			= mpSettings::BG_MOVIES_HALF_VLUME;
366 	} else if (fMutedVolumeBGMoviesRB->Value() == B_CONTROL_ON) {
367 		fSettings.backgroundMovieVolumeMode
368 			= mpSettings::BG_MOVIES_MUTED;
369 	}
370 
371 	Settings::Default()->SaveSettings(fSettings);
372 
373 	fRevertB->SetEnabled(IsRevertable());
374 }
375 
376 
377 void
378 SettingsWindow::Revert()
379 {
380 	fSettings = fLastSettings;
381 	AdoptSettings();
382 	Settings::Default()->SaveSettings(fSettings);
383 }
384 
385 
386 bool
387 SettingsWindow::IsRevertable() const
388 {
389 	return fSettings != fLastSettings;
390 }
391 
392