This repository was archived by the owner on Jul 22, 2023. It is now read-only.
forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventbinding.cs
More file actions
132 lines (108 loc) · 3.44 KB
/
eventbinding.cs
File metadata and controls
132 lines (108 loc) · 3.44 KB
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
using System;
namespace Python.Runtime
{
/// <summary>
/// Implements a Python event binding type, similar to a method binding.
/// </summary>
[Serializable]
internal class EventBinding : ExtensionType
{
private EventObject e;
private IntPtr target;
public EventBinding(EventObject e, IntPtr target)
{
Runtime.XIncref(target);
this.target = target;
this.e = e;
}
/// <summary>
/// EventBinding += operator implementation.
/// </summary>
public static IntPtr nb_inplace_add(IntPtr ob, IntPtr arg)
{
var self = (EventBinding)GetManagedObject(ob);
if (Runtime.PyCallable_Check(arg) < 1)
{
Exceptions.SetError(Exceptions.TypeError, "event handlers must be callable");
return IntPtr.Zero;
}
if (!self.e.AddEventHandler(self.target, arg))
{
return IntPtr.Zero;
}
Runtime.XIncref(self.pyHandle);
return self.pyHandle;
}
/// <summary>
/// EventBinding -= operator implementation.
/// </summary>
public static IntPtr nb_inplace_subtract(IntPtr ob, IntPtr arg)
{
var self = (EventBinding)GetManagedObject(ob);
if (Runtime.PyCallable_Check(arg) < 1)
{
Exceptions.SetError(Exceptions.TypeError, "invalid event handler");
return IntPtr.Zero;
}
if (!self.e.RemoveEventHandler(self.target, arg))
{
return IntPtr.Zero;
}
Runtime.XIncref(self.pyHandle);
return self.pyHandle;
}
/// <summary>
/// EventBinding __hash__ implementation.
/// </summary>
public static IntPtr tp_hash(IntPtr ob)
{
var self = (EventBinding)GetManagedObject(ob);
long x = 0;
long y = 0;
if (self.target != IntPtr.Zero)
{
x = Runtime.PyObject_Hash(self.target).ToInt64();
if (x == -1)
{
return new IntPtr(-1);
}
}
y = Runtime.PyObject_Hash(self.e.pyHandle).ToInt64();
if (y == -1)
{
return new IntPtr(-1);
}
x ^= y;
if (x == -1)
{
x = -1;
}
return new IntPtr(x);
}
/// <summary>
/// EventBinding __repr__ implementation.
/// </summary>
public static IntPtr tp_repr(IntPtr ob)
{
var self = (EventBinding)GetManagedObject(ob);
string type = self.target == IntPtr.Zero ? "unbound" : "bound";
string s = string.Format("<{0} event '{1}'>", type, self.e.name);
return Runtime.PyString_FromString(s);
}
/// <summary>
/// EventBinding dealloc implementation.
/// </summary>
public new static void tp_dealloc(IntPtr ob)
{
var self = (EventBinding)GetManagedObject(ob);
Runtime.XDecref(self.target);
self.Dealloc();
}
public static int tp_clear(IntPtr ob)
{
var self = (EventBinding)GetManagedObject(ob);
Runtime.Py_CLEAR(ref self.target);
return 0;
}
}
}