#include <defmask.h>
#include <form.h>
#include <msksheet.h>
#include <printer.h>

#include "../ba/bafrm.h"
      
// per lo sheet di edit campi      
#define idt_id 101
#define dsc_id 102
#define prn_id 103    
#define yps_id 104
#define xps_id 105
#define len_id 106
#define col_id 107
#define int_id 108
#define spc_id 109
#define fnl_id 110
#define fnr_id 111
#define typ_id 112
#define frm_id 113    

// Current section (edit, print)                           
HIDDEN TPrint_section* _cur_sect = NULL;
HIDDEN TMask*          _special_mask = NULL;

HIDDEN TPrint_section& section()
{
  CHECK(_cur_sect, "Can't print NULL section");
  return *_cur_sect;
}

///////////////////////////////////////////////////////////
// Gestione TFieldref su maschera
// ID   CAMPO                                       ATTIVO  
// xx0  Stringa completa del TFieldref                 
// xx1  Descrizione file 
// xx2  Bottone selezione file                         X
// xx3  Descrizione campo
// xx4  Bottone selezione campo                        X
// xx5  Primo carattere campo                          X
// xx6  Ultimo carattere campo                         X
///////////////////////////////////////////////////////////

HIDDEN void put_fieldref(const TFieldref& fr, TMask_field& f)
{             
  TRelation_description& rd = _cur_form->rel_desc();
  rd.set_cur_file(fr.file());

  TString80 desc; desc << fr;
  f.set(desc);
  
  TMask& m = f.mask();
  const short id = f.dlg(); // Campo contenente il TFieldref

  m.set(id+1, rd.file_desc());
  m.set(id+3, rd.get_field_description(fr.name()));
  m.set(id+5, (fr.from() > 0 || fr.to() > 0) ? fr.from()+1 : 0);
  m.set(id+6, fr.to() > fr.from() ? fr.to() : 0);
}


// Handler of F_BUT_FILE field on mask
HIDDEN bool but_file_handler(TMask_field& f, KEY k)                      
{
  if (k == K_F9)
  {                          
    TRelation_description& r = _cur_form->rel_desc();                                   
    
    TEdit_field& e = f.mask().efield(f.dlg()-1);
    TFieldref ref; ref = e.get();
    if (r.choose_file(ref.file()))
    {
      ref.set_file(r.file_num());
      put_fieldref(ref, e);
    }  
  }
  return TRUE;
}                      

HIDDEN bool but_file_handler_sub(TMask_field& f, KEY k)                      
{
  if (k == K_SPACE)
  {                          
    TRelation_description& r = _cur_form->rel_desc();                                   
    
    TEdit_field& e = f.mask().efield(f.dlg()-2);
    TFieldref ref; ref = e.get();
    if (r.choose_file(ref.file()))
    {
      ref.set_file(r.file_num());
      f.mask().set(F_FILE1,r.file_desc());  
    }
  }
  return TRUE;
}                      


// Handler of F_BUT_FIELD field on mask
HIDDEN bool but_field_handler(TMask_field& f, KEY k)                      
{
  if (k == K_F9)
  {                          
    TRelation_description& r = _cur_form->rel_desc();                                   
    TEdit_field& e = f.mask().efield(f.dlg()-3); // TBC
    TFieldref ref; ref = e.get();
    if (r.choose_field(ref.name()))
    {
      ref.set_name(r.field_name());
      put_fieldref(ref, e);
    }  
  }
  return TRUE;
}                      

// Handler of F_FROM field on mask
HIDDEN bool from_handler(TMask_field& f, KEY k)                      
{
  if (f.to_check(k))
  {
    TEdit_field& e = f.mask().efield(f.dlg()-5);
    TFieldref ref; ref = e.get();
    ref.set_from(atoi(f.get()));
    put_fieldref(ref, e);
  }
  return TRUE;
}

// Handler of F_TO field on mask
HIDDEN bool to_handler(TMask_field& f, KEY k)                      
{
  if (f.to_check(k))
  {
    TEdit_field& e = f.mask().efield(f.dlg()-6);
    TFieldref ref; ref = e.get();
    ref.set_to(atoi(f.get()));
    put_fieldref(ref, e);
  }
  return TRUE;
}

HIDDEN bool dateformat_handler(TMask_field& f, KEY k)
{
  if (k == K_SPACE) 
  {
    TMask& m = f.mask();
    
    char fmt[8]; 
    fmt[0] = m.get(F_DFORMAT)[0];
    fmt[1] = m.get(F_DDAY)[0];
    fmt[2] = m.get(F_DMONTH)[0];
    fmt[3] = m.get(F_DYEAR)[0];
    fmt[4] = m.get(F_DSEP)[0];
    fmt[5] = '\0';
    
    const TDate d(TODAY);
    const TFormatted_date ex(d,fmt);
    m.set(F_DEXAMPLE, ex.string());
  }
  return TRUE;
}

