/* side_panel.c generated by valac 0.18.0.1-c1117, the Vala compiler
 * generated from side_panel.vala, do not modify */

/*
 * This file is part of LaTeXila.
 *
 * Copyright © 2010-2012 Sébastien Wilmet
 *
 * LaTeXila 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.
 *
 * LaTeXila 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 LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <glib.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <gio/gio.h>
#include <gee.h>
#include <stdlib.h>
#include <string.h>
#include <pango/pango.h>
#include <glib/gi18n-lib.h>


#define TYPE_SIDE_PANEL (side_panel_get_type ())
#define SIDE_PANEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_SIDE_PANEL, SidePanel))
#define SIDE_PANEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_SIDE_PANEL, SidePanelClass))
#define IS_SIDE_PANEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_SIDE_PANEL))
#define IS_SIDE_PANEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_SIDE_PANEL))
#define SIDE_PANEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_SIDE_PANEL, SidePanelClass))

typedef struct _SidePanel SidePanel;
typedef struct _SidePanelClass SidePanelClass;
typedef struct _SidePanelPrivate SidePanelPrivate;

#define SIDE_PANEL_TYPE_SIDE_PANEL_COLUMN (side_panel_side_panel_column_get_type ())
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _SidePanel {
	GtkGrid parent_instance;
	SidePanelPrivate * priv;
};

struct _SidePanelClass {
	GtkGridClass parent_class;
};

struct _SidePanelPrivate {
	GSettings* _settings;
	GeeArrayList* _components;
	GtkComboBox* _combo_box;
	GtkListStore* _list_store;
	gint _current_component;
};

typedef enum  {
	SIDE_PANEL_SIDE_PANEL_COLUMN_PIXBUF,
	SIDE_PANEL_SIDE_PANEL_COLUMN_NAME,
	SIDE_PANEL_SIDE_PANEL_COLUMN_N_COLUMNS
} SidePanelSidePanelColumn;


static gpointer side_panel_parent_class = NULL;

GType side_panel_get_type (void) G_GNUC_CONST;
#define SIDE_PANEL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_SIDE_PANEL, SidePanelPrivate))
enum  {
	SIDE_PANEL_DUMMY_PROPERTY
};
static GType side_panel_side_panel_column_get_type (void) G_GNUC_UNUSED;
SidePanel* side_panel_new (void);
SidePanel* side_panel_construct (GType object_type);
static void side_panel_init_combo_box (SidePanel* self);
static GtkButton* side_panel_get_close_button (SidePanel* self);
static void side_panel_show_active_component (SidePanel* self);
static void _side_panel_show_active_component_gtk_combo_box_changed (GtkComboBox* _sender, gpointer self);
static void __lambda120_ (SidePanel* self);
static void ___lambda120__gtk_button_clicked (GtkButton* _sender, gpointer self);
void side_panel_add_component (SidePanel* self, const gchar* name, const gchar* stock_id, GtkGrid* component);
void side_panel_restore_state (SidePanel* self);
static void side_panel_finalize (GObject* obj);


static GType side_panel_side_panel_column_get_type (void) {
	static volatile gsize side_panel_side_panel_column_type_id__volatile = 0;
	if (g_once_init_enter (&side_panel_side_panel_column_type_id__volatile)) {
		static const GEnumValue values[] = {{SIDE_PANEL_SIDE_PANEL_COLUMN_PIXBUF, "SIDE_PANEL_SIDE_PANEL_COLUMN_PIXBUF", "pixbuf"}, {SIDE_PANEL_SIDE_PANEL_COLUMN_NAME, "SIDE_PANEL_SIDE_PANEL_COLUMN_NAME", "name"}, {SIDE_PANEL_SIDE_PANEL_COLUMN_N_COLUMNS, "SIDE_PANEL_SIDE_PANEL_COLUMN_N_COLUMNS", "n-columns"}, {0, NULL, NULL}};
		GType side_panel_side_panel_column_type_id;
		side_panel_side_panel_column_type_id = g_enum_register_static ("SidePanelSidePanelColumn", values);
		g_once_init_leave (&side_panel_side_panel_column_type_id__volatile, side_panel_side_panel_column_type_id);
	}
	return side_panel_side_panel_column_type_id__volatile;
}


SidePanel* side_panel_construct (GType object_type) {
	SidePanel * self = NULL;
	GSettings* _tmp0_;
	GeeArrayList* _tmp1_;
	GtkButton* _tmp2_ = NULL;
	GtkButton* close_button;
	GtkComboBox* _tmp3_;
	self = (SidePanel*) g_object_new (object_type, NULL);
	_tmp0_ = g_settings_new ("org.gnome.latexila.preferences.ui");
	_g_object_unref0 (self->priv->_settings);
	self->priv->_settings = _tmp0_;
	_tmp1_ = gee_array_list_new (GTK_TYPE_GRID, (GBoxedCopyFunc) g_object_ref, g_object_unref, NULL);
	_g_object_unref0 (self->priv->_components);
	self->priv->_components = _tmp1_;
	gtk_widget_set_margin_left ((GtkWidget*) self, 6);
	gtk_widget_set_margin_top ((GtkWidget*) self, 3);
	gtk_grid_set_column_spacing ((GtkGrid*) self, 3);
	gtk_grid_set_row_spacing ((GtkGrid*) self, 3);
	side_panel_init_combo_box (self);
	_tmp2_ = side_panel_get_close_button (self);
	close_button = _tmp2_;
	_tmp3_ = self->priv->_combo_box;
	gtk_grid_attach ((GtkGrid*) self, (GtkWidget*) _tmp3_, 0, 0, 1, 1);
	gtk_grid_attach ((GtkGrid*) self, (GtkWidget*) close_button, 1, 0, 1, 1);
	gtk_widget_show_all ((GtkWidget*) self);
	_g_object_unref0 (close_button);
	return self;
}


SidePanel* side_panel_new (void) {
	return side_panel_construct (TYPE_SIDE_PANEL);
}


static void _side_panel_show_active_component_gtk_combo_box_changed (GtkComboBox* _sender, gpointer self) {
	side_panel_show_active_component (self);
}


static void side_panel_init_combo_box (SidePanel* self) {
	GtkListStore* _tmp0_;
	GtkListStore* _tmp1_;
	GtkComboBox* _tmp2_;
	GtkComboBox* _tmp3_;
	GtkCellRendererPixbuf* _tmp4_;
	GtkCellRendererPixbuf* pixbuf_renderer;
	GtkComboBox* _tmp5_;
	GtkComboBox* _tmp6_;
	GtkCellRendererText* _tmp7_;
	GtkCellRendererText* text_renderer;
	GtkComboBox* _tmp8_;
	GtkComboBox* _tmp9_;
	GtkComboBox* _tmp10_;
	g_return_if_fail (self != NULL);
	_tmp0_ = gtk_list_store_new ((gint) SIDE_PANEL_SIDE_PANEL_COLUMN_N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING);
	_g_object_unref0 (self->priv->_list_store);
	self->priv->_list_store = _tmp0_;
	_tmp1_ = self->priv->_list_store;
	_tmp2_ = (GtkComboBox*) gtk_combo_box_new_with_model ((GtkTreeModel*) _tmp1_);
	g_object_ref_sink (_tmp2_);
	_g_object_unref0 (self->priv->_combo_box);
	self->priv->_combo_box = _tmp2_;
	_tmp3_ = self->priv->_combo_box;
	gtk_widget_set_hexpand ((GtkWidget*) _tmp3_, TRUE);
	_tmp4_ = (GtkCellRendererPixbuf*) gtk_cell_renderer_pixbuf_new ();
	g_object_ref_sink (_tmp4_);
	pixbuf_renderer = _tmp4_;
	_tmp5_ = self->priv->_combo_box;
	gtk_cell_layout_pack_start ((GtkCellLayout*) _tmp5_, (GtkCellRenderer*) pixbuf_renderer, FALSE);
	_tmp6_ = self->priv->_combo_box;
	gtk_cell_layout_set_attributes ((GtkCellLayout*) _tmp6_, (GtkCellRenderer*) pixbuf_renderer, "stock-id", SIDE_PANEL_SIDE_PANEL_COLUMN_PIXBUF, NULL, NULL);
	_tmp7_ = (GtkCellRendererText*) gtk_cell_renderer_text_new ();
	g_object_ref_sink (_tmp7_);
	text_renderer = _tmp7_;
	g_object_set (text_renderer, "ellipsize-set", TRUE, NULL);
	g_object_set (text_renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
	_tmp8_ = self->priv->_combo_box;
	gtk_cell_layout_pack_start ((GtkCellLayout*) _tmp8_, (GtkCellRenderer*) text_renderer, TRUE);
	_tmp9_ = self->priv->_combo_box;
	gtk_cell_layout_set_attributes ((GtkCellLayout*) _tmp9_, (GtkCellRenderer*) text_renderer, "text", SIDE_PANEL_SIDE_PANEL_COLUMN_NAME, NULL, NULL);
	_tmp10_ = self->priv->_combo_box;
	g_signal_connect_object (_tmp10_, "changed", (GCallback) _side_panel_show_active_component_gtk_combo_box_changed, self, 0);
	_g_object_unref0 (text_renderer);
	_g_object_unref0 (pixbuf_renderer);
}


static void __lambda120_ (SidePanel* self) {
	gtk_widget_hide ((GtkWidget*) self);
}


static void ___lambda120__gtk_button_clicked (GtkButton* _sender, gpointer self) {
	__lambda120_ (self);
}


static GtkButton* side_panel_get_close_button (SidePanel* self) {
	GtkButton* result = NULL;
	GtkButton* _tmp0_;
	GtkButton* close_button;
	const gchar* _tmp1_ = NULL;
	GtkImage* _tmp2_;
	GtkImage* _tmp3_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = (GtkButton*) gtk_button_new ();
	g_object_ref_sink (_tmp0_);
	close_button = _tmp0_;
	gtk_button_set_relief (close_button, GTK_RELIEF_NONE);
	gtk_button_set_focus_on_click (close_button, FALSE);
	_tmp1_ = _ ("Hide panel");
	gtk_widget_set_tooltip_text ((GtkWidget*) close_button, _tmp1_);
	_tmp2_ = (GtkImage*) gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
	g_object_ref_sink (_tmp2_);
	_tmp3_ = _tmp2_;
	gtk_container_add ((GtkContainer*) close_button, (GtkWidget*) _tmp3_);
	_g_object_unref0 (_tmp3_);
	g_signal_connect_object (close_button, "clicked", (GCallback) ___lambda120__gtk_button_clicked, self, 0);
	result = close_button;
	return result;
}


void side_panel_add_component (SidePanel* self, const gchar* name, const gchar* stock_id, GtkGrid* component) {
	GtkTreeIter iter = {0};
	GtkListStore* _tmp0_;
	GtkTreeIter _tmp1_ = {0};
	GtkListStore* _tmp2_;
	GtkTreeIter _tmp3_;
	const gchar* _tmp4_;
	const gchar* _tmp5_;
	GeeArrayList* _tmp6_;
	GtkGrid* _tmp7_;
	GtkGrid* _tmp8_;
	GeeArrayList* _tmp9_;
	gint _tmp10_;
	gint _tmp11_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (name != NULL);
	g_return_if_fail (stock_id != NULL);
	g_return_if_fail (component != NULL);
	_tmp0_ = self->priv->_list_store;
	gtk_list_store_append (_tmp0_, &_tmp1_);
	iter = _tmp1_;
	_tmp2_ = self->priv->_list_store;
	_tmp3_ = iter;
	_tmp4_ = stock_id;
	_tmp5_ = name;
	gtk_list_store_set (_tmp2_, &_tmp3_, SIDE_PANEL_SIDE_PANEL_COLUMN_PIXBUF, _tmp4_, SIDE_PANEL_SIDE_PANEL_COLUMN_NAME, _tmp5_, -1);
	_tmp6_ = self->priv->_components;
	_tmp7_ = component;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp6_, _tmp7_);
	_tmp8_ = component;
	_tmp9_ = self->priv->_components;
	_tmp10_ = gee_abstract_collection_get_size ((GeeCollection*) _tmp9_);
	_tmp11_ = _tmp10_;
	gtk_grid_attach ((GtkGrid*) self, (GtkWidget*) _tmp8_, 0, _tmp11_, 2, 1);
}


static gpointer _g_object_ref0 (gpointer self) {
	return self ? g_object_ref (self) : NULL;
}


void side_panel_restore_state (SidePanel* self) {
	GSettings* _tmp12_;
	gint _tmp13_ = 0;
	gint num;
	gint _tmp14_;
	GeeArrayList* _tmp15_;
	gint _tmp16_;
	gint _tmp17_;
	gint _tmp18_ = 0;
	GtkComboBox* _tmp19_;
	gint _tmp20_;
	GSettings* _tmp21_;
	GtkComboBox* _tmp22_;
	g_return_if_fail (self != NULL);
	{
		GeeArrayList* _tmp0_;
		GeeArrayList* _tmp1_;
		GeeArrayList* _component_list;
		GeeArrayList* _tmp2_;
		gint _tmp3_;
		gint _tmp4_;
		gint _component_size;
		gint _component_index;
		_tmp0_ = self->priv->_components;
		_tmp1_ = _g_object_ref0 (_tmp0_);
		_component_list = _tmp1_;
		_tmp2_ = _component_list;
		_tmp3_ = gee_abstract_collection_get_size ((GeeCollection*) _tmp2_);
		_tmp4_ = _tmp3_;
		_component_size = _tmp4_;
		_component_index = -1;
		while (TRUE) {
			gint _tmp5_;
			gint _tmp6_;
			gint _tmp7_;
			GeeArrayList* _tmp8_;
			gint _tmp9_;
			gpointer _tmp10_ = NULL;
			GtkGrid* component;
			GtkGrid* _tmp11_;
			_tmp5_ = _component_index;
			_component_index = _tmp5_ + 1;
			_tmp6_ = _component_index;
			_tmp7_ = _component_size;
			if (!(_tmp6_ < _tmp7_)) {
				break;
			}
			_tmp8_ = _component_list;
			_tmp9_ = _component_index;
			_tmp10_ = gee_abstract_list_get ((GeeAbstractList*) _tmp8_, _tmp9_);
			component = (GtkGrid*) _tmp10_;
			_tmp11_ = component;
			gtk_widget_hide ((GtkWidget*) _tmp11_);
			_g_object_unref0 (component);
		}
		_g_object_unref0 (_component_list);
	}
	_tmp12_ = self->priv->_settings;
	_tmp13_ = g_settings_get_int (_tmp12_, "side-panel-component");
	num = _tmp13_;
	_tmp14_ = num;
	_tmp15_ = self->priv->_components;
	_tmp16_ = gee_abstract_collection_get_size ((GeeCollection*) _tmp15_);
	_tmp17_ = _tmp16_;
	_tmp18_ = CLAMP (_tmp14_, 0, _tmp17_ - 1);
	num = _tmp18_;
	_tmp19_ = self->priv->_combo_box;
	_tmp20_ = num;
	gtk_combo_box_set_active (_tmp19_, _tmp20_);
	_tmp21_ = self->priv->_settings;
	_tmp22_ = self->priv->_combo_box;
	g_settings_bind (_tmp21_, "side-panel-component", (GObject*) _tmp22_, "active", G_SETTINGS_BIND_SET);
}


static void side_panel_show_active_component (SidePanel* self) {
	gboolean _tmp0_ = FALSE;
	gint _tmp1_;
	gboolean _tmp6_;
	GtkComboBox* _tmp11_;
	gint _tmp12_;
	gint _tmp13_;
	GeeArrayList* _tmp14_;
	gint _tmp15_;
	gpointer _tmp16_ = NULL;
	GtkGrid* _tmp17_;
	g_return_if_fail (self != NULL);
	_tmp1_ = self->priv->_current_component;
	if (0 <= _tmp1_) {
		gint _tmp2_;
		GeeArrayList* _tmp3_;
		gint _tmp4_;
		gint _tmp5_;
		_tmp2_ = self->priv->_current_component;
		_tmp3_ = self->priv->_components;
		_tmp4_ = gee_abstract_collection_get_size ((GeeCollection*) _tmp3_);
		_tmp5_ = _tmp4_;
		_tmp0_ = _tmp2_ < _tmp5_;
	} else {
		_tmp0_ = FALSE;
	}
	_tmp6_ = _tmp0_;
	if (_tmp6_) {
		GeeArrayList* _tmp7_;
		gint _tmp8_;
		gpointer _tmp9_ = NULL;
		GtkGrid* _tmp10_;
		_tmp7_ = self->priv->_components;
		_tmp8_ = self->priv->_current_component;
		_tmp9_ = gee_abstract_list_get ((GeeAbstractList*) _tmp7_, _tmp8_);
		_tmp10_ = (GtkGrid*) _tmp9_;
		gtk_widget_hide ((GtkWidget*) _tmp10_);
		_g_object_unref0 (_tmp10_);
	}
	_tmp11_ = self->priv->_combo_box;
	_tmp12_ = gtk_combo_box_get_active (_tmp11_);
	_tmp13_ = _tmp12_;
	self->priv->_current_component = _tmp13_;
	_tmp14_ = self->priv->_components;
	_tmp15_ = self->priv->_current_component;
	_tmp16_ = gee_abstract_list_get ((GeeAbstractList*) _tmp14_, _tmp15_);
	_tmp17_ = (GtkGrid*) _tmp16_;
	gtk_widget_show ((GtkWidget*) _tmp17_);
	_g_object_unref0 (_tmp17_);
}


static void side_panel_class_init (SidePanelClass * klass) {
	side_panel_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (SidePanelPrivate));
	G_OBJECT_CLASS (klass)->finalize = side_panel_finalize;
}


static void side_panel_instance_init (SidePanel * self) {
	self->priv = SIDE_PANEL_GET_PRIVATE (self);
	self->priv->_current_component = -1;
}


static void side_panel_finalize (GObject* obj) {
	SidePanel * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_SIDE_PANEL, SidePanel);
	_g_object_unref0 (self->priv->_settings);
	_g_object_unref0 (self->priv->_components);
	_g_object_unref0 (self->priv->_combo_box);
	_g_object_unref0 (self->priv->_list_store);
	G_OBJECT_CLASS (side_panel_parent_class)->finalize (obj);
}


GType side_panel_get_type (void) {
	static volatile gsize side_panel_type_id__volatile = 0;
	if (g_once_init_enter (&side_panel_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (SidePanelClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) side_panel_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (SidePanel), 0, (GInstanceInitFunc) side_panel_instance_init, NULL };
		GType side_panel_type_id;
		side_panel_type_id = g_type_register_static (GTK_TYPE_GRID, "SidePanel", &g_define_type_info, 0);
		g_once_init_leave (&side_panel_type_id__volatile, side_panel_type_id);
	}
	return side_panel_type_id__volatile;
}



