OSDN Git Service

aa5d64788632cd36e08091670a4ed9b48adeed6a
[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",              @"PreviewsNumber",
62         @"",                @"Drawer Size",
63         @"0.25",            @"x264CqSliderFractional",
64         @"YES",             @"AlertBuiltInPresetUpdate",
65         @"MetaX",           @"SendCompletedEncodeToApp",
66         nil]];
67 }
68
69 /**
70  * -[HBPreferencesController init]
71  *
72  * Initializes the preferences controller by loading Preferences.nib file.
73  *
74  */
75 - (id)init
76 {
77     if (self = [super initWithWindowNibName:@"Preferences"])
78     {
79         NSAssert([self window], @"[HBPreferencesController init] window outlet is not connected in Preferences.nib");
80     }
81     return self;
82 }
83
84 /**
85  * -[HBPreferencesController awakeFromNib]
86  *
87  * Called after all the outlets in the nib file have been attached. Sets up the
88  * toolbar and shows the "General" pane.
89  *
90  */
91 - (void) awakeFromNib
92 {
93     NSToolbar * toolbar = [[[NSToolbar alloc] initWithIdentifier: @"Preferences Toolbar"] autorelease];
94     [toolbar setDelegate: self];
95     [toolbar setAllowsUserCustomization: NO];
96     [toolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
97     [toolbar setSizeMode: NSToolbarSizeModeRegular];
98     [[self window] setToolbar: toolbar];
99
100     [toolbar setSelectedItemIdentifier: TOOLBAR_GENERAL];
101     [self setPrefView:nil];
102 }
103
104 - (NSToolbarItem *)toolbar: (NSToolbar *)toolbar
105      itemForItemIdentifier: (NSString *)ident
106  willBeInsertedIntoToolbar: (BOOL)flag
107 {
108     if ( [ident isEqualToString:TOOLBAR_GENERAL] )
109     {
110         return [self toolbarItemWithIdentifier:ident
111                                          label:NSLocalizedString(@"General", @"Preferences General Toolbar Item")
112                                          image:[NSImage imageNamed:NSImageNamePreferencesGeneral]];
113     }
114     else if ( [ident isEqualToString:TOOLBAR_PICTURE] )
115     {
116         return [self toolbarItemWithIdentifier:ident
117                                          label:NSLocalizedString(@"Picture", @"Preferences Picture Toolbar Item")
118                                          image:[NSImage imageNamed:@"pref-picture"]];
119     }
120     else if ( [ident isEqualToString:TOOLBAR_AUDIO] )
121     {
122         return [self toolbarItemWithIdentifier:ident
123                                          label:NSLocalizedString(@"Audio", @"Preferences Audio Toolbar Item")
124                                          image:[NSImage imageNamed:@"pref-audio"]];
125     }
126     else if ( [ident isEqualToString:TOOLBAR_ADVANCED] )
127     {
128         return [self toolbarItemWithIdentifier:ident
129                                          label:NSLocalizedString(@"Advanced", @"Preferences Advanced Toolbar Item")
130                                          image:[NSImage imageNamed:NSImageNameAdvanced]];
131     }
132
133     return nil;
134 }
135
136 - (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *) toolbar
137 {
138     return [self toolbarDefaultItemIdentifiers: toolbar];
139 }
140
141 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
142 {
143     return [self toolbarAllowedItemIdentifiers: toolbar];
144 }
145
146 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
147 {
148     return [NSArray arrayWithObjects: TOOLBAR_GENERAL, /*TOOLBAR_PICTURE, */
149                                         TOOLBAR_AUDIO, TOOLBAR_ADVANCED, nil];
150 }
151
152 /* Manage the send encode to xxx.app windows and field */
153 /*Opens the app browse window*/
154 - (IBAction) browseSendToApp: (id) sender
155 {
156     NSOpenPanel * panel;
157         
158     panel = [NSOpenPanel openPanel];
159     [panel setAllowsMultipleSelection: NO];
160     [panel setCanChooseFiles: YES];
161     [panel setCanChooseDirectories: NO ];
162     NSString * sendToAppDirectory;
163         if ([[NSUserDefaults standardUserDefaults] stringForKey:@"LastSendToAppDirectory"])
164         {
165                 sendToAppDirectory = [[NSUserDefaults standardUserDefaults] stringForKey:@"LastSendToAppDirectory"];
166         }
167         else
168         {
169                 sendToAppDirectory = @"/Applications";
170         }
171     [panel beginSheetForDirectory: sendToAppDirectory file: nil types: nil
172                    modalForWindow: [self window] modalDelegate: self
173                    didEndSelector: @selector( browseSendToAppDone:returnCode:contextInfo: )
174                       contextInfo: sender]; 
175 }
176
177 - (void) browseSendToAppDone: (NSOpenPanel *) sheet
178                   returnCode: (int) returnCode contextInfo: (void *) contextInfo
179 {
180     if( returnCode == NSOKButton )
181     {
182         NSString *sendToAppPath = [[sheet filenames] objectAtIndex: 0];
183         NSString *sendToAppDirectory = [sendToAppPath stringByDeletingLastPathComponent];
184         [[NSUserDefaults standardUserDefaults] setObject:sendToAppDirectory forKey:@"LastSendToAppDirectory"];
185         [sheet orderOut: self];
186         NSString *sendToAppName;
187         sendToAppName = [[sendToAppPath lastPathComponent] stringByDeletingPathExtension];
188         /* we set the name of the app to send to in the display field */
189         [fSendEncodeToAppField setStringValue:sendToAppName];
190         [[NSUserDefaults standardUserDefaults] setObject:[fSendEncodeToAppField stringValue] forKey:@"SendCompletedEncodeToApp"];
191         
192     }
193 }
194
195
196 @end
197
198 @implementation HBPreferencesController (Private)
199
200 - (void) setPrefView: (id) sender
201 {
202     NSView * view = fGeneralView;
203     if( sender )
204     {
205         NSString * identifier = [sender itemIdentifier];
206         if( [identifier isEqualToString: TOOLBAR_PICTURE] )
207             view = fPictureView;
208         else if( [identifier isEqualToString: TOOLBAR_AUDIO] )
209             view = fAudioView;
210         else if( [identifier isEqualToString: TOOLBAR_ADVANCED] )
211             view = fAdvancedView;
212         else;
213     }
214
215     NSWindow * window = [self window];
216     if( [window contentView] == view )
217         return;
218
219     NSRect windowRect = [window frame];
220     CGFloat difference = ( [view frame].size.height - [[window contentView] frame].size.height ) * [window userSpaceScaleFactor];
221     windowRect.origin.y -= difference;
222     windowRect.size.height += difference;
223
224     [view setHidden: YES];
225     [window setContentView: view];
226     [window setFrame: windowRect display: YES animate: YES];
227     [view setHidden: NO];
228
229     //set title label
230     if( sender )
231         [window setTitle: [sender label]];
232     else
233     {
234         NSToolbar * toolbar = [window toolbar];
235         NSString * itemIdentifier = [toolbar selectedItemIdentifier];
236         for( NSToolbarItem * item in [toolbar items] )
237             if( [[item itemIdentifier] isEqualToString: itemIdentifier] )
238             {
239                 [window setTitle: [item label]];
240                 break;
241             }
242     }
243 }
244
245 /**
246  * -[HBPreferencesController(Private) toolbarItemWithIdentifier:label:image:]
247  *
248  * Shared code for creating the NSToolbarItems for the Preferences toolbar.
249  *
250  */
251 - (NSToolbarItem *)toolbarItemWithIdentifier: (NSString *)identifier
252                                        label: (NSString *)label
253                                        image: (NSImage *)image
254 {
255     NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:identifier];
256     [item setLabel:label];
257     [item setImage:image];
258     [item setAction:@selector(setPrefView:)];
259     [item setAutovalidates:NO];
260     return [item autorelease];
261 }
262
263 @end