1
2(********************************************************************)
3(*                                                                  *)
4(*  ide7.sd7      Cgi dialog demo program.                          *)
5(*  Copyright (C) 2013, 2015  Thomas Mertes                         *)
6(*                                                                  *)
7(*  This program is free software; you can redistribute it and/or   *)
8(*  modify it under the terms of the GNU General Public License as  *)
9(*  published by the Free Software Foundation; either version 2 of  *)
10(*  the License, or (at your option) any later version.             *)
11(*                                                                  *)
12(*  This program is distributed in the hope that it will be useful, *)
13(*  but WITHOUT ANY WARRANTY; without even the implied warranty of  *)
14(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *)
15(*  GNU General Public License for more details.                    *)
16(*                                                                  *)
17(*  You should have received a copy of the GNU General Public       *)
18(*  License along with this program; if not, write to the           *)
19(*  Free Software Foundation, Inc., 51 Franklin Street,             *)
20(*  Fifth Floor, Boston, MA  02110-1301, USA.                       *)
21(*                                                                  *)
22(********************************************************************)
23
24
25$ include "seed7_05.s7i";
26  include "socket.s7i";
27  include "listener.s7i";
28  include "cgidialog.s7i";
29  include "cgi.s7i";
30  include "browser.s7i";
31  include "getf.s7i";
32  include "process.s7i";
33  include "draw.s7i";
34  include "bmp.s7i";
35  include "pic32.s7i";
36
37var label: progName is label("");
38var textArea: prog is textArea(30, 100);
39var submitButton: load      is submitButton(dialogColumn(image(createPixmap(load_pic,      1, light_gray)) & label("Load")));
40var submitButton: save      is submitButton(dialogColumn(image(createPixmap(save_pic,      1, light_gray)) & label("Save")));
41var submitButton: saveAs    is submitButton(dialogColumn(image(createPixmap(save_as_pic,   1, light_gray)) & label("Save as")));
42var submitButton: clear     is submitButton(dialogColumn(image(createPixmap(clear_pic,     1, light_gray)) & label("Clear")));
43var resetButton:  reset     is resetButton(dialogColumn(image(createPixmap(reset_pic,      1, light_gray)) & label("Reset")));
44var submitButton: execute   is submitButton(dialogColumn(image(createPixmap(execute_pic,   1, light_gray)) & label("Execute")));
45var submitButton: terminate is submitButton(dialogColumn(image(createPixmap(terminate_pic, 1, light_gray)) & label("Terminate")));
46var submitButton: exit      is submitButton(dialogColumn(image(createPixmap(exit_pic,      1, light_gray)) & label("Exit")));
47var submitButton: cancel    is submitButton(dialogColumn(image(createPixmap(cancel_pic,    1, light_gray)) & label("Cancel")));
48var submitButton: okay      is submitButton(dialogColumn(image(createPixmap(checkmark_pic, 1, light_gray)) & label("Okay")));
49var selection: fileList is selection.value;
50
51
52var dialogElement: formElement is
53  dialogColumn(
54    image(readBmp("header3.bmp")) &
55    header(1, "IDE7") &
56    dialogRow(label("Seed7 program:") & space(1) & progName) &
57    prog &
58    vspace(20) &
59    dialogSequence(load & save & saveAs & clear & reset & execute & terminate & exit)
60  );
61
62var webPage: mainWebPage is webForm("IDE7", formElement);
63
64var dialogElement: message is
65  dialogColumn(
66    header(1, "Message") &
67    label("Console programs are currently not supported.") &
68    vspace(20) &
69    dialogSequence(okay)
70  );
71
72var webPage: messagePage is webForm("IDE7 Message", message);
73
74var dialogElement: goodbye is
75  dialogColumn(
76    header(1, "Thank you for using IDE7") &
77    script("close();")
78  );
79
80var webPage: goodbyePage is webPage("IDE7 Goodbye", goodbye);
81
82
83const proc: loadFile (inout browserConnection: browser, in string: dirPath,
84    in string: extension) is func
85  local
86    var submitButton: load is submitButton(dialogColumn(
87        image(createPixmap(load_pic, 1, light_gray)) & label("Load")));
88    var string: fileName is "";
89    var array string: dirContent is 0 times "";
90    var webPage: selectionPage is webPage.value;
91  begin
92    if fileType(dirPath) = FILE_DIR then
93      for fileName range readDir(dirPath) do
94        if endsWith(fileName, extension) then
95          dirContent &:= fileName;
96        end if;
97      end for;
98    end if;
99    fileList := selection(min(10, length(dirContent)), dirContent);
100    selectionPage := webForm("IDE7 Load",
101          dialogColumn(
102          header(1, "Select file to be loaded") &
103          fileList &
104          vspace(20) &
105          dialogSequence(load & reset & cancel)
106        ));
107    display(browser, selectionPage);
108    if not cancel.activated then
109      # writeln("fileList.selected: " <& fileList.selected);
110      if fileList.selected <> "" then
111        progName.name := fileList.selected;
112        prog.content := getf(dirPath & "/" & fileList.selected);
113      end if;
114    end if;
115  end func;
116
117
118const proc: saveFileAs (inout browserConnection: browser, in string: dirPath) is func
119  local
120    var submitButton: save is submitButton(dialogColumn(
121        image(createPixmap(save_pic, 1, light_gray)) & label("Save")));
122    var textField: fileName is textField("", 0);
123    var webPage: fileNameSelectionPage is webPage.value;
124  begin
125    fileNameSelectionPage := webForm("IDE7 Save as",
126          dialogColumn(
127          header(1, "Enter name of file to be saved") &
128          fileName &
129          vspace(20) &
130          dialogSequence(save & reset & cancel)
131        ));
132    fileName.content := "";
133    display(browser, fileNameSelectionPage);
134    if not cancel.activated then
135      # writeln("fileName.content: " <& fileName.content);
136      if fileName.content <> "" then
137        putf(dirPath & "/" & fileName.content, prog.content);
138        progName.name := fileName.content;
139      end if;
140    end if;
141  end func;
142
143
144const proc: main is func
145  local
146    const string: workDir is ".";
147    var browserConnection: browser is browserConnection.value;
148    var file: childStdin is STD_NULL;
149    var file: childStdout is STD_NULL;
150    var process: currentProgram is process.value;
151  begin
152    browser := openBrowser;
153    repeat
154      display(browser, mainWebPage);
155      # writeln("prog:          " <& striToUtf8(prog.content));
156      # writeln("load:          " <& load.activated);
157      # writeln("save:          " <& save.activated);
158      # writeln("clear:         " <& clear.activated);
159      # writeln("execute:       " <& execute.activated);
160      # writeln("cancel:        " <& cancel.activated);
161      # writeln("exit:          " <& exit.activated);
162      if load.activated then
163        loadFile(browser, workDir, ".sd7");
164      elsif save.activated then
165        if progName.name <> "" then
166          putf(workDir & "/" & progName.name, prog.content);
167        end if;
168      elsif saveAs.activated then
169        saveFileAs(browser, workDir);
170      elsif clear.activated then
171        progName.name := "";
172        prog.content := "";
173      elsif execute.activated then
174        if progName.name <> "" then
175          if pos(prog.content, "GRAPH" & "_KEYBOARD") = 0 then
176            display(browser, messagePage);
177          else
178            currentProgram := startProcess(commandPath("s7"), [] ("-q", workDir & "/" & progName.name));
179          end if;
180        end if;
181      elsif terminate.activated then
182        if currentProgram <> process.value then
183          block
184            kill(currentProgram);
185          exception
186            catch FILE_ERROR:
187              noop;
188          end block;
189          currentProgram := process.value;
190        end if;
191      end if;
192    until exit.activated;
193    display(browser, goodbyePage);
194    close(browser);
195  end func;
196