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

#include "skipstone.h"
#include "pixmaps/home.xpm"
#include "pixmaps/back.xpm"
#include "pixmaps/reload.xpm"
#include "pixmaps/stop.xpm"
#include "pixmaps/forward.xpm"

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


/* make the menu here, so its easier to change/update */
void
make_menu(Bdata *bdata,GtkWidget *menubar,GtkAccelGroup *accelgroup)
{
	GtkWidget *file, *filemenu, *help, *helpmenu, *about;
	GtkWidget *close, *new_window, *open, *sep1, *close_win;
	GtkWidget *pref, *prefmenu;
	GtkWidget *toolbar, *toolbarmenu;
	GtkWidget *options,*pixmap,*text,*both, *sep2;
     	GtkWidget *cb_bmarktoolbar;
	GSList *radio_buttons = NULL;


	file = gtk_menu_item_new_with_label("File");
	gtk_menu_bar_append(GTK_MENU_BAR(menubar), file);

	filemenu = gtk_menu_new();
	gtk_menu_item_set_submenu(GTK_MENU_ITEM(file),filemenu);

	new_window = gtk_menu_item_new_with_label("New Window");
	gtk_signal_connect(GTK_OBJECT(new_window), "activate", GTK_SIGNAL_FUNC(blank_new_window), NULL);
	gtk_widget_add_accelerator(new_window,"activate",accelgroup,GDK_n,GDK_MOD1_MASK,GTK_ACCEL_VISIBLE);
	gtk_menu_append(GTK_MENU(filemenu), new_window);
	
	open = gtk_menu_item_new_with_label("Open in a New Window");
	gtk_signal_connect(GTK_OBJECT(open), "activate", GTK_SIGNAL_FUNC(open_in_new_window), NULL);
	gtk_widget_add_accelerator(open,"activate",accelgroup,GDK_o,GDK_MOD1_MASK,GTK_ACCEL_VISIBLE);
	gtk_menu_append(GTK_MENU(filemenu), open);     

	close_win = gtk_menu_item_new_with_label("Close ...");
	gtk_signal_connect(GTK_OBJECT(close_win), "activate", GTK_SIGNAL_FUNC(close_window), bdata);
	gtk_widget_add_accelerator(close_win,"activate",accelgroup,GDK_w,GDK_MOD1_MASK,GTK_ACCEL_VISIBLE);
	gtk_menu_append(GTK_MENU(filemenu), close_win);

	sep1 = gtk_menu_item_new();
	gtk_menu_append(GTK_MENU(filemenu), sep1);

	close = gtk_menu_item_new_with_label("Exit ...");
	gtk_signal_connect(GTK_OBJECT(close), "activate", GTK_SIGNAL_FUNC(exit_app), NULL);
	gtk_widget_add_accelerator(close,"activate",accelgroup,GDK_x,GDK_MOD1_MASK,GTK_ACCEL_VISIBLE);
	gtk_menu_append(GTK_MENU(filemenu), close);


	pref = gtk_menu_item_new_with_label("Preferences");
	gtk_menu_bar_append(GTK_MENU_BAR(menubar),pref);
	
	prefmenu = gtk_menu_new();
	gtk_menu_item_set_submenu(GTK_MENU_ITEM(pref),prefmenu);

	options = gtk_menu_item_new_with_label("Options");
	gtk_signal_connect(GTK_OBJECT(options),"activate",GTK_SIGNAL_FUNC(open_config_dialog),NULL);
	gtk_menu_append(GTK_MENU(prefmenu),options);
	
	sep2 = gtk_menu_item_new();
	gtk_menu_append(GTK_MENU(prefmenu), sep2);
	
	toolbar = gtk_menu_item_new_with_label("Toolbar");
	gtk_menu_append(GTK_MENU(prefmenu),toolbar);

	toolbarmenu = gtk_menu_new();
	gtk_menu_item_set_submenu(GTK_MENU_ITEM(toolbar),toolbarmenu);

	pixmap = gtk_radio_menu_item_new_with_label(radio_buttons,"Pixmaps");
	radio_buttons = gtk_radio_menu_item_group(GTK_RADIO_MENU_ITEM(pixmap));
	gtk_signal_connect(GTK_OBJECT(pixmap),"toggled",GTK_SIGNAL_FUNC(change_layout),GINT_TO_POINTER(0));
	gtk_menu_append(GTK_MENU(toolbarmenu),pixmap);

	text = gtk_radio_menu_item_new_with_label(radio_buttons,"Text");
	radio_buttons = gtk_radio_menu_item_group(GTK_RADIO_MENU_ITEM(text));
	gtk_signal_connect(GTK_OBJECT(text),"toggled",GTK_SIGNAL_FUNC(change_layout),GINT_TO_POINTER(1));
	gtk_menu_append(GTK_MENU(toolbarmenu),text);

	both = gtk_radio_menu_item_new_with_label(radio_buttons,"Both");
	radio_buttons = gtk_radio_menu_item_group(GTK_RADIO_MENU_ITEM(both));
	gtk_signal_connect(GTK_OBJECT(both),"toggled",GTK_SIGNAL_FUNC(change_layout),GINT_TO_POINTER(2));
	gtk_menu_append(GTK_MENU(toolbarmenu),both);
	
	cb_bmarktoolbar = gtk_check_menu_item_new_with_label("Hide Bookmark toolbar");
     	gtk_check_menu_item_set_show_toggle(GTK_CHECK_MENU_ITEM(cb_bmarktoolbar), TRUE);
     	gtk_signal_connect(GTK_OBJECT(cb_bmarktoolbar), "toggled", GTK_SIGNAL_FUNC(hide_bookmark_toolbar), bdata);
     
     	gtk_menu_append(GTK_MENU(prefmenu), cb_bmarktoolbar);
     
     	bdata->bookmark_menubar_item = gtk_menu_item_new_with_label("Bookmarks");
	gtk_menu_bar_append(GTK_MENU_BAR(menubar),bdata->bookmark_menubar_item);

	bdata->bookmark_toolbar_widgets = NULL;

	generate_bookmark_ui(bdata);

     	help = gtk_menu_item_new_with_label("Help");
	gtk_menu_bar_append(GTK_MENU_BAR(menubar), help);

	helpmenu = gtk_menu_new();
	gtk_menu_item_set_submenu(GTK_MENU_ITEM(help),helpmenu);
	
	about = gtk_menu_item_new_with_label("About SkipStone");
     	gtk_signal_connect(GTK_OBJECT(about), "activate", GTK_SIGNAL_FUNC(about_skipstone), NULL);
	gtk_menu_append(GTK_MENU(helpmenu), about);
     
	if (config.layout == 0) {
		gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(pixmap),TRUE);
	} else if (config.layout == 1) {
		gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(text),TRUE);
	} else {
		gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(both),TRUE);
	}
       	if (config.bmarktbar == 0) {
	     	gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(cb_bmarktoolbar),TRUE);
	}

	gtk_widget_show_all(menubar);
	return;
}

