1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 module gtkd.Implement;
21 
22 import std.algorithm;
23 import std.traits;
24 import std.meta;
25 import std.range;
26 import std.string;
27 import std.uni;
28 import std.conv;
29 import gtkc.gobjecttypes;
30 
31 /**
32  * This template generates the boilerplate needed to override
33  * GTK functions from D.
34  *
35  * Example:
36  * --------------------------
37  * class MyApplication : Application
38  * {
39  *   import gtkd.Implement;
40  *   import gtkc.gobject : g_object_newv;
41  *
42  *   mixin ImplementClass!GtkApplication;
43  *
44  *   this()
45  *   {
46  *     //TODO: sort out the constructor.
47  *     super(cast(GtkApplication*)g_object_newv(getType(), 0, null), true);
48  *
49  *     setApplicationId("org.gtkd.demo.popupmenu");
50  *     setFlags(GApplicationFlags.FLAGS_NONE);
51  *   }
52  *
53  *   override void activate()
54  *   {
55  *     new PopupMenuDemo(this);
56  *   }
57  * }
58  * --------------------------
59  */
60 mixin template ImplementClass(Class)
61 {
62 	mixin(ImplementClassImpl!(Class, typeof(this))());
63 }
64 
65 /**
66  * This template generates the boilerplate needed to implement a
67  * GTK interface in D.
68  *
69  * Base is the Gtk struct for the base class, and Iface is the
70  * Gtk Iface struct for the interface.
71  *
72  * In your constructor you will need to instantiate the Gtk class
73  * by calling the ObjectG costructor: `super(getType(), null);`
74  *
75  * If you are using ImplementInterface in conjunction with ImplementClass
76  * you will need to mixin ImplementClass before mixing in any interfaces.
77  */
78 mixin template ImplementInterface(Base, Iface)
79 {
80 	mixin(ImplementInterfaceImpl!(Base, Iface, typeof(this))());
81 }
82 
83 template ImplementClassImpl(Klass, Impl)
84 {
85 	string ImplementClassImpl()
86 	{
87 		string result;
88 
89 		result ~= "import glib.Str;\n"~
90 		          "import gobject.Type : Type;\n"~
91 		          "import gtkc.gobject : g_type_class_peek_parent, g_object_get_data;\n";
92 
93 		if ( !is(Klass == gtkc.gobjecttypes.GObject) )
94 			result ~= "import "~ getTypeImport!Klass() ~": "~ getTypeFunction!Klass()[0..$-2] ~";\n";
95 
96 		if ( !hasMember!(Impl, toCamelCase!Impl()) )
97 		{
98 			result ~= "\nstruct "~ toPascalCase!Impl() ~"\n"~
99 			          "{\n"~
100 			          "\t"~ Klass.stringof ~" parentInstance;\n"~
101 			          "}\n\n";
102 
103 			result ~= "struct "~ toPascalCase!Impl() ~"Class\n"~
104 			          "{\n"~
105 			          "\t"~ Klass.stringof ~"Class parentClass;\n"~
106 			          "}\n\n";
107 
108 			result ~= "protected "~ toPascalCase!Impl() ~"* "~ toCamelCase!Impl() ~";\n"~
109 			          "protected static "~ Klass.stringof ~"Class* parentClass = null;\n\n";
110 
111 			result ~= "protected override void* getStruct()\n"~
112 			          "{\n"~
113 			          "\treturn cast(void*)gObject;\n"~
114 			          "}\n\n";
115 		}
116 
117 		if ( !implements!Impl("getType") )
118 		{
119 			result ~= "public static GType getType()\n"~
120 			         "{\n"~
121 			          "\timport std.algorithm : startsWith;\n\n"~
122 			          "\tGType "~ toCamelCase!Impl() ~"Type = Type.fromName(\""~ toPascalCase!Impl() ~"\");\n\n"~
123 			          "\tif ("~ toCamelCase!Impl() ~"Type == GType.INVALID)\n"~
124 			          "\t{\n"~
125 			          "\t\t"~ toCamelCase!Impl() ~"Type = Type.registerStaticSimple(\n"~
126 			          "\t\t\t"~ getTypeFunction!Klass() ~",\n"~
127 			          "\t\t\t\""~ toPascalCase!Impl() ~"\",\n"~
128 			          "\t\t\tcast(uint)"~ toPascalCase!Impl() ~"Class.sizeof,\n"~
129 			          "\t\t\tcast(GClassInitFunc) &"~ toCamelCase!Impl() ~"ClassInit,\n"~
130 			          "\t\t\tcast(uint)"~ toPascalCase!Impl() ~".sizeof, null, cast(GTypeFlags)0);\n\n"~
131 			          "\t\tforeach ( member; __traits(derivedMembers, "~ Impl.stringof ~") )\n"~
132 			          "\t\t{\n"~
133 			          "\t\t\tstatic if ( member.startsWith(\"_implementInterface\") )\n"~
134 			          "\t\t\t\t__traits(getMember, "~ Impl.stringof ~", member)("~ toCamelCase!Impl() ~"Type);\n"~
135 			          "\t\t}\n"~
136 			          "\t}\n\n"~
137 			          "\treturn "~ toCamelCase!Impl() ~"Type;\n"~
138 			          "}\n\n";
139 		}
140 
141 		result ~= "extern(C)\n{\n";
142 
143 		if ( !implements!Impl(toCamelCase!Impl() ~"ClassInit") )
144 		{
145 			result ~= "static void "~ toCamelCase!Impl() ~"ClassInit (void* klass)\n"~
146 			          "{\n"~
147 			          "\tparentClass = cast("~ Klass.stringof ~"Class*) g_type_class_peek_parent(klass);\n";
148 
149 			result ~= setFunctionPointers!(getClass!Klass)();
150 
151 			result ~= "}\n\n";
152 		}
153 
154 		result ~= getWrapFunctions!(getClass!Klass)();
155 		result ~= "}";
156 
157 		return result;
158 	}
159 
160 	string setFunctionPointers(GtkClass)()
161 	{
162 		string result;
163 
164 		alias names = FieldNameTuple!GtkClass;
165 		foreach ( i, member; Fields!GtkClass )
166 		{
167 			static if ( names[i] == "parentClass" )
168 			{
169 				result ~= "\t"~ fullyQualifiedName!member ~"* "~ toCamelCase!member() ~" = cast("~ fullyQualifiedName!member ~"*)klass;\n";
170 				result ~= setFunctionPointers!member();
171 			}
172 			else if ( isCallable!member && 
173 			     implements!Impl(names[i]) &&
174 			     !implements!Impl(toCamelCase!Impl() ~ names[i].capitalizeFirst) )
175 //TODO: __traits(isOverrideFunction, Foo.foo) ?
176 			{
177 				result ~= "\t"~ toCamelCase!GtkClass() ~"."~ names[i] ~" = &"~ toCamelCase!Impl() ~ names[i].capitalizeFirst ~";\n";
178 			}
179 		}
180 
181 		result ~= "\n";
182 
183 		return result;
184 	}
185 
186 	string getWrapFunctions(GtkClass)()
187 	{
188 		string result;
189 
190 		alias names = FieldNameTuple!GtkClass;
191 		foreach ( i, member; Fields!GtkClass )
192 		{
193 			static if ( names[i] == "parentClass" )
194 			{
195 				result ~= getWrapFunctions!member();
196 			}
197 			else static if ( isCallable!member &&
198 			     implements!Impl(names[i]) &&
199 			     !implements!Impl(toCamelCase!Impl() ~ names[i].capitalizeFirst) )
200 //TODO: __traits(isOverrideFunction, Foo.foo) ?
201 			{
202 				result ~= getWrapFunction!(Impl, member, names[i]);
203 			}
204 		}
205 
206 		return result;
207 	}
208 
209 	template getClass(Instance)
210 	{
211 		mixin("import "~ getClassImport!Instance() ~"; alias getClass = "~ Instance.stringof ~"Class;");
212 	}
213 
214 	private string getClassImport(Klass)()
215 	{
216 		return fullyQualifiedName!Klass.replace("."~ Klass.stringof, "");
217 	}
218 }
219 
220 template ImplementInterfaceImpl(Base, Klass, Impl)
221 {
222 	string ImplementInterfaceImpl()
223 	{
224 		string result;
225 
226 		result ~= "import glib.Str;\n"~
227 		          "import gobject.Type : Type;\n"~
228 		          "import gtkc.gobject : g_type_class_peek_parent, g_object_get_data;\n";
229 
230 		if ( !is(Base == gtkc.gobjecttypes.GObject) )
231 			result ~= "import "~ getTypeImport!Base() ~": "~ getTypeFunction!Base()[0..$-2] ~";\n";
232 
233 		result ~= "import "~ getTypeImport!Klass() ~" : "~ getTypeFunction!Klass()[0..$-2] ~";\n\n";
234 
235 		if ( !hasMember!(Impl, toCamelCase!Impl()) )
236 		{
237 			result ~= "\nstruct "~ toPascalCase!Impl() ~"\n"~
238 			          "{\n"~
239 			          "\t"~ Base.stringof ~" parentInstance;\n"~
240 			          "}\n\n";
241 
242 			result ~= "struct "~ toPascalCase!Impl() ~"Class\n"~
243 			          "{\n"~
244 			          "\t"~ Base.stringof ~"Class parentClass;\n"~
245 			          "}\n\n";
246 
247 			result ~= "protected "~ toPascalCase!Impl() ~"* "~ toCamelCase!Impl() ~";\n"~
248 			          "protected static "~ Base.stringof ~"Class* parentClass = null;\n\n";
249 
250 			result ~= "protected override void* getStruct()\n"~
251 			          "{\n"~
252 			          "\treturn cast(void*)gObject;\n"~
253 			          "}\n\n";
254 
255 			if ( is(Base == gtkc.gobjecttypes.GObject) )
256 			{
257 				result ~= "public this()\n"~
258 				          "{\n"~
259 				          "\tauto p = super(getType(), null);\n"~
260 				          "\t"~ toCamelCase!Impl() ~" = cast("~ toPascalCase!Impl() ~"*) p.getObjectGStruct();\n"~
261 				          "}\n\n";
262 			}
263 		}
264 
265 		if ( !implements!Impl("getType") )
266 		{
267 			result ~= "public static GType getType()\n"~
268 			          "{\n"~
269 			          "\tGType "~ toCamelCase!Impl() ~"Type = Type.fromName(\""~ toPascalCase!Impl() ~"\");\n\n"~
270 			          "\tif ("~ toCamelCase!Impl() ~"Type == GType.INVALID)\n"~
271 			          "\t{\n"~
272 			          "\t\t"~ toCamelCase!Impl() ~"Type = Type.registerStaticSimple(\n"~
273 			          "\t\t\t"~ getTypeFunction!Base() ~",\n"~
274 			          "\t\t\t\""~ toPascalCase!Impl() ~"\",\n"~
275 			          "\t\t\tcast(uint)"~ toPascalCase!Impl() ~"Class.sizeof,\n"~
276 			          "\t\t\tcast(GClassInitFunc) &"~ toCamelCase!Impl() ~"ClassInit,\n"~
277 			          "\t\t\tcast(uint)"~ toPascalCase!Impl() ~".sizeof, null, cast(GTypeFlags)0);\n\n"~
278 			          "\t\tforeach ( member; __traits(derivedMembers, "~ Impl.stringof ~") )\n"~
279 			          "\t\t{\n"~
280 			          "\t\t\tstatic if ( member.startsWith(\"_implementInterface\") )\n"~
281 			          "\t\t\t\t__traits(getMember, "~ Impl.stringof ~", member)("~ toCamelCase!Impl() ~"Type);\n"~
282 			          "\t\t}\n"~
283 			          "\t}\n\n"~
284 			          "\treturn "~ toCamelCase!Impl() ~"Type;\n"~
285 			          "}\n\n";
286 		}
287 
288 		result ~= "static void _implementInterface"~ Klass.stringof ~"(GType type)\n"~
289 		          "{\n"~
290 		          "\tGInterfaceInfo "~ Klass.stringof ~"Info =\n"~
291 		          "\t{\n"~
292 		          "\t\tcast(GInterfaceInitFunc) &"~ toCamelCase!Klass() ~"Init,\n"~
293 		          "\t\tnull,\n"~
294 		          "\t\tnull\n"~
295 		          "\t};\n"~
296 		          "\tType.addInterfaceStatic(type, "~ getTypeFunction!Klass() ~", &"~ Klass.stringof ~"Info);\n"~
297 		          "};\n\n";
298 
299 		result ~= "extern(C)\n{\n";
300 
301 		if ( !implements!Impl(toCamelCase!Impl() ~"ClassInit") )
302 		{
303 			result ~= "static void "~ toCamelCase!Impl() ~"ClassInit (void* klass)\n"~
304 			          "{\n"~
305 			          "\tparentClass = cast("~ Base.stringof ~"Class*) g_type_class_peek_parent(klass);\n"~
306 			          "}\n\n";
307 		}
308 
309 		if ( !implements!Impl(toCamelCase!Klass() ~"Init") )
310 		{
311 			result ~= "static void "~ toCamelCase!Klass() ~"Init ("~ Klass.stringof ~" *iface)\n"~
312 			          "{\n";
313 
314 			auto names = FieldNameTuple!Klass;
315 			foreach ( i, member; Fields!Klass )
316 			{
317 				if ( isCallable!member && implements!Impl(names[i]) && (!implements!Impl("addOn"~ names[i].capitalizeFirst) || implements!Impl(toCamelCase!Impl() ~ names[i].capitalizeFirst) ) )
318 				{
319 					result ~= "\tiface."~ names[i] ~" = &"~ toCamelCase!Impl() ~ names[i].capitalizeFirst ~";\n";
320 				}
321 			}
322 
323 			result ~= "}\n\n";
324 		}
325 
326 		alias names = FieldNameTuple!Klass;
327 		foreach ( i, member; Fields!Klass )
328 		{
329 			if ( isCallable!member && 
330 			     implements!Impl(names[i]) &&
331 			     !implements!Impl(toCamelCase!Impl() ~ names[i].capitalizeFirst) &&
332 			     !implements!Impl("addOn"~ names[i].capitalizeFirst) )
333 			{
334 				result ~= getWrapFunction!(Impl, member, names[i]);
335 			}
336 		}
337 
338 		result ~= "}";
339 
340 		return result;
341 	}
342 }
343 
344 private string getTypeFunction(Iface)()
345 {
346 	string result;
347 
348 	if ( is(Iface == gtkc.gobjecttypes.GObject) )
349 		return "GType.OBJECT";
350 
351 	foreach ( i, char c; Iface.stringof )
352 	{
353 		if ( c.isUpper && i > 0 )
354 			result ~= "_"~c;
355 		else
356 			result ~= c;
357 	}
358 
359 	return result.toLower.replace("_iface", "")~ "_get_type()";
360 }
361 
362 private string getTypeImport(Iface)()
363 {
364 	return fullyQualifiedName!Iface.replace("types."~ Iface.stringof, "");
365 }
366 
367 private string getWrapFunction(Impl, Member, string name)()
368 {
369 	string result;
370 
371 	static if ( isCallable!Member )
372 	{
373 		alias Params = Parameters!Member;
374 		alias STC = ParameterStorageClass;
375 		auto ParamStorage = [STC.none, ParameterStorageClassTuple!(__traits(getMember, Impl, name))];
376 		auto ParamNames = ["iface", ParameterIdentifierTuple!(__traits(getMember, Impl, name))];
377 		alias DParamTypes = AliasSeq!(void, Parameters!(__traits(getMember, Impl, name)));
378 
379 		result ~= "static "~ ReturnType!Member.stringof ~" "~ toCamelCase!Impl() ~ name.capitalizeFirst ~"(";
380 
381 		foreach ( i, param; Params )
382 		{
383 			if ( i > 0 )
384 				result ~= ", ";
385 			result ~= param.stringof ~" "~ ParamNames[i];
386 		}
387 
388 		result ~= ")\n"~
389 		          "{\n";
390 
391 		if ( implements!Impl("get"~ Impl.stringof ~"Struct") && implements!Impl("getStruct") )
392 			result ~= "\tauto impl = ObjectG.getDObject!("~ Impl.stringof ~")(cast("~ toPascalCase!Impl() ~"*)iface);\n";
393 		else
394 			result ~= "\tauto impl = cast("~ Impl.stringof ~")g_object_get_data(cast(GObject*)iface, \"GObject\".ptr);\n";
395 
396 		foreach ( i, param; Params )
397 		{
398 			if ( ParamStorage[i] == STC.out_ && isGtkdType!(DParamTypes[i]) )
399 				result ~= "\t"~ DParamTypes[i].stringof ~" d_"~ ParamNames[i] ~";\n";
400 			else if ( ParamStorage[i] == STC.ref_ && isGtkdType!(DParamTypes[i]) )
401 				result ~= "\t"~ DParamTypes[i].stringof ~" d_"~ ParamNames[i] ~" = "~ ParamNames[i] ~".get"~ DParamTypes[i].stringof ~"Struct();\n";
402 		}
403 
404 		if ( is(ReturnType!Member == void) )
405 			result ~= "\n\timpl."~ name ~"(";
406 		else
407 			result ~= "\n\tauto ret = impl."~ name ~"(";
408 
409 		foreach ( i, param; Params )
410 		{
411 			if ( i == 0 )
412 				continue;
413 			if ( i > 1 )
414 				result ~= ", ";
415 
416 			if ( (ParamStorage[i] == STC.out_ || ParamStorage[i] == STC.ref_) && isGtkdType!(DParamTypes[i]) )
417 				result ~= "d_"~ ParamNames[i];
418 			else if ( isGtkdType!(DParamTypes[i]) )
419 				result ~= "ObjectG.getDObject!("~ DParamTypes[i].stringof ~")("~ ParamNames[i] ~")";
420 			else
421 				result ~= ParamNames[i];
422 		}
423 
424 		result ~= ");\n\n";
425 
426 		foreach ( i, param; Params )
427 		{
428 			if ( (ParamStorage[i] == STC.out_ || ParamStorage[i] == STC.ref_) && isGtkdType!(DParamTypes[i]) )
429 			{
430 				result ~= "\tif ( d_"~ ParamNames[i] ~" !is null )\n"~
431 				          "\t\t"~ ParamNames[i] ~" = d_"~ ParamNames[i] ~".get"~ DParamTypes[i].stringof ~"Struct();\n";
432 			}
433 		}
434 
435 		if ( isGtkdType!(ReturnType!(__traits(getMember, Impl, name))) && isPointer!(ReturnType!Member) )
436 			result ~= "\treturn ret.get"~ (ReturnType!(__traits(getMember, Impl, name))).stringof ~"Struct();\n";
437 		else if ( !is(ReturnType!Member == void) )
438 			result ~= "\treturn ret;\n";
439 
440 		result ~= "}\n\n";
441 	}
442 
443 	return result;
444 }
445 
446 private string toCamelCase(Type)()
447 {
448 	string result;
449 
450 	foreach (i, word; to!string(fullyQualifiedName!Type).split("."))
451 	{
452 		if ( i == 0 )
453 			word = word[0 .. 1].toLower ~ word[1 .. $];
454 		else
455 			word = word.capitalizeFirst;
456 
457 		result ~= word;
458 	}
459 
460 	return result;
461 }
462 
463 private string toPascalCase(Type)()
464 {
465 	string result;
466 
467 	foreach (word; to!string(fullyQualifiedName!Type).split("."))
468 	{
469 		result ~= word.capitalizeFirst;
470 	}
471 
472 	return result;
473 }
474 
475 private template isGtkdType(T)
476 {
477 	static if ( __traits(compiles, new T(cast(typeof(T.tupleof[0]))null, true)) )
478 		enum bool isGtkdType = hasMember!(T, "get"~ T.stringof ~"Struct");
479 	else
480 		enum bool isGtkdType = false;
481 }
482 
483 private bool implements(Impl)(string member)
484 {
485 	return (cast(string[])[__traits(derivedMembers, Impl)]).canFind(member);
486 }
487 
488 private string capitalizeFirst(string str)
489 {
490 	if ( str.empty )
491 		return str;
492 	else if ( str.length == 1 )
493 		return str.toUpper;
494 	else
495 		return str[0 .. 1].toUpper ~ str[1 .. $];
496 }