#ifndef __PRINTER_H
#include <printer.h>
#endif 
    
#ifndef __PROGIND_H
#include <progind.h>
#endif
    
#include "../ba/baformed.h"
#include "sc3.h"
#include "sc3100a.h"
#include "sc3100b.h"
#include "sc3100c.h"
#include "sc21pec.h"

/////////////////////////////////////////////////////////////////
//  serve per ricalcolare le posizioni                         //   
/////////////////////////////////////////////////////////////////

typedef struct {
  char name_1[80];  // Fontname old
  char name_2[80];  // Fontname new
  int  size_1;      // size (height) of old font
  int  size_2;      // size (height) of new font
  double ratio;     // ratio (width_old_font/width_new_font)
} s_data;                

class TForm_EC : public TForm
{
protected:
  virtual void pre_edit_checks(TMask& m, TPrint_section* sec);
  virtual bool post_edit_checks(TMask& m, TPrint_section* sec);
public:
  virtual const char* section_mask() { return "sc3100s" ;}
  virtual void change_number_format(int w, int dec, const char* p);
  TForm_EC (): TForm() {};
  TForm_EC (const char* form, const char * code = "", int editlevel = 0, const char* desc = "")
    : TForm(form,code,editlevel,desc) {};
  virtual ~TForm_EC() {};
};

void TForm_EC::pre_edit_checks(TMask& m, TPrint_section* sec)
{ 
  // Se sta editando il body abilita i primi 4, se sta editando il footer abilita l'ultimo
  if (sec->section_type() == 'B')
    m.enable(-2);
  if (sec->section_type() == 'F')
    m.enable(-3);
  CHECK(exist('H', last_page),"La testata dell'ultima pagina non esiste!");
  m.set(F_DES1,find_field('H',last_page,PEC_SALDO).prompt());
  m.set(F_DES2,find_field('H',last_page,PEC_RITENUTE).prompt());
  m.set(F_DES3,find_field('H',last_page,PEC_ABBUONI).prompt());
  m.set(F_DES4,find_field('H',last_page,PEC_DIFFCAM).prompt());
  m.set(F_DES5,find_field('H',last_page,PEC_RIPORTO).prompt());
}
    
bool TForm_EC::post_edit_checks(TMask& m, TPrint_section* sec)
{ 
  CHECK(exist('H', last_page),"La testata dell'ultima pagina non esiste!");
  section('H',last_page).set_dirty();
  TForm_item& saldo    = find_field('H',last_page,PEC_SALDO);
  TForm_item& abbuoni  = find_field('H',last_page,PEC_RITENUTE);
  TForm_item& diffcam  = find_field('H',last_page,PEC_ABBUONI);
  TForm_item& ritenute = find_field('H',last_page,PEC_DIFFCAM);
  TForm_item& riporto  = find_field('H',last_page,PEC_RIPORTO);
  saldo.set_prompt(m.get(F_DES1));
  saldo.set_dirty();
  abbuoni.set_prompt(m.get(F_DES2));
  abbuoni.set_dirty();
  diffcam.set_prompt(m.get(F_DES3));
  diffcam.set_dirty();
  ritenute.set_prompt(m.get(F_DES4));
  ritenute.set_dirty();
  riporto.set_prompt(m.get(F_DES5));
  riporto.set_dirty();
  return TRUE;
}

void TForm_EC::change_number_format(int w, int dec, const char* p)
{
  char secs[] = { "FB" };
  char ptyp[] = { "LOEF" };
  TPrint_section* ps;
  
  for (int sc = 0; sc < 4; sc++)
    for (int pt = 0; pt < 4; pt++)
       if ((ps = exist(secs[sc], char2page(ptyp[pt]), FALSE)) != NULL)
         ps_change_number_format(*ps, w, dec, p);
}

