macosx: Rewrote the About window auto-scroll behavior
[vlc.git] / 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  * AboutWindowController implementation
40  *****************************************************************************/
41 @interface AboutWindowController ()
42 {
43     NSString *o_authors;
44 }
45 @end
46
47 @implementation AboutWindowController
48
49 - (id)init
50 {
51     self = [super initWithWindowNibName:@"About"];
52     if (self) {
53
54         [self setWindowFrameAutosaveName:@"about"];
55     }
56
57     return self;
58 }
59
60 - (void) dealloc
61 {
62     [[NSNotificationCenter defaultCenter] removeObserver: self];
63 }
64
65 /*****************************************************************************
66 * VLC About Window
67 *****************************************************************************/
68
69 - (void)showAbout
70 {
71     [self window];
72
73     /* Show the window */
74     [o_credits_scrollview setHidden:YES];
75     [o_credits_textview setHidden:YES];
76     [o_joinus_txt setHidden:NO];
77     [o_copyright_field setHidden:NO];
78     [o_revision_field setHidden:NO];
79     [o_name_version_field setHidden:NO];
80     [o_credits_textview scrollPoint:NSMakePoint(0, 0)];
81
82     [self showWindow:nil];
83 }
84
85 - (void)windowDidLoad
86 {
87     [[self window] setCollectionBehavior: NSWindowCollectionBehaviorFullScreenAuxiliary];
88
89     /* Get the localized info dictionary (InfoPlist.strings) */
90     NSDictionary *o_local_dict;
91     o_local_dict = [[NSBundle mainBundle] localizedInfoDictionary];
92
93     /* Setup the copyright field */
94     [o_copyright_field setStringValue: [o_local_dict objectForKey:@"NSHumanReadableCopyright"]];
95
96     /* l10n */
97     [[self window] setTitle: _NS("About VLC media player")];
98     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];
99     NSAttributedString *attrStr;
100     attrStr = [[NSAttributedString alloc] initWithString:_NS("Credits") attributes:stringAttributes];
101     [o_credits_btn setAttributedTitle:attrStr];
102     attrStr = [[NSAttributedString alloc] initWithString:_NS("License") attributes:stringAttributes];
103     [o_gpl_btn setAttributedTitle:attrStr];
104     attrStr = [[NSAttributedString alloc] initWithString:_NS("Authors") attributes:stringAttributes];
105     [o_authors_btn setAttributedTitle:attrStr];
106     attrStr = nil;
107     [o_trademarks_txt setStringValue:_NS("VLC media player and VideoLAN are trademarks of the VideoLAN Association.")];
108
109     /* setup the creator / revision field */
110     NSString *compiler;
111 #ifdef __clang__
112     compiler = [NSString stringWithFormat:@"clang %s", __clang_version__];
113 #else
114     compiler = [NSString stringWithFormat:@"llvm-gcc %s", __VERSION__];
115 #endif
116     [o_revision_field setStringValue: [NSString stringWithFormat: _NS("Compiled by %s with %@"), VLC_CompileBy(), compiler]];
117
118     /* Setup the nameversion field */
119     [o_name_version_field setStringValue: [NSString stringWithFormat:@"Version %s (%s)", VERSION_MESSAGE, PLATFORM]];
120
121     NSMutableArray *tmpArray = [NSMutableArray arrayWithArray: [toNSStr(psz_authors) componentsSeparatedByString:@"\n\n"]];
122     NSUInteger count = [tmpArray count];
123     for (NSUInteger i = 0; i < count; i++) {
124         [tmpArray replaceObjectAtIndex:i withObject:[[tmpArray objectAtIndex:i]stringByReplacingOccurrencesOfString:@"\n" withString:@", "]];
125         [tmpArray replaceObjectAtIndex:i withObject:[[tmpArray objectAtIndex:i]stringByReplacingOccurrencesOfString:@", -" withString:@"\n-" options:0 range:NSRangeFromString(@"0 30")]];
126         [tmpArray replaceObjectAtIndex:i withObject:[[tmpArray objectAtIndex:i]stringByReplacingOccurrencesOfString:@"-, " withString:@"-\n" options:0 range:NSRangeFromString(@"0 30")]];
127         [tmpArray replaceObjectAtIndex:i withObject:[[tmpArray objectAtIndex:i]stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]]];
128     }
129     o_authors = [tmpArray componentsJoinedByString:@"\n\n"];
130
131     /* setup join us! */
132     NSString *joinus = toNSStr(_(""
133                                  "<p>VLC media player is a free and open source media player, encoder, and "
134                                  "streamer made by the volunteers of the <a href=\"http://www.videolan.org/"
135                                  "\"><span style=\" text-decoration: underline; color:#0057ae;\">VideoLAN</"
136                                  "span></a> community.</p><p>VLC uses its internal codecs, works on "
137                                  "essentially every popular platform, and can read almost all files, CDs, "
138                                  "DVDs, network streams, capture cards and other media formats!</p><p><a href="
139                                  "\"http://www.videolan.org/contribute/\"><span style=\" text-decoration: "
140                                  "underline; color:#0057ae;\">Help and join us!</span></a>"));
141     NSString *fontfamily;
142     if (OSX_YOSEMITE)
143         fontfamily = @"Helvetica Neue";
144     else
145         fontfamily = @"Lucida Grande";
146     NSString *joinUsWithStyle = [NSString stringWithFormat:@"<div style=\"text-align:left;font-family:%@;\">%@</div>",
147                                  fontfamily, joinus];
148     NSAttributedString *joinus_readytorender = [[NSAttributedString alloc] initWithHTML:[joinUsWithStyle dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES] options:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:NSUTF8StringEncoding] forKey:NSCharacterEncodingDocumentOption] documentAttributes:NULL];
149     [o_joinus_txt setAllowsEditingTextAttributes: YES];
150     [o_joinus_txt setSelectable: YES];
151     [o_joinus_txt setAttributedStringValue:joinus_readytorender];
152
153     [o_credits_textview setString: @""];
154
155     /* Setup the window */
156     [o_credits_textview setDrawsBackground: NO];
157     [o_credits_scrollview setDrawsBackground: NO];
158     [[self window] setExcludedFromWindowsMenu:YES];
159     [[self window] setMenu:nil];
160     [[self window] setBackgroundColor: [NSColor colorWithCalibratedWhite:.96 alpha:1.]];
161
162     if (config_GetInt(VLCIntf, "macosx-icon-change")) {
163         /* After day 354 of the year, the usual VLC cone is replaced by another cone
164          * wearing a Father Xmas hat.
165          * Note: this icon doesn't represent an endorsement of The Coca-Cola Company.
166          */
167         NSCalendar *gregorian =
168         [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
169         NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:[NSDate date]];
170
171         if (dayOfYear >= 354)
172             [o_icon_view setImage: [NSImage imageNamed:@"vlc-xmas"]];
173     }
174 }
175
176 - (void)windowDidBecomeKey:(NSNotification *)notification
177 {
178     [(VLCScrollingClipView *)[o_credits_scrollview contentView] startScrolling];
179 }
180
181 - (void)windowDidResignKey:(NSNotification *)notification
182 {
183     [(VLCScrollingClipView *)[o_credits_scrollview contentView] stopScrolling];
184 }
185
186 - (IBAction)buttonAction:(id)sender
187 {
188     [o_credits_scrollview setHidden:NO];
189     [o_credits_textview setHidden:NO];
190     [o_joinus_txt setHidden:YES];
191     [o_copyright_field setHidden:YES];
192     [o_revision_field setHidden:YES];
193     [o_name_version_field setHidden:YES];
194
195     if (sender == o_authors_btn)
196         [o_credits_textview setString:o_authors];
197     else if (sender == o_credits_btn)
198         [o_credits_textview setString:[toNSStr(psz_thanks) stringByReplacingOccurrencesOfString:@"\n" withString:@" " options:0 range:NSRangeFromString(@"680 2")]];
199     else
200         [o_credits_textview setString:toNSStr(psz_license)];
201
202     [(VLCScrollingClipView *)[o_credits_scrollview contentView] resetScrolling];
203     [(VLCScrollingClipView *)[o_credits_scrollview contentView] startScrolling];
204 }
205
206 /*****************************************************************************
207 * VLC GPL Window, action called from the about window and the help menu
208 *****************************************************************************/
209
210 - (void)showGPL
211 {
212     [self showAbout];
213     [self buttonAction:nil];
214 }
215
216 @end
217
218 /*****************************************************************************
219  * VLC Help Window Controller
220  *****************************************************************************/
221
222 @implementation HelpWindowController
223
224 - (id)init
225 {
226     self = [super initWithWindowNibName:@"Help"];
227     if (self) {
228
229         [self setWindowFrameAutosaveName:@"help"];
230     }
231
232     return self;
233 }
234
235 - (void)windowDidLoad
236 {
237     [[self window] setTitle: _NS("VLC media player Help")];
238     [o_help_fwd_btn setToolTip: _NS("Next")];
239     [o_help_bwd_btn setToolTip: _NS("Previous")];
240     [o_help_home_btn setToolTip: _NS("Index")];
241 }
242
243 - (void)showHelp
244 {
245     [self showWindow:nil];
246     [self helpGoHome:nil];
247 }
248
249 - (IBAction)helpGoHome:(id)sender
250 {
251     [[o_help_web_view mainFrame] loadHTMLString: _NS(I_LONGHELP)
252                                         baseURL: [NSURL URLWithString:@"http://videolan.org"]];
253 }
254
255 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
256 {
257     /* delegate to update button states (we're the frameLoadDelegate for our help's webview)« */
258     [o_help_fwd_btn setEnabled: [o_help_web_view canGoForward]];
259     [o_help_bwd_btn setEnabled: [o_help_web_view canGoBack]];
260 }
261
262 @end