/* TemplateEditor.c generated by valac 0.56.19, the Vala compiler
 * generated from TemplateEditor.vala, do not modify */

/*
* Copyright (c) 2025-2026 (https://github.com/phase1geo/Minder)
*
* 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
*
* Authored by: Trevor Williams <phase1geo@gmail.com>
*/

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

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define TYPE_TEMPLATE_EDITOR (template_editor_get_type ())
#define TEMPLATE_EDITOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_TEMPLATE_EDITOR, TemplateEditor))
#define TEMPLATE_EDITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_TEMPLATE_EDITOR, TemplateEditorClass))
#define IS_TEMPLATE_EDITOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_TEMPLATE_EDITOR))
#define IS_TEMPLATE_EDITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_TEMPLATE_EDITOR))
#define TEMPLATE_EDITOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_TEMPLATE_EDITOR, TemplateEditorClass))

typedef struct _TemplateEditor TemplateEditor;
typedef struct _TemplateEditorClass TemplateEditorClass;
typedef struct _TemplateEditorPrivate TemplateEditorPrivate;

#define TYPE_TEMPLATE_GROUP (template_group_get_type ())
#define TEMPLATE_GROUP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_TEMPLATE_GROUP, TemplateGroup))
#define TEMPLATE_GROUP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_TEMPLATE_GROUP, TemplateGroupClass))
#define IS_TEMPLATE_GROUP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_TEMPLATE_GROUP))
#define IS_TEMPLATE_GROUP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_TEMPLATE_GROUP))
#define TEMPLATE_GROUP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_TEMPLATE_GROUP, TemplateGroupClass))

typedef struct _TemplateGroup TemplateGroup;
typedef struct _TemplateGroupClass TemplateGroupClass;
enum  {
	TEMPLATE_EDITOR_0_PROPERTY,
	TEMPLATE_EDITOR_NUM_PROPERTIES
};
static GParamSpec* template_editor_properties[TEMPLATE_EDITOR_NUM_PROPERTIES];
#define _template_group_unref0(var) ((var == NULL) ? NULL : (var = (template_group_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

#define TYPE_MAIN_WINDOW (main_window_get_type ())
#define MAIN_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_MAIN_WINDOW, MainWindow))
#define MAIN_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_MAIN_WINDOW, MainWindowClass))
#define IS_MAIN_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_MAIN_WINDOW))
#define IS_MAIN_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_MAIN_WINDOW))
#define MAIN_WINDOW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_MAIN_WINDOW, MainWindowClass))

typedef struct _MainWindow MainWindow;
typedef struct _MainWindowClass MainWindowClass;

#define TYPE_TEMPLATE (template_get_type ())
#define TEMPLATE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_TEMPLATE, Template))
#define TEMPLATE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_TEMPLATE, TemplateClass))
#define IS_TEMPLATE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_TEMPLATE))
#define IS_TEMPLATE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_TEMPLATE))
#define TEMPLATE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_TEMPLATE, TemplateClass))

typedef struct _Template Template;
typedef struct _TemplateClass TemplateClass;
typedef void (*TemplateAddLoadFunc) (Template* template, gpointer user_data);
typedef void (*TemplateDeleteFunc) (gpointer user_data);
typedef struct _Block76Data Block76Data;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_array_unref0(var) ((var == NULL) ? NULL : (var = (g_array_unref (var), NULL)))
#define _template_unref0(var) ((var == NULL) ? NULL : (var = (template_unref (var), NULL)))
typedef struct _Block77Data Block77Data;
enum  {
	TEMPLATE_EDITOR_CLOSE_SIGNAL,
	TEMPLATE_EDITOR_EDIT_MODE_CHANGED_SIGNAL,
	TEMPLATE_EDITOR_NUM_SIGNALS
};
static guint template_editor_signals[TEMPLATE_EDITOR_NUM_SIGNALS] = {0};

struct _TemplateEditor {
	GtkBox parent_instance;
	TemplateEditorPrivate * priv;
};

struct _TemplateEditorClass {
	GtkBoxClass parent_class;
};

struct _TemplateEditorPrivate {
	TemplateGroup* _templates;
	GeeHashSet* _names_to_delete;
	GtkListBox* _lb;
	GtkLabel* _add_label;
	GtkButton* _del_btn;
	GtkButton* _edit_btn;
};

struct _Block76Data {
	int _ref_count_;
	TemplateEditor* self;
	GtkSearchEntry* search_add;
	GtkButton* cancel_btn;
	TemplateGroup* templates;
	TemplateAddLoadFunc add_func;
	gpointer add_func_target;
	TemplateAddLoadFunc load_func;
	gpointer load_func_target;
	TemplateDeleteFunc del_func;
	gpointer del_func_target;
};

struct _Block77Data {
	int _ref_count_;
	TemplateEditor* self;
	GtkCheckButton* check;
	gchar* name;
};

static gint TemplateEditor_private_offset;
static gpointer template_editor_parent_class = NULL;

VALA_EXTERN GType template_editor_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (TemplateEditor, g_object_unref)
VALA_EXTERN gpointer template_group_ref (gpointer instance);
VALA_EXTERN void template_group_unref (gpointer instance);
VALA_EXTERN GParamSpec* param_spec_template_group (const gchar* name,
                                       const gchar* nick,
                                       const gchar* blurb,
                                       GType object_type,
                                       GParamFlags flags);
