/* copyright 	Maher Awamy <muhri@muhri.net>		*
 *		Charlie Schmidt <ishamael@themes.org>	*
 *							*
 *  released under the GPL				*/


#include "skipstone.h"
#include "pixmaps/logo.xpm"

extern GSList *window_count;
extern ConfigData config;
extern GList *combo_urls;


void 
blank_new_window(GtkWidget *menuitem,gpointer data)
{
	make_window(NULL);
	return;
}


void
open_in_new_window(GtkWidget *menuitem,gpointer data) 
{
	DialogData *ddata;
	GtkWidget *ok, *cancel, *label;

	ddata = g_new0(DialogData,1);
	ddata->dialog = gtk_dialog_new();
	gtk_window_set_title(GTK_WINDOW(ddata->dialog), "Enter URL");
	gtk_window_set_default_size(GTK_WINDOW(ddata->dialog), 250,70);
	gtk_window_set_position(GTK_WINDOW(ddata->dialog), GTK_WIN_POS_MOUSE);
	gtk_signal_connect(GTK_OBJECT(ddata->dialog), "destroy", GTK_SIGNAL_FUNC(destroy_dialog), ddata);
	ddata->entry = gtk_entry_new();     
	label = gtk_label_new("Enter URL:");
	ok = gtk_button_new_with_label ("Ok");
	cancel = gtk_button_new_with_label("Cancel");

	gtk_signal_connect(GTK_OBJECT(ok), "clicked", GTK_SIGNAL_FUNC(on_url_entry_ok), ddata);
	gtk_signal_connect(GTK_OBJECT(ddata->entry), "activate", GTK_SIGNAL_FUNC(on_url_entry_ok), ddata);
	gtk_signal_connect(GTK_OBJECT(cancel), "clicked", GTK_SIGNAL_FUNC(destroy_dialog), ddata);

	gtk_box_pack_start(GTK_BOX(GTK_DIALOG (ddata->dialog)->vbox), label, FALSE, FALSE, FALSE);
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG (ddata->dialog)->vbox), ddata->entry, FALSE, FALSE, FALSE);
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG (ddata->dialog)->action_area), ok, TRUE, TRUE, FALSE);
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG (ddata->dialog)->action_area), cancel, TRUE, TRUE, FALSE);

	gtk_widget_show_all(ddata->dialog);
	gtk_widget_grab_focus(ddata->entry);
 
    	return;
}


void
close_window(GtkWidget *menuitem,Bdata *bdata) 
{
	gtk_widget_destroy(bdata->window);
	return;
}



void 
exit_app(GtkWidget *menuitem,gpointer data)
{
	quit_skipstone();
	return;
}



