/* math-variable-popover.c generated by valac 0.56.0, the Vala compiler
 * generated from math-variable-popover.vala, do not modify */

/*
 * Copyright (C) 2008-2012 Robert Ancell
 *
 * 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. See http://www.gnu.org/copyleft/gpl.html the full text of the
 * license.
 */

#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "calculator.h"
#include <gtk/gtk.h>
#include <gio/gio.h>

#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_MATH_VARIABLE (math_variable_get_type ())
#define MATH_VARIABLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_MATH_VARIABLE, MathVariable))
#define MATH_VARIABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_MATH_VARIABLE, MathVariableClass))
#define IS_MATH_VARIABLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_MATH_VARIABLE))
#define IS_MATH_VARIABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_MATH_VARIABLE))
#define MATH_VARIABLE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_MATH_VARIABLE, MathVariableClass))

typedef struct _MathVariable MathVariable;
typedef struct _MathVariableClass MathVariableClass;
typedef struct _MathVariablePrivate MathVariablePrivate;
enum  {
	MATH_VARIABLE_0_PROPERTY,
	MATH_VARIABLE_NUM_PROPERTIES
};
static GParamSpec* math_variable_properties[MATH_VARIABLE_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

#define TYPE_MATH_POPOVER (math_popover_get_type ())
#define MATH_POPOVER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_MATH_POPOVER, MathPopover))
#define MATH_POPOVER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_MATH_POPOVER, MathPopoverClass))
#define IS_MATH_POPOVER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_MATH_POPOVER))
#define IS_MATH_POPOVER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_MATH_POPOVER))
#define MATH_POPOVER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_MATH_POPOVER, MathPopoverClass))

typedef struct _MathPopover MathPopover;
typedef struct _MathPopoverClass MathPopoverClass;
typedef struct _MathPopoverPrivate MathPopoverPrivate;

#define TYPE_MATH_VARIABLE_POPOVER (math_variable_popover_get_type ())
#define MATH_VARIABLE_POPOVER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_MATH_VARIABLE_POPOVER, MathVariablePopover))
#define MATH_VARIABLE_POPOVER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_MATH_VARIABLE_POPOVER, MathVariablePopoverClass))
#define IS_MATH_VARIABLE_POPOVER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_MATH_VARIABLE_POPOVER))
#define IS_MATH_VARIABLE_POPOVER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_MATH_VARIABLE_POPOVER))
#define MATH_VARIABLE_POPOVER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_MATH_VARIABLE_POPOVER, MathVariablePopoverClass))

typedef struct _MathVariablePopover MathVariablePopover;
typedef struct _MathVariablePopoverClass MathVariablePopoverClass;
typedef struct _MathVariablePopoverPrivate MathVariablePopoverPrivate;
enum  {
	MATH_VARIABLE_POPOVER_0_PROPERTY,
	MATH_VARIABLE_POPOVER_NUM_PROPERTIES
};
static GParamSpec* math_variable_popover_properties[MATH_VARIABLE_POPOVER_NUM_PROPERTIES];
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))

struct _MathVariable {
	GObject parent_instance;
	MathVariablePrivate * priv;
	gchar* name;
	Number* value;
};

struct _MathVariableClass {
	GObjectClass parent_class;
};

struct _MathPopover {
	GtkPopover parent_instance;
	MathPopoverPrivate * priv;
	MathEquation* equation;
	GListStore* model;
};

struct _MathPopoverClass {
	GtkPopoverClass parent_class;
	gint (*get_item_index) (MathPopover* self, gconstpointer item);
	gboolean (*is_deletable) (MathPopover* self, gconstpointer item);
	gboolean (*is_editable) (MathPopover* self, gconstpointer item);
	gchar* (*get_item_text) (MathPopover* self, gconstpointer item);
};

struct _MathVariablePopover {
	MathPopover parent_instance;
	MathVariablePopoverPrivate * priv;
};

struct _MathVariablePopoverClass {
	MathPopoverClass parent_class;
};

struct _MathVariablePopoverPrivate {
	GtkListBox* variable_list;
	GtkEntry* variable_name_entry;
	GtkButton* store_variable_button;
};

