File: APILookupGObject.txt

package info (click to toggle)
glib-d 2.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 252 kB
  • sloc: makefile: 5
file content (1157 lines) | stat: -rw-r--r-- 35,793 bytes parent folder | download | duplicates (2)
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
# This file is part of gtkD.
#
# gtkD is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# gtkD 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with gtkD; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

#############################################
### Definitions for wrapping Gtk+ ###########
#############################################

# must start with wrap
wrap: gobject
file: GObject-2.0.gir

noAlias: Type
addEnums: start

	/**
	 * A value which represents the unique identifier of a registered type.
	 */
	enum GType : size_t
	{
		INVALID = 0<<2,
		NONE = 1<<2,
		INTERFACE = 2<<2,
		CHAR = 3<<2,
		UCHAR = 4<<2,
		BOOLEAN = 5<<2,
		INT = 6<<2,
		UINT = 7<<2,
		LONG = 8<<2,
		ULONG = 9<<2,
		INT64 = 10<<2,
		UINT64 = 11<<2,
		ENUM = 12<<2,
		FLAGS = 13<<2,
		FLOAT = 14<<2,
		DOUBLE = 15<<2,
		STRING = 16<<2,
		POINTER = 17<<2,
		BOXED = 18<<2,
		PARAM = 19<<2,
		OBJECT = 20<<2,
		VARIANT = 21<<2,
	}
addEnums: end

struct: CClosure
class: CClosure
import: glib.ConstructionException
import: gobject.ObjectG
noCode: new
noCode: new_swap
noCode: new_object
noCode: new_object_swap
code: start
	/**
	 * Creates a new closure which invokes callbackFunc with userData as
	 * the last parameter.
	 *
	 * Params:
	 *     callbackFunc = the function to invoke
	 *     userData = user data to pass to callbackFunc
	 *     destroyData = destroy notify to be called when userData is no longer used
	 *     swap = if true invoce with usrData as the first parameter
	 *
	 * Throws: ConstructionException GTK+ fails to create the object.
	 */
	public this(GCallback callbackFunc, void* userData, GClosureNotify destroyData, bool swap)
	{
		GClosure* p;

		if ( swap )
			p = g_cclosure_new_swap(callbackFunc, userData, destroyData);
		else
			p = g_cclosure_new(callbackFunc, userData, destroyData);

		if(p is null)
		{
			throw new ConstructionException("null returned by new");
		}

		this(cast(GCClosure*) p);
	}

	/**
	 * A variant of this() which uses object as userData and
	 * calls ObjectG.watchClosure() on object and the created
	 * closure. This function is useful when you have a callback closely
	 * associated with a gobject.ObjectG, and want the callback to no longer run
	 * after the object is is freed.
	 *
	 * Params:
	 *     callbackFunc = the function to invoke
	 *     object = a gobject.ObjectG.ObjectG to pass to callbackFunc
	 *     swap = if true invoce with usrData as the first parameter
	 *
	 * Throws: ConstructionException GTK+ fails to create the object.
	 */
	public this(GCallback callbackFunc, ObjectG object, bool swap)
	{
		GClosure* p;

		if ( swap )
			p = g_cclosure_new_object_swap(callbackFunc, (object is null) ? null : object.getObjectGStruct());
		else
			p = g_cclosure_new_object(callbackFunc, (object is null) ? null : object.getObjectGStruct());

		if(p is null)
		{
			throw new ConstructionException("null returned by new_object");
		}

		this(cast(GCClosure*) p);
	}
code: end

