//	bfe2 - gtk wrapper functions
//	Copyright (c) 2003 Brand Huntsman
//


#include "common.h"
#include "functions.h"


//////////////////////////////////////////////////////////////////////////

// global

// local
uint toggle_group_button_hack;

//////////////////////////////////////////////////////////////////////////


gint window_delete_event( GtkWidget *widget, GdkEvent *event, gpointer data ){
	return(FALSE);
}


static void toggle_group_button( GtkWidget *widget, gpointer data ){
	s_bgroup *group = (s_bgroup *)data;
	GtkWidget *button;
	uint x;

	if(toggle_group_button_hack){
		toggle_group_button_hack = 0;
		return;
	}

	for(x = 0; x < group->nr_buttons; x++)
		if(group->button[x].widget == widget) break;

	if(group->current == x){
		gtk_signal_disconnect(GTK_OBJECT(widget), group->button[x].id);
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
		toggle_group_button_hack = 1;
		group->button[x].id = gtk_signal_connect(GTK_OBJECT(widget), "clicked",
			GTK_SIGNAL_FUNC(toggle_group_button), (gpointer)group);
	} else {
		button = group->button[group->current].widget;
		gtk_signal_disconnect(GTK_OBJECT(button), group->button[group->current].id);
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), FALSE);
		group->button[group->current].id = gtk_signal_connect(GTK_OBJECT(button), "clicked",
			GTK_SIGNAL_FUNC(toggle_group_button), (gpointer)group);
		group->current = x;
		if(group->callback) (group->callback)(group);
	}
}


//////////////////////////////////////////////////////////////////////////


GtkWidget *new_hbox( GtkWidget *box, gboolean pack ){
	GtkWidget *hbox;

	hbox = gtk_hbox_new(FALSE, 5);
	gtk_box_pack_start(GTK_BOX(box), hbox, pack, pack, 0);
	gtk_widget_show(hbox);

	return(hbox);
}


GtkWidget *new_vbox( GtkWidget *box, gboolean pack ){
	GtkWidget *vbox;

	vbox = gtk_vbox_new(FALSE, 5);
	gtk_box_pack_start(GTK_BOX(box), vbox, pack, pack, 0);
	gtk_widget_show(vbox);

	return(vbox);
}


void new_gap( GtkWidget *box ){
	GtkWidget *sep;

	sep = gtk_label_new("");
	gtk_box_pack_start(GTK_BOX(box), sep, TRUE, TRUE, 0);
	gtk_widget_show(sep);
}


GtkCList *new_list( GtkWidget *box, uint columns, gchar *titles[] ){
	GtkWidget *scrolled_window;
	GtkCList *list;
	uint x;

	// create scrolled window
	scrolled_window = gtk_scrolled_window_new(NULL, NULL);
	gtk_container_set_border_width(GTK_CONTAINER(scrolled_window), 0);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
		GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	gtk_box_pack_start(GTK_BOX(box), scrolled_window, TRUE, TRUE, 0);
	gtk_widget_show(scrolled_window);

	// create list
	list = (GtkCList *)gtk_clist_new_with_titles(columns, titles);
	for(x = 0; x < columns; x++)
		gtk_clist_set_column_auto_resize(list, x, TRUE);
	gtk_clist_set_selection_mode(list, GTK_SELECTION_SINGLE);
	gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET(list));
	gtk_widget_show(GTK_WIDGET(list));

	gtk_clist_column_titles_hide(list);

	return(list);
}


GtkWidget *new_label( GtkWidget *box, gboolean pack, char *message, gboolean wrap ){
	GtkWidget *label;

	label = gtk_label_new(message);
	if(wrap == TRUE) gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
	gtk_box_pack_start(GTK_BOX(box), label, pack, pack, 0);
	gtk_widget_show(label);

	return(label);
}


GtkWidget *new_text_entry( GtkWidget *box, gboolean pack, uint width ){
	GtkWidget *text;

	text = gtk_entry_new();
	gtk_widget_set_usize(text, width, WIDGET_HEIGHT);
	gtk_box_pack_start(GTK_BOX(box), text, pack, pack, 0);
	gtk_widget_show(text);

	return(text);
}