VALA_EXTERN void value_set_template_group (GValue* value,
                               gpointer v_object);
VALA_EXTERN void value_take_template_group (GValue* value,
                                gpointer v_object);
VALA_EXTERN gpointer value_get_template_group (const GValue* value);
VALA_EXTERN GType template_group_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (TemplateGroup, template_group_unref)
VALA_EXTERN GType main_window_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (MainWindow, g_object_unref)
VALA_EXTERN gpointer template_ref (gpointer instance);
VALA_EXTERN void template_unref (gpointer instance);
VALA_EXTERN GParamSpec* param_spec_template (const gchar* name,
                                 const gchar* nick,
                                 const gchar* blurb,
                                 GType object_type,
                                 GParamFlags flags);
VALA_EXTERN void value_set_template (GValue* value,
                         gpointer v_object);
VALA_EXTERN void value_take_template (GValue* value,
                          gpointer v_object);
VALA_EXTERN gpointer value_get_template (const GValue* value);
VALA_EXTERN GType template_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (Template, template_unref)
VALA_EXTERN TemplateEditor* template_editor_new (MainWindow* win,
                                     TemplateGroup* templates,
                                     TemplateAddLoadFunc add_func,
                                     gpointer add_func_target,
                                     TemplateAddLoadFunc load_func,
                                     gpointer load_func_target,
                                     TemplateDeleteFunc del_func,
                                     gpointer del_func_target);
VALA_EXTERN TemplateEditor* template_editor_construct (GType object_type,
                                           MainWindow* win,
                                           TemplateGroup* templates,
                                           TemplateAddLoadFunc add_func,
                                           gpointer add_func_target,
                                           TemplateAddLoadFunc load_func,
                                           gpointer load_func_target,
                                           TemplateDeleteFunc del_func,
                                           gpointer del_func_target);
static Block76Data* block76_data_ref (Block76Data* _data76_);
static void block76_data_unref (void * _userdata_);
static void __lambda17_ (Block76Data* _data76_);
VALA_EXTERN GArray* template_group_get_names (TemplateGroup* self);
static void ___lambda17__gtk_search_entry_search_changed (GtkSearchEntry* _sender,
                                                   gpointer self);
static void __lambda18_ (Block76Data* _data76_);
VALA_EXTERN gboolean template_group_save_as_template (TemplateGroup* self,
                                          const gchar* name,
                                          TemplateAddLoadFunc func,
                                          gpointer func_target);
static void ___lambda18__gtk_search_entry_activate (GtkSearchEntry* _sender,
                                             gpointer self);
static void __lambda19_ (Block76Data* _data76_,
                  GtkListBoxRow* row);
VALA_EXTERN Template* template_group_get_template (TemplateGroup* self,
                                       const gchar* name);
static void ___lambda19__gtk_list_box_row_selected (GtkListBox* _sender,
                                             GtkListBoxRow* row,
                                             gpointer self);
static void __lambda20_ (TemplateEditor* self);
static void ___lambda20__gtk_button_clicked (GtkButton* _sender,
                                      gpointer self);
static void __lambda21_ (Block76Data* _data76_);
static gboolean __lambda22_ (TemplateEditor* self,
                      gchar* name);
VALA_EXTERN gboolean template_group_delete_template (TemplateGroup* self,
                                         const gchar* name);
static gboolean ___lambda22__gee_forall_func (gpointer g,
                                       gpointer self);
static void ___lambda21__gtk_button_clicked (GtkButton* _sender,
                                      gpointer self);
static void __lambda23_ (TemplateEditor* self);
static void ___lambda23__gtk_button_clicked (GtkButton* _sender,
                                      gpointer self);
static void __lambda24_ (Block76Data* _data76_,
                  gboolean edit_mode);
static void ___lambda24__template_editor_edit_mode_changed (TemplateEditor* _sender,
                                                     gboolean edit_mode,
                                                     gpointer self);
static void template_editor_create_item (TemplateEditor* self,
                                  const gchar* name);
static Block77Data* block77_data_ref (Block77Data* _data77_);
static void block77_data_unref (void * _userdata_);
static void __lambda25_ (Block77Data* _data77_);
static void ___lambda25__gtk_check_button_toggled (GtkCheckButton* _sender,
                                            gpointer self);
static void __lambda26_ (Block77Data* _data77_,
                  gboolean edit_mode);
static void ___lambda26__template_editor_edit_mode_changed (TemplateEditor* _sender,
                                                     gboolean edit_mode,
                                                     gpointer self);
VALA_EXTERN void template_editor_update_list (TemplateEditor* self);
static void __lambda27_ (TemplateEditor* self,
                  gboolean edit_mode);
static void ___lambda27__template_editor_edit_mode_changed (TemplateEditor* _sender,
                                                     gboolean edit_mode,
                                                     gpointer self);
static void template_editor_finalize (GObject * obj);
static GType template_editor_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
template_editor_get_instance_private (TemplateEditor* self)
{
	return G_STRUCT_MEMBER_P (self, TemplateEditor_private_offset);
}

