PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/SparkleShare/Mac/SparkleAbout.cs

http://github.com/hbons/SparkleShare
C# | 219 lines | 147 code | 57 blank | 15 comment | 4 complexity | d92dcb2181e69591215d661157b09b7c MD5 | raw file
  1//   SparkleShare, a collaboration and sharing tool.
  2//   Copyright (C) 2010  Hylke Bons <hylkebons@gmail.com>
  3//
  4//   This program is free software: you can redistribute it and/or modify
  5//   it under the terms of the GNU General Public License as published by
  6//   the Free Software Foundation, either version 3 of the License, or
  7//   (at your option) any later version.
  8//
  9//   This program is distributed in the hope that it will be useful,
 10//   but WITHOUT ANY WARRANTY; without even the implied warranty of
 11//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 12//   GNU General Public License for more details.
 13//
 14//   You should have received a copy of the GNU General Public License
 15//   along with this program. If not, see <http://www.gnu.org/licenses/>.
 16
 17
 18using System;
 19using System.Drawing;
 20using System.IO;
 21
 22using MonoMac.AppKit;
 23using MonoMac.Foundation;
 24
 25namespace SparkleShare {
 26
 27    public class SparkleAbout : NSWindow {
 28
 29        public SparkleAboutController Controller = new SparkleAboutController ();
 30
 31        private NSTextField version_text_field, updates_text_field, credits_text_field;
 32        private SparkleLink website_link, credits_link, report_problem_link, debug_log_link;
 33        private NSImage about_image;
 34        private NSImageView about_image_view;
 35        private NSButton hidden_close_button;
 36
 37
 38        public SparkleAbout (IntPtr handle) : base (handle) { }
 39
 40        public SparkleAbout () : base ()
 41        {
 42            SetFrame (new RectangleF (0, 0, 640, 281), true);
 43            Center ();
 44
 45            Delegate    = new SparkleAboutDelegate ();
 46            StyleMask   = (NSWindowStyle.Closable | NSWindowStyle.Titled);
 47            Title       = "About SparkleShare";
 48            MaxSize     = new SizeF (640, 281);
 49            MinSize     = new SizeF (640, 281);
 50            HasShadow   = true;
 51            BackingType = NSBackingStore.Buffered;
 52            Level       = NSWindowLevel.Floating;
 53
 54            this.hidden_close_button = new NSButton () {
 55                Frame                     = new RectangleF (0, 0, 0, 0),
 56                KeyEquivalentModifierMask = NSEventModifierMask.CommandKeyMask,
 57                KeyEquivalent             = "w"
 58            };
 59
 60            CreateAbout ();
 61
 62
 63            this.hidden_close_button.Activated += delegate { Controller.WindowClosed (); };
 64
 65            Controller.HideWindowEvent += delegate {
 66                Program.Controller.Invoke (() => PerformClose (this));
 67            };
 68
 69            Controller.ShowWindowEvent += delegate {
 70                Program.Controller.Invoke (() => OrderFrontRegardless ());
 71            };
 72
 73            Controller.UpdateLabelEvent += delegate (string text) {
 74                Program.Controller.Invoke (() => { this.updates_text_field.StringValue = text; });
 75            };
 76
 77
 78            ContentView.AddSubview (this.hidden_close_button);
 79        }
 80
 81
 82        private void CreateAbout ()
 83        {
 84            this.about_image = NSImage.ImageNamed ("about");
 85            this.about_image.Size = new SizeF (720, 260);
 86
 87            this.about_image_view = new NSImageView () {
 88                Image = this.about_image,
 89                Frame = new RectangleF (0, 0, 720, 260)
 90            };
 91
 92            this.version_text_field = new SparkleLabel ("version " + Controller.RunningVersion, NSTextAlignment.Left) {
 93                DrawsBackground = false,
 94                Frame           = new RectangleF (295, 140, 318, 22),
 95                TextColor       = NSColor.White
 96            };
 97
 98            this.updates_text_field = new SparkleLabel ("Checking for updates...", NSTextAlignment.Left) {
 99                DrawsBackground = false,
100                Frame           = new RectangleF (295, Frame.Height - 232, 318, 98),
101                TextColor       = NSColor.FromCalibratedRgba (1.0f, 1.0f, 1.0f, 0.5f)
102            };
103
104            this.credits_text_field = new SparkleLabel (
105                @"Copyright © 2010–" + DateTime.Now.Year + " Hylke Bons and others." +
106                "\n\n" + 
107                "SparkleShare is Open Source. You are free to use, modify, and redistribute it " +
108                "under the GNU GPLv3.", NSTextAlignment.Left) {
109                
110                DrawsBackground = false,
111                Frame           = new RectangleF (295, Frame.Height - 260, 318, 98),
112                TextColor       = NSColor.White
113            };
114
115            this.website_link       = new SparkleLink ("Website", Controller.WebsiteLinkAddress);
116            this.website_link.Frame = new RectangleF (new PointF (295, 25), this.website_link.Frame.Size);
117            
118            this.credits_link       = new SparkleLink ("Credits", Controller.CreditsLinkAddress);
119            this.credits_link.Frame = new RectangleF (
120                new PointF (this.website_link.Frame.X + this.website_link.Frame.Width + 10, 25),
121                this.credits_link.Frame.Size);
122            
123            this.report_problem_link       = new SparkleLink ("Report a problem", Controller.ReportProblemLinkAddress);
124            this.report_problem_link.Frame = new RectangleF (
125                new PointF (this.credits_link.Frame.X + this.credits_link.Frame.Width + 10, 25),
126                this.report_problem_link.Frame.Size);
127            
128            this.debug_log_link       = new SparkleLink ("Debug log", Controller.DebugLogLinkAddress);
129            this.debug_log_link.Frame = new RectangleF (
130                new PointF (this.report_problem_link.Frame.X + this.report_problem_link.Frame.Width + 10, 25),
131                this.debug_log_link.Frame.Size);
132
133            ContentView.AddSubview (this.about_image_view);
134            ContentView.AddSubview (this.version_text_field);
135            ContentView.AddSubview (this.updates_text_field);
136            ContentView.AddSubview (this.credits_text_field);
137            ContentView.AddSubview (this.website_link);
138            ContentView.AddSubview (this.credits_link);
139            ContentView.AddSubview (this.report_problem_link);
140            ContentView.AddSubview (this.debug_log_link);
141        }
142
143
144        public override void OrderFrontRegardless ()
145        {
146            NSApplication.SharedApplication.ActivateIgnoringOtherApps (true);
147            MakeKeyAndOrderFront (this);
148
149            if (Program.UI != null)
150                Program.UI.UpdateDockIconVisibility ();
151
152            base.OrderFrontRegardless ();
153        }
154
155
156        public override void PerformClose (NSObject sender)
157        {
158            base.OrderOut (this);
159
160            if (Program.UI != null)
161                Program.UI.UpdateDockIconVisibility ();
162
163            return;
164        }
165
166
167        private class SparkleAboutDelegate : NSWindowDelegate {
168            
169            public override bool WindowShouldClose (NSObject sender)
170            {
171                (sender as SparkleAbout).Controller.WindowClosed ();
172                return false;
173            }
174        }
175        
176        
177        private class SparkleLink : NSTextField {
178            
179            private NSUrl url;
180            
181            
182            public SparkleLink (string text, string address) : base ()
183            {
184                this.url = new NSUrl (address);
185                
186                AllowsEditingTextAttributes = true;
187                BackgroundColor = NSColor.White;
188                Bordered        = false;
189                DrawsBackground = false;
190                Editable        = false;
191                Selectable      = false;
192                
193                NSData name_data = NSData.FromString ("<a href='" + this.url +
194                    "' style='font-size: 9pt; font-family: \"Helvetica Neue\"; color: #739ECF'>" + text + "</a></font>");
195                
196                NSDictionary name_dictionary       = new NSDictionary();
197                NSAttributedString name_attributes = new NSAttributedString (name_data, new NSUrl ("file://"), out name_dictionary);
198                
199                NSMutableAttributedString s = new NSMutableAttributedString ();
200                s.Append (name_attributes);
201                
202                Cell.AttributedStringValue = s;
203                SizeToFit ();
204            }
205            
206            
207            public override void MouseUp (NSEvent e)
208            {
209                Program.Controller.OpenWebsite (this.url.ToString ());
210            }
211            
212            
213            public override void ResetCursorRects ()
214            {
215                AddCursorRect (Bounds, NSCursor.PointingHandCursor);
216            }
217        }
218    }
219}