void
  make_window(const gchar *url)
{
	Bdata *bdata = make_browser();
     
     	
     	if (url) { /* do I have an url to load */
	  	gtk_moz_embed_load_url(bdata->embed, url);
     	  	gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(bdata->combo)->entry), url);
     	} else {
		gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(bdata->combo)->entry),config.home);
     		gtk_moz_embed_load_url(bdata->embed, config.home);
     	}
	return;
}

Bdata *
make_browser(void)
{
     	Bdata *bdata; /* the browser strcut */
     	GtkAccelGroup *accelgroup;
     	GtkWidget *vbox, *hbox, *menubar, *menuhandlebox;
     	GtkWidget *back, *home, *forward, *stop, *reload;
     	GdkPixmap *back_pixmap, *home_pixmap, *forward_pixmap, *stop_pixmap, *reload_pixmap;
     	GdkBitmap *back_bitmap, *home_bitmap, *forward_bitmap, *stop_bitmap, *reload_bitmap;
     	GdkColormap *colormap;
	gchar *win_title;


     	win_title = g_strdup_printf ("SkipStone %d.%d.%d",SKIPSTONE_MAJOR,SKIPSTONE_MINOR,SKIPSTONE_MICRO);
 

    	bdata = g_new0(Bdata,1);
	
     	bdata->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
     	gtk_window_set_title(GTK_WINDOW(bdata->window), win_title);

     	g_free(win_title);

     	gtk_window_set_default_size(GTK_WINDOW(bdata->window),config.xsize,config.ysize);
     	gtk_window_set_policy(GTK_WINDOW(bdata->window), TRUE, TRUE, FALSE);
     	gtk_widget_realize(bdata->window);

     	accelgroup = gtk_accel_group_new();
     	gtk_window_add_accel_group(GTK_WINDOW(bdata->window), accelgroup);

     	vbox = gtk_vbox_new(0,0);
     	gtk_container_add(GTK_CONTAINER(bdata->window), vbox);

     	menubar = gtk_menu_bar_new();
     	bdata->handlebox = gtk_handle_box_new();

	bdata->bookmark_toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,GTK_TOOLBAR_TEXT);
     	gtk_toolbar_set_space_size(GTK_TOOLBAR(bdata->bookmark_toolbar),5);
     	gtk_toolbar_set_button_relief(GTK_TOOLBAR(bdata->bookmark_toolbar), GTK_RELIEF_NONE);
     	gtk_container_set_border_width(GTK_CONTAINER(bdata->bookmark_toolbar),2);
     	
     	bdata->bmarkhandlebox = gtk_handle_box_new();
     	make_menu(bdata,menubar,accelgroup);
	

	gtk_container_add(GTK_CONTAINER(bdata->bmarkhandlebox),bdata->bookmark_toolbar);

     	menuhandlebox = gtk_handle_box_new ();
     	gtk_container_add(GTK_CONTAINER(menuhandlebox), menubar);
     	
     	gtk_box_pack_start(GTK_BOX(vbox), menuhandlebox, FALSE, FALSE, FALSE);
     	gtk_box_pack_start(GTK_BOX(vbox), bdata->handlebox, FALSE, FALSE, FALSE);
	gtk_box_pack_start(GTK_BOX(vbox), bdata->bmarkhandlebox,FALSE,FALSE,0);

     	/* toolbar and pixmaps */
     	bdata->toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,GTK_TOOLBAR_BOTH);
     	gtk_toolbar_set_button_relief(GTK_TOOLBAR(bdata->toolbar), GTK_RELIEF_NONE);
     	gtk_container_set_border_width(GTK_CONTAINER(bdata->toolbar),5);
     	gtk_toolbar_set_space_size(GTK_TOOLBAR(bdata->toolbar),5);
     	gtk_container_add(GTK_CONTAINER(bdata->handlebox), bdata->toolbar);


     	colormap = gtk_widget_get_colormap(bdata->window);


     	back_pixmap = gdk_pixmap_colormap_create_from_xpm_d(NULL, colormap, &back_bitmap, NULL, back_xpm);
     	back = gtk_pixmap_new (back_pixmap, back_bitmap);
     	gtk_toolbar_append_item(GTK_TOOLBAR(bdata->toolbar), "Back","Go back to previous page","back func", back, GTK_SIGNAL_FUNC(on_back),bdata);
     	gtk_toolbar_append_space(GTK_TOOLBAR(bdata->toolbar));

     	forward_pixmap = gdk_pixmap_colormap_create_from_xpm_d(NULL, colormap, &forward_bitmap, NULL, forward_xpm);
     	forward = gtk_pixmap_new (forward_pixmap, forward_bitmap);
     	gtk_toolbar_append_item(GTK_TOOLBAR(bdata->toolbar), "Forward", "Go Forward","forward func", forward, GTK_SIGNAL_FUNC(on_forward), bdata);
     	gtk_toolbar_append_space(GTK_TOOLBAR(bdata->toolbar));

     	reload_pixmap = gdk_pixmap_colormap_create_from_xpm_d(NULL, colormap, &reload_bitmap, NULL, reload_xpm);
     	reload = gtk_pixmap_new(reload_pixmap,reload_bitmap);
     	gtk_toolbar_append_item(GTK_TOOLBAR(bdata->toolbar), "Reload","Reloads current Page","stop func", reload, GTK_SIGNAL_FUNC(on_reload), bdata);
     	gtk_toolbar_append_space(GTK_TOOLBAR(bdata->toolbar));

     	stop_pixmap = gdk_pixmap_colormap_create_from_xpm_d(NULL, colormap, &stop_bitmap, NULL, stop_xpm);
     	stop = gtk_pixmap_new(stop_pixmap,stop_bitmap);
     	gtk_toolbar_append_item(GTK_TOOLBAR(bdata->toolbar), "Stop","Stops Loading","stop func", stop, GTK_SIGNAL_FUNC(on_stop), bdata);
     	gtk_toolbar_append_space(GTK_TOOLBAR(bdata->toolbar));

     	bdata->combo = gtk_combo_new();
	if (combo_urls !=NULL) {	
     		gtk_combo_set_popdown_strings(GTK_COMBO(bdata->combo),combo_urls);
	}
	gtk_combo_disable_activate(GTK_COMBO(bdata->combo));
     	gtk_widget_set_usize(GTK_WIDGET(bdata->combo), 400,30);
     	gtk_toolbar_append_widget(GTK_TOOLBAR(bdata->toolbar), bdata->combo, "Enter a URL here",NULL);
     	gtk_toolbar_append_space(GTK_TOOLBAR(bdata->toolbar));


     	home_pixmap = gdk_pixmap_colormap_create_from_xpm_d(NULL,colormap,&home_bitmap,NULL,home_xpm);
     	home = gtk_pixmap_new(home_pixmap,home_bitmap);
     	gtk_toolbar_append_item(GTK_TOOLBAR(bdata->toolbar), "Home","Go to Homepage","home func", home, GTK_SIGNAL_FUNC(go_home), bdata);
     	gtk_toolbar_append_space(GTK_TOOLBAR(bdata->toolbar));

     	/*   unref toolbar pixmaps    */
     	gdk_pixmap_unref(home_pixmap);
     	gdk_pixmap_unref(back_pixmap);
     	gdk_pixmap_unref(forward_pixmap);
     	gdk_pixmap_unref(reload_pixmap);
     	gdk_pixmap_unref(stop_pixmap);
     	gdk_bitmap_unref(forward_bitmap);
     	gdk_bitmap_unref(back_bitmap);
     	gdk_bitmap_unref(home_bitmap);
     	gdk_bitmap_unref(stop_bitmap);
     	gdk_bitmap_unref(reload_bitmap);     
     	/*=============================*/


     	bdata->embed = GTK_MOZ_EMBED (gtk_moz_embed_new());
     	gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(bdata->embed), TRUE, TRUE, FALSE);

     	hbox = gtk_hbox_new(0,0);
     	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, FALSE);

     	bdata->statusbar = gtk_statusbar_new();
     	gtk_box_pack_start(GTK_BOX(hbox), bdata->statusbar, TRUE, TRUE, FALSE);

     	bdata->progressbar = gtk_progress_bar_new();
     	gtk_box_pack_end(GTK_BOX(hbox), bdata->progressbar, FALSE,FALSE,FALSE);
     	gtk_progress_set_activity_mode(GTK_PROGRESS(bdata->progressbar), 1);


     	/* start signals */
     	gtk_signal_connect(GTK_OBJECT(bdata->window), "destroy", GTK_SIGNAL_FUNC(destroy_window_callback), bdata);
     	gtk_signal_connect(GTK_OBJECT(GTK_COMBO(bdata->combo)->entry), "activate", GTK_SIGNAL_FUNC(on_entry),  bdata);
	gtk_signal_connect(GTK_OBJECT(GTK_COMBO(bdata->combo)->popwin),"hide",GTK_SIGNAL_FUNC(on_list_select),bdata);
     	gtk_signal_connect(GTK_OBJECT(bdata->embed), "net_start", GTK_SIGNAL_FUNC(start_load), bdata);
     	gtk_signal_connect(GTK_OBJECT(bdata->embed), "link_message", GTK_SIGNAL_FUNC(link_message), bdata);
     	gtk_signal_connect(GTK_OBJECT(bdata->embed), "title", GTK_SIGNAL_FUNC(title_changed), bdata);
     	gtk_signal_connect(GTK_OBJECT(bdata->embed), "location", GTK_SIGNAL_FUNC(url_clicked), bdata);
     	gtk_signal_connect(GTK_OBJECT(bdata->embed), "net_stop", GTK_SIGNAL_FUNC(stop_load), bdata);
	gtk_signal_connect(GTK_OBJECT(bdata->embed), "new_window", GTK_SIGNAL_FUNC(moz_new_window_cb), bdata);
        gtk_signal_connect(GTK_OBJECT(bdata->embed), "open_uri", GTK_SIGNAL_FUNC(handle_open_uri), bdata);
	gtk_signal_connect(GTK_OBJECT(bdata->embed), "net_state", GTK_SIGNAL_FUNC(net_state_cb), bdata);
