OSDN Git Service

MacGui: Make .m4v (Use iTunes Friendly Naming) default in preferences.
[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         @"1",               @"LoggingLevel",
59         @"NO",              @"EncodeLogLocation",
60         @"10",              @"PreviewsNumber",
61         @"",                @"Drawer Size",
62         @"0.25",            @"x264CqSliderFractional",
63         @"YES",             @"AlertBuiltInPresetUpdate",
64         @"MetaX",           @"SendCompletedEncodeToApp",
65         nil]];
66 }
67
68 /**
69  * -[HBPreferencesController init]
70  *
71  * Initializes the preferences controller by loading Preferences.nib file.
72  *
73  */
74 - (id)init
75 {
76     if (self = [super initWithWindowNibName:@"Preferences"])
77     {
78         NSAssert([self window], @"[HBPreferencesController init] window outlet is not connected in Preferences.nib");
79     }
80     return self;
81 }
82
83 /**
84  * -[HBPreferencesController awakeFromNib]
85  *
86  * Called after all the outlets in the nib file have been attached. Sets up the
87  * toolbar and shows the "General" pane.
88  *
89  */
90 - (void) awakeFromNib
91 {
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];
98
99     [toolbar setSelectedItemIdentifier: TOOLBAR_GENERAL];
100     [self setPrefView:nil];
101 }
102
103 - (NSToolbarItem *)toolbar: (NSToolbar *)toolbar
104      itemForItemIdentifier: (NSString *)ident
105  willBeInsertedIntoToolbar: (BOOL)flag
106 {
107     if ( [ident isEqualToString:TOOLBAR_GENERAL] )
108     {
109         return [self toolbarItemWithIdentifier:ident
110                                          label:NSLocalizedString(@"General", @"Preferences General Toolbar Item")
111                                          image:[NSImage imageNamed:NSImageNamePreferencesGeneral]];
112     }
113     else if ( [ident isEqualToString:TOOLBAR_PICTURE] )
114     {
115         return [self toolbarItemWithIdentifier:ident
116                                          label:NSLocalizedString(@"Picture", @"Preferences Picture Toolbar Item")
117                                          image:[NSImage imageNamed:@"pref-picture"]];
118     }
119     else if ( [ident isEqualToString:TOOLBAR_AUDIO] )
120     {
121         return [self toolbarItemWithIdentifier:ident
122                                          label:NSLocalizedString(@"Audio", @"Preferences Audio Toolbar Item")
123                                          image:[NSImage imageNamed:@"pref-audio"]];
124     }
125     else if ( [ident isEqualToString:TOOLBAR_ADVANCED] )
126     {
127         return [self toolbarItemWithIdentifier:ident
128                                          label:NSLocalizedString(@"Advanced", @"Preferences Advanced Toolbar Item")
129                                          image:[NSImage imageNamed:NSImageNameAdvanced]];
130     }
131
132     return nil;
133 }
134
135 - (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *) toolbar
136 {
137     return [self toolbarDefaultItemIdentifiers: toolbar];
138 }
139
140 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
141 {
142     return [self toolbarAllowedItemIdentifiers: toolbar];
143 }
144
145 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
146 {
147     return [NSArray arrayWithObjects: TOOLBAR_GENERAL, /*TOOLBAR_PICTURE, */
148                                         TOOLBAR_AUDIO, TOOLBAR_ADVANCED, nil];
149 }
150
151 /* Manage the send encode to xxx.app windows and field */
152 /*Opens the app browse window*/
153 - (IBAction) browseSendToApp: (id) sender
154 {
155     NSOpenPanel * panel;
156         
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"])
163         {
164                 sendToAppDirectory = [[NSUserDefaults standardUserDefaults] stringForKey:@"LastSendToAppDirectory"];
165         }
166         else
167         {
168                 sendToAppDirectory = @"/Applications";
169         }
170     [panel beginSheetForDirectory: sendToAppDirectory file: nil types: nil
171                    modalForWindow: [self window] modalDelegate: self
172                    didEndSelector: @selector( browseSendToAppDone:returnCode:contextInfo: )
173                       contextInfo: sender]; 
174 }
175
176 - (void) browseSendToAppDone: (NSOpenPanel *) sheet
177                   returnCode: (int) returnCode contextInfo: (void *) contextInfo
178 {
179     if( returnCode == NSOKButton )
180     {
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"];
190         
191     }
192 }
193
194
195 @end
196
197 @implementation HBPreferencesController (Private)
198
199 - (void) setPrefView: (id) sender
200 {
201     NSView * view = fGeneralView;
202     if( sender )
203     {
204         NSString * identifier = [sender itemIdentifier];
205         if( [identifier isEqualToString: TOOLBAR_PICTURE] )
206             view = fPictureView;
207         else if( [identifier isEqualToString: TOOLBAR_AUDIO] )
208             view = fAudioView;
209         else if( [identifier isEqualToString: TOOLBAR_ADVANCED] )
210             view = fAdvancedView;
211         else;
212     }
213
214     NSWindow * window = [self window];
215     if( [window contentView] == view )
216         return;
217
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;
222
223     [view setHidden: YES];
224     [window setContentView: view];
225     [window setFrame: windowRect display: YES animate: YES];
226     [view setHidden: NO];
227
228     //set title label
229     if( sender )
230         [window setTitle: [sender label]];
231     else
232     {
233         NSToolbar * toolbar = [window toolbar];
234         NSString * itemIdentifier = [toolbar selectedItemIdentifier];
235         for( NSToolbarItem * item in [toolbar items] )
236             if( [[item itemIdentifier] isEqualToString: itemIdentifier] )
237             {
238                 [window setTitle: [item label]];
239                 break;
240             }
241     }
242 }
243
244 /**
245  * -[HBPreferencesController(Private) toolbarItemWithIdentifier:label:image:]
246  *
247  * Shared code for creating the NSToolbarItems for the Preferences toolbar.
248  *
249  */
250 - (NSToolbarItem *)toolbarItemWithIdentifier: (NSString *)identifier
251                                        label: (NSString *)label
252                                        image: (NSImage *)image
253 {
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];
260 }
261
262 @end