class TForm_EC_editor : public TForm_editor
{ 
  s_data   _prm;
  TMask  * _msk; // maschera di selezione sezione
protected:
  static bool cpy_handler(TMask_field& f, KEY k);
  static bool font_handler(TMask_field& f, KEY k);
  static bool code_handler(TMask_field& f, KEY k);
  static bool lng_handler(TMask_field& f, KEY k);
  static bool ccodes_handler(TMask_field& f, KEY k);
  static bool clngs_handler(TMask_field& f, KEY k);
  bool load_fonts();       
  void change_pos(char sc, pagetype pt);
  bool recalculate_positions(const char* name, int size);
  void remove_temp_items(char sec, pagetype p);
  void put_examples(char sec, pagetype p);
  void remove_examples(char sec, pagetype p);
  void hook_footer_to_body(TPrint_section* f, TPrint_section* b);
  void unhook_footer(TPrint_section* f);
  void hide_body_items(TPrint_section* b);
  void show_body_items(TPrint_section* b);
  void remove_form(TLocalisamfile& frm, TLocalisamfile& rfr,const TString& t,const TString& c);
  virtual const char* selection_mask() const { return "sc3100a"; }
  virtual bool ask_profile();
  virtual bool edit(char s, pagetype p);
  virtual void print();
public:
  TForm_EC_editor() {_msk = NULL;}
  virtual ~TForm_EC_editor() {}
}; 

inline TForm_EC_editor& app() { return (TForm_EC_editor&) main_app();}

BOOLEAN XVT_CALLCONV1 wprms (long data)
{                        
  s_data* st =(s_data*)data;
  WINDOW prwin = xvt_print_create_win(printer().get_printrcd(),"");
  long width_old,width_new;
  TString spc(100);
  spc.fill('m');
  xvt_set_font(prwin,st->name_1, XVT_FS_NONE, st->size_1);  
  width_old = xvt_dwin_get_text_width(prwin,(char*)(const char*)spc, 100);
  xvt_set_font(prwin,st->name_2, XVT_FS_NONE, st->size_2);  
  width_new = xvt_dwin_get_text_width(prwin,(char*)(const char*)spc, 100);
  st->ratio = (double)width_old / (double)width_new;
  xvt_vobj_destroy(prwin);

  return FALSE;
}
                            
bool TForm_EC_editor::ccodes_handler(TMask_field& f, KEY k)
{
  TMask& m = f.mask();
  if (k==K_TAB)
  {              
    TString base(m.get(F_CBASE));
    TString code(f.get());
    code << m.get(F_CCODELS);
    TLocalisamfile frm(LF_FORM);
    frm.zero();
    frm.put("TIPOPROF",base);
    frm.put("CODPROF",code);
    if (frm.read(_isequal)== NOERR)
      m.set(F_CDESCS,frm.get("DESC"));
    else 
      m.reset(F_CDESCS);
  }
  return TRUE; 
}

bool TForm_EC_editor::clngs_handler(TMask_field& f, KEY key)
{
  TMask& m = f.mask();
  if (key==K_TAB)
  {              
    TString base(m.get(F_CBASE));
    TString code(m.get(F_CCODES));
    code << f.get();
    TLocalisamfile frm(LF_FORM);
    frm.zero();
    frm.put("TIPOPROF",base);
    frm.put("CODPROF",code);
    if (frm.read(_isequal)== NOERR)
      m.set(F_CDESCS,frm.get("DESC"));
    else 
      m.reset(F_CDESCS);
  }
  return TRUE;
}

