/* c.dialogue */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "swis.h"
#include "flex.h"
#include "os.h"
#include "wimp.h"
#include "wimpt.h"
#include "win.h"
#include "event.h"
#include "dbox.h"
#include "werr.h"
#include "txt.h"
#include "txtedit.h"
#include "$.aim_kern.h.window"
#include "$.aim_kern.h.parse"

#define CANCEL 29
#define DEFAULT 1
#define QUERY 2
#define D_CMD 3
#define HELP 31

#define Q_CANCEL 29
#define Q_OK 30
#define Q_HLP 31
#define Q_ARG 0
#define Q_RNG 8
#define Q_VAL 16
#define Q_CMD 28
#define Q_TITLE 32

int num_arg;
int qh;

extern int max_image;
extern BOOL dbox_semaphor;
extern dbox d;
extern txt helpwnd;
extern char help_file[];
extern int cmd_window;
extern long help_index[];

char * prt_help(long offset,char* str);
BOOL query_new(dbox dq,char * cmd,char *hbuff,char * rest);

 BOOL dialogue(char * rest,char * command)
{
  void txt_resize(txt hwnd, char *ch);
  BOOL is_argument(char *buf,int buflen);
  wimp_wstate state;
  wimp_eventstr *e;
  BOOL flag=FALSE,done=FALSE,help=FALSE,persist=FALSE;
  dbox_field reply;
  int i;
  FILE *hfp;
  char buf[12], *hbuff, *hptr;

   hbuff=(char*) calloc(2000,sizeof(char));
   dbox_semaphor=TRUE;
   query_new(d,command,hbuff,rest);
   dbox_show(d);
   dbox_setfield(d,Q_HLP,"Help");
   while(!done)
    {
     reply = dbox_fillin(d);

     switch ( reply ) {

       case  dbox_CLOSE:
                       rest[0]='\0';
                       flag=FALSE;
                       done=TRUE; 
                       break;

       case  Q_OK:  
                       rest[0]=' ';rest[1]='\0';
                       for (i=0; i<num_arg;i++)
                        {
                          dbox_getfield(d,Q_VAL+i,buf,12);
                          if (is_argument(buf,12))
                              {
                               strcat(rest,buf);
                              }
                          strcat(rest,",");
                        }
                       rest[strlen(rest)-1]='\0';
                       flag=TRUE;
                       done=TRUE;
                       break;                       
   /*
      case  QUERY:    
                       done=query_new(command,hbuff,rest);
                       rest[0]=' ';rest[1]='?';rest[2]='\0';
                       flag=TRUE;
                       done=TRUE;
                       break;
*/
       case CANCEL:   
                       rest[0]='\0';
                       flag=FALSE;
                       done=TRUE;
       case HELP:      
                       if (help)
                          {
                           txt_hide(helpwnd);
                           help=FALSE;
                           dbox_setfield(d,Q_HLP,"Help");
                           break;
                          }
                       i = stablk(command, 0);
                       hptr=prt_help(help_index[cmd_help(i)],hbuff);
                       if (*hptr== '\0') strcpy(hbuff,"No help text found\n");
                       txt_bufsize(helpwnd);
                       txt_settitle(helpwnd,command);
                       txt_show(helpwnd);
                       txt_setcharoptions(helpwnd,txt_CARET,0);
                       txt_delete(helpwnd,10000);
                       txt_insertstring(helpwnd,hptr);
                       txt_resize(helpwnd,hptr);
                       help=TRUE;
                       dbox_setfield(d,Q_HLP,"Hide help");
                       break;

      }
    }
  txt_hide(helpwnd);
  dbox_hide(d);

               /* Force command window to front */
 wimpt_noerr(wimp_get_wind_state(cmd_window, &state));
 state.o.behind = -1;
 wimp_open_wind(&state.o);
 free(hbuff);
 return(flag);
}

BOOL is_argument(char *s,int len)
{    
/* Test whether the argument is a valid 'aim argument' 
    valid -> return TRUE
    invalid -> return FALSE
*/

 size_t l;
 char *sptr;
 int i;

 sptr=s;
 l=strlen(sptr);
 l=min(l,len); 
/* if argument length == 1 everything is ok */
 if (l <=1) return(TRUE);                     
/* if argument length > 1 and not a number -> invalid argument */
 if (l >1)  
   {
     for (i=0;i < l;i++) 
       {
        if (!isdigit(*sptr) && (*sptr != '-' && *sptr != '+')) return(FALSE);
        sptr++;
       }
   }
/* Otherwise all ok */
 return(TRUE);
}

