OSDN Git Service

MacGui: New preferences window. We need some new icons for the toolbar.
[handbrake-jp/handbrake-jp-git.git] / macosx / Controller.mm
1 /* $Id: Controller.mm,v 1.79 2005/11/04 19:41:32 titer Exp $
2
3    This file is part of the HandBrake source code.
4    Homepage: <http://handbrake.m0k.org/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include "Controller.h"
8 #include "a52dec/a52.h"
9 #import "HBOutputPanelController.h"
10 #import "HBPreferencesController.h"
11 /* Added to integrate scanning into HBController */
12 #include <IOKit/IOKitLib.h>
13 #include <IOKit/storage/IOMedia.h>
14 #include <IOKit/storage/IODVDMedia.h>
15 #include "HBDVDDetector.h"
16 #include "dvdread/dvd_reader.h"
17
18 #define _(a) NSLocalizedString(a,NULL)
19
20 static int FormatSettings[4][10] =
21   { { HB_MUX_MP4 | HB_VCODEC_FFMPEG | HB_ACODEC_FAAC,
22           HB_MUX_MP4 | HB_VCODEC_X264   | HB_ACODEC_FAAC,
23           0,
24           0},
25     { HB_MUX_AVI | HB_VCODEC_FFMPEG | HB_ACODEC_LAME,
26           HB_MUX_AVI | HB_VCODEC_FFMPEG | HB_ACODEC_AC3,
27           HB_MUX_AVI | HB_VCODEC_X264   | HB_ACODEC_LAME,
28           HB_MUX_AVI | HB_VCODEC_X264   | HB_ACODEC_AC3},
29     { HB_MUX_OGM | HB_VCODEC_FFMPEG | HB_ACODEC_VORBIS,
30           HB_MUX_OGM | HB_VCODEC_FFMPEG | HB_ACODEC_LAME,
31           0,
32           0 },
33     { HB_MUX_MKV | HB_VCODEC_FFMPEG | HB_ACODEC_FAAC,
34           HB_MUX_MKV | HB_VCODEC_FFMPEG | HB_ACODEC_AC3,
35           HB_MUX_MKV | HB_VCODEC_FFMPEG | HB_ACODEC_LAME,
36           HB_MUX_MKV | HB_VCODEC_FFMPEG | HB_ACODEC_VORBIS,
37           HB_MUX_MKV | HB_VCODEC_X264   | HB_ACODEC_FAAC,
38           HB_MUX_MKV | HB_VCODEC_X264   | HB_ACODEC_AC3,
39           HB_MUX_MKV | HB_VCODEC_X264   | HB_ACODEC_LAME,
40           HB_MUX_MKV | HB_VCODEC_X264   | HB_ACODEC_VORBIS,
41           0,
42           0 } };
43
44
45 /* We setup the toolbar values here */
46 static NSString *        ToggleDrawerIdentifier             = @"Toggle Drawer Item Identifier";
47 static NSString *        StartEncodingIdentifier            = @"Start Encoding Item Identifier";
48 static NSString *        PauseEncodingIdentifier            = @"Pause Encoding Item Identifier";
49 static NSString *        ShowQueueIdentifier                = @"Show Queue Item Identifier";
50 static NSString *        AddToQueueIdentifier               = @"Add to Queue Item Identifier";
51 static NSString *        ShowActivityIdentifier             = @"Debug Output Item Identifier";
52 static NSString *        ChooseSourceIdentifier             = @"Choose Source Item Identifier";
53
54 /**
55  * Returns the number of jobs groups in the queue.
56  * @param h Handle to hb_handle_t.
57  * @return Number of job groups.
58  */
59 static int hb_group_count(hb_handle_t * h)    
60 {
61         hb_job_t * job;
62         int count = 0;
63         int index = 0;
64         while( ( job = hb_job( h, index++ ) ) )
65         {
66                 if (job->sequence_id == 0)
67                         count++;
68         }
69         return count;
70 }
71
72 /*******************************
73  * HBController implementation *
74  *******************************/
75 @implementation HBController
76
77 - init
78 {
79     self = [super init];
80     [HBPreferencesController registerUserDefaults];
81     fHandle = NULL;
82     outputPanel = [[HBOutputPanelController alloc] init];
83     fPictureController = [[PictureController alloc] initWithDelegate:self];
84     fQueueController = [[HBQueueController alloc] init];
85     fAdvancedOptions = [[HBAdvancedController alloc] init];
86     fPreferencesController = [[HBPreferencesController alloc] init];
87     return self;
88 }
89
90 - (void) applicationDidFinishLaunching: (NSNotification *) notification
91 {
92     int    build;
93     char * version;
94
95     // Init libhb
96         int debugLevel = [[NSUserDefaults standardUserDefaults] boolForKey:@"ShowVerboseOutput"] ? HB_DEBUG_ALL : HB_DEBUG_NONE;
97     fHandle = hb_init(debugLevel, [[NSUserDefaults standardUserDefaults] boolForKey:@"CheckForUpdates"]);
98
99         // Set the Growl Delegate
100     [GrowlApplicationBridge setGrowlDelegate: self];    
101     /* Init others controllers */
102     [fPictureController SetHandle: fHandle];
103     [fQueueController   setHandle: fHandle];
104         
105     fChapterTitlesDelegate = [[ChapterTitles alloc] init];
106     [fChapterTable setDataSource:fChapterTitlesDelegate];
107
108      /* Call UpdateUI every 1/2 sec */
109     [[NSRunLoop currentRunLoop] addTimer: [NSTimer
110         scheduledTimerWithTimeInterval: 0.5 target: self
111         selector: @selector( updateUI: ) userInfo: NULL repeats: YES]
112         forMode: NSEventTrackingRunLoopMode];
113
114     if( ( build = hb_check_update( fHandle, &version ) ) > -1 )
115     {
116         /* Update available - tell the user */
117         
118         NSBeginInformationalAlertSheet( _( @"Update is available" ),
119             _( @"Go get it!" ), _( @"Discard" ), NULL, fWindow, self,
120             @selector( updateAlertDone:returnCode:contextInfo: ),
121             NULL, NULL, [NSString stringWithFormat:
122             _( @"HandBrake %s (build %d) is now available for download." ),
123             version, build] );
124         return;
125
126     }
127
128     // Open debug output window now if it was visible when HB was closed
129     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"OutputPanelIsOpen"])
130         [self showDebugOutputPanel:nil];
131
132     // Open queue window now if it was visible when HB was closed
133     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"QueueWindowIsOpen"])
134         [self showQueueWindow:nil];
135
136         [self openMainWindow:nil];
137         
138     /* Show scan panel ASAP */
139     [self performSelectorOnMainThread: @selector(showScanPanel:)
140         withObject: NULL waitUntilDone: NO];
141 }
142
143 - (NSApplicationTerminateReply) applicationShouldTerminate:
144     (NSApplication *) app
145 {
146     hb_state_t s;
147     hb_get_state2( fHandle, &s );
148     if ( s.state ==  HB_STATE_WORKING )    
149     {
150         [self Cancel: NULL];
151         return NSTerminateCancel;
152     }    
153     return NSTerminateNow;
154 }
155
156 - (void)applicationWillTerminate:(NSNotification *)aNotification
157 {
158         [outputPanel release];
159         [fQueueController release];
160         hb_close(&fHandle);
161 }
162
163
164 - (void) awakeFromNib
165 {
166     [fWindow center];
167     [fWindow setExcludedFromWindowsMenu:YES];
168     [fAdvancedOptions setView:fAdvancedView];
169
170     /* Initialize currentScanCount so HB can use it to
171                 evaluate successive scans */
172         currentScanCount = 0;
173         
174     /* Init UserPresets .plist */
175         [self loadPresets];
176                 
177         fRipIndicatorShown = NO;  // initially out of view in the nib
178         
179         /* Show/Dont Show Presets drawer upon launch based
180                 on user preference DefaultPresetsDrawerShow*/
181         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultPresetsDrawerShow"] > 0)
182         {
183                 [fPresetDrawer open];
184         }
185         
186         
187     /* Destination box*/
188     [fDstFormatPopUp removeAllItems];
189     [fDstFormatPopUp addItemWithTitle: _( @"MP4 file" )];
190     [fDstFormatPopUp addItemWithTitle: _( @"AVI file" )];
191     [fDstFormatPopUp addItemWithTitle: _( @"OGM file" )];
192         [fDstFormatPopUp addItemWithTitle: _( @"MKV file" )];
193     [fDstFormatPopUp selectItemAtIndex: 0];
194         
195     [self formatPopUpChanged: NULL];
196     
197         /* We enable the create chapters checkbox here since we are .mp4 */     
198         [fCreateChapterMarkers setEnabled: YES];
199         if ([fDstFormatPopUp indexOfSelectedItem] == 0 && [[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultChapterMarkers"] > 0)
200         {
201                 [fCreateChapterMarkers setState: NSOnState];
202         }
203         
204         
205         
206         
207     [fDstFile2Field setStringValue: [NSString stringWithFormat:
208         @"%@/Desktop/Movie.mp4", NSHomeDirectory()]];
209         
210     /* Video encoder */
211     [fVidEncoderPopUp removeAllItems];
212     [fVidEncoderPopUp addItemWithTitle: @"FFmpeg"];
213     [fVidEncoderPopUp addItemWithTitle: @"XviD"];
214         
215     
216         
217     /* Video quality */
218     [fVidTargetSizeField setIntValue: 700];
219         [fVidBitrateField    setIntValue: 1000];
220         
221     [fVidQualityMatrix   selectCell: fVidBitrateCell];
222     [self videoMatrixChanged: NULL];
223         
224     /* Video framerate */
225     [fVidRatePopUp removeAllItems];
226         [fVidRatePopUp addItemWithTitle: _( @"Same as source" )];
227     for( int i = 0; i < hb_video_rates_count; i++ )
228     {
229         if ([[NSString stringWithCString: hb_video_rates[i].string] isEqualToString: [NSString stringWithFormat: @"%.3f",23.976]])
230                 {
231                         [fVidRatePopUp addItemWithTitle:[NSString stringWithFormat: @"%@%@",
232                                 [NSString stringWithCString: hb_video_rates[i].string], @" (NTSC Film)"]];
233                 }
234                 else if ([[NSString stringWithCString: hb_video_rates[i].string] isEqualToString: [NSString stringWithFormat: @"%d",25]])
235                 {
236                         [fVidRatePopUp addItemWithTitle:[NSString stringWithFormat: @"%@%@",
237                                 [NSString stringWithCString: hb_video_rates[i].string], @" (PAL Film/Video)"]];
238                 }
239                 else if ([[NSString stringWithCString: hb_video_rates[i].string] isEqualToString: [NSString stringWithFormat: @"%.2f",29.97]])
240                 {
241                         [fVidRatePopUp addItemWithTitle:[NSString stringWithFormat: @"%@%@",
242                                 [NSString stringWithCString: hb_video_rates[i].string], @" (NTSC Video)"]];
243                 }
244                 else
245                 {
246                         [fVidRatePopUp addItemWithTitle:
247                                 [NSString stringWithCString: hb_video_rates[i].string]];
248                 }
249     }
250     [fVidRatePopUp selectItemAtIndex: 0];
251         
252         /* Picture Settings */
253         [fPicLabelPAROutputX setStringValue: @""];
254         [fPicSettingPARWidth setStringValue: @""];
255         [fPicSettingPARHeight setStringValue:  @""];
256         
257         /* Set Auto Crop to On at launch */
258     [fPictureController setAutoCrop:YES];
259         
260         /* Audio bitrate */
261     [fAudBitratePopUp removeAllItems];
262     for( int i = 0; i < hb_audio_bitrates_count; i++ )
263     {
264         [fAudBitratePopUp addItemWithTitle:
265                                 [NSString stringWithCString: hb_audio_bitrates[i].string]];
266
267     }
268     [fAudBitratePopUp selectItemAtIndex: hb_audio_bitrates_default];
269         
270     /* Audio samplerate */
271     [fAudRatePopUp removeAllItems];
272     for( int i = 0; i < hb_audio_rates_count; i++ )
273     {
274         [fAudRatePopUp addItemWithTitle:
275             [NSString stringWithCString: hb_audio_rates[i].string]];
276     }
277     [fAudRatePopUp selectItemAtIndex: hb_audio_rates_default];
278         
279     /* Bottom */
280     [fStatusField setStringValue: @""];
281         
282     [self enableUI: NO];
283         [self setupToolbar];
284         
285         [fPresetsActionButton setMenu:fPresetsActionMenu];
286         
287         /* We disable the Turbo 1st pass checkbox since we are not x264 */
288         [fVidTurboPassCheck setEnabled: NO];
289         [fVidTurboPassCheck setState: NSOffState];
290         
291         
292         /* lets get our default prefs here */
293         [self getDefaultPresets: NULL];
294         /* lets initialize the current successful scancount here to 0 */
295         currentSuccessfulScanCount = 0;
296     
297 }
298
299 - (void) loadPresets {
300         /* We declare the default NSFileManager into fileManager */
301         NSFileManager * fileManager = [NSFileManager defaultManager];
302         /* we set the files and support paths here */
303         AppSupportDirectory = @"~/Library/Application Support/HandBrake";
304     AppSupportDirectory = [AppSupportDirectory stringByExpandingTildeInPath];
305     //UserPresetsFile = @"~/Library/Application Support/HandBrake/UserPresets.plist";
306     //UserPresetsFile = [UserPresetsFile stringByExpandingTildeInPath];
307         /* We check for the app support directory for handbrake */
308         if ([fileManager fileExistsAtPath:AppSupportDirectory] == 0) 
309         {
310                 // If it doesnt exist yet, we create it here 
311                 [fileManager createDirectoryAtPath:AppSupportDirectory attributes:nil];
312         }
313         /* We check for the presets.plist here */
314         if ([fileManager fileExistsAtPath:UserPresetsFile] == 0) 
315         {
316                 [fileManager createFileAtPath:UserPresetsFile contents:nil attributes:nil];
317         }
318         UserPresetsFile = @"~/Library/Application Support/HandBrake/UserPresets.plist";
319         UserPresetsFile = [[UserPresetsFile stringByExpandingTildeInPath]retain];
320         
321         UserPresets = [[NSMutableArray alloc] initWithContentsOfFile:UserPresetsFile];
322         if (nil == UserPresets) 
323         {
324                 UserPresets = [[NSMutableArray alloc] init];
325                 [self addFactoryPresets:NULL];
326         }
327         
328 }
329
330 // ============================================================
331 // NSToolbar Related Methods
332 // ============================================================
333
334 - (void) setupToolbar {
335     toolbar = [[[NSToolbar alloc] initWithIdentifier: @"HandBrake Toolbar"] autorelease];
336     
337     [toolbar setAllowsUserCustomization: YES];
338     [toolbar setAutosavesConfiguration: YES];
339     [toolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
340     
341     [toolbar setDelegate: self];
342     
343     [fWindow setToolbar: toolbar];
344 }
345
346 - (NSToolbarItem *) toolbar: (NSToolbar *)toolbar itemForItemIdentifier:
347     (NSString *) itemIdent willBeInsertedIntoToolbar:(BOOL) willBeInserted {
348     NSToolbarItem * item = [[NSToolbarItem alloc] initWithItemIdentifier: itemIdent];
349     
350     if ([itemIdent isEqualToString: ToggleDrawerIdentifier])
351     {
352         [item setLabel: @"Toggle Presets"];
353         [item setPaletteLabel: @"Toggler Presets"];
354         [item setToolTip: @"Open/Close Preset Drawer"];
355         [item setImage: [NSImage imageNamed: @"Drawer-List2"]];
356         [item setTarget: self];
357         [item setAction: @selector(toggleDrawer:)];
358         [item setAutovalidates: NO];
359     }
360     else if ([itemIdent isEqualToString: StartEncodingIdentifier])
361     {
362         [item setLabel: @"Start"];
363         [item setPaletteLabel: @"Start Encoding"];
364         [item setToolTip: @"Start Encoding"];
365         [item setImage: [NSImage imageNamed: @"Play"]];
366         [item setTarget: self];
367         [item setAction: @selector(Rip:)];
368     }
369     else if ([itemIdent isEqualToString: ShowQueueIdentifier])
370     {
371         [item setLabel: @"Show Queue"];
372         [item setPaletteLabel: @"Show Queue"];
373         [item setToolTip: @"Show Queue"];
374         [item setImage: [NSImage imageNamed: @"Brushed Window"]];
375         [item setTarget: self];
376         [item setAction: @selector(showQueueWindow:)];
377         [item setAutovalidates: NO];
378     }
379     else if ([itemIdent isEqualToString: AddToQueueIdentifier])
380     {
381         [item setLabel: @"Add to Queue"];
382         [item setPaletteLabel: @"Add to Queue"];
383         [item setToolTip: @"Add to Queue"];
384         [item setImage: [NSImage imageNamed: @"Add"]];
385         [item setTarget: self];
386         [item setAction: @selector(addToQueue:)];
387     }
388     else if ([itemIdent isEqualToString: PauseEncodingIdentifier])
389     {
390         [item setLabel: @"Pause"];
391         [item setPaletteLabel: @"Pause Encoding"];
392         [item setToolTip: @"Pause Encoding"];
393         [item setImage: [NSImage imageNamed: @"Pause"]];
394         [item setTarget: self];
395         [item setAction: @selector(Pause:)];
396     }
397     else if ([itemIdent isEqualToString: ShowActivityIdentifier]) {
398         [item setLabel: @"Activity Window"];
399         [item setPaletteLabel: @"Show Activity Window"];
400         [item setToolTip: @"Show Activity Window"];
401         [item setImage: [NSImage imageNamed: @"Terminal"]];
402         [item setTarget: self];
403         [item setAction: @selector(showDebugOutputPanel:)];
404         [item setAutovalidates: NO];
405     }
406     else if ([itemIdent isEqualToString: ChooseSourceIdentifier])
407     {
408         [item setLabel: @"Source"];
409         [item setPaletteLabel: @"Source"];
410         [item setToolTip: @"Choose Video Source"];
411         [item setImage: [NSImage imageNamed: @"Disc"]];
412         [item setTarget: self];
413         [item setAction: @selector(showScanPanel:)];
414         [item setAutovalidates: NO];
415     }
416     else
417     {
418         [item release];
419         return nil;
420     }
421
422     return item;
423 }
424
425 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
426 {
427     return [NSArray arrayWithObjects: ChooseSourceIdentifier, NSToolbarSeparatorItemIdentifier, StartEncodingIdentifier,
428         PauseEncodingIdentifier, AddToQueueIdentifier, ShowQueueIdentifier, NSToolbarFlexibleSpaceItemIdentifier, 
429                 NSToolbarSpaceItemIdentifier, ShowActivityIdentifier, ToggleDrawerIdentifier, nil];
430 }
431
432 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
433 {
434     return [NSArray arrayWithObjects:  StartEncodingIdentifier, PauseEncodingIdentifier, AddToQueueIdentifier,
435         ChooseSourceIdentifier, ShowQueueIdentifier, ShowActivityIdentifier, ToggleDrawerIdentifier,
436         NSToolbarCustomizeToolbarItemIdentifier, NSToolbarFlexibleSpaceItemIdentifier, NSToolbarSpaceItemIdentifier,
437         NSToolbarSpaceItemIdentifier, NSToolbarSeparatorItemIdentifier, nil];
438 }
439
440 - (BOOL) validateToolbarItem: (NSToolbarItem *) toolbarItem
441 {
442     NSString * ident = [toolbarItem itemIdentifier];
443         
444     if (fHandle)
445     {
446         hb_state_t s;
447         hb_get_state2( fHandle, &s );
448         
449         if (s.state == HB_STATE_WORKING || s.state == HB_STATE_MUXING)
450         {
451             if ([ident isEqualToString: StartEncodingIdentifier])
452             {
453                 [toolbarItem setImage: [NSImage imageNamed: @"Stop"]];
454                 [toolbarItem setLabel: @"Cancel"];
455                 [toolbarItem setPaletteLabel: @"Cancel"];
456                 [toolbarItem setToolTip: @"Cancel Encoding"];
457                 return YES;
458             }
459             if ([ident isEqualToString: PauseEncodingIdentifier])
460             {
461                 [toolbarItem setImage: [NSImage imageNamed: @"Pause"]];
462                 [toolbarItem setLabel: @"Pause"];
463                 [toolbarItem setPaletteLabel: @"Pause Encoding"];
464                 [toolbarItem setToolTip: @"Pause Encoding"];
465                 return YES;
466             }
467             if (SuccessfulScan)
468                 if ([ident isEqualToString: AddToQueueIdentifier])
469                     return YES;
470         }
471         else if (s.state == HB_STATE_PAUSED)
472         {
473             if ([ident isEqualToString: PauseEncodingIdentifier])
474             {
475                 [toolbarItem setImage: [NSImage imageNamed: @"Play"]];
476                 [toolbarItem setLabel: @"Resume"];
477                 [toolbarItem setPaletteLabel: @"Resume Encoding"];
478                 [toolbarItem setToolTip: @"Resume Encoding"];
479                 return YES;
480             }
481             if ([ident isEqualToString: StartEncodingIdentifier])
482                 return YES;
483             if ([ident isEqualToString: AddToQueueIdentifier])
484                 return YES;
485         }
486         else if (s.state == HB_STATE_SCANNING)
487             return NO;
488         else if (s.state == HB_STATE_WORKDONE || s.state == HB_STATE_SCANDONE || SuccessfulScan)
489         {
490             if ([ident isEqualToString: StartEncodingIdentifier])
491             {
492                 [toolbarItem setImage: [NSImage imageNamed: @"Play"]];
493                 [toolbarItem setLabel: @"Start"];
494                 [toolbarItem setPaletteLabel: @"Start Encoding"];
495                 [toolbarItem setToolTip: @"Start Encoding"];
496                 return YES;
497             }
498             if ([ident isEqualToString: AddToQueueIdentifier])
499                 return YES;
500         }
501
502     }
503     
504     if ([ident isEqualToString: ShowQueueIdentifier])
505         return YES;
506     if ([ident isEqualToString: ToggleDrawerIdentifier])
507         return YES;
508     if ([ident isEqualToString: ChooseSourceIdentifier])
509         return YES;
510     if ([ident isEqualToString: ShowActivityIdentifier])
511         return YES;
512     
513     return NO;
514 }
515
516 - (BOOL) validateMenuItem: (NSMenuItem *) menuItem
517 {
518     SEL action = [menuItem action];
519     
520     hb_state_t s;
521     hb_get_state2( fHandle, &s );
522     
523     if (fHandle)
524     {
525         if (action == @selector(addToQueue:) || action == @selector(showPicturePanel:) || action == @selector(showAddPresetPanel:))
526             return SuccessfulScan && [fWindow attachedSheet] == nil;
527         
528         if (action == @selector(showScanPanel:))
529         {
530             if (s.state == HB_STATE_SCANNING)
531                 return NO;
532             else
533                 return [fWindow attachedSheet] == nil;
534         }
535         if (action == @selector(selectDefaultPreset:))
536             return [tableView selectedRow] >= 0 && [fWindow attachedSheet] == nil;
537         if (action == @selector(Pause:))
538         {
539             if (s.state == HB_STATE_WORKING)
540             {
541                 if(![[menuItem title] isEqualToString:@"Pause Encoding"])
542                     [menuItem setTitle:@"Pause Encoding"];
543                 return YES;
544             }
545             else if (s.state == HB_STATE_PAUSED)
546             {
547                 if(![[menuItem title] isEqualToString:@"Resume Encoding"])
548                     [menuItem setTitle:@"Resume Encoding"];
549                 return YES;
550             }
551             else
552                 return NO;
553         }
554         if (action == @selector(Rip:))
555             if (s.state == HB_STATE_WORKING || s.state == HB_STATE_MUXING || s.state == HB_STATE_PAUSED)
556             {
557                 if(![[menuItem title] isEqualToString:@"Stop Encoding"])
558                     [menuItem setTitle:@"Stop Encoding"];
559                 return YES;
560             }
561             else if (SuccessfulScan)
562             {
563                 if(![[menuItem title] isEqualToString:@"Start Encoding"])
564                     [menuItem setTitle:@"Start Encoding"];
565                 return [fWindow attachedSheet] == nil;
566             }
567             else
568                 return NO;
569         }
570     
571     return YES;
572 }
573
574
575 // register a test notification and make
576 // it enabled by default
577 #define SERVICE_NAME @"Encode Done"
578 - (NSDictionary *)registrationDictionaryForGrowl 
579
580     NSDictionary *registrationDictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
581     [NSArray arrayWithObjects:SERVICE_NAME,nil], GROWL_NOTIFICATIONS_ALL, 
582     [NSArray arrayWithObjects:SERVICE_NAME,nil], GROWL_NOTIFICATIONS_DEFAULT, 
583     nil]; 
584
585     return registrationDictionary; 
586
587
588 - (void) TranslateStrings
589 {
590     [fSrcTitleField     setStringValue: _( @"Title:" )];
591     [fSrcChapterField   setStringValue: _( @"Chapters:" )];
592     [fSrcChapterToField setStringValue: _( @"to" )];
593     [fSrcDuration1Field setStringValue: _( @"Duration:" )];
594
595     [fDstFormatField    setStringValue: _( @"Format:" )];
596     [fDstCodecsField    setStringValue: _( @"Codecs:" )];
597     [fDstFile1Field     setStringValue: _( @"File:" )];
598     [fDstBrowseButton   setTitle:       _( @"Browse" )];
599
600     [fVidRateField      setStringValue: _( @"Framerate (fps):" )];
601     [fVidEncoderField   setStringValue: _( @"Encoder:" )];
602     [fVidQualityField   setStringValue: _( @"Quality:" )];
603 }
604
605 /***********************************************************************
606  * UpdateDockIcon
607  ***********************************************************************
608  * Shows a progression bar on the dock icon, filled according to
609  * 'progress' (0.0 <= progress <= 1.0).
610  * Called with progress < 0.0 or progress > 1.0, restores the original
611  * icon.
612  **********************************************************************/
613 - (void) UpdateDockIcon: (float) progress
614 {
615     NSImage * icon;
616     NSData * tiff;
617     NSBitmapImageRep * bmp;
618     uint32_t * pen;
619     uint32_t black = htonl( 0x000000FF );
620     uint32_t red   = htonl( 0xFF0000FF );
621     uint32_t white = htonl( 0xFFFFFFFF );
622     int row_start, row_end;
623     int i, j;
624
625     /* Get application original icon */
626     icon = [NSImage imageNamed: @"NSApplicationIcon"];
627
628     if( progress < 0.0 || progress > 1.0 )
629     {
630         [NSApp setApplicationIconImage: icon];
631         return;
632     }
633
634     /* Get it in a raw bitmap form */
635     tiff = [icon TIFFRepresentationUsingCompression:
636             NSTIFFCompressionNone factor: 1.0];
637     bmp = [NSBitmapImageRep imageRepWithData: tiff];
638     
639     /* Draw the progression bar */
640     /* It's pretty simple (ugly?) now, but I'm no designer */
641
642     row_start = 3 * (int) [bmp size].height / 4;
643     row_end   = 7 * (int) [bmp size].height / 8;
644
645     for( i = row_start; i < row_start + 2; i++ )
646     {
647         pen = (uint32_t *) ( [bmp bitmapData] + i * [bmp bytesPerRow] );
648         for( j = 0; j < (int) [bmp size].width; j++ )
649         {
650             pen[j] = black;
651         }
652     }
653     for( i = row_start + 2; i < row_end - 2; i++ )
654     {
655         pen = (uint32_t *) ( [bmp bitmapData] + i * [bmp bytesPerRow] );
656         pen[0] = black;
657         pen[1] = black;
658         for( j = 2; j < (int) [bmp size].width - 2; j++ )
659         {
660             if( j < 2 + (int) ( ( [bmp size].width - 4.0 ) * progress ) )
661             {
662                 pen[j] = red;
663             }
664             else
665             {
666                 pen[j] = white;
667             }
668         }
669         pen[j]   = black;
670         pen[j+1] = black;
671     }
672     for( i = row_end - 2; i < row_end; i++ )
673     {
674         pen = (uint32_t *) ( [bmp bitmapData] + i * [bmp bytesPerRow] );
675         for( j = 0; j < (int) [bmp size].width; j++ )
676         {
677             pen[j] = black;
678         }
679     }
680
681     /* Now update the dock icon */
682     tiff = [bmp TIFFRepresentationUsingCompression:
683             NSTIFFCompressionNone factor: 1.0];
684     icon = [[NSImage alloc] initWithData: tiff];
685     [NSApp setApplicationIconImage: icon];
686     [icon release];
687 }
688
689 - (void) updateUI: (NSTimer *) timer
690 {
691
692     hb_list_t  * list;
693     list = hb_get_titles( fHandle );    
694     /* check to see if there has been a new scan done
695         this bypasses the constraints of HB_STATE_WORKING
696         not allowing setting a newly scanned source */
697         int checkScanCount = hb_get_scancount( fHandle );
698         if (checkScanCount > currentScanCount)
699         {
700                 
701                 currentScanCount = checkScanCount;
702                 //[fScanController Cancel: NULL];
703                 [fScanIndicator setIndeterminate: NO];
704                 [fScanIndicator setDoubleValue: 0.0];
705                 [fScanIndicator setHidden: YES];
706
707                 [self showNewScan: NULL];
708         }
709         
710     BOOL jobGroups = [[NSUserDefaults standardUserDefaults] boolForKey:@"QueueShowsJobsAsGroups"];
711         
712     hb_state_t s;
713     hb_get_state( fHandle, &s );
714         
715         
716     switch( s.state )
717     {
718         case HB_STATE_IDLE:
719                 break;
720 #define p s.param.scanning                      
721         case HB_STATE_SCANNING:
722                 {
723             [fSrcDVD2Field setStringValue: [NSString stringWithFormat:
724                 _( @"Scanning title %d of %d..." ),
725                 p.title_cur, p.title_count]];
726             [fScanIndicator setIndeterminate: NO];
727                         [fScanIndicator setDoubleValue: 100.0 * ( p.title_cur - 1 ) /
728                 p.title_count];
729             break;
730                 }
731 #undef p
732         
733 #define p s.param.scandone
734         case HB_STATE_SCANDONE:
735         {
736                         
737                         [fScanIndicator setIndeterminate: NO];
738             [fScanIndicator setDoubleValue: 0.0];
739                         [fScanIndicator setHidden: YES];
740                         [self showNewScan: NULL];
741             [toolbar validateVisibleItems];
742                         break;
743         }
744 #undef p
745                         
746 #define p s.param.working
747         case HB_STATE_WORKING:
748         {
749             float progress_total;
750             NSMutableString * string;
751                         /* Currently, p.job_cur and p.job_count get screwed up when adding
752                                 jobs during encoding, if they cannot be fixed in libhb, will implement a
753                                 nasty but working cocoa solution */
754                         /* Update text field */
755                         string = [NSMutableString stringWithFormat: _( @"Encoding: task %d of %d, %.2f %%" ), p.job_cur, p.job_count, 100.0 * p.progress];
756             
757                         if( p.seconds > -1 )
758             {
759                 [string appendFormat:
760                     _( @" (%.2f fps, avg %.2f fps, ETA %02dh%02dm%02ds)" ),
761                     p.rate_cur, p.rate_avg, p.hours, p.minutes, p.seconds];
762             }
763             [fStatusField setStringValue: string];
764                         
765             /* Update slider */
766                         progress_total = ( p.progress + p.job_cur - 1 ) / p.job_count;
767             [fRipIndicator setIndeterminate: NO];
768             [fRipIndicator setDoubleValue: 100.0 * progress_total];
769                         
770             // If progress bar hasn't been revealed at the bottom of the window, do
771             // that now. This code used to be in _Rip. I moved it to here to handle
772             // the case where hb_start is called by HBQueueController and not from
773             // HBController.
774             if (!fRipIndicatorShown)
775             {
776                 NSRect frame = [fWindow frame];
777                 if (frame.size.width <= 591)
778                     frame.size.width = 591;
779                 frame.size.height += 36;
780                 frame.origin.y -= 36;
781                 [fWindow setFrame:frame display:YES animate:YES];
782                 fRipIndicatorShown = YES;
783             }
784
785             /* Update dock icon */
786             [self UpdateDockIcon: progress_total];
787                         
788             // Has current job changed? That means the queue has probably changed as
789                         // well so update it
790             if (fLastKnownCurrentJob != hb_current_job(fHandle))
791             {
792                 fLastKnownCurrentJob = hb_current_job(fHandle);
793                 [fQueueController updateQueueUI];
794             }
795             [fQueueController updateCurrentJobUI];
796             
797             break;
798         }
799 #undef p
800                         
801 #define p s.param.muxing
802         case HB_STATE_MUXING:
803         {
804             NSMutableString * string;
805                         
806             /* Update text field */
807             string = [NSMutableString stringWithFormat:
808                 _( @"Muxing..." )];
809             [fStatusField setStringValue: string];
810                         
811             /* Update slider */
812             [fRipIndicator setIndeterminate: YES];
813             [fRipIndicator startAnimation: nil];
814                         
815             /* Update dock icon */
816             [self UpdateDockIcon: 1.0];
817                         
818                         // Pass along the info to HBQueueController
819             [fQueueController updateCurrentJobUI];
820                         
821             break;
822         }
823 #undef p
824                         
825         case HB_STATE_PAUSED:
826                     [fStatusField setStringValue: _( @"Paused" )];
827             
828                         // Pass along the info to HBQueueController
829             [fQueueController updateCurrentJobUI];
830
831             break;
832                         
833         case HB_STATE_WORKDONE:
834         {
835             [fStatusField setStringValue: _( @"Done." )];
836             [fRipIndicator setIndeterminate: NO];
837             [fRipIndicator setDoubleValue: 0.0];
838             [toolbar validateVisibleItems];
839
840             /* Restore dock icon */
841             [self UpdateDockIcon: -1.0];
842
843             if (jobGroups)
844             {
845                 // Delete all remaining scans of this job
846                 hb_job_t * job;
847                 while( ( job = hb_job( fHandle, 0 ) ) && (job->sequence_id != 0) )
848                     hb_rem( fHandle, job );
849             }
850
851             // Start processing back up if jobs still left in queue
852             if (hb_count(fHandle) > 0)
853             {
854                 hb_start(fHandle);
855                 break;
856             }
857                         
858             if (fRipIndicatorShown)
859             {
860                 NSRect frame = [fWindow frame];
861                 if (frame.size.width <= 591)
862                                     frame.size.width = 591;
863                 frame.size.height += -36;
864                 frame.origin.y -= -36;
865                 [fWindow setFrame:frame display:YES animate:YES];
866                                 fRipIndicatorShown = NO;
867                         }
868                         
869             // Queue has been modified so update the UI
870                         fLastKnownCurrentJob = nil;
871             [fQueueController updateQueueUI];
872             [fQueueController updateCurrentJobUI];
873                         
874             /* Check to see if the encode state has not been cancelled
875                                 to determine if we should check for encode done notifications */
876                         if (fEncodeState != 2)                  {
877                                 /* If Growl Notification or Window and Growl has been selected */
878                                 if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Growl Notification"] || 
879                                         [[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Alert Window And Growl"])
880                 {
881                                         /*Growl Notification*/
882                                         [self showGrowlDoneNotification: NULL];
883                 }
884                 /* If Alert Window or Window and Growl has been selected */
885                                 if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Alert Window"] || 
886                                         [[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Alert Window And Growl"])
887                 {
888                                         /*On Screen Notification*/
889                                         int status;
890                                         NSBeep();
891                                         status = NSRunAlertPanel(@"Put down that cocktail...",@"your HandBrake encode is done!", @"OK", nil, nil);
892                                         [NSApp requestUserAttention:NSCriticalRequest];
893                                         if ( status == NSAlertDefaultReturn ) 
894                                         {
895                                                 [self enableUI: YES];
896                                         }
897                 }
898                                 else
899                                 {
900                                         [self enableUI: YES];
901                                 }
902                                    /* If sleep has been selected */ 
903             if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Put Computer To Sleep"]) 
904                 { 
905                /* Sleep */ 
906                NSDictionary* errorDict; 
907                NSAppleEventDescriptor* returnDescriptor = NULL; 
908                NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource: 
909                         @"tell application \"Finder\" to sleep"]; 
910                returnDescriptor = [scriptObject executeAndReturnError: &errorDict]; 
911                [scriptObject release]; 
912                [self enableUI: YES]; 
913                 } 
914             /* If Shutdown has been selected */ 
915             if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Shut Down Computer"]) 
916                 { 
917                /* Shut Down */ 
918                NSDictionary* errorDict; 
919                NSAppleEventDescriptor* returnDescriptor = NULL; 
920                NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource: 
921                         @"tell application \"Finder\" to shut down"]; 
922                returnDescriptor = [scriptObject executeAndReturnError: &errorDict]; 
923                [scriptObject release]; 
924                [self enableUI: YES]; 
925                 }
926                         
927                                                 // MetaX insertion via AppleScript
928                         if([[NSUserDefaults standardUserDefaults] boolForKey: @"sendToMetaX"] == YES)
929                         {
930                         NSAppleScript *myScript = [[NSAppleScript alloc] initWithSource: [NSString stringWithFormat: @"%@%@%@", @"tell application \"MetaX\" to open (POSIX file \"", [fDstFile2Field stringValue], @"\")"]];
931                         [myScript executeAndReturnError: nil];
932                         [myScript release];
933                         }
934                         
935                         
936                         }
937                         else
938                         {
939                                 [self enableUI: YES];
940                         }
941             break;
942         }
943     }
944         
945     /* Lets show the queue status here in the main window */
946         int queue_count = jobGroups ? hb_group_count( fHandle ) : hb_count( fHandle );
947         if( queue_count )
948         {
949                 [fQueueStatus setStringValue: [NSString stringWithFormat:
950                         @"%d task%s in the queue",
951                                                  queue_count, ( queue_count > 1 ) ? "s" : ""]];
952         }
953         else
954         {
955                 [fQueueStatus setStringValue: @""];
956         }
957 }
958
959 - (IBAction) showNewScan:(id)sender
960 {
961         hb_list_t  * list;
962         hb_title_t * title;
963         int indxpri=0;    // Used to search the longuest title (default in combobox)
964         int longuestpri=0; // Used to search the longuest title (default in combobox)
965         
966         list = hb_get_titles( fHandle );
967         
968         if( !hb_list_count( list ) )
969         {
970                 /* We display a message if a valid dvd source was not chosen */
971                 [fSrcDVD2Field setStringValue: @"No Valid Title Found"];
972         SuccessfulScan = 0;
973         }
974         else
975         {
976      /* We increment the successful scancount here by one,
977                    which we use at the end of this function to tell the gui
978                    if this is the first successful scan since launch and whether
979                    or not we should set all settings to the defaults */
980                 
981         SuccessfulScan = 1;
982         currentSuccessfulScanCount++;
983
984                 [self enableUI: YES];
985
986         [toolbar validateVisibleItems];
987                 
988                 [fSrcTitlePopUp removeAllItems];
989                 for( int i = 0; i < hb_list_count( list ); i++ )
990                 {
991                         title = (hb_title_t *) hb_list_item( list, i );
992                         /*Set DVD Name at top of window*/
993                         [fSrcDVD2Field setStringValue:[NSString stringWithUTF8String: title->name]];
994                         
995                         currentSource = [NSString stringWithUTF8String: title->dvd];
996                         
997                         
998                         /* Use the dvd name in the default output field here 
999                                 May want to add code to remove blank spaces for some dvd names*/
1000                         /* Check to see if the last destination has been set,use if so, if not, use Desktop */
1001                         if ([[NSUserDefaults standardUserDefaults] stringForKey:@"LastDestinationDirectory"])
1002                         {
1003                                 [fDstFile2Field setStringValue: [NSString stringWithFormat:
1004                                         @"%@/%@.mp4", [[NSUserDefaults standardUserDefaults] stringForKey:@"LastDestinationDirectory"],[NSString
1005                   stringWithUTF8String: title->name]]];
1006                         }
1007                         else
1008                         {
1009                                 [fDstFile2Field setStringValue: [NSString stringWithFormat:
1010                                         @"%@/Desktop/%@.mp4", NSHomeDirectory(),[NSString
1011                   stringWithUTF8String: title->name]]];
1012                         }
1013                         
1014                         
1015                         if (longuestpri < title->hours*60*60 + title->minutes *60 + title->seconds)
1016                         {
1017                                 longuestpri=title->hours*60*60 + title->minutes *60 + title->seconds;
1018                                 indxpri=i;
1019                         }
1020                         
1021                         
1022                         int format = [fDstFormatPopUp indexOfSelectedItem];
1023                         char * ext = NULL;
1024                         switch( format )
1025                         {
1026                                 case 0:
1027                                         
1028                                         /*Get Default MP4 File Extension for mpeg4 (.mp4 or .m4v) from prefs*/
1029                                         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultMpegName"] > 0)
1030                                         {
1031                                                 ext = "m4v";
1032                                         }
1033                                         else
1034                                         {
1035                                                 ext = "mp4";
1036                                         }
1037                                         break;
1038                                 case 1: 
1039                                         ext = "avi";
1040                                         break;
1041                                 case 2:
1042                                         ext = "ogm";
1043                                         break;
1044                                 case 3:
1045                                         ext = "mkv";
1046                                         break;
1047                         }
1048                         
1049                         
1050                         NSString * string = [fDstFile2Field stringValue];
1051                         /* Add/replace File Output name to the correct extension*/
1052                         if( [string characterAtIndex: [string length] - 4] == '.' )
1053                         {
1054                                 [fDstFile2Field setStringValue: [NSString stringWithFormat:
1055                                         @"%@.%s", [string substringToIndex: [string length] - 4],
1056                                         ext]];
1057                         }
1058                         else
1059                         {
1060                                 [fDstFile2Field setStringValue: [NSString stringWithFormat:
1061                                         @"%@.%s", string, ext]];
1062                         }
1063                         
1064                         
1065                         [fSrcTitlePopUp addItemWithTitle: [NSString
1066                     stringWithFormat: @"%d - %02dh%02dm%02ds",
1067                                 title->index, title->hours, title->minutes,
1068                                 title->seconds]];
1069                         
1070                 }
1071                 // Select the longuest title
1072                 [fSrcTitlePopUp selectItemAtIndex: indxpri];
1073                 [self titlePopUpChanged: NULL];
1074                 
1075                 
1076                 
1077                 [self enableUI: YES];
1078                 
1079                 /* we record the current source name here in case the next scan is unsuccessful,
1080                                 then we can replace the scan progress with the old name if necessary */
1081        sourceDisplayName = [NSString stringWithFormat:[fSrcDVD2Field stringValue]];
1082        
1083        /* if its the initial successful scan after awakeFromNib */
1084            if (currentSuccessfulScanCount == 1)
1085            {
1086            [self selectDefaultPreset: NULL];
1087            /* if Deinterlace upon launch is specified in the prefs, then set to 1 for "Fast",
1088            if not, then set to 0 for none */
1089            if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultDeinterlaceOn"] > 0)
1090                [fPictureController setDeinterlace:1];
1091            else
1092                [fPictureController setDeinterlace:0];
1093            }
1094        
1095         }
1096 }
1097
1098 -(IBAction)showGrowlDoneNotification:(id)sender
1099 {
1100   [GrowlApplicationBridge 
1101           notifyWithTitle:@"Put down that cocktail..." 
1102               description:@"your HandBrake encode is done!" 
1103          notificationName:SERVICE_NAME
1104                  iconData:nil 
1105                  priority:0 
1106                  isSticky:1 
1107              clickContext:nil];
1108 }
1109
1110 - (void) enableUI: (bool) b
1111 {
1112     NSControl * controls[] =
1113       { fSrcTitleField, fSrcTitlePopUp,
1114         fSrcChapterField, fSrcChapterStartPopUp, fSrcChapterToField,
1115         fSrcChapterEndPopUp, fSrcDuration1Field, fSrcDuration2Field,
1116         fDstFormatField, fDstFormatPopUp, fDstCodecsField,
1117         fDstCodecsPopUp, fDstFile1Field, fDstFile2Field,
1118         fDstBrowseButton, fVidRateField, fVidRatePopUp,
1119         fVidEncoderField, fVidEncoderPopUp, fVidQualityField,
1120         fVidQualityMatrix, fVidGrayscaleCheck, fSubField, fSubPopUp,
1121         fAudLang1Field, fAudLang1PopUp, fAudLang2Field, fAudLang2PopUp,
1122         fAudTrack1MixLabel, fAudTrack1MixPopUp, fAudTrack2MixLabel, fAudTrack2MixPopUp,
1123         fAudRateField, fAudRatePopUp, fAudBitrateField,
1124         fAudBitratePopUp, fPictureButton,fQueueStatus, 
1125                 fPicSrcWidth,fPicSrcHeight,fPicSettingWidth,fPicSettingHeight,fPicSettingARkeep,
1126                 fPicSettingDeinterlace,fPicLabelSettings,fPicLabelSrc,fPicLabelOutp,
1127                 fPicLabelAr,fPicLabelDeinterlace,fPicLabelSrcX,fPicLabelOutputX,
1128                 fPicLabelPAROutputX,fPicSettingPARWidth,fPicSettingPARHeight,
1129                 fPicSettingPAR,fPicLabelAnamorphic,tableView,fPresetsAdd,fPresetsDelete,
1130                 fCreateChapterMarkers,fVidTurboPassCheck,fDstMpgLargeFileCheck,fPicLabelAutoCrop,
1131                 fPicSettingAutoCrop,fPicSettingDetelecine,fPicLabelDetelecine,fPicLabelDenoise,fPicSettingDenoise,fSubForcedCheck,};
1132
1133     for( unsigned i = 0;
1134          i < sizeof( controls ) / sizeof( NSControl * ); i++ )
1135     {
1136         if( [[controls[i] className] isEqualToString: @"NSTextField"] )
1137         {
1138             NSTextField * tf = (NSTextField *) controls[i];
1139             if( ![tf isBezeled] )
1140             {
1141                 [tf setTextColor: b ? [NSColor controlTextColor] :
1142                     [NSColor disabledControlTextColor]];
1143                 continue;
1144             }
1145         }
1146         [controls[i] setEnabled: b];
1147
1148     }
1149         
1150         if (b) {
1151
1152         /* if we're enabling the interface, check if the audio mixdown controls need to be enabled or not */
1153         /* these will have been enabled by the mass control enablement above anyway, so we're sense-checking it here */
1154         [self setEnabledStateOfAudioMixdownControls: NULL];
1155         
1156         } else {
1157
1158                 [tableView setEnabled: NO];
1159         
1160         }
1161
1162     [self videoMatrixChanged: NULL];
1163     [fAdvancedOptions enableUI:b];
1164 }
1165
1166 - (IBAction) showScanPanel: (id) sender
1167 {
1168     [self enableUI: NO];
1169         
1170         [self browseSources:NULL];
1171 }
1172
1173 - (void) browseSources: (id) sender
1174 {
1175     NSOpenPanel * panel;
1176         
1177     panel = [NSOpenPanel openPanel];
1178     [panel setAllowsMultipleSelection: NO];
1179     [panel setCanChooseFiles: YES];
1180     [panel setCanChooseDirectories: YES ];
1181     NSString * sourceDirectory;
1182         if ([[NSUserDefaults standardUserDefaults] stringForKey:@"LastSourceDirectory"])
1183         {
1184                 sourceDirectory = [[NSUserDefaults standardUserDefaults] stringForKey:@"LastSourceDirectory"];
1185         }
1186         else
1187         {
1188                 sourceDirectory = @"~/Desktop";
1189                 sourceDirectory = [sourceDirectory stringByExpandingTildeInPath];
1190         }
1191    [panel beginSheetForDirectory: sourceDirectory file: nil types: nil
1192                                    modalForWindow: fWindow modalDelegate: self
1193                                    didEndSelector: @selector( browseSourcesDone:returnCode:contextInfo: )
1194                                           contextInfo: nil];
1195 }
1196
1197 - (void) browseSourcesDone: (NSOpenPanel *) sheet
1198                  returnCode: (int) returnCode contextInfo: (void *) contextInfo
1199 {
1200     /* User selected a file to open */
1201         if( returnCode == NSOKButton )
1202     {
1203         [fSrcDVD2Field setStringValue: _( @"Opening a new source ..." )];
1204                 [fScanIndicator setHidden: NO];
1205             [fScanIndicator setIndeterminate: YES];
1206         [fScanIndicator startAnimation: nil];
1207                 
1208                 /* we set the last source directory in the prefs here */
1209                 NSString *sourceDirectory = [[[sheet filenames] objectAtIndex: 0] stringByDeletingLastPathComponent];
1210                 [[NSUserDefaults standardUserDefaults] setObject:sourceDirectory forKey:@"LastSourceDirectory"];
1211                 
1212         NSString *path = [[sheet filenames] objectAtIndex: 0];
1213         HBDVDDetector *detector = [HBDVDDetector detectorForPath:path];
1214         if( [detector isVideoDVD] )
1215         {
1216             // The chosen path was actually on a DVD, so use the raw block
1217             // device path instead.
1218             path = [detector devicePath];
1219         }
1220                 
1221                 hb_scan( fHandle, [path UTF8String], 0 );
1222         }
1223         else // User clicked Cancel in browse window
1224         {
1225                 /* if we have a title loaded up */
1226                 if ([[fSrcDVD2Field stringValue] length] > 0)
1227                 {
1228             [self enableUI: YES];
1229         }
1230         }
1231 }
1232
1233 - (IBAction) openMainWindow: (id) sender
1234 {
1235     [fWindow  makeKeyAndOrderFront:nil];
1236     [fWindow setReleasedWhenClosed: YES];
1237 }
1238
1239 - (BOOL) windowShouldClose: (id) sender
1240 {
1241     [fWindow setReleasedWhenClosed: NO];
1242     return YES;
1243 }
1244
1245 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
1246 {
1247     if( !flag ) {
1248         [fWindow  makeKeyAndOrderFront:nil];
1249         [fWindow setReleasedWhenClosed: YES];
1250         
1251         return YES;
1252     }
1253     
1254     return NO;
1255 }
1256
1257 - (IBAction) videoMatrixChanged: (id) sender;
1258 {
1259     bool target, bitrate, quality;
1260
1261     target = bitrate = quality = false;
1262     if( [fVidQualityMatrix isEnabled] )
1263     {
1264         switch( [fVidQualityMatrix selectedRow] )
1265         {
1266             case 0:
1267                 target = true;
1268                 break;
1269             case 1:
1270                 bitrate = true;
1271                 break;
1272             case 2:
1273                 quality = true;
1274                 break;
1275         }
1276     }
1277     [fVidTargetSizeField  setEnabled: target];
1278     [fVidBitrateField     setEnabled: bitrate];
1279     [fVidQualitySlider    setEnabled: quality];
1280     [fVidTwoPassCheck     setEnabled: !quality &&
1281         [fVidQualityMatrix isEnabled]];
1282     if( quality )
1283     {
1284         [fVidTwoPassCheck setState: NSOffState];
1285                 [fVidTurboPassCheck setHidden: YES];
1286                 [fVidTurboPassCheck setState: NSOffState];
1287     }
1288
1289     [self qualitySliderChanged: sender];
1290     [self calculateBitrate: sender];
1291         [self customSettingUsed: sender];
1292 }
1293
1294 - (IBAction) qualitySliderChanged: (id) sender
1295 {
1296     [fVidConstantCell setTitle: [NSString stringWithFormat:
1297         _( @"Constant quality: %.0f %%" ), 100.0 *
1298         [fVidQualitySlider floatValue]]];
1299                 [self customSettingUsed: sender];
1300 }
1301
1302 - (IBAction) browseFile: (id) sender
1303 {
1304     /* Open a panel to let the user choose and update the text field */
1305     NSSavePanel * panel = [NSSavePanel savePanel];
1306         /* We get the current file name and path from the destination field here */
1307         [panel beginSheetForDirectory: [[fDstFile2Field stringValue] stringByDeletingLastPathComponent] file: [[fDstFile2Field stringValue] lastPathComponent]
1308                                    modalForWindow: fWindow modalDelegate: self
1309                                    didEndSelector: @selector( browseFileDone:returnCode:contextInfo: )
1310                                           contextInfo: NULL];
1311 }
1312
1313 - (void) browseFileDone: (NSSavePanel *) sheet
1314     returnCode: (int) returnCode contextInfo: (void *) contextInfo
1315 {
1316     if( returnCode == NSOKButton )
1317     {
1318         [fDstFile2Field setStringValue: [sheet filename]];
1319     }
1320 }
1321
1322 - (IBAction) showPicturePanel: (id) sender
1323 {
1324         hb_list_t  * list  = hb_get_titles( fHandle );
1325     hb_title_t * title = (hb_title_t *) hb_list_item( list,
1326             [fSrcTitlePopUp indexOfSelectedItem] );
1327     [fPictureController showPanelInWindow:fWindow forTitle:title];
1328 }
1329
1330 - (void)pictureSettingsDidChange {
1331         [self calculatePictureSizing: NULL];
1332 }
1333
1334 - (void) PrepareJob
1335 {
1336     hb_list_t  * list  = hb_get_titles( fHandle );
1337     hb_title_t * title = (hb_title_t *) hb_list_item( list,
1338             [fSrcTitlePopUp indexOfSelectedItem] );
1339     hb_job_t * job = title->job;
1340     //int i;
1341
1342     /* Chapter selection */
1343     job->chapter_start = [fSrcChapterStartPopUp indexOfSelectedItem] + 1;
1344     job->chapter_end   = [fSrcChapterEndPopUp   indexOfSelectedItem] + 1;
1345         
1346     /* Format and codecs */
1347     int format = [fDstFormatPopUp indexOfSelectedItem];
1348     int codecs = [fDstCodecsPopUp indexOfSelectedItem];
1349     job->mux    = FormatSettings[format][codecs] & HB_MUX_MASK;
1350     job->vcodec = FormatSettings[format][codecs] & HB_VCODEC_MASK;
1351     job->acodec = FormatSettings[format][codecs] & HB_ACODEC_MASK;
1352     /* If mpeg-4, then set mpeg-4 specific options like chapters and > 4gb file sizes */
1353         if ([fDstFormatPopUp indexOfSelectedItem] == 0)
1354         {
1355         /* We set the largeFileSize (64 bit formatting) variable here to allow for > 4gb files based on the format being
1356                 mpeg4 and the checkbox being checked 
1357                 *Note: this will break compatibility with some target devices like iPod, etc.!!!!*/
1358                 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"AllowLargeFiles"] > 0 && [fDstMpgLargeFileCheck state] == NSOnState)
1359                 {
1360                         job->largeFileSize = 1;
1361                 }
1362                 else
1363                 {
1364                         job->largeFileSize = 0;
1365                 }
1366         }
1367         if ([fDstFormatPopUp indexOfSelectedItem] == 0 || [fDstFormatPopUp indexOfSelectedItem] == 3)
1368         {
1369           /* We set the chapter marker extraction here based on the format being
1370                 mpeg4 or mkv and the checkbox being checked */
1371                 if ([fCreateChapterMarkers state] == NSOnState)
1372                 {
1373                         job->chapter_markers = 1;
1374                 }
1375                 else
1376                 {
1377                         job->chapter_markers = 0;
1378                 }
1379         }
1380         if( ( job->vcodec & HB_VCODEC_FFMPEG ) &&
1381         [fVidEncoderPopUp indexOfSelectedItem] > 0 )
1382     {
1383         job->vcodec = HB_VCODEC_XVID;
1384     }
1385     if( job->vcodec & HB_VCODEC_X264 )
1386     {
1387                 if ([fVidEncoderPopUp indexOfSelectedItem] > 0 )
1388             {
1389                         /* Just use new Baseline Level 3.0 
1390                         Lets Deprecate Baseline Level 1.3h264_level*/
1391                         job->h264_level = 30;
1392                         job->mux = HB_MUX_IPOD;
1393                         /* move sanity check for iPod Encoding here */
1394                         job->pixel_ratio = 0 ;
1395                         
1396                 }
1397                 
1398                 /* Set this flag to switch from Constant Quantizer(default) to Constant Rate Factor Thanks jbrjake
1399                 Currently only used with Constant Quality setting*/
1400                 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultCrf"] > 0 && [fVidQualityMatrix selectedRow] == 2)
1401                 {
1402                 job->crf = 1;
1403                 }
1404                 
1405                 /* Below Sends x264 options to the core library if x264 is selected*/
1406                 /* Lets use this as per Nyx, Thanks Nyx!*/
1407                 job->x264opts = (char *)calloc(1024, 1); /* Fixme, this just leaks */
1408                 /* Turbo first pass if two pass and Turbo First pass is selected */
1409                 if( [fVidTwoPassCheck state] == NSOnState && [fVidTurboPassCheck state] == NSOnState )
1410                 {
1411                         /* pass the "Turbo" string to be appended to the existing x264 opts string into a variable for the first pass */
1412                         NSString *firstPassOptStringTurbo = @":ref=1:subme=1:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0";
1413                         /* append the "Turbo" string variable to the existing opts string.
1414                         Note: the "Turbo" string must be appended, not prepended to work properly*/
1415                         NSString *firstPassOptStringCombined = [[fAdvancedOptions optionsString] stringByAppendingString:firstPassOptStringTurbo];
1416                         strcpy(job->x264opts, [firstPassOptStringCombined UTF8String]);
1417                 }
1418                 else
1419                 {
1420                         strcpy(job->x264opts, [[fAdvancedOptions optionsString] UTF8String]);
1421                 }
1422                 
1423         job->h264_13 = [fVidEncoderPopUp indexOfSelectedItem];
1424     }
1425
1426     /* Video settings */
1427     if( [fVidRatePopUp indexOfSelectedItem] > 0 )
1428     {
1429         job->vrate      = 27000000;
1430         job->vrate_base = hb_video_rates[[fVidRatePopUp
1431             indexOfSelectedItem]-1].rate;
1432     }
1433     else
1434     {
1435         job->vrate      = title->rate;
1436         job->vrate_base = title->rate_base;
1437     }
1438
1439     switch( [fVidQualityMatrix selectedRow] )
1440     {
1441         case 0:
1442             /* Target size.
1443                Bitrate should already have been calculated and displayed
1444                in fVidBitrateField, so let's just use it */
1445         case 1:
1446             job->vquality = -1.0;
1447             job->vbitrate = [fVidBitrateField intValue];
1448             break;
1449         case 2:
1450             job->vquality = [fVidQualitySlider floatValue];
1451             job->vbitrate = 0;
1452             break;
1453     }
1454
1455     job->grayscale = ( [fVidGrayscaleCheck state] == NSOnState );
1456
1457     /* Subtitle settings */
1458     job->subtitle = [fSubPopUp indexOfSelectedItem] - 2;
1459
1460     /* Audio tracks and mixdowns */
1461     /* check for the condition where track 2 has an audio selected, but track 1 does not */
1462     /* we will use track 2 as track 1 in this scenario */
1463     if ([fAudLang1PopUp indexOfSelectedItem] > 0)
1464     {
1465         job->audios[0] = [fAudLang1PopUp indexOfSelectedItem] - 1;
1466         job->audios[1] = [fAudLang2PopUp indexOfSelectedItem] - 1; /* will be -1 if "none" is selected */
1467         job->audios[2] = -1;
1468         job->audio_mixdowns[0] = [[fAudTrack1MixPopUp selectedItem] tag];
1469         job->audio_mixdowns[1] = [[fAudTrack2MixPopUp selectedItem] tag];
1470     }
1471     else if ([fAudLang2PopUp indexOfSelectedItem] > 0)
1472     {
1473         job->audios[0] = [fAudLang2PopUp indexOfSelectedItem] - 1;
1474         job->audio_mixdowns[0] = [[fAudTrack2MixPopUp selectedItem] tag];
1475         job->audios[1] = -1;
1476     }
1477     else
1478     {
1479         job->audios[0] = -1;
1480     }
1481
1482     /* Audio settings */
1483     job->arate = hb_audio_rates[[fAudRatePopUp
1484                      indexOfSelectedItem]].rate;
1485     job->abitrate = [[fAudBitratePopUp selectedItem] tag];
1486     
1487     job->filters = hb_list_init();
1488    
1489         /* Detelecine */
1490     if ([fPictureController detelecine])
1491     {
1492         hb_list_add( job->filters, &hb_filter_detelecine );
1493     }
1494    
1495     /* Deinterlace */
1496     if ([fPictureController deinterlace] == 1)
1497     {
1498         /* Run old deinterlacer by default */
1499         hb_filter_deinterlace.settings = "-1"; 
1500         hb_list_add( job->filters, &hb_filter_deinterlace );
1501     }
1502     else if ([fPictureController deinterlace] == 2)
1503     {
1504         /* Yadif mode 0 (1-pass with spatial deinterlacing.) */
1505         hb_filter_deinterlace.settings = "0"; 
1506         hb_list_add( job->filters, &hb_filter_deinterlace );            
1507     }
1508     else if ([fPictureController deinterlace] == 3)
1509     {
1510         /* Yadif (1-pass w/o spatial deinterlacing) and Mcdeint */
1511         hb_filter_deinterlace.settings = "2:-1:1"; 
1512         hb_list_add( job->filters, &hb_filter_deinterlace );            
1513     }
1514     else if ([fPictureController deinterlace] == 4)
1515     {
1516         /* Yadif (2-pass w/ spatial deinterlacing) and Mcdeint*/
1517         hb_filter_deinterlace.settings = "1:-1:1"; 
1518         hb_list_add( job->filters, &hb_filter_deinterlace );            
1519     }
1520         
1521         /* Denoise */
1522         
1523         if ([fPictureController denoise] == 1) // Weak in popup
1524         {
1525                 hb_filter_denoise.settings = "2:1:2:3"; 
1526         hb_list_add( job->filters, &hb_filter_denoise );        
1527         }
1528         else if ([fPictureController denoise] == 2) // Medium in popup
1529         {
1530                 hb_filter_denoise.settings = "3:2:2:3"; 
1531         hb_list_add( job->filters, &hb_filter_denoise );        
1532         }
1533         else if ([fPictureController denoise] == 3) // Strong in popup
1534         {
1535                 hb_filter_denoise.settings = "7:7:5:5"; 
1536         hb_list_add( job->filters, &hb_filter_denoise );        
1537         }
1538
1539 }
1540
1541
1542
1543 - (IBAction) addToQueue: (id) sender
1544 {
1545 /* We get the destination directory from the destingation field here */
1546         NSString *destinationDirectory = [[fDstFile2Field stringValue] stringByDeletingLastPathComponent];
1547         /* We check for a valid destination here */
1548         if ([[NSFileManager defaultManager] fileExistsAtPath:destinationDirectory] == 0) 
1549         {
1550                 NSRunAlertPanel(@"Warning!", @"This is not a valid destination directory!", @"OK", nil, nil);
1551         }
1552         else
1553         {
1554                 
1555                 hb_list_t  * list  = hb_get_titles( fHandle );
1556                 hb_title_t * title = (hb_title_t *) hb_list_item( list,
1557                                                                                                                   [fSrcTitlePopUp indexOfSelectedItem] );
1558                 hb_job_t * job = title->job;
1559                 
1560         // Assign a sequence number, starting at zero, to each job added so they can
1561         // be lumped together in the UI.
1562         job->sequence_id = -1;
1563                 
1564                 [self PrepareJob];
1565                 
1566                 /* Destination file */
1567                 job->file = [[fDstFile2Field stringValue] UTF8String];
1568
1569                 if( [fSubForcedCheck state] == NSOnState )
1570                 {
1571                     job->subtitle_force = 1;
1572                 } else {
1573                     job->subtitle_force = 0;
1574                 }
1575
1576                 /*
1577                  * subtitle of -1 is a scan
1578                  */
1579                 if( job->subtitle == -1 )
1580                 {
1581                     char *x264opts_tmp;
1582
1583                     /*
1584                      * When subtitle scan is enabled do a fast pre-scan job
1585                      * which will determine which subtitles to enable, if any.
1586                      */
1587                     job->pass = -1;
1588                     x264opts_tmp = job->x264opts;
1589                     job->subtitle = -1;
1590
1591                     job->x264opts = NULL;
1592                     
1593                     job->subtitle_scan = 1;  
1594
1595                     job->select_subtitle = (hb_subtitle_t**)malloc(sizeof(hb_subtitle_t*));
1596                     *(job->select_subtitle) = NULL;
1597                     
1598                     /*
1599                      * Add the pre-scan job
1600                      */
1601                                         job->sequence_id++; // for job grouping
1602                     hb_add( fHandle, job );
1603
1604                     job->x264opts = x264opts_tmp;
1605                 } else {
1606                     job->select_subtitle = NULL;
1607                 }
1608
1609                 /* No subtitle were selected, so reset the subtitle to -1 (which before
1610                  * this point meant we were scanning
1611                  */
1612                 if( job->subtitle == -2 )
1613                 {
1614                     job->subtitle = -1;
1615                 }
1616
1617                 if( [fVidTwoPassCheck state] == NSOnState )
1618                 {
1619             hb_subtitle_t **subtitle_tmp = job->select_subtitle;
1620             job->select_subtitle = NULL;
1621             job->subtitle_scan = 0;
1622
1623                         job->pass = 1;
1624                         job->sequence_id++; // for job grouping
1625                         hb_add( fHandle, job );
1626             
1627                         job->pass = 2;
1628                         job->sequence_id++; // for job grouping
1629                         
1630                         job->x264opts = (char *)calloc(1024, 1); /* Fixme, this just leaks */  
1631                         strcpy(job->x264opts, [[fAdvancedOptions optionsString] UTF8String]);
1632
1633             job->select_subtitle = subtitle_tmp;
1634
1635                         hb_add( fHandle, job );
1636                 }
1637                 else
1638                 {
1639                         job->subtitle_scan = 0;
1640                         job->pass = 0;
1641                         job->sequence_id++; // for job grouping
1642                         hb_add( fHandle, job );
1643                 }
1644         
1645         [[NSUserDefaults standardUserDefaults] setObject:destinationDirectory forKey:@"LastDestinationDirectory"];
1646         /* Lets try to update stuff, taken from remove in the queue controller */
1647         [fQueueController performSelectorOnMainThread: @selector( updateQueueUI )
1648         withObject: NULL waitUntilDone: NO];
1649         }
1650 }
1651
1652 - (IBAction) Rip: (id) sender
1653 {
1654     /* Rip or Cancel ? */
1655     hb_state_t s;
1656     hb_get_state2( fHandle, &s );
1657
1658     if(s.state == HB_STATE_WORKING || s.state == HB_STATE_PAUSED)
1659         {
1660         [self Cancel: sender];
1661         return;
1662     }
1663         /* if there is no job in the queue, then add it to the queue and rip 
1664         otherwise, there are already jobs in queue, so just rip the queue */
1665         int count = hb_count( fHandle );
1666         if( count < 1 )
1667         {
1668                 [self addToQueue: sender];
1669                 }
1670     
1671             /* We check for duplicate name here */
1672         if( [[NSFileManager defaultManager] fileExistsAtPath:
1673             [fDstFile2Field stringValue]] )
1674     {
1675         NSBeginCriticalAlertSheet( _( @"File already exists" ),
1676             _( @"Cancel" ), _( @"Overwrite" ), NULL, fWindow, self,
1677             @selector( overWriteAlertDone:returnCode:contextInfo: ),
1678             NULL, NULL, [NSString stringWithFormat:
1679             _( @"Do you want to overwrite %@?" ),
1680             [fDstFile2Field stringValue]] );
1681         return;
1682     }
1683         /* We get the destination directory from the destination field here */
1684         NSString *destinationDirectory = [[fDstFile2Field stringValue] stringByDeletingLastPathComponent];
1685         /* We check for a valid destination here */
1686         if ([[NSFileManager defaultManager] fileExistsAtPath:destinationDirectory] == 0) 
1687         {
1688                 NSRunAlertPanel(@"Warning!", @"This is not a valid destination directory!", @"OK", nil, nil);
1689         }
1690         else
1691         {
1692         [[NSUserDefaults standardUserDefaults] setObject:destinationDirectory forKey:@"LastDestinationDirectory"];
1693                 [self _Rip];
1694         }
1695         
1696
1697
1698 }
1699
1700 - (void) overWriteAlertDone: (NSWindow *) sheet
1701     returnCode: (int) returnCode contextInfo: (void *) contextInfo
1702 {
1703     if( returnCode == NSAlertAlternateReturn )
1704     {
1705         [self _Rip];
1706     }
1707 }
1708
1709 - (void) updateAlertDone: (NSWindow *) sheet
1710     returnCode: (int) returnCode contextInfo: (void *) contextInfo
1711 {
1712     if( returnCode == NSAlertAlternateReturn )
1713     {
1714         /* Show scan panel */
1715         [self performSelectorOnMainThread: @selector(showScanPanel:)
1716             withObject: NULL waitUntilDone: NO];
1717         return;
1718     }
1719
1720     /* Go to HandBrake homepage and exit */
1721     [self openHomepage: NULL];
1722     [NSApp terminate: self];
1723 }
1724
1725 - (void) _Rip
1726 {
1727     /* Let libhb do the job */
1728     hb_start( fHandle );
1729         /*set the fEncodeState State */
1730         fEncodeState = 1;
1731 }
1732
1733 - (IBAction) Cancel: (id) sender
1734 {
1735     NSBeginCriticalAlertSheet( _( @"Cancel - Are you sure?" ),
1736         _( @"Keep working" ), _( @"Cancel encoding" ), NULL, fWindow, self,
1737         @selector( _Cancel:returnCode:contextInfo: ), NULL, NULL,
1738         _( @"Encoding won't be recoverable." ) );
1739 }
1740
1741 - (void) _Cancel: (NSWindow *) sheet
1742     returnCode: (int) returnCode contextInfo: (void *) contextInfo
1743 {
1744     if( returnCode == NSAlertAlternateReturn )
1745     {
1746         hb_stop( fHandle );
1747                 /*set the fEncodeState State */
1748              fEncodeState = 2;
1749     }
1750 }
1751
1752 - (IBAction) Pause: (id) sender
1753 {
1754     hb_state_t s;
1755     hb_get_state2( fHandle, &s );
1756
1757     if( s.state == HB_STATE_PAUSED )
1758     {
1759         hb_resume( fHandle );
1760     }
1761     else
1762     {
1763         hb_pause( fHandle );
1764     }
1765 }
1766
1767 - (IBAction) titlePopUpChanged: (id) sender
1768 {
1769     hb_list_t  * list  = hb_get_titles( fHandle );
1770     hb_title_t * title = (hb_title_t*)
1771         hb_list_item( list, [fSrcTitlePopUp indexOfSelectedItem] );
1772                 
1773                 
1774     /* If Auto Naming is on. We create an output filename of dvd name - title number */
1775     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultAutoNaming"] > 0)
1776         {
1777                 [fDstFile2Field setStringValue: [NSString stringWithFormat:
1778                         @"%@/%@-%d.%@", [[fDstFile2Field stringValue] stringByDeletingLastPathComponent],
1779                         [NSString stringWithUTF8String: title->name],
1780                           title->index,
1781                         [[fDstFile2Field stringValue] pathExtension]]]; 
1782         }
1783
1784     /* Update chapter popups */
1785     [fSrcChapterStartPopUp removeAllItems];
1786     [fSrcChapterEndPopUp   removeAllItems];
1787     for( int i = 0; i < hb_list_count( title->list_chapter ); i++ )
1788     {
1789         [fSrcChapterStartPopUp addItemWithTitle: [NSString
1790             stringWithFormat: @"%d", i + 1]];
1791         [fSrcChapterEndPopUp addItemWithTitle: [NSString
1792             stringWithFormat: @"%d", i + 1]];
1793     }
1794     [fSrcChapterStartPopUp selectItemAtIndex: 0];
1795     [fSrcChapterEndPopUp   selectItemAtIndex:
1796         hb_list_count( title->list_chapter ) - 1];
1797     [self chapterPopUpChanged: NULL];
1798
1799 /* Start Get and set the initial pic size for display */
1800         hb_job_t * job = title->job;
1801         fTitle = title; 
1802         /* Turn Deinterlace on/off depending on the preference */
1803         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultDeinterlaceOn"] > 0)
1804         {
1805                 [fPictureController setDeinterlace:1];
1806         }
1807         else
1808         {
1809                 [fPictureController setDeinterlace:0];
1810         }
1811         
1812         /* Pixel Ratio Setting */
1813         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"PixelRatio"])
1814     {
1815                 job->pixel_ratio = 1 ;
1816         }
1817         else
1818         {
1819                 job->pixel_ratio = 0 ;
1820         }
1821         /*Set Source Size Fields Here */
1822         [fPicSrcWidth setStringValue: [NSString stringWithFormat:
1823                                                          @"%d", fTitle->width]];
1824         [fPicSrcHeight setStringValue: [NSString stringWithFormat:
1825                                                          @"%d", fTitle->height]];
1826                                                          
1827         /* Set Auto Crop to on upon selecting a new title */
1828     [fPictureController setAutoCrop:YES];
1829     
1830         /* We get the originial output picture width and height and put them
1831         in variables for use with some presets later on */
1832         PicOrigOutputWidth = job->width;
1833         PicOrigOutputHeight = job->height;
1834         AutoCropTop = job->crop[0];
1835         AutoCropBottom = job->crop[1];
1836         AutoCropLeft = job->crop[2];
1837         AutoCropRight = job->crop[3];
1838         /* we test getting the max output value for pic sizing here to be used later*/
1839         [fPicSettingWidth setStringValue: [NSString stringWithFormat:
1840                 @"%d", PicOrigOutputWidth]];
1841         [fPicSettingHeight setStringValue: [NSString stringWithFormat:
1842                 @"%d", PicOrigOutputHeight]];
1843         /* we run the picture size values through
1844         calculatePictureSizing to get all picture size
1845         information*/
1846         [self calculatePictureSizing: NULL];
1847         /* Run Through encoderPopUpChanged to see if there
1848                 needs to be any pic value modifications based on encoder settings */
1849         //[self encoderPopUpChanged: NULL];
1850         /* END Get and set the initial pic size for display */ 
1851
1852     /* Update subtitle popups */
1853     hb_subtitle_t * subtitle;
1854     [fSubPopUp removeAllItems];
1855     [fSubPopUp addItemWithTitle: @"None"];
1856     [fSubPopUp addItemWithTitle: @"Autoselect"];
1857     for( int i = 0; i < hb_list_count( title->list_subtitle ); i++ )
1858     {
1859         subtitle = (hb_subtitle_t *) hb_list_item( title->list_subtitle, i );
1860
1861         /* We cannot use NSPopUpButton's addItemWithTitle because
1862            it checks for duplicate entries */
1863         [[fSubPopUp menu] addItemWithTitle: [NSString stringWithCString:
1864             subtitle->lang] action: NULL keyEquivalent: @""];
1865     }
1866     [fSubPopUp selectItemAtIndex: 0];
1867         
1868         [self subtitleSelectionChanged: NULL];
1869     
1870     /* Update chapter table */
1871     [fChapterTitlesDelegate resetWithTitle:title];
1872     [fChapterTable reloadData];
1873
1874     /* Update audio popups */
1875     [self addAllAudioTracksToPopUp: fAudLang1PopUp];
1876     [self addAllAudioTracksToPopUp: fAudLang2PopUp];
1877     /* search for the first instance of our prefs default language for track 1, and set track 2 to "none" */
1878         NSString * audioSearchPrefix = [[NSUserDefaults standardUserDefaults] stringForKey:@"DefaultLanguage"];
1879         [self selectAudioTrackInPopUp: fAudLang1PopUp searchPrefixString: audioSearchPrefix selectIndexIfNotFound: 1];
1880     [self selectAudioTrackInPopUp: fAudLang2PopUp searchPrefixString: NULL selectIndexIfNotFound: 0];
1881         
1882         /* changing the title may have changed the audio channels on offer, */
1883         /* so call audioTrackPopUpChanged for both audio tracks to update the mixdown popups */
1884         [self audioTrackPopUpChanged: fAudLang1PopUp];
1885         [self audioTrackPopUpChanged: fAudLang2PopUp];
1886         /* lets call tableViewSelected to make sure that any preset we have selected is enforced after a title change */
1887         [self tableViewSelected:NULL];
1888 }
1889
1890 - (IBAction) chapterPopUpChanged: (id) sender
1891 {
1892     
1893         /* If start chapter popup is greater than end chapter popup,
1894         we set the end chapter popup to the same as start chapter popup */
1895         if ([fSrcChapterStartPopUp indexOfSelectedItem] > [fSrcChapterEndPopUp indexOfSelectedItem])
1896         {
1897                 [fSrcChapterEndPopUp selectItemAtIndex: [fSrcChapterStartPopUp indexOfSelectedItem]];
1898     }
1899
1900                 
1901         hb_list_t  * list  = hb_get_titles( fHandle );
1902     hb_title_t * title = (hb_title_t *)
1903         hb_list_item( list, [fSrcTitlePopUp indexOfSelectedItem] );
1904
1905     hb_chapter_t * chapter;
1906     int64_t        duration = 0;
1907     for( int i = [fSrcChapterStartPopUp indexOfSelectedItem];
1908          i <= [fSrcChapterEndPopUp indexOfSelectedItem]; i++ )
1909     {
1910         chapter = (hb_chapter_t *) hb_list_item( title->list_chapter, i );
1911         duration += chapter->duration;
1912     }
1913     
1914     duration /= 90000; /* pts -> seconds */
1915     [fSrcDuration2Field setStringValue: [NSString stringWithFormat:
1916         @"%02lld:%02lld:%02lld", duration / 3600, ( duration / 60 ) % 60,
1917         duration % 60]];
1918
1919     [self calculateBitrate: sender];
1920 }
1921
1922 - (IBAction) formatPopUpChanged: (id) sender
1923 {
1924     NSString * string = [fDstFile2Field stringValue];
1925     int format = [fDstFormatPopUp indexOfSelectedItem];
1926     char * ext = NULL;
1927         /* Initially set the large file (64 bit formatting) output checkbox to hidden */
1928     [fDstMpgLargeFileCheck setHidden: YES];
1929     /* Update the codecs popup */
1930     [fDstCodecsPopUp removeAllItems];
1931     switch( format )
1932     {
1933         case 0:
1934                         /*Get Default MP4 File Extension*/
1935                         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultMpegName"] > 0)
1936                         {
1937                                 ext = "m4v";
1938                         }
1939                         else
1940                         {
1941                                 ext = "mp4";
1942                         }
1943             [fDstCodecsPopUp addItemWithTitle:
1944                 _( @"MPEG-4 Video / AAC Audio" )];
1945             [fDstCodecsPopUp addItemWithTitle:
1946                 _( @"AVC/H.264 Video / AAC Audio" )];
1947                         /* We enable the create chapters checkbox here since we are .mp4*/
1948                         [fCreateChapterMarkers setEnabled: YES];
1949                         /* We show the Large File (64 bit formatting) checkbox since we are .mp4 
1950                         if we have enabled the option in the global preferences*/
1951                         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"AllowLargeFiles"] > 0)
1952                         {
1953                                 [fDstMpgLargeFileCheck setHidden: NO];
1954                         }
1955                                 else
1956                                 {
1957                                         /* if not enable in global preferences, we additionaly sanity check that the
1958                                         hidden checkbox is set to off. */
1959                                         [fDstMpgLargeFileCheck setState: NSOffState];
1960                                 }
1961                                 break;
1962         case 1: 
1963             ext = "avi";
1964             [fDstCodecsPopUp addItemWithTitle:
1965                 _( @"MPEG-4 Video / MP3 Audio" )];
1966             [fDstCodecsPopUp addItemWithTitle:
1967                 _( @"MPEG-4 Video / AC-3 Audio" )];
1968             [fDstCodecsPopUp addItemWithTitle:
1969                 _( @"AVC/H.264 Video / MP3 Audio" )];
1970             [fDstCodecsPopUp addItemWithTitle:
1971                 _( @"AVC/H.264 Video / AC-3 Audio" )];
1972                         /* We disable the create chapters checkbox here since we are NOT .mp4 
1973                         and make sure it is unchecked*/
1974                         [fCreateChapterMarkers setEnabled: NO];
1975                         [fCreateChapterMarkers setState: NSOffState];
1976                         break;
1977         case 2:
1978             ext = "ogm";
1979             [fDstCodecsPopUp addItemWithTitle:
1980                 _( @"MPEG-4 Video / Vorbis Audio" )];
1981             [fDstCodecsPopUp addItemWithTitle:
1982                 _( @"MPEG-4 Video / MP3 Audio" )];
1983             /* We disable the create chapters checkbox here since we are NOT .mp4 
1984                         and make sure it is unchecked*/
1985                         [fCreateChapterMarkers setEnabled: NO];
1986                         [fCreateChapterMarkers setState: NSOffState];
1987                         break;
1988                 case 3:
1989             ext = "mkv";
1990             [fDstCodecsPopUp addItemWithTitle:
1991                 _( @"MPEG-4 Video / AAC Audio" )];
1992                                 [fDstCodecsPopUp addItemWithTitle:
1993                 _( @"MPEG-4 Video / AC-3 Audio" )];
1994                         [fDstCodecsPopUp addItemWithTitle:
1995                 _( @"MPEG-4 Video / MP3 Audio" )];
1996                         [fDstCodecsPopUp addItemWithTitle:
1997                 _( @"MPEG-4 Video / Vorbis Audio" )];
1998             
1999                         [fDstCodecsPopUp addItemWithTitle:
2000                 _( @"AVC/H.264 Video / AAC Audio" )];
2001                         [fDstCodecsPopUp addItemWithTitle:
2002                 _( @"AVC/H.264 Video / AC-3 Audio" )];
2003                         [fDstCodecsPopUp addItemWithTitle:
2004                 _( @"AVC/H.264 Video / MP3 Audio" )];
2005                         [fDstCodecsPopUp addItemWithTitle:
2006                 _( @"AVC/H.264 Video / Vorbis Audio" )];
2007             /* We enable the create chapters checkbox here since */
2008                         [fCreateChapterMarkers setEnabled: YES];
2009                         break;
2010     }
2011     [self codecsPopUpChanged: NULL];
2012
2013     /* Add/replace to the correct extension */
2014     if( [string characterAtIndex: [string length] - 4] == '.' )
2015     {
2016         [fDstFile2Field setStringValue: [NSString stringWithFormat:
2017             @"%@.%s", [string substringToIndex: [string length] - 4],
2018             ext]];
2019     }
2020     else
2021     {
2022         [fDstFile2Field setStringValue: [NSString stringWithFormat:
2023             @"%@.%s", string, ext]];
2024     }
2025
2026         /* changing the format may mean that we can / can't offer mono or 6ch, */
2027         /* so call audioTrackPopUpChanged for both audio tracks to update the mixdown popups */
2028         [self audioTrackPopUpChanged: fAudLang1PopUp];
2029         [self audioTrackPopUpChanged: fAudLang2PopUp];
2030         /* We call the method to properly enable/disable turbo 2 pass */
2031         [self twoPassCheckboxChanged: sender];
2032         /* We call method method to change UI to reflect whether a preset is used or not*/
2033         [self customSettingUsed: sender];       
2034         
2035 }
2036
2037 - (IBAction) codecsPopUpChanged: (id) sender
2038 {
2039     int format = [fDstFormatPopUp indexOfSelectedItem];
2040     int codecs = [fDstCodecsPopUp indexOfSelectedItem];
2041         
2042     [fAdvancedOptions setHidden:YES];
2043
2044     /* Update the encoder popup*/
2045     if( ( FormatSettings[format][codecs] & HB_VCODEC_X264 ) )
2046     {
2047         /* MPEG-4 -> H.264 */
2048         [fVidEncoderPopUp removeAllItems];
2049                 [fVidEncoderPopUp addItemWithTitle: @"x264 (h.264 Main)"];
2050                 [fVidEncoderPopUp addItemWithTitle: @"x264 (h.264 iPod)"];
2051                 [fVidEncoderPopUp selectItemAtIndex: 0];
2052         [fAdvancedOptions setHidden:NO];
2053     }
2054     
2055     else if( ( FormatSettings[format][codecs] & HB_VCODEC_FFMPEG ) )
2056     {
2057         /* H.264 -> MPEG-4 */
2058         [fVidEncoderPopUp removeAllItems];
2059         [fVidEncoderPopUp addItemWithTitle: @"FFmpeg"];
2060         [fVidEncoderPopUp addItemWithTitle: @"XviD"];
2061         [fVidEncoderPopUp selectItemAtIndex: 0];
2062                                 
2063     }
2064
2065     if( FormatSettings[format][codecs] & HB_ACODEC_AC3 )
2066     {
2067         /* AC-3 pass-through: disable samplerate and bitrate */
2068         [fAudRatePopUp    setEnabled: NO];
2069         [fAudBitratePopUp setEnabled: NO];
2070     }
2071     else
2072     {
2073         [fAudRatePopUp    setEnabled: YES];
2074         [fAudBitratePopUp setEnabled: YES];
2075     }
2076     /* changing the codecs on offer may mean that we can / can't offer mono or 6ch, */
2077         /* so call audioTrackPopUpChanged for both audio tracks to update the mixdown popups */
2078         [self audioTrackPopUpChanged: fAudLang1PopUp];
2079         [self audioTrackPopUpChanged: fAudLang2PopUp];
2080
2081     [self calculateBitrate: sender];
2082     [self twoPassCheckboxChanged: sender];
2083 }
2084
2085 - (IBAction) encoderPopUpChanged: (id) sender
2086 {
2087     
2088         /* Check to see if we need to modify the job pic values based on x264 (iPod) encoder selection */
2089     if ([fDstFormatPopUp indexOfSelectedItem] == 0 && [fDstCodecsPopUp indexOfSelectedItem] == 1 && [fVidEncoderPopUp indexOfSelectedItem] == 1)
2090     {
2091                 hb_job_t * job = fTitle->job;
2092                 job->pixel_ratio = 0 ;
2093                 
2094                 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultPicSizeAutoiPod"] > 0)
2095                 {
2096                         
2097                         if (job->width > 640)
2098                         {
2099                                 job->width = 640;
2100                         }
2101                         job->keep_ratio = 1;
2102                         hb_fix_aspect( job, HB_KEEP_WIDTH );
2103                         
2104                 }
2105                 /* Make sure the 64bit formatting checkbox is off */
2106                 [fDstMpgLargeFileCheck setState: NSOffState];
2107         }
2108     
2109         [self calculatePictureSizing: sender];
2110         [self twoPassCheckboxChanged: sender];
2111 }
2112
2113 - (IBAction) twoPassCheckboxChanged: (id) sender
2114 {
2115         /* check to see if x264 is chosen */
2116         int format = [fDstFormatPopUp indexOfSelectedItem];
2117     int codecs = [fDstCodecsPopUp indexOfSelectedItem];
2118         if( ( FormatSettings[format][codecs] & HB_VCODEC_X264 ) )
2119     {
2120                 if( [fVidTwoPassCheck state] == NSOnState)
2121                 {
2122                         [fVidTurboPassCheck setHidden: NO];
2123                 }
2124                 else
2125                 {
2126                         [fVidTurboPassCheck setHidden: YES];
2127                         [fVidTurboPassCheck setState: NSOffState];
2128                 }
2129                 /* Make sure Two Pass is checked if Turbo is checked */
2130                 if( [fVidTurboPassCheck state] == NSOnState)
2131                 {
2132                         [fVidTwoPassCheck setState: NSOnState];
2133                 }
2134         }
2135         else
2136         {
2137                 [fVidTurboPassCheck setHidden: YES];
2138                 [fVidTurboPassCheck setState: NSOffState];
2139         }
2140         
2141         /* We call method method to change UI to reflect whether a preset is used or not*/
2142         [self customSettingUsed: sender];
2143 }
2144
2145 - (IBAction ) videoFrameRateChanged: (id) sender
2146 {
2147     /* We call method method to calculatePictureSizing to error check detelecine*/
2148     [self calculatePictureSizing: sender];
2149
2150     /* We call method method to change UI to reflect whether a preset is used or not*/
2151         [self customSettingUsed: sender];
2152 }
2153
2154 - (IBAction) setEnabledStateOfAudioMixdownControls: (id) sender
2155 {
2156
2157     /* enable/disable the mixdown text and popupbutton for audio track 1 */
2158     [fAudTrack1MixPopUp setEnabled: ([fAudLang1PopUp indexOfSelectedItem] == 0) ? NO : YES];
2159     [fAudTrack1MixLabel setTextColor: ([fAudLang1PopUp indexOfSelectedItem] == 0) ?
2160         [NSColor disabledControlTextColor] : [NSColor controlTextColor]];
2161
2162     /* enable/disable the mixdown text and popupbutton for audio track 2 */
2163     [fAudTrack2MixPopUp setEnabled: ([fAudLang2PopUp indexOfSelectedItem] == 0) ? NO : YES];
2164     [fAudTrack2MixLabel setTextColor: ([fAudLang2PopUp indexOfSelectedItem] == 0) ?
2165         [NSColor disabledControlTextColor] : [NSColor controlTextColor]];
2166
2167 }
2168
2169 - (IBAction) addAllAudioTracksToPopUp: (id) sender
2170 {
2171
2172     hb_list_t  * list  = hb_get_titles( fHandle );
2173     hb_title_t * title = (hb_title_t*)
2174         hb_list_item( list, [fSrcTitlePopUp indexOfSelectedItem] );
2175
2176         hb_audio_t * audio;
2177
2178     [sender removeAllItems];
2179     [sender addItemWithTitle: _( @"None" )];
2180     for( int i = 0; i < hb_list_count( title->list_audio ); i++ )
2181     {
2182         audio = (hb_audio_t *) hb_list_item( title->list_audio, i );
2183         [[sender menu] addItemWithTitle:
2184             [NSString stringWithCString: audio->lang]
2185             action: NULL keyEquivalent: @""];
2186     }
2187     [sender selectItemAtIndex: 0];
2188
2189 }
2190
2191 - (IBAction) selectAudioTrackInPopUp: (id) sender searchPrefixString: (NSString *) searchPrefixString selectIndexIfNotFound: (int) selectIndexIfNotFound
2192 {
2193
2194     /* this method can be used to find a language, or a language-and-source-format combination, by passing in the appropriate string */
2195     /* e.g. to find the first French track, pass in an NSString * of "Francais" */
2196     /* e.g. to find the first English 5.1 AC3 track, pass in an NSString * of "English (AC3) (5.1 ch)" */
2197     /* if no matching track is found, then selectIndexIfNotFound is used to choose which track to select instead */
2198     
2199         if (searchPrefixString != NULL) 
2200         {
2201
2202         for( int i = 0; i < [sender numberOfItems]; i++ )
2203         {
2204             /* Try to find the desired search string */
2205             if ([[[sender itemAtIndex: i] title] hasPrefix:searchPrefixString])
2206             {
2207                 [sender selectItemAtIndex: i];
2208                 return;
2209             }
2210         }
2211         /* couldn't find the string, so select the requested "search string not found" item */
2212         /* index of 0 means select the "none" item */
2213         /* index of 1 means select the first audio track */
2214         [sender selectItemAtIndex: selectIndexIfNotFound];
2215         }
2216     else
2217     {
2218         /* if no search string is provided, then select the selectIndexIfNotFound item */
2219         [sender selectItemAtIndex: selectIndexIfNotFound];
2220     }
2221
2222 }
2223
2224 - (IBAction) audioTrackPopUpChanged: (id) sender
2225 {
2226     /* utility function to call audioTrackPopUpChanged without passing in a mixdown-to-use */
2227     [self audioTrackPopUpChanged: sender mixdownToUse: 0];
2228 }
2229
2230 - (IBAction) audioTrackPopUpChanged: (id) sender mixdownToUse: (int) mixdownToUse
2231 {
2232
2233     /* make sure we have a selected title before continuing */
2234     if (fTitle == NULL) return;
2235
2236     /* find out if audio track 1 or 2 was changed - this is passed to us in the tag of the sender */
2237     /* the sender will have been either fAudLang1PopUp (tag = 0) or fAudLang2PopUp (tag = 1) */
2238     int thisAudio = [sender tag];
2239
2240     /* get the index of the selected audio */
2241     int thisAudioIndex = [sender indexOfSelectedItem] - 1;
2242
2243     /* Handbrake can't currently cope with ripping the same source track twice */
2244     /* So, if this audio is also selected in the other audio track popup, set that popup's selection to "none" */
2245     /* get a reference to the two audio track popups */
2246     NSPopUpButton * thisAudioPopUp  = (thisAudio == 1 ? fAudLang2PopUp : fAudLang1PopUp);
2247     NSPopUpButton * otherAudioPopUp = (thisAudio == 1 ? fAudLang1PopUp : fAudLang2PopUp);
2248     /* if the same track is selected in the other audio popup, then select "none" in that popup */
2249     /* unless, of course, both are selected as "none!" */
2250     if ([thisAudioPopUp indexOfSelectedItem] != 0 && [thisAudioPopUp indexOfSelectedItem] == [otherAudioPopUp indexOfSelectedItem]) {
2251         [otherAudioPopUp selectItemAtIndex: 0];
2252         [self audioTrackPopUpChanged: otherAudioPopUp];
2253     }
2254
2255     /* pointer for the hb_audio_s struct we will use later on */
2256     hb_audio_t * audio;
2257
2258     /* find out what the currently-selected output audio codec is */
2259     int format = [fDstFormatPopUp indexOfSelectedItem];
2260     int codecs = [fDstCodecsPopUp indexOfSelectedItem];
2261     int acodec = FormatSettings[format][codecs] & HB_ACODEC_MASK;
2262
2263     /* pointer to this track's mixdown NSPopUpButton */
2264     NSTextField   * mixdownTextField;
2265     NSPopUpButton * mixdownPopUp;
2266
2267     /* find our mixdown NSTextField and NSPopUpButton */
2268     if (thisAudio == 0)
2269     {
2270         mixdownTextField = fAudTrack1MixLabel;
2271         mixdownPopUp = fAudTrack1MixPopUp;
2272     }
2273     else
2274     {
2275         mixdownTextField = fAudTrack2MixLabel;
2276         mixdownPopUp = fAudTrack2MixPopUp;
2277     }
2278
2279     /* delete the previous audio mixdown options */
2280     [mixdownPopUp removeAllItems];
2281
2282     /* check if the audio mixdown controls need their enabled state changing */
2283     [self setEnabledStateOfAudioMixdownControls: NULL];
2284
2285     if (thisAudioIndex != -1)
2286     {
2287
2288         /* get the audio */
2289         audio = (hb_audio_t *) hb_list_item( fTitle->list_audio, thisAudioIndex );
2290         if (audio != NULL)
2291         {
2292
2293             /* find out if our selected output audio codec supports mono and / or 6ch */
2294             /* we also check for an input codec of AC3 or DCA,
2295                as they are the only libraries able to do the mixdown to mono / conversion to 6-ch */
2296             /* audioCodecsSupportMono and audioCodecsSupport6Ch are the same for now,
2297                but this may change in the future, so they are separated for flexibility */
2298             int audioCodecsSupportMono = ((audio->codec == HB_ACODEC_AC3 ||
2299                 audio->codec == HB_ACODEC_DCA) && acodec == HB_ACODEC_FAAC);
2300             int audioCodecsSupport6Ch =  ((audio->codec == HB_ACODEC_AC3 ||
2301                 audio->codec == HB_ACODEC_DCA) && acodec == HB_ACODEC_FAAC);
2302
2303             /* check for AC-3 passthru */
2304             if (audio->codec == HB_ACODEC_AC3 && acodec == HB_ACODEC_AC3)
2305             {
2306                     [[mixdownPopUp menu] addItemWithTitle:
2307                         [NSString stringWithCString: "AC3 Passthru"]
2308                         action: NULL keyEquivalent: @""];
2309             }
2310             else
2311             {
2312
2313                 /* add the appropriate audio mixdown menuitems to the popupbutton */
2314                 /* in each case, we set the new menuitem's tag to be the amixdown value for that mixdown,
2315                    so that we can reference the mixdown later */
2316
2317                 /* keep a track of the min and max mixdowns we used, so we can select the best match later */
2318                 int minMixdownUsed = 0;
2319                 int maxMixdownUsed = 0;
2320                 
2321                 /* get the input channel layout without any lfe channels */
2322                 int layout = audio->input_channel_layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK;
2323
2324                 /* do we want to add a mono option? */
2325                 if (audioCodecsSupportMono == 1) {
2326                     id<NSMenuItem> menuItem = [[mixdownPopUp menu] addItemWithTitle:
2327                         [NSString stringWithCString: hb_audio_mixdowns[0].human_readable_name]
2328                         action: NULL keyEquivalent: @""];
2329                     [menuItem setTag: hb_audio_mixdowns[0].amixdown];
2330                     if (minMixdownUsed == 0) minMixdownUsed = hb_audio_mixdowns[0].amixdown;
2331                     maxMixdownUsed = MAX(maxMixdownUsed, hb_audio_mixdowns[0].amixdown);
2332                 }
2333
2334                 /* do we want to add a stereo option? */
2335                 /* offer stereo if we have a mono source and non-mono-supporting codecs, as otherwise we won't have a mixdown at all */
2336                 /* also offer stereo if we have a stereo-or-better source */
2337                 if ((layout == HB_INPUT_CH_LAYOUT_MONO && audioCodecsSupportMono == 0) || layout >= HB_INPUT_CH_LAYOUT_STEREO) {
2338                     id<NSMenuItem> menuItem = [[mixdownPopUp menu] addItemWithTitle:
2339                         [NSString stringWithCString: hb_audio_mixdowns[1].human_readable_name]
2340                         action: NULL keyEquivalent: @""];
2341                     [menuItem setTag: hb_audio_mixdowns[1].amixdown];
2342                     if (minMixdownUsed == 0) minMixdownUsed = hb_audio_mixdowns[1].amixdown;
2343                     maxMixdownUsed = MAX(maxMixdownUsed, hb_audio_mixdowns[1].amixdown);
2344                 }
2345
2346                 /* do we want to add a dolby surround (DPL1) option? */
2347                 if (layout == HB_INPUT_CH_LAYOUT_3F1R || layout == HB_INPUT_CH_LAYOUT_3F2R || layout == HB_INPUT_CH_LAYOUT_DOLBY) {
2348                     id<NSMenuItem> menuItem = [[mixdownPopUp menu] addItemWithTitle:
2349                         [NSString stringWithCString: hb_audio_mixdowns[2].human_readable_name]
2350                         action: NULL keyEquivalent: @""];
2351                     [menuItem setTag: hb_audio_mixdowns[2].amixdown];
2352                     if (minMixdownUsed == 0) minMixdownUsed = hb_audio_mixdowns[2].amixdown;
2353                     maxMixdownUsed = MAX(maxMixdownUsed, hb_audio_mixdowns[2].amixdown);
2354                 }
2355
2356                 /* do we want to add a dolby pro logic 2 (DPL2) option? */
2357                 if (layout == HB_INPUT_CH_LAYOUT_3F2R) {
2358                     id<NSMenuItem> menuItem = [[mixdownPopUp menu] addItemWithTitle:
2359                         [NSString stringWithCString: hb_audio_mixdowns[3].human_readable_name]
2360                         action: NULL keyEquivalent: @""];
2361                     [menuItem setTag: hb_audio_mixdowns[3].amixdown];
2362                     if (minMixdownUsed == 0) minMixdownUsed = hb_audio_mixdowns[3].amixdown;
2363                     maxMixdownUsed = MAX(maxMixdownUsed, hb_audio_mixdowns[3].amixdown);
2364                 }
2365
2366                 /* do we want to add a 6-channel discrete option? */
2367                 if (audioCodecsSupport6Ch == 1 && layout == HB_INPUT_CH_LAYOUT_3F2R && (audio->input_channel_layout & HB_INPUT_CH_LAYOUT_HAS_LFE)) {
2368                     id<NSMenuItem> menuItem = [[mixdownPopUp menu] addItemWithTitle:
2369                         [NSString stringWithCString: hb_audio_mixdowns[4].human_readable_name]
2370                         action: NULL keyEquivalent: @""];
2371                     [menuItem setTag: hb_audio_mixdowns[4].amixdown];
2372                     if (minMixdownUsed == 0) minMixdownUsed = hb_audio_mixdowns[4].amixdown;
2373                     maxMixdownUsed = MAX(maxMixdownUsed, hb_audio_mixdowns[4].amixdown);
2374                 }
2375
2376                 /* auto-select the best mixdown based on our saved mixdown preference */
2377                 
2378                 /* for now, this is hard-coded to a "best" mixdown of HB_AMIXDOWN_DOLBYPLII */
2379                 /* ultimately this should be a prefs option */
2380                 int useMixdown;
2381                 
2382                 /* if we passed in a mixdown to use - in order to load a preset - then try and use it */
2383                 if (mixdownToUse > 0)
2384                 {
2385                     useMixdown = mixdownToUse;
2386                 }
2387                 else
2388                 {
2389                     useMixdown = HB_AMIXDOWN_DOLBYPLII;
2390                 }
2391                 
2392                 /* if useMixdown > maxMixdownUsed, then use maxMixdownUsed */
2393                 if (useMixdown > maxMixdownUsed) useMixdown = maxMixdownUsed;
2394
2395                 /* if useMixdown < minMixdownUsed, then use minMixdownUsed */
2396                 if (useMixdown < minMixdownUsed) useMixdown = minMixdownUsed;
2397
2398                 /* select the (possibly-amended) preferred mixdown */
2399                 [mixdownPopUp selectItemWithTag: useMixdown];
2400                                 
2401                                 /* lets call the audioTrackMixdownChanged method here to determine appropriate bitrates, etc. */
2402                 [self audioTrackMixdownChanged: NULL];
2403             }
2404
2405         }
2406         
2407     }
2408
2409         /* see if the new audio track choice will change the bitrate we need */
2410     [self calculateBitrate: sender];    
2411
2412 }
2413 - (IBAction) audioTrackMixdownChanged: (id) sender
2414 {
2415
2416     /* find out what the currently-selected output audio codec is */
2417     int format = [fDstFormatPopUp indexOfSelectedItem];
2418     int codecs = [fDstCodecsPopUp indexOfSelectedItem];
2419     int acodec = FormatSettings[format][codecs] & HB_ACODEC_MASK;
2420     
2421     /* storage variable for the min and max bitrate allowed for this codec */
2422     int minbitrate;
2423     int maxbitrate;
2424     
2425     switch( acodec )
2426     {
2427         case HB_ACODEC_FAAC:
2428             /* check if we have a 6ch discrete conversion in either audio track */
2429             if ([[fAudTrack1MixPopUp selectedItem] tag] == HB_AMIXDOWN_6CH || [[fAudTrack2MixPopUp selectedItem] tag] == HB_AMIXDOWN_6CH)
2430             {
2431                 /* FAAC is happy using our min bitrate of 32 kbps, even for 6ch */
2432                 minbitrate = 32;
2433                 /* If either mixdown popup includes 6-channel discrete, then allow up to 384 kbps */
2434                 maxbitrate = 384;
2435                 break;
2436             }
2437             else
2438             {
2439                 /* FAAC is happy using our min bitrate of 32 kbps for stereo or mono */
2440                 minbitrate = 32;
2441                 /* FAAC won't honour anything more than 160 for stereo, so let's not offer it */
2442                 /* note: haven't dealt with mono separately here, FAAC will just use the max it can */
2443                 maxbitrate = 160;
2444                 break;
2445             }
2446
2447         case HB_ACODEC_LAME:
2448             /* Lame is happy using our min bitrate of 32 kbps */
2449             minbitrate = 32;
2450             /* Lame won't encode if the bitrate is higher than 320 kbps */
2451             maxbitrate = 320;
2452             break;
2453
2454         case HB_ACODEC_VORBIS:
2455             /* Vorbis causes a crash if we use a bitrate below 48 kbps */
2456             minbitrate = 48;
2457             /* Vorbis can cope with 384 kbps quite happily, even for stereo */
2458             maxbitrate = 384;
2459             break;
2460
2461         default:
2462             /* AC3 passthru disables the bitrate dropdown anyway, so we might as well just use the min and max bitrate */
2463             minbitrate = 32;
2464             maxbitrate = 384;
2465         
2466     }
2467
2468     [fAudBitratePopUp removeAllItems];
2469
2470     for( int i = 0; i < hb_audio_bitrates_count; i++ )
2471     {
2472         if (hb_audio_bitrates[i].rate >= minbitrate && hb_audio_bitrates[i].rate <= maxbitrate)
2473         {
2474             /* add a new menuitem for this bitrate */
2475             id<NSMenuItem> menuItem = [[fAudBitratePopUp menu] addItemWithTitle:
2476                 [NSString stringWithCString: hb_audio_bitrates[i].string]
2477                 action: NULL keyEquivalent: @""];
2478             /* set its tag to be the actual bitrate as an integer, so we can retrieve it later */
2479             [menuItem setTag: hb_audio_bitrates[i].rate];
2480         }
2481     }
2482
2483     /* select the default bitrate (but use 384 for 6-ch AAC) */
2484     if ([[fAudTrack1MixPopUp selectedItem] tag] == HB_AMIXDOWN_6CH || [[fAudTrack2MixPopUp selectedItem] tag] == HB_AMIXDOWN_6CH)
2485     {
2486         [fAudBitratePopUp selectItemWithTag: 384];
2487     }
2488     else
2489     {
2490         [fAudBitratePopUp selectItemWithTag: hb_audio_bitrates[hb_audio_bitrates_default].rate];
2491     }
2492
2493 }
2494
2495 - (IBAction) subtitleSelectionChanged: (id) sender
2496 {
2497         if ([fSubPopUp indexOfSelectedItem] == 0)
2498         {
2499         [fSubForcedCheck setState: NSOffState];
2500         [fSubForcedCheck setEnabled: NO];       
2501         }
2502         else
2503         {
2504         [fSubForcedCheck setEnabled: YES];      
2505         }
2506         
2507 }
2508
2509 /* lets set the picture size back to the max from right after title scan
2510    Lets use an IBAction here as down the road we could always use a checkbox
2511    in the gui to easily take the user back to max. Remember, the compiler
2512    resolves IBActions down to -(void) during compile anyway */
2513 - (IBAction) revertPictureSizeToMax: (id) sender
2514 {
2515         hb_job_t * job = fTitle->job;
2516         /* We use the output picture width and height
2517         as calculated from libhb right after title is set
2518         in TitlePopUpChanged */
2519         job->width = PicOrigOutputWidth;
2520         job->height = PicOrigOutputHeight;
2521     [fPictureController setAutoCrop:YES];
2522         /* Here we use the auto crop values determined right after scan */
2523         job->crop[0] = AutoCropTop;
2524         job->crop[1] = AutoCropBottom;
2525         job->crop[2] = AutoCropLeft;
2526         job->crop[3] = AutoCropRight;
2527                                 
2528                                 
2529                                 [self calculatePictureSizing: sender];
2530                                 /* We call method method to change UI to reflect whether a preset is used or not*/    
2531                                 [self customSettingUsed: sender];
2532 }
2533
2534
2535 /* Get and Display Current Pic Settings in main window */
2536 - (IBAction) calculatePictureSizing: (id) sender
2537 {
2538         [fPicSettingWidth setStringValue: [NSString stringWithFormat:@"%d", fTitle->job->width]];
2539         [fPicSettingHeight setStringValue: [NSString stringWithFormat:@"%d", fTitle->job->height]];
2540                 
2541         if (fTitle->job->pixel_ratio == 1)
2542         {
2543         int titlewidth = fTitle->width-fTitle->job->crop[2]-fTitle->job->crop[3];
2544         int arpwidth = fTitle->job->pixel_aspect_width;
2545         int arpheight = fTitle->job->pixel_aspect_height;
2546         int displayparwidth = titlewidth * arpwidth / arpheight;
2547         int displayparheight = fTitle->height-fTitle->job->crop[0]-fTitle->job->crop[1];
2548         
2549         [fPicSettingWidth setStringValue: [NSString stringWithFormat:@"%d", titlewidth]];
2550         [fPicSettingHeight setStringValue: [NSString stringWithFormat:@"%d", displayparheight]];
2551         [fPicLabelPAROutputX setStringValue: @"x"];
2552         [fPicSettingPARWidth setStringValue: [NSString stringWithFormat:@"%d", displayparwidth]];
2553         [fPicSettingPARHeight setStringValue: [NSString stringWithFormat:@"%d", displayparheight]];
2554         
2555         fTitle->job->keep_ratio = 0;
2556         }
2557         else
2558         {
2559         [fPicLabelPAROutputX setStringValue: @""];
2560         [fPicSettingPARWidth setStringValue: @""];
2561         [fPicSettingPARHeight setStringValue:  @""];
2562         }
2563                                 
2564         /* Set ON/Off values for the deinterlace/keep aspect ratio according to boolean */      
2565         if (fTitle->job->keep_ratio > 0)
2566         {
2567                 [fPicSettingARkeep setStringValue: @"On"];
2568         }
2569         else
2570         {
2571                 [fPicSettingARkeep setStringValue: @"Off"];
2572         }       
2573     /* Detelecine */
2574     if ([fPictureController detelecine]) {
2575         [fPicSettingDetelecine setStringValue: @"Yes"];
2576     }
2577     else {
2578         [fPicSettingDetelecine setStringValue: @"No"];
2579     }
2580
2581         /* Deinterlace */
2582         if ([fPictureController deinterlace] == 0)
2583         {
2584                 [fPicSettingDeinterlace setStringValue: @"Off"];
2585         }
2586         else if ([fPictureController deinterlace] == 1)
2587         {
2588                 [fPicSettingDeinterlace setStringValue: @"Fast"];
2589         }
2590         else if ([fPictureController deinterlace] == 2)
2591         {
2592                 [fPicSettingDeinterlace setStringValue: @"Slow"];
2593         }
2594         else if ([fPictureController deinterlace] == 3)
2595         {
2596                 [fPicSettingDeinterlace setStringValue: @"Slower"];
2597         }
2598         else if ([fPictureController deinterlace] ==4)
2599         {
2600                 [fPicSettingDeinterlace setStringValue: @"Slowest"];
2601         }
2602         /* Denoise */
2603         if ([fPictureController denoise] == 0)
2604         {
2605                 [fPicSettingDenoise setStringValue: @"Off"];
2606         }
2607         else if ([fPictureController denoise] == 1)
2608         {
2609                 [fPicSettingDenoise setStringValue: @"Weak"];
2610         }
2611         else if ([fPictureController denoise] == 2)
2612         {
2613                 [fPicSettingDenoise setStringValue: @"Medium"];
2614         }
2615         else if ([fPictureController denoise] == 3)
2616         {
2617                 [fPicSettingDenoise setStringValue: @"Strong"];
2618         }
2619         
2620         if (fTitle->job->pixel_ratio > 0)
2621         {
2622                 [fPicSettingPAR setStringValue: @""];
2623         }
2624         else
2625         {
2626                 [fPicSettingPAR setStringValue: @"Off"];
2627         }
2628         /* Set the display field for crop as per boolean */
2629         if (![fPictureController autoCrop])
2630         {
2631             [fPicSettingAutoCrop setStringValue: @"Custom"];
2632         }
2633         else
2634         {
2635                 [fPicSettingAutoCrop setStringValue: @"Auto"];
2636         }       
2637         /* check video framerate and turn off detelecine if necessary */
2638         if (fTitle->rate_base == 1126125 || [[fVidRatePopUp titleOfSelectedItem] isEqualToString: @"23.976 (NTSC Film)"])
2639         {
2640                 [fPictureController setDetelecine:0];
2641         }
2642         
2643         
2644         
2645         /* below will trigger the preset, if selected, to be
2646         changed to "Custom". Lets comment out for now until
2647         we figure out a way to determine if the picture values
2648         changed modify the preset values */     
2649         //[self customSettingUsed: sender];
2650 }
2651
2652 - (IBAction) calculateBitrate: (id) sender
2653 {
2654     if( !fHandle || [fVidQualityMatrix selectedRow] != 0 )
2655     {
2656         return;
2657     }
2658
2659     hb_list_t  * list  = hb_get_titles( fHandle );
2660     hb_title_t * title = (hb_title_t *) hb_list_item( list,
2661             [fSrcTitlePopUp indexOfSelectedItem] );
2662     hb_job_t * job = title->job;
2663
2664     [self PrepareJob];
2665
2666     [fVidBitrateField setIntValue: hb_calc_bitrate( job,
2667             [fVidTargetSizeField intValue] )];
2668 }
2669
2670 /* Method to determine if we should change the UI
2671 To reflect whether or not a Preset is being used or if
2672 the user is using "Custom" settings by determining the sender*/
2673 - (IBAction) customSettingUsed: (id) sender
2674 {
2675         if ([sender stringValue] != NULL)
2676         {
2677                 /* Deselect the currently selected Preset if there is one*/
2678                 [tableView deselectRow:[tableView selectedRow]];
2679                 [[fPresetsActionMenu itemAtIndex:0] setEnabled: NO];
2680                 /* Change UI to show "Custom" settings are being used */
2681                 [fPresetSelectedDisplay setStringValue: @"Custom"];
2682                 
2683                 curUserPresetChosenNum = nil;
2684         }
2685
2686 }
2687
2688    /* We use this method to recreate new, updated factory
2689    presets */
2690 - (IBAction)addFactoryPresets:(id)sender
2691 {
2692     /* First, we delete any existing built in presets */
2693     [self deleteFactoryPresets: sender];
2694     /* Then, we re-create new built in presets programmatically CreateIpodOnlyPreset*/
2695     [UserPresets addObject:[self createNormalPreset]];
2696     [UserPresets addObject:[self createClassicPreset]];
2697     [UserPresets addObject:[self createQuickTimePreset]];
2698         [UserPresets addObject:[self createIpodLowPreset]];
2699         [UserPresets addObject:[self createIpodHighPreset]];
2700         [UserPresets addObject:[self createAppleTVPreset]];
2701     [UserPresets addObject:[self createiPhonePreset]];
2702         [UserPresets addObject:[self createPSThreePreset]];
2703         [UserPresets addObject:[self createPSPPreset]];
2704         [UserPresets addObject:[self createFilmPreset]];
2705     [UserPresets addObject:[self createTelevisionPreset]];
2706     [UserPresets addObject:[self createAnimationPreset]];
2707     [UserPresets addObject:[self createBedlamPreset]];
2708     [UserPresets addObject:[self createDeuxSixQuatrePreset]];
2709     [UserPresets addObject:[self createBrokePreset]];
2710     [UserPresets addObject:[self createBlindPreset]];
2711     [UserPresets addObject:[self createCRFPreset]];
2712     
2713     [self addPreset];
2714 }
2715 - (IBAction)deleteFactoryPresets:(id)sender
2716 {
2717     //int status;
2718     NSEnumerator *enumerator = [UserPresets objectEnumerator];
2719         id tempObject;
2720     
2721         //NSNumber *index;
2722     NSMutableArray *tempArray;
2723
2724
2725         tempArray = [NSMutableArray array];
2726         /* we look here to see if the preset is we move on to the next one */
2727         while ( tempObject = [enumerator nextObject] )  
2728                 {
2729                         /* if the preset is "Factory" then we put it in the array of
2730                         presets to delete */
2731                         if ([[tempObject objectForKey:@"Type"] intValue] == 0)
2732                         {
2733                                 [tempArray addObject:tempObject];
2734                         }
2735         }
2736         
2737         [UserPresets removeObjectsInArray:tempArray];
2738         [tableView reloadData];
2739         [self savePreset];   
2740
2741 }
2742
2743 - (IBAction) showAddPresetPanel: (id) sender
2744 {
2745     /* Deselect the currently selected Preset if there is one*/
2746     [tableView deselectRow:[tableView selectedRow]];
2747
2748     /* Populate the preset picture settings popup here */
2749     [fPresetNewPicSettingsPopUp removeAllItems];
2750     [fPresetNewPicSettingsPopUp addItemWithTitle:@"None"];
2751     [fPresetNewPicSettingsPopUp addItemWithTitle:@"Current"];
2752     [fPresetNewPicSettingsPopUp addItemWithTitle:@"Source Maximum (post source scan)"];
2753     [fPresetNewPicSettingsPopUp selectItemAtIndex: 0];  
2754         
2755     /* Erase info from the input fields fPresetNewDesc*/
2756         [fPresetNewName setStringValue: @""];
2757         [fPresetNewDesc setStringValue: @""];
2758         /* Show the panel */
2759         [NSApp beginSheet: fAddPresetPanel modalForWindow: fWindow modalDelegate: NULL didEndSelector: NULL contextInfo: NULL];
2760 }
2761
2762 - (IBAction) closeAddPresetPanel: (id) sender
2763 {
2764     [NSApp endSheet: fAddPresetPanel];
2765     [fAddPresetPanel orderOut: self];
2766 }
2767
2768 - (IBAction)addUserPreset:(id)sender
2769 {
2770     if (![[fPresetNewName stringValue] length])
2771             NSRunAlertPanel(@"Warning!", @"You need to insert a name for the preset.", @"OK", nil , nil);
2772     else
2773     {
2774         /* Here we create a custom user preset */
2775         [UserPresets addObject:[self createPreset]];
2776         [self addPreset];
2777         
2778         [self closeAddPresetPanel:NULL];
2779     }
2780 }
2781 - (void)addPreset
2782 {
2783
2784         
2785         /* We Sort the Presets By Factory or Custom */
2786         NSSortDescriptor * presetTypeDescriptor=[[[NSSortDescriptor alloc] initWithKey:@"Type" 
2787                                                     ascending:YES] autorelease];
2788         /* We Sort the Presets Alphabetically by name */
2789         NSSortDescriptor * presetNameDescriptor=[[[NSSortDescriptor alloc] initWithKey:@"PresetName" 
2790                                                     ascending:YES selector:@selector(caseInsensitiveCompare:)] autorelease];
2791         NSArray *sortDescriptors=[NSArray arrayWithObjects:presetTypeDescriptor,presetNameDescriptor,nil];
2792         NSArray *sortedArray=[UserPresets sortedArrayUsingDescriptors:sortDescriptors];
2793         [UserPresets setArray:sortedArray];
2794         
2795         
2796         /* We Reload the New Table data for presets */
2797     [tableView reloadData];
2798    /* We save all of the preset data here */
2799     [self savePreset];
2800 }
2801
2802 - (IBAction)insertPreset:(id)sender
2803 {
2804     int index = [tableView selectedRow];
2805     [UserPresets insertObject:[self createPreset] atIndex:index];
2806     [tableView reloadData];
2807     [self savePreset];
2808 }
2809
2810 - (NSDictionary *)createPreset
2811 {
2812     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
2813         /* Get the New Preset Name from the field in the AddPresetPanel */
2814     [preset setObject:[fPresetNewName stringValue] forKey:@"PresetName"];
2815         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
2816         [preset setObject:[NSNumber numberWithInt:1] forKey:@"Type"];
2817         /*Set whether or not this is default, at creation set to 0*/
2818         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
2819         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
2820         [preset setObject:[NSNumber numberWithInt:[fPresetNewPicSettingsPopUp indexOfSelectedItem]] forKey:@"UsesPictureSettings"];
2821         /* Get New Preset Description from the field in the AddPresetPanel*/
2822         [preset setObject:[fPresetNewDesc stringValue] forKey:@"PresetDescription"];
2823         /* File Format */
2824     [preset setObject:[fDstFormatPopUp titleOfSelectedItem] forKey:@"FileFormat"];
2825         /* Chapter Markers fCreateChapterMarkers*/
2826         [preset setObject:[NSNumber numberWithInt:[fCreateChapterMarkers state]] forKey:@"ChapterMarkers"];
2827         /* Allow Mpeg4 64 bit formatting +4GB file sizes */
2828         [preset setObject:[NSNumber numberWithInt:[fDstMpgLargeFileCheck state]] forKey:@"Mp4LargeFile"];
2829         /* Codecs */
2830         [preset setObject:[fDstCodecsPopUp titleOfSelectedItem] forKey:@"FileCodecs"];
2831         /* Video encoder */
2832         [preset setObject:[fVidEncoderPopUp titleOfSelectedItem] forKey:@"VideoEncoder"];
2833         /* x264 Option String */
2834         [preset setObject:[fAdvancedOptions optionsString] forKey:@"x264Option"];
2835         
2836         [preset setObject:[NSNumber numberWithInt:[fVidQualityMatrix selectedRow]] forKey:@"VideoQualityType"];
2837         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
2838         [preset setObject:[fVidBitrateField stringValue] forKey:@"VideoAvgBitrate"];
2839         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
2840         
2841         /* Video framerate */
2842         [preset setObject:[fVidRatePopUp titleOfSelectedItem] forKey:@"VideoFramerate"];
2843         /* GrayScale */
2844         [preset setObject:[NSNumber numberWithInt:[fVidGrayscaleCheck state]] forKey:@"VideoGrayScale"];
2845         /* 2 Pass Encoding */
2846         [preset setObject:[NSNumber numberWithInt:[fVidTwoPassCheck state]] forKey:@"VideoTwoPass"];
2847         /* Turbo 2 pass Encoding fVidTurboPassCheck*/
2848         [preset setObject:[NSNumber numberWithInt:[fVidTurboPassCheck state]] forKey:@"VideoTurboTwoPass"];
2849         /*Picture Settings*/
2850         hb_job_t * job = fTitle->job;
2851         /* Basic Picture Settings */
2852         /* Use Max Picture settings for whatever the dvd is.*/
2853         [preset setObject:[NSNumber numberWithInt:0] forKey:@"UsesMaxPictureSettings"];
2854         [preset setObject:[NSNumber numberWithInt:fTitle->job->width] forKey:@"PictureWidth"];
2855         [preset setObject:[NSNumber numberWithInt:fTitle->job->height] forKey:@"PictureHeight"];
2856         [preset setObject:[NSNumber numberWithInt:fTitle->job->keep_ratio] forKey:@"PictureKeepRatio"];
2857         [preset setObject:[NSNumber numberWithInt:[fPictureController deinterlace]] forKey:@"PictureDeinterlace"];
2858         [preset setObject:[NSNumber numberWithInt:fTitle->job->pixel_ratio] forKey:@"PicturePAR"];
2859         [preset setObject:[NSNumber numberWithInt:[fPictureController detelecine]] forKey:@"PictureDetelecine"];
2860         [preset setObject:[NSNumber numberWithInt:[fPictureController denoise]] forKey:@"PictureDenoise"]; 
2861         /* Set crop settings here */
2862         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
2863         [preset setObject:[NSNumber numberWithInt:[fPictureController autoCrop]] forKey:@"PictureAutoCrop"];
2864
2865         [preset setObject:[NSNumber numberWithInt:job->crop[0]] forKey:@"PictureTopCrop"];
2866     [preset setObject:[NSNumber numberWithInt:job->crop[1]] forKey:@"PictureBottomCrop"];
2867         [preset setObject:[NSNumber numberWithInt:job->crop[2]] forKey:@"PictureLeftCrop"];
2868         [preset setObject:[NSNumber numberWithInt:job->crop[3]] forKey:@"PictureRightCrop"];
2869         
2870         /*Audio*/
2871         /* Audio Sample Rate*/
2872         [preset setObject:[fAudRatePopUp titleOfSelectedItem] forKey:@"AudioSampleRate"];
2873         /* Audio Bitrate Rate*/
2874         [preset setObject:[fAudBitratePopUp titleOfSelectedItem] forKey:@"AudioBitRate"];
2875         /* Subtitles*/
2876         [preset setObject:[fSubPopUp titleOfSelectedItem] forKey:@"Subtitles"];
2877         
2878
2879     [preset autorelease];
2880     return preset;
2881
2882 }
2883
2884 - (NSDictionary *)createIpodLowPreset
2885 {
2886     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
2887         /* Get the New Preset Name from the field in the AddPresetPanel */
2888     [preset setObject:@"iPod Low-Rez" forKey:@"PresetName"];
2889         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
2890         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
2891         /*Set whether or not this is default, at creation set to 0*/
2892         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
2893         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
2894         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
2895         /* Get the New Preset Description from the field in the AddPresetPanel */
2896     [preset setObject:@"HandBrake's low resolution settings for the iPod. Optimized for great playback on the iPod screen, with smaller file size." forKey:@"PresetDescription"];
2897         /* File Format */
2898     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
2899         /* Chapter Markers*/
2900          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
2901     /* Codecs */
2902         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
2903         /* Video encoder */
2904         [preset setObject:@"x264 (h.264 iPod)" forKey:@"VideoEncoder"];
2905         /* x264 Option String */
2906         [preset setObject:@"keyint=300:keyint-min=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1" forKey:@"x264Option"];
2907         /* Video quality */
2908         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
2909         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
2910         [preset setObject:@"700" forKey:@"VideoAvgBitrate"];
2911         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
2912         
2913         /* Video framerate */
2914         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
2915         /* GrayScale */
2916         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
2917         /* 2 Pass Encoding */
2918         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
2919         
2920         /*Picture Settings*/
2921         //hb_job_t * job = fTitle->job;
2922         /* Basic Picture Settings */
2923         /* Use Max Picture settings for whatever the dvd is.*/
2924         [preset setObject:[NSNumber numberWithInt:0] forKey:@"UsesMaxPictureSettings"];
2925         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
2926         [preset setObject:[NSNumber numberWithInt:320] forKey:@"PictureWidth"];
2927         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
2928         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
2929         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
2930         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
2931         /* Set crop settings here */
2932         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
2933         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
2934     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
2935         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
2936         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
2937         
2938         /*Audio*/
2939         /* Audio Sample Rate*/
2940         [preset setObject:@"48" forKey:@"AudioSampleRate"];
2941         /* Audio Bitrate Rate*/
2942         [preset setObject:@"160" forKey:@"AudioBitRate"];
2943         /* Subtitles*/
2944         [preset setObject:@"None" forKey:@"Subtitles"];
2945         
2946
2947     [preset autorelease];
2948     return preset;
2949
2950 }
2951
2952
2953 - (NSDictionary *)createIpodHighPreset
2954 {
2955     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
2956         /* Get the New Preset Name from the field in the AddPresetPanel */
2957     [preset setObject:@"iPod High-Rez" forKey:@"PresetName"];
2958         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
2959         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
2960         /*Set whether or not this is default, at creation set to 0*/
2961         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
2962         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
2963         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
2964         /* Get the New Preset Description from the field in the AddPresetPanel */
2965     [preset setObject:@"HandBrake's high resolution settings for the iPod. Good video quality, great for viewing on a TV using your iPod" forKey:@"PresetDescription"];
2966         /* File Format */
2967     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
2968         /* Chapter Markers*/
2969          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
2970     /* Codecs */
2971         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
2972         /* Video encoder */
2973         [preset setObject:@"x264 (h.264 iPod)" forKey:@"VideoEncoder"];
2974         /* x264 Option String */
2975         [preset setObject:@"keyint=300:keyint-min=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1" forKey:@"x264Option"];
2976         /* Video quality */
2977         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
2978         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
2979         [preset setObject:@"1500" forKey:@"VideoAvgBitrate"];
2980         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
2981         
2982         /* Video framerate */
2983         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
2984         /* GrayScale */
2985         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
2986         /* 2 Pass Encoding */
2987         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
2988         
2989         /*Picture Settings*/
2990         //hb_job_t * job = fTitle->job;
2991         /* Basic Picture Settings */
2992         /* Use Max Picture settings for whatever the dvd is.*/
2993         [preset setObject:[NSNumber numberWithInt:0] forKey:@"UsesMaxPictureSettings"];
2994         [preset setObject:[NSNumber numberWithInt:640] forKey:@"PictureWidth"];
2995         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
2996         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
2997         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
2998         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
2999         /* Set crop settings here */
3000         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3001         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3002         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3003     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3004         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3005         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3006         
3007         /*Audio*/
3008         /* Audio Sample Rate*/
3009         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3010         /* Audio Bitrate Rate*/
3011         [preset setObject:@"160" forKey:@"AudioBitRate"];
3012         /* Subtitles*/
3013         [preset setObject:@"None" forKey:@"Subtitles"];
3014         
3015
3016     [preset autorelease];
3017     return preset;
3018
3019 }
3020
3021 - (NSDictionary *)createAppleTVPreset
3022 {
3023     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3024         /* Get the New Preset Name from the field in the AddPresetPanel */
3025     [preset setObject:@"AppleTV" forKey:@"PresetName"];
3026         /*Set whether or not this is a user preset where 0 is factory, 1 is user*/
3027         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3028         /*Set whether or not this is default, at creation set to 0*/
3029         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3030         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3031         [preset setObject:[NSNumber numberWithInt:2] forKey:@"UsesPictureSettings"];
3032         /* Get the New Preset Description from the field in the AddPresetPanel */
3033     [preset setObject:@"HandBrake's settings for the AppleTV. Provides a good balance between quality and file size, and optimizes performance." forKey:@"PresetDescription"];
3034         /* File Format */
3035     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3036         /* Chapter Markers*/
3037          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3038         /* Codecs */
3039         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
3040         /* Video encoder */
3041         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
3042         /* x264 Option String (We can use this to tweak the appleTV output)*/
3043         [preset setObject:@"bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=2:cabac=0" forKey:@"x264Option"];
3044         /* Video quality */
3045         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3046         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3047         [preset setObject:@"2500" forKey:@"VideoAvgBitrate"];
3048         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3049         
3050         /* Video framerate */
3051         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3052         /* GrayScale */
3053         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3054         /* 2 Pass Encoding */
3055         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
3056         
3057         /*Picture Settings*/
3058         /* For AppleTV we only want to retain UsesMaxPictureSettings
3059         which depend on the source dvd picture settings, so we don't
3060         record the current dvd's picture info since it will vary from
3061         source to source*/
3062         //hb_job_t * job = fTitle->job;
3063         //hb_job_t * job = title->job;
3064         /* Basic Picture Settings */
3065         /* Use Max Picture settings for whatever the dvd is.*/
3066         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
3067         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
3068         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3069         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
3070         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3071         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
3072         /* Set crop settings here */
3073         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3074         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3075     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3076         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3077         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3078         
3079         /*Audio*/
3080         /* Audio Sample Rate*/
3081         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3082         /* Audio Bitrate Rate*/
3083         [preset setObject:@"160" forKey:@"AudioBitRate"];
3084         /* Subtitles*/
3085         [preset setObject:@"None" forKey:@"Subtitles"];
3086         
3087
3088     [preset autorelease];
3089     return preset;
3090
3091 }
3092
3093 - (NSDictionary *)createPSThreePreset
3094 {
3095     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3096         /* Get the New Preset Name from the field in the AddPresetPanel */
3097     [preset setObject:@"PS3" forKey:@"PresetName"];
3098         /*Set whether or not this is a user preset where 0 is factory, 1 is user*/
3099         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3100         /*Set whether or not this is default, at creation set to 0*/
3101         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3102         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3103         [preset setObject:[NSNumber numberWithInt:2] forKey:@"UsesPictureSettings"];
3104         /* Get the New Preset Description from the field in the AddPresetPanel */
3105     [preset setObject:@"HandBrake's settings for the Sony PlayStation 3." forKey:@"PresetDescription"];
3106         /* File Format */
3107     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3108         /* Chapter Markers*/
3109          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3110         /* Codecs */
3111         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
3112         /* Video encoder */
3113         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
3114         /* x264 Option String (We can use this to tweak the appleTV output)*/
3115         [preset setObject:@"level=41:subme=5:me=umh" forKey:@"x264Option"];
3116         /* Video quality */
3117         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3118         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3119         [preset setObject:@"2500" forKey:@"VideoAvgBitrate"];
3120         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3121         
3122         /* Video framerate */
3123         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3124         /* GrayScale */
3125         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3126         /* 2 Pass Encoding */
3127         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
3128         
3129         /*Picture Settings*/
3130         /* For PS3 we only want to retain UsesMaxPictureSettings
3131         which depend on the source dvd picture settings, so we don't
3132         record the current dvd's picture info since it will vary from
3133         source to source*/
3134         /* Use Max Picture settings for whatever the dvd is.*/
3135         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
3136         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
3137         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3138         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
3139         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3140         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
3141         /* Set crop settings here */
3142         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3143         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3144     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3145         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3146         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3147         
3148         /*Audio*/
3149         /* Audio Sample Rate*/
3150         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3151         /* Audio Bitrate Rate*/
3152         [preset setObject:@"160" forKey:@"AudioBitRate"];
3153         /* Subtitles*/
3154         [preset setObject:@"None" forKey:@"Subtitles"];
3155         
3156
3157     [preset autorelease];
3158     return preset;
3159
3160 }
3161 - (NSDictionary *)createPSPPreset
3162 {
3163     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3164         /* Get the New Preset Name from the field in the AddPresetPanel */
3165     [preset setObject:@"PSP" forKey:@"PresetName"];
3166         /*Set whether or not this is a user preset where 0 is factory, 1 is user*/
3167         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3168         /*Set whether or not this is default, at creation set to 0*/
3169         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3170         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3171         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3172         /* Get the New Preset Description from the field in the AddPresetPanel */
3173     [preset setObject:@"HandBrake's settings for the Sony PlayStation Portable." forKey:@"PresetDescription"];
3174         /* File Format */
3175     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3176         /* Chapter Markers*/
3177          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3178         /* Codecs */
3179         [preset setObject:@"MPEG-4 Video / AAC Audio" forKey:@"FileCodecs"];
3180         /* Video encoder */
3181         [preset setObject:@"FFmpeg" forKey:@"VideoEncoder"];
3182         /* x264 Option String (We can use this to tweak the appleTV output)*/
3183         [preset setObject:@"" forKey:@"x264Option"];
3184         /* Video quality */
3185         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3186         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3187         [preset setObject:@"1024" forKey:@"VideoAvgBitrate"];
3188         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3189         
3190         /* Video framerate */
3191         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3192         /* GrayScale */
3193         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3194         /* 2 Pass Encoding */
3195         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
3196         
3197         /*Picture Settings*/
3198         /* For PS3 we only want to retain UsesMaxPictureSettings
3199         which depend on the source dvd picture settings, so we don't
3200         record the current dvd's picture info since it will vary from
3201         source to source*/
3202         /* Use Max Picture settings for whatever the dvd is.*/
3203         [preset setObject:[NSNumber numberWithInt:0] forKey:@"UsesMaxPictureSettings"];
3204         [preset setObject:@"368" forKey:@"PictureWidth"];
3205         [preset setObject:@"208" forKey:@"PictureHeight"];
3206         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
3207         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3208         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
3209         /* Set crop settings here */
3210         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3211         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3212         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3213     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3214         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3215         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3216         
3217         /*Audio*/
3218         /* Audio Sample Rate*/
3219         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3220         /* Audio Bitrate Rate*/
3221         [preset setObject:@"128" forKey:@"AudioBitRate"];
3222         /* Subtitles*/
3223         [preset setObject:@"None" forKey:@"Subtitles"];
3224         
3225
3226     [preset autorelease];
3227     return preset;
3228
3229 }
3230
3231 - (NSDictionary *)createNormalPreset
3232 {
3233     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3234         /* Get the New Preset Name from the field in the AddPresetPanel */
3235     [preset setObject:@"Normal" forKey:@"PresetName"];
3236         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3237         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3238         /*Set whether or not this is default, at creation set to 0*/
3239         [preset setObject:[NSNumber numberWithInt:1] forKey:@"Default"];
3240         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3241         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3242         /* Get the New Preset Description from the field in the AddPresetPanel */
3243     [preset setObject:@"HandBrake's normal, default settings." forKey:@"PresetDescription"];
3244         /* File Format */
3245     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3246         /* Chapter Markers*/
3247          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3248     /* Codecs */
3249         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
3250         /* Video encoder */
3251         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
3252         /* x264 Option String */
3253         [preset setObject:@"ref=2:bframes=2:subme=5:me=umh" forKey:@"x264Option"];
3254         /* Video quality */
3255         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3256         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3257         [preset setObject:@"1500" forKey:@"VideoAvgBitrate"];
3258         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3259         
3260         /* Video framerate */
3261         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3262         /* GrayScale */
3263         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3264         /* 2 Pass Encoding */
3265         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
3266         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
3267         
3268         /*Picture Settings*/
3269         //hb_job_t * job = fTitle->job;
3270         /* Basic Picture Settings */
3271         /* Use Max Picture settings for whatever the dvd is.*/
3272         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
3273         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3274         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
3275         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3276         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
3277         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3278         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
3279         /* Set crop settings here */
3280         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3281         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3282     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3283         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3284         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3285         
3286         /*Audio*/
3287         /* Audio Sample Rate*/
3288         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3289         /* Audio Bitrate Rate*/
3290         [preset setObject:@"160" forKey:@"AudioBitRate"];
3291         /* Subtitles*/
3292         [preset setObject:@"None" forKey:@"Subtitles"];
3293         
3294
3295     [preset autorelease];
3296     return preset;
3297
3298 }
3299
3300 - (NSDictionary *)createClassicPreset
3301 {
3302     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3303         /* Get the New Preset Name from the field in the AddPresetPanel */
3304     [preset setObject:@"Classic" forKey:@"PresetName"];
3305         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3306         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3307         /*Set whether or not this is default, at creation set to 0*/
3308         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3309         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3310         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3311         /* Get the New Preset Description from the field in the AddPresetPanel */
3312     [preset setObject:@"HandBrake's traditional, faster, lower-quality settings." forKey:@"PresetDescription"];
3313         /* File Format */
3314     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3315         /* Chapter Markers*/
3316          [preset setObject:[NSNumber numberWithInt:0] forKey:@"ChapterMarkers"];
3317     /* Codecs */
3318         [preset setObject:@"MPEG-4 Video / AAC Audio" forKey:@"FileCodecs"];
3319         /* Video encoder */
3320         [preset setObject:@"FFmpeg" forKey:@"VideoEncoder"];
3321         /* x264 Option String */
3322         [preset setObject:@"" forKey:@"x264Option"];
3323         /* Video quality */
3324         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3325         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3326         [preset setObject:@"1000" forKey:@"VideoAvgBitrate"];
3327         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3328         
3329         /* Video framerate */
3330         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3331         /* GrayScale */
3332         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3333         /* 2 Pass Encoding */
3334         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
3335         
3336         /*Picture Settings*/
3337         //hb_job_t * job = fTitle->job;
3338         /* Basic Picture Settings */
3339         /* Use Max Picture settings for whatever the dvd is.*/
3340         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
3341         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3342         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
3343         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3344         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
3345         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3346         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
3347         /* Set crop settings here */
3348         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3349         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3350     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3351         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3352         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3353         
3354         /*Audio*/
3355         /* Audio Sample Rate*/
3356         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3357         /* Audio Bitrate Rate*/
3358         [preset setObject:@"160" forKey:@"AudioBitRate"];
3359         /* Subtitles*/
3360         [preset setObject:@"None" forKey:@"Subtitles"];
3361         
3362
3363     [preset autorelease];
3364     return preset;
3365
3366 }
3367
3368 - (NSDictionary *)createFilmPreset
3369 {
3370     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3371         /* Get the New Preset Name from the field in the AddPresetPanel */
3372     [preset setObject:@"Film" forKey:@"PresetName"];
3373         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3374         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3375         /*Set whether or not this is default, at creation set to 0*/
3376         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3377         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3378         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3379         /* Get the New Preset Description from the field in the AddPresetPanel */
3380     [preset setObject:@"HandBrake's preset for feature films." forKey:@"PresetDescription"];
3381         /* File Format */
3382     [preset setObject:@"MKV file" forKey:@"FileFormat"];
3383         /* Chapter Markers*/
3384          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3385     /* Codecs */
3386         [preset setObject:@"AVC/H.264 Video / AC-3 Audio" forKey:@"FileCodecs"];
3387         /* Video encoder */
3388         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
3389         /* x264 Option String */
3390         [preset setObject:@"ref=3:mixed-refs:bframes=3:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=6:analyse=all:8x8dct:trellis=1:no-fast-pskip" forKey:@"x264Option"];
3391         /* Video quality */
3392         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3393         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3394         [preset setObject:@"2000" forKey:@"VideoAvgBitrate"];
3395         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3396         
3397         /* Video framerate */
3398         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3399         /* GrayScale */
3400         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3401         /* 2 Pass Encoding */
3402         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
3403         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
3404         
3405         /*Picture Settings*/
3406         //hb_job_t * job = fTitle->job;
3407         /* Basic Picture Settings */
3408         /* Use Max Picture settings for whatever the dvd is.*/
3409         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
3410         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3411         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
3412         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3413         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
3414         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3415         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
3416         /* Set crop settings here */
3417         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3418         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3419     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3420         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3421         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3422         
3423         /*Audio*/
3424         /* Audio Sample Rate*/
3425         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3426         /* Audio Bitrate Rate*/
3427         [preset setObject:@"160" forKey:@"AudioBitRate"];
3428         /* Subtitles*/
3429         [preset setObject:@"None" forKey:@"Subtitles"];
3430         
3431
3432     [preset autorelease];
3433     return preset;
3434
3435 }
3436
3437 - (NSDictionary *)createTelevisionPreset
3438 {
3439     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3440         /* Get the New Preset Name from the field in the AddPresetPanel */
3441     [preset setObject:@"Television" forKey:@"PresetName"];
3442         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3443         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3444         /*Set whether or not this is default, at creation set to 0*/
3445         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3446         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3447         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3448         /* Get the New Preset Description from the field in the AddPresetPanel */
3449     [preset setObject:@"HandBrake's settings for video from television." forKey:@"PresetDescription"];
3450         /* File Format */
3451     [preset setObject:@"MKV file" forKey:@"FileFormat"];
3452         /* Chapter Markers*/
3453          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3454     /* Codecs */
3455         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
3456         /* Video encoder */
3457         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
3458         /* x264 Option String */
3459         [preset setObject:@"ref=3:mixed-refs:bframes=6:bime:weightb:direct=auto:b-pyramid:me=umh:subme=6:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip" forKey:@"x264Option"];
3460         /* Video quality */
3461         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3462         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3463         [preset setObject:@"1300" forKey:@"VideoAvgBitrate"];
3464         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3465         
3466         /* Video framerate */
3467         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3468         /* GrayScale */
3469         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3470         /* 2 Pass Encoding */
3471         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
3472         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
3473         
3474         /*Picture Settings*/
3475         //hb_job_t * job = fTitle->job;
3476         /* Basic Picture Settings */
3477         /* Use Max Picture settings for whatever the dvd is.*/
3478         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
3479         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3480         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
3481         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3482         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
3483         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureDeinterlace"];
3484         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
3485         /* Set crop settings here */
3486         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3487         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3488     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3489         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3490         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3491         
3492         /*Audio*/
3493         /* Audio Sample Rate*/
3494         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3495         /* Audio Bitrate Rate*/
3496         [preset setObject:@"160" forKey:@"AudioBitRate"];
3497         /* Subtitles*/
3498         [preset setObject:@"None" forKey:@"Subtitles"];
3499         
3500
3501     [preset autorelease];
3502     return preset;
3503
3504 }
3505
3506 - (NSDictionary *)createAnimationPreset
3507 {
3508     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3509         /* Get the New Preset Name from the field in the AddPresetPanel */
3510     [preset setObject:@"Animation" forKey:@"PresetName"];
3511         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3512         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3513         /*Set whether or not this is default, at creation set to 0*/
3514         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3515         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3516         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3517         /* Get the New Preset Description from the field in the AddPresetPanel */
3518     [preset setObject:@"HandBrake's settings for cartoons, anime, and CGI." forKey:@"PresetDescription"];
3519         /* File Format */
3520     [preset setObject:@"MKV file" forKey:@"FileFormat"];
3521         /* Chapter Markers*/
3522          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3523     /* Codecs */
3524         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
3525         /* Video encoder */
3526         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
3527         /* x264 Option String */
3528         [preset setObject:@"ref=5:mixed-refs:bframes=6:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=5:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip:filter=2,2" forKey:@"x264Option"];
3529         /* Video quality */
3530         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3531         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3532         [preset setObject:@"1000" forKey:@"VideoAvgBitrate"];
3533         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3534         
3535         /* Video framerate */
3536         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3537         /* GrayScale */
3538         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3539         /* 2 Pass Encoding */
3540         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
3541         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
3542         
3543         /*Picture Settings*/
3544         //hb_job_t * job = fTitle->job;
3545         /* Basic Picture Settings */
3546         /* Use Max Picture settings for whatever the dvd is.*/
3547         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
3548         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3549         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
3550         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3551         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
3552         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureDeinterlace"];
3553         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
3554         /* Set crop settings here */
3555         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3556         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3557     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3558         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3559         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3560         
3561         /*Audio*/
3562         /* Audio Sample Rate*/
3563         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3564         /* Audio Bitrate Rate*/
3565         [preset setObject:@"160" forKey:@"AudioBitRate"];
3566         /* Subtitles*/
3567         [preset setObject:@"None" forKey:@"Subtitles"];
3568         
3569
3570     [preset autorelease];
3571     return preset;
3572
3573 }
3574
3575 - (NSDictionary *)createQuickTimePreset
3576 {
3577     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3578         /* Get the New Preset Name from the field in the AddPresetPanel */
3579     [preset setObject:@"QuickTime" forKey:@"PresetName"];
3580         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3581         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3582         /*Set whether or not this is default, at creation set to 0*/
3583         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3584         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3585         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3586         /* Get the New Preset Description from the field in the AddPresetPanel */
3587     [preset setObject:@"HandBrake's high quality settings for use with QuickTime. It can be slow, so use it when the Normal preset doesn't look good enough." forKey:@"PresetDescription"];
3588         /* File Format */
3589     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3590         /* Chapter Markers*/
3591          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3592     /* Codecs */
3593         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
3594         /* Video encoder */
3595         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
3596         /* x264 Option String */
3597         [preset setObject:@"ref=3:mixed-refs:bframes=3:bime:weightb:b-rdo:direct=auto:me=umh:subme=5:analyse=all:8x8dct:trellis=1:no-fast-pskip" forKey:@"x264Option"];
3598         /* Video quality */
3599         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3600         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3601         [preset setObject:@"2000" forKey:@"VideoAvgBitrate"];
3602         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3603         
3604         /* Video framerate */
3605         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3606         /* GrayScale */
3607         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3608         /* 2 Pass Encoding */
3609         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
3610         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
3611         
3612         /*Picture Settings*/
3613         //hb_job_t * job = fTitle->job;
3614         /* Basic Picture Settings */
3615         /* Use Max Picture settings for whatever the dvd is.*/
3616         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
3617         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3618         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
3619         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3620         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
3621         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3622         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
3623         /* Set crop settings here */
3624         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3625         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3626     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3627         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3628         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3629         
3630         /*Audio*/
3631         /* Audio Sample Rate*/
3632         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3633         /* Audio Bitrate Rate*/
3634         [preset setObject:@"160" forKey:@"AudioBitRate"];
3635         /* Subtitles*/
3636         [preset setObject:@"None" forKey:@"Subtitles"];
3637         
3638
3639     [preset autorelease];
3640     return preset;
3641
3642 }
3643
3644 - (NSDictionary *)createBedlamPreset
3645 {
3646     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3647         /* Get the New Preset Name from the field in the AddPresetPanel */
3648     [preset setObject:@"Bedlam" forKey:@"PresetName"];
3649         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3650         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3651         /*Set whether or not this is default, at creation set to 0*/
3652         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3653         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3654         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3655         /* Get the New Preset Description from the field in the AddPresetPanel */
3656     [preset setObject:@"HandBrake's settings maxed out for slowest encoding and highest quality. Use at your own risk. So slow it's not just insane...it's a trip to the looney bin." forKey:@"PresetDescription"];
3657         /* File Format */
3658     [preset setObject:@"MKV file" forKey:@"FileFormat"];
3659         /* Chapter Markers*/
3660          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3661     /* Codecs */
3662         [preset setObject:@"AVC/H.264 Video / AC-3 Audio" forKey:@"FileCodecs"];
3663         /* Video encoder */
3664         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
3665         /* x264 Option String */
3666         [preset setObject:@"ref=16:mixed-refs:bframes=6:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=7:me-range=64:analyse=all:8x8dct:trellis=2:no-fast-pskip:no-dct-decimate:filter=-2,-1" forKey:@"x264Option"];
3667         /* Video quality */
3668         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3669         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3670         [preset setObject:@"1800" forKey:@"VideoAvgBitrate"];
3671         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3672         
3673         /* Video framerate */
3674         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3675         /* GrayScale */
3676         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3677         /* 2 Pass Encoding */
3678         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
3679         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
3680         
3681         /*Picture Settings*/
3682         //hb_job_t * job = fTitle->job;
3683         /* Basic Picture Settings */
3684         /* Use Max Picture settings for whatever the dvd is.*/
3685         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
3686         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3687         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
3688         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3689         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
3690         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3691         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
3692         /* Set crop settings here */
3693         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3694         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3695     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3696         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3697         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3698         
3699         /*Audio*/
3700         /* Audio Sample Rate*/
3701         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3702         /* Audio Bitrate Rate*/
3703         [preset setObject:@"160" forKey:@"AudioBitRate"];
3704         /* Subtitles*/
3705         [preset setObject:@"None" forKey:@"Subtitles"];
3706         
3707
3708     [preset autorelease];
3709     return preset;
3710
3711 }
3712
3713 - (NSDictionary *)createiPhonePreset
3714 {
3715     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3716         /* Get the New Preset Name from the field in the AddPresetPanel */
3717     [preset setObject:@"iPhone" forKey:@"PresetName"];
3718         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3719         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3720         /*Set whether or not this is default, at creation set to 0*/
3721         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3722         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3723         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3724         /* Get the New Preset Description from the field in the AddPresetPanel */
3725     [preset setObject:@"HandBrake's settings for the iPhone." forKey:@"PresetDescription"];
3726         /* File Format */
3727     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3728         /* Chapter Markers*/
3729          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3730     /* Codecs */
3731         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
3732         /* Video encoder */
3733         [preset setObject:@"x264 (h.264 iPod)" forKey:@"VideoEncoder"];
3734         /* x264 Option String */
3735         [preset setObject:@"cabac=0:ref=1:analyse=all:me=umh:subme=6:no-fast-pskip=1:trellis=1" forKey:@"x264Option"];
3736         /* Video quality */
3737         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3738         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3739         [preset setObject:@"960" forKey:@"VideoAvgBitrate"];
3740         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3741         
3742         /* Video framerate */
3743         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3744         /* GrayScale */
3745         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3746         /* 2 Pass Encoding */
3747         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
3748         
3749         /*Picture Settings*/
3750         //hb_job_t * job = fTitle->job;
3751         /* Basic Picture Settings */
3752         /* Use Max Picture settings for whatever the dvd is.*/
3753         [preset setObject:[NSNumber numberWithInt:0] forKey:@"UsesMaxPictureSettings"];
3754         [preset setObject:[NSNumber numberWithInt:480] forKey:@"PictureWidth"];
3755         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3756         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
3757         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3758         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
3759         /* Set crop settings here */
3760         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3761         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3762         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3763     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3764         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3765         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3766         
3767         /*Audio*/
3768         /* Audio Sample Rate*/
3769         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3770         /* Audio Bitrate Rate*/
3771         [preset setObject:@"128" forKey:@"AudioBitRate"];
3772         /* Subtitles*/
3773         [preset setObject:@"None" forKey:@"Subtitles"];
3774         
3775
3776     [preset autorelease];
3777     return preset;
3778
3779 }
3780
3781 - (NSDictionary *)createDeuxSixQuatrePreset
3782 {
3783     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3784         /* Get the New Preset Name from the field in the AddPresetPanel */
3785     [preset setObject:@"Deux Six Quatre" forKey:@"PresetName"];
3786         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3787         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3788         /*Set whether or not this is default, at creation set to 0*/
3789         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3790         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3791         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3792         /* Get the New Preset Description from the field in the AddPresetPanel */
3793     [preset setObject:@"HandBrake's preset for true high profile x264 quality. A good balance of quality and speed, based on community standards found in the wild. This preset will give you a much better sense of x264's capabilities than vanilla main profile." forKey:@"PresetDescription"];
3794         /* File Format */
3795     [preset setObject:@"MKV file" forKey:@"FileFormat"];
3796         /* Chapter Markers*/
3797          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3798     /* Codecs */
3799         [preset setObject:@"AVC/H.264 Video / AC-3 Audio" forKey:@"FileCodecs"];
3800         /* Video encoder */
3801         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
3802         /* x264 Option String */
3803         [preset setObject:@"ref=5:mixed-refs:bframes=3:bime:weightb:b-rdo:b-pyramid:me=umh:subme=7:trellis=1:analyse=all:8x8dct:no-fast-pskip" forKey:@"x264Option"];
3804         /* Video quality */
3805         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3806         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3807         [preset setObject:@"1600" forKey:@"VideoAvgBitrate"];
3808         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3809         
3810         /* Video framerate */
3811         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3812         /* GrayScale */
3813         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3814         /* 2 Pass Encoding */
3815         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
3816         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
3817         
3818         /*Picture Settings*/
3819         //hb_job_t * job = fTitle->job;
3820         /* Basic Picture Settings */
3821         /* Use Max Picture settings for whatever the dvd is.*/
3822         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
3823         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3824         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
3825         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3826         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
3827         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3828         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
3829         /* Set crop settings here */
3830         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3831         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3832     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3833         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3834         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3835         
3836         /*Audio*/
3837         /* Audio Sample Rate*/
3838         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3839         /* Audio Bitrate Rate*/
3840         [preset setObject:@"160" forKey:@"AudioBitRate"];
3841         /* Subtitles*/
3842         [preset setObject:@"None" forKey:@"Subtitles"];
3843         
3844
3845     [preset autorelease];
3846     return preset;
3847
3848 }
3849
3850 - (NSDictionary *)createBrokePreset
3851 {
3852     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3853         /* Get the New Preset Name from the field in the AddPresetPanel */
3854     [preset setObject:@"Broke" forKey:@"PresetName"];
3855         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3856         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3857         /*Set whether or not this is default, at creation set to 0*/
3858         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3859         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3860         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3861         /* Get the New Preset Description from the field in the AddPresetPanel */
3862     [preset setObject:@"HandBrake's preset for people without a lot of money to waste on hard drives. Tries to maximize quality for burning to CDs, so you can party like it's 1999." forKey:@"PresetDescription"];
3863         /* File Format */
3864     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3865         /* Chapter Markers*/
3866          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3867     /* Codecs */
3868         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
3869         /* Video encoder */
3870         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
3871         /* x264 Option String */
3872         [preset setObject:@"ref=3:mixed-refs:bframes=6:bime:weightb:b-rdo:b-pyramid:direct=auto:me=umh:subme=6:trellis=1:analyse=all:8x8dct:no-fast-pskip" forKey:@"x264Option"];
3873         /* Video quality */
3874         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoQualityType"];
3875         [preset setObject:@"695" forKey:@"VideoTargetSize"];
3876         [preset setObject:@"1600" forKey:@"VideoAvgBitrate"];
3877         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3878         
3879         /* Video framerate */
3880         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3881         /* GrayScale */
3882         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3883         /* 2 Pass Encoding */
3884         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
3885         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
3886         
3887         /*Picture Settings*/
3888         //hb_job_t * job = fTitle->job;
3889         /* Basic Picture Settings */
3890         /* Use Max Picture settings for whatever the dvd is.*/
3891         [preset setObject:[NSNumber numberWithInt:0] forKey:@"UsesMaxPictureSettings"];
3892         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3893         [preset setObject:[NSNumber numberWithInt:640] forKey:@"PictureWidth"];
3894         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3895         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
3896         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3897         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
3898         /* Set crop settings here */
3899         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3900         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3901     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3902         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3903         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3904         
3905         /*Audio*/
3906         /* Audio Sample Rate*/
3907         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3908         /* Audio Bitrate Rate*/
3909         [preset setObject:@"128" forKey:@"AudioBitRate"];
3910         /* Subtitles*/
3911         [preset setObject:@"None" forKey:@"Subtitles"];
3912         
3913
3914     [preset autorelease];
3915     return preset;
3916
3917 }
3918
3919 - (NSDictionary *)createBlindPreset
3920 {
3921     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3922         /* Get the New Preset Name from the field in the AddPresetPanel */
3923     [preset setObject:@"Blind" forKey:@"PresetName"];
3924         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3925         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3926         /*Set whether or not this is default, at creation set to 0*/
3927         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3928         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3929         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3930         /* Get the New Preset Description from the field in the AddPresetPanel */
3931     [preset setObject:@"HandBrake's preset for impatient people who don't care about picture quality." forKey:@"PresetDescription"];
3932         /* File Format */
3933     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3934         /* Chapter Markers*/
3935          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3936     /* Codecs */
3937         [preset setObject:@"MPEG-4 Video / AAC Audio" forKey:@"FileCodecs"];
3938         /* Video encoder */
3939         [preset setObject:@"FFmpeg" forKey:@"VideoEncoder"];
3940         /* x264 Option String */
3941         [preset setObject:@"" forKey:@"x264Option"];
3942         /* Video quality */
3943         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3944         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3945         [preset setObject:@"512" forKey:@"VideoAvgBitrate"];
3946         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3947         
3948         /* Video framerate */
3949         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3950         /* GrayScale */
3951         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3952         /* 2 Pass Encoding */
3953         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
3954         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTurboTwoPass"];
3955         
3956         /*Picture Settings*/
3957         //hb_job_t * job = fTitle->job;
3958         /* Basic Picture Settings */
3959         /* Use Max Picture settings for whatever the dvd is.*/
3960         [preset setObject:[NSNumber numberWithInt:0] forKey:@"UsesMaxPictureSettings"];
3961         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3962         [preset setObject:[NSNumber numberWithInt:512] forKey:@"PictureWidth"];
3963         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3964         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
3965         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3966         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
3967         /* Set crop settings here */
3968         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3969         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3970     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3971         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3972         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3973         
3974         /*Audio*/
3975         /* Audio Sample Rate*/
3976         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3977         /* Audio Bitrate Rate*/
3978         [preset setObject:@"128" forKey:@"AudioBitRate"];
3979         /* Subtitles*/
3980         [preset setObject:@"None" forKey:@"Subtitles"];
3981         
3982
3983     [preset autorelease];
3984     return preset;
3985
3986 }
3987
3988 - (NSDictionary *)createCRFPreset
3989 {
3990     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3991         /* Get the New Preset Name from the field in the AddPresetPanel */
3992     [preset setObject:@"Constant Quality Rate" forKey:@"PresetName"];
3993         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3994         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3995         /*Set whether or not this is default, at creation set to 0*/
3996         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3997         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3998         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3999         /* Get the New Preset Description from the field in the AddPresetPanel */
4000     [preset setObject:@"HandBrake's preset for consistently excellent quality in one pass, with the downside of entirely unpredictable file sizes and bitrates." forKey:@"PresetDescription"];
4001         /* File Format */
4002     [preset setObject:@"MKV file" forKey:@"FileFormat"];
4003         /* Chapter Markers*/
4004          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
4005     /* Codecs */
4006         [preset setObject:@"AVC/H.264 Video / AC-3 Audio" forKey:@"FileCodecs"];
4007         /* Video encoder */
4008         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
4009         /* x264 Option String */
4010         [preset setObject:@"ref=3:mixed-refs:bframes=3:b-pyramid:b-rdo:bime:weightb:filter=-2,-1:subme=6:trellis=1:analyse=all:8x8dct:me=umh" forKey:@"x264Option"];
4011         /* Video quality */
4012         [preset setObject:[NSNumber numberWithInt:2] forKey:@"VideoQualityType"];
4013         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
4014         [preset setObject:@"2000" forKey:@"VideoAvgBitrate"];
4015         [preset setObject:[NSNumber numberWithFloat:0.6471] forKey:@"VideoQualitySlider"];
4016         
4017         /* Video framerate */
4018         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
4019         /* GrayScale */
4020         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
4021         /* 2 Pass Encoding */
4022         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
4023         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTurboTwoPass"];
4024         
4025         /*Picture Settings*/
4026         //hb_job_t * job = fTitle->job;
4027         /* Basic Picture Settings */
4028         /* Use Max Picture settings for whatever the dvd is.*/
4029         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
4030         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
4031         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
4032         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
4033         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
4034         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
4035         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
4036         /* Set crop settings here */
4037         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
4038         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
4039     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
4040         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
4041         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
4042         
4043         /*Audio*/
4044         /* Audio Sample Rate*/
4045         [preset setObject:@"48" forKey:@"AudioSampleRate"];
4046         /* Audio Bitrate Rate*/
4047         [preset setObject:@"160" forKey:@"AudioBitRate"];
4048         /* Subtitles*/
4049         [preset setObject:@"None" forKey:@"Subtitles"];
4050         
4051
4052     [preset autorelease];
4053     return preset;
4054
4055 }
4056
4057
4058 - (IBAction)deletePreset:(id)sender
4059 {
4060     int status;
4061     NSEnumerator *enumerator;
4062     NSNumber *index;
4063     NSMutableArray *tempArray;
4064     id tempObject;
4065     
4066     if ( [tableView numberOfSelectedRows] == 0 )
4067         return;
4068     /* Alert user before deleting preset */
4069         /* Comment out for now, tie to user pref eventually */
4070
4071     //NSBeep();
4072     status = NSRunAlertPanel(@"Warning!", @"Are you sure that you want to delete the selected preset?", @"OK", @"Cancel", nil);
4073     
4074     if ( status == NSAlertDefaultReturn ) {
4075         enumerator = [tableView selectedRowEnumerator];
4076         tempArray = [NSMutableArray array];
4077         
4078         while ( (index = [enumerator nextObject]) ) {
4079             tempObject = [UserPresets objectAtIndex:[index intValue]];
4080             [tempArray addObject:tempObject];
4081         }
4082         
4083         [UserPresets removeObjectsInArray:tempArray];
4084         [tableView reloadData];
4085         [self savePreset];   
4086     }
4087 }
4088
4089 - (IBAction)getDefaultPresets:(id)sender
4090 {
4091         int i = 0;
4092     NSEnumerator *enumerator = [UserPresets objectEnumerator];
4093         id tempObject;
4094         while (tempObject = [enumerator nextObject])
4095         {
4096                 NSDictionary *thisPresetDict = tempObject;
4097                 if ([[thisPresetDict objectForKey:@"Default"] intValue] == 1) // 1 is HB default
4098                 {
4099                         presetHbDefault = i;    
4100                 }
4101                 if ([[thisPresetDict objectForKey:@"Default"] intValue] == 2) // 2 is User specified default
4102                 {
4103                         presetUserDefault = i;  
4104                 }
4105                 i++;
4106         }
4107 }
4108
4109 - (IBAction)setDefaultPreset:(id)sender
4110 {
4111     int i = 0;
4112     NSEnumerator *enumerator = [UserPresets objectEnumerator];
4113         id tempObject;
4114         /* First make sure the old user specified default preset is removed */
4115         while (tempObject = [enumerator nextObject])
4116         {
4117                 /* make sure we are not removing the default HB preset */
4118                 if ([[[UserPresets objectAtIndex:i] objectForKey:@"Default"] intValue] != 1) // 1 is HB default
4119                 {
4120                         [[UserPresets objectAtIndex:i] setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
4121                 }
4122                 i++;
4123         }
4124         /* Second, go ahead and set the appropriate user specfied preset */
4125         /* we get the chosen preset from the UserPresets array */
4126         if ([[[UserPresets objectAtIndex:[tableView selectedRow]] objectForKey:@"Default"] intValue] != 1) // 1 is HB default
4127         {
4128                 [[UserPresets objectAtIndex:[tableView selectedRow]] setObject:[NSNumber numberWithInt:2] forKey:@"Default"];
4129         }
4130         presetUserDefault = [tableView selectedRow];
4131         
4132         /* We save all of the preset data here */
4133     [self savePreset];
4134         /* We Reload the New Table data for presets */
4135     [tableView reloadData];
4136 }
4137
4138 - (IBAction)selectDefaultPreset:(id)sender
4139 {
4140         /* if there is a user specified default, we use it */
4141         if (presetUserDefault)
4142         {
4143         [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:presetUserDefault] byExtendingSelection:NO];
4144         [self tableViewSelected:NULL];
4145         }
4146         else if (presetHbDefault) //else we use the built in default presetHbDefault
4147         {
4148         [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:presetHbDefault] byExtendingSelection:NO];
4149         [self tableViewSelected:NULL];
4150         }
4151 }
4152
4153 - (IBAction)tableViewSelected:(id)sender
4154 {
4155     /* Since we cannot disable the presets tableView in terms of clickability
4156            we will use the enabled state of the add presets button to determine whether
4157            or not clicking on a preset will do anything */
4158         if ([fPresetsAdd isEnabled])
4159         {
4160                 if ([tableView selectedRow] >= 0)
4161                 {       
4162                         /* we get the chosen preset from the UserPresets array */
4163                         chosenPreset = [UserPresets objectAtIndex:[tableView selectedRow]];
4164                         curUserPresetChosenNum = [sender selectedRow];
4165                         /* we set the preset display field in main window here */
4166                         [fPresetSelectedDisplay setStringValue: [NSString stringWithFormat: @"%@",[chosenPreset valueForKey:@"PresetName"]]];
4167                         if ([[chosenPreset objectForKey:@"Default"] intValue] == 1)
4168                         {
4169                                 [fPresetSelectedDisplay setStringValue: [NSString stringWithFormat: @"%@ (Default)",[chosenPreset valueForKey:@"PresetName"]]];
4170                         }
4171                         else
4172                         {
4173                                 [fPresetSelectedDisplay setStringValue: [NSString stringWithFormat: @"%@",[chosenPreset valueForKey:@"PresetName"]]];
4174                         }
4175                         /* File Format */
4176                         [fDstFormatPopUp selectItemWithTitle: [NSString stringWithFormat:[chosenPreset valueForKey:@"FileFormat"]]];
4177                         [self formatPopUpChanged: NULL];
4178                         
4179                         /* Chapter Markers*/
4180                         [fCreateChapterMarkers setState:[[chosenPreset objectForKey:@"ChapterMarkers"] intValue]];
4181                         /* Allow Mpeg4 64 bit formatting +4GB file sizes */
4182                         [fDstMpgLargeFileCheck setState:[[chosenPreset objectForKey:@"Mp4LargeFile"] intValue]];
4183                         /* Codecs */
4184                         [fDstCodecsPopUp selectItemWithTitle: [NSString stringWithFormat:[chosenPreset valueForKey:@"FileCodecs"]]];
4185                         [self codecsPopUpChanged: NULL];
4186                         /* Video encoder */
4187                         [fVidEncoderPopUp selectItemWithTitle: [NSString stringWithFormat:[chosenPreset valueForKey:@"VideoEncoder"]]];
4188                         
4189                         /* We can show the preset options here in the gui if we want to
4190                                 so we check to see it the user has specified it in the prefs */
4191                         [fAdvancedOptions setOptions: [NSString stringWithFormat:[chosenPreset valueForKey:@"x264Option"]]];
4192                         
4193                         /* Lets run through the following functions to get variables set there */
4194                         [self encoderPopUpChanged: NULL];
4195                         
4196                         [self calculateBitrate: NULL];
4197                         
4198                         /* Video quality */
4199                         [fVidQualityMatrix selectCellAtRow:[[chosenPreset objectForKey:@"VideoQualityType"] intValue] column:0];
4200                         
4201                         [fVidTargetSizeField setStringValue: [NSString stringWithFormat:[chosenPreset valueForKey:@"VideoTargetSize"]]];
4202                         [fVidBitrateField setStringValue: [NSString stringWithFormat:[chosenPreset valueForKey:@"VideoAvgBitrate"]]];
4203                         
4204                         [fVidQualitySlider setFloatValue: [[chosenPreset valueForKey:@"VideoQualitySlider"] floatValue]];
4205                         [self videoMatrixChanged: NULL];
4206                         
4207                         /* Video framerate */
4208                         [fVidRatePopUp selectItemWithTitle: [NSString stringWithFormat:[chosenPreset valueForKey:@"VideoFramerate"]]];
4209                         
4210                         /* GrayScale */
4211                         [fVidGrayscaleCheck setState:[[chosenPreset objectForKey:@"VideoGrayScale"] intValue]];
4212                         
4213                         /* 2 Pass Encoding */
4214                         [fVidTwoPassCheck setState:[[chosenPreset objectForKey:@"VideoTwoPass"] intValue]];
4215                         [self twoPassCheckboxChanged: NULL];
4216                         /* Turbo 1st pass for 2 Pass Encoding */
4217                         [fVidTurboPassCheck setState:[[chosenPreset objectForKey:@"VideoTurboTwoPass"] intValue]];
4218                         
4219                         /*Audio*/
4220                         
4221                         /* Audio Sample Rate*/
4222                         [fAudRatePopUp selectItemWithTitle: [NSString stringWithFormat:[chosenPreset valueForKey:@"AudioSampleRate"]]];
4223                         /* Audio Bitrate Rate*/
4224                         [fAudBitratePopUp selectItemWithTitle: [NSString stringWithFormat:[chosenPreset valueForKey:@"AudioBitRate"]]];
4225                         /*Subtitles*/
4226                         [fSubPopUp selectItemWithTitle: [NSString stringWithFormat:[chosenPreset valueForKey:@"Subtitles"]]];
4227                         
4228                         /* Picture Settings */
4229                         /* Look to see if we apply these here in objectForKey:@"UsesPictureSettings"] */
4230                         if ([[chosenPreset objectForKey:@"UsesPictureSettings"]  intValue] > 0)
4231                         {
4232                                 hb_job_t * job = fTitle->job;
4233                                 /* Check to see if we should use the max picture setting for the current title*/
4234                                 if ([[chosenPreset objectForKey:@"UsesPictureSettings"]  intValue] == 2 || [[chosenPreset objectForKey:@"UsesMaxPictureSettings"]  intValue] == 1)
4235                                 {
4236                                         /* Use Max Picture settings for whatever the dvd is.*/
4237                                         [self revertPictureSizeToMax: NULL];
4238                                         job->keep_ratio = [[chosenPreset objectForKey:@"PictureKeepRatio"]  intValue];
4239                                         if (job->keep_ratio == 1)
4240                                         {
4241                                                 hb_fix_aspect( job, HB_KEEP_WIDTH );
4242                                                 if( job->height > fTitle->height )
4243                                                 {
4244                                                         job->height = fTitle->height;
4245                                                         hb_fix_aspect( job, HB_KEEP_HEIGHT );
4246                                                 }
4247                                         }
4248                                         job->pixel_ratio = [[chosenPreset objectForKey:@"PicturePAR"]  intValue];
4249                                 }
4250                                 else // Apply picture settings that were in effect at the time the preset was saved
4251                                 {
4252                                         job->width = [[chosenPreset objectForKey:@"PictureWidth"]  intValue];
4253                                         job->height = [[chosenPreset objectForKey:@"PictureHeight"]  intValue];
4254                                         job->keep_ratio = [[chosenPreset objectForKey:@"PictureKeepRatio"]  intValue];
4255                                         if (job->keep_ratio == 1)
4256                                         {
4257                                                 hb_fix_aspect( job, HB_KEEP_WIDTH );
4258                                                 if( job->height > fTitle->height )
4259                                                 {
4260                                                         job->height = fTitle->height;
4261                                                         hb_fix_aspect( job, HB_KEEP_HEIGHT );
4262                                                 }
4263                                         }
4264                                         job->pixel_ratio = [[chosenPreset objectForKey:@"PicturePAR"]  intValue];
4265                     [fPictureController setDeinterlace:[[chosenPreset objectForKey:@"PictureDeinterlace"] intValue]];
4266                                         
4267                                         if ([chosenPreset objectForKey:@"PictureDetelecine"])
4268                                         {
4269                         [fPictureController setDetelecine:[[chosenPreset objectForKey:@"PictureDetelecine"] intValue]];
4270                                         }
4271                                         if ([chosenPreset objectForKey:@"PictureDenoise"])
4272                                         {
4273                         [fPictureController setDenoise:[[chosenPreset objectForKey:@"PictureDenoise"] intValue]];
4274                                         }
4275                                         /* If Cropping is set to custom, then recall all four crop values from
4276                                                 when the preset was created and apply them */
4277                                         if ([[chosenPreset objectForKey:@"PictureAutoCrop"]  intValue] == 0)
4278                                         {
4279                         [fPictureController setAutoCrop:NO];
4280                                                 
4281                                                 /* Here we use the custom crop values saved at the time the preset was saved */
4282                                                 job->crop[0] = [[chosenPreset objectForKey:@"PictureTopCrop"]  intValue];
4283                                                 job->crop[1] = [[chosenPreset objectForKey:@"PictureBottomCrop"]  intValue];
4284                                                 job->crop[2] = [[chosenPreset objectForKey:@"PictureLeftCrop"]  intValue];
4285                                                 job->crop[3] = [[chosenPreset objectForKey:@"PictureRightCrop"]  intValue];
4286                                                 
4287                                         }
4288                                         else /* if auto crop has been saved in preset, set to auto and use post scan auto crop */
4289                                         {
4290                         [fPictureController setAutoCrop:YES];
4291                         /* Here we use the auto crop values determined right after scan */
4292                                                 job->crop[0] = AutoCropTop;
4293                                                 job->crop[1] = AutoCropBottom;
4294                                                 job->crop[2] = AutoCropLeft;
4295                                                 job->crop[3] = AutoCropRight;
4296                                                 
4297                                         }
4298                                 }
4299                                 [self calculatePictureSizing: NULL]; 
4300                         }
4301                         
4302                         
4303                         [[fPresetsActionMenu itemAtIndex:0] setEnabled: YES];
4304                         }
4305 }
4306 }
4307
4308
4309
4310 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
4311 {
4312     return [UserPresets count];
4313 }
4314
4315 /* we use this to determine display characteristics for
4316 each table cell based on content currently only used to
4317 show the built in presets in a blue font. */
4318 - (void)tableView:(NSTableView *)aTableView
4319  willDisplayCell:(id)aCell 
4320  forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
4321 {
4322     NSDictionary *userPresetDict = [UserPresets objectAtIndex:rowIndex];
4323         NSFont *txtFont;
4324         NSColor *fontColor;
4325         NSColor *shadowColor;
4326         txtFont = [NSFont systemFontOfSize: [NSFont smallSystemFontSize]];
4327         /* First, we check to see if its a selected row, if so, we use white since its highlighted in blue */
4328         if ([[aTableView selectedRowIndexes] containsIndex:rowIndex] && ([tableView editedRow] != rowIndex))
4329         {
4330                 
4331                 fontColor = [NSColor whiteColor];
4332                 shadowColor = [NSColor colorWithDeviceRed:(127.0/255.0) green:(140.0/255.0) blue:(160.0/255.0) alpha:1.0];
4333         }
4334         else
4335         {
4336                 /* We set the properties of unselected rows */
4337                 /* if built-in preset (defined by "type" == 0) we use a blue font */
4338                 if ([[userPresetDict objectForKey:@"Type"] intValue] == 0)
4339                 {
4340                         fontColor = [NSColor blueColor];
4341                 }
4342                 else // User created preset, use a black font
4343                 {
4344                         fontColor = [NSColor blackColor];
4345                 }
4346                 shadowColor = nil;
4347         }
4348         /* We check to see if this is the HB default, if so, color it appropriately */
4349         if (!presetUserDefault && presetHbDefault && rowIndex == presetHbDefault)
4350         {
4351         txtFont = [NSFont boldSystemFontOfSize: [NSFont smallSystemFontSize]];
4352         }
4353         /* We check to see if this is the User Specified default, if so, color it appropriately */
4354         if (presetUserDefault && rowIndex == presetUserDefault)
4355         {
4356         txtFont = [NSFont boldSystemFontOfSize: [NSFont smallSystemFontSize]];
4357         }
4358         
4359         [aCell setTextColor:fontColor];
4360         [aCell setFont:txtFont];
4361         /* this shadow stuff (like mail app) for some reason looks crappy, commented out
4362         temporarily in case we want to resurrect it */
4363         /*
4364         NSShadow *shadow = [[NSShadow alloc] init];
4365         NSSize shadowOffset = { width: 1.0, height: -1.5};
4366         [shadow setShadowOffset:shadowOffset];
4367         [shadow setShadowColor:shadowColor];
4368         [shadow set];
4369         */
4370         
4371 }
4372 /* Method to display tooltip with the description for each preset, if available */
4373 - (NSString *)tableView:(NSTableView *)aTableView toolTipForCell:(NSCell *)aCell 
4374                    rect:(NSRectPointer)aRect tableColumn:(NSTableColumn *)aTableColumn
4375                     row:(int)rowIndex mouseLocation:(NSPoint)aPos
4376 {
4377      /* initialize the tooltip contents variable */
4378          NSString *loc_tip;
4379      /* if there is a description for the preset, we show it in the tooltip */
4380          if ([[UserPresets objectAtIndex:rowIndex] valueForKey:@"PresetDescription"])
4381          {
4382         loc_tip = [NSString stringWithFormat: @"%@",[[UserPresets objectAtIndex:rowIndex] valueForKey:@"PresetDescription"]];
4383         return (loc_tip);
4384          }
4385          else
4386          {
4387         loc_tip = @"No description available";
4388          }
4389          return (loc_tip);
4390
4391 }
4392
4393 - (id)tableView:(NSTableView *)aTableView
4394       objectValueForTableColumn:(NSTableColumn *)aTableColumn
4395       row:(int)rowIndex
4396 {
4397 id theRecord, theValue;
4398     
4399     theRecord = [UserPresets objectAtIndex:rowIndex];
4400     theValue = [theRecord objectForKey:[aTableColumn identifier]];
4401     return theValue;
4402 }
4403
4404 // NSTableDataSource method that we implement to edit values directly in the table...
4405 - (void)tableView:(NSTableView *)aTableView
4406         setObjectValue:(id)anObject
4407         forTableColumn:(NSTableColumn *)aTableColumn
4408         row:(int)rowIndex
4409 {
4410     id theRecord;
4411     
4412     theRecord = [UserPresets objectAtIndex:rowIndex];
4413     [theRecord setObject:anObject forKey:@"PresetName"];
4414     /* We Sort the Presets By Factory or Custom */
4415         NSSortDescriptor * presetTypeDescriptor=[[[NSSortDescriptor alloc] initWithKey:@"Type" 
4416                                                     ascending:YES] autorelease];
4417                 /* We Sort the Presets Alphabetically by name */
4418         NSSortDescriptor * presetNameDescriptor=[[[NSSortDescriptor alloc] initWithKey:@"PresetName" 
4419                                                     ascending:YES selector:@selector(caseInsensitiveCompare:)] autorelease];
4420         NSArray *sortDescriptors=[NSArray arrayWithObjects:presetTypeDescriptor,presetNameDescriptor,nil];
4421     NSArray *sortedArray=[UserPresets sortedArrayUsingDescriptors:sortDescriptors];
4422         [UserPresets setArray:sortedArray];
4423         /* We Reload the New Table data for presets */
4424     [tableView reloadData];
4425    /* We save all of the preset data here */
4426     [self savePreset];
4427 }
4428
4429 - (void)savePreset
4430 {
4431     [UserPresets writeToFile:UserPresetsFile atomically:YES];
4432         /* We get the default preset in case it changed */
4433         [self getDefaultPresets: NULL];
4434
4435 }
4436
4437 - (void) controlTextDidChange: (NSNotification *) notification
4438 {
4439     [self calculateBitrate: NULL];
4440 }
4441
4442 - (IBAction) openHomepage: (id) sender
4443 {
4444     [[NSWorkspace sharedWorkspace] openURL: [NSURL
4445         URLWithString:@"http://handbrake.m0k.org/"]];
4446 }
4447
4448 - (IBAction) openForums: (id) sender
4449 {
4450     [[NSWorkspace sharedWorkspace] openURL: [NSURL
4451         URLWithString:@"http://handbrake.m0k.org/forum/"]];
4452 }
4453 - (IBAction) openUserGuide: (id) sender
4454 {
4455     [[NSWorkspace sharedWorkspace] openURL: [NSURL
4456         URLWithString:@"http://handbrake.m0k.org/trac/wiki/HandBrakeGuide"]];
4457 }
4458
4459 /**
4460  * Shows debug output window.
4461  */
4462 - (IBAction)showDebugOutputPanel:(id)sender
4463 {
4464     [outputPanel showOutputPanel:sender];
4465 }
4466
4467 /**
4468  * Shows preferences window modally.
4469  */
4470 - (IBAction) showPreferencesWindow: (id) sender
4471 {
4472     NSWindow * window = [fPreferencesController window];
4473     if (![window isVisible])
4474         [window center];
4475
4476     [window makeKeyAndOrderFront: nil];
4477 }
4478
4479 /**
4480  * Shows queue window.
4481  */
4482 - (IBAction) showQueueWindow:(id)sender
4483 {
4484     [fQueueController showQueueWindow:sender];
4485 }
4486
4487
4488 - (IBAction) toggleDrawer:(id)sender {
4489     [fPresetDrawer toggle:self];
4490 }
4491
4492 @end