~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Ide.WelcomePage/WelcomePageListButton.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// WelcomePageListButton.cs
 
3
//
 
4
// Author:
 
5
//       lluis <${AuthorEmail}>
 
6
//
 
7
// Copyright (c) 2012 lluis
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
//
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
//
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
using System;
 
27
using MonoDevelop.Core;
 
28
using MonoDevelop.Components;
 
29
using Gtk;
 
30
 
 
31
namespace MonoDevelop.Ide.WelcomePage
 
32
{
 
33
        public class WelcomePageListButton: EventBox
 
34
        {
 
35
                private static Gdk.Cursor hand_cursor = new Gdk.Cursor(Gdk.CursorType.Hand1);
 
36
                string title, subtitle, actionUrl;
 
37
                Gdk.Pixbuf icon;
 
38
                bool mouseOver;
 
39
                bool pinned;
 
40
                Gdk.Rectangle starRect;
 
41
                bool mouseOverStar;
 
42
 
 
43
                static Gdk.Pixbuf starNormal;
 
44
                static Gdk.Pixbuf starNormalHover;
 
45
                static Gdk.Pixbuf starPinned;
 
46
                static Gdk.Pixbuf starPinnedHover;
 
47
 
 
48
                public event EventHandler PinClicked;
 
49
 
 
50
                public bool DrawLeftBorder { get; set; }
 
51
                public bool DrawRightBorder { get; set; }
 
52
 
 
53
                public int BorderPadding { get; set; }
 
54
 
 
55
                public int LeftTextPadding { get; set; }
 
56
                public int InternalPadding { get; set; }
 
57
 
 
58
                static WelcomePageListButton ()
 
59
                {
 
60
                        starNormal = Gdk.Pixbuf.LoadFromResource ("star-normal.png");
 
61
                        starNormalHover = Gdk.Pixbuf.LoadFromResource ("star-normal-hover.png");
 
62
                        starPinned = Gdk.Pixbuf.LoadFromResource ("star-pinned.png");
 
63
                        starPinnedHover = Gdk.Pixbuf.LoadFromResource ("star-pinned-hover.png");
 
64
                }
 
65
 
 
66
                public WelcomePageListButton (string title, string subtitle, Gdk.Pixbuf icon, string actionUrl)
 
67
                {
 
68
                        VisibleWindow = false;
 
69
                        this.title = title;
 
70
                        this.subtitle = subtitle;
 
71
                        this.icon = icon;
 
72
                        this.actionUrl = actionUrl;
 
73
                        WidthRequest = Styles.WelcomeScreen.Pad.Solutions.SolutionTile.Width;
 
74
                        HeightRequest = Styles.WelcomeScreen.Pad.Solutions.SolutionTile.Height + 2;
 
75
                        Events |= (Gdk.EventMask.EnterNotifyMask | Gdk.EventMask.LeaveNotifyMask | Gdk.EventMask.ButtonReleaseMask | Gdk.EventMask.PointerMotionMask);
 
76
 
 
77
                        LeftTextPadding = Styles.WelcomeScreen.Pad.Solutions.SolutionTile.TextLeftPadding;
 
78
                        InternalPadding = Styles.WelcomeScreen.Pad.Padding;
 
79
                }
 
80
 
 
81
                public bool AllowPinning { get; set; }
 
82
 
 
83
                public bool Pinned {
 
84
                        get { return pinned; }
 
85
                        set {
 
86
                                pinned = value;
 
87
                                QueueDraw ();
 
88
                        }
 
89
                }
 
90
 
 
91
                protected override bool OnEnterNotifyEvent (Gdk.EventCrossing evnt)
 
92
                {
 
93
                        GdkWindow.Cursor = hand_cursor;
 
94
                        mouseOver = true;
 
95
                        QueueDraw ();
 
96
                        return base.OnEnterNotifyEvent (evnt);
 
97
                }
 
98
                
 
99
                protected override bool OnLeaveNotifyEvent (Gdk.EventCrossing evnt)
 
100
                {
 
101
                        GdkWindow.Cursor = null;
 
102
                        mouseOver = false;
 
103
                        QueueDraw ();
 
104
                        return base.OnLeaveNotifyEvent (evnt);
 
105
                }
 
106
 
 
107
                protected override bool OnButtonReleaseEvent (Gdk.EventButton evnt)
 
108
                {
 
109
                        if (evnt.Button == 1) {
 
110
                                if (mouseOverStar && AllowPinning) {
 
111
                                        pinned = !pinned;
 
112
                                        QueueDraw ();
 
113
                                        if (PinClicked != null)
 
114
                                                PinClicked (this, EventArgs.Empty);
 
115
                                        return true;
 
116
                                } else if (mouseOver) {
 
117
                                        WelcomePageSection.DispatchLink (actionUrl);
 
118
                                        return true;
 
119
                                }
 
120
                        }
 
121
                        return base.OnButtonReleaseEvent (evnt);
 
122
                }
 
123
 
 
124
                protected override bool OnMotionNotifyEvent (Gdk.EventMotion evnt)
 
125
                {
 
126
                        var so = starRect.Contains (Allocation.X + (int)evnt.X, Allocation.Y + (int)evnt.Y);
 
127
                        if (so != mouseOverStar) {
 
128
                                mouseOverStar = so;
 
129
                                QueueDraw ();
 
130
                        }
 
131
                        return base.OnMotionNotifyEvent (evnt);
 
132
                }
 
133
 
 
134
                protected override bool OnExposeEvent (Gdk.EventExpose evnt)
 
135
                {
 
136
                        using (var ctx = Gdk.CairoHelper.Create (evnt.Window)) {
 
137
                                if (mouseOver) {
 
138
                                        if (BorderPadding <= 0) {
 
139
                                                ctx.Rectangle (Allocation.X, Allocation.Y, Allocation.Width, Allocation.Height);
 
140
                                                ctx.Color = CairoExtensions.ParseColor (Styles.WelcomeScreen.Pad.Solutions.SolutionTile.HoverBackgroundColor);
 
141
                                                ctx.Fill ();
 
142
                                                ctx.MoveTo (Allocation.X, Allocation.Y + 0.5);
 
143
                                                ctx.RelLineTo (Allocation.Width, 0);
 
144
                                                ctx.MoveTo (Allocation.X, Allocation.Y + Allocation.Height - 0.5);
 
145
                                                ctx.RelLineTo (Allocation.Width, 0);
 
146
                                                
 
147
                                                if (DrawRightBorder) {
 
148
                                                        ctx.MoveTo (Allocation.Right + 0.5, Allocation.Y + 0.5);
 
149
                                                        ctx.LineTo (Allocation.Right + 0.5, Allocation.Bottom - 0.5);
 
150
                                                }
 
151
                                                if (DrawLeftBorder) {
 
152
                                                        ctx.MoveTo (Allocation.Left + 0.5, Allocation.Y + 0.5);
 
153
                                                        ctx.LineTo (Allocation.Left + 0.5, Allocation.Bottom - 0.5);
 
154
                                                }
 
155
                                                
 
156
                                                ctx.LineWidth = 1;
 
157
                                                ctx.Color = CairoExtensions.ParseColor (Styles.WelcomeScreen.Pad.Solutions.SolutionTile.HoverBorderColor);
 
158
                                                ctx.Stroke ();
 
159
                                        } else {
 
160
                                                Gdk.Rectangle region = Allocation;
 
161
                                                region.Inflate (-BorderPadding, -BorderPadding);
 
162
 
 
163
                                                ctx.RoundedRectangle (region.X + 0.5, region.Y + 0.5, region.Width - 1, region.Height - 1, 3);
 
164
                                                ctx.Color = CairoExtensions.ParseColor (Styles.WelcomeScreen.Pad.Solutions.SolutionTile.HoverBackgroundColor);
 
165
                                                ctx.FillPreserve ();
 
166
                                                
 
167
                                                ctx.LineWidth = 1;
 
168
                                                ctx.Color = CairoExtensions.ParseColor (Styles.WelcomeScreen.Pad.Solutions.SolutionTile.HoverBorderColor);
 
169
                                                ctx.Stroke ();
 
170
                                        }
 
171
                                }
 
172
 
 
173
                                // Draw the icon
 
174
 
 
175
                                int x = Allocation.X + InternalPadding;
 
176
                                int y = Allocation.Y + (Allocation.Height - icon.Height) / 2;
 
177
                                Gdk.CairoHelper.SetSourcePixbuf (ctx, icon, x, y);
 
178
                                ctx.Paint ();
 
179
 
 
180
                                if (AllowPinning && (mouseOver || pinned)) {
 
181
                                        Gdk.Pixbuf star;
 
182
                                        if (pinned) {
 
183
                                                if (mouseOverStar)
 
184
                                                        star = starPinnedHover;
 
185
                                                else
 
186
                                                        star = starPinned;
 
187
                                        } else {
 
188
                                                if (mouseOverStar)
 
189
                                                        star = starNormalHover;
 
190
                                                else
 
191
                                                        star = starNormal;
 
192
                                        }
 
193
                                        x = x + icon.Width - star.Width + 3;
 
194
                                        y = y + icon.Height - star.Height + 3;
 
195
                                        Gdk.CairoHelper.SetSourcePixbuf (ctx, star, x, y);
 
196
                                        ctx.Paint ();
 
197
                                        starRect = new Gdk.Rectangle (x, y, star.Width, star.Height);
 
198
                                }
 
199
 
 
200
                                // Draw the text
 
201
 
 
202
                                int textWidth = Allocation.Width - LeftTextPadding - InternalPadding * 2;
 
203
 
 
204
                                var face = Platform.IsMac ? Styles.WelcomeScreen.Pad.TitleFontFamilyMac : Styles.WelcomeScreen.Pad.TitleFontFamilyWindows;
 
205
                                Pango.Layout titleLayout = new Pango.Layout (PangoContext);
 
206
                                titleLayout.Width = Pango.Units.FromPixels (textWidth);
 
207
                                titleLayout.Ellipsize = Pango.EllipsizeMode.End;
 
208
                                titleLayout.SetMarkup (WelcomePageSection.FormatText (face, Styles.WelcomeScreen.Pad.Solutions.SolutionTile.TitleFontSize, false, Styles.WelcomeScreen.Pad.MediumTitleColor, title));
 
209
 
 
210
                                Pango.Layout subtitleLayout = null;
 
211
 
 
212
                                if (!string.IsNullOrEmpty (subtitle)) {
 
213
                                        subtitleLayout = new Pango.Layout (PangoContext);
 
214
                                        subtitleLayout.Width = Pango.Units.FromPixels (textWidth);
 
215
                                        subtitleLayout.Ellipsize = Pango.EllipsizeMode.Start;
 
216
                                        subtitleLayout.SetMarkup (WelcomePageSection.FormatText (face, Styles.WelcomeScreen.Pad.Solutions.SolutionTile.PathFontSize, false, Styles.WelcomeScreen.Pad.SmallTitleColor, subtitle));
 
217
                                }
 
218
 
 
219
                                int height = 0;
 
220
                                int w, h1, h2;
 
221
                                titleLayout.GetPixelSize (out w, out h1);
 
222
                                height += h1;
 
223
 
 
224
                                if (subtitleLayout != null) {
 
225
                                        height += Styles.WelcomeScreen.Pad.Solutions.SolutionTile.TitleBottomMargin;
 
226
                                        subtitleLayout.GetPixelSize (out w, out h2);
 
227
                                        height += h2;
 
228
                                }
 
229
 
 
230
                                int tx = Allocation.X + InternalPadding + LeftTextPadding;
 
231
                                int ty = Allocation.Y + (Allocation.Height - height) / 2;
 
232
                                ctx.MoveTo (tx, ty);
 
233
                                Pango.CairoHelper.ShowLayout (ctx, titleLayout);
 
234
 
 
235
                                if (subtitleLayout != null) {
 
236
                                        ty += h1 + Styles.WelcomeScreen.Pad.Solutions.SolutionTile.TitleBottomMargin;
 
237
                                        ctx.MoveTo (tx, ty);
 
238
                                        Pango.CairoHelper.ShowLayout (ctx, subtitleLayout);
 
239
                                }
 
240
                        }
 
241
                        return true;
 
242
                }
 
243
        }
 
244
}
 
245