/* mini-graph.c generated by valac 0.17.7, the Vala compiler
 * generated from mini-graph.vala, do not modify */

/* This file is part of GNOME Boxes. License: LGPLv2+*/

#include <glib.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <float.h>
#include <math.h>
#include <cairo.h>
#include <gdk/gdk.h>


#define BOXES_TYPE_MINI_GRAPH (boxes_mini_graph_get_type ())
#define BOXES_MINI_GRAPH(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), BOXES_TYPE_MINI_GRAPH, BoxesMiniGraph))
#define BOXES_MINI_GRAPH_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), BOXES_TYPE_MINI_GRAPH, BoxesMiniGraphClass))
#define BOXES_IS_MINI_GRAPH(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), BOXES_TYPE_MINI_GRAPH))
#define BOXES_IS_MINI_GRAPH_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), BOXES_TYPE_MINI_GRAPH))
#define BOXES_MINI_GRAPH_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), BOXES_TYPE_MINI_GRAPH, BoxesMiniGraphClass))

typedef struct _BoxesMiniGraph BoxesMiniGraph;
typedef struct _BoxesMiniGraphClass BoxesMiniGraphClass;
typedef struct _BoxesMiniGraphPrivate BoxesMiniGraphPrivate;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _BoxesMiniGraph {
	GtkDrawingArea parent_instance;
	BoxesMiniGraphPrivate * priv;
};

struct _BoxesMiniGraphClass {
	GtkDrawingAreaClass parent_class;
};

struct _BoxesMiniGraphPrivate {
	gdouble* _points;
	gint _points_length1;
	gint __points_size_;
	gint _npoints;
	gdouble _ymax;
	gboolean ymax_set;
};


static gpointer boxes_mini_graph_parent_class = NULL;

GType boxes_mini_graph_get_type (void) G_GNUC_CONST;
#define BOXES_MINI_GRAPH_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), BOXES_TYPE_MINI_GRAPH, BoxesMiniGraphPrivate))
enum  {
	BOXES_MINI_GRAPH_DUMMY_PROPERTY,
	BOXES_MINI_GRAPH_NPOINTS
};
BoxesMiniGraph* boxes_mini_graph_new (gdouble* points, int points_length1, gint npoints);
BoxesMiniGraph* boxes_mini_graph_construct (GType object_type, gdouble* points, int points_length1, gint npoints);
void boxes_mini_graph_set_points (BoxesMiniGraph* self, gdouble* value, int value_length1);
void boxes_mini_graph_set_npoints (BoxesMiniGraph* self, gint value);
BoxesMiniGraph* boxes_mini_graph_new_with_ymax (gdouble* points, int points_length1, gdouble ymax, gint npoints);
BoxesMiniGraph* boxes_mini_graph_construct_with_ymax (GType object_type, gdouble* points, int points_length1, gdouble ymax, gint npoints);
static void boxes_mini_graph_set_ymax (BoxesMiniGraph* self, gdouble value);
static gdouble boxes_mini_graph_max (BoxesMiniGraph* self);
gdouble* boxes_mini_graph_get_points (BoxesMiniGraph* self, int* result_length1);
static gboolean boxes_mini_graph_real_draw (GtkWidget* base, cairo_t* cr);
gint boxes_mini_graph_get_npoints (BoxesMiniGraph* self);
static gdouble boxes_mini_graph_get_ymax (BoxesMiniGraph* self);
static gdouble* _vala_array_dup3 (gdouble* self, int length);
static void boxes_mini_graph_finalize (GObject* obj);
static void _vala_boxes_mini_graph_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec);
static void _vala_boxes_mini_graph_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec);


BoxesMiniGraph* boxes_mini_graph_construct (GType object_type, gdouble* points, int points_length1, gint npoints) {
	BoxesMiniGraph * self = NULL;
	gdouble* _tmp0_;
	gint _tmp0__length1;
	gint _tmp1_;
	self = (BoxesMiniGraph*) g_object_new (object_type, NULL);
	_tmp0_ = points;
	_tmp0__length1 = points_length1;
	boxes_mini_graph_set_points (self, _tmp0_, _tmp0__length1);
	_tmp1_ = npoints;
	boxes_mini_graph_set_npoints (self, _tmp1_);
	return self;
}


BoxesMiniGraph* boxes_mini_graph_new (gdouble* points, int points_length1, gint npoints) {
	return boxes_mini_graph_construct (BOXES_TYPE_MINI_GRAPH, points, points_length1, npoints);
}


