1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
|
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2005 Novell, Inc.
//
// Authors:
// Jackson Harper (jackson@ximian.com)
//
//
using System;
using System.IO;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
namespace System.Windows.Forms {
internal class X11Dnd {
private enum State {
Accepting,
Dragging
}
private enum DragState {
None,
Beginning,
Dragging,
Entered
}
private interface IDataConverter {
void GetData (X11Dnd dnd, DataObject data, ref XEvent xevent);
void SetData (X11Dnd dnd, object data, ref XEvent xevent);
}
private delegate void MimeConverter (IntPtr dsp,
DataObject data, ref XEvent xevent);
private class MimeHandler {
public string Name;
public IntPtr Type;
public IntPtr NonProtocol;
public IDataConverter Converter;
public MimeHandler (string name, IDataConverter converter)
{
Name = name;
Converter = converter;
}
public override string ToString ()
{
return "MimeHandler {" + Name + "}";
}
}
private MimeHandler [] MimeHandlers = {
// new MimeHandler ("WCF_DIB"),
// new MimeHandler ("image/gif", new MimeConverter (ImageConverter)),
// new MimeHandler ("text/rtf", new MimeConverter (RtfConverter)),
// new MimeHandler ("text/richtext", new MimeConverter (RtfConverter)),
new MimeHandler ("text/plain", new TextConverter ()),
new MimeHandler ("text/html", new HtmlConverter ()),
new MimeHandler ("text/uri-list", new UriListConverter ()),
new MimeHandler ("application/x-mono-serialized-object",
new SerializedObjectConverter ())
};
private class SerializedObjectConverter : IDataConverter {
public void GetData (X11Dnd dnd, DataObject data, ref XEvent xevent)
{
MemoryStream stream = dnd.GetData (ref xevent);
BinaryFormatter bf = new BinaryFormatter ();
if (stream.Length == 0)
return;
stream.Seek (0, 0);
object obj = bf.Deserialize (stream);
data.SetData (obj);
}
public void SetData (X11Dnd dnd, object data, ref XEvent xevent)
{
if (data == null)
return;
MemoryStream stream = new MemoryStream ();
BinaryFormatter bf = new BinaryFormatter ();
bf.Serialize (stream, data);
IntPtr buffer = Marshal.AllocHGlobal ((int) stream.Length);
stream.Seek (0, 0);
for (int i = 0; i < stream.Length; i++) {
Marshal.WriteByte (buffer, i, (byte) stream.ReadByte ());
}
dnd.SetProperty (ref xevent, buffer, (int) stream.Length);
}
}
private class HtmlConverter : IDataConverter {
public void GetData (X11Dnd dnd, DataObject data, ref XEvent xevent)
{
string text = dnd.GetText (ref xevent, false);
if (text == null)
return;
data.SetData (DataFormats.Text, text);
data.SetData (DataFormats.UnicodeText, text);
}
public void SetData (X11Dnd dnd, object data, ref XEvent xevent)
{
IntPtr buffer;
int len;
string str = data as string;
if (str == null)
return;
if (xevent.SelectionRequestEvent.target == (IntPtr)Atom.XA_STRING) {
byte [] bytes = Encoding.ASCII.GetBytes (str);
buffer = Marshal.AllocHGlobal (bytes.Length);
len = bytes.Length;
for (int i = 0; i < len; i++)
Marshal.WriteByte (buffer, i, bytes [i]);
} else {
buffer = Marshal.StringToHGlobalAnsi (str);
len = 0;
while (Marshal.ReadByte (buffer, len) != 0)
len++;
}
dnd.SetProperty (ref xevent, buffer, len);
Marshal.FreeHGlobal (buffer);
}
}
private class TextConverter : IDataConverter {
public void GetData (X11Dnd dnd, DataObject data, ref XEvent xevent)
{
string text = dnd.GetText (ref xevent, true);
if (text == null)
return;
data.SetData (DataFormats.Text, text);
data.SetData (DataFormats.UnicodeText, text);
}
public void SetData (X11Dnd dnd, object data, ref XEvent xevent)
{
IntPtr buffer;
int len;
string str = data as string;
if (data == null)
return;
if (xevent.SelectionRequestEvent.target == (IntPtr)Atom.XA_STRING) {
byte [] bytes = Encoding.ASCII.GetBytes (str);
buffer = Marshal.AllocHGlobal (bytes.Length);
len = bytes.Length;
for (int i = 0; i < len; i++)
Marshal.WriteByte (buffer, i, bytes [i]);
} else {
buffer = Marshal.StringToHGlobalAnsi (str);
len = 0;
while (Marshal.ReadByte (buffer, len) != 0)
len++;
}
dnd.SetProperty (ref xevent, buffer, len);
Marshal.FreeHGlobal (buffer);
}
}
private class UriListConverter : IDataConverter {
public void GetData (X11Dnd dnd, DataObject data, ref XEvent xevent)
{
string text = dnd.GetText (ref xevent, false);
if (text == null)
return;
// TODO: Do this in a loop instead of just splitting
ArrayList uri_list = new ArrayList ();
string [] lines = text.Split (new char [] { '\r', '\n' });
foreach (string line in lines) {
// # is a comment line (see RFC 2483)
if (line.StartsWith ("#"))
continue;
try {
Uri uri = new Uri (line);
uri_list.Add (uri.LocalPath);
} catch { }
}
string [] l = (string []) uri_list.ToArray (typeof (string));
if (l.Length < 1)
return;
data.SetData (DataFormats.FileDrop, l);
data.SetData ("FileName", l [0]);
data.SetData ("FileNameW", l [0]);
}
public void SetData (X11Dnd dnd, object data, ref XEvent xevent)
{
string [] uri_list = data as string [];
if (uri_list == null)
return;
StringBuilder res = new StringBuilder ();
foreach (string uri_str in uri_list) {
Uri uri = new Uri (uri_str);
res.Append (uri.ToString ());
res.Append ("\r\n");
}
IntPtr buffer = Marshal.StringToHGlobalAnsi ((string) data);
int len = 0;
while (Marshal.ReadByte (buffer, len) != 0)
len++;
dnd.SetProperty (ref xevent, buffer, len);
}
}
private class DragData {
public IntPtr Window;
public DragState State;
public object Data;
public IntPtr Action;
public IntPtr [] SupportedTypes;
public MouseButtons MouseState;
public IntPtr LastWindow;
public IntPtr LastTopLevel;
public bool WillAccept;
public void Reset ()
{
State = DragState.None;
Data = null;
SupportedTypes = null;
WillAccept = false;
}
}
private class DropData {
}
// This version seems to be the most common
private static readonly IntPtr [] XdndVersion = new IntPtr [] { new IntPtr (4) };
private IntPtr display;
private DragData drag_data;
private IntPtr XdndAware;
private IntPtr XdndSelection;
private IntPtr XdndEnter;
private IntPtr XdndLeave;
private IntPtr XdndPosition;
private IntPtr XdndDrop;
private IntPtr XdndFinished;
private IntPtr XdndStatus;
private IntPtr XdndTypeList;
private IntPtr XdndActionCopy;
private IntPtr XdndActionMove;
private IntPtr XdndActionLink;
private IntPtr XdndActionPrivate;
private IntPtr XdndActionList;
private IntPtr XdndActionDescription;
private IntPtr XdndActionAsk;
private State state;
private int converts_pending;
private bool position_recieved;
private bool status_sent;
private IntPtr target;
private IntPtr source;
private IntPtr toplevel;
private DataObject data;
private IntPtr drag_action;
private Control control;
private int pos_x, pos_y;
private DragDropEffects allowed;
private DragEventArgs drag_event;
private Cursor CursorNo;
private Cursor CursorCopy;
private Cursor CursorMove;
private Cursor CursorLink;
private IntPtr CurrentCursorHandle;
public X11Dnd (IntPtr display)
{
this.display = display;
Init ();
}
public void SetAllowDrop (Hwnd hwnd, bool allow)
{
int[] atoms;
if (hwnd.allow_drop == allow)
return;
atoms = new int[XdndVersion.Length];
for (int i = 0; i < XdndVersion.Length; i++) {
atoms[i] = XdndVersion[i].ToInt32();
}
XplatUIX11.XChangeProperty (display, hwnd.whole_window, XdndAware,
(IntPtr) Atom.XA_ATOM, 32,
PropertyMode.Replace, atoms, allow ? 1 : 0);
hwnd.allow_drop = allow;
}
public DragDropEffects StartDrag (IntPtr handle, object data,
DragDropEffects allowed_effects)
{
drag_data = new DragData ();
drag_data.Window = handle;
drag_data.State = DragState.Beginning;
drag_data.MouseState = XplatUIX11.MouseState;
drag_data.Data = data;
drag_data.SupportedTypes = DetermineSupportedTypes (data);
drag_data.Action = ActionFromEffect (allowed_effects);
if (CursorNo == null) {
// Make sure the cursors are created
CursorNo = new Cursor (typeof (X11Dnd), "DnDNo.cur");
CursorCopy = new Cursor (typeof (X11Dnd), "DnDCopy.cur");
CursorMove = new Cursor (typeof (X11Dnd), "DnDMove.cur");
CursorLink = new Cursor (typeof (X11Dnd), "DnDLink.cur");
}
drag_data.LastTopLevel = IntPtr.Zero;
return DragDropEffects.Copy;
}
public void HandleButtonRelease (ref XEvent xevent)
{
if (drag_data == null)
return;
if (!((drag_data.MouseState == MouseButtons.Left &&
xevent.ButtonEvent.button == 1) ||
(drag_data.MouseState == MouseButtons.Right &&
xevent.ButtonEvent.button == 3)))
return;
if (drag_data.State == DragState.Beginning) {
state = State.Accepting;
} else if (drag_data.State != DragState.None) {
if (drag_data.WillAccept) {
SendDrop (drag_data.LastTopLevel, xevent.AnyEvent.window,
xevent.ButtonEvent.time);
}
XplatUIX11.XUngrabPointer (display, IntPtr.Zero);
drag_data.State = DragState.None;
// WE can't reset the drag data yet as it is still
// most likely going to be used by the SelectionRequest
// handlers
}
}
public bool HandleMotionNotify (ref XEvent xevent)
{
if (drag_data == null)
return false;
if (drag_data.State == DragState.Beginning) {
int suc;
drag_data.State = DragState.Dragging;
suc = XplatUIX11.XSetSelectionOwner (display, XdndSelection,
drag_data.Window,
xevent.ButtonEvent.time);
if (suc == 0) {
Console.Error.WriteLine ("Could not take ownership of XdndSelection aborting drag.");
drag_data.Reset ();
return false;
}
suc = XplatUIX11.XGrabPointer (display, xevent.AnyEvent.window,
false,
EventMask.ButtonMotionMask |
EventMask.PointerMotionMask |
EventMask.ButtonPressMask |
EventMask.ButtonReleaseMask,
GrabMode.GrabModeAsync,
GrabMode.GrabModeAsync,
IntPtr.Zero, IntPtr.Zero/*CursorCopy.Handle*/, IntPtr.Zero);
if (suc != 0) {
Console.Error.WriteLine ("Could not grab pointer aborting drag.");
drag_data.Reset ();
return false;
}
drag_data.State = DragState.Dragging;
} else if (drag_data.State != DragState.None) {
bool dnd_aware = false;
IntPtr toplevel = IntPtr.Zero;
IntPtr window = XplatUIX11.RootWindowHandle;
IntPtr root, child;
int x_temp, y_temp;
int mask_return;
while (XplatUIX11.XQueryPointer (display,
window,
out root, out child,
out x_temp, out y_temp,
out xevent.MotionEvent.x,
out xevent.MotionEvent.y,
out mask_return)) {
if (!dnd_aware) {
dnd_aware = IsWindowDndAware (window);
if (dnd_aware) {
toplevel = window;
xevent.MotionEvent.x_root = x_temp;
xevent.MotionEvent.y_root = y_temp;
}
}
if (child == IntPtr.Zero)
break;
window = child;
}
if (window != drag_data.LastWindow && drag_data.State == DragState.Entered) {
drag_data.State = DragState.Dragging;
// TODO: Send a Leave if this is an MWF window
if (toplevel != drag_data.LastTopLevel)
SendLeave (drag_data.LastTopLevel, xevent.MotionEvent.window);
}
drag_data.State = DragState.Entered;
if (toplevel != drag_data.LastTopLevel) {
// Entering a new toplevel window
SendEnter (toplevel, drag_data.Window, drag_data.SupportedTypes);
} else {
// Already in a toplevel window, so send a position
SendPosition (toplevel, drag_data.Window,
drag_data.Action,
xevent.MotionEvent.x_root,
xevent.MotionEvent.y_root,
xevent.MotionEvent.time);
}
drag_data.LastTopLevel = toplevel;
drag_data.LastWindow = window;
return true;
}
return false;
}
// DEBUG CODE REMOVE
private string GetText (IntPtr handle) {
string text = String.Empty;
IntPtr textptr;
textptr = IntPtr.Zero;
XplatUIX11.XFetchName (display, handle, ref textptr);
if (textptr != IntPtr.Zero) {
text = Marshal.PtrToStringAnsi(textptr);
XplatUIX11.XFree (textptr);
}
return text;
}
// return true if the event is handled here
public bool HandleClientMessage (ref XEvent xevent)
{
// most common so we check it first
if (xevent.ClientMessageEvent.message_type == XdndPosition)
return Accepting_HandlePositionEvent (ref xevent);
if (xevent.ClientMessageEvent.message_type == XdndEnter)
return Accepting_HandleEnterEvent (ref xevent);
if (xevent.ClientMessageEvent.message_type == XdndDrop)
return Accepting_HandleDropEvent (ref xevent);
if (xevent.ClientMessageEvent.message_type == XdndLeave)
return Accepting_HandleLeaveEvent (ref xevent);
if (xevent.ClientMessageEvent.message_type == XdndStatus)
return HandleStatusEvent (ref xevent);
return false;
}
public bool HandleSelectionNotifyEvent (ref XEvent xevent)
{
if (source != XplatUIX11.XGetSelectionOwner (display, XdndSelection))
return false;
MimeHandler handler = FindHandler ((IntPtr) xevent.SelectionEvent.target);
if (handler == null)
return false;
if (data == null)
data = new DataObject ();
handler.Converter.GetData (this, data, ref xevent);
converts_pending--;
if (converts_pending <= 0 && position_recieved) {
drag_event = new DragEventArgs (data, 0, pos_x, pos_y,
allowed, DragDropEffects.None);
control.DndEnter (drag_event);
SendStatus (source, drag_event.Effect);
status_sent = true;
}
return true;
}
public bool HandleSelectionRequestEvent (ref XEvent xevent)
{
if (xevent.SelectionRequestEvent.selection != XdndSelection)
return false;
MimeHandler handler = FindHandler (xevent.SelectionRequestEvent.target);
if (handler == null)
return false;
handler.Converter.SetData (this, drag_data.Data, ref xevent);
return true;
}
private void SetProperty (ref XEvent xevent, IntPtr data, int length)
{
XEvent sel = new XEvent();
sel.SelectionEvent.type = XEventName.SelectionNotify;
sel.SelectionEvent.send_event = true;
sel.SelectionEvent.display = display;
sel.SelectionEvent.selection = xevent.SelectionRequestEvent.selection;
sel.SelectionEvent.target = xevent.SelectionRequestEvent.target;
sel.SelectionEvent.requestor = xevent.SelectionRequestEvent.requestor;
sel.SelectionEvent.time = xevent.SelectionRequestEvent.time;
sel.SelectionEvent.property = IntPtr.Zero;
XplatUIX11.XChangeProperty (display, xevent.SelectionRequestEvent.requestor,
xevent.SelectionRequestEvent.property,
xevent.SelectionRequestEvent.target,
8, PropertyMode.Replace, data, length);
sel.SelectionEvent.property = xevent.SelectionRequestEvent.property;
XplatUIX11.XSendEvent (display, xevent.SelectionRequestEvent.requestor, false,
(IntPtr)EventMask.NoEventMask, ref sel);
return;
}
private void Reset ()
{
ResetSourceData ();
ResetTargetData ();
}
private void ResetSourceData ()
{
converts_pending = 0;
data = null;
}
private void ResetTargetData ()
{
position_recieved = false;
status_sent = false;
}
private bool Accepting_HandleEnterEvent (ref XEvent xevent)
{
Reset ();
source = xevent.ClientMessageEvent.ptr1;
toplevel = xevent.AnyEvent.window;
target = IntPtr.Zero;
ConvertData (ref xevent);
return true;
}
private bool Accepting_HandlePositionEvent (ref XEvent xevent)
{
pos_x = (int) xevent.ClientMessageEvent.ptr3 >> 16;
pos_y = (int) xevent.ClientMessageEvent.ptr3 & 0xFFFF;
allowed = EffectFromAction (xevent.ClientMessageEvent.ptr5);
IntPtr parent, child, new_child;
parent = XplatUIX11.XRootWindow (display, 0);
child = toplevel;
while (true) {
int xd, yd;
new_child = IntPtr.Zero;
if (!XplatUIX11.XTranslateCoordinates (display,
parent, child, pos_x, pos_y,
out xd, out yd, out new_child))
break;
if (new_child == IntPtr.Zero)
break;
child = new_child;
}
if (target != child) {
// We have moved into a new control
// or into a control for the first time
Finish ();
}
target = child;
Hwnd hwnd = Hwnd.ObjectFromHandle (target);
Control c = Control.FromHandle (hwnd.client_window);
if (c == null)
return true;
if (!c.allow_drop) {
SendStatus (source, DragDropEffects.None);
Finish ();
return true;
}
control = c;
position_recieved = true;
if (converts_pending > 0)
return true;
if (!status_sent) {
drag_event = new DragEventArgs (data, 0, pos_x, pos_y,
allowed, DragDropEffects.None);
control.DndEnter (drag_event);
SendStatus (source, drag_event.Effect);
status_sent = true;
} else {
drag_event.x = pos_x;
drag_event.y = pos_y;
SendStatus (source, drag_event.Effect);
control.DndOver (drag_event);
}
return true;
}
private void Finish ()
{
if (control != null) {
if (drag_event == null) {
if (data == null)
data = new DataObject ();
drag_event = new DragEventArgs (data,
0, pos_x, pos_y,
allowed, DragDropEffects.None);
}
control.DndLeave (drag_event);
}
ResetTargetData ();
}
private bool Accepting_HandleDropEvent (ref XEvent xevent)
{
if (control != null && drag_event != null) {
drag_event = new DragEventArgs (data,
0, pos_x, pos_y,
allowed, DragDropEffects.None);
control.DndDrop (drag_event);
}
SendFinished ();
return true;
}
private bool Accepting_HandleLeaveEvent (ref XEvent xevent)
{
if (control != null && drag_event != null)
control.DndLeave (drag_event);
// Reset ();
return true;
}
private bool HandleStatusEvent (ref XEvent xevent)
{
if (drag_data != null && drag_data.State == DragState.Entered) {
drag_data.WillAccept = ((int) xevent.ClientMessageEvent.ptr2 & 0x1) != 0;
Cursor cursor = CursorNo;
if (drag_data.WillAccept) {
// Same order as on MS
IntPtr action = xevent.ClientMessageEvent.ptr5;
if (action == XdndActionCopy)
cursor = CursorCopy;
else if (action == XdndActionLink)
cursor = CursorLink;
else if (action == XdndActionMove)
cursor = CursorMove;
}
// TODO: Try not to set the cursor so much
//if (cursor.Handle != CurrentCursorHandle) {
XplatUIX11.XChangeActivePointerGrab (display,
EventMask.ButtonMotionMask |
EventMask.PointerMotionMask |
EventMask.ButtonPressMask |
EventMask.ButtonReleaseMask,
cursor.Handle, IntPtr.Zero);
CurrentCursorHandle = cursor.Handle;
//}
}
return true;
}
private DragDropEffects EffectFromAction (IntPtr action)
{
DragDropEffects allowed = DragDropEffects.None;
if (action == XdndActionCopy)
allowed = DragDropEffects.Copy;
else if (action == XdndActionMove)
allowed |= DragDropEffects.Move;
if (action == XdndActionLink)
allowed |= DragDropEffects.Link;
return allowed;
}
private IntPtr ActionFromEffect (DragDropEffects effect)
{
IntPtr action = IntPtr.Zero;
// We can't OR together actions on XDND so sadly the primary
// is the only one shown here
if ((effect & DragDropEffects.Copy) != 0)
action = XdndActionCopy;
else if ((effect & DragDropEffects.Move) != 0)
action = XdndActionMove;
else if ((effect & DragDropEffects.Link) != 0)
action = XdndActionLink;
return action;
}
private bool ConvertData (ref XEvent xevent)
{
bool match = false;
if (source != XplatUIX11.XGetSelectionOwner (display, XdndSelection)) {
return false;
}
Control mwfcontrol = MwfWindow (source);
if (mwfcontrol != null && drag_data != null) {
DataObject dragged = drag_data.Data as DataObject;
if (dragged != null) {
data = dragged;
} else {
if (data == null)
data = new DataObject ();
SetDataWithFormats (drag_data.Data);
}
return true;
}
foreach (IntPtr atom in SourceSupportedList (ref xevent)) {
MimeHandler handler = FindHandler (atom);
if (handler == null)
continue;
XplatUIX11.XConvertSelection (display, XdndSelection, handler.Type,
handler.NonProtocol, toplevel, IntPtr.Zero /* CurrentTime */);
converts_pending++;
match = true;
}
return match;
}
private void SetDataWithFormats (object value)
{
if (value is string) {
data.SetData (DataFormats.Text, value);
data.SetData (DataFormats.UnicodeText, value);
}
data.SetData (value);
}
private MimeHandler FindHandler (IntPtr atom)
{
if (atom == IntPtr.Zero)
return null;
foreach (MimeHandler handler in MimeHandlers) {
if (handler.Type == atom)
return handler;
}
return null;
}
private MimeHandler FindHandler (string name)
{
foreach (MimeHandler handler in MimeHandlers) {
if (handler.Name == name)
return handler;
}
return null;
}
private void SendStatus (IntPtr source, DragDropEffects effect)
{
XEvent xevent = new XEvent ();
xevent.AnyEvent.type = XEventName.ClientMessage;
xevent.AnyEvent.display = display;
xevent.ClientMessageEvent.window = source;
xevent.ClientMessageEvent.message_type = XdndStatus;
xevent.ClientMessageEvent.format = 32;
xevent.ClientMessageEvent.ptr1 = toplevel;
if (effect != DragDropEffects.None)
xevent.ClientMessageEvent.ptr2 = (IntPtr) 1;
xevent.ClientMessageEvent.ptr5 = ActionFromEffect (effect);
XplatUIX11.XSendEvent (display, source, false, IntPtr.Zero, ref xevent);
}
private void SendEnter (IntPtr handle, IntPtr from, IntPtr [] supported)
{
XEvent xevent = new XEvent ();
xevent.AnyEvent.type = XEventName.ClientMessage;
xevent.AnyEvent.display = display;
xevent.ClientMessageEvent.window = handle;
xevent.ClientMessageEvent.message_type = XdndEnter;
xevent.ClientMessageEvent.format = 32;
xevent.ClientMessageEvent.ptr1 = from;
// (int) xevent.ClientMessageEvent.ptr2 & 0x1)
// int ptr2 = 0x1;
// xevent.ClientMessageEvent.ptr2 = (IntPtr) ptr2;
// (e)->xclient.data.l[1] = ((e)->xclient.data.l[1] & ~(0xFF << 24)) | ((v) << 24)
xevent.ClientMessageEvent.ptr2 = (IntPtr) ((long)XdndVersion [0] << 24);
if (supported.Length > 0)
xevent.ClientMessageEvent.ptr3 = supported [0];
if (supported.Length > 1)
xevent.ClientMessageEvent.ptr4 = supported [1];
if (supported.Length > 2)
xevent.ClientMessageEvent.ptr5 = supported [2];
XplatUIX11.XSendEvent (display, handle, false, IntPtr.Zero, ref xevent);
}
private void SendDrop (IntPtr handle, IntPtr from, IntPtr time)
{
XEvent xevent = new XEvent ();
xevent.AnyEvent.type = XEventName.ClientMessage;
xevent.AnyEvent.display = display;
xevent.ClientMessageEvent.window = handle;
xevent.ClientMessageEvent.message_type = XdndDrop;
xevent.ClientMessageEvent.format = 32;
xevent.ClientMessageEvent.ptr1 = from;
xevent.ClientMessageEvent.ptr3 = time;
XplatUIX11.XSendEvent (display, handle, false, IntPtr.Zero, ref xevent);
}
private void SendPosition (IntPtr handle, IntPtr from, IntPtr action, int x, int y, IntPtr time)
{
XEvent xevent = new XEvent ();
xevent.AnyEvent.type = XEventName.ClientMessage;
xevent.AnyEvent.display = display;
xevent.ClientMessageEvent.window = handle;
xevent.ClientMessageEvent.message_type = XdndPosition;
xevent.ClientMessageEvent.format = 32;
xevent.ClientMessageEvent.ptr1 = from;
xevent.ClientMessageEvent.ptr3 = (IntPtr) ((x << 16) | (y & 0xFFFF));
xevent.ClientMessageEvent.ptr4 = time;
xevent.ClientMessageEvent.ptr5 = action;
XplatUIX11.XSendEvent (display, handle, false, IntPtr.Zero, ref xevent);
}
private void SendLeave (IntPtr handle, IntPtr from)
{
XEvent xevent = new XEvent ();
xevent.AnyEvent.type = XEventName.ClientMessage;
xevent.AnyEvent.display = display;
xevent.ClientMessageEvent.window = handle;
xevent.ClientMessageEvent.message_type = XdndLeave;
xevent.ClientMessageEvent.format = 32;
xevent.ClientMessageEvent.ptr1 = from;
XplatUIX11.XSendEvent (display, handle, false, IntPtr.Zero, ref xevent);
}
private void SendFinished ()
{
XEvent xevent = new XEvent ();
xevent.AnyEvent.type = XEventName.ClientMessage;
xevent.AnyEvent.display = display;
xevent.ClientMessageEvent.window = source;
xevent.ClientMessageEvent.message_type = XdndFinished;
xevent.ClientMessageEvent.format = 32;
xevent.ClientMessageEvent.ptr1 = toplevel;
XplatUIX11.XSendEvent (display, source, false, IntPtr.Zero, ref xevent);
}
// There is a somewhat decent amount of overhead
// involved in setting up dnd so we do it lazily
// as a lot of applications do not even use it.
private void Init ()
{
XdndAware = XplatUIX11.XInternAtom (display, "XdndAware", false);
XdndEnter = XplatUIX11.XInternAtom (display, "XdndEnter", false);
XdndLeave = XplatUIX11.XInternAtom (display, "XdndLeave", false);
XdndPosition = XplatUIX11.XInternAtom (display, "XdndPosition", false);
XdndStatus = XplatUIX11.XInternAtom (display, "XdndStatus", false);
XdndDrop = XplatUIX11.XInternAtom (display, "XdndDrop", false);
XdndSelection = XplatUIX11.XInternAtom (display, "XdndSelection", false);
XdndFinished = XplatUIX11.XInternAtom (display, "XdndFinished", false);
XdndTypeList = XplatUIX11.XInternAtom (display, "XdndTypeList", false);
XdndActionCopy = XplatUIX11.XInternAtom (display, "XdndActionCopy", false);
XdndActionMove = XplatUIX11.XInternAtom (display, "XdndActionMove", false);
XdndActionLink = XplatUIX11.XInternAtom (display, "XdndActionLink", false);
XdndActionPrivate = XplatUIX11.XInternAtom (display, "XdndActionPrivate", false);
XdndActionList = XplatUIX11.XInternAtom (display, "XdndActionList", false);
XdndActionDescription = XplatUIX11.XInternAtom (display, "XdndActionDescription", false);
XdndActionAsk = XplatUIX11.XInternAtom (display, "XdndActionAsk", false);
foreach (MimeHandler handler in MimeHandlers) {
handler.Type = XplatUIX11.XInternAtom (display, handler.Name, false);
handler.NonProtocol = XplatUIX11.XInternAtom (display,
String.Concat ("MWFNonP+", handler.Name), false);
}
}
private IntPtr [] SourceSupportedList (ref XEvent xevent)
{
IntPtr [] res;
if (((int) xevent.ClientMessageEvent.ptr2 & 0x1) == 0) {
res = new IntPtr [3];
res [0] = xevent.ClientMessageEvent.ptr3;
res [1] = xevent.ClientMessageEvent.ptr4;
res [2] = xevent.ClientMessageEvent.ptr5;
} else {
IntPtr type;
int format;
IntPtr count;
IntPtr remaining;
IntPtr data = IntPtr.Zero;
XplatUIX11.XGetWindowProperty (display, source, XdndTypeList,
IntPtr.Zero, new IntPtr(32), false, (IntPtr) Atom.XA_ATOM,
out type, out format, out count,
out remaining, ref data);
res = new IntPtr [count.ToInt32()];
for (int i = 0; i < count.ToInt32(); i++) {
res [i] = (IntPtr) Marshal.ReadInt32 (data, i *
Marshal.SizeOf (typeof (int)));
}
XplatUIX11.XFree (data);
}
return res;
}
private string GetText (ref XEvent xevent, bool unicode)
{
int nread = 0;
IntPtr nitems;
IntPtr bytes_after;
StringBuilder builder = new StringBuilder ();
do {
IntPtr actual_type;
int actual_fmt;
IntPtr data = IntPtr.Zero;
if (0 != XplatUIX11.XGetWindowProperty (display,
xevent.AnyEvent.window,
(IntPtr) xevent.SelectionEvent.property,
IntPtr.Zero, new IntPtr(0xffffff), false,
(IntPtr) Atom.AnyPropertyType, out actual_type,
out actual_fmt, out nitems, out bytes_after,
ref data)) {
XplatUIX11.XFree (data);
break;
}
if (unicode)
builder.Append (Marshal.PtrToStringUni (data));
else
builder.Append (Marshal.PtrToStringAnsi (data));
nread += nitems.ToInt32();
XplatUIX11.XFree (data);
} while (bytes_after.ToInt32() > 0);
if (nread == 0)
return null;
return builder.ToString ();
}
private MemoryStream GetData (ref XEvent xevent)
{
int nread = 0;
IntPtr nitems;
IntPtr bytes_after;
MemoryStream res = new MemoryStream ();
do {
IntPtr actual_type;
int actual_fmt;
IntPtr data = IntPtr.Zero;
if (0 != XplatUIX11.XGetWindowProperty (display,
xevent.AnyEvent.window,
(IntPtr) xevent.SelectionEvent.property,
IntPtr.Zero, new IntPtr(0xffffff), false,
(IntPtr) Atom.AnyPropertyType, out actual_type,
out actual_fmt, out nitems, out bytes_after,
ref data)) {
XplatUIX11.XFree (data);
break;
}
for (int i = 0; i < nitems.ToInt32(); i++)
res.WriteByte (Marshal.ReadByte (data, i));
nread += nitems.ToInt32();
XplatUIX11.XFree (data);
} while (bytes_after.ToInt32() > 0);
return res;
}
private Control MwfWindow (IntPtr window)
{
Hwnd hwnd = Hwnd.ObjectFromHandle (window);
if (hwnd == null)
return null;
Control res = Control.FromHandle (hwnd.client_window);
return res;
}
private bool IsWindowDndAware (IntPtr handle)
{
bool res = true;
// Check the version number, we need greater than 3
IntPtr actual;
int format;
IntPtr count;
IntPtr remaining;
IntPtr data = IntPtr.Zero;
XplatUIX11.XGetWindowProperty (display, handle, XdndAware, IntPtr.Zero, new IntPtr(0x8000000), false,
(IntPtr) Atom.XA_ATOM, out actual, out format,
out count, out remaining, ref data);
if (actual != (IntPtr) Atom.XA_ATOM || format != 32 ||
count.ToInt32() == 0 || data == IntPtr.Zero) {
if (data != IntPtr.Zero)
XplatUIX11.XFree (data);
return false;
}
int version = Marshal.ReadInt32 (data, 0);
if (version < 3) {
Console.Error.WriteLine ("XDND Version too old (" + version + ").");
XplatUIX11.XFree (data);
return false;
}
// First type is actually the XDND version
if (count.ToInt32() > 1) {
res = false;
for (int i = 1; i < count.ToInt32(); i++) {
IntPtr type = (IntPtr) Marshal.ReadInt32 (data, i *
Marshal.SizeOf (typeof (int)));
for (int j = 0; j < drag_data.SupportedTypes.Length; j++) {
if (drag_data.SupportedTypes [j] == type) {
res = true;
break;
}
}
}
}
XplatUIX11.XFree (data);
return res;
}
private IntPtr [] DetermineSupportedTypes (object data)
{
ArrayList res = new ArrayList ();
if (data is string) {
MimeHandler handler = FindHandler ("text/plain");
if (handler != null)
res.Add (handler.Type);
}/* else if (data is Bitmap)
res.Add (data);
*/
if (data is IDataObject) {
}
if (data is ISerializable) {
MimeHandler handler = FindHandler ("application/x-mono-serialized-object");
if (handler != null)
res.Add (handler.Type);
}
return (IntPtr []) res.ToArray (typeof (IntPtr));
}
}
}
|