/*
 * Copyright (C) 2004 John Ellis
 * Copyright (C) 2008 - 2016 The Geeqie Team
 *
 * Author: Vladimir Nadvornik
 *
 * 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.
 */

#include "bar.h"

#include <string>

#include <glib-object.h>
#include <pango/pango.h>

#include <config.h>

#include "compat.h"
#include "filedata.h"
#include "intl.h"
#include "layout.h"
#include "main-defines.h"
#include "menu.h"
#include "metadata.h"
#include "rcfile.h"
#include "ui-misc.h"


namespace
{

constexpr gint SIDEBAR_DEFAULT_WIDTH = 250;

} // namespace

struct KnownPanes
{
	PaneType type;
	const gchar *id;
	const gchar *title;
	const gchar *config;
};

static const gchar default_config_histogram[] =
"<gq>"
"    <layout id = '_current_'>"
"        <bar>"
"            <pane_histogram id = 'histogram' expanded = 'true' histogram_channel = '4' histogram_mode = '0' />"
"        </bar>"
"    </layout>"
"</gq>";

static const gchar default_config_title[] =
"<gq>"
"    <layout id = '_current_'>"
"        <bar>"
"            <pane_comment id = 'title' expanded = 'true' key = 'Xmp.dc.title' height = '40' />"
"        </bar>"
"    </layout>"
"</gq>";

static const gchar default_config_headline[] =
"<gq>"
"    <layout id = '_current_'>"
"        <bar>"
"            <pane_comment id = 'headline' expanded = 'true' key = 'Xmp.photoshop.Headline'  height = '40' />"
"        </bar>"
"    </layout>"
"</gq>";

static const gchar default_config_keywords[] =
"<gq>"
"    <layout id = '_current_'>"
"        <bar>"
"            <pane_keywords id = 'keywords' expanded = 'true' key = '" KEYWORD_KEY "' />"
"        </bar>"
"    </layout>"
"</gq>";

static const gchar default_config_comment[] =
"<gq>"
"    <layout id = '_current_'>"
"        <bar>"
"            <pane_comment id = 'comment' expanded = 'true' key = '" COMMENT_KEY "' height = '150' />"
"        </bar>"
"    </layout>"
"</gq>";
static const gchar default_config_rating[] =
"<gq>"
"    <layout id = '_current_'>"
"        <bar>"
"            <pane_rating id = 'rating' expanded = 'true' />"
"        </bar>"
"    </layout>"
"</gq>";

static const gchar default_config_exif[] =
"<gq>"
"    <layout id = '_current_'>"
"        <bar>"
"            <pane_exif id = 'exif' expanded = 'true' >"
"                <entry key = 'formatted.Camera' if_set = 'true' editable = 'false' />"
"                <entry key = 'formatted.DateTime' if_set = 'true' editable = 'false' />"
"                <entry key = 'formatted.localtime' if_set = 'true' editable = 'false' />"
"                <entry key = 'formatted.ShutterSpeed' if_set = 'true' editable = 'false' />"
"                <entry key = 'formatted.Aperture' if_set = 'true' editable = 'false' />"
"                <entry key = 'formatted.ExposureBias' if_set = 'true' editable = 'false' />"
"                <entry key = 'formatted.ISOSpeedRating' if_set = 'true' editable = 'false' />"
"                <entry key = 'formatted.FocalLength' if_set = 'true' editable = 'false' />"
"                <entry key = 'formatted.FocalLength35mmFilm' if_set = 'true' editable = 'false' />"
"                <entry key = 'formatted.Flash' if_set = 'true' editable = 'false' />"
"                <entry key = 'Exif.Photo.ExposureProgram' if_set = 'true' editable = 'false' />"
"                <entry key = 'Exif.Photo.MeteringMode' if_set = 'true' editable = 'false' />"
"                <entry key = 'Exif.Photo.LightSource' if_set = 'true' editable = 'false' />"
"                <entry key = 'formatted.ColorProfile' if_set = 'true' editable = 'false' />"
"                <entry key = 'formatted.SubjectDistance' if_set = 'true' editable = 'false' />"
"                <entry key = 'formatted.Resolution' if_set = 'true' editable = 'false' />"
"                <entry key = '" ORIENTATION_KEY "' if_set = 'true' editable = 'false' />"
"                <entry key = 'formatted.star_rating' if_set = 'true' editable = 'false' />"
"            </pane_exif>"
"        </bar>"
"    </layout>"
"</gq>";