static gpointer
_template_group_ref0 (gpointer self)
{
	return self ? template_group_ref (self) : NULL;
}

static Block76Data*
block76_data_ref (Block76Data* _data76_)
{
	g_atomic_int_inc (&_data76_->_ref_count_);
	return _data76_;
}

static void
block76_data_unref (void * _userdata_)
{
	Block76Data* _data76_;
	_data76_ = (Block76Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data76_->_ref_count_)) {
		TemplateEditor* self;
		self = _data76_->self;
		_g_object_unref0 (_data76_->cancel_btn);
		_g_object_unref0 (_data76_->search_add);
		_template_group_unref0 (_data76_->templates);
		_g_object_unref0 (self);
		g_slice_free (Block76Data, _data76_);
	}
}

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

static gboolean
string_contains (const gchar* self,
                 const gchar* needle)
{
	gchar* _tmp0_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (needle != NULL, FALSE);
	_tmp0_ = strstr ((gchar*) self, (gchar*) needle);
	result = _tmp0_ != NULL;
	return result;
}

static void
__lambda17_ (Block76Data* _data76_)
{
	TemplateEditor* self;
	gchar* text = NULL;
	const gchar* _tmp0_;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	GArray* names = NULL;
	TemplateGroup* _tmp3_;
	GArray* _tmp4_;
	gboolean _tmp22_ = FALSE;
	const gchar* _tmp23_;
	GtkLabel* _tmp27_;
	GtkLabel* _tmp28_;
	const gchar* _tmp29_;
	gchar* _tmp30_;
	gchar* _tmp31_;
	self = _data76_->self;
	_tmp0_ = gtk_editable_get_text ((GtkEditable*) _data76_->search_add);
	_tmp1_ = _tmp0_;
	_tmp2_ = g_strdup (_tmp1_);
	text = _tmp2_;
	_tmp3_ = self->priv->_templates;
	_tmp4_ = template_group_get_names (_tmp3_);
	names = _tmp4_;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp5_ = FALSE;
			_tmp5_ = TRUE;
			while (TRUE) {
				GArray* _tmp7_;
				GtkListBoxRow* row = NULL;
				GtkListBox* _tmp8_;
				GtkListBoxRow* _tmp9_;
				GtkListBoxRow* _tmp10_;
				gchar* name = NULL;
				GArray* _tmp11_;
				const gchar* _tmp12_;
				gchar* _tmp13_;
				gboolean _tmp14_ = FALSE;
				gboolean _tmp15_ = FALSE;
				const gchar* _tmp16_;
				GtkListBoxRow* _tmp21_;
				if (!_tmp5_) {
					gint _tmp6_;
					_tmp6_ = i;
					i = _tmp6_ + 1;
				}
				_tmp5_ = FALSE;
				_tmp7_ = names;
				if (!(((guint) i) < _tmp7_->len)) {
					break;
				}
				_tmp8_ = self->priv->_lb;
				_tmp9_ = gtk_list_box_get_row_at_index (_tmp8_, i);
				_tmp10_ = _g_object_ref0 (_tmp9_);
				row = _tmp10_;
				_tmp11_ = names;
				_tmp12_ = g_array_index (_tmp11_, gchar*, (guint) i);
				_tmp13_ = g_strdup (_tmp12_);
				name = _tmp13_;
				_tmp16_ = text;
				if (g_strcmp0 (_tmp16_, "") == 0) {
					_tmp15_ = TRUE;
				} else {
					const gchar* _tmp17_;
					const gchar* _tmp18_;
					_tmp17_ = name;
					_tmp18_ = text;
					_tmp15_ = string_contains (_tmp17_, _tmp18_);
				}
				if (_tmp15_) {
					_tmp14_ = TRUE;
				} else {
					GeeHashSet* _tmp19_;
					const gchar* _tmp20_;
					_tmp19_ = self->priv->_names_to_delete;
					_tmp20_ = name;
					_tmp14_ = gee_abstract_collection_contains ((GeeAbstractCollection*) _tmp19_, _tmp20_);
				}
				_tmp21_ = row;
				gtk_widget_set_visible ((GtkWidget*) _tmp21_, _tmp14_);
				_g_free0 (name);
				_g_object_unref0 (row);
			}
		}
	}
	_tmp23_ = text;
	if (g_strcmp0 (_tmp23_, "") != 0) {
		GtkButton* _tmp24_;
		gboolean _tmp25_;
		gboolean _tmp26_;
		_tmp24_ = self->priv->_edit_btn;
		_tmp25_ = gtk_widget_get_visible ((GtkWidget*) _tmp24_);
		_tmp26_ = _tmp25_;
		_tmp22_ = _tmp26_;
	} else {
		_tmp22_ = FALSE;
	}
	_tmp27_ = self->priv->_add_label;
	gtk_widget_set_visible ((GtkWidget*) _tmp27_, _tmp22_);
	_tmp28_ = self->priv->_add_label;
	_tmp29_ = text;
	_tmp30_ = g_strdup_printf (_ ("Create '%s' template"), _tmp29_);
	_tmp31_ = _tmp30_;
	gtk_label_set_label (_tmp28_, _tmp31_);
	_g_free0 (_tmp31_);
	_g_array_unref0 (names);
	_g_free0 (text);
}