HIDDEN bool fmt_handler(TMask_field& f, KEY k)
{              
  if (k==K_ENTER || k==K_TAB)
  {                           
    TMask& m = f.mask();
    TPrint_section& s = ::section();
    if (s.columnwise())
    {
      const int fmt_len = m.get(F_PICTURE).len();       // length of picture
      m.set(F_LENFMT,fmt_len);
      m.set(F_NUMCOL,s.fields());
    }
    else
      m.set(F_NUMCOL,999);
  }
  return TRUE;    
}

HIDDEN bool font_handler(TMask_field& f, KEY key)
{
  if (key == K_SPACE)
  {
    TWait_cursor hourglass;

    const char* family = f.get();
    const int MAXSIZES = 16;
    long sizes[MAXSIZES];
    BOOLEAN scalable = FALSE;
    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_SIZE);
    lst.replace_items(pn1, pn2);
    
    TString16 str; str.format("%d",printer().get_char_size());
    lst.set(str);
  }
  return TRUE;
}


///////////////////////////////////////////////////////////
// TForm_flags
///////////////////////////////////////////////////////////

// Set mask fields
// Certified 100%
void TForm_flags::print_on(TMask& m)
{
  m.set(F_DISABLED,  enabled   ? " " : "X");
  m.set(F_HIDDEN,    shown     ? " " : "X");
  m.set(F_AUTOMAGIC, automagic ? "X" : " ");
  m.set(F_FINKL,     finkl     ? " " : "X");
  m.set(F_FINKR,     finkr     ? " " : "X");
}


// Get mask fields
// Certified 100%
void TForm_flags::read_from(const TMask& m)
{
  shown     = !m.get_bool(F_HIDDEN);
  enabled   = !m.get_bool(F_DISABLED);
  automagic = m.get_bool(F_AUTOMAGIC);
  finkl     = !m.get_bool(F_FINKL);
  finkr     = !m.get_bool(F_FINKR);
}

///////////////////////////////////////////////////////////
// TForm_item
///////////////////////////////////////////////////////////

void TForm_item::print_on_sheet_row(TToken_string& tt) const
{
  TString tmp(80);
  tt.add(_id, idt_id - 101);
  tt.add(_desc, dsc_id - 101);
  tt.add(_x, _section->columnwise() ? col_id - 101 : xps_id - 101);
  tt.add(_width, len_id - 101);
  if (!_section->columnwise())
    tt.add(_y, yps_id - 101);
  tt.add(class_name(), typ_id - 101);
  tt.add(shown() ? " " : "X", prn_id - 101);
  tmp = example(); tt.add(tmp, frm_id - 101);

  if (_section->columnwise())
  {
    tt.add(_ofs, spc_id -101);
    tt.add(finkl() ? " " : "X", fnl_id -101);
    tt.add(finkr() ? " " : "X", fnr_id -101);
    tt.add(_col_head, int_id - 101);
  }
}

void TForm_item::print_on(TMask& m)
{
  m.set(F_CLASS, class_name());
  m.set(F_ID, id());
  m.set(F_KEY, key());
  m.set(F_X, _x);
  m.set(F_Y, _y);
  m.set(F_PROMPT, _prompt);
  m.set(F_WIDTH, _width);
  if (_section->columnwise())
    m.set(F_INTEST, _col_head);
  m.set(F_HEIGHT, _height);
  m.set(F_SPACES, _ofs);

  _flag.print_on(m);

  for (int g = 1; g <= 24; g++)
    m.set(F_GROUP+g, _group[g] ? "X" : " ");
}

void TForm_item::read_from(const TMask& m)
{
  _desc = m.get(F_KEY);
  _x = atoi(m.get(F_X));
  _y = atoi(m.get(F_Y));
  _prompt = m.get(F_PROMPT);
  _width = atoi(m.get(F_WIDTH));
  if (_section->columnwise())
    _col_head = m.get(F_INTEST);
  _height = atoi(m.get(F_HEIGHT));
  _id     = atoi(m.get(F_ID));
  _ofs    = atoi(m.get(F_SPACES));

  _flag.read_from(m);

  _group.reset();
  for (int g = 1; g <= 24; g++)
    _group.set(g, m.get_bool(F_GROUP+g));
}

void TForm_item::read_from(TToken_string& s)
{
  _id     = s.get_int(idt_id - 101);
  _desc   = s.get(dsc_id - 101);
  _x      = _section->columnwise() ? s.get_int(col_id - 101) : s.get_int(xps_id - 101);
  _width = s.get_int(len_id - 101);
  if (!_section->columnwise())
    _y      = s.get_int(yps_id - 101);
  _flag.set_shown(s.get(prn_id - 101)[0] != 'X');

  if (_section->columnwise())
  {
    _ofs    = s.get_int(spc_id - 101);
    _flag.set_finkl(s.get(fnl_id - 101)[0] != 'X');
    _flag.set_finkr(s.get(fnr_id - 101)[0] != 'X');
    _col_head = s.get(int_id - 101);
  }
  set_dirty();
}

