OSDN Git Service

MacGui: Fixed bitrate calculation for target size.
[handbrake-jp/handbrake-jp-git.git] / macosx / Controller.mm
index 47b46dc..856ab21 100644 (file)
@@ -80,7 +80,7 @@ static int hb_group_count(hb_handle_t * h)
     [HBPreferencesController registerUserDefaults];
     fHandle = NULL;
     outputPanel = [[HBOutputPanelController alloc] init];
-    fPictureController = [[PictureController alloc] init];
+    fPictureController = [[PictureController alloc] initWithDelegate:self];
     fQueueController = [[HBQueueController alloc] init];
     fAdvancedOptions = [[HBAdvancedController alloc] init];
     return self;
@@ -104,11 +104,11 @@ static int hb_group_count(hb_handle_t * h)
     fChapterTitlesDelegate = [[ChapterTitles alloc] init];
     [fChapterTable setDataSource:fChapterTitlesDelegate];
 
-     /* Call UpdateUI every 2/10 sec */
+     /* Call UpdateUI every 1/2 sec */
     [[NSRunLoop currentRunLoop] addTimer: [NSTimer
-        scheduledTimerWithTimeInterval: 0.2 target: self
-        selector: @selector( updateUI: ) userInfo: NULL repeats: FALSE]
-        forMode: NSModalPanelRunLoopMode];
+        scheduledTimerWithTimeInterval: 0.5 target: self
+        selector: @selector( updateUI: ) userInfo: NULL repeats: YES]
+        forMode: NSEventTrackingRunLoopMode];
 
     if( ( build = hb_check_update( fHandle, &version ) ) > -1 )
     {
@@ -165,14 +165,7 @@ static int hb_group_count(hb_handle_t * h)
     [fWindow center];
     [fWindow setExcludedFromWindowsMenu:YES];
     [fAdvancedOptions setView:fAdvancedView];
-       /* set the main menu bar so it doesnt auto enable the menu items
-          so we can manually do it with setEnabled: This should be changed
-          to use validateUserInterfaceItem: along with setAutoEnablesItems: YES
-          in the next release */
-       [fMenuBarFileMenu setAutoenablesItems: NO];
-    [fMenuBarWindowMenu setAutoenablesItems: NO];
-       [fMenuPauseEncode setEnabled: NO];
-       [self TranslateStrings];
+
     /* Initialize currentScanCount so HB can use it to
                evaluate successive scans */
        currentScanCount = 0;
@@ -519,18 +512,78 @@ static int hb_group_count(hb_handle_t * h)
     return NO;
 }
 
+- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
+{
+    SEL action = [menuItem action];
+    
+    hb_state_t s;
+    hb_get_state2( fHandle, &s );
+    
+    if (fHandle)
+    {
+        if (action == @selector(addToQueue:) || action == @selector(showPicturePanel:) || action == @selector(showAddPresetPanel:))
+            return SuccessfulScan && [fWindow attachedSheet] == nil;
+        
+        if (action == @selector(showScanPanel:))
+        {
+            if (s.state == HB_STATE_SCANNING)
+                return NO;
+            else
+                return [fWindow attachedSheet] == nil;
+        }
+        if (action == @selector(selectDefaultPreset:))
+            return [tableView selectedRow] >= 0 && [fWindow attachedSheet] == nil;
+        if (action == @selector(Pause:))
+        {
+            if (s.state == HB_STATE_WORKING)
+            {
+                if(![[menuItem title] isEqualToString:@"Pause Encoding"])
+                    [menuItem setTitle:@"Pause Encoding"];
+                return YES;
+            }
+            else if (s.state == HB_STATE_PAUSED)
+            {
+                if(![[menuItem title] isEqualToString:@"Resume Encoding"])
+                    [menuItem setTitle:@"Resume Encoding"];
+                return YES;
+            }
+            else
+                return NO;
+        }
+        if (action == @selector(Rip:))
+            if (s.state == HB_STATE_WORKING || s.state == HB_STATE_MUXING || s.state == HB_STATE_PAUSED)
+            {
+                if(![[menuItem title] isEqualToString:@"Stop Encoding"])
+                    [menuItem setTitle:@"Stop Encoding"];
+                return YES;
+            }
+            else if (SuccessfulScan)
+            {
+                if(![[menuItem title] isEqualToString:@"Start Encoding"])
+                    [menuItem setTitle:@"Start Encoding"];
+                return [fWindow attachedSheet] == nil;
+            }
+            else
+                return NO;
+        }
+    
+    return YES;
+}
+
+
 // register a test notification and make
 // it enabled by default
 #define SERVICE_NAME @"Encode Done"
 - (NSDictionary *)registrationDictionaryForGrowl 
 { 
-NSDictionary *registrationDictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
-[NSArray arrayWithObjects:SERVICE_NAME,nil], GROWL_NOTIFICATIONS_ALL, 
-[NSArray arrayWithObjects:SERVICE_NAME,nil], GROWL_NOTIFICATIONS_DEFAULT, 
-nil]; 
+    NSDictionary *registrationDictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
+    [NSArray arrayWithObjects:SERVICE_NAME,nil], GROWL_NOTIFICATIONS_ALL, 
+    [NSArray arrayWithObjects:SERVICE_NAME,nil], GROWL_NOTIFICATIONS_DEFAULT, 
+    nil]; 
 
