/*  gfocustimer
 *  Copyright (C) 1999 James Cameron, <quozl@us.netrek.org>
 *
 *  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.
*/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#define XK_MISCELLANY
#include <X11/keysymdef.h>
#include <gdk/gdkkeysyms.h>

#include "callbacks.h"
#include "interface.h"
#include "support.h"
#include "utilities.h"
#include "data.h"
#include "total.h"
#include "entry.h"
#include "charge.h"
#include "preferences.h"
#include "focus.h"
#include "connect.h"
#include "observation.h"
#include "shortcuts.h"

/* main window */
struct mw mw;

/* preferences window */
struct pw pw;

/* preferences data */
struct pd pd;


/*
 * Main Window							********
 */


/* called when the main window is shown */
void
on_main_show                           (GtkWidget       *widget,
                                        gpointer         user_data)
{
  /* save the widget id's and generally initialise our main window struct */
  mw.widget      = widget;
  mw.preferences = GTK_MENU_ITEM(lookup (widget, "Preferences"));
  mw.edit        = GTK_MENU_ITEM(lookup (widget, "Edit"));
  mw.delete      = GTK_MENU_ITEM(lookup (widget, "Delete"));
  mw.list        = GTK_CLIST(lookup(widget,"list"));
  mw.row         = -1;
  mw.ed          = NULL;
  mw.total.row   = -1;
  mw.total.time  = 0;
  mw.charging    = GTK_TOGGLE_BUTTON(lookup(widget,"charging"));
  mw.focus       = GTK_TOGGLE_BUTTON(lookup(widget,"focus"));
  mw.begin       = 0;
  mw.charge      = 0;

  /* per-entry buttons are to start off disabled */
  gtk_widget_set_sensitive (GTK_WIDGET(mw.edit), 0);
  gtk_widget_set_sensitive (GTK_WIDGET(mw.delete), 0);

  pw.widget = NULL;

  observation_initialise();
  entry_initialise();
  focus_initialise();
  shortcut_initialise();
  shortcut_load();

  /* default, load, and apply preferences while loading data */
  {
    struct pd was, is;
    preferences_default (&pd);
    was = pd;
    is = pd;
    preferences_load (&is);
    preferences_apply (&was, &is);
    pd = is;
  }

  entry_load();

  if (pd.charging)
    gtk_toggle_button_set_active (mw.charging, TRUE);

  if (pd.watching)
    gtk_toggle_button_set_active (mw.focus, TRUE);

}


/* called when window is deleted by window manager */
gboolean
on_main_delete_event                   (GtkWidget       *widget,
                                        GdkEvent        *event,
                                        gpointer         user_data)
{
  gtk_widget_hide(GTK_WIDGET(mw.widget));
  entry_save();
  preferences_save (&pd);
  gtk_main_quit();
  return TRUE;
}


/* called when the File - Preferences menu item is selected */
void
on_Preferences_activate                (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
  preferences_create();
}


/* called when the File - Save menu item is selected */
void
on_Save_activate                       (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
  entry_save();
}


/* called when the File - Exit menu item is selected */
void
on_Exit_activate                       (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
  entry_save();
  preferences_save (&pd);
  gtk_main_quit();
}


/* called when the Entry - Create menu item is selected */
void
on_Create__activate                    (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
  entry_create();
}


/* called when the Entry - Edit menu item is selected */
void
on_Edit__activate                      (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
  entry_edit();
}


/* called when the Entry - Delete menu item is selected */
void
on_Delete__activate                    (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
  entry_delete();
}


/* called when the Help - About menu item is selected */
void
on_About_activate                      (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
  message ("About gfocustimer",
"gfocustimer version 1.0, Copyright (C) 2001 James Cameron, "
"<quozl@us.netrek.org>\n"
"gfocustimer comes with ABSOLUTELY NO WARRANTY; for details see source.\n"
"This is free software, and you are welcome to redistribute it "
"under certain conditions;\nsee source for details.\n"
"\n"
"gfocustimer is a program for recording the time spent in various windows.\n"
"\n"
"Problem reports, suggestions, and patches are welcome.\n");
}
