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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Mac/Xwt.Mac/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 Xwt.Backends;
 
29
using MonoMac.AppKit;
 
30
using MonoMac.CoreGraphics;
 
31
using Xwt.Engine;
 
32
 
 
33
namespace Xwt.Mac
 
34
{
 
35
        public class CanvasBackend: ViewBackend<NSView,ICanvasEventSink>, ICanvasBackend
 
36
        {
 
37
                CanvasView view;
 
38
                
 
39
                public CanvasBackend ()
 
40
                {
 
41
                }
 
42
 
 
43
                public override void Initialize ()
 
44
                {
 
45
                        view = new CanvasView (EventSink);
 
46
                        ViewObject = view;
 
47
                }
 
48
                
 
49
                public Rectangle Bounds {
 
50
                        get {
 
51
                                return new Rectangle (0, 0, view.Frame.Width, view.Frame.Height);
 
52
                        }
 
53
                }
 
54
                
 
55
                public void QueueDraw ()
 
56
                {
 
57
                        view.NeedsDisplay = true;
 
58
                }
 
59
                
 
60
                public void QueueDraw (Rectangle rect)
 
61
                {
 
62
                        view.NeedsToDraw (new System.Drawing.RectangleF ((float)rect.X, (float)rect.Y, (float)rect.Width, (float)rect.Height));
 
63
                }
 
64
                
 
65
                public void AddChild (IWidgetBackend widget, Rectangle rect)
 
66
                {
 
67
                        var v = GetWidget (widget);
 
68
                        view.AddSubview (v);
 
69
                        
 
70
                        // Not using SetWidgetBounds because the view is flipped
 
71
                        v.Frame = new System.Drawing.RectangleF ((float)rect.X, (float)rect.Y, (float)rect.Width, (float)rect.Height);;
 
72
                        v.NeedsDisplay = true;
 
73
                }
 
74
                
 
75
                public void RemoveChild (IWidgetBackend widget)
 
76
                {
 
77
                        var v = GetWidget (widget);
 
78
                        v.RemoveFromSuperview ();
 
79
                }
 
80
                
 
81
                public void SetChildBounds (IWidgetBackend widget, Rectangle rect)
 
82
                {
 
83
                        var w = GetWidget (widget);
 
84
                        
 
85
                        // Not using SetWidgetBounds because the view is flipped
 
86
                        w.Frame = new System.Drawing.RectangleF ((float)rect.X, (float)rect.Y, (float)rect.Width, (float)rect.Height);;
 
87
                        w.NeedsDisplay = true;
 
88
                }
 
89
        }
 
90
        
 
91
        class CanvasView: NSView, IViewObject
 
92
        {
 
93
                ICanvasEventSink eventSink;
 
94
                
 
95
                public CanvasView (ICanvasEventSink eventSink)
 
96
                {
 
97
                        this.eventSink = eventSink;
 
98
                }
 
99
                
 
100
                public Widget Frontend { get; set; }
 
101
                
 
102
                public NSView View {
 
103
                        get { return this; }
 
104
                }
 
105
                
 
106
                public override bool IsFlipped {
 
107
                        get {
 
108
                                return true;
 
109
                        }
 
110
                }
 
111
 
 
112
                public override void DrawRect (System.Drawing.RectangleF dirtyRect)
 
113
                {
 
114
                        Toolkit.Invoke (delegate {
 
115
                                CGContext ctx = NSGraphicsContext.CurrentContext.GraphicsPort;
 
116
 
 
117
                                //fill BackgroundColor
 
118
                                ctx.SetFillColor (Frontend.BackgroundColor.ToCGColor ());
 
119
                                ctx.FillRect (Bounds);
 
120
 
 
121
                                var backend = new CGContextBackend {
 
122
                                        Context = ctx,
 
123
                                        InverseViewTransform = ctx.GetCTM ().Invert ()
 
124
                                };
 
125
                                eventSink.OnDraw (backend, new Rectangle (dirtyRect.X, dirtyRect.Y, dirtyRect.Width, dirtyRect.Height));
 
126
                        });
 
127
                }
 
128
                
 
129
                public override void RightMouseDown (NSEvent theEvent)
 
130
                {
 
131
                        var p = ConvertPointFromView (theEvent.LocationInWindow, null);
 
132
                        ButtonEventArgs args = new ButtonEventArgs ();
 
133
                        args.X = p.X;
 
134
                        args.Y = p.Y;
 
135
                        args.Button = PointerButton.Right;
 
136
                        Toolkit.Invoke (delegate {
 
137
                                eventSink.OnButtonPressed (args);
 
138
                        });
 
139
                }
 
140
                
 
141
                public override void RightMouseUp (NSEvent theEvent)
 
142
                {
 
143
                        var p = ConvertPointFromView (theEvent.LocationInWindow, null);
 
144
                        ButtonEventArgs args = new ButtonEventArgs ();
 
145
                        args.X = p.X;
 
146
                        args.Y = p.Y;
 
147
                        args.Button = PointerButton.Right;
 
148
                        Toolkit.Invoke (delegate {
 
149
                                eventSink.OnButtonReleased (args);
 
150
                        });
 
151
                }
 
152
                
 
153
                public override void MouseDown (NSEvent theEvent)
 
154
                {
 
155
                        var p = ConvertPointFromView (theEvent.LocationInWindow, null);
 
156
                        ButtonEventArgs args = new ButtonEventArgs ();
 
157
                        args.X = p.X;
 
158
                        args.Y = p.Y;
 
159
                        args.Button = PointerButton.Left;
 
160
                        Toolkit.Invoke (delegate {
 
161
                                eventSink.OnButtonPressed (args);
 
162
                        });
 
163
                }
 
164
                
 
165
                public override void MouseUp (NSEvent theEvent)
 
166
                {
 
167
                        var p = ConvertPointFromView (theEvent.LocationInWindow, null);
 
168
                        ButtonEventArgs args = new ButtonEventArgs ();
 
169
                        args.X = p.X;
 
170
                        args.Y = p.Y;
 
171
                        args.Button = (PointerButton) theEvent.ButtonNumber + 1;
 
172
                        Toolkit.Invoke (delegate {
 
173
                                eventSink.OnButtonReleased (args);
 
174
                        });
 
175
                }
 
176
                
 
177
                public override void MouseMoved (NSEvent theEvent)
 
178
                {
 
179
                        var p = ConvertPointFromView (theEvent.LocationInWindow, null);
 
180
                        MouseMovedEventArgs args = new MouseMovedEventArgs ((long) TimeSpan.FromSeconds (theEvent.Timestamp).TotalMilliseconds, p.X, p.Y);
 
181
                        Toolkit.Invoke (delegate {
 
182
                                eventSink.OnMouseMoved (args);
 
183
                        });
 
184
                }
 
185
                
 
186
                public override void MouseDragged (NSEvent theEvent)
 
187
                {
 
188
                        var p = ConvertPointFromView (theEvent.LocationInWindow, null);
 
189
                        MouseMovedEventArgs args = new MouseMovedEventArgs ((long) TimeSpan.FromSeconds (theEvent.Timestamp).TotalMilliseconds, p.X, p.Y);
 
190
                        Toolkit.Invoke (delegate {
 
191
                                eventSink.OnMouseMoved (args);
 
192
                        });
 
193
                }
 
194
                
 
195
                public override void SetFrameSize (System.Drawing.SizeF newSize)
 
196
                {
 
197
                        base.SetFrameSize (newSize);
 
198
                        Toolkit.Invoke (delegate {
 
199
                                eventSink.OnBoundsChanged ();
 
200
                        });
 
201
                }
 
202
        }
 
203
}
 
204