/*
 *  CWStation
 *  Copyright (c) 2004 by Thierry Leconte (F4DWV)
 *
 *      $Id: image.c,v 1.10 2004/04/24 07:19:48 f4dwv Exp $
 *
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library 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 Library General Public License for more details.
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */
                                                                                                                              
#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <gtk/gtk.h>

#include "callbacks.h"
#include "interface.h"
#include "support.h"

extern GtkWidget *window1;

extern gboolean Txflag;

void save_data (void);


GtkWidget *OpenChooser = NULL;

void
on_ouvrir1_activate (GtkMenuItem * menuitem, gpointer user_data)
{
  OpenChooser = create_openfilechooser ();
  gtk_widget_show (OpenChooser);
}


void
on_openfile_clicked (GtkButton * button, gpointer user_data)
{
  GtkTextBuffer *Text;
  const gchar *selected_filename;
  gchar *filetext;
  GError *ferr;

  selected_filename =
    gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (OpenChooser));

  gtk_widget_destroy (OpenChooser);

  Text =
    gtk_text_view_get_buffer (GTK_TEXT_VIEW
			      (lookup_widget
			       (GTK_WIDGET (window1), "TxText")));

  if (g_file_get_contents (selected_filename, &filetext, NULL, &ferr))
    {
      gtk_text_buffer_set_text (Text, filetext, strlen (filetext));
      g_free ((gpointer) filetext);
    }
  g_free ((gpointer) selected_filename);
}

void
on_configuration1_activate (GtkMenuItem * menuitem, gpointer user_data)
{
}

void
on____propos1_activate (GtkMenuItem * menuitem, gpointer user_data)
{
GtkWidget *w;

w=create_apropos();
gtk_widget_show (w);

}

/* -------------- Macro  Window ---------------------------------*/

static GtkWidget *MacroWin;
static gchar mbuttonname[8][32] =
  { "M1", "M2", "M3", "M4", "M5", "M6", "M7", "M8" };
static gchar macrostr[8][256] =
  { "M1 ", "M2 ", "M3 ", "M4 ", "M5 ", "M6 ", "M7 ", "M8 " };

void
on_macro1_activate (GtkMenuItem * menuitem, gpointer user_data)
{
  int i;
  GtkWidget *w;

  MacroWin = create_MacroWin ();

  for (i = 0; i < 8; i++)
    {
      gchar wname[32];

      sprintf (wname, "macrolabel%d", i + 1);
      w = lookup_widget (GTK_WIDGET (MacroWin), wname);
      gtk_entry_set_text (GTK_ENTRY (w), mbuttonname[i]);

      sprintf (wname, "macrotxt%d", i + 1);
      w = lookup_widget (GTK_WIDGET (MacroWin), wname);
      gtk_entry_set_text (GTK_ENTRY (w), macrostr[i]);
    }

  gtk_widget_show (MacroWin);
}


void
macrobutton_set (void)
{
  int i;
  GtkWidget *w;

  for (i = 0; i < 8; i++)
    {
      gchar wname[32];

      sprintf (wname, "macrobutton%d", i + 1);
      w = lookup_widget (GTK_WIDGET (window1), wname);
      gtk_button_set_label (GTK_BUTTON (w), mbuttonname[i]);

    }
}

void
on_okbutton_mac_clicked (GtkButton * button, gpointer user_data)
{
  int i;
  GtkWidget *w;

  for (i = 0; i < 8; i++)
    {
      gchar wname[32];

      sprintf (wname, "macrolabel%d", i + 1);
      w = lookup_widget (GTK_WIDGET (MacroWin), wname);
      strcpy (mbuttonname[i], gtk_entry_get_text (GTK_ENTRY (w)));

      sprintf (wname, "macrotxt%d", i + 1);
      w = lookup_widget (GTK_WIDGET (MacroWin), wname);
      strcpy (macrostr[i], gtk_entry_get_text (GTK_ENTRY (w)));

    }
  gtk_widget_destroy (MacroWin);

  macrobutton_set ();

  save_data ();
}