static const gchar default_config_file_info[] =
"<gq>"
"    <layout id = '_current_'>"
"        <bar>"
"            <pane_exif id = 'file_info' expanded = 'true' >"
"                <entry key = 'file.mode' if_set = 'false' editable = 'false' />"
"                <entry key = 'file.date' if_set = 'false' editable = 'false' />"
"                <entry key = 'file.size' if_set = 'false' editable = 'false' />"
"                <entry key = 'file.owner' if_set = 'false' editable = 'false' />"
"                <entry key = 'file.group' if_set = 'false' editable = 'false' />"
"                <entry key = 'file.class' if_set = 'false' editable = 'false' />"
"                <entry key = 'file.link' if_set = 'false' editable = 'false' />"
"            </pane_exif>"
"        </bar>"
"    </layout>"
"</gq>";

static const gchar default_config_location[] =
"<gq>"
"    <layout id = '_current_'>"
"        <bar>"
"            <pane_exif id = 'location' expanded = 'true' >"
"                <entry key = 'formatted.GPSPosition' if_set = 'true' editable = 'false' />"
"                <entry key = 'formatted.GPSAltitude' if_set = 'true' editable = 'false' />"
"                <entry key = 'formatted.timezone' if_set = 'true' editable = 'false' />"
"                <entry key = 'Xmp.photoshop.Country' if_set = 'false' editable = 'true' />"
"                <entry key = 'Xmp.iptc.CountryCode' if_set = 'false' editable = 'true' />"
"                <entry key = 'Xmp.photoshop.State' if_set = 'false' editable = 'true' />"
"                <entry key = 'Xmp.photoshop.City' if_set = 'false' editable = 'true' />"
"                <entry key = 'Xmp.iptc.Location' if_set = 'false' editable = 'true' />"
"            </pane_exif>"
"        </bar>"
"    </layout>"
"</gq>";

static const gchar default_config_copyright[] =
"<gq>"
"    <layout id = '_current_'>"
"        <bar>"
"            <pane_exif id = 'copyright' expanded = 'true' >"
"                <entry key = 'Xmp.dc.creator' if_set = 'true' editable = 'false' />"
"                <entry key = 'Xmp.dc.contributor' if_set = 'true' editable = 'false' />"
"                <entry key = 'Xmp.dc.rights' if_set = 'false' editable = 'false' />"
"            </pane_exif>"
"        </bar>"
"    </layout>"
"</gq>";

#if HAVE_LIBSHUMATE
static const gchar default_config_gps[] =
"<gq>"
"    <layout id = '_current_'>"
"        <bar>"
"            <pane_gps id = 'gps' expanded = 'true'"
"                      map-id = 'osm-mapnik'"
"                      zoom-level = '8'"
"                      latitude = '50116666'"
"                      longitude = '8683333' />"
"        </bar>"
"    </layout>"
"</gq>";
#endif

static const KnownPanes known_panes[] = {
/* default sidebar */
	{PANE_HISTOGRAM,	"histogram",	N_("Histogram"),	default_config_histogram},
	{PANE_COMMENT,		"title",	N_("Title"),		default_config_title},
	{PANE_KEYWORDS,		"keywords",	N_("Keywords"),		default_config_keywords},
	{PANE_COMMENT,		"comment",	N_("Comment"),		default_config_comment},
	{PANE_RATING,		"rating",	N_("Star Rating"),	default_config_rating},
	{PANE_COMMENT,		"headline",	N_("Headline"),		default_config_headline},
	{PANE_EXIF,		"exif",		N_("Exif"),		default_config_exif},
/* other pre-configured panes */
	{PANE_EXIF,		"file_info",	N_("File info"),	default_config_file_info},
	{PANE_EXIF,		"location",	N_("Location and GPS"),	default_config_location},
	{PANE_EXIF,		"copyright",	N_("Copyright"),	default_config_copyright},
#if HAVE_LIBSHUMATE
	{PANE_GPS,		"gps",	N_("GPS Map"),	default_config_gps},
#endif
	{PANE_UNDEF,		nullptr,		nullptr,			nullptr}
};

