/*
**  $Id: route_dialog.c,v 1.8 1999/04/25 14:50:53 jochen Exp $
**
**  GXSNMP -- An snmp management application
**
**  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, Cambridge, MA 02139, USA.
*/

/*
**  route_dialog.c is the dialog for the route table function
*/

#include <gnome.h>
#include "main.h"			/* Needed for gxsnmp struct */
#include "route_dialog.h"
#include "tables.h"
#include "gnome-property-dialog.h"
#include "gnome-dialog-create-button.h"

extern gxsnmp *app_info;

/*************************************************************************
 *  Static data 
 ************************************************************************/

#define CLIST_COLUMNS 8

static char * titles [] =
{
  "Protocol", "Destination", "Netmask", "Gateway", "Interface", "Metric",
  "Route Age", "Type"
};

static gint title_widths[8] = { 8, 13, 13, 13, 8, 6, 8, 8 };
static gint total_height = 16;
static gint total_width	= 86;	/* Empirically determined :-| */
/**************************************************************************
 *  Forward declarations and callback functions
 *************************************************************************/
static void route_dialog_class_init (GXsnmp_route_dialogClass  *klass);
static void route_dialog_init	    (GXsnmp_route_dialog       *dialog);
static void route_dialog_cb         (GnomeDialog               *dialog,
				     gint                      button,
				     gpointer                  data);

/**************************************************************************
 *  gxsnmp_route_dialog_get_type()
 *************************************************************************/
GtkType
gxsnmp_route_dialog_get_type ()
{
  static guint widget_type = 0;

  if (!widget_type)
    {
      GtkTypeInfo widget_info =
      {
        "GXsnmp_route_dialog",
        sizeof (GXsnmp_route_dialog),
        sizeof (GXsnmp_route_dialogClass),
        (GtkClassInitFunc) route_dialog_class_init,
        (GtkObjectInitFunc) route_dialog_init,
        (GtkArgSetFunc) NULL,
        (GtkArgGetFunc) NULL
      };
      widget_type = gtk_type_unique (gnome_dialog_get_type (), 
				     &widget_info);
    }
  return widget_type;
}

/****************************************************************************
 *  The class initialization subroutine
 ***************************************************************************/

static void
route_dialog_class_init (GXsnmp_route_dialogClass *class)
{
}

/*****************************************************************************
**
**  The widget initialization subroutine
**
*****************************************************************************/

static void
route_dialog_init (GXsnmp_route_dialog *dialog)
{
  GtkWidget * label;
  GtkWidget * scrolled_win;	
  gint        c_width;		/* Average width of a character */
  gint	      c_height;		/* Average height of a character */
  gint	      i;

/*
**  Build the main framework for the whole thing
*/

  dialog->table = gtk_table_new (1, 2, FALSE);
  gtk_box_pack_start (GTK_BOX (GNOME_DIALOG (dialog)->vbox),
		      dialog->table, TRUE, TRUE, 0);
  gtk_widget_show (dialog->table);
  c_width  = gdk_string_width (dialog->table->style->font, "xW") / 2;
  c_height = dialog->table->style->font->ascent + 
             dialog->table->style->font->descent;

/*
**  The title label
*/

  label = gtk_label_new (_("Node route table"));
  gtk_table_attach (GTK_TABLE (dialog->table), label,
		    0, 1, 0, 1, 0, 0, 0, 0);
  gtk_widget_show (label);

/*
**  Create a scrolled window, and insert the clist in the window
*/

  scrolled_win = gtk_scrolled_window_new (NULL, NULL);
  gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 5);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
                                  GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
  gtk_table_attach (GTK_TABLE (dialog->table), scrolled_win, 
		    0, 1, 1, 2, 0, 0, 0, 0);
  gtk_widget_show (scrolled_win);

  dialog->clist = gtk_clist_new_with_titles (CLIST_COLUMNS, titles);
 
  gtk_container_add (GTK_CONTAINER (scrolled_win), dialog->clist);
  for (i=0; i < CLIST_COLUMNS; i++)
    {
    gtk_clist_set_column_width (GTK_CLIST (dialog->clist), i, title_widths[i] * c_width);
    }
  gtk_widget_set_usize (dialog->clist, total_width * c_width , 
			       total_height * c_height);
  gtk_widget_show (dialog->clist);
  gnome_dialog_close_hides (GNOME_DIALOG (dialog), FALSE);

  /* Append some buttons */
  /* Button 0 */
  gnome_dialog_append_button_with_pixmap (GNOME_DIALOG (dialog),
					  "Abort",
					  GNOME_STOCK_PIXMAP_STOP);
  /* Button 1 */
  gnome_dialog_append_button_with_pixmap (GNOME_DIALOG (dialog),
					  "Reload",
					  GNOME_STOCK_PIXMAP_REFRESH);
  /* Button 2 & 3 */
  gnome_dialog_append_buttons (GNOME_DIALOG (dialog),
			       GNOME_STOCK_BUTTON_CLOSE, 
			       GNOME_STOCK_BUTTON_HELP,
			       NULL);

  gtk_signal_connect (GTK_OBJECT (dialog), "clicked",
		      (GtkSignalFunc) route_dialog_cb, dialog);

}
/****************************************************************************
 * Callback for the custom dialog buttons
 ***************************************************************************/
