]> git.sesse.net Git - vlc/blobdiff - modules/gui/macosx/coredialogs.m
macosx: CAS: sync steppers and fields, and add NumberFormatters (fixes #8598)
[vlc] / modules / gui / macosx / coredialogs.m
index 8ed476de35395288595251b9641f29cf0beffcce..7d24fc63dcb7fd9b3500ecfff67b48017dc52cca 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * coredialogs.m: Mac OS X Core Dialogs
  *****************************************************************************
- * Copyright (C) 2005-2009 the VideoLAN team
+ * Copyright (C) 2005-2012 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Derk-Jan Hartman <hartman at videolan dot org>
 /* for the icon in our custom error panel */
 #import <ApplicationServices/ApplicationServices.h>
 
-
-/*****************************************************************************
- * Local prototypes.
- *****************************************************************************/
-
-bool b_progress_cancelled;
-static void updateProgressPanel (void *, const char *, float);
-static bool checkProgressPanel (void *);
-static void destroyProgressPanel (void *);
-
 /*****************************************************************************
  * VLCCoreDialogProvider implementation
  *****************************************************************************/
@@ -53,27 +43,14 @@ static VLCCoreDialogProvider *_o_sharedInstance = nil;
 
 -(id)init
 {
-    if( _o_sharedInstance )
+    if (_o_sharedInstance)
         [self dealloc];
-    else
-    {
+    else {
         _o_sharedInstance = [super init];
-        [[NSNotificationCenter defaultCenter] addObserver:self
-                                                 selector:@selector(performDialogEvent:)
-                                                     name: @"VLCNewCoreDialogEventNotification"
-                                                   object:self];
-        [[NSNotificationCenter defaultCenter] addObserver: self
-                                                 selector:@selector(performProgressBarEvent:)
-                                                     name:@"VLCCoreDialogProgressBarUpdateNotification" 
-                                                   object: self];
-        [[NSNotificationCenter defaultCenter] addObserver: self
-                                                 selector:@selector(performProgressBarEvent:)
-                                                     name:@"VLCCoreDialogProgressBarDestroyNotification" 
-                                                   object: self];
         o_error_panel = [[VLCErrorPanel alloc] init];
         b_progress_cancelled = NO;
     }
-    
+
     return _o_sharedInstance;
 }
 
@@ -86,41 +63,42 @@ static VLCCoreDialogProvider *_o_sharedInstance = nil;
     [o_prog_cancel_btn setTitle: _NS("Cancel")];
     [o_prog_bar setUsesThreadedAnimation: YES];
 
-}    
+}
 
--(void)performDialogEvent: (NSNotification *)o_notification
+-(void)performEventWithObject: (NSValue *)o_value ofType: (const char*)type
 {
-    NSValue *o_value = [[o_notification userInfo] objectForKey:@"VLCDialogPointer"];
-    NSString *o_type = [[o_notification userInfo] objectForKey:@"VLCDialogType"];
-
-    if( [o_type isEqualToString: @"dialog-fatal"] )
-        [self showFatalDialog: o_value];
-    else if( [o_type isEqualToString: @"dialog-question"] )
-        [self showQuestionDialog: o_value];
-    else if( [o_type isEqualToString: @"dialog-login"] )
-        [self showLoginDialog: o_value];
-    else if( [o_type isEqualToString: @"dialog-progress-bar"] )
-        [self showProgressDialog: o_value];
+    NSString *o_type = @(type);
+
+    if ([o_type isEqualToString: @"dialog-error"])
+        [self performSelectorOnMainThread:@selector(showFatalDialog:) withObject:o_value waitUntilDone:YES];
+    else if ([o_type isEqualToString: @"dialog-critical"])
+        [self performSelectorOnMainThread:@selector(showFatalWaitDialog:) withObject:o_value waitUntilDone:YES];
+    else if ([o_type isEqualToString: @"dialog-question"])
+        [self performSelectorOnMainThread:@selector(showQuestionDialog:) withObject:o_value waitUntilDone:YES];
+    else if ([o_type isEqualToString: @"dialog-login"])
+        [self performSelectorOnMainThread:@selector(showLoginDialog:) withObject:o_value waitUntilDone:YES];
+    else if ([o_type isEqualToString: @"dialog-progress-bar"])
+        [self performSelectorOnMainThread:@selector(showProgressDialogOnMainThread:) withObject: o_value waitUntilDone:YES];
     else
-        msg_Err( VLCIntf, "unhandled dialog type: '%s'", [o_type UTF8String] );
+        msg_Err(VLCIntf, "unhandled dialog type: '%s'", type);
 }
 
 -(void)showFatalDialog: (NSValue *)o_value
 {
     dialog_fatal_t *p_dialog = [o_value pointerValue];
-    /* do we need to block ? */
-    if( p_dialog->modal == YES )
-    {
-        NSAlert *o_alert;
-        o_alert = [NSAlert alertWithMessageText: [NSString stringWithUTF8String: p_dialog->title] defaultButton: _NS("OK") alternateButton: nil otherButton: nil informativeTextWithFormat: [NSString stringWithUTF8String: p_dialog->message]];
-        [o_alert setAlertStyle: NSCriticalAlertStyle];
-        [o_alert runModal];
-    }
-    else
-    {
-        [o_error_panel addError: [NSString stringWithUTF8String: p_dialog->title] withMsg: [NSString stringWithUTF8String: p_dialog->message]];
-        [o_error_panel showPanel];
-    }
+
+    [o_error_panel addError: @(p_dialog->title) withMsg: @(p_dialog->message)];
+    [o_error_panel showPanel];
+}
+
+-(void)showFatalWaitDialog: (NSValue *)o_value
+{
+    dialog_fatal_t *p_dialog = [o_value pointerValue];
+    NSAlert *o_alert;
+
+    o_alert = [NSAlert alertWithMessageText: @(p_dialog->title) defaultButton: _NS("OK") alternateButton: nil otherButton: nil informativeTextWithFormat: @"%s", p_dialog->message];
+    [o_alert setAlertStyle: NSCriticalAlertStyle];
+    [o_alert runModal];
 }
 
 -(void)showQuestionDialog: (NSValue *)o_value