struct BarData
{
	GtkWidget *widget;
	GtkWidget *vbox;
	FileData *fd;
	GtkWidget *label_file_name;

	LayoutWindow *lw;
	gint width;
};

static const gchar *bar_pane_get_default_config(const gchar *id)
{
	const KnownPanes *pane = known_panes;

	while (pane->id)
		{
		if (strcmp(pane->id, id) == 0) break;
		pane++;
		}
	if (!pane->id) return nullptr;
	return pane->config;
}

static void height_spin_changed_cb(GtkSpinButton *spin, gpointer data)
{
	gtk_widget_set_size_request(static_cast<GtkWidget *>(data), -1, gtk_spin_button_get_value_as_int(spin));
}

static void height_spin_key_press_cb(GtkEventControllerKey *, gint keyval, guint, GdkModifierType, gpointer data)
{
	if ((keyval == GDK_KEY_Return || keyval == GDK_KEY_KP_Enter || keyval == GDK_KEY_Escape))
		{
		gq_gtk_widget_destroy(static_cast<GtkWidget *>(data));
		}
}

static gboolean expander_height_cb(GtkEventControllerKey *controller, guint, guint, GdkModifierType, gpointer)
{
	GtkWidget *window = gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(controller));
	gq_gtk_widget_destroy(window);

	return TRUE;
}

static void bar_expander_height_cb(GtkWidget *, gpointer data)
{
	auto expander = static_cast<GtkWidget *>(data);
	GtkWidget *window;
	gint x;
	gint y;
	gint w;
	gint h;
	GdkDisplay *display;
	GdkSeat *seat;
	GdkDevice *device;

	display = gdk_display_get_default();
	seat = gdk_display_get_default_seat(display);
	device = gdk_seat_get_pointer(seat);
	get_device_position(device, x, y);

	window = gtk_window_new();

	gtk_window_set_modal(GTK_WINDOW(window), TRUE);
	gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
	gtk_window_set_default_size(GTK_WINDOW(window), 50, 30); //** @FIXME set these values in a more sensible way */
	GtkEventController *controller = gtk_event_controller_key_new();
	g_signal_connect(controller, "key-pressed", G_CALLBACK(expander_height_cb), nullptr);
	gtk_widget_add_controller(window, controller);

	gtk_widget_show(window);

	GtkWidget *data_box = gtk_expander_get_child(GTK_EXPANDER(expander));
	gtk_widget_get_size_request(data_box, &w, &h);

	GtkWidget *spin = gtk_spin_button_new_with_range(1, 1000, 1);
	g_signal_connect(G_OBJECT(spin), "value-changed", G_CALLBACK(height_spin_changed_cb), data_box);
	controller = gtk_event_controller_key_new();
	g_signal_connect(controller, "key-pressed", G_CALLBACK(height_spin_key_press_cb), window);
	gtk_widget_add_controller(spin, controller);

	gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), h);
	gq_gtk_container_add(window, spin);
	gtk_widget_show(spin);
	gtk_widget_grab_focus(spin);
}

void menu_expander_height_cb(GSimpleAction *, GVariant *, gpointer data)
{
	bar_expander_height_cb(nullptr, data);
}

static void bar_expander_add_action_cb(GSimpleAction *, GVariant *parameter, gpointer)
{
	if (!parameter) return;

	const gchar *id = g_variant_get_string(parameter, nullptr);
	const gchar *config = bar_pane_get_default_config(id);

	if (config) load_config_from_buf(config, strlen(config), FALSE);
}