void
on_macro_clicked (GtkButton * button, gpointer user_data)
{
  int i;
  GtkWidget *w;

  for (i = 0; i < 8; i++)
    {
      gchar wname[32];

      sprintf (wname, "macrobutton%d", i + 1);
      w = lookup_widget (GTK_WIDGET (window1), wname);
      if (w == GTK_WIDGET (button))
	{
	  GtkTextBuffer *Text;

	  Text =
	    gtk_text_view_get_buffer (GTK_TEXT_VIEW
				      (lookup_widget
				       (GTK_WIDGET (window1), "TxText")));
	  gtk_text_buffer_insert_at_cursor (Text, macrostr[i],
					    strlen (macrostr[i]));

	  break;
	}

    }

}

/* -------------- Training ---------------------------------*/
static GtkWidget *Training;
static guint tr_min = 5;
static guint tr_max = 5;
static guint tr_len = 180;
static gchar tr_alpha[128] = "ABCDEFGHIJKLMNOPQRST";

void
on_training1_activate (GtkMenuItem * menuitem, gpointer user_data)
{
  GtkWidget *w;

  Training = create_Training ();

  w = lookup_widget (GTK_WIDGET (Training), "trminSpin");
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (w), (gdouble) tr_min);

  w = lookup_widget (GTK_WIDGET (Training), "trmaxSpin");
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (w), (gdouble) tr_max);

  w = lookup_widget (GTK_WIDGET (Training), "trlenSpin");
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (w), (gdouble) tr_len);

  w = lookup_widget (GTK_WIDGET (Training), "TrainingAlphabet");
  gtk_entry_set_text (GTK_ENTRY (GTK_BIN (w)->child), tr_alpha);


  gtk_widget_show (Training);
}


void
on_oktraining_clicked (GtkButton * button, gpointer user_data)
{
  GtkWidget *w;
  double v;

  w = lookup_widget (GTK_WIDGET (Training), "trminSpin");
  v = gtk_spin_button_get_value (GTK_SPIN_BUTTON (w));
  tr_min = (guint) v;

  w = lookup_widget (GTK_WIDGET (Training), "trmaxSpin");
  v = gtk_spin_button_get_value (GTK_SPIN_BUTTON (w));
  tr_max = (guint) v;

  w = lookup_widget (GTK_WIDGET (Training), "trlenSpin");
  v = gtk_spin_button_get_value (GTK_SPIN_BUTTON (w));
  tr_len = (guint) v;

  w = lookup_widget (GTK_WIDGET (Training), "TrainingAlphabet");
  strcpy (tr_alpha, gtk_entry_get_text (GTK_ENTRY (GTK_BIN (w)->child)));

  gtk_widget_destroy (Training);
  save_data ();
}

/* ----------------- training text generation ----------------*/
void
on_training_button_clicked (GtkButton * button, gpointer user_data)
{
  GtkTextBuffer *Text;
  gchar tr_text[1000];
  guint n, nc, ng, ngl, nalpha;

  nalpha = strlen (tr_alpha);
  if (nalpha == 0)
    return;

  ngl = tr_min + (guint) (((double) random () * (tr_max - tr_min)) / (double) RAND_MAX);
  n = 4; 
  strcpy(tr_text,"VVV\n");
  for (nc = ng = 0; nc < tr_len; nc++, ng++)
    {
      guint i;

      if (ng == ngl)
	{
	  tr_text[n++] = ' ';
	  ng = 0;
	  ngl =
	    tr_min +
	    (guint) (((double) random () * (1 + tr_max - tr_min)) /
		     (double) RAND_MAX);
	}

      i = (guint) (((double) random () * nalpha) / (double) RAND_MAX);
      tr_text[n++] = tr_alpha[i];
    }
  strcpy(&(tr_text[n]),"\n~");
  n+=2;

  Text =
    gtk_text_view_get_buffer (GTK_TEXT_VIEW
			      (lookup_widget
			       (GTK_WIDGET (window1), "TxText")));
  gtk_text_buffer_set_text (Text, tr_text, n);
}


/* -------------- cw setting  ---------------------------------*/
static guint cw_speed = 12;
static guint cw_space = 100;
static guint cw_pitch = 700;
static guint cw_shape = 5;


void
on_speed_changed                       (GtkSpinButton   *spinbutton,
                                        gpointer         user_data)
{
  gdouble v;
  v = gtk_spin_button_get_value (spinbutton);
  cw_speed = (guint) v;
  setcwdit (cw_speed, cw_space, cw_pitch, cw_shape);
}