struct: DClosure
namespace:
import: glib.Str
import: glib.Variant
import: gobject.c.functions
import: gobject.c.types
import: gobject.Closure
import: gobject.ObjectG
import: gobject.ParamSpec
import: core.memory
import: std.algorithm
import: std.conv
import: std.traits
import: std.typecons
code: start
	struct DGClosure(T)
	{
		GClosure closure;
		T callback;
	}

	/**
	* DClosure is a wrapper around the gobject library's GClosure with special handling for marshalling D delegates and function pointers as callbacks.
	*
	* Closures are central to the concept of asynchronous signal delivery which is widely used throughout GTK+ and GNOME applications.
	* A closure is an abstraction, a generic representation of a callback.
	*/
	class DClosure : Closure
	{
		private void* callback;

		/** Get the main Gtk struct */
		public GClosure* getDClosureStruct(bool transferOwnership = false)
		{
			if (transferOwnership)
				ownedRef = false;
			return gClosure;
		}

		/**
		* Sets our main struct and passes it to the parent class.
		*/
		public this (GClosure* gClosure, bool ownedRef = false)
		{
			super(gClosure, ownedRef);
		}

		/**
		* Create a new Closure that will call `callback` when it's invoked.
		*
		* Params:
		*     callback = a delegate or function to call when the DClosure is invoked.
		*     swap     = Should the first and last parameter passed to the callback be swapped.
		*                This is usefull when using the closure for a Signal, where the instance is
		*                 the first parameter, but when using delegates it usually isn't used.
		*/
		this(T)(T callback, bool swap = false)
		if ( isCallable!T )
		{
			GClosure* gClosure = g_closure_new_simple(DGClosure!(T).sizeof, null);
			g_closure_ref(gClosure);
			g_closure_sink(gClosure);
			g_closure_set_marshal(gClosure, &d_closure_marshal!T);
			if ( swap ) gClosure.derivativeFlag = true;

			auto dClosure = cast(DGClosure!(T)*)gClosure;
			dClosure.callback = callback;

			static if ( isDelegate!T )
				this.callback = callback.ptr;
			else static if ( isFunctionPointer!T )
				this.callback = callback;
			else
				this.callback = &callback;

			super(gClosure, true);
		}

		extern(C) static void d_closure_marshal(T)(GClosure* closure, GValue* return_value, uint n_param_values, /*const*/ GValue* param_values, void* invocation_hint, void* marshal_data)
		{
			DGClosure!(T)* cl = cast(DGClosure!(T)*)closure;

			if ( Parameters!(T).length > n_param_values )
				assert(false, "DClosure doesn't have enough parameters.");

			if ( closure.derivativeFlag )
			{
				GValue[] swapped = new GValue[n_param_values];
				swapped[0..n_param_values-1] = param_values[1..n_param_values];
				swapped[n_param_values-1] = param_values[0];
				param_values = swapped.ptr;
			}

			mixin(getCallbackCall!T());
		}

		private static string getCallbackCall(T)()
		{
			if (!__ctfe) assert(false);

			string call;

			alias Params = Parameters!T;
			foreach ( param; Params )
			{
				static if ( __traits(compiles, TemplateOf!param) && __traits(isSame, TemplateOf!param, glib.c.types.Scoped) )
					call ~= "import "~moduleName!(TemplateArgsOf!(param)[0])~";\n";
				else static if ( is(param == class) || is(param == interface) || is(param == struct) || is(param == enum) )
					call ~= "import "~moduleName!param~";\n";
				else static if ( isPointer!param && ( is(PointerTarget!param == struct) || is(PointerTarget!param == enum)) )
					//The moduleName template gives an forward reference error here.
				call ~= "import "~fullyQualifiedName!param.findSplitAfter(".c.types")[0]~";\n";
			}
			alias Ret = ReturnType!T;
			static if ( is(Ret == class) || is(Ret == interface) || is(Ret == struct) || is(Ret == enum) )
				call ~= "import "~moduleName!Ret~";\n";
			else static if ( isPointer!Ret && ( is(PointerTarget!Ret == struct) || is(PointerTarget!Ret == enum)) )
				call ~= "import "~fullyQualifiedName!Ret.findSplitAfter(".c.types")[0]~";\n";

			static if ( !is(Ret == void) )
				call ~= "auto ret = ";
			call ~= "cl.callback(";

			foreach ( i, param; Params )
			{
				if ( i > 0 )
					call ~= ", ";
				call ~= getValue!param(i);
			}
			call ~= ");\n";

			static if ( is(Ret == bool) )
				call ~= "g_value_set_boolean(return_value, ret);";
			else static if ( is(Ret == byte) )
				call ~= "g_value_set_schar(return_value, ret);";
			else static if ( is(Ret == ubyte) )
				call ~= "g_value_set_uchar(return_value, ret);";
			else static if ( is(Ret == int) )
				call ~= "g_value_set_int(return_value, ret);";
			else static if ( is(Ret == uint) )
				call ~= "g_value_set_uint(return_value, ret);";
			else static if ( is(Ret == long) )
				call ~= "g_value_set_int64(return_value, ret);";
			else static if ( is(Ret == ulong) )
				call ~= "g_value_set_uint64(return_value, ret);";
			else static if ( is(Ret == float) )
				call ~= "g_value_set_float(return_value, ret);";
			else static if ( is(Ret == double) )
				call ~= "g_value_set_double(return_value, ret);";
			else static if ( is(Ret == string) )
				call ~= "g_value_set_string(return_value, Str.toStringz(ret));";
			else static if ( is(Ret == string[]) )
				call ~= "g_value_set_pointer(return_value, Str.toStringzArray(ret));";
			else static if ( is(Ret == enum) )
				call ~= "g_type_is_a(return_value.gType, GType.ENUM) ? g_value_set_enum(return_value, ret) : g_value_set_flags(return_value, ret);";
			else static if ( isPointer!Ret )
				call ~= "g_type_is_a(return_value.gType, GType.POINTER) ? g_value_set_pointer(return_value, ret) : (g_type_is_a(return_value.gType, GType.BOXED) ? g_value_set_boxed(return_value, ret) : g_value_set_object(return_value, ret));";
			else static if ( is(Ret == interface) )
				call ~= "g_value_set_object(return_value, (cast(ObjectG)ret).getObjectGStruct());";
			else static if ( is(Ret == class) )
			{
				static if ( is(Ret == Variant) )
					call ~= "g_value_set_variant(return_value, ret.getVariantStruct());";
				else static if ( is(Ret == ParamSpec) )
					call ~= "g_value_set_param(return_value, ret.getParamSpecStruct());";
				else static if ( is(Ret : ObjectG) )
					call ~= "g_value_set_object(return_value, ret.getObjectGStruct());";
				else
					call ~= "g_type_is_a(return_value.gType, GType.POINTER) ? g_value_set_pointer(return_value, ret.get"~Ret.stringof~"Struct()) : (g_type_is_a(return_value.gType, GType.BOXED) ? g_value_set_boxed(return_value, ret.get"~Ret.stringof~"Struct()) : g_value_set_object(return_value, ret.get"~Ret.stringof~"Struct()));";
			}

			return call;
		}

		private static string getValue(Param)(int index)
		{
			if (!__ctfe) assert(false);

			static if ( is(Param == bool) )
				return "g_value_get_boolean(&param_values["~to!string(index)~"]) != 0";
			else static if ( is(Param == byte) )
				return "g_value_get_schar(&param_values["~to!string(index)~"])";
			else static if ( is(Param == ubyte) )
				return "g_value_get_uchar(&param_values["~to!string(index)~"])";
			else static if ( is(Param == int) )
				return "g_value_get_int(&param_values["~to!string(index)~"])";
			else static if ( is(Param == uint) )
				return "g_value_get_uint(&param_values["~to!string(index)~"])";
			else static if ( is(Param == long) )
				return "g_value_get_int64(&param_values["~to!string(index)~"])";
			else static if ( is(Param == ulong) )
				return "g_value_get_uint64(&param_values["~to!string(index)~"])";
			else static if ( is(Param == float) )
				return "g_value_get_float(&param_values["~to!string(index)~"])";
			else static if ( is(Param == double) )
				return "g_value_get_double(&param_values["~to!string(index)~"])";
			else static if ( is(Param == string) )
				return "Str.toString(g_value_get_string(&param_values["~to!string(index)~"]))";
			else static if ( is(Param == string[]) )
				return "Str.toStringArray(cast(const(char*)*)g_value_get_pointer(&param_values["~to!string(index)~"]))";
			else static if ( is(Param == enum) )
				return "cast("~fullyQualifiedName!Param~")(g_type_is_a(param_values["~to!string(index)~"].gType, GType.ENUM) ? g_value_get_enum(&param_values["~to!string(index)~"]) : g_value_get_flags(&param_values["~to!string(index)~"]))";
			else static if ( isPointer!Param )
				return "cast("~fullyQualifiedName!Param~")(g_type_is_a(param_values["~to!string(index)~"].gType, GType.POINTER) ? g_value_get_pointer(&param_values["~to!string(index)~"]) : (g_type_is_a(param_values["~to!string(index)~"].gType, GType.BOXED) ? g_value_get_boxed(&param_values["~to!string(index)~"]) : g_value_get_object(&param_values["~to!string(index)~"])))";
			else static if ( __traits(compiles, TemplateOf!Param) && __traits(isSame, TemplateOf!Param, glib.c.types.Scoped) )
				return "getScopedGobject!("~fullyQualifiedName!(TemplateArgsOf!(Param)[0])~")(cast(typeof("~fullyQualifiedName!(TemplateArgsOf!(Param)[0])~".tupleof[0]))(g_type_is_a(param_values["~to!string(index)~"].gType, GType.POINTER) ? g_value_get_pointer(&param_values["~to!string(index)~"]) : (g_type_is_a(param_values["~to!string(index)~"].gType, GType.BOXED) ? g_value_get_boxed(&param_values["~to!string(index)~"]) : g_value_get_object(&param_values["~to!string(index)~"]))))";
			else static if ( is(Param == interface) )
				return "ObjectG.getDObject!("~fullyQualifiedName!Param~")(cast(GObject*)g_value_get_object(&param_values["~to!string(index)~"]))";
			else static if ( is(Param == class) )
			{
				static if ( is(Param == Variant) )
					return "new Variant(g_value_get_variant(&param_values["~to!string(index)~"]))";
				else static if ( is(Param== ParamSpec) )
					return "new ParamSpec(g_value_get_param(&param_values["~to!string(index)~"]))";
				else static if ( is(Param : ObjectG) )
					return "ObjectG.getDObject!("~fullyQualifiedName!Param~")(cast(typeof("~fullyQualifiedName!Param~".tupleof[0]))g_value_get_object(&param_values["~to!string(index)~"]))";
				else
					return "ObjectG.getDObject!("~fullyQualifiedName!Param~")(cast(typeof("~fullyQualifiedName!Param~".tupleof[0]))(g_type_is_a(param_values["~to!string(index)~"].gType, GType.POINTER) ? g_value_get_pointer(&param_values["~to!string(index)~"]) : (g_type_is_a(param_values["~to!string(index)~"].gType, GType.BOXED) ? g_value_get_boxed(&param_values["~to!string(index)~"]) : g_value_get_object(&param_values["~to!string(index)~"]))))";
			}
		}
	}
