#include <varmask.h>       
#include <utility.h>
              
HIDDEN TString __varmask_str;

const TString& TVariable_mask::get(short fld_id) const
{
   if (present( fld_id ))
     return TMask::get( fld_id );
   else     
   {
     __varmask_str.cut(0);
     return __varmask_str;
   }
}

void TVariable_mask::set_handler(short fld_id, CONTROL_HANDLER handler)
{
  if(present( fld_id ))
    TMask::set_handler( fld_id, handler );
}               

void TVariable_mask::set(short fld_id, const char* str, bool hit )
{
  if(present( fld_id ))
    TMask::set( fld_id, str, hit );
}

void TVariable_mask::show(short fld_id, bool on )
{
  if(present( fld_id ) )
    TMask::show( fld_id, on );
}
 
void TVariable_mask::enable(short fld_id, bool on )
{
  if(present( fld_id ))
    TMask::enable( fld_id, on );
}

void TVariable_mask::check_field( short fld_id )
{
  if (present( fld_id ))
    TMask::check_field(fld_id);
}

TMask_field* TVariable_mask::parse_field(TScanner& scanner)
{
  if (scanner.key() == "SP")
  {
    _sheets++;
    return new TVariable_sheet_field(this);
  }

  return TMask::parse_field(scanner);
}

TVariable_mask::TVariable_mask(const char* name, int num, int max) : TMask()
{

  if (name && *name)
    read_mask(name, num, max);
}


TMask& TVariable_sheet_field::sheet_mask() const
{ 
  TMask & m = TSheet_field::sheet_mask();
  const int current = selected();
  
  if (_getmask != NULL && current >= 0 && current < items())
  {
    TMask & vm = *_getmask(current, m);
    
    vm.set_sheet((TSheet_field *) this);
    return vm;
  }
  return m;
}                                  
 
void TVariable_sheet_field::post_insert(int r)
{ 
  if (!active() || _getmask == NULL || r < 0 || r >= items())
    return;
  const TMask& m =  *_getmask(r, TSheet_field::sheet_mask());

  for (short id = FIRST_FIELD; id <= _last_column_id ; id++)
  {                                 
    const int col = cid2index(id);
    if (column_enabled(col))
    {
      int pos = m.id2pos(id);
      const int firstpos = pos;
                   
      if (pos >= 0)
      {  
        for(int dlg = id; pos >= 0; pos = m.id2pos(dlg += 100))
        {
          const TMask_field& f = m.fld(pos);
          if (f.shown() || f.ghost())
          {       
            const bool on = f.enabled();
            enable_cell(r, col, on);
          }  
          break;
        }  
      }
      else
        disable_cell(r, col);
    }
  }
}                                  
 
TVariable_sheet_field::~TVariable_sheet_field()
{ 
  TMask & m = TSheet_field::sheet_mask();
}