bool TForm_item::edit(TMask& m)
{
  m.enable(F_CLASS, m.insert_mode());
  m.reset();

  if (m.insert_mode())
  {
    short id = 0;
    for (word i = 0; i < section().fields(); i++)
    {
      const TForm_item& f = section().field(i);
      if (f.id() > id) id = f.id();
    }
    _id = id+1;
  }

  print_on(m);

  const bool godmode = form().edit_level() > 1;
  m.enable_page(1, godmode);
  m.enable(-7, godmode);
  m.enable(F_ID, godmode);
  m.enable(F_KEY,godmode);
  m.enable(F_Y,!_section->columnwise());
  m.enable(F_INTEST,_section->columnwise());
  if (_flag.memo)
  {
    m.disable(F_PROMPT);
    m.hide(F_PROMPT);
    m.enable(F_MEMO);
    m.show(F_MEMO);
    m.enable(F_HEIGHT);
    m.show(F_HEIGHT);
  }
  else
  {
    m.hide(F_MEMO);
    m.disable(F_MEMO);
  }
  const bool dirty = m.run() == K_ENTER;
  if (dirty)
  {
    read_from(m);
    set_dirty();
  }
  return dirty;
}

///////////////////////////////////////////////////////////
// TForm_string
///////////////////////////////////////////////////////////

bool TForm_string::edit(TMask& m)
{
  const bool godmode = form().edit_level() > 1;
  m.enable(F_PROMPT, godmode ? TRUE : (_field.items()==0));
  return TForm_item::edit(m);
}

void TForm_string::print_body(ostream& out) const
{
  TForm_item::print_body(out);
  if (_picture.not_empty())
    out << " PICTURE \"" << _picture << "\"" << endl;
  for (int i = 0; i < _field.items(); i++)
    out << " FIELD " << field(i) << endl;
}

bool TForm_string::read_from(const TRectype& prof)
{
  bool changed = TForm_item::read_from(prof);

  const TString& pict = prof.get("PICT");
  if (_picture != pict)
  {
    _picture = pict;
    changed = TRUE;
  }

  if (has_memo())
  {
    const TString& m = prof.get("TESTO");
    if (_memo != m)
    {
      _memo = m;
      changed = TRUE;
    }
  }

  return changed;
}

void TForm_string::print_on(TToken_string& row)
{
  TForm_item::print_on(row);
  if (_field.items() && form().edit_level() > 1)
    row << '|' << field(0);
}

void TForm_string::print_on(TRectype& prof)
{
  TForm_item::print_on(prof);
  prof.put("PICT", _picture);
  if (has_memo())
  {
    prof.put("TESTO", _memo);
  }
}

void TForm_string::print_on(TMask& m)
{
  TForm_item::print_on(m);
  for (int i = 0; i < _field.items(); i++)
    put_fieldref(field(i), m.field(i == 0 ? F_FIELDREF1 : F_FIELDREF2));

  m.set(F_PICTURE, _picture);
  m.set(F_MEMO, _memo);

  TSheet_field& s = (TSheet_field&)m.field(F_ITEMS);
  s.reset();
  if (_message.items() > 0)
  {
    TToken_string& row = s.row(0);
    row = " | ";
    row.add(message(0));
  }
}

void TForm_string::read_from(const TMask& m)
{
  TForm_item::read_from(m);
  _picture = m.get(F_PICTURE);
  _memo    = m.get(F_MEMO);

  for (int i = 0; i < 2; i++)
  {
    const TString& f = m.get(i == 0 ? F_FIELDREF1 : F_FIELDREF2);
    if (f.not_empty())
    {
      TFieldref* fr = (TFieldref*)_field.objptr(i);
      if (fr == NULL)
      {
        fr = new TFieldref(f, 0);
        _field.add(fr, i);
      }
      *fr = f;
    }
    else
      _field.destroy(i);
  }

  TSheet_field& f = (TSheet_field&)m.field(F_ITEMS);
  TToken_string& msg = f.row(0);
  if (msg.empty_items())
    _message.destroy(0);
  else
    _message.add(msg.get(2), 0);
}

///////////////////////////////////////////////////////////
// TForm_subsection
///////////////////////////////////////////////////////////