static gpointer math_variable_parent_class = NULL;
static gint MathVariablePopover_private_offset;
static gpointer math_variable_popover_parent_class = NULL;
static gchar** math_variable_popover_RESERVED_VARIABLE_NAMES;
static gint math_variable_popover_RESERVED_VARIABLE_NAMES_length1;
static gchar** math_variable_popover_RESERVED_VARIABLE_NAMES = NULL;
static gint math_variable_popover_RESERVED_VARIABLE_NAMES_length1 = 0;
static gint _math_variable_popover_RESERVED_VARIABLE_NAMES_size_ = 0;

VALA_EXTERN GType math_variable_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (MathVariable, g_object_unref)
VALA_EXTERN MathVariable* math_variable_new (const gchar* name,
                                 Number* value);
VALA_EXTERN MathVariable* math_variable_construct (GType object_type,
                                       const gchar* name,
                                       Number* value);
VALA_EXTERN gint math_variable_name_compare_func (MathVariable* var1,
                                      MathVariable* var2);
VALA_EXTERN gboolean math_variable_name_equal_func (MathVariable* var1,
                                        MathVariable* var2);
static void math_variable_finalize (GObject * obj);
static GType math_variable_get_type_once (void);
VALA_EXTERN GType math_popover_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (MathPopover, g_object_unref)
VALA_EXTERN GType math_variable_popover_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (MathVariablePopover, g_object_unref)
VALA_EXTERN gint math_popover_get_item_index (MathPopover* self,
                                  gconstpointer item);
VALA_EXTERN gboolean math_popover_is_deletable (MathPopover* self,
                                    gconstpointer item);
VALA_EXTERN gboolean math_popover_is_editable (MathPopover* self,
                                   gconstpointer item);
VALA_EXTERN gchar* math_popover_get_item_text (MathPopover* self,
                                   gconstpointer item);
VALA_EXTERN MathVariablePopover* math_variable_popover_new (MathEquation* equation,
                                                GListStore* model,
                                                GCompareDataFunc compare_func,
                                                gpointer compare_func_target);
VALA_EXTERN MathVariablePopover* math_variable_popover_construct (GType object_type,
                                                      MathEquation* equation,
                                                      GListStore* model,
                                                      GCompareDataFunc compare_func,
                                                      gpointer compare_func_target);
static gint __lambda21_ (MathVariablePopover* self,
                  gconstpointer a,
                  gconstpointer b);
static gint ___lambda21__gcompare_data_func (gconstpointer a,
                                      gconstpointer b,
                                      gpointer self);
VALA_EXTERN MathPopover* math_popover_construct (GType object_type,
                                     GType t_type,
                                     GBoxedCopyFunc t_dup_func,
                                     GDestroyNotify t_destroy_func,
                                     MathEquation* equation,
                                     GListStore* model,
                                     GCompareDataFunc compare_func,
                                     gpointer compare_func_target);
static GtkWidget* __lambda22_ (MathVariablePopover* self,
                        GObject* variable);
VALA_EXTERN GtkWidget* math_popover_make_item_row (MathPopover* self,
                                       gconstpointer item);
static GtkWidget* ___lambda22__gtk_list_box_create_widget_func (GObject* item,
                                                         gpointer self);
static void math_variable_popover_handler (MathVariablePopover* self,
                                    const gchar* answer,
                                    Number* number,
                                    gint number_base,
                                    guint representation_base);
static void _math_variable_popover_handler_math_equation_history_signal (MathEquation* _sender,
                                                                  const gchar* answer,
                                                                  Number* number,
                                                                  gint number_base,
                                                                  guint representation_base,
                                                                  gpointer self);
static void math_variable_popover_delete_variable_cb (MathVariablePopover* self,
                                               MathVariable* variable);
static void _math_variable_popover_delete_variable_cb_math_popover_item_deleted (MathPopover* _sender,
                                                                          gconstpointer item,
                                                                          gpointer self);
static gint math_variable_popover_real_get_item_index (MathPopover* base,
                                                MathVariable* item);
static gboolean __lambda23_ (GObject* a,
                      GObject* b);
static gboolean ___lambda23__gequal_func (gconstpointer a,
                                   gconstpointer b);
VALA_EXTERN void math_popover_item_edited_cb (MathPopover* self,
                                  gconstpointer item);
static void math_variable_popover_insert_variable_cb (MathVariablePopover* self,
                                               GtkListBoxRow* row);
static void _math_variable_popover_insert_variable_cb_gtk_list_box_row_activated (GtkListBox* _sender,
                                                                           GtkListBoxRow* row,
                                                                           gpointer self);