static void
___lambda17__gtk_search_entry_search_changed (GtkSearchEntry* _sender,
                                              gpointer self)
{
	__lambda17_ (self);
}

static void
__lambda18_ (Block76Data* _data76_)
{
	TemplateEditor* self;
	TemplateGroup* _tmp0_;
	const gchar* _tmp1_;
	const gchar* _tmp2_;
	self = _data76_->self;
	_tmp0_ = self->priv->_templates;
	_tmp1_ = gtk_editable_get_text ((GtkEditable*) _data76_->search_add);
	_tmp2_ = _tmp1_;
	template_group_save_as_template (_tmp0_, _tmp2_, _data76_->add_func, _data76_->add_func_target);
	gtk_editable_set_text ((GtkEditable*) _data76_->search_add, "");
}

static void
___lambda18__gtk_search_entry_activate (GtkSearchEntry* _sender,
                                        gpointer self)
{
	__lambda18_ (self);
}

static void
__lambda19_ (Block76Data* _data76_,
             GtkListBoxRow* row)
{
	TemplateEditor* self;
	GtkButton* _tmp0_;
	gboolean _tmp1_;
	gboolean _tmp2_;
	self = _data76_->self;
	if (row == NULL) {
		return;
	}
	g_signal_emit (self, template_editor_signals[TEMPLATE_EDITOR_CLOSE_SIGNAL], 0);
	_tmp0_ = self->priv->_edit_btn;
	_tmp1_ = gtk_widget_get_visible ((GtkWidget*) _tmp0_);
	_tmp2_ = _tmp1_;
	if (_tmp2_) {
		gint index = 0;
		GArray* names = NULL;
		GArray* _tmp3_;
		GArray* _tmp4_;
		index = gtk_list_box_row_get_index (row);
		_tmp3_ = template_group_get_names (_data76_->templates);
		names = _tmp3_;
		_tmp4_ = names;
		if (((guint) index) == _tmp4_->len) {
			TemplateGroup* _tmp5_;
			const gchar* _tmp6_;
			const gchar* _tmp7_;
			_tmp5_ = self->priv->_templates;
			_tmp6_ = gtk_editable_get_text ((GtkEditable*) _data76_->search_add);
			_tmp7_ = _tmp6_;
			template_group_save_as_template (_tmp5_, _tmp7_, _data76_->add_func, _data76_->add_func_target);
		} else {
			Template* template = NULL;
			TemplateGroup* _tmp8_;
			GArray* _tmp9_;
			const gchar* _tmp10_;
			Template* _tmp11_;
			Template* _tmp12_;
			_tmp8_ = self->priv->_templates;
			_tmp9_ = names;
			_tmp10_ = g_array_index (_tmp9_, gchar*, (guint) index);
			_tmp11_ = template_group_get_template (_tmp8_, _tmp10_);
			template = _tmp11_;
			_tmp12_ = template;
			if (_tmp12_ != NULL) {
				Template* _tmp13_;
				_tmp13_ = template;
				_data76_->load_func (_tmp13_, _data76_->load_func_target);
			}
			_template_unref0 (template);
		}
		_g_array_unref0 (names);
	}
}

static void
___lambda19__gtk_list_box_row_selected (GtkListBox* _sender,
                                        GtkListBoxRow* row,
                                        gpointer self)
{
	__lambda19_ (self, row);
}

static void
__lambda20_ (TemplateEditor* self)
{
	g_signal_emit (self, template_editor_signals[TEMPLATE_EDITOR_EDIT_MODE_CHANGED_SIGNAL], 0, FALSE);
}

static void
___lambda20__gtk_button_clicked (GtkButton* _sender,
                                 gpointer self)
{
	__lambda20_ ((TemplateEditor*) self);
}

static gboolean
__lambda22_ (TemplateEditor* self,
             gchar* name)
{
	TemplateGroup* _tmp0_;
	gboolean result;
	g_return_val_if_fail (name != NULL, FALSE);
	_tmp0_ = self->priv->_templates;
	template_group_delete_template (_tmp0_, name);
	result = TRUE;
	_g_free0 (name);
	return result;
}

static gboolean
___lambda22__gee_forall_func (gpointer g,
                              gpointer self)
{
	gboolean result;
	result = __lambda22_ ((TemplateEditor*) self, (gchar*) g);
	return result;
}

static void
__lambda21_ (Block76Data* _data76_)
{
	TemplateEditor* self;
	GeeHashSet* _tmp0_;
	GeeHashSet* _tmp1_;
	self = _data76_->self;
	g_signal_emit (self, template_editor_signals[TEMPLATE_EDITOR_CLOSE_SIGNAL], 0);
	_tmp0_ = self->priv->_names_to_delete;
	gee_abstract_collection_foreach ((GeeAbstractCollection*) _tmp0_, ___lambda22__gee_forall_func, self);
	_tmp1_ = self->priv->_names_to_delete;
	gee_abstract_collection_clear ((GeeAbstractCollection*) _tmp1_);
	g_signal_emit (self, template_editor_signals[TEMPLATE_EDITOR_EDIT_MODE_CHANGED_SIGNAL], 0, FALSE);
	_data76_->del_func (_data76_->del_func_target);
}