bool TForm_subsection::edit(TMask& m)
{
  // mask con nome e bottone edit contents / annulla
  TMask mu("ba2100u");
  mu.set(F_CAPTION, _name);
  mu.set(F_WIDTH,   _width);
  mu.set(F_HEIGHT,  _height);
  mu.set(F_X,       _x);
  mu.set(F_Y,       _y);

  mu.set_handler(F_BUT_FILE1, but_file_handler_sub);

  if (_file_id != -1)
  {
    // set file description
    form().rel_desc().set_cur_file(_file_id);
    TString80 desc; desc << form().rel_desc().file_desc();
    mu.set(F_FILE1, desc);
  }

  KEY k;

  // vedere se e' nuova etc.
  // gestire aggiunta / modifica menu

  while ((k = mu.run()) != K_ESC)
  {
    if (mu.field(F_CAPTION).dirty())
      _name = mu.get(F_CAPTION);

    if (mu.field(F_WIDTH).dirty())
      _width = mu.get_int(F_WIDTH);

    if (mu.field(F_HEIGHT).dirty())
      _height = mu.get_int(F_HEIGHT);

    if (mu.field(F_X).dirty())
      _x = mu.get_int(F_X);

    if (mu.field(F_Y).dirty())
      _y = mu.get_int(F_Y);

    if (mu.field(F_FILE1).dirty())
    {
      if (mu.get(F_FILE1).empty())
        _file_id = -1;
      else
        _file_id = form().rel_desc().file_num();
    }

    if (k == K_INS)
      _ssec.edit(_name);
    else if (k == K_DEL)
    {
      // remove myself

    }
    else if (k == K_ENTER)
      break;
  }

  return k != K_ESC;
}


void TForm_subsection::print_on(ostream& out) const
{
  out << "SEZIONE " << _name << ' ' << _width << ' ' << _height
    << ' ' << _x << ' ' << _y;

  if (_file_id != -1)
    out << " FILE " << _file_id;
  out << "\n";

  for (word i = 0; i < _ssec.fields(); i++)
    out << _ssec.field(i);
  out << "\nEND\n";
}
               
///////////////////////////////////////////////////////////
// TPrint_section
///////////////////////////////////////////////////////////

// @doc EXTERNAL

// @mfunc Legge dal record <p rec> altezza e offset prima colonna della sezione
//
// @rdesc Ritorna se i valori letti hanno modificato quelli attuali
bool TPrint_section::read_from(
     const TRectype& prof)  // @parm Record dal quela leggere i valori

// @comm Nel caso il record non sia LF_RFORM viene dato un messaggio di <f CHECK>.
{
  CHECK(prof.num() == LF_RFORM, "Il record deve essere del file LF_RFORM");

  bool changed = FALSE;
  const word h = (word)prof.get_int("HGT");
  const word l = (word)prof.get_int("LEN");
  const word y = (word)prof.get_int("Y");
  if (_height != h)
  {
    _height = h;
    changed = TRUE;
  }
  if (_ofspc != l)
  {
    _ofspc = l;
    changed = TRUE;
  }
  if (_ofsvr != y)
  {
    _ofsvr = y;
    changed = TRUE;
  }
  return changed;
}

void TPrint_section::print_on(TRectype& prof)
{
  CHECK(prof.num() == LF_RFORM, "Il record deve essere del file LF_RFORM");
  prof.put("ID", 0);
  prof.put("X", 0);
  prof.put("Y", _ofsvr);
  prof.put("LEN", _ofspc);
  prof.put("HGT", _height);
}

typedef struct 
{
  TString80 name_1; // Fontname old
  TString80 name_2; // Fontname new
  int  size_1;      // size (height) of old font
  int  size_2;      // size (height) of new font
  real ratio;       // ratio (width_old_font/width_new_font)
} s_data;

BOOLEAN wpr(long data)
{
  s_data* st =(s_data*)data;
  WINDOW prwin = xvt_print_create_win(printer().get_printrcd(),"");
  long width_old,width_new;
  TString spc(128, 'm');
  xvtil_set_font(prwin,st->name_1, XVT_FS_NONE, st->size_1);
  width_old = xvt_dwin_get_text_width(prwin, spc, spc.len());
  xvtil_set_font(prwin,st->name_2, XVT_FS_NONE, st->size_2);
  width_new = xvt_dwin_get_text_width(prwin,spc, spc.len());
  st->ratio = (double)width_old / (double)width_new;
  xvt_vobj_destroy(prwin);

  return FALSE;
}

bool TPrint_section::special_field_handler(TMask_field& f, KEY k)
{
  if (k == K_SPACE && _special_mask != NULL)
     _special_mask->run();
  return TRUE;
}