void
on_space_changed                       (GtkSpinButton   *spinbutton,
                                        gpointer         user_data)
{
  gdouble v;
  v = gtk_spin_button_get_value (spinbutton);
  cw_space = (guint) v;
  setcwdit (cw_speed, cw_space, cw_pitch, cw_shape);
}


void
on_pitch_changed                       (GtkSpinButton   *spinbutton,
                                        gpointer         user_data)
{
  gdouble v;
  v = gtk_spin_button_get_value (spinbutton);
  cw_pitch = (guint) v;
  setcwdit (cw_speed, cw_space, cw_pitch, cw_shape);
}


void
on_shape_changed                       (GtkSpinButton   *spinbutton,
                                        gpointer         user_data)
{
  gdouble v;
  v = gtk_spin_button_get_value (spinbutton);
  cw_shape = (guint) v;

  setcwdit (cw_speed, cw_space, cw_pitch, cw_shape);
}


void
cwdit_init (void)
{
  GtkWidget *w;
  gdouble v;

  w = lookup_widget (GTK_WIDGET (window1), "SpeedSpin");
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (w), (gdouble) cw_speed);

  w = lookup_widget (GTK_WIDGET (window1), "SpaceSpin");
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (w), (gdouble) cw_space);

  w = lookup_widget (GTK_WIDGET (window1), "PitchSpin");
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (w), (gdouble) cw_pitch);

  w = lookup_widget (GTK_WIDGET (window1), "ShapeSpin");
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (w), (gdouble) cw_shape);

}

/* ---------------------- Tx -------------------------------------*/

void
on_Tx_toggled (GtkToggleButton * togglebutton, gpointer user_data)
{

  Txflag = gtk_toggle_button_get_active (togglebutton);

  if (Txflag)
    gtk_widget_set_sensitive (lookup_widget
			      (GTK_WIDGET (window1), "Training_button"),
			      FALSE);
  else
    gtk_widget_set_sensitive (lookup_widget
			      (GTK_WIDGET (window1), "Training_button"),
			      TRUE);

}

/* ---------------------- load/save conf -----------------------------*/

void
save_data (void)
{
  FILE *fd;
  gint i;
  gchar filename[256];

  strcpy(filename,getenv("HOME"));
  strcat(filename,"/.cwstation.conf");
  
  fd = fopen(filename, "w");
  if(fd==NULL) return;

  fprintf (fd, "cws0001\n");
  fprintf (fd, "Cw %d %d %d %d\n", cw_speed, cw_space, cw_pitch, cw_shape);
  fprintf (fd, "Training %d %d %d %s\n", tr_min, tr_max, tr_len, tr_alpha);
  for (i = 0; i < 8; i++)
    {
      fprintf (fd, "Macro %s %s\n", mbuttonname[i], macrostr[i]);
    }

  fclose (fd);
}

void
load_data (void)
{
  FILE *fd;
  gint i;
  gchar filename[256];

  strcpy(filename,getenv("HOME"));
  strcat(filename,"/.cwstation.conf");
  
  fd = fopen(filename, "r");

  if (fd != NULL)
    {

      fscanf (fd, "cws0001\n");

      fscanf (fd, "Cw %d %d %d %d\n", &cw_speed, &cw_space, &cw_pitch, &cw_shape);

      fscanf (fd, "Training %d %d %d %s\n", &tr_min, &tr_max, &tr_len, &tr_alpha);

      for (i = 0; i < 8; i++)
	{
	  guint n;

	  fscanf (fd, "Macro %s ", &(mbuttonname[i]));
	  fgets ((char *)&(macrostr[i]), 256, fd);
	  n = strlen (macrostr[i]);
	  if (n == 0)
	    continue;
	  if (macrostr[i][n - 1] == '\n')
	    macrostr[i][n - 1] = '\0';
	  else
	    macrostr[i][n] = '\0';
	}
      fclose (fd);
    }

  cwdit_init ();
  macrobutton_set ();

}

/*-------------- save & quit ------------------------------*/
void
savequit                               (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
save_data();
gtk_main_quit();
}

