(********************************************************************)
(*                                                                  *)
(*  ide7.sd7      Cgi dialog demo program.                          *)
(*  Copyright (C) 2013, 2015  Thomas Mertes                         *)
(*                                                                  *)
(*  This program is free software; you can redistribute it and/or   *)
(*  modify it under the terms of the GNU General Public License as  *)
(*  published by the Free Software Foundation; either version 2 of  *)
(*  the License, or (at your option) any later version.             *)
(*                                                                  *)
(*  This program is distributed in the hope that it will be useful, *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of  *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *)
(*  GNU General Public License for more details.                    *)
(*                                                                  *)
(*  You should have received a copy of the GNU General Public       *)
(*  License along with this program; if not, write to the           *)
(*  Free Software Foundation, Inc., 51 Franklin Street,             *)
(*  Fifth Floor, Boston, MA  02110-1301, USA.                       *)
(*                                                                  *)
(********************************************************************)


$ include "seed7_05.s7i";
  include "socket.s7i";
  include "listener.s7i";
  include "cgidialog.s7i";
  include "cgi.s7i";
  include "browser.s7i";
  include "getf.s7i";
  include "process.s7i";
  include "draw.s7i";
  include "bmp.s7i";
  include "pic32.s7i";

var label: progName is label("");
var textArea: prog is textArea(30, 100);
var submitButton: load      is submitButton(dialogColumn(image(createPixmap(load_pic,      1, light_gray)) & label("Load")));
var submitButton: save      is submitButton(dialogColumn(image(createPixmap(save_pic,      1, light_gray)) & label("Save")));
var submitButton: saveAs    is submitButton(dialogColumn(image(createPixmap(save_as_pic,   1, light_gray)) & label("Save as")));
var submitButton: clear     is submitButton(dialogColumn(image(createPixmap(clear_pic,     1, light_gray)) & label("Clear")));
var resetButton:  reset     is resetButton(dialogColumn(image(createPixmap(reset_pic,      1, light_gray)) & label("Reset")));
var submitButton: execute   is submitButton(dialogColumn(image(createPixmap(execute_pic,   1, light_gray)) & label("Execute")));
var submitButton: terminate is submitButton(dialogColumn(image(createPixmap(terminate_pic, 1, light_gray)) & label("Terminate")));
var submitButton: exit      is submitButton(dialogColumn(image(createPixmap(exit_pic,      1, light_gray)) & label("Exit")));
var submitButton: cancel    is submitButton(dialogColumn(image(createPixmap(cancel_pic,    1, light_gray)) & label("Cancel")));
var submitButton: okay      is submitButton(dialogColumn(image(createPixmap(checkmark_pic, 1, light_gray)) & label("Okay")));
var selection: fileList is selection.value;


var dialogElement: formElement is
  dialogColumn(
    image(readBmp("header3.bmp")) &
    header(1, "IDE7") &
    dialogRow(label("Seed7 program:") & space(1) & progName) &
    prog &
    vspace(20) &
    dialogSequence(load & save & saveAs & clear & reset & execute & terminate & exit)
  );

var webPage: mainWebPage is webForm("IDE7", formElement);

var dialogElement: message is
  dialogColumn(
    header(1, "Message") &
    label("Console programs are currently not supported.") &
    vspace(20) &
    dialogSequence(okay)
  );

var webPage: messagePage is webForm("IDE7 Message", message);

var dialogElement: goodbye is
  dialogColumn(
    header(1, "Thank you for using IDE7") &
    script("close();")
  );

var webPage: goodbyePage is webPage("IDE7 Goodbye", goodbye);


const proc: loadFile (inout browserConnection: browser, in string: dirPath,
    in string: extension) is func
  local
    var submitButton: load is submitButton(dialogColumn(
        image(createPixmap(load_pic, 1, light_gray)) & label("Load")));
    var string: fileName is "";
    var array string: dirContent is 0 times "";
    var webPage: selectionPage is webPage.value;
  begin
    if fileType(dirPath) = FILE_DIR then
      for fileName range readDir(dirPath) do
        if endsWith(fileName, extension) then
          dirContent &:= fileName;
        end if;
      end for;
    end if;
    fileList := selection(min(10, length(dirContent)), dirContent);
    selectionPage := webForm("IDE7 Load",
          dialogColumn(
          header(1, "Select file to be loaded") &
          fileList &
          vspace(20) &
          dialogSequence(load & reset & cancel)
        ));
    display(browser, selectionPage);
    if not cancel.activated then
      # writeln("fileList.selected: " <& fileList.selected);
      if fileList.selected <> "" then
        progName.name := fileList.selected;
        prog.content := getf(dirPath & "/" & fileList.selected);
      end if;
    end if;
  end func;


const proc: saveFileAs (inout browserConnection: browser, in string: dirPath) is func
  local
    var submitButton: save is submitButton(dialogColumn(
        image(createPixmap(save_pic, 1, light_gray)) & label("Save")));
    var textField: fileName is textField("", 0);
    var webPage: fileNameSelectionPage is webPage.value;
  begin
    fileNameSelectionPage := webForm("IDE7 Save as",
          dialogColumn(
          header(1, "Enter name of file to be saved") &
          fileName &
          vspace(20) &
          dialogSequence(save & reset & cancel)
        ));
    fileName.content := "";
    display(browser, fileNameSelectionPage);
    if not cancel.activated then
      # writeln("fileName.content: " <& fileName.content);
      if fileName.content <> "" then
        putf(dirPath & "/" & fileName.content, prog.content);
        progName.name := fileName.content;
      end if;
    end if;
  end func;


const proc: main is func
  local
    const string: workDir is ".";
    var browserConnection: browser is browserConnection.value;
    var file: childStdin is STD_NULL;
    var file: childStdout is STD_NULL;
    var process: currentProgram is process.value;
  begin
    browser := openBrowser;
    repeat
      display(browser, mainWebPage);
      # writeln("prog:          " <& toUtf8(prog.content));
      # writeln("load:          " <& load.activated);
      # writeln("save:          " <& save.activated);
      # writeln("clear:         " <& clear.activated);
      # writeln("execute:       " <& execute.activated);
      # writeln("cancel:        " <& cancel.activated);
      # writeln("exit:          " <& exit.activated);
      if load.activated then
        loadFile(browser, workDir, ".sd7");
      elsif save.activated then
        if progName.name <> "" then
          putf(workDir & "/" & progName.name, prog.content);
        end if;
      elsif saveAs.activated then
        saveFileAs(browser, workDir);
      elsif clear.activated then
        progName.name := "";
        prog.content := "";
      elsif execute.activated then
        if progName.name <> "" then
          if pos(prog.content, "GRAPH" & "_KEYBOARD") = 0 then
            display(browser, messagePage);
          else
            currentProgram := startProcess(commandPath("s7"), [] ("-q", workDir & "/" & progName.name));
          end if;
        end if;
      elsif terminate.activated then
        if currentProgram <> process.value then
          block
            kill(currentProgram);
          exception
            catch FILE_ERROR:
              noop;
          end block;
          currentProgram := process.value;
        end if;
      end if;
    until exit.activated;
    display(browser, goodbyePage);
    close(browser);
  end func;