static void math_variable_popover_variable_name_changed_cb (MathVariablePopover* self,
                                                     GtkEditable* editable);
static void _math_variable_popover_variable_name_changed_cb_gtk_editable_changed (GtkEditable* _sender,
                                                                           gpointer self);
static void math_variable_popover_store_variable_cb (MathVariablePopover* self,
                                              GtkWidget* widget);
static gboolean _vala_string_array_contains (gchar* * stack,
                                      gssize stack_length,
                                      const gchar* needle);
static void _math_variable_popover_store_variable_cb_gtk_button_clicked (GtkButton* _sender,
                                                                  gpointer self);
static gboolean math_variable_popover_real_is_deletable (MathPopover* base,
                                                  MathVariable* variable);
static gboolean math_variable_popover_real_is_editable (MathPopover* base,
                                                 MathVariable* variable);
static gchar* math_variable_popover_real_get_item_text (MathPopover* base,
                                                 MathVariable* variable);
static void math_variable_popover_finalize (GObject * obj);
static GType math_variable_popover_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

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

MathVariable*
math_variable_construct (GType object_type,
                         const gchar* name,
                         Number* value)
{
	MathVariable * self = NULL;
	gchar* _tmp0_;
	Number* _tmp1_;
	g_return_val_if_fail (name != NULL, NULL);
	self = (MathVariable*) g_object_new (object_type, NULL);
	_tmp0_ = g_strdup (name);
	_g_free0 (self->name);
	self->name = _tmp0_;
	_tmp1_ = _g_object_ref0 (value);
	_g_object_unref0 (self->value);
	self->value = _tmp1_;
	return self;
}

MathVariable*
math_variable_new (const gchar* name,
                   Number* value)
{
	return math_variable_construct (TYPE_MATH_VARIABLE, name, value);
}

gint
math_variable_name_compare_func (MathVariable* var1,
                                 MathVariable* var2)
{
	GCompareFunc _tmp0_;
	const gchar* _tmp1_;
	const gchar* _tmp2_;
	gint result;
	g_return_val_if_fail (var1 != NULL, 0);
	g_return_val_if_fail (var2 != NULL, 0);
	_tmp0_ = ((GCompareFunc) g_strcmp0);
	_tmp1_ = var1->name;
	_tmp2_ = var2->name;
	result = _tmp0_ (_tmp1_, _tmp2_);
	return result;
}

gboolean
math_variable_name_equal_func (MathVariable* var1,
                               MathVariable* var2)
{
	const gchar* _tmp0_;
	const gchar* _tmp1_;
	gboolean result;
	g_return_val_if_fail (var1 != NULL, FALSE);
	g_return_val_if_fail (var2 != NULL, FALSE);
	_tmp0_ = var1->name;
	_tmp1_ = var2->name;
	result = g_strcmp0 (_tmp0_, _tmp1_) == 0;
	return result;
}

static void
math_variable_class_init (MathVariableClass * klass,
                          gpointer klass_data)
{
	math_variable_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->finalize = math_variable_finalize;
}

static void
math_variable_instance_init (MathVariable * self,
                             gpointer klass)
{
}

static void
math_variable_finalize (GObject * obj)
{
	MathVariable * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_MATH_VARIABLE, MathVariable);
	_g_free0 (self->name);
	_g_object_unref0 (self->value);
	G_OBJECT_CLASS (math_variable_parent_class)->finalize (obj);
}

static GType
math_variable_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (MathVariableClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) math_variable_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (MathVariable), 0, (GInstanceInitFunc) math_variable_instance_init, NULL };
	GType math_variable_type_id;
	math_variable_type_id = g_type_register_static (G_TYPE_OBJECT, "MathVariable", &g_define_type_info, 0);
	return math_variable_type_id;
}

GType
math_variable_get_type (void)
{
	static volatile gsize math_variable_type_id__once = 0;
	if (g_once_init_enter (&math_variable_type_id__once)) {
		GType math_variable_type_id;
		math_variable_type_id = math_variable_get_type_once ();
		g_once_init_leave (&math_variable_type_id__once, math_variable_type_id);
	}
	return math_variable_type_id__once;
}

static inline gpointer
math_variable_popover_get_instance_private (MathVariablePopover* self)
{
	return G_STRUCT_MEMBER_P (self, MathVariablePopover_private_offset);
}