static void bar_menu_popup(GtkWidget *widget)
{
	GtkWidget *expander = nullptr;

	if (!g_object_get_data(G_OBJECT(widget), "bar_data"))
		{
		GtkWidget *bar = widget;
		do
			{
			bar = gtk_widget_get_parent(bar);
			}
		while (bar && !g_object_get_data(G_OBJECT(bar), "bar_data"));
		if (!bar) return;

		expander = widget;
		}

	gboolean display_height_option = FALSE;
	if (expander)
		{
		GtkWidget *pane = gtk_expander_get_child(GTK_EXPANDER(expander));
		auto *pd = static_cast<PaneData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));

		display_height_option = pd && (pd->type == PANE_COMMENT ||
		                                pd->type == PANE_KEYWORDS ||
		                                pd->type == PANE_GPS ||
		                                pd->type == PANE_RATING);
		}

	popup_menu_bar(expander, display_height_option ? G_CALLBACK(bar_expander_height_cb) : nullptr, widget);
}


static gboolean bar_menu_expander_common(GtkWidget *widget, guint button)
{
	if (button == GDK_BUTTON_SECONDARY)
		{
		bar_menu_popup(widget);
		return TRUE;
		}
	return FALSE;
}

static void bar_menu_expander_gesture_cb(GtkGestureClick *gesture, gint, gdouble, gdouble, gpointer)
{
	GtkWidget *widget = gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(gesture));
	bar_menu_expander_common(widget, gtk_gesture_single_get_current_button(GTK_GESTURE_SINGLE(gesture)));
}

static void bar_expander_cb(GObject *object, GParamSpec *, gpointer)
{
	GtkExpander *expander;

	expander = GTK_EXPANDER(object);
	GtkWidget *child = gtk_expander_get_child(expander);

	if (gtk_expander_get_expanded(expander))
		{
		gq_gtk_widget_show_all(child);
		}
	else
		{
		gtk_widget_hide(child);
		}

	auto *image = static_cast<GtkImage *>(g_object_get_data(G_OBJECT(expander), "bar_expander_button_image"));
	if (image)
		{
		gtk_image_set_from_icon_name(image, gtk_expander_get_expanded(expander) ? GQ_ICON_PAN_UP : GQ_ICON_PAN_DOWN);
		}
}

static GtkWidget *bar_expander_label_widget_new(GtkWidget *expander, GtkWidget *title)
{
	GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);

	gtk_widget_set_tooltip_text(expander, _("Expand or collapse pane"));
	gtk_box_append(GTK_BOX(box), title);
	gtk_widget_set_hexpand(title, TRUE);
	gtk_widget_set_halign(title, GTK_ALIGN_FILL);

	return box;
}

static GtkWidget *bar_menu_add_button_new(GtkWidget *toolbar)
{
	GtkWidget *button = gtk_menu_button_new();
	GtkWidget *content = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
	GtkWidget *image = gtk_image_new_from_icon_name(GQ_ICON_ADD);
	GtkWidget *label = gtk_label_new(_("Add"));
	g_autoptr(GMenu) menu_model = g_menu_new();

	for (const KnownPanes *pane = known_panes; pane->id; pane++)
		{
		g_autoptr(GMenuItem) item = g_menu_item_new(_(pane->title), nullptr);
		g_menu_item_set_action_and_target_value(item, "bar.add-pane", g_variant_new_string(pane->id));
		g_menu_append_item(menu_model, item);
		}

	GtkWidget *popover = gtk_popover_menu_new_from_model(G_MENU_MODEL(menu_model));

	GSimpleActionGroup *action_group = g_simple_action_group_new();
	GSimpleAction *action = g_simple_action_new("add-pane", G_VARIANT_TYPE_STRING);
	g_signal_connect(action, "activate", G_CALLBACK(bar_expander_add_action_cb), nullptr);
	g_action_map_add_action(G_ACTION_MAP(action_group), G_ACTION(action));
	g_object_unref(action);

	gtk_box_append(GTK_BOX(content), image);
	gtk_box_append(GTK_BOX(content), label);
	gtk_menu_button_set_child(GTK_MENU_BUTTON(button), content);
	gtk_widget_set_tooltip_text(button, _("Add Pane"));
	gtk_menu_button_set_popover(GTK_MENU_BUTTON(button), popover);
	gtk_widget_insert_action_group(button, "bar", G_ACTION_GROUP(action_group));
	g_object_set_data_full(G_OBJECT(button), "bar-action-group", action_group, g_object_unref);

	gq_gtk_container_add(toolbar, button);
	gtk_widget_show(button);

	return button;
}