GtkWidget *new_button( GtkWidget *box, gboolean pack, char *name ){
	GtkWidget *button;

	button = gtk_button_new_with_label(name);
	gtk_widget_set_usize(button, 0, WIDGET_HEIGHT);
	gtk_box_pack_start(GTK_BOX(box), button, pack, pack, 0);
	gtk_widget_show(button);

	return(button);
}


GtkWidget *new_check_button( GtkWidget *box, gboolean pack, char *name, gboolean state ){
	GtkWidget *button;

	button = gtk_check_button_new_with_label(name);
	gtk_widget_set_usize(button, 0, WIDGET_HEIGHT);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), state);
	gtk_box_pack_start(GTK_BOX(box), button, pack, pack, 0);
	gtk_widget_show(button);

	return(button);
}


GtkWidget *new_toggle_button( GtkWidget *box, gboolean pack, char *name ){
	GtkWidget *button;

	button = gtk_toggle_button_new_with_label(name);
	gtk_widget_set_usize(button, 0, WIDGET_HEIGHT);
	gtk_box_pack_start(GTK_BOX(box), button, pack, pack, 0);
	gtk_widget_show(button);

	return(button);
}


s_bgroup *new_button_group( GtkWidget *box, gboolean pack, e_orientation orientation, uint nr_buttons, char *titles[] ){
	GtkWidget *bbox, *button;
	s_bgroup *group;
	uint x;

	group = (s_bgroup *)malloc(sizeof(s_bgroup) + sizeof(struct s_bgroup_button) * (nr_buttons - 1));
	if(group == NULL){
		g_print("BFE: Couldn't allocate button group.\n");
		exit(1);
	}

	// create vbox or hbox
	if(orientation == VERTICAL) bbox = gtk_vbox_new(FALSE, 0);
	else bbox = gtk_hbox_new(FALSE, 0);
	gtk_box_pack_start(GTK_BOX(box), bbox, pack, pack, 0);
	gtk_widget_show(bbox);

	// create togle buttons
	for(x = 0; x < nr_buttons; x++){
		button = new_toggle_button(bbox, TRUE, titles[x]);
		if(x == 0) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), TRUE);
		group->button[x].id = gtk_signal_connect(GTK_OBJECT(button), "clicked",
			GTK_SIGNAL_FUNC(toggle_group_button), (gpointer)group);
		group->button[x].widget = button;
	}
	group->callback = NULL;
	group->current = 0;
	group->nr_buttons = nr_buttons;

	toggle_group_button_hack = 0;
	return(group);
}
void set_bgroup( s_bgroup *group, uint button ){
	if(button != group->current && button < group->nr_buttons)
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(group->button[button].widget), TRUE);
}


void new_separator( GtkWidget *box, gboolean pack, e_orientation orientation ){
	GtkWidget *sep;

	if(orientation == VERTICAL)
		sep = gtk_vseparator_new();
	else sep = gtk_hseparator_new();
	gtk_box_pack_start(GTK_BOX(box), sep, pack, pack, 0);
	gtk_widget_show(sep);
}


GtkWidget *new_window( GtkWindowType type, char *title, uint width, uint height ){
	GtkWidget *window;

	// create window
	window = gtk_window_new(type);
	gtk_signal_connect(GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(window_delete_event), NULL);
	gtk_container_set_border_width(GTK_CONTAINER(window), 5);
	gtk_widget_set_usize(GTK_WIDGET(window), width, height);
	gtk_window_set_title(GTK_WINDOW(window), title);
	gtk_widget_show(window);

	return(window);
}


GtkWidget *new_window_vbox( GtkWidget *window ){
	GtkWidget *vbox;

	// create window vbox
	vbox = gtk_vbox_new(FALSE, 5);
	gtk_container_add(GTK_CONTAINER(window), vbox);
	gtk_widget_show(vbox);

	return(vbox);
}
