]> git.sesse.net Git - vlc/blob - modules/gui/macosx/interaction.m
* fixed keywords and encoding
[vlc] / modules / gui / macosx / interaction.m
1 /*****************************************************************************
2  * interaction.h: Mac OS X interaction dialogs
3  *****************************************************************************
4  * Copyright (C) 2005-2006 the VideoLAN team
5  * $Id $
6  *
7  * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8  *          Felix Kühne <fkuehne at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include "intf.h"
26 #import "interaction.h"
27
28 /*****************************************************************************
29  * VLCInteractionList implementation
30  *****************************************************************************/
31 @implementation VLCInteractionList
32
33 -(id)init
34 {
35     [super init];
36     o_interaction_list = [[NSMutableArray alloc] initWithCapacity:1];
37     [[NSNotificationCenter defaultCenter] addObserver:self
38         selector:@selector(newInteractionEvent:)
39         name: @"VLCNewInteractionEventNotification"
40         object:self];
41
42     return self;
43 }
44
45 -(void)newInteractionEvent: (NSNotification *)o_notification
46 {
47     VLCInteraction *o_interaction;
48     NSValue *o_value = [[o_notification userInfo] objectForKey:@"VLCDialogPointer"];
49     interaction_dialog_t *p_dialog = [o_value pointerValue];
50
51     switch( p_dialog->i_action )
52     {
53     case INTERACT_NEW:
54         [self addInteraction: p_dialog];
55         break;
56     case INTERACT_UPDATE:
57         o_interaction = (VLCInteraction *)p_dialog->p_private;
58         [o_interaction updateDialog];
59         break;
60     case INTERACT_HIDE:
61         o_interaction = (VLCInteraction *)p_dialog->p_private;
62         [o_interaction hideDialog];
63         break;
64     case INTERACT_DESTROY:
65         o_interaction = (VLCInteraction *)p_dialog->p_private;
66         [o_interaction destroyDialog];
67         [self removeInteraction:o_interaction];
68         p_dialog->i_status = DESTROYED_DIALOG;
69         break;
70     }
71 }
72
73 -(void)addInteraction: (interaction_dialog_t *)p_dialog
74 {
75
76     VLCInteraction *o_interaction = [[VLCInteraction alloc] initDialog: p_dialog];
77     
78     p_dialog->p_private = (void *)o_interaction;
79     [o_interaction_list addObject:[o_interaction autorelease]];
80     [o_interaction runDialog];
81 }
82
83 -(void)removeInteraction: (VLCInteraction *)o_interaction
84 {
85     [o_interaction_list removeObject:o_interaction];
86 }
87
88 -(void)dealloc
89 {
90     [[NSNotificationCenter defaultCenter] removeObserver:self];
91     [o_interaction_list removeAllObjects];
92     [o_interaction_list release];
93     [super dealloc];
94 }
95
96 @end
97
98 /*****************************************************************************
99  * VLCInteraction implementation
100  *****************************************************************************/
101 @implementation VLCInteraction
102
103 -(id)initDialog: (interaction_dialog_t *)_p_dialog
104 {
105     p_intf = VLCIntf;
106     [super init];
107     p_dialog = _p_dialog;
108     return self;
109 }
110
111 -(void)runDialog
112 {
113     int i = 0;
114     id o_window = NULL;
115     if( !p_dialog )
116         msg_Err( p_intf, "serious issue (p_dialog == nil)" );
117
118     if( !nib_interact_loaded )
119         nib_interact_loaded = [NSBundle loadNibNamed:@"Interaction" owner:self];
120
121     NSString *o_title = [NSString stringWithUTF8String:p_dialog->psz_title ? p_dialog->psz_title : "title"];
122     NSString *o_description = [NSString stringWithUTF8String:p_dialog->psz_description ? p_dialog->psz_description : ""];
123     
124     vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
125     if( p_vout != NULL )
126     {
127         NSEnumerator * o_enum = [[NSApp orderedWindows] objectEnumerator];
128
129         while( ( o_window = [o_enum nextObject] ) )
130         {
131             if( [[o_window className] isEqualToString: @"VLCWindow"] )
132             {
133                 vlc_object_release( (vlc_object_t *)p_vout );
134                 break;
135             }
136         }
137         vlc_object_release( (vlc_object_t *)p_vout );
138     }
139     else
140     {
141         o_window = [NSApp mainWindow];
142     }
143     
144     msg_Dbg( p_intf, "Title: %s", [o_title UTF8String] );
145     msg_Dbg( p_intf, "Description: %s", [o_description UTF8String] );
146     if( p_dialog->i_id == DIALOG_ERRORS )
147     {
148         for( i = 0; i < p_dialog->i_widgets; i++ )
149         {
150             msg_Err( p_intf, "Error: %s", p_dialog->pp_widgets[i]->psz_text );
151         }
152     }
153     else
154     {
155         for( i = 0; i < p_dialog->i_widgets; i++ )
156         {
157             msg_Dbg( p_intf, "widget: %s", p_dialog->pp_widgets[i]->psz_text );
158             o_description = [o_description stringByAppendingString: \
159                 [NSString stringWithUTF8String: \
160                     p_dialog->pp_widgets[i]->psz_text]];
161         }
162         if( p_dialog->i_flags & DIALOG_OK_CANCEL )
163         {
164             NSBeginInformationalAlertSheet( o_title, @"OK" , @"Cancel", nil, \
165                 o_window, self,@selector(sheetDidEnd: returnCode: contextInfo:),\
166                 NULL, nil, o_description );
167         }
168         else if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
169         {
170             NSBeginInformationalAlertSheet( o_title, @"Yes", @"No", @"Cancel", \
171                 o_window, self,@selector(sheetDidEnd: returnCode: contextInfo:),\
172                 NULL, nil, o_description );
173         }
174         else if( p_dialog->i_type & WIDGET_PROGRESS )
175         {
176             [o_prog_title setStringValue: o_title];
177             [o_prog_description setStringValue: o_description];
178             [o_prog_bar setUsesThreadedAnimation: YES];
179             [o_prog_bar setDoubleValue: 0];
180             [NSApp beginSheet: o_prog_win modalForWindow: o_window \
181                 modalDelegate: self didEndSelector: \
182                 nil \
183                 contextInfo: nil];
184             [o_prog_win makeKeyWindow];
185         }
186         else
187             msg_Warn( p_intf, "requested dialog type not implemented yet" );
188     }
189 }
190
191 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
192     contextInfo:(void *)o_context
193 {
194     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
195     if( i_return == NSAlertDefaultReturn )
196     {
197         p_dialog->i_return = DIALOG_OK_YES;
198     }
199     else if( i_return == NSAlertAlternateReturn && ( p_dialog->i_flags & DIALOG_OK_CANCEL ) )
200     {
201         p_dialog->i_return = DIALOG_CANCELLED;
202     }
203     else if( i_return == NSAlertAlternateReturn )
204     {
205         p_dialog->i_return = DIALOG_NO;
206     }
207     else if( i_return == NSAlertOtherReturn )
208     {
209         p_dialog->i_return = DIALOG_CANCELLED;
210     }
211     p_dialog->i_status = ANSWERED_DIALOG;
212     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
213 }
214
215 -(void)updateDialog
216 {
217     int i = 0;
218     for( i = 0 ; i< p_dialog->i_widgets; i++ )
219     {
220         /*msg_Dbg( p_intf, "update event, current value %i for index %i",
221         (int)(p_dialog->pp_widgets[i]->val.f_float), i);*/
222         if( p_dialog->i_type & WIDGET_PROGRESS )
223             [o_prog_bar setDoubleValue: \
224                 (double)(p_dialog->pp_widgets[i]->val.f_float)];
225     }
226 }
227
228 -(void)hideDialog
229 {
230     msg_Dbg( p_intf, "hide event" );
231     if( p_dialog->i_type & WIDGET_PROGRESS )
232     {
233         [NSApp endSheet: o_prog_win];
234         [o_prog_win close];
235     }
236 }
237
238 -(void)destroyDialog
239 {
240     msg_Dbg( p_intf, "destroy event" );
241 }
242
243 - (IBAction)cancelAndClose:(id)sender
244 {
245     /* tell the core that the dialog was cancelled */
246     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
247     p_dialog->i_return = DIALOG_CANCELLED;
248     p_dialog->i_status = ANSWERED_DIALOG;
249     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
250     msg_Dbg( p_intf, "dialog cancelled" );
251 }
252
253 @end