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