/*  Note: You are free to use whatever license you want.
    Eventually you will be able to edit it within Glade. */

/*  xsteak
 *  Copyright (C) 1999 Razi Lotfi-Tabrizi (razi@cs.uni-frankfurt.de)
 *
 *  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 "gladesrc.h"
#include "gladesig.h"
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <stdio.h>
#include <gtk/gtk.h> 
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include "pinguin_steak_icon.xpm"


#define MAXLINE 1024
/* #define DICTPROGRAM "woerterbuch" */
#define DICTPROGRAM "steak"
#define HELPFILE "xhelp.txt"

int kontext_variable=0;
int german_variable=0;
int english_variable=0;
int case_sensitive_variable=0;
int normal_and_or=0;  /* 1 steht fuer AND, 2 fuer OR und 0 fuer gar keine Verbindung */

int del_variable=0;
int help_fenster_anzahl=0;


void terminate(int test) /* Routine fuer SIGTERM & SIGINT */
{
   
   fprintf(stderr,"The program was killed or canceled !\n");
   gtk_exit(0);
}



int
main (int argc, char *argv[])
{
   GtkWidget *window1;
   GdkPixmap *icon_pixmap = NULL;
   GdkBitmap *icon_mask = NULL;
   GtkStyle *icon_style;
 
   /* SIGTERM  & SIGINT Routine */
   signal(SIGTERM,terminate);
   signal(SIGINT,terminate);

   gtk_set_locale ();
   gtk_init (&argc, &argv);
   gtk_rc_parse(".xsteakconfig");

   /*
    * The following code was added by Glade to create one of each component
    * (except popup menus), just so that you see something after building
    * the project. Delete any components that you don't want shown initially.
    */
   window1 = create_window1 ();

   icon_style = gtk_widget_get_style(window1);
   icon_pixmap = gdk_pixmap_create_from_xpm_d(window1->window, &icon_mask,
                                              &icon_style->bg[GTK_STATE_NORMAL], 
                                              (gchar **)pinguin_steak_icon_xpm);
   gdk_window_set_icon(window1->window, NULL, icon_pixmap, icon_mask);  
   
   gdk_window_set_icon_name (window1->window, "Steak Dictionary");

/*    gdk_window_set_decorations (window->window, GDK_DECOR_ALL | GDK_DECOR_MENU); */
/*    gdk_window_set_functions (window->window, GDK_FUNC_ALL | GDK_FUNC_RESIZE); */
  

   gtk_widget_show (window1);

   gtk_main ();
   return 0;
}


void
on_window1_destroy                     (GtkObject       *object,
                                        gpointer         user_data)
{
  gtk_exit(0);
}


void
on_buttonSearch_clicked                (GtkButton       *button,
                                        gpointer         user_data)
{
   char *prog=DICTPROGRAM;
   char *eingabewort00=gtk_entry_get_text(GTK_ENTRY(user_data));
   char kkkk[3];
   char sprache[4];
   char nichtsandor[5];
   int nnnn;
   FILE *fp;
   char command[MAXLINE];
   char line[MAXLINE];
   int del_variable_bak=0;   
   GdkColor blau_colour,rot_colour,gruen_colour;
   GdkColormap *cmap_blue, *cmap_green, *cmap_red;
   GtkWidget *text1, *window1;
   
   
   window1=gtk_widget_get_toplevel ((GtkWidget*) button);
   text1=gtk_object_get_data(GTK_OBJECT(window1),"text1");

   /* Get the system colour map and allocate the colour blue */
   cmap_blue = gdk_colormap_get_system();
   blau_colour.red = 0;
   blau_colour.green = 0;
   blau_colour.blue = 0xffff;
   if (!gdk_color_alloc(cmap_blue, &blau_colour)) 
   {
      g_error("couldn't allocate colour");
   }
   /* Get the system colour map and allocate the colour green */
   cmap_green = gdk_colormap_get_system();
   gruen_colour.red = 0;
   gruen_colour.green = 0xffff;
   gruen_colour.blue = 0x6000;
   if (!gdk_color_alloc(cmap_green, &gruen_colour)) 
   {
      g_error("couldn't allocate colour");
   }
   /* Get the system colour map and allocate the colour red */
   cmap_red = gdk_colormap_get_system();
   rot_colour.red = 0xffff;
   rot_colour.green = 0;
   rot_colour.blue = 0;
   if (!gdk_color_alloc(cmap_red, &rot_colour)) {
      g_error("couldn't allocate colour");
   }



   if (kontext_variable == 1)
   {
      strcpy(kkkk, "+k");
   }
   else
   {
      strcpy(kkkk, "-k");
   }


   if ((english_variable == 1) && (german_variable == 1))
   {
      strcpy(sprache, "-de");
   }
   else if ((english_variable == 0) && (german_variable == 1))
   {
      strcpy(sprache, "-d");
   }
   else
   {
      strcpy(sprache, "-e");
   }


   if (normal_and_or == 1)
   {
      strcpy(nichtsandor, "-and");
   }
   else if (normal_and_or == 2)
   {
      strcpy(nichtsandor, "-or");
   }
   else
   {
      strcpy(nichtsandor, sprache);
   }
      

   if (case_sensitive_variable == 1)
   {
      sprintf(command, "%s %s %s %s %s %s %s", prog, "-nowait", kkkk, sprache, "-noignor", nichtsandor, eingabewort00);
   }
   else
   {
      sprintf(command, "%s %s %s %s %s %s", prog, "-nowait", kkkk, sprache, nichtsandor, eingabewort00);
   }
   

   gtk_text_freeze(GTK_TEXT(text1));
   gtk_text_backward_delete  (GTK_TEXT(text1), (guint) del_variable);
   if ( (fp = popen(command, "r")) == NULL)
   {
      fprintf(stderr,"popen error");
      gtk_exit(0);
   }
   
   while ((fgets(line, MAXLINE, fp)) != NULL)
   {
      nnnn = strlen(line);
      if (strncmp("--------------------------------------------------",line,50) != 0)
      {
         if (strncmp("---------- Jetzt die Kontext-Eintraege ----------",line,49) == 0)
         {
            strcpy(line,"---------------- Jetzt die Kontext-Eintraege ----------------\n");
            nnnn = strlen(line);
            del_variable_bak = del_variable_bak + nnnn;
            gtk_text_insert(GTK_TEXT(text1),NULL,&gruen_colour,NULL,line,nnnn);
         }
         else if((strncmp("          English     ->     Deutsch",line,36) == 0) || (strncmp("          Deutsch     <->     English",line,37) == 0) || (strncmp("          Deutsch     ->     English",line,36) == 0))
         {
            del_variable_bak = del_variable_bak + nnnn;
            gtk_text_insert(GTK_TEXT(text1),NULL,&blau_colour,NULL,line,nnnn);
         }
         else if((strncmp("Das Wort \"",line,10) == 0) || (strncmp("Kein Wort wurde mit dem Praefix \"",line,33) == 0 ))
         {
            del_variable_bak = del_variable_bak + nnnn;
            gtk_text_insert(GTK_TEXT(text1),NULL,&rot_colour,NULL,line,nnnn);
         }
         else if ((strncmp("Die Woerter mit Praefix \"",line,25) == 0))
         {
            del_variable_bak = del_variable_bak + nnnn;
            gtk_text_insert(GTK_TEXT(text1),NULL,NULL,NULL,line,nnnn);
            strcpy(line,"----------------------------------------------------\n");
            nnnn = strlen(line);
            del_variable_bak = del_variable_bak + nnnn;
            gtk_text_insert(GTK_TEXT(text1),NULL,&gruen_colour,NULL,line,nnnn);
            
         }            
         else            
         {
            del_variable_bak = del_variable_bak + nnnn;
            gtk_text_insert(GTK_TEXT(text1),NULL,NULL,NULL,line,nnnn);
         }
         
      }
   }
   
   del_variable = del_variable_bak;
   
   if (ferror(fp))
      printf("fgets error");
   pclose(fp);
   gtk_text_thaw(GTK_TEXT(text1));
}