BoxesMiniGraph* boxes_mini_graph_construct_with_ymax (GType object_type, gdouble* points, int points_length1, gdouble ymax, gint npoints) {
	BoxesMiniGraph * self = NULL;
	gdouble* _tmp0_;
	gint _tmp0__length1;
	gdouble _tmp1_;
	gint _tmp2_;
	self = (BoxesMiniGraph*) g_object_new (object_type, NULL);
	_tmp0_ = points;
	_tmp0__length1 = points_length1;
	boxes_mini_graph_set_points (self, _tmp0_, _tmp0__length1);
	_tmp1_ = ymax;
	boxes_mini_graph_set_ymax (self, _tmp1_);
	_tmp2_ = npoints;
	boxes_mini_graph_set_npoints (self, _tmp2_);
	return self;
}


BoxesMiniGraph* boxes_mini_graph_new_with_ymax (gdouble* points, int points_length1, gdouble ymax, gint npoints) {
	return boxes_mini_graph_construct_with_ymax (BOXES_TYPE_MINI_GRAPH, points, points_length1, ymax, npoints);
}


static gdouble boxes_mini_graph_max (BoxesMiniGraph* self) {
	gdouble result = 0.0;
	gdouble* _tmp0_;
	gint _tmp0__length1;
	gdouble* _tmp1_;
	gint _tmp1__length1;
	gdouble* _tmp2_;
	gint _tmp2__length1;
	gdouble* _tmp3_;
	gint _tmp3__length1;
	gdouble _tmp4_;
	gdouble max;
	gdouble* _tmp5_;
	gint _tmp5__length1;
	gdouble* _tmp6_;
	gint _tmp6__length1;
	g_return_val_if_fail (self != NULL, 0.0);
	_tmp0_ = boxes_mini_graph_get_points (self, &_tmp0__length1);
	_tmp1_ = _tmp0_;
	_tmp1__length1 = _tmp0__length1;
	if (_tmp1__length1 == 0) {
		result = 1.0;
		return result;
	}
	_tmp2_ = boxes_mini_graph_get_points (self, &_tmp2__length1);
	_tmp3_ = _tmp2_;
	_tmp3__length1 = _tmp2__length1;
	_tmp4_ = _tmp3_[0];
	max = _tmp4_;
	_tmp5_ = boxes_mini_graph_get_points (self, &_tmp5__length1);
	_tmp6_ = _tmp5_;
	_tmp6__length1 = _tmp5__length1;
	{
		gdouble* p_collection = NULL;
		gint p_collection_length1 = 0;
		gint _p_collection_size_ = 0;
		gint p_it = 0;
		p_collection = _tmp6_;
		p_collection_length1 = _tmp6__length1;
		for (p_it = 0; p_it < _tmp6__length1; p_it = p_it + 1) {
			gdouble p = 0.0;
			p = p_collection[p_it];
			{
				gdouble _tmp7_;
				gdouble _tmp8_;
				_tmp7_ = p;
				_tmp8_ = max;
				if (_tmp7_ > _tmp8_) {
					gdouble _tmp9_;
					_tmp9_ = p;
					max = _tmp9_;
				}
			}
		}
	}
	result = max;
	return result;
}


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


