70ce18c61e4382039c9a5ec4d72338e8d509e925
[vlc.git] / modules / gui / macosx / info.m
1 /*****************************************************************************
2  * info.m: MacOS X info panel
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: info.m,v 1.4 2003/03/14 01:23:06 hartman Exp $
6  *
7  * Authors: Derk-Jan Hartman <thedj@users.sourceforge.net>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include "intf.h"
25 #include "info.h"
26
27 /*****************************************************************************
28  * VLCInfo implementation 
29  *****************************************************************************/
30 @implementation VLCInfo
31
32 - (void)awakeFromNib
33 {
34     [o_window setExcludedFromWindowsMenu: YES];
35 }
36
37 - (id)init
38 {
39     self = [super init];
40
41     if( self != nil )
42     {
43         o_strings = [[NSMutableDictionary alloc] init];
44     }
45
46     return( self );
47 }
48
49 - (void)dealloc
50 {
51     [o_strings release];
52     [super dealloc];
53 }
54
55 - (IBAction)toggleInfoPanel:(id)sender
56 {
57     if( [o_window isVisible] )
58     {
59         [o_window orderOut: sender];
60     }
61     else
62     {
63         [o_window orderFront: sender];
64         [self updateInfo];
65     }
66 }
67
68 - (IBAction)showCategory:(id)sender
69 {
70     NSString * o_title = [o_selector titleOfSelectedItem];
71     [o_view setString: [o_strings objectForKey: o_title]];
72     [o_view setNeedsDisplay: YES];
73 }
74
75 - (void)updateInfo
76 {
77     if( ![o_window isVisible] )
78     {
79         return;
80     }
81
82     intf_thread_t * p_intf = [NSApp getIntf]; 
83     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
84                                                        FIND_ANYWHERE );
85
86     if( p_playlist == NULL )
87     {
88         return;
89     }
90
91     vlc_mutex_lock( &p_playlist->object_lock );
92
93     if ( p_playlist->p_input == NULL )
94     {
95         vlc_mutex_unlock( &p_playlist->object_lock );
96         vlc_object_release( p_playlist );
97         return;
98     }
99
100     [o_strings removeAllObjects];
101     [o_selector removeAllItems];
102
103     vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
104     input_info_category_t * p_category = p_playlist->p_input->stream.p_info;
105
106     while( p_category )
107     {
108         [self createInfoView: p_category];
109         p_category = p_category->p_next;
110     }
111
112     vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
113     vlc_mutex_unlock( &p_playlist->object_lock );
114     vlc_object_release( p_playlist );
115
116     [o_selector selectItemAtIndex: 0];
117     [self showCategory: o_selector];
118 }
119
120 - (void)createInfoView:(input_info_category_t *)p_category
121 {
122     NSString * o_title;
123     NSMutableString * o_content;
124     input_info_t * p_info;
125
126     /* Add a category */
127     o_title = [NSString stringWithCString: p_category->psz_name];
128     [o_selector addItemWithTitle: o_title];
129
130     /* Create empty content string */
131     o_content = [NSMutableString string];
132
133     /* Add the fields */
134     p_info = p_category->p_info;
135
136     while( p_info )
137     {
138         [o_content appendFormat: @"%@: %@\n\n", [NSApp localizedString: p_info->psz_name],
139                                                 [NSApp localizedString: p_info->psz_value]]; 
140         p_info = p_info->p_next;
141     }
142
143     [o_strings setObject: o_content forKey: o_title];
144 }
145
146 @end
147
148 @implementation VLCInfo (NSMenuValidation)
149  
150 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
151 {
152     BOOL bEnabled = TRUE;
153
154     intf_thread_t * p_intf = [NSApp getIntf];
155     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
156                                                        FIND_ANYWHERE );
157
158     if( p_playlist != NULL )
159     {
160         vlc_mutex_lock( &p_playlist->object_lock );
161     }
162
163     if( [[o_mi title] isEqualToString: _NS("Info")] )
164     {
165         if( p_playlist == NULL || p_playlist->p_input == NULL )
166         {
167             bEnabled = FALSE;
168         }
169     }
170     
171     if( p_playlist != NULL )
172     {
173         vlc_mutex_unlock( &p_playlist->object_lock );
174         vlc_object_release( p_playlist );
175     }
176
177     return( bEnabled );
178 }
179
180 @end