#ifndef __APPLICATION_H
#define __APPLICATION_H

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

#ifndef __STRINGS_H
#include <strings.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;

  long _savefirm;
  bool _create_ok;           // Succesfully created
  word _waiting;
  
  void terminate();          // End of application
  void set_cursor(bool w);   // Change mouse cursor
  bool config();             // Change parameters
  void about() const;        // About box

protected:
  const char* get_module_name() const;
  static long XVT_CALLCONV1 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 use_files() const { return TRUE; }

  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 on_config_change();
  virtual void on_firm_change();

  void set_perms();

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; }
  virtual bool ok() const { return _create_ok; }
  virtual void print();
  virtual bool firm_change_enabled() const;
  virtual const char * extra_modules() const {return "";}
  
  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; }

  static void check_parameters(int & argc, char *argv[]);

  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;
  
  void wait_for(word taskid) { _waiting = taskid; }
  void wake_up() { _waiting = 0xFFFF; }
  word waiting() const { return _waiting; }
  
  void begin_wait() { set_cursor(TRUE); }  // Set CURSOR_WAIT
  void end_wait()   { set_cursor(FALSE); } // Set CURSOR_ARROW

  // Main window menu runtime interface 
  bool add_menu(TString_array& menu, MENU_TAG id = 0);
  bool remove_menu(MENU_TAG id);

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

bool xvt_running();        // xvt is running?
TApplication& main_app();

#endif /* __APPLICATION_H */
