#include <execp.h>
#include <modtbapp.h>
#include <tabmod.h>

// @cmember Richiede la maschera da usare
TMask* TTable_module_application::get_mask(int)
{
  CHECK(_msk, "Null mask");
  return _msk; 
}

// @cmember Ritorna la relazione da modificare
TRelation* TTable_module_application::get_relation() const
{ 
  CHECK(_rel, "Null relation");
  return _rel; 
}

void TTable_module_application::print()
{
  const TModule_table& tab = (const TModule_table&)file();
  TString16 cmd;
  cmd << tab.module() << "1 -0 " << tab.name();
  TExternal_app stampa(cmd);
  stampa.run();
}

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

TMask* TTable_module_application::user_create_mask()
{
  TModule_table& tab = (TModule_table&)file();
  TString16 name = tab.module();
  if (tab.customer() > 0)
  {
    TString8 c; 
    c.format("%06ld", tab.customer());
    name << c;
  }
  name << "tb" << tab.name();
  return new TMask(name);
}

bool TTable_module_application::user_create()
{
  if (argc() < 3) 
    return false;

  TString4 tabname;
  tabname.strncpy(argv(2), 4);
  tabname.upper();
  if (tabname[0] != '&')
    tabname.insert("&");   // Forza la tabella di modulo
  _rel = new TRelation(tabname);
  _msk = user_create_mask();
    
  TFilename rep = _msk->source_file().name();
  const int tb = rep.find("tb");
  if (tb > 0)
    rep.overwrite("st", tb);
  if (rep.custom_path()) 
    enable_menu_item(M_FILE_PRINT);
  
  TString title;
  _msk->get_caption(title);
  set_title(title);

  return true;
}

bool TTable_module_application::user_destroy() 
{
  if (_msk) delete _msk;
  if (_rel) delete _rel;
  return true;
}