Rebol Programming For The Absolute Beginner
By: Nick Antonaccio Updated: 3-9-06
Contents:
1. For New Programmers and for New Rebolers
2. Some Perspective for Beginners
3. Using the Rebol Interpreter to Speak to the Computer
4. Learning the Rebol Language
5. First Code Examples - Creating a GUI Window
6. More Examples
7. A Quick Comparison
8. Understanding Variables and Functions
9. Rebol Words
10. GUI Words and Grammar - Some More Depth
10.1 Actions
11. Creating Your Own Variable Words
12. Blocks
13. Function Words
14. Several Ways to Create Functions in Rebol - Passing Variables
15. Conditional Operations
16. Looping
17. Working With Longer Examples
18. Embedding Binary Data
19. Modular Programming and Code Reuse
20. A Quick Summary
21. 8 Complete Rebol Programs For You To Study
21.1 Little Email Client
21.2 FTP Chat Room
21.3 Image Effector
21.4 Sliding Tile Game
21.5 Guitar Chord Diagram Maker
21.6 Listview Database
21.7 Peer-to-Peer Instant Messenger
21.8 3D Example
22. Menus
23. Understanding the CGI Interface and Web Programming with Rebol
24. Parsing
25. Other Topics
25.1 Common Errors
25.2 6 Rebol Flavors
25.3 Spread the Word: 7 Reasons to Learn and Use Rebol
26. What Next?
26.1 A Final Point
27. Feedback
1. For New Programmers and for New Rebolers
This tutorial demonstrates how easy it is to accomplish real world programming goals with a flexible and powerful language called Rebol. The text aims to teach average users to program computers to do useful things, without the long and difficult learning curve imposed by other programming languages. If you're an experienced programmer, be sure to check out the final example applications. You'll be amazed at Rebol's compact code and simple cross-platform usability.
A downloadable package of 11 code examples from this tutorial is available at:
http://musiclessonz.com/rebol_tutorial_examples.zip.
The zip file contains screen shots and executables demonstrating what can be easily accomplished by a casual, part-time Rebol coder, within several weeks of study.
The concepts demonstrated throughout the tutorial will help new programmers understand other languages and programming tools, and will lay the groundwork for novices to acquire essential coding skills. Even novices will be able to actually create useful Rebol programs in the first few days. And Rebol isn't limited to certain types of applications or operating systems. Code written in Rebol is able to run unchanged on over 40 operating systems, and can be used to build an enormous variety of user applications with modern graphics, CGI interfaces, network functionality, database connectivity, and much more. If you're used to other development environments, you'll find that Rebol is a beautifully designed language, contained in a dramatically small and efficient package that elegantly replaces many other mainstream tools. Learning Rebol is productive, enjoyable, and thought provoking for both new and experienced coders. No matter what your experience level, learning Rebol will save you time and frustration at some point in your programming endeavors. It's a fantastically versatile little tool for which you'll find many uses.
2. Some Perspective for Beginners
Before diving into the raw mechanics, here's a fundamental perspective that's helpful to understand:
Modern computers, operating systems and programs all do very basic things, in limited ways, with a limited scope of data types:
- They let users input various types of data: text, images, sounds, video, etc.
- They let users save, retrieve, organize, share/transfer, manipulate, alter, view and otherwise deal with that data in useful ways.
Everything that can be done with a modern computer basically involves manipulating text and non-text data (non-text data is called "binary" data). In the current state of modern computing, data of all types is typically input, manipulated, and returned via graphical user interfaces such as Windows program interfaces, web forms displayed in browsers, and other keyboard/mouse driven "GUI"s. Data is saved on local hard drives and storage devices (CDs, thumb drives, etc.) and on remote web servers, and is typically transferred via local networks and Internet connections. Knowing how to control those familiar computing elements to allow users to manipulate data, is the goal of learning to program. It doesn't matter whether you're interested in writing business applications to work with inventory and scheduling (text data), programs to alter Internet web pages or emails (text and image data), programs to organize or play music (binary data), programs to transfer files across networks (text and/or binary data), programs to broadcast video and sound across the Internet (rapidly transferred sequential frames of binary data), programs to control robotic equipment (binary data sent over a connected cable), programs to play games, etc... They all require learning to input, manipulate, and return data of some sort. You can do all those things with Rebol, and once you've done it in one language, it's easier to do with other specialized languages and programming tools.
Rebol handles common user interfaces and data types easily and intuitively. It allows programmers to quickly build graphic interfaces to input and return all common types of data. It can easily manipulate text, graphics, and sounds in useful ways, and it provides simple methods to save, retrieve, and share data across all types of hardware, networks, and the Internet. That makes it a great way to begin learning how to program.
3. Using the Rebol Interpreter to Speak to the Computer
The Rebol interpreter is a program that runs on your computer. It translates written text organized in the Rebol language syntax ("source code") to instructions the computer understands. One of the great things about Rebol is that it's a very small program, contained in a single file, that runs on just about every type of computer and operating system (Windows PC, Macintosh computer, Linux web server, etc.), without any complicated install process. It lets you speak to all those machines using the same language. You just download the Rebol interpreter program, feed it a text file full of code, and the machine does what you want, with the data you want. It's easy to use.
To get the free Rebol interpreter for Microsoft Windows, go to http://rebol.com/view-platforms.html and download the view.exe file for Windows - it's clearly marked, just click it with your mouse and save it to your hard drive. When you run view.exe for the first time, you can install it if you want, but you don't have to. Just follow the instructions on screen. Once you've got the Rebol interpreter downloaded and running on your computer, click the "Console" icon, and you're ready to start typing in Rebol programs. To create your first program, type the following line into the Rebol interpreter, and then press the [Enter] (return) key on your keyboard:
alert "Hello world!"
Before going any further, give it a try. Download Rebol, and type in the code above to see how it works. It's simple and it only takes a second. If you want to run Rebol on any other operating system, just select, download and run the correct file for your computer. It works the same way on every operating system.
4. Learning the Rebol Language
To learn any programming language, you'll need to type in lots of code, by rote. Be sure to manually key in the examples in this tutorial, not just to see them work, but to get used to speaking the language. Avoid copying and pasting if you really want to learn. At first, you'll learn to code in the same way you'd learn a new spoken language - by immersing yourself in the words and phrases of the language (even if at first you don't understand what's being said). Repeating sentences by rote is a natural way to get a feel for how a language sounds and works. Certain words, phrases and patterns eventually become familiar, and the language syntax becomes clear and more intuitive.
Seeing the examples run is absolutely essential. It helps form associations between things the computer does, and chunks of code that make those things occur. Just reading through the code isn't enough - seeing how it executes is the key. If you don't have time to manually type in code, at very least paste it into the interpreter.
Because programming languages require more specific syntax than spoken languages, learning the exact grammar is more important than in spoken language. Simple examples like the ones that follow are easy to remember - just like simple phrases memorized in spoken foreign languages. To become more fluent, however, the correct order of words, characters and other elements must be typed in exactly, in a much more specific way than components of spoken language. The interpreter is not intelligent enough to "guess" what you intend to do, and if you get the grammar wrong (even one character), it'll interpret your code incorrectly and do something different then you want. Sometimes, you'll get an error, and the interpreter will tell you where you got the grammar wrong in your code. Sometimes your program just won't work correctly. Getting intimately familiar with the syntax, from the beginning, will help you avoid those problems.
5. First Code Examples - Creating a GUI Window
Now for some code! Computer programs typically use Graphical User Interfaces to get data from the user and to display data to the user. GUIs work intuitively, and modern computer users are familiar with them. They contain clickable buttons, text entry fields, menus, images, and other "widgets" that allow the user to interact with the computer. Windows programs are GUIs. Web pages are also GUIs. Users click buttons with the mouse pointer to perform actions, select settings from menus, type text data into fields, etc. Most modern programming languages include some facility to build graphic interfaces that can be used to interact with the user. Rebol makes GUI creation easy. To create a simple GUI window, just type the following line into the Rebol interpreter, and press [ENTER]. Notice the "view layout" words in the examples below - you'll use them every time you create a GUI interface in Rebol:
view layout/size [] 400x300
That line of code creates a window 400 pixels across and 300 pixels down (pixels are dots on the computer screen). It doesn't do anything yet, but the GUI window can be moved around the screen, minimized and closed (with the "X" in the upper right hand corner), just like any other Windows program. In other programming languages, just creating a window like that can take several pages of code and lots of preliminary understanding. Rebol makes it easy.
To add a button to the above GUI, type the following code. Notice that the word "button" has been added between the brackets:
view layout/size [button] 400x300
Now the GUI has a generic blue button that you can click with the mouse. To add some text to the button, type the following code. Notice that the text "Click Me" has been added after the button:
view layout/size [button "Click Me"] 400x300
To make the button do something, type the following code, and then click the GUI button with your mouse pointer. Notice that the text [alert "Hello World"] has been added after the button text:
view layout/size [button "Click Me" [alert "Hello World"]] 400x300
Now when you click the button in your GUI, the computer responds by alerting you with the message "Hello World". To make the program do something a bit more interactive, type the following, and then click the GUI button once again. Notice the "data: request-text" addition at the beginning of the line. That code requests some text from the user and assigns it to the word "data", so it can be referred to and used in the program:
data: request-text view layout/size [
button "Click Me" [alert data]] 400x300
The code above is split onto two lines so that it fits within the width of this web page, but it can be typed into the Rebol interpreter as a single line. That one line is all it takes to create a program which gets some data from the user, creates a graphic user interface that waits for user interaction, and does something with the input data (displays it in a little dialog box).
Next, we'll save some data to your computer's hard drive. Type in the following code, and click "yes" if the Rebol interpreter asks for your permission to write to the hard drive. Notice the "write %/c/data.txt data" added to the end of the line. That code writes the data collected from the user, to a file on the C: drive called "data.txt"
data: request-text view layout/size [
button "Click Me" [alert data]] 400x300 write %/c/data.txt data
If you look on your computer's C: drive after closing the GUI, you'll see that there now exists a text file (C:\data.txt) containing the text you typed into the program. (If you're working in an operating system other than Windows, you'll need to change the "c" character in the above line to refer to a root directory on your hard drive).
With that one line of code, you've got a working program that actually does something useful. It gets, displays, and saves some data from a user, using familiar GUI interactions. You could adjust it to store phone numbers, usernames/passwords, or any other useful information. It's easy - and things only get more interesting from there!
Rebol is great at dealing with all types of common data - not just text. You can easily display photos and other graphics in your GUIs, play sounds, display web pages, etc. And it's just as good at dealing with data transferred across networks and the Internet. Here's some code that downloads an image from a web server and displays it in a GUI - notice the "view layout" words again:
view layout [image load http://rebol.com/view/bay.jpg]
The word "image" inside the brackets displays a picture in the GUI. The word "load" downloads the image to be displayed.
Rebol allows many built-in effects to be applied to images. Notice the "effect [effect type]" code in the following examples:
view layout [image load http://rebol.com/view/bay.jpg effect [Grayscale]]
view layout [image load http://rebol.com/view/bay.jpg effect [Emboss]]
view layout [image load http://rebol.com/view/bay.jpg effect [Flip 1x1]]
Here's how you can read that same file from the web server and save it to your C: drive. The "/binary" modifier is used whenever dealing with binary (non-text) data:
write/binary %/c/bay.jpg read/binary http://rebol.com/view/bay.jpg
Now you can read the image directly from your hard drive and display it in a GUI. Just type in the code below. Notice the "view layout" words again, and this time in between the brackets, the file is loaded from the local C: drive:
view layout [image load %/c/bay.jpg]
It's important to note here that you could also use that image in other graphic applications on your computer - including other programs that aren't written in Rebol. You could, for example, open that file in an image editing program, or attach it to an email and send it to a friend. It's critical to understand that data can be interoperable between languages, programming tools, operating systems, and other connected domains. As you learn more about programming, you may find specialized tools that accomplish certain programming goals easily and effectively. You can trade data between various tools by just saving it to a shared storage medium. In that way, all programming languages and tools can be used together to accomplish complex goals. That's a key concept to keep in mind when learning about general programming. Very little technology is truly new. The basis for most computing applications has been around for several decades (hard drives for storing files within directory structures, hardware for inputting and outputting text, graphic, audio, and other data, networks for transferring data between machines, etc.). Those base components haven't changed too dramatically. They've simply improved in speed and capacity - and the software tools used to work with them have evolved to allow programmers to do high level things more quickly and easily. An adept programmer writing an application to deal with large amounts of text data shared across the Internet, however, will realize that the data all still resides in files on a hard drive somewhere, in a machine connected to a network, and that data can be accessed by various old fashioned programming means - even if it was created and put there by a program using cutting edge database technology, displayed in a flashy new graphic interface, and transferred over broadband wireless connections. It's still just text data saved in a file somewhere, and it can be manipulated via virtually any language that provides access to networks and text files! Keep that in mind as your exposure to various programming tools expands...
6. More Examples
Below are some more short examples of reading, writing, and manipulating data on the hard drive and Internet, and interacting with the user. Type them into the Rebol interpreter to familiarize yourself with a bit more of the Rebol language. You may want to configure Rebol to open straight to the interpreter console. Click the "User" menu on the Rebol Viewtop, and uncheck "Open Desktop On Startup". That'll save you the trouble of clicking the "Console" button every time you start Rebol.
The following line displays the current day and time in the interpreter:
print now
The word "print" displays text data directly in the Rebol interpreter. The word "now" refers to the current date and time.
The following line performs some mathematical computations, and displays the result:
print (10 + 12) / 2
The following code asks the user to choose a file on the hard drive:
request-file
The code below allows the user to choose a color:
request-color
The following code asks the user a yes-no question:
request "Are you having fun yet?"
Here's a nice way to let the user select a date:
request-date
The code below requests a username and password from the user:
request-pass
The following code opens your computer's web browser and displays the indicated web page:
browse http://rebol.com
The following code launches Rebol's built in text editor, and opens the file c:\test.txt
editor %/c/test.txt
Notice the percent character ("%") in the example above. In Rebol, that character is used to represent all file labels. Because Rebol can be used on many operating systems, and because those operating systems all use different syntax formats to refer to drives, paths, etc., Rebol uses the universal format: %/drive/path/path/.../file.ext . For example, "%/c/windows/notepad.exe" refers to "C:\Windows\Notepad.exe" in Windows. Rebol converts that syntax to the appropriate operating system format, so that your code can be written once and used on every operating system, without alteration.
The following line sends an email to user@website.com, containing the text "Hi user. How are you doing?". Try replacing the username and website with your own email address (If you downloaded and ran Rebol without actually installing it, you'll need to run the configuration wizard in order to send the email. To do that, type "install" and follow the instructions.):
send user@website.com "Hi user. How are you doing?"
The line below sends a web page to user:
send user@website.com read http://www.rebol.com
The code below displays the contents of user's email inbox:
print read pop://user:pass@website.com
The following line uploads a single file to user's web server using ftp:
write/binary ftp://user:pass@website.com read/binary %file
The following uploads an entire directory of files to user's web server:
foreach file load %./ [if not dir? file [write/binary join
ftp://user:pass@website.com/ file read/binary file]]
It all looks a lot like spoken English, doesn't it? You just need to type things in correctly, and the computer does what you want. The more of the language you learn, the more you'll be able to make the computer do your bidding... Easy, right?
7. A Quick Comparison
To provide a quick idea of how much easier Rebol is than other languages, here's a short example. The simplest code to create a basic Rebol GUI window was presented earlier:
view layout/size [] 400x300
It works on every type of computer, in exactly the same way.
Code for the same simple example is presented below in the popular programming language "C++". It does the exact same thing as the Rebol one-liner above, except it only works in Microsoft Windows. If you want to do the same thing on a Macintosh computer, you need to memorize a completely different page of C++ code. The same is true for Unix, Linux, Beos, or any other operating system. You have to learn enormous chunks of code to do very simple things, and those chunks of code are different for every type of computer. Furthermore, you typically need to spend a semester's worth of time learning very basic things about coding format and fundamentals about how a computer thinks before you even begin to tackle useful basics like the code below:
#include <windows.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "C_Example";
int WINAPI
WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd;
/* This is the handle for our window */
MSG messages;
/* Here messages to the application are saved */
WNDCLASSEX wincl;
/* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
/* This function is called by windows */
wincl.style = CS_DBLCLKS;
/* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
/* No menu */
wincl.cbClsExtra = 0;
/* No extra bytes after the window class */
wincl.cbWndExtra = 0;
/* structure or the window instance */
/* Use Windows's default color as window background */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register window class. If it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0,
/* Extended possibilites for variation */
szClassName,
/* Classname */
"C_Example",
/* Title Text */
WS_OVERLAPPEDWINDOW,
/* default window */
CW_USEDEFAULT,
/* Windows decides the position */
CW_USEDEFAULT,
/* where the window ends up on the screen */
400,
/* The programs width */
300,
/* and height in pixels */
HWND_DESKTOP,
/* The window is a child-window to desktop */
NULL,
/* No menu */
hThisInstance,
/* Program Instance handler */
NULL
/* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
/* Run the message loop.
It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages
into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 -
The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows
function DispatchMessage() */
LRESULT CALLBACK
WindowProcedure (HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
/* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0);
/* send a WM_QUIT to the message queue */
break;
default:
/* for messages that we don't deal with */
return DefWindowProc (hwnd, message,
wParam, lParam);
}
return 0;
}
Yuck. Back to Rebol...
8. Understanding Variables and Functions
All programming languages make use of two important features: variables and functions. Functions are commands that tell the computer to do something. They're similar to verbs in spoken language. You've used some functions already in the earlier examples. For example, "print" is a function - it can be thought of as a verb that represents some action.
Variables are names given to data to be input, stored, manipulated, displayed, etc. They're similar to nouns in spoken language. For example, "now" can be thought of as a variable. It's a noun referring to a piece of data (the current time).
Functions and variables must be used in a syntax defined by the programming language you're using. No existing computer language allows you to speak freely as in the natural language sentence "show the user some photos of their children". You have to write the commands in a more specific way - using a syntax that invariably involves variables and functions. As a programmer, to get the computer to "show the user a photo", you'd:
- assign the picture filename to a variable (noun)
- use a function (verb) to display the image referred to by the variable
- assign the function action to a button or some other graphic widget in a GUI (which is itself created by arranging variables and functions in a specific order)
Rebol does many things in as "high level" a way as can be expected - as close to speaking to the computer in human English as is currently possible, but it still requires specific syntax and structure. All contemporary computer languages do. If you want to learn how to program in any language, you'll need to learn how to use variables and functions in the grammar the language defines. That's at the core of all programming.
In Rebol, variables and functions are assigned to "words".
9. Rebol Words
If you want to give a label to some data - so that it can be used in your program - you must assign it a word. The same is true for actions performed by functions. There are many function words built into Rebol that represent common actions ("print", "alert", "request-date", etc). You've seen a number of those built-in words already in the earlier examples. To create your own functions, you take previously created function words and variables, group them together in a specific order that accomplishes what you want, and assign a word to that collection of code. Then you can refer to that group of actions and/or related data, using the word you've assigned to it.
Learning the function words that are already defined in a programming language is a big part of learning to program. Those words are the existing vocabulary of the language, and in order to work correctly, they typically expect some other words to follow afterwards, in a specific order and format. The word "write", for example, writes data to a storage device (hard drive, flash drive, web server, etc). It's a function that performs an action, and expects the name of a file to be written to the hard drive, and then the name of the data to write to that file. It's gotta be written in that order:
write %/c/text.txt "This is some random text"
If you type the above line in the wrong order, it won't work:
write "This is some random text" %/c/text.txt ; WRONG
If you type it in backwards, Rebol won't understand the syntax, and you'll receive an error.
10. GUI Words and Grammar - Some More Depth
As you saw in the earlier examples, the words "view layout", followed by two brackets ("[]") can be used to display a Graphic User Interface in Rebol. You can put elements that you want to see in the GUI, inside the brackets. Rebol contains words that display all the common graphic elements used in GUIs. Try typing in the following code examples:
view layout [button]
view layout [field]
view layout [text "Rebol is really pretty easy to program"]
view layout [text-list]
view layout [
button
field
text "Rebol is really pretty easy to program"
text-list
check
]
Notice that the words can be separated by "white space" inside the brackets. Extra spaces, carriage returns, and other empty characters are ignored by the interpreter. Tab stops are traditionally used to indent the lines within brackets, but they're not required.
More descriptive characteristics about the graphic elements can be included directly after each of their respective words. Such modifiers are called "facets" in Rebol, and they allow you to adjust all characteristics of every type of graphic widget (size, color, displayed text), etc. Try typing in the code below - it's the same as the above code, with some additional facet characteristics:
view layout [
button red "Click Me"
field "Enter some text here"
text "Rebol is really pretty easy to program" purple
text-list 400x300 "line 1" "line 2" "another line"
check yellow
]
10.1 Actions
IMPORTANT: If you want a graphic element to perform an action, just put the action word(s) in brackets after it. When the GUI element is clicked with the mouse or otherwise activated, the action will be performed. Type in the following code to see how it works:
view layout [button [alert "You clicked the button."] ]
view layout [button red "Click Me" [alert "You clicked the red button."]]
view layout [
text "Some action examples. Try using each widget:"
button red "Click Me" [alert "You clicked the red button."]
field 400 "Type some text here, then press [Enter] on your keyboard."
[alert value]
text-list 400x300 "Select this line" "Then this line" "Now this line"
[alert value]
check yellow [alert "You clicked the yellow check box."]
button "Quit" [quit]
]
Notice how the word "value" refers to the selected item in the text-list, and to the text contained in the text input field.
Alternate actions (i.e., those triggered by a right click of the mouse) can be included for any graphic widget. Just enclose them in a second block, surrounded by brackets:
view layout [button [alert "left click"] [alert "right click"]]
Here are some other GUI elements used in the Rebol language:
view layout [
backcolor white
h1 "More GUI Examples:"
box red 500x2
bar: progress
slider 200x16 [bar/data: value show bar]
area "Type here"
drop-down
across
toggle "Click" "Here" [print value]
rotary "Click" "Again" "And Again" [print value]
choice "Choose" "Item 1" "Item 2" "Item 3" [print value]
radio radio radio
led
arrow
return
text "Normal"
text "Bold" bold
text "Italic" italic
text "Underline" underline
text "Bold italic underline" bold italic underline
text "Serif style text" font-name font-serif
text "Spaced text" font [space: 5x0]
return
h1 "Heading 1"
h2 "Heading 2"
h3 "Heading 3"
h4 "Heading 4"
tt "Typewriter text"
code "Code text"
below
text "Big" font-size 32
title "Centered title" 200
across
vtext "Normal"
vtext "Bold" bold
vtext "Italic" italic
vtext "Underline" underline
vtext "Bold italic underline" bold italic underline
vtext "Serif style text" font-name font-serif
vtext "Spaced text" font [space: 5x0]
return
vh1 "Video Heading 1"
vh2 "Video Heading 2"
vh3 "Video Heading 3"
vh4 "Video Heading 3"
label "Label"
below
vtext "Big" font-size 32
banner "Banner" 200
]
The examples above demonstrate how Rebol creates GUIs that can be used to input and display data in a variety of ways that are familiar to users. They can be customized using facets, and they can perform actions. That's a big part of building a typical modern computer program! For more information about GUI design, see http://rebol.com/docs/easy-vid.html and http://rebol.com/docs/view-guide.html. To make your GUIs do useful things, you need to learn more about making the language manipulate data in useful ways...
11. Creating Your Own Variable Words
Just like spoken languages, programming languages are malleable and expressive. There's never only one way to write a given program. You need to choose and use your own words, and you need to organize them so they have the meaning and function you intend. Just like in spoken language, you have to think about what you're trying to say, and organize your thought process to say it. In Rebol you have the additional benefit of being able to create your own language words to express actions and to label data.
Words are created and assigned to variables and functions in Rebol by the use of the colon (":") character. You can use any word you want to refer to any specific data or action(s). For example, If you want to use the word "picture" to refer to an image file on the Internet, you could do the following:
picture: load http://rebol.com/view/bay.jpg
The above line creates a variable (label) that can be used in your program anywhere you want to read and use the file located at http://rebol.com/view/bay.jpg . The built-in Rebol word "load" does the actual work of going out to the web site address and collecting the image data.
Now you can use the word "picture" to refer to the above image. Display it in a GUI using the following code:
view layout [image picture]
The words "view", "layout", and "image" are built into Rebol, and the word "picture" is now just as valid, because it's been defined to the interpreter.
Because the word "picture" is _variable_, you can also redefine and _change_ the data referred to by it:
picture: load http://rebol.com/view/demos/palms.jpg
Now, when you use the word "picture" in your program, it refers to a different file at a different Internet location. Writing the same GUI code now displays that different photo:
view layout [image picture]
You can also make the word "picture" refer to a file on your hard drive, or anywhere else you'd like:
picture: load %/c/bay.jpg
Here are some more examples of creating and using variable words. Type them into the Rebol interpreter to see how they work, and to understand how assigned words can take the place of _any_ data. NOTE: anything after a semicolon in Rebol code is ignored by the interpreter. It's used to include human readable comments in the code. You don't need to type any of the comments into the interpreter:
acolor: "blue"
alert acolor ; alerts you with a dialog box displaying the text "blue"
print acolor ; prints the word "blue" in the Rebol interpreter
anumber: 12
print anumber ; prints the number 12 in the interpreter
computation: (10 + 12) / 2
print computation ; prints the answer
filename: request-file
print filename ; prints the name of the user chosen file
chosen-color: request-color
print chosen-color
answer: request "Are you having fun yet?"
print answer
pick-a-date: request-date
print pick-a-date
userpass: request-pass
print userpass
webpage: http://rebol.com
browse webpage
file: %./test.txt
editor file
; launches Rebol's built in text editor and opens the filename
; assigned above
email-address: user@webpage.com
message: "Hi Luke. How are you doing?"
send email-address message
The key is to be aware of the colon character (":"). It's the thing that tells Rebol to create a new variable word and assign it to some value that'll be used later.
12. Blocks
In Rebol, you can use words to represent multiple pieces of data grouped together, collections of other word variables, and other programming elements. Just surround the data in brackets. Type in the code below to see how it works:
somecolors: ["red" "yellow" "blue" "black"]
; "somecolors" is now a defined word used to represent the entire
; data block enclosed in brackets.
print somecolors
In Rebol, collections like this are called "blocks", and they're very important. In fact, they're the primary organizational unit in Rebol and the main structure in which data is stored in the language. That's very important - remember, programming is all about inputting, storing, retrieving and otherwise manipulating data. In Rebol, ANY type of data can be assigned a word, and blocks can contain any combination of words and raw data. You've already used blocks - brackets were snuck in earlier to display GUI elements and to perform actions on them. Any group of words surrounded by brackets forms a block, and you can assign a word to refer to that block. Words referring to complex blocks of data can be likewise grouped together into other blocks simply by surrounding the words with brackets. This makes dealing with very complex and useful data structures very easy in Rebol.
Type in the code below to see how an entire GUI layout can be built and represented by using one word label:
gui-layout: [button field text-list]
"gui-layout" now refers to that entire block of code. You can display it using the standard "view layout" syntax:
view layout gui-layout
Blocks of data can also be spread out over several lines, and can be separated by extra white space. Just surround the elements with brackets:
gui-layout2: [
button red "Click Me"
field "Enter some text here."
text "Rebol is really pretty easy to program." purple
text-list 400x300 "line 1" "line 2" "another line"
check yellow
]
Now display it in a GUI:
view layout gui-layout2
Layout blocks much bigger than that can be stored in a single word!
NOTE: It's standard practice to indent compound blocks with consecutive tab stops. Starting and ending brackets are typically placed at the same indentation level. This is conventional in most programming languages, because it makes complex code easier to read. For example, the compound block below:
[blue red green [1 2 4 [jan feb march [monday tuesday wednesday]]]]
can be written a bit more clearly as:
[blue red green
[1 2 4
[jan feb march
[monday tuesday wednesday]
]
]
]
Indentation is not required, but it's very helpful when dealing with more intricate programming structures.
Here's a simple data table example, containing schedule information within a block:
schedule: [
["John Smith" "Monday" "3:00 pm"]
["Dave Jones" "Tuesday" "11:00 am"]
["Janet Duffy" "Wednesday" "4:45 pm"]
]
You can display the above block in a GUI, using the built-in Rebol word "list". Notice the "data schedule" code in the last line below - it inserts the entire schedule block defined above as data to be displayed in the list widget (the line "across text 150 text 150 text 100" is just syntax expected by the list widget to define its graphic layout):
view layout [
vh2 "This Week's Appointments:"
list 600x400 [
across text 150 text 150 text 100
] data schedule
]
The beginnings of a graphical "database" application, in several lines of code... Not tough at all!
Many of Rebol's built in words help you manipulate data stored in blocks. Type in the following code to see how the "sort" word works:
somecolors: ["red" "yellow" "blue" "black"]
sortedcolors: sort somecolors
"Sort" is a built in function word that alphabetically (/ordinally) sorts the elements of a given block. The line above creates the newly defined word "sortedcolors", and assigns it to the sorted block of words contained in "somecolors".
print sortedcolors
; This code displays the sorted block of text.
print first sortedcolors
; "first" is another built-in word.
; It selects the first item in a given block.
find somecolors "red"
; "find" is a built in word that searches for data within a block.
You can easily save blocks of data to your hard drive, read them from a web server, and perform other file operations with them.
write %/c/colors.txt somecolors
; writes the entire block of text represented by "somecolors"
; to a text file called colors.txt on the C: drive.
Here's an interesting twist, demonstrating how Rebol can easily mix datatypes within a block:
an-image: load http://rebol.com/view/bay.jpg
; downloads an image from the Internet and assigns the
; word "an-image" to it.
append sortedcolors an-image
; "append" adds the downloaded image to the end of the data block
; currently containing the simple text words defined above.
; Now the block contains both text and binary image data,
; all assigned to a single word - not a problem in Rebol!
Now you can select items from that new block:
print first sortedcolors
; prints the first item in the data block - the text "black".
view layout [image fifth sortedcolors]
; displays the fifth item in the data block -
; the image downloaded above - in a simple GUI.
Here's some more notation to be familiar with when working with sequential data in blocks (that type of data is called "series" in Rebol):
view layout [image sortedcolors/5]
; "sortedcolors/5" is another way to refer to the fifth item
; in a data block.
The following example puts the above notation to work in a useful way:
length-of-block: length? sortedcolors
; the built in word "length?" returns the number of items
; in the block (5 in this case).
view layout compose [image sortedcolors/(length-of-block)]
The word "compose" allows variables in parentheses to be evaluated and inserted as if they'd been typed explicitly into the code of the program. In the example above, the code reads as if sortedcolors/5 had been typed in manually.
The following examples demonstrate additional words used to traverse sequential series of data within blocks:
insert sortedcolors "mauve"
; adds the word "mauve" to the sortedcolors block of data.
remove sortedcolors
; removes the first item from the block.
head sortedcolors
; sets a position marker at the beginning of the data block
next sortedcolors
; sets a position marker at the next item in the data block
last sortedcolors
; sets a position marker at the last item in the data block
back sortedcolors
; sets a position marker at the previous item in the data block
tail sortedcolors
; sets a position marker after the last item in the data block
The fact that you can mix together all types of data within a block, refer to parts of blocks by name, and access/alter data within them using built-in functions, is very powerful and useful. Blocks and variable words assigned to blocks help you store, manipulate, and refer to all the data you'll deal with in your programs. Doing so is similar to assigning nouns to groups of people, places, and things in spoken language. Within those groups, smaller groups and individual items can be named, ordered and otherwise organized - in the same way that complex database applications allow you to store and work with all types of information. The possibilities of dealing with data in that way are endless.
13. Function Words
It's easy to define your own function words (actions), once you know some of the built in vocabulary of Rebol. Creating new functions is comparable to creating your own verb words in a spoken language. Just be careful not to unintentionally use words that are already defined in the Rebol language, or in your current program. That would change the meaning of the existing word. For example, you could accidentally change the meaning of the word "write" to refer to a picture on your hard drive by typing the following:
write: read %/c/bay.jpg
; *** DON'T TYPE THAT IN - it'll change the meaning of the word
; "write" in the Rebol interpreter (only for the current session).
; It's an example of what not to do. ***
You can protect all of the built-in Rebol words by typing in "protect-system". That'll alert you with an error and disallow any attempt to redefine native Rebol words. You still must be careful not to accidentally redefine words that you've created. Word definitions only last for the current session, so if you make a mess, all you need to do is restart the Rebol interpreter.
Here's a really important concept: (drum role) ... In Rebol, you can use single words to represent entire blocks of actions (i.e., collections of function words grouped together). In fact, blocks like that form the primary code sections that make up programs in the Rebol language. Here's an example of several function words grouped together into a block (i.e., enclosed in brackets), and assigned a new function word:
some-actions: [
alert "Here is one action."
print "Here's a second action."
write %/c/anotheraction.txt "Here's a third action."
]
The above code has created a kind of super-verb that refers to several actions. You can perform the actions contained in any block using the "do" function word. To perform all the actions in the above code block, just type:
do some-actions
You can also include the word "does" right inside a word definition - that'll make the actions (function words) contained in the block perform automatically every time the new word is used in Rebol:
more-actions: does [
alert "4"
alert "5"
alert "6"
]
In fact, by including the "does" command right in the word definition, you've just created a new function (or "subroutine") that can be used like any other built-in action word in Rebol! You can now talk to the interpreter using that word, and it understands what you mean for it to do. After you've entered the code above, try typing the word "more-actions" into the Rebol interpreter:
more-actions
It's taken on a life of its own! As with variable words, the key is to be aware of the colon character - that's the symbol that actually creates the new function word.
Here's an example of useful little action word to clear the command line screen in the Rebol interpreter.
cls: does [prin "^(1B)[J"]
The native way to clear the Rebol interpreter's command screen is by typing "prin "^(1B)[J". That's kind of awkward to type, and even tougher to remember. Instead, we can assign the word "cls" to perform the action - just like in old Basic languages. Now just type:
cls
and the screen clears - that's much easier.
Here's a little program that creates the new action word "send-email". It provides a simple text requestor interface for users to send email:
send-email: does [
email-address: to-email request-text/title/default
"Enter an email address:" "user@webpage.com"
; the above line creates the new variable word "email-address"
; "email-address" is assigned the value of the
; text input using the built in word "request-text"
; the "title" and "default" refinements customize the
; info displayed in the text requestor.
message: request-text
; the above line creates the new variable word "message"
; and assigns it to some requested text
send email-address message
; the above line sends the user-provided message to the
; email address given earlier
alert "Your message has been sent."
]
send-email ; do the routine above
send-email ; do it again to send another message to someone else
The above process is VERY important. It's the basis of how you perform more complex and useful actions in Rebol. Blocks of actions and blocks of data form the basis of how you program in Rebol. And performing actions upon data is what you'll do to complete all your programming tasks. In Rebol you just group bits of data together into blocks, and assign a name to refer to it. You also group functions together into blocks to perform actions upon the data, and assign a name to refer to those actions. You can even combine complete groups of data and functions into blocks that can be assigned unique word identifiers that perform complete programmatic tasks for you: store and manipulate data all with a single word that you define! That allows you to create your own unique language in any program you write, using words that you define. You could create, for example, your own language that reads something like "email tom at noon with the main news page from yahoo.com". Those words aren't built into Rebol, but they can be assigned the appropriate action and data meanings to make that sentence a completely functional piece of code that the computer understands! (It would still need to be syntactically correct and exact). Building word meanings in that way is called building a "dialect" in Rebol, and it's one of the ways Rebol is different from other languages. You could conceivably write a dialect for programming robotic devices that reads "Vacuum the floor. Move in a concentric circles around the perimeter". You could write a dialect for doctors that reads "print prescription for John Smith" (Ponder for a moment the possibilities for that type of natural language capability and flexibility...). Translating the existing Rebol language to any other language is as simple as assigning built-in function/data words to words in the desired language (for example, translating the print function to French, Italian, and Spanish is as simple as typing "imprime: stampa: impresion: :print"). Other languages focus on different ways of grouping and managing functions and variables. Typically, other languages use more cryptic ways of doing things. For example, blocks that contain both fully encapsulated functions and variables are called "objects". You'll see more about "object-oriented" programming as you study Rebol and other languages in depth...
14. Several Ways to Create Functions in Rebol - Passing Variables
There are several built-in words in the Rebol language that allow you to create more complex function words. To create simple functions, you can use the "does" command, as described above. But some functions are more complicated than that. They perform work upon _variable_ data. For example, the following simple function displays the square root of 4. That's all it can do:
sqr-four: does [print square-root 4]
This type of function is similar to what you saw in the previous section. The word "sqr-four" is now assigned to the action "print square-root 4" (the word "square-root" is built into Rebol). After entering the above line in the Rebol interpreter, type:
sqr-four
It'll give you the expected result of 2.
Say now, however, that you now want to do something using numbers other than 4... What if you want to create a function that adds 4 to some other number, and then computes the square root of that sum? For that to work, the other number must be changeable, and therefore must be assigned a variable name. That variable name can then be "passed" to the function. The built in word "func" is used to create functions to which changeable variables can be passed. The syntax for the word "func" expects it to be followed by two blocks of code. The first block contains the name(s) of the variables to be passed. The second block contains the actions to be taken. Here's how it looks:
func [names of variables to be passed] [
actions to be taken with those variables
]
In the following line, a function is created in which a variable word "anumber" is named and passed. That whole function is assigned the word "sqr-var":
sqr-var: func [anumber] [print square-root (4 + anumber)]
Now you can use the word "sqr-var", and the Rebol interpreter knows what to do with the assigned data. Try the following code:
sqr-var 12 ; prints "4", the square root of 12+4 (16)
sqr-var 96 ; prints "10", the square root of 96+4 (100)
Printing the square root of 4+somenumber may not sound so exciting to you, but it helped to illustrate one of the most important techniques employed in all modern programming languages. The process of passing variable parameters to functions is a fundamental part of all modern programming. It's perhaps the single most common element in contemporary languages, and understanding how it's done is absolutely essential. Below are some more interesting real-world examples.
The following line creates a simple function to display images:
display: func [filename] [view layout [image load filename]]
It accepts an image filename as the passed parameter (%somefile.jpg, %somefile.gif, %somefile.png, or %somefile.bmp), and then creates a GUI to display the image. That set of actions is assigned to the word "display". Simple, right? Now you can use the word "display" like this:
image1: to-file request-file/title trim {
Select an image from your hard drive:} ""
; gets an image filename from the user
display image1
; displays the above image using the new function word
display http://rebol.com/view/bay.jpg
; displays the image at the above url
display %/c/bay.jpg
; displays an image that was saved to the
; hard drive earlier in this tutorial
Once the word "display" is defined in your programs, you can use it as if it's a built in Rebol action word.
Here's an example that asks the user for 2 website urls, and then opens those sites in separate brower windows:
openwebsite: func [nameofwebsite] [browse nameofwebsite]
; The line above creates a new function that passes a url
; to the built-in Rebol word "browse", to open the passed
; web site name in the user's default browser. It also
; assigns the new function word "openwebsite" to that set
; of actions.
website1: request-text/title "Enter a web site URL:"
; The line above assigns a new variable word "website1" to text
; returned by the built-in Rebol "request-text" function.
website2: request-text/title "Enter another web site URL:"
; Gets some more text and assigns the new variable word "website2"
; to it.
openwebsite website1
; The line above uses the new "openwebsite" function word defined
; above, and passes it the "website1" variable word
openwebsite website2
; Uses the openwebsite function again, this time passing the
; website2 variable
In that example, the word "openwebsite" is assigned to a new function definition. "Website1" and "website2" are labels for variables passed to that function. Below is a variation which assigns a single word to that entire process:
display-website: does [
openwebsite: func [nameofwebsite] [browse nameofwebsite]
website: request-text/title "Enter a web site URL:"
openwebsite website
]
Now you can use the single word "display-website" in your programs to do that entire block of code.
display-website
Getting used to the above syntax and way of thinking is absolutely essential. Remember, dealing with all types of data is the main thing you'll do as a programmer. Passing variable data to functions that you create is the main way you'll perform actions upon data in all your programming. That's the current state of programming, and it's what you'll do in virtually every programming situation. Rebol makes it easy to deal with data, using its built in support for most common data types. Understanding how to input, manipulate, and output that data is your main objective. Using functions and variables as described above is a fundamental part of that process.
15. Conditional Operations
Programs often need to make decisions based on user input, program states, data content, etc. "If the user selects this option from a list, respond by executing this function". That's a common situation. "If a certain amount of time has passed, save data to the hard drive". Giving the computer a variety of actions to perform, based on a variety of expected conditions, is a fundamental programming technique used in all languages.
Mathematical operators such as = < > <> (equal, less-than, greater-than, not-equal) are typically used to perform conditional operations. Type in the following code to see how a conditional operation works:
if now/time > 12:00 [alert "It's after noon."]
; now/time is a variation, or "refinement" of the built-in
; function "now", that returns only the current time.
Here's a more complex example:
daily-calories: to-integer request-text/title {How many
calories have you eaten today?}
; gets some info from the user and assigns the variable word
; "daily-calories". The built-in "to-integer" function
; helps make sure Rebol interprets that info as a number.
; The "{}" characters surrounding the title text work the
; same way as quotes, but allows the string of text to
; span several lines. Very important.
if daily-calories > 2500 [alert "You need to stop eating now."]
The built-in Rebol word "either" chooses between two blocks of functions to perform, based on a conditional evaluation. Its syntax is:
either {condition} [
block to perform if the condition is true
][
block to perform if the condition is false
]
Here's an example:
either now/time > 8:00am [alert "It's time to get up!"] [
alert "You can keep on sleeping."]
Here's a variation of the above example that allows you to set the wake up time:
wake-up: to-time request-text/title "What time do you want to wake up?"
either now/time > wake-up [alert "It's time to get up!"] [
alert {You can keep on sleeping."}]
The built-in Rebol word "switch" chooses between numerous functions to perform, based on conditional evaluations. Its syntax is:
switch/default {main value} [
{value 1} [block to execute if value 1 = main value
{value 2} [block to execute if value 2 = main value]
{value 3} [block to execute if value 3 = main value]
; etc...
] [default block of code to execute if none of the the values match]
You can compare as many values as you want against the main value, and run a block of code for each matching value. Here's an example:
favorite-day: request-text/title "What's your favorite day of the week?"
switch/default favorite-day [
"Monday" [alert "Monday is the worst! Just the start of the week..."]
"Tuesday" [alert "Tuesdays and Thursdays are both ok, I guess..."]
"Wednesday" [alert "The hump day - the week is halfway over!"]
"Thursday" [alert "Tuesdays and Thursdays are both ok, I guess..."]
"Friday" [alert "Yay! TGIF!"]
"Saturday" [alert "Of course, the weekend!"]
"Sunday" [alert "Of course, the weekend!"]
] [alert "You didn't type in the name of a day!"]
"Switch" gets used a lot because programs often require numerous multiple evaluation results to choose from.
Rebol includes a rich set of words and functional structures that help you evaluate conditions in all types of situations, and with all types of data. Understanding how to use them is a big part of learning the language.
16. Looping
Programs are often required to check conditions and to execute functions repeatedly. In fact, in most large applications the computer often loops through many instances of repetitive work. For example, in a reminder program, the application may need to continually check the time and date to see if the user should be reminded of a given event at the moment. In other types of programs, the computer may need to repetitively scan through collections of data, or repeatedly request/respond to user input. To handle such situations, "loop" structures provide programmatic ways to methodically repeat actions.
The built in word "forever" creates a simple repeating loop. Its syntax is:
forever [block of actions to repeat]
The following code builds a simple timer that alerts the user when one minute has passed. It uses a forever loop to continually check the time.
alarm-time: now/time + 60
; assign a variable to the time 60 seconds from now
forever [if now/time = alarm-time [alert "60 seconds has passed" break]]
Notice the word "break" in the example above. It exits the forever loop so that the program doesn't run on forever once the alert has been displayed.
Here's a more interactive version that uses some info provided by the user. The heart of the program is still the loop at the end:
event-name: request-text/title "What do you want to be reminded of?"
; requests the name of an event from the user
seconds: to-integer request-text/title trim {
How many seconds do you want to wait?}
; requests a number of seconds to wait
alert join "it's now " [
now/time ", and you'll be alerted in " seconds " seconds."
]
; display a message
alarm-time: now/time + seconds
; set the alarm time
forever [
if now/time = alarm-time [
alert join "it's now " [
alarm-time ", and " seconds
" seconds have passed. It's time for: " event-name
]
break
]
]
; the forever loop continually compares the set alarm time to
; the current time, then displays an alert when they match.
Notice the "join" word used in several of the lines above. It's syntax format is:
join {data} [block of data items]
Using that format, it joins together variables, text, blocks and other bits of data, so that they can be printed together, displayed, and otherwise manipulated to form a single piece of data. It's very useful! A variation on join is "rejoin". It takes a sigle block of data, and concatenates all the individual items into one piece of data. You should be familiar with this syntax too:
rejoin [item1 item2 item3 ...]
Now, back to loops. Here's a simple forever loop that displays/updates the current time in a GUI. Notice the block indentation:
view layout [
timer: field
button "Start" [
forever [
set-face timer now/time
wait 1
]
]
]
The above GUI contains two widgets: a text field which is assigned the variable label "timer", and a button with the word "Start" on it. The action block for the button contains a forever loop that loops 2 actions repeatedly (until the user closes the GUI): the built in word "set-face" sets the text in the "timer" field to the current time, the program waits 1 second, and then loops.
Often, the data dealt with in each repetition of a loop must change. Like most languages, Rebol includes a variety of functions and programmatic structures that allow you to loop through blocks of data and perform operations using consecutively changed values. A common looping structure in many languages is the "for" structure. It allows you to specify a starting value, an ending value, an incremental value, and a variable name to hold the current value, so that you can loop through consecutively changing values in a controlled way. Here's the basic Rebol syntax for a "for" loop:
for {variable word to hold current value} {starting value} {ending value} {incremental value} [block of code to perform, which can make use of the current variable value]
Here are some simple examples. Be sure to type them in to see how they work:
for counter 1 10 1 [print counter]
; starts on 1 and counts to 10 by increments of 1
for counter 10 1 -1 [print counter]
; starts on 10 and counts backwards to 1 by increments of -1
for counter 10 100 10 [print counter]
; starts on 10 and counts to 100 by increments of 10
for counter 1 5 .5 [print counter]
; starts on 1 and counts to 5 by increments of .5
for timer 8:00 9:00 0:05 [print timer]
; starts at 8:00am and counts to 9:00am by increments of 5 seconds
for dimes $0.00 $1.00 $0.10 [print dimes]
; starts at 0 cents and counts to 1 dollar by increments of a dime
for date 1-dec-2005 25-jan-2006 8 [print date]
; starts at December 12, 2005 and counts to January 25, 2006
; and by increments of 8 days
for alphabet #"a" #"z" 1 [prin alphabet]
; starts at the character a and counts to z by increments of 1 letter
Notice that Rebol can easily loop through various types of data, forewords, backwards, and in partial increments, with a native understanding of those data types. It can automatically increment dates, money, time, etc. In other languages, that can be tough to do.
Also, notice the use of the "prin" word in the last example. It works like "print", except it doesn't automatically insert a carriage return (i.e., it prints each successively output character next to the previous one, instead of on separate lines).
Here's a "for" loop example that displays the first 5 file names in the current folder on your hard drive:
files: read %.
; gets the current directory list,
; and assigns that block of filenames to the variable "files"
for count 1 5 1 compose [print files/(count)]
; starts printing with the 1st item in the block,
; and counts up to the 5th item.
In the example above, "files/1" is the syntax representing the first item in the file list, "files/2" represents the second, and so on. Notice the "compose" word used in the for loop. In the example above, the first time though the loop, the code reads as if [print files/1] had been typed in manually, etc.
"Foreach" is another useful looping structure built into Rebol. It lets you easily loop through a block of data. Its syntax is formatted as follows:
foreach {variable name referring to each consecutive item in the given block} [a given block] [block of functions to be executed upon each item in the given block, using the variable name to refer to each item successively]
The example below prints the name of every file in the current directory on your hard drive:
folder: read %.
; gets the current directory list,
; and assigns that block of filenames to the variable "folder"
foreach file folder [print file]
; loops through each file name contained in the "folder" block,
; and prints each one consecutively.
The following line reads and prints each successive message in a user's email box:
foreach mail read pop://user:pass@website.com [print mail]
"While" is other useful looping structure found in most programming languages. It repeatedly performs a conditional evaluation, and then performs a block of code while the condition is true. While loops are formatted as follows:
while [condition] [
block of functions to be executed while the condition is true
]
Here's a simple example:
x: 1 ; create an initial counter value
while [x <= 5] [
alert to-string x
x: x + 1
]
In English, that code reads "x initially equals 1. While x is less than or equal to 5, display the value of x, and add 1 to the value of x". (i.e., the program displays a count from 1 to 5)
NOTE: In Rebol, the code "x: x + 1" adds 1 to the current value of x. It's one of the most commonly used expressions in all programming, found in all sorts of looping situations. Notice also the word "to-string". It converts the number value in "x" to a text ("string") value. This is required because the "alert" word in Rebol only displays string types of data.
Here are some additional "while" loop examples:
while [not request "End the program now?"] [
alert "Select YES to end the program."
]
In the above example "not" reverses the value of data received from the user (i.e., yes becomes no and visa versa)
alert "Please select today's date" while [request-date <> now/date] [
alert join "Please select TODAY's date. It's " [now/date]]
while [request-pass <> ["secret" "password"]] [
alert "The username is 'secret' and the password is 'password'"]
The example below uses several loops to alert the user to feed the cat, every 6 hours between 8am and 8pm. It uses a for loop to increment the times to be alerted, a while loop to continually compare the incremented times with the current time, and a forever loop to do the same thing every day, continuously. Notice the indentation:
forever [
for timer 8:00am 8:00pm 6:00 [
while [now/time <= timer] [wait .5]
; don't do anything, so long as it's not time to
; feed the cat. The "wait .5" action gives the
; user a chance to close the program.
[alert join "It's now " now/time ". Time to feed the cat."]
; if the while loop has exited, it must be time to
; feed the cat. After alerting, go back and wait
; for the next meal time to arrive.
]
]
17. Working With Longer Examples
Most programs are longer than the examples shown so far in this tutorial. Below is an example of a more complex program. It allows the user to type in the Internet address of a web cam, and display streaming video from it. It displays the video images in a GUI that has several buttons used to control the size, location, and action on screen. In order to avoid typing in this program every time you run it, it can be saved as a text file. Whenever you save a Rebol program to a text file, the code must begin with the following bit of text:
REBOL []
That text tells the Rebol interpreter that the file contains a valid Rebol program. It must be included at the beginning of any saved Rebol program. You should include additional documentation about the program such as title and version info in between the brackets, but it's not required.
Type in or copy/paste the complete code source below into a text editor such as Windows Notepad. You can also use the built in Rebol text editor by typing "editor". Save it as a file called "webcam.r" on your C:\ drive.
(Note: this example includes a number of Rebol words and techniques that have not yet been included in this tutorial. The purpose of the example is simply to provide a longer text example that can be cut, pasted, and saved into a file. Don't worry if you can't understand the code.)
Rebol [Title: "Webcam Viewer"]
; try http://www.webcam-index.com for some webcam links.
temp-url: "http://type-some-website-here.com"
while [true] [
webcam-url: to-url request-text/title/default trim {
Enter the web cam URL:} temp-url
either attempt [webcam: load webcam-url]
[break]
[either request [trim {
That webcam is not currently available.} trim {
Try Again} "Quit"]
[temp-url: to-string webcam-url]
[quit]
]
]
resize-screen: func [size] [
webcam/size: to-pair size
window/size: (to-pair size) + 40x72
show window
]
window: layout [
across
btn "Stop" [webcam/rate: none show webcam]
btn "Start" [
webcam/rate: 0
webcam/image: load webcam-url
show webcam
]
rotary "320x240" "640x480" "160x120" [
resize-screen to-pair value
]
btn" Exit" [quit] return
webcam: image load webcam-url 320x240
with [
rate: 0
feel/engage: func [face action event][
switch action [
time [face/image: load webcam-url show face]
]
]
]
]
view center-face window
Once you've saved the webcam.r program to C:\, you can run it in any one of a variety of ways:
- If you've already installed the Rebol interpreter in Windows, just find the C:\webcam.r file icon in your file explorer and double click it (i.e., click My Computer -> C: -> webcam.r). The Rebol interpreter automatically executes the script. By default, during Rebol's initial installation, all files with an ".r" extension are associated with the interpreter. They can be clicked and run as if they're executable programs, just like ".exe" files. This is the most common way to run Rebol scripts, and it works the same way on all major graphic operating systems.
- Type "do %/c/webcam.r" into the Rebol interpreter.
- Scripts can be run at the command line. In Windows, copy rebol.exe and webcam.r to the same folder (C:\), then click Start -> Run, and type "C:\rebol.exe C:\webcam.r" Those commands will start the Rebol interpreter and do the webcam.r code.
- At the Windows command prompt (in a Windows DOS box), type "C:\rebol.exe C:\webcam.r"
- Create a text file called webcam.bat, containing the text "C:\rebol.exe C:\webcam.r" . Click on the webcam.bat file in Windows, and it'll run those commands.
- Use a program such as XpackerX ( http://www.marmaladefoo.com ) to package and distribute the program. XpackerX allows you to wrap the Rebol interpreter and webcam.r program into a single executable file that has a clickable icon, and automatically runs both files. That allows you to create a single file executable Windows program that can be distributed and run like any other application. Just click it and run...
- Buy the commercial "SDK" version of Rebol, which provides the best method for packaging Rebol applications.
IMPORTANT: To turn off the default security requestor that continually asks permission to read/write the hard drive, type "secure none" in the Rebol interpreter, and then run the program with "do {filename}r". Running "C:\rebol.exe -s {filename}" does the same thing . The "-s" launches the Rebol interpreter without any security features turned on, making it behave like a typical Windows program.
Being able to save, run, and distribute your programs is important. You should become very familiar with the options above.
18. Embedding Binary Data
You'll often need to use images and other binary data in your Rebol programs. For example, simple games often use graphic and sound files as part of their interface. As you've seen, there are many ways for Rebol to load in such binary data. You can read it from a hard disk, you can download it from the Internet, you can read it from just about any other local or networked storage medium, etc. When distributing your Rebol programs, however, those methods aren't always desirable. You don't want to require your user to download a number of images and sounds every time they play a simple game. They may not always have an Internet connection available. A possible alternative is to make a zip file or similar package that includes your program and all its supporting files. Installing such packages can be complicated for users, though, and that's an unnecessary step. Rebol provides a method to encode and include external files within your programs. To see how it works, use the code below:
Rebol [Title: "Binary Embedder"]
system/options/binary-base: 64
file: to-file request-file/only
data: read/binary file
editor data
Type that program into your text editor, save it as a text file in "C:\embed.r", and then run it using any of the methods described in the previous section (try typing "do %\c\embed.r" into the Rebol interpreter). When run, the program will let you select a file, read it, and then display a binary representation of its data in the built-in editor. You can copy and paste the text of that printout directly into your program, assign it a variable name, and use it as if it had been read straight from a hard drive or another storage medium.
Here's an example. Download the picture below - you can use your web browser to go to the url, and then save the image to your hard drive. Or, you can download it using Rebol, as demonstrated earlier in the tutorial:
http://musiclessonz.com/test.png
Once you've downloaded the file, use the program above to select and convert it to binary data. You should get the following printout:
64#{
iVBORw0KGgoAAAANSUhEUgAAAFUAAABkCAIAAAB4sesFAAAAE3RFWHRTb2Z0d2Fy
ZQBSRUJPTC9WaWV3j9kWeAAAAU1JREFUeJztlzEOgzAQBHkaT7s2ryZUUZoYRz4t
e9xsSzTjEXIktqP3trsPcPPo7z36e4/+3qO/9y76t/qjn3766V/oj4jBb86nUyZP
lM7kidKZPFE6kydq/Pjxq/nSElGv3qv50vj/o59++hNQM6Z93+P3zqefAw12Fyqh
v/ToX+4Pt0ubiNKZPFE6Ux5q/O/436lkh6affvrpp38ZRT/99Ov6+f4tPPqX+8Ps
/meidCZPlM7kidKZPFE6kydKZ/JE6UyeKJ3JE6UzeaJ0Jk+UzuSJ0pk8UTMmvn8L
j/7l/nC7tIkonekLdXm9dafSmeinn376D/rpp5/+vv1GqBkT37+FR/9yf7hd2kSU
zuSJ0pk8UTqTJ0pn8kTpTJ4onckTpTN5onQmT5TO5InSmTxROpMnasbE92/h0b/Q
//jR33v09x79vUd/73XvfwNmVzlr+eOLmgAAAABJRU5ErkJggg==
}
Now copy and paste that data into the Rebol interpreter and assign it a variable label, like this:
picture: load 64#{
iVBORw0KGgoAAAANSUhEUgAAAFUAAABkCAIAAAB4sesFAAAAE3RFWHRTb2Z0d2Fy
ZQBSRUJPTC9WaWV3j9kWeAAAAU1JREFUeJztlzEOgzAQBHkaT7s2ryZUUZoYRz4t
e9xsSzTjEXIktqP3trsPcPPo7z36e4/+3qO/9y76t/qjn3766V/oj4jBb86nUyZP
lM7kidKZPFE6kydq/Pjxq/nSElGv3qv50vj/o59++hNQM6Z93+P3zqefAw12Fyqh
v/ToX+4Pt0ubiNKZPFE6Ux5q/O/436lkh6affvrpp38ZRT/99Ov6+f4tPPqX+8Ps
/meidCZPlM7kidKZPFE6kydKZ/JE6UyeKJ3JE6UzeaJ0Jk+UzuSJ0pk8UTMmvn8L
j/7l/nC7tIkonekLdXm9dafSmeinn376D/rpp5/+vv1GqBkT37+FR/9yf7hd2kSU
zuSJ0pk8UTqTJ0pn8kTpTJ4onckTpTN5onQmT5TO5InSmTxROpMnasbE92/h0b/Q
//jR33v09x79vUd/73XvfwNmVzlr+eOLmgAAAABJRU5ErkJggg==
}
You'll get the response below, indicating that the image has be defined and created:
make image! [85x100 #{
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF...}
Now you can use the variable "picture" as you would any other data. Display it, save it, transfer it between networked computers, etc. Here it is in a GUI:
view layout [image picture]
The example below displays a photo of my dog, and then saves it to the hard drive as a .png image:
dog: load 64#{
iVBORw0KGgoAAAANSUhEUgAAAFkAAACNBAMAAAAuisulAAAAMFBMVEUeEw6kh3OM
TRZDSE/RxbWFal1KLiCtkoakZSXW0cm9mnlCNzq5no5oSDTg29B0UkzBjRe5AAAA
CXBIWXMAAC4jAAAuIwF4pT92AAAQ80lEQVR4nG1Yf2gjZ3r+kiW6gjHJZHdOjuLU
W0GMKRgShG7/UIVqqu2eUtd3c9pvpe6uGJ32dnqca+zhIoa6glubczCBZVsQhGkX
9UR369zgEKeqXeic0G6xERHTxLGxWFCH+WehGIKIVx2viTJ8fd9v5M2m9JN/CPx8
zzzv8/74PpkIQTUYVDUtqMFP+L22uKitaaXFktaEZZXgyzpdD0hQEFRcz+CA10pr
HIurVLKa36KpAHhhAA8CeBHIm8BcwgXA5+APCGM0GBSCQU4ML5VvaDWfLav5HDez
40dAHhSQHaHwDNUw1pB5EXiR3hqIR/S+I3FupA4iWDPWjRYXUlovIfe3SuRcp+6q
Ag92QK5ebVmGhgFqKMQq8e8Sj5LadkMQnoFBh321ZTSbmtZcszBIHuuAW5L6p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T7w2DCCDGLc+tidX3q8/egxLwJDu5fPJkNcwL0/cK
14Bz6TvcDnNyJ5MYHCZs9XkdE/9H1C1ye555P1lG43ybTpn/v3XrfwFzPoFQpggI
EQAAAABJRU5ErkJggg==
}
view layout [image dog]
save/png %dog.png dog
The code above can be shortened dramatically using the built in Rebol word "compress". Rebol's built in compression and decompression only work with text strings, so the binary embedder program provided earlier must be adjusted as follows to create compressed embedded data:
REBOL [Title: "Rebol Binary Embedder"]
system/options/binary-base: 64
file: to-file request-file/only
if not file [quit]
uncompressed: read/binary file
compressed: compress to-string uncompressed
; Note that the line above converts the binary data
; to a text string, and then compresses it.
editor compressed
alert rejoin ["Uncompressed size: " length? uncompressed
" bytes. Compressed size: " length? compressed " bytes."]
IMPORTANT: To use the compressed version of the data above, you'll need to reverse the text-binary conversion after decompressing it. To do that, use the following code:
to-binary decompress {compressed data}
So, using the variables above, the following two lines of code display the same image:
view layout [image load uncompressed]
view layout [image load to-binary decompress compressed]
Here's a complete example demonstrating the size difference between raw and compressed embedded data:
; Here's an uncompressed embedded image:
image-uncompressed: load 64#{
iVBORw0KGgoAAAANSUhEUgAAAP8AAAEsCAIAAACDt/KoAAAAE3RFWHRTb2Z0d2Fy
ZQBSRUJPTC9WaWV3j9kWeAAAB9FJREFUeJzt3bGRHEkQBMETDaKd5v8UmlmzIaoz
MGtZ4QLUIBtBLXM//0lb/bz9D5BeY/3ay/q1l/VrL+vXXtavvaxfe1m/9rJ+7WX9
2sv6tZf1ay/r117Wr72sX3tZv/ayfu1l/drL+rWX9Wsv69de1q+9rF97Wb/2sn7t
Zf3ay/q1l/VrL+vXXmD9P1ICmCh4WkoAEwVPSwlgouBpKQFMFDwtJYCJgqelBDBR
8LSUACYKnpYSwETB01ICmCh4WkoAEwVPSwlgouBpKQFMFDwtJYCJgqelBDBR8LSU
ACYKnv7w+1f8W+fyP307B+a8MxA8Xf92Dsyx/jnjIPQPvD/xixn8G4wjqH/g/Qnr
r42jfuD9CeuvjaN+4P0J66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BEwdNSApgoeFpKABMFT0sJYKLg
aSkBTJQ7LX0569de1q+9rF97Wb/2sn7tZf3ay/q1l/VrL+vXXtavvaxfe1m/9rJ+
7WX92sv6tZf1ay/r117Wr72sX3tZv/ayfu1l/drL+rWX9Wsv69de1q+9rF97Wb/2
sn7t9T/igalLLsvMjgAAAABJRU5ErkJggg==
}
; Here's a compressed version of the same data:
image-compressed: load to-binary decompress 64#{
eJzrDPBz5+WS4mJgYOD19HAJAtL/GRgYdTiYgKzm7Z9WACnhEteIkuD8tJLyxKJU
hiBXJ38f/bDM1PL+m2IVDAzsFz1dHEMq5ry9u3GijKcAy0Fh3kVzn/0XmRW5WXGV
sUF25EOmKwrSjrrF9v89o//u+cs/IS75763Tv7ZO/5qt//p63LX1e9fEV0fu/7ap
7m0qZRIJf+2DmGZoVER5MQiz+ntzJix6kKnJ6CNio6va0Nm0fCmLQeCHLVMY1Ljm
TRM64HLwMpGK/334Hf4n+vkn+1pr9md7jAVsYv+X8Z3Z+M/yscIX/j32H7sl/0j3
KK+of/CX8/X63sV1w51WqNj1763MjOS/xcccX8hzzFtXDwyXL9f/P19/f0vxz4f2
OucaHfmZDwID+P7Hso/5snw8m+qevH1030pG4kr8fhNC4f/34Z89ov+vHe4vAeut
SsdqX8T/OYUCv9iblr++f67R8pp9ukzLv8YHL39tL07o+3pekn1h/dDVBgzLU/d3
9te/Lki4cNgBmA6/lO+J/RPdzty8Rr5y94/tfOxsX6/r8xJK0/UW9vlH93/9oAzR
e09yKIUBVbT9/br/U/m7x6CU98VAAJS2ZPPF/197eEDhtfs9vX9rDzc6/v3qzUyo
nJA/dz76Y77tHw+w3gXlbEMpDKihza/+7/o/c3+DU54tDwsobR2/fXR/qYXBiV8T
t3eDEmpA/d9LDASK0y/tnz+H/Ynmt78E1vti7lAKA6pouxz/X7v+uR045ZFdRE6x
1q21pG7NiSzx1f5R40pvvdNn+oB1P4Onq5/LOqeEJgCemFy1KQgAAA==
}
view layout [image image-uncompressed]
view layout [image image-compressed]
The compressed version of the image data is exactly the same, but much smaller when included in your programs. When you use compression like that, just be sure to include the "to-binary decompress" words when loading the embedded data.
Using the short binary embedder program provided earlier, you can convert any type of file into embeddable text data. Images, sounds, videos, even entire executable programs can be included in your distributed code!
19. Modular Programming and Code Reuse
Rebol provides a fantastic array of easy to use programming tools built right into the language. As you've seen, Rebol's built-in words can accomplish useful "high-level" actions that can be the basis of simple programs. You can even use the interpreter as a powerful little swiss army knife utility that doesn't require any real programming (i.e., as a multiplatform text editor, calculator, email reader/sender, image viewer/editor, command interface for copying/pasting files, ftp uploader, etc.). For larger programs, however, the built in capabilities are merely simple building blocks. To build more complex applications, you need to _create_ new functionality by combining and using the native words, grammatical structures, and simple pieces of code. Language elements are just raw materials that can be put together to achieve more powerful and specific goals. Toward that end, a very important concept in programming is (drum roll...): the reuse of existing code.
Never re-invent the wheel. Reusing bits and pieces of existing code is essential if you want to become a productive programmer. The creation of functions is a basic way to implement code reuse - once you create a new function word to accomplish a given action, you can copy its definition and use it over and over again in your programs. That saves you the trouble of reinventing those actions every time they're needed in your programs. It also reduces the likelihood of introducing errors - old trusted code is less likely to contain bugs if it's already been tested and put to use in a variety of situations.
As demonstrated earlier, the built in word "do" opens and runs Rebol code that's been saved to a text file. You can use it to import existing modules of code, as if that code had been typed into your program. That existing code can contain function and variable definitions, new programmatic structures, and even complete programs of any length and level of complexity. Once those words and definitions have been imported into your program, you can use the included functions and variables as if they're native words in the language. You don't even necessarily need to know how they were created. Try typing in the following code example, and save it to "C:\play_sound.r".
Rebol [title: "play-sound"]
play-sound: func [sound-file] [
wait 0
ring: load sound-file
sound-port: open sound://
insert sound-port ring
wait sound-port
close sound-port
]
The code above creates a new function word "play-sound", which accepts a passed variable filename "sound-file" (that file must be a ".wav" file), and plays the sound through your computer speakers. You don't have to understand how the code works - just type it in and save it to C:\play_sound.r . Now, whenever you want to play a sound, you can include the code in your program:
do %/c/play_sound.r
And use the "play-sound" function just like any other built-in function word (the syntax is "play-sound {sound-file}"):
play-sound %/C/WINDOWS/Media/chimes.wav
Easy, right? You only have to type in the line "do %/c/play_sound.r" once in your program. After that, the word "play-sound" is defined, and you can use it wherever you need:
alert "Here's a sound:"
play-sound %/C/WINDOWS/Media/chimes.wav
alert "And here's another sound:"
play-sound %/C/WINDOWS/Media/chord.wav
alert "Now try choosing a .wav file from your hard drive:"
play-sound to-file request-file/file %/C/WINDOWS/Media/tada.wav
This whole concept becomes much more useful with the realization that (final drum roll...): You don't have to write everything yourself! There exists a community of developers around the world working to create useful pieces of code. Finding and learning to use modules of code, functions, and complete dialects created by other programmers will remain at the heart of your initial learning process, and will continue to play an important role in your efforts as a proficient developer.
Many self contained modules of Rebol code have been created to extend the built-in abilities of Rebol, and that list continues to grow as the language matures. Learning to use dialects and parts of programs created by others will make you a much more capable programmer. Existing modules of code can help you do high level, complex, and specific things with ease, so that you don't have to start from scratch in every programming effort.
In that way, learning to program is a lot like learning to use other types of technology that exist in our society. To call your friend on the phone, for example, you don't need to reinvent the telephone and all its electronic components. You don't need to manufacture any of those items, and you don't need to install miles of cable. You just need to know how to use the existing phone system. Furthermore, you can put that system to use in more complex operations that are functional to you at another level. Business owners rely on the phone to contact clients and employees, without necessarily caring how the system works. They just use it functionally as part of their higher level business goals. Existing code modules can work for programmers in a similar way. They make available high level functions that can be used to build even higher level, specific, and complex applications. You just need to know how to use them.
To reuse code modules, it's essential to first learn the language, so that you can understand code written by other developers. You may need to adjust and extend code written by others, so that it fits your needs more exactly. In most cases, however, you can just learn how to use the words in an imported module or dialect, execute a "do {filename}" command, and you're off and running with a whole new language extention. Here's an example:
The web site http://www.dobeash.com/it/ offers several free extension modules for the Rebol language. They provide a module called "RebGUI", for example, which extends the already powerful GUI syntax built into Rebol ("view layout..."). Using RebGUI, it's possible to easily display graphic widgets that aren't natively possible in Rebol. RebGUI constructs those components from the built in Rebol raw materials, and makes them reusable in your own programs. To use RebGUI, just download the files at the web site above, and unzip them to C:\ . Then type in the following code and save it as C:\rebgui_example.r. You can run it by any of the means described in the previous section ("do %/c/rebgui_example.r", etc.):
REBOL []
do %\c\rebgui.r
display "Grid" [
table #WH options [
"Day" left .5 "Time" left .3 "Name" left .3
] data [
Monday 9:00 "John" Tuesday 9:30 "Jane" Wednesday 10:00 "Bob"
]
]
do-events
The code above makes use of some new commands that aren't part of the native Rebol language. Specifically, the functions and variables "display", "table", "#WH", "options", and "data" are defined in the rebgui.r file, and they add new functionality to the Rebol language. Using those words, as defined in rebgui.r, the above code displays the given data block [Monday 9:00 "John" Tuesday 9:30 "Jane" Wednesday 10:00 "Bob"] in a resizable GUI display that can be automatically sorted by clicking on column headers in the GUI. That type of display is a common requirement in modern programs that display lists of data ("database" applications), so the added commands are a welcome addition to the Rebol language. RebGUI contains a broad collection of additional functions that are useful in building intricate GUIs. To use them, you must first understand basic Rebol syntax, and then learn how to use the RebGUI language extensions within that syntax. Once you've done that, you can simply include the "rebgui.r" file in your programs, and use those language extensions as if they're part of the language. The key is to understand that RebGUI commands are built from native raw materials in the Rebol language, and to use them, all you need to do is import rebgui.r using the "do" command.
NOTE: rebgui.r is packaged with several additional files, which are in turn imported within the rebgui.r code. Those files contain "lower level" code that actually define the new words and grammar that make up RebGUI itself. They contain code that must all stay together with rebgui.r. Just as the above program is not complete without the included rebgui.r file, rebgui.r itself is not complete without its included files. As you begin to create longer and more complex programs, your source code will often consist of many separate source files tied together to make up a whole program. Managing and remembering which included files are required in your programs becomes more of an obligation as you build applications of greater complexity.
Here are some web links containing free modules that can help you accomplish useful programmatic tasks in Rebol:
http://www.hmkdesign.dk/rebol/list-view/list-view.r - a powerful listview widget to display and manipulate formatted data in GUI applications.
http://www.dobeash.com/it/rebdb/ - a database module that lets you easily store and organize large amounts of data using the "SQL" database language. There's also a spell checker module that can be included in your programs.
http://www.rebol.org/cgi-bin/cgiwrap/rebol/view-script.r?script=rebzip.r - a module to compress/decompress zip formatted files
http://www.colellachiara.com/soft/Misc/pdf-maker.r - a dialect to create pdf files directly in Rebol
http://softinnov.org/rebol/mysql.shtml - a module to directly manipulate mysql databases within Rebol
http://www.rebol.org/cgi-bin/cgiwrap/rebol/view-script.r?script=menu-system.r - a dialect to create all types of useful GUI menus in Rebol
http://softinnov.org/rebol/uniserve.shtml - a framework to help build client-server network applications
http://www.rebol.net/demos/BF02D682713522AA/i-rebot.r http://www.rebol.net/demos/BF02D682713522AA/objective.r and http://www.rebol.net/demos/BF02D682713522AA/histogram.r - these examples contain a 3D engine module written entirely in native Rebol. The module lets you easily add and manipulate 3D graphics objects in your Rebol apps.
http://web.archive.org/web/20030411094732/www3.sympatico.ca/gavin.mckenzie/ - a Rebol XML parser library
http://box.lebeda.ws/~hmm/rswf/ - a dialect to create flash (SWF) files directly from Rebol scripts
http://www.rebol.net/docs/makedoc.html - converts text files into nicely formatted HTML files
http://www.rebol.org - the official Rebol library - full of many additional modules and useful code fragments. The first place to look when searching for Rebol source code.
Pieces of code aren't always intended strictly for modular reuse, but can be found within published open source programs. Searching for useful code sections within programs released to the public is a vital way to save time and programming effort, and to improve your capabilities as a developer. A great deal of potential work in any conceivable programming domain has already been accomplished by developers around the world. Always check for functionalities you need in related programs written by other programmers. If the examples you find are well commented and organized, you'll likely be able to reuse some of the code with limited effort. (Be on the lookout especially for useful function word definitions).
A list of Internet resources containing available source code has been included at the end of this tutorial. The programs available at those sites are a gold mine of existing code, necessary for productive development work.
Using external programs as "modules":
Remember that data is often interoperable between existing programs. Once data has been saved to a storage medium, you can pass it to other tools. The built-in Rebol word "Call" allows you to run other programs on your computer. It provides a variety of options to send command line parameters and deal with output from those programs. Using "Call", you can execute all of the built in "shell" commands included in your computer's operating system (i.e., DOS and Unix commands). You can even embed and use entire premade applications to help manipulate data in your Rebol programs. The example below opens Windows' Notepad to edit the "rebgui_example.r" text file created earlier:
call "notepad.exe c:\rebgui_example.r"
This next example opens Windows' Paint program to edit an image we downloaded earlier in the tutorial:
call "mspaint.exe c:\bay.jpg"
Here's an example that embeds an executable program into the code, writes it to the hard drive, and then runs it with the call function:
program: load to-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OBS7MnUVHoK0osoTV+22qF56c95qKdtZBzB66J/imSc
/Rmsg/KDdHFbA9O3RrZWByD/qPf1KTCwze3y2KCbn9vnP4ExoItiwr11zvncqq6+
oXGV//XVa5qCzXxL6M3ZfBfMZyFPBvywgD3FGDjLnGVl83o4T+HJAZ/PFxWTqrcj
GxerHljRqyL9sWXxqU2/nkHki1H4HDkvJeM7vZooeLdnNU2R10K34G1XdgveTmE7
vmv7fNDcFY1u3ABpNa5J6rZd9MouqGpjw6z1GLXn6vDxV/s9o1cYvcroNUanGP2J
UZ3RG4zeZPQ2o3cY/YtRqCdqZ3Qho6WMuhitYHQZ0pr6mRr21Zvv03VFuuMoX0Gd
VqT7BlupKFoXw8eo/8yynUR+HvEa4g3EPxEXYuwSxOWIaxADiGHEBKKGeADxCOIx
a1wXkE81zH/ut0OdG0LtjQ2+hCSBzLUKWoeSyErC+pickIQgfAmhgaSG319xPEvo
ioQ6Ld9D0CL04ddZQuknaxA4W1hRtXeySa0DXWM7BHjDFhHkhLUKYs2cJTcrA0H4
mmtXYgk+m1GVTBBOsVVbXJGDsNTWKexIqpqQ4aWYqgbps4LPCDFNMPcLYXQpldrC
g0bcVHcKcQ220DqyB4PTHYKWScZVgCGsw/LBEgHWsjYLZR2zRTMxWZUwfaFwOAot
SXVXTIuLM9V/ZeuSMw/UxW/s4KOF6W2GNjmp8Uo6rci8ImsZRVLxG+1hZWhgrlv6
/4F/ABcSIgQAEAAA
}
write/binary %program.exe program
call %program.exe
Copy and paste that entire block of code into the Rebol interpreter and run it. It will execute a little demo program, which was written and created by a language entirely unrelated to Rebol.
There are a world of open source applications available to perform just about every specific high level task conceivable. Many are free to use and distribute even in commercial applications (see http://sourceforge.net ). If you can interface with them on the command line, they can be used to help in your own applications - even if you don't know how they were written. This extends the power of the language, but can also restrict it's cross platform usability. For example, if you write a program that calls DOS operating system commands, that program can't be used on a UNIX system.
Whenever you use an executable or code created by another programmer, be absolutely sure to check, and follow, the licensing terms by which it's distributed.
20. A Quick Summary
The list below summarizes some key characteristics of the Rebol language. Knowing how to put these elements to use constitutes a fundamental understanding of how Rebol works:
- To start off, Rebol has hundreds of built-in function words that perform common tasks. As in other languages, function words are typically followed by passed parameters. To accomplish a desired goal, functions are arranged in order, one after another to complete a given task. Expressions are evaluated in left to right order, line terminators are not required, and empty white space (spaces, tabs, newlines, etc.) can be inserted as desired to make the code more readable. Text after a semicolon and before a new line is treated as a comment. Just knowing the predefined functions in the language and being able to organize them in a useful order will allow you get a lot of work done with Rebol.
- Many common data types are understood and handled natively by the Rebol interpreter. Numbers, text strings, money values, urls, binary representations of images, sounds, etc. are all automatically recognized. Network resources and Internet protocols (http documents, ftp directories, email accounts, dns services, etc.) can be accessed natively. Data of any type can be written to and read from virtually any connected device or resource. Remember that the percent symbol ("%") is used to refer to local file values. Because many types of values are native to Rebol, looping through data and making conditional decisions based upon user input and data content is straightforward and flexible.
- Both action code and data are stored in "blocks", which are delineated by starting and ending brackets ("[]"). Data and code contained in blocks are separated by white space. Functions contained in blocks can be evaluated (their actions performed) using the "do" word. New function words can also be defined using the "does" and "func" words. The "func" word allows you to pass your own specified parameters to newly defined function words. You can "do" a module of code contained in a text file, as long as it contains the minimum header "Rebol[]". Data contained in blocks can be of any type, and blocks can be automatically treated as lists of data, called "series". Series can be manipulated using built-in functions that enable searching, sorting, ordering, and otherwise organizing the blocked data.
- The syntax "view layout [block]" is used to create basic GUI layouts. Add words to the block to create graphic widgets: "button", "field", "text-list", etc. Add color, position, spacing, and other facet words after each widget word. Add action blocks after each widget to perform functions when the widget is activated (i.e., when the widget is clicked with a mouse, the enter key pressed, etc.). Refer to items in the GUI layout using path refinements (i.e., "face/offset" refers to the position of the selected widget). Those simple guidelines can be used to create useful GUIs for data input and output.
- Any data or code can be assigned a word label. Word labels are assigned to values, variables, functions, expressions, and blocks of any type, using the colon character (":"). Once assigned, variable words can be used to represent all of the functions and data contained in the given expression, block, etc. Just put a colon at the end of a word, and thereafter it represents all the following actions and/or data. That forms a big part of Rebol's language structure, and is the basis for it's flexible natural language dialecting abilities.
21. 8 Complete Rebol Programs For You To Study
The following programs use a number of techniques explained throughout the tutorial. They demonstrate how pieces of code can be put together to construct complete applications, and they provide some guidance as to how basic language building blocks can be assembled for useful purposes. There are detailed line-by-line explanations of each program included in the comments (it's amazing how short these programs are without the comments!). Studying these examples is perhaps the most valuable part of the tutorial.
A downloadable package of all the demo programs is available at: http://musiclessonz.com/rebol_tutorial_examples.zip. The zip file contains screenshots, separated source code, and packaged executables (clickable ".exe" files) of each example. The XpackerX XML files used to create each executable are also included.
NOTE: In order to fit the code examples within the width of a web page, certain formatting options have been introduced. Long lines of code have been shortened, so they they don't get chopped off when printed. The built in word "trim" has been used to clean long strings of text that are written on separate lines.
mystring: trim {
This string}
is the same as:
mystring: "This string"
"Join" has also been used to join together long sections of text:
join "All " [
"the "
"same "
"line of text."
]
is the same as:
"All the same line of text."
Blocks have also been written onto multiple lines where necessary:
myblock: [This
block
]
is the same as:
myblock: [This block]
To run the example programs in this section, you can use any of the methods described earlier: type or copy/paste the code into the Rebol interpreter, save the above program as a text file and run it using "do {filename}", pack it with XpackerX, etc.
21.1 Little Email Client
The first example is a complete graphical email client that can be used to read and send messages. The code is heavily commented to provide line-by-line explanations of how each element works:
Rebol [Title: "Little Email Client"] ; (required header)
view layout [
; The first line creates the GUI layout
h1 "Send Email:"
; This line adds a text label to the GUI
address: field "recipient@website.com"
; This line creates a text entry field, containing
; the default text "recipient@website.com". It assigns
; the variable word "address" to the text entered here.
subject: field "Subject"
; another text entry field for the email subject line
body: area "Body"
; This creates a larger, multiline text entry area for
; the body text of the email.
btn "Send" [
; A button with the word "send". The functions
; inside this action block are executed whenever
; the button is clicked.
send/subject to-email address/text body/text subject/text
; This line does most of the work. It uses the
; built-in Rebol word "send" to send the email. The
; send function, with its "/subject" refinement
; accepts three parameters. It's passed the current
; text contained in each field labeled above
; (referred to as "address/text" "body/text" and
; "subject/text"). The built-in "to-email" function
; ensures that the address text is treated as an
; email data value.
alert "Message Sent."
; alerts the user when the previous line is complete.
]
h1 "Read Email:"
; Another text label
mailbox: field "pop://user:pass@website.com"
; Another text entry field. The user's email account
; info is entered here.
btn "Read" [
; An additional button, this time with an action
; block that reads messages from a specified mailbox.
; It only takes one line:
editor read to-url mailbox/text
; The built-in "to-url" function ensures that the
; text in the mailbox field is treated as a url.
; The contents of the mailbox are read and displayed
; in the built-in Rebol editor.
]
]
Here's the same code, without comments - it's very simple:
Rebol [Title: "Little Email Client"]
view layout [
h1 "Send Email:"
address: field "recipient@website.com"
subject: field "Subject"
body: area "Body"
btn "Send" [
send/subject to-email address/text body/text subject/text
alert "Message Sent."
]
h1 "Read Email:"
mailbox: field "pop://user:pass@website.com"
btn "Read" [
editor read to-url mailbox/text
]
]
21.2 FTP Chat Room
The second example is a simple chat application that lets users send instant text messages back and forth across the Internet. It includes password protected access for administrators to erase chat contents. It also allows users to pause activity momentarily, and requires a username/password to continue ["secret" "password"]. The chat "rooms" are created by dynamically creating, reading, appending, and saving text files via ftp (to use the program, you'll need access to an available ftp server: ftp address, username, and password).
Rebol [title: "FTP Chat Room"] ; required header
webserver: to-url request-text/title/default trim {
Web Server Address:} {ftp://user:pass@website.com/chat.txt}
; get the url of a webserver text file to use for the chat.
; The ftp username, password, domain, and filename must be
; entered in the format shown.
name: request-text/title "Enter your name:"
; get the user's name
cls: does [prin "^(1B)[J"]
; "cls" now clears the screen (explained earlier in the tutorial).
write/append webserver join now [
": " name " has entered the room." newline
]
; The line above writes some text to the webserver
; the "/append" refinement adds it to the existing
; text in the webserver file (as opposed to erasing
; what's already there). Using "join", the text
; written to the webserver is the combined value of
; {the user's name}, some static text, the current
; date and time, and a carriage return.
forever [
current-chat: read webserver
; read the messages that are currently on the webserver,
; and assign the variable word "current-chat"
cls ; clear the screen using the word defined above
print join "--------------------------------------------------" [
newline {You are logged in as: } name newline
{Type "room" to switch chat rooms.} newline
{Type "lock" to pause/lock your chat.} newline
{Type "quit" to end your chat.} newline
{Type "clear" to erase the current chat.} newline
{Press [ENTER] to periodically update the display.} newline
"--------------------------------------------------" newline]
; displays a greeting and some instructions
print join "Here's the current chat text at: " [webserver newline]
print current-chat
sent-message: copy join name [
" says: " entered-text: ask "You say: "
]
; get the text to send, then check for commands below
; ("quit", "clear", "room", "lock", and [ENTER])
; The built-in word "ask" requests some info within
; the interpreter.
switch/default entered-text [
"quit" [break]
; if the user typed in "quit",
; stop the forever loop (exit the program)
"clear" [
if/else request-pass = ["secret" "password"] [
write webserver ""] [alert trim {
You must know the administrator
password to clear the room!}
]
]
; if the user typed in "clear", erase the
; current text chat. But first, ask user
; for the administrator username/password
"room" [
write/append webserver join now [
": " name " has left the room." newline]
webserver: to-url request-text/title/default {New Web
Server Address:} to-string webserver
write/append webserver join now [
": " name " has entered the room." newline
]
]
; if the user typed in "room", request a new
; webserver address, and run some code that was
; presented earlier in the program,
; using the newly entered "webserver" variable,
; to effectively change chat "rooms".
"lock" [
alert trim {The program will now pause for 5 seconds.
You'll need the correct username and password
to continue.}
pause-time: now/time + 5
; assign a variable to the time 5 seconds from now
forever [if now/time = pause-time [
; wait 5 seconds
while [request-pass <> ["secret" "password"]] [
alert "Incorrect password - look in the source!"
]
; don't go on until the user gets the password right.
break
]
]
; exit the forever loop after 5 seconds have passed
]
] [if entered-text <> "" [
write/append webserver join sent-message [newline]]]
; default case: as long as the entered message is not
; blank ([Enter]), write the message to the web server
; (append it to the current text)
]
; when the "forever" loop is exited, do the following:
cls print "Goodbye!"
write/append webserver join now [
": " name " has closed chat." newline]
wait 1
The bulk of the program runs within a "forever" loop, and uses a conditional "switch" statement to decide how to respond to user input. This is a classic structure that can be adjusted to match a variety of generalized situations in which the computer repeatedly waits for and responds to user interaction.
21.3 Image Effector
The next application creates a GUI interface, downloads and displays an image from the Internet, allows you to apply effects to it, and lets you save the effected image to the hard drive. In the mix, there are several routines which get data, and alert the user with text information.
REBOL [Title: ""]
; header is still required, even if a title isn't included
effect-types: ["Invert" "Grayscale" "Emboss" "Blur" "Sharpen"
"Flip 1x1" "Rotate 90" "Tint 83" "Contrast 66"
"Luma 150" "None"]
; this creates a short list of image effects that are built
; into Rebol, and assigns the variable word "effect-types"
; to the block
do %/c/play_sound.r
; The line above imports the simple "play-sound" function
; created earlier in the tutorial. For this program to work
; correctly as it is, the play_sound.r file should be saved
; to C:\
image-url: to-url request-text/title/default {
Enter the url of an image to use:} trim {
http://rebol.com/view/demos/palms.jpg}
; ask user for the location of a new image (with a default
; location), and assign it to the word "new-image"
gui: [
; The following code displays the program menu, using a
; "choice" button widget (a menu-select type of button
; built in to Rebol). The button is 160 pixels
; across, and is placed at the uppermost, leftmost
; pixel in the GUI (0x0) using the built-in word "at".
; The action block for the button contains various
; functions to be performed, based on the selected choice
; (using conditional "if" evaluations. This could have
; been done with less code, using a "switch" syntax.
; "If" was used, however, to demonstrate that there are
; always alternate ways to express yourself in code -
; just like in spoken language.).
across
; horizontally aligns all the following GUI widgets,
; so they appear next to each other in the layout
; (the default behavior in Rebol is to align elements
; vertically).
space -1
; changes the spacing of consecutive widgets so they're
; on top of each other
at 20x2 choice 160 tan trim {
Save Image} "View Saved Image" "Download New Image" trim {
-------------} "Exit" [
if value = "Save Image" [
filename: to-file request-file/title/file/save trim {
Save file as:} "Save" %/c/effectedimage.png
; request a filename to save the image as,
; defaults to "c:\effectedimage.png"
save/png filename to-image picture
; save the image to hard drive
]
if value = "View Saved Image" [
view-filename: to-file request-file/title/file trim {
View file:} "Save" filename
view/new center-face layout [image load view-filename]
; read the selected image from the hard drive
; and display it in a new GUI window
]
if value = "Download New Image" [
new-image: load to-url request-text/title/default trim {
Enter a new image url} trim {
http://www.rebol.com/view/bay.jpg}
; ask for the location of a new image,
; and assign it to the word "new-image"
picture/image: new-image
; replace the old image with the new one
show picture ; update the GUI display
]
if value = "-------------" [] ; don't do anything
if value = "Exit" [
play-sound %/c/windows/media/tada.wav
quit ; exit the program
]
]
choice tan "Info" "About"
[alert "Image Effector - Copyright 2005, Nick Antonaccio"]
; a simple "about" box
below
; vertically aligns successive GUI widgets -
; the opposite of "across"
space 5
; spread out the widgets some more
pad 2
; put 2 pixels of blank space before the next widget
box 550x1 white
; draws a line 550 pixels wide, 1 pixel tall
; (just a cosmetic separator)
pad 10
; put some more space between widgets
vh1 "Double click each effect in the list on the right:"
; a big text header for the GUI
return
; advances to the next row in the GUI
across
picture: image load image-url
; get the image entered at the beginning of the program,
; and give it a label
text-list data effect-types [
current-effect: to-string value
picture/effect: to-block form current-effect
show picture
]
; The code above creates a text-list gui widget
; and assigns a block of actions to it, to be run whenever the
; user clicks on the list. The block of actions is indented
; and each action is placed on separate line for readability.
; The first line assigns the word "current-effect" to the value
; which the user has selected from the list. The second line
; applies that effect to the image (the words "to-block" and "form"
; are required for the way effects are applied syntactically.
; The third line displays the newly effected image. The "show"
; word is _very_ important. It needs to be used whenever a GUI
; element is updated.
]
view/options center-face layout gui [no-title]
; display the gui block above
; "/options [no title]" displays the window without a title bar
; (so it can't be moved around),
; and "center-face" centers the window on the screen
The built-in words included in the image effector program are: rebol[], invert, grayscale, emboss, blur, sharpen, flip, rotate, tint, contrast, luma, none, vh1, across, image, load, text-list data, to-string, to-block, to-image, to-url, value, form, show, return, button, save/png, alert, view/new, center-face, layout, at, now/time, forever, while, request-pass, request-text/title/default, request-file/title/file/save, break, quit, center-face, space, box, pad.
The image effector also imports and uses the "play-sound" module created earlier in the tutorial. It plays a sound when the program is shut down.
21.4 Sliding Tile Game
Here's a simple example that implements a GUI version of the classic sliding tile game ... in only 8 lines of code. The board contains 15 movable pieces and 1 empty space. The point is to rearrange the pieces in inverse numerical order on the grid. A piece can only be moved if the empty space is adjacent to its border. Click on a piece to slide it into the empty space.
Rebol [Title: "Sliding Tile Game"]
gui: [
; Define some basic layout parameters. "origin 0x0"
; starts the layout in the upper left corner of the
; GUI window. "space 0x0" dictates that there's no
; space between adjacent widgets, and "across" lays
; out consecutive widgets next to each other:
origin 0x0 space 0x0 across
; The "style" word below allows you to redefine the
; appearance and action characteristics of any built-
; in GUI widget. The section below creates a newly
; defined button style called "piece", with an action
; block that swaps the current button's position with
; that of the adjacent empty space. That action is
; run whenever one of the buttons is clicked.
style piece button 60x60 [
; The line below checks to see if the clicked button
; is adjacent to the empty space. The "offset"
; refinement contains the position of the given
; widget. The word "face" is used to refer to the
; currently clicked widget. The "empty" button is
; defined later (at the end of the GUI layout).
; It's ok that the empty button is not yet defined,
; because this code is not evaluated until the
; the entire layout is built and "view"ed:
if not find [0x60 60x0 0x-60 -60x0]
face/offset - empty/offset [exit]
; In English, that reads "subtract the position of
; the empty space from the position of the clicked
; button (the positions are in the form of
; Horizontal x Vertical coordinate pairs). If that
; difference isn't 60 pixels on one of the 4 sides,
; then don't do anything." (60 pixels is the size of
; the "piece" button defined above.)
; The next three lines swap the positions of the
; clicked button with the empty button.
; First, create a variable to hold the current
; position of the clicked button:
temp: face/offset
; Next, move the button's position to that of the
; current empty space:
face/offset: empty/offset
; Last, move the empty space (button), to the old
; position occupied by the clicked button:
empty/offset: temp
]
; The lines below draw the "piece" style buttons onto
; the GUI display. Each of these buttons contains all
; of the action code defined for the piece style above:
piece "1" piece "2" piece "3" piece "4" return
piece "5" piece "6" piece "7" piece "8" return
piece "9" piece "10" piece "11" piece "12" return
piece "13" piece "14" piece "15"
empty: piece 200.200.200 edge [size: 0]
]
; Display the whole GUI block, centered on the user's screen:
view center-face layout gui
Here's the whole program without any comments, and reduced to a more compact format. It's tiny:
Rebol [Title: "Sliding Tile Game"]
view center-face gui: layout [
origin 0x0 space 0x0 across
style p button 60x60 [
if not find [0x60 60x0 0x-60 -60x0]
face/offset - empty/offset [exit]
temp: face/offset face/offset: empty/offset
empty/offset: temp
]
p "A" p "B" p "C" p "D" return p "E" p "F" p "G" p "H" return
p "I" p "J" p "K" p "L" return p "M" p "N" p "O"
empty: p 200.200.200 edge [size: 0]
]
Be sure to take a break from coding and play a few games :)
21.5 Guitar Chord Diagram Maker
The fifth example is a program that creates, saves, and prints collections of guitar chord fretboard diagrams. It demonstrates some more common and useful file, data, and GUI manipulation techniques:
Rebol [Title: "Guitar Chord Diagram Maker"]
; load embedded images:
fretboard: load 64#{
iVBORw0KGgoAAAANSUhEUgAAAFUAAABkCAIAAAB4sesFAAAACXBIWXMAAAsTAAAL
EwEAmpwYAAAA2UlEQVR4nO3YQQqDQBAF0XTIwXtuNjfrLITs0rowGqbqbRWxEEL+
RFU9wJ53v8DN7Gezn81+NvvZXv3liLjmPX6n/4NL//72s9l/QGbWd5m53dbc8/kR
uv5RJ/QvzH42+9nsZ7OfzX62nfOPzZzzyNUxxh8+qhfVHo94/rM49y+b/Wz2s9nP
Zj+b/WzuX/cvmfuXzX42+9nsZ7OfzX4296/7l8z9y2Y/m/1s9rPZz2Y/m/vX/Uvm
/mWzn81+NvvZ7Gezn8396/4l2/n+y6N/f/vZ7Gezn81+tjenRWXD3TC8nAAAAABJ
RU5ErkJggg==
}
barimage: load 64#{
iVBORw0KGgoAAAANSUhEUgAAAEoAAAAFCAIAAABtvO2fAAAACXBIWXMAAAsTAAAL
EwEAmpwYAAAAHElEQVR4nGNsaGhgGL6AaaAdQFsw6r2hDIa59wCf/AGKgzU3RwAA
AABJRU5ErkJggg==
}
dot: load 64#{
iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAACXBIWXMAAAsTAAAL
EwEAmpwYAAAAFElEQVR4nGNsaGhgwA2Y8MiNYGkA22EBlPG3fjQAAAAASUVORK5C
YII=
}
; Gui Design:
; The routine below was copied from
; http://rebol.com/how-to/feel.html
movestyle: [
engage: func [face action event] [
if action = 'down [
face/data: event/offset
remove find face/parent-face/pane face
append face/parent-face/pane face
]
if find [over away] action [
face/offset: face/offset + event/offset - face/data
]
show face
]
]
; With that defined, adding "feel movestyle" to any widget
; makes it movable within the GUI. It's very useful for all
; sorts of graphic applications... If you want to pursue
; building graphic layouts that respond to user events, learning
; all about how "feel" works in Rebol is very important. See
; the URL above for more info.
gui: [
backdrop white
; makes the GUI background white
currentfretboard: image fretboard 255x300
; show the fretboard image, and resize it
; (the saved image is actually 85x100 pixels)
currentbar: image barimage 240x15 feel movestyle
; Show the bar image, resize it, and make it movable.
; Notice the "feel movestyle". Thats' what enables
; the dragging.
text "INSTRUCTIONS:" underline
text "Drag dots and other widgets onto the fretboard."
across
text "Resize the fretboard:"
tab
; "tab" aligns the next GUI element with a predefined
; column spacer
rotary "255x300" "170x200" "85x100" [
currentfretboard/size: to-pair value show currentfretboard
switch value [
"255x300" [currentbar/size: 240x15 show currentbar]
"170x200" [currentbar/size: 160x10 show currentbar]
"85x100" [currentbar/size: 80x5 show currentbar]
]
]
; The rotary button above lets you select a size for the
; fretboard. In the action block, the fretboard image is
; resized, and then the bar image is also resized,
; according to the value chosen. This keeps the bar size
; proportioned correctly to the fretboard image.
; After each resize, the GUI is updated to actually display
; the changed image. The built-in word "show" updates the
; display. This needs to be done whenever a widget is
; changed within a GUI. Be aware of this - not "show"ing
; a changed GUI element is an easily overlooked source of
; errors.
return
button "Save Diagram" [
filename: to-file request-file/save/file "1.png"
save/png filename to-image currentfretboard
]
; The action block of the above button requests a filename
; from the user, and then saves the current fretboard image
; to that filename.
tab
; The action block of the button below prints out a user-
; selected set of images to an html page, where they can be
; viewed together, uploaded the Internet, sent to a printer,
; etc.
button "Print" [
filelist:
sort request-file/title "Select image(s) to print:"
; Get a list of files to print.
html: copy "<html><body>"
; start creating a block that holds the html layout,
; and give it the label "html".
foreach file filelist [
append html rejoin [
{<img src="file:///} to-local-file file {">}
]
]
; The foreach loop builds an html layout that displays
; each of the selected images.
append html [</body></html>]
; finish up the html layout. Now the variable "html"
; contains a complete html document that will be
; written to the hard drive and opened in the default
; browser. The code below accomplishes that.
write %chords.html trim/auto html
browse %chords.html
]
]
; Each of the following loops puts 50 movable dots onto the GUI,
; all at the same locations. This creates three stacks of dots
; that the user can move around the screen and put onto the
; fretboard. There are three sizes to accommodate the resizing
; feature of the fretboard image. Notice the "feel movestyle"
; code at the end of each line. Again, that's what makes the
; dots draggable.
loop 50 [append gui [at 275x50 image dot 30x30 feel movestyle]]
loop 50 [append gui [at 275x100 image dot 20x20 feel movestyle]]
loop 50 [append gui [at 275x140 image dot 10x10 feel movestyle]]
; The following loops add some additional dragable widgets to
; the GUI.
loop 6 [append gui [at 273x165 text "X" bold feel movestyle]]
loop 6 [append gui [at 273x185 text "O" bold feel movestyle]]
view layout gui
21.6 Listview Database
This example is a simple database program with a GUI interface. It let's you store, search, alter, organize and display data in a way that's useful. This type of application is common in any situation where categorical information must be organized. Although this example handles client schedule information, the techniques it demonstrates can be directly applied to managing information of any type - inventories, customer/vendor lists, payroll/tax records, mp3 collections, photo albums, high score tables in games, etc. This program uses the listview module found at http://www.hmkdesign.dk/rebol/list-view/list-view.r, which is compressed and embedded within the script. The listview module handles all the main work of displaying, sorting, filtering, altering, and manipulating data, with a familiar user interface that's easy to program. Documentation is available at http://www.hmkdesign.dk/rebol/list-view/list-view.html. Learning to use the listview module is absolutely essential if you intend to do any programming that involves information management. It's very simple to learn - you can get the basics in one sitting, and you will use it repeatedly.
The program below creates a default database if one doesn't already exist, and makes a date stamped backup of the previous file whenever data is saved. There's an additional function that traps the GUI close button to keep the program from being shut down accidentally.
Rebol [title: "Database"]
; The function below watches for the GUI close button, to keep
; the program from being shut down accidentally. The code was
; adjusted from an example at:
; http://www.rebolforces.com/view-faq.html
evt-close: func [face event] [
either event/type = 'close [
inform layout [
across
Button "Save Changes" [
; when the save button is clicked, a backup data
; file is automatically created:
backup-file: to-file rejoin ["backup_" now/date]
write backup-file read %database.db
save %database.db theview/data quit
]
Button "Lose Changes" [quit]
Button "CANCEL" [hide-popup]
] none ] [
event
]
]
insert-event-func :evt-close
; The code below is the list-view.r module in compressed format.
; It's decompressed, and then imported with the "do" command.
; Like any other module, you don't have to understand how it was
; programmed (the uncompressed code is all just native Rebol).
; You just have to include the compressed blob, and learn how
; to use it...
do decompress #{
789CCD3D6B73E3C871DFF52BE6369592F67629905A9F73A6B2A7B2EFECD8E557
2AE738A96221551031142181040D8092B8A9FC97FCD474F7BCBA07038ADA3B3B
E6D56909CCABA7BBA75FD333FCB75FFEE28FBF538B33A5FE54F5B59EAB377FFE
CD77EA77BFF9FE4F933FFFE697FFA17E552CF51B28FD558585FF58575D3F79AC
F4D3650B2F7FBEEFD74D3B578B37BFD6DBB67A50BFAF1E0A5DABDFB6504D6F3B
BD7D9343B56F9BDDA1ADEED63DF47E359D7EF55EC1DF9FAA89FAF5EF7FABBED3
5D75B7C521BE6D75D1EB728EA55F4D665793AB9FC1DBEFE01DBDFAE9643A9B5C
CDE0D59F75DB55CD76AEA697D3CBABAFE1CDEFAA250E3757FF0D0F4AFDE2FBEF
D4C5D3D3D365B383D7CDBE5DEACBA6BDCB6A53ADCB6EBB72621F2E77EBDD5B6A
F5EF9D5645AF0E505F354F5B059378B88492FF81FFFF75DFEE1A1AE05FF456B7
45AD76E68D428C2042D453D5AFD5A6D81ED40AE6B16F75A7564DABF650A7DA2A
C0EA2576F487A6F760FE695D411D40AC827F8BC7A2AA8BDB1A619853F1BAEF77
F32CC379AC370F25E1E9B27CC85A7DDBD499A74426690218D39B4615DB52F5BA
EB9705CCF787765E428FAEF36609DDB59A7509B3DB140FBA6C965738E30DE0F0
F3A7D03FF79FD972DD6FEACF6B3A013AF44D7BF8FCC17D0F1608A4F4AFCDAB39
2D2EA5BED740F71FDC717E969F9D75FDA1AE3EE96C53C02A6BA97F6AB0AA740D
CBE7177FFC4FC38C66E00EAAC25279BE9AD2A32EEFE01109465FD5C294CF9E67
4AAF567A098BF4BCBAD58FB08C974D8DABFBEA27B0CACCFF3975317C4FAF57CD
161A6F9BADA6C75DD1167620FCAA164F6DB1BB99AB555177DA74B4AA8BBB0E10
4480ABBEB8BDD5A56A352C9EAD6AB693FD76D52CF71D30D86EDFDB165AD7B6D3
65FF3CE9F5739FE9B2EAD56D050C6F26ACA08BB22D9E60A8FD7609BD830853C5
B257B05E735745A96AA58ABA560B605FAA714ED3EAD46DDD2C1F6EE85D665EE5
2AB452AC60AE76D5F281D7343DFDF337808B65514FF0C9B7CCCFE4BFA5EE09DB
1E614A358FBA152FF4F6AE4072451301EA6C7B3695FA71AEEE748F2B713537F0
00CAA1CE24FEAECED9836FDF01BB2CD7D4359F69895290BF008080AF80E5F45F
F6456D70A4904DBFCCC284D562BFAD51DA1375F2A8036413A069B66D26DDBA79
A22EF2A886E9710980027A9AD5AAD3D05733A117664C4240668AA2C6BE57F69E
0F80388E60024ED836FDC44E8A8DFEF2E083D999BE6C276B40834145D7172D54
4EBE9EF321635CA841577A5B8A16F3410BF56AA4A5D12611C7BF3FE843CC18B0
0889E06C2CCF93C478C05DA0B463C41BA0B0BF8FEA1FDEFCD7E4CD00A382E506
70C3B2D7CFC089B8FC138CBFD5C349E1A78695D5AF6F469AC420E05250AB0C06
A9BAF584A69A980DA068A42148B663AD4E40B9141E3962D5C93F75DEE97A7566
CA480FE0DB97D48011D62448F1AB9FB2D50633608802D6BF91460A358EB6DF0B
6043B0BDCE6BBDEA9D2AB8AD8BE5C35900F015B29F8404BDE95B90726801BA8A
F4B62DB64102EE8AAA9D29FC7BA5B21A458E6AD50EDE5C0539B39BC1C8801F56
D7955C214CCF891298C2128C54B5707846CB0D2ACE32EC1BFECC184F60D901CB
AEB0EC0ACB2A3024617DF7601201404537C1CEA18383A79B6A197A56FBBAB654
62EA72ABD9235374F8D50F6F5404D307CD63EB50B10245404A4EFD78AA019628
8EC019BB7ECC108AC952FB59505F7E56AC2E6958D7A06D9EEC0AC065411A3B51
543FF22500E353E78139A2D579ADBE47D1D5374DADFA6A37B27AAEDD572B4D08
AEF056D124091898172C0754E1386C59F4C5B5AA9BED1DC8DD667FB70E6D72D1
DEB49D23136BFE7E25D06DBB9F87CE79DD6BB009EAE26015231FC9F58EEC11DB
11A336422FF9023919987355FEC8EC615029308116D404451108F5B0922D4780
83B440003FAA733230ECF7A2063F039E39F1A5FDB4906C01E3A04C4517E48678
6C5D959AC46CCC3F25C377C43DB05AE67EBD9EA64B18ED784F82B194F80029B2
6E5DAD62AD4BA800C0417E832538D934A5464C6CF6755FA991F7B852621BC154
45C64A16401FBA9C80A4DE6FA4EA19DA66D0C0CA5B12876047EF61C2D63C8C20
1AEA3A03F86258A01C8A13255285C31868DA7878BBC41CC6E76B3E40D3C1FB7C
685779741E8518A47B18EEE8349C49319842B2769A8992A0C6CF4EC1396265B3
F0F52AAE6B55DE297523E22FE2719D92EC1A5075963586988BA8899527F48E34
E66BAA5F0DAAC7101DD3D2BC0E703294B6465727C8C1D5B7C302D3E202AE6C7E
1892ECB845878A0298B2AB6EABBAEA0FA0AB9461695528E43F80B05096B76F01
BFBAEFABED9D0279026F9BB6ACB6A0F8B0D527DD36D09769F5D4ECC1695E178F
9A6A3E633B57E9BDEAF6E8D07560F0FD945AD8EEA3468774A30FCF5368D46378
CA34D075B5B15040ABAD06371DF1EA56E135859B6E8B4EDB89811EEF804F6A5D
1FC09167B3E8A82621B8734380BAC3316E01E5A0C43072A61E4D84EFF2F2122A
B560FA57184E6BF5535B010CCD8AC020610CCF6D417643DBEC016DB1A9FF1253
0FC58BF5EA8792084898CD86CD9BBA9C5844CC5F12C5B1F21F679A11971C3FC6
2D77961559FC43AB8ABF068B2A9EB6D3B6A11F7C33D2575C34E82F02DD982FE9
6234188CC2DFEFD4A2450F83D011A935F20CFBAC6CF6A0DE27CB1A293270DA53
96C6005B29854CE4307D53D77C76290FCE2162A40914BD20A89C99B42A85D992
1BF50FF81AD82D273A82F41759095C3BE32E2146A7DC1B442E9EAB5FFDFCDB5F
7277705D8238873F93DB7E6BBEAC2A30DFFDD3B269B7DA96D65D4F7F262B0C2F
764B68A94BEEBB18A7F1C314BD4B034659B5FD01FD38E8D41AC5F89A50C71A8E
0423A7CF53E759CE7E32BDB4FFC7F14933F96B0A21BC574D59BEB7D2E73D88A2
E5C31D08846D6986A5E01C389B2C64A9AEAEE0DF0FF0FFD554CDBE867FA730CA
D7533EA20877960D88A08535F3705E18B76C3BEB037760A417E1C1B4EC76C512
24F9C4F600D2AE2D6D0D2A47D181C47322C60AA9C42B60C06AA9DDD3B2814712
B27327D1B02B29AF4C90883FAF7551A2DFE69E3D19B0F15355F66B78E9FEDD3D
FB57A21E460BE09DFD2714451EA12FE80F3BCD2B3A478EFBF8655BDCF1E755D5
33A601866BEA1ABA2778E6C8E293B536FB49369A0153F2D0AE2786E9A69C04C4
533C9EE011ED31081A0698CF2B8181562013C03D300EB66B99C38F9C402B9A55
5C4FCC3C78BD60557828E815DAD748DAA25B824B087FB60DBEB735820D8E753A
985DADADED8D7641B06B0DB083568E1179479E0CBE5F7AB0668A2D78B4BEA7AF
87EB1B57406818E60012C01802AE529899E5830AC39FBD9E60012337185DDB3E
3069C096779E85C6F55F3CA64D1D74970DC5AB12D06B14B7571782DB802ACDD3
0D0301390CB51217713D182E456BF8DF8AACEED0F57A93B9A2CC44417EFC685A
B1240368821EBE67946AB323BC19B7DFB8B6CDED3DE0FC8BDC3976A6A67E3635
8D00B390D1046CFDC00F2BD68C948799C06351EFB5FD3E658BC58DDE7DCA9DE8
EE3EA9FD0EA071513EC26D2C3C0DC2A109AD0858CCADCE1764368096C9D9F085
E5A028463457914982032455B30B8E0F43B7731545652DB1A901ED019168BE71
B063549F74387ED19B1DA837921F14EE777B466635C05B9884D54EDBE2B1BA33
162A228BA4F699957250DBF0AE208D0B789B31409881890B4BA5459F60E1D97D
C6E2210497F351C3CA083ECB2CC4210DF2199D77AD7E044FE74E7350E8253C67
DD43B523CA077E60CD8C394B3CC06AD21F1B7B22D670ABE1D3DC4D2E5426BEC9
171EC2E45CFC4C8FE286194CE8F3CE5C733581A1830565D5368260FC4EE674BA
165B7D87DE4E34E724FEB6A0EFFE8E118181021B9948CCF39DC40C6B6807409F
F8E638AE60C94B039B0CA15091DBB1B2E2098460E025CDE0117AC4FCEC8834CE
CFA4B35F5A8C0CC085503C6C62FEFDA9241A92274D9131B40A1C48CF00DDF63E
E84A3133E79378960739B64293F9668C18E8AE582CF1419CA524442522E5C637
74C2D020011110C942B22C3986AC85967AF7E68D159F5444115CBB3B445B1F45
DF23CA7153268496785CA971E6B9EB5CA24720250CE2E722DA5AE3CDC7C5C40B
D0D467D7185AA76885896C204B6816F2C0221E1B6A566AB9060B887044639890
09E86B1B13DAB8584EB7D3CB6A552DCF184060FE16308752AF0A30032795AAD4
93B23C073A1BDEA9BB8961B8208E1273F17112CBC481A45110DDF536F76C0C64
DA156D87012BC7ED92F7597E83EFDF75A3BE5153B138AE0943B4606DD8E7B6EA
3168B5D9C36AB8B586EFAEAE2815E9F6A0BE668DCD8C9D0B65FE1502C8E3C95A
74A6EF2FD48587E79DFA1AD44778CE32F5F5DBB7AC9327675B9ACF7EDB5771C0
C186172DFE9F4419347F8241645CE909D0E0464C8A3FC4CB1AD882B2AF688582
58DB217291BD040676BAE8D5BDA74E620FC42DAE0D2C422ACFE6F7A2821FCB32
5E0992B332CB371ECF8FB8DD6F025117B80B43CBD070C05D36C772B76D5A61ED
28A4042D2AF5D1E16C9065E11C38CFE7D76A59EBA28546CB624F296FEA767FF7
45DC8E057A39D72B3961117A11159964B6EBC46B22F9C28A3C63E1C29C6F982F
61340FD9E4B45A2B34B0D9AE2D17246CFD71A33320049D7A87F42A22339FEE22
57D508AC8AC61F59F9CCED65A30201B878E5110BDEC2D77F990F39AC088EDF61
B01C99CD2B68EEDD767833B40622D43DF72D98F699A1309F8EF74D69E311FC6B
0E08D62033C180F121AD8C7DAD0CEC608DE22EAEAD3EA82B8FEF0017D79CC0DD
DA7182D0CDE958C3D0B33E3781015BEAF9CAC9F5A6D5C572AD7618D1B3992DB0
AE761939A038771B0004D599B9E01A59330EA1C695F3DD4596965D18C69F264E
76695D47795918440CF56E071577454DFC30693BE5AED4FAF061DAF9822FC7C0
AB2BD1E7C0D4A0CDFE68044669320DA6ECC549B60AB7DB22B2707EADB6A8E131
C6C22C9484C0712D6C7A99CB9E5B10FF2C5B698EF513670C58F73C9A550A7801
35233B6754D7AB60D5CFB0DC1203B0488065A5AECE07A66A6C63FB67261E1306
32DFB5065A9F47B2292E268BE27829C622BC07C2B229B8B91D7AA8A5B01C1AD6
BE2AF11ABA6C5D0D56C74C0DCA2ED0B18542E3BB7917E62DD4F60F9CF1FD4BF5
CF293770C0E7173878D4390BFFA4C761E2D7B063C213B125B19C5BB61925EBDE
B1E132EBBD8761596D13B2B5A222D40061163A584CD9EAC7C964EA8255655378
EB40A7D5C40260D65F212DD56AE061FD888934D269B983395525F33E141E0950
6BD5AACCA833B1EC2CD020F25ACA44B6CF6BDC47F05E86CC51764ACB7A23CE2D
C128412E144988B1D9B622BE27CC1EAB149652693371298D060B028B88A9055A
F630F7A706B733845AE425C6BB37BEC052A62E79E57E6EF7334838F019216ECD
EEA009AA50DA6E46CE13A2296B8B27836DCC1DED5C90A562C94D481943211548
83EDA1692457AA32BDCCC7177F3ED45B0407C70560DFBA15946492D35E3BBC34
FBCBC45A3030BCC817FC4D6A870FE506470771B3C2A473B4C9B7C5C687992AB5
020F9E6121E82E2C988B7C2CA9908D491645336C151C49088B39EDB954E5B303
E0251F0864E0C0CF21B51C99E88620B89EB9A5684CBE2A9B2FCDA2476A8477C3
DDEDDAD98DACC947DE8B410646FC4FDA755DFC75A7E7E7C418213929371356EF
B55389498BCD5934ABB2769E5B81F9629874189628EEF839A6C4EFE873F4FA4E
B75F2894015FE4E935FB43576BE40E792916BDE7ABD487851856088B769E045D
550A0C5A0CE14C6E949BDE88D783C539E1E0F8663D0E47C2D4DA39E0B3B6434B
874D84362CC54C705B1C98ADB8055F6B7F4B4E8E15B62673890285F495B58141
61C8A26D8B0328A7AAAF800005A6440D5ECD22638D32BF5CF6D884F1F8C0EB34
C30B0C3798028E683610D87E600D5E415738BC7F31A317720551CBC0EBAC7168
C63C2E33865732DC9F357FF7DBEA2F6006D02AF26143E7DC1A86054C7B12F91C
3026EBB0EA541A089B625BEDF6F56093E7FA69ADB78ACE7850ECAE51C56EA781
7188B2A815BAF783F01625B35486951FB6CDD3F6CC420EFA85897F162EC06966
A5D63B91836AB7A4C82B256EA56F4E397F2114987094A9DE95BACACD300C7B16
0B0E0AEC273FB222A8DC34C4D08C33508C056769E31345507C7187D26E72922D
C2359F053A48152F70722660F84B436C332D2F78CCAC5E237D52BA32B299B266
5B9BE88430503B3C1185275C089F0BB40878166FBC8FC71640E5E271F0850F51
F4CE584889AB53064D1C476384907E2292C3BE49C84A41282BD88241CC082199
96A68A35CC04F19B353CDDE4F095E5DE882BECAE38AD2561240A52A38F4CE61B
2333C321D9B7AFA5D57124F9436DCC4B37091461DF44B96DEE0446B949309CA8
C0AD014F4C8BCD084B5F07B1834F82E79C7F4CFD10883E7DF57D96A6776ADE33
BF0181F1FD0B7243003E60CA48A362266BA57C01E60847E39106626E73F58A45
3304C546390C3C0696FF5DF00A160EB93D6525E91A8B1202D1F0FCE974E99B5D
208EEA37BBD74BC401950C70B188526C41052B07C70FA20EC6B72A61C41A0B3C
1897041561973F55800E99A1E2332D7E54996769312EF3E88899957C562187DC
1C2FAEB1D28D211A530586299DE3EF2403F504FC69867E993F5FCDA77C4E824F
47E660D394D3E7426C6ADA8277EA4D2A3735675AC5873C38BF28E3418B7E5C7B
1749A0FAA1813D8B229A381B5676CDCE819C50DB198B2F56CD8758E5CE92B7DD
68B7E36FBC729927E5162FB7070377894805338209E6E15249AC6CBEB6DD58AC
7020D85919559FCDFD1439432797A544BAD3DDE9840E11CF922E1057C3C1BDB3
21AA45225665F3F386F915CC8BE3397C30A0B75D9CCD5236A28AD844BC568F55
B707C8305B2D913CE113C8BD7EB65C8A94B2AC22AD2F80CDC714831B50BAD3E1
D9811F2ABF007320034793D21BD155C90E6FD597917AF2F5CD56A2A5CEB8AB18
9DAD10994700076E56A177679289F91BE33E1063924CB299D150946012B266A0
35E74F244A2C6C16836C5FE34F4B2843BFE65E0A02C2C12642FCA307C04238CD
4891A6CEE3B15302C41F6DB44103494D966EECBC94B2A76305FCB02CDBE37D91
3A7E5222E627D57B847D778C34428A3A1727829920F5960C3757CD5405C924B0
3C1CE597875DB9D52772C1CFAC4752E1166AE9F6932830ADB4DFD190F9DE5671
82392C93E473B99C43028CF062995D938A4A0524CACCFEA32EA5B29D9BDAB380
B854969E9066C22774E17E178E4FE47F380F8345FB7DB0AC6F2656F5A8325F9C
FF61BFB9D56D8EAF29C45FE6A22711715C48F8C73D4F8FD95826C4E717068723
5C0F03B2C930B71F405662684874408E45FC7214027FC0A772795282BD727429
C4F18263F2C5C34B67278008E608050D7E82AA3A37572A78E7909F1C89B33B2C
EF8D4AE3646249A8C73D4F3B10CF32E1D62C71E1EA98DC7701EE792296B452C9
A18CC8853629EEB25FAE69DF05A551A7F63B9EB04467A0CC150B8538CB6A8E25
893CB5E8FA0CFC981B1B245AF3D0353B50951AC31E02E015B99149679FAEA697
F67F26C5DB08B2D13354B3A367A8D8112A09F628C429AC9849B83C92085B5702
5D43608E4D6AB53201EB10DCC413294F32B8ACB7E6100521109F9ED6783E74D7
D487BB66AB3E3C7FA5FEE979F613359B3D7F95AB555DA50C49B29EC879227E08
D728C834109F2ECBF2A1C82202E0D5479EB1131FABA75A0EEFCE619269480381
8C394994D9C5D99E58CD62062FEC8A7788E8A8D08BCDA691CC16FB3B1235DCED
C1E38011910677C888C32CEEC80B6344220FB3A246EE38210618BDD522CA2147
9D1BAE8148DE0171C269DAE86590586B963B45F45F0FB3A7D6E9E4293619BBD3
662E0A434C0A3E1AA49033C0792DC76E897A2E5B5F1CAA4A9E759747CC44963F
26C9B3D291E636E530D4CB1751C32102F00384B2E96A6EB91C3922363AF65866
9CFCA421102B3D7D2FD4E088821837003C02DDF124C3735CA06045C1431A4021
298EAEE3CF1A1F45C6622000C63016BF19EE47BB23E47FDDC5F312C5C74D5099
5278ECCE99780A8610A0EFB2E8CC71546F98DA673ED2F71F643A8AA9D9E4C853
3301D31355F20C88ADB248CC43E87D0B86BBFB6DECEE9FA3CA81DB0EFCD4F867
9810A1F9E75A129C978E98177F3BCD732A070EB8E35416F416BA4F0570A6BA5F
7B3380CE7EC7088A586F516F2F2DB6E309C8EE33B62CFEDF79946E327895C53F
9B82FD320D0C66E360AFE0A7976E6C93A41F879DB22729A849BD6058C408D373
7740DE2DC4936BD22D0E27A2C344E258524874619D2352946206D07B3FCC8769
B4BD65E03D2E6573BE89EE13F03EEB2ACA9D32EEB0BF92C0BF0F171760F254A2
D0A4BE7EA344BEEAF0AE04AC928CD971E20CA0727045E35A4F3631CA29033306
1043B9EB0D4400402C9191D06ADF4C4ABDAC36C06E33958DD44A98FCEC0208B2
3CA2595EAB6F8B7A89592FDA5CE3B36B9B9D6EEB43387333081E5852456E9585
CE15632C510A430F0914CD150B4985065FAA0B8A4C3FE359DF656BC2D4CF6F8F
2EAC231435DC98202815847B30CC55BFF0658C6AF69A8C74D4262294B95380DF
33C0AF2BCA8FB0AFA5D00849F9ED1E434AFA38893BE06F73A42FAE00A9249629
513A28798BDBB96D9039C3C4E4725B2AC83B3B82F4CDCC18F13D6AAE2B41BDB8
F400A545C977885C06B5BD5AC35D28E086CD17C2D4C5E1FDFE4614008B2383D3
7CE1A77AC8D5D9708E073F9DCC9D29E4B790940DB27F8D4A0A99071CB34D666E
88C753769EA3D59B45FE4699FFDE29275C07F3F2B340D9CE261140C0847D363F
D9A078AE00A4F367F9D64CC46FC8FABE9E5F85D735CBE12FAB478CDE8BA5C8DA
BD65180AB75BF20E180CD978378E73176BC72C74BD0B5394160FF3D4E4049A24
C2317ACB639F830D82F9405E244F1B470B3B0A05B3B80D5E0410401B179C815F
0601D3D4360648A58AFAEE3E01F6B8F81C9182A94E16AC5D1215DDA744A4173A
0B44C6432701F10B77330F7B3761B5638327488D49DDD85D9CBA41A3059EC174
EA76E2EC4DD8A484118A43B3EFB31E2F09F2E72EECA52E74EB187D93A7EB1B7F
B9D2867ED801CCF5B6E93ABA4148AB29D83BA5BA70FC3475170B6587B75CC6BC
669F132F27F27288A784C76D4018B9D19EA5552C2E2E6C02E8297B9CEE8AF613
E04C812B6CC7AE557A1B351733879A5359211D3B245630141F1CEAF68047F09F
0740DD70B68700DCC8607593E0EC24E5DDD551C35DED3CA1B96D7A496C100CE8
CC7B4BDC21BE6284FE26DC3E4E3684B938CC98110BFA472C549A5DD43BBA71FC
22226134A4AFDECB532BC5FD8E81B8A82C360C643023664EB33C5DEFC6297039
391866AB31AEB7A42D6912F56C5FC6A43A27F66ED59038CF0941C96E28129917
D7E682CB0298A279C0AD6AB44F2F2F2F55853BFF7A0336F11A89D753B3ED3970
5B4919327D43BC62B1115C9F607132859B2538289260FCDE24016130D68F5E12
42671812DBBDB2B615D78B5C1C334EB88F99BBE8C8E510ECC51E073FABCE12C5
EC3484E1ED4EE281898867F5A6FC24DEC849445256F66A6D33F4E0F021BD662F
A354D944C8E9874FEA07D969C61EB6C684E5D0F064EF1032470F112D9E7F317D
66ED7028BCE77006915BE6369BC25FD5FD6AF56674A6DBAC948A8D25D9BD4279
71C96D35725AFB845FA69189444E8C599E993ECFD2DE5A226BDB8C274639418B
B2081D3F283E32197F4011955F42FBE6128A1CAF47F7911017FFD62C91468F0B
EC452E486DE4452C5CBC0B228ED80E73F030C9F0661E3CC79642328B9C4B562B
F5C55D96B1D2C45B356FF8A1DA4EE3B593FEB64B71F7A5B703F91D98D28CBB56
DD7EB7031292D4A72546DCCFC0323CCE226546B2D958D9D25DD5A11E14B8E094
CE4363A121B8B037ED90187FB257148338DF14DBE24E2BF30B01F67783FCE599
78B930ABBBA4DF1FB387663BB56A9B0D9DD1F189BAAD06389650B969F15663BA
D5A7061FC2A30E80999FE4118CA47C053E3672F646258E69870D032C7A27CE88
F35F352247801F8B26D78588CD6FB35E3A7D11AD5B42F05C3DC8BB0F5E6FF98C
5AB9C376E3762E7422564454780F168C1328713BE62D5F18A4E0C1FA2FA1C0A5
324E822D1F3576D9825E8724F6706C9D207EEF990A20240EB7B42C6E0D0FBF53
F7CE1B7A0790A406F04702FD28C221B3275B1D77A1F598DC3FBDF7A6D4C0FA1F
D47E69F7EF5E0A9141FBA389E1AC9A4D114F148D5DDD6C1B0E5978ACA64D74FD
F88A3628553EAA9897CCE7B4DD539BB23E3EAF0430C7414D6C8D8674F4D12EED
4C928D6D8EFCAB710F7DFE60AAA4CE469F363A8DF31934B5908FD2153FE96D79
FCF09BF1E4EF0354AF8021DDFFA91CC54E2AFCBDD2ECF55CFD778C7190A2FCCA
6E7B4D8379F0E64896A92B3CF91F374F8A6D160F39A60A53E2DB0B5CFB0554D8
F4F2672F4869BC77AD2EEE68F01BB09C283534D13968F987C1F505665493E87C
0AC2EF333A6D3156683610EF33F6E357DE547A318DD97CA2E3E02966F309E90F
6A48CFD13EC7BA1B76319ADC556DC0D0BCB1F31C51BF6E83DB7A4A36AF31C9A1
836447FEE9613D74B4F3767171EF3C2833B2D9AD51C0947114CF7D08507561AA
A72B9D2A901C51937958496CDF67C90C8DF039F2334A7CCC6491BB5A3E59C842
5847BAB8E8FD5D4DF16F28DE7BF9E3EDB5D9576FD1C41E41B45BDDE11ECCD181
D3F61A4D96FD289E885F0C71028BABC1DB9B306376873F37190FACF026E40FEA
CDE5E5E59B14100B391CFBB13E51EDA8CC89B236AC91CD2FED272BD7DADBEA5D
D499F77F702D8B3B9D7C9B68574FB91F8CE5E1597E5AC216B3E89559E3E9081E
17E52C5469F60747829532403A7AC82058FDEC22753301EB52F1CB9AA36E5E19
92F1B93FE4F2BA5CBB74842699DB4BC4A02C908BE46F05BF9C5A2E458BDB90F3
FB08C6F989B6120C1B5F444C11F8D89D3B21A12D31342697AD128ECE77708259
95915E83E18053BE8882F212744A328B21E730F0C4F1D9689C2A0505CCBAAEEC
A55A3CCF7CC10B6437831D0409ACA1E345FAE7CC1C835AE039ACA3BC1D4F182D
45BA50BAEAFD36722EB614019F21203A9029B266483B381C9954AB6BF3FBF12E
B7342D1106EE7733769BDDF806D6F1A5E6CE738CAC37897C374498E2EB174E1E
4FC9F49998D25D7DD8213D8A5ADBCBFEE4DE3A0703E4E387A86151DEEFBB7E98
BF11FF480BDA1FE2AA2096397218141EC766480A8DED26F6D3994E86452AF214
E41DC926FD31456044F590E6324279D795E49DA459586CC12D04AD547438E1A8
707848067FE8C8FEEF8ECA446D2EA6CF138C91316E01A6E22C10DB8D17B3E7E9
EB1AE0B190573598BC6E88B1132DFEABE334BF5DE95E881A2E62166568BC2EC3
5D1E3C58477B73E62F9A16738CDBDA63ABE6ADB928D59FEB8661CDB924CBFA89
7B1DF88657E2AE3FEA810F3308FCBB94499F3EC92BF39B56E2B38FB875257E31
96D241F19761DCC656986F7E969FFD1F598CCF767B840000
}
; The following conditional evaluation checks to see if a
; database file exists. If not, it creates a file with
; some empty blocks:
if not exists? %database.db [write %database.db {[][]}]
; Now the stored data is read into a variable word:
database: load %database.db
; Here's the guts of the program. Be sure to read the
; list-view documentation to see how the widget works.
view center-face gui: layout [
h3 {To enter data, double-click any row, and type directly
into the listview. Click column headers to sort:}
theview: list-view 775x200 with [
data-columns: [Student Teacher Day Time Phone
Parent Age Payments Reschedule Notes]
data: copy database
tri-state-sort: false
editable?: true
]
across
button "add row" [theview/insert-row]
button "remove row" [
if (to-string request-list "Are you sure?"
[yes no]) = "yes" [
theview/remove-row
]
]
button "filter data" [
filter-text: request-text/title trim {
Filter Text (leave blank to refresh all data):}
theview/filter-string: filter-text
theview/update
]
button "save db" [
backup-file: to-file rejoin ["backup_" now/date]
write backup-file read %database.db
save %database.db theview/data
]
]
Here's a stripped down version that's completely functional (only 18 lines of code!). It runs the list-view module from an external file on the hard drive. If the module doesn't exist on the hard drive, it's downloaded directly from the web server (instead of embedding it in the code, as above). So, if the list-view module is not distributed with this script, an available Internet connection is required to run it:
Rebol []
if not exists? %list-view.r [write %list-view.r read
http://www.hmkdesign.dk/rebol/list-view/list-view.r
]
do %list-view.r
if not exists? %database.db [write %database.db {[][]}]
database: load %database.db
view center-face gui: layout [
theview: list-view 775x200 with [
data-columns: [Student Teacher Day Time Phone
Parent Age Payments Reschedule Notes]
data: copy database
tri-state-sort: false
editable?: true
]
across
button "add row" [theview/insert-row]
button "remove row" [theview/remove-row]
button "filter data" [
filter-text: request-text/title trim {
Filter Text (leave blank to refresh all data):}
theview/filter-string: filter-text
theview/update
]
button "save db" [save %database.db theview/data]
]
In both programs above, clicking on a column header sorts the data by the selected column, ascending or descending. Clicking the diamond in the upper right hand corner returns the data to its unsorted order. Selecting a row of data with the mouse allows each cell to be edited directly in the listview. Because inline editing is possible, no additional GUI widgets are required for data input/output. For example, no text input fields are needed because all data is entered and displayed directly in the listview. That's a very powerful tool which is useful in a wide variety of situations.
All of the display and data manipulation features in the example above are enabled by the imported list-view module. It makes database programming really easy in Rebol. For more information about dealing with lists of data using Rebol's raw tools, see the pages below:
http://compkarori.com/vanilla/display/VID+list+style
http://compkarori.com/vanilla/display/list+examples
Also, be sure to see the links below for examples of how to organize and manipulate data using the native features of Rebol, and some useful third-party modules:
http://www.rebol.net/cookbook/recipes/0012.html
http://www.dobeash.com/it/rebdb/
http://softinnov.org/rebol/mysql.shtml
There are more links to database tools on the rebDB page. Understanding and being able to use such database tools is an important part of creating modern applications of all types.
21.7 Peer-to-Peer Instant Messenger
This example allows two users to connect directly via a TCP/IP network port (a user selected IP address and port setting) to exchange messages. Unlike the FTP Chat Room above, this example requires no third party web server to store or transfer data. Text is sent directly between two computers, across an established network socket connection (on either the Internet or a local network). The application can act as either client or server, depending on the user's selection. To work properly, the server machine needs to have an exposed IP address or an open router/firewall port. The client machine can be located behind a router or firewall, without any forwarded incoming ports. Program operation can be demonstrated on a single computer. For instructions, see the help documentation included in the code.
The underlying code in this example is based on the Rebol cookbook example at http://www.rebol.net/cookbook/recipes/0028.html. See that page for a detailed explanation of how Rebol opens, connects, and sends data across TCP/IP ports. For information about transferring binary files and data directly across a peer-to-peer connection, see http://www.rebol.net/cookbook/recipes/0058.html. The examples there provide a simple model for adding file sharing abilities to your network applications. The techniques demonstrated in the Rebol networking examples form the basis for building non-native protocols, and can be used to send data through serial ports, to control external hardware devices, etc.
For basic information about how to configure routers and network ports for use in this example, see http://portforward.com. That type of configuration is beyond the scope of this tutorial, but is required for every sort of peer-to-peer ("p2p") network application - file sharing, multiuser online games, web cam sharing, instant messaging, web serving, etc. For an even more fundamental explanation about how networks work, and for information about how to configure a typical network setup in MS Windows, see http://com-pute.com/FreeTutorials/Other/NetworkBasics.html.
REBOL [Title: "Peer-to-Peer Instant Messenger"]
connected: false
; This is a "flag" variable, used to mark whether or not the
; two machines have already connected. It helps to more
; gracefully handle connection and shutdown actions throughout
; the script.
; The code below traps the close button (just a variation of
; the routine used in the previous database example). It
; assures that all open ports are closed, and sends a message
; to the remote machine that the connection has been terminated.
; Notice that the lines in the disconnect message are sent
; in reverse order. When they're received by the other machine,
; they're printed out one at a time, each line on top of the
; previous - so it appears correctly when viewed on the other
; side.
insert-event-func closedown: func [face event] [
either event/type = 'close [
if connected [
insert port trim {
*************************************************
AND RECONNECT.
YOU MUST RESTART THE APPLICATION
TO CONTINUE WITH ANOTHER CHAT,
THE REMOTE PARTY HAS DISCONNECTED.
*************************************************
}
close port
if mode/text = "Server Mode" [close listen]
]
quit
] [event]
]
view/new center-face gui: layout [
across
at 5x2 ; this code positions the following items in the GUI
; The text below appears as a menu option in the upper
; left hand corner of the GUI. When it's clicked, the
; text contained in the "display" area is saved to a
; user selected file.
text bold "Save Chat" [
filename: to-file request-file/title/file/save trim {
Save file as:} "Save" %/c/chat.txt
write filename display/text
]
; The text below is another menu option. It displays
; the user's IP address when clicked. It relies on a
; public web server to find the external address
; (whatismyip.com). The "parse" command is used to
; extract the IP address from the page. Parsing is
; covered in a separate dedicated section later in
; the tutorial.
text bold "Lookup IP" [
parse read http://whatismyip.com [
thru <title> copy my-ip to </title> (
alert to-string rejoin ["external: "
my-ip " internal: " read dns://]
)
]
]
; The text below is a third menu option. It displays
; the help text when clicked.
text bold "Help" [
alert {
Enter the IP address and port number in the fields
provided. If you will listen for others to call you,
use the rotary button to select "Server Mode" (you
must have an exposed IP address and/or an open port
to accept an incoming chat). Select "Client Mode" if
you will connect to another's chat server (you can do
that even if you're behind an unconfigured firewall,
router, etc.). Click "Connect" to begin the chat.
To test the application on one machine, open two
instances of the chat application, leave the IP set
to "localhost" on both. Set one instance to run as
server, and the other as client, then click connect.
You can edit the chat text directly in the display
area, and you can save the text to a local file.
}
]
return
; Below are the widgets used to enter connection info.
; Notice the labels assigned to each item. Later, the
; text contained in these widgets is referred to as
; <label>/text. Take a good look at the action block
; for the rotary button too. Whenever it's clicked,
; it either hides or shows the other widgets. When in
; server mode, no connection IP address is needed - the
; application just waits for a connection on the given
; port. Hiding the IP address field spares the user some
; confusion.
lab1: h3 "IP Address:" IP: field "localhost" 102
lab2: h3 "Port:" portspec: field "9083" 50
mode: rotary 120 "Client Mode" "Server Mode" [
either value = "Client Mode" [
show lab1 show IP
][
hide lab1 hide IP
]
]
; Below is the connect button, and the large action block
; that does most of the work. When the button is clicked,
; it's first hidden, so that the user isn't tempted to
; open the port again (that would cause an error). Then,
; a TCP/IP port is opened - the type (server/client) is
; determined using an "either" construct. If an error
; occurs in either of the port opening operations, the
; error is trapped and the user is alerted with a message -
; that's more graceful and informative than letting the
; program crash with an error. Notice that the IP
; address and port info are gathered from the fields above.
; If the server mode is selected (i.e., if the "mode" button
; above isn't displaying the text "Client Mode"), then the
; the TCP ports are opened in listening mode - waiting
; for a client to connect. If the client mode is selected,
; an attempt is made to open a direct connection to the IP
; address and port selected.
cnnct: button red "Connect" [
hide cnnct
either mode/text = "Client Mode" [
if error? try [
port: open/direct/lines/no-wait to-url rejoin [
"tcp://" IP/text ":" portspec/text]
][alert "Server is not responding." return]
][
if error? try [
listen: open/direct/lines/no-wait to-url rejoin [
"tcp://:" portspec/text]
wait listen
port: first listen
][alert "Server is already running." return]
]
; After the ports have been opened, the text entry field
; is highlighted, and the connection flag is set to true.
; Focusing on the text entry field provides a nice visual
; cue to the user that the connection has been made, but
; it's not required.
focus entry
connected: true
; The forever loop below continuously waits for data to
; appear in the open network connection. Whenever data
; is inserted on the other side, it's copied and
; appended to the current text in the display area, and
; then the display area is updated to show the new text.
forever [
wait port
foreach msg any [copy port []] [
display/text: rejoin [
">>> "msg newline display/text]
]
show display
]
]
; Below are the display area and text entry fields. Notice
; the labels assigned to each. The "return"s just put each
; widget on a new line in the GUI (because the layout mode
; is set to "across" above).
return display: area "" 537x500
return entry: field 428 ; the numbers are pixel sizes
; The send button below does some more important work.
; First, it checks to see if the connection has been made
; (using the flag set above). If so, it inserts the text
; contained in the "entry" field above into the open TCP/IP
; port, to be picked up by the remote machine - if the
; connection has been made, the program on the other end
; is waiting to read any data inserted into that port.
; After sending the data across the network connection,
; the text is appended to the local current text display
; area, and the display is updated:
button "Send Text" [
if connected [
insert port entry/text focus entry
display/text: rejoin [
"<<< " entry/text newline display/text]
show display
]
]
]
show gui do-events ; these are required because the "/new"
; refinement is used above.
To actually use this example, the client user needs to know the IP address of the server machine. In practical application, this could be handled by simply emailing the server's current IP address, but that's a pretty clunky solution. As an exercise, try writing a small addition to the code that automatically posts the current server IP address to a consistently available ftp server every time the program is run (the function to obtain current IP addresses is already included). It should only take one line of code. Write another line to automatically download and insert the IP address into the text field when run in client mode, and you've got a fully automated connection mechanism. If you want to get fancy, you could set up an ID schema tied to individual user IP addresses - all updated automatically and selectable at runtime. You could add features to track who's online/offline at any given time, etc.
21.8 3D Example
This last example is a simple demo of how to use Andrew Hoadley's r3D module to add useful 3D renderings to your applications. For more detailed and interesting code examples, see Andrew's examples at http://www.rebol.net/demos/download.html.
REBOL [
Title: "r3D Example"
Notes: {Simplified from:
http://www.rebol.net/demos/BF02D682713522AA/i-rebot.r }
]
; This section loads the compressed r3D library:
do to-string decompress 64#{
eJzdPGtT28iWn+Nf0cOXsTMYkGXznL1bBMzEtQSnjPMAipqSpTboRpa8kmwwv37P
Od0tdethOzNTu1VLJUTqPu9XP5VR/8Pwmj002NhPA37KdmL7cqfBzhfpcxTD63no
xfyFfYwcL+Ar6PnK48SPwlNm7R3sHTQeG43GGbuI5qvYf3pOWdNtsc7BweEuMzER
6jOPZ36C2MxP2DOP+WTFnmInTLm3y6Yx5yyaMvfZiZ/4Lksj5oQrNgd+gBBNUscP
/fCJOcwFbgi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A57DQcljAsoC/XWixM1606vsfGt6vyUFIAAA=
}
; This section sets some default values:
base-rot: 150.0 ; degree of base rotation default value
; camera translation and 'look at' values
cameraTransx: 300.0 cameraTransy: 300.0 cameraTransz: 300.0
cameraLookatx: 0.0 cameraLookaty: 0.0 cameraLookatz: 100.0
; The update function below builds the world, sets up the camera
; and renders the world, filling the 'RenderTriangles' block
; with all of the Triangle, pen and fill-pen commands necessary
; to draw the world (in reverse Z order). The "cube-model"
; variable and all the functions such as "r3d-scale",
; "r3d-rotatez", "r3d-translate" are defined in the compressed
; r3D module. To understand how to use the r3D module, pay
; particular attention to the newly defined variables in this
; function. It's where most of the work of creating unique
; 3D objects is accomplished:
update: does [
world: copy [] ; re-create the world
; PLACE Robot Base
base-modelWorld: r3d-scale 100.0 100.0 50.0
base-object: reduce [ cube-model base-modelWorld navy ]
append world reduce [ base-object ]
; PLACE Robot Stand
stand-modelWorld: r3d-compose-m4 reduce [
r3d-scale 35.0 35.0 150.0
r3d-rotatez base-rot
r3d-translate 0.0 0.0 52.0
]
stand-object: reduce [ cube-model stand-modelWorld red ]
append world reduce [ stand-object ]
; NEXT - SET UP THE CAMERA TO VIEW THE WORLD
; create the transform for the camera
camera: r3d-position-object
reduce [ cameratransx cameratransy cameratransz ]
reduce [ cameraLookatx cameralookaty cameralookatz ]
[ 0.0 0.0 1.0 ]
; Get Projection matrix
Projection: r3d-perspective 250.0
RenderTriangles: render world camera Projection 400x360
]
RenderTriangles: copy []
; Below is the GUI for displaying and controlling the 3D object.
; It's mostly just standard text labels and some slider widgets
; that call the update function, and then refresh the GUI using
; "show". The update function does all the real work:
out: layout [
r3d-viewport: box 400x360 black effect [draw RenderTriangles]
across
style lab label 55 right yellow
style lab2 label 40 right
vh2 "Spin Top Box:" rbslider: slider 60x16 [
base-rot: (value * 300.0) update show r3d-viewport ]
return
lab "Position"
lab2 "x" cpos_x: slider 60x16 [cameratransx:
(value * 600 - 300.0) update show r3d-viewport]
lab2 "y" cpos_y: slider 60x16 [cameratransy:
(value * 600 - 300.0) update show r3d-viewport]
lab2 "z" cpos_z: slider 60x16 [cameratransz:
(value * 600) update show r3d-viewport]
return
lab "Look at"
lab2 "x" clook_x: slider 60x16 [cameraLookatx:
(value * 400 - 200.0) update show r3d-viewport]
lab2 "y" clook_y: slider 60x16 [cameraLookaty:
(value * 400 - 200.0) update show r3d-viewport]
lab2 "z" clook_z: slider 60x16 [cameraLookatz:
(value * 200 ) update show r3d-viewport]
]
update
view out
22. Menus
One oddity about Rebol's GUI dialect is that it doesn't incorporate a native way to create standard menus. The "choice" button widget is a close substitute that's useful in short applications, but it doesn't look or operate in the typical way users expect (the "Image Effector" program above demonstrates how choice buttons can be used to provide menu functionality). Menus are important - they're a fundamental part of standard GUI design, and users tend to look for them intuitively.
Here's a simple prototype that can be included in your programs to provide additional menu functionality:
Rebol [Title: "Simple Menu Example"]
view center-face gui: layout/size [
at 100x100 H3 "You selected:"
display: field
; Here's the menu. Make sure it goes AFTER other GUI code.
; If you put it before other code, the menu will appear be-
; hind other widgets in the GUI. The menu is basically just
; a text-list widget, which is initially hidden off-screen
; at position -200x-200. When an item in the list is
; clicked upon, the action block for the text-list runs
; through a conditional switch structure, to decide what to
; do for the chosen item. The code for each option first
; re-hides the menu by repositioning it offscreen (at
; -200x-200 again). For use in your own programs, you can
; put as many items as you want in the list, and the action
; block for each item can perform any actions you want.
; Here, each option just updates the text in the "display"
; text entry field, created above. Change, add to, or
; delete the "item1" "item2" and "quit" elements to suit
; your needs:
origin 2x2 space 5x5 across
at -200x-200 file-menu: text-list "item1" "item2" "quit" [
switch value [
"item1" [
face/offset: -200x-200
show file-menu
; PUT YOUR CODE HERE:
set-face display "File / item1"
]
"item2" [
face/offset: -200x-200
show file-menu
; PUT YOUR CODE HERE:
set-face display "File / item2"
]
"quit" [quit]
]
]
; The menu initially just appears as some text choices at
; the top of the GUI. When the "File" menu is clicked,
; the action block of that text widget repositions the
; text-list above, so that it appears directly underneath
; the File menu ("face/offset" is the location of the
; currently selected text widget). It disappears when
; clicked again - the code checks to see if the text-list
; is positioned beneath the menu. If so, it repositions
; it out of sight.
at 2x2
text bold "File" [
either (face/offset + 0x22) = file-menu/offset [
file-menu/offset: -200x-200
show file-menu
][
file-menu/offset: (face/offset + 0x22)
show file-menu
]
]
; Here's an additional top level menu option. It provides
; just a single choice. Instead of opening a text-list
; widget with multiple options, it simply ensures that the
; other menu is closed (re-hidden), and then runs some code.
text bold "Help" [
file-menu/offset: -200x-200
show file-menu
; PUT YOUR CODE HERE:
set-face display "Help"
]
] 400x300
If you need full blown menus with all the bells and whistles, animated icons, appropriate look-and-feel for various operating systems, and every possible display option, a module has been created to easily provide that capability: http://www.rebol.org/library/scripts/menu-system.r. Here's a minimal example demonstrating it's use:
do http://www.rebol.org/library/scripts/menu-system.r
menu-data: [edit: item "Menu" menu [item "Item1" item "Item2"]]
reb-style: [item style action [alert item/body/text]]
view center-face layout/size [
at 2x2 menu-bar menu menu-data menu-style reb-style
] 400x500
The demo at http://www.rebol.org/library/scripts/menu-system-demo.r shows off advanced features of the module. Below is an intermediate example with explanations of the most important features. The menu module has been compressed and embedded in the code:
REBOL [Title: "Menu Example"]
; The following line imports the compressed menu module.
do decompress #{
789CED7DED761B3792E8EFEBA740343F24AD4D5352EC24C3331E1F59A213258E
E548B233191EDE735A64536A9B6433ECA64566BD8F71DFF7A2AAF0D9F8E82645
27D9DD602672B31B28140A8542A150285C745F9CBF62BD078CA78BECE6B62C3A
8CFD27FE847492CF567378CDF606FBECE8E0E0293BB99D6745992553D69D16E9
58653D1E8F19662DD83C2DD2F9C774F8F881FA7A910E79A97976BD28B37CCA92
E9902D8A94655356E48BF920C537D7D93499AFD8289F4F8A47EC2E2B6F593EC7
7FF345C914AC493ECC46D92001488F58324FD92C9D4FB2B24C876C36CF3F6643
FE50DE2625FF937268E3717E974D6FD8209F0E3328546858507A92961D8DE9FF
AB225BB07C24B11CE4439E7F5194BC8D65C2B1871A92EBFC237C12A45290789A
E66536481FF16C59C1C61C28C032F08056DB48F29A07E3249BA4F3C7319478D5
06B5244ABCF5C30547D38315DB165A8C5A6D821BE683C5249D9689ECDB36EFB6
9C679AB34952A6F32C1917BA67B05F01BAD922ABB1AFD30C0B43A6693249013B
78D68DB9CDC7439E619AEB4C9C266561B5923788E0E7F38223B262D729301D6F
5ACED2E990BF4D81BF386293BC4C19118FC3E09033CEBD16AC11CF44E42AF251
79076C23F9B298A503E0470E2003769D03274E89278B02DBA6205D7D7776C92E
CF5F5EFD7C7CD165FCF9CDC5F9BBB3D3EE297BF10BFFD86527E76F7EB938FBF6
BB2BF6DDF9ABD3EEC5253B7E7DCADFBEBEBA387BF1F6EAFCE252C1DA39BEE410
7630C3F1EB5F58F75F6F2EBA9797ECFC829DFDF8E6D51907CA6BB9387E7D75D6
BD7CC4CE5E9FBC7A7B7AF6FADB478C0362AFCFAF74035F9DFD7876C5F35F9D3F
422CDCF2ECFC25FBB17B71F21DFF79FCE2ECD5D9D52F58F1CBB3ABD750E94B5E
AB1605ECCDF1C5D5D9C9DB57C717ECCDDB8B37E7975D060D3E3DBB3C79757CF6
63F7F431C787E3C0BAEFBAAFAFD8E577C7AF5E55DAAFA09DFFFCBA7B01AD32A9
C05E7439D6C72F5E75B16A68FFE9D945F7E40A1AAA9F4E386D39C2AF1E697097
6FBA2767F0A6FBAF2E6FE6F1C52F8F04F0CBEE4F6F796EFE919D1EFF78FC2D6F
F55E53AAF17E3C797BD1FD115AC34975F9F6C5E5D5D9D5DBAB2EFBF6FCFC143B
E5B27BF1EEEC840315E9F4F8EA18ABE6653919D57B4E86F38B5F000A341049FD
88FDFC5D97BFBF00AA21198EA17D979C1C275766360E8D53C7C0513780BDEE7E
FBEAECDBEEEB932E643B07703F9F5D76F779CF9C5D42060E1BE8F8F331AFFC2D
36037AE42DEF3BDD172F6D167E841DC8CE5EB2E3D3771CD8A92CC5BBFCF24CF0
09D2E3E43B41D4C73BAC2E6175FFF5A0FFE0C1A05CB6B86459F059094673BA2C
F95CF5C0CA57DC26C3FCAE954D929BB483934A0FE69F92E19BBE98DB20E1EB0E
2B5645994EDAF90C655D9BC468EB3A29B44C0B67E9B0AF9EA86CA2CE719E0CF9
EBBFFDE783ECDD8BF38BBB831FBEBDC98F797A7DF9F6B6FBF6863FBD809FC73F
9D1CFF02FF8EBE69FFFD161E5E4CBE7F7571F0D371FBEEB47DFCE6E1CD838F49
7A051F4EFEF5E2ECE77FFDC89F0AF8FDAA7BD73D9ECCEEB0F48B2F2FBEBF7AFB
F6BBC39F7E7A7BF2DB2FDFAEDE24C9F827FEE1ECF5F70F2EBA2FDFA6AFE7C383
5FBBE777EF8FDFBE181E9F9E9E7D7F7AF64BF2EF9FDFBCFF7E74F9EFC5717E74
FD53B6BAFEE1877FBF9AFCFAF6B78B41F7F0EADFE70FDF7ED91D3D78F3F4EFA7
776F57DF9DDCFDF0EDAF372FF2C1CB7CF1F0EED5CDEDEDACFCE1DDE9FBF7DFFE
38FB707EFDD5ABF4F8CDCDC9AFBF7DDF3DBC7C77F9E4E3FBEF6F2E0E8BE3F70F
7EFEF7F7B7C7CB9BF7DF1497072F3ECE9E941F3EBE990C96CBB47BD25EFDB67A
F26A79F8F1F8F8E555FBFCB7BF1F5FBE6FBFBC3DCDAEFFF5DD3552A83B7EF9E0
EAC3E5E2A7C9C9C983FF6AD41FD8AB7697E0AF3EF1C92029789F8F16D301EB7D
4CC68B14DEA405E78B820BF0019F8956B3F479FB2E9F0F59C7CC40C581E55A45
F65BFA5C02D9B948CBC51C2649364A06E96E817918E481D9E46079C0391CBEB0
5E7EFD3E1D945FF4597B9C0F9231E5E1F82EC625AF5F8F2A78DFC1326D7854EF
D59B0E3B3CE069897F6B878F7F3C8911C0EBEE60852D441BAAF0D567E141C504
55154024758536AFCF5F775B45324AE92B02E323349F978345B9432F590FFFF9
82CFDED3F48BBE394293E98AF512AE48521603481F08DB7750981665321DA4AD
7C2451D0D3A3ECA7AB8BB75D968DF8945F66E58A65A8CFCDD35F17D99C4FF25C
AB290AB6078893CA729715E9FEE31D054714939D29B016E57AC0375F08BC8C76
40134CF58151E9E7029AF5691799E519BB01993595F5ED025F5AF946F9623A7C
CE461957CE9050950202A3BEC04D9575A8E6658DA6090B4CD3BBD61014E0B13B
2AA66C31CDA65C911CF36E1B72A52C9D30CAFB78C7ECEB49F2219503FB6396DE
F13F43F8AF4563675DBC3061C33B04FA9A8FB90F5F704EE2150864FB9BC04C87
20E307F9389F77583A1AF16E94FFF2AA801B9AC1B1D96134A2B908071B676DBB
A7D3742C5A8143127E57F8091267DF7972277B809A28204B89038DEF7B8A4282
6F1DD135ED19D768A765CB120756E691CC299A1E000AC9CAD861B36CF0A15A18
B9E21FFFC47FDB30C1B7930157D38953ABFFD9F37D90B6D8D135A4530F1C1F44
8E6887E8A41F3905AAC4E2ED063D3F9B3EA7EF6D415FBE286AD1E3736A0CB604
C59D4B18DD4A408733799195FA253EF9CB60765E2BACBA7C991025EB6DDF18F6
86FC3619C5CABF73CABFE0A8850A1AE866350969A1273E41ADDE2CC9E65F68DC
883B2D44B8202F531A66BA093D374B47FD44BEEA1DB1C33ED5FACCCB4D95C254
CD4355F8A02F7A0EDFB30A8FD9AC00F9841CA8BEE66B47BEDE2BB3B468231D31
5BD1EE2058174AC695D9B6D01E235020DB63CC56034A0EB5FB81BACE87ABF688
ABD91D16C30AB23D866C11088A4E7108ED1A3A213C2E9A920618413617C287B4
D2243F049E2DD0240940F77C14405D8B009CD5A030387F83683A725E57014036
7F619B858385EB1A8279EAF9CEC81681658289C00A83A9CAEC695EB274322B57
CF231C43C32124AF91F72466837C32CB8BB43D4CD35960E603111B991449A1DD
4EDA4B8A1648547660A00A734F7BC55A6CAF48C19467EAE7B5441034DD676D76
B41FD509F79A82F242E93B6FFB81E90B92F543B02D6ABE9E91E915405C094C06
1FFC6A9FCB8638DB4237B626C9FC8398CB08CEBAA9022F99CFF3BB4DE11913B9
42CE525CF4744BC07BE56236AEACAC1452505AB1B5BB4EB1F3B9030D12AC8F43
3AA55E9069306C779E0CB33C32381A0C2F93045C9F48A76C0F5BBBCF1744E371
CB7C31C8E68371CABE5E7EC3BE74190E49CA7CAC1869C0E036057D617B0D8866
311B33CEA669EB2E1B96B7EC887E0C92192B7E5D80091A7FBFCF613380AB82E3
2850C8CBBEE434F96A7978C80E8F964FB59EE5FBCF5E2F6A36DE9CF5B07813DE
2B16D72ED3DD5B02477966968F5737F9342EFAA4D8355A835277C9A52EFBC679
BD0269CA3F1DD648D41858DE5521B04F3E07D887B5F2FFFE44A859DCE1F24872
590C9535D2CE3B002AD69D7B435CF364E56EC16EF38F7C714533165FFDD84C6D
2C442AF345712BE5F9E6A962A0B15EE19ACFBF60EBC227D912C043B4840F1A34
959BB62B488126B5A7E9DD3D90F7266504CA26937498719A8E57B473067866D3
9BFBAF2FFF4A0D53DFB163A0C0DD661552566F0B1E28ED7A31EE482130B509F3
A769369CE5B3166E617BA78E7ECFB51A89DD651800FED902B815C7120E12A891
CFC0D3A11CF26BB5BB5F5305FEBA6715768BA382659C261FD32D08575B26BD02
A81EE9BA98A27C4D87C2EE027E0583319FC65D99E5015BD130EE87677F7BAC6F
2149BB15B2BBE403E9FED854F5AEEF9F418C69D1370FDCC022CE51F90DF36E51
D5B502EAF906AA39171AF968A40C6B6A45105BE6DA393BAC9C2FFC16655F8A82
2D3A86810F467C7B964CC3B047F93C4D06B7ACC8AEC7E0C182206A146F29256B
113689292BD805C45A5E3B6D35C922C2025E5F9DA2EACD3C5FCCF466910024FA
123F468105A4910737AB1347C9B8A8EF456469B3B8BBD8673ECD4FB572FDB597
C36E6A0E89AC5F0DBD4EFEAA0C38AFE685032F19A08F131ADE6D761543729E73
0CF89CC47B89D73E2FC1D348F09E8D4D608216153C63BB1C024D794ABAD81364
3F2AC05DEA8EC876B2F6BA7DED75B24BF048E7892D0E25E6FC03C26596E0AC0E
963FDCA9296EB351793F8D073AD3943A882CBCD4CDE6AFBD8D835CE62C00BF9B
12C69D1B54817A3A6423BFB54F30D6BDE831CC8D49A80F1E88C36865196D2BB1
DD221D8F02EBA5980110F8BFA2CD78F6B1F1C7753207F8E606A0FA6D307DB198
C1C24A7D521F404F11DBB98E30B0D0B037D3B04DB4CBE5B57908AB256E37693E
22DB7A6817D46AA0264868CB524A1E2CDB501930048B516F4CC6422E216377B9
9A3A6D95790B57A935659AEF78421AE58345D19EE62D3504FC23CC1D073652D6
66604C67F9DC88621D23C28E234592095C3B6A752893E0A845C729AE0AD6ED19
43AA33C343AA54DA9CA2BCB513703DB65B1BD7ED36E982462DF66B1B1EB902E9
F7B1BAD880EB4D26751A4388369BA4ED2FE97FFF353B4963EFD25D73143672AF
EAC5B1CF1EB2BD83E521FB0F9129FB2DDD6FD4D0BA65BEF41BF9AC1845ECF790
C06B0B80783C1985CB56C23B852FD6154EE8B1156DB7E9C5051AE130990FDBE4
C165CF43E49965BB66A90553DF193BB019DCDCB72A9EA09A6DC2DB76FA98D076
FCB6F003C74EDEC7E8F5209C4CD4EAE8CF4001AF47DD9F03354C8A587F342281
A495EDED908D5466AD2DDF370D9332D9668772C1B855FE206B1A78447518380C
74D88794FF11BB9BEB57046628F043C7A3571B4EC7CEEC414892BCD4EEB6AC87
361EC2722D932835D702B5A1499512116E7BF0B00B62F01C0AC9FE5A1F035A48
B2E04479BDC8C6436BAA549F775EC037E530F98895F94D8AAA091E6B03854CE9
85300D1686FEE6B167EF24A8D2C0971DE38023CCBF958C70CC0DDFC303429AA7
4536044BFA545781C718C8295D3AAAEFC8021C2B9E7F5A66A32C9DEB221C046F
204DC9827E6611F83CCFF0E00595118674AB556DA21569287CA9EF1AB295AED3
61D63A09DFD3040858DBEF499000062E400DCB303ED8C54D3989E348BAF0BBEB
7461B0E818C04D43869B5F398057F24B0767772743F884BA55086380AF88E1B5
C9CBF55C35897228647C3994B365C7AE36ECC6A9FD297D45BC6E89A603580053
E921D9A9345FBEF7345F7A41FA8AF8D1B0BD238D6E190AE73BDF6E0B328BE1FC
EAB568B9E4D2C508C770310B57CC66B8A57A8B39FEA8AA94A047B0945B974113
6F29CB55D42A5A35B80597E1243087191CF07517D04A66920013D9B42F80CA48
87100CC9B743C784313F9745E23B40F1CB22E5F7EF5F7DB88B0F01DB66571447
96890061A3C02114AC0F5575CCAE99B4DCC3AF8F1E1F7EF5CDE3C3A75F8668D8
2C5167EAF30DC8D8F73401D0211476B83C723A58A02F5611A6AE1EB0DA2A23AD
CB30B0FCDD1D27AB7C51B66843A4CA27AFF0235959CC89D39E0FFD3317BEAD9C
2BA0E37C3859EA4E12B37FCFE4BA2215C06C84F389CFC805D4EEED0ACE619FD8
EE7582FFB45AAD3EDB8BD0D94A519356C71E51F694594DF67E6807B666784B2B
AF6BCC685404952825340BA5D2A24AECAED3CD640E828A94F196719DCC3E3162
11EAB47C56C2AF16A9327BC17A31B7702614C737CB9C5129FCE947D9EFE3063D
BB4B26BE608590CC2EC2ECD2B2429C4A68586A8B2F6DA1DF109978DF4905ABA6
FF20ADDB8790FC8484BEDC2DC6C8BBCD49290A0031C5E35FE4C4E4AFC7FB32BC
C50B498F2F88BC31BD898D2C485AC5219B1296DD801774BD65D2AC525075A84E
3E9C25B29B56DFDB15FB911A0FA1057C725EF56BB0F3EF746E4E9ADE2EA8F030
7FC0BF45DF40489E93FE24E4D927705BC6DFB3A4BC857F3743D95A55C0F97859
5F140A24C2834E1E47A4AB990865D6C320090DCB8876F71A66276AB0DE306F5A
81D4011B648F7F15478D10DEF318A11B1037DE4DE4FEDE000CA4BD08ACFDF8D7
3F1CFE7DBA234A4221C22379EAC5927970731340FB7EA92D04011E1103498040
FCA2408A002912EE270AAC65FB5AA2609371BD81F8587F6CAF293C7E2751103F
70584DF16EBA9F2830607986AAF9F50F87BF4D51609130200ACC4E6235B131B4
6A141007E691C8C34DF46761173484C02E2EC43E9165D718F3750A95F0FCA3CD
788245A09E894125D81F0C88EB5155DA34054137558276A5D9B44163FFB0B62A
2BAB1A807B08693F0C26D26234791ACD6DD8BC8AA93268A51436A2E0F79AAE52
751986CA908D52EE5285BEB3CDD9028C99F72112194B8336D91891B0EA358824
ECB221936C8C4858D5E64402746B26830DB9CD26E6E7E7B8F08C700F6EB01BF1
F93922DC08BF9CC0A67136F8CC5DD86820C7C7F11FDD818D86597C94DDABFBC2
BAFB2E9AE17FD7C94B458EAA99C58C2D20DFC64F8060D81E42C09FEE6504EA89
53479FC481813ABA24B3993C45D1A62867BB9345B948C6F5A76DCCA2782484CA
3714ECE6C9927C345AB3D1587FD41E0769371FB11E98BBE9F88EB5EA1B67C202
DE788CE843427526A938DE950EABAD7C379F42A15D38EE850F2BAE437FC2D35F
EC5383D2A31114C27346F000BE579F1A1C3BEAAF4316D1917229B97D298EE66A
08526C1B191B682BE4E930CAE66A05BE7122270C7099127B6DE423415BA7AAB2
C0618C4AFA4B63D72D361B3ACC8AE47A8C9C9A4EE1A9914A2AF88F803C63124A
C45864A6863B03A1E334FA496D268A09ED41ED10C20CC9F0FDA2103BB7EE16FF
317E65D24B496CF062DC120A273E17BEC6655ECA48A860EF5E4CD221BB5E395B
BE543EB6B9AB1998A2F5E03B30A56342A51813A8569828088F6A8F8481119210
063D010C7A0218F4C461D003C2A0471521B5C199CA5052F57734021D8D4147A3
D05138740C24ECD091F2C42B524610D0420D43F1D1213B2014D208C88300FB72
118FA7BA22D9EC435DAEDB4E8BE27E70DCAC4FA05208A4D5E373773FCE063772
0E1A1AC76C7DD39241C6C3AF96875FB9390CEA4E92A5F19B1E45FCDBDAE05966
40BDC3469DEDA262F42EA2A2198EA9905E1D2F7341F620D789B3126ADF8EF5BE
5A3E610F8D58C6F88DCB551553370226ECD6A45A5F73A87A9D3864CD48A99313
99A59AAAB155ADC2EE17F78D1E77D8475A16E0A36017412BB96BE9A138FF2408
1E72658DB2ADF382661431D28C00701D37AB44133E928AC15B924D1693160642
A68D6D2F91D48068AFCC3A0C88EEFCE32121695F0203333610B4D6DB32418B20
B63121EDC12080923829B4A2AE35280AA18C0CA4318491CB46D4921810270E92
871791C6514CCC5E4020767B2231F3C2F2537A16F5D8BDBCB6EB6B234F8E90BC
D6624E341A73DB51D015F8EDF3454500EB32347CAB2835A7BB3938CDDE7CC844
70C6CA1468026810EF18492163487399E2D168DA4B43A7682FA1359236F84BB5
157F491AE10F2D8DF0A7F60AFC932818C66831085191459AEACB8E4915DE20E2
9ECFA78468C9B1F40B38359E971DB357C2A303321A1DE69DA45446B3335D1E32
23B679B10B9141B54C907B5955F054D37406BB0027BD411A7F6B8DB2162CC1BF
32D49C9F0246610B9860F750614B8E43610B182F6C9077CD89B9D9D8AF889575
457804850AEFC7917DEA10B5716A82747C263065ADEE0A6DB837BAE3CB268557
6661359756253ACDA851FA9B12A663CB1F3545D8AA50DF95C6F2CC112DB84DF8
F843BB3C46EE5348E84205E11A899AF38EBA59045F3AD72B28BF54AF3BBCB08D
0A8BAAD13D5666F2A69499D18952A28A97ED14E321AF976C4DED126FE782771D
89E8E1D1C1F2F09B3EC37C151A19F4305D40232708ACD6ABEFE2ADF76C157DAB
9EAE62D1F355025EF08415F39DB1A242B15356CC77CE4A15734E5A550C1E9C80
557B83E1826C7ACC228ECA61B6C27456E76258D70FD3FC5A925C9CD0984BC682
6F22B6EBCA642F2AC327684E42A57DED8E53CE49E4719BA2953E9F1355DBF0D4
07C00A82385A5039C1E58FA6AEFD275622964AAFF79471A6EA7DC39EF6112E18
D4A0520BC326F1BB03516363D162DDCC91E8B12A602A22240431A8F1D89C7620
386CB0548B7DC94B0582B4064B3DDCA8941743CF590F56E12A8013BA89E3FF40
060AB6431C871C062F9E07E2E8205673349DA29B3FE5C5B7C6AD25F6B3E627E0
AFC750DA137E0FABFE5CC90C2B62C614C34AAD306262A801A69EEB478C202A9C
C5CBCC7B26850E27C568E22D42A153B8D8C9C7543F8268DADDEE3C867A493218
A4EAB455E8829F36E5124D51906E52C2B683873FE869304E93B978C6CBBEC4F3
9F2862C0DA49F3C6321DDC271CA7D5073B976959C02564181D106605DC6B28D8
5EF6387D8C752D4AFE136F3115ABACDD42DEAC8741382D789E73C8BAAE2B3561
E6C0CE2C9FDA1170C073132E7E01FF4D9A2C335ECD4D3A87F9F20D7C848262A9
974C5777B7299F2C304E92B857B59CA7691525445D4CBE045BCFA83B27F051B4
72E83650E3E7B97F06D1D0BC5E352BDADE9D8405FCD5E7FEE157657F8A17C204
E83E370882871A087BF8EB292448D5B890D3146923A192B827098F9E7C66DC3C
BE10E27596F315EBF1CA647FD15917DF1C093371E5804DB59C2D4C5C18428530
8EDAA8F35B6E71EB97405C5CF3719B262AC610142FDAF314C35011059D7A8739
D9213AE6602093431DCA728755C8B036709C7A49F501D0A818D54B043C6FE40F
3A755C96C9E03655D10680B23866D2255CC13CBD41B255224F6100CAAA7A5B49
7AF0426ECFB865FE91ABF4DDEAF885C7215EAC6B45C5AA8EDECA5D7A7EDC7E04
BCF2B98E42C4A7FD39EFDD95D8D04E87C640C66EC7FD4771D59CB8F519DF8B6B
832B446A5FA760C8623B67487A11D44BBC448D9C2EF485B5D6027B3887C3CAF0
2C258F1BD72B1941F8B10A4C7A69839C71799112D01DFC11868967D01C98D43E
71D79B6A1FDB1BA6A364312ECD4064EDC4BE10194E363B4B979F61B170BD6277
B7D900FB749E8E00EB5C459AC093DA31D909CB2CD55B05FB98CDC139C710A6CE
ACA2C40E8D4E0C96698E268AA28ADF604462F8E43EE58B08BEEA11C040F043C1
339EF9B622CB1079D74E459C6217C7106795E2C031AAA1F202486223F8DBC7B6
55841F7E70EB14ECA5EA9CE2DD9D9E3A81A39C3A89CDE0AFB74EFC60D7D916DC
14B20EF62C49AB0F4BC668D777FB4A38136CD64F84BC0B1952B58FAE133E5369
7A19B486AFEB92DB20DB56A8166A49EC36243FC96484936774ADAD12B85E8C22
F7F820116A5DD74C1B9FBC85541BC6372B2AAC9AC1C235DE1618BB0131EF80B0
AB5C91CA5FC53D88AC0EF29F20F6DCE0DA180A75B4D41801418216478AF2F8A2
E738C4097E09D1BBA6878528EFC8320DF0ECC84252A00B3D4CBC65E1B33090FE
EADC353BB7492857E18758150A6CE775FB78A7B9CCA94EAF162550584742F39B
F21E52A030E608A3209CD090732D6279364D89EF02B79EA615CD1A43AE2A65CA
7458CB4A9F82DD64552C181F407FEEA57183056DDD7AB14E07ABD8CF3C2B2D9D
462A30785DF8050F1CB05A95C68CADE1E879121B77E4B4420C23273E8D55ADB1
C1832169C0E6845DE663141D2255C4251999D15137E38BDFCF5A62868373AC25
A8F88B85AF752F538869221C62C56EF38C452474F13F97D06410B23677344A40
E80E7B07CDBA2F9D2DBB4288CE348CC2A4FE9F26FC22E4135828027A68E7D28F
26F520FD7A83A41C54EC6FF730D5DCCF50F359CC34B4655C6FA4F9CB38F3A730
CE8406825F5D263BA7A32CC7D7E0B689860608762FB48219024AEC3A783471D1
875E48F40D60358124BAD30B09BF21520D20C9F5BA1F5221B3D541F21DB278A0
7FBA920678E23A99DB3B65C6DE19EF072D91E8C212E3ABFE666BB855326D96DC
D9610FC4043EE5D3F16ABF66B6A8C2136D35833A4A9906AF6B670A264DD1FD10
9B0B50B2FB906052F0CB6A24284F8A4DA79FC738FF97C4FF4BE263FA5398E371
67138EDE0536CACA5B383685220AF707BF901400162FE1220FB87A9AA9BD3B31
E4387D8AE92E9F7126B3713A819B55866C95965FEC0444A74CBFCF0C4642627B
F398056F0BB399056F0B739A0D6FE399AD492C605F52B39EE91554B910069C3A
F0335C8FD24AEE12D3FA5C9D0E71856FCC83E6955C95C50EDE469AF88471C5BD
41DCD0D20357A8F86C83B68CB1BCDB4BBEABF81CE1D75051E997E62F4A5F8365
653C677F59339AB3674A33E7B66C48ABC4E796F71F3A1C55B7FB21BA7D367D2E
BC9104A9CCCB6A9449CE3EBCE19F5465C52D58CA35A8DDBF15A13D98CCB6300D
C61D6BC1CB3D8D43D2E1EFD5BA087DB36C3B527943A51112DC8F645FF716A64D
40ECD5212C7CCF827778A1E9AD87CBEEC58CA13285BE67784E635CB6F00B3CC0
4597E840D61A67702745A32BCEFC1472A9E47F835105FCC00D6F166A8185BC81
281DC86B84ACB6CDDB174046ADF53A3BDDFF18CC1CD9D0DA9C4A067719E7BB11
93ED2E17D8CE09DDE72C82BBEFC17A008619BAFDEF8A58DEBB4AF12CF66BEE80
B7857245CDD912CE12A6BCBE6F0B2970371B72015DAEA81FD19B213464550E83
7914A2768E6DE0DD34393E6AE13BDD02DB8A8B29DE9928DDEF7FFF24D7996A1B
DFF6949DA66BA266C20B1222D2C99E7D0A488B29FACECB2EEFE98E37F08DEDD9
6523333BEBD957555627ABE83C8477316E5B60BC03A0855EA13C0275906EE8C6
350E2C5F9040359242A48A1657BF928DA4EACD8392943482C54DADD1C9769C26
1FD76524234520CFF96C20BCC42B7B7B565EC15ACE461B36A794B765EA4B369B
2B25D834753EE915FC12BD88753E12915C78AF55BAC4B8ACBE7A9DA0B8ACB863
92F9BE8C563BC23419A8B7E810DE66294A31A1283867BA00C35DB13AA47589D7
F91F56911FFD07FCE49ADD1AE8D7F334F9D026631CB3C7B6499B8E230322C224
D63CF430F6B58F5C8F85ADC266065F1BD591E2CA66B1B7DDD1A3B8160590DF82
ECAD756D4EE4D60C4254785A021FB37C51F81B23393B5D96F384E5A07956DB67
FDC07CC2A051D50C881F9342699631D915AEC2A2A7765B43462A527204AFFABC
20F691515747F508A2F124FCE375FB412C5D8FE50A458872206BC7D39B0694A9
184861281667504F3556A121D9FD28DC2BF1AFC7090FDF6F27795607A67CC3BE
7372DCDD66E3D08226BC04B661C36CA7492A877AD3358FDD057E2CB1796E2744
D6E7C6D465D81BF02D8EE2107E7E3948EDB4FB3CD4326F8AC946187A41A982E3
F2F7912856C7F5D7171CEBCA0A2BFD2538FED70B0E7346FB33CA0D1CA75B901B
516B1EC9A6B5958BBB7932633B3FC3DFA4944A6399CFF0566E1F5F193AA150A5
632B9550CF7828197605DF40DF88E227D33D353DB7373CE442FAF642DA572F24
44EF63D815ECB6D68CF079D9203CB36F830FFEBB747D659A0C31C51A5D6F77F9
E036991BFD6E957299006E20A7FD55BC54180AB33D4EB3D7E7AFBBFBA6C30FA4
0ABB60E61EFCB557DEDAA9BA6AAA249F6C4198680F89AC0DBAD7E51C116B04E4
016D15C323B9FA56A0B82EE0A1B9CCEF696EF7496DCC2A6CD82832BD4598D19F
BF342F4DF373E1AF8B64FC9C2D3851E6B87F83BDA67FDA203CD787540D553239
63E33EC24AB07EC87C019F3758E4C7A686DF67C52F950CA76157176FBB58235C
4D3A1EAF644E5728DB56C66A7B41AB8C6C59627BAB3184D4EF7FB003B75FEB52
03B856AC222674B4C1389BC957D5E4A51DD8905B60A0B6776D4D527870E16DEA
DB20201ECC5A201E1A6DB4DB40BBE2F44ED7720B7B9E6B23FACF67AC252A42B7
091FD06DA0FE8F676CCFE900910FE08B57FB7D97F0A2664FD5B658371637D416
0EE0798825110D2308A5F34E354293250249461473DFF1572E811B0C938E8FC5
1B40D27B1E5670A02A7F3BBC19ECDF2091A9DB6A892C49E310A6E5F47EA43190
ABB6316A08346ECCD6B8C5ED992AA02061BC3CE3725F0C1E66A38841DE0D0020
CC1A4CA146FD7A74BC2F43B8E220D066DDEEDA11E3C2F46386191A51B01127EA
F1850E187E3A5238A62059637E2EB863FA4C0008DB011AD3A728D399FF1247BD
3050A2226CDB909C80F9F584249409CFA746AB0C850656BE8D56B7D66CB542B5
61AB71C6F4B75A11B0766DE5AB0A9295CBDCE2E65A418515D54FDA4B0DADC6E0
5BA1DC6DD9DEF54AF02D7ABC82FBF084AF1652AEFD26937C312DF982089C2C93
F9AA6E7166AB8D2AA7DC9CB71139CDC17F165B0495F225C2F42635EF48F0B8FE
0ED371993090220CBA38B4E6C36C143918F21AC2078B194C12B665CA7B3B7C2A
FB01D7CDB00E12E816B705D7FD325515D74DCCC46B0DD3CA00A98E0DD190CF3A
C22181575B83A18FE4FD4790BCF58EB955FA362D20E6A0DAEC2D66CE629B19F9
D712385BEDBFCD6415A406FDE70B26452AA4D7DD874BB0B05B9F95DC997692CC
5A1FD255DCE23401B3CC9075DF755F5FB57FE8FE127790161EA3F88F362EF14A
426206EB27174B339768367D80D86C5CB67AE4089646112342D6802F276EA9F1
7F6FF3494A7FD011155FE3135899F87F7D7454451F4F3BF5AB187AD4158B9076
F44624892F7A23D9CC50C0D3550FFEE6CA388EFE0BFFC4E68C6184F3DEE6A094
64E44DD1D362CBC4EB32EABC88847ED99D630CDF672C7E27A13499119A51BF46
A78584379C0999B795B7574DBC18F412AA38472AABAD7A5F0BA3ADE9A67DA0C8
6B3B68D093E92EE1F97BE1701EEBD8A1FFB6C3A4771B519A0528B0F7B79DFFFB
E38E9173BFA6536AACEB9084AD573B92C6B343AAF61BF39B79CDD4E05E240B2A
0C3381FE2E1FE161E76755C1567A222D06C98C8257A89EF076C4EE381D954C67
DC0F76D92E04AF2866C920555DA6D94EBB83FBF1D945A966E2D3ABF22CB2AB67
F3025204B0949D8D019BAE5131C008731D8CC58E286D6D4500A3445F07B0D85F
AB05ACE78B75003720054C3F6B612CF7976A3A4F1FEB0A323D4663174E96B65F
A56E81D887222450C07BD1432881FB4ED7DDFF6C7266EC816C3C9C8A368E88C1
AB96081C2DF39A07A2E193FAE01E988193BF7466C67F6298144C3C1AA74FDC30
ABA4473B90A7CD2CC38875B44240081886E8AC726087AD9225229D9D7C55F162
7D6C68535054D17ECCFC5530ABB8D170972BFC830FAD326FE1DCE06417AEFFB5
6622B92CB7286B7B5275C478519F6DF7CD8E9485213DD847C9B5D3EFAA3FFB17
D95225C63989CB5FFE7FD2DB50A8E1633FA07A43F27F71CE11FC795574F3849C
C52E145ADE7AE5B055E58A0FB82503BA2A72F7065DBD01996CAF8B688C73FFAD
1B58444464EBB9027F344E6E78AEDE284BC7C30AA224199947F64152D124521D
2AC2FC6E23436757E92C29DD3E8BB70617E635D1FC199DC43B7261E2D882AD1F
683FECB027EC0F4B36FF92B78245EF7B5D76B6E564D3EE36E163A383F7ABF03E
A65D1BB2CD098B1CEE3FFC6131EF29E29F0C6EB81D98B2CDEA8E08160A546A32
AB773F0212DE20D911505BF8CBAFF08BB3D73D3EB1A4785A71769B4C1663F6E5
5113D3BBE8A1A64807D1AD698DC43298C1EE91A06A35D61166482639C4146245
5DDA115E56095BB186155D9E35EBB5359BA39A458322D62C82680FFF70DB20AD
BBA08444E3727DFAF6ABFFB91A32DDEE63CE4CEAB3BE10890C52182651C47701
34AA57292B2DC4BAB108CAF242C6DDE04C7899552E2EAA46B6D3B72599971611
F6955D09CC651CAC37541C6A99487212B6EF696ACB09C832CEE0179AB498BEE7
C8A21F15366632156F5C4C68EA74AE68BDF1DCAE14767622E93A89C066E46321
87443240D217A79B55411514C229485F8205C1D4904D6F3A6E41F12558E3D0C0
D4AE714898060AD2D6B0AF207C89145431339C8274C99448EEC208FCCCC8EE5A
2190F12558505D04EF1494D7D8070AD2FD53BE1AE94BB046E37AE80EBBDFB4AD
E1A966DC1F9E71E7F256F04378706517D2CA2494FAE22794BCEA8B686C16945F
021446B83C7FE2AF11BE846B54E59C1AE14B888B7CA3457DC197A125EE3D0488
77E6F4C88BDECD3C196620CA0E97877092A3843BEAC71F7D07563C52E36879D4
4446980B1E70CB1C26F3611B4FD0230D7A74992E0726A77F4241A933BBD7102D
2A80912D55BE7AD2448460439B888BE051E08A74902A088E091F518F9E3E7DFC
D513FEFF3E0BE61184F7ECDEBA52C5AA50900B9FE14F00822B5E22F9D6101B91
A1D6A0365B0884590547754FDE5F7CB43CE0D37176C37B8A4CEC1FE5AF016D35
487249425D8F213634FDE5B45FE19FBE5A4E44C8660B9B3F2182B6500B238852
AA97CFB39B8CE3F1940FB74932C7E76C3A4405077C3BC00AFE1CEA8B90434BC2
CF5DDB268284695162D017F91886E1E1DF8FE03F63B830C5EC56E6DD0CE50E33
7FF4E3766B6B3828DF03A1A6DB6B77081A4CC110BD41838DD8D6F8790F9AB98F
EA7D49D1348B72354ED1CE0662C4D0A9D9304BC0B0F638A490EF9C52065AE240
D41D84553C624959CEB36BB8EFED11AE0F08C1425C2D5A0975B5F3421EE7B0A2
2F1776D4C8D0A282A3714C66277C0DCD13D52032462D89B04DB3DC88938999F8
E84853B39DF8DB68E8B1994F85074F448078D1ECC7DE9B5331278515D75ABDEA
3EC39827AE4B67B16BE48DEBD12D6A884868F44C77FE86927F21A4977BD42271
65AC89A1B9A0A92C525C9ECD042FB09EB8A5020122F97E7F33587555A703D955
DAE189626714095C1AABF5025F21A14F790BD192CC251E1F1F9C5569AC6DA5DD
90FCA67CBA8765577077EEBDDF13F3D10A09788C7859440EFDC4CAC56C0C8BFA
BDA095C24B2BBC7849018B5A46A826AEEAC8CAE3861442082E418966F57F712F
A9FDC4FFDB954B59A3FD2AFAED2746411BD7A080D27EF156455E9880AE83114E
54063ABB38BB7C22130BFF578AAEBDF0C6A774A2A4239B0487C03C13741644EC
C81991CFB4112B966F1C09EB94EFD3DA6DEEA16BC39083E23F7771F0F47D1C39
CAC64802CCB156BF88C1FAB938931063BD719E0C9BE4A706484E363E6C87A985
6127C24492A2E3AC6CC967C558DBE02BBF362A933F34A9B09F115C71B736EBED
E1EFFD90E3804B02BB5A24081DF06B280B8B46A36F2B83AFD998B3567742015D
5FAC0893D49FB16D4ABB763A7D9D16E2F2CA685E7D73AA78182BCDAA70B3BF47
C8EF836B2C10BD70954D6AFD6EC545D4FD1B4DEBB570A3B19AF51B2D9681C146
23D8F51B0D284504F8069C1024CC16B8C16D846AC8FABD1744740B3DE847D41D
83887ADC8F774B5DA0074E7CDCFCC11DA0593DCEE95B247F632DF18F50120DC3
8C97205A49F4835A4716883D1A4D0AB12BDB5C004A83EE3A6228169B0996771D
EB0D5F0CA4D3E84D66C2CE439124D416A8BDEC34EE488C5D8BE8B12E599F1E58
996209331977232A4FA6637C57A87B29B85681260CEBD2167A198DE2E0ACDAD7
5BB6D7AEC05D9A88D6683A570818722BADA51766A0FBE8F06898CF6077C9BF78
2E0DB8CBA64338A3E31CE0B309472628610EDA66DA793347D3E0755A30BA168D
F32BA7E6D4B857651D7889BAEDE0FEB8D961FF772ED2A29C670315AF08BE6A42
4E922599C8CCCB16E028880670CE9B055D00E74304DD2DAE9D4CD2619694E978
55DB66153B5C41B7050859DBE8DE61CB9487264DC453381509B35ED57E677CED
B027CB2D38C04902C9D07E925E4409A475DFE275856A277C476A4548C9B3F41D
7D9A58FC3E5C1EB0FFD020DB4BF6904F298747216CC11FBA85C790C99485C7F5
EE91AAF0F0E0DD3D92473ACBBD1DD8B8D83C697872BBA2480739C403E772EA79
456C3585A74F4F2E3B951ADA4B9F74956DC157C48B66FB3CDD6E9530223798AC
60BD0958CF5CD632C34899014FEE951EB23D4157986090279F1B04D8DF00DE91
60F050CBDAABFDCA54F2D01CE6D5E3A4FF5423B65D55B68DC106B1203ABE8821
1DB64A0BB754252E8659C7FD53AB86A87513696B1322FAC0F8C9EABB76C7A4A2
43C1695E43C02A4FEAAA98B9A3571D01F17838460D0139ABC31375EA29B6EFC0
E52FBCFDA3672F376C0B53AE6D3A3252A78A8D37A64F9F3970CD3026EB403562
8CF43D1B2CA6B03266210DC91E706106F2A88EA59836C385D64EC9763539E792
866DC0847657AF6492156C060F6620F82BFB54F83C09A560DD64B799BC2C69B3
BF5979AF280922A9C057674E99BCA1152A0B9D502C5C940A89113B451709C5D5
71AB33A03C14E1C04C02EB284A5283681FCA2F6D76E40A6882C727097680CF9E
0C91C5B0A929F42BC3548F4F63C4FAEE141B593D21F8E49FA6DACA7FAFC7ECA6
4621478BAFE94BA3E94BBB5B44BD2D0F13DB048903B65BD132DB18A06BBF4299
954599558532EB4A2CB3BF6D38266EBED3539F031B0392CB2BF0CE02EECED59E
6945B5E6899BDD9DDAAD498897A98BA8D64C070B6A028E12618F19A93E1B63CE
582FF2596D96CF16B3B6200A5E77888B1DB1D65D2BB937FCA8AAE46D427C15DD
13F10BE4B15CDB76D2C8CEE44B644F29577044AE3D498AD20A6D8E8BC86BB8C6
134F47603CDB4A7050FBC09BF92974620E00DA39A55BBFE39B48A7049CD70D0E
AEA2A38CA7289D3C810398A34DC8E524E4929B74ABD72CED5C828B94B830B5C4
9B3DC78BB4607BE9321D804B181BE4C354DEA5BA5B488F64BC9CCDE104E7DAE1
3E732F1AE60A1799A4F08867462629B86ED007116BD7AE5D27F053A03674B1B2
0FD57AA25E4172A277F47B0177D68E3C3C4BB645BAAC065A277C41A0991DF8C7
292DA6A274322B57CF290C72E0D25C48C39C5D83575DC7A435DB05C6F1E6C7C1
EB8FD91B084C8CBE938BEB36D8AA343658DF5A7670AF7FE5E1D7DF3C3EFCE6E0
F1E1DF0FAB1195D5360FF9C94E93091F4B3B57C96D3E497698B0211D4A0F70F2
8395F210C36290DD25EC0F6B9C9D330F5219A750552CEAEAF004060571E21000
A52BA51E1787F2FC10753F522F99DF14FAC9EFA3DE122E750041488886102A87
ABA770B19A8B25ED1A5070644FA8996D0B1F41C55075F2C0F1A05CD2ED8DA855
CB200DEEF0924790DDFCE2627B9FCA8DAE24E4D5097DD2D7FE8AFC97F05774CA
61B00BDA46923302DEEA5DE840DFD28A4BB116F063283A919EA582A353380402
4A7E18C10914894BB871F9C231127E3C7BF84E9E9FC7EFA21FFA61E34BF043C4
594665513B6F1A0972B8896FA242125D82F79CAB28ED420AC0D13DF2076EA444
504916267535093540C590879F7541D8AB8976C5744F372A84D78F33DC15246A
C5B785CD244F19C8FADADE1350BE241DF2EBDCF11B2B6CDA6FFF89F2DB4711AA
1CF61B818AFBC9C9E44A0CDE781C8D4EBC82CD2A31195111976672397DE1BC08
F7C5BBF1E0AB69838106297C0F86998CF156CEB3E94D6CABDB4C883F283EB19D
6E33C56DB5B8F98EB3DF1A5BEF0E3E345D0AEA8B56A1CC6A0E82AB2A1D669E93
683E9C94AA639E076E52D093E2D48AF34BF0E33A11C782CA5CCBB76B6326EB22
85C08D0926C0B6DC111C32E0276144C6D28651392A44E5048DE050F2A8A73649
96BA907F0622EA516EC1546185FB26DC2F16E5CC1A8C30A7724FD3C87B9F0E73
B369425B3B8074699A26B533E7D990268B7199C9D857F0EC212D7830CBB522AE
A9AFC71F425DC03B6F96E151B40FEC30D24D38ADD00847C768526979A986446A
12016C031343DCB4005EE8D2DB056C073EE3026AEBBEF53FAD510E0E964787D5
7BF0F4EAC67714F0ABD869BF3E0BAD85CCF38336B50CEF2BE7F81B1C28350381
C88551706D66F8B619AA829AE8D5FC7F74B47C5A59D5E9A86B95806B32C9B867
06EB71D59ACD72AE56E34237C8832440A6640292AEDB38053D2734E9554C90E8
6C1D6269A31CC1FDC73FC10E968C31FE859F6F0392D746907AFC1E5842791755
FDB639BEEE38033382116E2A604DB043D6AA9EA25078B1BE2AF810E2D30B640E
37120D3C8D028C8A6B8E1001DD56D65B4CC710A0AC057352BF7A41395E6F1E97
1503AE04F011418312AC77F882EAA1906562E9175F0EC9DAC2734E1840EEBF98
D94C14D9B925884028D6A35CAF568A88D1B79C844446BC2D8CF52A2F3A54650D
2921E9827C79D5D9127521D5531852E47C58B88AB8533324C98332D21D445D6E
C0BA9084E89CA66DF2388DA38F16DB5A98B020C2CBBCD261469C6F50940CB552
4036A9F4DE8A68035152D14D70A65DD3DE6658DAEE6368A3597CAB86366578C2
6A7C8627F810303C25D3C16D2E8309852684901989E20F218488BD498C725A39
3E67142F84168582B3E9155F124C9810A4683EDAD9091DC0AA2A2E14AA62958E
C77C983E6418D302C35A043097E1B4508AB761F5C776AFCBD880AA865A3B7C82
BE7DAC073EAB078FF9FF409D1DB7F4CF593E5EDDE453F674F935579BF89F6F96
87315F76A1277DBD3CFC3A2CABAD2580B1FDCB5AECE860D972AFD753D069588A
6D675A449B0B58010BF82508433AADC7C58E2F44AA838DF457960218EE606C89
0D4381A3E1FCC0F64C6710FA00FE457B075CABB4E8B0CF0971E889FF22536399
125D064012EB8107FDFF0F1217899678240100
}
; Below is an example of how to format a menu block.
; Note that there are two menus in the following
; block. The "file" menu is indented and spread
; across several lines. The edit menu is all on
; one line. Notice that you can place action blocks
; after each menu item, to be performed whenever the
; menu item is selected - as with the [print "You
; chose Item 1"] block below:
menu-data: [
file: item "File"
menu [
new: item "Item 1" [print "You chose Item 1"]
open: item "Item 2" ; icons [1.png 2.png]
---
recent: item "Look In Here..."
menu [
item "WIN A PRIZE!"
item "Try door number two"
]
---
exit: item <Ctrl-Q> "Exit"
]
edit: item "Edit" menu [item "copy" item "paste"]
]
; Pay particular attention to the action block in the menu
; style definition below. You'll change it to execute your
; own selected functions for each possible menu choice.
; Most of the style definition is totally optional. It's
; designed to look like a native Microsoft menu. The
; example at
; http://www.rebol.org/library/scripts/menu-system-demo.r
; contains many more examples of menu styles and options.
; The only part that's required in the example below is
; the action block in the "item style" section. Everything
; else serves only to adjust the cosmetic appearance of the
; menu:
winxp-menu: layout-menu/style copy menu-data xp-style: [
menu style edge [size: 1x1 color: 178.180.191 effect: none]
color white
spacing 2x2
effect none
item style
font [name: "Tahoma" size: 11 colors: reduce [
black black silver silver]]
colors [none 187.183.199]
effects none
edge [size: 1x1 colors: reduce [none 178.180.191]
effects: []]
action [
; Change the lines below to fit your needs.
; You can use the action block of each item
; in the switch structure to run your own
; functions. "item/body/text" refers to the
; selected menu item. This does the exact same
; thing as including a code block for each item
; in the menu definition above (i.e., you can
; put the [quit] block after the "exit" item
; above, and it will perform the same way -
; just like the "[print "You chose Item 1"]"
; block after the "new" item above).
switch/default item/body/text [
"exit" [quit]
"WIN A PRIZE!" [alert "You win!"]
"Try door number two" [alert "Bad choice :("]
] [print item/body/text] ; default thing to do
]
]
; Here is a simple function to trap the GUI close event. This
; must be included whenever the menu module is used, or a
; portion of the application will continue to run after being
; shut down.
evt-close: func [face event] [
either event/type = 'close [quit] [event]
]
insert-event-func :evt-close
; And finally, here's the user interface:
window: layout/size [
; The line below shows the winxp style menu:
at 2x2 app-menu: menu-bar menu menu-data menu-style xp-style
; The line below shows the same menu, whenever the button
; is clicked:
at 150x200 btn "Menu Button" [
show-menu/offset window winxp-menu
0x1 * face/size + face/offset - 1x0
]
] 400x500
view center-face window
23. Understanding the CGI Interface and Web Programming with Rebol
To understand CGI web programming, it's helps to have a little perspective. In mainstream computing, there are three ways users commonly interact with programs to input/return data. You've already seen examples of two methods:
1) The shell: data can be input/viewed directly in the Rebol interpreter. In short scripts, the "ask" and "print" words can be used to enter and display data. In this format, program flow can be manipulated by requesting text options from the user, and using conditional operations to react to input. Simple menus can be created by printing lists of choices to be selected by the user:
Rebol []
forever [
prin "^(1B)[J"
print "Select from the following options:^/"
print "1 - Print message 1 time"
print "2 - Print message 2 times"
print "3 - Print message 3 times"
print "4 - Quit^/"
answer: to-integer ask "Your choice? "
if answer = 4 [ask "^/Goodbye! Press any key to end." quit]
print ""
loop answer [print "Rebol is great!"]
print ""
ask "Press [ENTER] to continue"
]
2) GUIs: within graphic user interfaces, text is entered and displayed in text fields, areas, and list views. Program flow is manipulated by responding to mouse clicks on graphic buttons, menu lists, and other widgets:
Rebol []
view layout [
text "How many times should the message be displayed?"
choice "1" "2" "3" [
loop to-integer value [alert "Rebol is great!"]
]
]
3) CGI: this interface allows data to be input via a web page, using text fields, areas, and dropdown boxes in an html form. Data entered into a form is submitted and processed by the program that you create and store on your web server. Returned data is displayed as formatted text, tables, and other html elements that are dynamically created and output by your program.
There are two parts:
1) Html page:
<HTML><HEAD><TITLE>Data Entry Form</TITLE></HEAD><BODY>
<FORM ACTION="http://yourwebserver/yourrebolscript.cgi">
How many times should the message be displayed? <BR>
<INPUT TYPE="TEXT" NAME="times" SIZE="25">
<INPUT TYPE="SUBMIT" NAME="Submit" VALUE="Submit">
</FORM>
</BODY></HTML>
2) CGI script:
#!/home/youruserpath/rebol -cs
REBOL []
print "content-type: text/html^/"
print [<HTML><HEAD><TITLE>"Page title"</TITLE></HEAD><BODY>]
times: decode-cgi system/options/cgi/query-string
loop to-integer times/2 [print ["Rebol is great!" <BR>]]
print [</BODY></HTML>]
The first two options are typically used to create programs that run on a local computer. They're appropriate for programs that manipulate local user databases, games, audio/video applications, etc. The CGI option is useful when you want to create programs for the Internet.
All three methods above can be used to manipulate data stored online. You can, for example, write a program that uses a local shell or GUI interface to access and manipulate data stored on a web server. Multi-user business applications that share databases among employees at different locations, for example, are well suited to that type of client-server design. Each user gets a copy of the program, and everyone connects to a centralized data repository that exists at one location on a shared network.
To present data on the Internet, however, you don't always want to force users to download and install a local application. Contemporary computer users are familiar with custom database queries, web page searches, specialized email feedback forms, etc. that can be accessed directly on web pages. Rebol's built-in CGI interface enables this possibility, allowing the language to interact directly with information entered via web pages. Rebol's CGI ability allows you to write code that runs on a web server, receives data input from web pages, and returns data to the user's browser. In this way, html pages on your web site can form the complete user interface to programs you write. PHP, PERL and ASP are popular languages for such Internet programming, but if you know Rebol, you don't need to learn them. You can upload the Rebol interpreter to your web server and write cgi applications in the powerful and simple Rebol language syntax (just choose the Rebol version for the operating system running on your server). With CGI, users can interact entirely via a familiar web page interface, without ever knowing or caring that a program exists behind the scenes. That's one of the most common types of computer application in contemporary use. And Rebol makes it easy to create those types of programs.
The detailed mechanics of CGI programming are slightly beyond the scope of this tutorial, but the following crash course will get you started:
Learning html is required. That's true if you intend to attempt any type of Internet programming. Html is the layout language used to format text and GUI elements on all web pages. Html is not really a programming language - it doesn't let you manipulate data. It's just a simple markup format that allows you to shape the visual appearance of pages viewed in a browser.
In html, items on a web page are enclosed between starting and ending "tags":
<STARTING TAG>Some item to be included on a web page</ENDING TAG>
There are tags to effect the layout in every possible way. To bold some text, for example, do the following:
<STRONG>some bolded text</STRONG>
To create a table with three rows of data, do the following:
<TABLE border=1>
<TR><TD>First Row</TD></TR>
<TR><TD>Second Row</TD></TR>
<TR><TD>Third Row</TD></TR>
</TABLE>
Notice that every
<opening tag>
in the code above is followed by a corresponding
</closing tag>
Some tags surround all of the page, some tags surround portions of the page, and they're often nested inside one another to create more complex designs.
A minimal format to create a web page is shown below. Notice that the title is nested between head tags, and the entire document is nested within html tags. The page content seen by the user is surrounded by body tags:
<HTML><HEAD><TITLE>Page title</TITLE></HEAD><BODY>
A bunch of text and html formatting goes here...
</BODY></HTML>
If you save the above code to a text file called "yourpage.html", upload it to a web server, and surf to http://yourwebserver.com/yourpage.html , you'll see in your browser a page entitled "Page title", with the text "A bunch of text and html formatting goes here...". All web pages work that way - in fact, this very tutorial is an html document. Click View -> Source in your browser, and you'll see the html tags that are used to format the document.
You can create a CGI program on your web site that outputs the same web page above. Here's the format:
#!/home/youruserpath/rebol -cs
REBOL []
print "content-type: text/html^/"
print [<HTML><HEAD><TITLE>"Page title"</TITLE></HEAD><BODY>]
print ["A bunch of text and html formatting goes here..."]
print [</BODY></HTML>]
If you save the code above to a text file called "yourpage.cgi", upload it to a web server where you have Rebol installed in {youruserpath}, and surf to http://yourwebserver.com/yourpage.cgi , you'll see in your browser the exact same web page as in the previous example (to make it work, there will likely be some additional setup steps required to install Rebol - that's covered below). The first three lines in the code above are required to make your CGI scripts work properly. Memorize them by rote. The additional lines simply print out the html contained in the previous example. Simple, right?
That may seem like a lot of work to produce the same result as the previous example, but it enables the real power of CGI, which is to include dynamic, changeable information on your page. Say, for example, that you want to display the current time and date in the body of your web page. Html doesn't allow you do that, but the CGI program below does:
#!/home/youruserpath/rebol -cs
REBOL []
print "content-type: text/html^/"
print [<HTML><HEAD><TITLE>"Page title"</TITLE></HEAD><BODY>]
print ["The current date and time is: " now]
print [</BODY></HTML>]
The fifth line above contains the built in Rebol word "now", which is evaluated and printed on the web page. In the same way, you can read and print data that's been saved to the hard drive on your web server, you can perform calculations and other data manipulations such as conditional evaluations and loops, you can access databases, send emails, create and manipulate images and sounds, and do anything else that the programming language is capable of. Output data can be formatted as a nice web page that your user sees online.
The example below is a short program that reads and sorts the contents of a text file called "users.txt" and displays it on your web page:
#!/home/youruserpath/rebol -cs
REBOL []
print "content-type: text/html^/"
print [<HTML><HEAD><TITLE>"Page title"</TITLE></HEAD><BODY>]
if exists? %users.txt [data: sort read/lines %users.txt print data]
print [</BODY></HTML>]
If you want user data to be input into your CGI program, you need to create an html page with a "form". Html forms include text entry fields, dropdown selection boxes and other widgets that allow for data entry. The form should contain a single "action" that links to the web address of your CGI program. The form template below contains a text entry field and an action that points to http://yourwebserver/yourrebolscript.cgi .
<HTML><HEAD><TITLE>Data Entry Form</TITLE></HEAD><BODY>
<FORM ACTION="http://yourwebserver/yourrebolscript.cgi">
<INPUT TYPE="TEXT" NAME="username" SIZE="25">
<INPUT TYPE="SUBMIT" NAME="Submit" VALUE="Submit">
</FORM>
</BODY></HTML>
To do something useful with the data sent by the above html form, you'll need to write a CGI program. That program will be stored at http://yourwebserver/yourrebolscript.cgi . To extract the information submitted by the form, include the following line in your Rebol CGI program:
a-word: decode-cgi system/options/cgi/query-string
You can use the assigned word above to refer to all the data sent when the html form was submitted. Rebol automatically assigns words to the data input in the html form. If you type "Fred Thompson" as the name in the text entry field on the form above, Rebol will decode it as:
[username: "Fred Thompson"]
You can use that data in a CGI program as follows:
#!/home/youruserpath/rebol -cs
REBOL []
print "content-type: text/html^/"
print [<HTML><HEAD><TITLE>"Page title"</TITLE></HEAD><BODY>]
username: decode-cgi system/options/cgi/query-string
print ["Hello " second username "!"]
print [</BODY></HTML>]
Just save that CGI code as http://yourwebserver/yourrebolscript.cgi , as referenced in the form action in the html page above. When you enter "Fred Thompson" in the html page, it sends the info to the CGI script, which displays a dynamically created web page saying "Hello Fred Thompson!".
Here's a cgi application that prints output which includes an html form layout. The action of that form points to the address of the CGI program itself (i.e., no additional html page is required to make this script work - it prints one out for its own use). The program outputs a simple page that displays the names of all its former visitors, lets you enter your name, and then calls itself when you click submit, which starts the process over again:
#!/home/youruserpath/rebol -cs
REBOL []
print "content-type: text/html^/"
print [<HTML><HEAD><TITLE>"Page title"</TITLE></HEAD><BODY>]
print ["Here's a list of visitors who've signed this page:" ]
print [<BR>] ; prints a carriage return
either exists? %users.txt [
newname: decode-cgi system/options/cgi/query-string
if newname/2 <> none [
write/append %users.txt join "<BR>" [newname/2]
]
signatures: read %users.txt
print signatures
] [write %users.txt ""]
print [<FORM ACTION="http://yourwebserver/yourrebolscript.cgi">]
print [<BR><HR><BR>"Please enter your name:"<BR>]
print ["Name: "<INPUT TYPE="TEXT" NAME="username" SIZE="25">]
print [<INPUT TYPE="SUBMIT" NAME="Submit" VALUE="Submit">]
print [</FORM>]
print [</BODY></HTML>]
If you memorize the syntax of the previous two examples, you're well on your way to being able to program dynamic web applications with Rebol. The two-part template required to create CGI programs is the same regardless of how complex a program you create:
- Design an html web page with a form that gets all the input required from the user. Point the form action to the url of your CGI script.
- Design a Rebol CGI program to process the data input via the web form. The first three lines should be the same as the format shown in the template. Use "decode-cgi system/options/cgi/query-string" to automatically assign variable words to the data entered on the web form. To provide program output, print html formatted content to the user's browser using standard "print" statements. You can include an html form in the formatted output to call another CGI application.
Learning to use CGI comfortably requires a working knowledge of html. The required basics are very easy to learn. You can study enough html in one day to begin programming CGI applications in Rebol. Understanding how html forms work is the most important part. Try the search below for more information:
http://www.google.com/search?q=html+tutorial
Rebol CGI programming is covered in greater depth at the following links:
http://rebol.com/docs/cgi1.html
http://rebol.com/docs/cgi2.html
http://rebol.com/docs/cgi-bbs.html
http://www.rebol.net/cookbook/recipes/0045.html
The site below provides a free downloadable web server program written entirely in Rebol:
http://plain.at/vpavlu/plain-dev/r80v5/r80v5.html
It provides an alternative to CGI programming that lets you include Rebol code directly in your html pages, using a format similar to the PHP programming language. Using the above server, you can include Rebol code to be evaluated directly in your web pages using the following syntax:
<?rebol {Rebol code to be evaluated} ?>
For example:
<HTML>
<BODY>
The time is currently: <?rebol print now/time ?>.
</BODY>
</HTML>
That way, a CGI interface isn't even required. It's a convenient option.
24. Parsing
The built-in function "parse" is an important part of the Rebol language. It's used to import and convert organized chunks of external data into the block format that Rebol recognizes natively. It also provides a means of dissecting, searching, comparing, extracting, and acting upon organized information within unformatted text data. Its pattern matching abilities are similar to regular expressions, found in Perl and other languages. As with many things, the Rebol way is powerful, elegant and simple to use.
The basic format for parse is:
parse <data> <matching rules>
Parse has several modes of use. The simplest mode just splits up text at common delimiters and converts those pieces into a Rebol block. To do this, just specify "none" as the matching rule. Common delimiters are spaces, commas, tabs, semicolons, and newlines. Here are some examples:
text1: "apple orange pear"
parsed-block1: parse text1 none
text2: "apple,orange,pear"
parsed-block2: parse text2 none
text3: "apple orange pear"
parsed-block3: parse text3 none
text4: "apple;orange;pear"
parsed-block4: parse text4 none
text5: "apple,orange pear"
parsed-block5: parse text5 none
text6: {"apple","orange","pear"}
parsed-block6: parse text6 none
text7: {
apple
orange
pear
}
parsed-block7: parse text7 none
All of the above parsed blocks evaluate to ["apple" "orange" "pear"]. That's useful because it enables easy importing of data files created by other programs. You could, for example, use the "none" rule to import CSV files created by a spreadsheet or database application. The data is automatically converted to native Rebol data blocks for use in your scripts.
If you need to split files based on some character other than the common delimiters, you can specify the delimiter as a rule. Just put the delimiter in quotes:
text: "apple*orange*pear"
parsed-block: parse text "*"
text: "apple&orange&pear"
parsed-block: parse text "&"
text: "apple & orange&pear"
parsed-block: parse text "&"
You can also include mixed multiple characters to be used as delimiters:
text: "apple&orange*pear"
parsed-block: parse text "&*"
text: "apple&orange*pear"
parsed-block: parse text "*&" ; the order doesn't matter
All of the above parsed blocks also evaluate to the same ["apple" "orange" "pear"]. Using the "splitting" mode of parse is a great way to get formatted tables of data into your Rebol programs.
Splitting the text below by carriage returns, you run into a little problem:
text: { First Name
Last Name
Street Address
City, State, Zip}
parsed-block: parse text "^/"
; ^/ is the Rebol symbol for a carriage return
Spaces are included in the parsing rule by default (parse automatically splits at all empty space), so you get a block of data that's more broken up than intended:
["First" "Name" "Last" "Name" "Street" "Address" "City,"
"State," "Zip"]
You can use the "/all" refinement to eliminate spaces from the delimiter rule. The code below:
text: { First Name
Last Name
Street Address
City, State, Zip}
parsed-block: parse/all text "^/"
converts the given text to the following block:
[" First Name" " Last Name" " Street Address"
" City, State, Zip"]
Now you can trim the extra space from each of the strings:
foreach item parsed-block [trim item]
and you get the following parsed-block, as intended:
["First Name" "Last Name" "Street Address" "City, State, Zip"]
The example above could be adjusted to pull unformatted lines of info from any text file into your Rebol application. But that's not all parse can do. You can also use it to check whether any specific data exists within a given block. To do that, specify the rule (matching pattern) as the item you're searching for. Here's an example:
parse ["apple"] ["apple"]
parse ["apple" "orange"] ["apple" "orange"]
Both lines above evaluate to true because they match exactly. IMPORTANT: By default, as soon as parse comes across something that doesn't match, the entire expression evaluates to false, EVEN if the given rule IS found one or more times in the data. For example, the following is false:
parse ["apple" "orange"] ["apple"]
But that's just default behavior. You can control how parse responds to items that don't match. Adding the words below to a rule will return true if the given rule matches the data in the specified way:
- "any" - the rule matches the data zero or more times
- "some" - the rule matches the data one or more times
- "opt" - the rule matches the data zero or one time
- "one" - the rule matches the data exactly one time
- an integer - the rule matches the data the given number of times
- two integers - the rule matches the data a number of times included in the range between the two integers
The following examples are all true:
parse ["apple" "orange"] [any string!]
parse ["apple" "orange"] [some string!]
parse ["apple" "orange"] [1 2 string!]
You can create rules that include multiple match options - just separate the choices by a "|" character and enclose them in brackets. The following is true:
parse ["apple" "orange"] [any [string! | url! | number!]]
You can trigger actions to occur whenever a rule is matched. Just enclose the action(s) in parentheses:
parse ["apple" "orange"] [any [string!
(alert "The block contains a string.") | url! | number!]]
You can skip through data, ignoring chunks until you get to, or past a given condition. The word "to" ignores data UNTIL the condition is found. The word "thru" ignores data until JUST PAST the condition is found. The following is true:
parse [234.1 $50 http://rebol.com "apple"] [thru string!]
The real value of pattern matching is that you can search for and extract data from unformated text, in an organized way. The word "copy" is used to assign a variable to matched data. For example, the following code downloads the raw html from the Rebol homepage, ignores everything except what's between the html title tags, and displays that text:
parse read http://rebol.com [
thru <title> copy parsed-text to </title> (alert parsed-text)
]
Here's a useful example that removes all comments from a given Rebol script (any part of a line that begins with a semicolon ";"). This code is based on the script at http://www.rebol.org/library/scripts/uncomment.r . First it asks for the file name, and assigns the contents of that file to the variable word "code". Using the "to" and "thru" words, the content is then parsed for any text that begins with the ";" character, and ends with a newline. The action block of the parse rule uses the built-in "remove/part" function to remove that parsed text from the code. Finally, the parsed code is send to the built-in Rebol text editor for viewing, saving, etc.:
code: read to-file request-file
parse/all code [any [
to #";" begin: thru newline ending: (
remove/part begin ((index? ending) - (index? begin))) :begin
]
]
editor code
To learn more about mining raw data with parse, see the following links:
http://www.codeconscious.com/rebol/parse-tutorial.html
http://www.rebol.com/docs/core23/rebolcore-15.html
http://en.wikibooks.org/wiki/REBOL_Programming/Language_Features/Parse
http://www.rebolforces.com/zine/rzine-1-06.html#sect4.
25. Other Topics
25.1 Common Errors
Listed below are solutions to a variety of common errors you'll run into when first experimenting with Rebol:
1) "** Syntax Error: Script is missing a REBOL header" - Whenever you "do" a script that's saved as a file, it must contain at least a minimum required header at the top of the code. Just include the following text at the beginning of the script:
Rebol []
2) "** Syntax Error: Missing ] at end-of-script" - You'll get this error if you don't put a closing bracket at the end of a block. You'll see a similar error for unclosed strings. The code below will give you an error, because it's missing a "]" at the end of the block:
fruits: ["apple" "orange" "pear" "grape"
print fruits
Instead it should be:
fruits: ["apple" "orange" "pear" "grape"]
print fruits
Indenting blocks helps to find and eliminate these kinds of errors.
3) "** Script Error: request expected str argument of type: string block object none" - This type of error occurs when you try to pass the wrong type of value to a function. The code below will give you an error, because Rebol automatically interprets the website variable as a url, and the "alert" function requires a string value:
website: http://rebol.com
alert website
The code below solves the problem by converting the url value to a string before passing it to the alert function:
website: to-string http://rebol.com
alert website
4) "** Script Error: word has no value" - Miss-spellings will elicit this type of error. You'll run into it any time you try to use a word that isn't defined (either natively in the Rebol interpreter, or by you, in previous code):
wrod: "Hello world"
print word
5) IMPORTANT: Here's a quirk of Rebol that doesn't elicit an error, but which can cause confusing results, especially if you're familiar with other languages:
unexpected: [
empty-variable: ""
append empty-variable "*"
print empty-variable
]
do unexpected
do unexpected
do unexpected
The line:
empty-variable: ""
doesn't re-initialize the variable to an empty state. Instead, every time the block is run, "empty-variable" contains the previous value. In order to set the variable back to empty, as intended, use the word "copy" as follows:
unexpected: [
empty-variable: copy ""
append empty-variable "*"
print empty-variable
]
do unexpected
do unexpected
do unexpected
6) Load/Save, Read/Write, Mold, Reform, etc - another point of confusion you may run into initially with Rebol has to do with various words that read, write, and format data. When saving data to a file on your hard drive, for example, you can use either of the words "save" or "write". "Save" is used to store data in a format more directly usable by Rebol. "Write" saves data in a raw, 'unRebolized' form. "Load" and "read" share a comparable relationship. "Load" reads data in a way that is more automatically understood and put to use in Rebol code. "Read" opens data in exactly the format it's saved, byte for byte. Generally, data that is "save"d should also be "load"ed, and data that's "write"ed should be "read". For more information, see the following Rebol dictionary entries:
http://rebol.com/docs/words/wload.html
http://rebol.com/docs/words/wsave.html
http://rebol.com/docs/words/wread.html
http://rebol.com/docs/words/wwrite.html
Other built-in words such as "mold" and "reform" help you deal with text in ways that are either more human-readable or more natively readable by the Rebol interpreter. For a helpful explanation, see http://www.rebol.net/cookbook/recipes/0015.html.
7) Order of precedence - Rebol expressions are always evaluated from left to right, regardless of the operations involved. If you want specific mathematical operators to be evaluated first, they should either be enclosed in parenthesis or put first in the expression. For example, to the Rebol interpreter:
2 + 4 * 6
is the same as:
(2 + 4) * 6 ; the left side is evaluated first
== 6 * 6
== 36
This is contrary to other familiar evaluation rules. In many languages, for example, multiplication is typically handled before addition. So, the same expression:
2 + 4 * 6
is treated as:
2 + (4 * 6) ; the multiplication operator is evaluated first
== 2 + 24
== 26
In Rebol, the left-right order of precedence is consistent, easy to remember, and holds true for all evaluated expressions. Line terminators are not even required, as in other languages (i.e., semicolons in C-like languages), so the following example:
save %text.txt read http://rebol.com print sort read %text.txt
may be written a bit more intuitively like this:
save %text.txt (read http://rebol.com)
print (sort (read %text.txt))
That whole script is evaluated as follows: The "save" function accepts two parameters - first a filename, then the data to be written. That data is read from http://rebol.com (the next expression in order). Next in line, the "print" function takes one parameter. That data is the sorted text read from the text.txt file. The whole thing is simply evaluated left to right. Becoming accustomed to this thought process can be the source of some initial errors if you're already used to other languages.
Trapping Errors:
There are several simple ways to keep your program from crashing when an error occurs. The words "error?" and "try" together provide a way to check for and handle expected error situations. For example, if no Internet connection is available, the code below will crash abruptly with an error:
html: read http://rebol.com
The adjusted code below will handle the error more gracefully:
if error? try [html: read http://rebol.com] [
alert "Unavailable."
]
The word "attempt" is an alternative to the "error? try" routine. It returns the evaluated contents of a given block if it succeeds. Otherwise it returns "none":
if not attempt [html: read http://rebol.com] [
alert "Unavailable."
]
To clarify, "error? try [block]" evaluates to true if the block produces an error, and "attempt [block]" evaluates to false if the block produces an error.
For a complete explanation of Rebol error codes, see: http://www.rebol.com/docs/core23/rebolcore-17.html.
25.2 6 Rebol Flavors
This tutorial covers a version of the Rebol language interpreter called Rebol/View. Rebol/View is actually only one of several available Rebol releases. Here's a quick description of the different versions:
- View - free to download and use, it includes language constructs used to create and manipulate graphic elements. View comes with the built-in dialect called "VID", which is a shorthand mini-language used to display common GUI widgets. View and VID dialect concepts have been integrated throughout this document. The "layout" word in a typical "view layout" GUI design actually signifies the use of VID dialect code in the enclosed block. The VID dialect is used internally by the Rebol interpreter to parse and convert simple VID code to lower level View commands, which are composed from scratch by the rudimentary display engine in Rebol. VID makes GUI creation simple, without the need to deal with graphics at a rudimentary level. But for fine control of all graphic operations, the full View language is exposed in Rebol/View, and can be mixed with VID code. View also has a built-in "draw" dialect that's used to compose and alter images on screen. Aside from graphic effects, View has built in sound, and access to the "call" function for executing command line applications. The newest official releases of View can be download from http://rebol.com/view-platforms.html. The newest test versions are at http://www.rebol.net/builds/. Older versions are at http://rebol.com/platforms-view.html.
- Core - a text-only version of the language that provides basic functionality. It's smaller than View (about 1/2 the file size), without the GUI extensions, but still fully network enabled and able to run all non-graphic Rebol code constructs. It's intended for console and server applications, such as CGI scripting, in which the GUI facilities are not needed. Core is also free and can be download from http://rebol.com/platforms.html. Newest versions are at http://www.rebol.net/builds/. Older versions are at http://rebol.com/platforms-core.html.
- View/Pro - created for professional developers, it adds encryption features, Dll access and more. Pro licenses are not free.
- SDK - also intended for professionals, it adds the ability create stand-alone executables from Rebol scripts, as well as Windows registry access and more to View/Pro. SDK licenses are not free.
- Command - another commercial solution, it adds native access to common database systems, SSL, FastCGI and other features to View/Pro.
- Command/SDK - combines features of SDK and Command.
Some of the functionalities provides by SDK and Command versions of Rebol have been enabled by modules, patches, and applications created by the Rebol user community. For example, mysql and postgre database access, dll access, and stand-alone executable packaging can be managed by free third party creations (search rebol.org for options). Because those solutions don't conform to official Rebol standards, and because no support for them is offered by Rebol Technologies, commercial solutions by RT are recommended for critical work.
25.3 Spread the Word: 7 Reasons to Learn and Use Rebol
Rebol is an extraordinary tool, but it's relatively young and unknown. To become more useful and mature, it needs the support of a growing user base. New modules, dialects, and useful functions are created by active coders, and that helps make Rebol even more powerful. If you like the benefits that Rebol offers, take a moment and send this link to a friend. Doing that will help to spread the "Rebolution" :)
- Compared to other development environments, Rebol is tiny. In it's heaviest uncompressed form, the Rebol interpreter is ~600k. You can distribute the entire compressed language, including the interpreter, built-in documentation, and more features than can be found in other multimegabyte development environments, in 100k-400k - depending on the version you choose.
- Code written in Rebol is able to run unchanged on over 40 operating systems. Aside from providing a great development solution for cross platform tasks, Rebol is also a useful power tool for daily work. It provides a consistent, simple interface to help you get things done on any operating system (as a multiplatform file manager, text editor, calculator, email client, ftp client, newsreader, image viewer/editor, etc.).
- Rebol/View and Rebol/Core are free for commercial and non-commercial use.
- Rebol is easy to learn. Even absolute beginners can learn to complete complex and useful programming tasks within weeks. A comprehensive variety of built in data types, native network connectivity, super-simple GUI creation, easy data manipulation and parsing features, and a consistent interface to all types of files, data, and functions make it straightforward to use. There is not a simpler language to learn, and Rebol's consistent language style/syntax is maintained throughout the learning curve.
- Rebol isn't limited to certain types of applications. You can use it to build an enormous variety of programs with modern graphics, CGI interfaces for web programming, network functionality, database connectivity, and much more. Instead of learning and using a variety of languages and development tools to complete various tasks, you can focus on improving your skills in one simple paradigm.
- The Rebol user community is friendly and knowledgeable. Help for most problems and questions is generally available on the Rebol mailing list, and the response time is usually very quick (Rebol users love to demonstrate just how capable and elegant Rebol is).
- The Rebol language has the potential to build dialects that are strikingly human-readable and lightweight. Like any other programming language, it makes use of variables, function definitions, object constructs, etc., but it's got some unusual twists that enable language extensions which are simple for even inexperienced programmers to implement. The built-in dialects in Rebol demonstrate just how accessible Rebol's underlying syntax concepts can make complex tasks, even for untrained coders. Because of that, Rebol offers new potential for average users to program their computers.
26. What Next?
At this point, you've seen a useful set of concepts and techniques, but you've only just begun.
If you take a close look at the code that's been presented in this tutorial, play with it and absorb the full meaning of how it works, you'll understand some of the real power, flexibility, and ease with which Rebol allows you to store, retrieve, display, and otherwise manipulate data in any computer environment. By using the built in function words in Rebol, you can do a lot of useful programming without writing much code, and with a very easy learning curve. You can create GUIs to input, save, transfer, and display data. You can read, send, alter, and display the text of emails and web pages. You can create, view, alter and manipulate images and sounds. You can display, sort, filter, and organize database-like blocks of text and binary data.
Remember that Rebol can run on just about any computer, web server, etc., and can allow you to deal with all types of data just as easily: text, images, sounds, money values, times and dates, web addresses, etc. It has built in functions to easily work with networks. Your Rebol programs can be written in parts that run on separate machines and work in tandem by communicating across the Internet. When the language doesn't have words or data types to do exactly what you want, you can create your own functional words and blocks that do the collection of actions to the collection of data you want to work with. Once you understand how to save data using word variables and block structures, and perform actions with homemade functions, you're ready to delve more deeply into the syntax and thought processes that make the language flow.
Begin to study and understand the logic and work flow that occurs in longer programs. Figure out what types of data you want your programs to work with. Figure out how to get that data into your programs (input from a GUI, read from a local hard drive or CD, downloaded from a remote web site or email server, output from another program and saved to disk, etc.). Determine data structures which can represent it in your program (simple variables, simple block lists, compound block structures, database systems, etc.). Decide where you want data to be saved (on a local disk, on a file server in your local network, on a web server on the Internet, etc). Decide what actions you want to perform on that data (organizing, sorting, categorizing, editing, erasing, filtering, separating, transferring, displaying, listening to, etc.), and use/create functions that perform those actions. In Rebol, all those things are easily understood and accomplished. Familiarize yourself with common approaches to managing typical situations in the types of programs you want to write. Become familiar with the data structures, functions, conditional operations, loops, and general code flow seen in those types of programs. Read and memorize code for programs that others have written. Explore and memorize all the built-in words in the language. Learn how others create and use new words to solve common programming problems.
By doing those things, you'll learn to speak the Rebol language fluently. The tutorial at http://www.rebol.com/docs/rebol-tutorial-3109.pdf provides a nice summary of fundamental concepts. It's a great document to read next. To learn Rebol in earnest, start by reading the Rebol core users manual: http://rebol.com/docs/core23/rebolcore.html. It covers all of the data types, built-in word functions and ways of dealing with data that make up the Rebol/Core language (but not the graphic extensions in View). It also includes many basic examples of code that you can use in your programs to complete common programmatic tasks. Also, be sure to keep the Rebol function dictionary handy whenever you write any Rebol code: http://rebol.com/docs/dictionary.html. It defines all the words in the Rebol language and their specific syntax use. It's also helpful in cross-referencing function words that do related actions in the language. Along the way, read the Rebol View and VID documents at: http://rebol.com/docs/easy-vid.html , http://rebol.com/docs/view-guide.html , and http://rebol.com/docs/view-system.html. Those documents explain how to write Graphical User Interfaces in Rebol. Once you've got an understanding of the grammar and vocabulary of the language, dive into the Rebol cookbook: http://www.rebol.net/cookbook/. It contains many simple and useful examples of code needed to create real-world applications. When you've read all that, finish the rest of the documents at http://rebol.com/docs.html to get more specific examples about how to program using sound, network, web sites, compression, security, and other features in common use.
Beyond the basic documentation, there is a library of hundreds of commented and approved source codes for Rebol programs available at http://rebol.org. It's a great way to immerse yourself into reading the language as it exists in the real world. In addition, you can use it to copy and paste examples of code from other programs as a basis for programs you want to write (be sure to give credit to the author(s) whenever you do anything of that sort). There's also a searchable mailing list archive with nearly 45,000 emails at the same rebol.org web site. It contains answers to numerous questions encountered by Rebol programmers. There are many other web sites such as http://www.codeconscious.com/rebol/ and http://www.rebolforces.com/ that provide more help in understanding and using the language. For a complete list of all web pages and articles related to Rebol, see http://dmoz.org/Computers/Programming/Languages/REBOL/
Finally, the Rebol interpreter itself is a fantastic source of code and reference material. The built-in word "help" provides required syntax structure for all of the built-in words in Rebol, and the word "what" lists all built-in words. Together, those two words provide a reference that's sufficient for most situations in which you need syntax help - without any external guide. You can also use the built-in word "source" to get the actual internal Rebol code that defines many of the mid-high level function words in the language. The interpreter also provides an embedded way for the community to share useful code. The Rebol "desktop" that appears by default when you run the view.exe interpreter is a gateway into a world of "Rebsites" that developers use to post code for programs they've created. Surfing the public rebsites on which developers make shared code available is a great way to explore the language more deeply. All of the code in the rebol.org archive, and much more, is available on the rebsites (It's amazing how much that little view.exe file lets you do!)
If you're interested at all in programming, you'll find all of the Rebol documentation and resources interesting, and a little addictive, because they show you how to do virtually everything you've ever imagined doing as a programmer. Rebol is a tiny tool (compared to the file size of other interpreters and compilers), it's very powerful, and it's completely cross-platform capable. You'll be able to program local Windows computers, as well as Linux web servers, Macs, and other types of computers used in all sorts of different environments. You'll be able to program business applications, audio-visual applications, network and Internet applications, games, etc. And you'll get started right away, with a quick learning curve that'll have you doing useful things immediately as a programmer. On top of that, you'll learn fundamentals about programming that you'll carry around to other languages as well.
26.1 A Final Point
Rebol is a language that neatly "wraps" up most common functions that various operating systems enable. It provides a single, simple format that lets you talk to all different computers in the same way. It's got it's own way of speaking that is different from many other languages (see http://www.ddj.com/documents/s=3211/nam1012432597/index.html to get an idea of how Rebol is different). In technical terms, that grammar and syntax is called the "api". If you continue to pursue programming in other languages, and in various environments, you'll encounter different language apis. Eventually, as a serious programmer, you'll typically learn to deal with the raw api of the operating system you're working on. It is the base language that most other languages are actually translating to. Because the operating system needs to access the computer hardware quickly, it is written in a "lower level" language - one that is formatted to think more like the computer's raw calculations, and less like human speech.
With Rebol, you can do most typical things that programmers want to do, but there are many functions in the various operating system api's that aren't included (i.e., web cam access, sound input, low level hardware control, etc.). To do that, be prepared to explore the raw operating system api, and the language(s) in which it was written. On Windows, Unix, Macintosh, and other platforms, that typically means learning the syntax and structure of the "C" and "C++" languages. Also, learning common methods for accessing shared code files such as .dll's is very important. Once you've learned the full Rebol api, that's a good direction to take in your studies...
Enjoy!
27. Feedback
For feedback, bugs reports, suggestions, etc., please use the form below:
Or contact me by email at:
reverse {moc tod znosselcisum ta lober}
Copyright © Nick Antonaccio 2005-2006, All Rights Reserved
|