-return registrationDictionary; 
+    return registrationDictionary; 
 } 
+
 - (void) TranslateStrings
 {
     [fSrcTitleField     setStringValue: _( @"Title:" )];
@@ -635,8 +688,8 @@ return registrationDictionary;
 - (void) updateUI: (NSTimer *) timer
 {
 
-hb_list_t  * list;
-list = hb_get_titles( fHandle );       
+    hb_list_t  * list;
+    list = hb_get_titles( fHandle );   
     /* check to see if there has been a new scan done
        this bypasses the constraints of HB_STATE_WORKING
        not allowing setting a newly scanned source */
@@ -731,10 +784,6 @@ list = hb_get_titles( fHandle );
             /* Update dock icon */
             [self UpdateDockIcon: progress_total];
                        
-                       /* Main Menu controls */
-                       [fMenuPauseEncode setTitle: @"Pause Encode"];
-                       [fMenuStartEncode setTitle: @"Cancel Encode"];
-
             // Has current job changed? That means the queue has probably changed as
                        // well so update it
             if (fLastKnownCurrentJob != hb_current_job(fHandle))
@@ -775,8 +824,6 @@ list = hb_get_titles( fHandle );
         case HB_STATE_PAUSED:
                    [fStatusField setStringValue: _( @"Paused" )];
             
-                       [fMenuPauseEncode setTitle: @"Resume Encode"];
-
                        // Pass along the info to HBQueueController
             [fQueueController updateCurrentJobUI];
 
@@ -788,11 +835,7 @@ list = hb_get_titles( fHandle );
             [fRipIndicator setIndeterminate: NO];
             [fRipIndicator setDoubleValue: 0.0];
             [toolbar validateVisibleItems];
-            
-                       /* Main Menu Controls*/
-                       [fMenuPauseEncode setTitle: @"Pause Encode"];
-                       [fMenuPauseEncode setEnabled: NO];
-                       [fMenuStartEncode setTitle: @"Start Encode"];
+
             /* Restore dock icon */
             [self UpdateDockIcon: -1.0];
 
@@ -910,11 +953,6 @@ list = hb_get_titles( fHandle );
        {
                [fQueueStatus setStringValue: @""];
        }
-       
-    [[NSRunLoop currentRunLoop] addTimer: [NSTimer
-        scheduledTimerWithTimeInterval: 0.5 target: self
-                                                         selector: @selector( updateUI: ) userInfo: NULL repeats: FALSE]
-                                                                forMode: NSModalPanelRunLoopMode];
 }
 
 - (IBAction) showNewScan:(id)sender
@@ -930,7 +968,6 @@ list = hb_get_titles( fHandle );
        {
                /* We display a message if a valid dvd source was not chosen */
                [fSrcDVD2Field setStringValue: @"No Valid Title Found"];
-        [fMenuOpenSource setEnabled: YES];
         SuccessfulScan = 0;
        }
        else
@@ -944,12 +981,7 @@ list = hb_get_titles( fHandle );
         currentSuccessfulScanCount++;
 
                [self enableUI: YES];
-               /* Enable/Disable Menu Controls Accordingly */
-               [fMenuOpenSource setEnabled: YES];
-               [fMenuStartEncode setEnabled: YES];
-               [fMenuAddToQueue setEnabled: YES];
-               [fMenuPicturePanelShow setEnabled: YES];
-               [fMenuQueuePanelShow setEnabled: YES];
+
         [toolbar validateVisibleItems];
                
                [fSrcTitlePopUp removeAllItems];
@@ -1132,13 +1164,6 @@ list = hb_get_titles( fHandle );
 
 - (IBAction) showScanPanel: (id) sender
 {
-    /* Enable/Disable Menu Controls Accordingly */
-       [fMenuOpenSource setEnabled: NO];
-       [fMenuStartEncode setEnabled: NO];
-       [fMenuAddToQueue setEnabled: NO];
-       
-       [fMenuPicturePanelShow setEnabled: NO];
-       [fMenuQueuePanelShow setEnabled: NO];
     [self enableUI: NO];
        
        [self browseSources:NULL];
@@ -1196,17 +1221,10 @@ list = hb_get_titles( fHandle );
        }
        else // User clicked Cancel in browse window
        {
-               /* use the outlets to the main menu bar to determine what to
-                  enable and disable */
-               [fMenuOpenSource setEnabled: YES];
-        [fMenuQueuePanelShow setEnabled: YES];
                /* if we have a title loaded up */
                if ([[fSrcDVD2Field stringValue] length] > 0)
                {
-               [fMenuAddToQueue setEnabled: YES];
-               [fMenuStartEncode setEnabled: YES];
-               [fMenuPicturePanelShow setEnabled: YES];
-        [self enableUI: YES];
+            [self enableUI: YES];
         }
        }
 }
