//Filename: gtkalarm.cpp
//Author:   Ian Larsen
//Date:     22 APR 1999
//Descrip:  Driver file for gtkalarm

//  Copyright (C) 1999 Ian Larsen
// 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 <time.h>
#include <stdlib.h>
#include <string.h>
#include <iostream.h>

GtkWidget *button;
GtkWidget *test;
GtkWidget *hourEntry;
GtkWidget *minuteEntry;
GtkWidget *commandEntry;
gint alarmOff;
gint alarmStarted;
gint minutes;
gint hours;
tm *locTime;

struct Timer{
  int hour;
  int minute;
};

Timer timer, Alarm;
gint runClock(gpointer data){ 
  time_t now = time(0);
  locTime = localtime(&now);
  minutes = (*locTime).tm_min;
  hours = (*locTime).tm_hour;
  
  if (hours < 0){
    hours = hours + 24;
  }
  timer.hour = hours;
  timer.minute = minutes;
  if (timer.minute == Alarm.minute &&
      timer.hour == Alarm.hour &&
      alarmOff == FALSE){
    alarmOff = TRUE;
  }
  if (alarmOff == TRUE && alarmStarted == FALSE){
    int length =  strlen (gtk_entry_get_text (GTK_ENTRY (commandEntry)));
    char command[length];
    strcpy (command, gtk_entry_get_text (GTK_ENTRY (commandEntry)));
    //add background symbol if not present
    if (command [length - 1] != '&'){
      strcat (command, " &");
    }
    system(command);
    alarmStarted = TRUE;
    gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON ( button ), FALSE );
    gtk_entry_set_editable ( GTK_ENTRY (commandEntry), TRUE );
  }
  return 1;
}

gint delete_event (GtkWidget *widget, GdkEvent *event, gpointer data){
  return (FALSE); //window can be closed by a delete event/
}

//destroys window and quits
void destroy (GtkWidget *widget, gpointer data){
  gtk_main_quit ();
}

void Close (GtkWidget *widget, gpointer data){
  gtk_widget_destroy (data);
}

void about (GtkWidget *widget, gpointer data){
  GtkWidget *about;
  GtkWidget *atext;
  GtkWidget *close;
  GtkWidget *abox;
  about = gtk_window_new(GTK_WINDOW_DIALOG);
  gtk_container_border_width (GTK_CONTAINER (about), 10);
  gtk_widget_show(about);
  
  abox = gtk_vbox_new (FALSE, 0);
  gtk_widget_show(abox);
  
  atext = gtk_label_new("This program created by Ian Larsen.\n  Licensed under the GPL.\n  Send comments to bluesmani@yahoo.com \n");
  gtk_widget_show(atext);
  
  close = gtk_button_new_with_label ("Close");
  gtk_signal_connect (GTK_OBJECT (close), "clicked",
		      GTK_SIGNAL_FUNC (Close), about);
  gtk_widget_show(close);

  gtk_box_pack_start (GTK_BOX (abox), atext, FALSE, FALSE, 5);
  gtk_box_pack_start (GTK_BOX (abox), close, FALSE, FALSE, 5);
  gtk_container_add(GTK_CONTAINER (about), abox);
  
}


void testCommand (GtkWidget *widget, gpointer data){
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (test))){
    int length =  strlen (gtk_entry_get_text (GTK_ENTRY (commandEntry)));
    char command[length];
    strcpy (command, gtk_entry_get_text (GTK_ENTRY (commandEntry)));
    //add background symbol if not present
    if (command [length - 1] != '&'){
      strcat (command, " &");
    }
    system(command);
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (test), FALSE);
  }
}
//Executed when Set Alarm button is clicked(GtkWidget *widget, gpointer data)
void setAlarm(GtkWidget *widget, gpointer data){
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button))){
    alarmOff = FALSE;
    alarmStarted = FALSE;
    Alarm.hour = (gtk_spin_button_get_value_as_int
		   (GTK_SPIN_BUTTON (hourEntry)));
    Alarm.minute = (gtk_spin_button_get_value_as_int
		     (GTK_SPIN_BUTTON (minuteEntry)));
    gtk_entry_set_editable (GTK_ENTRY (commandEntry), FALSE);
  }
  
  else { 
    gtk_entry_set_editable (GTK_ENTRY (commandEntry), TRUE );
    alarmOff = TRUE;
    alarmStarted = TRUE;
  }
}