static void
___lambda21__gtk_button_clicked (GtkButton* _sender,
                                 gpointer self)
{
	__lambda21_ (self);
}

static void
__lambda23_ (TemplateEditor* self)
{
	g_signal_emit (self, template_editor_signals[TEMPLATE_EDITOR_EDIT_MODE_CHANGED_SIGNAL], 0, TRUE);
}

static void
___lambda23__gtk_button_clicked (GtkButton* _sender,
                                 gpointer self)
{
	__lambda23_ ((TemplateEditor*) self);
}

static void
__lambda24_ (Block76Data* _data76_,
             gboolean edit_mode)
{
	TemplateEditor* self;
	const gchar* _tmp0_ = NULL;
	GtkButton* _tmp1_;
	GtkButton* _tmp2_;
	self = _data76_->self;
	if (edit_mode) {
		_tmp0_ = _ ("Search Templates");
	} else {
		_tmp0_ = _ ("Create or Search Templates");
	}
	g_object_set (_data76_->search_add, "placeholder-text", _tmp0_, NULL);
	_tmp1_ = self->priv->_del_btn;
	gtk_widget_set_visible ((GtkWidget*) _tmp1_, edit_mode);
	gtk_widget_set_visible ((GtkWidget*) _data76_->cancel_btn, edit_mode);
	_tmp2_ = self->priv->_edit_btn;
	gtk_widget_set_visible ((GtkWidget*) _tmp2_, !edit_mode);
}

static void
___lambda24__template_editor_edit_mode_changed (TemplateEditor* _sender,
                                                gboolean edit_mode,
                                                gpointer self)
{
	__lambda24_ (self, edit_mode);
}