static void
route_dialog_cb (GnomeDialog *dialog, gint button, gpointer data)
{
  GXsnmp_route_dialog  *rdialog;
  rt_data              *rt;
  rdialog = GXSNMP_ROUTE_DIALOG (dialog);
  rt = rdialog->rt;
  switch (button)
    {
    case 0:                                       /* Abort button */
      g_print ("Abort!\n");
      if (rt->table)
	g_snmp_table_destroy (rt->table);
      rt->table = NULL;
      route_dialog_set_state (rdialog);
      break;
    case 1:                                       /* Reload */
      gtk_clist_clear (GTK_CLIST (rdialog->clist));
      rt->table   = NULL;
      start_request (rt);
      g_print ("Reload %s!\n", rt->host.name);
      break;
    case 2:                                       /* Close button */
      if (rt->table)
	g_snmp_table_destroy (rt->table);
      rt->table = NULL;
      gnome_dialog_close (GNOME_DIALOG (dialog));
      break;
    case 3:                                       /* Help button */
      g_print ("Help me! Help me!\n");
      break;
    default:
      g_print ("Button %d hit!\n", button);
      break;
    }
}
/****************************************************************************
 * Set the state of the buttons.
 ***************************************************************************/
void
route_dialog_set_state (GXsnmp_route_dialog *dialog)
{
  rt_data   *rt;

  rt = dialog->rt;
  if (rt->table)
    {
      g_print ("Setting state to a request running state....\n");
      /* have a request running turn off the reload button */
      gnome_dialog_set_sensitive (GNOME_DIALOG (dialog), 1, FALSE);
      /* Make sure the abort button can be clicked */
      gnome_dialog_set_sensitive (GNOME_DIALOG (dialog), 0, TRUE);
    }
  else
    {
      g_print ("Setting state to idle..\n");
      gnome_dialog_set_sensitive (GNOME_DIALOG (dialog), 0, FALSE);
      gnome_dialog_set_sensitive (GNOME_DIALOG (dialog), 1, TRUE);
    }
}

/****************************************************************************
 *  Public function to create a new route dialog widget  
 ****************************************************************************/
GtkWidget *
gxsnmp_route_dialog_new (rt_data *rt)
{
  GXsnmp_route_dialog * route_dialog;

  route_dialog = gtk_type_new( gxsnmp_route_dialog_get_type());
  route_dialog->rt = rt;
  route_dialog_set_state (route_dialog);
  return GTK_WIDGET (route_dialog);
}

/* EOF */
