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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.WPF/Xwt.WPFBackend/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
//       Eric Maupin <ermau@xamarin.com>
 
6
//
 
7
// Copyright (c) 2012 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 System.Drawing;
 
30
using System.Windows;
 
31
using System.Windows.Media;
 
32
using System.Windows.Media.Imaging;
 
33
using Xwt.Engine;
 
34
using SWC = System.Windows.Controls;
 
35
using Xwt.Backends;
 
36
using PixelFormat = System.Drawing.Imaging.PixelFormat;
 
37
 
 
38
namespace Xwt.WPFBackend
 
39
{
 
40
        public class CanvasBackend
 
41
                : WidgetBackend, ICanvasBackend
 
42
        {
 
43
                public CanvasBackend ()
 
44
                {
 
45
                        Canvas = new ExCanvas();
 
46
                        Canvas.Render += OnRender;
 
47
 
 
48
                        this.image = new SWC.Image();
 
49
                        SWC.Panel.SetZIndex (this.image, -1);
 
50
 
 
51
                        Canvas.Children.Add (this.image);
 
52
                }
 
53
 
 
54
                public void QueueDraw()
 
55
                {
 
56
                        this.fullRedraw = true;
 
57
 
 
58
                        if (!this.queued) {
 
59
                            Toolkit.QueueExitAction (Render);
 
60
                            this.queued = true;
 
61
                        }
 
62
                }
 
63
 
 
64
                public void QueueDraw (Rectangle rect)
 
65
                {
 
66
                        if (this.fullRedraw)
 
67
                                return;
 
68
 
 
69
                        this.dirtyRects.Add (rect.ToInt32Rect());
 
70
 
 
71
                        if (!this.queued) {
 
72
                            Toolkit.QueueExitAction (Render);
 
73
                            this.queued = true;
 
74
                        }
 
75
                }
 
76
 
 
77
                public void AddChild (IWidgetBackend widget, Rectangle bounds)
 
78
                {
 
79
                        UIElement element = widget.NativeWidget as UIElement;
 
80
                        if (element == null)
 
81
                                throw new ArgumentException();
 
82
 
 
83
                        if (!Canvas.Children.Contains (element))
 
84
                                Canvas.Children.Add (element);
 
85
 
 
86
                        SetChildBounds (widget, bounds);
 
87
                }
 
88
 
 
89
                public void SetChildBounds (IWidgetBackend widget, Rectangle bounds)
 
90
                {
 
91
                        FrameworkElement element = widget.NativeWidget as FrameworkElement;
 
92
                        if (element == null)
 
93
                                throw new ArgumentException();
 
94
 
 
95
                        double hratio = HeightPixelRatio;
 
96
                        double wratio = WidthPixelRatio;
 
97
 
 
98
                        SWC.Canvas.SetTop (element, bounds.Top * hratio);
 
99
                        SWC.Canvas.SetLeft (element, bounds.Left * wratio);
 
100
 
 
101
                        // We substract the widget margin here because the size we are assigning is the actual size, not including the WPF marings
 
102
                        var h = bounds.Height - ((WidgetBackend)widget).Frontend.Margin.VerticalSpacing;
 
103
                        var w = bounds.Width - ((WidgetBackend)widget).Frontend.Margin.HorizontalSpacing;
 
104
 
 
105
                        element.Height = (h > 0) ? h * hratio : 0;
 
106
                        element.Width = (w > 0) ? w * wratio : 0;
 
107
 
 
108
                        ((FrameworkElement) widget.NativeWidget).UpdateLayout();
 
109
                }
 
110
 
 
111
                public void RemoveChild (IWidgetBackend widget)
 
112
                {
 
113
                        UIElement element = widget.NativeWidget as UIElement;
 
114
                        if (element == null)
 
115
                                throw new ArgumentException();
 
116
 
 
117
                        Canvas.Children.Remove (element);
 
118
                }
 
119
 
 
120
                private bool queued;
 
121
                private bool fullRedraw;
 
122
 
 
123
                private readonly SWC.Image image;
 
124
                private WriteableBitmap wbitmap;
 
125
                private Bitmap bbitmap;
 
126
                private readonly List<Int32Rect> dirtyRects = new List<Int32Rect> ();
 
127
 
 
128
                private double pwidth = -1;
 
129
                private double pheight = -1;
 
130
 
 
131
                private ExCanvas Canvas
 
132
                {
 
133
                        get { return (ExCanvas) Widget; }
 
134
                        set { Widget = value; }
 
135
                }
 
136
 
 
137
                private ICanvasEventSink CanvasEventSink
 
138
                {
 
139
                        get { return (ICanvasEventSink) EventSink; }
 
140
                }
 
141
 
 
142
                private void OnRender (object sender, EventArgs e)
 
143
                {
 
144
                        Render();
 
145
                }
 
146
 
 
147
                private void Render()
 
148
                {
 
149
                        this.queued = false;
 
150
 
 
151
                        if (!Widget.IsVisible)
 
152
                                return;
 
153
 
 
154
                        if (Canvas.ActualHeight != this.pheight || Canvas.ActualWidth != this.pwidth)
 
155
                        {
 
156
                                double heightRatio = HeightPixelRatio;
 
157
                                double widthRatio = WidthPixelRatio;
 
158
 
 
159
                                int nheight = (int) Math.Round (Canvas.ActualHeight * heightRatio) + 1;
 
160
                                int nwidth = (int) Math.Round (Canvas.ActualWidth * widthRatio) + 1;
 
161
 
 
162
                                if (nheight == 0 || nwidth == 0)
 
163
                                        return;
 
164
 
 
165
                                this.wbitmap = new WriteableBitmap (nwidth, nheight, widthRatio * 96, heightRatio * 96, PixelFormats.Pbgra32, null);
 
166
                                this.bbitmap = new Bitmap (nwidth, nheight, this.wbitmap.BackBufferStride, PixelFormat.Format32bppPArgb, this.wbitmap.BackBuffer);
 
167
                                this.image.Source = this.wbitmap;
 
168
 
 
169
                                this.pheight = Canvas.ActualHeight;
 
170
                                this.pwidth = Canvas.ActualWidth;
 
171
                        }
 
172
 
 
173
                        this.wbitmap.Lock();
 
174
 
 
175
                        using (Graphics g = Graphics.FromImage (this.bbitmap))
 
176
                                CanvasEventSink.OnDraw (new DrawingContext (g), new Rectangle (0, 0, this.wbitmap.PixelWidth, this.wbitmap.PixelHeight));
 
177
 
 
178
                        if (this.fullRedraw || this.dirtyRects.Count == 0) {
 
179
                                this.wbitmap.AddDirtyRect (new Int32Rect (0, 0, this.wbitmap.PixelWidth, this.wbitmap.PixelHeight));
 
180
                                this.fullRedraw = false;
 
181
                        } else {
 
182
                                for (int i = 0; i < this.dirtyRects.Count; ++i) {
 
183
                                        Int32Rect r = this.dirtyRects [i];
 
184
                                        if (r.X >= this.wbitmap.PixelWidth || r.Y >= this.wbitmap.PixelHeight)
 
185
                                                continue;
 
186
 
 
187
                                        if (r.X < 0)
 
188
                                                r.X = 0;
 
189
                                        if (r.Y < 0)
 
190
                                                r.Y = 0;
 
191
                                        if (r.X + r.Width > this.wbitmap.PixelWidth)
 
192
                                                r.Width = this.wbitmap.PixelWidth - r.X;
 
193
                                        if (r.Y + r.Height > this.wbitmap.PixelHeight)
 
194
                                                r.Height = this.wbitmap.PixelHeight - r.Y;
 
195
 
 
196
                                        this.wbitmap.AddDirtyRect (r);
 
197
                                }
 
198
                        }
 
199
 
 
200
                        this.dirtyRects.Clear();
 
201
                        this.wbitmap.Unlock();
 
202
                }
 
203
        }
 
204
}
 
 
b'\\ No newline at end of file'