Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "scriptconfigpanel.h"
Toshihiro Shimizu 890ddd
#include "filebrowserpopup.h"
Toshihiro Shimizu 890ddd
#include "application.h"
Toshihiro Shimizu 890ddd
#include "util.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tw/mainshell.h"
Toshihiro Shimizu 890ddd
#include "tw/label.h"
Toshihiro Shimizu 890ddd
#include "tw/button.h"
Toshihiro Shimizu 890ddd
#include "tw/keycodes.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tw/message.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace TwConsts;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
PathFileField::PathFileField(TWidget *parent, string name)
Shinya Kitaoka 120a6e
    : TTextField(parent, name) {
Shinya Kitaoka 120a6e
  m_page = dynamic_cast<scriptconfigpanel *="">(parent);</scriptconfigpanel>
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void PathFileField::onFocusChange(bool on) {
Shinya Kitaoka 120a6e
  if (!on) close();
Shinya Kitaoka 120a6e
  TWidget::onFocusChange(on);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void PathFileField::close() {
Shinya Kitaoka 120a6e
  static wstring oldText;
Shinya Kitaoka 120a6e
  if (oldText != m_text) {
Shinya Kitaoka 120a6e
    TFilePath fp(m_text);
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      wstring uncString;
Shinya Kitaoka 120a6e
      try {
Shinya Kitaoka 120a6e
        uncString = toWideString(convertToUncString(fp));
Shinya Kitaoka 120a6e
        m_text    = uncString;
Shinya Kitaoka 120a6e
        m_page->loadScript(TFilePath(uncString));
Shinya Kitaoka 120a6e
      } catch (TException &) {
Shinya Kitaoka 120a6e
        TFileStatus fs(fp);
Shinya Kitaoka 120a6e
        if (fs.doesExist() && !fs.isDirectory())
Shinya Kitaoka 120a6e
          m_page->loadScript(fp);
Shinya Kitaoka 120a6e
        else {
Shinya Kitaoka 120a6e
          ScriptTask *task = dynamic_cast<scripttask *="">(m_page->getTask());</scripttask>
Shinya Kitaoka 120a6e
          if (task) task->m_arg1 = toString(m_text);
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      oldText = m_text;
Shinya Kitaoka 120a6e
    } catch (...) {
Shinya Kitaoka 120a6e
      TMessage::error("boh");
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  invalidate();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void PathFileField::keyDown(int key, unsigned long flags, const TPoint &p) {
Shinya Kitaoka 120a6e
  if (key == TK_Return)
Shinya Kitaoka 120a6e
    close();
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    TTextField::keyDown(key, flags, p);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
enum textFieldType { M_ARG1 = 0, M_ARG2, M_ARG3, M_ARG4, M_ARG5 };
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class ArgumentChanger : public TTextField::Action {
Shinya Kitaoka 120a6e
  ScriptConfigPanel *m_scp;
Shinya Kitaoka 120a6e
  textFieldType m_typeField;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  ArgumentChanger(ScriptConfigPanel *scp, textFieldType type)
Shinya Kitaoka 120a6e
      : m_scp(scp), m_typeField(type) {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void sendCommand(std::wstring value) {
Shinya Kitaoka 120a6e
    m_scp->onTextField(toString(value), m_typeField);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ScriptConfigPanel::ScriptConfigPanel(TWidget *parent)
Shinya Kitaoka 120a6e
    : TaskConfigPanel(parent), m_task(new ScriptTask()) {
Shinya Kitaoka 120a6e
  m_fileLbl = new TLabel(this);
Shinya Kitaoka 120a6e
  m_fileLbl->setText("Arg #1:");
Shinya Kitaoka 120a6e
  m_file      = new PathFileField(this);
Shinya Kitaoka 120a6e
  m_browseBtn = new TButton(this);
Shinya Kitaoka 120a6e
  m_browseBtn->setTitle("...");
Shinya Kitaoka 120a6e
  tconnect(*m_browseBtn, this, &ScriptConfigPanel::browseFiles);
Shinya Kitaoka 120a6e
  // m_file->addAction(new ArgumentChanger(this, M_ARG1));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_arg2Lbl = new TLabel(this);
Shinya Kitaoka 120a6e
  m_arg2Lbl->setText("Arg #2:");
Shinya Kitaoka 120a6e
  m_arg2 = new TTextField(this);
Shinya Kitaoka 120a6e
  m_arg2->addAction(new ArgumentChanger(this, M_ARG2));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_arg3Lbl = new TLabel(this);
Shinya Kitaoka 120a6e
  m_arg3Lbl->setText("Arg #3:");
Shinya Kitaoka 120a6e
  m_arg3 = new TTextField(this);
Shinya Kitaoka 120a6e
  m_arg3->addAction(new ArgumentChanger(this, M_ARG3));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_arg4Lbl = new TLabel(this);
Shinya Kitaoka 120a6e
  m_arg4Lbl->setText("Arg #4:");
Shinya Kitaoka 120a6e
  m_arg4 = new TTextField(this);
Shinya Kitaoka 120a6e
  m_arg4->addAction(new ArgumentChanger(this, M_ARG4));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_arg5Lbl = new TLabel(this);
Shinya Kitaoka 120a6e
  m_arg5Lbl->setText("Arg #5:");
Shinya Kitaoka 120a6e
  m_arg5 = new TTextField(this);
Shinya Kitaoka 120a6e
  m_arg5->addAction(new ArgumentChanger(this, M_ARG5));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ScriptConfigPanel::configureNotify(const TDimension &d) {
Shinya Kitaoka 120a6e
  int x0 = 20;
Shinya Kitaoka 120a6e
  int y  = d.ly - 1 - 10;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int x = x0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_fileLbl->setGeometry(x, y - 15, x + 100, y);
Shinya Kitaoka 120a6e
  m_file->setGeometry(x + 104, y - 15, x + 650, y);
Shinya Kitaoka 120a6e
  m_browseBtn->setGeometry(x + 660, y - 15, x + 680, y);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  y -= 40;
Shinya Kitaoka 120a6e
  m_arg2Lbl->setGeometry(x, y - 15, x + 100, y);
Shinya Kitaoka 120a6e
  m_arg2->setGeometry(x + 104, y - 15, x + 650, y);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  y -= 40;
Shinya Kitaoka 120a6e
  m_arg3Lbl->setGeometry(x, y - 15, x + 100, y);
Shinya Kitaoka 120a6e
  m_arg3->setGeometry(x + 104, y - 15, x + 650, y);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  y -= 40;
Shinya Kitaoka 120a6e
  m_arg4Lbl->setGeometry(x, y - 15, x + 100, y);
Shinya Kitaoka 120a6e
  m_arg4->setGeometry(x + 104, y - 15, x + 650, y);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  y -= 40;
Shinya Kitaoka 120a6e
  m_arg5Lbl->setGeometry(x, y - 15, x + 100, y);
Shinya Kitaoka 120a6e
  m_arg5->setGeometry(x + 104, y - 15, x + 650, y);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ScriptConfigPanel::setTask(SubmitPageTask *task) {
Shinya Kitaoka 120a6e
  m_task = dynamic_cast<scripttask *="">(task);</scripttask>
Shinya Kitaoka 120a6e
  assert(m_task);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_file->setText(m_task->m_arg1);
Shinya Kitaoka 120a6e
  m_arg2->setText(m_task->m_arg2);
Shinya Kitaoka 120a6e
  m_arg3->setText(m_task->m_arg3);
Shinya Kitaoka 120a6e
  m_arg4->setText(m_task->m_arg4);
Shinya Kitaoka 120a6e
  m_arg5->setText(m_task->m_arg5);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
SubmitPageTask *ScriptConfigPanel::getTask() const { return m_task; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ScriptConfigPanel::browseFiles() {
Shinya Kitaoka 120a6e
  static FileBrowserPopup *popup = 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!popup) {
Shinya Kitaoka 120a6e
    ScriptConfigPanel *csp = this;
Shinya Kitaoka 120a6e
    vector<string> fileTypes;</string>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
    fileTypes.push_back("bat");
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
    fileTypes.push_back("csh");
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
    popup = new FileBrowserPopup(csp, fileTypes);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    popup->setOkAction(new TFileBrowserPopupAction<scriptconfigpanel>(</scriptconfigpanel>
Shinya Kitaoka 120a6e
        csp, &ScriptConfigPanel::loadScript));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!popup) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TFilePath fp = Application::instance()->getCurrentFolder();
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
E'stato necessario fare questo controllo perche' il popup non e' in grado
Shinya Kitaoka 120a6e
di impostare opportunamente le cose
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
  string path = toString(fp.getWideString()).c_str();
Shinya Kitaoka 120a6e
  if (fp != TFilePath() && (path[0] != '\\')) popup->setCurrentDir(fp);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TDimension d = TMainshell::getMainshell()->getSize();
Toshihiro Shimizu 890ddd
#ifdef WIN32
Shinya Kitaoka 120a6e
  HDC hdc = GetDC(0);
Shinya Kitaoka 120a6e
  d.lx    = GetDeviceCaps(hdc, HORZRES);
Shinya Kitaoka 120a6e
  d.ly    = GetDeviceCaps(hdc, VERTRES);
Shinya Kitaoka 120a6e
  ReleaseDC(0, hdc);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  d -= popup->getSize();
Shinya Kitaoka 120a6e
  // TDimension d = TMainshell::getMainshell()->getSize() - popup->getSize();
Shinya Kitaoka 120a6e
  popup->popup(TPoint(d.lx / 2, d.ly / 2));
Shinya Kitaoka 120a6e
  popup->setCaption("Load Script");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ScriptConfigPanel::loadScript(const TFilePath &fp) {
Shinya Kitaoka 120a6e
  if (!TFileStatus(fp).doesExist()) {
Shinya Kitaoka 120a6e
    string msg = toString(fp.getWideString()) + "\n";
Shinya Kitaoka 120a6e
    msg += "File not found\n";
Shinya Kitaoka 120a6e
    msg += "Please verify that the path and file name are correct";
Shinya Kitaoka 120a6e
    m_file->setText("");
Shinya Kitaoka 120a6e
    TMessage::error(msg);
Shinya Kitaoka 120a6e
    return;
Toshihiro Shimizu 890ddd
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  wstring uncString = fp.getWideString();
Shinya Kitaoka 120a6e
  if (m_file->getText() != uncString) try {
Shinya Kitaoka 120a6e
      uncString = toWideString(convertToUncString(fp));
Shinya Kitaoka 120a6e
    } catch (TException &e) {
Shinya Kitaoka 120a6e
      TMessage::error(toString(e.getMessage()));
Shinya Kitaoka 120a6e
      return;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Application::instance()->setCurrentFolder(fp.getParentDir());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_file->setText(uncString);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
#ifdef WIN32
Shinya Kitaoka 120a6e
int x = uncString.find("\\",2);
Shinya Kitaoka 120a6e
while (x != string::npos)
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
uncString.replace(x,1,"/",0,1);
Shinya Kitaoka 120a6e
x = uncString.find("\\",x+1);
Shinya Kitaoka 120a6e
}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
  m_task->m_arg1 = toString(uncString);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ScriptConfigPanel::onTextField(string value, int type) {
Shinya Kitaoka 120a6e
  if (!m_task) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  switch (type) {
Shinya Kitaoka 120a6e
  case M_ARG1:
Shinya Kitaoka 120a6e
    m_task->m_arg1 = value;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case M_ARG2:
Shinya Kitaoka 120a6e
    m_task->m_arg2 = value;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case M_ARG3:
Shinya Kitaoka 120a6e
    m_task->m_arg3 = value;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case M_ARG4:
Shinya Kitaoka 120a6e
    m_task->m_arg4 = value;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case M_ARG5:
Shinya Kitaoka 120a6e
    m_task->m_arg5 = value;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}