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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Gtk/Xwt.GtkBackend/CanvasBackend.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
// CanvasBackend.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez <lluis@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2011 Xamarin Inc
 
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
 
 
27
using System;
 
28
using System.Collections.Generic;
 
29
using Xwt.Backends;
 
30
using System.Linq;
 
31
using Xwt.Engine;
 
32
using Xwt.CairoBackend;
 
33
 
 
34
namespace Xwt.GtkBackend
 
35
{
 
36
        public class CanvasBackend: WidgetBackend, ICanvasBackend
 
37
        {
 
38
                public CanvasBackend ()
 
39
                {
 
40
                }
 
41
                
 
42
                public override void Initialize ()
 
43
                {
 
44
                        Widget = new CustomCanvas ();
 
45
                        Widget.Frontend = Frontend;
 
46
                        Widget.EventSink = EventSink;
 
47
                        Widget.Events |= Gdk.EventMask.ButtonPressMask | Gdk.EventMask.ButtonReleaseMask | Gdk.EventMask.PointerMotionMask;
 
48
                        Widget.Show ();
 
49
                }
 
50
 
 
51
                new CustomCanvas Widget {
 
52
                        get { return (CustomCanvas)base.Widget; }
 
53
                        set { base.Widget = value; }
 
54
                }
 
55
                
 
56
                protected new ICanvasEventSink EventSink {
 
57
                        get { return (ICanvasEventSink)base.EventSink; }
 
58
                }
 
59
 
 
60
                public void QueueDraw ()
 
61
                {
 
62
                        Widget.QueueDraw ();
 
63
                }
 
64
                
 
65
                public void QueueDraw (Rectangle rect)
 
66
                {
 
67
                        Widget.QueueDrawArea ((int)rect.X, (int)rect.Y, (int)rect.Width, (int)rect.Height);
 
68
                }
 
69
                
 
70
                public Rectangle Bounds {
 
71
                        get {
 
72
                                return new Rectangle (0, 0, Widget.Allocation.Width, Widget.Allocation.Height);
 
73
                        }
 
74
                }
 
75
 
 
76
                public void AddChild (IWidgetBackend widget, Rectangle bounds)
 
77
                {
 
78
                        var w = ((IGtkWidgetBackend)widget).Widget;
 
79
                        Widget.Add (w);
 
80
                        Widget.SetAllocation (w, bounds);
 
81
                }
 
82
                
 
83
                public void RemoveChild (IWidgetBackend widget)
 
84
                {
 
85
                        var w = ((IGtkWidgetBackend)widget).Widget;
 
86
                        Widget.Remove (w);
 
87
                }
 
88
                
 
89
                public void SetChildBounds (IWidgetBackend widget, Rectangle bounds)
 
90
                {
 
91
                        var w = ((IGtkWidgetBackend)widget).Widget;
 
92
                        Widget.SetAllocation (w, bounds);
 
93
                }
 
94
        }
 
95
        
 
96
        class CustomCanvas: Gtk.EventBox
 
97
        {
 
98
                public Widget Frontend;
 
99
                public ICanvasEventSink EventSink;
 
100
                Dictionary<Gtk.Widget, Rectangle> children = new Dictionary<Gtk.Widget, Rectangle> ();
 
101
                
 
102
                public CustomCanvas ()
 
103
                {
 
104
                        GtkWorkarounds.FixContainerLeak (this);
 
105
 
 
106
                        WidgetFlags |= Gtk.WidgetFlags.AppPaintable;
 
107
                }
 
108
                
 
109
                public void SetAllocation (Gtk.Widget w, Rectangle rect)
 
110
                {
 
111
                        children [w] = rect;
 
112
                        QueueResize ();
 
113
                }
 
114
                
 
115
                protected override void OnAdded (Gtk.Widget widget)
 
116
                {
 
117
                        children.Add (widget, new Rectangle (0,0,0,0));
 
118
                        widget.Parent = this;
 
119
                        QueueResize ();
 
120
                }
 
121
                
 
122
                protected override void OnRemoved (Gtk.Widget widget)
 
123
                {
 
124
                        children.Remove (widget);
 
125
                        widget.Unparent ();
 
126
                }
 
127
                
 
128
                protected override void OnSizeRequested (ref Gtk.Requisition requisition)
 
129
                {
 
130
                        base.OnSizeRequested (ref requisition);
 
131
                        IWidgetSurface ws = Frontend.Surface;
 
132
                        int h, w;
 
133
                        if (ws.SizeRequestMode == SizeRequestMode.HeightForWidth) {
 
134
                                w = (int)ws.GetPreferredWidth ().MinSize;
 
135
                                h = (int)ws.GetPreferredHeightForWidth (w).MinSize;
 
136
                        } else {
 
137
                                h = (int)ws.GetPreferredHeight ().MinSize;
 
138
                                w = (int)ws.GetPreferredWidthForHeight(h).MinSize;
 
139
                        }
 
140
                        if (requisition.Width < w)
 
141
                                requisition.Width = w;
 
142
                        if (requisition.Height < h)
 
143
                                requisition.Height = h;
 
144
                        foreach (var cr in children)
 
145
                                cr.Key.SizeRequest ();
 
146
                }
 
147
                
 
148
                protected override void OnUnrealized ()
 
149
                {
 
150
                        base.OnUnrealized ();
 
151
                        lastAllocation = new Gdk.Rectangle ();
 
152
                }
 
153
                
 
154
                Gdk.Rectangle lastAllocation;
 
155
                
 
156
                protected override void OnSizeAllocated (Gdk.Rectangle allocation)
 
157
                {
 
158
                        base.OnSizeAllocated (allocation);
 
159
                        if (!lastAllocation.Equals (allocation))
 
160
                                ((IWidgetSurface)Frontend).Reallocate ();
 
161
                        lastAllocation = allocation;
 
162
                        foreach (var cr in children) {
 
163
                                var r = cr.Value;
 
164
                                cr.Key.SizeAllocate (new Gdk.Rectangle ((int)r.X, (int)r.Y, (int)r.Width, (int)r.Height));
 
165
                        }
 
166
                }
 
167
                
 
168
                protected override void ForAll (bool includeInternals, Gtk.Callback callback)
 
169
                {
 
170
                        base.ForAll (includeInternals, callback);
 
171
                        foreach (var c in children.Keys.ToArray ())
 
172
                                callback (c);
 
173
                }
 
174
                
 
175
                protected override bool OnExposeEvent (Gdk.EventExpose evnt)
 
176
                {
 
177
                        Toolkit.Invoke (delegate {
 
178
                                using (var context = CreateContext ()) {
 
179
                                        var a = evnt.Area;
 
180
                                        EventSink.OnDraw (context, new Rectangle (a.X, a.Y, a.Width, a.Height));
 
181
                                }
 
182
                        });
 
183
                        return base.OnExposeEvent (evnt);
 
184
                }
 
185
                
 
186
                public CairoContextBackend CreateContext ()
 
187
                {
 
188
                        CairoContextBackend ctx = new CairoContextBackend ();
 
189
                        if (!IsRealized) {
 
190
                                Cairo.Surface sf = new Cairo.ImageSurface (Cairo.Format.ARGB32, 1, 1);
 
191
                                Cairo.Context c = new Cairo.Context (sf);
 
192
                                ctx.Context = c;
 
193
                                ctx.TempSurface = sf;
 
194
                        } else {
 
195
                                ctx.Context = Gdk.CairoHelper.Create (GdkWindow);
 
196
                        }
 
197
                        return ctx;
 
198
                }
 
199
        }
 
200
}
 
201