void
open_config_dialog(GtkWidget *menuitem,gpointer data)
{
	Cdata *config_window;
	GtkWidget *mailer_label, *home_label, *xsize_label, *ysize_label, *download_label, *maxpopup_label;
        GtkWidget *vbox, *home_hbox, *width_hbox, *buttonbox, *email_hbox, *download_hbox, *maxpopupbox;
      	GtkWidget *notebook, *page1_label, *page1_vbox, *page2_label, *page2_vbox,
       		  *page3_label, *page3_vbox;
     	
     	config_window = g_new0(Cdata,1);
	config_window->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(config_window->window), "SkipStone configuration");
     	notebook = gtk_notebook_new();
     
	vbox = gtk_vbox_new(0,5);	
     	gtk_container_add(GTK_CONTAINER(config_window->window),vbox);
     	gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
     	gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, FALSE);
     	
     	/* page 1 */
     	page1_label = gtk_label_new("Home");
     	page1_vbox = gtk_vbox_new(0,5);
     	gtk_container_set_border_width(GTK_CONTAINER(page1_vbox), 5);
     	gtk_notebook_append_page(GTK_NOTEBOOK(notebook),page1_vbox,page1_label);	
     	home_label = gtk_label_new("Home page: ");
     	gtk_widget_set_usize(home_label,100,10);
     	config_window->home_entry = gtk_entry_new();
	gtk_entry_set_text(GTK_ENTRY(config_window->home_entry),config.home);
	home_hbox = gtk_hbox_new(0,0);
     	gtk_box_pack_start(GTK_BOX(page1_vbox), home_hbox, TRUE, TRUE, FALSE);
     	gtk_box_pack_start(GTK_BOX(home_hbox), home_label, FALSE, FALSE, FALSE);
     	gtk_box_pack_start(GTK_BOX(home_hbox), config_window->home_entry, FALSE, FALSE, FALSE);

     	
     	/* page 2 */
     	page2_label = gtk_label_new("Helper apps");
     	page2_vbox = gtk_vbox_new(0,5);
     	gtk_container_set_border_width(GTK_CONTAINER(page2_vbox), 5);
     	gtk_notebook_append_page(GTK_NOTEBOOK(notebook), page2_vbox, page2_label);
     	mailer_label = gtk_label_new("Mailer: ");
     	gtk_widget_set_usize(mailer_label,100,10);
     	config_window->email_entry = gtk_entry_new();
     	gtk_entry_set_text(GTK_ENTRY(config_window->email_entry),config.mailer);
	email_hbox = gtk_hbox_new(0,0);
     	gtk_box_pack_start(GTK_BOX(page2_vbox), email_hbox, TRUE, TRUE, FALSE);
     	gtk_box_pack_start(GTK_BOX(email_hbox), mailer_label, FALSE, FALSE, FALSE);
     	gtk_box_pack_start(GTK_BOX(email_hbox), config_window->email_entry, FALSE, FALSE, FALSE);
     	
     	download_label = gtk_label_new("Download With: ");
     	gtk_widget_set_usize(download_label,100,10);
     	config_window->downloader_entry = gtk_entry_new();
     	gtk_entry_set_text(GTK_ENTRY(config_window->downloader_entry), config.downloader);
     	
     	download_hbox = gtk_hbox_new(0,0);
     	gtk_box_pack_start(GTK_BOX(page2_vbox), download_hbox, TRUE, TRUE, FALSE);
     	gtk_box_pack_start(GTK_BOX(download_hbox), download_label, FALSE, FALSE, FALSE);
     	gtk_box_pack_start(GTK_BOX(download_hbox), config_window->downloader_entry, FALSE, FALSE, FALSE);
     	
     	/* page 3 */
     	page3_label = gtk_label_new("Misc");
     	page3_vbox = gtk_vbox_new(0,5);
     	gtk_container_set_border_width(GTK_CONTAINER(page3_vbox), 5);
     	gtk_notebook_append_page(GTK_NOTEBOOK(notebook), page3_vbox, page3_label);
     
     	maxpopup_label = gtk_label_new("Maximum popup entries: ");
     	config_window->maxpopups_adj = gtk_adjustment_new(config.maxpopupitems,5,50,1,1,1);
     	config_window->maxpopups = gtk_spin_button_new(GTK_ADJUSTMENT(config_window->maxpopups_adj),1,0);
     	maxpopupbox = gtk_hbox_new(0,0);
     	gtk_box_pack_start(GTK_BOX(page3_vbox), maxpopupbox, TRUE, TRUE, FALSE);
	gtk_box_pack_start(GTK_BOX(maxpopupbox), maxpopup_label, FALSE, FALSE, FALSE);
     	gtk_box_pack_start(GTK_BOX(maxpopupbox), config_window->maxpopups ,FALSE, FALSE, FALSE);
     
     
	xsize_label = gtk_label_new("Window width: ");
	config_window->xsize_sbutton_adj = gtk_adjustment_new(config.xsize,100,1600,1,1,1);
	config_window->xsize_sbutton = gtk_spin_button_new(GTK_ADJUSTMENT(config_window->xsize_sbutton_adj),1,0);
	width_hbox = gtk_hbox_new(0,0);
     	gtk_box_pack_start(GTK_BOX(page3_vbox), width_hbox, TRUE, TRUE, FALSE);
     	gtk_box_pack_start(GTK_BOX(width_hbox), xsize_label, FALSE, FALSE, FALSE);
     	gtk_box_pack_start(GTK_BOX(width_hbox), config_window->xsize_sbutton, FALSE, FALSE, FALSE);

	ysize_label = gtk_label_new("Window height: ");
	config_window->ysize_sbutton_adj = gtk_adjustment_new(config.ysize,100,1200,1,1,1);
	config_window->ysize_sbutton = gtk_spin_button_new(GTK_ADJUSTMENT(config_window->ysize_sbutton_adj),1,0);
	
     	gtk_box_pack_start(GTK_BOX(width_hbox), ysize_label, FALSE, FALSE, FALSE);
     	gtk_box_pack_start(GTK_BOX(width_hbox), config_window->ysize_sbutton, FALSE, FALSE, FALSE);

	config_window->ok_button = gtk_button_new_with_label("Ok");
	config_window->cancel_button = gtk_button_new_with_label("Cancel");
	gtk_widget_set_usize(config_window->ok_button,55,22);
     	gtk_widget_set_usize(config_window->cancel_button,55,22);
     
	buttonbox = gtk_hbox_new(0,0);
     	gtk_box_pack_start(GTK_BOX(vbox), buttonbox, FALSE, FALSE, FALSE);
     	gtk_box_pack_start(GTK_BOX(buttonbox), config_window->ok_button, TRUE, TRUE, FALSE);
     	gtk_box_pack_start(GTK_BOX(buttonbox), config_window->cancel_button, TRUE, TRUE, FALSE);
     
	gtk_widget_show_all(config_window->window);

	gtk_signal_connect(GTK_OBJECT(config_window->window),"destroy",GTK_SIGNAL_FUNC(destroy_config_window),config_window);
	gtk_signal_connect(GTK_OBJECT(config_window->cancel_button),"clicked",GTK_SIGNAL_FUNC(close_config_window),config_window);
	gtk_signal_connect(GTK_OBJECT(config_window->ok_button),"clicked",GTK_SIGNAL_FUNC(save_config),config_window);

	return;
}