static gint
__lambda21_ (MathVariablePopover* self,
             gconstpointer a,
             gconstpointer b)
{
	gint result;
	result = math_variable_name_compare_func (IS_MATH_VARIABLE (a) ? ((MathVariable*) a) : NULL, IS_MATH_VARIABLE (b) ? ((MathVariable*) b) : NULL);
	return result;
}

static gint
___lambda21__gcompare_data_func (gconstpointer a,
                                 gconstpointer b,
                                 gpointer self)
{
	gint result;
	result = __lambda21_ ((MathVariablePopover*) self, a, b);
	return result;
}

static GtkWidget*
__lambda22_ (MathVariablePopover* self,
             GObject* variable)
{
	GtkWidget* _tmp0_;
	GtkWidget* result;
	g_return_val_if_fail (variable != NULL, NULL);
	_tmp0_ = math_popover_make_item_row ((MathPopover*) self, IS_MATH_VARIABLE (variable) ? ((MathVariable*) variable) : NULL);
	result = _tmp0_;
	return result;
}

static GtkWidget*
___lambda22__gtk_list_box_create_widget_func (GObject* item,
                                              gpointer self)
{
	GtkWidget* result;
	result = __lambda22_ ((MathVariablePopover*) self, item);
	return result;
}

static void
_math_variable_popover_handler_math_equation_history_signal (MathEquation* _sender,
                                                             const gchar* answer,
                                                             Number* number,
                                                             gint number_base,
                                                             guint representation_base,
                                                             gpointer self)
{
	math_variable_popover_handler ((MathVariablePopover*) self, answer, number, number_base, representation_base);
}

static void
_math_variable_popover_delete_variable_cb_math_popover_item_deleted (MathPopover* _sender,
                                                                     gconstpointer item,
                                                                     gpointer self)
{
	math_variable_popover_delete_variable_cb ((MathVariablePopover*) self, (MathVariable*) item);
}

MathVariablePopover*
math_variable_popover_construct (GType object_type,
                                 MathEquation* equation,
                                 GListStore* model,
                                 GCompareDataFunc compare_func,
                                 gpointer compare_func_target)
{
	MathVariablePopover * self = NULL;
	GtkListBox* _tmp0_;
	g_return_val_if_fail (equation != NULL, NULL);
	g_return_val_if_fail (model != NULL, NULL);
	self = (MathVariablePopover*) math_popover_construct (object_type, TYPE_MATH_VARIABLE, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, equation, model, ___lambda21__gcompare_data_func, self);
	_tmp0_ = self->priv->variable_list;
	gtk_list_box_bind_model (_tmp0_, (GListModel*) model, ___lambda22__gtk_list_box_create_widget_func, g_object_ref (self), g_object_unref);
	g_signal_connect_object (equation, "history-signal", (GCallback) _math_variable_popover_handler_math_equation_history_signal, self, 0);
	g_signal_connect_object ((MathPopover*) self, "item-deleted", (GCallback) _math_variable_popover_delete_variable_cb_math_popover_item_deleted, self, 0);
	return self;
}

MathVariablePopover*
math_variable_popover_new (MathEquation* equation,
                           GListStore* model,
                           GCompareDataFunc compare_func,
                           gpointer compare_func_target)
{
	return math_variable_popover_construct (TYPE_MATH_VARIABLE_POPOVER, equation, model, compare_func, compare_func_target);
}

static gboolean
__lambda23_ (GObject* a,
             GObject* b)
{
	gboolean result;
	g_return_val_if_fail (a != NULL, FALSE);
	g_return_val_if_fail (b != NULL, FALSE);
	result = math_variable_name_equal_func (IS_MATH_VARIABLE (a) ? ((MathVariable*) a) : NULL, IS_MATH_VARIABLE (b) ? ((MathVariable*) b) : NULL);
	return result;
}

static gboolean
___lambda23__gequal_func (gconstpointer a,
                          gconstpointer b)
{
	gboolean result;
	result = __lambda23_ ((GObject*) a, (GObject*) b);
	return result;
}