code: end

struct: Object
import: core.memory
import: glib.c.functions : g_datalist_get_flags
import: gobject.DClosure
import: gobject.Signals
import: std.traits
merge: InitiallyUnowned
noSignal: notify
move: clear_object Object
inout: clear_object object_ptr

code: start
	protected bool isGcRoot;
	package DClosure[gulong] signals;

	/**
	 * Sets our main struct and passes store it on the gobject.
	 * Add a gabage collector root to the gtk+ struct so it doesn't get collect
	 */
	public this (GObject* gObject, bool ownedRef = false)
	{
		this.gObject = gObject;
		if ( gObject !is  null )
		{
			setDataFull("GObject", cast(void*)this, cast(GDestroyNotify)&destroyNotify);
			addToggleRef(cast(GToggleNotify)&toggleNotify, cast(void*)this);

			//If the refCount is larger then 1 toggleNotify isn't called
			if (gObject.refCount > 1 && !isGcRoot)
			{
				GC.addRoot(cast(void*)this);
				isGcRoot = true;
			}

			//Remove the floating reference if there is one.
			if ( isFloating() )
			{
				refSink();
				unref();
			}
			//If we already owned this reference remove the one added by addToggleRef.
			else if ( ownedRef )
			{
				unref();
			}
		}
	}

	extern(C)
	{
		static void destroyNotify(ObjectG obj)
		{
			if ( obj.isGcRoot )
			{
				GC.removeRoot(cast(void*)obj);
				obj.isGcRoot = false;
			}

			if ( obj.hasToggleRef() )
				obj.removeToggleRef(cast(GToggleNotify)&toggleNotify, cast(void*)obj);

			obj.gObject = null;
		}

		static void toggleNotify(ObjectG obj, GObject* object, int isLastRef)
		{
			if ( isLastRef && obj.isGcRoot )
			{
				GC.removeRoot(cast(void*)obj);
				obj.isGcRoot = false;
			}
			else if ( !obj.isGcRoot )
			{
				GC.addRoot(cast(void*)obj);
				obj.isGcRoot = true;
			}
		}
	}

	~this()
	{
		static if ( isPointer!(typeof(g_object_steal_data)) )
			bool libLoaded = Linker.isLoaded(LIBRARY_GOBJECT);
		else
			enum libLoaded = true;

		if ( libLoaded && gObject !is null )
		{
			// Remove the GDestroyNotify callback,
			// for when the D object is destroyed before the C one.
			g_object_steal_data(gObject, cast(char*)"GObject");

			if ( isGcRoot )
			{
				GC.removeRoot(cast(void*)this);
				isGcRoot = false;
			}

			if ( hasToggleRef() )
				g_object_remove_toggle_ref(gObject, cast(GToggleNotify)&toggleNotify, cast(void*)this);
			else
				g_object_unref(gObject);
		}
	}

	/** */
	T opCast(T)()
	{
		if ( !this )
			return null;

		static if ( is(T : ObjectG)
			&& !is(T == interface)
			&& is(typeof(new T(cast(typeof(T.tupleof[0]))gObject, false))) )
		{
			//If a regular cast works, return the result.
			if ( auto r = cast(T)super )
				return r;

			//Prints a warning if the cast is invalid.
			//g_type_check_instance_cast(cast(GTypeInstance*)gObject, T.getType());

			//Can we cast this type to T.
			if ( !g_type_is_a(gObject.gTypeInstance.gClass.gType, T.getType()) )
				return null;

			//Remove the GDestroyNotify callback for the original d object.
			g_object_steal_data(gObject, "GObject");
			//Remove the original object as a GC root if needed.
			if ( isGcRoot )
			{
				GC.removeRoot(cast(void*)this);
				isGcRoot = false;
			}

			if ( hasToggleRef() )
			{
				//Add a reference for the original D object before we remove the toggle reference.
				g_object_ref(gObject);
				g_object_remove_toggle_ref(gObject, cast(GToggleNotify)&toggleNotify, cast(void*)this);
			}

			//The new object handles the memory management.
			return new T(cast(typeof(T.tupleof[0]))gObject, false);
		}
		else static if ( is(T == interface)
			&& hasStaticMember!(T, "getType")
			&& is(ReturnType!(T.getType) == GType) )
		{
			//If a regular cast works, return the result.
			if ( auto r = cast(T)super )
				return r;

			//Do we implement interface T.
			if ( !g_type_is_a(gObject.gTypeInstance.gClass.gType, T.getType()) )
				return null;

			return getInterfaceInstance!T(gObject);
		}
		else
			return cast(T)super;
	}

	unittest
	{
		ObjectG obj = null;
		
		assert( (cast(Binding)obj) is null );
	}

	/**
	 * Gets a D Object from the objects table of associations.
	 * Params:
	 *  obj = GObject containing the associations.
	 * Returns: the D Object if found, or a newly constructed object if no such Object exists.
	 */
	public static RT getDObject(T, RT=T, U)(U obj, bool ownedRef = false)
	{
		if ( obj is null )
		{
			return null;
		}

		static if ( is(T : ObjectG) && !is(RT == interface) )
		{
			auto p = g_object_get_data(cast(GObject*)obj, Str.toStringz("GObject"));

			if ( p !is null )
				return cast(RT)cast(ObjectG)p;
			else
				return new T(obj, ownedRef);
		}
		else static if ( is(RT == interface) && hasMember!(RT, "getType") && is(ReturnType!(RT.getType) == GType) )
		{
			auto p = g_object_get_data(cast(GObject*)obj, Str.toStringz("GObject"));

			if ( p !is null )
				return cast(RT)cast(ObjectG)p;
			else
				return getInterfaceInstance!RT(cast(GObject*)obj);
		}
		else
		{
			return new T(obj, ownedRef);
		}
	}

	private static I getInterfaceInstance(I)(GObject* instance)
	{
		static class Impl: ObjectG, I
		{
			public this (GObject* gObject, bool ownedRef = false)
			{
				super(gObject, ownedRef);
			}
			
			/** the main Gtk struct as a void* */
			protected override void* getStruct()
			{
				return cast(void*)gObject;
			}

			// add the interface capabilities
			mixin("import "~ moduleName!I[0..$-2] ~"T;import "~ moduleName!I ~"; mixin "~ __traits(identifier, I)[0..$-2] ~"T!("~__traits(identifier, Impl)~");");
		}

		ClassInfo ci = Impl.classinfo;
		Impl iface;
		void* p;

		//Skip all the setup for the memory management,
		//and only add an extra reference for the instance returned.
		p = GC.malloc(ci.initializer.length, GC.BlkAttr.FINALIZE, ci);
		p[0..ci.initializer.length] = ci.initializer;
		iface = cast(Impl)p;
		iface.gObject = instance;
		iface.doref();

		return iface;
	}

	/**
	 * Is there a toggle ref connected to this object.
	 */
	private bool hasToggleRef()
	{
		enum TOGGLE_REF_FLAG = 0x1;

		if ( (g_datalist_get_flags(&gObject.qdata) & TOGGLE_REF_FLAG) != 0 )
			//TODO: Assumes we always have the gObject data set if the toggleRef is connected to this instance.
			return (g_object_get_data(gObject, cast(char*)"GObject") is cast(void*)this);
		else
			return false;
	}

	public void removeGcRoot()
	{
		if ( hasToggleRef() )
		{
			g_object_ref(gObject);
			g_object_remove_toggle_ref(gObject, cast(GToggleNotify)&toggleNotify, cast(void*)this);
		}

		if ( isGcRoot )
		{
			GC.removeRoot(cast(void*)this);
			isGcRoot = false;
		}
	}

	/** */
	public void setProperty(T)(string propertyName, T value)
	{
		setProperty(propertyName, new Value(value));
	}

	deprecated("Use the member function")
	public static void unref(ObjectG obj)
	{
		obj.unref();
	}

	deprecated("Use the member function")
	public static ObjectG doref(ObjectG obj)
	{
		return obj.doref();
	}

	/**
	 * The notify signal is emitted on an object when one of its
	 * properties has been changed. Note that getting this signal
	 * doesn't guarantee that the value of the property has actually
	 * changed, it may also be emitted when the setter for the property
	 * is called to reinstate the previous value.
	 *
	 * This signal is typically used to obtain change notification for a
	 * single property.
	 *
	 * It is important to note that you must use
	 * canonical parameter names for the property.
	 *
	 * Params:
	 *     dlg          = The callback.
	 *     property     = Set this if you only want to receive the signal for a specific property.
	 *     connectFlags = The behavior of the signal's connection.
	 */	
	gulong addOnNotify(void delegate(ParamSpec, ObjectG) dlg, string property = "", ConnectFlags connectFlags=cast(ConnectFlags)0)
	{
		string signalName;

		if ( property == "" )
			signalName = "notify";
		else
			signalName = "notify::"~ property;

		return Signals.connect(this, signalName, dlg, connectFlags ^ ConnectFlags.SWAPPED);
	}
