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 @"1", @"LoggingLevel",
59 @"NO", @"EncodeLogLocation",
60 @"10", @"PreviewsNumber",
62 @"0.25", @"x264CqSliderFractional",
63 @"YES", @"AlertBuiltInPresetUpdate",
64 @"MetaX", @"SendCompletedEncodeToApp",
69 * -[HBPreferencesController init]
71 * Initializes the preferences controller by loading Preferences.nib file.
76 if (self = [super initWithWindowNibName:@"Preferences"])
78 NSAssert([self window], @"[HBPreferencesController init] window outlet is not connected in Preferences.nib");
84 * -[HBPreferencesController awakeFromNib]
86 * Called after all the outlets in the nib file have been attached. Sets up the
87 * toolbar and shows the "General" pane.
92 NSToolbar * toolbar = [[[NSToolbar alloc] initWithIdentifier: @"Preferences Toolbar"] autorelease];
93 [toolbar setDelegate: self];
94 [toolbar setAllowsUserCustomization: NO];
95 [toolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
96 [toolbar setSizeMode: NSToolbarSizeModeRegular];
97 [[self window] setToolbar: toolbar];
99 [toolbar setSelectedItemIdentifier: TOOLBAR_GENERAL];
100 [self setPrefView:nil];
103 - (NSToolbarItem *)toolbar: (NSToolbar *)toolbar
104 itemForItemIdentifier: (NSString *)ident
105 willBeInsertedIntoToolbar: (BOOL)flag
107 if ( [ident isEqualToString:TOOLBAR_GENERAL] )
109 return [self toolbarItemWithIdentifier:ident
110 label:NSLocalizedString(@"General", @"Preferences General Toolbar Item")
111 image:[NSImage imageNamed:NSImageNamePreferencesGeneral]];
113 else if ( [ident isEqualToString:TOOLBAR_PICTURE] )
115 return [self toolbarItemWithIdentifier:ident
116 label:NSLocalizedString(@"Picture", @"Preferences Picture Toolbar Item")
117 image:[NSImage imageNamed:@"pref-picture"]];
119 else if ( [ident isEqualToString:TOOLBAR_AUDIO] )
121 return [self toolbarItemWithIdentifier:ident
122 label:NSLocalizedString(@"Audio", @"Preferences Audio Toolbar Item")
123 image:[NSImage imageNamed:@"pref-audio"]];
125 else if ( [ident isEqualToString:TOOLBAR_ADVANCED] )
127 return [self toolbarItemWithIdentifier:ident
128 label:NSLocalizedString(@"Advanced", @"Preferences Advanced Toolbar Item")
129 image:[NSImage imageNamed:NSImageNameAdvanced]];
135 - (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *) toolbar
137 return [self toolbarDefaultItemIdentifiers: toolbar];
140 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
142 return [self toolbarAllowedItemIdentifiers: toolbar];
145 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
147 return [NSArray arrayWithObjects: TOOLBAR_GENERAL, /*TOOLBAR_PICTURE, */
148 TOOLBAR_AUDIO, TOOLBAR_ADVANCED, nil];
151 /* Manage the send encode to xxx.app windows and field */
152 /*Opens the app browse window*/
153 - (IBAction) browseSendToApp: (id) sender
157 panel = [NSOpenPanel openPanel];
158 [panel setAllowsMultipleSelection: NO];
159 [panel setCanChooseFiles: YES];
160 [panel setCanChooseDirectories: NO ];
161 NSString * sendToAppDirectory;
162 if ([[NSUserDefaults standardUserDefaults] stringForKey:@"LastSendToAppDirectory"])
164 sendToAppDirectory = [[NSUserDefaults standardUserDefaults] stringForKey:@"LastSendToAppDirectory"];
168 sendToAppDirectory = @"/Applications";
170 [panel beginSheetForDirectory: sendToAppDirectory file: nil types: nil
171 modalForWindow: [self window] modalDelegate: self
172 didEndSelector: @selector( browseSendToAppDone:returnCode:contextInfo: )
173 contextInfo: sender];
176 - (void) browseSendToAppDone: (NSOpenPanel *) sheet
177 returnCode: (int) returnCode contextInfo: (void *) contextInfo
179 if( returnCode == NSOKButton )
181 NSString *sendToAppPath = [[sheet filenames] objectAtIndex: 0];
182 NSString *sendToAppDirectory = [sendToAppPath stringByDeletingLastPathComponent];
183 [[NSUserDefaults standardUserDefaults] setObject:sendToAppDirectory forKey:@"LastSendToAppDirectory"];
184 [sheet orderOut: self];
185 NSString *sendToAppName;
186 sendToAppName = [[sendToAppPath lastPathComponent] stringByDeletingPathExtension];
187 /* we set the name of the app to send to in the display field */
188 [fSendEncodeToAppField setStringValue:sendToAppName];
189 [[NSUserDefaults standardUserDefaults] setObject:[fSendEncodeToAppField stringValue] forKey:@"SendCompletedEncodeToApp"];
197 @implementation HBPreferencesController (Private)
199 - (void) setPrefView: (id) sender
201 NSView * view = fGeneralView;
204 NSString * identifier = [sender itemIdentifier];
205 if( [identifier isEqualToString: TOOLBAR_PICTURE] )
207 else if( [identifier isEqualToString: TOOLBAR_AUDIO] )
209 else if( [identifier isEqualToString: TOOLBAR_ADVANCED] )
210 view = fAdvancedView;
214 NSWindow * window = [self window];
215 if( [window contentView] == view )
218 NSRect windowRect = [window frame];
219 CGFloat difference = ( [view frame].size.height - [[window contentView] frame].size.height ) * [window userSpaceScaleFactor];
220 windowRect.origin.y -= difference;
221 windowRect.size.height += difference;
223 [view setHidden: YES];
224 [window setContentView: view];
225 [window setFrame: windowRect display: YES animate: YES];
226 [view setHidden: NO];
230 [window setTitle: [sender label]];
233 NSToolbar * toolbar = [window toolbar];
234 NSString * itemIdentifier = [toolbar selectedItemIdentifier];
235 for( NSToolbarItem * item in [toolbar items] )
236 if( [[item itemIdentifier] isEqualToString: itemIdentifier] )
238 [window setTitle: [item label]];
245 * -[HBPreferencesController(Private) toolbarItemWithIdentifier:label:image:]
247 * Shared code for creating the NSToolbarItems for the Preferences toolbar.
250 - (NSToolbarItem *)toolbarItemWithIdentifier: (NSString *)identifier
251 label: (NSString *)label
252 image: (NSImage *)image
254 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:identifier];
255 [item setLabel:label];
256 [item setImage:image];
257 [item setAction:@selector(setPrefView:)];
258 [item setAutovalidates:NO];
259 return [item autorelease];