/*
 * EveryBuddy 
 *
 * Copyright (C) 1999, Torrey Searle <tsearle@uci.edu>
 *
 * 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
 *
 */

#include <gtk/gtk.h>
#include <gdk/gdkprivate.h>
#include "dialog.h"
#include "util.h"
#include "status.h"
#include "pixmaps/ebicon.xpm"
#include "pixmaps/tb_yes.xpm"
#include "pixmaps/tb_no.xpm"

static GdkPixmap *icon_pm = NULL;
static GdkBitmap *icon_bm = NULL;

/*------------------------------------------------------------------------*/
/* Function for making buttons											  */
/*------------------------------------------------------------------------*/

static void set_option(GtkWidget * w, int * data )
{
	*data = !(*data);	
	write_prefs();
}

GtkWidget *eb_button(const char *text, int *value, GtkWidget *page)
{
    GtkWidget *button;
    button = gtk_check_button_new_with_label(text);
    gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button), *value);
    gtk_box_pack_start(GTK_BOX(page), button, FALSE, FALSE, 0);
    gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(set_option), value);
    gtk_widget_show(button);
    return button;
}


/*------------------------------------------------------------------------*/
/* Function for creating Everybuddy Icon on Windows                       */
/*------------------------------------------------------------------------*/

void eb_icon(GdkWindow *w)
{
	GdkAtom icon_atom;
	glong data[2];
        if (icon_pm == NULL) {
                icon_pm = gdk_pixmap_create_from_xpm_d(w, &icon_bm,
                          NULL, (gchar **)ebicon);
        }

		data[0] = ((GdkPixmapPrivate *)icon_pm)->xwindow;
		data[1] = ((GdkPixmapPrivate *)icon_bm)->xwindow;

		icon_atom = gdk_atom_intern( "KWM_WIN_ICON", FALSE);
		gdk_property_change (w, icon_atom, icon_atom,
				32, GDK_PROP_MODE_REPLACE,
				(guchar *)data, 2);

        gdk_window_set_icon(w, NULL, icon_pm, icon_bm);
//		gdk_window_set_back_pixmap(w, icon_pm, 0 );
 //       gdk_window_set_group(w, statuswindow->window);
}


void do_dialog( gchar * message, gchar * title, void (*action)(GtkWidget * widget, gpointer data), gpointer data )
{
	GtkWidget * dialog_window;
	GtkWidget * vbox;
	GtkWidget * hbox;
	GtkWidget * button;
	GtkWidget * label;
	GdkPixmap *pm;
	GdkBitmap *bm;

	GtkWidget * button_box;
	dialog_window = gtk_window_new(GTK_WINDOW_DIALOG);

	gtk_widget_realize(dialog_window);
	gtk_window_set_title(GTK_WINDOW(dialog_window), title );



	vbox = gtk_vbox_new(FALSE, 5);
	label = gtk_label_new(message);
	gtk_box_pack_start(GTK_BOX(vbox),label, TRUE, TRUE, 5);
	gtk_widget_show(label);

	hbox = gtk_hbox_new(TRUE, 5);

    button_box = gtk_hbox_new(FALSE, 5);
	pm = gdk_pixmap_create_from_xpm_d(statuswindow->window, &bm, NULL, tb_yes_xpm);
	label = gtk_pixmap_new(pm, bm);

	gtk_box_pack_start(GTK_BOX(button_box), label, FALSE, FALSE, 2 );
	gtk_widget_show(label);

	label = gtk_label_new("Yes");
	gtk_box_pack_start(GTK_BOX(button_box), label, FALSE, FALSE, 2 );
	gtk_widget_show(label);


	button = gtk_button_new();
	gtk_container_add(GTK_CONTAINER(button), button_box);
	gtk_widget_show(button_box);
	gtk_signal_connect(GTK_OBJECT(button), "clicked",
			 		   GTK_SIGNAL_FUNC(action), data );
	gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
			                  GTK_SIGNAL_FUNC(gtk_widget_destroy),
							  (gpointer)dialog_window);
	gtk_object_set_user_data(GTK_OBJECT(button), (gpointer)1);
	
	gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 5 );
	gtk_widget_show(button);

    button_box = gtk_hbox_new(FALSE, 5);
	pm = gdk_pixmap_create_from_xpm_d(statuswindow->window, &bm, NULL, tb_no_xpm);
	label = gtk_pixmap_new(pm, bm);

	gtk_box_pack_start(GTK_BOX(button_box), label, FALSE, FALSE, 2 );
	gtk_widget_show(label);

	label = gtk_label_new("No");
	gtk_box_pack_start(GTK_BOX(button_box), label, FALSE, FALSE, 2 );
	gtk_widget_show(label);


	button = gtk_button_new();
	gtk_container_add(GTK_CONTAINER(button), button_box);
	gtk_widget_show(button_box);

	gtk_signal_connect(GTK_OBJECT(button), "clicked",
			 		   GTK_SIGNAL_FUNC(action), data);
	gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
			                  GTK_SIGNAL_FUNC(gtk_widget_destroy),
							  (gpointer)dialog_window);
	gtk_object_set_user_data(GTK_OBJECT(button), (gpointer)0 );

	gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 5);
	gtk_widget_show(button);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
	gtk_widget_show(hbox);

	gtk_container_add(GTK_CONTAINER(dialog_window), vbox);
	gtk_widget_show(vbox);
	gtk_widget_show(dialog_window);
}

	
	

/*------------------------------------------------------------------------*/
/*  The dialog for getting an error                                       */
/*------------------------------------------------------------------------*/

void
do_error_dialog(char *message, char *title)
{
    GtkWidget *d;
    GtkWidget *label;
    GtkWidget *close;


    d = gtk_dialog_new();

        label = gtk_label_new(message);
        gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
    gtk_widget_show(label);
    close = gtk_button_new_with_label("Close");
    gtk_widget_show(close);
    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(d)->vbox),
        label, FALSE, FALSE, 5);
    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(d)->action_area),
        close, FALSE, FALSE, 5);



    gtk_widget_realize(d);
    gtk_window_set_title(GTK_WINDOW(d), title);
    gtk_signal_connect_object(GTK_OBJECT(close), "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(d));
	gtk_window_set_modal(GTK_WINDOW(d), TRUE);

    gtk_widget_show(d);
}