code: end

struct: ObjectClass
merge: InitiallyUnownedClass

struct: ParamSpecBoolean
noCode: true

struct: ParamSpecBoxed
noCode: true

struct: ParamSpecChar
noCode: true

struct: ParamSpecDouble
noCode: true

struct: ParamSpecEnum
noCode: true

struct: ParamSpecFlags
noCode: true

struct: ParamSpecFloat
noCode: true

struct: ParamSpecGType
noCode: true

struct: ParamSpecInt
noCode: true

struct: ParamSpecInt64
noCode: true

struct: ParamSpecLong
noCode: true

struct: ParamSpecObject
noCode: true

struct: ParamSpecOverride
noCode: true

struct: ParamSpecParam
noCode: true

struct: ParamSpecPointer
noCode: true

struct: ParamSpecString
noCode: true

struct: ParamSpecUChar
noCode: true

struct: ParamSpecUInt
noCode: true

struct: ParamSpecUInt64
noCode: true

struct: ParamSpecULong
noCode: true

struct: ParamSpecUnichar
noCode: true

struct: ParamSpecValueArray
noCode: true

struct: ParamSpecVariant
noCode: true

struct: Signals
import: gobject.DClosure
import: std.traits
move: signal_handler_disconnect Signals handler_disconnect
noCode: handler_disconnect
code: start
	/**
	 * Connects a callback to a signal for a particular object.
	 *
	 * The handler will be called before the default handler of the signal.
	 *
	 * Params:
	 *     instance       = the instance to connect to.
	 *     detailedSignal = a string of the form "signal-name::detail".
	 *     callback       = the callback to connect.
	 *     connectFlags   = a combination of ConnectFlags.
	 *
	 * Returns: the handler ID, of type gulong (always greater than 0 for successful connections)
	 */
	public static gulong connect(T)(ObjectG instance, string detailedSignal, T callback, ConnectFlags connectFlags = cast(ConnectFlags)0)
		if ( isCallable!T && !is(T == GCallback) )
	{
		bool after = (connectFlags & ConnectFlags.AFTER) != false;
		bool swap = (connectFlags & ConnectFlags.SWAPPED) != false;

		DClosure closure = new DClosure(callback, swap);
		gulong id = Signals.connectClosure(instance, detailedSignal, closure, after);

		instance.signals[id] = closure;
		return id;
	}

	deprecated public static gulong connectData(void* instanc, string detailedSignal, GCallback cHandler, Object data, GClosureNotify destroyData, GConnectFlags connectFlags)
	{
		return g_signal_connect_data(instanc, Str.toStringz(detailedSignal), cHandler, cast(void*)data, destroyData, connectFlags);
	}

	/**
	 * Disconnects a handler from an instance so it will not be called during
	 * any future or currently ongoing emissions of the signal it has been
	 * connected to. The @handler_id becomes invalid and may be reused.
	 *
	 * The @handler_id has to be a valid signal handler id, connected to a
	 * signal of @instance.
	 *
	 * Params:
	 *     instance = The instance to remove the signal handler from.
	 *     handlerId = Handler id of the handler to be disconnected.
	 */
	public static void handlerDisconnect(ObjectG instance, gulong handlerId)
	{
		instance.signals.remove(handlerId);
		g_signal_handler_disconnect((instance is null) ? null : instance.getObjectGStruct(), handlerId);
	}

	/**
	 * Connects a GCallback function to a signal for a particular object.
	 * 
	 * The handler will be called before the default handler of the signal.
	 *
	 * See [memory management of signal handlers][signal-memory-management] for
	 * details on how to handle the return value and memory management of @data.
	 * 
	 * Params:
	 *     instance       = the instance to connect to.
	 *     detailedSignal = a string of the form "signal-name::detail".
	 *     cHandler       = the GCallback to connect.
	 *     data           = data to pass to cHandler calls.
	 *
	 * Returns: the handler ID, of type gulong (always greater than 0 for successful connections)
	 */
	public static gulong connect(ObjectG instanc, string detailedSignal, GCallback cHandler, void* data)
	{
		return g_signal_connect_data((instanc is null) ? null : instanc.getObjectGStruct(), Str.toStringz(detailedSignal), cHandler, data, null, cast(ConnectFlags)0);
	}