bool TPrint_section::repos_fields(const char* name, int size)
{
  TPrint_section& ps = ::section();
  bool rt = FALSE;
  if (ps.form().fontname() != name ||
    ps.form().fontsize() != size)
  {
    if (!ps.form().dirty()) ps.form().set_dirty();
    ps.set_dirty();
    if (yesno_box("E' stato cambiato il font o la dimensione del carattere.\nSi desidera aggiornare le coordinate dei campi?"))
    {
      rt = TRUE;
      s_data prm;
      prm.size_1=ps.form().fontsize();
      prm.name_1 = ps.form().fontname();
      prm.size_2=size;
      prm.name_2 = name;
      prm.ratio = 1.0;
      // Next 3 lines may be changed
      xvt_print_open();
      xvt_print_start_thread (wpr, (long)&prm);
      xvt_print_close();

      if (ps.form().edit_level() > 1)
      {
        TMask rm("Rapporto tra i caratteri", 1, 30, 4);
        rm.add_number(DLG_USER, 0, "Rapporto ", 1, 1, 8, "", 4);
        rm.add_button(DLG_OK, 0, "Conferma", -12, -1, 10, 2);
        rm.add_button(DLG_CANCEL, 0, "Annulla", -22, -1, 10, 2);
        real ratio = prm.ratio;
        rm.set(DLG_USER, ratio);
        if (rm.run() == K_ENTER)
        {
          ratio = rm.get_real(DLG_USER);
          prm.ratio = ratio;
        }
      }

      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))
        {
          TPrint_section* sec = ps.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();
              }
            }
          }
        }
      }
    }
    ps.form().fontname() = name;
    ps.form().fontsize() = size;
    // Aggiorna lo spreadsheet
    TSheet_field& ms = ps._msk->sfield(F_FIELDS);
    TToken_string tt(128);
    const word flds = ps.fields();
    for (word i = 0; i < flds; i++)
    {
      TForm_item& f = ps.field(i);
      f.print_on_sheet_row(tt);
      ms.row(i) = tt;
    }
  }
  return rt;
}
               