static gint
math_variable_popover_real_get_item_index (MathPopover* base,
                                           MathVariable* item)
{
	MathVariablePopover * self;
	guint position = 0U;
	GListStore* _tmp0_;
	guint _tmp1_ = 0U;
	gboolean _tmp2_;
	gint result;
	self = (MathVariablePopover*) base;
	g_return_val_if_fail (item != NULL, 0);
	_tmp0_ = ((MathPopover*) self)->model;
	_tmp2_ = g_list_store_find_with_equal_func (_tmp0_, G_TYPE_CHECK_INSTANCE_TYPE (item, G_TYPE_OBJECT) ? ((GObject*) item) : NULL, ___lambda23__gequal_func, &_tmp1_);
	position = _tmp1_;
	if (_tmp2_) {
		result = (gint) position;
		return result;
	} else {
		result = -1;
		return result;
	}
}

static void
math_variable_popover_handler (MathVariablePopover* self,
                               const gchar* answer,
                               Number* number,
                               gint number_base,
                               guint representation_base)
{
	MathVariable* _tmp0_;
	MathVariable* _tmp1_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (answer != NULL);
	g_return_if_fail (number != NULL);
	_tmp0_ = math_variable_new ("_", number);
	_tmp1_ = _tmp0_;
	math_popover_item_edited_cb ((MathPopover*) self, _tmp1_);
	_g_object_unref0 (_tmp1_);
}

static void
math_variable_popover_insert_variable_cb (MathVariablePopover* self,
                                          GtkListBoxRow* row)
{
	MathVariable* variable = NULL;
	GListStore* _tmp0_;
	GObject* _tmp1_;
	MathVariable* _tmp2_;
	MathEquation* _tmp3_;
	const gchar* _tmp4_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (row != NULL);
	_tmp0_ = ((MathPopover*) self)->model;
	_tmp1_ = g_list_model_get_item ((GListModel*) _tmp0_, (guint) gtk_list_box_row_get_index (row));
	_tmp2_ = IS_MATH_VARIABLE (_tmp1_) ? ((MathVariable*) _tmp1_) : NULL;
	if (_tmp2_ == NULL) {
		_g_object_unref0 (_tmp1_);
	}
	variable = _tmp2_;
	_tmp3_ = ((MathPopover*) self)->equation;
	_tmp4_ = variable->name;
	math_equation_insert (_tmp3_, _tmp4_);
	_g_object_unref0 (variable);
}

static void
_math_variable_popover_insert_variable_cb_gtk_list_box_row_activated (GtkListBox* _sender,
                                                                      GtkListBoxRow* row,
                                                                      gpointer self)
{
	math_variable_popover_insert_variable_cb ((MathVariablePopover*) self, row);
}

static gchar*
string_replace (const gchar* self,
                const gchar* old,
                const gchar* replacement)
{
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	GError* _inner_error0_ = NULL;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (old != NULL, NULL);
	g_return_val_if_fail (replacement != NULL, NULL);
	if ((*((gchar*) self)) == '\0') {
		_tmp1_ = TRUE;
	} else {
		_tmp1_ = (*((gchar*) old)) == '\0';
	}
	if (_tmp1_) {
		_tmp0_ = TRUE;
	} else {
		_tmp0_ = g_strcmp0 (old, replacement) == 0;
	}
	if (_tmp0_) {
		gchar* _tmp2_;
		_tmp2_ = g_strdup (self);
		result = _tmp2_;
		return result;
	}
	{
		GRegex* regex = NULL;
		gchar* _tmp3_;
		gchar* _tmp4_;
		GRegex* _tmp5_;
		GRegex* _tmp6_;
		gchar* _tmp7_ = NULL;
		GRegex* _tmp8_;
		gchar* _tmp9_;
		gchar* _tmp10_;
		_tmp3_ = g_regex_escape_string (old, -1);
		_tmp4_ = _tmp3_;
		_tmp5_ = g_regex_new (_tmp4_, 0, 0, &_inner_error0_);
		_tmp6_ = _tmp5_;
		_g_free0 (_tmp4_);
		regex = _tmp6_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
		_tmp8_ = regex;
		_tmp9_ = g_regex_replace_literal (_tmp8_, self, (gssize) -1, 0, replacement, 0, &_inner_error0_);
		_tmp7_ = _tmp9_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_regex_unref0 (regex);
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
		_tmp10_ = _tmp7_;
		_tmp7_ = NULL;
		result = _tmp10_;
		_g_free0 (_tmp7_);
		_g_regex_unref0 (regex);
		return result;
	}
	goto __finally0;
	__catch0_g_regex_error:
	{
		g_clear_error (&_inner_error0_);
		g_assert_not_reached ();
	}
	__finally0:
	g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
	g_clear_error (&_inner_error0_);
	return NULL;
}

