xref: /haiku/src/apps/mediaplayer/settings/SettingsWindow.cpp (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
1 /*
2  * Copyright 2008-2011, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Fredrik Modéen <fredrik@modeen.se>
7  *		Stephan Aßmus <superstippi@gmx.de>
8  */
9 
10 
11 #include "SettingsWindow.h"
12 
13 #include <stdio.h>
14 
15 #include <Box.h>
16 #include <Button.h>
17 #include <Catalog.h>
18 #include <CheckBox.h>
19 #include <ControlLook.h>
20 #include <LayoutBuilder.h>
21 #include <Locale.h>
22 #include <OptionPopUp.h>
23 #include <SpaceLayoutItem.h>
24 #include <String.h>
25 #include <StringView.h>
26 #include <RadioButton.h>
27 #include <View.h>
28 
29 
30 #undef B_TRANSLATION_CONTEXT
31 #define B_TRANSLATION_CONTEXT "MediaPlayer-SettingsWindow"
32 
33 
34 enum {
35 	M_SETTINGS_CHANGED = 0x3000,
36 
37 	M_SETTINGS_SAVE,
38 	M_SETTINGS_CANCEL,
39 	M_SETTINGS_REVERT
40 };
41 
42 
43 SettingsWindow::SettingsWindow(BRect frame)
44  	:
45  	BWindow(frame, B_TRANSLATE("MediaPlayer settings"), B_FLOATING_WINDOW_LOOK,
46  		B_FLOATING_ALL_WINDOW_FEEL,
47  		B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE | B_NOT_RESIZABLE
48  			| B_AUTO_UPDATE_SIZE_LIMITS)
49 {
50 	const float kSpacing = be_control_look->DefaultItemSpacing();
51 
52 	BBox* settingsBox = new BBox(B_PLAIN_BORDER, NULL);
53 	BGroupLayout* settingsLayout = new BGroupLayout(B_VERTICAL, kSpacing / 2);
54 	settingsBox->SetLayout(settingsLayout);
55 	BBox* buttonBox = new BBox(B_PLAIN_BORDER, NULL);
56 	BGroupLayout* buttonLayout = new BGroupLayout(B_HORIZONTAL, kSpacing / 2);
57 	buttonBox->SetLayout(buttonLayout);
58 
59 	BStringView* playModeLabel = new BStringView("stringViewPlayMode",
60 		B_TRANSLATE("Play mode"));
61 	BStringView* viewOptionsLabel = new BStringView("stringViewViewOpions",
62 		B_TRANSLATE("View options"));
63 	BStringView* bgMoviesModeLabel = new BStringView("stringViewPlayBackg",
64 		B_TRANSLATE("Volume of background clips"));
65 	BAlignment alignment(B_ALIGN_LEFT, B_ALIGN_VERTICAL_CENTER);
66 	playModeLabel->SetExplicitAlignment(alignment);
67 	playModeLabel->SetFont(be_bold_font);
68 	viewOptionsLabel->SetExplicitAlignment(alignment);
69 	viewOptionsLabel->SetFont(be_bold_font);
70 	bgMoviesModeLabel->SetExplicitAlignment(alignment);
71 	bgMoviesModeLabel->SetFont(be_bold_font);
72 
73 	fAutostartCB = new BCheckBox("chkboxAutostart",
74 		B_TRANSLATE("Automatically start playing"),
75 		new BMessage(M_SETTINGS_CHANGED));
76 
77 	fCloseWindowMoviesCB = new BCheckBox("chkBoxCloseWindowMovies",
78 		B_TRANSLATE("Close window after playing video"),
79 		new BMessage(M_SETTINGS_CHANGED));
80 	fCloseWindowSoundsCB = new BCheckBox("chkBoxCloseWindowSounds",
81 		B_TRANSLATE("Close window after playing audio"),
82 		new BMessage(M_SETTINGS_CHANGED));
83 
84 	fLoopMoviesCB = new BCheckBox("chkBoxLoopMovie",
85 		B_TRANSLATE("Loop video"),
86 		new BMessage(M_SETTINGS_CHANGED));
87 	fLoopSoundsCB = new BCheckBox("chkBoxLoopSounds",
88 		B_TRANSLATE("Loop audio"),
89 		new BMessage(M_SETTINGS_CHANGED));
90 
91 	fUseOverlaysCB = new BCheckBox("chkBoxUseOverlays",
92 		B_TRANSLATE("Use hardware video overlays if available"),
93 		new BMessage(M_SETTINGS_CHANGED));
94 	fScaleBilinearCB = new BCheckBox("chkBoxScaleBilinear",
95 		B_TRANSLATE("Scale movies smoothly (non-overlay mode)"),
96 		new BMessage(M_SETTINGS_CHANGED));
97 
98 	fScaleFullscreenControlsCB = new BCheckBox("chkBoxScaleControls",
99 		B_TRANSLATE("Scale controls in full screen mode"),
100 		new BMessage(M_SETTINGS_CHANGED));
101 
102 	fResumeOP = new BOptionPopUp("resume",
103 		B_TRANSLATE("Resume:"), new BMessage(M_SETTINGS_CHANGED));
104 	fResumeOP->AddOption(
105 		B_TRANSLATE("never"), mpSettings::RESUME_NEVER);
106 	fResumeOP->AddOption(
107 		B_TRANSLATE("ask every time"), mpSettings::RESUME_ASK);
108 	fResumeOP->AddOption(
109 		B_TRANSLATE("always"), mpSettings::RESUME_ALWAYS);
110 
111 	fSubtitleSizeOP = new BOptionPopUp("subtitleSize",
112 		B_TRANSLATE("Subtitle size:"), new BMessage(M_SETTINGS_CHANGED));
113 	fSubtitleSizeOP->AddOption(
114 		B_TRANSLATE("Small"), mpSettings::SUBTITLE_SIZE_SMALL);
115 	fSubtitleSizeOP->AddOption(
116 		B_TRANSLATE("Medium"), mpSettings::SUBTITLE_SIZE_MEDIUM);
117 	fSubtitleSizeOP->AddOption(
118 		B_TRANSLATE("Large"), mpSettings::SUBTITLE_SIZE_LARGE);
119 
120 	fSubtitlePlacementOP = new BOptionPopUp("subtitlePlacement",
121 		B_TRANSLATE("Subtitle placement:"), new BMessage(M_SETTINGS_CHANGED));
122 	fSubtitlePlacementOP->AddOption(B_TRANSLATE("Bottom of video"),
123 		mpSettings::SUBTITLE_PLACEMENT_BOTTOM_OF_VIDEO);
124 	fSubtitlePlacementOP->AddOption(B_TRANSLATE("Bottom of screen"),
125 		mpSettings::SUBTITLE_PLACEMENT_BOTTOM_OF_SCREEN);
126 
127 	fFullVolumeBGMoviesRB = new BRadioButton("rdbtnfullvolume",
128 		B_TRANSLATE("Full volume"), new BMessage(M_SETTINGS_CHANGED));
129 
130 	fHalfVolumeBGMoviesRB = new BRadioButton("rdbtnhalfvolume",
131 		B_TRANSLATE("Low volume"), new BMessage(M_SETTINGS_CHANGED));
132 
133 	fMutedVolumeBGMoviesRB = new BRadioButton("rdbtnfullvolume",
134 		B_TRANSLATE("Muted"), new BMessage(M_SETTINGS_CHANGED));
135 
136 	fRevertB = new BButton("revert", B_TRANSLATE("Revert"),
137 		new BMessage(M_SETTINGS_REVERT));
138 
139 	BButton* cancelButton = new BButton("cancel", B_TRANSLATE("Cancel"),
140 		new BMessage(M_SETTINGS_CANCEL));
141 
142 	BButton* okButton = new BButton("ok", B_TRANSLATE("OK"),
143 		new BMessage(M_SETTINGS_SAVE));
144 	okButton->MakeDefault(true);
145 
146 	// Build the layout
147 	BGroupLayout* volumeGroup;
148 	BGroupLayout* startGroup;
149 	BGroupLayout* playGroup;
150 	BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
151 		.AddGroup(settingsLayout)
152 			.SetInsets(B_USE_WINDOW_SPACING, B_USE_WINDOW_SPACING,
153 				B_USE_WINDOW_SPACING, B_USE_DEFAULT_SPACING)
154 			.Add(playModeLabel)
155 			.AddGroup(B_HORIZONTAL, 0)
156 				.GetLayout(&playGroup)
157 				.AddStrut(10)
158 				.AddGroup(B_VERTICAL, 0)
159 					.GetLayout(&startGroup)
160 					.Add(fAutostartCB)
161 					.AddGrid(kSpacing, 0)
162 						.Add(BSpaceLayoutItem::CreateHorizontalStrut(kSpacing), 0, 0)
163 						.Add(fCloseWindowMoviesCB, 1, 0)
164 						.Add(BSpaceLayoutItem::CreateHorizontalStrut(kSpacing), 0, 1)
165 						.Add(fCloseWindowSoundsCB, 1, 1)
166 					.End()
167 					.Add(fLoopMoviesCB)
168 					.Add(fLoopSoundsCB)
169 					.Add(fResumeOP)
170 				.End()
171 			.End()
172 			.AddStrut(kSpacing)
173 
174 			.Add(viewOptionsLabel)
175 			.AddGroup(B_HORIZONTAL, 0)
176 				.AddStrut(10)
177 				.AddGroup(B_VERTICAL, 0)
178 					.Add(fUseOverlaysCB)
179 					.Add(fScaleBilinearCB)
180 					.Add(fScaleFullscreenControlsCB)
181 					.Add(fSubtitleSizeOP)
182 					.Add(fSubtitlePlacementOP)
183 				.End()
184 			.End()
185 			.AddStrut(kSpacing)
186 
187 			.Add(bgMoviesModeLabel)
188 			.AddGroup(B_HORIZONTAL, 0)
189 				.AddStrut(10)
190 				.AddGroup(B_VERTICAL, 0)
191 					.GetLayout(&volumeGroup)
192 					.Add(fFullVolumeBGMoviesRB)
193 					.Add(fHalfVolumeBGMoviesRB)
194 					.Add(fMutedVolumeBGMoviesRB)
195 				.End()
196 			.End()
197 			.AddStrut(kSpacing)
198 		.End()
199 		.AddGroup(buttonLayout)
200 			.SetInsets(B_USE_WINDOW_SPACING, B_USE_DEFAULT_SPACING,
201 				B_USE_WINDOW_SPACING, B_USE_WINDOW_SPACING)
202 			.Add(fRevertB)
203 			.AddGlue()
204 			.Add(cancelButton)
205 			.Add(okButton);
206 
207 	startGroup->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
208 	playGroup->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
209 	volumeGroup->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
210 }
211 
212 
213 SettingsWindow::~SettingsWindow()
214 {
215 }
216 
217 
218 void
219 SettingsWindow::Show()
220 {
221 	// The Settings that we want to be able to revert to is the state at which
222 	// the SettingsWindow was shown. So the current settings are stored in
223 	// fLastSettings.
224 	Settings::Default()->Get(fLastSettings);
225 	fSettings = fLastSettings;
226 	AdoptSettings();
227 
228 	BWindow::Show();
229 }
230 
231 
232 bool
233 SettingsWindow::QuitRequested()
234 {
235 	Hide();
236 	return false;
237 }
238 
239 
240 void
241 SettingsWindow::MessageReceived(BMessage* message)
242 {
243 	switch (message->what) {
244 		case M_SETTINGS_CHANGED:
245 			ApplySettings();
246 			break;
247 
248 		case B_KEY_DOWN:
249 			int32 index;
250 			if (message->FindInt32("key", &index) == B_OK && index == 1)
251 				PostMessage(B_QUIT_REQUESTED);
252 			break;
253 
254 		case M_SETTINGS_REVERT:
255 			Revert();
256 			break;
257 
258 		case M_SETTINGS_CANCEL:
259 			Revert();
260 			// fall through
261 		case M_SETTINGS_SAVE:
262 			PostMessage(B_QUIT_REQUESTED);
263 			break;
264 
265 		default:
266 			BWindow::MessageReceived(message);
267 			break;
268 	}
269 }
270 
271 
272 void
273 SettingsWindow::AdoptSettings()
274 {
275 	fAutostartCB->SetValue(fSettings.autostart);
276 	fCloseWindowMoviesCB->SetValue(fSettings.closeWhenDonePlayingMovie);
277 	fCloseWindowSoundsCB->SetValue(fSettings.closeWhenDonePlayingSound);
278 	fLoopMoviesCB->SetValue(fSettings.loopMovie);
279 	fLoopSoundsCB->SetValue(fSettings.loopSound);
280 
281 	fUseOverlaysCB->SetValue(fSettings.useOverlays);
282 	fScaleBilinearCB->SetValue(fSettings.scaleBilinear);
283 	fScaleFullscreenControlsCB->SetValue(fSettings.scaleFullscreenControls);
284 
285 	fResumeOP->SetValue(fSettings.resume);
286 	fSubtitleSizeOP->SetValue(fSettings.subtitleSize);
287 	fSubtitlePlacementOP->SetValue(fSettings.subtitlePlacement);
288 
289 	fFullVolumeBGMoviesRB->SetValue(fSettings.backgroundMovieVolumeMode
290 		== mpSettings::BG_MOVIES_FULL_VOLUME);
291 	fHalfVolumeBGMoviesRB->SetValue(fSettings.backgroundMovieVolumeMode
292 		== mpSettings::BG_MOVIES_HALF_VLUME);
293 	fMutedVolumeBGMoviesRB->SetValue(fSettings.backgroundMovieVolumeMode
294 		== mpSettings::BG_MOVIES_MUTED);
295 
296 	fRevertB->SetEnabled(IsRevertable());
297 }
298 
299 
300 void
301 SettingsWindow::ApplySettings()
302 {
303 	fSettings.autostart = fAutostartCB->Value() == B_CONTROL_ON;
304 	fSettings.closeWhenDonePlayingMovie
305 		= fCloseWindowMoviesCB->Value() == B_CONTROL_ON;
306 	fSettings.closeWhenDonePlayingSound
307 		= fCloseWindowSoundsCB->Value() == B_CONTROL_ON;
308 	fSettings.loopMovie = fLoopMoviesCB->Value() == B_CONTROL_ON;
309 	fSettings.loopSound = fLoopSoundsCB->Value() == B_CONTROL_ON;
310 
311 	fSettings.useOverlays = fUseOverlaysCB->Value() == B_CONTROL_ON;
312 	fSettings.scaleBilinear = fScaleBilinearCB->Value() == B_CONTROL_ON;
313 	fSettings.scaleFullscreenControls
314 		= fScaleFullscreenControlsCB->Value() == B_CONTROL_ON;
315 
316 	fSettings.resume = fResumeOP->Value();
317 	fSettings.subtitleSize = fSubtitleSizeOP->Value();
318 	fSettings.subtitlePlacement = fSubtitlePlacementOP->Value();
319 
320 	if (fFullVolumeBGMoviesRB->Value() == B_CONTROL_ON) {
321 		fSettings.backgroundMovieVolumeMode
322 			= mpSettings::BG_MOVIES_FULL_VOLUME;
323 	} else if (fHalfVolumeBGMoviesRB->Value() == B_CONTROL_ON) {
324 		fSettings.backgroundMovieVolumeMode
325 			= mpSettings::BG_MOVIES_HALF_VLUME;
326 	} else if (fMutedVolumeBGMoviesRB->Value() == B_CONTROL_ON) {
327 		fSettings.backgroundMovieVolumeMode
328 			= mpSettings::BG_MOVIES_MUTED;
329 	}
330 
331 	Settings::Default()->Update(fSettings);
332 
333 	fRevertB->SetEnabled(IsRevertable());
334 }
335 
336 
337 void
338 SettingsWindow::Revert()
339 {
340 	fSettings = fLastSettings;
341 	AdoptSettings();
342 	Settings::Default()->Update(fSettings);
343 }
344 
345 
346 bool
347 SettingsWindow::IsRevertable() const
348 {
349 	return fSettings != fLastSettings;
350 }
351 
352