bool TForm_EC_editor::cpy_handler(TMask_field& f, KEY k)
{ 
  if (k==K_SPACE)
  {
    TLocalisamfile frm(LF_FORM),rfr(LF_RFORM);
    TMask mm("sc3100b");
    mm.set_handler(F_CCODES,ccodes_handler);
    mm.set_handler(F_CCODELS,clngs_handler); 
    const TMask & m = f.mask();
    mm.set(F_CCODED, m.get(F_CODE));
    mm.set(F_CCODELD, m.get(F_CODEL));
    mm.set(F_CDESCD, m.get(F_DESC));
    mm.set(F_CBASE,BASE_EC_PROFILE);
    while (mm.run() == K_ENTER) // Prende in input il nome del profilo sorgente
    {
      long scode = mm.get_long(F_CCODES);
      long dcode = mm.get_long(F_CCODED);
      char slng  = mm.get(F_CCODELS)[0];
      char dlng  = mm.get(F_CCODELD)[0];
      TString form = mm.get(F_CBASE); 
      TString scod,dcod;
      scod.format("%04ld%c",scode,slng); // Codice profilo sorgente
      if (scode == 0) scod = "";
      dcod.format("%04ld%c",dcode,dlng); // Codice profilo destinazione
      frm.zero();
      frm.put("TIPOPROF",form);frm.put("CODPROF",dcod);
      if (frm.read()==NOERR)
      {
        error_box("Il profilo specificato come destinazione e' gia' esistente.");
        continue;
      }
      //Effettua la copia dei record.
      frm.zero(); frm.put("TIPOPROF",form); // Questo vale per tutti
      frm.put("CODPROF",scod);    //Profilo sorgente...
      if (frm.read()==NOERR)
      {
        TLocalisamfile rfr_to_write(LF_RFORM);
        frm.put("CODPROF",dcod); //Cambia il codice del profilo con quello di destinazione e lo scrive
        frm.put("DESC",mm.get(F_CDESCD)); // Mette la descrizione 
        if (scod.empty()) // Se la copia avviene dal profilo standard...
        {
          frm.put("GRID","+++++++++-|"); // copia anche i parametri di default
          frm.put("FONTNAME","Courier New");
          frm.put("FONTSIZE",7);
        }
        if (frm.write() != NOERR)
          frm.rewrite();
        if (frm.status() != NOERR) 
        {
          error_box("Errore %d in scrittura testata profilo.",frm.status());
          break;
        } 
        TProgind pi(50,"Copia in corso...",FALSE,FALSE,10);
        rfr.zero();rfr.put("TIPOPROF",form);
        rfr.put("CODPROF",scod);  //Profilo sorgente          
        rfr.read(_isgteq);
        while ( rfr.get("CODPROF") == scod && rfr_to_write.good() && !rfr.eof())
        {                                     
          rfr_to_write.curr() = rfr.curr();
          rfr_to_write.put("CODPROF",dcod); // cambia il codice del profilo con quello di destinazione
          if (rfr_to_write.write() != NOERR)
            rfr_to_write.rewrite();
          rfr.read(_isgreat);
        } 
        pi.cancel();
        if (rfr_to_write.status() != NOERR) 
        {
          error_box("Errore %d in scrittura righe profilo.",rfr_to_write.status());
          break ;
        }
        break;
      } else
         error_box("Non esiste il profilo sorgente specificato.");
    }
  }
  return TRUE; 
}


bool TForm_EC_editor::code_handler(TMask_field& f, KEY key)
{
  TMask& m = f.mask();
  if (key==K_TAB && f.get().not_empty())
  {              
    TString base(m.get(F_BASE));
    TString code(f.get());
    code << m.get(F_CODEL);
    TLocalisamfile frm(LF_FORM);
    frm.zero();
    frm.put("TIPOPROF",base);
    frm.put("CODPROF",code);
    if (frm.read(_isequal)== NOERR)
      m.set(F_DESC,frm.get("DESC"));
    else 
      m.reset(F_DESC);
  }
  return TRUE;
}

bool TForm_EC_editor::lng_handler(TMask_field& f, KEY key)
{
  TMask& m = f.mask();
  if (key==K_TAB && m.get(F_CODE).not_empty())
  {              
    TString base(m.get(F_BASE));
    TString code(m.get(F_CODE));
    code << f.get();
    TLocalisamfile frm(LF_FORM);
    frm.zero();
    frm.put("TIPOPROF",base);
    frm.put("CODPROF",code);
    if (frm.read(_isequal)== NOERR)
      m.set(F_DESC,frm.get("DESC"));
    else 
      m.reset(F_DESC);
  }
  return TRUE;
}