@@ -129,23 +107,23 @@ static VLCCoreDialogProvider *_o_sharedInstance = nil;
     NSAlert *o_alert;
     NSString *o_yes, *o_no, *o_cancel;
     NSInteger i_returnValue = 0;
-    
-    if( p_dialog->yes != NULL )
-        o_yes = [NSString stringWithUTF8String: p_dialog->yes];
-    if( p_dialog->no != NULL )
-        o_no = [NSString stringWithUTF8String: p_dialog->no];
-    if( p_dialog->cancel != NULL )
-        o_cancel = [NSString stringWithUTF8String: p_dialog->cancel];
-
-    o_alert = [NSAlert alertWithMessageText: [NSString stringWithUTF8String: p_dialog->title] defaultButton: o_yes alternateButton:o_no otherButton: o_cancel informativeTextWithFormat: [NSString stringWithUTF8String: p_dialog->message]];
+
+    if (p_dialog->yes != NULL)
+        o_yes = @(p_dialog->yes);
+    if (p_dialog->no != NULL)
+        o_no = @(p_dialog->no);
+    if (p_dialog->cancel != NULL)
+        o_cancel = @(p_dialog->cancel);
+
+    o_alert = [NSAlert alertWithMessageText: @(p_dialog->title) defaultButton: o_yes alternateButton:o_no otherButton: o_cancel informativeTextWithFormat: @"%s", p_dialog->message];
     [o_alert setAlertStyle: NSInformationalAlertStyle];
     i_returnValue = [o_alert runModal];
 
-    if( i_returnValue == NSAlertDefaultReturn )
+    if (i_returnValue == NSAlertDefaultReturn)
         p_dialog->answer = 1;
-    if( i_returnValue == NSAlertAlternateReturn )
+    if (i_returnValue == NSAlertAlternateReturn)
         p_dialog->answer = 2;
-    if( i_returnValue == NSAlertOtherReturn )
+    if (i_returnValue == NSAlertOtherReturn)
         p_dialog->answer = 3;
 }
 
@@ -154,120 +132,107 @@ static VLCCoreDialogProvider *_o_sharedInstance = nil;
     dialog_login_t *p_dialog = [o_value pointerValue];
     NSInteger i_returnValue = 0;
 
-    [o_auth_title_txt setStringValue: [NSString stringWithUTF8String: p_dialog->title]];
-    [o_auth_win setTitle: [NSString stringWithUTF8String: p_dialog->title]];
-    [o_auth_description_txt setStringValue: [NSString stringWithUTF8String: p_dialog->message]];
+    [o_auth_title_txt setStringValue: @(p_dialog->title)];
+    [o_auth_win setTitle: @(p_dialog->title)];
+    [o_auth_description_txt setStringValue: @(p_dialog->message)];
     [o_auth_login_fld setStringValue: @""];
     [o_auth_pw_fld setStringValue: @""];
 
     [o_auth_win center];
     i_returnValue = [NSApp runModalForWindow: o_auth_win];
     [o_auth_win close];