void bar_set_fd(GtkWidget *bar, FileData *fd)
{
	auto *bd = static_cast<BarData *>(g_object_get_data(G_OBJECT(bar), "bar_data"));
	if (!bd) return;

	file_data_unref(bd->fd);
	bd->fd = file_data_ref(fd);

	for (GtkWidget *expander = gtk_widget_get_first_child(bd->vbox);
	     expander;
	     expander = gtk_widget_get_next_sibling(expander))
		{
		GtkWidget *widget = gtk_expander_get_child(GTK_EXPANDER(expander));

		auto *pd = static_cast<PaneData *>(g_object_get_data(G_OBJECT(widget), "pane_data"));
		if (pd && pd->pane_set_fd)
			{
			pd->pane_set_fd(widget, fd);
			}
		}

	gtk_label_set_text(GTK_LABEL(bd->label_file_name), bd->fd ? bd->fd->name : "");
}

void bar_notify_selection(GtkWidget *bar, gint count)
{
	auto *bd = static_cast<BarData *>(g_object_get_data(G_OBJECT(bar), "bar_data"));
	if (!bd) return;

	for (GtkWidget *expander = gtk_widget_get_first_child(bd->vbox);
	     expander;
	     expander = gtk_widget_get_next_sibling(expander))
		{
		GtkWidget *widget = gtk_expander_get_child(GTK_EXPANDER(expander));

		auto *pd = static_cast<PaneData *>(g_object_get_data(G_OBJECT(widget), "pane_data"));
		if (pd && pd->pane_notify_selection)
			{
			pd->pane_notify_selection(widget, count);
			}
		}
}

gboolean bar_event(GtkWidget *bar, GdkEvent *event)
{
	auto *bd = static_cast<BarData *>(g_object_get_data(G_OBJECT(bar), "bar_data"));
	if (!bd) return FALSE;

	for (GtkWidget *child = gtk_widget_get_first_child(bd->vbox);
	    child;
	    child = gtk_widget_get_next_sibling(child))
		{
		GtkWidget *widget = gtk_expander_get_child(GTK_EXPANDER(child));

		auto *pd = static_cast<PaneData *>(g_object_get_data(G_OBJECT(widget), "pane_data"));
		if (pd && pd->pane_event && pd->pane_event(widget, event))
			{
			return TRUE;
			}
		}

	return FALSE;
}

GtkWidget *bar_find_pane_by_id(GtkWidget *bar, PaneType type, const gchar *id)
{
	if (!id || !id[0]) return nullptr;

	if (!bar)
		{
		return nullptr;
		}

	auto *bd = static_cast<BarData *>(g_object_get_data(G_OBJECT(bar), "bar_data"));
	if (!bd) return nullptr;

	for (GtkWidget *child = gtk_widget_get_first_child(bd->vbox);
	    child;
	    child = gtk_widget_get_next_sibling(child))
		{
		GtkWidget *widget = gtk_expander_get_child(GTK_EXPANDER(child));

		auto *pd = static_cast<PaneData *>(g_object_get_data(G_OBJECT(widget), "pane_data"));
		if (pd && type == pd->type && strcmp(id, pd->id) == 0)
			{
			return widget;
			}
		}

	return nullptr;
}

void bar_clear(GtkWidget *bar)
{
	BarData *bd;

	bd = static_cast<BarData *>(g_object_get_data(G_OBJECT(bar), "bar_data"));
	if (!bd) return;

	while (GtkWidget *child = gtk_widget_get_first_child(bd->vbox))
		{
		gtk_box_remove(GTK_BOX(bd->vbox), child);
		}
}

