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

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Components/PopoverWidget.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
// PopoverWidget.cs
 
3
//
 
4
// Author:
 
5
//       Mike KrĆ¼ger <mkrueger@xamarin.com>
 
6
//       Lluis Sanchez <lluis@xamarin.com>
 
7
//
 
8
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
 
9
//
 
10
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
11
// of this software and associated documentation files (the "Software"), to deal
 
12
// in the Software without restriction, including without limitation the rights
 
13
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
14
// copies of the Software, and to permit persons to whom the Software is
 
15
// furnished to do so, subject to the following conditions:
 
16
//
 
17
// The above copyright notice and this permission notice shall be included in
 
18
// all copies or substantial portions of the Software.
 
19
//
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
21
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
22
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
23
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
24
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
25
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
26
// THE SOFTWARE.
 
27
using System;
 
28
using Gtk;
 
29
using Gdk;
 
30
 
 
31
namespace MonoDevelop.Components
 
32
{
 
33
        public class PopoverWidget: Gtk.EventBox, Animatable
 
34
        {
 
35
                PopoverWindowTheme theme;
 
36
 
 
37
                PopupPosition position;
 
38
                Gtk.Alignment alignment;
 
39
 
 
40
                Gdk.Size targetSize;
 
41
                Gdk.Size paintSize;
 
42
 
 
43
                bool disableSizeCheck;
 
44
 
 
45
                const int MinArrowSpacing = 5;
 
46
 
 
47
                public PopoverWidget ()
 
48
                {
 
49
                        AppPaintable = true;
 
50
                        VisibleWindow = false;
 
51
 
 
52
                        alignment = new Alignment (0, 0, 1f, 1f);
 
53
                        alignment.Show ();
 
54
                        Add (alignment);
 
55
 
 
56
                        disableSizeCheck = false;
 
57
 
 
58
                        SizeRequested += (object o, SizeRequestedArgs args) => {
 
59
                                if (this.AnimationIsRunning("Resize") && !disableSizeCheck) {
 
60
                                        Gtk.Requisition result = new Gtk.Requisition ();
 
61
                                        result.Width  = Math.Max (args.Requisition.Width, Math.Max (Allocation.Width, targetSize.Width));
 
62
                                        result.Height = Math.Max (args.Requisition.Height, Math.Max (Allocation.Height, targetSize.Height));
 
63
                                        args.Requisition = result;
 
64
                                }
 
65
                        };
 
66
 
 
67
                        UpdatePadding ();
 
68
                }
 
69
 
 
70
                public bool EnableAnimation { get; set; }
 
71
 
 
72
                public PopoverWindowTheme Theme { 
 
73
                        get { 
 
74
                                if (theme == null) {
 
75
                                        theme = new PopoverWindowTheme ();
 
76
                                        theme.RedrawNeeded += OnRedrawNeeded;
 
77
                                }
 
78
                                return theme; 
 
79
                        }
 
80
                        set {
 
81
                                if (theme == value)
 
82
                                        return;
 
83
 
 
84
                                theme.RedrawNeeded -= OnRedrawNeeded;
 
85
                                theme = value;
 
86
                                theme.RedrawNeeded += OnRedrawNeeded;
 
87
 
 
88
                                UpdatePadding ();
 
89
                                QueueDraw ();
 
90
                        }
 
91
                }
 
92
 
 
93
                public bool ShowArrow {
 
94
                        get { return Theme.ShowArrow; }
 
95
                        set { Theme.ShowArrow = value; }
 
96
                }
 
97
 
 
98
                public Gtk.Alignment ContentBox {
 
99
                        get { return alignment; }
 
100
                }
 
101
 
 
102
                public PopupPosition PopupPosition {
 
103
                        get { return position; }
 
104
                        set { Theme.TargetPosition = position = value; }
 
105
                }
 
106
                
 
107
                public void AnimatedResize ()
 
108
                {
 
109
                        if (!EnableAnimation || !GtkUtil.ScreenSupportsARGB ()) {
 
110
                                QueueResize();
 
111
                                return;
 
112
                        }
 
113
 
 
114
                        disableSizeCheck = true;
 
115
                        Gtk.Requisition sizeReq = Gtk.Requisition.Zero;
 
116
                        // use OnSizeRequested instead of SizeRequest to bypass internal GTK caching
 
117
                        OnSizeRequested (ref sizeReq);
 
118
                        disableSizeCheck = false;
 
119
 
 
120
                        Gdk.Size size = new Gdk.Size (sizeReq.Width, sizeReq.Height);
 
121
 
 
122
                        // ensure that our apint area is big enough for our padding
 
123
                        if (paintSize.Width <= 15 || paintSize.Height <= 15)
 
124
                                paintSize = size;
 
125
 
 
126
                        targetSize = size;
 
127
                        Gdk.Size start = paintSize;
 
128
                        Func<float, Gdk.Size> transform = x => new Gdk.Size ((int)(start.Width + (size.Width - start.Width) * x),
 
129
                                                                             (int)(start.Height + (size.Height - start.Height) * x));
 
130
                        this.Animate ("Resize",
 
131
                                      length: 150,
 
132
                                      easing: Easing.SinInOut,
 
133
                                      transform: transform,
 
134
                                      callback: s => paintSize = s,
 
135
                                      finished: (x, aborted) => { if (!aborted) MaybeReanimate(); });
 
136
                        QueueResize ();
 
137
                }
 
138
 
 
139
                void MaybeReanimate ()
 
140
                {
 
141
                        disableSizeCheck = true;
 
142
                        Gtk.Requisition sizeReq = Gtk.Requisition.Zero;
 
143
                        OnSizeRequested (ref sizeReq);
 
144
                        disableSizeCheck = false;
 
145
 
 
146
                        if (sizeReq.Width == paintSize.Width && sizeReq.Height == paintSize.Height)
 
147
                                QueueResize ();
 
148
                        else
 
149
                                AnimatedResize (); //Desired size changed mid animation
 
150
                }
 
151
 
 
152
                protected override bool OnExposeEvent (Gdk.EventExpose evnt)
 
153
                {
 
154
                        if ((position & PopupPosition.Top) != 0 || (position & PopupPosition.Bottom) != 0)
 
155
                                theme.ArrowOffset = Allocation.Width / 2;
 
156
                        else
 
157
                                theme.ArrowOffset = Allocation.Height / 2;
 
158
 
 
159
                        using (var context = Gdk.CairoHelper.Create (evnt.Window)) {
 
160
                                context.Save ();
 
161
                                Theme.SetBorderPath (context, BorderAllocation, position);
 
162
                                context.Clip ();
 
163
                                OnDrawContent (evnt, context); // Draw content first so we can easily clip it
 
164
                                context.Restore ();
 
165
 
 
166
 
 
167
                                // protect against overriden methods which leave in a bad state
 
168
                                context.Save ();
 
169
                                if (Theme.DrawPager) {
 
170
                                        Theme.RenderPager (context, 
 
171
                                                           PangoContext,
 
172
                                                           new Gdk.Rectangle (Allocation.X, Allocation.Y, paintSize.Width, paintSize.Height));
 
173
                                }
 
174
 
 
175
                                Theme.RenderBorder (context, BorderAllocation, position);
 
176
                                context.Restore ();
 
177
 
 
178
                        }
 
179
                        return base.OnExposeEvent (evnt);
 
180
                }
 
181
 
 
182
                protected virtual void OnDrawContent (Gdk.EventExpose evnt, Cairo.Context context)
 
183
                {
 
184
                        Theme.RenderBackground (context, new Gdk.Rectangle (Allocation.X, Allocation.Y, paintSize.Width, paintSize.Height));
 
185
                }
 
186
 
 
187
                void UpdatePadding ()
 
188
                {
 
189
                        uint top,left,bottom,right;
 
190
                        top = left = bottom = right = (uint)Theme.Padding + 1;
 
191
 
 
192
                        if (ShowArrow) {
 
193
                                if ((position & PopupPosition.Top) != 0)
 
194
                                        top += (uint)Theme.ArrowLength;
 
195
                                else if ((position & PopupPosition.Bottom) != 0)
 
196
                                        bottom += (uint)Theme.ArrowLength;
 
197
                                else if ((position & PopupPosition.Left) != 0)
 
198
                                        left += (uint)Theme.ArrowLength;
 
199
                                else if ((position & PopupPosition.Right) != 0)
 
200
                                        right += (uint)Theme.ArrowLength;
 
201
                        }
 
202
                        alignment.SetPadding (top, bottom, left, right);
 
203
                }
 
204
 
 
205
                void OnRedrawNeeded (object sender, EventArgs args)
 
206
                {
 
207
                        UpdatePadding ();
 
208
                        QueueDraw ();
 
209
                }
 
210
 
 
211
                protected override void OnSizeAllocated (Rectangle allocation)
 
212
                {
 
213
                        if (!this.AnimationIsRunning ("Resize"))
 
214
                                paintSize = new Gdk.Size (allocation.Width, allocation.Height);
 
215
 
 
216
                        base.OnSizeAllocated (allocation);
 
217
                }
 
218
 
 
219
                protected Rectangle ChildAllocation {
 
220
                        get {
 
221
                                var rect = BorderAllocation;
 
222
                                rect.Inflate (-Theme.Padding - 1, -Theme.Padding - 1);
 
223
                                return rect;
 
224
                        }
 
225
                }
 
226
 
 
227
                Rectangle BorderAllocation {
 
228
                        get {
 
229
                                var rect = new Gdk.Rectangle (Allocation.X, Allocation.Y, paintSize.Width, paintSize.Height);
 
230
                                if (ShowArrow) {
 
231
                                        if ((position & PopupPosition.Top) != 0) {
 
232
                                                rect.Y += Theme.ArrowLength;
 
233
                                                rect.Height -= Theme.ArrowLength;
 
234
                                        }
 
235
                                        else if ((position & PopupPosition.Bottom) != 0) {
 
236
                                                rect.Height -= Theme.ArrowLength;
 
237
                                        }
 
238
                                        else if ((position & PopupPosition.Left) != 0) {
 
239
                                                rect.X += Theme.ArrowLength;
 
240
                                                rect.Width -= Theme.ArrowLength;
 
241
                                        }
 
242
                                        else if ((position & PopupPosition.Right) != 0) {
 
243
                                                rect.Width -= Theme.ArrowLength;
 
244
                                        }
 
245
                                }
 
246
                                return rect;
 
247
                        }
 
248
                }       }
 
249
}
 
250