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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Mac/Xwt.Mac/ViewBackend.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
// ViewBackend.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 System.Drawing;
 
30
using System.IO;
 
31
using System.Linq;
 
32
using System.Xml;
 
33
using MonoMac.AppKit;
 
34
using MonoMac.Foundation;
 
35
using MonoMac.ObjCRuntime;
 
36
using Xwt;
 
37
using Xwt.Backends;
 
38
using Xwt.Engine;
 
39
 
 
40
namespace Xwt.Mac
 
41
{
 
42
        public abstract class ViewBackend<T,S>: IWidgetBackend,IMacViewBackend where T:NSView where S:IWidgetEventSink
 
43
        {
 
44
                Widget frontend;
 
45
                S eventSink;
 
46
                IViewObject viewObject;
 
47
                WidgetEvent currentEvents;
 
48
                bool autosize;
 
49
                
 
50
                void IBackend.InitializeBackend (object frontend)
 
51
                {
 
52
                        this.frontend = (Widget) frontend;
 
53
                        if (viewObject != null)
 
54
                                viewObject.Frontend = (Widget) frontend;
 
55
                }
 
56
                
 
57
                void IWidgetBackend.Initialize (IWidgetEventSink sink)
 
58
                {
 
59
                        eventSink = (S) sink;
 
60
                        Initialize ();
 
61
                }
 
62
 
 
63
                public void SetAutosizeMode (bool autosize)
 
64
                {
 
65
                        this.autosize = autosize;
 
66
                        if (autosize)
 
67
                                AutoUpdateSize ();
 
68
                }
 
69
 
 
70
                
 
71
                public virtual void Initialize ()
 
72
                {
 
73
                }
 
74
                
 
75
                public S EventSink {
 
76
                        get { return eventSink; }
 
77
                }
 
78
                
 
79
                IWidgetEventSink IMacViewBackend.EventSink {
 
80
                        get { return EventSink; }
 
81
                }
 
82
 
 
83
                public Widget Frontend {
 
84
                        get {
 
85
                                return this.frontend;
 
86
                        }
 
87
                }
 
88
                
 
89
                public object NativeWidget {
 
90
                        get {
 
91
                                return Widget;
 
92
                        }
 
93
                }
 
94
                
 
95
                public T Widget {
 
96
                        get { return (T) ViewObject.View; }
 
97
                }
 
98
                
 
99
                public IViewObject ViewObject {
 
100
                        get { return viewObject; }
 
101
                        set {
 
102
                                viewObject = value;
 
103
                                viewObject.Frontend = frontend;
 
104
                        }
 
105
                }
 
106
                
 
107
                public bool Visible {
 
108
                        get { return !Widget.Hidden; }
 
109
                        set { Widget.Hidden = !value; }
 
110
                }
 
111
                
 
112
                public virtual bool Sensitive {
 
113
                        get { return true; }
 
114
                        set { }
 
115
                }
 
116
                
 
117
                public virtual bool CanGetFocus {
 
118
                        get { return true; }
 
119
                        set { }
 
120
                }
 
121
                
 
122
                public virtual bool HasFocus {
 
123
                        get { return false; }
 
124
                }
 
125
                
 
126
                public void SetFocus ()
 
127
                {
 
128
                }
 
129
                
 
130
                public string TooltipText {
 
131
                        get {
 
132
                                return Widget.ToolTip;
 
133
                        }
 
134
                        set {
 
135
                                Widget.ToolTip = value;
 
136
                        }
 
137
                }
 
138
                
 
139
                public void NotifyPreferredSizeChanged ()
 
140
                {
 
141
                        EventSink.OnPreferredSizeChanged ();
 
142
                }
 
143
                
 
144
                public void SetCursor (CursorType cursor)
 
145
                {
 
146
                        NSCursor ctype;
 
147
                        if (cursor == CursorType.Arrow)
 
148
                                ctype = NSCursor.ArrowCursor;
 
149
                        else if (cursor == CursorType.Crosshair)
 
150
                                ctype = NSCursor.CrosshairCursor;
 
151
                        else if (cursor == CursorType.Hand)
 
152
                                ctype = NSCursor.ClosedHandCursor;
 
153
                        else if (cursor == CursorType.IBeam)
 
154
                                ctype = NSCursor.IBeamCursor;
 
155
                        else if (cursor == CursorType.ResizeDown)
 
156
                                ctype = NSCursor.ResizeDownCursor;
 
157
                        else if (cursor == CursorType.ResizeUp)
 
158
                                ctype = NSCursor.ResizeUpCursor;
 
159
                        else if (cursor == CursorType.ResizeLeft)
 
160
                                ctype = NSCursor.ResizeLeftCursor;
 
161
                        else if (cursor == CursorType.ResizeRight)
 
162
                                ctype = NSCursor.ResizeRightCursor;
 
163
                        else if (cursor == CursorType.ResizeLeftRight)
 
164
                                ctype = NSCursor.ResizeLeftRightCursor;
 
165
                        else if (cursor == CursorType.ResizeUpDown)
 
166
                                ctype = NSCursor.ResizeUpDownCursor;
 
167
                        else
 
168
                                ctype = NSCursor.ArrowCursor;
 
169
                        // TODO: assign the cursor
 
170
                }
 
171
                
 
172
                ~ViewBackend ()
 
173
                {
 
174
                        Dispose (false);
 
175
                }
 
176
                
 
177
                public void Dispose ()
 
178
                {
 
179
                        GC.SuppressFinalize (this);
 
180
                        Dispose (true);
 
181
                }
 
182
                
 
183
                protected virtual void Dispose (bool disposing)
 
184
                {
 
185
                }
 
186
                
 
187
                public virtual SizeRequestMode SizeRequestMode {
 
188
                        get { return SizeRequestMode.HeightForWidth; }
 
189
                }
 
190
                
 
191
                Size IWidgetBackend.Size {
 
192
                        get { return new Size (Widget.WidgetWidth (), Widget.WidgetHeight ()); }
 
193
                }
 
194
                
 
195
                NSView IMacViewBackend.View {
 
196
                        get { return (NSView) Widget; }
 
197
                }
 
198
                
 
199
                public static NSView GetWidget (IWidgetBackend w)
 
200
                {
 
201
                        return ((IMacViewBackend)w).View;
 
202
                }
 
203
 
 
204
                public static NSView GetWidget (Widget w)
 
205
                {
 
206
                        return GetWidget ((IWidgetBackend)WidgetRegistry.GetBackend (w));
 
207
                }
 
208
                
 
209
                public virtual object Font {
 
210
                        get {
 
211
                                if (Widget is NSControl)
 
212
                                        return ((NSControl)(object)Widget).Font;
 
213
                                if (Widget is NSText)
 
214
                                        return ((NSText)(object)Widget).Font;
 
215
                                return NSFont.ControlContentFontOfSize (NSFont.SystemFontSize);
 
216
                        }
 
217
                        set {
 
218
                                if (Widget is NSControl)
 
219
                                        ((NSControl)(object)Widget).Font = (NSFont) value;
 
220
                                if (Widget is NSText)
 
221
                                        ((NSText)(object)Widget).Font = (NSFont) value;
 
222
                        }
 
223
                }
 
224
                
 
225
                public virtual Xwt.Drawing.Color BackgroundColor {
 
226
                        get;
 
227
                        set;
 
228
                }
 
229
                
 
230
                #region IWidgetBackend implementation
 
231
                
 
232
                public Point ConvertToScreenCoordinates (Point widgetCoordinates)
 
233
                {
 
234
                        var lo = Widget.ConvertPointToBase (new PointF ((float)widgetCoordinates.X, (float)widgetCoordinates.Y));
 
235
                        lo = Widget.Window.ConvertBaseToScreen (lo);
 
236
                        return new Point (lo.X, lo.Y);
 
237
                }
 
238
                
 
239
                protected virtual Size GetNaturalSize ()
 
240
                {
 
241
//                      double w1 = Widget.FittingSize.Width;
 
242
                        return new Size (Widget.WidgetWidth(), Widget.WidgetHeight ());
 
243
                }
 
244
 
 
245
                public WidgetSize GetPreferredWidth ()
 
246
                {
 
247
                        var w = GetNaturalSize ().Width;
 
248
                        var s = new Xwt.WidgetSize (w, w);
 
249
                        if (minWidth != -1 && s.MinSize > minWidth)
 
250
                                s.MinSize = minWidth;
 
251
                        return s;
 
252
                }
 
253
                
 
254
                public WidgetSize GetPreferredHeight ()
 
255
                {
 
256
                        var h = GetNaturalSize ().Height;
 
257
                        var s = new Xwt.WidgetSize (h, h);
 
258
                        if (minHeight != -1 && s.MinSize > minHeight)
 
259
                                s.MinSize = minHeight;
 
260
                        return s;
 
261
                }
 
262
 
 
263
                public WidgetSize GetPreferredHeightForWidth (double width)
 
264
                {
 
265
                        return GetPreferredHeight ();
 
266
                }
 
267
 
 
268
                public WidgetSize GetPreferredWidthForHeight (double height)
 
269
                {
 
270
                        return GetPreferredWidth ();
 
271
                }
 
272
                
 
273
                double minWidth = -1, minHeight = -1;
 
274
                
 
275
                public void SetMinSize (double width, double height)
 
276
                {
 
277
                        minWidth = width;
 
278
                        minHeight = height;
 
279
                }
 
280
                
 
281
                public void SetNaturalSize (double width, double height)
 
282
                {
 
283
                        // Nothing to do
 
284
                }
 
285
                
 
286
                public virtual void UpdateLayout ()
 
287
                {
 
288
                        var m = Frontend.Margin;
 
289
                        Widget.SetBoundsOrigin (new PointF (-(float)m.Left, -(float)m.Top));
 
290
                        if (autosize)
 
291
                                AutoUpdateSize ();
 
292
                }
 
293
 
 
294
                void AutoUpdateSize ()
 
295
                {
 
296
                        var ws = Frontend.Surface.GetPreferredWidth ();
 
297
                        var h = Frontend.Surface.GetPreferredHeightForWidth (ws.NaturalSize);
 
298
                        Widget.SetWidgetBounds (new Rectangle (0, 0, ws.NaturalSize, h.NaturalSize));
 
299
                }
 
300
                
 
301
                public virtual void EnableEvent (object eventId)
 
302
                {
 
303
                        if (eventId is WidgetEvent) {
 
304
                                WidgetEvent ev = (WidgetEvent) eventId;
 
305
                                currentEvents |= ev;
 
306
                        }
 
307
                }
 
308
                
 
309
                public virtual void DisableEvent (object eventId)
 
310
                {
 
311
                        if (eventId is WidgetEvent) {
 
312
                                WidgetEvent ev = (WidgetEvent) eventId;
 
313
                                currentEvents &= ~ev;
 
314
                        }
 
315
                }
 
316
                
 
317
                static Selector draggingEnteredSel = new Selector ("draggingEntered:");
 
318
                static Selector draggingUpdatedSel = new Selector ("draggingUpdated:");
 
319
                static Selector draggingExitedSel = new Selector ("draggingExited:");
 
320
                static Selector prepareForDragOperationSel = new Selector ("prepareForDragOperation:");
 
321
                static Selector performDragOperationSel = new Selector ("performDragOperation:");
 
322
                static Selector concludeDragOperationSel = new Selector ("concludeDragOperation:");
 
323
                static HashSet<Type> typesConfiguredForDragDrop = new HashSet<Type> ();
 
324
                
 
325
                static void SetupForDragDrop (Type type)
 
326
                {
 
327
                        lock (typesConfiguredForDragDrop) {
 
328
                                if (typesConfiguredForDragDrop.Add (type)) {
 
329
                                        Class c = new Class (type);
 
330
                                        c.AddMethod (draggingEnteredSel.Handle, new Func<IntPtr,IntPtr,IntPtr,NSDragOperation> (DraggingEntered), "i@:@");
 
331
                                        c.AddMethod (draggingUpdatedSel.Handle, new Func<IntPtr,IntPtr,IntPtr,NSDragOperation> (DraggingUpdated), "i@:@");
 
332
                                        c.AddMethod (draggingExitedSel.Handle, new Action<IntPtr,IntPtr,IntPtr> (DraggingExited), "v@:@");
 
333
                                        c.AddMethod (prepareForDragOperationSel.Handle, new Func<IntPtr,IntPtr,IntPtr,bool> (PrepareForDragOperation), "B@:@");
 
334
                                        c.AddMethod (performDragOperationSel.Handle, new Func<IntPtr,IntPtr,IntPtr,bool> (PerformDragOperation), "B@:@");
 
335
                                        c.AddMethod (concludeDragOperationSel.Handle, new Action<IntPtr,IntPtr,IntPtr> (ConcludeDragOperation), "v@:@");
 
336
                                }
 
337
                        }
 
338
                }
 
339
                
 
340
                public void DragStart (DragStartData sdata)
 
341
                {
 
342
                        var lo = Widget.ConvertPointToBase (new PointF (Widget.Bounds.X, Widget.Bounds.Y));
 
343
                        lo = Widget.Window.ConvertBaseToScreen (lo);
 
344
                        var ml = NSEvent.CurrentMouseLocation;
 
345
                        var pb = NSPasteboard.FromName (NSPasteboard.NSDragPasteboardName);
 
346
                        if (pb == null)
 
347
                                throw new InvalidOperationException ("Could not get pasteboard");
 
348
                        if (sdata.Data == null)
 
349
                                throw new ArgumentNullException ("data");
 
350
                        InitPasteboard (pb, sdata.Data);
 
351
                        var img = (NSImage)sdata.ImageBackend;
 
352
                        var pos = new PointF (ml.X - lo.X - (float)sdata.HotX, lo.Y - ml.Y - (float)sdata.HotY + img.Size.Height);
 
353
                        Widget.DragImage (img, pos, new SizeF (0, 0), NSApplication.SharedApplication.CurrentEvent, pb, Widget, true);
 
354
                }
 
355
                
 
356
                public void SetDragSource (TransferDataType[] types, DragDropAction dragAction)
 
357
                {
 
358
                }
 
359
                
 
360
                public void SetDragTarget (TransferDataType[] types, DragDropAction dragAction)
 
361
                {
 
362
                        SetupForDragDrop (Widget.GetType ());
 
363
                        var dtypes = types.Select (t => ToNSDragType (t)).ToArray ();
 
364
                        Widget.RegisterForDraggedTypes (dtypes);
 
365
                }
 
366
                
 
367
                static NSDragOperation DraggingEntered (IntPtr sender, IntPtr sel, IntPtr dragInfo)
 
368
                {
 
369
                        return DraggingUpdated (sender, sel, dragInfo);
 
370
                }
 
371
                
 
372
                static NSDragOperation DraggingUpdated (IntPtr sender, IntPtr sel, IntPtr dragInfo)
 
373
                {
 
374
                        IViewObject ob = Runtime.GetNSObject (sender) as IViewObject;
 
375
                        if (ob == null)
 
376
                                return NSDragOperation.None;
 
377
                        var backend = (ViewBackend<T,S>) WidgetRegistry.GetBackend (ob.Frontend);
 
378
                        
 
379
                        NSDraggingInfo di = new NSDraggingInfo (dragInfo);
 
380
                        var types = di.DraggingPasteboard.Types.Select (t => ToXwtDragType (t)).ToArray ();
 
381
                        var pos = new Point (di.DraggingLocation.X, di.DraggingLocation.Y);
 
382
                        
 
383
                        if ((backend.currentEvents & WidgetEvent.DragOverCheck) != 0) {
 
384
                                var args = new DragOverCheckEventArgs (pos, types, ConvertAction (di.DraggingSourceOperationMask));
 
385
                                backend.OnDragOverCheck (di, args);
 
386
                                if (args.AllowedAction == DragDropAction.None)
 
387
                                        return NSDragOperation.None;
 
388
                                if (args.AllowedAction != DragDropAction.Default)
 
389
                                        return ConvertAction (args.AllowedAction);
 
390
                        }
 
391
                        
 
392
                        if ((backend.currentEvents & WidgetEvent.DragOver) != 0) {
 
393
                                TransferDataStore store = new TransferDataStore ();
 
394
                                FillDataStore (store, di.DraggingPasteboard, ob.View.RegisteredDragTypes ());
 
395
                                var args = new DragOverEventArgs (pos, store, ConvertAction (di.DraggingSourceOperationMask));
 
396
                                backend.OnDragOver (di, args);
 
397
                                if (args.AllowedAction == DragDropAction.None)
 
398
                                        return NSDragOperation.None;
 
399
                                if (args.AllowedAction != DragDropAction.Default)
 
400
                                        return ConvertAction (args.AllowedAction);
 
401
                        }
 
402
                        
 
403
                        return di.DraggingSourceOperationMask;
 
404
                }
 
405
                
 
406
                static void DraggingExited (IntPtr sender, IntPtr sel, IntPtr dragInfo)
 
407
                {
 
408
                        IViewObject ob = Runtime.GetNSObject (sender) as IViewObject;
 
409
                        if (ob != null) {
 
410
                                var backend = (ViewBackend<T,S>) WidgetRegistry.GetBackend (ob.Frontend);
 
411
                                Toolkit.Invoke (delegate {
 
412
                                        backend.eventSink.OnDragLeave (EventArgs.Empty);
 
413
                                });
 
414
                        }
 
415
                }
 
416
                
 
417
                static bool PrepareForDragOperation (IntPtr sender, IntPtr sel, IntPtr dragInfo)
 
418
                {
 
419
                        IViewObject ob = Runtime.GetNSObject (sender) as IViewObject;
 
420
                        if (ob == null)
 
421
                                return false;
 
422
                        
 
423
                        var backend = (ViewBackend<T,S>) WidgetRegistry.GetBackend (ob.Frontend);
 
424
                        
 
425
                        NSDraggingInfo di = new NSDraggingInfo (dragInfo);
 
426
                        var types = di.DraggingPasteboard.Types.Select (t => ToXwtDragType (t)).ToArray ();
 
427
                        var pos = new Point (di.DraggingLocation.X, di.DraggingLocation.Y);
 
428
                        
 
429
                        if ((backend.currentEvents & WidgetEvent.DragDropCheck) != 0) {
 
430
                                var args = new DragCheckEventArgs (pos, types, ConvertAction (di.DraggingSourceOperationMask));
 
431
                                bool res = Toolkit.Invoke (delegate {
 
432
                                        backend.eventSink.OnDragDropCheck (args);
 
433
                                });
 
434
                                if (args.Result == DragDropResult.Canceled || !res)
 
435
                                        return false;
 
436
                        }
 
437
                        return true;
 
438
                }
 
439
                
 
440
                static bool PerformDragOperation (IntPtr sender, IntPtr sel, IntPtr dragInfo)
 
441
                {
 
442
                        IViewObject ob = Runtime.GetNSObject (sender) as IViewObject;
 
443
                        if (ob == null)
 
444
                                return false;
 
445
                        
 
446
                        var backend = (ViewBackend<T,S>) WidgetRegistry.GetBackend (ob.Frontend);
 
447
                        
 
448
                        NSDraggingInfo di = new NSDraggingInfo (dragInfo);
 
449
                        var pos = new Point (di.DraggingLocation.X, di.DraggingLocation.Y);
 
450
                        
 
451
                        if ((backend.currentEvents & WidgetEvent.DragDrop) != 0) {
 
452
                                TransferDataStore store = new TransferDataStore ();
 
453
                                FillDataStore (store, di.DraggingPasteboard, ob.View.RegisteredDragTypes ());
 
454
                                var args = new DragEventArgs (pos, store, ConvertAction (di.DraggingSourceOperationMask));
 
455
                                Toolkit.Invoke (delegate {
 
456
                                        backend.eventSink.OnDragDrop (args);
 
457
                                });
 
458
                                return args.Success;
 
459
                        } else
 
460
                                return false;
 
461
                }
 
462
                
 
463
                static void ConcludeDragOperation (IntPtr sender, IntPtr sel, IntPtr dragInfo)
 
464
                {
 
465
                        Console.WriteLine ("ConcludeDragOperation");
 
466
                }
 
467
                
 
468
                protected virtual void OnDragOverCheck (NSDraggingInfo di, DragOverCheckEventArgs args)
 
469
                {
 
470
                        Toolkit.Invoke (delegate {
 
471
                                eventSink.OnDragOverCheck (args);
 
472
                        });
 
473
                }
 
474
                
 
475
                protected virtual void OnDragOver (NSDraggingInfo di, DragOverEventArgs args)
 
476
                {
 
477
                        Toolkit.Invoke (delegate {
 
478
                                eventSink.OnDragOver (args);
 
479
                        });
 
480
                }
 
481
                
 
482
                void InitPasteboard (NSPasteboard pb, TransferDataSource data)
 
483
                {
 
484
                        pb.ClearContents ();
 
485
                        foreach (var t in data.DataTypes) {
 
486
                                if (t == TransferDataType.Text) {
 
487
                                        pb.AddTypes (new string[] { NSPasteboard.NSStringType }, null);
 
488
                                        pb.SetStringForType ((string)data.GetValue (t), NSPasteboard.NSStringType);
 
489
                                }
 
490
                        }
 
491
                }
 
492
 
 
493
                static void FillDataStore (TransferDataStore store, NSPasteboard pb, string[] types)
 
494
                {
 
495
                        foreach (var t in types) {
 
496
                                if (!pb.Types.Contains (t))
 
497
                                        continue;
 
498
                                if (t == NSPasteboard.NSStringType)
 
499
                                        store.AddText (pb.GetStringForType (t));
 
500
                                else if (t == NSPasteboard.NSFilenamesType) {
 
501
                                        string data = pb.GetStringForType (t);
 
502
                                        XmlDocument doc = new XmlDocument ();
 
503
                                        doc.XmlResolver = null; // Avoid DTD validation
 
504
                                        doc.LoadXml (data);
 
505
                                        store.AddUris (doc.SelectNodes ("/plist/array/string").Cast<XmlElement> ().Select (e => new Uri (e.InnerText)).ToArray ());
 
506
                                }
 
507
                        }
 
508
                }
 
509
                
 
510
                static NSDragOperation ConvertAction (DragDropAction action)
 
511
                {
 
512
                        NSDragOperation res = (NSDragOperation)0;
 
513
                        if ((action & DragDropAction.Copy) != 0)
 
514
                                res |= NSDragOperation.Copy;
 
515
                        if ((action & DragDropAction.Move) != 0)
 
516
                                res |= NSDragOperation.Move;
 
517
                        if ((action & DragDropAction.Link) != 0)
 
518
                                res |= NSDragOperation.Link;
 
519
                        return res;
 
520
                }
 
521
                
 
522
                static DragDropAction ConvertAction (NSDragOperation action)
 
523
                {
 
524
                        if (action == NSDragOperation.AllObsolete)
 
525
                                return DragDropAction.All;
 
526
                        DragDropAction res = (DragDropAction)0;
 
527
                        if ((action & NSDragOperation.Copy) != 0)
 
528
                                res |= DragDropAction.Copy;
 
529
                        if ((action & NSDragOperation.Move) != 0)
 
530
                                res |= DragDropAction.Move;
 
531
                        if ((action & NSDragOperation.Link) != 0)
 
532
                                res |= DragDropAction.Link;
 
533
                        return res;
 
534
                }
 
535
                
 
536
                static string ToNSDragType (TransferDataType type)
 
537
                {
 
538
                        if (type == TransferDataType.Text) return NSPasteboard.NSStringType;
 
539
                        if (type == TransferDataType.Uri) return NSPasteboard.NSFilenamesType;
 
540
                        if (type == TransferDataType.Image) return NSPasteboard.NSPictType;
 
541
                        if (type == TransferDataType.Rtf) return NSPasteboard.NSRtfType;
 
542
                        return type.Id;
 
543
                }
 
544
                
 
545
                static TransferDataType ToXwtDragType (string type)
 
546
                {
 
547
                        if (type == NSPasteboard.NSStringType)
 
548
                                return TransferDataType.Text;
 
549
                        if (type == NSPasteboard.NSFilenamesType)
 
550
                                return TransferDataType.Uri;
 
551
                        if (type == NSPasteboard.NSPictType)
 
552
                                return TransferDataType.Image;
 
553
                        if (type == NSPasteboard.NSRtfType)
 
554
                                return TransferDataType.Rtf;
 
555
                        return TransferDataType.FromId (type);
 
556
                }
 
557
                
 
558
                #endregion
 
559
        }
 
560
 
 
561
        public interface IMacViewBackend
 
562
        {
 
563
                NSView View { get; }
 
564
                Widget Frontend { get; }
 
565
                void NotifyPreferredSizeChanged ();
 
566
                IWidgetEventSink EventSink { get; }
 
567
 
 
568
                // To be called when the widget is a root and is not inside a Xwt window. For example, when it is in a popover or a tooltip
 
569
                // In that case, the widget has to listen to the change event of the children and resize itself
 
570
                void SetAutosizeMode (bool autosize);
 
571
        }
 
572
}
 
573