/*
 * Copyright (C) 2003 Clemens Fuchslocher
 *
 * 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 "InfoDialog.h"


InfoDialog::InfoDialog(const char *title)
{
	// Dialog
	this->dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(dialog), title);
	gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
	gtk_window_set_resizable(GTK_WINDOW(dialog), false);
	gtk_widget_set_usize(GTK_WIDGET(dialog), 500, 300);
	gtk_container_set_border_width(GTK_CONTAINER(dialog), 4);
	gtk_signal_connect_object(GTK_OBJECT(dialog),
		"destroy", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(dialog));

	// Mainbox
	GtkWidget *mainbox = gtk_vbox_new(false, 1);

	// TextView
	this->textView = gtk_text_view_new();
	gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(this->textView), GTK_WRAP_NONE);
	gtk_text_view_set_editable(GTK_TEXT_VIEW(this->textView), false);

	this->textBuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(this->textView));
	gtk_text_buffer_create_tag(this->textBuffer, "heading", "weight", PANGO_WEIGHT_BOLD, "scale", PANGO_SCALE_X_LARGE, NULL);

	// Scrollbar
	GtkWidget *scrollbar = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollbar), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
	gtk_container_set_border_width(GTK_CONTAINER(scrollbar), 4);

	// Separator
	GtkWidget *separator = gtk_hseparator_new();

	// Ok
	GtkWidget *ok = gtk_button_new_from_stock(GTK_STOCK_OK);
	GTK_WIDGET_SET_FLAGS(ok, GTK_CAN_DEFAULT);
	gtk_signal_connect_object(GTK_OBJECT(ok),
		"clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(dialog));

	GtkWidget *ok_box = gtk_hbutton_box_new();
	gtk_button_box_set_layout(GTK_BUTTON_BOX(ok_box), GTK_BUTTONBOX_END);
	gtk_button_box_set_spacing(GTK_BUTTON_BOX(ok_box), 5);

	// Pack
	gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrollbar), this->textView);
	gtk_box_pack_start(GTK_BOX(mainbox), scrollbar, true, true, 0);
	gtk_box_pack_start(GTK_BOX(mainbox), separator, false, false, 0);
	gtk_container_add(GTK_CONTAINER(ok_box), ok);
	gtk_box_pack_start(GTK_BOX(mainbox), ok_box, false, false, 5);
	gtk_container_add(GTK_CONTAINER(this->dialog), mainbox);
	gtk_widget_grab_default(ok);
}


void InfoDialog::addHeader(const char *header)
{
	GtkTextIter iter;
	gtk_text_buffer_get_iter_at_offset(this->textBuffer, &iter, -1);
	gtk_text_buffer_insert_with_tags_by_name(this->textBuffer, &iter, header, -1, "heading", NULL);
}


void InfoDialog::addText(const char *text)
{
	GtkTextIter iter;
	gtk_text_buffer_get_iter_at_offset(this->textBuffer, &iter, -1);
	gtk_text_buffer_insert(textBuffer, &iter, "\t", -1);
	gtk_text_buffer_insert(textBuffer, &iter, text, -1);
}


void InfoDialog::performAction(void)
{
	gtk_widget_show_all(this->dialog);
}
