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

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Components/PopoverWindow.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
// SearchPopupWindow.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 MonoDevelop.Ide;
 
29
using MonoDevelop.Ide.Gui;
 
30
using Mono.TextEditor;
 
31
using Gdk;
 
32
 
 
33
namespace MonoDevelop.Components
 
34
{
 
35
        public class PopoverWindow : Gtk.Window, Animatable
 
36
        {
 
37
                PopoverWindowTheme theme;
 
38
 
 
39
                PopupPosition position;
 
40
                Gtk.Alignment alignment;
 
41
 
 
42
                Gdk.Rectangle currentCaret;
 
43
                Gdk.Window targetWindow;
 
44
                Gtk.Widget parent;
 
45
                bool eventProvided;
 
46
 
 
47
                Gdk.Size targetSize;
 
48
                Gdk.Size paintSize;
 
49
 
 
50
                bool disableSizeCheck;
 
51
 
 
52
                const int MinArrowSpacing = 5;
 
53
 
 
54
                public PopoverWindow () : this(Gtk.WindowType.Popup)
 
55
                {
 
56
                }
 
57
 
 
58
                public PopoverWindow (Gtk.WindowType type) : base(type)
 
59
                {
 
60
                        SkipTaskbarHint = true;
 
61
                        SkipPagerHint = true;
 
62
                        AppPaintable = true;
 
63
                        TypeHint = WindowTypeHint.Tooltip;
 
64
                        CheckScreenColormap ();
 
65
 
 
66
                        alignment = new Alignment (0, 0, 1f, 1f);
 
67
                        alignment.Show ();
 
68
                        Add (alignment);
 
69
 
 
70
                        disableSizeCheck = false;
 
71
 
 
72
                        SizeRequested += (object o, SizeRequestedArgs args) => {
 
73
                                if (this.AnimationIsRunning("Resize") && !disableSizeCheck) {
 
74
                                        Gtk.Requisition result = new Gtk.Requisition ();
 
75
                                        result.Width  = Math.Max (args.Requisition.Width, Math.Max (Allocation.Width, targetSize.Width));
 
76
                                        result.Height = Math.Max (args.Requisition.Height, Math.Max (Allocation.Height, targetSize.Height));
 
77
                                        args.Requisition = result;
 
78
                                }
 
79
                        };
 
80
 
 
81
                        UpdatePadding ();
 
82
                }
 
83
 
 
84
                public PopoverWindowTheme Theme { 
 
85
                        get { 
 
86
                                if (theme == null) {
 
87
                                        theme = new PopoverWindowTheme ();
 
88
                                        theme.RedrawNeeded += OnRedrawNeeded;
 
89
                                }
 
90
                                return theme; 
 
91
                        }
 
92
                        set {
 
93
                                if (theme == value)
 
94
                                        return;
 
95
 
 
96
                                theme.RedrawNeeded -= OnRedrawNeeded;
 
97
                                theme = value;
 
98
                                theme.RedrawNeeded += OnRedrawNeeded;
 
99
 
 
100
                                UpdatePadding ();
 
101
                                QueueDraw ();
 
102
                        }
 
103
                }
 
104
 
 
105
                public bool ShowArrow {
 
106
                        get { return Theme.ShowArrow; }
 
107
                        set { Theme.ShowArrow = value; }
 
108
                }
 
109
 
 
110
                public Gtk.Alignment ContentBox {
 
111
                        get { return alignment; }
 
112
                }
 
113
                
 
114
                public void ShowPopup (Gtk.Widget widget, PopupPosition position)
 
115
                {
 
116
                        ShowPopup (widget, null, Gdk.Rectangle.Zero, position);
 
117
                }
 
118
                
 
119
                public void ShowPopup (Gtk.Widget widget, Gdk.EventButton evt, PopupPosition position)
 
120
                {
 
121
                        ShowPopup (widget, evt, Gdk.Rectangle.Zero, position);
 
122
                }
 
123
 
 
124
                public void ShowPopup (Gtk.Widget widget, Gdk.Rectangle caret, PopupPosition position)
 
125
                {
 
126
                        ShowPopup (widget, null, caret, position);
 
127
                }
 
128
 
 
129
                void ShowPopup (Gtk.Widget parent, Gdk.EventButton evt, Gdk.Rectangle caret, PopupPosition position)
 
130
                {
 
131
                        this.parent = parent;
 
132
                        this.currentCaret = caret;
 
133
                        Theme.TargetPosition = position;
 
134
 
 
135
                        if (evt != null) {
 
136
                                eventProvided = true;
 
137
                                targetWindow = evt.Window;
 
138
                        } else
 
139
                                targetWindow = parent.GdkWindow;
 
140
 
 
141
                        RepositionWindow ();
 
142
                }
 
143
 
 
144
                public void AnimatedResize ()
 
145
                {
 
146
                        if (!GtkUtil.ScreenSupportsARGB ()) {
 
147
                                QueueResize();
 
148
                                return;
 
149
                        }
 
150
 
 
151
                        disableSizeCheck = true;
 
152
                        Gtk.Requisition sizeReq = Gtk.Requisition.Zero;
 
153
                        // use OnSizeRequested instead of SizeRequest to bypass internal GTK caching
 
154
                        OnSizeRequested (ref sizeReq);
 
155
                        disableSizeCheck = false;
 
156
 
 
157
                        Gdk.Size size = new Gdk.Size (sizeReq.Width, sizeReq.Height);
 
158
 
 
159
                        // ensure that our apint area is big enough for our padding
 
160
                        if (paintSize.Width <= 15 || paintSize.Height <= 15)
 
161
                                paintSize = size;
 
162
 
 
163
                        targetSize = size;
 
164
                        Gdk.Size start = paintSize;
 
165
                        Func<float, Gdk.Size> transform = x => new Gdk.Size ((int)(start.Width + (size.Width - start.Width) * x),
 
166
                                                                             (int)(start.Height + (size.Height - start.Height) * x));
 
167
                        this.Animate ("Resize",
 
168
                                      length: 150,
 
169
                                      easing: Easing.SinInOut,
 
170
                                      transform: transform,
 
171
                                      callback: s => paintSize = s,
 
172
                                      finished: (x, aborted) => { if (!aborted) MaybeReanimate(); });
 
173
                        QueueResize ();
 
174
                }
 
175
 
 
176
                void MaybeReanimate ()
 
177
                {
 
178
                        disableSizeCheck = true;
 
179
                        Gtk.Requisition sizeReq = Gtk.Requisition.Zero;
 
180
                        OnSizeRequested (ref sizeReq);
 
181
                        disableSizeCheck = false;
 
182
 
 
183
                        if (sizeReq.Width == paintSize.Width && sizeReq.Height == paintSize.Height)
 
184
                                QueueResize ();
 
185
                        else
 
186
                                AnimatedResize (); //Desired size changed mid animation
 
187
                }
 
188
 
 
189
                /// <summary>
 
190
                /// Gets or sets the maximum Y top bound. The popover window will be placed below this bound.
 
191
                /// 0 means it's not set. Default value: 0
 
192
                /// </summary>
 
193
                public int MaximumYTopBound {
 
194
                        get;
 
195
                        set;
 
196
                }
 
197
 
 
198
                public void RepositionWindow ()
 
199
                {
 
200
                        if (parent == null)
 
201
                                return;
 
202
 
 
203
                        int x, y;
 
204
                        Gdk.Rectangle caret = currentCaret;
 
205
                        Gdk.Window window = targetWindow;
 
206
                        if (targetWindow == null)
 
207
                                return;
 
208
                        PopupPosition position = Theme.TargetPosition;
 
209
                        this.position = Theme.TargetPosition;
 
210
                        UpdatePadding ();
 
211
 
 
212
                        window.GetOrigin (out x, out y);
 
213
                        var alloc = parent.Allocation;
 
214
 
 
215
                        if (eventProvided) {
 
216
                                caret.X = x;
 
217
                                caret.Y = y;
 
218
                                caret.Width = caret.Height = 1;
 
219
                        } else {
 
220
                                if (caret.Equals (Gdk.Rectangle.Zero))
 
221
                                        caret = new Gdk.Rectangle (0, 0, alloc.Width, alloc.Height);
 
222
                                caret = GtkUtil.ToScreenCoordinates (parent, parent.GdkWindow, caret);
 
223
                        }
 
224
 
 
225
                        Gtk.Requisition request = SizeRequest ();
 
226
                        var screen = parent.Screen;
 
227
                        Gdk.Rectangle geometry = GtkWorkarounds.GetUsableMonitorGeometry (screen, screen.GetMonitorAtPoint (x, y));
 
228
 
 
229
                        // Flip the orientation if the window doesn't fit the screen.
 
230
 
 
231
                        int intPos = (int) position;
 
232
                        switch ((PopupPosition)(intPos & 0x0f)) {
 
233
                        case PopupPosition.Top:
 
234
                                if (caret.Bottom + request.Height > geometry.Bottom)
 
235
                                        intPos = (intPos & 0xf0) | (int)PopupPosition.Bottom;
 
236
                                break;
 
237
                        case PopupPosition.Bottom:
 
238
                                if (caret.Top - request.Height < geometry.X)
 
239
                                        intPos = (intPos & 0xf0) | (int)PopupPosition.Top;
 
240
                                break;
 
241
                        case PopupPosition.Right:
 
242
                                if (caret.X - request.Width < geometry.X)
 
243
                                        intPos = (intPos & 0xf0) | (int)PopupPosition.Left;
 
244
                                break;
 
245
                        case PopupPosition.Left:
 
246
                                if (caret.Right + request.Width > geometry.Right)
 
247
                                        intPos = (intPos & 0xf0) | (int)PopupPosition.Right;
 
248
                                break;
 
249
                        }
 
250
 
 
251
                        position = (PopupPosition) intPos;
 
252
                        UpdatePadding ();
 
253
 
 
254
                        // Calculate base coordinate
 
255
 
 
256
                        switch ((PopupPosition)((int)position & 0x0f)) {
 
257
                        case PopupPosition.Top:
 
258
                                y = caret.Bottom;
 
259
                                break;
 
260
                        case PopupPosition.Bottom:
 
261
                                y = caret.Y - request.Height; break;
 
262
                        case PopupPosition.Right:
 
263
                                x = caret.X - request.Width; break;
 
264
                        case PopupPosition.Left:
 
265
                                x = caret.Right; break;
 
266
                        }
 
267
                        int offset;
 
268
                        if ((position & PopupPosition.Top) != 0 || (position & PopupPosition.Bottom) != 0) {
 
269
                                if (((int)position & 0x10) != 0)
 
270
                                        x = caret.X - MinArrowSpacing - Theme.ArrowWidth/2;
 
271
                                else if (((int)position & 0x20) != 0)
 
272
                                        x = caret.Right - request.Width + MinArrowSpacing + Theme.ArrowWidth/2;
 
273
                                else
 
274
                                        x = caret.X + (caret.Width - request.Width) / 2;
 
275
 
 
276
                                if (x < geometry.Left)
 
277
                                        x = geometry.Left;
 
278
                                else if (x + request.Width > geometry.Right)
 
279
                                        x = geometry.Right - request.Width;
 
280
 
 
281
                                offset = caret.X + caret.Width / 2 - x;
 
282
                                if (offset - Theme.ArrowWidth/2 < MinArrowSpacing)
 
283
                                        offset = MinArrowSpacing + Theme.ArrowWidth/2;
 
284
                                if (offset > request.Width - MinArrowSpacing - Theme.ArrowWidth/2)
 
285
                                        offset = request.Width - MinArrowSpacing - Theme.ArrowWidth/2;
 
286
                        }
 
287
                        else {
 
288
                                if (((int)position & 0x10) != 0)
 
289
                                        y = caret.Y - MinArrowSpacing - Theme.ArrowWidth/2;
 
290
                                else if (((int)position & 0x20) != 0)
 
291
                                        y = caret.Bottom - request.Height + MinArrowSpacing + Theme.ArrowWidth/2;
 
292
                                else
 
293
                                        y = caret.Y + (caret.Height - request.Height) / 2;
 
294
 
 
295
                                if (y < geometry.Top)
 
296
                                        y = geometry.Top;
 
297
                                else if (y + request.Height > geometry.Bottom)
 
298
                                        y = geometry.Bottom - request.Height;
 
299
                                if (MaximumYTopBound > 0)
 
300
                                        y = Math.Max (MaximumYTopBound, y);
 
301
 
 
302
                                offset = caret.Y + caret.Height / 2 - y;
 
303
                                if (offset - Theme.ArrowWidth/2 < MinArrowSpacing)
 
304
                                        offset = MinArrowSpacing + Theme.ArrowWidth/2;
 
305
                                if (offset > request.Height - MinArrowSpacing - Theme.ArrowWidth/2)
 
306
                                        offset = request.Height - MinArrowSpacing - Theme.ArrowWidth/2;
 
307
                        }
 
308
                        Theme.ArrowOffset = offset;
 
309
                        this.position = position;
 
310
                        UpdatePadding ();
 
311
 
 
312
                        Move (x, y);
 
313
                        Show ();
 
314
                        DesktopService.RemoveWindowShadow (this);
 
315
                }
 
316
                
 
317
                public bool SupportsAlpha {
 
318
                        get;
 
319
                        private set;
 
320
                }
 
321
 
 
322
                void CheckScreenColormap ()
 
323
                {
 
324
                        SupportsAlpha = Screen.IsComposited;
 
325
                        if (SupportsAlpha) {
 
326
                                Colormap = Screen.RgbaColormap;
 
327
                        } else {
 
328
                                Colormap = Screen.RgbColormap;
 
329
                        }
 
330
                }
 
331
 
 
332
                protected override void OnScreenChanged (Gdk.Screen previous_screen)
 
333
                {
 
334
                        base.OnScreenChanged (previous_screen);
 
335
                        CheckScreenColormap ();
 
336
                }
 
337
 
 
338
                protected override bool OnExposeEvent (Gdk.EventExpose evnt)
 
339
                {
 
340
                        bool retVal;
 
341
                        bool changed;
 
342
                        using (var context = Gdk.CairoHelper.Create (evnt.Window)) {
 
343
                                context.Save ();
 
344
                                if (SupportsAlpha) {
 
345
                                        context.Operator = Cairo.Operator.Source;
 
346
                                        context.SetSourceRGBA (1, 1, 1, 0);
 
347
                                } else {
 
348
                                        context.Operator = Cairo.Operator.Over;
 
349
                                        context.SetSourceRGB (1, 1, 1);
 
350
                                }
 
351
                                context.Paint ();
 
352
                                context.Restore ();
 
353
 
 
354
                                OnDrawContent (evnt, context); // Draw content first so we can easily clip it
 
355
                                retVal = base.OnExposeEvent (evnt);
 
356
 
 
357
                                changed = Theme.SetBorderPath (context, BorderAllocation, position);
 
358
                                context.Operator = Cairo.Operator.DestIn;
 
359
                                context.SetSourceRGBA (1, 1, 1, 1);
 
360
                                context.Fill ();
 
361
                                context.Operator = Cairo.Operator.Over;
 
362
 
 
363
                                // protect against overriden methods which leave in a bad state
 
364
                                context.Save ();
 
365
                                if (Theme.DrawPager) {
 
366
                                        Theme.RenderPager (context, 
 
367
                                                           PangoContext,
 
368
                                                           new Gdk.Rectangle (Allocation.X, Allocation.Y, paintSize.Width, paintSize.Height));
 
369
                                }
 
370
 
 
371
                                Theme.RenderBorder (context, BorderAllocation, position);
 
372
                                context.Restore ();
 
373
 
 
374
                        }
 
375
 
 
376
                        if (changed)
 
377
                                GtkWorkarounds.UpdateNativeShadow (this);
 
378
 
 
379
                        return retVal;
 
380
                }
 
381
 
 
382
                protected virtual void OnDrawContent (Gdk.EventExpose evnt, Cairo.Context context)
 
383
                {
 
384
                        Theme.RenderBackground (context, new Gdk.Rectangle (Allocation.X, Allocation.Y, paintSize.Width, paintSize.Height));
 
385
                }
 
386
 
 
387
                void UpdatePadding ()
 
388
                {
 
389
                        uint top,left,bottom,right;
 
390
                        top = left = bottom = right = (uint)Theme.Padding + 1;
 
391
 
 
392
                        if (ShowArrow) {
 
393
                                if ((position & PopupPosition.Top) != 0)
 
394
                                        top += (uint)Theme.ArrowLength;
 
395
                                else if ((position & PopupPosition.Bottom) != 0)
 
396
                                        bottom += (uint)Theme.ArrowLength;
 
397
                                else if ((position & PopupPosition.Left) != 0)
 
398
                                        left += (uint)Theme.ArrowLength;
 
399
                                else if ((position & PopupPosition.Right) != 0)
 
400
                                        right += (uint)Theme.ArrowLength;
 
401
                        }
 
402
                        alignment.SetPadding (top, bottom, left, right);
 
403
                }
 
404
 
 
405
                void OnRedrawNeeded (object sender, EventArgs args)
 
406
                {
 
407
                        UpdatePadding ();
 
408
                        QueueDraw ();
 
409
                }
 
410
 
 
411
                protected override void OnSizeAllocated (Rectangle allocation)
 
412
                {
 
413
                        if (!this.AnimationIsRunning ("Resize"))
 
414
                                paintSize = new Gdk.Size (allocation.Width, allocation.Height);
 
415
 
 
416
                        base.OnSizeAllocated (allocation);
 
417
                }
 
418
 
 
419
                protected Rectangle ChildAllocation {
 
420
                        get {
 
421
                                var rect = BorderAllocation;
 
422
                                rect.Inflate (-Theme.Padding - 1, -Theme.Padding - 1);
 
423
                                return rect;
 
424
                        }
 
425
                }
 
426
 
 
427
                Rectangle BorderAllocation {
 
428
                        get {
 
429
                                var rect = new Gdk.Rectangle (Allocation.X, Allocation.Y, paintSize.Width, paintSize.Height);
 
430
                                if (ShowArrow) {
 
431
                                        if ((position & PopupPosition.Top) != 0) {
 
432
                                                rect.Y += Theme.ArrowLength;
 
433
                                                rect.Height -= Theme.ArrowLength;
 
434
                                        }
 
435
                                        else if ((position & PopupPosition.Bottom) != 0) {
 
436
                                                rect.Height -= Theme.ArrowLength;
 
437
                                        }
 
438
                                        else if ((position & PopupPosition.Left) != 0) {
 
439
                                                rect.X += Theme.ArrowLength;
 
440
                                                rect.Width -= Theme.ArrowLength;
 
441
                                        }
 
442
                                        else if ((position & PopupPosition.Right) != 0) {
 
443
                                                rect.Width -= Theme.ArrowLength;
 
444
                                        }
 
445
                                }
 
446
                                return rect;
 
447
                        }
 
448
                }
 
449
        }
 
450
}
 
451