
/******************************************************************************
 *
 * RCS ID
 * $Id: gadgets,v 1.6 2000/02/20 17:55:30 david Exp $
 *
 * HISTORY
 * $Log: gadgets,v $
 * Revision 1.6  2000/02/20 17:55:30  david
 * Added upperbound, lowerbound and step size to parmStruct - used by
 * P_INTETER parameters.
 *
 * Revision 1.5  2000/02/08 20:18:16  david
 * Added help text for parameters.
 *
 * Revision 1.4  2000/01/29 17:24:46  david
 * Changed message and event handler registering to be driver by tables.
 *
 * Revision 1.3  2000/01/11 20:17:54  david
 * Added a define to split out booleans into General, File Create
 * and Filename read options. Not released yet - just playing with it.
 * Checked that !smbconfig will compile standalone.
 *
 * Revision 1.2  1999/11/23 22:04:02  david
 * Increased filename length to MAXPATHLEN
 *
 * Revision 1.1  1999/11/07 15:15:19  david
 * New WIMP configuration using toolbox libs
 *
 *
 *
 *****************************************************************************/                    

#include <stdlib.h>
#include <string.h>
#include <stdio.h>  
#include "kernel.h"
#include "swis.h"
#include "wimp.h"
#include "toolbox.h"
#include "event.h"
#include "wimplib.h"
#include "proginfo.h"
#include "quit.h"   
#include "gadgets.h"
#include "version.h"


#include "includes.h"
#include "smb.h"

#include "smbconfig.h"



tAddGadgetFncs addGadgetFncs[] =
{
  { P_BOOL,     addOptionButtonGadget },
  { P_BOOLREV,  NULL },
  { P_CHAR,     NULL },
  { P_INTEGER,  addNumberRangeGadget },
  { P_OCTAL,    NULL },
  { P_STRING,   addWriteableFieldGadget },
  { P_USTRING,  addWriteableFieldGadget },
  { P_GSTRING,  addWriteableFieldGadget },
  { P_UGSTRING, addWriteableFieldGadget },
  { P_ENUM,     addRadioGadget },
  { P_SEP,      NULL }
};
                                       

#define MAX_BOOL_COUNT 3

parm_type paramsOrder[] = {
     P_STRING,P_USTRING,P_GSTRING,P_UGSTRING,
     P_INTEGER,P_OCTAL,
     P_CHAR,       
     P_BOOL, 
#ifdef SPLIT_BOOLEANS
     P_BOOL,P_BOOL,  
#endif
     P_BOOLREV,
     P_ENUM,
     P_SEP
};

typedef struct sParamsBoolOrder {
    char *optName;
    int  flag;
    int  notFlag; 
} tParamsBoolOrder;

tParamsBoolOrder boolOrder[] = 
{
  { "General Options", FLAG_USER_EDIT, FLAG_FILE_CREATE | FLAG_FILE_READ  },
  { "Filename Creation Options", FLAG_FILE_CREATE, 0 },
  { "Filename Read Options", FLAG_FILE_READ, 0 },
  { NULL , 0 }
};

char *help_message="hello"; 
char *cancels="Cancel";
char *oks="OK"; 
char *Sharddisc  ="Sfile_bda\0\0\0";
char *Sprinters  ="S!printers";
char *Ssmharddisc="Ssmall_bda";
char *Ssmprinters="Ssm!printers";

void setHelpMessage(GadgetHeader *header, char *helpMsg)
{                                    
  if (helpMsg==NULL)
  {
    header->help_message = help_message;
    header->max_help     = sizeof(help_message)+1;
  }
  else 
  {
    header->help_message = helpMsg;
    header->max_help     = strlen(helpMsg);
    DEBUG(5,("help msg len %d, help msg %s\n",
          header->max_help, header->help_message));
  }  
}

