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