5
// Mike KrĆ¼ger <mkrueger@xamarin.com>
6
// Lluis Sanchez <lluis@xamarin.com>
8
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
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:
17
// The above copyright notice and this permission notice shall be included in
18
// all copies or substantial portions of the Software.
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
31
namespace MonoDevelop.Components
33
public class PopoverWidget: Gtk.EventBox, Animatable
35
PopoverWindowTheme theme;
37
PopupPosition position;
38
Gtk.Alignment alignment;
43
bool disableSizeCheck;
45
const int MinArrowSpacing = 5;
47
public PopoverWidget ()
50
VisibleWindow = false;
52
alignment = new Alignment (0, 0, 1f, 1f);
56
disableSizeCheck = false;
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;
70
public bool EnableAnimation { get; set; }
72
public PopoverWindowTheme Theme {
75
theme = new PopoverWindowTheme ();
76
theme.RedrawNeeded += OnRedrawNeeded;
84
theme.RedrawNeeded -= OnRedrawNeeded;
86
theme.RedrawNeeded += OnRedrawNeeded;
93
public bool ShowArrow {
94
get { return Theme.ShowArrow; }
95
set { Theme.ShowArrow = value; }
98
public Gtk.Alignment ContentBox {
99
get { return alignment; }
102
public PopupPosition PopupPosition {
103
get { return position; }
104
set { Theme.TargetPosition = position = value; }
107
public void AnimatedResize ()
109
if (!EnableAnimation || !GtkUtil.ScreenSupportsARGB ()) {
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;
120
Gdk.Size size = new Gdk.Size (sizeReq.Width, sizeReq.Height);
122
// ensure that our apint area is big enough for our padding
123
if (paintSize.Width <= 15 || paintSize.Height <= 15)
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",
132
easing: Easing.SinInOut,
133
transform: transform,
134
callback: s => paintSize = s,
135
finished: (x, aborted) => { if (!aborted) MaybeReanimate(); });
139
void MaybeReanimate ()
141
disableSizeCheck = true;
142
Gtk.Requisition sizeReq = Gtk.Requisition.Zero;
143
OnSizeRequested (ref sizeReq);
144
disableSizeCheck = false;
146
if (sizeReq.Width == paintSize.Width && sizeReq.Height == paintSize.Height)
149
AnimatedResize (); //Desired size changed mid animation
152
protected override bool OnExposeEvent (Gdk.EventExpose evnt)
154
if ((position & PopupPosition.Top) != 0 || (position & PopupPosition.Bottom) != 0)
155
theme.ArrowOffset = Allocation.Width / 2;
157
theme.ArrowOffset = Allocation.Height / 2;
159
using (var context = Gdk.CairoHelper.Create (evnt.Window)) {
161
Theme.SetBorderPath (context, BorderAllocation, position);
163
OnDrawContent (evnt, context); // Draw content first so we can easily clip it
167
// protect against overriden methods which leave in a bad state
169
if (Theme.DrawPager) {
170
Theme.RenderPager (context,
172
new Gdk.Rectangle (Allocation.X, Allocation.Y, paintSize.Width, paintSize.Height));
175
Theme.RenderBorder (context, BorderAllocation, position);
179
return base.OnExposeEvent (evnt);
182
protected virtual void OnDrawContent (Gdk.EventExpose evnt, Cairo.Context context)
184
Theme.RenderBackground (context, new Gdk.Rectangle (Allocation.X, Allocation.Y, paintSize.Width, paintSize.Height));
187
void UpdatePadding ()
189
uint top,left,bottom,right;
190
top = left = bottom = right = (uint)Theme.Padding + 1;
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;
202
alignment.SetPadding (top, bottom, left, right);
205
void OnRedrawNeeded (object sender, EventArgs args)
211
protected override void OnSizeAllocated (Rectangle allocation)
213
if (!this.AnimationIsRunning ("Resize"))
214
paintSize = new Gdk.Size (allocation.Width, allocation.Height);
216
base.OnSizeAllocated (allocation);
219
protected Rectangle ChildAllocation {
221
var rect = BorderAllocation;
222
rect.Inflate (-Theme.Padding - 1, -Theme.Padding - 1);
227
Rectangle BorderAllocation {
229
var rect = new Gdk.Rectangle (Allocation.X, Allocation.Y, paintSize.Width, paintSize.Height);
231
if ((position & PopupPosition.Top) != 0) {
232
rect.Y += Theme.ArrowLength;
233
rect.Height -= Theme.ArrowLength;
235
else if ((position & PopupPosition.Bottom) != 0) {
236
rect.Height -= Theme.ArrowLength;
238
else if ((position & PopupPosition.Left) != 0) {
239
rect.X += Theme.ArrowLength;
240
rect.Width -= Theme.ArrowLength;
242
else if ((position & PopupPosition.Right) != 0) {
243
rect.Width -= Theme.ArrowLength;