summaryrefslogtreecommitdiffstats
path: root/qtsharp/src/bindings/static/QtSlot.cs
blob: 6bf14da8ef94fa6c1f9237ea3f48424d08aeaf16 (plain)
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
// QtSlot.cs - Represents C# slot connections
//
// Copyright (C) 2002  Nick Zigarovich (nick@chemlab.org)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

namespace Qt {
	using System;
	using System.Reflection;
	using System.Text;

	internal class QtSlot {
		protected QObject receiver;
		protected string slot;
		private string name;
		private string args;
		protected MethodInfo slotInfo;
		private IntPtr wrapperPtr;
		private Delegate callback;

		public QObject Receiver {
			get { return receiver; }
		}

		public string Slot {
			get { return slot; }
		}

		public MethodInfo Info {
			get { return slotInfo; }
		}

		public string Identifier {
			get { return receiver.RawObject.ToString()+slot; }
		}

		public string Name {
			get { return name; }
		}

		public string Args {
			get { return args; }
		}

		public string NormalizedArgs {
			get { return NormalizeSlotArgs(args); }
		}

		public IntPtr WrapperPtr {
			get { return wrapperPtr; }
			set { wrapperPtr = value; }
		}

		public Delegate Callback {
			get { return callback; }
			set { callback = value; }
		}

		public bool IsCSharpSlot {
			get { return slotInfo != null; }
		}

		public QtSlot() {}

		public QtSlot(QObject receiver, string slot)
		{
			this.receiver = receiver;
			this.slot = slot;
			string[] slotsplit = slot.Split(new char[] {'('});

			try {
				name = slotsplit[0];
				args = "("+slotsplit[1];
			}
			catch (IndexOutOfRangeException) {
				throw new ArgumentException ("Invalid slot format: " + slot);
			}

			slotInfo = LookupSlot(receiver, name, NormalizedArgs);
			wrapperPtr = IntPtr.Zero;
			callback = null;
		}

		public void SlotBridge_QString (IntPtr value0)
		{
			QString csvalue0 = QtSupport.LookupObject (value0, typeof (QString)) as QString;
		
			Invoke (new object[] {csvalue0});
		}
		
		public void SlotBridge_QDockWindow (IntPtr value0)
		{
			QDockWindow csvalue0 = QtSupport.LookupObject (value0, typeof (QDockWindow)) as QDockWindow;
		
			Invoke (new object[] {csvalue0});
		}
		
		public void SlotBridge_QDropEvent (IntPtr value0)
		{
			QDropEvent csvalue0 = QtSupport.LookupObject (value0, typeof (QDropEvent)) as QDropEvent;
		
			Invoke (new object[] {csvalue0});
		}
		
		public void SlotBridge_QDropEventQListViewItem (IntPtr value0, IntPtr value1)
		{
			QDropEvent csvalue0 = QtSupport.LookupObject (value0, typeof (QDropEvent)) as QDropEvent;
			QListViewItem csvalue1 = QtSupport.LookupObject (value1, typeof (QListViewItem)) as QListViewItem;
		
			Invoke (new object[] {csvalue0, csvalue1});
		}
		
		public void SlotBridge_QIconViewItem (IntPtr value0)
		{
			QIconViewItem csvalue0 = QtSupport.LookupObject (value0, typeof (QIconViewItem)) as QIconViewItem;
		
			Invoke (new object[] {csvalue0});
		}
		
		public void SlotBridge_QListBoxItem (IntPtr value0)
		{
			QListBoxItem csvalue0 = QtSupport.LookupObject (value0, typeof (QListBoxItem)) as QListBoxItem;
		
			Invoke (new object[] {csvalue0});
		}
		
		public void SlotBridge_QListViewItem (IntPtr value0)
		{
			QListViewItem csvalue0 = QtSupport.LookupObject (value0, typeof (QListViewItem)) as QListViewItem;
		
			Invoke (new object[] {csvalue0});
		}
		
		public void SlotBridge_QListViewItemQListViewItem (IntPtr value0, IntPtr value1)
		{
			QListViewItem csvalue0 = QtSupport.LookupObject (value0, typeof (QListViewItem)) as QListViewItem;
			QListViewItem csvalue1 = QtSupport.LookupObject (value1, typeof (QListViewItem)) as QListViewItem;
		
			Invoke (new object[] {csvalue0, csvalue1});
		}
		