void makeWindow(){
  GtkWidget *window;
  GtkWidget *label;
  GtkWidget *vbox;
  GtkWidget *hbox;
  GtkWidget *menuBar;
  GtkObject *hourAdj;
  GtkObject *minAdj;
  static GtkItemFactoryEntry menu_items[] = {
    { "/File",         NULL,           NULL,  0,  "<Branch>" },
    { "/File/Quit",    NULL,  gtk_main_quit,  0,  NULL },
    { "/Help",         NULL,           NULL,  0,  "<Branch>" },
    { "/Help/About",   NULL,          about,  0,  NULL },
  };
  
  GtkItemFactory *item_factory;
  gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
  item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "<main>", 0);
  gtk_item_factory_create_items (item_factory, nmenu_items, menu_items, NULL);
  menuBar = gtk_item_factory_get_widget (item_factory, "<main>");


  //set adjustments
  hourAdj = gtk_adjustment_new(23,0,23,1,3,3);
  minAdj = gtk_adjustment_new(59,0,59,1,3,3);
  
  //window
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "GTK Alarm");
  gtk_window_set_policy (GTK_WINDOW (window), FALSE, FALSE, FALSE);
  gtk_signal_connect (GTK_OBJECT (window), "destroy",
  		      GTK_SIGNAL_FUNC (destroy), NULL );
  gtk_container_border_width (GTK_CONTAINER (window), 0);

  //create boxes
  hbox = gtk_hbox_new (FALSE, 0);
  vbox = gtk_vbox_new (FALSE, 0);
  gtk_widget_show (hbox);
  gtk_widget_show (vbox);
 
  //pack menu
  gtk_box_pack_start (GTK_BOX (vbox), menuBar, FALSE, TRUE, 0);
  gtk_widget_show (menuBar);
  
  //draw "Command" label
  label = gtk_label_new( "Command:" );
  gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 3);
  gtk_widget_show ( label );

  //new hbox
  hbox = gtk_hbox_new (TRUE, 0);
  gtk_widget_show ( hbox );
  //create command entry box
  commandEntry = gtk_entry_new();
  gtk_box_pack_start (GTK_BOX (hbox), commandEntry, FALSE, FALSE, 3);
  gtk_widget_show (commandEntry);
 //pack horizontal box
  gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);

  //new hbox
  hbox = gtk_hbox_new (TRUE, 0);
  gtk_widget_show ( hbox );
  //set and draw labels
  //hour label
  label = gtk_label_new( "Hour:  " );
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 2);
  gtk_widget_show (label);
  //minute label
  label = gtk_label_new( "Minute:" );
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 2);
  gtk_widget_show (label);
  //pack horizontal box
  gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);

  //new horizontal box
  hbox = gtk_hbox_new (FALSE, 0 );
  gtk_widget_show ( hbox );
  //set up spin buttons
  //hour
  hourEntry = gtk_spin_button_new (GTK_ADJUSTMENT (hourAdj), 0, 0 );
  gtk_spin_button_set_wrap ( GTK_SPIN_BUTTON ( hourEntry ), TRUE );
  gtk_spin_button_set_value ( GTK_SPIN_BUTTON ( hourEntry ), hours );
  gtk_box_pack_start ( GTK_BOX ( hbox ), hourEntry, TRUE, TRUE, 20 );
  gtk_widget_show ( hourEntry );
  //minute
  minuteEntry = gtk_spin_button_new (GTK_ADJUSTMENT (minAdj), 0, 0 );
  gtk_spin_button_set_wrap( GTK_SPIN_BUTTON ( minuteEntry ), 1 );
  gtk_spin_button_set_value( GTK_SPIN_BUTTON ( minuteEntry ), minutes );
  gtk_box_pack_start ( GTK_BOX ( hbox ), minuteEntry, TRUE, TRUE, 20 );
  gtk_widget_show ( minuteEntry );
  //pack horizontal box
  gtk_box_pack_start ( GTK_BOX ( vbox ), hbox, FALSE, FALSE, 2 );

  //new horizontal box
  hbox = gtk_hbox_new ( TRUE, 0 );
  gtk_widget_show ( hbox );
  //set up Test button
  test = gtk_toggle_button_new_with_label ( "Test" );
  gtk_box_pack_start ( GTK_BOX ( hbox ), test, TRUE, TRUE, 10 );
  gtk_widget_show ( test );
  gtk_signal_connect( GTK_OBJECT ( test ), "clicked", 
		      GTK_SIGNAL_FUNC ( testCommand ), NULL );
  //set up Alarm set button
  button = gtk_toggle_button_new_with_label ( "Set Alarm" );
  gtk_box_pack_end ( GTK_BOX ( hbox ), button, TRUE, TRUE, 10 );
  gtk_widget_show ( button );
  gtk_signal_connect( GTK_OBJECT ( button ), "clicked", 
		      GTK_SIGNAL_FUNC ( setAlarm ), NULL );
 //pack horizontal box
  gtk_box_pack_end ( GTK_BOX ( vbox ), hbox, FALSE, FALSE, 3 );
  
  //add vbox to window container
  gtk_container_add ( GTK_CONTAINER ( window ), vbox );

  //show window
  gtk_widget_show(window);
}

int main(int argc, char *argv[] ){
  time_t now = time(0);
  locTime = localtime(&now);
  alarmOff = TRUE;
  alarmStarted = TRUE;
  hours = (*locTime).tm_hour;
  minutes = (*locTime).tm_min;
  if (hours < 0){
    hours = hours + 24;
  }

  gtk_init (&argc, &argv); //gtk initialize function
 
  makeWindow(); //sets up window
  
  gtk_timeout_add(1000, runClock, NULL);//execute runclock once per 1000ms 

  gtk_main();  //start main loop
  
  return 0;
}