TemplateEditor*
template_editor_construct (GType object_type,
                           MainWindow* win,
                           TemplateGroup* templates,
                           TemplateAddLoadFunc add_func,
                           gpointer add_func_target,
                           TemplateAddLoadFunc load_func,
                           gpointer load_func_target,
                           TemplateDeleteFunc del_func,
                           gpointer del_func_target)
{
	TemplateEditor * self = NULL;
	Block76Data* _data76_;
	TemplateGroup* _tmp0_;
	TemplateGroup* _tmp1_;
	GeeHashSet* _tmp2_;
	gchar* _tmp3_;
	GtkSearchEntry* _tmp4_ = NULL;
	GtkListBox* _tmp5_ = NULL;
	GtkScrolledWindow* scrolled = NULL;
	GtkListBox* _tmp6_;
	GtkWidget* _tmp7_;
	GtkScrolledWindow* _tmp8_ = NULL;
	GtkListBox* _tmp9_;
	GtkButton* _tmp10_ = NULL;
	GtkButton* _tmp11_ = NULL;
	GtkButton* _tmp12_;
	GtkButton* _tmp13_;
	GtkButton* _tmp14_ = NULL;
	GtkButton* _tmp15_;
	GtkBox* btn_box = NULL;
	GtkBox* _tmp16_ = NULL;
	GtkButton* _tmp17_;
	GtkButton* _tmp18_;
	g_return_val_if_fail (win != NULL, NULL);
	g_return_val_if_fail (templates != NULL, NULL);
	_data76_ = g_slice_new0 (Block76Data);
	_data76_->_ref_count_ = 1;
	_tmp0_ = _template_group_ref0 (templates);
	_template_group_unref0 (_data76_->templates);
	_data76_->templates = _tmp0_;
	_data76_->add_func = add_func;
	_data76_->add_func_target = add_func_target;
	_data76_->load_func = load_func;
	_data76_->load_func_target = load_func_target;
	_data76_->del_func = del_func;
	_data76_->del_func_target = del_func_target;
	self = (TemplateEditor*) g_object_new (object_type, "orientation", GTK_ORIENTATION_VERTICAL, "spacing", 5, NULL);
	_data76_->self = g_object_ref (self);
	_tmp1_ = _template_group_ref0 (_data76_->templates);
	_template_group_unref0 (self->priv->_templates);
	self->priv->_templates = _tmp1_;
	_tmp2_ = gee_hash_set_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, NULL, NULL, NULL, NULL, NULL, NULL);
	_g_object_unref0 (self->priv->_names_to_delete);
	self->priv->_names_to_delete = _tmp2_;
	_tmp3_ = g_strdup (_ ("Create or search templates"));
	_tmp4_ = (GtkSearchEntry*) gtk_search_entry_new ();
	gtk_widget_set_halign ((GtkWidget*) _tmp4_, GTK_ALIGN_FILL);
	gtk_widget_set_margin_start ((GtkWidget*) _tmp4_, 5);
	gtk_widget_set_margin_end ((GtkWidget*) _tmp4_, 5);
	g_object_set (_tmp4_, "placeholder-text", _tmp3_, NULL);
	_g_free0 (_tmp3_);
	gtk_editable_set_width_chars ((GtkEditable*) _tmp4_, 40);
	g_object_ref_sink (_tmp4_);
	_data76_->search_add = _tmp4_;
	g_signal_connect_data (_data76_->search_add, "search-changed", (GCallback) ___lambda17__gtk_search_entry_search_changed, block76_data_ref (_data76_), (GClosureNotify) block76_data_unref, 0);
	g_signal_connect_data (_data76_->search_add, "activate", (GCallback) ___lambda18__gtk_search_entry_activate, block76_data_ref (_data76_), (GClosureNotify) block76_data_unref, 0);
	_tmp5_ = (GtkListBox*) gtk_list_box_new ();
	gtk_widget_set_valign ((GtkWidget*) _tmp5_, GTK_ALIGN_START);
	g_object_ref_sink (_tmp5_);
	_g_object_unref0 (self->priv->_lb);
	self->priv->_lb = _tmp5_;
	_tmp6_ = self->priv->_lb;
	_tmp7_ = _g_object_ref0 ((GtkWidget*) _tmp6_);
	_tmp8_ = (GtkScrolledWindow*) gtk_scrolled_window_new ();
	g_object_set (_tmp8_, "hscrollbar-policy", GTK_POLICY_NEVER, NULL);
	g_object_set (_tmp8_, "vscrollbar-policy", GTK_POLICY_AUTOMATIC, NULL);
	gtk_scrolled_window_set_child (_tmp8_, _tmp7_);
	_g_object_unref0 (_tmp7_);
	g_object_ref_sink (_tmp8_);
	scrolled = _tmp8_;
	gtk_widget_set_size_request ((GtkWidget*) scrolled, -1, 300);
	_tmp9_ = self->priv->_lb;
	g_signal_connect_data (_tmp9_, "row-selected", (GCallback) ___lambda19__gtk_list_box_row_selected, block76_data_ref (_data76_), (GClosureNotify) block76_data_unref, 0);
	_tmp10_ = (GtkButton*) gtk_button_new_with_label (_ ("Cancel"));
	gtk_widget_set_halign ((GtkWidget*) _tmp10_, GTK_ALIGN_END);
	gtk_widget_set_hexpand ((GtkWidget*) _tmp10_, TRUE);
	gtk_widget_set_visible ((GtkWidget*) _tmp10_, FALSE);
	g_object_ref_sink (_tmp10_);
	_data76_->cancel_btn = _tmp10_;
	g_signal_connect_object (_data76_->cancel_btn, "clicked", (GCallback) ___lambda20__gtk_button_clicked, self, 0);
	_tmp11_ = (GtkButton*) gtk_button_new_with_label (_ ("Delete"));
	gtk_widget_set_halign ((GtkWidget*) _tmp11_, GTK_ALIGN_START);
	gtk_widget_set_hexpand ((GtkWidget*) _tmp11_, TRUE);
	gtk_widget_set_visible ((GtkWidget*) _tmp11_, FALSE);
	gtk_widget_set_sensitive ((GtkWidget*) _tmp11_, FALSE);
	g_object_ref_sink (_tmp11_);
	_g_object_unref0 (self->priv->_del_btn);
	self->priv->_del_btn = _tmp11_;
	_tmp12_ = self->priv->_del_btn;
	gtk_widget_add_css_class ((GtkWidget*) _tmp12_, "destructive-action");
	_tmp13_ = self->priv->_del_btn;
	g_signal_connect_data (_tmp13_, "clicked", (GCallback) ___lambda21__gtk_button_clicked, block76_data_ref (_data76_), (GClosureNotify) block76_data_unref, 0);
	_tmp14_ = (GtkButton*) gtk_button_new_with_label (_ ("Edit"));
	gtk_widget_set_halign ((GtkWidget*) _tmp14_, GTK_ALIGN_END);
	gtk_widget_set_hexpand ((GtkWidget*) _tmp14_, TRUE);
	gtk_widget_set_sensitive ((GtkWidget*) _tmp14_, FALSE);
	g_object_ref_sink (_tmp14_);
	_g_object_unref0 (self->priv->_edit_btn);
	self->priv->_edit_btn = _tmp14_;
	_tmp15_ = self->priv->_edit_btn;
	g_signal_connect_object (_tmp15_, "clicked", (GCallback) ___lambda23__gtk_button_clicked, self, 0);
	g_signal_connect_data (self, "edit-mode-changed", (GCallback) ___lambda24__template_editor_edit_mode_changed, block76_data_ref (_data76_), (GClosureNotify) block76_data_unref, 0);
	_tmp16_ = (GtkBox*) gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
	gtk_widget_set_margin_start ((GtkWidget*) _tmp16_, 5);
	gtk_widget_set_margin_end ((GtkWidget*) _tmp16_, 5);
	g_object_ref_sink (_tmp16_);
	btn_box = _tmp16_;
	_tmp17_ = self->priv->_del_btn;
	gtk_box_append (btn_box, (GtkWidget*) _tmp17_);
	_tmp18_ = self->priv->_edit_btn;
	gtk_box_append (btn_box, (GtkWidget*) _tmp18_);
	gtk_box_append (btn_box, (GtkWidget*) _data76_->cancel_btn);
	gtk_box_append ((GtkBox*) self, (GtkWidget*) _data76_->search_add);
	gtk_box_append ((GtkBox*) self, (GtkWidget*) scrolled);
	gtk_box_append ((GtkBox*) self, (GtkWidget*) btn_box);
	_g_object_unref0 (btn_box);
	_g_object_unref0 (scrolled);
	block76_data_unref (_data76_);
	_data76_ = NULL;
	return self;
}

