]> git.sesse.net Git - vlc/blob - modules/gui/macosx/about.m
macosx: robustify media key trap handling
[vlc] / modules / gui / macosx / about.m
1 /*****************************************************************************
2  * about.m: MacOS X About Panel
3  *****************************************************************************
4  * Copyright (C) 2001-2014 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <thedj@users.sourceforge.net>
8  *          Felix Paul Kühne <fkuehne -at- videolan.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 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #import "intf.h"
29 #import "about.h"
30 #import <vlc_intf_strings.h>
31 #import <vlc_about.h>
32 #import "CompatibilityFixes.h"
33
34 /* this is a bit weird, but we should be confident that there will be more than
35  * one arch to support again one day */
36 #define PLATFORM "Intel 64bit"
37
38 /*****************************************************************************
39  * VLAboutBox implementation
40  *****************************************************************************/
41 @implementation VLAboutBox
42
43 static VLAboutBox *_o_sharedInstance = nil;
44
45 + (VLAboutBox *)sharedInstance
46 {
47     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
48 }
49
50 - (id)init
51 {
52     if (_o_sharedInstance)
53         [self dealloc];
54     else
55         _o_sharedInstance = [super init];
56
57     return _o_sharedInstance;
58 }
59
60 - (void) dealloc
61 {
62     [o_authors release];
63     [[NSNotificationCenter defaultCenter] removeObserver: self];
64     [super dealloc];
65 }
66
67 - (void)awakeFromNib
68 {
69     if (!OSX_SNOW_LEOPARD)
70         [o_about_window setCollectionBehavior: NSWindowCollectionBehaviorFullScreenAuxiliary];
71 }
72
73 /*****************************************************************************
74 * VLC About Window
75 *****************************************************************************/
76
77 - (void)showAbout
78 {
79     if (! b_isSetUp) {
80         /* Get the localized info dictionary (InfoPlist.strings) */
81         NSDictionary *o_local_dict;
82         o_local_dict = [[NSBundle mainBundle] localizedInfoDictionary];
83
84         /* Setup the copyright field */
85         [o_copyright_field setStringValue: [o_local_dict objectForKey:@"NSHumanReadableCopyright"]];
86
87         /* l10n */
88         [o_about_window setTitle: _NS("About VLC media player")];
89         NSDictionary *stringAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:NSUnderlineStyleSingle], NSUnderlineStyleAttributeName, [NSColor colorWithCalibratedRed:0. green:0.3411 blue:0.6824 alpha:1.], NSForegroundColorAttributeName, [NSFont systemFontOfSize:13], NSFontAttributeName, nil];
90         NSAttributedString *attrStr;
91         attrStr = [[NSAttributedString alloc] initWithString:_NS("Credits") attributes:stringAttributes];
92         [o_credits_btn setAttributedTitle:attrStr];
93         [attrStr release];
94         attrStr = [[NSAttributedString alloc] initWithString:_NS("License") attributes:stringAttributes];
95         [o_gpl_btn setAttributedTitle:attrStr];
96         [attrStr release];
97         attrStr = [[NSAttributedString alloc] initWithString:_NS("Authors") attributes:stringAttributes];
98         [o_authors_btn setAttributedTitle:attrStr];
99         [attrStr release];
100         [o_trademarks_txt setStringValue:_NS("VLC media player and VideoLAN are trademarks of the VideoLAN Association.")];
101
102         /* setup the creator / revision field */
103         NSString *compiler;
104 #ifdef __clang__
105         compiler = [NSString stringWithFormat:@"clang %s", __clang_version__];
106 #else
107         compiler = [NSString stringWithFormat:@"llvm-gcc %s", __VERSION__];
108 #endif
109         [o_revision_field setStringValue: [NSString stringWithFormat: _NS("Compiled by %s with %@"), VLC_CompileBy(), compiler]];
110
111         /* Setup the nameversion field */
112         [o_name_version_field setStringValue: [NSString stringWithFormat:@"Version %s (%s)", VERSION_MESSAGE, PLATFORM]];
113
114         NSMutableArray *tmpArray = [NSMutableArray arrayWithArray: [[NSString stringWithUTF8String:psz_authors] componentsSeparatedByString:@"\n\n"]];
115         NSUInteger count = [tmpArray count];
116         for (NSUInteger i = 0; i < count; i++) {
117             [tmpArray replaceObjectAtIndex:i withObject:[[tmpArray objectAtIndex:i]stringByReplacingOccurrencesOfString:@"\n" withString:@", "]];
118             [tmpArray replaceObjectAtIndex:i withObject:[[tmpArray objectAtIndex:i]stringByReplacingOccurrencesOfString:@", -" withString:@"\n-" options:0 range:NSRangeFromString(@"0 30")]];
119             [tmpArray replaceObjectAtIndex:i withObject:[[tmpArray objectAtIndex:i]stringByReplacingOccurrencesOfString:@"-, " withString:@"-\n" options:0 range:NSRangeFromString(@"0 30")]];
120             [tmpArray replaceObjectAtIndex:i withObject:[[tmpArray objectAtIndex:i]stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]]];
121         }
122         o_authors = [tmpArray componentsJoinedByString:@"\n\n"];
123         [o_authors retain];
124
125         /* setup join us! */
126         NSString *joinus = [NSString stringWithUTF8String:_(""
127                                                             "<p>VLC media player is a free and open source media player, encoder, and "
128                                                             "streamer made by the volunteers of the <a href=\"http://www.videolan.org/"
129                                                             "\"><span style=\" text-decoration: underline; color:#0057ae;\">VideoLAN</"
130                                                             "span></a> community.</p><p>VLC uses its internal codecs, works on "
131                                                             "essentially every popular platform, and can read almost all files, CDs, "
132                                                             "DVDs, network streams, capture cards and other media formats!</p><p><a href="
133                                                             "\"http://www.videolan.org/contribute/\"><span style=\" text-decoration: "
134                                                             "underline; color:#0057ae;\">Help and join us!</span></a>")];
135         NSString *fontfamily;
136         if (OSX_YOSEMITE)
137             fontfamily = @"Helvetica Neue";
138         else
139             fontfamily = @"Lucida Grande";
140         NSString *joinUsWithStyle = [NSString stringWithFormat:@"<div style=\"text-align:left;font-family:%@;\">%@</div>",
141                                      fontfamily, joinus];
142         NSAttributedString *joinus_readytorender = [[NSAttributedString alloc] initWithHTML:[joinUsWithStyle dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES] options:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:NSUTF8StringEncoding] forKey:NSCharacterEncodingDocumentOption] documentAttributes:NULL];
143         [o_joinus_txt setAllowsEditingTextAttributes: YES];
144         [o_joinus_txt setSelectable: YES];
145         [o_joinus_txt setAttributedStringValue:joinus_readytorender];
146
147         [joinus_readytorender release];
148         [o_credits_textview setString: @""];
149
150         /* Setup the window */
151         [o_credits_textview setDrawsBackground: NO];
152         [o_credits_scrollview setDrawsBackground: NO];
153         [o_about_window setExcludedFromWindowsMenu:YES];
154         [o_about_window setMenu:nil];
155         [o_about_window center];
156         [o_about_window setBackgroundColor: [NSColor colorWithCalibratedWhite:.96 alpha:1.]];
157
158         if (config_GetInt(VLCIntf, "macosx-icon-change")) {
159             /* After day 354 of the year, the usual VLC cone is replaced by another cone
160              * wearing a Father Xmas hat.
161              * Note: this icon doesn't represent an endorsement of The Coca-Cola Company.
162              */
163             NSCalendar *gregorian =
164             [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
165             NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:[NSDate date]];
166             [gregorian release];
167
168             if (dayOfYear >= 354)
169                 [o_icon_view setImage: [NSImage imageNamed:@"vlc-xmas"]];
170         }
171
172         b_isSetUp = YES;
173     }
174
175     /* Show the window */
176     b_restart = YES;
177     [o_credits_scrollview setHidden:YES];
178     [o_credits_textview setHidden:YES];
179     [o_joinus_txt setHidden:NO];
180     [o_copyright_field setHidden:NO];
181     [o_revision_field setHidden:NO];
182     [o_name_version_field setHidden:NO];
183     [o_credits_textview scrollPoint:NSMakePoint(0, 0)];
184     [o_about_window makeKeyAndOrderFront: nil];
185 }
186
187 - (void)windowDidBecomeKey:(NSNotification *)notification
188 {
189     o_scroll_timer = [NSTimer scheduledTimerWithTimeInterval: 1/6
190                                                       target:self
191                                                     selector:@selector(scrollCredits:)
192                                                     userInfo:nil
193                                                      repeats:YES];
194 }
195
196 - (void)windowDidResignKey:(NSNotification *)notification
197 {
198     [o_scroll_timer invalidate];
199 }
200
201 - (void)scrollCredits:(NSTimer *)timer
202 {
203     if (b_restart) {
204         /* Reset the starttime */
205         i_start = [NSDate timeIntervalSinceReferenceDate] + 4.0;
206         f_current = 0;
207         f_end = [o_credits_textview bounds].size.height - [o_credits_scrollview bounds].size.height;
208         b_restart = NO;
209     }
210
211     if ([NSDate timeIntervalSinceReferenceDate] >= i_start) {
212         /* Increment the scroll position */
213         f_current += 0.005;
214
215         /* Scroll to the position */
216         [o_credits_textview scrollPoint:NSMakePoint(0, f_current)];
217
218         /* If at end, restart at the top */
219         if (f_current >= f_end) {
220             /* f_end may be wrong on first run, so don't trust it too much */
221             if (f_end == [o_credits_textview bounds].size.height - [o_credits_scrollview bounds].size.height) {
222                 sleep(2);
223                 b_restart = YES;
224                 [o_credits_textview scrollPoint:NSMakePoint(0, 0)];
225             } else
226                 f_end = [o_credits_textview bounds].size.height - [o_credits_scrollview bounds].size.height;
227         }
228     }
229 }
230
231 - (IBAction)buttonAction:(id)sender
232 {
233     [o_credits_scrollview setHidden:NO];
234     [o_credits_textview setHidden:NO];
235     [o_joinus_txt setHidden:YES];
236     [o_copyright_field setHidden:YES];
237     [o_revision_field setHidden:YES];
238     [o_name_version_field setHidden:YES];
239
240     if (sender == o_authors_btn)
241         [o_credits_textview setString:o_authors];
242     else if (sender == o_credits_btn)
243         [o_credits_textview setString:[[NSString stringWithUTF8String:psz_thanks] stringByReplacingOccurrencesOfString:@"\n" withString:@" " options:0 range:NSRangeFromString(@"680 2")]];
244     else
245         [o_credits_textview setString:[NSString stringWithUTF8String:psz_license]];
246
247     [o_credits_textview scrollPoint:NSMakePoint(0, 0)];
248     b_restart = YES;
249 }
250
251 /*****************************************************************************
252 * VLC GPL Window, action called from the about window and the help menu
253 *****************************************************************************/
254
255 - (void)showGPL
256 {
257     [self showAbout];
258     [self buttonAction:nil];
259 }
260
261 /*****************************************************************************
262 * VLC Generic Help Window
263 *****************************************************************************/
264
265 - (void)showHelp
266 {
267     [o_help_window setTitle: _NS("VLC media player Help")];
268     [o_help_fwd_btn setToolTip: _NS("Next")];
269     [o_help_bwd_btn setToolTip: _NS("Previous")];
270     [o_help_home_btn setToolTip: _NS("Index")];
271
272     [o_help_window makeKeyAndOrderFront: self];
273
274     [[o_help_web_view mainFrame] loadHTMLString: _NS(I_LONGHELP)
275                                         baseURL: [NSURL URLWithString:@"http://videolan.org"]];
276 }
277
278 - (IBAction)helpGoHome:(id)sender
279 {
280     [[o_help_web_view mainFrame] loadHTMLString: _NS(I_LONGHELP)
281                                         baseURL: [NSURL URLWithString:@"http://videolan.org"]];
282 }
283
284 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
285 {
286     /* delegate to update button states (we're the frameLoadDelegate for our help's webview)« */
287     [o_help_fwd_btn setEnabled: [o_help_web_view canGoForward]];
288     [o_help_bwd_btn setEnabled: [o_help_web_view canGoBack]];
289 }
290
291 @end