void 
change_layout(GtkWidget *menuitem,gpointer data) 
{
	config.layout = GPOINTER_TO_INT(data);
	update_layout();
	return;
}


void
destroy_window_callback(GtkWidget *window,Bdata *bdata) 
{
	gtk_progress_set_activity_mode(GTK_PROGRESS(bdata->progressbar),0);
	if (bdata->timer) {
		gtk_timeout_remove(bdata->timer);
	}

	window_count = g_slist_remove(window_count,bdata);

	if (g_slist_length(window_count) == 0) {
		quit_skipstone();
	}
	g_free(bdata->link);
     	g_free(bdata);
	return;
}



void
on_entry(GtkWidget *entry,Bdata *bdata) 
{
     	const gchar *url;
   	gchar *curl;

	url = gtk_entry_get_text(GTK_ENTRY(entry));
	gtk_moz_embed_load_url(bdata->embed,url);
     	curl = g_strdup(url);
	insert_in_combo(curl,bdata);

     	
     	return;
}


	
void
on_list_select(GtkWidget *combo,Bdata *bdata) 
{
	const gchar *url;

	url = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(bdata->combo)->entry));
	gtk_moz_embed_load_url(bdata->embed,url);
	return;
}


void
destroy_config_window(GtkWidget *window,Cdata *config_window) 
{
	g_free(config_window);
	return;
}

void close_config_window(GtkWidget* button, Cdata *config_window) {
     gtk_widget_destroy(config_window->window);
     return;
}

void
save_config(GtkWidget *button,Cdata *config_window) 
{
	g_free(config.home);
	g_free(config.mailer);
     	g_free(config.downloader);
     
	config.home = g_strdup(gtk_entry_get_text(GTK_ENTRY(config_window->home_entry)));
	config.xsize = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(config_window->xsize_sbutton));
	config.ysize = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(config_window->ysize_sbutton));
	config.mailer = g_strdup(gtk_entry_get_text(GTK_ENTRY(config_window->email_entry)));
	config.downloader = g_strdup(gtk_entry_get_text(GTK_ENTRY(config_window->downloader_entry)));       
	config.maxpopupitems = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(config_window->maxpopups));
     	
     	write_config();

	gtk_widget_destroy(config_window->window);
	return;
}


void
destroy_dialog(GtkWidget *dialog,DialogData *ddata)
{
	gtk_widget_destroy(ddata->dialog);
	g_free(ddata);
	return;
}


void
on_url_entry_ok(GtkWidget *button,DialogData *ddata)
{
     	gchar *url;

     	url = gtk_entry_get_text(GTK_ENTRY(ddata->entry));
     	if (*url) {
     		make_window(url);		
		destroy_dialog(NULL, ddata);
     	} 
     	return;
}


