#ifndef __MASK_H
#define __MASK_H

#ifndef __WINDOW_H
#include <window.h>
#endif

#ifndef __MASKFLD_H
#include <maskfld.h>
#endif

#ifndef __REAL_H
#include <real.h>
#endif


// @T
typedef bool (*MASK_HANDLER)(TMask& mask, KEY key);
enum TMaskmode { NO_MODE, MODE_INS , MODE_MOD , MODE_DEL ,
                 MODE_VIS , MODE_QUERY, MODE_QUERYINS,
                 MODE_SEARCH };
// @END

// @C
// Classe TMask : public TWindow
// @END

class TMask : public TWindow
{
  // @DPRIV
  enum { MAX_PAGES = 12 };      // Massimo numero di pagine nella maschera
  WINDOW _pagewin[MAX_PAGES+1]; // Windows of the pages
  WINDOW _pagepag[MAX_PAGES];   // Windows of pgup/pgdn
  WINDOW _pagetag[MAX_PAGES];   // Windows of pagetags
  
  int _pages;                   // Number of pages of the mask
  int _page;                    // Current page

  TBit_array _enabled;          // Are pages enabled
  int    _mode;                 // Mode of the mask
  TArray _field;                // Fields in the mask
  
  int _first_focus;             // First control to have focus
  int _focus;                   // Control with focus
  int _sheets;                  // Number of sheets

  MASK_HANDLER _handler;        // User defined key handler

  TFilename _source_file;       // Source file of the mask
  TFilename _workfile;          // Name of savefile
  long _lastpos;                // last read offset on savefile

  real _exchange;               // Current value exhange
  bool _sheetmask;              // Mask owned by a sheet

protected:
  // Ritorna la finestra della pagina corrente (Usare con attenzione)
  WINDOW win() const { return _page == -1 ? NULL_WIN : _pagewin[_page]; }
  WINDOW toolwin() const { return _pagewin[MAX_PAGES]; }

  int find_field_win(WINDOW win) const; // number of field with window win

  void set_mask_fields() const;         // update screen

  void load_checks() const;             // load checks related fields
  virtual void start_run();             // called when the mask starts to run
  virtual TMask_field* parse_field(TScanner& scanner);

  void init_mask();
  WINDOW read_page(TScanner& scanner, bool toolbar = FALSE);
  void read_mask(const char* name, int num, int max);
  void add_buttons();

  int find_parent_page(const TMask_field&) const;
  int find_first_field(WINDOW w, int dir) const;
  int find_active_field(int first, int dir) const;                
  bool check_current_page();              // Check all the fields on the current page
  void next_page(int p);                  // Show next/previous page
  int curr_page() const { return _page; } // Current page number

  bool test_focus_change(WINDOW w = NULL_WIN);
  void control_handler(EVENT* ep);
  void handler(WINDOW win, EVENT* ep);

public:
  // @FPUB
  // crea leggendo descrizione da file .msk
  TMask(const char* name, int num = 0, int max = MAX_PAGES);

  // crea mask vuota con parametri dati
  TMask(const char* title, int pages, int cols, int rows, int xpos = -1,
        int ypos = -1);

  virtual ~TMask();

  const TFilename& source_file() const { return _source_file; }

  // aggiunta campi a runtime
  void add_static (short id, int page, const char* prompt, int x, int y,
                   const char* flags = "");
  void add_string (short id, int page, const char* prompt, int x, int y,
                   int dim, const char* flags = "", int width = 0);
  void add_number (short id, int page, const char* prompt, int x, int y,
                   int dim,  const char* flags = "", int ndec = 0);
  void add_date   (short id, int page, const char* prompt, int x, int y,
                   const char* flags = "");
  void add_button (short id, int page, const char* prompt, int x, int y,
                   int dx = 9, int dy = 1, const char* flags = "");

  void add_radio(short id, int page, const char* prompt, int x, int y,
                 int dx, const char* codes, const char* items, const char* flags = "");

  int fields() const { return _field.items(); }
  int sheets() const { return _sheets; }
  
  void set_mode(int m) { _mode = m; }
  int mode() const { return _mode; }
  
  void set_exchange(bool show_value, const real& nuo);
  const real& exchange() const { return _exchange; }

  bool check_fields();
  void get_mask_fields();               // read screen contents
  virtual bool stop_run(KEY key);       // called to close the mask
  virtual bool can_be_closed() const;

  virtual void open();
  virtual void close();
  virtual void enable(bool on) const;

  int id2pos(short id) const;
  TMask_field& fld(int i) const { return (TMask_field&)_field[i]; } // Ritorna il campo i-esimo della maschera
  TMask_field& field(short id) const;   // field with given id
  TEdit_field& efield(short id) const;  // edit-field with given id

  void set(short fld_id, const char* str, bool hit=FALSE);
  void set(short fld_id, long num, bool hit=FALSE);
  const TString& get(short fld_id) const;
  long get_long(short fld_id) const;
  int get_int(short fld_id) const { return (int)get_long(fld_id); }
  bool get_bool(short fld_id) const;

  int first_focus(short id);
  void set_focus();
  void move_focus_field(int num);
  void set_focus_win(WINDOW win, bool force);
  int focus_field() const { return _focus;}

  virtual bool on_key(KEY key);
  
  void enable(short fld_id, bool on = TRUE);               // Abilita un campo
  void disable(short fld_id) { enable(fld_id, FALSE); }
  void enable_default(short fld_id = -1);

  void enable_page(byte p, bool on = TRUE);
  void disable_page(byte p) { enable_page(p, FALSE); }
  bool page_enabled(byte p) const;
  
  byte num_keys() const;
  void enable_key(byte key, bool on = TRUE);
  void disable_key(byte key) { enable_key(key, FALSE); }
  short get_key_field(byte key, bool first) const;
  bool key_valid(int key) const;

  void show(short fld_id = -1, bool on = TRUE);
  void hide(short fld_id = -1) { show(fld_id, FALSE); }
  void show_default(short fld_id = -1);

  void reset(short fld_id = -1);
  void undo(short fld_id = -1);

  void autoload(const TRelation* = NULL);
  void autosave(TRelation* = NULL) const;

  void send_key(KEY key, short id, TMask_field* from = NULL);
  void set_handler(short fld_id, CONTROL_HANDLER handler);
  void set_handler(MASK_HANDLER handler);
  void set_workfile(const char* workfile) { _workfile = workfile; _lastpos = 0L;}
  bool save(bool append = FALSE) const;
  bool load(bool reset = FALSE);

  short dirty() const;    // Ritorna il primo campo dirty
  bool is_sheetmask() const { return _sheetmask; }

  bool no_mode() const { return _mode == NO_MODE; }
  bool query_mode() const { return _mode == MODE_QUERY || _mode == MODE_QUERYINS; }
  bool edit_mode() const { return _mode == MODE_MOD; }
  bool insert_mode() const { return _mode == MODE_QUERYINS || _mode == MODE_INS; }

  virtual const char* get_caption() const;
  virtual void set_caption(const char* c);
};

#endif // __MASK_H