bool TForm_EC_editor::font_handler(TMask_field& f, KEY key)
{
  if (key == K_SPACE)
  {                
    main_app().begin_wait();           

    const char* family = f.get();
    const int MAXSIZES = 16;
    long sizes[MAXSIZES]; 
    BOOLEAN scalable;
    const int num_sizes = (int)xvt_fmap_get_family_sizes(printer().get_printrcd(), 
                                                         (char*)family, sizes, &scalable, MAXSIZES);
    
    TToken_string pn1(80), pn2(80);                                                    
    if (scalable)
    {
      for (int i = 4; i <= 32; i++)
      {
        pn1.add(i);
        pn2.add(i);
      }               
    }  
    else                           
    {
      if (num_sizes > 0)
      {
        for (int i = 0; i < num_sizes; i++)
          pn1.add(sizes[i]);
      }
      else pn1.add(printer().get_char_size());
      pn2 = pn1;
    }
    TList_field& lst = (TList_field&)f.mask().field(F_SSIZE);
    lst.replace_items(pn1, pn2);
    lst.set(format("%d",printer().get_char_size()));
    
    main_app().end_wait();           
  }   
  return TRUE;
}

bool TForm_EC_editor::load_fonts()
{
  const int MAX_FAMILIES = 128;
  char* family[MAX_FAMILIES];
  const int num_families = (int)xvt_fmap_get_families(printer().get_printrcd(), family, MAX_FAMILIES);
  bool  font_found = FALSE;
  TToken_string pn1(256), pn2(256);                                                    
  
  for (int i = 0; i < num_families; i++)
  {
    pn1.add(family[i]);
    pn2.add(family[i]);
    if (!font_found) 
      if (form().fontname() == family[i]) font_found = TRUE;
    xvt_mem_free(family[i]);
  }  
  TList_field& lst = (TList_field&)_msk->field(F_SFONT);
  if (!font_found)
  {
    warning_box("Il font %s non esiste per la stampante di default.",(const char*) form().fontname());
    pn1.add(form().fontname());
    pn2.add(form().fontname());
  }
  lst.replace_items(pn1, pn2);
  lst.set(form().fontname());
  return font_found;
}  

void TForm_EC_editor::change_pos(char sc, pagetype pt)
{
  TPrint_section* sec = form().exist(sc, pt);
  if (sec != NULL && !sec->columnwise())
  {
    sec->set_dirty();                       
    for (word i = 0; i < sec->fields() ; i++)
    {
      TForm_item& fi = sec->field(i);
      short value = fi.x();
      if (value > 0 && (_prm.ratio != 1.0))
      {
        real x_pos;
        x_pos = value * _prm.ratio;
        x_pos.round();
        fi.set_x((short)x_pos.integer());
        fi.set_dirty();
      }
    }
  }
}

bool TForm_EC_editor::recalculate_positions(const char* name, int size)
{   
  if (!form().dirty()) form().set_dirty();
  _prm.size_1=form().fontsize();
  strcpy(_prm.name_1,form().fontname());
  _prm.size_2=size;
  strcpy(_prm.name_2,name);
  _prm.ratio = 1.0;
  // Next 3 lines may be changed
  xvt_print_open();
  xvt_print_start_thread (wprms, (long)&_prm); 
  xvt_print_close();

  const char sechar[4] = { 'B', 'F', 'G',  'H' };
  for (int sn = 0; sn < 4 ; sn++) 
  {
    const char sc = sechar[sn];
    for (pagetype pt = odd_page; pt <= last_page; pt = pagetype(pt+1))
      change_pos(sc,pt);
  }  
  return TRUE;
}

void TForm_EC_editor::remove_temp_items(char sec, pagetype p)
{                    
  TPrint_section* s = form().exist(sec,p);
  if (s!=NULL)
  {
    const word items = s->fields();
    for (word j=0;j<items; j++)
    {
      if  (s->field(j).temp())
        s->destroy_field(j,FALSE);
    }
    s->field_array().pack(); 
  }
}


void TForm_EC_editor::put_examples(char sez, pagetype p)
{
  TPrint_section* s = form().exist(sez,p);
  if (s!=NULL)
  {
    const word items = s->fields();
    for (word i=0;i<items;i++)
    {
      TForm_item& fi = s->field(i);
      if (fi.fields()!=0) continue;
      if (fi.memo()) 
        fi.set(fi.memo_info());
      else
      if (fi.prompt().empty())
      { 
        if (fi.class_name() == "DATA")
        {
          const TDate d(TODAY);
          fi.set(d);
        }
        else
          if (fi.class_name() == "NUMERO")
          {
            fi.set_prompt(fi.example());
            fi.temp() = TRUE;
          }
          else
            fi.set(fi.key());
      }
    }
  }
}

void TForm_EC_editor::remove_examples(char sez, pagetype p)
{
  TPrint_section* s = form().exist(sez,p);
  if (s!=NULL)
  {
    const word items = s->fields();
    for (word i=0;i<items;i++)
    {
      TForm_item& fi = s->field(i);
      if (fi.fields()!=0) continue;
      if (fi.memo()) 
        fi.set("");
      else
        if (fi.class_name() == "NUMERO" && fi.temp())
        {
          fi.set_prompt("");
          fi.temp() = FALSE;
        }
    }
  }
}

void TForm_EC_editor::hook_footer_to_body(TPrint_section* f, TPrint_section* b)
{
  const word items = b->columnwise() ? f->fields() : 0;
  b->reset_tabs();
  for (word i=0;i<items;i++)
  {
    TForm_item& fi = f->field(i);
    if (fi.x()>0 || !fi.shown()) continue;
    short id = fi.id();
    if (id >= PEC_TSALDO && id <= PEC_TUNASSIGNED) id = PEC_DESCR;
    else
      if (id == PEC_UNASSIGNED) id = PEC_AVERE; 

    TForm_item& fb = b->find_field(id);
    if (!fb.shown())
    {
      fi.hide();
      fi.set_x(-1);
    } else
        fi.set_x(fb.x());
    fi.ofs() = 999;
  }
}

void TForm_EC_editor::unhook_footer(TPrint_section* f)
{
  const word items = f->fields();
  for (word i=0;i<items;i++)
  {
    TForm_item& fi = f->field(i);
    if (fi.ofs()==999)
    { 
      if (fi.x() == -1) fi.show();
      fi.ofs() = 0;
      fi.set_x(0);
    }
  }
}

void TForm_EC_editor::hide_body_items(TPrint_section* b)
{
  const word items = b->fields();
  for (word i=0;i<items;i++)
  {
    TForm_item& fi = b->field(i);
    if (fi.shown() && fi.prompt().not_empty())
    {
      fi.hide();
      fi.temp() = TRUE;
    }
  }
}

void TForm_EC_editor::show_body_items(TPrint_section* b)
{
  const word items = b->fields();
  for (word i=0;i<items;i++)
  {
    TForm_item& fi = b->field(i);
    if (fi.temp())
    {
      fi.show();
      fi.temp() = FALSE;
    }
  }
}