void bar_write_config(GtkWidget *bar, GString *outstr, gint indent)
{
	if (!bar) return;

	auto *bd = static_cast<BarData *>(g_object_get_data(G_OBJECT(bar), "bar_data"));
	if (!bd) return;

	WRITE_NL(); WRITE_STRING("<bar ");
	WRITE_BOOL_FULL("enabled", gtk_widget_get_visible(bar));
	WRITE_INT(*bd, width);
	WRITE_STRING(">");

	indent++;
	WRITE_NL(); WRITE_STRING("<clear/>");

	for (GtkWidget *expander = gtk_widget_get_first_child(bd->vbox);
	    expander;
	    expander = gtk_widget_get_next_sibling(expander))
		{
		GtkWidget *widget = gtk_expander_get_child(GTK_EXPANDER(expander));

		auto *pd = static_cast<PaneData *>(g_object_get_data(G_OBJECT(widget), "pane_data"));
		if (!pd) continue;

		pd->expanded = gtk_expander_get_expanded(GTK_EXPANDER(expander));

		if (pd->pane_write_config)
			pd->pane_write_config(widget, outstr, indent);
		}

	indent--;
	WRITE_NL(); WRITE_STRING("</bar>");
}

void bar_update_expander(GtkWidget *pane)
{
	auto pd = static_cast<PaneData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
	GtkWidget *expander;

	if (!pd) return;

	expander = gtk_widget_get_parent(pane);

	gtk_expander_set_expanded(GTK_EXPANDER(expander), pd->expanded);
}

void bar_add(GtkWidget *bar, GtkWidget *pane)
{
	auto bd = static_cast<BarData *>(g_object_get_data(G_OBJECT(bar), "bar_data"));
	auto pd = static_cast<PaneData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));

	if (!bd) return;
	if (!pd) return;

	pd->lw = bd->lw;
	pd->bar = bar;

	GtkWidget *expander = gtk_expander_new(nullptr);
	DEBUG_NAME(expander);
	if (pd && pd->title)
		{
		gtk_expander_set_label_widget(GTK_EXPANDER(expander), bar_expander_label_widget_new(expander, pd->title));
		gtk_widget_show(pd->title);
		}

	gq_gtk_box_pack_start(GTK_BOX(bd->vbox), expander, FALSE, TRUE, 0);

	GtkGesture *gesture = gtk_gesture_click_new();
	gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(gesture), GDK_BUTTON_SECONDARY);
	g_signal_connect(gesture, "released", G_CALLBACK(bar_menu_expander_gesture_cb), bd);
	gtk_widget_add_controller(expander, GTK_EVENT_CONTROLLER(gesture));
	g_signal_connect(expander, "notify::expanded", G_CALLBACK(bar_expander_cb), pd);

	gq_gtk_container_add(expander, pane);

	gtk_expander_set_expanded(GTK_EXPANDER(expander), pd->expanded);

	gtk_widget_show(expander);

	if (bd->fd && pd && pd->pane_set_fd) pd->pane_set_fd(pane, bd->fd);
}

void bar_populate_default(GtkWidget *)
{
	const gchar *populate_id[] = {"histogram", "title", "keywords", "comment", "rating", "exif"};

	for (const gchar *id : populate_id)
		{
		const gchar *config = bar_pane_get_default_config(id);
		if (config) load_config_from_buf(config, strlen(config), FALSE);
		}
}

static void bar_paned_position_changed_cb(GObject *paned, GParamSpec *, gpointer data)
{
	auto *bar = static_cast<GtkWidget *>(data);
	auto *bd = static_cast<BarData *>(g_object_get_data(G_OBJECT(bar), "bar_data"));
	if (!bd) return;

	bd->width = gtk_paned_get_position(GTK_PANED(paned));
}

void bar_close(GtkWidget *bar)
{
	BarData *bd;

	bd = static_cast<BarData *>(g_object_get_data(G_OBJECT(bar), "bar_data"));
	if (!bd) return;

	/* @FIXME This causes a g_object_unref failed error on exit */
	gq_gtk_widget_destroy(bd->widget);
}

static void bar_destroy(gpointer data)
{
	auto bd = static_cast<BarData *>(data);

	file_data_unref(bd->fd);
	g_free(bd);
}