static void
math_variable_popover_variable_name_changed_cb (MathVariablePopover* self,
                                                GtkEditable* editable)
{
	GtkEntry* entry = NULL;
	GtkEntry* _tmp0_;
	const gchar* _tmp1_;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	GtkButton* _tmp5_;
	const gchar* _tmp6_;
	const gchar* _tmp7_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (editable != NULL);
	_tmp0_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_TYPE (editable, gtk_entry_get_type ()) ? ((GtkEntry*) editable) : NULL);
	entry = _tmp0_;
	_tmp1_ = gtk_editable_get_text ((GtkEditable*) entry);
	_tmp2_ = _tmp1_;
	_tmp3_ = string_replace (_tmp2_, " ", "_");
	_tmp4_ = _tmp3_;
	gtk_editable_set_text ((GtkEditable*) entry, _tmp4_);
	_g_free0 (_tmp4_);
	_tmp5_ = self->priv->store_variable_button;
	_tmp6_ = gtk_editable_get_text ((GtkEditable*) entry);
	_tmp7_ = _tmp6_;
	gtk_widget_set_sensitive ((GtkWidget*) _tmp5_, g_strcmp0 (_tmp7_, "") != 0);
	_g_object_unref0 (entry);
}

static void
_math_variable_popover_variable_name_changed_cb_gtk_editable_changed (GtkEditable* _sender,
                                                                      gpointer self)
{
	math_variable_popover_variable_name_changed_cb ((MathVariablePopover*) self, _sender);
}

static gboolean
_vala_string_array_contains (gchar* * stack,
                             gssize stack_length,
                             const gchar* needle)
{
	gssize i;
	for (i = 0; i < stack_length; i++) {
		if (g_strcmp0 (stack[i], needle) == 0) {
			return TRUE;
		}
	}
	return FALSE;
}

static void
math_variable_popover_store_variable_cb (MathVariablePopover* self,
                                         GtkWidget* widget)
{
	gchar* name = NULL;
	GtkEntry* _tmp0_;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	gboolean _tmp3_ = FALSE;
	gboolean _tmp4_ = FALSE;
	const gchar* _tmp5_;
	Number* z = NULL;
	MathEquation* _tmp10_;
	Number* _tmp11_;
	Number* _tmp12_;
	Number* _tmp13_;
	GtkEntry* _tmp30_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (widget != NULL);
	_tmp0_ = self->priv->variable_name_entry;
	_tmp1_ = gtk_editable_get_text ((GtkEditable*) _tmp0_);
	_tmp2_ = g_strdup (_tmp1_);
	name = _tmp2_;
	_tmp5_ = name;
	if (g_strcmp0 (_tmp5_, "") == 0) {
		_tmp4_ = TRUE;
	} else {
		const gchar* _tmp6_;
		gchar** _tmp7_;
		gint _tmp7__length1;
		_tmp6_ = name;
		_tmp7_ = math_variable_popover_RESERVED_VARIABLE_NAMES;
		_tmp7__length1 = math_variable_popover_RESERVED_VARIABLE_NAMES_length1;
		_tmp4_ = _vala_string_array_contains (_tmp7_, _tmp7__length1, _tmp6_);
	}
	if (_tmp4_) {
		_tmp3_ = TRUE;
	} else {
		GHashTable* _tmp8_;
		const gchar* _tmp9_;
		_tmp8_ = parser_CONSTANTS;
		_tmp9_ = name;
		_tmp3_ = g_hash_table_contains (_tmp8_, _tmp9_);
	}
	if (_tmp3_) {
		_g_free0 (name);
		return;
	}
	_tmp10_ = ((MathPopover*) self)->equation;
	_tmp11_ = math_equation_get_number (_tmp10_);
	_tmp12_ = _tmp11_;
	z = _tmp12_;
	_tmp13_ = z;
	if (_tmp13_ != NULL) {
		MathEquation* _tmp14_;
		MathVariables* _tmp15_;
		MathVariables* _tmp16_;
		const gchar* _tmp17_;
		Number* _tmp18_;
		_tmp14_ = ((MathPopover*) self)->equation;
		_tmp15_ = math_equation_get_variables (_tmp14_);
		_tmp16_ = _tmp15_;
		_tmp17_ = name;
		_tmp18_ = z;
		math_variables_set (_tmp16_, _tmp17_, _tmp18_);
	} else {
		MathEquation* _tmp19_;
		gboolean _tmp20_;
		gboolean _tmp21_;
		_tmp19_ = ((MathPopover*) self)->equation;
		_tmp20_ = math_equation_get_is_result (_tmp19_);
		_tmp21_ = _tmp20_;
		if (_tmp21_) {
			MathEquation* _tmp22_;
			MathVariables* _tmp23_;
			MathVariables* _tmp24_;
			const gchar* _tmp25_;
			MathEquation* _tmp26_;
			Number* _tmp27_;
			Number* _tmp28_;
			_tmp22_ = ((MathPopover*) self)->equation;
			_tmp23_ = math_equation_get_variables (_tmp22_);
			_tmp24_ = _tmp23_;
			_tmp25_ = name;
			_tmp26_ = ((MathPopover*) self)->equation;
			_tmp27_ = math_equation_get_answer (_tmp26_);
			_tmp28_ = _tmp27_;
			math_variables_set (_tmp24_, _tmp25_, _tmp28_);
		} else {
			const gchar* _tmp29_;
			_tmp29_ = name;
			g_warning ("math-variable-popover.vala:96: Can't add variable %s, the display is n" \
"ot a number", _tmp29_);
		}
	}
	_tmp30_ = self->priv->variable_name_entry;
	gtk_editable_set_text ((GtkEditable*) _tmp30_, "");
	_g_object_unref0 (z);
	_g_free0 (name);
}

