GTKD resources

Mr. Pib via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Aug 11 20:52:53 PDT 2017


Here is what I came up with

import gdkpixbuf.Pixbuf;
GdkPixbuf*[string] __ImageCache;

void SetImage(string imgName)(gtk.Image T)
{
	import std.path, std.conv, gdkpixbuf.Pixbuf;
	GError* err = null;
	if (imgName !in __ImageCache)
	{
		GdkPixbufLoader* __PixbufLoader;
		writeln("Image not cached, caching: "~imgName);
		enum imgData = cast(ubyte[])(import(baseName(imgName)).ptr);
		
		if (__PixbufLoader == null)		
			__PixbufLoader = gdk_pixbuf_loader_new();
		
		assert(__PixbufLoader, "Error: Cannot create pixbuf loader!");

		if (!gdk_pixbuf_loader_write(__PixbufLoader, 
cast(char*)imgData.ptr, imgData.length, &err))
			assert(__PixbufLoader, "Error: Cannot load pixbuf loader!");
		if (!gdk_pixbuf_loader_close(__PixbufLoader, &err))
			assert(__PixbufLoader, "Error: Cannot create pixbuf!");

		auto pixbuf = gdk_pixbuf_loader_get_pixbuf(__PixbufLoader);		
		if (pixbuf == null)
			assert("Error: Cannot load pixbuf!");

		__ImageCache[imgName] = pixbuf;				
	}

	import gtk.c.functions;
	gtk_image_set_from_pixbuf(T.getImageStruct(), 
__ImageCache[imgName]);

}

Maybe that will help someone. Not sure if it is the best way but 
seems to work.


More information about the Digitalmars-d-learn mailing list