5
// Lluis Sanchez <lluis@xamarin.com>
7
// Copyright (c) 2011 Xamarin Inc
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
30
using MonoMac.CoreGraphics;
35
public class CanvasBackend: ViewBackend<NSView,ICanvasEventSink>, ICanvasBackend
39
public CanvasBackend ()
43
public override void Initialize ()
45
view = new CanvasView (EventSink);
49
public Rectangle Bounds {
51
return new Rectangle (0, 0, view.Frame.Width, view.Frame.Height);
55
public void QueueDraw ()
57
view.NeedsDisplay = true;
60
public void QueueDraw (Rectangle rect)
62
view.NeedsToDraw (new System.Drawing.RectangleF ((float)rect.X, (float)rect.Y, (float)rect.Width, (float)rect.Height));
65
public void AddChild (IWidgetBackend widget, Rectangle rect)
67
var v = GetWidget (widget);
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;
75
public void RemoveChild (IWidgetBackend widget)
77
var v = GetWidget (widget);
78
v.RemoveFromSuperview ();
81
public void SetChildBounds (IWidgetBackend widget, Rectangle rect)
83
var w = GetWidget (widget);
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;
91
class CanvasView: NSView, IViewObject
93
ICanvasEventSink eventSink;
95
public CanvasView (ICanvasEventSink eventSink)
97
this.eventSink = eventSink;
100
public Widget Frontend { get; set; }
106
public override bool IsFlipped {
112
public override void DrawRect (System.Drawing.RectangleF dirtyRect)
114
Toolkit.Invoke (delegate {
115
CGContext ctx = NSGraphicsContext.CurrentContext.GraphicsPort;
117
//fill BackgroundColor
118
ctx.SetFillColor (Frontend.BackgroundColor.ToCGColor ());
119
ctx.FillRect (Bounds);
121
var backend = new CGContextBackend {
123
InverseViewTransform = ctx.GetCTM ().Invert ()
125
eventSink.OnDraw (backend, new Rectangle (dirtyRect.X, dirtyRect.Y, dirtyRect.Width, dirtyRect.Height));
129
public override void RightMouseDown (NSEvent theEvent)
131
var p = ConvertPointFromView (theEvent.LocationInWindow, null);
132
ButtonEventArgs args = new ButtonEventArgs ();
135
args.Button = PointerButton.Right;
136
Toolkit.Invoke (delegate {
137
eventSink.OnButtonPressed (args);
141
public override void RightMouseUp (NSEvent theEvent)
143
var p = ConvertPointFromView (theEvent.LocationInWindow, null);
144
ButtonEventArgs args = new ButtonEventArgs ();
147
args.Button = PointerButton.Right;
148
Toolkit.Invoke (delegate {
149
eventSink.OnButtonReleased (args);
153
public override void MouseDown (NSEvent theEvent)
155
var p = ConvertPointFromView (theEvent.LocationInWindow, null);
156
ButtonEventArgs args = new ButtonEventArgs ();
159
args.Button = PointerButton.Left;
160
Toolkit.Invoke (delegate {
161
eventSink.OnButtonPressed (args);
165
public override void MouseUp (NSEvent theEvent)
167
var p = ConvertPointFromView (theEvent.LocationInWindow, null);
168
ButtonEventArgs args = new ButtonEventArgs ();
171
args.Button = (PointerButton) theEvent.ButtonNumber + 1;
172
Toolkit.Invoke (delegate {
173
eventSink.OnButtonReleased (args);
177
public override void MouseMoved (NSEvent theEvent)
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);
186
public override void MouseDragged (NSEvent theEvent)
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);
195
public override void SetFrameSize (System.Drawing.SizeF newSize)
197
base.SetFrameSize (newSize);
198
Toolkit.Invoke (delegate {
199
eventSink.OnBoundsChanged ();