bool TForm_EC_editor::edit(char s, pagetype p)
{
  _msk= new TMask("sc3100c");
  TString80 caption;
  bool dirty = FALSE;
  bool other = FALSE;
  KEY k;
  
  if (extra())
  {
    if (s == 'R')
      dirty = edit_relation();
    else  
      if (s == 'D')
        form_config();
    else
      if (s == 'K')
        dirty = edit_fincatura();
    else
      if (s == 'J')
        dirty = edit_formato_numero();
    else
      if (s == 'Y')
        dirty = edit_formato_data();
      else other = TRUE;
  }
  if (!extra() || (extra() && other))
  {
    printer().set_char_size(form().fontsize()); // Questo settera' il carattere della stampante a quello del form
    // Setta l'handler per il listbox dei fonts e per aggiornare i valori dello sfondo
    _msk->set_handler(F_SFONT,font_handler);
    // carica i dati relativi all'offset
    _msk->set(F_SX, form().offset_x());
    _msk->set(F_SY, form().offset_y());
    TString ctp;
    ctp << form().char_to_pos();
    _msk->set(F_SCTP, ctp);
    _msk->set(F_SIPX, form().ipx());
    _msk->set(F_SIPY, form().ipy());
    _msk->set(F_FOOT_H,form().exist('F',odd_page)->height());
    // carica i dati relativi ai flags (memorizzati in HEADER LAST)
    if (form().exist('H', last_page))
    {
      TForm_item& flags = form().find_field('H',last_page,PEC_FLAGS);
      TToken_string tt(flags.prompt());
      if (tt.get(0) != NULL)
        _msk->set(F_SVALUTA,tt.get(0));
      if (tt.get(1) != NULL)
        _msk->set(F_FINK,tt.get(1));
      if (tt.get(2) != NULL)
        _msk->set(F_MAXTOT,tt.get(2));
    }
    // carica i dati relativi allo sfondo (memorizzati in GRAPHICS ODD)
    if (form().exist('G', odd_page))
    {
      TForm_item& pict = form().find_field('G',odd_page,PEC_PICTURE);
      _msk->set(F_BACKGROUND,pict.prompt());
      _msk->set(F_BACKWIDTH,pict.width());
      _msk->set(F_BACKHEIGHT,pict.height());
      _msk->set(F_BACKX,pict.x());
      _msk->set(F_BACKY,pict.y());
    }
    bool font_changed=FALSE,font_found;
    while (TRUE)  
    {             
      // Carica i fonts disponibili nel listbox
      bool local_dirty = FALSE;
      font_found = load_fonts();
      enable_menu_item(M_FILE_PRINT);
      k = _msk->run();
      disable_menu_item(M_FILE_PRINT);                          
      if ((_msk->field(F_SFONT).dirty() || _msk->field(F_SSIZE).dirty()))
      {
        font_changed = TRUE;
        TString  ff(_msk->get(F_SFONT));
        int      ss=_msk->get_int(F_SSIZE);
        if (font_found && yesno_box("E' stato cambiato il font o la dimensione del carattere\ndevo aggiornare le coordinate dei campi"))
          dirty = recalculate_positions(ff, ss); // Va bene dirty e non local_dirty
        form().fontname() = ff;
        form().fontsize() = ss;
        printer().set_char_size(ss); // Questo settera' il carattere della stampante a quello del form
      }
      if (k==K_F3) local_dirty = edit_fincatura();
      else if (k==K_F4) local_dirty = edit_formato_data();
      else if (k==K_F5) local_dirty = edit_formato_numero();
      else if (k!= K_ESC && k!=K_ENTER)
      {
        if (k==K_F6) { s = 'H'; caption = "Testata"; }
        else if (k==K_F7) { s = 'B'; caption = "Corpo"; }
        else if (k==K_F8) { s = 'F'; caption = "Piede"; }
        local_dirty = TRUE;
        if (!form().exist(s, odd_page))
        {
          const KEY k = yesnocancel_box("La sezione %s non esiste:\n"
                                        "si desidera generarla?", 
                                        (const char*)caption);
          if (k == K_ESC) 
            local_dirty = FALSE;
          else
          {
            TPrint_section* sec = form().exist(s, odd_page, TRUE);
            if (k == K_YES) 
            {
              const TPrint_section* def = form().exist(s, odd_page);
              if (def) *sec = *def;
              else local_dirty = FALSE;
            }
          }
        }            
        if (local_dirty)
        { 
          TPrint_section saved_section(form().section(s,odd_page));
          
          local_dirty = form().section(s,odd_page).edit(caption);
          if (!local_dirty) // E' stato premuto annulla dalla maschera di sezione
          {                 // Quindi ripristina la vecchia sezione
            form().section(s,odd_page) = saved_section;
            if (font_changed && font_found)
              //se e' stato cambiato il font allora in _prm ho i dati necessari
              // per ricostruire le dimensioni corrette
              change_pos(s,odd_page);
          }
        }
      }
      if (local_dirty || _msk->dirty()!=0 || k == K_ENTER) dirty=TRUE;
      if (k==K_ESC || k==K_ENTER)
        break;
    } // End of while
  }   // End if
  
  if (_msk->dirty() != 0 || dirty)
  {
    form().fontname() = _msk->get(F_SFONT);
    form().fontsize() = _msk->get_int(F_SSIZE);
    form().offset_x() = _msk->get_int(F_SX);
    form().offset_y() = _msk->get_int(F_SY);
    form().char_to_pos() = _msk->get(F_SCTP)[0];
    form().ipx() = _msk->get_int(F_SIPX);
    form().ipy() = _msk->get_int(F_SIPY);
    if (form().exist('H', last_page))
    {
      TForm_item& flags = form().find_field('H',last_page,PEC_FLAGS);
      TString s(_msk->get(F_SVALUTA));
      s << "|" << _msk->get(F_FINK); s << "|" << _msk->get(F_MAXTOT);
      form().section('H',last_page).set_dirty();
      flags.set_prompt(s);
      flags.set_dirty();
    }
    if (form().exist('G', odd_page))
    {
      TForm_item& pict = form().find_field('G',odd_page,PEC_PICTURE);
      pict.set_prompt(_msk->get(F_BACKGROUND));
      pict.width() = _msk->get_int(F_BACKWIDTH);
      pict.height() = _msk->get_int(F_BACKHEIGHT);
      pict.set_x(_msk->get_int(F_BACKX));
      pict.y() = _msk->get_int(F_BACKY);
      form().section('G',odd_page).set_dirty();
      pict.set_dirty();
    }
    form().set_dirty();  
    if (!dirty) dirty = TRUE;
  }
  if ((dirty && k==K_ESC && yesno_box("Salvare le modifiche?")) || (dirty && k==K_ENTER))
  { // Se si preme annulla e sono state effettuate modifiche chiede la conferma per salvare
    // Se invece si preme conferma e sono state effettuate modifiche non chiede la conferma
    if (form().code().empty())
    {
      TFilename n(form().name()); n.ext("frm");
      TFilename bak(n); bak.ext("bak");
      rename(n, bak);
      ofstream out(n);
      form().print_on(out);
    }
    else 
    {
      TForm& ff = form();
      ff.write_profile();
      TLocalisamfile r(LF_RFORM); // scrivera' un record sulle righe, relativo alla sezione
      r.put("TIPOPROF",ff.name());// per memorizzare i formati numero/data
      r.put("CODPROF",ff.code());
      r.put("SEZ","DN");
      r.put("ID",0); 
      TToken_string t(get_formato_data(),'\n');
      t.add(get_formato_numero());
      r.put("SPECIAL",t);
      if (r.write()!= NOERR) r.rewrite();
    }
  }
  else
    if (k==K_ESC && form()._isnew)
    {
      TLocalisamfile f(LF_FORM),r(LF_RFORM);
      remove_form(f,r,form().name(),form().code());
      mask().reset(F_BASE);
      mask().reset(F_CODE);
      mask().reset(F_CODEL);
      mask().reset(F_DESC);
    }
  if (_msk!=NULL) delete _msk;
  _msk = NULL;
  if (!extra()) dispatch_e_menu(MENU_ITEM(14));
  return dirty;
}                                  