		public void SlotBridge_QListViewItemQListViewItemQListViewItem (IntPtr value0, IntPtr value1, IntPtr value2)
		{
			QListViewItem csvalue0 = QtSupport.LookupObject (value0, typeof (QListViewItem)) as QListViewItem;
			QListViewItem csvalue1 = QtSupport.LookupObject (value1, typeof (QListViewItem)) as QListViewItem;
			QListViewItem csvalue2 = QtSupport.LookupObject (value2, typeof (QListViewItem)) as QListViewItem;

			Invoke (new object[] {csvalue0, csvalue1, csvalue2});
		}
		
		public void SlotBridge_QNetworkOperation (IntPtr value0)
		{
			QNetworkOperation csvalue0 = QtSupport.LookupObject (value0, typeof (QNetworkOperation)) as QNetworkOperation;
		
			Invoke (new object[] {csvalue0});
		}
		
		public void SlotBridge_QObject (IntPtr value0)
		{
			QObject csvalue0 = QtSupport.LookupObject (value0, typeof (QObject)) as QObject;
		
			Invoke (new object[] {csvalue0});
		}
		
		public void SlotBridge_QToolBar (IntPtr value0)
		{
			QToolBar csvalue0 = QtSupport.LookupObject (value0, typeof (QToolBar)) as QToolBar;
		
			Invoke (new object[] {csvalue0});
		}
		
		public void SlotBridge_QWidget (IntPtr value0)
		{
			QWidget csvalue0 = QtSupport.LookupObject (value0, typeof (QWidget)) as QWidget;
		
			Invoke (new object[] {csvalue0});
		}
		
		public void SlotBridge_intQIconViewItem (int value0, IntPtr value1)
		{
			QIconViewItem csvalue1 = QtSupport.LookupObject (value1, typeof (QIconViewItem)) as QIconViewItem;

			Invoke (new object[] {value0, csvalue1});
		}

		public void SlotBridge_intQListBoxItem (int value0, IntPtr value1)
		{
			QListBoxItem csvalue1 = QtSupport.LookupObject (value1, typeof (QListBoxItem)) as QListBoxItem;

			Invoke (new object[] {value0, csvalue1});
		}

		public void Invoke(object[] args)
		{
			if (slotInfo != null) {
				slotInfo.Invoke(receiver, args);
			}
		}

		private static MethodInfo LookupSlot(QObject receiver, string name, string args)
		{
			MethodInfo ret = null;

			foreach (MethodInfo info in receiver.GetType().GetMethods(
				BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance ))
			{
				string infoargs = "(" + ShortArgs (info.ToString().Split(new char[] {'('})[1].Replace(" ", "")) + ")";
				string nargs = args.Replace ("string", "String"); // FIXME

				if (nargs.CompareTo(infoargs) == 0 &&
					name.CompareTo(info.Name) == 0)
				{
					ret = info;
					break;
				}
			}

			return ret;
		}

		private static string ShortArgs (string args)
		{
			StringBuilder builder = new StringBuilder ();
			string [] s = args.TrimEnd (')').Split (',');
			foreach (string st in s) {
				string [] str = st.Trim (' ').Split ('.');
				string stri = str [str.Length -1];
				builder.Append (stri+", ");
			}
			return builder.ToString ().TrimEnd (',', ' ');
		}

		private static string NormalizeSlotArgs(string methodargs)
		{
			string[] args = methodargs.Replace("(", "").Replace(")", "").Split(new char[] {','});
			StringBuilder sb = new StringBuilder();
			sb.Append("(");
			int count = 0;

			// FIXME Handle unsigned, bool, and byte cases. Would lookups in a hashtable be faster?
			foreach (string arg in args) {
				if (count > 0) sb.Append(",");

				if (arg == "short")
					sb.Append("Int16");
				else if (arg == "int")
					sb.Append("Int32");
				else if (arg == "long")
					sb.Append("Int64");
				else if (arg == "float")
					sb.Append("Single");
				else if (arg == "double")
					sb.Append("Double");
				else if (arg == "char")
					sb.Append("Char");
				else
					sb.Append(arg);

				count++;
			}

			sb.Append(")");
			return sb.ToString();
		}

		// For ArrayList storage.
		public override bool Equals(object obj)
		{
			return ((QtSlot)obj).Identifier == Identifier;
		}
	}
}