// handlers for section editing
bool TPrint_section::detail_field_handler(TMask_field& f, KEY k)
{
  if (k == K_SPACE)
  {
    const TFixed_string sm(_cur_form->section_mask());
    if (_cur_form && sm.left(2) == "ba")
    {
      TString name(_msk->get(F_FONT));
      int     size = _msk->get_int(F_SIZE);
      repos_fields(name, size);
    }

    bool consider_sheet_mask = FALSE;
    if (f.mask().is_running())  // Se la maschera di editing dello sheet e' running
      consider_sheet_mask = TRUE;     // allora deve tener conto anche di essa per l'I/O

    // to avoid kasinations with recursion
    TPrint_section& section = ::section();
    TSheet_field& ms = *f.mask().get_sheet();
    int field = ms.selected();
    TToken_string& tt = ms.row(field);
    TToken_string  tt_mask(tt);
    if (consider_sheet_mask)
    {
      TMask& msheet = f.mask();
      tt_mask.add(msheet.get(idt_id), idt_id - 101);
      tt_mask.add(msheet.get(dsc_id), dsc_id - 101);
      tt_mask.add(msheet.get(len_id), len_id - 101);
      tt_mask.add(msheet.get(prn_id), prn_id - 101);

      if (section.columnwise())
      {
        tt_mask.add(msheet.get(col_id), col_id - 101);
        tt_mask.add(msheet.get(spc_id), spc_id -101);
        tt_mask.add(msheet.get(int_id), int_id - 101);
        tt_mask.add(msheet.get(fnl_id), fnl_id -101);
        tt_mask.add(msheet.get(fnr_id), fnr_id -101);
      }
      else
      {
        tt_mask.add(msheet.get(xps_id), xps_id - 101);
        tt_mask.add(msheet.get(yps_id), yps_id - 101);
      }
    }

    TMask msk("ba2100f");

    msk.set_handler(F_DFORMAT,    dateformat_handler);
    msk.set_handler(F_DYEAR,      dateformat_handler);
    msk.set_handler(F_DMONTH,     dateformat_handler);
    msk.set_handler(F_DDAY,       dateformat_handler);
    msk.set_handler(F_DSEP,       dateformat_handler);

    msk.set_handler(F_FILE1,  but_file_handler);
    msk.set_handler(F_FIELD1, but_field_handler);
    msk.set_handler(F_FROM1,      from_handler);
    msk.set_handler(F_TO1,        to_handler);
    msk.set_handler(F_FILE2,  but_file_handler);
    msk.set_handler(F_FIELD2, but_field_handler);
    msk.set_handler(F_FROM2,      from_handler);
    msk.set_handler(F_TO2,        to_handler);
    msk.set_handler(F_PICTURE,fmt_handler);
    // TBI set_mode etc, vedi sotto

    // gna'
    TForm_item& fi = section.field(field);

    msk.enable(F_OPTIONS, fi.special_items() > 3);
    msk.enable(F_SPACES, section.columnwise());
    msk.enable(F_FINKL, section.columnwise());
    msk.enable(F_FINKR, section.columnwise());

    // build option mask
    if (fi.special_items() > 3)
    {
      msk.set_handler(F_OPTIONS, special_field_handler);

      CHECK(fi.special_items() < 18, "Quanti special! Non ho nessuna voglia di "
            " farti una maschera a piu' pagine. Ripensaci e riprova");

      _special_mask = new TMask("Variabili personalizzate", 1, 78, fi.special_items() + 3);
      _special_mask->add_button(DLG_OK, 0, "", -12, -1, 10, 2);
      _special_mask->add_button(DLG_CANCEL, 0, "", -22, -1, 10, 2);
      fi.specials().restart();

      for (int k = 0; k < fi.special_items(); k++)
      {
         THash_object*  ho = fi.specials().get_hashobj();
         TToken_string& tt = (TToken_string&)(ho->obj());
         TString type = tt.get(0);
         TString val  = tt.get(1);
         TToken_string des(tt.get(2),'/');
         TString prompt(des.get(0));

         if (type == "STRINGA")
         {
           _special_mask->add_string(101+k, 0, prompt, 1, k+1, des.get_int(1),"");
           _special_mask->set(101+k, val);
         }
         else if (type == "NUMERO")
         {
           _special_mask->add_number(101+k, 0, prompt, 1, k+1, des.get_int(1),"",des.get_int(2));
           _special_mask->set(101+k, val);
         }
         else if (type == "LISTA")
         {
           TToken_string codes(128);
           TToken_string value(128);

           for (int jj = 2; jj < des.items(); jj++)
           {
             CHECK (jj < (des.items() - 1), "AAARGH! 'Sta LISTA special e' fatta male");
             TString t1(des.get(jj++));
             TString t2(des.get(jj));
             codes.add(t1);
             value.add(t2);
           }

           _special_mask->add_list(101+k, 0, prompt, 1, k+1, des.get_int(1), "", codes, value);
           _special_mask->set(101+k, val);
         }
         else if (type == "BOOLEAN")
         {
           _special_mask->add_boolean(101+k, 0, prompt, 1, k+1);
           _special_mask->set(101+k, val);
         }
         else if (type == "DATA")
         {
           _special_mask->add_date(101+k, 0, prompt, 1, k+1);
           _special_mask->set(101+k, val);
         }
      }
    }
    if (consider_sheet_mask)
      fi.read_from(tt_mask);
    else
      fi.read_from(tt);
    fi.edit(msk);

    // check specials
    if (_special_mask != NULL)
    {
      if (_special_mask->last_key() == K_ENTER)
      {
        fi.specials().restart();

        for (int k = 0; k < fi.special_items(); k++)
        {
           THash_object*  ho = fi.specials().get_hashobj();
           TToken_string& tt = (TToken_string&)(ho->obj());
           TString val  = tt.get(1);
           TString nvl  = _special_mask->get(k + 101);
           if (nvl != val)
           {
             tt.add(nvl, 1);
             if (tt.items() == 3) tt.add("X");
             fi.set_dirty();
           }
         }
       }

       if (_special_mask != NULL)
       {
         delete _special_mask;
         _special_mask = NULL;
       }
    }

    if (!consider_sheet_mask)
      fi.print_on_sheet_row(tt);
    else
      {
        TMask& msheet = f.mask();
        fi.print_on_sheet_row(tt_mask);
        msheet.set(idt_id, tt_mask.get_int(idt_id - 101));
        msheet.set(dsc_id, tt_mask.get(dsc_id - 101));
        msheet.set(len_id, tt_mask.get_int(len_id - 101));
        msheet.set(prn_id, tt_mask.get(prn_id - 101));
        msheet.set(frm_id, fi.example());

        if (section.columnwise())
        {
          msheet.set(spc_id, tt_mask.get_int(spc_id - 101));
          msheet.set(col_id, tt_mask.get_int(col_id - 101));
          msheet.set(int_id, tt_mask.get(int_id - 101));
          msheet.set(fnl_id, tt_mask.get(fnl_id - 101));
          msheet.set(fnr_id, tt_mask.get(fnr_id - 101));
        }
        else
        {
          msheet.set(xps_id, tt_mask.get_int(xps_id - 101));
          msheet.set(yps_id, tt_mask.get_int(yps_id - 101));
        }
      }

    ms.force_update();

    _cur_sect = &section;
  }
  return TRUE;
}