GtkWidget *bar_new(LayoutWindow *lw)
{
	BarData *bd;
	GtkWidget *box;
	GtkWidget *tbar;
	GtkWidget *add_box;

	bd = g_new0(BarData, 1);

	bd->lw = lw;

	bd->widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
	DEBUG_NAME(bd->widget);
	g_object_set_data_full(G_OBJECT(bd->widget), "bar_data", bd, bar_destroy);

	g_signal_connect_object(G_OBJECT(lw->utility_paned), "notify::position",
	                        G_CALLBACK(bar_paned_position_changed_cb), bd->widget, GConnectFlags(0));

	bd->width = SIDEBAR_DEFAULT_WIDTH;

	box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
	DEBUG_NAME(box);

	bd->label_file_name = gtk_label_new("");
	gtk_label_set_ellipsize(GTK_LABEL(bd->label_file_name), PANGO_ELLIPSIZE_END);
	gtk_label_set_selectable(GTK_LABEL(bd->label_file_name), TRUE);
	gtk_label_set_xalign(GTK_LABEL(bd->label_file_name), 0.5);
	gtk_label_set_yalign(GTK_LABEL(bd->label_file_name), 0.5);

	gq_gtk_box_pack_start(GTK_BOX(box), bd->label_file_name, TRUE, TRUE, 0);
	gtk_widget_show(bd->label_file_name);

	gq_gtk_box_pack_start(GTK_BOX(bd->widget), box, FALSE, FALSE, 0);
	gtk_widget_show(box);

	GtkWidget *scrolled = gtk_scrolled_window_new();
	DEBUG_NAME(scrolled);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
		GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
	gq_gtk_box_pack_start(GTK_BOX(bd->widget), scrolled, TRUE, TRUE, 0);
	gtk_widget_show(scrolled);


	bd->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
	gq_gtk_container_add(scrolled, bd->vbox);
	gtk_widget_remove_css_class(gtk_widget_get_first_child(scrolled), "frame");

	add_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
	DEBUG_NAME(add_box);
	gq_gtk_box_pack_end(GTK_BOX(bd->widget), add_box, FALSE, FALSE, 0);
	tbar = pref_toolbar_new(add_box);
	bar_menu_add_button_new(tbar);
	gtk_widget_show(add_box);

	gtk_scrolled_window_set_has_frame(GTK_SCROLLED_WINDOW(scrolled), true);
	gtk_widget_show(bd->vbox);
	return bd->widget;
}


GtkWidget *bar_update_from_config(GtkWidget *bar, const gchar **attribute_names, const gchar **attribute_values, LayoutWindow *lw, gboolean startup)
{
	gboolean enabled = TRUE;
	gint width = SIDEBAR_DEFAULT_WIDTH;

	while (*attribute_names)
		{
		const gchar *option = *attribute_names++;
		const gchar *value = *attribute_values++;

		if (READ_BOOL_FULL("enabled", enabled)) continue;
		if (READ_INT_FULL("width", width)) continue;

		config_file_error((std::string("Unknown attribute: ") + option + " = " + value).c_str());
		}

	if (startup)
		{
		gtk_paned_set_position(GTK_PANED(lw->utility_paned), width);
		}

	gtk_widget_set_visible(bar, enabled);

	return bar;
}

GtkWidget *bar_new_from_config(LayoutWindow *lw, const gchar **attribute_names, const gchar **attribute_values)
{
	GtkWidget *bar = bar_new(lw);
	return bar_update_from_config(bar, attribute_names, attribute_values, lw, TRUE);
}

GtkWidget *bar_pane_expander_title(const gchar *title)
{
	GtkWidget *widget = gtk_label_new(title);

	pref_label_bold(widget, TRUE, FALSE);
	gtk_label_set_ellipsize(GTK_LABEL(widget), PANGO_ELLIPSIZE_END);

	return widget;
}

gboolean bar_pane_translate_title(PaneType type, const gchar *id, gchar **title)
{
	const KnownPanes *pane = known_panes;

	if (!title) return FALSE;
	while (pane->id)
		{
		if (pane->type == type && strcmp(pane->id, id) == 0) break;
		pane++;
		}
	if (!pane->id) return FALSE;

	if (*title && **title && strcmp(pane->title, *title) != 0) return FALSE;

	g_free(*title);
	*title = g_strdup(_(pane->title));
	return TRUE;
}

/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */
