// gRascal, the Advanced Scientific CALculator
// Copyright (C) 2001, Sebastian Ritterbusch (Rascal@Ritterbusch.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 <fstream.h>

#include "calterm.cpp"

static void rascal_load(string a,Calterm &b)
{
   FILE *in=fopen(a.c_str(),"rt");
   while(!feof(in))
   {
      char buffer[1025];
      int n=fread(buffer,1,1024,in);
      if(n>0)
      {
         buffer[n]=0;
         b.send_command(buffer);
      }   
   }
   fclose(in);
}

static void rascal_save(string a,Calterm &c)
{
   FILE *out=fopen(a.c_str(),"wt");

   string b=c.do_command("variables;functions;\n");
   int i;
   for(i=0;i<b.length();i++)
   {
      int userfunc=0;
      int j=i;
      for(;i<b.length() && b[i]!='\n' && b[i]!='\r';i++)
         if(b[i]=='=')
            userfunc=1;
      if(userfunc)
      {
         fwrite(b.c_str()+j,i-j,1,out);
         if(b[i-1]!=';')
            fwrite(";\n",2,1,out);
         else
            fwrite("\n",1,1,out);
      }
   }
   fclose(out);
}

static void window_example(Calterm & a)
{
   GtkWidget *window;
   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   gtk_widget_show  (window);
}

int exist(string filename)
{  
   ifstream i(filename.c_str());
   return !(i.fail());
}

int main(int argc,char **argv)
{
   Calterm a(string("grascal ")+string(VERSION),"/usr/local/bin/rascal","--shellcolour 4 --outputcolour 0");

   if(exist("./rascal"))
      a.setClientPath("./rascal");
   
   if(!exist(a.ClientPath()))
   {
      if(exist("../rascal"))
         a.setClientPath("../rascal");
   }   
   
   a.addMenu(new MenuTop("/_File"));
   a.addMenu(new MenuFileSelect("/File/_Load","<control>L","Load Rascal File",rascal_load));
   a.addMenu(new MenuFileSelect("/File/_Save","<control>S","Save Rascal File",rascal_save));
   a.addMenu(new MenuSeparator("/File/sep1"));
   a.addMenu(new MenuGtkFunction("/File/_Quit","<control>Q",(GtkItemFactoryCallback)gtk_main_quit));

   a.addMenu(new MenuTop("/_Show"));
   a.addMenu(new MenuSimpleSend("/Show/_Variables","","variables;\n"));
   a.addMenu(new MenuSimpleSend("/Show/_Functions","","functions;\n"));
   a.addMenu(new MenuSimpleSend("/Show/_Output Precision","","ShowPrecision;\n"));

//   a.addMenu(new MenuTop("/_Options"));
//   a.addMenu(new MenuFunction("/Options/Open Example Window","",window_example));

   a.addMenu(new MenuTop("/_Examples"));
   a.addMenu(new MenuSimpleSend("/Examples/_Defining a function","","f(x)=sin(x)+(x*x);\nf(2)\n"));
   a.addMenu(new MenuSimpleSend("/Examples/_Inverting a matrix","","1/[1 2;3 4]\n"));
   a.addMenu(new MenuSimpleSend("/Examples/I_teration","","x=0;for(k=1;k<=10;k++) x=x+k\n"));

   a.addMenu(new MenuTopRightAlign("/_Help"));
   a.addMenu(new MenuSimpleSend("/Help/Introduction","","help;\n"));
   a.addMenu(new MenuSimpleSend("/Help/Modules","","modules;\n"));
   a.addMenu(new MenuSimpleSend("/Help/Index","","help index;\n"));
   a.addMenu(new MenuSimpleSend("/Help/Licence","","licence;\n"));
   a.addMenu(new MenuSimpleSend("/Help/Changelog","","help changes;\n"));
   a.addMenu(new MenuSeparator("/Help/sep2"));
   a.addMenu(new MenuShowUrl("/Help/Manual","","file:/usr/local/share/doc/rascal/html/index.html"));
   a.addMenu(new MenuSeparator("/Help/sep1"));
   a.addMenu(new MenuShowUrl("/Help/Rascal Homepage","","http://rascal.sourceforge.net/"));
         
   a.start(&argc,&argv);
   return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1