Pure Code With Maxbox Introduction
Pure Code With Maxbox Introduction
Pure Code With Maxbox Introduction
In maXbox you will start the web server example as a script, so the web server IS the script
that starts the Indy objects, configuration from ini-file and a browser too; on board is also an addon or more: Options/Add_ons/Easy_Browser/.
Before this starter code will work you will need to download maXbox from a website. It can
be down-loaded from http://sourceforge.net/projects/maxbox (youll find the download to
maxbox3.zip on the top of the page). Once the download has finished, unzip the file, making sure
that you preserve the folder structure as it is. If you double-click maxbox3.exe the box opens a
default demo program. Test it with F9 / F2 or press Compile and you should hear a sound. So far
so good now well open the examples:
305_webserver_arduino3ibz_rgb_led.txt
If you cant find the two files try also the zip-file loaded from:
http://www.softwareschule.ch/examples/305_webserver_arduino3ibz_rgb_led.txt
Now lets take a look at the code of this project. Our first line is
01 program Motion_HTTPServer_Arduino41_RGB_LED;
This example requires two instant objects (you remember, all objects are converted and
precompiled) from the classes: TIdCustomHTTPServer and TComPort so the second one is to
establish a connection with the COM Ports to Arduino (see below).
TComPort by Dejan Crnila2 are Delphi/C++ Builder serial communications components. It is
generally easy to use for basic serial communications, alternative to the TurboPower ASYNCPro.
It includes 5 components: TComPort, TComDataPacket, TComComboBox, TComRadioGroup and
TComLed. With these tools you can build serial communication apps easier and faster than ever.
First we start with the web server and second we explain the COM port.
After creating the object in line 125 we use first methods to configure our server calling Port and
IP. The object makes a bind connection with the Active method by passing a web server
configuration.
So the object HTTPServer has some methods and properties like Active you can find in the
TIdCustomHTTPServer.pas unit or IdHTTPServer library. A library is a collection of code or
classes, which you can include in your program or in maXbox already done. By storing your
commonly used code in a library, you can reuse more code.
Lets get back to our HTTP Create in line 125. In line 131 and 132 you see a Port and IP
address configuration of a const in line 13, instead of IP you can also set a host name as
parameter.
126
127
128
129
bindings.Clear;
130
bindings.Add;
131
132
133
Active:= true;
134
onCommandGet:= @HTTPServerGet;
http://sourceforge.net/projects/comport/
2
135
136
Although you can find plenty to complain about in this code, its really not that bad. Its compact
and simple and easy to understand. However, within this code it is easy to see the power of
scripting because its agile and high available but you cant hide the functionality.
If a maXbox script or app is programmed with the default host standard, it is always started
relative to the path where the maXbox3.exe as the host itself is:
playMP3(ExePath+'examples\maxbox.mp3');
So for example you want to play a song or refer to other external resources in a script, your
external file will be found relative to ExePath():
E:\Program Files\maxbox\maxbox3\'examples\maxbox.mp3'
This problem might not be identified in the testing process, since the average user installs to the
default drive of the archive and directory and testing might not include the option of changing the
installation directory.
By the way you find all info concerning a script or app in menu /Program/Information/
Another solution to prevent hard coded literals is a constant or the call of a user dialog. An
indirect reference, such as a variable inside the program called FileName, could be expanded
by accessing a "select browse for file" dialog window, and the program code would not have to be
changed if the file moved.
procedure GetMediaData(self: TObject);
begin
if PromptForFileName(selectFile, 'Media files (*.mp3)|*.mp3|*.mpg)|*.mpg', '',
'Select your mX3 media file Directory',
'D:\kleiner2005\download', False)
You dont have to install maXbox or a script anyway, just unzip or copy the file
3
then begin
// Display this full file/path value
However it is advisable for programmers and developers not to fix the installation path of a
program or hard code some resources, since the default installation path is different in different
natural languages, and different computers may be configured differently. It is a common
assumption that all computers running Win have the primary hard disk labelled as drive C:\, but
this is not the case.
As you will see the configuration of maXbox is possible with a boot loader script and a simple inifile too. Extensions are possible with the Open Tools API and a small CLI (Command Line
Interface). Hope you can read tutorials 1 till 26 at (or menu /Help/Tutorials/:
http://sourceforge.net/apps/mediawiki/maxbox/
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
Tutorial
We go on with the boot loader and his functionality. maXbox and the script loader system has
default folders which organize files logically on the hard disk. You can also copy all 6 folders and
root files to a folder on your USB-stick that stores the same content in your maXbox installation
folder and it will start from the stick!
23.05.2013
23.05.2013
09.05.2013
09.05.2012
12.05.2013
17.03.2013
29.03.2013
11.12.2007
19:54
19:54
14:58
21:50
22:04
13:14
23:59
21:04
<DIR>
<DIR>
<DIR>
<DIR>
<DIR>
<DIR>
docs
examples
exercices
crypt
source
web
97'370 bds_delphi.dci
254'464 dbxint30.dll
11.11.2012
09.04.2013
28.11.2010
27.10.2005
07.11.2010
07.02.2013
21.10.2011
02.01.2009
11.05.2013
11.05.2013
12.05.2013
03.12.2012
12.05.2013
12.05.2013
21.04.2012
14.11.2005
10.12.2012
12.05.2013
11.10.2010
19:13
13:43
00:39
22:54
18:53
00:23
18:13
02:05
23:49
21:38
23:06
00:33
15:45
00:46
09:48
12:00
09:37
00:47
22:49
580'096
5'426
3'866
103'424
138
10'544
59'060
71'807
11'887'616
5'133'220
994
12'503
42'773
2'309'571
9'533
383'488
17'202
36'854
135'168
dmath.dll
firstdemo3.txt
firstdemo3.uc
income.dll
maildef.ini
maxbootscript_.txt
maxbox.mp3
maxbox.png
maxbox3.exe
maxbox3clx
maxboxdef.ini
maxboxerrorlog.txt
maxboxnews.htm
maxbox_functions_all.pdf
maxdefine.inc
midas.dll
pas_includebox.inc
readmefirst_maxbox3.txt
TIFFRead.dll
When you start the box a boot script is loaded. Within the boot script, you can perform common
source control tasks, such as file check in, check out, and of course change IDE settings and
synchronization of your current version. This is a script where you can put all the global settings
and styles of the IDE of maXbox, for example:
with maxForm1 do begin
caption:= caption +'Boot Loader Script maxbootscript.txt';
color:= clteal;
IntfNavigator1Click(self);
tbtnCompile.caption:= 'Compile!';
tbtnUsecase.caption:= 'UML UC';
maxform1.ShellStyle1Click(self);
memo2.font.size:= 16;
Info1Click(self);
end;
Writeln('BOOTSCRIPT ' +BOOTSCRIPT+ ' loaded')
When you want to see a complete copy of that file, look at:
07.02.2013
00:23
10'544 maxbootscript_.txt
When you delete the underscore in the filename to maxbootscript.txt the system performs
next time when you load maXbox and presents you with a different view. This boot script results
in the picture below for example. The trick of renaming the file has a simple explanation. The inifile default to load the boot script is YES so it can be easier to rename the file instead of change
the ini-file to set to YES, cause of missing permissions, testing or so: BOOTSCRIPT=Y
Maybe you want to change the colour or the caption of a button or a frame; you can do this by
accessing the Open Tools API of the object maxForm1.
In this section we deal with multiple instances of maXbox and his creation. You can create
multiple instances of the same app to execute multitask code, just type <F4>. For example, you
can launch a new instance within a script of the box in response to some user action, allowing
each script to perform the expected response.
ExecuteShell(ExePath+'maxbox3.exe','"'+ExePath+'examples\'+script+'"');
S_ShellExecute(ExePath+'maxbox3.exe',ExePath+'examples\'+script,secmdopen);
Theres no good way to launch one application (maXbox) with multiple scripts in it.
Maybe with OLE Automation in that sense you open office programs (word, excel) or other
external shell objects. CreateOleObject creates a single uninitialized object of the class
specified by the ClassName parameter. ClassName specifies the string representation of the
Class ID (CLSID). CreateOleObject is used to create an object of a specified type when the
CLSID is known and when the object is on a local or in-proc server. Only the objects that are not
part of an aggregate are created using CreateOleObject.
Try the example of OLE Objects 318_excel_export3.TXT and the tutorial 19.
An external script or a least a second one could also be a test case to compare the behaviour.
Each test case and test project is reusable and rerun able, and can be automated through the
use of shell scripts or console commands.
Ok we have been finished the boot script what about a template file. The code template file
bds_delphi.dci stands for code completion templates.
In the Code Editor, type an object, class, structure or a pattern name followed by <Ctrl J> to
display the object. But its not a full code completion where you get after the dot (.) a list of types,
properties, methods, and events, if you are using the Delphi or C# languages.
Its more a template copy of your building blocks. In the menu /Debug/Code Completion List
you get the defaults, here is an extract:
[cases | case statement | Borland.EditOptions.Pascal]
case | of
: ;
: ;
end;
[trye | try except | Borland.EditOptions.Pascal]
try |
except
end;
My favour is: myform<Ctrl J> which represents or copies a form builder in your editor.
Useless to say you can add your own templates to the file. Many of the Code Editor features
are also available when editing HTML and CSS files. Code Completion (CTRL+J) and syntax
highlighting are available for HTML, XML and CSS files.
Most of the Open Tools API declarations reside also in that file bds_delphi.dci.(at the
bottom) Note: Call the methods with maxForm1., e.g.: maxForm1.ShellStyle1Click(self);
A standard approach to break a running loop in a script or configuration is the well known
KeyPress or IsKeyPressed function you can use and check:
procedure LoopTest;
begin
Randomize;
REPEAT
Writeln(intToStr(Random(256*256)));
UNTIL isKeyPressed; //on memo2 output
if isKeypressed then writeln(Key has been pressed!');
end;
As you know the memo2 is the output window as the shell, so the keypress is related to memo2;
by the way memo1 is the editor itself!
Another function KeyPressed(VK: Integer): Boolean; returns True, if key VK has been
pressed.
Lets jump to the Ini-file. Many applications use ini files to store configuration information. Using
ini files has the advantage that they can be used in cross-platform applications and they are easy
to read and edit.
The ini file format is still popular; many configuration files (such as Desktop or Persistence
settings file) are in this format. This format is especially useful in cross-platform applications,
where you can't always count on a system Registry for storing configuration information.
I never was a friend of the Registry so you can also start maXbox from a stick.
In maXbox code, TIniFile is the game of advantage. When you instantiate the TIniFile or
TMemIniFile object, you pass the name of the ini file as a parameter to the constructor. If the file
does not exist, it is automatically created. You are then free to read values using the various read
methods, such as ReadString, ReadDate, ReadInteger, or ReadBool.
This is how we can read the ini file of maXbox: maxboxdef.ini
procedure getMaxBoxIniShort;
begin
with TIniFile.Create(ExePath+'maxboxdef.ini') do
try
except_conf:= ReadString('Form','EXCEPTIONLOG','');
execute_conf:= ReadString('Form','EXECUTESHELL','');
boot_conf:= ReadString('Form','BOOTSCRIPT','');
ip_port:= ReadInteger('Web','IPPORT',0);
finally
writeln('inifile sysdata1: '+except_conf+':'+execute_conf);
writeln('inifile sysdata2: '+boot_conf+':'+intToStr(ip_port));
Free;
end;
end;
This process is handled directly, through an object so each time it changes timestamp of the file
also and not on demand.
In other words TIniFile works directly with the ini file on disk while TMemIniFile buffers all
changes in memory and does not write them to disk until you call the UpdateFile method.
Alternatively, if you want to read an entire section of the ini file, you can use the ReadSection
method. Similarly, you can write values using methods such as WriteBool, WriteInteger,
WriteDate, or WriteString.
Each of the Read routines takes three parameters. The first parameter (Form in our example)
identifies the section of the ini file. The second parameter identifies the value you want to read,
and the third is a default value in case the section or value doesn't exist in the ini file.
In maXbox you can also start with read only mode (Options/Save before Compile), so nothing
will be write on the disk.
//*** Definitions for maXbox mX3 ***
[FORM]
LAST_FILE=E:\maxbox\maxbox3\examples\140_drive_typedemo.txt //10 files
FONTSIZE=14
EXTENSION=txt
SCREENX=1386
SCREENY=1077
MEMHEIGHT=350
PRINTFONT=Courier New
LINENUMBERS=Y
EXCEPTIONLOG=Y
//save log files menu Debug/Show Last Exceptions
EXECUTESHELL=Y
//prevents execution of ExecuteShell()/ExecuteCommand()
BOOTSCRIPT=Y
//enabling load a boot script
MACRO=Y
//put macros in your source header file
NAVIGATOR=Y
//set the nav listbox at the right side of editor
MEMORYREPORT=Y
//shows memory report on closing maXbox
[WEB]
IPPORT=8080
//internal webserver ../Options/Add Ons/WebServer2
IPHOST=192.168.1.53
ROOTCERT=filepathY
//for use of HTTPS and certificates
SCERT=filepathY
RSAKEY=filepathY
VERSIONCHECK=Y
//checks over web the version
Now lets take a look at the code of the memory report in the project file:
Application.CreateForm(TMaxForm1, MaxForm1);
if maxform1.STATMemoryReport = true then
ReportMemoryLeaksOnShutdown:= true;
We name it, means the ini-file sets the STATMemoryReport true or false.
This example requires two objects from the classes: TMaxForm1 and TMemoryManager of
mX4 so the second one is from the well known VCL Lib.
This re includes a new memory manager that significantly improves start-up time, runtime speed,
and hyper threading performance.
If the ini-file doesnt exist, renamed or damaged, maXbox produces a new one with the default
values. Test it by copy the maXbox3.exe in an empty directory; just says that a template file is
missing but it starts and will run!
If you only want to install a new maXbox with file or directory names, be sure the ini-file will not
be overwritten by unpacking the zip (so lets make a copy before).
Maybe you just know that by starting the maXbox it checks on the internet the last version if the
ini-file allows this VERSIONCHECK=Y.
Lets do the last step with an include file. Under Include Files, list the files you want to include in a
script. A script can include a script from a file or from another unit. To include script from a file,
use the following code statement:
305_indy_elizahttpserver.TXT
{$I ..\maxbox3\examples\305_eliza_engine.INC}
If you need to specify additional compiler options, you can invoke the compiler from the command
line with the Command Line Interface (CLI).
As you know, theres a simple test to run the CLI out of the box with a ShellExecute() or a
similar RunFile() Command.
ShellExecute3(ExePath+'maxbox3.exe',ExePath+'examples\'+ascript,secmdopen);
ShellExecute3(ExePath+'maxbox3.exe',
ExePath+'examples\003_pas_motion.txt',secmdopen);
A simple CLI is more relevant today than ever for scripting, and modern shell implementations
such as maXbox or PowerShell have a lot to bring to the table.
10
At pre last is to say you can use DLLs too. Selecting this type of application sets up your project
as a DLL dependency, with the exported methods expected by the Library, e.g.:
43: procedure SetErrCode(ErrCode: Integer);
external 'SetErrCode@dmath.dll'; { Sets error code }
function
10:
procedure TForm1_FormCreateShowRunDialog;
var ShellApplication: Variant;
begin
ShellApplication:= CreateOleObject('Shell.Application');
ShellApplication.FileRun;
end;
11
: Macro Demo
: 21/09/2010
14:56
- #date:01.06.2013 16:38:20
* #path E:\maxbox\maxbox3\examples\
* #file 369_macro_demo.txt
* #perf-50:0:4.484
* History
******************************************************************}
All macros are marked with red. One of my favour is #locs means lines of code and you get
always the certainty if something has changed by the numbers of line.
So the editor has a programmatic macro system which allows the pre compiler to be extended by
user code I would say user tags.
Below an internal extract from the help file All Functions List maxbox_functions_all.pdf:
//---------------------------------------------------------------------------10181: //**************mX4 Macro Tags ****************************************
10182: //---------------------------------------------------------------------10183:
10184: #name, #date, #host, #path, #file, #head, #sign, #teach
10185:
10186: SearchAndCopy(memo1.lines, '#name', getUserNameWin, 11);
10187: SearchAndCopy(memo1.lines, '#date', datetimetoStr(now), 11);
10188: SearchAndCopy(memo1.lines, '#host', getComputernameWin, 11);
10189: SearchAndCopy(memo1.lines, '#path', fpath, 11);
10190: SearchAndCopy(memo1.lines, '#file', fname, 11);
10191: SearchAndCopy(memo1.lines, '#locs', intToStr(getCodeEnd), 11);
10192: SearchAndCopy(memo1.lines, '#perf', perftime, 11);
10193: SearchAndCopy(memo1.lines, '#head',Format('%s: %s: %s %s ',
10194: [getUserNameWin, getComputernameWin, datetimetoStr(now), Act_Filename]),11);
10195: SearchAndCopy(memo1.lines, '#sign',Format('%s: %s: %s ',
[getUserNameWin, getComputernameWin, datetimetoStr(now)]), 11);
10196: SearchAndCopy(memo1.lines, '#tech',Format('perf: %s threads: %d %s %s',
[perftime, numprocessthreads, getIPAddress(getComputerNameWin),
timetoStr(time)]), 11);
12
Some macros produce simple combinations of one liner tags but at least they replace the
content by reference in contrary to templates which just copy a content by value.
You simply put the line above on the boot script and make sure the ini file has it set to Yes.
BOOTSCRIPT=Y
//enabling load a boot script
In combination with the Open Tools API you can tweak the GUI with new or change buttons,
events and behaviour for example:
if extractFileName(maxform1.appname) = '370_synedit.txt' then begin
Options:= +[eoShowSpecialChars];
ActiveLineColor:= clyellow;
maxform1.tbtnUseCase.caption:= 'SynScriptUC';
maxform1.ShellStyle1Click(self)
end else
ActiveLineColor:= clgreen;
13
More examples at 370_synedit.txt and all the other changeable properties or methods you
find at the bottom of the help file <All Functions List> maxbox_functions_all.pdf
10197: //---------------------------------------------------------------------------10198: //**************mX4 Public Tools API *****************************************
10199: //----------------------------------------------------------------------------
//-----------------------------------------------------------------------------10702:
10703:
10704:
10705:
10706:
You can also enhance the API with functions like the example above GetEnvironmentString:
function getEnvironmentString2: string;
var
list: TStringList;
i: Integer;
begin
list:= TStringList.Create;
try
GetEnvironmentVars(list, False);
for i:= 0 to list.Count-1 do
result:= result + list[i]+#13#10;
finally
list.Free;
end;
end;
The Open Tools API is a collection of classes and functions of SynEdit and VCL components for
extending and enhancing the design and your editor environment.
Unlike other development tools, you use maXbox (Delphi) to extend maXbox. You don't need to
learn a new scripting language because PascalScript works for you.
The Open Tools API puts you in control; reshape maXbox to match your needs.
I mean an elegant and efficient application has some script features.
14
Feedback: max@kleiner.com
Links of maXbox and DelphiWebStart:
http://www.softwareschule.ch/maxbox.htm
http://sourceforge.net/projects/maxbox
http://sourceforge.net/apps/mediawiki/maxbox/
http://sourceforge.net/projects/delphiwebstart
http://www.softwareschule.ch/maxbox_mainscreen.png
15
1.6 Advertising
Pure Code
for Object Scripting
http://sourceforge.net/projects/maxbox
16