TemplateEditor*
template_editor_new (MainWindow* win,
                     TemplateGroup* templates,
                     TemplateAddLoadFunc add_func,
                     gpointer add_func_target,
                     TemplateAddLoadFunc load_func,
                     gpointer load_func_target,
                     TemplateDeleteFunc del_func,
                     gpointer del_func_target)
{
	return template_editor_construct (TYPE_TEMPLATE_EDITOR, win, templates, add_func, add_func_target, load_func, load_func_target, del_func, del_func_target);
}

static Block77Data*
block77_data_ref (Block77Data* _data77_)
{
	g_atomic_int_inc (&_data77_->_ref_count_);
	return _data77_;
}

static void
block77_data_unref (void * _userdata_)
{
	Block77Data* _data77_;
	_data77_ = (Block77Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data77_->_ref_count_)) {
		TemplateEditor* self;
		self = _data77_->self;
		_g_object_unref0 (_data77_->check);
		_g_free0 (_data77_->name);
		_g_object_unref0 (self);
		g_slice_free (Block77Data, _data77_);
	}
}

static void
__lambda25_ (Block77Data* _data77_)
{
	TemplateEditor* self;
	gboolean _tmp0_;
	gboolean _tmp1_;
	GtkButton* _tmp4_;
	GeeHashSet* _tmp5_;
	gint _tmp6_;
	gint _tmp7_;
	self = _data77_->self;
	_tmp0_ = gtk_check_button_get_active (_data77_->check);
	_tmp1_ = _tmp0_;
	if (_tmp1_) {
		GeeHashSet* _tmp2_;
		_tmp2_ = self->priv->_names_to_delete;
		gee_abstract_collection_add ((GeeAbstractCollection*) _tmp2_, _data77_->name);
	} else {
		GeeHashSet* _tmp3_;
		_tmp3_ = self->priv->_names_to_delete;
		gee_abstract_collection_remove ((GeeAbstractCollection*) _tmp3_, _data77_->name);
	}
	_tmp4_ = self->priv->_del_btn;
	_tmp5_ = self->priv->_names_to_delete;
	_tmp6_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp5_);
	_tmp7_ = _tmp6_;
	gtk_widget_set_sensitive ((GtkWidget*) _tmp4_, _tmp7_ > 0);
}

static void
___lambda25__gtk_check_button_toggled (GtkCheckButton* _sender,
                                       gpointer self)
{
	__lambda25_ (self);
}

static void
__lambda26_ (Block77Data* _data77_,
             gboolean edit_mode)
{
	TemplateEditor* self;
	self = _data77_->self;
	gtk_widget_set_visible ((GtkWidget*) _data77_->check, edit_mode);
	if (!edit_mode) {
		gtk_check_button_set_active (_data77_->check, FALSE);
	}
}

static void
___lambda26__template_editor_edit_mode_changed (TemplateEditor* _sender,
                                                gboolean edit_mode,
                                                gpointer self)
{
	__lambda26_ (self, edit_mode);
}

static void
template_editor_create_item (TemplateEditor* self,
                             const gchar* name)
{
	Block77Data* _data77_;
	gchar* _tmp0_;
	GtkCheckButton* _tmp1_ = NULL;
	GtkLabel* lbl = NULL;
	GtkLabel* _tmp2_ = NULL;
	GtkBox* box = NULL;
	GtkBox* _tmp3_;
	GtkListBox* _tmp4_;
	GtkButton* _tmp5_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (name != NULL);
	_data77_ = g_slice_new0 (Block77Data);
	_data77_->_ref_count_ = 1;
	_data77_->self = g_object_ref (self);
	_tmp0_ = g_strdup (name);
	_g_free0 (_data77_->name);
	_data77_->name = _tmp0_;
	_tmp1_ = (GtkCheckButton*) gtk_check_button_new ();
	gtk_widget_set_halign ((GtkWidget*) _tmp1_, GTK_ALIGN_START);
	gtk_widget_set_visible ((GtkWidget*) _tmp1_, FALSE);
	g_object_ref_sink (_tmp1_);
	_data77_->check = _tmp1_;
	g_signal_connect_data (_data77_->check, "toggled", (GCallback) ___lambda25__gtk_check_button_toggled, block77_data_ref (_data77_), (GClosureNotify) block77_data_unref, 0);
	g_signal_connect_data (self, "edit-mode-changed", (GCallback) ___lambda26__template_editor_edit_mode_changed, block77_data_ref (_data77_), (GClosureNotify) block77_data_unref, 0);
	_tmp2_ = (GtkLabel*) gtk_label_new (_data77_->name);
	gtk_widget_set_halign ((GtkWidget*) _tmp2_, GTK_ALIGN_START);
	g_object_ref_sink (_tmp2_);
	lbl = _tmp2_;
	_tmp3_ = (GtkBox*) gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
	g_object_ref_sink (_tmp3_);
	box = _tmp3_;
	gtk_box_append (box, (GtkWidget*) _data77_->check);
	gtk_box_append (box, (GtkWidget*) lbl);
	_tmp4_ = self->priv->_lb;
	gtk_list_box_append (_tmp4_, (GtkWidget*) box);
	_tmp5_ = self->priv->_edit_btn;
	gtk_widget_set_sensitive ((GtkWidget*) _tmp5_, TRUE);
	_g_object_unref0 (box);
	_g_object_unref0 (lbl);
	block77_data_unref (_data77_);
	_data77_ = NULL;
}

