//      bfe2 - main
//      Copyright (c) 1999-2003 Brand Huntsman
//


#include "common.h"
#include "functions.h"


//////////////////////////////////////////////////////////////////////////

// global
GtkWidget *bochs_button, *refreshall_button, *notebook;
uint show_state;

// local
GtkWidget *state_hbox, *quit_window, *state_button;
static guint id;

//////////////////////////////////////////////////////////////////////////


void destroy_window( GtkWidget *widget, gpointer data ){
	bfe_quit(NULL);
}


void toggle_state( GtkWidget *widget, gpointer data ){
	if(show_state){
		gtk_widget_hide(state_hbox);
		gtk_object_set(GTK_OBJECT(state_button), "label", " Show State ", NULL);
		show_state = 0;
	} else {
		gtk_widget_show(state_hbox);
		gtk_object_set(GTK_OBJECT(state_button), "label", " Hide State ", NULL);
		show_state = 1;
		stateUpdate();
	}
}


void toggle_bochs( GtkWidget *widget, gpointer data ){
	if(bochs_offline){
g_print("bochs on\n");
		bochsStart();
		gtk_object_set(GTK_OBJECT(bochs_button), "label", " Kill Bochs ", NULL);
		if(bochs_offline){
			gtk_signal_disconnect(GTK_OBJECT(widget), id);
			gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), FALSE);
			id = gtk_signal_connect(GTK_OBJECT(widget), "clicked", GTK_SIGNAL_FUNC(toggle_bochs), NULL);
			gtk_object_set(GTK_OBJECT(bochs_button), "label", " Start Bochs ", NULL);
		}
	} else {
g_print("bochs off\n");
		bochsShutdown();
		gtk_object_set(GTK_OBJECT(bochs_button), "label", " Start Bochs ", NULL);
	}
}


void destroy_quit_window( GtkWidget *widget, gpointer data ){
	gtk_widget_destroy(quit_window);
}
void confirm_quit( ){
	#ifdef __CONFIRM_QUIT__
		GtkWidget *vbox, *hbox, *button;

		// create quit confirmation window
		quit_window = new_window(GTK_WINDOW_DIALOG, "BFE - Quit?", 300, 100);
		gtk_signal_connect(GTK_OBJECT(quit_window), "destroy", GTK_SIGNAL_FUNC(destroy_quit_window), NULL);
		vbox = new_window_vbox(quit_window);

		// confirmation label
		new_label(vbox, TRUE, "Do you really want to quit?", FALSE);

		hbox = new_hbox(vbox, FALSE);

		// yes button
		button = new_button(hbox, TRUE, " Yes ");
		gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(destroy_window), NULL);

		// no button
		button = new_button(hbox, TRUE, " No ");
		gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(destroy_quit_window), NULL);
	#else
		bfe_quit(NULL);
	#endif
}


GtkWidget *add_scrolled_page( const char *name ){
	GtkWidget *scrolled_window, *frame, *vbox;

	vbox = gtk_vbox_new(FALSE, 5);
	gtk_notebook_append_page(GTK_NOTEBOOK(notebook), vbox, gtk_label_new(name));
	gtk_widget_show(vbox);

	// create scrolled window
	scrolled_window = gtk_scrolled_window_new(NULL, NULL);
	gtk_container_set_border_width(GTK_CONTAINER(scrolled_window), 0);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
		GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
	gtk_widget_show(scrolled_window);

	frame = gtk_frame_new(NULL);
	gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
	gtk_container_set_border_width(GTK_CONTAINER(frame), 5);
	gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window), frame);
	gtk_widget_show(frame);

	vbox = gtk_vbox_new(FALSE, 5);
	gtk_container_add(GTK_CONTAINER(frame), vbox);
	gtk_widget_show(vbox);

	return(vbox);
}


GtkWidget *add_page( const char *name ){
	GtkWidget *vbox;

	vbox = gtk_vbox_new(FALSE, 5);
	gtk_notebook_append_page(GTK_NOTEBOOK(notebook), vbox, gtk_label_new(name));
	gtk_widget_show(vbox);

	return(vbox);
}


void refreshall_windows( GtkWidget *widget, gpointer data ){
	s_memwin *m;
	s_structwin *s;

	for(s = structwin_head; s != NULL; s = s->next)
		structuresUpdateWindow(s);

	for(m = memwin_head; m != NULL; m = m->next)
		memoryUpdateWindow(m);
}


//////////////////////////////////////////////////////////////////////////


void bfe_quit( char *message ){
	historyRepeat();
	if(message == NULL) bochsShutdown();
	else g_print("BFE: %s\n", message);
	historyClose();
	gtk_main_quit();
}


int main( int argc, char *argv[] ){
	GtkWidget *window, *window_vbox, *hbox, *button;
	char path[LEN_RCPATH];

	show_state = 0;
	bochsInit();

	gtk_init(&argc, &argv);
	snprintf(path, LEN_RCPATH, "%s/.gtkrc", getenv("HOME"));	// ~/.gtkrc
	gtk_rc_parse(path);
	snprintf(path, LEN_RCPATH, "%s/gtkrc-bfe", CFGDIR);		// $PREFIX/$SYSCONF/gtkrc-bfe
	gtk_rc_parse(path);
	snprintf(path, LEN_RCPATH, "%s/.gtkrc-bfe", getenv("HOME"));	// ~/.gtkrc-bfe
	gtk_rc_parse(path);

	// create window
	window = new_window(GTK_WINDOW_TOPLEVEL, "BFE -- http://www.qzx.com/bfe", 630, 500);
	gtk_signal_connect(GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(destroy_window), NULL);
	window_vbox = new_window_vbox(window);

	/////////////////////////////////////////////

	ctrlInit(window_vbox);

	notebook = gtk_notebook_new();
	gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_BOTTOM);
	gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), TRUE);
	gtk_box_pack_start(GTK_BOX(window_vbox), notebook, TRUE, TRUE, 0);
	gtk_widget_show(notebook);

	breakpointsInit(add_scrolled_page("Breakpoints"));
	watchpointsInit(add_scrolled_page("Watchpoints"));
	memoryInit(add_scrolled_page("Memory"));
	structuresInit(add_page("Structures"));
	prefsInit(add_scrolled_page("Preferences"));
	historyInit(add_page("History"));

	/////////////////////////////////////////////

	state_hbox = gtk_hbox_new(FALSE, 12);
	gtk_box_pack_start(GTK_BOX(window_vbox), state_hbox, FALSE, FALSE, 0);

	stateInit(state_hbox);

	/////////////////////////////////////////////

	hbox = new_hbox(window_vbox, FALSE);

	// start button
	bochs_button = new_toggle_button(hbox, FALSE, " Start Bochs ");
	id = gtk_signal_connect(GTK_OBJECT(bochs_button), "clicked", GTK_SIGNAL_FUNC(toggle_bochs), NULL);

	// refresh-all button
	refreshall_button = new_button(hbox, FALSE, " Refresh All Windows ");
	gtk_signal_connect(GTK_OBJECT(refreshall_button), "clicked", GTK_SIGNAL_FUNC(refreshall_windows), NULL);

	// state button
	state_button = new_toggle_button(hbox, TRUE, " Show State ");
	gtk_signal_connect(GTK_OBJECT(state_button), "clicked", GTK_SIGNAL_FUNC(toggle_state), NULL);

	// quit button
	button = new_button(hbox, FALSE, " Quit ");
	gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(confirm_quit), NULL);

	/////////////////////////////////////////////

	bochsShutdown();
	structuresUpdate();

	gtk_main();
	exit(0);
}
