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",
72 * -[HBPreferencesController init]
74 * Initializes the preferences controller by loading Preferences.nib file.
79 if (self = [super initWithWindowNibName:@"Preferences"])
81 NSAssert([self window], @"[HBPreferencesController init] window outlet is not connected in Preferences.nib");
87 * -[HBPreferencesController awakeFromNib]
89 * Called after all the outlets in the nib file have been attached. Sets up the
90 * toolbar and shows the "General" pane.
95 NSToolbar * toolbar = [[[NSToolbar alloc] initWithIdentifier: @"Preferences Toolbar"] autorelease];
96 [toolbar setDelegate: self];
97 [toolbar setAllowsUserCustomization: NO];
98 [toolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
99 [toolbar setSizeMode: NSToolbarSizeModeRegular];
100 [[self window] setToolbar: toolbar];
102 [toolbar setSelectedItemIdentifier: TOOLBAR_GENERAL];
103 [self setPrefView:nil];
106 - (NSToolbarItem *)toolbar: (NSToolbar *)toolbar
107 itemForItemIdentifier: (NSString *)ident
108 willBeInsertedIntoToolbar: (BOOL)flag
110 if ( [ident isEqualToString:TOOLBAR_GENERAL] )
112 return [self toolbarItemWithIdentifier:ident
113 label:NSLocalizedString(@"General", @"Preferences General Toolbar Item")
114 image:[NSImage imageNamed:NSImageNamePreferencesGeneral]];
116 else if ( [ident isEqualToString:TOOLBAR_PICTURE] )
118 return [self toolbarItemWithIdentifier:ident
119 label:NSLocalizedString(@"Picture", @"Preferences Picture Toolbar Item")
120 image:[NSImage imageNamed:@"pref-picture"]];
122 else if ( [ident isEqualToString:TOOLBAR_AUDIO] )
124 return [self toolbarItemWithIdentifier:ident
125 label:NSLocalizedString(@"Audio", @"Preferences Audio Toolbar Item")
126 image:[NSImage imageNamed:@"pref-audio"]];
128 else if ( [ident isEqualToString:TOOLBAR_ADVANCED] )
130 return [self toolbarItemWithIdentifier:ident
131 label:NSLocalizedString(@"Advanced", @"Preferences Advanced Toolbar Item")
132 image:[NSImage imageNamed:NSImageNameAdvanced]];
138 - (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *) toolbar
140 return [self toolbarDefaultItemIdentifiers: toolbar];
143 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
145 return [self toolbarAllowedItemIdentifiers: toolbar];
148 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
150 return [NSArray arrayWithObjects: TOOLBAR_GENERAL, /*TOOLBAR_PICTURE, */
151 TOOLBAR_AUDIO, TOOLBAR_ADVANCED, nil];
154 /* Manage the send encode to xxx.app windows and field */
155 /*Opens the app browse window*/
156 - (IBAction) browseSendToApp: (id) sender
160 panel = [NSOpenPanel openPanel];
161 [panel setAllowsMultipleSelection: NO];
162 [panel setCanChooseFiles: YES];
163 [panel setCanChooseDirectories: NO ];
164 NSString * sendToAppDirectory;
165 if ([[NSUserDefaults standardUserDefaults] stringForKey:@"LastSendToAppDirectory"])
167 sendToAppDirectory = [[NSUserDefaults standardUserDefaults] stringForKey:@"LastSendToAppDirectory"];
171 sendToAppDirectory = @"/Applications";
173 [panel beginSheetForDirectory: sendToAppDirectory file: nil types: nil
174 modalForWindow: [self window] modalDelegate: self
175 didEndSelector: @selector( browseSendToAppDone:returnCode:contextInfo: )
176 contextInfo: sender];
179 - (void) browseSendToAppDone: (NSOpenPanel *) sheet
180 returnCode: (int) returnCode contextInfo: (void *) contextInfo
182 if( returnCode == NSOKButton )
184 NSString *sendToAppPath = [[sheet filenames] objectAtIndex: 0];
185 NSString *sendToAppDirectory = [sendToAppPath stringByDeletingLastPathComponent];
186 [[NSUserDefaults standardUserDefaults] setObject:sendToAppDirectory forKey:@"LastSendToAppDirectory"];
187 [sheet orderOut: self];
188 NSString *sendToAppName;
189 sendToAppName = [[sendToAppPath lastPathComponent] stringByDeletingPathExtension];
190 /* we set the name of the app to send to in the display field */
191 [fSendEncodeToAppField setStringValue:sendToAppName];
192 [[NSUserDefaults standardUserDefaults] setObject:[fSendEncodeToAppField stringValue] forKey:@"SendCompletedEncodeToApp"];
200 @implementation HBPreferencesController (Private)
202 - (void) setPrefView: (id) sender
204 NSView * view = fGeneralView;
207 NSString * identifier = [sender itemIdentifier];
208 if( [identifier isEqualToString: TOOLBAR_PICTURE] )
210 else if( [identifier isEqualToString: TOOLBAR_AUDIO] )
212 else if( [identifier isEqualToString: TOOLBAR_ADVANCED] )
213 view = fAdvancedView;
217 NSWindow * window = [self window];
218 if( [window contentView] == view )
221 NSRect windowRect = [window frame];
222 CGFloat difference = ( [view frame].size.height - [[window contentView] frame].size.height ) * [window userSpaceScaleFactor];
223 windowRect.origin.y -= difference;
224 windowRect.size.height += difference;
226 [view setHidden: YES];
227 [window setContentView: view];
228 [window setFrame: windowRect display: YES animate: YES];
229 [view setHidden: NO];
233 [window setTitle: [sender label]];
236 NSToolbar * toolbar = [window toolbar];
237 NSString * itemIdentifier = [toolbar selectedItemIdentifier];
238 for( NSToolbarItem * item in [toolbar items] )
239 if( [[item itemIdentifier] isEqualToString: itemIdentifier] )
241 [window setTitle: [item label]];
248 * -[HBPreferencesController(Private) toolbarItemWithIdentifier:label:image:]
250 * Shared code for creating the NSToolbarItems for the Preferences toolbar.
253 - (NSToolbarItem *)toolbarItemWithIdentifier: (NSString *)identifier
254 label: (NSString *)label
255 image: (NSImage *)image
257 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:identifier];
258 [item setLabel:label];
259 [item setImage:image];
260 [item setAction:@selector(setPrefView:)];
261 [item setAutovalidates:NO];
262 return [item autorelease];