/* Bluefish HTML Editor
 * pixmap.c
 *
 * Copyright (C) 2002-2026 Oliver Sessink
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

#include "bluefish.h"
#include "pixmap.h"

/*
#include "pixmaps/toolbar_icons.c"

typedef struct {
	const guint id;
	const guint8 *data;
} Tpixmap;

typedef struct {
	const guint id;
	const gchar *stock_id;
} Tstockpixmap;

static Tpixmap tp[] = {
	{102, pixmap_view_in_browser},
 	{104, pixmap_bookmarks},
	{105, pixmap_filetree},
};

static Tstockpixmap tsp[] = {
	{1000, GTK_STOCK_FIND},
	{1001, GTK_STOCK_FIND_AND_REPLACE},
	{1002, GTK_STOCK_PREFERENCES},
	{1003, GTK_STOCK_GO_UP},
	{1004, GTK_STOCK_GO_DOWN}	,
	{1005, GTK_STOCK_SPELL_CHECK}
};

GtkWidget *new_pixmap(gint type) {
	GtkWidget *widget;
	gint i;
	if (type < 1000) {
		GdkPixbuf *pixbuf;
		for (i=0;i<(sizeof(tp)/sizeof(Tpixmap));i++) {
			if (tp[i].id == type) {
				break;
			}
		}
		if (i >= sizeof(tp)/sizeof(Tpixmap)) {
			g_print("new_pixmap, requested unknown type %d\n", type);
			i = sizeof(tp)/sizeof(Tpixmap)-1;
		}
		pixbuf = gdk_pixbuf_new_from_inline(-1,tp[i].data,FALSE,NULL);
		widget = gtk_image_new_from_pixbuf(pixbuf);
		g_object_unref(pixbuf);
	} else {
		for (i=0;i<(sizeof(tsp)/sizeof(Tstockpixmap));i++) {
			if (tsp[i].id == type) {
				break;
			}
		}
		if (i >= sizeof(tp)/sizeof(Tpixmap)) {
			g_print("new_pixmap, requested unknown type %d\n", type);
			i = sizeof(tp)/sizeof(Tpixmap)-1;
		}
		widget = gtk_image_new_from_stock(tsp[i].stock_id,GTK_ICON_SIZE_BUTTON);
	}
	return widget;
}*/

GtkWidget *load_pixmap(const gchar *name) {
	GdkPixbuf *pixbuf;
	GtkWidget *widget;
	GError *gerror=NULL;
	gchar *resourcepath = g_strconcat("/nl/openoffice/bluefish/pixmaps/", name,".png", NULL);
	pixbuf = gdk_pixbuf_new_from_resource(resourcepath, &gerror);
	if (gerror) {
		g_print("failed to load icon %s from resource: %s\n",resourcepath,gerror->message);
		g_error_free(gerror);
		g_free(resourcepath);
	}
	widget = gtk_image_new_from_pixbuf(pixbuf);
	g_object_unref(pixbuf);
	return widget;
}

void
register_bf_stock_icons(void)
{
	GdkPixbuf *pixbuf;
	GError *gerror=NULL;

	GtkIconFactory *icon_factory = gtk_icon_factory_new();
	
	/* for each icon we want to register as stock item */
	pixbuf = gdk_pixbuf_new_from_resource("/nl/openoffice/bluefish/pixmaps/view_in_browser.png", &gerror);
	if (gerror) {
		g_print("failed to load icon /nl/openoffice/bluefish/pixmaps/view_in_browser.png from resource: %s\n",gerror->message);
		g_error_free(gerror);
	}
	GtkIconSet *icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
	g_object_unref(pixbuf);
	gtk_icon_factory_add(icon_factory, BF_STOCK_BROWSER_PREVIEW, icon_set);
	gtk_icon_set_unref(icon_set);
	gtk_icon_factory_add_default(icon_factory);
	g_object_unref(icon_factory);
	
	/*
	static struct {
		const guint8 *data;
		gchar *stock_id;
	} stock_icons[] = {
			{pixmap_view_in_browser, BF_STOCK_BROWSER_PREVIEW}
	};

	GtkIconFactory *icon_factory;
	GtkIconSet *icon_set;
	gint i;

	icon_factory = gtk_icon_factory_new();
	for (i = 0; i < G_N_ELEMENTS(stock_icons); i++) {
		GdkPixbuf *pixbuf = gdk_pixbuf_new_from_inline(-1,stock_icons[i].data,FALSE,NULL);
		icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);

		gtk_icon_factory_add(icon_factory, stock_icons[i].stock_id, icon_set);
		gtk_icon_set_unref(icon_set);
		
		gtk_icon_theme_add_builtin_icon(stock_icons[i].stock_id,gdk_pixbuf_get_width(pixbuf), pixbuf);
		
		g_object_unref(pixbuf);
	}
	gtk_icon_factory_add_default(icon_factory);
	g_object_unref(icon_factory);
	*/
}

void set_default_icon(void) {
	GError *gerror=NULL;
	GdkPixbuf *pixbuf = gdk_pixbuf_new_from_resource("/nl/openoffice/bluefish/pixmaps/bluefish_icon1.png", &gerror);
	if (gerror) {
		g_print("set_default_icon, failed to load icon /nl/openoffice/bluefish/pixmaps/bluefish_icon1.png from resource: %s\n",gerror->message);
		g_error_free(gerror);
		return;
	}
	/*GdkPixbuf *pixbuf = gdk_pixbuf_new_from_inline(-1,pixmap_bluefish_icon1,FALSE,NULL);*/
	GList *tmplist = g_list_append(NULL, pixbuf);
	gtk_window_set_default_icon_list(tmplist);
	g_list_free(tmplist);
	g_object_unref(pixbuf);
}

gchar *detect_theme_name(void) {
	gchar *themename=NULL;
	GtkSettings *settings = gtk_settings_get_default();
	g_object_get(settings, "gtk-icon-theme-name", &themename, NULL);
	g_print("got theme name %s\n",themename);
	return themename;
}

GResource *load_resources(const gchar *prefix) {
	GError *gerror=NULL;
	GResource *resource;
	gchar *resourcefile;
	/*g_print("load_resources, main_v->props.icon_theme=%s\n",main_v->props.icon_theme);*/
	if (main_v->props.icon_theme && main_v->props.icon_theme[0]!= '\0' && g_strcmp0(main_v->props.icon_theme, "auto")!=0) {
		resourcefile = g_strconcat(prefix, "-", main_v->props.icon_theme, ".gresource", NULL);
	} else {
		gchar *themename = detect_theme_name();
		resourcefile = g_strconcat(prefix, "-", themename, ".gresource", NULL);
		g_free(themename);
	}
	resource = g_resource_load(resourcefile, &gerror);
	if (gerror) {
		g_print("Could not load resources %s: %s\n",resourcefile,gerror->message);
		g_error_free(gerror);
		gerror=NULL;
	}
	g_free(resourcefile);
	if (!resource) {
		/* fall back to the default gresource */
		resourcefile = g_strconcat(prefix, ".gresource", NULL);
		resource = g_resource_load(resourcefile, &gerror);
		if (gerror) {
			g_print("Could not load resources %s: %s\n",resourcefile,gerror->message);
			g_error_free(gerror);
		}
		g_free(resourcefile);
	}
	g_resources_register(resource);
	return resource;
}