code: end

struct: Type
import: gobject.ObjectG
code: start
	public static T* getInstanceClass(T)(ObjectG obj)
	{
		return cast(T*) (cast(GTypeInstance*)obj.getObjectGStruct()).gClass;
	}

	/**
	 * Get the unique name that is assigned to the Objects type.
	 * Returns: Static type name or NULL.
	 */
	public static string name(ObjectG obj)
	{
		GType type = (cast(GTypeInstance*)obj.getObjectGStruct()).gClass.gType;

		return name(type);
	}
code: end

struct: Value
import: gobject.Type
import: std.traits
code: start
	/** */
	public this()
	{
		this(new GValue);
	}

	/** */
	this(GOBJECT)(GOBJECT obj)
		if ( is(GOBJECT == class) && hasMember!(GOBJECT, "getType") )
	{
		this();
		init(GOBJECT.getType());

		static if ( is(GOBJECT : ObjectG) )
		{
			setObject(obj);
		}
		else
		{
			if ( Type.isA(gValue.gType, GType.BOXED) )
				setBoxed(obj.tupleof[0]);
			else
				setPointer(obj.tupleof[0]);
		}
	}


	/** */
	this(string value)
	{
		this();
		init(GType.STRING);
		setString(value);
	}

	/** */
	this(BOOL)(BOOL value)
		if( isBoolean!BOOL )
	{
		this();
		init(GType.BOOLEAN);
		setBoolean(value);
	}

	/** */
	this(INT)(INT value)
		if ( isIntegral!INT )
	{
		this();

		static if ( is(OriginalType!INT == int) )
		{
			init(GType.INT);
			setInt(value);
		}
		else static if ( is(OriginalType!INT == uint) )
		{
			init(GType.UINT);
			setUint(value);
		}
		else static if ( is(OriginalType!INT == long) )
		{
			init(GType.INT64);
			setInt64(value);
		}
		else static if ( is(OriginalType!INT == ulong) )
		{
			init(GType.UINT64);
			setUint64(value);
		}
		else
		{
			init(GType.INT);
			setInt(value);
		}
	}

	/** */
	this(FLOAT)(FLOAT value)
		if ( isFloatingPoint!FLOAT )
	{
		this();

		static if ( is( FLOAT == float ) )
		{
			init(GType.FLOAT);
			setFloat(value);
		}
		else
		{
			init(GType.DOUBLE);
			setDouble(value);
		}
	}

	/**
	 * The GType of the contianed value.
	 */
	public @property GType gType()
	{
		return gValue.gType;
	}

	/**
	 * Retrieves a TYPE from Value, the Value must contain the appropriate type.
	 */
	public TYPE get(TYPE)()
	{
		static if ( is(TYPE == bool) )
			return getBoolean();
		else static if ( is(TYPE == byte) )
			return getSchar();
		else static if ( is(TYPE == ubyte) )
			return getUchar();
		else static if ( is(TYPE == int) )
			return getInt();
		else static if ( is(TYPE == uint) )
			return getUint();
		else static if ( is(TYPE == long) )
			return getInt64();
		else static if ( is(TYPE == ulong) )
			return getUint64();
		else static if ( is(TYPE == float) )
			return getFloat();
		else static if ( is(TYPE == double) )
			return getDouble();
		else static if ( is(TYPE == string) )
			return getString();
		else static if ( is(TYPE == string[]) )
			return Str.toStringArray(cast(const(char*)*)getPointer());
		else static if ( is(TYPE == enum) )
			return cast(TYPE)(Type.isA(gValue.gType, GType.ENUM) ? getEnum() : getFlags());
		else static if ( isPointer!TYPE )
			return cast(TYPE)(Type.isA(gValue.gType, GType.POINTER) ? getPointer() : (Type.isA(gValue.gType, GType.BOXED) ? getBoxed() : g_value_get_object(gValue)));
		else static if ( is(TYPE == interface) )
			return cast(TYPE)getObject();
		else static if ( is(TYPE == class) )
		{
			static if ( is(TYPE == Variant) )
				return getVariant();
			else static if ( is(TYPE== ParamSpec) )
				return getParam();
			else static if ( is(TYPE : ObjectG) )
				return cast(TYPE)getObject();
			else
				return ObjectG.getDObject!(TYPE)(cast(typeof(TYPE.tupleof[0]))(Type.isA(gValue.gType, GType.POINTER) ? getPointer() : (Type.isA(gValue.gType, GType.BOXED) ? getBoxed() : g_value_get_object(gValue))));
		}
	}