static void
_math_variable_popover_store_variable_cb_gtk_button_clicked (GtkButton* _sender,
                                                             gpointer self)
{
	math_variable_popover_store_variable_cb ((MathVariablePopover*) self, _sender);
}

static void
math_variable_popover_delete_variable_cb (MathVariablePopover* self,
                                          MathVariable* variable)
{
	MathEquation* _tmp0_;
	MathVariables* _tmp1_;
	MathVariables* _tmp2_;
	const gchar* _tmp3_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (variable != NULL);
	_tmp0_ = ((MathPopover*) self)->equation;
	_tmp1_ = math_equation_get_variables (_tmp0_);
	_tmp2_ = _tmp1_;
	_tmp3_ = variable->name;
	math_variables_delete (_tmp2_, _tmp3_);
}

static gboolean
math_variable_popover_real_is_deletable (MathPopover* base,
                                         MathVariable* variable)
{
	MathVariablePopover * self;
	const gchar* _tmp0_;
	gchar** _tmp1_;
	gint _tmp1__length1;
	gboolean result;
	self = (MathVariablePopover*) base;
	g_return_val_if_fail (variable != NULL, FALSE);
	_tmp0_ = variable->name;
	_tmp1_ = math_variable_popover_RESERVED_VARIABLE_NAMES;
	_tmp1__length1 = math_variable_popover_RESERVED_VARIABLE_NAMES_length1;
	result = !_vala_string_array_contains (_tmp1_, _tmp1__length1, _tmp0_);
	return result;
}

static gboolean
math_variable_popover_real_is_editable (MathPopover* base,
                                        MathVariable* variable)
{
	MathVariablePopover * self;
	gboolean result;
	self = (MathVariablePopover*) base;
	g_return_val_if_fail (variable != NULL, FALSE);
	result = FALSE;
	return result;
}

static gchar*
math_variable_popover_real_get_item_text (MathPopover* base,
                                          MathVariable* variable)
{
	MathVariablePopover * self;
	gchar* text = NULL;
	Number* _tmp0_;
	gchar* result;
	self = (MathVariablePopover*) base;
	g_return_val_if_fail (variable != NULL, NULL);
	_tmp0_ = variable->value;
	if (_tmp0_ != NULL) {
		gchar* value_text = NULL;
		MathEquation* _tmp1_;
		Serializer* _tmp2_;
		Serializer* _tmp3_;
		Number* _tmp4_;
		gchar* _tmp5_;
		const gchar* _tmp6_;
		const gchar* _tmp7_;
		gchar* _tmp8_;
		_tmp1_ = ((MathPopover*) self)->equation;
		_tmp2_ = math_equation_get_serializer (_tmp1_);
		_tmp3_ = _tmp2_;
		_tmp4_ = variable->value;
		_tmp5_ = serializer_to_string (_tmp3_, _tmp4_);
		value_text = _tmp5_;
		_tmp6_ = variable->name;
		_tmp7_ = value_text;
		_tmp8_ = g_strdup_printf ("<b>%s</b> = %s", _tmp6_, _tmp7_);
		_g_free0 (text);
		text = _tmp8_;
		_g_free0 (value_text);
	} else {
		const gchar* _tmp9_;
		gchar* _tmp10_;
		_tmp9_ = variable->name;
		_tmp10_ = g_strdup_printf ("<b>%s</b>", _tmp9_);
		_g_free0 (text);
		text = _tmp10_;
	}
	result = text;
	return result;
}

