Beginning REALbasic - From Novice To Professional (2006)
.pdf256 C H A P T E R 9 ■ P R O C E S S I N G T E X T F I L E S
Dim prs |
as PrinterSetup |
'Declare a PrinterSetup |
variable |
||
'Define |
a |
variable to hold Page Setup |
settings |
|
|
Dim strPrintSettings As String |
|
|
|||
prs = New |
PrinterSetup |
'Instantiate |
the Printer |
Setup object |
If prs.PageSetupDialog then 'Display the Page Setup dialog window strPrintSettings = prs.SetupString 'Assign Page Setup settings
End if
MsgBox strPrintSettings 'Display print settings
In this example, all the settings managed by the Page Setup dialog window are assigned to a variable named strPrintSettings. The contents of this variable are then displayed in a pop-up window, as you see in Figure 9-4. However, if the user clicks on Cancel, a Nil value is returned in place of print settings.
Figure 9-4. Examining the settings stored in the Page Setup dialog window, as seen on Windows
■Note The Linux version of REALbasic does not support the Page Setup dialog window. If you attempt to use it, REALbasic returns a value of False and the dialog window is not displayed.
Using the Print Dialog Window
To present the user with a Print dialog, you can use the OpenPrinterDialog function. If the user clicks the dialog’s OK button, REALbasic returns a graphic’s object whose methods can then be used to print the file. Otherwise, a Nil value is returned. The following example demonstrates how to work with the OpenPrinterDialog function.
|
|
|
|
C H A P T E R 9 ■ P R O C E S S I N G T E X T F I L E S |
257 |
|
Dim gphFile As Graphics |
'Declare a Graphics object |
|
|
|||
gphFile = OpenPrinterDialog() 'Display the Print |
dialog window |
|
|
|||
If gphFile<> Nil Then |
|
'Submit print job if user didn't click on Cancel |
|
|||
'Print text with a 1-inch |
margin on the left and |
top side of the |
page |
|
||
gphFile.DrawString _ |
|
|
|
|
|
|
"Once upon a time there |
was a little boy named |
William.", 100, |
100 |
|
||
gphFile.NextPage |
the current page and create a new page |
|
|
|||
'Print text with a 1-inch |
margin on the left and |
top side of the |
page |
|
||
gphFile.DrawString "The End!", 100, 100 |
|
|
|
End
REALbasic uses the Graphics object to handle printing, which is why the preceding example began by declaring a Graphics object named gphFile. Next, the OpenPrinterDialog function was used to display the Print dialog windows, as Figure 9-5 shows.
Figure 9-5. Displaying the Print dialog window on Mac OS X
If the user clicks Cancel, a value of Nil is returned and nothing happens. Otherwise, the Graphic class’s DrawString method is used to draw text. The text is drawn at a location specified by X and Y coordinates. These coordinates represent pixel locations starting from the upper left-hand corner of the printed page. Next, the Graphic class’s NextPage method is used. This method prints the current page and creates a new one. A second page is then drawn and printed in similar fashion.
258 |
C H A P T E R 9 ■ P R O C E S S I N G T E X T F I L E S |
Bypassing the Print Dialog Window
If you prefer, you can skip the display of the Print dialog window and use the OpenPrinter function to handle file printing. Except for pausing to display a Print dialog window, this function works identically to the OpenPrinterDialog function, as the following example shows.
Dim gphFile As Graphics |
'Declare a |
Graphics object |
|
||
gphFile |
= OpenPrinter() |
'Retrieve a |
Graphics object required to print |
||
'Print text with a 1-inch |
margin (72 pixels equals an inch) |
|
|||
'on the |
left and top side |
of the page |
|
|
|
gphFile.DrawString _ |
|
|
|
||
"Once |
upon a time there |
was a little boy named William.", |
72, 72 |
||
gphFile.NextPage |
the current |
page and create a new |
page |
'Print text with a 1-inch margin (72 pixels equals an inch) 'on the left and top side of the page
gphFile.DrawString "The End!", 72, 72
Printing Styled Text
If a file contains styled text, you want to retain the format of the styled text when the file prints. To do so, you can use the DrawString method of the StyledTextPrinter class.
'Declare a |
variable representing the |
StyledText printer class |
||
Dim stp |
As |
StyledTextPrinter |
|
|
Dim gphFile As Graphics 'Declare a |
variable representing the Graphic class |
|||
gphFile |
= OpenPrinterDialog() |
'Display the Print dialog window |
If gphFile <> Nil then 'Print if the user did not click on Cancel
'Use the EditField's StyledTextPrinter method to return a StyledTextPrinter object 'using the specified Graphics object and width (in pixels)
stp = EditField1.StyledTextPrinter(gphFile, 72 * 7.5) 'Pixels times inches
'Specify start coordinated of the upper left-hand corner of the print page and the 'height of the printed image
stp.DrawBlock 100, 100, 72 * 10 '72 pixels by 10 inches
End If
Notice, in this example, the DrawBlock method was used in place of the DrawString method, which is a requirement for printing styled text.
C H A P T E R 9 ■ P R O C E S S I N G T E X T F I L E S |
259 |
Creating a REALbasic Word Processor
This chapter wraps up by showing you how to create a REALbasic word processor application. This application, called the RB Word Processor, gives you a chance to work with most of the information you learned in this chapter.
This chapter demonstrates how to develop the RB Word Processor using the Windows version of REALbasic. However, as with the other sample applications you worked on in this book, you can also create the application using either the Macintosh or Linux version of REALbasic. All the steps are the same. Figure 9-6 provides a look at the RB Word Processor in action. As you can see, it consists of a menu system, an EditField control, and a number of PushButton and BevelButton controls.
Figure 9-6. A sneak peak at the RB Word processor application
Putting Together the User Interface
The first step in creating the RB Word Processor application is to create a new desktop application. Next, modify the properties specified in Table 9-1.
260 |
C H A P T E R 9 ■ P R O C E S S I N G T E X T F I L E S |
Table 9-1. Property Modifications for the RB Word Processor Application
Object |
Property |
Value |
Window1 |
Name |
EditorWindow |
|
Title |
RB Word Processor |
|
Width |
587 |
|
Height |
536 |
|
Resizable |
Checked |
|
MaximizeButton |
Checked |
|
|
|
Now, add an EditField control to the window and resize it until it takes up most of the window’s available space. Next, modify the properties belonging to the EditField control, as you see in Table 9-2.
Table 9-2. Property Modifications for the EditField Control
Object |
Property |
Value |
EditField1 |
Name |
edfTextArea |
|
Left |
0 |
|
Top |
50 |
|
Width |
587 |
|
Height |
486 |
|
LockLeft |
Checked |
|
LockTop |
Checked |
|
LockRight |
Checked |
|
LockBottom |
Checked |
|
MultiLine |
Checked |
|
Styled |
Checked |
|
|
|
Now, it’s time to add controls that will be used to format text. First, drag-and-drop a BevelButton control to the upper left-hand area of the window, just above the EditField control. The BevelButton control is similar to the PushButton control, except it can be configured to act like a drop-down list, which is how it is used in this application. Specifically, this control is used to display a list of fonts. Modify its properties as shown in Table 9-3.
C H A P T E R 9 ■ P R O C E S S I N G T E X T F I L E S |
261 |
Table 9-3. Property Modifications for the First BevelButton Control
Object |
Property |
Value |
BevelButton1 |
Name |
bbFontType |
|
Caption |
|
|
Hasmenu |
1 – Normal menu |
|
Bold |
Checked |
|
TextSize |
0 |
|
|
|
Add another BevelButton control just to the right of the first one. This control is used to display a list of font sizes. Modify its properties, as you see in Table 9-4.
Table 9-4. Property Modifications for the Second BevelButton Control
Object |
Property |
Value |
BevelButton2 |
Name |
bbFontSize |
|
Caption |
|
|
Hasmenu |
1 – Normal menu |
|
Bold |
Checked |
|
TextSize |
0 |
|
|
|
Now, select the Line control, and then draw a vertical line about a quarter inch to the right of the second BevelButton control. This line is used to visually organize and separate the two BevelButton controls from the other controls you are about to add.
Next, add three PushButton controls to the right of the Line control, and then modify their properties, as you can see in Table 9-5.
Table 9-5. Property Modifications for the Three PushButton Controls
Object |
Property |
Value |
PushButton1 |
Name |
pbBold |
|
Caption |
Bold |
|
Bold |
Checked |
PushButton2 |
Name |
pbItalics |
|
Caption |
Italic |
|
Bold |
Checked |
|
Italic |
Checked |
Continued
262 C H A P T E R 9 ■ P R O C E S S I N G T E X T F I L E S
Table 9-5. Continued
Object |
Property |
Value |
PushButton3 |
Name |
pbUnderline |
|
Caption |
Underline |
|
Bold |
Checked |
|
Underline |
Checked |
|
|
|
Now that the window and its controls are set up, it’s time to put together the application’s menu system. Double-click the Menubar1 item located on the Projects screen. REALbasic displays a default set of menus and menu items. Modify the menu system by adding and configuring the menu and menu items you see in Table 9-6.
Table 9-6. Menus and Menu Items for the RB Word Processor Application
Menu |
Menu Name Menu Item |
Text Property |
Key |
MenuModifier |
File |
FileMenu |
&File |
|
|
|
FileNew |
&New |
N |
Checked |
|
FileOpen |
&Open |
O |
Checked |
|
FileSave |
&Save |
S |
Checked |
|
FilePrint |
P |
Checked |
|
Help |
HelpMenu |
&Help |
|
|
|
HelpAbout |
&About |
|
|
|
|
|
|
|
The menu items for the File menu should be added in the order listed in Table 9-6. In addition, you should also add three Line Separator bars. Place the first Line Separator bar after the Open menu item, add the second one just after the Save menu item, and put the last one just after the Print menu item. Once this is done, the application’s interface is complete.
Defining Supported File Types
To work properly, you must tell REALbasic what types of files the RB Word Processor should work with. To do this, you need to add a File Type Set by opening the Project Editor and clicking Project Add File Type Set. Next, open the File Type Set Editor by double-clicking the File Type Set you just created and click the Add File Types button.
The RB Word Processor application should handle both text and rtf files, so you need to add two separate entries to the File Type Set. For the first entry, enter a Display Name of rtf, an Object Name of rtf, a MacType of TEXT, a MacCreator of MSWD, and an .rtf Extension. For the second entry, enter a Display Name of text, an Object Name of text, a MacType of Text, a MacCreator of ????, and a .txt Extension.
C H A P T E R 9 ■ P R O C E S S I N G T E X T F I L E S |
263 |
Adding Custom Constants and Properties
This application makes use of the MessageDialog class to display pop-up windows. To standardize the display of the text displayed in the title bar of these pop-up windows, a constant name cTitlebarMsg is added to the application’s window. To do so, open the EditorWindow screen and click the Add Constant button. Name the constant cTitlebarMsg and assign it a default value of RB Word Processor. Then, click the String button to specify its data type.
You also need to add a pair of custom properties to the application. To add the first property, click the Add Property button, declare a new property with a name of NewChanges, and assign it a date type of Boolean as follows:
NewChanges As Boolean
The application toggles the value assigned to this property to identify when a text document has an unsaved change. Next, click the Add Property button again and type the following statement into the declaration field:
PageSetup As String
This property is used to store Page Setup settings specified by the user when printing text files. Now that you’ve added the application’s custom constants and properties, all that remains is to add the program code required to tie everything together.
Adding Code That Supports the Menu System
Like most desktop applications, the RB Word Processor makes a lot of its functionality available to the user via its menu system. Because this application involves a fairly sizeable amount of code, the code for each menu item is covered separately in the sections that follow.
Coding the New Menu Item
The first menu item located in the File menu is the New menu item. When selected, a new window should be opened, enabling the user to begin working on a new document while keeping any other windows open. The user should be able to open a new text document at any time, even when the application does not have any visible windows displayed.
Menu systems on Windows and Linux are attached directly to the top of Windows. However, on the Macintosh, menu systems are displayed at the top of the viewing area. On Macintosh, it is possible for no windows to be visible and yet to have the application’s menu system displayed. To support the application on Macintosh, you need to make the New menu item available at all times, even when no windows are open. Therefore, adding the menu handler for the New menu item to the window will not suffice. Instead, you need to add the menu handler to the App class object. Any code added to the App class object is made available to the entire application, not only to a particular window.
To add the menu handler for the New menu item, select the App screen and click the Add Menu Handler button. Enter FileMenu in the MenuItem Name field, and then add the following code statements.
'Declare a variable representing a new window Dim TextWindow as EditorWindow
TextWindow = New EditorWindow 'Instantiate and display the new window
264 |
C H A P T E R 9 ■ P R O C E S S I N G T E X T F I L E S |
The first statement declares a variable named TextWindow based on the EditorWindow. The next line instantiates and displays the new window, enabling the user to work with more than one text document at a time.
Coding the Open Menu Item
The following shows the code for the FileOpen menu handler. This code, like the rest of the code in this application, is associated with the application’s window and not with the App class object. It uses the GetOpenFolderItem function to display a standard Open dialog window and enables the user to select a text file. On Windows, the Open dialog displays RTF files. On Macintosh and Linux, TEXT files are displayed in the dialog.
Dim SourceFile |
As |
FolderItem |
'Declare a |
variable representing the FolderItem |
Dim TextWindow |
as |
EditorWindow |
'Declare |
a variable representing a Window |
'Display different file types based on the operating systems in use |
||||
#If TargetWin32 = |
True |
|
|
|
SourceFile |
= GetOpenFolderItem("rtf") |
'On Windows use RTF |
||
#Else |
|
|
|
|
SourceFile |
= GetOpenFolderItem("text") |
'On Macintosh/Linux use Text |
||
#Endif |
|
|
|
|
If SourceFile |
<> |
Nil then 'Make sure the |
user did not click on cancel |
|
TextWindow = |
New EditorWindow |
'Instantiate a new window |
'Open new window and display contents of the file (as styled text) SourceFile.OpenStyledEditField TextWindow.edfTextArea
'Set the titlebar text equal to name of the file TextWindow.Title = SourceFile.Name
End If
If the user selects a file, the OpenStyledEditField method is used to open it, so any styled text in the file is retained.
Coding the Save Menu Item
The following shows the code for the FileSave menu handler. It uses the GetSaveFolderItem function to display a standard Save As dialog window and the SaveStyledEditField function to save the text document using the user supplied filename.
Dim TargetFile As FolderItem |
'Declare a FolderItem variable |
C H A P T E R 9 ■ P R O C E S S I N G T E X T F I L E S |
265 |
'Display different file types based on the operating systems in use #If TargetWin32 = True
TargetFile = GetSaveFolderItem("rtf", "Untitled") 'Windows uses RTF #Else
'Macintosh and Linux uses TEXT
TargetFile = GetSaveFolderItem("text", "Untitled") #Endif
If TargetFile <> Nil Then 'Make sure the user did not click on cancel
'Save contents of EditField in the specified file (as styled text) TargetFile.SaveStyledEditField edfTextArea
'Set the titlebar text equal to the name of the opened file Title = TargetFile.Name
'Disable the FileSave menu item FileSave.Enabled = True
'Since changes have been saved set NewChanges property to False NewChanges = False
End If
Note, the method ends by displaying the FileSave menu item and changing the value of NewChanges property to False, indicating no unsaved changes exist. For the Save menu to work properly, it needs to be enabled at the appropriate time. This is accomplished by opening the TextChange event handler belonging to the edfTextArea control and adding the following statements.
'Update this property to show that the user has made a change NewChanges = True
This event handler executes any time the user makes a change in edfTextArea. To finish things up for the Save menu item, you also need to add the following code statements to the edfTextArea control’s EnableMenuItems event handler.
'If this property is equal to True then enable the Save menu item If NewChanges = True Then
FileSave.Enabled = True End If
This menu handler executes any time the user accesses the application’s menu system. Its job is to enable the Save menu item when the value of NewChanges is set equal to True.