code: end

struct: WeakRef
code: start
	/** */
	this(void* object)
	{
		g_weak_ref_init(gWeakRef, object);
	}
code: end

#
# Move functions defined as global into there respective classes
#

struct:

move: boxed_copy Boxed copy
move: boxed_free Boxed free
move: boxed_type_register_static Boxed type_register_static
move: pointer_type_register_static Boxed

move: enum_complete_type_info Enums complete_type_info
move: enum_get_value Enums get_value
move: enum_get_value_by_name Enums get_value_by_name
move: enum_get_value_by_nick Enums get_value_by_nick
move: enum_register_static Enums register_static

version 2.54: start
	move: enum_to_string Enums
	move: flags_to_string Enums
version: end

move: flags_complete_type_info Flags complete_type_info
move: flags_get_first_value Flags get_first_value
move: flags_get_value_by_name Flags get_value_by_name
move: flags_get_value_by_nick Flags get_value_by_nick
move: flags_register_static Flags register_static

# ParamSpec Constructors?
move: param_spec_boolean Value
move: param_spec_boxed Value
move: param_spec_char Value
move: param_spec_double Value
move: param_spec_enum Value
move: param_spec_flags Value
move: param_spec_float Value
move: param_spec_gtype Value
move: param_spec_int Value
move: param_spec_int64 Value
move: param_spec_long Value
move: param_spec_object Value
move: param_spec_override Value
move: param_spec_param Value
move: param_spec_pointer Value
move: param_spec_string Value
move: param_spec_uchar Value
move: param_spec_uint Value
move: param_spec_uint64 Value
move: param_spec_ulong Value
move: param_spec_unichar Value
move: param_spec_value_array Value
move: param_spec_variant Value

