OSDN Git Service

MacGui: Use libhb for audio limits as well as defaults.
[handbrake-jp/handbrake-jp-git.git] / macosx / HBPreferencesController.m
1 /**
2  * @file
3  * Implementation of class HBPreferencesController.
4  */
5
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"
11
12 /**
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.
17  *
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).
21  */
22
23 @interface HBPreferencesController (Private)
24
25 - (void) setPrefView: (id) sender;
26 - (NSToolbarItem *)toolbarItemWithIdentifier: (NSString *)identifier
27                                        label: (NSString *)label
28                                        image: (NSImage *)image;
29
30 @end
31
32 @implementation HBPreferencesController
33
34 /**
35  * +[HBPreferencesController registerUserDefaults]
36  *
37  * Registers default values to user defaults. This is called immediately
38  * when HandBrake starts, from [HBController init].
39  */
40 + (void)registerUserDefaults
41 {
42     NSString *desktopDirectory =  [@"~/Desktop" stringByExpandingTildeInPath];
43
44     [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
45         @"YES",             @"CheckForUpdates",
46         @"Open Source",     @"LaunchSourceBehavior",
47         @"English",         @"DefaultLanguage",
48         @"YES",             @"UseCoreAudio",
49         @"YES",              @"DefaultMpegName",
50         @"YES",             @"UseDvdNav",
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",
63         @"",                @"Drawer Size",
64         @"0.25",            @"x264CqSliderFractional",
65         @"YES",             @"AlertBuiltInPresetUpdate",
66         @"MetaX",           @"SendCompletedEncodeToApp",
67                 @"YES",                         @"AC3PassthruDefaultsToAC3",
68                 @"YES",                         @"CodecDefaultsMixdown",
69         nil]];
70 }
71
72 /**
73  * -[HBPreferencesController init]
74  *
75  * Initializes the preferences controller by loading Preferences.nib file.
76  *
77  */
78 - (id)init
79 {
80     if (self = [super initWithWindowNibName:@"Preferences"])
81     {
82         NSAssert([self window], @"[HBPreferencesController init] window outlet is not connected in Preferences.nib");
83     }
84     return self;
85 }
86
87 /**
88  * -[HBPreferencesController awakeFromNib]
89  *
90  * Called after all the outlets in the nib file have been attached. Sets up the
91  * toolbar and shows the "General" pane.
92  *
93  */
94 - (void) awakeFromNib
95 {
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];
102
103     [toolbar setSelectedItemIdentifier: TOOLBAR_GENERAL];
104     [self setPrefView:nil];
105 }
106
107 - (NSToolbarItem *)toolbar: (NSToolbar *)toolbar
108      itemForItemIdentifier: (NSString *)ident
109  willBeInsertedIntoToolbar: (BOOL)flag
110 {
111     if ( [ident isEqualToString:TOOLBAR_GENERAL] )
112     {
113         return [self toolbarItemWithIdentifier:ident
114                                          label:NSLocalizedString(@"General", @"Preferences General Toolbar Item")
115                                          image:[NSImage imageNamed:NSImageNamePreferencesGeneral]];
116     }
117     else if ( [ident isEqualToString:TOOLBAR_PICTURE] )
118     {
119         return [self toolbarItemWithIdentifier:ident
120                                          label:NSLocalizedString(@"Picture", @"Preferences Picture Toolbar Item")
121                                          image:[NSImage imageNamed:@"pref-picture"]];
122     }
123     else if ( [ident isEqualToString:TOOLBAR_AUDIO] )
124     {
125         return [self toolbarItemWithIdentifier:ident
126                                          label:NSLocalizedString(@"Audio", @"Preferences Audio Toolbar Item")
127                                          image:[NSImage imageNamed:@"pref-audio"]];
128     }
129     else if ( [ident isEqualToString:TOOLBAR_ADVANCED] )
130     {
131         return [self toolbarItemWithIdentifier:ident
132                                          label:NSLocalizedString(@"Advanced", @"Preferences Advanced Toolbar Item")
133                                          image:[NSImage imageNamed:NSImageNameAdvanced]];
134     }
135
136     return nil;
137 }
138
139 - (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *) toolbar
140 {
141     return [self toolbarDefaultItemIdentifiers: toolbar];
142 }
143
144 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
145 {
146     return [self toolbarAllowedItemIdentifiers: toolbar];
147 }
148
149 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
150 {
151     return [NSArray arrayWithObjects: TOOLBAR_GENERAL, /*TOOLBAR_PICTURE, */
152                                         TOOLBAR_AUDIO, TOOLBAR_ADVANCED, nil];
153 }
154
155 /* Manage the send encode to xxx.app windows and field */
156 /*Opens the app browse window*/
157 - (IBAction) browseSendToApp: (id) sender
158 {
159     NSOpenPanel * panel;
160         
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"])
167         {
168                 sendToAppDirectory = [[NSUserDefaults standardUserDefaults] stringForKey:@"LastSendToAppDirectory"];
169         }
170         else
171         {
172                 sendToAppDirectory = @"/Applications";
173         }
174     [panel beginSheetForDirectory: sendToAppDirectory file: nil types: nil
175                    modalForWindow: [self window] modalDelegate: self
176                    didEndSelector: @selector( browseSendToAppDone:returnCode:contextInfo: )
177                       contextInfo: sender]; 
178 }
179
180 - (void) browseSendToAppDone: (NSOpenPanel *) sheet
181                   returnCode: (int) returnCode contextInfo: (void *) contextInfo
182 {
183     if( returnCode == NSOKButton )
184     {
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"];
194         
195     }
196 }
197
198
199 @end
200
201 @implementation HBPreferencesController (Private)
202
203 - (void) setPrefView: (id) sender
204 {
205     NSView * view = fGeneralView;
206     if( sender )
207     {
208         NSString * identifier = [sender itemIdentifier];
209         if( [identifier isEqualToString: TOOLBAR_PICTURE] )
210             view = fPictureView;
211         else if( [identifier isEqualToString: TOOLBAR_AUDIO] )
212             view = fAudioView;
213         else if( [identifier isEqualToString: TOOLBAR_ADVANCED] )
214             view = fAdvancedView;
215         else;
216     }
217
218     NSWindow * window = [self window];
219     if( [window contentView] == view )
220         return;
221
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;
226
227     [view setHidden: YES];
228     [window setContentView: view];
229     [window setFrame: windowRect display: YES animate: YES];
230     [view setHidden: NO];
231
232     //set title label
233     if( sender )
234         [window setTitle: [sender label]];
235     else
236     {
237         NSToolbar * toolbar = [window toolbar];
238         NSString * itemIdentifier = [toolbar selectedItemIdentifier];
239         for( NSToolbarItem * item in [toolbar items] )
240             if( [[item itemIdentifier] isEqualToString: itemIdentifier] )
241             {
242                 [window setTitle: [item label]];
243                 break;
244             }
245     }
246 }
247
248 /**
249  * -[HBPreferencesController(Private) toolbarItemWithIdentifier:label:image:]
250  *
251  * Shared code for creating the NSToolbarItems for the Preferences toolbar.
252  *
253  */
254 - (NSToolbarItem *)toolbarItemWithIdentifier: (NSString *)identifier
255                                        label: (NSString *)label
256                                        image: (NSImage *)image
257 {
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];
264 }
265
266 @end