static void
math_variable_popover_class_init (MathVariablePopoverClass * klass,
                                  gpointer klass_data)
{
	gchar* _tmp0_;
	gchar* _tmp1_;
	gchar** _tmp2_;
	math_variable_popover_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &MathVariablePopover_private_offset);
	((MathPopoverClass *) klass)->get_item_index = (gint (*) (MathPopover*, gconstpointer)) math_variable_popover_real_get_item_index;
	((MathPopoverClass *) klass)->is_deletable = (gboolean (*) (MathPopover*, gconstpointer)) math_variable_popover_real_is_deletable;
	((MathPopoverClass *) klass)->is_editable = (gboolean (*) (MathPopover*, gconstpointer)) math_variable_popover_real_is_editable;
	((MathPopoverClass *) klass)->get_item_text = (gchar* (*) (MathPopover*, gconstpointer)) math_variable_popover_real_get_item_text;
	G_OBJECT_CLASS (klass)->finalize = math_variable_popover_finalize;
	gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), "/org/gnome/calculator/math-variable-popover.ui");
	_tmp0_ = g_strdup ("_");
	_tmp1_ = g_strdup ("rand");
	_tmp2_ = g_new0 (gchar*, 2 + 1);
	_tmp2_[0] = _tmp0_;
	_tmp2_[1] = _tmp1_;
	math_variable_popover_RESERVED_VARIABLE_NAMES = _tmp2_;
	math_variable_popover_RESERVED_VARIABLE_NAMES_length1 = 2;
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "variable_list", FALSE, MathVariablePopover_private_offset + G_STRUCT_OFFSET (MathVariablePopoverPrivate, variable_list));
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "variable_name_entry", FALSE, MathVariablePopover_private_offset + G_STRUCT_OFFSET (MathVariablePopoverPrivate, variable_name_entry));
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "store_variable_button", FALSE, MathVariablePopover_private_offset + G_STRUCT_OFFSET (MathVariablePopoverPrivate, store_variable_button));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "insert_variable_cb", G_CALLBACK(_math_variable_popover_insert_variable_cb_gtk_list_box_row_activated));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "variable_name_changed_cb", G_CALLBACK(_math_variable_popover_variable_name_changed_cb_gtk_editable_changed));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "store_variable_cb", G_CALLBACK(_math_variable_popover_store_variable_cb_gtk_button_clicked));
}

static void
math_variable_popover_instance_init (MathVariablePopover * self,
                                     gpointer klass)
{
	self->priv = math_variable_popover_get_instance_private (self);
	gtk_widget_init_template (GTK_WIDGET (self));
}

static void
math_variable_popover_finalize (GObject * obj)
{
	MathVariablePopover * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_MATH_VARIABLE_POPOVER, MathVariablePopover);
	G_OBJECT_CLASS (math_variable_popover_parent_class)->finalize (obj);
}

static GType
math_variable_popover_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (MathVariablePopoverClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) math_variable_popover_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (MathVariablePopover), 0, (GInstanceInitFunc) math_variable_popover_instance_init, NULL };
	GType math_variable_popover_type_id;
	math_variable_popover_type_id = g_type_register_static (TYPE_MATH_POPOVER, "MathVariablePopover", &g_define_type_info, 0);
	MathVariablePopover_private_offset = g_type_add_instance_private (math_variable_popover_type_id, sizeof (MathVariablePopoverPrivate));
	return math_variable_popover_type_id;
}

GType
math_variable_popover_get_type (void)
{
	static volatile gsize math_variable_popover_type_id__once = 0;
	if (g_once_init_enter (&math_variable_popover_type_id__once)) {
		GType math_variable_popover_type_id;
		math_variable_popover_type_id = math_variable_popover_get_type_once ();
		g_once_init_leave (&math_variable_popover_type_id__once, math_variable_popover_type_id);
	}
	return math_variable_popover_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;
}

