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