/*
 * $RCSfile: progress_bar.c,v $  $Revision: 1.1 $  $Name:  $
 * $Id: progress_bar.c,v 1.1 2006/03/21 05:47:23 bpaauwe Exp $
 * $Author: bpaauwe $
 * $Date: 2006/03/21 05:47:23 $
 * ----------------------------------------------------------------------------
 *
 *  Copyright (c) Bob Paauwe (2006)
 *
 *  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 2 of the License, or (at your option)
 *  any later version.
 *
 *  This program 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 this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * ----------------------------------------------------------------------------
 *
 *  Description:
 *    Impliments a dialog box with a generic progress bar.
 * ----------------------------------------------------------------------------
 */
#include <string.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <glade/glade.h>
#include <glade/glade-build.h>

#ifdef WITH_GNOME
#  include <libgnomeui/libgnomeui.h>
#endif
#include "globals.h"
#include "scenes.h"

float progress_bar_active;
GtkWidget *progress_dialog;
GtkWidget *progress_bar;
static gdouble progress;

void initialize_progress_bar(GladeXML *xml)
{
	progress_bar_active = 0.0;
	progress_dialog = glade_xml_get_widget(xml, "progress_dialog");
	progress_bar = glade_xml_get_widget(xml, "progress_bar");
	gtk_widget_hide(progress_dialog);
}

/*
 * progress_bar_open
 *
 * Make the progress bar dialog visable and initialize the text.
 */
void progress_bar_open(char *msg, int steps)
{
	gtk_widget_show(progress_dialog);
	gtk_progress_bar_set_fraction((GtkProgressBar *)progress_bar, 0.0);
	while (gtk_events_pending()) {
		gtk_main_iteration();
	}
	gtk_progress_bar_set_text((GtkProgressBar *)progress_bar, msg);
	while (gtk_events_pending()) {
		gtk_main_iteration();
	}
	gtk_widget_queue_draw(progress_dialog);
	while (gtk_events_pending()) {
		gtk_main_iteration();
	}
	if (steps > 0) {
		progress_bar_active = 100.0 / (float)steps;
	} else {
		progress_bar_active = 1.0;
	}
	progress = 0.0;
}


/*
 * progress_bar_close
 *
 * Close down the progress bar.
 */
void progress_bar_close()
{
	gtk_widget_hide(progress_dialog);
	progress_bar_active = 0;
}


/*
 * progress_bar_update
 *
 * Update the progress_bar value.
 */
void progress_bar_update(int percentage)
{
	progress += progress_bar_active;

	if (progress > 100.0)  {
		return;
	}

	/* gtk_progress_bar_pulse((GtkProgressBar *)progress_bar); */
	gtk_progress_bar_set_fraction((GtkProgressBar *)progress_bar,
			progress / 100.0);
	gtk_widget_queue_draw(progress_bar);
	gtk_widget_queue_draw(progress_dialog);
	while (gtk_events_pending()) {
		gtk_main_iteration();
	}
}