void sortGadgetList(tpGadgetList* gadgetList, 
                    int offset,
                    int (*cmpFnc)(const void *, const void *))
{
   tpGadgetList iGadget,iPrevGadget,jGadget,jPrevGadget;
   tGadgetList aGadget;

   iGadget=*gadgetList;
   iPrevGadget=NULL;

   while (iGadget!=NULL)
   {
      jGadget=iGadget->next;
      jPrevGadget=iPrevGadget;
      while (jGadget!=NULL)
      {
         if (cmpFnc((const char*)((int)jGadget+offset),
                        (const char*)((int)iGadget+offset))<0)
         {

            /* swap the data but *not* the pointers */
            memcpy(&aGadget,jGadget,offsetof(tGadgetList,prev));
            memcpy(jGadget,iGadget,offsetof(tGadgetList,prev));
            memcpy(iGadget,&aGadget,offsetof(tGadgetList,prev));           

         }                 
         jPrevGadget=jGadget;
         jGadget=jGadget->next;
      }                    
      iPrevGadget=iGadget;
      iGadget=iGadget->next;
   } 
}

void addGadget(tpGadgetList* gadgetList, tpGadgetList newGadget)
{      
   tpGadgetList aGadget;

   if (*gadgetList==NULL)
   {
      *gadgetList=newGadget;  
      newGadget->prev=NULL;
   }
   else
   {
      aGadget=*gadgetList;
      while (aGadget->next!=NULL)
      {
         aGadget=aGadget->next;
      }  
      aGadget->next=newGadget;
      newGadget->prev=aGadget;
   }
}

void deleteGadget(tpGadgetList* gadgetList, tpGadgetList aGadget)
{      


   if (aGadget==*gadgetList)
   {  
      *gadgetList=aGadget->next; 
#if 0
      if (aGadget->next)
         *gadgetList=aGadget->next; 
      else
         *gadgetList=NULL;
#endif

      if ( *gadgetList )
         (*gadgetList)->prev=NULL;
   }
   else
   {  
      aGadget->prev->next=aGadget->next;   

      if (aGadget->next)
         aGadget->next->prev=aGadget->prev;
#if 0
      aGadget=*gadgetList;
      while (aGadget->next!=NULL)
      {
         aGadget=aGadget->next;
      }  
      aGadget->next=newGadget;
      newGadget->prev=aGadget;  
#endif
   }
}

tpGadgetList addLabelBox(ObjectId     objectId,
                         char         *label,
                         int          *x,
                         int          *y,
                         int          *compId,
                         tpGadgetList *gadgetList)
{
   tpGadgetList aGadget;

   aGadget = (tpGadgetList) calloc (1, sizeof(tGadgetList));
   if (aGadget!=NULL)
   {                                                              

      aGadget->index.paramIndex = 0;
      aGadget->agadget.hdr.flags = 0;
      aGadget->agadget.hdr.type   = LabelledBox_Base;
      aGadget->agadget.hdr.box.xmin = 0;
      aGadget->agadget.hdr.box.ymin = -100; 
      aGadget->agadget.hdr.box.xmax = 100;
      aGadget->agadget.hdr.box.ymax = 0;
      aGadget->agadget.hdr.component_id = *compId;
      aGadget->agadget.hdr.help_message = help_message;
      aGadget->agadget.hdr.max_help     = sizeof(help_message)+1;  
 
      aGadget->agadget.data.labelled_box.label = label;

      addGadget(gadgetList,aGadget);

      window_add_gadget(0,objectId,
                        &aGadget->agadget,
                        &aGadget->agadget.hdr.component_id);
                                               
     /* (*y) = (*y) - 60; */
 
      (*compId)++;

   }                        

   return aGadget;
}