-    if( i_returnValue )
-    {
-        *p_dialog->username = strdup( [[o_auth_login_fld stringValue] UTF8String] );
-        *p_dialog->password = strdup( [[o_auth_pw_fld stringValue] UTF8String] );
-    }
-    else
+    if (i_returnValue)
     {
-         *p_dialog->username = *p_dialog->password = NULL;
-    }
+        *p_dialog->username = strdup([[o_auth_login_fld stringValue] UTF8String]);
+        *p_dialog->password = strdup([[o_auth_pw_fld stringValue] UTF8String]);
+    } else
+        *p_dialog->username = *p_dialog->password = NULL;
 }
 
 -(IBAction)loginDialogAction:(id)sender
 {
-    if( [[sender title] isEqualToString: _NS("OK")] )
+    if ([[sender title] isEqualToString: _NS("OK")])
         [NSApp stopModalWithCode: 1];
     else
         [NSApp stopModalWithCode: 0];
 }
 
--(void)showProgressDialog: (NSValue *)o_value
+-(void)showProgressDialogOnMainThread: (NSValue *)o_value
 {
+    /* we work-around a Cocoa limitation here, since you cannot delay an execution
+     * on the main thread within a single call */
+    b_progress_cancelled = NO;
+
     dialog_progress_bar_t *p_dialog = [o_value pointerValue];
 
-    if( p_dialog->title != NULL )
-    {
-        [o_prog_win setTitle: [NSString stringWithUTF8String: p_dialog->title]];
-        [o_prog_title_txt setStringValue: [NSString stringWithUTF8String: p_dialog->title]];
-    }
-    else
+    if (!p_dialog || b_progress_cancelled)
+        return;
+
+    if (p_dialog->title != NULL)
     {
+        [o_prog_win setTitle: @(p_dialog->title)];
+        [o_prog_title_txt setStringValue: @(p_dialog->title)];
+    } else {
         [o_prog_win setTitle: @""];
         [o_prog_title_txt setStringValue: @""];
     }
-    if( p_dialog->cancel != NULL )
-        [o_prog_cancel_btn setTitle: [NSString stringWithUTF8String: p_dialog->cancel]];
+    if (p_dialog->cancel != NULL)
+        [o_prog_cancel_btn setTitle: @(p_dialog->cancel)];
     else
         [o_prog_cancel_btn setTitle: _NS("Cancel")];
-    if( p_dialog->message != NULL )
-        [o_prog_description_txt setStringValue: [NSString stringWithUTF8String: p_dialog->message]];
+    if (p_dialog->message != NULL)
+        [o_prog_description_txt setStringValue: @(p_dialog->message)];
     else
         [o_prog_description_txt setStringValue: @""];
-    [o_prog_bar setDoubleValue: 0];
 
-    p_dialog->pf_update = updateProgressPanel;
-    p_dialog->pf_check = checkProgressPanel;
-    p_dialog->pf_destroy = destroyProgressPanel;
-    p_dialog->p_sys = self;
-
-    [NSApp runModalForWindow: o_prog_win];
+    if (VLCIntf)
+        [self performSelector:@selector(showProgressDialog:) withObject: o_value afterDelay:3.00];
 }
 
--(void)performProgressBarEvent: (NSNotification *)o_notification
+-(void)showProgressDialog: (NSValue *)o_value
 {
-    NSLog( @"%@ received", [o_notification name] );
-    if( [[o_notification name] isEqualToString: @"VLCCoreDialogProgressBarUpdateNotification"] )
-    {
-        NSNumber *o_number = [[o_notification userInfo] objectForKey:@"IntValue"];
-        NSString *o_text = [[o_notification userInfo] objectForKey:@"Text"];
-        [o_prog_description_txt setStringValue: o_text];
-        [o_prog_bar setDoubleValue: [o_number doubleValue]];
-    }
-    if( [[o_notification name] isEqualToString: @"VLCCoreDialogProgressBarDestroyNotification"] )
-    {
-        [NSApp stopModalWithCode: 0];
-        [o_prog_win close];
-    }
-}
+    dialog_progress_bar_t *p_dialog = [o_value pointerValue];
 