void
on_englishbutton2_toggled              (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
   if (english_variable == 0)
   {
      english_variable = 1;
   }
   else
   {
      english_variable = 0;
   }
}


void
on_or_checkbutton_toggled              (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
   if (GTK_TOGGLE_BUTTON (togglebutton)->active)
   { 
      normal_and_or=2;
      if (GTK_TOGGLE_BUTTON (user_data)->active)
      {
         gtk_toggle_button_set_state(user_data, FALSE);
      }
   }
   else if ((GTK_TOGGLE_BUTTON (user_data)->active) != 1)
   {
      normal_and_or=0;
   }
}


void
on_and_checkbutton_toggled             (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
   if (GTK_TOGGLE_BUTTON (togglebutton)->active)
   {
      normal_and_or=1;
      if (GTK_TOGGLE_BUTTON (user_data)->active)
      {
         gtk_toggle_button_set_state(user_data, FALSE);
      }
   }
   else if ((GTK_TOGGLE_BUTTON (user_data)->active) != 1)
   {
      normal_and_or=0;
   }
}


void
on_casesensitive_toggled               (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
   if (case_sensitive_variable == 0)
   {
      case_sensitive_variable = 1;
   }
   else
   {
      case_sensitive_variable = 0;
   }
}


void
on_germanbutton1_toggled               (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
   if (german_variable == 0)
   {
      german_variable = 1;
   }
   else
   {
      german_variable = 0;
   }
}


void
on_buttonHelp_clicked                  (GtkButton       *button,
                                        gpointer         user_data)
{
   if (help_fenster_anzahl == 0)
   {
      GtkWidget *help_window;
      GtkWidget *text2;
/*       FILE *fp; */
/*       char command[MAXLINE]; */
/*       char line[MAXLINE]; */
/*       int nnnn; */
      
      help_window = create_help_window ();
      gtk_widget_show (help_window);
      help_fenster_anzahl = 1;


      text2=gtk_object_get_data(GTK_OBJECT(help_window),"text2");
   
/*       sprintf(command, "%s %s", "cat", HELPFILE); */
/*       gtk_text_freeze(GTK_TEXT(text2)); */
/*       if ( (fp = popen(command, "r")) == NULL) */
/*       { */
/*          fprintf(stderr,"popen error"); */
/*          gtk_exit(0); */
/*       } */
   
/*       while ((fgets(line, MAXLINE, fp)) != NULL) */
/*       { */
/*          nnnn = strlen(line); */
/*          gtk_text_insert(GTK_TEXT(text2),NULL,NULL,NULL,line,nnnn); */
/*       } */
      
/*       if (ferror(fp)) */
/*          printf("fgets error"); */
/*       pclose(fp); */
/*       gtk_text_thaw(GTK_TEXT(text2)); */
   }
}

void
on_button2_clicked                     (GtkButton       *button,
                                        gpointer         user_data)
{
   if (help_fenster_anzahl == 1)
   {
      help_fenster_anzahl = 0;
   }
}






void
on_checkbutton3_toggled                (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
   if (kontext_variable == 0)
   {
      kontext_variable = 1;
   }
   else
   {
      kontext_variable = 0;
   }
}