@@ -1297,34 +1315,19 @@ list = hb_get_titles( fHandle );
     if( returnCode == NSOKButton )
     {
         [fDstFile2Field setStringValue: [sheet filename]];
-               
     }
 }
 
 - (IBAction) showPicturePanel: (id) sender
 {
-    /* Enable/Disable Menu Controls Accordingly */
-       [fMenuOpenSource setEnabled: NO];
-       [fMenuStartEncode setEnabled: NO];
-       [fMenuAddToQueue setEnabled: NO];
-       
-       [fMenuPicturePanelShow setEnabled: NO];
-       [fMenuQueuePanelShow setEnabled: NO];
-       
        hb_list_t  * list  = hb_get_titles( fHandle );
     hb_title_t * title = (hb_title_t *) hb_list_item( list,
             [fSrcTitlePopUp indexOfSelectedItem] );
-    
     [fPictureController showPanelInWindow:fWindow forTitle:title];
+}
 
-       [fMenuOpenSource setEnabled: YES];
-       [fMenuStartEncode setEnabled: YES];
-       [fMenuAddToQueue setEnabled: YES];
-       
-       [fMenuPicturePanelShow setEnabled: YES];
-       [fMenuQueuePanelShow setEnabled: YES];
-       
-       [self calculatePictureSizing: sender];
+- (void)pictureSettingsDidChange {
+       [self calculatePictureSizing: NULL];
 }
 
 - (void) PrepareJob
@@ -1724,8 +1727,6 @@ list = hb_get_titles( fHandle );
     hb_start( fHandle );
        /*set the fEncodeState State */
        fEncodeState = 1;
-
-       [fMenuPauseEncode setEnabled: YES];
 }
 
 - (IBAction) Cancel: (id) sender
@@ -1766,7 +1767,7 @@ list = hb_get_titles( fHandle );
 {
     hb_list_t  * list  = hb_get_titles( fHandle );
     hb_title_t * title = (hb_title_t*)
-        hb_list_item( list, [fSrcTitlePopUp indexOfSelectedItem] );
+    hb_list_item( list, [fSrcTitlePopUp indexOfSelectedItem] );
                
                
     /* If Auto Naming is on. We create an output filename of dvd name - title number */
@@ -1810,7 +1811,6 @@ list = hb_get_titles( fHandle );
        /* Pixel Ratio Setting */
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"PixelRatio"])
     {
-
                job->pixel_ratio = 1 ;
        }
        else
@@ -1897,7 +1897,6 @@ list = hb_get_titles( fHandle );
     }
 
                
-       
        hb_list_t  * list  = hb_get_titles( fHandle );
     hb_title_t * title = (hb_title_t *)
         hb_list_item( list, [fSrcTitlePopUp indexOfSelectedItem] );
@@ -2497,12 +2496,12 @@ list = hb_get_titles( fHandle );
 {
        if ([fSubPopUp indexOfSelectedItem] == 0)
        {
-       [fSubForcedCheck setState: NSOffState];
-       [fSubForcedCheck setEnabled: NO];       
+        [fSubForcedCheck setState: NSOffState];
+        [fSubForcedCheck setEnabled: NO];      
        }
        else
        {
-       [fSubForcedCheck setEnabled: YES];      
+        [fSubForcedCheck setEnabled: YES];     
        }
        
 }
@@ -2667,7 +2666,7 @@ list = hb_get_titles( fHandle );
     [fVidBitrateField setIntValue: hb_calc_bitrate( job,
             [fVidTargetSizeField intValue] )];
                        
