|
|
Systems Ecology |
|
Examples (DM Snippets)
Documentation
Reference
Downloads
Contact
MacMETH DM AuxLib SciLib EasyMW MW ISIS RMS Shell RMS Extras RASS |
||
|
Dialog Machine Snippets
Introduction
Files
File I/O
Windows
Entry Forms (elementary data types)
Dynamic Entry Forms
1 - IntroductionThe following code snippets demonstrate the typical usage of the Dialog Machine. Under a given header they provide copy and paste snippets for the necessary imports, constant or variable declarations plus the typical statements required to implement the desired function.
Dialog Machine Version 3.0
2 - Files
(*----------------------------------------------------*)
(*===== Open an existing file without dialog =====*)
(*----------------------------------------------------*)
FROM DMLanguage IMPORT fileResBase;
FROM DMMessages IMPORT DoInform;
FROM DMFiles IMPORT TextFile, neverOpenedFile,
Lookup, Response, Close;
VAR
myFile: TextFile;
myFile.filename := "The File";
Lookup(myFile,myFile.filename,FALSE(*new*));
IF (myFile.res=done) THEN
(*. *** read from it as you want *** .*)
ELSE (* an error occurred *)
DoInform(fileResBase+ORD(myFile.res),
"MyModule","MyProcedure",myFile.filename);
END(*IF*);
myFile := neverOpenedFile;
(*----------------------------------------------------------------------*)
(*===== Open a new (or overwrite existing) file without dialog =====*)
(*----------------------------------------------------------------------*)
FROM DMLanguage IMPORT fileResBase;
FROM DMMessages IMPORT DoInform;
FROM DMFiles IMPORT TextFile, neverOpenedFile,
Lookup, Response, Close;
VAR
myFile: TextFile;
myFile.filename := "The File";
Lookup(myFile,myFile.filename,TRUE(*new*));
IF (myFile.res=done) THEN
(*. *** write on it as you want *** .*)
ELSE (* an error occurred *)
DoInform(fileResBase+ORD(myFile.res),
"MyModule","MyProcedure",myFile.filename);
END(*IF*);
myFile := neverOpenedFile;
(*---------------------------------------------------*)
(*===== Open a file with dialog for reading =====*)
(*---------------------------------------------------*)
FROM DMLanguage IMPORT allOk, fileResBase;
FROM DMMessages IMPORT DoInform;
FROM DMFiles IMPORT TextFile, neverOpenedFile,
GetExistingFile, Response, Close;
VAR
myFile: TextFile; resCode: INTEGER;
GetExistingFile(myFile,"Please select file 'The File'");
IF (myFile.res=done) THEN
(*. *** read from it as you want *** .*)
ELSIF (myFile.res<>cancelled) THEN (* an error occurred *)
resCode := fileResBase+ORD(myFile.res);
DoInform(resCode,"MyModule","MyProcedure",myFile.filename);
ELSE
(*. restore or do nothing, since cancelled .*)
END(*IF*);
myFile := neverOpenedFile;
(*---------------------------------------------------*)
(*===== Open a file with dialog for writing =====*)
(*---------------------------------------------------*)
FROM DMLanguage IMPORT allOk, fileResBase;
FROM DMMessages IMPORT DoInform;
FROM DMStrings IMPORT AssignString;
FROM DMFiles IMPORT TextFile, neverOpenedFile,
CreateNewFile, Response, Close;
VAR
myFile: TextFile; resCode: INTEGER;
CreateNewFile(myFile,"Save as",myFile.filename);
IF (myFile.res=done) THEN
(*. *** write to it as you want *** .*)
ELSIF (myFile.res<>cancelled) THEN (* an error occurred *)
resCode := fileResBase+ORD(myFile.res);
DoInform(resCode,"MyModule","MyProcedure",myFile.filename);
ELSE
(*. restore or do nothing, since cancelled .*)
END(*IF*);
myFile := neverOpenedFile;
AssignString("DefaultName",myFile.filename);
3 - File I/O
(*-----------------------------------*)
(*===== Reading from a file =====*)
(*-----------------------------------*)
FROM DMFiles IMPORT TextFile, neverOpenedFile,
Lookup, GetExistingFile, Response, Close,
IsOpen, EOF, EOL, ReadChar;
(* the following snippet assumes the file is already open *)
PROCEDURE ReadLn (VAR f: TextFile; VAR line: ARRAY OF CHAR);
VAR i,n: INTEGER; ch: CHAR;
BEGIN (* ReadLn *)
line[0] := 0C;
IF NOT IsOpen(f) OR EOF(f) THEN RETURN END;
n := HIGH(line); i := 0; ReadChar(f,ch);
WHILE (i<=n) AND (ch<>EOL) AND NOT EOF(f) DO
line[i] := ch;
ReadChar(f,ch); INC(i);
END(*WHILE*);
IF i<=n THEN line[i] := 0C END;
END ReadLn;
4 - Windows
(*-------------------------------*)
(*===== Create a window =====*)
(*-------------------------------*)
FROM DMWindows IMPORT Window, WindowsDone, notExistingWindow,
WindowKind, ScrollBars, CloseAttr, ZoomAttr, WFFixPoint,
WindowFrame, CreateWindow, AutoRestoreProc;
FROM DMWindIO IMPORT BackgroundWidth, BackgroundHeight;
VAR
myWindow: Window;
PROCEDURE MakeMyWindow;
VAR wf: WindowFrame;
BEGIN
wf.x := margin;
wf.y := margin;
wf.w := BackgroundWidth()-2*margin;
wf.h := BackgroundHeight()-2*margin;
CreateWindow(myWindow,
GrowOrShrinkOrDrag, WithoutScrollBars,
WithCloseBox, WithZoomBox, bottomLeft,
wf, "MyWindow", AutoRestoreProc);
IF WindowsDone THEN
(*. AddWindowHandler(.., SetWindowFont(... or any other
action depending on the existence of the window myWindow .*)
END(*IF*);
END MakeMyWindow;
myWindow := notExistingWindow;
MakeMyWindow;
5 - Entry Forms (elementary data types)
(*---------------------------------------*)
(*===== Entry forms - INTEGER =====*)
(*---------------------------------------*)
FROM DMEntryForms IMPORT FormFrame, WriteLabel, DefltUse, UseEntryForm,
IntField, LongRealField, CheckBox, StringField, CharField;
VAR
theInt: INTEGER;
PROCEDURE DoEntryForm;
CONST lem = 5; tab = 30;
VAR ef: FormFrame; ok: BOOLEAN; cl: INTEGER;
BEGIN (*DoEntryForm*)
cl := 2;
WriteLabel(cl,lem,"Title"); INC(cl);
WriteLabel(cl,lem,"DescriptionOfInteger");
IntField(cl,tab,7,theInt,useAsDeflt,MIN(INTEGER),MAX(INTEGER)); INC(cl);
ef.x:= 0; ef.y:= -1 (*display entry form in middle of screen*);
ef.lines:= cl+1; ef.columns:= 55;
UseEntryForm(ef,ok);
END DoEntryForm;
(*------------------------------------*)
(*===== Entry forms - REAL =====*)
(*------------------------------------*)
VAR
theReal: LONGREAL;
(*. insert in DoEntryForm (see above) following editable field .*)
WriteLabel(cl,lem,"DescriptionOfReal");
LongRealField(cl,tab,7,theReal,useAsDeflt,minTheReal,maxTheReal); INC(cl);
(*--------------------------------------------------*)
(*===== Entry forms - CheckBox (BOOLEAN) =====*)
(*--------------------------------------------------*)
VAR
theOptionFlag: BOOLEAN;
(*. insert in DoEntryForm (see above) following editable field .*)
CheckBox(cl,lem,"DescriptionOfOption",theOptionFlag); INC(cl);
(*--------------------------------------*)
(*===== Entry forms - String =====*)
(*--------------------------------------*)
VAR
theString: ARRAY [0..hi] OF CHAR;
CONST
fieldwidth = 20;
(*. insert in DoEntryForm (see above) following editable field .*)
StringField(cl,tab,fieldwidth,theString,useAsDeflt); INC(cl);
(*------------------------------------*)
(*===== Entry forms - CHAR =====*)
(*------------------------------------*)
VAR
theChar: CHAR;
(*. add in DoEntryForm (see above) following constant(s) .*)
anyCharLegal = 177C; (* any char is legal, even none *)
just1CharLegal = ""; (* any char is legal, but one must be entered *)
(*. insert in DoEntryForm (see above) following editable field .*)
WriteLabel(cl,lem,"Character");
CharField(cl,tab,theChar,useAsDeflt,legalCharset); INC(cl);
(*---------------------------------------------*)
(*===== Entry forms - Radio Buttons =====*)
(*---------------------------------------------*)
FROM DMEntryForms IMPORT RadioButtonID, DefineRadioButtonSet,
RadioButton, FormFrame, WriteLabel, DefltUse, UseEntryForm;
TYPE
ColorChoices = (brownish, greenish); (* extend at your heart's content *)
VAR
curCol: ColorChoices;
TYPE
Choices = ColorChoices; (*. equivalent local type with yours .*)
PROCEDURE DoEntryForm(VAR curChoice(*In/Out*): Choices);
CONST lem = 5; tab = 30;
VAR ef: FormFrame; ok: BOOLEAN; cl: INTEGER;
makeChoiceRadButSet: RadioButtonID;
radBut: ARRAY [MIN(Choices)..MAX(Choices)] OF RadioButtonID;
radButTxt: ARRAY [MIN(Choices)..MAX(Choices)] OF ARRAY [0..31] OF CHAR;
PROCEDURE MakeRadButtonSet(curChoice: Choices);
VAR ci: Choices;
BEGIN (* MakeRadButtonSet *)
DefineRadioButtonSet(makeChoiceRadButSet);
FOR ci := MIN(Choices) TO MAX(Choices) DO
RadioButton( radBut[ci], cl, lem, radButTxt[ci]); INC(cl);
END(*FOR*);
makeChoiceRadButSet := radBut[curChoice];
END MakeRadButtonSet;
PROCEDURE WhichChoice(chosenRBut: RadioButtonID;
rb: ARRAY OF RadioButtonID): Choices;
VAR i: Choices;
BEGIN (* WhichChoice *)
i := MIN(Choices);
WHILE (i
6 - Dynamic Entry Forms
FROM DMLanguage IMPORT
okButtonText, cancelButtonText;
FROM DMWindows IMPORT
Window, WindowFrame,
ModalWindowKind, ScrollBars,
CreateModalWindow, UseWindowModally,
AutoRestoreProc, RemoveWindow;
FROM DMWindIO IMPORT
SelectForOutput,
SetWindowFont, WindowFont, FontStyle, CellWidth,
BackgroundWidth, BackgroundHeight,
SetPen, WriteString;
FROM DMEditFields IMPORT
EditItem,
MakeIntField, IsInteger,
MakePushButton, UseAsDefaultButton,
SelectField;
VAR
theInt1: INTEGER;
theInt2: INTEGER;
TYPE
MyModalDialog = RECORD
myEntryForm: Window;
wf: WindowFrame;
acceptIt, cancelled, entryFormOk: BOOLEAN;
okBut, cancelBut: EditItem;
(*. from here on insert your dialog specific objects .*)
middle, yTxtLn: INTEGER;
oldInt1: INTEGER; intF1: EditItem;
oldInt2: INTEGER; intF2: EditItem;
myBut: EditItem;
(*. define more of your own objects ad libitum .*)
(*. ... .*)
END (* RECORD *);
VAR
mdlg: MyModalDialog;
PROCEDURE DoOk;
BEGIN
(* get possibly edited, new values *)
mdlg.acceptIt := IsInteger(mdlg.intF1, theInt1) AND
IsInteger(mdlg.intF2, theInt2);
(*. possibly make further tests, e.g. conistency tests,
before accepting the values definitely .*)
END DoOk;
PROCEDURE DoCancel;
BEGIN
mdlg.cancelled:=TRUE;
(* restore old values *)
theInt1:= mdlg.oldInt1; theInt2:= mdlg.oldInt2;
END DoCancel;
PROCEDURE DoMyButton;
BEGIN
(*. your statements .*)
(*. e.g. .*)
SelectForOutput(mdlg.myEntryForm);
SetPen(mdlg.middle, mdlg.yTxtLn);
WriteString("Hello");
END DoMyButton;
PROCEDURE DoModalDialog;
CONST leftMarg = 14; lineH = 22; butWidth = 75; intFW = 6;
VAR x,y: INTEGER;
BEGIN
(* save old values *)
mdlg.oldInt1:=theInt1;
mdlg.oldInt2:=theInt2;
(* determine size and position of modal dialog (entry form) and open window *)
mdlg.wf.w:=360; mdlg.wf.h:=150;
mdlg.wf.x := (BackgroundWidth()-mdlg.wf.w) DIV 2;
mdlg.wf.y := (BackgroundHeight()-mdlg.wf.h) DIV 2;
CreateModalWindow(mdlg.myEntryForm, SingleFrameShadowed,
WithoutScrollBars, mdlg.wf, AutoRestoreProc);
SetWindowFont(Chicago, 12, FontStyle{});
mdlg.middle := (mdlg.wf.w-6*CellWidth()) DIV 2;
(* display editing fields for each variable you wish to edit *)
x := leftMarg; y := mdlg.wf.h-lineH;
SetPen(x,y); WriteString("Integer 1 (theInt1)");
x := mdlg.middle;
MakeIntField(mdlg.myEntryForm, mdlg.intF1, x, y, intFW, theInt1, 0, 10);
x := leftMarg; y := y - lineH;
SetPen(x,y); WriteString("Integer 2 (theInt2)");
x := mdlg.middle;
MakeIntField(mdlg.myEntryForm, mdlg.intF2, x, y, intFW, theInt2, MIN(INTEGER), MAX(INTEGER));
x := leftMarg; y := y - lineH-11; mdlg.yTxtLn := y;
MakePushButton(mdlg.myEntryForm, mdlg.myBut, x, y, butWidth DIV CellWidth(),
"Say it", DoMyButton);
(*. define your own fields ad libitum .*)
(*. ... .*)
(* make standard Ok and Cancel buttons *)
MakePushButton(mdlg.myEntryForm, mdlg.okBut, mdlg.wf.w-butWidth-leftMarg, lineH,
butWidth DIV CellWidth(), okButtonText, DoOk);
UseAsDefaultButton(mdlg.okBut);
MakePushButton(mdlg.myEntryForm, mdlg.cancelBut, leftMarg, lineH,
butWidth DIV CellWidth(), cancelButtonText, DoCancel);
(* place cursor into e.g. the first editable field or wherever you wish *)
SelectField(mdlg.intF1);
(* fire up the modal dialog *)
UseWindowModally(mdlg.myEntryForm, mdlg.acceptIt, mdlg.cancelled);
(* dialog has finished, clean up *)
RemoveWindow(mdlg.myEntryForm);
mdlg.entryFormOk := NOT mdlg.cancelled;
IF mdlg.entryFormOk AND mdlg.acceptIt THEN
(*. do postprocessing if necessary .*)
END(*IF*);
END DoModalDialog;
7 - Keyboard Usage and Portable Programing
(*------------------------------------------------------*)
(*===== Programing special characters portably =====*)
(*------------------------------------------------------*)
FROM DMKeyChars IMPORT ProgrammedOn, ComputerPlatform, BestCH;
VAR bullet: CHAR;
ProgrammedOn(Mac);
bullet := BestCH(245C); (* = CHR(165) = "•" (on Mac only) *)
(* NOTE: don't program bullet := BestCH("•"), since some transfer
techniques such as WINFTP (Windows ftp) alter the source code
when transfering it from one machine to the other *)
(*-----------------------------*)
(*===== Key shortcuts =====*)
(*-----------------------------*)
FROM DMMaster IMPORT AddKeyboardHandler, InspectKey, KeyAccepted;
FROM DMKeyChars IMPORT command, capslock, shift;
PROCEDURE MyKeyboardHandler;
VAR ch: CHAR; m: BITSET;
BEGIN (* MyKeyboardHandler *)
InspectKey(ch, m); ch := CAP(ch); (* case insensitive *)
IF (m={command,shift}) AND (ch="H") THEN
KeyAccepted;
(*. *** do what you want *** .*)
END(*IF*);
END MyKeyboardHandler;
PROCEDURE InstallMyKeyboardHandler;
CONST lowPriority = (MAX(INTEGER) DIV 4) * 3;
BEGIN (* InstallMyKeyboardHandler *)
AddKeyboardHandler(MyKeyboardHandler,lowPriority);
END InstallMyKeyboardHandler;
(*-----------------------------------*)
(*===== Portable programing =====*)
(*-----------------------------------*)
FROM DMStrings IMPORT FindInString;
FROM DMSystem IMPORT GetComputerName, ComputerSystem, SUN, IBMPC,
IBMRisc6000;
PROCEDURE RunsOnAMac(): BOOLEAN;
VAR cn: ARRAY [0..3] OF CHAR; macInName: BOOLEAN; fst,lst, cs: INTEGER;
BEGIN (* RunsOnAMac *)
GetComputerName(cn);
macInName:= (CAP(cn[0])="M") AND (CAP(cn[1])="A") AND (CAP(cn[2])="C");
fst := 0; lst := 0;
macInName := macInName OR FindInString(cn,"Mac",fst,lst);
cs := ComputerSystem();
RETURN macInName OR (cs |
||
|
|
||
| RAMSES@env.ethz.ch | Last modified 1/30/23 | [Top of page] |