void
on_back(GtkWidget *button,Bdata *bdata) 
{
	gtk_moz_embed_go_back(bdata->embed);
	return;
}


void
on_forward(GtkWidget *button,Bdata *bdata) 
{
	gtk_moz_embed_go_forward(bdata->embed);
	return;
}


void
on_reload(GtkWidget *button, Bdata *bdata)
{
     	gtk_moz_embed_reload(bdata->embed,0);
     	return;
}


void
on_stop(GtkWidget *button, Bdata *bdata)
{
     	gtk_moz_embed_stop_load(bdata->embed);
	return;
}


void
go_home(GtkWidget *button, Bdata *bdata)
{
     	gtk_moz_embed_load_url(bdata->embed,config.home);
     	return;
}

/* create_dialog(sometitle, sometext) creates a dialog with an ok button 
 * gboolean - if true then the logo is passed also.*/
void
  create_dialog(gchar *title, gchar *labeltext, gint logo)
{
     	GtkWidget *dialog, *ok, *label, *logo_pic;
     	GdkPixmap *logo_pixmap;
     	GdkBitmap *logo_bitmap;
     	GdkColormap *colormap;
     	
     	dialog = gtk_dialog_new();
     	label = gtk_label_new(labeltext);
     	gtk_label_set_line_wrap(GTK_LABEL(label), 1);
     	ok = gtk_button_new_with_label("Ok!");
     	gtk_widget_set_usize(ok, 55, 25);
     	gtk_window_set_title(GTK_WINDOW(dialog), title);
     	gtk_window_set_default_size(GTK_WINDOW(dialog), 150, 100);
     	gtk_container_set_border_width(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), 5);
     	if (logo) {
	     	colormap = gtk_widget_get_colormap(dialog);
	     	logo_pixmap = gdk_pixmap_colormap_create_from_xpm_d(NULL, colormap, &logo_bitmap, NULL, logo_xpm);
	     	logo_pic = gtk_pixmap_new(logo_pixmap, logo_bitmap);
	     	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), logo_pic, TRUE, TRUE, 5);
	     	gdk_pixmap_unref(logo_pixmap);
	     	gdk_bitmap_unref(logo_bitmap);
	}
     	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, TRUE, TRUE, 5);
     	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), ok, FALSE, FALSE, 5);
     	gtk_signal_connect(GTK_OBJECT(ok), "clicked", GTK_SIGNAL_FUNC(close_generic_dialog), dialog);
     	gtk_widget_show_all(dialog);
	return;
}
/* callback to destroy the generic dialog */
void close_generic_dialog(GtkWidget *button, GtkWidget *dialog)
{
     	gtk_widget_destroy(dialog);
	return;
}

void about_skipstone(GtkWidget *menuitem)
{
     	gchar *title = g_strdup_printf("About SkipStone %d.%d.%d", SKIPSTONE_MAJOR, SKIPSTONE_MINOR, SKIPSTONE_MICRO);
     	gchar *abouttext = "SkipStone is a light Gtk+ web browser\nthat embeds Mozilla through GtkMozEmbed\nCopyright(c)\n\tMaher Awamy <muhri@muhri.net>\n\tCharlie Schmidt <ishamael@themes.org>";
       	create_dialog(title,abouttext,1);
     	g_free(title);
     	return;
}

void
  hide_bookmark_toolbar(GtkCheckMenuItem *cb_menuitem, Bdata *bdata)
{
     gint i;
     if (cb_menuitem->active) {
	     	gtk_widget_hide(bdata->bmarkhandlebox);
		config.bmarktbar = 0;
     	} else {
		gtk_widget_show(bdata->bmarkhandlebox);
		config.bmarktbar = 1;
	}
	if (window_count !=NULL) {
	    for (i = 0; i < g_slist_length(window_count); i++) { 	
		 	Bdata *bdata = g_slist_nth_data(window_count,i);
		 	if (config.bmarktbar == 0) {
		 		gtk_widget_hide(bdata->bmarkhandlebox);
			} else {
			     	gtk_widget_show(bdata->bmarkhandlebox);
			}
	    }
	}
     	return;
}
