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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Gtk/Xwt.GtkBackend/WidgetBackend.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
// WidgetBackend.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 Xwt;
 
30
using System.Collections.Generic;
 
31
using System.Linq;
 
32
using Xwt.Engine;
 
33
using Xwt.Drawing;
 
34
 
 
35
namespace Xwt.GtkBackend
 
36
{
 
37
        public class WidgetBackend: IWidgetBackend, IGtkWidgetBackend
 
38
        {
 
39
                Gtk.Widget widget;
 
40
                Widget frontend;
 
41
                Gtk.Alignment alignment;
 
42
                Gtk.EventBox eventBox;
 
43
                IWidgetEventSink eventSink;
 
44
                WidgetEvent enabledEvents;
 
45
                bool destroyed;
 
46
                
 
47
                bool minSizeSet;
 
48
                
 
49
                class DragDropData
 
50
                {
 
51
                        public TransferDataSource CurrentDragData;
 
52
                        public Gdk.DragAction DestDragAction;
 
53
                        public Gdk.DragAction SourceDragAction;
 
54
                        public int DragDataRequests;
 
55
                        public TransferDataStore DragData;
 
56
                        public bool DragDataForMotion;
 
57
                        public Gtk.TargetEntry[] ValidDropTypes;
 
58
                        public Point LastDragPosition;
 
59
                }
 
60
                
 
61
                DragDropData dragDropInfo;
 
62
 
 
63
                const WidgetEvent dragDropEvents = WidgetEvent.DragDropCheck | WidgetEvent.DragDrop | WidgetEvent.DragOver | WidgetEvent.DragOverCheck;
 
64
                const WidgetEvent sizeCheckEvents = WidgetEvent.PreferredWidthCheck | WidgetEvent.PreferredHeightCheck | WidgetEvent.PreferredHeightForWidthCheck | WidgetEvent.PreferredWidthForHeightCheck;
 
65
                
 
66
                void IBackend.InitializeBackend (object frontend)
 
67
                {
 
68
                        this.frontend = (Widget) frontend;
 
69
                }
 
70
                
 
71
                void IWidgetBackend.Initialize (IWidgetEventSink sink)
 
72
                {
 
73
                        eventSink = sink;
 
74
                        Initialize ();
 
75
                }
 
76
                
 
77
                public virtual void Initialize ()
 
78
                {
 
79
                }
 
80
                
 
81
                public IWidgetEventSink EventSink {
 
82
                        get { return eventSink; }
 
83
                }
 
84
                
 
85
                public Widget Frontend {
 
86
                        get { return frontend; }
 
87
                }
 
88
                
 
89
                public object NativeWidget {
 
90
                        get {
 
91
                                return RootWidget;
 
92
                        }
 
93
                }
 
94
                
 
95
                public Gtk.Widget Widget {
 
96
                        get { return widget; }
 
97
                        set {
 
98
                                if (widget != null) {
 
99
                                        value.Visible = widget.Visible;
 
100
                                        GtkEngine.ReplaceChild (widget, value);
 
101
                                }
 
102
                                widget = value;
 
103
                        }
 
104
                }
 
105
                
 
106
                public Gtk.Widget RootWidget {
 
107
                        get {
 
108
                                return alignment ?? eventBox ?? (Gtk.Widget) Widget;
 
109
                        }
 
110
                }
 
111
                
 
112
                public virtual bool Visible {
 
113
                        get { return Widget.Visible; }
 
114
                        set {
 
115
                                Widget.Visible = value; 
 
116
                                if (alignment != null)
 
117
                                        alignment.Visible = value;
 
118
                                if (eventBox != null)
 
119
                                        eventBox.Visible = value;
 
120
                        }
 
121
                }
 
122
                
 
123
                public virtual bool Sensitive {
 
124
                        get { return Widget.Sensitive; }
 
125
                        set {
 
126
                                Widget.Sensitive = value;
 
127
                                if (eventBox != null)
 
128
                                        eventBox.Sensitive = value;
 
129
                        }
 
130
                }
 
131
                
 
132
                public bool CanGetFocus {
 
133
                        get { return Widget.CanFocus; }
 
134
                        set { Widget.CanFocus = value; }
 
135
                }
 
136
                
 
137
                public bool HasFocus {
 
138
                        get { return Widget.IsFocus; }
 
139
                }
 
140
                
 
141
                public void SetFocus ()
 
142
                {
 
143
                        Widget.IsFocus = true;
 
144
                }
 
145
 
 
146
                public string TooltipText {
 
147
                        get {
 
148
                                return Widget.TooltipText;
 
149
                        }
 
150
                        set {
 
151
                                Widget.TooltipText = value;
 
152
                        }
 
153
                }
 
154
                
 
155
                static Dictionary<CursorType,Gdk.Cursor> gtkCursors = new Dictionary<CursorType, Gdk.Cursor> ();
 
156
                
 
157
                public void SetCursor (CursorType cursor)
 
158
                {
 
159
                        AllocEventBox ();
 
160
                        Gdk.Cursor gc;
 
161
                        if (!gtkCursors.TryGetValue (cursor, out gc)) {
 
162
                                Gdk.CursorType ctype;
 
163
                                if (cursor == CursorType.Arrow)
 
164
                                        ctype = Gdk.CursorType.LeftPtr;
 
165
                                else if (cursor == CursorType.Crosshair)
 
166
                                        ctype = Gdk.CursorType.Crosshair;
 
167
                                else if (cursor == CursorType.Hand)
 
168
                                        ctype = Gdk.CursorType.Hand1;
 
169
                                else if (cursor == CursorType.IBeam)
 
170
                                        ctype = Gdk.CursorType.Xterm;
 
171
                                else if (cursor == CursorType.ResizeDown)
 
172
                                        ctype = Gdk.CursorType.BottomSide;
 
173
                                else if (cursor == CursorType.ResizeUp)
 
174
                                        ctype = Gdk.CursorType.TopSide;
 
175
                                else if (cursor == CursorType.ResizeLeft)
 
176
                                        ctype = Gdk.CursorType.LeftSide;
 
177
                                else if (cursor == CursorType.ResizeRight)
 
178
                                        ctype = Gdk.CursorType.RightSide;
 
179
                                else if (cursor == CursorType.ResizeLeftRight)
 
180
                                        ctype = Gdk.CursorType.SbHDoubleArrow;
 
181
                                else if (cursor == CursorType.ResizeUpDown)
 
182
                                        ctype = Gdk.CursorType.SbVDoubleArrow;
 
183
                                else
 
184
                                        ctype = Gdk.CursorType.Arrow;
 
185
                                
 
186
                                gtkCursors [cursor] = gc = new Gdk.Cursor (ctype);
 
187
                        }
 
188
                        if (EventsRootWidget.GdkWindow == null) {
 
189
                                EventHandler h = null;
 
190
                                h = delegate {
 
191
                                        EventsRootWidget.GdkWindow.Cursor = gc;
 
192
                                        EventsRootWidget.Realized -= h;
 
193
                                };
 
194
                                EventsRootWidget.Realized += h;
 
195
                        } else
 
196
                                EventsRootWidget.GdkWindow.Cursor = gc;
 
197
                }
 
198
                
 
199
                ~WidgetBackend ()
 
200
                {
 
201
                        Dispose (false);
 
202
                }
 
203
                
 
204
                public void Dispose ()
 
205
                {
 
206
                        GC.SuppressFinalize (this);
 
207
                        Dispose (true);
 
208
                }
 
209
                
 
210
                protected virtual void Dispose (bool disposing)
 
211
                {
 
212
                        if (Widget != null && disposing && Widget.Parent == null && !destroyed) {
 
213
                                MarkDestroyed (Frontend);
 
214
                                Widget.Destroy ();
 
215
                        }
 
216
                }
 
217
 
 
218
                void MarkDestroyed (Widget w)
 
219
                {
 
220
                        var bk = (WidgetBackend) WidgetRegistry.GetBackend (w);
 
221
                        bk.destroyed = true;
 
222
                        foreach (var c in w.Surface.Children)
 
223
                                MarkDestroyed (c);
 
224
                }
 
225
 
 
226
                public Size Size {
 
227
                        get {
 
228
                                return new Size (Widget.Allocation.Width, Widget.Allocation.Height);
 
229
                        }
 
230
                }
 
231
                
 
232
                DragDropData DragDropInfo {
 
233
                        get {
 
234
                                if (dragDropInfo == null)
 
235
                                        dragDropInfo = new DragDropData ();
 
236
                                return dragDropInfo;
 
237
                        }
 
238
                }
 
239
                
 
240
                public Point ConvertToScreenCoordinates (Point widgetCoordinates)
 
241
                {
 
242
                        if (Widget.ParentWindow == null)
 
243
                                return Point.Zero;
 
244
                        int x, y;
 
245
                        Widget.ParentWindow.GetOrigin (out x, out y);
 
246
                        var a = Widget.Allocation;
 
247
                        x += a.X;
 
248
                        y += a.Y;
 
249
                        return new Point (x + widgetCoordinates.X, y + widgetCoordinates.Y);
 
250
                }
 
251
                
 
252
                public virtual WidgetSize GetPreferredWidth ()
 
253
                {
 
254
                        bool oldFlag = doubleSizeRequestCheckSupported;
 
255
                        try {
 
256
                                gettingPreferredSize = true;
 
257
                                doubleSizeRequestCheckSupported = false;
 
258
                                var s = new WidgetSize (Widget.SizeRequest ().Width);
 
259
                                if (minSizeSet && Frontend.MinWidth != -1)
 
260
                                        s.MinSize = Frontend.MinWidth;
 
261
                                return s;
 
262
                        } finally {
 
263
                                gettingPreferredSize = false;
 
264
                                doubleSizeRequestCheckSupported = oldFlag;
 
265
                        }
 
266
                }
 
267
                
 
268
                public virtual WidgetSize GetPreferredHeight ()
 
269
                {
 
270
                        bool oldFlag = doubleSizeRequestCheckSupported;
 
271
                        try {
 
272
                                gettingPreferredSize = true;
 
273
                                doubleSizeRequestCheckSupported = false;
 
274
                                var s = new WidgetSize (Widget.SizeRequest ().Height);
 
275
                                if (minSizeSet && Frontend.MinHeight != -1)
 
276
                                        s.MinSize = Frontend.MinHeight;
 
277
                                return s;
 
278
                        } finally {
 
279
                                gettingPreferredSize = false;
 
280
                                doubleSizeRequestCheckSupported = oldFlag;
 
281
                        }
 
282
                }
 
283
                
 
284
                public virtual WidgetSize GetPreferredHeightForWidth (double width)
 
285
                {
 
286
                        bool oldFlag = doubleSizeRequestCheckSupported;
 
287
                        try {
 
288
                                gettingPreferredSize = true;
 
289
                                doubleSizeRequestCheckSupported = false;
 
290
                                var s = new WidgetSize (Widget.SizeRequest ().Height);
 
291
                                if (minSizeSet && Frontend.MinHeight != -1)
 
292
                                        s.MinSize = Frontend.MinHeight;
 
293
                                return s;
 
294
                        } finally {
 
295
                                gettingPreferredSize = false;
 
296
                                doubleSizeRequestCheckSupported = oldFlag;
 
297
                        }
 
298
                }
 
299
                
 
300
                public virtual WidgetSize GetPreferredWidthForHeight (double height)
 
301
                {
 
302
                        bool oldFlag = doubleSizeRequestCheckSupported;
 
303
                        try {
 
304
                                gettingPreferredSize = true;
 
305
                                doubleSizeRequestCheckSupported = false;
 
306
                                var s = new WidgetSize (Widget.SizeRequest ().Width);
 
307
                                if (minSizeSet && Frontend.MinWidth != -1)
 
308
                                        s.MinSize = Frontend.MinWidth;
 
309
                                return s;
 
310
                        } finally {
 
311
                                gettingPreferredSize = false;
 
312
                                doubleSizeRequestCheckSupported = oldFlag;
 
313
                        }
 
314
                }
 
315
                
 
316
                public void SetMinSize (double width, double height)
 
317
                {
 
318
                        if (width != -1 || height != -1) {
 
319
                                EnableSizeCheckEvents ();
 
320
                                minSizeSet = true;
 
321
                        }
 
322
                        else {
 
323
                                minSizeSet = false;
 
324
                                DisableSizeCheckEvents ();
 
325
                        }
 
326
                }
 
327
                
 
328
                public void SetNaturalSize (double width, double height)
 
329
                {
 
330
                        // Nothing to do
 
331
                }
 
332
                
 
333
                Pango.FontDescription customFont;
 
334
                
 
335
                public virtual object Font {
 
336
                        get {
 
337
                                return customFont ?? Widget.Style.FontDescription;
 
338
                        }
 
339
                        set {
 
340
                                var fd = (Pango.FontDescription) value;
 
341
                                customFont = fd;
 
342
                                Widget.ModifyFont (fd);
 
343
                        }
 
344
                }
 
345
                
 
346
                Color? customBackgroundColor;
 
347
                
 
348
                public virtual Color BackgroundColor {
 
349
                        get {
 
350
                                return customBackgroundColor.HasValue ? customBackgroundColor.Value : Util.ToXwtColor (Widget.Style.Background (Gtk.StateType.Normal));
 
351
                        }
 
352
                        set {
 
353
                                customBackgroundColor = value;
 
354
                                Widget.ModifyBg (Gtk.StateType.Normal, Util.ToGdkColor (value));
 
355
                        }
 
356
                }
 
357
                
 
358
                public bool UsingCustomBackgroundColor {
 
359
                        get { return customBackgroundColor.HasValue; }
 
360
                }
 
361
                
 
362
                Gtk.Widget IGtkWidgetBackend.Widget {
 
363
                        get { return RootWidget; }
 
364
                }
 
365
                
 
366
                protected virtual Gtk.Widget EventsRootWidget {
 
367
                        get { return eventBox ?? Widget; }
 
368
                }
 
369
                
 
370
                public static Gtk.Widget GetWidget (IWidgetBackend w)
 
371
                {
 
372
                        return w != null ? ((IGtkWidgetBackend)w).Widget : null;
 
373
                }
 
374
                
 
375
                public virtual void UpdateLayout ()
 
376
                {
 
377
                        if (frontend.Margin.HorizontalSpacing == 0 && frontend.Margin.VerticalSpacing == 0) {
 
378
                                if (alignment != null) {
 
379
                                        alignment.Remove (alignment.Child);
 
380
                                        GtkEngine.ReplaceChild (alignment, EventsRootWidget);
 
381
                                        alignment.Destroy ();
 
382
                                        alignment = null;
 
383
                                }
 
384
                        } else {
 
385
                                if (alignment == null) {
 
386
                                        alignment = new Gtk.Alignment (0, 0, 1, 1);
 
387
                                        GtkEngine.ReplaceChild (EventsRootWidget, alignment);
 
388
                                        alignment.Add (EventsRootWidget);
 
389
                                        alignment.Visible = Widget.Visible;
 
390
                                }
 
391
                                alignment.LeftPadding = (uint) frontend.Margin.Left;
 
392
                                alignment.RightPadding = (uint) frontend.Margin.Right;
 
393
                                alignment.TopPadding = (uint) frontend.Margin.Top;
 
394
                                alignment.BottomPadding = (uint) frontend.Margin.Bottom;
 
395
                        }
 
396
                        Widget.QueueResize ();
 
397
 
 
398
                        if (!Widget.IsRealized) {
 
399
                                // This is a workaround to a GTK bug. When a widget is inside a ScrolledWindow, sometimes the QueueResize call on
 
400
                                // the widget is ignored if the widget is not realized.
 
401
                                var p = Widget.Parent;
 
402
                                while (p != null && !(p is Gtk.ScrolledWindow))
 
403
                                        p = p.Parent;
 
404
                                if (p != null)
 
405
                                        p.QueueResize ();
 
406
                        }
 
407
                }
 
408
                
 
409
                void AllocEventBox ()
 
410
                {
 
411
                        // Wraps the widget with an event box. Required for some
 
412
                        // widgets such as Label which doesn't have its own gdk window
 
413
 
 
414
                        if (eventBox == null && EventsRootWidget.IsNoWindow) {
 
415
                                eventBox = new Gtk.EventBox ();
 
416
                                eventBox.Visible = Widget.Visible;
 
417
                                eventBox.Sensitive = Widget.Sensitive;
 
418
                                if (alignment != null) {
 
419
                                        alignment.Remove (alignment.Child);
 
420
                                        alignment.Add (eventBox);
 
421
                                } else
 
422
                                        GtkEngine.ReplaceChild (Widget, eventBox);
 
423
                                eventBox.Add (Widget);
 
424
                        }
 
425
                }
 
426
                
 
427
                public virtual void EnableEvent (object eventId)
 
428
                {
 
429
                        if (eventId is WidgetEvent) {
 
430
                                WidgetEvent ev = (WidgetEvent) eventId;
 
431
                                switch (ev) {
 
432
                                case WidgetEvent.DragLeave:
 
433
                                        AllocEventBox ();
 
434
                                        EventsRootWidget.DragLeave += HandleWidgetDragLeave;
 
435
                                        break;
 
436
                                case WidgetEvent.DragStarted:
 
437
                                        AllocEventBox ();
 
438
                                        EventsRootWidget.DragBegin += HandleWidgetDragBegin;
 
439
                                        break;
 
440
                                case WidgetEvent.KeyPressed:
 
441
                                        Widget.KeyPressEvent += HandleKeyPressEvent;
 
442
                                        break;
 
443
                                case WidgetEvent.KeyReleased:
 
444
                                        Widget.KeyReleaseEvent += HandleKeyReleaseEvent;
 
445
                                        break;
 
446
                                case WidgetEvent.GotFocus:
 
447
                                        EventsRootWidget.Events |= Gdk.EventMask.FocusChangeMask;
 
448
                                        Widget.FocusGrabbed += HandleWidgetFocusInEvent;
 
449
                                        break;
 
450
                                case WidgetEvent.LostFocus:
 
451
                                        EventsRootWidget.Events |= Gdk.EventMask.FocusChangeMask;
 
452
                                        Widget.FocusOutEvent += HandleWidgetFocusOutEvent;
 
453
                                        break;
 
454
                                case WidgetEvent.MouseEntered:
 
455
                                        AllocEventBox ();
 
456
                                        EventsRootWidget.Events |= Gdk.EventMask.EnterNotifyMask;
 
457
                                        EventsRootWidget.EnterNotifyEvent += HandleEnterNotifyEvent;
 
458
                                        break;
 
459
                                case WidgetEvent.MouseExited:
 
460
                                        AllocEventBox ();
 
461
                                        EventsRootWidget.Events |= Gdk.EventMask.LeaveNotifyMask;
 
462
                                        EventsRootWidget.LeaveNotifyEvent += HandleLeaveNotifyEvent;
 
463
                                        break;
 
464
                                case WidgetEvent.ButtonPressed:
 
465
                                        AllocEventBox ();
 
466
                                        EventsRootWidget.Events |= Gdk.EventMask.ButtonPressMask;
 
467
                                        EventsRootWidget.ButtonPressEvent += HandleButtonPressEvent;
 
468
                                        break;
 
469
                                case WidgetEvent.ButtonReleased:
 
470
                                        AllocEventBox ();
 
471
                                        EventsRootWidget.Events |= Gdk.EventMask.ButtonReleaseMask;
 
472
                                        EventsRootWidget.ButtonReleaseEvent += HandleButtonReleaseEvent;
 
473
                                        break;
 
474
                                case WidgetEvent.MouseMoved:
 
475
                                        AllocEventBox ();
 
476
                                        EventsRootWidget.Events |= Gdk.EventMask.PointerMotionMask;
 
477
                                        EventsRootWidget.MotionNotifyEvent += HandleMotionNotifyEvent;
 
478
                                        break;
 
479
                                case WidgetEvent.BoundsChanged:
 
480
                                        Widget.SizeAllocated += HandleWidgetBoundsChanged;
 
481
                                        break;
 
482
                case WidgetEvent.MouseScrolled:
 
483
                    AllocEventBox();
 
484
                    EventsRootWidget.Events |= Gdk.EventMask.ScrollMask;
 
485
                    Widget.ScrollEvent += HandleScrollEvent;
 
486
                    break;
 
487
                                }
 
488
                                if ((ev & dragDropEvents) != 0 && (enabledEvents & dragDropEvents) == 0) {
 
489
                                        // Enabling a drag&drop event for the first time
 
490
                                        AllocEventBox ();
 
491
                                        EventsRootWidget.DragDrop += HandleWidgetDragDrop;
 
492
                                        EventsRootWidget.DragMotion += HandleWidgetDragMotion;
 
493
                                        EventsRootWidget.DragDataReceived += HandleWidgetDragDataReceived;
 
494
                                }
 
495
                                if ((ev & sizeCheckEvents) != 0) {
 
496
                                        EnableSizeCheckEvents ();
 
497
                                }
 
498
                                enabledEvents |= ev;
 
499
                        }
 
500
                }
 
501
 
 
502
                void EnableSizeCheckEvents ()
 
503
                {
 
504
                        if ((enabledEvents & sizeCheckEvents) == 0 && !minSizeSet) {
 
505
                                // Enabling a size request event for the first time
 
506
                                Widget.SizeRequested += HandleWidgetSizeRequested;
 
507
                                Widget.SizeAllocated += HandleWidgetSizeAllocated;;
 
508
                        }
 
509
                }
 
510
                
 
511
                public virtual void DisableEvent (object eventId)
 
512
                {
 
513
                        if (eventId is WidgetEvent) {
 
514
                                WidgetEvent ev = (WidgetEvent) eventId;
 
515
                                switch (ev) {
 
516
                                case WidgetEvent.DragLeave:
 
517
                                        EventsRootWidget.DragLeave -= HandleWidgetDragLeave;
 
518
                                        break;
 
519
                                case WidgetEvent.DragStarted:
 
520
                                        EventsRootWidget.DragBegin -= HandleWidgetDragBegin;
 
521
                                        break;
 
522
                                case WidgetEvent.KeyPressed:
 
523
                                        Widget.KeyPressEvent -= HandleKeyPressEvent;
 
524
                                        break;
 
525
                                case WidgetEvent.KeyReleased:
 
526
                                        Widget.KeyReleaseEvent -= HandleKeyReleaseEvent;
 
527
                                        break;
 
528
                                case WidgetEvent.GotFocus:
 
529
                                        Widget.FocusInEvent -= HandleWidgetFocusInEvent;
 
530
                                        break;
 
531
                                case WidgetEvent.LostFocus:
 
532
                                        Widget.FocusOutEvent -= HandleWidgetFocusOutEvent;
 
533
                                        break;
 
534
                                case WidgetEvent.MouseEntered:
 
535
                                        EventsRootWidget.EnterNotifyEvent -= HandleEnterNotifyEvent;
 
536
                                        break;
 
537
                                case WidgetEvent.MouseExited:
 
538
                                        EventsRootWidget.LeaveNotifyEvent -= HandleLeaveNotifyEvent;
 
539
                                        break;
 
540
                                case WidgetEvent.ButtonPressed:
 
541
                                        EventsRootWidget.Events &= ~Gdk.EventMask.ButtonPressMask;
 
542
                                        EventsRootWidget.ButtonPressEvent -= HandleButtonPressEvent;
 
543
                                        break;
 
544
                                case WidgetEvent.ButtonReleased:
 
545
                                        EventsRootWidget.Events &= Gdk.EventMask.ButtonReleaseMask;
 
546
                                        EventsRootWidget.ButtonReleaseEvent -= HandleButtonReleaseEvent;
 
547
                                        break;
 
548
                                case WidgetEvent.MouseMoved:
 
549
                                        EventsRootWidget.Events &= Gdk.EventMask.PointerMotionMask;
 
550
                                        EventsRootWidget.MotionNotifyEvent -= HandleMotionNotifyEvent;
 
551
                                        break;
 
552
                                case WidgetEvent.BoundsChanged:
 
553
                                        Widget.SizeAllocated -= HandleWidgetBoundsChanged;
 
554
                                        break;
 
555
                case WidgetEvent.MouseScrolled:
 
556
                    EventsRootWidget.Events &= ~Gdk.EventMask.ScrollMask;
 
557
                    Widget.ScrollEvent -= HandleScrollEvent;
 
558
                    break;
 
559
                                }
 
560
                                
 
561
                                enabledEvents &= ~ev;
 
562
                                
 
563
                                if ((ev & dragDropEvents) != 0 && (enabledEvents & dragDropEvents) == 0) {
 
564
                                        // All drag&drop events have been disabled
 
565
                                        EventsRootWidget.DragDrop -= HandleWidgetDragDrop;
 
566
                                        EventsRootWidget.DragMotion -= HandleWidgetDragMotion;
 
567
                                        EventsRootWidget.DragDataReceived -= HandleWidgetDragDataReceived;
 
568
                                }
 
569
                                if ((ev & sizeCheckEvents) != 0) {
 
570
                                        DisableSizeCheckEvents ();
 
571
                                }
 
572
                                if ((ev & WidgetEvent.GotFocus) == 0 && (enabledEvents & WidgetEvent.LostFocus) == 0) {
 
573
                                        EventsRootWidget.Events &= ~Gdk.EventMask.FocusChangeMask;
 
574
                                }
 
575
                        }
 
576
                }
 
577
                
 
578
                void DisableSizeCheckEvents ()
 
579
                {
 
580
                        if ((enabledEvents & sizeCheckEvents) == 0 && !minSizeSet) {
 
581
                                // All size request events have been disabled
 
582
                                Widget.SizeRequested -= HandleWidgetSizeRequested;
 
583
                                Widget.SizeAllocated -= HandleWidgetSizeAllocated;;
 
584
                        }
 
585
                }
 
586
 
 
587
                Gdk.Rectangle lastAllocation;
 
588
                void HandleWidgetBoundsChanged (object o, Gtk.SizeAllocatedArgs args)
 
589
                {
 
590
                        if (Widget.Allocation != lastAllocation) {
 
591
                                lastAllocation = Widget.Allocation;
 
592
                                Toolkit.Invoke (delegate {
 
593
                                        EventSink.OnBoundsChanged ();
 
594
                                });
 
595
                        }
 
596
                }
 
597
                
 
598
                enum SizeCheckStep
 
599
                {
 
600
                        SizeRequest,
 
601
                        PreAllocate,
 
602
                        AdjustSize,
 
603
                        FinalAllocate
 
604
                }
 
605
                
 
606
                SizeCheckStep sizeCheckStep = SizeCheckStep.SizeRequest;
 
607
                int realRequestedWidth;
 
608
                int realRequestedHeight;
 
609
                bool gettingPreferredSize;
 
610
                static bool doubleSizeRequestCheckSupported = true;
 
611
                
 
612
                public bool IsPreallocating {
 
613
                        get { return sizeCheckStep == SizeCheckStep.AdjustSize || sizeCheckStep == SizeCheckStep.PreAllocate; }
 
614
                }
 
615
 
 
616
                void HandleWidgetSizeRequested (object o, Gtk.SizeRequestedArgs args)
 
617
                {
 
618
                        if (gettingPreferredSize)
 
619
                                return;
 
620
                        
 
621
                        var req = args.Requisition;
 
622
                        
 
623
                        if ((enabledEvents & sizeCheckEvents) == 0) {
 
624
                                // If no sizing event is set, it means this handler was set because there is a min size.
 
625
                                if (Frontend.MinWidth != -1)
 
626
                                        req.Width = (int) Frontend.MinWidth;
 
627
                                if (Frontend.MinHeight != -1)
 
628
                                        req.Height = (int) Frontend.MinHeight;
 
629
                                return;
 
630
                        }
 
631
                        
 
632
                        if (sizeCheckStep == SizeCheckStep.AdjustSize) {
 
633
                                req.Width = realRequestedWidth;
 
634
                                req.Height = realRequestedHeight;
 
635
                                sizeCheckStep = SizeCheckStep.FinalAllocate;
 
636
                        }
 
637
                        else {
 
638
                                Toolkit.Invoke (delegate {
 
639
                                        if (EventSink.GetSizeRequestMode () == SizeRequestMode.HeightForWidth) {
 
640
                                                if ((enabledEvents & WidgetEvent.PreferredWidthCheck) != 0) {
 
641
                                                    var w = eventSink.OnGetPreferredWidth ();
 
642
                                                        req.Width = (int) w.MinSize;
 
643
                                                }
 
644
                                                if ((enabledEvents & WidgetEvent.PreferredHeightForWidthCheck) != 0) {
 
645
                                                        if (doubleSizeRequestCheckSupported) {
 
646
                                                                sizeCheckStep = SizeCheckStep.PreAllocate;
 
647
                                                                realRequestedWidth = req.Width; // Store the width, since it will be used in the next iteration
 
648
                                                        } else {
 
649
                                                                var h = eventSink.OnGetPreferredHeightForWidth (req.Width);
 
650
                                                                req.Height = (int) h.MinSize;
 
651
                                                                sizeCheckStep = SizeCheckStep.FinalAllocate;
 
652
                                                        }
 
653
                                                }
 
654
                                                else if ((enabledEvents & WidgetEvent.PreferredHeightCheck) != 0) {
 
655
                                                        var h = eventSink.OnGetPreferredHeight ();
 
656
                                                        req.Height = (int) h.MinSize;
 
657
                                                        sizeCheckStep = SizeCheckStep.FinalAllocate;
 
658
                                                }
 
659
                                        } else {
 
660
                                                if ((enabledEvents & WidgetEvent.PreferredHeightCheck) != 0) {
 
661
                                                        var h = eventSink.OnGetPreferredHeight ();
 
662
                                                        req.Height = (int) h.MinSize;
 
663
                                                }
 
664
                                                if ((enabledEvents & WidgetEvent.PreferredWidthForHeightCheck) != 0) {
 
665
                                                        if (doubleSizeRequestCheckSupported) {
 
666
                                                                sizeCheckStep = SizeCheckStep.PreAllocate;
 
667
                                                                realRequestedHeight = req.Height; // Store the height, since it will be used in the next iteration
 
668
                                                        } else {
 
669
                                                                var w = eventSink.OnGetPreferredWidthForHeight (req.Height);
 
670
                                                                req.Width = (int) w.MinSize;
 
671
                                                                sizeCheckStep = SizeCheckStep.FinalAllocate;
 
672
                                                        }
 
673
                                                }
 
674
                                                else if ((enabledEvents & WidgetEvent.PreferredWidthCheck) != 0) {
 
675
                                                    var w = eventSink.OnGetPreferredWidth ();
 
676
                                                        req.Width = (int) w.MinSize;
 
677
                                                        sizeCheckStep = SizeCheckStep.FinalAllocate;
 
678
                                                }
 
679
                                        }
 
680
                                });
 
681
                        }
 
682
                        args.Requisition = req;
 
683
                }
 
684
 
 
685
                void HandleWidgetSizeAllocated (object o, Gtk.SizeAllocatedArgs args)
 
686
                {
 
687
                        if ((enabledEvents & sizeCheckEvents) == 0) {
 
688
                                // If no sizing event is set, it means this handler was set because there is a min size.
 
689
                                // In that case, there isn't any thing left to do here
 
690
                                return;
 
691
                        }
 
692
                        
 
693
                        Toolkit.Invoke (delegate {
 
694
                                if (sizeCheckStep == SizeCheckStep.SizeRequest && (enabledEvents & sizeCheckEvents) != sizeCheckEvents) {
 
695
                                        var ev = EventSink.GetSizeRequestMode () == SizeRequestMode.HeightForWidth ? WidgetEvent.PreferredWidthCheck | WidgetEvent.PreferredHeightForWidthCheck : WidgetEvent.PreferredHeightCheck | WidgetEvent.PreferredWidthForHeightCheck;
 
696
                                        // If all size request methods are overriden, the widget's size request won't be called, so this status is correct
 
697
                                        if ((enabledEvents & ev) != ev)
 
698
                                                Console.WriteLine ("SizeRequest not called. Should not happen.");
 
699
                                }
 
700
                                else if (sizeCheckStep == SizeCheckStep.PreAllocate || sizeCheckStep == SizeCheckStep.AdjustSize) {
 
701
                                        if (EventSink.GetSizeRequestMode () == SizeRequestMode.HeightForWidth) {
 
702
                                                realRequestedHeight = (int) eventSink.OnGetPreferredHeightForWidth (args.Allocation.Width).MinSize;
 
703
                                                sizeCheckStep = SizeCheckStep.AdjustSize;
 
704
                                                Widget.QueueResize ();
 
705
                                        } else {
 
706
                                                realRequestedWidth = (int) eventSink.OnGetPreferredWidthForHeight (args.Allocation.Height).MinSize;
 
707
                                                sizeCheckStep = SizeCheckStep.AdjustSize;
 
708
                                                Widget.QueueResize ();
 
709
                                        }
 
710
                                }
 
711
                        });
 
712
                }
 
713
 
 
714
                [GLib.ConnectBefore]
 
715
                void HandleKeyReleaseEvent (object o, Gtk.KeyReleaseEventArgs args)
 
716
                {
 
717
                        Key k = (Key)args.Event.KeyValue;
 
718
                        ModifierKeys m = ModifierKeys.None;
 
719
                        if ((args.Event.State & Gdk.ModifierType.ShiftMask) != 0)
 
720
                                m |= ModifierKeys.Shift;
 
721
                        if ((args.Event.State & Gdk.ModifierType.ControlMask) != 0)
 
722
                                m |= ModifierKeys.Control;
 
723
                        if ((args.Event.State & Gdk.ModifierType.Mod1Mask) != 0)
 
724
                                m |= ModifierKeys.Alt;
 
725
                        KeyEventArgs kargs = new KeyEventArgs (k, m, false, (long)args.Event.Time);
 
726
                        Toolkit.Invoke (delegate {
 
727
                                EventSink.OnKeyReleased (kargs);
 
728
                        });
 
729
                        if (kargs.Handled)
 
730
                                args.RetVal = true;
 
731
                }
 
732
 
 
733
                [GLib.ConnectBefore]
 
734
                void HandleKeyPressEvent (object o, Gtk.KeyPressEventArgs args)
 
735
                {
 
736
                        Key k = (Key)args.Event.KeyValue;
 
737
                        ModifierKeys m = ModifierKeys.None;
 
738
                        if ((args.Event.State & Gdk.ModifierType.ShiftMask) != 0)
 
739
                                m |= ModifierKeys.Shift;
 
740
                        if ((args.Event.State & Gdk.ModifierType.ControlMask) != 0)
 
741
                                m |= ModifierKeys.Control;
 
742
                        if ((args.Event.State & Gdk.ModifierType.Mod1Mask) != 0)
 
743
                                m |= ModifierKeys.Alt;
 
744
                        KeyEventArgs kargs = new KeyEventArgs (k, m, false, (long)args.Event.Time);
 
745
                        Toolkit.Invoke (delegate {
 
746
                                EventSink.OnKeyPressed (kargs);
 
747
                        });
 
748
                        if (kargs.Handled)
 
749
                                args.RetVal = true;
 
750
                }
 
751
 
 
752
        [GLib.ConnectBefore]
 
753
        void HandleScrollEvent(object o, Gtk.ScrollEventArgs args)
 
754
        {
 
755
            var sc = ConvertToScreenCoordinates (new Point (0, 0));
 
756
            var direction = Util.ConvertScrollDirection(args.Event.Direction);
 
757
 
 
758
            var a = new MouseScrolledEventArgs ((long) args.Event.Time, args.Event.XRoot - sc.X, args.Event.YRoot - sc.Y, direction);
 
759
            Toolkit.Invoke (delegate {
 
760
                EventSink.OnMouseScrolled(a);
 
761
            });
 
762
            if (a.Handled)
 
763
                args.RetVal = true;
 
764
        }
 
765
        
 
766
 
 
767
                void HandleWidgetFocusOutEvent (object o, Gtk.FocusOutEventArgs args)
 
768
                {
 
769
                        Toolkit.Invoke (delegate {
 
770
                                EventSink.OnLostFocus ();
 
771
                        });
 
772
                }
 
773
 
 
774
                void HandleWidgetFocusInEvent (object o, EventArgs args)
 
775
                {
 
776
                        Toolkit.Invoke (delegate {
 
777
                                EventSink.OnGotFocus ();
 
778
                        });
 
779
                }
 
780
 
 
781
                void HandleLeaveNotifyEvent (object o, Gtk.LeaveNotifyEventArgs args)
 
782
                {
 
783
                        if (args.Event.Detail == Gdk.NotifyType.Inferior)
 
784
                                return;
 
785
                        Toolkit.Invoke (delegate {
 
786
                                EventSink.OnMouseExited ();
 
787
                        });
 
788
                }
 
789
 
 
790
                void HandleEnterNotifyEvent (object o, Gtk.EnterNotifyEventArgs args)
 
791
                {
 
792
                        if (args.Event.Detail == Gdk.NotifyType.Inferior)
 
793
                                return;
 
794
                        Toolkit.Invoke (delegate {
 
795
                                EventSink.OnMouseEntered ();
 
796
                        });
 
797
                }
 
798
 
 
799
                void HandleMotionNotifyEvent (object o, Gtk.MotionNotifyEventArgs args)
 
800
                {
 
801
                        var sc = ConvertToScreenCoordinates (new Point (0, 0));
 
802
                        var a = new MouseMovedEventArgs ((long) args.Event.Time, args.Event.XRoot - sc.X, args.Event.YRoot - sc.Y);
 
803
                        Toolkit.Invoke (delegate {
 
804
                                EventSink.OnMouseMoved (a);
 
805
                        });
 
806
                        if (a.Handled)
 
807
                                args.RetVal = true;
 
808
                }
 
809
 
 
810
                void HandleButtonReleaseEvent (object o, Gtk.ButtonReleaseEventArgs args)
 
811
                {
 
812
                        var sc = ConvertToScreenCoordinates (new Point (0, 0));
 
813
                        var a = new ButtonEventArgs ();
 
814
                        a.X = args.Event.XRoot - sc.X;
 
815
                        a.Y = args.Event.YRoot - sc.Y;
 
816
                        a.Button = (PointerButton) args.Event.Button;
 
817
                        Toolkit.Invoke (delegate {
 
818
                                EventSink.OnButtonReleased (a);
 
819
                        });
 
820
                        if (a.Handled)
 
821
                                args.RetVal = true;
 
822
                }
 
823
 
 
824
                [GLib.ConnectBeforeAttribute]
 
825
                void HandleButtonPressEvent (object o, Gtk.ButtonPressEventArgs args)
 
826
                {
 
827
                        var sc = ConvertToScreenCoordinates (new Point (0, 0));
 
828
                        var a = new ButtonEventArgs ();
 
829
                        a.X = args.Event.XRoot - sc.X;
 
830
                        a.Y = args.Event.YRoot - sc.Y;
 
831
                        a.Button = (PointerButton) args.Event.Button;
 
832
                        if (args.Event.Type == Gdk.EventType.TwoButtonPress)
 
833
                                a.MultiplePress = 2;
 
834
                        else if (args.Event.Type == Gdk.EventType.ThreeButtonPress)
 
835
                                a.MultiplePress = 3;
 
836
                        else
 
837
                                a.MultiplePress = 1;
 
838
                        Toolkit.Invoke (delegate {
 
839
                                EventSink.OnButtonPressed (a);
 
840
                        });
 
841
                        if (a.Handled)
 
842
                                args.RetVal = true;
 
843
                }
 
844
                
 
845
                [GLib.ConnectBefore]
 
846
                void HandleWidgetDragMotion (object o, Gtk.DragMotionArgs args)
 
847
                {
 
848
                        args.RetVal = DoDragMotion (args.Context, args.X, args.Y, args.Time);
 
849
                }
 
850
                
 
851
                internal bool DoDragMotion (Gdk.DragContext context, int x, int y, uint time)
 
852
                {
 
853
                        DragDropInfo.LastDragPosition = new Point (x, y);
 
854
                        
 
855
                        DragDropAction ac;
 
856
                        if ((enabledEvents & WidgetEvent.DragOverCheck) == 0) {
 
857
                                if ((enabledEvents & WidgetEvent.DragOver) != 0)
 
858
                                        ac = DragDropAction.Default;
 
859
                                else
 
860
                                        ac = ConvertDragAction (DragDropInfo.DestDragAction);
 
861
                        }
 
862
                        else {
 
863
                                // This is a workaround to what seems to be a mac gtk bug.
 
864
                                // Suggested action is set to all when no control key is pressed
 
865
                                var cact = ConvertDragAction (context.Actions);
 
866
                                if (cact == DragDropAction.All)
 
867
                                        cact = DragDropAction.Move;
 
868
 
 
869
                                var target = Gtk.Drag.DestFindTarget (EventsRootWidget, context, null);
 
870
                                var targetTypes = Util.GetDragTypes (new Gdk.Atom[] { target });
 
871
                                DragOverCheckEventArgs da = new DragOverCheckEventArgs (new Point (x, y), targetTypes, cact);
 
872
                                Toolkit.Invoke (delegate {
 
873
                                        EventSink.OnDragOverCheck (da);
 
874
                                });
 
875
                                ac = da.AllowedAction;
 
876
                                if ((enabledEvents & WidgetEvent.DragOver) == 0 && ac == DragDropAction.Default)
 
877
                                        ac = DragDropAction.None;
 
878
                        }
 
879
                        
 
880
                        if (ac == DragDropAction.None) {
 
881
                                OnSetDragStatus (context, x, y, time, (Gdk.DragAction)0);
 
882
                                return true;
 
883
                        }
 
884
                        else if (ac == DragDropAction.Default) {
 
885
                                // Undefined, we need more data
 
886
                                QueryDragData (context, time, true);
 
887
                                return true;
 
888
                        }
 
889
                        else {
 
890
//                              Gtk.Drag.Highlight (Widget);
 
891
                                OnSetDragStatus (context, x, y, time, ConvertDragAction (ac));
 
892
                                return true;
 
893
                        }
 
894
                }
 
895
                
 
896
                [GLib.ConnectBefore]
 
897
                void HandleWidgetDragDrop (object o, Gtk.DragDropArgs args)
 
898
                {
 
899
                        args.RetVal = DoDragDrop (args.Context, args.X, args.Y, args.Time);
 
900
                }
 
901
                
 
902
                internal bool DoDragDrop (Gdk.DragContext context, int x, int y, uint time)
 
903
                {
 
904
                        DragDropInfo.LastDragPosition = new Point (x, y);
 
905
                        var cda = ConvertDragAction (context.Action);
 
906
 
 
907
                        DragDropResult res;
 
908
                        if ((enabledEvents & WidgetEvent.DragDropCheck) == 0) {
 
909
                                if ((enabledEvents & WidgetEvent.DragDrop) != 0)
 
910
                                        res = DragDropResult.None;
 
911
                                else
 
912
                                        res = DragDropResult.Canceled;
 
913
                        }
 
914
                        else {
 
915
                                DragCheckEventArgs da = new DragCheckEventArgs (new Point (x, y), Util.GetDragTypes (context.Targets), cda);
 
916
                                Toolkit.Invoke (delegate {
 
917
                                        EventSink.OnDragDropCheck (da);
 
918
                                });
 
919
                                res = da.Result;
 
920
                                if ((enabledEvents & WidgetEvent.DragDrop) == 0 && res == DragDropResult.None)
 
921
                                        res = DragDropResult.Canceled;
 
922
                        }
 
923
                        if (res == DragDropResult.Canceled) {
 
924
                                Gtk.Drag.Finish (context, false, false, time);
 
925
                                return true;
 
926
                        }
 
927
                        else if (res == DragDropResult.Success) {
 
928
                                Gtk.Drag.Finish (context, true, cda == DragDropAction.Move, time);
 
929
                                return true;
 
930
                        }
 
931
                        else {
 
932
                                // Undefined, we need more data
 
933
                                QueryDragData (context, time, false);
 
934
                                return true;
 
935
                        }
 
936
                }
 
937
 
 
938
                void HandleWidgetDragLeave (object o, Gtk.DragLeaveArgs args)
 
939
                {
 
940
                        Toolkit.Invoke (delegate {
 
941
                                eventSink.OnDragLeave (EventArgs.Empty);
 
942
                        });
 
943
                }
 
944
                
 
945
                void QueryDragData (Gdk.DragContext ctx, uint time, bool isMotionEvent)
 
946
                {
 
947
                        DragDropInfo.DragDataForMotion = isMotionEvent;
 
948
                        DragDropInfo.DragData = new TransferDataStore ();
 
949
                        DragDropInfo.DragDataRequests = DragDropInfo.ValidDropTypes.Length;
 
950
                        foreach (var t in DragDropInfo.ValidDropTypes) {
 
951
                                var at = Gdk.Atom.Intern (t.Target, true);
 
952
                                Gtk.Drag.GetData (EventsRootWidget, ctx, at, time);
 
953
                        }
 
954
                }
 
955
                
 
956
                void HandleWidgetDragDataReceived (object o, Gtk.DragDataReceivedArgs args)
 
957
                {
 
958
                        args.RetVal = DoDragDataReceived (args.Context, args.X, args.Y, args.SelectionData, args.Info, args.Time);
 
959
                }
 
960
                
 
961
                internal bool DoDragDataReceived (Gdk.DragContext context, int x, int y, Gtk.SelectionData selectionData, uint info, uint time)
 
962
                {
 
963
                        if (DragDropInfo.DragDataRequests == 0) {
 
964
                                // Got the data without requesting it. Create the datastore here
 
965
                                DragDropInfo.DragData = new TransferDataStore ();
 
966
                                DragDropInfo.LastDragPosition = new Point (x, y);
 
967
                                DragDropInfo.DragDataRequests = 1;
 
968
                        }
 
969
                        
 
970
                        DragDropInfo.DragDataRequests--;
 
971
                        
 
972
                        if (!Util.GetSelectionData (selectionData, DragDropInfo.DragData)) {
 
973
                                return false;
 
974
                        }
 
975
 
 
976
                        if (DragDropInfo.DragDataRequests == 0) {
 
977
                                if (DragDropInfo.DragDataForMotion) {
 
978
                                        // If no specific action is set, it means that no key has been pressed.
 
979
                                        // In that case, use Move or Copy or Link as default (when allowed, in this order).
 
980
                                        var cact = ConvertDragAction (context.Actions);
 
981
                                        if (cact != DragDropAction.Copy && cact != DragDropAction.Move && cact != DragDropAction.Link) {
 
982
                                                if (cact.HasFlag (DragDropAction.Move))
 
983
                                                        cact = DragDropAction.Move;
 
984
                                                else if (cact.HasFlag (DragDropAction.Copy))
 
985
                                                        cact = DragDropAction.Copy;
 
986
                                                else if (cact.HasFlag (DragDropAction.Link))
 
987
                                                        cact = DragDropAction.Link;
 
988
                                                else
 
989
                                                        cact = DragDropAction.None;
 
990
                                        }
 
991
 
 
992
                                        DragOverEventArgs da = new DragOverEventArgs (DragDropInfo.LastDragPosition, DragDropInfo.DragData, cact);
 
993
                                        Toolkit.Invoke (delegate {
 
994
                                                EventSink.OnDragOver (da);
 
995
                                        });
 
996
                                        OnSetDragStatus (context, (int)DragDropInfo.LastDragPosition.X, (int)DragDropInfo.LastDragPosition.Y, time, ConvertDragAction (da.AllowedAction));
 
997
                                        return true;
 
998
                                }
 
999
                                else {
 
1000
                                        // Use Context.Action here since that's the action selected in DragOver
 
1001
                                        var cda = ConvertDragAction (context.Action);
 
1002
                                        DragEventArgs da = new DragEventArgs (DragDropInfo.LastDragPosition, DragDropInfo.DragData, cda);
 
1003
                                        Toolkit.Invoke (delegate {
 
1004
                                                EventSink.OnDragDrop (da);
 
1005
                                        });
 
1006
                                        Gtk.Drag.Finish (context, da.Success, cda == DragDropAction.Move, time);
 
1007
                                        return true;
 
1008
                                }
 
1009
                        } else
 
1010
                                return false;
 
1011
                }
 
1012
                
 
1013
                protected virtual void OnSetDragStatus (Gdk.DragContext context, int x, int y, uint time, Gdk.DragAction action)
 
1014
                {
 
1015
                        Gdk.Drag.Status (context, action, time);
 
1016
                }
 
1017
                
 
1018
                void HandleWidgetDragBegin (object o, Gtk.DragBeginArgs args)
 
1019
                {
 
1020
                        // If SetDragSource has not been called, ignore the event
 
1021
                        if (DragDropInfo.SourceDragAction == default (Gdk.DragAction))
 
1022
                                return;
 
1023
 
 
1024
                        DragStartData sdata = null;
 
1025
                        Toolkit.Invoke (delegate {
 
1026
                                sdata = EventSink.OnDragStarted ();
 
1027
                        });
 
1028
                        
 
1029
                        if (sdata == null)
 
1030
                                return;
 
1031
                        
 
1032
                        DragDropInfo.CurrentDragData = sdata.Data;
 
1033
                        
 
1034
                        if (sdata.ImageBackend != null)
 
1035
                                Gtk.Drag.SetIconPixbuf (args.Context, (Gdk.Pixbuf) sdata.ImageBackend, (int)sdata.HotX, (int)sdata.HotY);
 
1036
                        
 
1037
                        HandleDragBegin (null, args);
 
1038
                }
 
1039
                
 
1040
                class IconInitializer
 
1041
                {
 
1042
                        public Gdk.Pixbuf Image;
 
1043
                        public double HotX, HotY;
 
1044
                        public Gtk.Widget Widget;
 
1045
                        
 
1046
                        public static void Init (Gtk.Widget w, Gdk.Pixbuf image, double hotX, double hotY)
 
1047
                        {
 
1048
                                IconInitializer ii = new WidgetBackend.IconInitializer ();
 
1049
                                ii.Image = image;
 
1050
                                ii.HotX = hotX;
 
1051
                                ii.HotY = hotY;
 
1052
                                ii.Widget = w;
 
1053
                                w.DragBegin += ii.Begin;
 
1054
                        }
 
1055
                        
 
1056
                        void Begin (object o, Gtk.DragBeginArgs args)
 
1057
                        {
 
1058
                                Gtk.Drag.SetIconPixbuf (args.Context, Image, (int)HotX, (int)HotY);
 
1059
                                Widget.DragBegin -= Begin;
 
1060
                        }
 
1061
                }
 
1062
                
 
1063
                public void DragStart (DragStartData sdata)
 
1064
                {
 
1065
                        AllocEventBox ();
 
1066
                        Gdk.DragAction action = ConvertDragAction (sdata.DragAction);
 
1067
                        DragDropInfo.CurrentDragData = sdata.Data;
 
1068
                        EventsRootWidget.DragBegin += HandleDragBegin;
 
1069
                        if (sdata.ImageBackend != null)
 
1070
                                IconInitializer.Init (EventsRootWidget, (Gdk.Pixbuf) sdata.ImageBackend, sdata.HotX, sdata.HotY);
 
1071
                        Gtk.Drag.Begin (EventsRootWidget, Util.BuildTargetTable (sdata.Data.DataTypes), action, 1, Gtk.Global.CurrentEvent);
 
1072
                }
 
1073
 
 
1074
                void HandleDragBegin (object o, Gtk.DragBeginArgs args)
 
1075
                {
 
1076
                        EventsRootWidget.DragEnd += HandleWidgetDragEnd;
 
1077
                        EventsRootWidget.DragFailed += HandleDragFailed;
 
1078
                        EventsRootWidget.DragDataDelete += HandleDragDataDelete;
 
1079
                        EventsRootWidget.DragDataGet += HandleWidgetDragDataGet;
 
1080
                }
 
1081
                
 
1082
                void HandleWidgetDragDataGet (object o, Gtk.DragDataGetArgs args)
 
1083
                {
 
1084
                        Util.SetDragData (DragDropInfo.CurrentDragData, args);
 
1085
                }
 
1086
 
 
1087
                void HandleDragFailed (object o, Gtk.DragFailedArgs args)
 
1088
                {
 
1089
                        Console.WriteLine ("FAILED");
 
1090
                }
 
1091
                
 
1092
                void HandleDragDataDelete (object o, Gtk.DragDataDeleteArgs args)
 
1093
                {
 
1094
                        DoDragaDataDelete ();
 
1095
                }
 
1096
                
 
1097
                internal void DoDragaDataDelete ()
 
1098
                {
 
1099
                        FinishDrag (true);
 
1100
                }
 
1101
                
 
1102
                void HandleWidgetDragEnd (object o, Gtk.DragEndArgs args)
 
1103
                {
 
1104
                        FinishDrag (false);
 
1105
                }
 
1106
                
 
1107
                void FinishDrag (bool delete)
 
1108
                {
 
1109
                        EventsRootWidget.DragEnd -= HandleWidgetDragEnd;
 
1110
                        EventsRootWidget.DragDataGet -= HandleWidgetDragDataGet;
 
1111
                        EventsRootWidget.DragFailed -= HandleDragFailed;
 
1112
                        EventsRootWidget.DragDataDelete -= HandleDragDataDelete;
 
1113
                        EventsRootWidget.DragBegin -= HandleDragBegin; // This event is subscribed only when manualy starting a drag
 
1114
                        Toolkit.Invoke (delegate {
 
1115
                                eventSink.OnDragFinished (new DragFinishedEventArgs (delete));
 
1116
                        });
 
1117
                }
 
1118
                
 
1119
                public void SetDragTarget (TransferDataType[] types, DragDropAction dragAction)
 
1120
                {
 
1121
                        DragDropInfo.DestDragAction = ConvertDragAction (dragAction);
 
1122
                        var table = Util.BuildTargetTable (types);
 
1123
                        DragDropInfo.ValidDropTypes = (Gtk.TargetEntry[]) table;
 
1124
                        OnSetDragTarget (DragDropInfo.ValidDropTypes, DragDropInfo.DestDragAction);
 
1125
                }
 
1126
                
 
1127
                protected virtual void OnSetDragTarget (Gtk.TargetEntry[] table, Gdk.DragAction actions)
 
1128
                {
 
1129
                        AllocEventBox ();
 
1130
                        Gtk.Drag.DestSet (EventsRootWidget, Gtk.DestDefaults.Highlight, table, actions);
 
1131
                }
 
1132
                
 
1133
                public void SetDragSource (TransferDataType[] types, DragDropAction dragAction)
 
1134
                {
 
1135
                        AllocEventBox ();
 
1136
                        DragDropInfo.SourceDragAction = ConvertDragAction (dragAction);
 
1137
                        var table = Util.BuildTargetTable (types);
 
1138
                        OnSetDragSource (Gdk.ModifierType.Button1Mask, (Gtk.TargetEntry[]) table, DragDropInfo.SourceDragAction);
 
1139
                }
 
1140
                
 
1141
                protected virtual void OnSetDragSource (Gdk.ModifierType modifierType, Gtk.TargetEntry[] table, Gdk.DragAction actions)
 
1142
                {
 
1143
                        Gtk.Drag.SourceSet (EventsRootWidget, modifierType, table, actions);
 
1144
                }
 
1145
                
 
1146
                Gdk.DragAction ConvertDragAction (DragDropAction dragAction)
 
1147
                {
 
1148
                        Gdk.DragAction action = (Gdk.DragAction)0;
 
1149
                        if ((dragAction & DragDropAction.Copy) != 0)
 
1150
                                action |= Gdk.DragAction.Copy;
 
1151
                        if ((dragAction & DragDropAction.Move) != 0)
 
1152
                                action |= Gdk.DragAction.Move;
 
1153
                        if ((dragAction & DragDropAction.Link) != 0)
 
1154
                                action |= Gdk.DragAction.Link;
 
1155
                        return action;
 
1156
                }
 
1157
                
 
1158
                DragDropAction ConvertDragAction (Gdk.DragAction dragAction)
 
1159
                {
 
1160
                        DragDropAction action = (DragDropAction)0;
 
1161
                        if ((dragAction & Gdk.DragAction.Copy) != 0)
 
1162
                                action |= DragDropAction.Copy;
 
1163
                        if ((dragAction & Gdk.DragAction.Move) != 0)
 
1164
                                action |= DragDropAction.Move;
 
1165
                        if ((dragAction & Gdk.DragAction.Link) != 0)
 
1166
                                action |= DragDropAction.Link;
 
1167
                        return action;
 
1168
                }
 
1169
        }
 
1170
        
 
1171
        public interface IGtkWidgetBackend
 
1172
        {
 
1173
                Gtk.Widget Widget { get; }
 
1174
        }
 
1175
}
 
1176