3 * Implementation of class HBPreferencesController.
6 #import "HBPreferencesController.h"
7 #define TOOLBAR_GENERAL @"TOOLBAR_GENERAL"
8 #define TOOLBAR_PICTURE @"TOOLBAR_PICTURE"
9 #define TOOLBAR_AUDIO @"TOOLBAR_AUDIO"
10 #define TOOLBAR_ADVANCED @"TOOLBAR_ADVANCED"
13 * This class controls the preferences window of HandBrake. Default values for
14 * all preferences and user defaults are specified in class method
15 * @c registerUserDefaults. The preferences window is loaded from
16 * Preferences.nib file when HBPreferencesController is initialized.
18 * All preferences are bound to user defaults in Interface Builder, therefore
19 * no getter/setter code is needed in this file (unless more complicated
20 * preference settings are added that cannot be handled with Cocoa bindings).
23 @interface HBPreferencesController (Private)
25 - (void) setPrefView: (id) sender;
26 - (NSToolbarItem *)toolbarItemWithIdentifier: (NSString *)identifier
27 label: (NSString *)label
28 image: (NSImage *)image;
32 @implementation HBPreferencesController
35 * +[HBPreferencesController registerUserDefaults]
37 * Registers default values to user defaults. This is called immediately
38 * when HandBrake starts, from [HBController init].
40 + (void)registerUserDefaults
42 NSString *desktopDirectory = [@"~/Desktop" stringByExpandingTildeInPath];
44 [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
45 @"YES", @"CheckForUpdates",
46 @"Open Source", @"LaunchSourceBehavior",
47 @"English", @"DefaultLanguage",
48 @"YES", @"UseCoreAudio",
49 @"YES", @"DefaultMpegName",
51 @"", @"DefAdvancedx264Flags",
52 @"YES", @"DefaultPresetsDrawerShow",
53 desktopDirectory, @"LastDestinationDirectory",
54 desktopDirectory, @"LastSourceDirectory",
55 @"NO", @"DefaultAutoNaming",
56 @"NO", @"DisableDvdAutoDetect",
57 @"Alert Window", @"AlertWhenDone",
58 @"YES", @"AlertWhenDoneSound",
59 @"1", @"LoggingLevel",
60 @"NO", @"EncodeLogLocation",
61 @"10", @"MinTitleScanSeconds",
62 @"10", @"PreviewsNumber",
64 @"0.25", @"x264CqSliderFractional",
65 @"YES", @"AlertBuiltInPresetUpdate",
66 @"MetaX", @"SendCompletedEncodeToApp",
67 @"YES", @"AC3PassthruDefaultsToAC3",
68 @"YES", @"CodecDefaultsMixdown",
73 * -[HBPreferencesController init]
75 * Initializes the preferences controller by loading Preferences.nib file.
80 if (self = [super initWithWindowNibName:@"Preferences"])
82 NSAssert([self window], @"[HBPreferencesController init] window outlet is not connected in Preferences.nib");
88 * -[HBPreferencesController awakeFromNib]
90 * Called after all the outlets in the nib file have been attached. Sets up the
91 * toolbar and shows the "General" pane.
96 NSToolbar * toolbar = [[[NSToolbar alloc] initWithIdentifier: @"Preferences Toolbar"] autorelease];
97 [toolbar setDelegate: self];
98 [toolbar setAllowsUserCustomization: NO];
99 [toolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
100 [toolbar setSizeMode: NSToolbarSizeModeRegular];
101 [[self window] setToolbar: toolbar];
103 [toolbar setSelectedItemIdentifier: TOOLBAR_GENERAL];
104 [self setPrefView:nil];
107 - (NSToolbarItem *)toolbar: (NSToolbar *)toolbar
108 itemForItemIdentifier: (NSString *)ident
109 willBeInsertedIntoToolbar: (BOOL)flag
111 if ( [ident isEqualToString:TOOLBAR_GENERAL] )
113 return [self toolbarItemWithIdentifier:ident
114 label:NSLocalizedString(@"General", @"Preferences General Toolbar Item")
115 image:[NSImage imageNamed:NSImageNamePreferencesGeneral]];
117 else if ( [ident isEqualToString:TOOLBAR_PICTURE] )
119 return [self toolbarItemWithIdentifier:ident
120 label:NSLocalizedString(@"Picture", @"Preferences Picture Toolbar Item")
121 image:[NSImage imageNamed:@"pref-picture"]];
123 else if ( [ident isEqualToString:TOOLBAR_AUDIO] )
125 return [self toolbarItemWithIdentifier:ident
126 label:NSLocalizedString(@"Audio", @"Preferences Audio Toolbar Item")
127 image:[NSImage imageNamed:@"pref-audio"]];
129 else if ( [ident isEqualToString:TOOLBAR_ADVANCED] )
131 return [self toolbarItemWithIdentifier:ident
132 label:NSLocalizedString(@"Advanced", @"Preferences Advanced Toolbar Item")
133 image:[NSImage imageNamed:NSImageNameAdvanced]];
139 - (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *) toolbar
141 return [self toolbarDefaultItemIdentifiers: toolbar];
144 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
146 return [self toolbarAllowedItemIdentifiers: toolbar];
149 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
151 return [NSArray arrayWithObjects: TOOLBAR_GENERAL, /*TOOLBAR_PICTURE, */
152 TOOLBAR_AUDIO, TOOLBAR_ADVANCED, nil];
155 /* Manage the send encode to xxx.app windows and field */
156 /*Opens the app browse window*/
157 - (IBAction) browseSendToApp: (id) sender
161 panel = [NSOpenPanel openPanel];
162 [panel setAllowsMultipleSelection: NO];
163 [panel setCanChooseFiles: YES];
164 [panel setCanChooseDirectories: NO ];
165 NSString * sendToAppDirectory;
166 if ([[NSUserDefaults standardUserDefaults] stringForKey:@"LastSendToAppDirectory"])
168 sendToAppDirectory = [[NSUserDefaults standardUserDefaults] stringForKey:@"LastSendToAppDirectory"];
172 sendToAppDirectory = @"/Applications";
174 [panel beginSheetForDirectory: sendToAppDirectory file: nil types: nil
175 modalForWindow: [self window] modalDelegate: self
176 didEndSelector: @selector( browseSendToAppDone:returnCode:contextInfo: )
177 contextInfo: sender];
180 - (void) browseSendToAppDone: (NSOpenPanel *) sheet
181 returnCode: (int) returnCode contextInfo: (void *) contextInfo
183 if( returnCode == NSOKButton )
185 NSString *sendToAppPath = [[sheet filenames] objectAtIndex: 0];
186 NSString *sendToAppDirectory = [sendToAppPath stringByDeletingLastPathComponent];
187 [[NSUserDefaults standardUserDefaults] setObject:sendToAppDirectory forKey:@"LastSendToAppDirectory"];
188 [sheet orderOut: self];
189 NSString *sendToAppName;
190 sendToAppName = [[sendToAppPath lastPathComponent] stringByDeletingPathExtension];
191 /* we set the name of the app to send to in the display field */
192 [fSendEncodeToAppField setStringValue:sendToAppName];
193 [[NSUserDefaults standardUserDefaults] setObject:[fSendEncodeToAppField stringValue] forKey:@"SendCompletedEncodeToApp"];
201 @implementation HBPreferencesController (Private)
203 - (void) setPrefView: (id) sender
205 NSView * view = fGeneralView;
208 NSString * identifier = [sender itemIdentifier];
209 if( [identifier isEqualToString: TOOLBAR_PICTURE] )
211 else if( [identifier isEqualToString: TOOLBAR_AUDIO] )
213 else if( [identifier isEqualToString: TOOLBAR_ADVANCED] )
214 view = fAdvancedView;
218 NSWindow * window = [self window];
219 if( [window contentView] == view )
222 NSRect windowRect = [window frame];
223 CGFloat difference = ( [view frame].size.height - [[window contentView] frame].size.height ) * [window userSpaceScaleFactor];
224 windowRect.origin.y -= difference;
225 windowRect.size.height += difference;
227 [view setHidden: YES];
228 [window setContentView: view];
229 [window setFrame: windowRect display: YES animate: YES];
230 [view setHidden: NO];
234 [window setTitle: [sender label]];
237 NSToolbar * toolbar = [window toolbar];
238 NSString * itemIdentifier = [toolbar selectedItemIdentifier];
239 for( NSToolbarItem * item in [toolbar items] )
240 if( [[item itemIdentifier] isEqualToString: itemIdentifier] )
242 [window setTitle: [item label]];
249 * -[HBPreferencesController(Private) toolbarItemWithIdentifier:label:image:]
251 * Shared code for creating the NSToolbarItems for the Preferences toolbar.
254 - (NSToolbarItem *)toolbarItemWithIdentifier: (NSString *)identifier
255 label: (NSString *)label
256 image: (NSImage *)image
258 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:identifier];
259 [item setLabel:label];
260 [item setImage:image];
261 [item setAction:@selector(setPrefView:)];
262 [item setAutovalidates:NO];
263 return [item autorelease];