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

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/RoundButton.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
// RoundButton.cs
 
3
//  
 
4
// Author:
 
5
//       Mike KrĆ¼ger <mkrueger@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
 
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 Gtk;
 
28
using Gdk;
 
29
using MonoDevelop.Components;
 
30
using Cairo;
 
31
using MonoDevelop.Ide;
 
32
using System.Reflection;
 
33
 
 
34
 
 
35
namespace MonoDevelop.Components.MainToolbar
 
36
{
 
37
        class LazyImage
 
38
        {
 
39
                string resourceName;
 
40
 
 
41
                ImageSurface img;
 
42
                public ImageSurface Img {
 
43
                        get {
 
44
                                if (img == null)
 
45
                                        img = CairoExtensions.LoadImage (Assembly.GetCallingAssembly (), resourceName);
 
46
                                return img;
 
47
                        }
 
48
                }
 
49
 
 
50
                public LazyImage (string resourceName)
 
51
                {
 
52
                        this.resourceName = resourceName;
 
53
                }
 
54
 
 
55
                public static implicit operator ImageSurface(LazyImage lazy)
 
56
                {
 
57
                        return lazy.Img;
 
58
                }
 
59
 
 
60
        }
 
61
 
 
62
        class RoundButton : Gtk.EventBox
 
63
        {
 
64
                const int height = 32;
 
65
/*              Cairo.Color borderColor;
 
66
 
 
67
                Cairo.Color fill0Color;
 
68
                Cairo.Color fill1Color;
 
69
                Cairo.Color fill2Color;
 
70
                Cairo.Color fill3Color;*/
 
71
 
 
72
                LazyImage btnNormal/*, btnInactive, btnHover, btnPressed*/;
 
73
 
 
74
                LazyImage iconRunNormal, iconRunDisabled;
 
75
                LazyImage iconStopNormal, iconStopDisabled;
 
76
                LazyImage iconBuildNormal, iconBuildDisabled;
 
77
 
 
78
                public enum OperationIcon {
 
79
                        Run,
 
80
                        Build,
 
81
                        Stop
 
82
                }
 
83
 
 
84
                public RoundButton ()
 
85
                {
 
86
                        WidgetFlags |= Gtk.WidgetFlags.AppPaintable;
 
87
                        Events |= EventMask.ButtonPressMask | EventMask.ButtonReleaseMask | EventMask.LeaveNotifyMask | EventMask.PointerMotionMask;
 
88
                        VisibleWindow = false;
 
89
                        SetSizeRequest (height, height);
 
90
 
 
91
                        btnNormal = new LazyImage ("btExecuteBase-Normal.png");
 
92
//                      btnInactive = new LazyImage ("btExecuteBase-Disabled.png");
 
93
                        //btnPressed = new LazyImage ("btExecuteBase-Pressed.png");
 
94
//                      btnHover = new LazyImage ("btExecuteBase-Hover.png");
 
95
 
 
96
                        iconRunNormal = new LazyImage ("icoExecute-Normal.png");
 
97
                        iconRunDisabled = new LazyImage ("icoExecute-Disabled.png");
 
98
 
 
99
                        iconStopNormal = new LazyImage ("icoStop-Normal.png");
 
100
                        iconStopDisabled = new LazyImage ("icoStop-Disabled.png");
 
101
 
 
102
                        iconBuildNormal = new LazyImage ("icoBuild-Normal.png");
 
103
                        iconBuildDisabled = new LazyImage ("icoBuild-Disabled.png");
 
104
                }
 
105
 
 
106
                StateType hoverState = StateType.Normal;
 
107
 
 
108
                protected override bool OnMotionNotifyEvent (EventMotion evnt)
 
109
                {
 
110
 
 
111
                        State = IsInside (evnt.X, evnt.Y) ? hoverState : StateType.Normal;;
 
112
                        return base.OnMotionNotifyEvent (evnt);
 
113
                }
 
114
 
 
115
 
 
116
                protected override bool OnLeaveNotifyEvent (EventCrossing evnt)
 
117
                {
 
118
                        State = StateType.Normal;
 
119
                        return base.OnLeaveNotifyEvent (evnt);
 
120
                }
 
121
 
 
122
                protected override bool OnButtonPressEvent (EventButton evnt)
 
123
                {
 
124
                        if (evnt.Button == 1 && IsInside (evnt.X, evnt.Y)) {
 
125
                                hoverState = State = StateType.Selected;
 
126
                        }
 
127
                        return true;
 
128
                }
 
129
 
 
130
                protected override bool OnButtonReleaseEvent (EventButton evnt)
 
131
                {
 
132
                        if (State == StateType.Selected)
 
133
                                OnClicked (EventArgs.Empty);
 
134
                        State = IsInside (evnt.X, evnt.Y) ? StateType.Prelight : StateType.Normal;;
 
135
                        hoverState = StateType.Prelight; 
 
136
                        return true;
 
137
                }
 
138
 
 
139
                bool IsInside (double x, double y)
 
140
                {
 
141
                        var xr = x - Allocation.Width / 2;
 
142
                        var yr = y - Allocation.Height / 2;
 
143
                        return Math.Sqrt (xr * xr + yr * yr) <= height / 2;
 
144
                }
 
145
 
 
146
                protected override void OnSizeRequested (ref Requisition requisition)
 
147
                {
 
148
                        requisition.Width = btnNormal.Img.Width;
 
149
                        requisition.Height = btnNormal.Img.Height + 2;
 
150
                        base.OnSizeRequested (ref requisition);
 
151
                }
 
152
 
 
153
                ImageSurface GetIcon()
 
154
                {
 
155
                        switch (icon) {
 
156
                        case OperationIcon.Stop:
 
157
                                return State ==  StateType.Insensitive ? iconStopDisabled : iconStopNormal;
 
158
                        case OperationIcon.Run:
 
159
                                return State ==  StateType.Insensitive ? iconRunDisabled : iconRunNormal;
 
160
                        case OperationIcon.Build:
 
161
                                return State ==  StateType.Insensitive ? iconBuildDisabled : iconBuildNormal;
 
162
                        }
 
163
                        throw new InvalidOperationException ();
 
164
                }
 
165
 
 
166
                OperationIcon icon;
 
167
                public OperationIcon Icon {
 
168
                        get { return icon; }
 
169
                        set {
 
170
                                if (value != icon) {
 
171
                                        icon = value;
 
172
                                        QueueDraw ();
 
173
                                }
 
174
                        }
 
175
                }
 
176
 
 
177
                protected override bool OnExposeEvent (EventExpose evnt)
 
178
                {
 
179
                        using (var context = Gdk.CairoHelper.Create (evnt.Window)) {
 
180
                                DrawBackground (context, Allocation, 15, State);
 
181
                                var icon = GetIcon();
 
182
                                icon.Show (
 
183
                                        context,
 
184
                                        Allocation.X + (icon.Width - Allocation.Width) / 2,
 
185
                                        Allocation.Y + (icon.Height - Allocation.Height) / 2
 
186
                                );
 
187
                        }
 
188
                        return base.OnExposeEvent (evnt);
 
189
                }
 
190
 
 
191
                void DrawBackground (Cairo.Context context, Gdk.Rectangle region, int radius, StateType state)
 
192
                {
 
193
                        double rad = radius - 0.5;
 
194
                        int centerX = region.X + region.Width / 2;
 
195
                        int centerY = region.Y + region.Height / 2;
 
196
 
 
197
                        context.MoveTo (centerX + rad, centerY);
 
198
                        context.Arc (centerX, centerY, rad, 0, Math.PI * 2);
 
199
 
 
200
                        double high;
 
201
                        double low;
 
202
                        switch (state) {
 
203
                        case StateType.Selected:
 
204
                                high = 0.85;
 
205
                                low = 1.0;
 
206
                                break;
 
207
                        case StateType.Prelight:
 
208
                                high = 1.0;
 
209
                                low = 0.9;
 
210
                                break;
 
211
                        case StateType.Insensitive:
 
212
                                high = 0.95;
 
213
                                low = 0.83;
 
214
                                break;
 
215
                        default:
 
216
                                high = 1.0;
 
217
                                low = 0.85;
 
218
                                break;
 
219
                        }
 
220
                        using (var lg = new LinearGradient (0, centerY - rad, 0, centerY +rad)) {
 
221
                                lg.AddColorStop (0, new Cairo.Color (high, high, high));
 
222
                                lg.AddColorStop (1, new Cairo.Color (low, low, low));
 
223
                                context.Pattern = lg;
 
224
                                context.FillPreserve ();
 
225
                        }
 
226
 
 
227
                        context.Color = new Cairo.Color (0, 0, 0, 0.4);
 
228
                        context.LineWidth = 1;
 
229
                        context.Stroke ();
 
230
                }
 
231
 
 
232
                public event EventHandler Clicked;
 
233
 
 
234
                protected virtual void OnClicked (EventArgs e)
 
235
                {
 
236
                        EventHandler handler = this.Clicked;
 
237
                        if (handler != null)
 
238
                                handler (this, e);
 
239
                }
 
240
        }
 
241
}
 
242