#include <stdtypes.h>
#include <tabutil.h>
#include <execp.h>
#include <utility.h>
#include <tabapp.h>

void Tab_application::print()
{
#if XVT_OS == XVT_OS_WIN
  TExternal_app stampa(format("ba3a -1 %s", (const char *) _tabname));
#else
  TExternal_app stampa(format("ba3 -1 %s", (const char *) _tabname));
#endif
  stampa.run();
}

void Tab_application::init_query_mode(TMask& m) 
{
  m.send_key(K_SHIFT + K_CTRL + 'e', -GR_MODIFY_PROTECTED);
  m.send_key(K_SHIFT + K_CTRL + 'e', -GR_RECORD_PROTECTED);
}

void Tab_application::init_modify_mode(TMask& m) 
{
  m.send_key(K_SHIFT + K_CTRL + 'd', -GR_MODIFY_PROTECTED);
  const bool enable = !(_rel->curr().get_bool(FPC));

  m.send_key(K_SHIFT + K_CTRL + 'd' + enable, -GR_RECORD_PROTECTED);
}

bool Tab_application::protected_record(TRectype& rec) 
{ 
  return rec.get_bool(FPC);
}

bool Tab_application::user_create()
{
  if (argc() < 3) 
    return FALSE;
  
  _tabname = argv(2);

  if (_tabname.empty()) 
    return FALSE;
  
  _tabname.upper();
  TString16 m, t(_tabname);

  if (t[0] == '%') t.ltrim(1);

  m << "BATB" << t;
  _msk = new TMask(m) ;

  for (int i = 0; i < _msk->fields(); i++)
    if (_msk->fld(i).in_group(GR_SEARCH))
    {
      set_search_field(_msk->fld(i).dlg());
      break;
    }

  _rel = new TRelation(_tabname);
  set_title(_msk->get_caption());
  
  TFilename rpt("batb"); rpt << t << ".rpt";
  if (fexist(rpt)) enable_menu_item(M_FILE_PRINT);

  return TRUE;
}

bool Tab_application::user_destroy() 
{
  if (_msk) delete  _msk;
  if (_rel) delete  _rel;
  return TRUE;
}