bool TPrint_section::detail_field_notify (TSheet_field& s, int r, KEY k)
{
  TPrint_section& sec = ::section();
  if (k == K_DEL || k == K_INS)
  {
    if (sec.form().edit_level() <= 1)
      return FALSE;
    if (k == K_DEL)
    {
      // elimina campo
      sec.destroy_field(r);
      sec.set_dirty();
    }
  }
  else if (k == (K_INS + K_CTRL))
  {
    // new field: set defaults and create field
    TForm_string* f = new TForm_string(&sec);
    sec.insert_field(r, f);
    TToken_string& tt = s.row(r);
    f->print_on_sheet_row(tt);
    tt.add("Nuovo campo", sec.columnwise() ? int_id : dsc_id);
    if (sec.columnwise()) tt.add(r+1, col_id - 101);
    s.force_update();
    sec.set_dirty();
  }
  else if (k == K_ENTER)
  {
    // modify field
    TForm_item*   fld = &sec.field(r);
    TToken_string& tt = s.row(r);

    if (sec.columnwise() && ((word)tt.get_int(col_id - 101) <= 0 ||
                             (word)tt.get_int(col_id - 101) > sec.fields()))
    {
      warning_box("Numero di colonna non accettabile (deve essere da 1 a %u)", sec.fields());
      return FALSE;
    }
    // ??? type changed ???
    if (strcmp(tt.get(typ_id - 101), fld->class_name()) != 0)
    {
      // so'ccazzi: crea nuovo campo del tipo dato e
      // copia gli special (e il resto)
      const TString typ(tt.get(typ_id - 101));
      TForm_item* fff = sec.create_item(typ);

      // copia SPECIALS
      TAssoc_array& aa = fld->specials();
      TAssoc_array& bb = fff->specials();
      THash_object* oo = NULL;
      aa.restart();

      for (int i = 0; i < aa.items(); i++)
      {
         oo = aa.get_hashobj();
         bb.add(oo->key(), oo->obj());
      }
      sec.change_field(r, fld = fff);
      fld->set_dirty();
    }
    // modifica valori
    fld->read_from(tt);
  }
  else
  if (k == K_SPACE)
  {
    if (_cur_form && TFixed_string(_cur_form->section_mask()).starts_with("ba"))
    {
      TString80 name(s.mask().get(F_FONT));
      int       size = s.mask().get_int(F_SIZE);
      if (repos_fields(name,size))
        s.force_update();
    }
  }

  return TRUE;
}

// @doc EXTERNAL

