/*
 * 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
 *
 */
 
/*
 * info_window.c
 * implementation for the info window
 *
 */
 
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <ctype.h>
#include "util.h"
#include "gtkhtml.h"
#include "pixmaps/cancel.xpm"
#include "info_window.h"
#include "dialog.h"

static void iw_destroy_event(GtkWidget *widget, gpointer data)
{
        info_window * iw = (info_window *)data;
      
        if(iw->info_data != NULL) {
          iw->cleanup(iw);
          free(iw->info_data);
          iw->info_data = NULL;
        }
        iw->remote_account->infowindow = NULL;
        gtk_widget_destroy(iw->window);
        iw->window=NULL;
        iw->info = NULL;
        g_free(iw);
}

static void iw_close_win(GtkWidget *widget, gpointer data)
{
        info_window * iw = (info_window *)data;
        gtk_widget_destroy(iw->window);
}



info_window * eb_info_window_new(eb_local_account * local, struct account * remote)
{
	GtkWidget *vbox;
        GtkWidget *hbox;
        GtkWidget *buttonbox;
        GtkWidget *label;
	GtkWidget *ok_button;
        GtkWidget *iconwid;
	GdkPixmap *icon;
        GdkBitmap *mask;
	info_window * iw;

        vbox = gtk_vbox_new(FALSE,0);
        hbox = gtk_hbox_new(FALSE,0);
        buttonbox = gtk_hbox_new(FALSE,0);

	iw = malloc(sizeof(info_window));
        iw->info_type = -1;
        iw->info_data = NULL;
	iw->remote_account = remote;
 	iw->local_user = local;

	iw->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

        gtk_window_set_policy(GTK_WINDOW(iw->window), TRUE, TRUE, TRUE);
        gtk_widget_realize(iw->window);

	iw->info = gtk_html_new(NULL,NULL);
	gtk_html_set_editable(GTK_HTML(iw->info), FALSE);
	gtk_html_set_transparent(GTK_HTML(iw->info), FALSE);
        iw->scrollwindow = gtk_scrolled_window_new(NULL,NULL);

        gtk_widget_realize(iw->window);	
	gtk_window_set_title(GTK_WINDOW(iw->window), remote->handle);
	eb_icon(iw->window->window);

        gtk_widget_set_usize(iw->scrollwindow, 375, 150);
	gtk_container_add(GTK_CONTAINER(iw->scrollwindow),iw->info);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(iw->scrollwindow),GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);

	gtk_box_pack_start(GTK_BOX(vbox), iw->scrollwindow, TRUE,TRUE, 5);
	gtk_widget_show(iw->scrollwindow);

        gtk_container_set_border_width(GTK_CONTAINER(iw->window), 5);

        gtk_signal_connect (GTK_OBJECT (iw->window), "destroy", GTK_SIGNAL_FUNC (iw_destroy_event), iw);

        icon = gdk_pixmap_create_from_xpm_d(iw->window->window, &mask, NULL, cancel_xpm);
	iconwid = gtk_pixmap_new(icon, mask);
	gtk_widget_show(iconwid);

        ok_button = gtk_button_new ();
        gtk_signal_connect (GTK_OBJECT (ok_button), "clicked", GTK_SIGNAL_FUNC (iw_close_win), iw);

        gtk_box_pack_start (GTK_BOX (buttonbox), iconwid,TRUE,TRUE,0);
        label = gtk_label_new("Close");
        gtk_box_pack_start (GTK_BOX (buttonbox), label,TRUE,TRUE,5);
        gtk_widget_show(buttonbox);
        gtk_container_add(GTK_CONTAINER(ok_button), buttonbox);
 
        gtk_box_pack_start(GTK_BOX(hbox), ok_button, TRUE,FALSE, 0);
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE,FALSE, 5);

	gtk_container_add(GTK_CONTAINER(iw->window), vbox);
        gtk_widget_show(iw->info);
        gtk_widget_show(label);
        gtk_widget_show(ok_button);
        gtk_widget_show(hbox);
 
        gtk_widget_show(vbox);
        gtk_widget_show(iw->window);

        return iw;
}


/*This is a very ugly hack that sorta works, there must be a better solution...*/
void clear_info_window(info_window *iw) {
     GtkHtml * html = (GtkHtml *)iw->info;
	 GtkHtmlBit * hb;
	 GList * hbits;
     GdkRectangle rect;
     gint width, height;
     gdk_window_get_size(GTK_WIDGET(html)->window, &width, &height);
     rect.x = 0;
     rect.y = 0;
     rect.width = width;
     rect.height = height;

     html->current_x = 0;
     html->current_y = 0;

	 hbits = html->html_bits;

	 while(hbits)
	 {
		 hb = (GtkHtmlBit *)hbits->data;
		 if(hb->fore)
			 gdk_color_free(hb->fore);
		 if(hb->back)
			 gdk_color_free(hb->back);
		 if(hb->text)
			 g_free(hb->text);
		 if(hb->url)
			 g_free(hb->url);
		 if(hb->pm)
			 gdk_pixmap_unref(hb->pm);

		 g_free(hb);

		 hbits = hbits->next;
	}
	 if(html->html_bits);
	 	g_list_free(html->html_bits);
	 if(html->urls)
		 g_list_free(html->urls);

	 html->html_bits=NULL;
	 html->urls=NULL;

     gdk_window_clear_area(GTK_WIDGET(html)->window, rect.x, rect.y, rect.width, rect.height);
//need to cause the window to repaint......
}


void eb_info_window_add_info( eb_account * remote_account, gchar* text ) {
//        struct contact * remote_contact = remote_account->account_contact;
	
	if(remote_account->infowindow)
	{
    gtk_html_append_text(GTK_HTML(remote_account->infowindow->info), text,0);
  }
}