move: param_type_register_static ParamSpec
move: param_value_convert ParamSpec
move: param_value_defaults ParamSpec
move: param_value_set_default ParamSpec
move: param_value_validate ParamSpec
move: param_values_cmp ParamSpec

move: signal_accumulator_first_wins Signals accumulator_first_wins
move: signal_accumulator_true_handled Signals accumulator_true_handled
move: signal_add_emission_hook Signals add_emission_hook
move: signal_chain_from_overridden Signals chain_from_overridden
move: signal_chain_from_overridden_handler Signals chain_from_overridden_handler
move: signal_connect_closure Signals connect_closure
move: signal_connect_closure_by_id Signals connect_closure_by_id
move: signal_connect_data Signals connect_data
move: signal_connect_object Signals connect_object
move: signal_emit Signals emit
move: signal_emit_by_name Signals emit_by_name
move: signal_emit_valist Signals emit_valist
move: signal_emitv Signals emitv
move: signal_get_invocation_hint Signals get_invocation_hint
move: signal_handler_block Signals handler_block
move: signal_handler_find Signals handler_find
move: signal_handler_is_connected Signals handler_is_connected
move: signal_handler_unblock Signals handler_unblock
move: signal_handlers_block_matched Signals handlers_block_matched
move: signal_handlers_destroy Signals handlers_destroy
move: signal_handlers_disconnect_matched Signals handlers_disconnect_matched
move: signal_handlers_unblock_matched Signals handlers_unblock_matched
move: signal_has_handler_pending Signals has_handler_pending
move: signal_list_ids Signals list_ids
move: signal_lookup Signals lookup
move: signal_name Signals name
move: signal_new Signals new
move: signal_new_class_handler Signals new_class_handler
move: signal_new_valist Signals new_valist
move: signal_newv Signals newv
move: signal_override_class_closure Signals override_class_closure
move: signal_override_class_handler Signals override_class_handler
move: signal_parse_name Signals parse_name
move: signal_query Signals query
move: signal_remove_emission_hook Signals remove_emission_
move: signal_set_va_marshaller Signals set_va_marshaller
move: signal_stop_emission Signals stop_emission
move: signal_stop_emission_by_name Signals stop_emission_by_name
move: signal_type_cclosure_new Signals type_cclosure_new