static void
__lambda27_ (TemplateEditor* self,
             gboolean edit_mode)
{
	GtkLabel* _tmp0_;
	_tmp0_ = self->priv->_add_label;
	gtk_widget_set_visible ((GtkWidget*) _tmp0_, !edit_mode);
}

static void
___lambda27__template_editor_edit_mode_changed (TemplateEditor* _sender,
                                                gboolean edit_mode,
                                                gpointer self)
{
	__lambda27_ ((TemplateEditor*) self, edit_mode);
}

void
template_editor_update_list (TemplateEditor* self)
{
	GtkListBox* _tmp0_;
	GArray* names = NULL;
	TemplateGroup* _tmp1_;
	GArray* _tmp2_;
	GtkLabel* _tmp8_ = NULL;
	GtkListBox* _tmp9_;
	GtkLabel* _tmp10_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->_lb;
	gtk_list_box_remove_all (_tmp0_);
	_tmp1_ = self->priv->_templates;
	_tmp2_ = template_group_get_names (_tmp1_);
	names = _tmp2_;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp3_ = FALSE;
			_tmp3_ = TRUE;
			while (TRUE) {
				GArray* _tmp5_;
				GArray* _tmp6_;
				const gchar* _tmp7_;
				if (!_tmp3_) {
					gint _tmp4_;
					_tmp4_ = i;
					i = _tmp4_ + 1;
				}
				_tmp3_ = FALSE;
				_tmp5_ = names;
				if (!(((guint) i) < _tmp5_->len)) {
					break;
				}
				_tmp6_ = names;
				_tmp7_ = g_array_index (_tmp6_, gchar*, (guint) i);
				template_editor_create_item (self, _tmp7_);
			}
		}
	}
	_tmp8_ = (GtkLabel*) gtk_label_new ("");
	gtk_widget_set_halign ((GtkWidget*) _tmp8_, GTK_ALIGN_START);
	gtk_widget_set_visible ((GtkWidget*) _tmp8_, FALSE);
	g_object_ref_sink (_tmp8_);
	_g_object_unref0 (self->priv->_add_label);
	self->priv->_add_label = _tmp8_;
	g_signal_connect_object (self, "edit-mode-changed", (GCallback) ___lambda27__template_editor_edit_mode_changed, self, 0);
	_tmp9_ = self->priv->_lb;
	_tmp10_ = self->priv->_add_label;
	gtk_list_box_append (_tmp9_, (GtkWidget*) _tmp10_);
	_g_array_unref0 (names);
}

static void
template_editor_class_init (TemplateEditorClass * klass,
                            gpointer klass_data)
{
	template_editor_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &TemplateEditor_private_offset);
	G_OBJECT_CLASS (klass)->finalize = template_editor_finalize;
	template_editor_signals[TEMPLATE_EDITOR_CLOSE_SIGNAL] = g_signal_new ("close", TYPE_TEMPLATE_EDITOR, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
	template_editor_signals[TEMPLATE_EDITOR_EDIT_MODE_CHANGED_SIGNAL] = g_signal_new ("edit-mode-changed", TYPE_TEMPLATE_EDITOR, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__BOOLEAN, G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
}

static void
template_editor_instance_init (TemplateEditor * self,
                               gpointer klass)
{
	self->priv = template_editor_get_instance_private (self);
}

static void
template_editor_finalize (GObject * obj)
{
	TemplateEditor * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_TEMPLATE_EDITOR, TemplateEditor);
	_template_group_unref0 (self->priv->_templates);
	_g_object_unref0 (self->priv->_names_to_delete);
	_g_object_unref0 (self->priv->_lb);
	_g_object_unref0 (self->priv->_add_label);
	_g_object_unref0 (self->priv->_del_btn);
	_g_object_unref0 (self->priv->_edit_btn);
	G_OBJECT_CLASS (template_editor_parent_class)->finalize (obj);
}

static GType
template_editor_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (TemplateEditorClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) template_editor_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (TemplateEditor), 0, (GInstanceInitFunc) template_editor_instance_init, NULL };
	GType template_editor_type_id;
	template_editor_type_id = g_type_register_static (gtk_box_get_type (), "TemplateEditor", &g_define_type_info, 0);
	TemplateEditor_private_offset = g_type_add_instance_private (template_editor_type_id, sizeof (TemplateEditorPrivate));
	return template_editor_type_id;
}

GType
template_editor_get_type (void)
{
	static volatile gsize template_editor_type_id__once = 0;
	if (g_once_init_enter (&template_editor_type_id__once)) {
		GType template_editor_type_id;
		template_editor_type_id = template_editor_get_type_once ();
		g_once_init_leave (&template_editor_type_id__once, template_editor_type_id);
	}
	return template_editor_type_id__once;
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

