xref: /haiku/src/apps/mediaplayer/settings/SettingsWindow.cpp (revision 0044a8c39ab5721051b6279506d1a8c511e20453)
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 <GridLayoutBuilder.h>
20 #include <GroupLayoutBuilder.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_TRANSLATE_CONTEXT
31 #define B_TRANSLATE_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 #define SPACE 10
44 #define SPACEING 7
45 #define BUTTONHEIGHT 20
46 
47 
48 SettingsWindow::SettingsWindow(BRect frame)
49  	:
50  	BWindow(frame, B_TRANSLATE("MediaPlayer settings"), B_FLOATING_WINDOW_LOOK,
51  		B_FLOATING_APP_WINDOW_FEEL,
52  		B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE | B_NOT_RESIZABLE
53  			| B_AUTO_UPDATE_SIZE_LIMITS)
54 {
55 	BBox* settingsBox = new BBox(B_PLAIN_BORDER, NULL);
56 	BGroupLayout* settingsLayout = new BGroupLayout(B_VERTICAL, 5);
57 	settingsBox->SetLayout(settingsLayout);
58 	BBox* buttonBox = new BBox(B_PLAIN_BORDER, NULL);
59 	BGroupLayout* buttonLayout = new BGroupLayout(B_HORIZONTAL, 5);
60 	buttonBox->SetLayout(buttonLayout);
61 
62 	BStringView* playModeLabel = new BStringView("stringViewPlayMode",
63 		B_TRANSLATE("Play mode"));
64 	BStringView* viewOptionsLabel = new BStringView("stringViewViewOpions",
65 		B_TRANSLATE("View options"));
66 	BStringView* bgMoviesModeLabel = new BStringView("stringViewPlayBackg",
67 		B_TRANSLATE("Volume of background clips"));
68 	BAlignment alignment(B_ALIGN_LEFT, B_ALIGN_VERTICAL_CENTER);
69 	playModeLabel->SetExplicitAlignment(alignment);
70 	playModeLabel->SetFont(be_bold_font);
71 	viewOptionsLabel->SetExplicitAlignment(alignment);
72 	viewOptionsLabel->SetFont(be_bold_font);
73 	bgMoviesModeLabel->SetExplicitAlignment(alignment);
74 	bgMoviesModeLabel->SetFont(be_bold_font);
75 
76 	fAutostartCB = new BCheckBox("chkboxAutostart",
77 		B_TRANSLATE("Automatically start playing"),
78 		new BMessage(M_SETTINGS_CHANGED));
79 
80 	fCloseWindowMoviesCB = new BCheckBox("chkBoxCloseWindowMovies",
81 		B_TRANSLATE("Close window after playing video"),
82 		new BMessage(M_SETTINGS_CHANGED));
83 	fCloseWindowSoundsCB = new BCheckBox("chkBoxCloseWindowSounds",
84 		B_TRANSLATE("Close window after playing audio"),
85 		new BMessage(M_SETTINGS_CHANGED));
86 
87 	fLoopMoviesCB = new BCheckBox("chkBoxLoopMovie",
88 		B_TRANSLATE("Loop video"),
89 		new BMessage(M_SETTINGS_CHANGED));
90 	fLoopSoundsCB = new BCheckBox("chkBoxLoopSounds",
91 		B_TRANSLATE("Loop audio"),
92 		new BMessage(M_SETTINGS_CHANGED));
93 
94 	fUseOverlaysCB = new BCheckBox("chkBoxUseOverlays",
95 		B_TRANSLATE("Use hardware video overlays if available"),
96 		new BMessage(M_SETTINGS_CHANGED));
97 	fScaleBilinearCB = new BCheckBox("chkBoxScaleBilinear",
98 		B_TRANSLATE("Scale movies smoothly (non-overlay mode)"),
99 		new BMessage(M_SETTINGS_CHANGED));
100 
101 	fScaleFullscreenControlsCB = new BCheckBox("chkBoxScaleControls",
102 		B_TRANSLATE("Scale controls in full screen mode"),
103 		new BMessage(M_SETTINGS_CHANGED));
104 
105 	fSubtitleSizeOP = new BOptionPopUp("subtitleSize",
106 		B_TRANSLATE("Subtitle size:"), new BMessage(M_SETTINGS_CHANGED));
107 	fSubtitleSizeOP->AddOption(
108 		B_TRANSLATE("Small"), mpSettings::SUBTITLE_SIZE_SMALL);
109 	fSubtitleSizeOP->AddOption(
110 		B_TRANSLATE("Medium"), mpSettings::SUBTITLE_SIZE_MEDIUM);
111 	fSubtitleSizeOP->AddOption(
112 		B_TRANSLATE("Large"), mpSettings::SUBTITLE_SIZE_LARGE);
113 
114 	fSubtitlePlacementOP = new BOptionPopUp("subtitlePlacement",
115 		B_TRANSLATE("Subtitle placement:"), new BMessage(M_SETTINGS_CHANGED));
116 	fSubtitlePlacementOP->AddOption(B_TRANSLATE("Bottom of video"),
117 		mpSettings::SUBTITLE_PLACEMENT_BOTTOM_OF_VIDEO);
118 	fSubtitlePlacementOP->AddOption(B_TRANSLATE("Bottom of screen"),
119 		mpSettings::SUBTITLE_PLACEMENT_BOTTOM_OF_SCREEN);
120 
121 	fFullVolumeBGMoviesRB = new BRadioButton("rdbtnfullvolume",
122 		B_TRANSLATE("Full volume"), new BMessage(M_SETTINGS_CHANGED));
123 
124 	fHalfVolumeBGMoviesRB = new BRadioButton("rdbtnhalfvolume",
125 		B_TRANSLATE("Low volume"), new BMessage(M_SETTINGS_CHANGED));
126 
127 	fMutedVolumeBGMoviesRB = new BRadioButton("rdbtnfullvolume",
128 		B_TRANSLATE("Muted"), new BMessage(M_SETTINGS_CHANGED));
129 
130 	fRevertB = new BButton("revert", B_TRANSLATE("Revert"),
131 		new BMessage(M_SETTINGS_REVERT));
132 
133 	BButton* cancelButton = new BButton("cancel", B_TRANSLATE("Cancel"),
134 		new BMessage(M_SETTINGS_CANCEL));
135 
136 	BButton* okButton = new BButton("ok", B_TRANSLATE("OK"),
137 		new BMessage(M_SETTINGS_SAVE));
138 	okButton->MakeDefault(true);
139 
140 
141 	// Build the layout
142 	SetLayout(new BGroupLayout(B_HORIZONTAL));
143 
144 	AddChild(BGroupLayoutBuilder(B_VERTICAL, 0)
145 		.Add(BGroupLayoutBuilder(settingsLayout)
146 			.Add(playModeLabel)
147 			.Add(BGroupLayoutBuilder(B_HORIZONTAL, 0)
148 				.Add(BSpaceLayoutItem::CreateHorizontalStrut(10))
149 				.Add(BGroupLayoutBuilder(B_VERTICAL, 0)
150 					.Add(fAutostartCB)
151 					.Add(BGridLayoutBuilder(5, 0)
152 						.Add(BSpaceLayoutItem::CreateHorizontalStrut(10), 0, 0)
153 						.Add(fCloseWindowMoviesCB, 1, 0)
154 						.Add(BSpaceLayoutItem::CreateHorizontalStrut(10), 0, 1)
155 						.Add(fCloseWindowSoundsCB, 1, 1)
156 					)
157 					.Add(fLoopMoviesCB)
158 					.Add(fLoopSoundsCB)
159 				)
160 			)
161 			.Add(BSpaceLayoutItem::CreateVerticalStrut(5))
162 
163 			.Add(viewOptionsLabel)
164 			.Add(BGroupLayoutBuilder(B_HORIZONTAL, 0)
165 				.Add(BSpaceLayoutItem::CreateHorizontalStrut(10))
166 				.Add(BGroupLayoutBuilder(B_VERTICAL, 0)
167 					.Add(fUseOverlaysCB)
168 					.Add(fScaleBilinearCB)
169 					.Add(fScaleFullscreenControlsCB)
170 					.Add(fSubtitleSizeOP)
171 					.Add(fSubtitlePlacementOP)
172 				)
173 			)
174 			.Add(BSpaceLayoutItem::CreateVerticalStrut(5))
175 
176 			.Add(bgMoviesModeLabel)
177 			.Add(BGroupLayoutBuilder(B_HORIZONTAL, 0)
178 				.Add(BSpaceLayoutItem::CreateHorizontalStrut(10))
179 				.Add(BGroupLayoutBuilder(B_VERTICAL, 0)
180 					.Add(fFullVolumeBGMoviesRB)
181 					.Add(fHalfVolumeBGMoviesRB)
182 					.Add(fMutedVolumeBGMoviesRB)
183 				)
184 			)
185 			.Add(BSpaceLayoutItem::CreateVerticalStrut(5))
186 
187 			.SetInsets(5, 5, 15, 5)
188 		)
189 		.Add(BGroupLayoutBuilder(buttonLayout)
190 			.Add(fRevertB)
191 			.AddGlue()
192 			.Add(cancelButton)
193 			.Add(okButton)
194 			.SetInsets(5, 5, 5, 5)
195 		)
196 	);
197 }
198 
199 
200 SettingsWindow::~SettingsWindow()
201 {
202 }
203 
204 
205 void
206 SettingsWindow::Show()
207 {
208 	// The Settings that we want to be able to revert to is the state at which
209 	// the SettingsWindow was shown. So the current settings are stored in
210 	// fLastSettings.
211 	Settings::Default()->Get(fLastSettings);
212 	fSettings = fLastSettings;
213 	AdoptSettings();
214 
215 	BWindow::Show();
216 }
217 
218 
219 bool
220 SettingsWindow::QuitRequested()
221 {
222 	Hide();
223 	return false;
224 }
225 
226 
227 void
228 SettingsWindow::MessageReceived(BMessage* message)
229 {
230 	switch (message->what) {
231 		case M_SETTINGS_CHANGED:
232 			ApplySettings();
233 			break;
234 
235 		case B_KEY_DOWN:
236 			int32 index;
237 			if (message->FindInt32("key", &index) == B_OK && index == 1)
238 				PostMessage(B_QUIT_REQUESTED);
239 			break;
240 
241 		case M_SETTINGS_REVERT:
242 			Revert();
243 			break;
244 
245 		case M_SETTINGS_CANCEL:
246 			Revert();
247 			// fall through
248 		case M_SETTINGS_SAVE:
249 			PostMessage(B_QUIT_REQUESTED);
250 			break;
251 
252 		default:
253 			BWindow::MessageReceived(message);
254 			break;
255 	}
256 }
257 
258 
259 void
260 SettingsWindow::AdoptSettings()
261 {
262 	fAutostartCB->SetValue(fSettings.autostart);
263 	fCloseWindowMoviesCB->SetValue(fSettings.closeWhenDonePlayingMovie);
264 	fCloseWindowSoundsCB->SetValue(fSettings.closeWhenDonePlayingSound);
265 	fLoopMoviesCB->SetValue(fSettings.loopMovie);
266 	fLoopSoundsCB->SetValue(fSettings.loopSound);
267 
268 	fUseOverlaysCB->SetValue(fSettings.useOverlays);
269 	fScaleBilinearCB->SetValue(fSettings.scaleBilinear);
270 	fScaleFullscreenControlsCB->SetValue(fSettings.scaleFullscreenControls);
271 
272 	fSubtitleSizeOP->SetValue(fSettings.subtitleSize);
273 	fSubtitlePlacementOP->SetValue(fSettings.subtitlePlacement);
274 
275 	fFullVolumeBGMoviesRB->SetValue(fSettings.backgroundMovieVolumeMode
276 		== mpSettings::BG_MOVIES_FULL_VOLUME);
277 	fHalfVolumeBGMoviesRB->SetValue(fSettings.backgroundMovieVolumeMode
278 		== mpSettings::BG_MOVIES_HALF_VLUME);
279 	fMutedVolumeBGMoviesRB->SetValue(fSettings.backgroundMovieVolumeMode
280 		== mpSettings::BG_MOVIES_MUTED);
281 
282 	fRevertB->SetEnabled(IsRevertable());
283 }
284 
285 
286 void
287 SettingsWindow::ApplySettings()
288 {
289 	fSettings.autostart = fAutostartCB->Value() == B_CONTROL_ON;
290 	fSettings.closeWhenDonePlayingMovie
291 		= fCloseWindowMoviesCB->Value() == B_CONTROL_ON;
292 	fSettings.closeWhenDonePlayingSound
293 		= fCloseWindowSoundsCB->Value() == B_CONTROL_ON;
294 	fSettings.loopMovie = fLoopMoviesCB->Value() == B_CONTROL_ON;
295 	fSettings.loopSound = fLoopSoundsCB->Value() == B_CONTROL_ON;
296 
297 	fSettings.useOverlays = fUseOverlaysCB->Value() == B_CONTROL_ON;
298 	fSettings.scaleBilinear = fScaleBilinearCB->Value() == B_CONTROL_ON;
299 	fSettings.scaleFullscreenControls
300 		= fScaleFullscreenControlsCB->Value() == B_CONTROL_ON;
301 
302 	fSettings.subtitleSize = fSubtitleSizeOP->Value();
303 	fSettings.subtitlePlacement = fSubtitlePlacementOP->Value();
304 
305 	if (fFullVolumeBGMoviesRB->Value() == B_CONTROL_ON) {
306 		fSettings.backgroundMovieVolumeMode
307 			= mpSettings::BG_MOVIES_FULL_VOLUME;
308 	} else if (fHalfVolumeBGMoviesRB->Value() == B_CONTROL_ON) {
309 		fSettings.backgroundMovieVolumeMode
310 			= mpSettings::BG_MOVIES_HALF_VLUME;
311 	} else if (fMutedVolumeBGMoviesRB->Value() == B_CONTROL_ON) {
312 		fSettings.backgroundMovieVolumeMode
313 			= mpSettings::BG_MOVIES_MUTED;
314 	}
315 
316 	Settings::Default()->Update(fSettings);
317 
318 	fRevertB->SetEnabled(IsRevertable());
319 }
320 
321 
322 void
323 SettingsWindow::Revert()
324 {
325 	fSettings = fLastSettings;
326 	AdoptSettings();
327 	Settings::Default()->Update(fSettings);
328 }
329 
330 
331 bool
332 SettingsWindow::IsRevertable() const
333 {
334 	return fSettings != fLastSettings;
335 }
336 
337