OSDN Git Service

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