void addTextFieldGadget ADD_GADGET_FUNC_PARAMS_DEF 
{     

   tpGadgetList aGadget=NULL;  

   aGadget = (tpGadgetList) calloc (1, sizeof(tGadgetList));
   if (aGadget!=NULL)
   {                                          
      *x = width;
      aGadget->index.paramIndex = index;
      aGadget->agadget.hdr.flags = 0;
      aGadget->agadget.hdr.type   = Button_Base;
      aGadget->agadget.hdr.box.xmin = 10;
      aGadget->agadget.hdr.box.ymin = (*y)-60; 

      if ((*x)==-1)
         aGadget->agadget.hdr.box.xmax = 500;
      else
         aGadget->agadget.hdr.box.xmax = *x;
      
      aGadget->agadget.hdr.box.ymax = (*y)-10;
      aGadget->agadget.hdr.component_id = *compId;
      setHelpMessage(&aGadget->agadget.hdr,parmStruct->helpString);
 
      aGadget->agadget.data.button.button_flags=
      WimpIcon_Text | WimpIcon_Indirected | WimpIcon_RJustified 
      /* WimpIcon_VCentred */
      | (WimpIcon_FGColour * 7) | (WimpIcon_BGColour * 1); 
      aGadget->agadget.data.button.value=parmStruct->label;
      aGadget->agadget.data.button.max_value=strlen(parmStruct->label)+1;
      aGadget->agadget.data.button.validation=NULL;
      aGadget->agadget.data.button.max_validation =0;

      addGadget(gadgetList,aGadget);

      window_add_gadget(0,objectId,
                        &aGadget->agadget,
                        &aGadget->agadget.hdr.component_id);  
      (*compId)++;
                                               
   }            

}

void addOptionButtonGadget ADD_GADGET_FUNC_PARAMS_DEF 
{     
  
   tpGadgetList aGadget;  
   void *ptr;

   aGadget = (tpGadgetList) calloc (1, sizeof(tGadgetList));
   if (aGadget!=NULL)
   {                                          
      if (((*x)+100)>(800))
      {       
         (*x) = 0;
         (*y) = (*y) - 60;
      }       

      aGadget->index.paramIndex = index;
      aGadget->parmStruct = parmStruct;
      aGadget->agadget.hdr.flags = 0;
      aGadget->agadget.hdr.type   = OptionButton_Base;
      aGadget->agadget.hdr.box.xmin = (*x)+50;
      aGadget->agadget.hdr.box.ymin = (*y)-60; 
      aGadget->agadget.hdr.box.xmax = (*x)+width+100;
      aGadget->agadget.hdr.box.ymax = (*y)-10;
      aGadget->agadget.hdr.component_id = *compId;
      setHelpMessage(&aGadget->agadget.hdr,parmStruct->helpString);

      ptr = getParamValuePtr(parmStruct, serviceNum);

      if (*(BOOL*)ptr==True)
      {
         aGadget->agadget.hdr.flags = 0x04;
      }

      aGadget->agadget.data.option_button.label=parmStruct->label;
      aGadget->agadget.data.option_button.max_label_len=strlen(parmStruct->label)+1;
      aGadget->agadget.data.option_button.event=0;
 

      addGadget(gadgetList,aGadget);

      window_add_gadget(0,objectId,
                        &aGadget->agadget,
                        &aGadget->agadget.hdr.component_id);  

      (*compId)++;

      (*x) +=(aGadget->agadget.hdr.box.xmax-aGadget->agadget.hdr.box.xmin);


                                               
   }            

}

void addWriteableFieldGadget ADD_GADGET_FUNC_PARAMS_DEF
{     
  
   tpGadgetList aGadget;  
   void *ptr;

   *x = width;

   addTextFieldGadget(gadgetList,
                      index,
                      objectId,
                      compId,
                      parmStruct,
                      x,
                      y,
                      width,
                      esg,
                      serviceNum);              

   aGadget = (tpGadgetList) calloc (1, sizeof(tGadgetList));
   if (aGadget!=NULL)
   {                                                              
      char *str;

      aGadget->index.paramIndex = index;
      aGadget->parmStruct = parmStruct;
      aGadget->agadget.hdr.flags = 0;
      aGadget->agadget.hdr.type   = WritableField_Base;
      aGadget->agadget.hdr.box.xmin = (*x)+50;
      aGadget->agadget.hdr.box.ymin = (*y)-60; 
      aGadget->agadget.hdr.box.xmax = (*x)+800;
      aGadget->agadget.hdr.box.ymax = (*y)-10;
      aGadget->agadget.hdr.component_id = *compId;  
#if 0
      aGadget->agadget.hdr.help_message = help_message;
      aGadget->agadget.hdr.max_help     = sizeof(help_message)+1;  
#else
      setHelpMessage(&aGadget->agadget.hdr,parmStruct->helpString);
#endif 
      str = (char*)calloc(255,sizeof(char));  

      ptr = getParamValuePtr(parmStruct, serviceNum);
      if (parmStruct->type==P_STRING || parmStruct->type==P_USTRING)
         strncpy(str,*(char **)ptr,255);
      else
         strncpy(str,(char *)ptr,255);

      aGadget->agadget.data.writable_field.text = str;
      aGadget->agadget.data.writable_field.max_text_len=255;
      aGadget->agadget.data.writable_field.allowable=NULL;
      aGadget->agadget.data.writable_field.max_allowable_len=0;
      aGadget->agadget.data.writable_field.before = -1;
      aGadget->agadget.data.writable_field.after  = -1;

      addGadget(gadgetList,aGadget);

      window_add_gadget(0,objectId,
                        &aGadget->agadget,
                        &aGadget->agadget.hdr.component_id);
                                               
      (*y) = (*y) - 60;
 
      (*compId)++;

   }       


                       

}

void addRadioGadget ADD_GADGET_FUNC_PARAMS_DEF
{     
   int i,n,len;
   int width2=0;
   tpGadgetList aGadget,labelGadget;  
   void *ptr;

   *x = 0; 
   i=0;     
   while (parmStruct->enum_list[i].value!=-1)
   {
      len=fontStrWidth(parmStruct->enum_list[i].name)+50;
      if (len>width2)
         width2=len;
      i++;
   }
   n=i;
            

   labelGadget=addLabelBox(objectId,
                           parmStruct->label,
                           x,
                           y,
                           compId,
                           gadgetList);
     
   labelGadget->agadget.hdr.box.xmin = (*x)+25;
   labelGadget->agadget.hdr.box.xmax = (*x)+800+width;
   labelGadget->agadget.hdr.box.ymax = (*y)-10;

   (*y) = (*y) - 60;

   i=0;
   
   while (parmStruct->enum_list[i].value!=-1)
   {
      aGadget = (tpGadgetList) calloc (1, sizeof(tGadgetList));
      if (aGadget!=NULL)
      {                         
         if (((*x)+150+width2)>(800+width))
         {       
            (*x) = 0;
            (*y) = (*y) - 60;
         }                                      

         aGadget->index.paramIndex = index;    
         aGadget->parmStruct = parmStruct;

         ptr = getParamValuePtr(parmStruct, serviceNum);
         if (parmStruct->enum_list[i].value == (*(int *)ptr) )
            aGadget->agadget.hdr.flags = RadioButton_On;
         else
            aGadget->agadget.hdr.flags = 0; 

         aGadget->agadget.hdr.type   = RadioButton_Base;
         aGadget->agadget.hdr.box.xmin = (*x)+50;
         aGadget->agadget.hdr.box.ymin = (*y)-60; 
         aGadget->agadget.hdr.box.xmax = (*x)+width2+100;
         aGadget->agadget.hdr.box.ymax = (*y)-10;
         aGadget->agadget.hdr.component_id = *compId;
         setHelpMessage(&aGadget->agadget.hdr,parmStruct->helpString);  
 
         aGadget->agadget.data.radio_button.group_number = *esg;
         aGadget->agadget.data.radio_button.label = parmStruct->enum_list[i].name;
         aGadget->agadget.data.radio_button.max_label_len = strlen(parmStruct->enum_list[i].name)+1;
         aGadget->agadget.data.radio_button.event = 0;  

         addGadget(gadgetList,aGadget);

         window_add_gadget(0,objectId,
                           &aGadget->agadget,
                           &aGadget->agadget.hdr.component_id);
                                               
 

         (*x) +=(aGadget->agadget.hdr.box.xmax-aGadget->agadget.hdr.box.xmin);
 
         (*compId)++;
      }

      i++;

   }       

   (*y) = (*y) - 60 - 60;                           
                   

   labelGadget->agadget.hdr.box.ymin = *y; 

   gadget_move_gadget(0,
                      objectId,
                      labelGadget->agadget.hdr.component_id,
                      &labelGadget->agadget.hdr.box);


   (*esg)++;
   
 

}


