#include <diction.h>
#include <mask.h>
#include <toolfld.h>
#include <statbar.h>
#include <urldefid.h>

///////////////////////////////////////////////////////////
// TTool_field
///////////////////////////////////////////////////////////

word TTool_field::class_id() const 
{ return CLASS_TOOL_FIELD; }

bool TTool_field::is_kind_of(word cid) const
{ return cid == CLASS_TOOL_FIELD || TOperable_field::is_kind_of(cid); }

void TTool_field::create(WINDOW parent)
{
  _parent = parent;
  _cid = _ctl_data._dlg;
}
  
void TTool_field::enable(bool on)
{
  TOperable_field::enable(on);
  xvt_toolbar_enable_control(parent(), dlg(), on);
}

void TTool_field::show(bool on)
{
  TOperable_field::show(on);
  xvt_toolbar_show_control(parent(), dlg(), on);
}

TTool_field::TTool_field(TMask* mask) : TOperable_field(mask)
{ }

///////////////////////////////////////////////////////////
// TButton_tool
///////////////////////////////////////////////////////////

word TButton_tool::class_id() const 
{ return CLASS_BUTTON_TOOL; }

bool TButton_tool::is_kind_of(word cid) const
{ return cid == CLASS_BUTTON_TOOL || TTool_field::is_kind_of(cid); }

void TButton_tool::parse_head(TScanner& scanner)
{
  _ctl_data._width = scanner.integer();
  if (_ctl_data._width <= 0) _ctl_data._width = 10;
  _ctl_data._height = scanner.integer(); // Height
  if (_ctl_data._height <= 0) _ctl_data._height = 1;
}

bool TButton_tool::parse_item(TScanner& scanner)
{
  if (scanner.key() == "PI")
  {        
    const short bmp = (short)scanner.integer();
    if (_ctl_data._bmp_up == 0)
      _ctl_data._bmp_up = bmp;
    return bmp > 0;
  }
  return TTool_field::parse_item(scanner);
}

void TButton_tool::set_exit_key(KEY k) 
{ 
  switch (k)
  {
  case K_F1: _exit_key = M_HELP_CONTENTS;  break;
  case K_F2: _exit_key = M_FILE_ABOUT;  break;
  default: _exit_key = k; break;
  }
}


void TButton_tool::create(WINDOW toolbar)
{
  TTool_field::create(toolbar);
  _exit_key = 0;
  switch (dlg())
  {
  case DLG_OK:
    if (_ctl_data._prompt.empty())
      _ctl_data._prompt = TR("Conferma");   
    set_exit_key(K_ENTER);
    if (_ctl_data._bmp_up <= 0)
      _ctl_data._bmp_up = BMP_OK;
    break;
  case DLG_CANCEL:
    if (_ctl_data._prompt.empty())
      _ctl_data._prompt = TR("Annulla");
    set_exit_key(K_ESC);
    if (_ctl_data._bmp_up <= 0)
      _ctl_data._bmp_up = BMP_CANCEL;
    break;
  case DLG_DELREC:  
    if (_ctl_data._prompt.empty())
      _ctl_data._prompt = TR("Elimina"); 
    set_exit_key(K_DEL);
    if (_ctl_data._bmp_up <= 0)
      _ctl_data._bmp_up = BMP_DELREC; 
    break;
  case DLG_PRINT:
    if (_ctl_data._prompt.empty())
      _ctl_data._prompt = TR("Stampa"); 
    set_exit_key(K_ENTER);
    if (_ctl_data._bmp_up <= 0)     
      _ctl_data._bmp_up = BMP_PRINT;
    break;
  case DLG_SETPRINT:
    if (_ctl_data._prompt.empty())
      _ctl_data._prompt = TR("Imposta Stampante"); 
    set_exit_key(M_FILE_PG_SETUP);
    if (_ctl_data._bmp_up <= 0)     
      _ctl_data._bmp_up = BMP_SETPRINT;
    break;
  case DLG_QUIT:
    if (_ctl_data._prompt.empty())
      _ctl_data._prompt = TR("Fine"); 
    set_exit_key(K_QUIT);
    if (_ctl_data._bmp_up <= 0)     
      _ctl_data._bmp_up = BMP_QUIT;
    break;
  case DLG_SELECT:
    if (_ctl_data._bmp_up <= 0)
      _ctl_data._bmp_up = BMP_SELECT;
    set_exit_key(K_ENTER);
    break;
  case DLG_HELP: 
    if (_ctl_data._prompt.empty())
      _ctl_data._prompt = TR("Help"); 
    if (_ctl_data._bmp_up <= 0)
      _ctl_data._bmp_up = BMP_HELP;
    set_exit_key(K_F1); 
    break;
  case DLG_INFO: 
    if (_ctl_data._prompt.empty())
      _ctl_data._prompt = TR("Informazioni"); 
    if (_ctl_data._bmp_up <= 0)
      _ctl_data._bmp_up = BMP_INFO;
    set_exit_key(K_F2); 
    break;
  default:
    break;
  }

  _ctl_data._prompt.strip("~&"); // Togli eventuali acceleratori antiestetici

  if (dlg() <= 0 || _ctl_data._bmp_up <= 0)
    xvt_toolbar_add_control(toolbar, -1, TOOL_SEPARATOR, "", 0, _ctl_data._width, -1);
  else
    xvt_toolbar_add_control(toolbar, dlg(), TOOL_BUTTON, _ctl_data._prompt, 
                            _ctl_data._bmp_up, _ctl_data._width, -1);

  TToken_string* mess = message(0);
  if (mess)
  {
    TToken_string msg(mess->get(0), ','); 
    const TFixed_string m = msg.get(0);
    if (m == "EXIT")
      set_exit_key(msg.get_int());
    else
    {
      if (msg.get_int() == 0) 
        set_exit_key(atoi(m));
    }
  } 
}

bool TButton_tool::on_key(KEY key)
{
  bool ok = true;
  if (key == K_SPACE)   
  {
    const TMask& m = mask();
    if (dlg() != DLG_CANCEL && dlg() != DLG_QUIT)
      ok = m.focus_field().on_key(K_TAB);
    if (ok)
    {
      if (xvt_vobj_get_attr(NULL_WIN, ATTR_SPEECH_MODE) & (1<<7))
      {
        TString str = prompt();
        str.strip("&~");
        xvt_dm_post_speech(str, 7, TRUE);
      }

      if (_exit_key > M_FILE && _exit_key < FONT_MENU_TAG)
      {
        dispatch_e_menu(TASK_WIN, _exit_key);
        ok = true;
      }
      else
      {
        ok = on_hit();
        if (ok && _exit_key > 0 && !has_message())
          mask().stop_run(_exit_key);
      }
    }    
  }  
  else
    ok = TTool_field::on_key(key);
  return ok;
}
  
TButton_tool::TButton_tool(TMask* mask) : TTool_field(mask), _exit_key(0)
{ }