- Creating Behavior Without Programming
- Using the Code Editor to Write REALbasic Code
- Getting Help with the REALbasic Language
- Mastering Dim and Assignment Statements
- Making Tests and Comparisons
- Writing Code that Branches
- Writing Code that Repeats
- Writing Your Own Methods
- Extending the HTML Editor
- Creating the Indent Menu Item
- Removing Existing Indentation
- Inserting Indentation Before Tags
- Handling the Indent Level
- Extending the Project
Using the Code Editor to Write REALbasic Code
Although some behavior of some objects can be controlled by object binding, most behavior is implemented in code. The lines of REALbasic code that you write as you create your application spell out what its objects can do and, consequently, what the application can do. You write that code in the Code Editor.
As mentioned earlier in this book, REALbasic code is object-oriented and event-driven. The Code Editor is your key to object-oriented, event-driven programming in REALbasic. Through it, you explicitly associate chunks of code with the relevant objects and with the events that trigger their action. You open the Code Editor for a PushButton control to edit the code for that object.
The Code Editor has two parts: the browser, which is the left pane; and the editor, which is the right pane (Figure 4.4). Because the Code Editor it is your primary tool for programming in REALbasic, it pays to learn how to use all its features.
Figure 4.4 The Code Editor has two parts: the browser and the editor.
To open the Code Editor for a control:
Open the window that contains the control.
Double-click the control.
The Code Editor for the control's parent window opens, with the desired control selected and the primary handler for that control opened.
Every control lives in some window. When you open the Code Editor for a control, you have access to the code for all the controls in that window.
To open the Code Editor for a window:
Double-click anywhere in the window itself (not a control).
or
Press Option-Tab.
or
Click the desired window in the Project window, and press Option-Tab.
To see all the controls for a window:
Open the Code Editor for the window.
Click the disclosure triangle next to controls in the browser pane.
To see all the events to which a control can respond:
Open the Code Editor for the window.
Click the disclosure triangle next to Controls in the browser pane.
Click the disclosure triangle next to the name of the control (Figure 4.5).
Figure 4.5 The browser pane (left) shows all the events to which a control can respond.
Like a control, a window is an object and has a set of events to which it can respond.
To see the code associated with a control's event handler:
Open the Code Editor for the window.
Click the disclosure triangle next to Controls in the browser pane.
Click the disclosure triangle next to the name of the control.
Click the name of the event handler. The editor pane will display the code, if any, that is executed when this event occurs (Figure 4.6).
Figure 4.6 The editor pane (right) shows the code associated with the selected event.
The window itself can respond to events, just as controls can.
To see all the events to which a window can respond:
Open the Code Editor for the window.
Click the disclosure triangle next to Events in the browser pane.
Click the disclosure triangle next to the name of the event.
To see the code associated with a window's event handler:
Open the Code Editor for the window.
Click the disclosure triangle next to Events in the browser pane.
Click the disclosure triangle next to the name of the event.
The editor pane will display the code, if any, that is executed when this event occurs.
What are these events to which your code needs to respond? One common and important category of event that your code should be able to handle is the user's selection of an item from a menu. Menu handlers are so common that they have their own section in the Code Editor's browser pane.
To see all the menu handlers for a window:
Open the Code Editor for the window.
Click the disclosure triangle next to Menu Handlers in the browser pane (Figure 4.7).
Figure 4.7 Menu handlers are listed separately in the browser pane.
To see the code associated with a menu handler:
Open the Code Editor for the window.
Click the disclosure triangle next to Menu Handlers in the browser pane.
Click the name of the menu handler. The editor pane will display the code, if any, that is executed when this menu item is selected (Figure 4.8).
Figure 4.8 The code associated with a menu item.
You can create your own handlers, called methods, and your own custom properties for objects. You'll learn about methods and custom properties later in this chapter.
You may find it useful to customize the editor to suit your preferred way of working.
To change the Code Editor's fonts:
Open the Preferences dialog via the Preferences menu.
In Mac OS X, the Preferences item is in the REALbasic menu; in Mac OS 9, it's in the Edit menu.
Set the fonts for the Code Editor, code printing, and the default for controls (Figure 4.9).
Figure 4.9 Setting the fonts for the Code Editor, code printing, and the default for controls.
To get more space for writing code:
Select a smaller code font in the Preferences dialog.
or
Drag the resize bar between the panes to the left (Figure 4.10).
Figure 4.10 To get more space for writing code, drag the resize bar between the panes to the left.
or
Press Shift-Tab. This last action will hide the browser. Pressing Shift-Tab a second time will make the browser visible again.
NOTE
This last method will work only if you have been navigating the items in the browser by using the keyboard arrow keys. If you have been using the mouse and clicking items, Shift-Tab only moves the focus between the browser and the Code Editor.
Tips
To get more room for editing in the Code Editor, you can drag the resize bar between the editing and browser panes to the left. If you drag it all the way, the browser pane disappears. To bring it back, Control-click anywhere in the Code Editor. Unlike the Shift-Tab method for hiding and showing the browser, this method always works.
Clicking the disclosure triangle next to an item in the Code Editor's browser pane is one way to eliminate some of the Code Editor's information overload. But you can hide all the handlers that don't have any code associated with them by clicking the Show and Hide Empty Methods button at the bottom of the window (Figure 4.11).
Figure 4.11 To hide all the handlers that don't have any code associated with them, click the Show and Hide Empty Methods button at the bottom of the window.
When you select a method in the browser pane of the Code Editor, its associated code appears in the editor pane. Even for an "empty" method, some code is supplied automatically: the first and last lines (Figure 4.12). You can't enter, delete, or change these lines directly, but you can ignore them except for the parameters listed in the first line. Parameters are explained later in this chapter in the discussion on writing your own methods.
Figure 4.12 The Code Editor always supplies the first and last lines of a handler or method. You get to write the rest.
To enter code in the code panel:
Type anywhere between the first and last lines.
REALbasic tries to guess what you are going to type. As you type, it offers its guess in light-gray type.
If the guess REALbasic offers is what you want, press Tab to accept it.
If REALbasic finds several plausible completions for what you are typing, it will display an ellipsis (...).
If REALbasic displays an ellipsis, press Tab to see a contextual menu of choices; if you are offered a menu, click one of the menu items to accept it.
This guesswork is called autocompletion.
Tips
You can use autocompletion in many places in REALbasic. Just watch what you type. If you see gray text appearing ahead of the characters you are entering, they are REALbasic's guess at what you intend to type. Press Tab to accept the guess or to see a contextual menu of guesses. If the guess is wrong, just go on typing.
Keep your eyes peeled for the Tips Window. It will give you good suggestions for writing code.
To edit your code:
Cut, copy, paste, and clear code via the Edit-menu items, just as you would do it with text.
Command-key alternatives work as you would expect, too.
or
Use the Edit-menu items Undo and Redo or their Command-key alternatives to retract unwise editing actions or reinstate wise ones.
or
Highlight text to be moved, and drag it to where you want it.
This technique works within a handler, between handlers, from the Online Language Reference to the Code Editor, and between the Code Editor and any word processor or text editor that supports drag and drop. You can also drag text clippings from the Desktop to the Code Editor.
Tips
Hold down the Shift key when you choose the Cut or Copy menu item or their Command-key equivalents to perform a "cut and append" or "copy and append" operation. The cut or copied text is appended to the text that's already in the Clipboard, rather than replacing it. This method is handy when you want to gather up several snippets of code and move them all to one new location.
To copy and paste a single word quickly, click the location where you want to paste, hold down the Option key, and click the word you want to paste. You can also use this technique to copy property or method names from the browser pane to your code. The real benefit of this shortcut is not speed but accuracy. Every word that you can copy rather than typing is one word that you can't mistype.
To find/replace code in the Code Editor:
Choose Edit > Find.
The Find/Replace window appears.
Enter the text to find.
Enter the text to replace it with, if any.
Select the scope of the search:
Source: search within the current handler/method.
Module: search within the current module. (Modules are explained in Chapter 5.)
Project: search anywhere in the current project.
Click one of the three buttons to find, replace and find again, or replace all occurrences (Figure 4.13).
Figure 4.13 The Code Editor supports Find and Replace operations within code.
Tips
You can jump immediately to the last method you were editing by clicking the Back button (the small arrow at the bottom of the Code Editor window). The Back button and the Forward button (next to it) work similarly to the Back and Forward buttons in a Web browser; they let you step backward and forward through the methods you have been editing (Figure 4.14).
Figure 4.14 The Back button and the Forward button work similarly to the Back and Forward buttons in a Web browser; they let you step backward and forward through the methods you have been editing.
You will find yourself frequently jumping back and forth between the Code Editor for a window and the window itself in the IDE. A quick way to open the window from its Code Editor is to click the leftmost icon at the bottom of the Code Editor window (Figure 4.15).
Figure 4.15 A quick way to open the window from its Code Editor is to click the leftmost icon at the bottom of the Code Editor window.
To print your code:
Choose File > Print, or press Command-P. The Print dialog appears.
Specify how much code to print: the current method, the current module, or the whole project.
Click Print.