void addNumberRangeGadget ADD_GADGET_FUNC_PARAMS_DEF
{     
   tpGadgetList aGadget;                                
   void *ptr;

   *x = width;
  
   addTextFieldGadget(gadgetList,
                      index,
                      objectId,
                      compId,
                      parmStruct,
                      x,
                      y,
                      width,
                      esg,
                      serviceNum);   
        
   aGadget = (tpGadgetList) calloc (1, sizeof(tGadgetList));

   if (aGadget!=NULL)
   {                                                              

      aGadget->index.paramIndex = index;
      aGadget->parmStruct = parmStruct;
      aGadget->agadget.hdr.flags = NumberRange_Writable | NumberRange_Adjusters ;
      aGadget->agadget.hdr.type   = NumberRange_Base;
      aGadget->agadget.hdr.box.xmin = (*x)+50;
      aGadget->agadget.hdr.box.ymin = (*y)-60; 
      aGadget->agadget.hdr.box.xmax = (*x)+250;
      aGadget->agadget.hdr.box.ymax = (*y)-10;
      aGadget->agadget.hdr.component_id = *compId;
      setHelpMessage(&aGadget->agadget.hdr,parmStruct->helpString);
 
      aGadget->agadget.data.number_range.lower_bound = parmStruct->lowerBound /*0*/;
      aGadget->agadget.data.number_range.upper_bound = parmStruct->upperBound /*10*/;
      aGadget->agadget.data.number_range.step_size   = parmStruct->stepSize   /*1*/;
      ptr = getParamValuePtr(parmStruct, serviceNum);
      aGadget->agadget.data.number_range.initial_value = *(int *)ptr;
      aGadget->agadget.data.number_range.precision = 0;
      aGadget->agadget.data.number_range.before = -1;
      aGadget->agadget.data.number_range.after = -1; 
      aGadget->agadget.data.number_range.display_length=0;

      addGadget(gadgetList,aGadget);

      window_add_gadget(0,objectId,
                        &aGadget->agadget,
                        &aGadget->agadget.hdr.component_id);
                                               
      (*y) = (*y) - 60;
 
      (*compId)++;

   }       

}

void addCancelAndSaveGadgets(tpGadgetList *gadgetList,
                             ObjectId     objectId,
                             int          *compId, 
                             int          *x,
                             int          *y,
                             int          width,
                             parm_class   class)
{
   tpGadgetList aGadget;                 

   (*y) = (*y) - 20;

   aGadget = (tpGadgetList) calloc (1, sizeof(tGadgetList));

   if (aGadget!=NULL)
   {                                                              
      (*x)=0;
      aGadget->index.paramIndex = 0;
      aGadget->agadget.hdr.flags = 2 /* cancel button */ ;
      aGadget->agadget.hdr.type   = ActionButton_Base;
      aGadget->agadget.hdr.box.xmin = width+350;
      aGadget->agadget.hdr.box.ymin = (*y)-70; 
      aGadget->agadget.hdr.box.xmax = width+550;
      aGadget->agadget.hdr.box.ymax = (*y)-10;
      aGadget->agadget.hdr.component_id = *compId;
      aGadget->agadget.hdr.help_message = help_message;
      aGadget->agadget.hdr.max_help     = sizeof(help_message)+1;  
 
      aGadget->agadget.data.action_button.text = cancels;
      aGadget->agadget.data.action_button.max_text_len = strlen(cancels)+1;
      aGadget->agadget.data.action_button.click_show   = 0;
      aGadget->agadget.data.action_button.event        = 0;       

      addGadget(gadgetList,aGadget);

      window_add_gadget(0,objectId,
                        &aGadget->agadget,
                        &aGadget->agadget.hdr.component_id);
                                               
  

      (*compId)++;

   }       

   aGadget = (tpGadgetList) calloc (1, sizeof(tGadgetList));

   if (aGadget!=NULL)
   {                                                              
      aGadget->index.paramIndex = 0;
      aGadget->agadget.hdr.flags = 1 /* default button */ ;
      aGadget->agadget.hdr.type   = ActionButton_Base;
      aGadget->agadget.hdr.box.xmin = width+600;
      aGadget->agadget.hdr.box.ymin = (*y)-74; 
      aGadget->agadget.hdr.box.xmax = width+800;
      aGadget->agadget.hdr.box.ymax = (*y)-6;
      aGadget->agadget.hdr.component_id = *compId;
      aGadget->agadget.hdr.help_message = help_message;
      aGadget->agadget.hdr.max_help     = sizeof(help_message)+1;  
 
      aGadget->agadget.data.action_button.text         = oks;
      aGadget->agadget.data.action_button.max_text_len = strlen(oks)+1;
      aGadget->agadget.data.action_button.click_show   = 0;
      aGadget->agadget.data.action_button.event        = 0;

      addGadget(gadgetList,aGadget);

      window_add_gadget(0,objectId,
                        &aGadget->agadget,
                        &aGadget->agadget.hdr.component_id);
                                               

 
      (*compId)++;

   }       

   (*y) = (*y) - 80;
}

