# PgAccess - Scripts and Forms

Beginning with 0.70 version, I have introduced in PgAccess two new modules for operating with scripts and forms.

This would give to PgAccess the power of creating application directly into PgAccess, defining new modules, procedures, forms and possibly making it a rapid development tool for PostgreSQL. The "scripts" and "forms" modules are using two new tables called pga_forms and pga_scripts. PgAccess take care of creating them if user is opening a new database and grant ALL permissions on them to PUBLIC.
Both scripts and forms are containing in fact sources of code written in Tcl/Tk and when the user has choose to "open" one of them, either by double-clicking in the main window or pressing the "Open" button PgAccess is searching for them in pga_forms or pga_scripts table, get the code and simply "eval" it !
Of course, when Designing a script, a simple text editor is opened and text is saved as is in pga_scripts table. When "designing" a form, a "form editor" that would be very similar with "Visual Tcl" would be invoked.

This mechanism and the extremely versatile scripting mode of Tcl/Tk would give PgAccess a great power for creating end user application using PosgreSQL. The most important thing is that the user could call procedures and functions that I have used for building up PgAccess !

### Forms

Forms are special Tcl/Tk source code that is used for creating windows and placing widgets inside it. When Tcl/Tk is "eval" them, a new window appears, with buttons as defined that could call "user defined scripts", "user defined procedures" or "internal PgAccess procedures".
For the moment, 0.70 version of PgAccess does not have a module for designing forms. It is intended to make an interface to the most powerful program of designing applications under Tcl/Tk , Visual Tcl , so it could handle forms designed to be used inside PgAccess.
Forms can hold all the widgets allowed in Tcl/Tk , buttons, check-boxes, radio-buttons, list-boxes, frames, canvases, etc. With these forms, you can control your application so PgAccess would become just a "shell", a startup point for you applications.

### Scripts

Scripts are normal Tcl/Tk code that is interpreted by Tcl/Tk. You can define your own procedures inside a script called "Library" for example. You can call your procedures from within another script, from another procedure.
The most important thing is that you have total access to the PgAccess's core of functions and procedures used by me in building PgAccess as an application. Just write `open_table "Your sample table"` and you'll see the result.
If you are writing a script called "Autoexec" then it will be executed every time the database is opened. You can put inside different commands that you want to be executed such as : running scripts that would define your own procedures such as `execute_script "My own procedure library"` or open a form with `open_form "Main window with menu buttons"` , and so on.

## Examples :

We would like to give you some examples for using forms and scripts. First of all, get your PgAccess 0.70 version NOW !

Define your first form. Remember, the form design module hasn't arrived yet :-( , so you will have to define your first form using an action query :
1. Click on Query tab and press "New" button
2. Enter "`Generate my first form`" in Query name field
3. Copy and paste from your browser window into query definition area the next text :
`insert into pga_forms values('My first form',' set base .pga_win_1; if {[winfo exists $base]} { wm deiconify $base; return }; toplevel $base -class Toplevel; wm focusmodel $base passive; wm geometry $base 395x389+325+188; wm maxsize $base 1009 738; wm minsize $base 1 1; wm overrideredirect $base 0; wm resizable $base 1 1; wm deiconify $base; wm title $base "User defined Form No.1"; button $base.b1 -command {execute_script "My first script"} -text "My first button" ; button $base.bexit -command {destroy [focus]} -padx 9 -pady 3 -text Exit ; place $base.bexit -x 340 -y 355 -anchor nw -bordermode ignore ; place $base.b1 -x 10 -y 10 -anchor nw;');`

4. Press "Save query definition button" and then "Close"
5. In the mai window, select by clicking the query "Generate my first form" and press "Open" button.

Your query must have been executed without errors! If you will check now the "Forms" tab, you will find there your first form. Press "Open" button and enjoy it! For the moment, if you will press "My first button" you will get an error message. Of course : we haven't yet defined our first script !

Defining our first script :
1. Click on Scripts tab and pres "New" button
2. Enter "My first script" in script's name field
3. Enter the body as the script the following statements :
`MsgBox "Warning" "PgAccess unleashed!"
open_table pga_scripts
` 4. Press "Save" button then "Cancel"

It's now the time to define our first library script. I am defining not because I need it. I could write directly in "My first script" the instructions for creating that warning window but I only wanted to show you how you can mix PgAccess script execution with Tcl/Tk code and so on.

Define our first library that will contain your "user defined" Tcl/Tk procedures and functions :
1. Click on Scripts tab and pres "New" button
2. Enter "My first library" in script's name field
3. Enter the body of the script the following statements :
`proc MsgBox {title msg} {
tk_messageBox -title $title -message $msg
}
` 4. Press "Save" button then "Cancel"

Define our first autoexec script that will contain commands that will be executed when opening database :
1. Click on Scripts tab and pres "New" button
2. Enter "Autoexec" in script's name field
3. Enter the body of the script the following statements :
`execute_script "My first library"
open_form "My first form"
` 4. Press "Save" button then "Cancel"

Everything is OK now! You will have to exit PgAccess and enter it again opening the same database ! Voila , your first form will pop-up on the screen, a message box is displayed and after clicking Ok button the table pga_scripts will be opened in table viewer revealing what's inside ! With this occasion I have shown how you could open in table view mode a "pga_..." system table that is hidden by PgAccess in main view mode!

I am stopping here, asking you to try this new features and sending me as more feed-backs as you can! What do you think about this new features ? How would you like to be developed PgAccess in future ? In this moment, I am working in recoding the main part of PgAccess in order to give to the user more "system" functions that would help him creating new applications very easy.
Remember : I'm waiting your messages at teo@flex.ro

You will also have the ability of hiding the main window of PgAccess at the beginning of "Autoexec" script execution and showing it before destroying "My first form". For this example, delete the previously defined "My first form" and create it with another action query with this code :
`insert into pga_forms values('My first form',' set base .pga_win_1; if {[winfo exists $base]} { wm deiconify $base; return }; toplevel $base -class Toplevel; wm focusmodel $base passive; wm geometry $base 395x389+325+188; wm maxsize $base 1009 738; wm minsize $base 1 1; wm overrideredirect $base 0; wm resizable $base 1 1; wm deiconify $base; wm title $base "User defined Form No.1"; button $base.b1 -command {execute_script "My first script"} -text "My first button" ; button $base.bexit -command {Window show .dw ; destroy [focus]} -padx 9 -pady 3 -text Exit ; place $base.bexit -x 340 -y 355 -anchor nw -bordermode ignore ; place $base.b1 -x 10 -y 10 -anchor nw;');
` This new one is just showing main window (.dw) before destroying the "user defined window" .
Also make "Autoexec" script to show like this :
`execute_script "My first library"
Window hide .dw
open_form "My first form"`