-                       
+    [self customSettingUsed:sender];
 }
 
 /* Method to determine if we should change the UI
@@ -2684,8 +2683,6 @@ the user is using "Custom" settings by determining the sender*/
                [fPresetSelectedDisplay setStringValue: @"Custom"];
                
                curUserPresetChosenNum = nil;
-
-               
        }
 
 }
@@ -2748,46 +2745,40 @@ the user is using "Custom" settings by determining the sender*/
 - (IBAction) showAddPresetPanel: (id) sender
 {
     /* Deselect the currently selected Preset if there is one*/
-               [tableView deselectRow:[tableView selectedRow]];
+    [tableView deselectRow:[tableView selectedRow]];
 
-       /* Populate the preset picture settings popup here */
-       [fPresetNewPicSettingsPopUp removeAllItems];
-       [fPresetNewPicSettingsPopUp addItemWithTitle:@"None"];
-       [fPresetNewPicSettingsPopUp addItemWithTitle:@"Current"];
-       [fPresetNewPicSettingsPopUp addItemWithTitle:@"Source Maximum (post source scan)"];
-       [fPresetNewPicSettingsPopUp selectItemAtIndex: 0];      
+    /* Populate the preset picture settings popup here */
+    [fPresetNewPicSettingsPopUp removeAllItems];
+    [fPresetNewPicSettingsPopUp addItemWithTitle:@"None"];
+    [fPresetNewPicSettingsPopUp addItemWithTitle:@"Current"];
+    [fPresetNewPicSettingsPopUp addItemWithTitle:@"Source Maximum (post source scan)"];
+    [fPresetNewPicSettingsPopUp selectItemAtIndex: 0]; 
        
-               /* Erase info from the input fields fPresetNewDesc*/
+    /* Erase info from the input fields fPresetNewDesc*/
        [fPresetNewName setStringValue: @""];
        [fPresetNewDesc setStringValue: @""];
        /* Show the panel */
-       [NSApp beginSheet: fAddPresetPanel modalForWindow: fWindow
-        modalDelegate: NULL didEndSelector: NULL contextInfo: NULL];
-    [NSApp runModalForWindow: fAddPresetPanel];
-    [NSApp endSheet: fAddPresetPanel];
-    [fAddPresetPanel orderOut: self];
-       
-       
+       [NSApp beginSheet: fAddPresetPanel modalForWindow: fWindow modalDelegate: NULL didEndSelector: NULL contextInfo: NULL];
 }
+
 - (IBAction) closeAddPresetPanel: (id) sender
 {
-       [NSApp stopModal];
+    [NSApp endSheet: fAddPresetPanel];
+    [fAddPresetPanel orderOut: self];
 }
 
-
 - (IBAction)addUserPreset:(id)sender
 {
-
-    /* Here we create a custom user preset */
-       [UserPresets addObject:[self createPreset]];
-       /* Erase info from the input fields */
-       [fPresetNewName setStringValue: @""];
-       [fPresetNewDesc setStringValue: @""];
-       /* We stop the modal window for the new preset */
-       [NSApp stopModal];
-    [self addPreset];
-       
-
+    if (![[fPresetNewName stringValue] length])
+            NSRunAlertPanel(@"Warning!", @"You need to insert a name for the preset.", @"OK", nil , nil);
+    else
+    {
+        /* Here we create a custom user preset */
+        [UserPresets addObject:[self createPreset]];
+        [self addPreset];
+        
+        [self closeAddPresetPanel:NULL];
+    }
 }
 - (void)addPreset
 {
@@ -4390,12 +4381,12 @@ show the built in presets in a blue font. */
      /* if there is a description for the preset, we show it in the tooltip */
         if ([[UserPresets objectAtIndex:rowIndex] valueForKey:@"PresetDescription"])
         {
-        loc_tip = [NSString stringWithFormat: @"%@",[[UserPresets objectAtIndex:rowIndex] valueForKey:@"PresetDescription"]];
-        return (loc_tip);
+        loc_tip = [NSString stringWithFormat: @"%@",[[UserPresets objectAtIndex:rowIndex] valueForKey:@"PresetDescription"]];
+        return (loc_tip);
         }
         else
         {
-        loc_tip = @"No description available";
+        loc_tip = @"No description available";
         }
         return (loc_tip);
 
@@ -4437,7 +4428,6 @@ id theRecord, theValue;
     [self savePreset];
 }
 
-
 - (void)savePreset
 {
     [UserPresets writeToFile:UserPresetsFile atomically:YES];
@@ -4446,8 +4436,6 @@ id theRecord, theValue;
 
 }
 
-
-
 - (void) controlTextDidBeginEditing: (NSNotification *) notification
 {
     [self calculateBitrate: NULL];