void TForm_EC_editor::print()
{
  TPrint_section* g = form().exist('G',odd_page);
  TPrint_section* h = form().exist('H',odd_page);
  TPrint_section* f = form().exist('F',odd_page);
  TPrint_section* b = form().exist('B',odd_page);
  bool remove = TRUE;
  
  CHECK(b!=NULL && f!=NULL && h!=NULL,"Una o piu' sezioni non sono presenti");
  // Rilegge la sezione grafica, perche' se e' la seconda volta che si
  // stampa deve ripristinare la situazione iniziale.
  // NB la sezione grafica deve contenere solo sfondo: le linee di fincatura 
  // qui generate non verranno salvate; analogamente il programma di stampa
  // vero e proprio.
  if (b->columnwise())
  {                
    bool gen_fink = TRUE;
    if (_msk != NULL)
    {
      TForm_item& pict = g->find_field(PEC_PICTURE);
      pict.set_prompt(_msk->get(F_BACKGROUND));
      pict.width() = _msk->get_int(F_BACKWIDTH);
      pict.height() = _msk->get_int(F_BACKHEIGHT);
      pict.set_x(_msk->get_int(F_BACKX));
      pict.y() = _msk->get_int(F_BACKY);
      form().offset_y() = _msk->get_int(F_SY);
      form().offset_x() = _msk->get_int(F_SX);
      form().set_mode((bkg_mode)_msk->get_int(F_FINK));
      if (form().mode() == none) gen_fink = FALSE;
    }
    // Schiaffa dentro le stringhe di esempio per ogni campo che non ha prompt o riferimenti sul file
    // per i campi memo si copia _memo in _prompt
    const int formlen = printer().formlen();
    const int h_height  = h->height();
    int rws[3] = {h_height,formlen-f->height(),0};    
    if (gen_fink)
      form().genera_fincatura(odd_page,h_height-2,formlen,rws);
    form().genera_intestazioni(odd_page,h_height-1);
  } else
     remove = FALSE;
  hook_footer_to_body(f,b);
  hide_body_items(b);
  put_examples('H',odd_page);
  put_examples('F',odd_page);
  TForm_editor::print();
  remove_examples('H',odd_page);
  remove_examples('F',odd_page);
  show_body_items(b);
  unhook_footer(f);
  if (remove)
  {         
    remove_temp_items('G',odd_page);        
    remove_temp_items('H',odd_page);        
  }
}

