WINDOW DEVICE COMMANDS --------------------------------------------------------------------------------------- In Liberty BASIC windows are treated like files, and we can refer to anything in this class as a BASIC 'Device'. To open a window we use the OPEN statement, and to close the window we use the CLOSE statement. To control the window we 'print' to it, just as we would print to a file. The commands are sent as strings to the device. As a simple example, here we will open a graphics window, center a pen (like a Logo turtle), and draw a simple spiral. We will then pause by opening a simple dialog. When you confirm the exit, we will close the window: button #graph, Exit, [exit], LR, 5, 5 'window will have a button open "Example" for graphics as #graph 'open graphics window print #graph, "up" 'make sure pen is up print #graph, "home" 'center the pen print #graph, "down" 'make sure pen is down for index = 1 to 30 'draw 30 spiral segments print #graph, "go "; index 'go foreward 'index' places print #graph, "turn 118" 'turn 118 degrees next index 'loop back 30 times print #graph, "flush" 'make the image 'stick' [inputLoop] input b$ : goto [inputLoop] 'wait for button press [exit] confirm "Close Window?"; answer$ 'dialog to confirm exit if answer$ = "no" then [inputLoop] 'if answer$ = "no" loop back close #graph end WINDOW TYPES: --------------------------------------------------------------------------------------- Liberty BASIC provides ten different kinds of window types, to which you can add as many buttons and pull-down menus as needed. Here is a list of the different kinds: graphics - open a graphic window graphics_fs - open a graphic window full screen (size of the screen) graphics_nsb - open a graphic window w/no scroll bars graphics_fs_nsb - open a graphic window full screen, w/no scroll bars text - open a text window text_fs - open a text window full screen text_nsb - open a text window w/no scroll bars text_nsb_ins - open a text window w/no scroll bars, with inset editor so that buttons can be placed around it text_fs_nsb - open a text window full screen, w/no scroll bars spreadsheet - open a spreadsheet window The way that you would specify what kind of window to open would be as follows: open "Window Title" for type as #handle where type would be one of the above ten descriptors CONTROLLING SIZE AND PLACEMENT OF WINDOWS -------------------------------------------------------------------------------------- The size and placement of any window can be easily determined before it is opened in Liberty BASIC (except for any window type with a _fs in its descriptor). If you do choose not to specify the size and placement of the windows that your programs open, Liberty BASIC will pick default sizes. However, for effect it is often best that you exercise control over this matter. There are four special variables that you can set to select the size and placement of your windows, whether they be text, graphics, or spreadsheet: UpperLeftX, UpperLeftY, WindowWidth, and WindowHeight Set UpperLeftX and UpperLeftY to the number of pixels from the upper-left corner of the screen to position the window. Often determining the distance from the upper-left corner of the screen is not as important as determining the size of the window. Set WindowWidth and WindowHeight to the number of pixels wide and high that you want the window to be when you open it. Once you have determined the size and placement of your window, then open it. Here is an example: [openStatus] UpperLeftX = 32 UpperLeftY = 32 WindowWidth = 190 WindowHeight = 160 open "Status Window" for spreadsheet as #stats This will open a window 32 pixels from the corner of the screen, and with a width of 190 pixels, and a height of 160 pixels. BUTTONS --------------------------------------------------------------------------------------- Buttons are easily added to Liberty BASIC windows. The format is simple: button #handle, "Label", [branchLabel], corner, distX, distY open "A Window!" for graphics as #handle By placing at least one button statement before the open statement, we can add button(s) to the window. Let's examine each part of the button statement: #handle - This needs to be the same as the handle of the window. "Label" - This is the text displayed on the button. If only one word is used, then the quotes are optional. [branchLabel] - This controls what the button does. When the user clicks on the button, then program execution continues at [branchLabel] as if the program had encountered a goto [branchLabel] statement. corner, distX, distY - Corner is used to indicate which corner of the window to anchor the button to. DistX and distY specify how far from that corner in x and y to place the button. The following values are permitted for corner: UL - Upper Left Corner UR - Upper Right Corner LL - Lower Left Corner LR - Lower Right Corner Whenever a running program sits idle at an input statement, it is possible for a button-press to effect some action. If any button is pressed while the program is busy doing something else, the button-press will be buffered and read later when an input statement is encountered. MENUS --------------------------------------------------------------------------------------- Menus are easily added to Liberty BASIC windows. The format is simple: menu #handle, "Title", "Line1", [branchLabel1], "Line2", [branchLabel2], ... open "A Window!" for graphics as #handle By placing at least one menu statement before the open statement, we can add menu(s) to the window. Let's examine each part of the menu statement: #handle - This needs to be the same as the handle of the window. "Title" - This is the title displayed on the menu bar. If only one word is used, then the quotes are optional. By including an ampersand & in front of the character desired, you can turn that character into a hot-key. For example, if the title is "&Help", the title will appear as Help. "Line1" and [branchLabel1] - This is a line item seen when the menu is pulled down. [branchLabel1] is the place where execution continues if this menu item is selected. Like "Title", "Line1" requires quotes only if there is more than one word. The ampersand & character is used to assign a hot-key to the label, as in "Title", above. "Line2" and [branchLabel2] - This is a second line item and branch label for the menu. You can have as many is needed, going on with "Line3 . . . 4 . . . 5", etc. Adding seperators between menu items to group them is easy. Simply add a bar | character between each group of items. For example: . . . "&Red", [colorRed], |, "&Size", [changeSize] . . . adds a line seperator between the Red and Size menu items like so: {Illustration was here} Whenever a running program sits idle at an input statement, it is possible for a menu selection to effect some action. If any menu selection is made while the program is busy doing something else, the selection will be buffered and read later when an input statement is encountered. GRAPHICS --------------------------------------------------------------------------------------- Because graphics can involve many detailed drawing operations, Liberty BASIC does not force you to use just one print # statement for each drawing task. If you want to perform several operations you can use a single line for each as such: print #handle, "cls" print #handle, "fill black" print #handle, "pen up" print #handle, "home" print #handle, "pen down" print #handle, "north" print #handle, "go 50" Or if you prefer: print #handle, "cls ; fill black ; pen up ; home ; pen down ; north ; go 50" will work just as well, and executes slightly faster. print #handle, "\text" Display text at the current pen position. Each additional \ in the text will cause a carraige return and line feed. Take for example, print #handle, "\text1\text2" will cause text1 to be printed at the pen position, and then text2 will be displayed directly under text1. print #handle, "cls" Clear the graphics window to white, erasing all drawn elements print #handle, "fill COLOR" Fill the window with COLOR. For a list of accepted colors see the color command below. print #handle, "up" Lift the pen up. All go or goto commands will now only move the pen to its new position without drawing. Any other drawing commands will simply be ignored until the pen is put back down. print #handle, "down" Just the opposite of up. This command reactivates the drawing process. print #handle, "color COLOR" Set the pen's color to be COLOR. Here is a list of valid colors (in alphabetical order): black, blue, brown, cyan, darkblue, darkcyan, darkgray, darkgreen, darkpink, darkred, green, lightgray, palegray, pink, red, white, yellow print #handle, "backcolor COLOR" This command sets the color used when drawn figures are filled with a color. The same colors are available as with the color command above. print #handle, "goto X Y" Move the pen to position X Y. Draw if the pen is down. print #handle, "place X Y" Position the pen at X Y. Do not draw even if the pen is down. print #handle, "go D" Go foreward D distance from the current position, and going in the current direction. print #handle, "north" Set the current direction to 270 (north). Zero degrees points to the right (east), 90 points down (south), and 180 points left (west). print #handle, "turn A" Turn from the current direction using angle A and adding it to the current direction. A can be positive or negative. print #handle, "line X1 Y1 X2 Y2" Draw a line from point X1 Y1 to point X2 Y2. If the pen is up, then no line will be drawn, but the pen will be positioned at X2 Y2. print #handle, "posxy" Return the position of the pen in x, y. This command must be followed by: input #handle, xVar, yVar which will assign the pen's position to xVar & yVar print #handle, "size S" Set the size of the pen to S. The default is 1. This will affect the thickness of lines and figures plotted with most of the commands listed in this section. print #handle, "flush" This ensures that drawn graphics 'stick'. Make sure to issue this command at the end of a drawing sequence to ensure that when the window is resized or overlapped and redrawn, its image will be retained. To each group of drawn items that is terminated with flush, there is assigned a segment ID number. See segment below. print #handle, "print" Send the plotted image to the Windows Print Manager for output. print #handle, "font facename width height" Set the pen's font to the specified face, width and height. If an exact match cannot be found, then Liberty BASIC will try to find a close match, with size being of more prominance than face. print #handle, "circle r" Draw a circle with radius r at the current pen position. print #handle, "circlefilled r" Draw a circle with radius r, and filled with the color specified using the command backcolor (see above). print #handle, "box x y" Draw a box using the pen position as one corner, and x, y as the other corner. print #handle, "boxfilled x y" Draw a box using the pen position as one corner, and x, y as the other corner. Fill the box with the color specified using the command backcolor (see above). print #handle, "ellipse w h" Draw an ellipse at the pen position of width w and height h. print #handle, "ellipsefilled w h" Draw an ellipse at the pen position of width w and height h. Fill the ellipse with the color specified using the command backcolor (see above). print #handle, "pie w h angle1 angle2" Draw a pie slice inside of an ellipse of width w and height h. Start the pie slice at angle1, and then sweep clockwise angle2 degrees if angle2 is positive, or sweep counter-clockwise angle2 degrees if angle2 is negative. print #handle, "piefilled w h angle1 angle2" Draw a pie slice inside of an ellipse of width w and height h. Start the slice at angle1, and then sweep clockwise angle2 degrees if angle2 is positive, or sweep counter-clockwise angle2 degrees if angle2 is negative. Fill the pie slice with the color specified using the command backcolor (see above). print #handle, "segment" This causes the window to return the segment ID of the most recently flushed drawing segment. This segment ID can then be retrieved with an input #handle, varName and varName will contain the segment ID number. Segment ID numbers are useful for manipulating different parts of a drawing. For an example, see delsegment below. print #handle, "delsegment n" This causes the drawn segment identified as n to be removed from the window's list of drawn items. Then when the window is redrawn the deleted segment will not be included in the redraw. print #handle, "redraw" This will cause the window to redraw all flushed drawn segments. Any deleted segments will not be redrawn (see delsegment above). Any items drawn since the last flush will not be redrawn either, and will be lost. print #handle, "discard" This causes all drawn items since the last flush to be discarded, but does not not force an immediate redraw, so the items that have been discarded will still be displayed until a redraw (see above). print #handle, "trapclose branchLabel" This will tell Liberty BASIC to continue execution of the program at branchLabel if the user double clicks on the system menu box or pulls down the system menu and selects close (see buttons1.bas example below). {Illustration was here} The trapclose code in buttons1.bas looks like this: open "This is a turtle graphics window!" for graphics_nsb as #1 print #1, "trapclose [quit]" [loop] ' stop and wait for buttons to be pressed input a$ goto [loop] And then the code that is executed when the window is closed looks like this: [quit] confirm "Do you want to quit Buttons?"; quit$ if quit$ = "no" then [loop] close #1 end Since this only works when the program is halted at an input statement, the special variable TrapClose permits detection of the window close when you are running a continuous loop that doesn't stop to get user input. As long as TrapClose <> "true", then the window has not been closed. Once it has been determined that TrapClose = "true", then it must be reset to "false" via the BASIC LET statement. See clock.bas for an example. print #handle, "when event branchLabel" This tells the window to process mouse events. These events occur when someone clicks, double-clicks, drags, or just moves the mouse inside of the graphics window. This provides a really simple mechanism for controlling flow of a program which uses the graphics window. For an example, see the program draw1.bas. Sending print #handle, "when leftButtonDown [startDraw]" to any graphics window will tell that window to force a goto [startDraw] when the mouse points inside of that window and someone press the left mouse button down. Whenever a mouse event does occur, Liberty BASIC places the x and y position of the mouse in the variables MouseX, and MouseY. The values will represent the number of pixels in x and y the mouse was from the upper left corner of the graphic window display pane. If the expression print #handle, "when event" is used, then trapping for that event is discontinued. It can however be reinstated at any time. Events that can be trapped: leftButtonDown - the left mouse button is now down leftButton Up - the left mouse button has been released leftButtonMove - the mouse moved while the left button is down leftButtonDouble - the left button has been double-clicked rightButtonDown - the right mouse button is now down rightButton Up - the right mouse button has been released rightButtonMove - the mouse moved while the right button is down rightButtonDouble - the right button has been double-clicked mouseMove - the mouse moved when no button was down SPREADSHEET --------------------------------------------------------------------------------------- The spreadsheet used in Liberty BASIC is composed of 35 rows of 26 columns labeled from A to Z. The upper-left-most cell is A1 and the lower-right-most cell is Z35. Each cell can contain one of three types of data: string, number, or formula. To enter one of these three types into any cell, simply move the selector over the cell on the spreadsheet and begin typing. When done entering that cell's contents, press 'Return'. A string is entered by preceding it with an apostrophe '. Each cell is 11 characters wide so if the string is longer than 11 characters it will run into the next cell to its right. A number is entered by entering its value, either an integer or a floating point number. A formula is a simple arithmatic expression, using numbers (see above) or cell references. The result of the formula is displayed in the cell's position. Any arithmatic precedence is ignored, so any formula is always evaluated from left to right and parenthesis are not permitted (They aren't needed). A formula to compute the average of 3 cells might be: a1 + a2 + a3 / 3 The spreadsheet is a very special widget. Alone it is a very simple but complete spreadsheet. But being able to send it commands and data and to be able to read back data from it via Liberty BASIC makes it a very powerful tool. For examples, see grapher.bas and customer.bas. Modes: The spreadsheet has two modes, manual and indirect. Manual mode means that that the operator can freely move about from cell to cell with the arrow keys. He/she can also insert formulas in manual mode. Using indirect mode, the user can only move to cells predefined by the controlling application, which also decides what type of data is contained by each cell, either string or number. Here are the commands: print #handle, "manual" The manual mode is the default setting. This mode permits the user to move the cell selector wherever he/she wants and to enter any of three data types into any cell: number, string, formula print #handle, "format COLUMN right|fixed|none" This command lets the application control formatting for an individual column (COLUMN can be any letter A .. Z). right - right justify column fixed - assume 2 decimal places for numbers, and right justify also none - left justify, default print #handle, "indirect" The indirect mode is the most useful when using a spreadsheet for data entry. It enables the application to control which cells the user has access to, and what kind of information they can contain. print #handle, "cell ADDRESS CONTENTS" Place CONTENTS into the cell at ADDRESS. ADDRESS can be any cell address from A1 to Z35. The letter A to Z must be in uppercase. CONTENTS can be any valid string, number or formula (see above). print #handle, "user ADDRESS string|number" Set aside the cell at ADDRESS (same rules apply as for ADDRESS in command cell, above) as a user cell and specify the data it contains to be either a string or a number (data entered will be automatically converted to correct type). This command is only effective when indirect mode is in effect (see above). print #handle, "select ADDRESS" Place the selector over the cell at ADDRESS (again, same rules). It is important to place the selector over the first cell that the user will edit. print #handle, "result? ADDRESS" Answer the result or value of the cell at ADDRESS (again, same rules). If ADDRESS is not a valid cell address, then an empty string will be returned. This command must be followed by: input #handle, var$ (or input #handle, var if number expected) which will leave the desired cell's contents in var$ (or var) print #handle, "formula? ADDRESS" Answer the formula of the cell at ADDRESS (again, same rules). This command must also be followed with: input #handle, var$ (should always be a string returned) which will leave the desired cell's formula in var$ print #handle, "flush" This commands forces the spreadsheet to display its most up to date results. print #handle, "load pathFileName" This causes a Liberty BASIC spreadsheet file (which always have an .abc extension) named pathFileName to be loaded, replacing the current data set. print #handle, "save pathFileName" This causes spreadsheet data set (which will always have an .abc extension) to be saved to disk at pathFileName. print #handle, "modified?" This returns a string (either "true" or "false") that indicates whether any data in the spreadsheet has been modified. This is useful for checking to see whether to save the contents of the window before closing it. To read the result, an input #handle, varName$, must be performed after. print #handle, "nolabels" This turns off the row and column labels. print #handle, "labels" This turns on the row and column labels. print #handle, "trapclose branchLabel" This will tell Liberty BASIC to continue execution of the program at branchLabel if the user double clicks on the system menu box or pulls down the system menu and selects close (see rolodex1.bas). See the text for trapclose in the above graphic window section for a more complete explanation. TEXT WINDOW --------------------------------------------------------------------------------------- The text window works a little differently. Whatever you print to a text window is displayed exactly as sent. The way to send commands to a text window is to make the ! character the first character in the string. It is also important to add a semicolon to the end of command line (a print #handle line with text window commands) as in the example below. If you don't, the print statement will force a carraige return into the text window each time you print a command to the window if you don't. For example: open "Example" for text as #1 'open a text window print #1, "Hello World" 'print Hello World in the window print #1, "!font helv 16 37" ; 'change the text window's font print #1, "!line 1" ; 'read line 1 input #1, string$ print "The first line of our text window is:" print string$ input "Press 'Return'"; r$ close #1 'close the window Here are the text window commands: print #handle, "!cls" ; Clears the text window of all text. print #handle, "!font faceName width height" ; Sets the font of the text window to the specified face of width and height. If an exact match cannot be found, then Liberty BASIC will try to match as closely as possible, with size figuring more prominently than face in the match. print #handle, "!line #" ; Returns the text at line #. If # is less than 1 or greater than the number of lines the text window contains, then "" (an empty string) is returned. After this command is issued, it must be followed by: input #handle, string$ which will assign the line's text to string$ print #handle, "!lines" ; Returns the number of lines in the text window. After this command is issued, it must be followed by: input #handle, countVar which will assign the line count to countVar print #handle, "!modified?" ; This returns a string (either "true" or "false") that indicates whether any data in the text window has been modified. This is useful for checking to see whether to save the contents of the window before closing it. To read the result, an input #handle, varName$, must be performed after. print #handle, "!selection?" ; This returns the highlighted text from the window. To read the result an input #handle, varName$ must be performed after. print #handle, "!selectall" ; This causes everything in the text window to be selected. print #handle, "!copy" ; This causes the currently selected text to be copied to the WINDOWS clipboard. print #handle, "!cut" ; This causes the currently selected text to be cut out of the text window and copied to the WINDOWS clipboard. print #handle, "!paste" ; This causes the text in the WINDOWS clipboard (if there is any) to be pasted into the text window at the current cursor position. print #handle, "!origin?" ; This causes the current text window origin to be returned. When a text window is first opened, the result would be row 1, column 1. To read the result an input #handle, rowVar, columnVar must be performed after. print #handle, "!origin row column" ; This forces the origin of the window to be row and column. print #handle, "!trapclose branchLabel" ; This will tell Liberty BASIC to continue execution of the program at branchLabel if the user double clicks on the system menu box or pulls down the system menu and selects close (see rolodex1.bas). See the text for trapclose in the above graphic window section for a more complete explanation. TIPS ------------------------------------------------------------------------ - Once its techniques are mastered, the spreadsheet becomes a much better mechanism for data entry than do plain INPUT statements in a BASIC program's main window. This is especially true when many items need to be entered. In this case, making the spreadsheet the control center for your application might be a good idea. Just add buttons to the spreadsheet to perform needed functions after data is entered. - Remember, any window can have buttons (except for the main window, which is for some applications best kept minimized). Take advantage of this. - Many applications do not need the main window at all, and in these cases, you can simply include the NOMAINWIN command in your programs, which causes the main window to be omitted from your running program. - Don't forget to take advantage of the PROMPT, CONFIRM, and NOTICE statements, which borrow the syntax from the INPUT statement, but do their thing in Windows dialogs. These simple statements can help make your programs more Windows-like. - When running grapher.bas, try pulling down the command menu and selecting Open. Two *.abc files will be offered. Load one of these and click on the Graph button. - If you want to edit more than one *.bas file at once, after you select a file to edit, pull down the Files menu and select BASIC Editor. A seperate editor window will open containing the file you select. NOTE: The file that is displayed will be the last saved version, so if you have made changes and have not saved them, they will not appear in the newly opened window. - When drawing graphic figures, you can create a three dimensional effect by first drawing each figure in gray or black, and with a slight offset, and then drawing the figures on top in some other color. This creates a shadow effect.