move: source_set_closure Closure
move: source_set_dummy_callback Closure

move: strdup_value_contents Value

move: type_add_class_cache_func Type add_class_cache_func
move: type_add_class_private Type add_class_private
move: type_add_instance_private Type add_instance_private
move: type_add_interface_check Type add_interface_check
move: type_add_interface_dynamic Type add_interface_dynamic
move: type_add_interface_static Type add_interface_static
move: type_check_class_cast Type check_class_cast
move: type_check_class_is_a Type check_class_is_a
move: type_check_instance Type check_instance
move: type_check_instance_cast Type check_instance_cast
move: type_check_instance_is_a Type check_instance_is_a
move: type_check_instance_is_fundamentally_a Type check_instance_is_fundamentally_a
move: type_check_is_value_type Type check_is_value_type
move: type_check_value Type check_value
move: type_check_value_holds Type check_value_holds
move: type_children Type children
move: type_create_instance Type create_instance
move: type_default_interface_peek Type default_interface_peek
move: type_default_interface_ref Type default_interface_ref
move: type_default_interface_unref Type default_interface_unref
move: type_depth Type depth
move: type_ensure Type ensure
move: type_free_instance Type free_instance
move: type_from_name Type from_name
move: type_fundamental Type fundamental
move: type_fundamental_next Type fundamental_next
move: type_get_plugin Type get_plugin
move: type_get_qdata Type get_qdata
move: type_get_type_registration_serial Type get_type_registration_serial
move: type_init Type init
move: type_init_with_debug_flags Type init_with_debug_flags
move: type_interfaces Type interfaces
move: type_is_a Type is_a
move: type_name Type name
move: type_name_from_class Type name_from_class
move: type_name_from_instance Type name_from_instance
move: type_next_base Type next_base
move: type_parent Type parent
move: type_qname Type qname
move: type_query Type query
move: type_register_dynamic Type register_dynamic
move: type_register_fundamental Type register_fundamental
move: type_register_static Type register_static
move: type_register_static_simple Type register_static_simple
move: type_remove_class_cache_func Type remove_class_cache_func
move: type_remove_interface_check Type remove_interface_check
move: type_set_qdata Type set_qdata
move: type_test_flags Type test_flags
move: type_get_instance_count Type get_instance_count
#move: type_value_table_peek Type value_table_peek