BOOL thisBoolIsOk(parm_type type, int flags, int boolCount)
{     
#ifdef SPLIT_BOOLEANS
  BOOL result=False;
                                  
  if (type!=P_BOOL)
    result=True;
  else
  {
    if ((boolCount < MAX_BOOL_COUNT) &&
        (boolOrder[boolCount].flag&flags) &&
        !(boolOrder[boolCount].notFlag&flags) )
      result=True;
  }

  return result;     
#else
  return True;
#endif
}

void addWindowGadgets(tpGadgetList *gadgets, 
                      ObjectId     winId, 
                      int          serviceIndex, 
                      parm_class   class)
{            
  int i=0,j=0,x=0,y,len=0,loop;
  int width;
  int compId,lastCompId;
  struct parm_struct *parmStruct;
  int esg=0;
  BBox extent;
  BOOL doOptionsBox=False;
  tpGadgetList labelGadget;                                 
  BOOL boolCount=0;

  if (*gadgets==NULL)
  {   
    parmStruct = lpGetParamStruct(i);

            
    width=0;
    while (parmStruct->label != NULL)
    {
       if ( (parmStruct->flags&FLAG_USER_EDIT) &&
            (parmStruct->class==class) )
       {                                          
          len=fontStrWidth(parmStruct->label)+50;
          if (len>width)
             width=len;
       }

       i++;
       parmStruct = lpGetParamStruct(i); 
    }


    compId=0;
    y=0;
        
    for (loop=0;paramsOrder[loop]<P_SEP;loop++)
    {

       if (paramsOrder[loop]==P_BOOL)
       {
          doOptionsBox=True;                                        

          x = 0;

          labelGadget = addLabelBox(winId,  
#ifdef SPLIT_BOOLEANS
                          boolOrder[boolCount].optName,
#else         
                          "Options",
#endif
                          &x,
                          &y,
                          &compId,
                          gadgets);   
          labelGadget->agadget.hdr.box.xmin = 25;
          labelGadget->agadget.hdr.box.xmax = 800+width;
          labelGadget->agadget.hdr.box.ymax = y-10;
          y -= 60;
       }                    

       i=0;
       lastCompId=compId;
       parmStruct = lpGetParamStruct(i);
       while (parmStruct->label != NULL)
       {
          if ( (parmStruct->flags&FLAG_USER_EDIT) &&
               (parmStruct->class==class)         &&
               (parmStruct->type==paramsOrder[loop]) &&
               (thisBoolIsOk(parmStruct->type, parmStruct->flags, boolCount)==True))
          {                                          
 
             j=0;                                
             
             while (addGadgetFncs[j].parmType!=P_SEP)
             {
                if (addGadgetFncs[j].parmType==parmStruct->type &&
                   addGadgetFncs[j].addGadgetFnc!=NULL)
                { 
                   addGadgetFncs[j].addGadgetFnc(gadgets,
                                                 i,
                                                 winId,
                                                 &compId,
                                                 parmStruct,  
                                                 &x,
                                                 &y,
                                                 width,
                                                 &esg,
                                                 serviceIndex);
                   break;
               }
               j++;
             }
          }

          i++;
          parmStruct = lpGetParamStruct(i); 
        }
         
        if (doOptionsBox==True)
        {
           doOptionsBox=False; 
           if (lastCompId!=compId)
           {    
              y -= 120;

              labelGadget->agadget.hdr.box.ymin = y;
              gadget_move_gadget(0,
                                 winId,
                                 labelGadget->agadget.hdr.component_id,
                                 &labelGadget->agadget.hdr.box);

           }
           else
           {
              y += 120;
              window_remove_gadget(0,winId,labelGadget->agadget.hdr.component_id);
           }      

           boolCount++;
        }

     }

     addCancelAndSaveGadgets(gadgets,winId,&compId,&x,&y,width,class);

     extent.xmin=0;
     extent.ymin=y-6;
     extent.xmax=width+800+25;
     extent.ymax=0;
     window_set_extent(0,winId,&extent);
  }

}