-void updateProgressPanel (void *priv, const char *text, float value)
-{
-    NSLog( @"we were updated with %s (%f)", text, value );
-    NSString *o_txt;
-    if( text != NULL )
-        o_txt = [NSString stringWithUTF8String: text];
-    [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCCoreDialogProgressBarUpdateNotification" object:[[VLCMain sharedInstance] getCoreDialogProvider] userInfo:[NSDictionary dictionaryWithObjectsAndKeys: o_txt, @"Text", [NSNumber numberWithInt: ((int)(value * 1000.))], @"IntValue", nil]];
+    if (!p_dialog || b_progress_cancelled)
+        return;
+
+    [o_prog_bar setDoubleValue: 0];
+    [o_prog_bar setIndeterminate: YES];
+    [o_prog_bar startAnimation: self];
+
+    [o_prog_win makeKeyAndOrderFront: self];
 }
 
-void destroyProgressPanel (void *priv)
+-(void)updateProgressPanelWithText: (NSString *)string andNumber: (double)d_number
 {
-    NSLog( @"we should destroy" );
-    [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCCoreDialogProgressBarDestroyNotification" object:[[VLCMain sharedInstance] getCoreDialogProvider] userInfo: nil];
+    [o_prog_description_txt setStringValue: string];
+    if (d_number > 0)
+        [o_prog_bar setIndeterminate: NO];
+    [o_prog_bar setDoubleValue: d_number];
 }
 
-bool checkProgressPanel (void *priv)
+-(void)destroyProgressPanel
 {
-    NSLog( @"we were checked" );
-    return b_progress_cancelled;
+    b_progress_cancelled = YES;
+    [o_prog_bar performSelectorOnMainThread:@selector(stopAnimation:) withObject:self waitUntilDone:YES];
+    [o_prog_win performSelectorOnMainThread:@selector(close) withObject:nil waitUntilDone:YES];
 }
 
 -(IBAction)progDialogAction:(id)sender
 {
-    NSLog( @"buttonAction!" );
     b_progress_cancelled = YES;
 }
 
--(id)getErrorPanel
+-(BOOL)progressCancelled
 {
-    return o_error_panel;
+    return b_progress_cancelled;
 }
 
--(void)dealloc
+-(id)errorPanel
 {
-    [[NSNotificationCenter defaultCenter] removeObserver:self];
-    [super dealloc];
+    return o_error_panel;
 }
+
 @end
 
 /*****************************************************************************
@@ -278,12 +243,8 @@ bool checkProgressPanel (void *priv)
 {
     [super init];
 
-    nib_loaded = [NSBundle loadNibNamed:@"InteractionErrorPanel" owner:self];
-
-    /* init strings */
-    [o_window setTitle: _NS("Errors and Warnings")];
-    [o_cleanup_button setTitle: _NS("Clean up")];
-    [o_messages_btn setTitle: _NS("Show Details")];
+    if (!b_nib_loaded)
+        b_nib_loaded = [NSBundle loadNibNamed:@"ErrorPanel" owner:self];
 
     /* init data sources */
     o_errors = [[NSMutableArray alloc] init];
@@ -292,6 +253,14 @@ bool checkProgressPanel (void *priv)
     return self;
 }
 
+- (void)awakeFromNib
+{
+    /* init strings */
+    [o_window setTitle: _NS("Errors and Warnings")];
+    [o_cleanup_button setTitle: _NS("Clean up")];
+    [o_messages_btn setTitle: _NS("Show Details")];
+}
+
 -(void)dealloc
 {
     [o_errors release];
@@ -315,11 +284,11 @@ bool checkProgressPanel (void *priv)
     [ourError
         addAttribute: NSFontAttributeName
         value: [NSFont boldSystemFontOfSize:11]
-        range: NSMakeRange( 0, [o_error length])];
+        range: NSMakeRange(0, [o_error length])];
     [o_errors addObject: ourError];
     [ourError release];
 
-    [o_icons addObject: [NSImage imageWithErrorIcon]];
+    [o_icons addObject: [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kAlertStopIcon)]];
 
     [o_error_table reloadData];
 }
@@ -347,11 +316,11 @@ bool checkProgressPanel (void *priv)
 - (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn:
     (NSTableColumn *)theTableColumn row: (NSInteger)row
 {
-    if( [[theTableColumn identifier] isEqualToString: @"error_msg"] )
-        return [o_errors objectAtIndex: row];
+    if ([[theTableColumn identifier] isEqualToString: @"error_msg"])
+        return o_errors[row];
 
-    if( [[theTableColumn identifier] isEqualToString: @"icon"] )
-        return [o_icons objectAtIndex: row];
+    if ([[theTableColumn identifier] isEqualToString: @"icon"])
+        return o_icons[row];
 
     return @"unknown identifier";
 }