xref: /haiku/src/apps/mediaplayer/settings/SettingsWindow.cpp (revision 778611c7e6a61b8ba072212756ce53eda826360a)
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_APP_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 	fSubtitleSizeOP = new BOptionPopUp("subtitleSize",
103 		B_TRANSLATE("Subtitle size:"), new BMessage(M_SETTINGS_CHANGED));
104 	fSubtitleSizeOP->AddOption(
105 		B_TRANSLATE("Small"), mpSettings::SUBTITLE_SIZE_SMALL);
106 	fSubtitleSizeOP->AddOption(
107 		B_TRANSLATE("Medium"), mpSettings::SUBTITLE_SIZE_MEDIUM);
108 	fSubtitleSizeOP->AddOption(
109 		B_TRANSLATE("Large"), mpSettings::SUBTITLE_SIZE_LARGE);
110 
111 	fSubtitlePlacementOP = new BOptionPopUp("subtitlePlacement",
112 		B_TRANSLATE("Subtitle placement:"), new BMessage(M_SETTINGS_CHANGED));
113 	fSubtitlePlacementOP->AddOption(B_TRANSLATE("Bottom of video"),
114 		mpSettings::SUBTITLE_PLACEMENT_BOTTOM_OF_VIDEO);
115 	fSubtitlePlacementOP->AddOption(B_TRANSLATE("Bottom of screen"),
116 		mpSettings::SUBTITLE_PLACEMENT_BOTTOM_OF_SCREEN);
117 
118 	fFullVolumeBGMoviesRB = new BRadioButton("rdbtnfullvolume",
119 		B_TRANSLATE("Full volume"), new BMessage(M_SETTINGS_CHANGED));
120 
121 	fHalfVolumeBGMoviesRB = new BRadioButton("rdbtnhalfvolume",
122 		B_TRANSLATE("Low volume"), new BMessage(M_SETTINGS_CHANGED));
123 
124 	fMutedVolumeBGMoviesRB = new BRadioButton("rdbtnfullvolume",
125 		B_TRANSLATE("Muted"), new BMessage(M_SETTINGS_CHANGED));
126 
127 	fRevertB = new BButton("revert", B_TRANSLATE("Revert"),
128 		new BMessage(M_SETTINGS_REVERT));
129 
130 	BButton* cancelButton = new BButton("cancel", B_TRANSLATE("Cancel"),
131 		new BMessage(M_SETTINGS_CANCEL));
132 
133 	BButton* okButton = new BButton("ok", B_TRANSLATE("OK"),
134 		new BMessage(M_SETTINGS_SAVE));
135 	okButton->MakeDefault(true);
136 
137 	// Build the layout
138 	BGroupLayout* volumeGroup;
139 	BGroupLayout* startGroup;
140 	BGroupLayout* playGroup;
141 	BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
142 		.AddGroup(settingsLayout)
143 			.SetInsets(kSpacing, kSpacing, kSpacing * 2, 0)
144 			.Add(playModeLabel)
145 			.AddGroup(B_HORIZONTAL, 0)
146 				.GetLayout(&playGroup)
147 				.AddStrut(10)
148 				.AddGroup(B_VERTICAL, 0)
149 					.GetLayout(&startGroup)
150 					.Add(fAutostartCB)
151 					.AddGrid(kSpacing, 0)
152 						.Add(BSpaceLayoutItem::CreateHorizontalStrut(kSpacing), 0, 0)
153 						.Add(fCloseWindowMoviesCB, 1, 0)
154 						.Add(BSpaceLayoutItem::CreateHorizontalStrut(kSpacing), 0, 1)
155 						.Add(fCloseWindowSoundsCB, 1, 1)
156 					.End()
157 					.Add(fLoopMoviesCB)
158 					.Add(fLoopSoundsCB)
159 				.End()
160 			.End()
161 			.AddStrut(kSpacing)
162 
163 			.Add(viewOptionsLabel)
164 			.AddGroup(B_HORIZONTAL, 0)
165 				.AddStrut(10)
166 				.AddGroup(B_VERTICAL, 0)
167 					.Add(fUseOverlaysCB)
168 					.Add(fScaleBilinearCB)
169 					.Add(fScaleFullscreenControlsCB)
170 					.Add(fSubtitleSizeOP)
171 					.Add(fSubtitlePlacementOP)
172 				.End()
173 			.End()
174 			.AddStrut(kSpacing)
175 
176 			.Add(bgMoviesModeLabel)
177 			.AddGroup(B_HORIZONTAL, 0)
178 				.AddStrut(10)
179 				.AddGroup(B_VERTICAL, 0)
180 					.GetLayout(&volumeGroup)
181 					.Add(fFullVolumeBGMoviesRB)
182 					.Add(fHalfVolumeBGMoviesRB)
183 					.Add(fMutedVolumeBGMoviesRB)
184 				.End()
185 			.End()
186 			.AddStrut(kSpacing)
187 		.End()
188 		.AddGroup(buttonLayout)
189 			.SetInsets(kSpacing)
190 			.Add(fRevertB)
191 			.AddGlue()
192 			.Add(cancelButton)
193 			.Add(okButton);
194 
195 	startGroup->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
196 	playGroup->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
197 	volumeGroup->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
198 }
199 
200 
201 SettingsWindow::~SettingsWindow()
202 {
203 }
204 
205 
206 void
207 SettingsWindow::Show()
208 {
209 	// The Settings that we want to be able to revert to is the state at which
210 	// the SettingsWindow was shown. So the current settings are stored in
211 	// fLastSettings.
212 	Settings::Default()->Get(fLastSettings);
213 	fSettings = fLastSettings;
214 	AdoptSettings();
215 
216 	BWindow::Show();
217 }
218 
219 
220 bool
221 SettingsWindow::QuitRequested()
222 {
223 	Hide();
224 	return false;
225 }
226 
227 
228 void
229 SettingsWindow::MessageReceived(BMessage* message)
230 {
231 	switch (message->what) {
232 		case M_SETTINGS_CHANGED:
233 			ApplySettings();
234 			break;
235 
236 		case B_KEY_DOWN:
237 			int32 index;
238 			if (message->FindInt32("key", &index) == B_OK && index == 1)
239 				PostMessage(B_QUIT_REQUESTED);
240 			break;
241 
242 		case M_SETTINGS_REVERT:
243 			Revert();
244 			break;
245 
246 		case M_SETTINGS_CANCEL:
247 			Revert();
248 			// fall through
249 		case M_SETTINGS_SAVE:
250 			PostMessage(B_QUIT_REQUESTED);
251 			break;
252 
253 		default:
254 			BWindow::MessageReceived(message);
255 			break;
256 	}
257 }
258 
259 
260 void
261 SettingsWindow::AdoptSettings()
262 {
263 	fAutostartCB->SetValue(fSettings.autostart);
264 	fCloseWindowMoviesCB->SetValue(fSettings.closeWhenDonePlayingMovie);
265 	fCloseWindowSoundsCB->SetValue(fSettings.closeWhenDonePlayingSound);
266 	fLoopMoviesCB->SetValue(fSettings.loopMovie);
267 	fLoopSoundsCB->SetValue(fSettings.loopSound);
268 
269 	fUseOverlaysCB->SetValue(fSettings.useOverlays);
270 	fScaleBilinearCB->SetValue(fSettings.scaleBilinear);
271 	fScaleFullscreenControlsCB->SetValue(fSettings.scaleFullscreenControls);
272 
273 	fSubtitleSizeOP->SetValue(fSettings.subtitleSize);
274 	fSubtitlePlacementOP->SetValue(fSettings.subtitlePlacement);
275 
276 	fFullVolumeBGMoviesRB->SetValue(fSettings.backgroundMovieVolumeMode
277 		== mpSettings::BG_MOVIES_FULL_VOLUME);
278 	fHalfVolumeBGMoviesRB->SetValue(fSettings.backgroundMovieVolumeMode
279 		== mpSettings::BG_MOVIES_HALF_VLUME);
280 	fMutedVolumeBGMoviesRB->SetValue(fSettings.backgroundMovieVolumeMode
281 		== mpSettings::BG_MOVIES_MUTED);
282 
283 	fRevertB->SetEnabled(IsRevertable());
284 }
285 
286 
287 void
288 SettingsWindow::ApplySettings()
289 {
290 	fSettings.autostart = fAutostartCB->Value() == B_CONTROL_ON;
291 	fSettings.closeWhenDonePlayingMovie
292 		= fCloseWindowMoviesCB->Value() == B_CONTROL_ON;
293 	fSettings.closeWhenDonePlayingSound
294 		= fCloseWindowSoundsCB->Value() == B_CONTROL_ON;
295 	fSettings.loopMovie = fLoopMoviesCB->Value() == B_CONTROL_ON;
296 	fSettings.loopSound = fLoopSoundsCB->Value() == B_CONTROL_ON;
297 
298 	fSettings.useOverlays = fUseOverlaysCB->Value() == B_CONTROL_ON;
299 	fSettings.scaleBilinear = fScaleBilinearCB->Value() == B_CONTROL_ON;
300 	fSettings.scaleFullscreenControls
301 		= fScaleFullscreenControlsCB->Value() == B_CONTROL_ON;
302 
303 	fSettings.subtitleSize = fSubtitleSizeOP->Value();
304 	fSettings.subtitlePlacement = fSubtitlePlacementOP->Value();
305 
306 	if (fFullVolumeBGMoviesRB->Value() == B_CONTROL_ON) {
307 		fSettings.backgroundMovieVolumeMode
308 			= mpSettings::BG_MOVIES_FULL_VOLUME;
309 	} else if (fHalfVolumeBGMoviesRB->Value() == B_CONTROL_ON) {
310 		fSettings.backgroundMovieVolumeMode
311 			= mpSettings::BG_MOVIES_HALF_VLUME;
312 	} else if (fMutedVolumeBGMoviesRB->Value() == B_CONTROL_ON) {
313 		fSettings.backgroundMovieVolumeMode
314 			= mpSettings::BG_MOVIES_MUTED;
315 	}
316 
317 	Settings::Default()->Update(fSettings);
318 
319 	fRevertB->SetEnabled(IsRevertable());
320 }
321 
322 
323 void
324 SettingsWindow::Revert()
325 {
326 	fSettings = fLastSettings;
327 	AdoptSettings();
328 	Settings::Default()->Update(fSettings);
329 }
330 
331 
332 bool
333 SettingsWindow::IsRevertable() const
334 {
335 	return fSettings != fLastSettings;
336 }
337 
338