int fillInGadgetFields(ObjectId     windowId,
                       tpGadgetList bGadget,
                       int          serviceIndex)
{  
   void *ptr; 
#if 0          
   int  i;
#endif
     
   while (bGadget!=NULL)
   { 

      switch (bGadget->agadget.hdr.type)
      { 
         
         case OptionButton_Base:
            ptr = getParamValuePtr(bGadget->parmStruct, serviceIndex);
            if (*(BOOL*)ptr==True)
            {
               optionbutton_set_state(0,
                                      windowId,
                                      bGadget->agadget.hdr.component_id,
                                      1); 
            }
            else
            {
               optionbutton_set_state(0,
                                      windowId,
                                      bGadget->agadget.hdr.component_id,
                                      0); 
            }
            break;

         case WritableField_Base:
            ptr = getParamValuePtr(bGadget->parmStruct, serviceIndex);
            if (bGadget->parmStruct->type==P_STRING || 
                bGadget->parmStruct->type==P_USTRING) 
            {
              strncpy(bGadget->agadget.data.writable_field.text,
                      *(char **)ptr,
                      255);
            }
            else
            {
              strncpy(bGadget->agadget.data.writable_field.text,
                      (char *)ptr,
                      255);
            }
            writablefield_set_value(0,
                                    windowId,
                                    bGadget->agadget.hdr.component_id,
                                    bGadget->agadget.data.writable_field.text);
            break;

         case RadioButton_Base:                                       
            if (bGadget->agadget.hdr.flags==RadioButton_On)
            {
               radiobutton_set_state(0,
                                     windowId,
                                     bGadget->agadget.hdr.component_id,
                                     1); 
            }
#if 0
            i=0;   
            while (bGadget->parmStruct->enum_list[i].value!=-1)
            {
               if (bGadget->parmStruct->enum_list[i].value == (*(int *)ptr) )
               {
                  radiobutton_set_state(0,
                                        windowId,
                                        bGadget->agadget.hdr.component_id,
                                        1); 
                  break;  
               }  
               i++;
            } 
#endif
            break;

         case NumberRange_Base:                                       
            ptr = getParamValuePtr(bGadget->parmStruct, serviceIndex);
            numberrange_set_value(0,
                                  windowId,
                                  bGadget->agadget.hdr.component_id,
                                  *(int *)ptr); 
            break;

         default:
            break;
      }

      bGadget=bGadget->next;
   }

   return 1;
}