static gboolean boxes_mini_graph_real_draw (GtkWidget* base, cairo_t* cr) {
	BoxesMiniGraph * self;
	gboolean result = FALSE;
	gint _tmp0_ = 0;
	gint width;
	gint _tmp1_ = 0;
	gint height;
	GtkStyleContext* _tmp2_ = NULL;
	GtkStyleContext* _tmp3_;
	GtkStyleContext* style;
	cairo_t* _tmp4_;
	GtkStyleContext* _tmp5_;
	GtkStateFlags _tmp6_ = 0;
	GdkRGBA _tmp7_ = {0};
	cairo_t* _tmp8_;
	gint _tmp9_;
	gint _tmp10_;
	cairo_t* _tmp11_;
	gint _tmp12_ = 0;
	gint _tmp13_;
	gint _tmp17_;
	gint nstep;
	gdouble _tmp18_ = 0.0;
	gboolean _tmp19_;
	gdouble _tmp23_;
	gdouble ymax;
	gdouble dy;
	gdouble dx;
	gint _tmp24_;
	gdouble _tmp27_;
	cairo_t* _tmp30_;
	GtkStyleContext* _tmp31_;
	GdkRGBA _tmp32_ = {0};
	gdouble x;
	gdouble* _tmp33_;
	gint _tmp33__length1;
	gdouble* _tmp34_;
	gint _tmp34__length1;
	cairo_t* _tmp47_;
	gdouble _tmp48_;
	gdouble _tmp49_;
	gint _tmp50_;
	cairo_t* _tmp51_;
	gint _tmp52_;
	cairo_t* _tmp53_;
	cairo_t* _tmp54_;
	GtkStyleContext* _tmp55_;
	GtkStateFlags _tmp56_ = 0;
	GdkRGBA _tmp57_ = {0};
	cairo_t* _tmp58_;
	gdouble* _tmp59_;
	gint _tmp59__length1;
	gdouble* _tmp60_;
	gint _tmp60__length1;
	cairo_t* _tmp73_;
	self = (BoxesMiniGraph*) base;
	g_return_val_if_fail (cr != NULL, FALSE);
	_tmp0_ = gtk_widget_get_allocated_width ((GtkWidget*) self);
	width = _tmp0_;
	_tmp1_ = gtk_widget_get_allocated_height ((GtkWidget*) self);
	height = _tmp1_;
	_tmp2_ = gtk_widget_get_style_context ((GtkWidget*) self);
	_tmp3_ = _g_object_ref0 (_tmp2_);
	style = _tmp3_;
	_tmp4_ = cr;
	_tmp5_ = style;
	_tmp6_ = gtk_widget_get_state_flags ((GtkWidget*) self);
	gtk_style_context_get_background_color (_tmp5_, _tmp6_, &_tmp7_);
	gdk_cairo_set_source_rgba (_tmp4_, &_tmp7_);
	_tmp8_ = cr;
	_tmp9_ = width;
	_tmp10_ = height;
	cairo_rectangle (_tmp8_, (gdouble) 0, (gdouble) 0, (gdouble) _tmp9_, (gdouble) _tmp10_);
	_tmp11_ = cr;
	cairo_fill (_tmp11_);
	_tmp13_ = self->priv->_npoints;
	if (_tmp13_ == (-1)) {
		gdouble* _tmp14_;
		gint _tmp14__length1;
		gdouble* _tmp15_;
		gint _tmp15__length1;
		_tmp14_ = boxes_mini_graph_get_points (self, &_tmp14__length1);
		_tmp15_ = _tmp14_;
		_tmp15__length1 = _tmp14__length1;
		_tmp12_ = _tmp15__length1;
	} else {
		gint _tmp16_;
		_tmp16_ = self->priv->_npoints;
		_tmp12_ = _tmp16_;
	}
	_tmp17_ = _tmp12_;
	nstep = _tmp17_ - 1;
	_tmp19_ = self->priv->ymax_set;
	if (_tmp19_) {
		gdouble _tmp20_;
		gdouble _tmp21_;
		_tmp20_ = boxes_mini_graph_get_ymax (self);
		_tmp21_ = _tmp20_;
		_tmp18_ = _tmp21_;
	} else {
		gdouble _tmp22_ = 0.0;
		_tmp22_ = boxes_mini_graph_max (self);
		_tmp18_ = _tmp22_;
	}
	_tmp23_ = _tmp18_;
	ymax = _tmp23_;
	dy = 0.0;
	dx = 0.0;
	_tmp24_ = nstep;
	if (_tmp24_ != 0) {
		gint _tmp25_;
		gint _tmp26_;
		_tmp25_ = width;
		_tmp26_ = nstep;
		dx = ((gdouble) _tmp25_) / _tmp26_;
	}
	_tmp27_ = ymax;
	if (_tmp27_ != ((gdouble) 0)) {
		gint _tmp28_;
		gdouble _tmp29_;
		_tmp28_ = height;
		_tmp29_ = ymax;
		dy = ((gdouble) _tmp28_) / _tmp29_;
	}
	_tmp30_ = cr;
	_tmp31_ = style;
	gtk_style_context_get_color (_tmp31_, GTK_STATE_FLAG_NORMAL, &_tmp32_);
	gdk_cairo_set_source_rgba (_tmp30_, &_tmp32_);
	x = 0.0;
	_tmp33_ = boxes_mini_graph_get_points (self, &_tmp33__length1);
	_tmp34_ = _tmp33_;
	_tmp34__length1 = _tmp33__length1;
	{
		gdouble* p_collection = NULL;
		gint p_collection_length1 = 0;
		gint _p_collection_size_ = 0;
		gint p_it = 0;
		p_collection = _tmp34_;
		p_collection_length1 = _tmp34__length1;
		for (p_it = 0; p_it < _tmp34__length1; p_it = p_it + 1) {
			gdouble p = 0.0;
			p = p_collection[p_it];
			{
				gint _tmp35_;
				gdouble _tmp36_;
				gdouble _tmp37_;
				gdouble y;
				gdouble _tmp38_;
				gdouble _tmp45_;
				gdouble _tmp46_;
				_tmp35_ = height;
				_tmp36_ = p;
				_tmp37_ = dy;
				y = _tmp35_ - (_tmp36_ * _tmp37_);
				_tmp38_ = x;
				if (_tmp38_ == 0.0) {
					cairo_t* _tmp39_;
					gdouble _tmp40_;
					gdouble _tmp41_;
					_tmp39_ = cr;
					_tmp40_ = x;
					_tmp41_ = y;
					cairo_move_to (_tmp39_, _tmp40_, _tmp41_);
				} else {
					cairo_t* _tmp42_;
					gdouble _tmp43_;
					gdouble _tmp44_;
					_tmp42_ = cr;
					_tmp43_ = x;
					_tmp44_ = y;
					cairo_line_to (_tmp42_, _tmp43_, _tmp44_);
				}
				_tmp45_ = x;
				_tmp46_ = dx;
				x = _tmp45_ + _tmp46_;
			}
		}
	}
	_tmp47_ = cr;
	_tmp48_ = x;
	_tmp49_ = dx;
	_tmp50_ = height;
	cairo_line_to (_tmp47_, _tmp48_ - _tmp49_, (gdouble) _tmp50_);
	_tmp51_ = cr;
	_tmp52_ = height;
	cairo_line_to (_tmp51_, (gdouble) 0, (gdouble) _tmp52_);
	_tmp53_ = cr;
	cairo_fill (_tmp53_);
	_tmp54_ = cr;
	_tmp55_ = style;
	_tmp56_ = gtk_widget_get_state_flags ((GtkWidget*) self);
	gtk_style_context_get_border_color (_tmp55_, _tmp56_, &_tmp57_);
	gdk_cairo_set_source_rgba (_tmp54_, &_tmp57_);
	_tmp58_ = cr;
	cairo_set_line_width (_tmp58_, 1.0);
	x = 0.0;
	_tmp59_ = boxes_mini_graph_get_points (self, &_tmp59__length1);
	_tmp60_ = _tmp59_;
	_tmp60__length1 = _tmp59__length1;
	{
		gdouble* p_collection = NULL;
		gint p_collection_length1 = 0;
		gint _p_collection_size_ = 0;
		gint p_it = 0;
		p_collection = _tmp60_;
		p_collection_length1 = _tmp60__length1;
		for (p_it = 0; p_it < _tmp60__length1; p_it = p_it + 1) {
			gdouble p = 0.0;
			p = p_collection[p_it];
			{
				gint _tmp61_;
				gdouble _tmp62_;
				gdouble _tmp63_;
				gdouble y;
				gdouble _tmp64_;
				gdouble _tmp71_;
				gdouble _tmp72_;
				_tmp61_ = height;
				_tmp62_ = p;
				_tmp63_ = dy;
				y = _tmp61_ - (_tmp62_ * _tmp63_);
				_tmp64_ = x;
				if (_tmp64_ == 0.0) {
					cairo_t* _tmp65_;
					gdouble _tmp66_;
					gdouble _tmp67_;
					_tmp65_ = cr;
					_tmp66_ = x;
					_tmp67_ = y;
					cairo_move_to (_tmp65_, _tmp66_, _tmp67_);
				} else {
					cairo_t* _tmp68_;
					gdouble _tmp69_;
					gdouble _tmp70_;
					_tmp68_ = cr;
					_tmp69_ = x;
					_tmp70_ = y;
					cairo_line_to (_tmp68_, _tmp69_, _tmp70_);
				}
				_tmp71_ = x;
				_tmp72_ = dx;
				x = _tmp71_ + _tmp72_;
			}
		}
	}
	_tmp73_ = cr;
	cairo_stroke (_tmp73_);
	result = TRUE;
	_g_object_unref0 (style);
	return result;
}


