/* math-variable-popover.c generated by valac 0.36.5, 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.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include "libcalculator.h"
#include <stdlib.h>
#include <string.h>
#include <gdk/gdk.h>


#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;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
typedef struct _Block2Data Block2Data;

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

struct _MathVariablePopoverClass {
	GtkPopoverClass parent_class;
};

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

struct _Block2Data {
	int _ref_count_;
	MathVariablePopover* self;
	GtkListBoxRow* row;
	gchar* name;
};


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;

GType math_variable_popover_get_type (void) G_GNUC_CONST;
#define MATH_VARIABLE_POPOVER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_MATH_VARIABLE_POPOVER, MathVariablePopoverPrivate))
enum  {
	MATH_VARIABLE_POPOVER_DUMMY_PROPERTY
};
MathVariablePopover* math_variable_popover_new (MathEquation* equation);
MathVariablePopover* math_variable_popover_construct (GType object_type, MathEquation* equation);
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 GtkListBoxRow* math_variable_popover_make_variable_row (MathVariablePopover* self, const gchar* name, Number* value);
static gint math_variable_popover_variable_list_sort (MathVariablePopover* self, GtkListBoxRow* row1, GtkListBoxRow* row2);
static gint _math_variable_popover_variable_list_sort_gtk_list_box_sort_func (GtkListBoxRow* row1, GtkListBoxRow* row2, gpointer self);
static void __lambda13_ (MathVariablePopover* self, const gchar* name, Number* value);
static void ___lambda13__math_variables_variable_added (MathVariables* _sender, const gchar* name, Number* value, gpointer self);
static void __lambda14_ (MathVariablePopover* self, const gchar* name, Number* value);
static GtkListBoxRow* math_variable_popover_find_row_for_variable (MathVariablePopover* self, const gchar* name);
static void ___lambda14__math_variables_variable_edited (MathVariables* _sender, const gchar* name, Number* new_value, gpointer self);
static void __lambda15_ (MathVariablePopover* self, const gchar* name);
static void ___lambda15__math_variables_variable_deleted (MathVariables* _sender, const gchar* name, gpointer self);
static Block2Data* block2_data_ref (Block2Data* _data2_);
static void block2_data_unref (void * _userdata_);
static void __lambda12_ (Block2Data* _data2_, GtkWidget* child);
static void ___lambda12__gtk_callback (GtkWidget* widget, gpointer self);
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 gboolean math_variable_popover_variable_name_key_press_cb (MathVariablePopover* self, GtkWidget* widget, GdkEventKey* event);
static gboolean _math_variable_popover_variable_name_key_press_cb_gtk_widget_key_press_event (GtkWidget* _sender, GdkEventKey* event, gpointer self);
static void math_variable_popover_variable_name_changed_cb (MathVariablePopover* self);
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, int stack_length, gchar* needle);
static void _math_variable_popover_store_variable_cb_gtk_button_clicked (GtkButton* _sender, gpointer self);
static void math_variable_popover_delete_variable_cb (MathVariablePopover* self, GtkWidget* widget);
static void _math_variable_popover_delete_variable_cb_gtk_button_clicked (GtkButton* _sender, gpointer self);
static void math_variable_popover_finalize (GObject * obj);
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func);


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


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 gint _math_variable_popover_variable_list_sort_gtk_list_box_sort_func (GtkListBoxRow* row1, GtkListBoxRow* row2, gpointer self) {
	gint result;
	result = math_variable_popover_variable_list_sort ((MathVariablePopover*) self, row1, row2);
	return result;
}


static void __lambda13_ (MathVariablePopover* self, const gchar* name, Number* value) {
	GtkListBox* _tmp0_;
	const gchar* _tmp1_;
	Number* _tmp2_;
	GtkListBoxRow* _tmp3_;
	GtkListBoxRow* _tmp4_;
	g_return_if_fail (name != NULL);
	g_return_if_fail (value != NULL);
	_tmp0_ = self->priv->variable_list;
	_tmp1_ = name;
	_tmp2_ = value;
	_tmp3_ = math_variable_popover_make_variable_row (self, _tmp1_, _tmp2_);
	_tmp4_ = _tmp3_;
	gtk_container_add ((GtkContainer*) _tmp0_, (GtkWidget*) _tmp4_);
	_g_object_unref0 (_tmp4_);
}


static void ___lambda13__math_variables_variable_added (MathVariables* _sender, const gchar* name, Number* value, gpointer self) {
	__lambda13_ ((MathVariablePopover*) self, name, value);
}


static void __lambda14_ (MathVariablePopover* self, const gchar* name, Number* value) {
	GtkListBox* _tmp0_;
	const gchar* _tmp1_;
	GtkListBoxRow* _tmp2_;
	GtkListBoxRow* _tmp3_;
	GtkListBox* _tmp4_;
	const gchar* _tmp5_;
	Number* _tmp6_;
	GtkListBoxRow* _tmp7_;
	GtkListBoxRow* _tmp8_;
	g_return_if_fail (name != NULL);
	g_return_if_fail (value != NULL);
	_tmp0_ = self->priv->variable_list;
	_tmp1_ = name;
	_tmp2_ = math_variable_popover_find_row_for_variable (self, _tmp1_);
	_tmp3_ = _tmp2_;
	gtk_container_remove ((GtkContainer*) _tmp0_, (GtkWidget*) _tmp3_);
	_g_object_unref0 (_tmp3_);
	_tmp4_ = self->priv->variable_list;
	_tmp5_ = name;
	_tmp6_ = value;
	_tmp7_ = math_variable_popover_make_variable_row (self, _tmp5_, _tmp6_);
	_tmp8_ = _tmp7_;
	gtk_container_add ((GtkContainer*) _tmp4_, (GtkWidget*) _tmp8_);
	_g_object_unref0 (_tmp8_);
}


static void ___lambda14__math_variables_variable_edited (MathVariables* _sender, const gchar* name, Number* new_value, gpointer self) {
	__lambda14_ ((MathVariablePopover*) self, name, new_value);
}


static void __lambda15_ (MathVariablePopover* self, const gchar* name) {
	GtkListBox* _tmp0_;
	const gchar* _tmp1_;
	GtkListBoxRow* _tmp2_;
	GtkListBoxRow* _tmp3_;
	g_return_if_fail (name != NULL);
	_tmp0_ = self->priv->variable_list;
	_tmp1_ = name;
	_tmp2_ = math_variable_popover_find_row_for_variable (self, _tmp1_);
	_tmp3_ = _tmp2_;
	gtk_container_remove ((GtkContainer*) _tmp0_, (GtkWidget*) _tmp3_);
	_g_object_unref0 (_tmp3_);
}


static void ___lambda15__math_variables_variable_deleted (MathVariables* _sender, const gchar* name, gpointer self) {
	__lambda15_ ((MathVariablePopover*) self, name);
}


MathVariablePopover* math_variable_popover_construct (GType object_type, MathEquation* equation) {
	MathVariablePopover * self = NULL;
	MathEquation* _tmp0_;
	MathEquation* _tmp1_;
	MathEquation* _tmp2_;
	gchar** names = NULL;
	MathEquation* _tmp3_;
	MathVariables* _tmp4_;
	MathVariables* _tmp5_;
	gint _tmp6_;
	gchar** _tmp7_;
	gint names_length1;
	gint _names_size_;
	GtkListBox* _tmp27_;
	GtkListBoxRow* _tmp28_;
	GtkListBoxRow* _tmp29_;
	GtkListBox* _tmp30_;
	MathEquation* _tmp31_;
	MathVariables* _tmp32_;
	MathVariables* _tmp33_;
	MathEquation* _tmp34_;
	MathVariables* _tmp35_;
	MathVariables* _tmp36_;
	MathEquation* _tmp37_;
	MathVariables* _tmp38_;
	MathVariables* _tmp39_;
	g_return_val_if_fail (equation != NULL, NULL);
	self = (MathVariablePopover*) g_object_new (object_type, NULL);
	_tmp0_ = equation;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	_g_object_unref0 (self->priv->equation);
	self->priv->equation = _tmp1_;
	_tmp2_ = equation;
	g_signal_connect_object (_tmp2_, "history-signal", (GCallback) _math_variable_popover_handler_math_equation_history_signal, self, 0);
	_tmp3_ = equation;
	_tmp4_ = math_equation_get_variables (_tmp3_);
	_tmp5_ = _tmp4_;
	_tmp7_ = math_variables_get_names (_tmp5_, &_tmp6_);
	names = _tmp7_;
	names_length1 = _tmp6_;
	_names_size_ = names_length1;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp8_ = FALSE;
			_tmp8_ = TRUE;
			while (TRUE) {
				gchar** _tmp10_;
				gint _tmp10__length1;
				gint _tmp11_;
				const gchar* _tmp12_;
				Number* value = NULL;
				MathEquation* _tmp13_;
				MathVariables* _tmp14_;
				MathVariables* _tmp15_;
				gchar** _tmp16_;
				gint _tmp16__length1;
				gint _tmp17_;
				const gchar* _tmp18_;
				Number* _tmp19_;
				GtkListBox* _tmp20_;
				gchar** _tmp21_;
				gint _tmp21__length1;
				gint _tmp22_;
				const gchar* _tmp23_;
				Number* _tmp24_;
				GtkListBoxRow* _tmp25_;
				GtkListBoxRow* _tmp26_;
				if (!_tmp8_) {
					gint _tmp9_;
					_tmp9_ = i;
					i = _tmp9_ + 1;
				}
				_tmp8_ = FALSE;
				_tmp10_ = names;
				_tmp10__length1 = names_length1;
				_tmp11_ = i;
				_tmp12_ = _tmp10_[_tmp11_];
				if (!(_tmp12_ != NULL)) {
					break;
				}
				_tmp13_ = equation;
				_tmp14_ = math_equation_get_variables (_tmp13_);
				_tmp15_ = _tmp14_;
				_tmp16_ = names;
				_tmp16__length1 = names_length1;
				_tmp17_ = i;
				_tmp18_ = _tmp16_[_tmp17_];
				_tmp19_ = math_variables_get (_tmp15_, _tmp18_);
				value = _tmp19_;
				_tmp20_ = self->priv->variable_list;
				_tmp21_ = names;
				_tmp21__length1 = names_length1;
				_tmp22_ = i;
				_tmp23_ = _tmp21_[_tmp22_];
				_tmp24_ = value;
				_tmp25_ = math_variable_popover_make_variable_row (self, _tmp23_, _tmp24_);
				_tmp26_ = _tmp25_;
				gtk_container_add ((GtkContainer*) _tmp20_, (GtkWidget*) _tmp26_);
				_g_object_unref0 (_tmp26_);
				_g_object_unref0 (value);
			}
		}
	}
	_tmp27_ = self->priv->variable_list;
	_tmp28_ = math_variable_popover_make_variable_row (self, "rand", NULL);
	_tmp29_ = _tmp28_;
	gtk_container_add ((GtkContainer*) _tmp27_, (GtkWidget*) _tmp29_);
	_g_object_unref0 (_tmp29_);
	_tmp30_ = self->priv->variable_list;
	gtk_list_box_set_sort_func (_tmp30_, _math_variable_popover_variable_list_sort_gtk_list_box_sort_func, g_object_ref (self), g_object_unref);
	_tmp31_ = equation;
	_tmp32_ = math_equation_get_variables (_tmp31_);
	_tmp33_ = _tmp32_;
	g_signal_connect_object (_tmp33_, "variable-added", (GCallback) ___lambda13__math_variables_variable_added, self, 0);
	_tmp34_ = equation;
	_tmp35_ = math_equation_get_variables (_tmp34_);
	_tmp36_ = _tmp35_;
	g_signal_connect_object (_tmp36_, "variable-edited", (GCallback) ___lambda14__math_variables_variable_edited, self, 0);
	_tmp37_ = equation;
	_tmp38_ = math_equation_get_variables (_tmp37_);
	_tmp39_ = _tmp38_;
	g_signal_connect_object (_tmp39_, "variable-deleted", (GCallback) ___lambda15__math_variables_variable_deleted, self, 0);
	names = (_vala_array_free (names, names_length1, (GDestroyNotify) g_free), NULL);
	return self;
}


MathVariablePopover* math_variable_popover_new (MathEquation* equation) {
	return math_variable_popover_construct (TYPE_MATH_VARIABLE_POPOVER, equation);
}


static void math_variable_popover_handler (MathVariablePopover* self, const gchar* answer, Number* number, gint number_base, guint representation_base) {
	GtkListBox* _tmp0_;
	GtkListBoxRow* _tmp1_;
	GtkListBoxRow* _tmp2_;
	GtkListBox* _tmp3_;
	Number* _tmp4_;
	GtkListBoxRow* _tmp5_;
	GtkListBoxRow* _tmp6_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (answer != NULL);
	g_return_if_fail (number != NULL);
	_tmp0_ = self->priv->variable_list;
	_tmp1_ = math_variable_popover_find_row_for_variable (self, "ans");
	_tmp2_ = _tmp1_;
	gtk_container_remove ((GtkContainer*) _tmp0_, (GtkWidget*) _tmp2_);
	_g_object_unref0 (_tmp2_);
	_tmp3_ = self->priv->variable_list;
	_tmp4_ = number;
	_tmp5_ = math_variable_popover_make_variable_row (self, "ans", _tmp4_);
	_tmp6_ = _tmp5_;
	gtk_container_add ((GtkContainer*) _tmp3_, (GtkWidget*) _tmp6_);
	_g_object_unref0 (_tmp6_);
}


static Block2Data* block2_data_ref (Block2Data* _data2_) {
	g_atomic_int_inc (&_data2_->_ref_count_);
	return _data2_;
}


static void block2_data_unref (void * _userdata_) {
	Block2Data* _data2_;
	_data2_ = (Block2Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data2_->_ref_count_)) {
		MathVariablePopover* self;
		self = _data2_->self;
		_g_free0 (_data2_->name);
		_g_object_unref0 (self);
		g_slice_free (Block2Data, _data2_);
	}
}


static void __lambda12_ (Block2Data* _data2_, GtkWidget* child) {
	MathVariablePopover* self;
	const gchar* _tmp0_;
	GtkWidget* _tmp1_;
	gconstpointer _tmp2_;
	self = _data2_->self;
	g_return_if_fail (child != NULL);
	_tmp0_ = _data2_->name;
	_tmp1_ = child;
	_tmp2_ = g_object_get_data ((GObject*) _tmp1_, "variable_name");
	if (g_strcmp0 (_tmp0_, (const gchar*) _tmp2_) == 0) {
		GtkWidget* _tmp3_;
		_tmp3_ = child;
		_data2_->row = G_TYPE_CHECK_INSTANCE_TYPE (_tmp3_, gtk_list_box_row_get_type ()) ? ((GtkListBoxRow*) _tmp3_) : NULL;
	}
}


static void ___lambda12__gtk_callback (GtkWidget* widget, gpointer self) {
	__lambda12_ (self, widget);
}


static GtkListBoxRow* math_variable_popover_find_row_for_variable (MathVariablePopover* self, const gchar* name) {
	GtkListBoxRow* result = NULL;
	Block2Data* _data2_;
	const gchar* _tmp0_;
	gchar* _tmp1_;
	GtkListBox* _tmp2_;
	GtkListBoxRow* _tmp3_;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (name != NULL, NULL);
	_data2_ = g_slice_new0 (Block2Data);
	_data2_->_ref_count_ = 1;
	_data2_->self = g_object_ref (self);
	_tmp0_ = name;
	_tmp1_ = g_strdup (_tmp0_);
	_g_free0 (_data2_->name);
	_data2_->name = _tmp1_;
	_data2_->row = NULL;
	_tmp2_ = self->priv->variable_list;
	gtk_container_foreach ((GtkContainer*) _tmp2_, ___lambda12__gtk_callback, _data2_);
	_tmp3_ = _g_object_ref0 (_data2_->row);
	result = _tmp3_;
	block2_data_unref (_data2_);
	_data2_ = NULL;
	return result;
}


static void math_variable_popover_insert_variable_cb (MathVariablePopover* self, GtkListBoxRow* row) {
	gchar* name = NULL;
	GtkListBoxRow* _tmp0_;
	gconstpointer _tmp1_;
	gchar* _tmp2_;
	MathEquation* _tmp3_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (row != NULL);
	_tmp0_ = row;
	_tmp1_ = g_object_get_data ((GObject*) _tmp0_, "variable_name");
	_tmp2_ = g_strdup ((const gchar*) _tmp1_);
	name = _tmp2_;
	_tmp3_ = self->priv->equation;
	math_equation_insert (_tmp3_, name);
	_g_free0 (name);
}


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 gboolean math_variable_popover_variable_name_key_press_cb (MathVariablePopover* self, GtkWidget* widget, GdkEventKey* event) {
	gboolean result = FALSE;
	gboolean _tmp0_ = FALSE;
	GdkEventKey* _tmp1_;
	guint _tmp2_;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (widget != NULL, FALSE);
	g_return_val_if_fail (event != NULL, FALSE);
	_tmp1_ = event;
	_tmp2_ = _tmp1_->keyval;
	if (_tmp2_ == ((guint) GDK_KEY_space)) {
		_tmp0_ = TRUE;
	} else {
		GdkEventKey* _tmp3_;
		guint _tmp4_;
		_tmp3_ = event;
		_tmp4_ = _tmp3_->keyval;
		_tmp0_ = _tmp4_ == ((guint) GDK_KEY_KP_Space);
	}
	if (_tmp0_) {
		GdkEventKey* _tmp5_;
		_tmp5_ = event;
		_tmp5_->keyval = (guint) GDK_KEY_underscore;
	}
	result = FALSE;
	return result;
}


static gboolean _math_variable_popover_variable_name_key_press_cb_gtk_widget_key_press_event (GtkWidget* _sender, GdkEventKey* event, gpointer self) {
	gboolean result;
	result = math_variable_popover_variable_name_key_press_cb ((MathVariablePopover*) self, _sender, event);
	return result;
}


static void math_variable_popover_variable_name_changed_cb (MathVariablePopover* self) {
	GtkButton* _tmp0_;
	GtkEntry* _tmp1_;
	const gchar* _tmp2_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->store_variable_button;
	_tmp1_ = self->priv->variable_name_entry;
	_tmp2_ = gtk_entry_get_text (_tmp1_);
	gtk_widget_set_sensitive ((GtkWidget*) _tmp0_, g_strcmp0 (_tmp2_, "") != 0);
}


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);
}


static gboolean _vala_string_array_contains (gchar* * stack, int stack_length, gchar* needle) {
	int 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;
	const gchar* _tmp4_;
	Number* z = NULL;
	MathEquation* _tmp7_;
	Number* _tmp8_;
	Number* _tmp9_;
	Number* _tmp10_;
	GtkEntry* _tmp27_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (widget != NULL);
	_tmp0_ = self->priv->variable_name_entry;
	_tmp1_ = gtk_entry_get_text (_tmp0_);
	_tmp2_ = g_strdup (_tmp1_);
	name = _tmp2_;
	_tmp4_ = name;
	if (g_strcmp0 (_tmp4_, "") == 0) {
		_tmp3_ = TRUE;
	} else {
		const gchar* _tmp5_;
		gchar** _tmp6_;
		gint _tmp6__length1;
		_tmp5_ = name;
		_tmp6_ = math_variable_popover_RESERVED_VARIABLE_NAMES;
		_tmp6__length1 = math_variable_popover_RESERVED_VARIABLE_NAMES_length1;
		_tmp3_ = _vala_string_array_contains (_tmp6_, _tmp6__length1, _tmp5_);
	}
	if (_tmp3_) {
		_g_free0 (name);
		return;
	}
	_tmp7_ = self->priv->equation;
	_tmp8_ = math_equation_get_number (_tmp7_);
	_tmp9_ = _tmp8_;
	z = _tmp9_;
	_tmp10_ = z;
	if (_tmp10_ != NULL) {
		MathEquation* _tmp11_;
		MathVariables* _tmp12_;
		MathVariables* _tmp13_;
		const gchar* _tmp14_;
		Number* _tmp15_;
		_tmp11_ = self->priv->equation;
		_tmp12_ = math_equation_get_variables (_tmp11_);
		_tmp13_ = _tmp12_;
		_tmp14_ = name;
		_tmp15_ = z;
		math_variables_set (_tmp13_, _tmp14_, _tmp15_);
	} else {
		MathEquation* _tmp16_;
		gboolean _tmp17_;
		gboolean _tmp18_;
		_tmp16_ = self->priv->equation;
		_tmp17_ = math_equation_get_is_result (_tmp16_);
		_tmp18_ = _tmp17_;
		if (_tmp18_) {
			MathEquation* _tmp19_;
			MathVariables* _tmp20_;
			MathVariables* _tmp21_;
			const gchar* _tmp22_;
			MathEquation* _tmp23_;
			Number* _tmp24_;
			Number* _tmp25_;
			_tmp19_ = self->priv->equation;
			_tmp20_ = math_equation_get_variables (_tmp19_);
			_tmp21_ = _tmp20_;
			_tmp22_ = name;
			_tmp23_ = self->priv->equation;
			_tmp24_ = math_equation_get_answer (_tmp23_);
			_tmp25_ = _tmp24_;
			math_variables_set (_tmp21_, _tmp22_, _tmp25_);
		} else {
			const gchar* _tmp26_;
			_tmp26_ = name;
			g_warning ("math-variable-popover.vala:108: Can't add variable %s, the display is " \
"not a number", _tmp26_);
		}
	}
	_tmp27_ = self->priv->variable_name_entry;
	gtk_entry_set_text (_tmp27_, "");
	_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, GtkWidget* widget) {
	gchar* name = NULL;
	GtkWidget* _tmp0_;
	gconstpointer _tmp1_;
	gchar* _tmp2_;
	MathEquation* _tmp3_;
	MathVariables* _tmp4_;
	MathVariables* _tmp5_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (widget != NULL);
	_tmp0_ = widget;
	_tmp1_ = g_object_get_data ((GObject*) _tmp0_, "variable_name");
	_tmp2_ = g_strdup ((const gchar*) _tmp1_);
	name = _tmp2_;
	_tmp3_ = self->priv->equation;
	_tmp4_ = math_equation_get_variables (_tmp3_);
	_tmp5_ = _tmp4_;
	math_variables_delete (_tmp5_, name);
	_g_free0 (name);
}


static void _math_variable_popover_delete_variable_cb_gtk_button_clicked (GtkButton* _sender, gpointer self) {
	math_variable_popover_delete_variable_cb ((MathVariablePopover*) self, _sender);
}


static GtkListBoxRow* math_variable_popover_make_variable_row (MathVariablePopover* self, const gchar* name, Number* value) {
	GtkListBoxRow* result = NULL;
	GtkListBoxRow* row = NULL;
	GtkListBoxRow* _tmp0_;
	GtkListBoxRow* _tmp1_;
	GtkStyleContext* _tmp2_;
	GtkListBoxRow* _tmp3_;
	const gchar* _tmp4_;
	gchar* _tmp5_;
	GtkBox* hbox = NULL;
	GtkBox* _tmp6_;
	gchar* text = NULL;
	Number* _tmp7_;
	GtkLabel* label = NULL;
	const gchar* _tmp18_;
	GtkLabel* _tmp19_;
	GtkLabel* _tmp20_;
	GtkLabel* _tmp21_;
	GtkBox* _tmp22_;
	GtkLabel* _tmp23_;
	const gchar* _tmp24_;
	gchar** _tmp25_;
	gint _tmp25__length1;
	GtkListBoxRow* _tmp35_;
	GtkBox* _tmp36_;
	GtkListBoxRow* _tmp37_;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (name != NULL, NULL);
	_tmp0_ = (GtkListBoxRow*) gtk_list_box_row_new ();
	g_object_ref_sink (_tmp0_);
	row = _tmp0_;
	_tmp1_ = row;
	_tmp2_ = gtk_widget_get_style_context ((GtkWidget*) _tmp1_);
	gtk_style_context_add_class (_tmp2_, "popover-row");
	_tmp3_ = row;
	_tmp4_ = name;
	_tmp5_ = g_strdup (_tmp4_);
	g_object_set_data_full ((GObject*) _tmp3_, "variable_name", _tmp5_, g_free);
	_tmp6_ = (GtkBox*) gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
	g_object_ref_sink (_tmp6_);
	hbox = _tmp6_;
	_tmp7_ = value;
	if (_tmp7_ != NULL) {
		gchar* value_text = NULL;
		MathEquation* _tmp8_;
		Serializer* _tmp9_;
		Serializer* _tmp10_;
		Number* _tmp11_;
		gchar* _tmp12_;
		const gchar* _tmp13_;
		const gchar* _tmp14_;
		gchar* _tmp15_;
		_tmp8_ = self->priv->equation;
		_tmp9_ = math_equation_get_serializer (_tmp8_);
		_tmp10_ = _tmp9_;
		_tmp11_ = value;
		_tmp12_ = serializer_to_string (_tmp10_, _tmp11_);
		value_text = _tmp12_;
		_tmp13_ = name;
		_tmp14_ = value_text;
		_tmp15_ = g_strdup_printf ("<b>%s</b> = %s", _tmp13_, _tmp14_);
		_g_free0 (text);
		text = _tmp15_;
		_g_free0 (value_text);
	} else {
		const gchar* _tmp16_;
		gchar* _tmp17_;
		_tmp16_ = name;
		_tmp17_ = g_strdup_printf ("<b>%s</b>", _tmp16_);
		_g_free0 (text);
		text = _tmp17_;
	}
	_tmp18_ = text;
	_tmp19_ = (GtkLabel*) gtk_label_new (_tmp18_);
	g_object_ref_sink (_tmp19_);
	label = _tmp19_;
	_tmp20_ = label;
	gtk_label_set_use_markup (_tmp20_, TRUE);
	_tmp21_ = label;
	gtk_widget_set_halign ((GtkWidget*) _tmp21_, GTK_ALIGN_START);
	_tmp22_ = hbox;
	_tmp23_ = label;
	gtk_box_pack_start (_tmp22_, (GtkWidget*) _tmp23_, TRUE, TRUE, (guint) 0);
	_tmp24_ = name;
	_tmp25_ = math_variable_popover_RESERVED_VARIABLE_NAMES;
	_tmp25__length1 = math_variable_popover_RESERVED_VARIABLE_NAMES_length1;
	if (!_vala_string_array_contains (_tmp25_, _tmp25__length1, _tmp24_)) {
		GtkButton* button = NULL;
		GtkButton* _tmp26_;
		GtkButton* _tmp27_;
		GtkStyleContext* _tmp28_;
		GtkButton* _tmp29_;
		const gchar* _tmp30_;
		gchar* _tmp31_;
		GtkButton* _tmp32_;
		GtkBox* _tmp33_;
		GtkButton* _tmp34_;
		_tmp26_ = (GtkButton*) gtk_button_new_from_icon_name ("list-remove-symbolic", GTK_ICON_SIZE_BUTTON);
		g_object_ref_sink (_tmp26_);
		button = _tmp26_;
		_tmp27_ = button;
		_tmp28_ = gtk_widget_get_style_context ((GtkWidget*) _tmp27_);
		gtk_style_context_add_class (_tmp28_, "flat");
		_tmp29_ = button;
		_tmp30_ = name;
		_tmp31_ = g_strdup (_tmp30_);
		g_object_set_data_full ((GObject*) _tmp29_, "variable_name", _tmp31_, g_free);
		_tmp32_ = button;
		g_signal_connect_object (_tmp32_, "clicked", (GCallback) _math_variable_popover_delete_variable_cb_gtk_button_clicked, self, 0);
		_tmp33_ = hbox;
		_tmp34_ = button;
		gtk_box_pack_start (_tmp33_, (GtkWidget*) _tmp34_, FALSE, TRUE, (guint) 0);
		_g_object_unref0 (button);
	}
	_tmp35_ = row;
	_tmp36_ = hbox;
	gtk_container_add ((GtkContainer*) _tmp35_, (GtkWidget*) _tmp36_);
	_tmp37_ = row;
	gtk_widget_show_all ((GtkWidget*) _tmp37_);
	result = row;
	_g_object_unref0 (label);
	_g_free0 (text);
	_g_object_unref0 (hbox);
	return result;
}


static gint math_variable_popover_variable_list_sort (MathVariablePopover* self, GtkListBoxRow* row1, GtkListBoxRow* row2) {
	gint result = 0;
	gchar* name1 = NULL;
	GtkListBoxRow* _tmp0_;
	gconstpointer _tmp1_;
	gchar* _tmp2_;
	gchar* name2 = NULL;
	GtkListBoxRow* _tmp3_;
	gconstpointer _tmp4_;
	gchar* _tmp5_;
	GCompareFunc _tmp6_;
	gint _tmp7_;
	g_return_val_if_fail (self != NULL, 0);
	g_return_val_if_fail (row1 != NULL, 0);
	g_return_val_if_fail (row2 != NULL, 0);
	_tmp0_ = row1;
	_tmp1_ = g_object_get_data ((GObject*) _tmp0_, "variable_name");
	_tmp2_ = g_strdup ((const gchar*) _tmp1_);
	name1 = _tmp2_;
	_tmp3_ = row2;
	_tmp4_ = g_object_get_data ((GObject*) _tmp3_, "variable_name");
	_tmp5_ = g_strdup ((const gchar*) _tmp4_);
	name2 = _tmp5_;
	_tmp6_ = g_strcmp0;
	_tmp7_ = _tmp6_ (name1, name2);
	result = _tmp7_;
	_g_free0 (name2);
	_g_free0 (name1);
	return result;
}


static void math_variable_popover_class_init (MathVariablePopoverClass * klass) {
	gint MathVariablePopover_private_offset;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gchar** _tmp2_;
	math_variable_popover_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (MathVariablePopoverPrivate));
	G_OBJECT_CLASS (klass)->finalize = math_variable_popover_finalize;
	MathVariablePopover_private_offset = g_type_class_get_instance_private_offset (klass);
	gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), "/org/gnome/calculator/math-variable-popover.ui");
	_tmp0_ = g_strdup ("ans");
	_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_key_press_cb", G_CALLBACK(_math_variable_popover_variable_name_key_press_cb_gtk_widget_key_press_event));
	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) {
	self->priv = MATH_VARIABLE_POPOVER_GET_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_unref0 (self->priv->equation);
	_g_object_unref0 (self->priv->variable_list);
	_g_object_unref0 (self->priv->variable_name_entry);
	_g_object_unref0 (self->priv->store_variable_button);
	G_OBJECT_CLASS (math_variable_popover_parent_class)->finalize (obj);
}


GType math_variable_popover_get_type (void) {
	static volatile gsize math_variable_popover_type_id__volatile = 0;
	if (g_once_init_enter (&math_variable_popover_type_id__volatile)) {
		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 (gtk_popover_get_type (), "MathVariablePopover", &g_define_type_info, 0);
		g_once_init_leave (&math_variable_popover_type_id__volatile, math_variable_popover_type_id);
	}
	return math_variable_popover_type_id__volatile;
}


static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	if ((array != NULL) && (destroy_func != NULL)) {
		int i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}


static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}