/* 
	gtk_signal_connect(GTK_OBJECT(bdata->window),"button_press_event",GTK_SIGNAL_FUNC(button_press),bdata);
	gtk_signal_connect(GTK_OBJECT(bdata->window),"button_release_event",GTK_SIGNAL_FUNC(button_press),bdata);
*/

     	/* End signals */
    	
     	gtk_widget_show_all(bdata->window);
     	gtk_toolbar_set_style(GTK_TOOLBAR(bdata->toolbar), config.layout);
     	gtk_widget_queue_resize(GTK_WIDGET(bdata->handlebox));	
     	if (config.bmarktbar == 0) {
	     	gtk_widget_hide(bdata->bmarkhandlebox);
	}
     	window_count = g_slist_append(window_count,bdata);
     	return bdata;
}

GtkWidget * get_popup_menu(Bdata *bdata)
{
             GtkWidget *menu, *m1,*m2,*m3;
             menu = gtk_menu_new();
             m1 = gtk_menu_item_new_with_label("Open in this Window");
             gtk_signal_connect(GTK_OBJECT(m1), "activate", GTK_SIGNAL_FUNC(browse_in_window_from_popup), bdata);
     	     gtk_menu_append(GTK_MENU(menu), m1);
             m2 = gtk_menu_item_new_with_label("Open in a new Window");
             gtk_menu_append(GTK_MENU(menu), m2);
             gtk_signal_connect(GTK_OBJECT(m2), "activate", GTK_SIGNAL_FUNC(make_window_from_popup), bdata);
       	     m3  = gtk_menu_item_new_with_label("Download Link...");
     	     gtk_signal_connect(GTK_OBJECT(m3), "activate", GTK_SIGNAL_FUNC(download_link_from_popup), bdata);
     	     gtk_menu_append(GTK_MENU(menu), m3);
     	     gtk_widget_show_all(menu);
             return menu;
             
}