// @mfunc Esegue l'edit della sezione di stampa
//
// @rdesc Ritorna TRUE se e' stata modificata effettivamente
bool TPrint_section::edit(
     const char* title)  // @parm Titolo della maschera di edit
{
  const bool is_ba_editor = TFixed_string(_form->section_mask()).starts_with("ba");
  bool nstd_dirty = FALSE;
  bool  font_found = FALSE;

  _cur_form = _form;
  _cur_sect = this;

  if (!_upsection)
  {
    TMask m(_form->section_mask());
    _msk = &m;
    m.set_caption(title);
    m.set(F_HEIGHT, _height);
    m.set(F_OFSPC, _ofspc);
    m.set(F_OFSVR, _ofsvr);
    if (is_ba_editor)
    {
      m.set(F_X, form().offset_x());
      m.set(F_Y, form().offset_y());
      TString16 str; str << form().char_to_pos();
      m.set(F_CTP, str);
      m.set(F_IPX, form().ipx());
      m.set(F_IPY, form().ipy());
      m.set(F_FPX, form().fpx());
      m.set(F_FLEN, printer().formlen());
    }
    else
       _form->pre_edit_checks(m,_cur_sect);
    TSheet_field& ms = m.sfield(F_FIELDS);

    m.hide(F_OFSVR);//Offset verticale, per _columnwise. Non ancora usato.
    if (_columnwise)
    {
      m.disable(F_HEIGHT);
      ms.delete_column(xps_id); ms.sheet_mask().hide(xps_id);
      ms.delete_column(yps_id); ms.sheet_mask().hide(yps_id);
    }
    else
    {
      m.hide(F_OFSPC);
      ms.delete_column(int_id); ms.sheet_mask().hide(int_id);
      ms.delete_column(col_id); ms.sheet_mask().hide(col_id);
      ms.delete_column(spc_id); ms.sheet_mask().hide(spc_id);
      ms.delete_column(fnl_id); ms.sheet_mask().hide(fnl_id);
      ms.delete_column(fnr_id); ms.sheet_mask().hide(fnr_id);
    }

    ms.enable_column(frm_id - 101, FALSE);

    if (form().edit_level() <= 1)
    {
      ms.enable_column(idt_id - 101, FALSE);
      ms.enable_column(typ_id - 101, FALSE);
    }

    // handlers
    ms.set_notify(detail_field_notify);
    ms.sheet_mask().set_handler(100, detail_field_handler);
    if (_form->edit_level()<=1) ms.sheet_mask().disable(DLG_DELREC);

    TToken_string tt(128);
    const word flds = fields();

    // fill sheet
    for (word i = 0; i < flds; i++)
    {
      TForm_item& f = field(i);
      f.print_on_sheet_row(tt);
      // TBI colorare se specials (e vedi se colorare solo se non standard)
      ms.row(-1) = tt;
    }

    if (is_ba_editor)
    {
      const int MAX_FAMILIES = 128;
      char* family[MAX_FAMILIES];
      const int num_families = (int)xvt_fmap_get_families(printer().get_printrcd(), family, MAX_FAMILIES);
      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&)m.field(F_FONT);
      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());
      printer().set_char_size(form().fontsize());
      m.set_handler(F_FONT,font_handler);
    }

    if(_columnwise)
    {
      bool ok = FALSE;
      while (m.run() != K_ESC)
      {
        TSheet_field& ms = m.sfield(F_FIELDS);
        const int items = ms.items();
        int i;
        
        for (i=0; i<items && !ok; i++)
        {
          TToken_string& tt = ms.row(i);
          ok = tt.get_char(prn_id - 101)==' ';
        }
        if (i==items && !ok) 
          error_box("Selezionare almeno una colonna stampabile.");
        if (ok) 
          break;
      }
      _msk = NULL;
      if (!ok) return FALSE;
    }
    else
      if (m.run() == K_ESC)
      {
        // Se premo Annulla ed il form e' nuovo lo devo cancellare.
        if (form()._isnew)
        {
          TLocalisamfile frm(LF_FORM);
          frm.zero();
          frm.put("TIPOPROF",form().name());
          frm.put("CODPROF",form().code());
          frm.remove();
        }
        _msk = NULL;
        return FALSE;
      }

    bool dirty = m.dirty() != 0;

    if (dirty)
    {
      // Controlli da fare sempre a prescindere dal form editor
      if (_height != (word)m.get_int(F_HEIGHT) )
      {
        _height = m.get_int(F_HEIGHT);
        _dirty=TRUE;
      }
      if (_ofspc != (word)m.get_int(F_OFSPC) )
      {
        _ofspc = m.get_int(F_OFSPC);
        _dirty=TRUE;
      }
      if (_ofsvr != (word)m.get_int(F_OFSVR) )
      {
        _ofsvr = m.get_int(F_OFSVR);
        _dirty=TRUE;
      }
      if (is_ba_editor)
      { // Controlli solo se il form editor e' quello base
        if (m.get_int(F_X) != form().offset_x() || m.get_int(F_Y) != form().offset_y())
        {
          form().offset_x() = m.get_int(F_X);
          form().offset_y() = m.get_int(F_Y);
          form().set_dirty();
          _dirty = TRUE;
        }
        if (m.get(F_CTP)[0] != form().char_to_pos() ||
            m.get_int(F_IPX) != form().ipx() ||
            m.get_int(F_IPY) != form().ipy() ||
            m.get_int(F_IPX) != form().fpx())
        {
          form().char_to_pos() = m.get(F_CTP)[0];
          form().ipx() = m.get_int(F_IPX);
          form().ipy() = m.get_int(F_IPY);
          form().fpx() = m.get_int(F_FPX);
          _dirty = TRUE;
        }

        TString80 name(m.get(F_FONT));
        int       size = m.get_int(F_SIZE);
        repos_fields(name,size);
      } else  // controlli se l'editor non e' quello base
          nstd_dirty = _form->post_edit_checks(m,_cur_sect);
    }
    _msk = NULL;
  }

  if (is_ba_editor)
    if (form()._isnew || (_dirty && yesno_box("Dati generali modificati. Salvare?")))
    {
      TLocalisamfile frm(LF_FORM);
      frm.zero();
      frm.put("TIPOPROF",form().name());
      frm.put("CODPROF",form().code());
      if (frm.read(_isequal,_lock) == NOERR)
      {
          frm.put("OFFY",form().offset_y());
          frm.put("OFFX",form().offset_x());
          frm.put("FONTNAME",form().fontname());
          frm.put("FONTSIZE",form().fontsize());
          frm.put("CTP",form().char_to_pos());
          frm.put("IPX", form().ipx());
          frm.put("IPY", form().ipy());
          frm.put("FPX", form().fpx());
          frm.rewrite();
          _dirty = FALSE;
      }
    }

  if (!_dirty)
    for (word j = 0; j < fields(); j++)
      _dirty |= field(j).dirty();

  set_dirty(_dirty);
  return (_dirty || nstd_dirty);
}

void TPrint_section::print_on(ostream& out) const
{
  out << ' ';
  switch (page_type())
  {
  case even_page:
    out << "EVEN"; break;
  case first_page:
    out << "FIRST"; break;
  case last_page:
    out << "LAST"; break;
  default:
    out << "ODD"; break;
  }
  out << ' ' << _height;
  if (_columnwise) out << " COLUMNWISE";
  out << endl << endl;
  for(word i = 0; i < fields(); i++)
    if (!field(i).temp()) out << field(i);
}