#ifndef __APPLICATION_H
#define __APPLICATION_H

#ifndef INCL_XVTH
#include <xvt.h>
#endif

#ifndef __PRINTER_H
#include <printer.h>
#endif

// @C
// Classe TApplication
// @END

#define MSG_AI "AI"      // message auto_insert          (relapp)
#define MSG_FS "FS"      // message filtered start (relapp)
#define MSG_LN "LN"      // message  (printapp -> relapp)
#define CHK_ALL -1                       // all authorization checks
#define CHK_DONGLE 0             // dongle authorization checks
#define CHK_USER 1                       // user authorization checks

class TApplication
{
  // @DPRIV
  int _bar;
  int __argc;
  const char** __argv;
  TBit_array _dongle_aut;
  TBit_array _user_aut;

  TString80 _name, _title;
  TPrinter* _printer;

  long _savefirm;
  bool _create_ok;
  static TString16 _user;

protected:
  const char* get_module_name() const;
  static long task_eh(WINDOW win, EVENT* ep);

  virtual long handler(WINDOW win, EVENT* ep);
  virtual bool extended_firm() const { return FALSE; } // Extended set_firm dialog box

  virtual bool create();        // Crea la finestra principale
  virtual bool menu(MENU_TAG) { return TRUE; }  // Controlla il menu
  virtual bool build_firm_data(long cod, bool flagcom = FALSE) { return TRUE;}
  virtual bool destroy();       // Rimuove l'applicazione
  virtual void print();

  virtual bool change_config(const char* var, const char* oldv, const char* newv);
  void set_user(const char * user) { _user = user; }

  void terminate();
  bool config();
  void about() const;

public:
  // @FPUB

  void setbar(int menubar) { _bar = menubar;}   // Modifica la menu-bar
  void run(int argc, char* argv[], const char* name);

  // @DES Queste funzioni possono essere ridefinite da ogni applicazione
  // @FPUB
  virtual word class_id() const { return CLASS_APPLICATION; }
  void stop_run();                              // Forza chiusura applicazione

  void check_menu_item(MENU_TAG item);   // Check menu
  void uncheck_menu_item(MENU_TAG item); // Uncheck
  void enable_menu_item(MENU_TAG item, bool on = TRUE);
  void disable_menu_item(MENU_TAG item) { enable_menu_item(item, FALSE); }
  void dispatch_e_menu(MENU_TAG item);

  const TString& name()  const { return _name; }
  const char** argv() const  { return __argv; }
  const char* argv(int i) const  { return __argv[i]; }
  int argc() const  { return __argc; }

  void set_title(const char* t) { _title = t; }
  const TString& title() const { return _title; }

  void wait_for(const char* name);
  void wake_up_caller() const;

  TPrinter* set_printer(TPrinter* p);
  TPrinter& printer();

  static void check_parameters(int & argc, char *argv[]);
  
  void set_perms();
  const TString& user() const { return _user; }
  bool has_module(int module, int checktype = CHK_ALL) const;
  bool set_firm(long cod = -1);
  long get_firm() const;
  const char* get_firm_dir() const;

  TApplication();
  virtual ~TApplication();
};

bool xvt_running();        // xvt is running?
TApplication* MainApp();   // main application (old fashioned: will be removed soon
TApplication& main_app();

#endif // __APPLICATION_H
