/*
 * This file is part of roccat-tools.
 *
 * roccat-tools 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.
 *
 * roccat-tools 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 roccat-tools. If not, see <http://www.gnu.org/licenses/>.
 */

#include "koneconfig_weight_frame.h"

#define KONECONFIG_WEIGHT_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), KONECONFIG_WEIGHT_FRAME_TYPE, KoneconfigWeightFrameClass))
#define IS_KONECONFIG_WEIGHT_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), KONECONFIG_WEIGHT_FRAME_TYPE))
#define KONECONFIG_WEIGHT_FRAME_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), KONECONFIG_WEIGHT_FRAME_TYPE, KoneconfigWeightFramePrivate))

struct _KoneconfigWeightFramePrivate {
	GtkButton *button;
	GtkLabel *label;
};

G_DEFINE_TYPE(KoneconfigWeightFrame, koneconfig_weight_frame, GTK_TYPE_FRAME);

enum {
	UPDATE,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

void koneconfig_weight_frame_unset_value(KoneconfigWeightFrame *frame) {
	gtk_label_set_text(frame->priv->label, "n/a");
}

void koneconfig_weight_frame_set_value(KoneconfigWeightFrame *frame, guint value) {
	gchar *text;

	text = g_strdup_printf("%ug", value);
	gtk_label_set_text(frame->priv->label, text);
	g_free(text);
}

GtkWidget *koneconfig_weight_frame_new(void) {
	KoneconfigWeightFrame *frame;

	frame = g_object_new(KONECONFIG_WEIGHT_FRAME_TYPE, NULL);

	return GTK_WIDGET(frame);
}

static void button_clicked_cb(GtkButton *button, gpointer user_data) {
	KoneconfigWeightFrame *frame = KONECONFIG_WEIGHT_FRAME(user_data);
	g_signal_emit((gpointer)frame, signals[UPDATE], 0);
}

static void koneconfig_weight_frame_init(KoneconfigWeightFrame *frame) {
	KoneconfigWeightFramePrivate *priv = KONECONFIG_WEIGHT_FRAME_GET_PRIVATE(frame);
	GtkWidget *content;

	frame->priv = priv;

	content = gtk_hbox_new(FALSE, 0);
	priv->button = GTK_BUTTON(gtk_button_new_with_label("Update"));
	priv->label = GTK_LABEL(gtk_label_new(NULL));

	g_signal_connect(G_OBJECT(priv->button), "clicked", G_CALLBACK(button_clicked_cb), frame);

	gtk_container_add(GTK_CONTAINER(frame), content);
	gtk_box_pack_start(GTK_BOX(content), GTK_WIDGET(priv->label), TRUE, TRUE, 0);
	gtk_box_pack_end(GTK_BOX(content), GTK_WIDGET(priv->button), TRUE, TRUE, 0);

	gtk_frame_set_label(GTK_FRAME(frame), "Fast-clip weight");

	koneconfig_weight_frame_unset_value(frame);

	gtk_widget_show_all(content);
}

static void koneconfig_weight_frame_class_init(KoneconfigWeightFrameClass *klass) {
	g_type_class_add_private(klass, sizeof(KoneconfigWeightFramePrivate));

	signals[UPDATE] = g_signal_new("update",
			G_TYPE_FROM_CLASS(klass),
			G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
			G_STRUCT_OFFSET(KoneconfigWeightFrameClass, update),
			NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}