void TForm_EC_editor::remove_form(TLocalisamfile& frm, TLocalisamfile& rfr,const TString& t,const TString& c)
{  
  frm.zero(); 
  frm.put("TIPOPROF", t); 
  if (c.not_empty())
    frm.put("CODPROF", c);
  if (frm.read() == NOERR)
    frm.remove();
  rfr.zero(); 
  rfr.put("TIPOPROF", t);
  rfr.put("CODPROF",  c);
  if (rfr.read(_isgteq) == NOERR)
  {
    for ( ; rfr.get("CODPROF") == c && rfr.get("TIPOPROF") == t; rfr.next())
      rfr.remove();
  }
}
bool TForm_EC_editor::ask_profile()
{  
  KEY k; 
  mask().set_handler(DLG_COPY,cpy_handler);
  mask().set_handler(F_CODE,code_handler);
  mask().set_handler(F_CODEL,lng_handler);
  mask().set(F_BASE,BASE_EC_PROFILE);

  disable_menu_item(M_FILE_PRINT);
  while ((k = mask().run()) != K_QUIT)
  {                                              
    TLocalisamfile frm(LF_FORM);
    TLocalisamfile rfr(LF_RFORM);
    TString fform = mask().get(F_BASE);
    TString fdesc = mask().get(F_DESC);
    long fcode = mask().get_long(F_CODE);
    char flng  = mask().get(F_CODEL)[0];
    TString cod;
    cod.format("%04ld%c",fcode,flng);
    if (!extra() && fcode == 0 && flng=='\0')
    {
      error_box("Non e' possibile modificare il profilo standard.");
      continue;
    }                        
    if (k == K_ENTER)
    {
      check_form();
      set_form(new TForm_EC(fform, (fcode!=0) ? cod : "", extra() ? 2 :1, fdesc));
      
      TLocalisamfile r(LF_RFORM); // Setta l'eventuale formato date/numero impostati
      r.put("TIPOPROF",fform);
      r.put("CODPROF",cod);
      r.put("SEZ","DN");r.put("ID",0);
      if (r.read() == NOERR)
      {
        TToken_string special (r.get("SPECIAL"),'\n');
        set_formato_data(special.get(0));
        set_formato_numero(special.get(1));
      }
      
      break;
    }            
    else if (k == K_DEL)
    {
      if (fcode != 0 && 
         yesno_box("Confermare la cancellazione del profilo %04ld%c", fcode,flng))
      { 
        TProgind pi(50,"Cancellazione in corso...",FALSE,FALSE,10);
        remove_form(frm,rfr,fform,cod);
        pi.cancel();
        mask().set(F_BASE,BASE_EC_PROFILE);
        mask().set(F_CODE,"");
        mask().set(F_CODEL,"");
        mask().set(F_DESC,"");
      }
    }                                                        
  }
  if (k == K_QUIT)
  {
    dispatch_e_menu(M_FILE_QUIT);   
    return TRUE;
  }
  return FALSE;
}

int sc3100(int argc, char** argv)
{                                             
  TForm_EC_editor a;      
  a.run(argc, argv, "Parametrizzazione profili EC");
  return 0;
}