gdouble* boxes_mini_graph_get_points (BoxesMiniGraph* self, int* result_length1) {
	gdouble* result;
	gdouble* _tmp0_;
	gint _tmp0__length1;
	gdouble* _tmp1_;
	gint _tmp1__length1;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_points;
	_tmp0__length1 = self->priv->_points_length1;
	_tmp1_ = _tmp0_;
	_tmp1__length1 = _tmp0__length1;
	if (result_length1) {
		*result_length1 = _tmp1__length1;
	}
	result = _tmp1_;
	return result;
}


static gdouble* _vala_array_dup3 (gdouble* self, int length) {
	return g_memdup (self, length * sizeof (gdouble));
}


void boxes_mini_graph_set_points (BoxesMiniGraph* self, gdouble* value, int value_length1) {
	gdouble* _tmp0_;
	gint _tmp0__length1;
	gdouble* _tmp1_;
	gint _tmp1__length1;
	g_return_if_fail (self != NULL);
	_tmp0_ = value;
	_tmp0__length1 = value_length1;
	_tmp1_ = (_tmp0_ != NULL) ? _vala_array_dup3 (_tmp0_, _tmp0__length1) : ((gpointer) _tmp0_);
	_tmp1__length1 = _tmp0__length1;
	self->priv->_points = (g_free (self->priv->_points), NULL);
	self->priv->_points = _tmp1_;
	self->priv->_points_length1 = _tmp1__length1;
	self->priv->__points_size_ = self->priv->_points_length1;
	gtk_widget_queue_draw ((GtkWidget*) self);
}