int storeInGadgetFields(ObjectId     windowId,
                        tpGadgetList bGadget,
                        int          serviceIndex)
{  
   void *ptr;           
   int  i;
   int  state,nbytes;
   ComponentId selected;

     
   while (bGadget!=NULL)
   { 

      switch (bGadget->agadget.hdr.type)
      { 
         
         case OptionButton_Base:
            ptr = getParamValuePtr(bGadget->parmStruct, serviceIndex);
            optionbutton_get_state(0,
                                   windowId,
                                   bGadget->agadget.hdr.component_id,
                                   &state); 
            if (state==1)
            {
               *(BOOL*)ptr=True;
            }
            else
            {
               *(BOOL*)ptr=False;
            }
            break;

         case WritableField_Base:
            ptr = getParamValuePtr(bGadget->parmStruct, serviceIndex);
            writablefield_get_value(0,
                                    windowId,
                                    bGadget->agadget.hdr.component_id,
                                    bGadget->agadget.data.writable_field.text,
                                    255,
                                    &nbytes);
            if (bGadget->parmStruct->type==P_STRING || 
                bGadget->parmStruct->type==P_USTRING) 
            {
              string_set(ptr,bGadget->agadget.data.writable_field.text);
            }
            else
            {
              strncpy((char *)ptr,
                      bGadget->agadget.data.writable_field.text,
                      nbytes);

            }
            break;

         case RadioButton_Base: 
            ptr = getParamValuePtr(bGadget->parmStruct, serviceIndex);
            radiobutton_get_state(0,
                                  windowId,
                                  bGadget->agadget.hdr.component_id,
                                  &state,
                                  &selected); 
            if (state==1)
            {      
               bGadget->agadget.hdr.flags=RadioButton_On;                   
               i=0;   
               while (bGadget->parmStruct->enum_list[i].value!=-1)
               {
                  if (strcmp(bGadget->agadget.data.radio_button.label,
                             bGadget->parmStruct->enum_list[i].name)==0)
                  {
                     *(int *)ptr = bGadget->parmStruct->enum_list[i].value;
                     break;
                  }  
                  i++;
               }  
            } 
            else
            {
               bGadget->agadget.hdr.flags=0;    
            }
            break;

         case NumberRange_Base:                                       
            ptr = getParamValuePtr(bGadget->parmStruct, serviceIndex);
            numberrange_get_value(0,
                                  windowId,
                                  bGadget->agadget.hdr.component_id,
                                  (int *)ptr); 
            break;

         default:
            break;
      }

      bGadget=bGadget->next;
   }

   return 1;
}

BOOL haveGadgetFieldsChanged(ObjectId     windowId,
                             tpGadgetList bGadget,
                             int          serviceIndex)
{  
   void *ptr;           
   int  i;
   int  state,nbytes;
   ComponentId selected;
   BOOL result=False;
   int  value; 
     
   while (bGadget!=NULL && result==False)
   { 

      switch (bGadget->agadget.hdr.type)
      { 
         
         case OptionButton_Base:
            ptr = getParamValuePtr(bGadget->parmStruct, serviceIndex);
            optionbutton_get_state(0,
                                   windowId,
                                   bGadget->agadget.hdr.component_id,
                                   &state); 
            if (state==1)
            {
               if (*(BOOL*)ptr!=True)
               {
                  result=True;
               }
            }
            else
            {
               if (*(BOOL*)ptr!=False)
               {
                  result=True;
               }
            }
            break;

         case WritableField_Base:
            ptr = getParamValuePtr(bGadget->parmStruct, serviceIndex);
            writablefield_get_value(0,
                                    windowId,
                                    bGadget->agadget.hdr.component_id,
                                    bGadget->agadget.data.writable_field.text,
                                    255,
                                    &nbytes);
            if (bGadget->parmStruct->type==P_STRING || 
                bGadget->parmStruct->type==P_USTRING) 
            {
              if (strcmp(*(char**)ptr,
                         bGadget->agadget.data.writable_field.text)!=0)
              {
                 result=True;
              }
            }
            else
            {                
             
              if (strncmp((char *)ptr,
                          bGadget->agadget.data.writable_field.text,
                          nbytes)!=0)
              {
                 result=True;
              }

            }
            break;

         case RadioButton_Base: 
            ptr = getParamValuePtr(bGadget->parmStruct, serviceIndex);
            radiobutton_get_state(0,
                                  windowId,
                                  bGadget->agadget.hdr.component_id,
                                  &state,
                                  &selected); 
            if (state==1)
            {                         
               i=0;   
               while (bGadget->parmStruct->enum_list[i].value!=-1)
               {
                  if (strcmp(bGadget->agadget.data.radio_button.label,
                             bGadget->parmStruct->enum_list[i].name)==0)
                  {
                     if (*(int *)ptr != bGadget->parmStruct->enum_list[i].value)
                     {
                        result=True;
                     }
                     break;
                  }  
                  i++;
               }  
            } 
            break;

         case NumberRange_Base:                                       
            ptr = getParamValuePtr(bGadget->parmStruct, serviceIndex);
            numberrange_get_value(0,
                                  windowId,
                                  bGadget->agadget.hdr.component_id,
                                  &value);
            if (*(int*)ptr != value)
            {
               result=True;
            } 
            break;

         default:
            break;
      }

      bGadget=bGadget->next;
   }

   return result;
}