void txt_resize(txt hwnd, char *s)
{ 
 /* Adjust the help-window-height to the help-text-height */
 wimp_wstate ste;
 os_error *err;
 int i,qh,linefeeds,high;
 size_t l;
 char * sptr;
/* calculate number of line feeds in string s */
 linefeeds=0;
 sptr=s;
 l=strlen(sptr);
  for (i=0; i <l;i++)
   {
    if (*sptr++ == '\n')linefeeds++;
   }    
linefeeds=min(linefeeds,18);
/* calculate help window height */
high=linefeeds*8*4;
/* resize help window */
 qh=txt_syshandle(hwnd);
 err=wimpt_complain(wimp_get_wind_state(qh,&ste));
 ste.o.box.y0=ste.o.box.y1-high;
 err=wimpt_complain(wimp_open_wind((wimp_openstr *)&ste.o));
 }

/* prt_help : displays a helpmessage loaded from outside in a text window */

char *
prt_help(long offset,char* str)
{
FILE *help_fd;
char fil[80],*ptr; 
extern char help_file[100]; 
*str='\0';
 strcpy(fil,help_file);
 strcat(fil,"AIMHELP");
 if ( (help_fd = fopen(fil,"r") ) == NULL)
  return str;
 fseek(help_fd, offset, SEEK_SET);
 fread(str,2000,1,help_fd);
 ptr=strchr(str,'$');
 if (ptr != NULL) *ptr='\0';
 fclose(help_fd);
 return str;
}
#define _LEN 127
#define _FLD 39
BOOL query_new(dbox dq,char * command,char *hbuff,char * rest)
{
  char cmd[8][_FLD+1],val[8][_FLD+1],rng[8][_FLD+1],c[_LEN+1],d[_LEN+1],r[_LEN+1],*ptr;
  char *hptr,*pc,*pd,*pr,*scr,scr1[_FLD+1];
  char range[3][10];
  int i;
  BOOL done;
  wimp_iconflags flg,btype;
  wimp_winfo inf;
  wimp_wstate ste;
  wimp_icon ic;
  os_error *err;
  

  num_arg=0;
  i = stablk(command, 0);
  hptr=prt_help(help_index[cmd_help(i)],hbuff);
  if (*hptr== '\0') return FALSE;
  strcpy(range[0],"<A-B>");
  strcpy(range[1],"<A-C>");  
  strcpy(range[2],"<A-D>");

  scr=scr1;
  for (i=0; i<8;i++)
    {
     cmd[i][0]= '\0';
     rng[i][0]= '\0';
     val[i][0]= '\0';
    }
 

 qh=dbox_syshandle(dq);
/*
 inf.w=qh;
 err=wimpt_complain(wimp_get_wind_state(qh,&ste));

  wprintf("x0=%d y1=%d x1=%d y0=%d\n",ste.o.box.x0,ste.o.box.y1,ste.o.box.x1,ste.o.box.y0); 
 */
 btype=0x0000F000;
 flg=wimp_INOSELECT+wimp_IDELETED; 
 for (i=0;i<8;i++)
   {
wimpt_complain(wimp_set_icon_state(qh,i,wimp_IFILLED,wimp_IFILLED));
wimpt_complain(wimp_set_icon_state(qh,i+8,wimp_IFILLED,wimp_IFILLED));
wimpt_complain(wimp_set_icon_state(qh,i+16,wimp_IFILLED,wimp_IFILLED));

wimpt_complain(wimp_set_icon_state(qh,i,0,flg));
wimpt_complain(wimp_set_icon_state(qh,i+8,0,flg));
wimpt_complain(wimp_set_icon_state(qh,i+16,0,flg));
   }

  ptr=strchr((const char *)hptr,':');ptr++;
  strncpy(c,ptr,_LEN);
  ptr=strchr(c,'\n');*ptr='\0';
  dbox_setfield(dq, Q_TITLE,c);

  ptr=strstr((const char *)hptr,"command :");
  hptr=ptr+1;
  strncpy(c,ptr,127);
  pc=strchr(c,':');
  if (pc != NULL)
  {
   pc++;
   ptr=strchr(pc,'\n');*ptr='\0';
   while(*pc == ' ' && pc < (c+_LEN))pc++;
   while(*pc != ' ' && pc < (c+_LEN))pc++;
  }

  ptr=strstr((const char *)hptr,"default");
  hptr=ptr+1;
  strncpy(d,ptr,_LEN);  
  pd=strchr(d,':');
  if (pd != NULL)
  {
   pd++;
   ptr=strchr(pd,'\n');*ptr='\0';
   while(*pd ==' ' && pd < (d+_LEN))pd++;
   while(*pd != ' ' && pd < (d+_LEN))pd++;
  }

  ptr=strstr((const char *)hptr,"range");
  strncpy(r,ptr,_LEN);
  pr=strchr(r,':');
  if (pr != NULL)
  {
   pr++;
   ptr=strchr(pr,'\n');*ptr='\0';
   while(*pr == ' ' && pr < (r+_LEN))pr++;
   while(*pr != ' ' && pr < (r+_LEN))pr++;
  }

  dbox_setfield(dq, Q_CMD, command);
  for (i=Q_ARG;i<Q_VAL+8;i++) dbox_setfield(dq,i,"");


/*  strncpy(cmd[0],ptr,_LEN); */
  for (i=0;i<8;i++)
    {
     if (i==0) ptr=strtok(pc,",\n");
     else ptr=strtok(NULL,",\n");
     if (ptr==NULL) break;
     strncpy(cmd[i],ptr,_FLD);   
    }
   num_arg=i;

  ptr=strtok(pr,",");
  strncpy(rng[0],ptr,_FLD);
  for (i=1;i<num_arg;i++)
    {
     ptr=strtok(NULL,",\n");
     if (ptr==NULL) break;
     strncpy(rng[i],ptr,_FLD);
    }

  ptr=strtok(pd,",");
  strncpy(val[0],ptr,_FLD);
  for (i=1;i<num_arg;i++)
    {
     ptr=strtok(NULL,",\n");
     if (ptr==NULL) break;
     strncpy(val[i],ptr,_FLD);
    }


  for (i=0;i<num_arg;i++)
    {
     if (cmd[i] != NULL)
      {
       scr=scr1;
       strncpy(scr,cmd[i],_FLD);
       ptr=scr;
       while(*(ptr)==' ' && ptr < (scr+_LEN))ptr++;
       scr=ptr;
       ptr=scr+strlen(scr)-1;
       while(*ptr ==' '  && ptr > scr)ptr--;
       ptr++;
       *ptr='\0';
       dbox_setfield(dq,Q_ARG+i,scr);
      }
    }


  for (i=0;i<num_arg;i++)
    {
     if (rng[i] != NULL)
      {
       scr=scr1;
       strncpy(scr,rng[i],_FLD);
       ptr=scr;
       while(*(ptr)==' ' && ptr < (scr+_LEN))ptr++;
       scr=ptr;
       ptr=scr+strlen(scr)-1;
       while(*ptr ==' '  && ptr > scr)ptr--;
       ptr++;
       *ptr='\0';
       if (strcmp(scr,"<A-D>")==0) strcpy(scr,range[max_image-2]);
       dbox_setfield(dq,Q_RNG+i,scr);
      }
    }


  for (i=0;i<num_arg;i++)
    {
     if (val[i] != NULL)
      {
       scr=scr1;
       strncpy(scr,val[i],_FLD);
       ptr=scr;
       while(*(ptr)==' ' && ptr < (scr+_LEN))ptr++;
       scr=ptr;
       ptr=scr+strlen(scr)-1;
       while(*ptr ==' ' && ptr > scr)ptr--;
       ptr++;
       *ptr='\0';
       dbox_setfield(dq,Q_VAL+i,scr);
      }
    }

 for (i=num_arg;i<8;i++)
   {
   wimpt_complain(wimp_set_icon_state(qh,i,0,wimp_IFILLED));
   wimpt_complain(wimp_set_icon_state(qh,i+8,0,wimp_IFILLED));    
   wimpt_complain(wimp_set_icon_state(qh,i+16,0,wimp_IFILLED));

   wimpt_complain(wimp_set_icon_state(qh,i,flg,flg));
   wimpt_complain(wimp_set_icon_state(qh,i+8,flg,flg));    
   wimpt_complain(wimp_set_icon_state(qh,i+16,flg,flg));   
   }
/*
 inf.w=qh;
 err=wimpt_complain(wimp_get_wind_info(&inf));
 wprintf("no. icons=%d\n",inf.info.nicons);
 err=wimpt_complain(wimp_get_icon_info(qh,15,&ic));
 wprintf("icon flag=%x %x\n",ic.flags,ic.flags&0x000F0000);

 err=wimpt_complain(wimp_get_icon_info(qh,num_arg+16,&ic));
 wprintf("icon flag=%x %x\n",ic.flags,ic.flags&0x0000F000);
 err=wimpt_complain(wimp_get_icon_info(qh,num_arg+15,&ic));
 wprintf("icon flag=%x %x\n",ic.flags,ic.flags&0x0000F000);
*/
return FALSE;
}