gint boxes_mini_graph_get_npoints (BoxesMiniGraph* self) {
	gint result;
	gint _tmp0_;
	g_return_val_if_fail (self != NULL, 0);
	_tmp0_ = self->priv->_npoints;
	result = _tmp0_;
	return result;
}


void boxes_mini_graph_set_npoints (BoxesMiniGraph* self, gint value) {
	gint _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = value;
	self->priv->_npoints = _tmp0_;
	g_object_notify ((GObject *) self, "npoints");
}


static gdouble boxes_mini_graph_get_ymax (BoxesMiniGraph* self) {
	gdouble result;
	gdouble _tmp0_;
	g_return_val_if_fail (self != NULL, 0.0);
	_tmp0_ = self->priv->_ymax;
	result = _tmp0_;
	return result;
}


static void boxes_mini_graph_set_ymax (BoxesMiniGraph* self, gdouble value) {
	gdouble _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = value;
	self->priv->_ymax = _tmp0_;
	self->priv->ymax_set = TRUE;
}


static void boxes_mini_graph_class_init (BoxesMiniGraphClass * klass) {
	boxes_mini_graph_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (BoxesMiniGraphPrivate));
	GTK_WIDGET_CLASS (klass)->draw = boxes_mini_graph_real_draw;
	G_OBJECT_CLASS (klass)->get_property = _vala_boxes_mini_graph_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_boxes_mini_graph_set_property;
	G_OBJECT_CLASS (klass)->finalize = boxes_mini_graph_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), BOXES_MINI_GRAPH_NPOINTS, g_param_spec_int ("npoints", "npoints", "npoints", G_MININT, G_MAXINT, -1, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE));
}


static void boxes_mini_graph_instance_init (BoxesMiniGraph * self) {
	self->priv = BOXES_MINI_GRAPH_GET_PRIVATE (self);
	self->priv->_npoints = -1;
	self->priv->ymax_set = FALSE;
}


static void boxes_mini_graph_finalize (GObject* obj) {
	BoxesMiniGraph * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, BOXES_TYPE_MINI_GRAPH, BoxesMiniGraph);
	self->priv->_points = (g_free (self->priv->_points), NULL);
	G_OBJECT_CLASS (boxes_mini_graph_parent_class)->finalize (obj);
}


GType boxes_mini_graph_get_type (void) {
	static volatile gsize boxes_mini_graph_type_id__volatile = 0;
	if (g_once_init_enter (&boxes_mini_graph_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (BoxesMiniGraphClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) boxes_mini_graph_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BoxesMiniGraph), 0, (GInstanceInitFunc) boxes_mini_graph_instance_init, NULL };
		GType boxes_mini_graph_type_id;
		boxes_mini_graph_type_id = g_type_register_static (GTK_TYPE_DRAWING_AREA, "BoxesMiniGraph", &g_define_type_info, 0);
		g_once_init_leave (&boxes_mini_graph_type_id__volatile, boxes_mini_graph_type_id);
	}
	return boxes_mini_graph_type_id__volatile;
}


static void _vala_boxes_mini_graph_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
	BoxesMiniGraph * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, BOXES_TYPE_MINI_GRAPH, BoxesMiniGraph);
	switch (property_id) {
		case BOXES_MINI_GRAPH_NPOINTS:
		g_value_set_int (value, boxes_mini_graph_get_npoints (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


static void _vala_boxes_mini_graph_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec) {
	BoxesMiniGraph * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, BOXES_TYPE_MINI_GRAPH, BoxesMiniGraph);
	switch (property_id) {
		case BOXES_MINI_GRAPH_NPOINTS:
		boxes_mini_graph_set_npoints (self, g_value_get_int (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}



