Post by bushrat on Jun 29, 2023 8:49:53 GMT -5
I'll restate the title of this post - it's for beginners, I'm one myself - I'll also state I am not an expert in anything.
This thread is intended to be a collection of posts originally posted under "Liberty BASIC v.4.5.1." and "LB v.4.5.1. Installation"
I thought I'd assemble the GUI posts there under a title more reachable for those like myself who are/were struggling to get a handle on LB GUI windows. Not that the LB documentation doesn't explain it but there are a few aspects of GUI window programming which for beginners need emphasising.
The GUI window is a very neat way of collecting information from the user about initial variable values, ranges of variable values etc. all of which were previously handled by input statements. You can't go wrong with input statements you can go wrong with GUI windows protocols unless you learn their fundamentals.
I'll start off soon with a simple example of an LB GUI window using 'statictext' statements only, but also using the commands 'trapclose' and 'wait'. The form and location of these in the coding were my biggest stumbling blocks to writing the code for my first attempts at LB GUI windows.
'trapclose' and 'wait' preamble.
--------------------------------
As a beginner I saw both 'trapclose' and 'close' commands used in LB GUI window coding. I wondered why both were necessary. It turns out when using LB you are indirectly interacting with both Microsoft Windows routines and Liberty Basic routines - with any language running under MS Windows you are always doing that, but LB has a very close relationship with MS Windows especially where LB GUI windows are concerned. I'm guessing that LB is accessing MS Windows own window generation routines to produce what you see as your 'LB window'. So there are two 'systems' you need to communicate with to write your 'LB window'. This is why both commands 'close' and 'trapclose' are necessary.
'Close' tells MS Windows the program wants to close a file (I'm presuming it's MS Windows which is really doing the file handling) whereas 'trapclose' tells LB the program wants to close the file. Both systems need to be informed of these program commands.
'Wait' - is a command with a very wide scope. It tells MS Windows/LB to halt, stop dead, and wait for an input device to signal something (an input event). That something could be a keystroke on the keyboard, a mouse click on a button, etc. which is immediately identified by the event handler and directs/redirects program control accordingly.
It is important to understand the consequences of issuing a 'wait' command are not necessarily tied back to the point in the program where the 'wait' command was issued. There could be many 'wait' commands in a GUI coding and depending on the specific input event, program execution could resume in many places.
So 'wait' is a kind of synchronous interrupt. It is puposely injected to achieve some specific redirection of program control at that point in the program. In case you're not aware, MS Windows and all operating systems are constantly listening for interrupts. Say the user issues a 'CTRL C' at some random point while a program is running, then (sometimes, but never on my machine) the running program will terminate. This would be an asynchronous interrupt. If operating systems didn't constantly listen for and respond to interrupts nothing would ever get done.
All that said, let's return to a simple LB GUI window.
A handy GUI.
============
I use 'Confirm' and 'Notice' commands a lot of the time, but they are not always appropriate. Once issued they won't let you get past them until you've given an answer at which point any screen output is lost?
The following GUI window lets you examine displayed results during a running program, print a hardcopy if desired, and decide whether you wish to continue the program with displayed results.
'program: Random shopping list.
'=====================
[reStartProgram]
for i=1 to 6
q(i)=int(rnd(1)*9)+1
next i
print "shopping list"
print "-------------"
print q(1); " party hats"
print q(2); " sausages"
print q(3); " hamburgers"
print q(4); " buns"
print q(5); " beers"
print q(6); " hand grenades"
print "(stay away from my parties...)"
'GUI 'Halt'
'==========
WindowWidth = 340
WindowHeight= 200
UpperLeftX=320
UpperLeftY=80
statictext #inspect, " Halt for shopping list inspection.", 45, 25, 300, 15
statictext #inspect, " ------------------------------------------", 45, 40, 300, 15
statictext #inspect, " Use 'File/Print' at this point for a hardcopy of shopping list", 30, 60, 400, 20
statictext #inspect, " then bring up this window to continue the program.", 30, 80, 400, 20
button #inspect,"Continue",[okInspect],UL,120,115,80,35
open "Halt" for dialog as #inspect '<-- think of the window #inspect as a file
#inspect "trapclose [exitInspect]"
wait '<-- run without the 'wait' and see what happens...
[okInspect]
close #inspect '<-- command to MS Windows - close #inspect
goto [reRunProgram]
[exitInspect]
close #inspect '<-- command to LB - close #inspect - END of GUI window.
[reRunProgram]
cls
confirm "Do you accept the shopping list?"; answer$
if answer$="no" then [reStartProgram] else goto [guestList]
[guestList]
print "Guest list"
print "----------"
print "Jane"
print "Suzy"
print "Ruby"
print " - they should be enough.. "
print
print "endRun"
end
I'm not going to discuss the window dimensions/positions or syntax of the 'statictext' commands they are clearly explained in tutorials and LB 'Help' - except to mention they must come before opening the dialog.
There is only one 'wait' command in the above so there is no confusion about where the program will halt and it's the mouse click on the button which reactivates the program after the 'wait' command.
That wraps up the first post in my GUI windows series. Feel free to comment. I know the above coding is rough but wanted to keep the GUI window as uncluttered as I could. Also I haven't yet figured how to properly insert code into posts, so to run the above you'll have to copy and paste as a new LB program file.
This thread is intended to be a collection of posts originally posted under "Liberty BASIC v.4.5.1." and "LB v.4.5.1. Installation"
I thought I'd assemble the GUI posts there under a title more reachable for those like myself who are/were struggling to get a handle on LB GUI windows. Not that the LB documentation doesn't explain it but there are a few aspects of GUI window programming which for beginners need emphasising.
The GUI window is a very neat way of collecting information from the user about initial variable values, ranges of variable values etc. all of which were previously handled by input statements. You can't go wrong with input statements you can go wrong with GUI windows protocols unless you learn their fundamentals.
I'll start off soon with a simple example of an LB GUI window using 'statictext' statements only, but also using the commands 'trapclose' and 'wait'. The form and location of these in the coding were my biggest stumbling blocks to writing the code for my first attempts at LB GUI windows.
'trapclose' and 'wait' preamble.
--------------------------------
As a beginner I saw both 'trapclose' and 'close' commands used in LB GUI window coding. I wondered why both were necessary. It turns out when using LB you are indirectly interacting with both Microsoft Windows routines and Liberty Basic routines - with any language running under MS Windows you are always doing that, but LB has a very close relationship with MS Windows especially where LB GUI windows are concerned. I'm guessing that LB is accessing MS Windows own window generation routines to produce what you see as your 'LB window'. So there are two 'systems' you need to communicate with to write your 'LB window'. This is why both commands 'close' and 'trapclose' are necessary.
'Close' tells MS Windows the program wants to close a file (I'm presuming it's MS Windows which is really doing the file handling) whereas 'trapclose' tells LB the program wants to close the file. Both systems need to be informed of these program commands.
'Wait' - is a command with a very wide scope. It tells MS Windows/LB to halt, stop dead, and wait for an input device to signal something (an input event). That something could be a keystroke on the keyboard, a mouse click on a button, etc. which is immediately identified by the event handler and directs/redirects program control accordingly.
It is important to understand the consequences of issuing a 'wait' command are not necessarily tied back to the point in the program where the 'wait' command was issued. There could be many 'wait' commands in a GUI coding and depending on the specific input event, program execution could resume in many places.
So 'wait' is a kind of synchronous interrupt. It is puposely injected to achieve some specific redirection of program control at that point in the program. In case you're not aware, MS Windows and all operating systems are constantly listening for interrupts. Say the user issues a 'CTRL C' at some random point while a program is running, then (sometimes, but never on my machine) the running program will terminate. This would be an asynchronous interrupt. If operating systems didn't constantly listen for and respond to interrupts nothing would ever get done.
All that said, let's return to a simple LB GUI window.
A handy GUI.
============
I use 'Confirm' and 'Notice' commands a lot of the time, but they are not always appropriate. Once issued they won't let you get past them until you've given an answer at which point any screen output is lost?
The following GUI window lets you examine displayed results during a running program, print a hardcopy if desired, and decide whether you wish to continue the program with displayed results.
'program: Random shopping list.
'=====================
[reStartProgram]
for i=1 to 6
q(i)=int(rnd(1)*9)+1
next i
print "shopping list"
print "-------------"
print q(1); " party hats"
print q(2); " sausages"
print q(3); " hamburgers"
print q(4); " buns"
print q(5); " beers"
print q(6); " hand grenades"
print "(stay away from my parties...)"
'GUI 'Halt'
'==========
WindowWidth = 340
WindowHeight= 200
UpperLeftX=320
UpperLeftY=80
statictext #inspect, " Halt for shopping list inspection.", 45, 25, 300, 15
statictext #inspect, " ------------------------------------------", 45, 40, 300, 15
statictext #inspect, " Use 'File/Print' at this point for a hardcopy of shopping list", 30, 60, 400, 20
statictext #inspect, " then bring up this window to continue the program.", 30, 80, 400, 20
button #inspect,"Continue",[okInspect],UL,120,115,80,35
open "Halt" for dialog as #inspect '<-- think of the window #inspect as a file
#inspect "trapclose [exitInspect]"
wait '<-- run without the 'wait' and see what happens...
[okInspect]
close #inspect '<-- command to MS Windows - close #inspect
goto [reRunProgram]
[exitInspect]
close #inspect '<-- command to LB - close #inspect - END of GUI window.
[reRunProgram]
cls
confirm "Do you accept the shopping list?"; answer$
if answer$="no" then [reStartProgram] else goto [guestList]
[guestList]
print "Guest list"
print "----------"
print "Jane"
print "Suzy"
print "Ruby"
print " - they should be enough.. "
print "endRun"
end
I'm not going to discuss the window dimensions/positions or syntax of the 'statictext' commands they are clearly explained in tutorials and LB 'Help' - except to mention they must come before opening the dialog.
There is only one 'wait' command in the above so there is no confusion about where the program will halt and it's the mouse click on the button which reactivates the program after the 'wait' command.
That wraps up the first post in my GUI windows series. Feel free to comment. I know the above coding is rough but wanted to keep the GUI window as uncluttered as I could. Also I haven't yet figured how to properly insert code into posts, so to run the above you'll have to copy and paste as a new LB program file.