xref: /haiku/src/apps/mediaplayer/settings/SettingsWindow.cpp (revision 23d878482ed22e55dad6d1fca1df7bea42eb157c)
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 	BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
139 		.AddGroup(settingsLayout)
140 			.SetInsets(kSpacing, kSpacing, kSpacing * 2, 0)
141 			.Add(playModeLabel)
142 			.AddGroup(B_HORIZONTAL, 0)
143 				.AddStrut(kSpacing)
144 				.AddGroup(B_VERTICAL, 0)
145 					.Add(fAutostartCB)
146 					.AddGrid(kSpacing, 0)
147 						.Add(BSpaceLayoutItem::CreateHorizontalStrut(kSpacing), 0, 0)
148 						.Add(fCloseWindowMoviesCB, 1, 0)
149 						.Add(BSpaceLayoutItem::CreateHorizontalStrut(kSpacing), 0, 1)
150 						.Add(fCloseWindowSoundsCB, 1, 1)
151 					.End()
152 					.Add(fLoopMoviesCB)
153 					.Add(fLoopSoundsCB)
154 				.End()
155 			.End()
156 			.AddStrut(kSpacing)
157 
158 			.Add(viewOptionsLabel)
159 			.AddGroup(B_HORIZONTAL, 0)
160 				.AddStrut(10)
161 				.AddGroup(B_VERTICAL, 0)
162 					.Add(fUseOverlaysCB)
163 					.Add(fScaleBilinearCB)
164 					.Add(fScaleFullscreenControlsCB)
165 					.Add(fSubtitleSizeOP)
166 					.Add(fSubtitlePlacementOP)
167 				.End()
168 			.End()
169 			.AddStrut(kSpacing)
170 
171 			.Add(bgMoviesModeLabel)
172 			.AddGroup(B_HORIZONTAL, 0)
173 				.AddStrut(10)
174 				.AddGroup(B_VERTICAL, 0)
175 					.Add(fFullVolumeBGMoviesRB)
176 					.Add(fHalfVolumeBGMoviesRB)
177 					.Add(fMutedVolumeBGMoviesRB)
178 				.End()
179 			.End()
180 			.AddStrut(kSpacing)
181 		.End()
182 		.AddGroup(buttonLayout)
183 			.SetInsets(kSpacing)
184 			.Add(fRevertB)
185 			.AddGlue()
186 			.Add(cancelButton)
187 			.Add(okButton);
188 }
189 
190 
191 SettingsWindow::~SettingsWindow()
192 {
193 }
194 
195 
196 void
197 SettingsWindow::Show()
198 {
199 	// The Settings that we want to be able to revert to is the state at which
200 	// the SettingsWindow was shown. So the current settings are stored in
201 	// fLastSettings.
202 	Settings::Default()->Get(fLastSettings);
203 	fSettings = fLastSettings;
204 	AdoptSettings();
205 
206 	BWindow::Show();
207 }
208 
209 
210 bool
211 SettingsWindow::QuitRequested()
212 {
213 	Hide();
214 	return false;
215 }
216 
217 
218 void
219 SettingsWindow::MessageReceived(BMessage* message)
220 {
221 	switch (message->what) {
222 		case M_SETTINGS_CHANGED:
223 			ApplySettings();
224 			break;
225 
226 		case B_KEY_DOWN:
227 			int32 index;
228 			if (message->FindInt32("key", &index) == B_OK && index == 1)
229 				PostMessage(B_QUIT_REQUESTED);
230 			break;
231 
232 		case M_SETTINGS_REVERT:
233 			Revert();
234 			break;
235 
236 		case M_SETTINGS_CANCEL:
237 			Revert();
238 			// fall through
239 		case M_SETTINGS_SAVE:
240 			PostMessage(B_QUIT_REQUESTED);
241 			break;
242 
243 		default:
244 			BWindow::MessageReceived(message);
245 			break;
246 	}
247 }
248 
249 
250 void
251 SettingsWindow::AdoptSettings()
252 {
253 	fAutostartCB->SetValue(fSettings.autostart);
254 	fCloseWindowMoviesCB->SetValue(fSettings.closeWhenDonePlayingMovie);
255 	fCloseWindowSoundsCB->SetValue(fSettings.closeWhenDonePlayingSound);
256 	fLoopMoviesCB->SetValue(fSettings.loopMovie);
257 	fLoopSoundsCB->SetValue(fSettings.loopSound);
258 
259 	fUseOverlaysCB->SetValue(fSettings.useOverlays);
260 	fScaleBilinearCB->SetValue(fSettings.scaleBilinear);
261 	fScaleFullscreenControlsCB->SetValue(fSettings.scaleFullscreenControls);
262 
263 	fSubtitleSizeOP->SetValue(fSettings.subtitleSize);
264 	fSubtitlePlacementOP->SetValue(fSettings.subtitlePlacement);
265 
266 	fFullVolumeBGMoviesRB->SetValue(fSettings.backgroundMovieVolumeMode
267 		== mpSettings::BG_MOVIES_FULL_VOLUME);
268 	fHalfVolumeBGMoviesRB->SetValue(fSettings.backgroundMovieVolumeMode
269 		== mpSettings::BG_MOVIES_HALF_VLUME);
270 	fMutedVolumeBGMoviesRB->SetValue(fSettings.backgroundMovieVolumeMode
271 		== mpSettings::BG_MOVIES_MUTED);
272 
273 	fRevertB->SetEnabled(IsRevertable());
274 }
275 
276 
277 void
278 SettingsWindow::ApplySettings()
279 {
280 	fSettings.autostart = fAutostartCB->Value() == B_CONTROL_ON;
281 	fSettings.closeWhenDonePlayingMovie
282 		= fCloseWindowMoviesCB->Value() == B_CONTROL_ON;
283 	fSettings.closeWhenDonePlayingSound
284 		= fCloseWindowSoundsCB->Value() == B_CONTROL_ON;
285 	fSettings.loopMovie = fLoopMoviesCB->Value() == B_CONTROL_ON;
286 	fSettings.loopSound = fLoopSoundsCB->Value() == B_CONTROL_ON;
287 
288 	fSettings.useOverlays = fUseOverlaysCB->Value() == B_CONTROL_ON;
289 	fSettings.scaleBilinear = fScaleBilinearCB->Value() == B_CONTROL_ON;
290 	fSettings.scaleFullscreenControls
291 		= fScaleFullscreenControlsCB->Value() == B_CONTROL_ON;
292 
293 	fSettings.subtitleSize = fSubtitleSizeOP->Value();
294 	fSettings.subtitlePlacement = fSubtitlePlacementOP->Value();
295 
296 	if (fFullVolumeBGMoviesRB->Value() == B_CONTROL_ON) {
297 		fSettings.backgroundMovieVolumeMode
298 			= mpSettings::BG_MOVIES_FULL_VOLUME;
299 	} else if (fHalfVolumeBGMoviesRB->Value() == B_CONTROL_ON) {
300 		fSettings.backgroundMovieVolumeMode
301 			= mpSettings::BG_MOVIES_HALF_VLUME;
302 	} else if (fMutedVolumeBGMoviesRB->Value() == B_CONTROL_ON) {
303 		fSettings.backgroundMovieVolumeMode
304 			= mpSettings::BG_MOVIES_MUTED;
305 	}
306 
307 	Settings::Default()->Update(fSettings);
308 
309 	fRevertB->SetEnabled(IsRevertable());
310 }
311 
312 
313 void
314 SettingsWindow::Revert()
315 {
316 	fSettings = fLastSettings;
317 	AdoptSettings();
318 	Settings::Default()->Update(fSettings);
319 }
320 
321 
322 bool
323 SettingsWindow::IsRevertable() const
324 {
325 	return fSettings != fLastSettings;
326 }
327 
328