Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Beginning REALbasic - From Novice To Professional (2006)

.pdf
Скачиваний:
234
Добавлен:
17.08.2013
Размер:
18.51 Mб
Скачать

236 C H A P T E R 8 O B J E C T - O R I E N T E D P R O G R A M M I N G

Figure 8-16. Defining a new method in the RB Picture Viewer application, as seen on Linux

Adding a Little Program Code

Once you finish configuring the application’s user interface and its menu system, and you add the required constant and method to Window1, it’s time to start adding the program code. Start by adding the following code statements to the new OpenImage method you just added to Window1.

'Declare variable to represent the FileType object Dim ImageTypes As New FileType

'Declare a variable representing a Picture object Dim pctImage As Picture

ImageTypes.Name= "Image Files" 'Specify string identifying supported files

'Specify supported file types

ImageTypes.Extensions=".jpeg; .jpg; .gif; .bmp; .pct; .png"

'Declare variable representing a folder object

Dim f as FolderItem = GetOpenFolderItem(ImageTypes)

 

 

C H A P T E R 8 O B J E C T - O R I E N T E D P R O G R A M M I N G

237

If f <> Nil then

'Perform only

if the user did not

click on the Cancel

 

pctImage = f.OpenAsPicture

'Set variable equal

to the select image

 

Return pctImage

'Return value representing the selected image

 

End if

 

 

 

 

Note Comments embedded alongside each statement provide a detailed explanation of what each statement does.

Next, let’s add the code for each of the menu items on MenuBar1. Start by clicking the Add Menu Handler button located on the Code Editor’s tool bar, and then select the FileOpen menu item from the MenuItem Name drop-down list. Add the following code statements to the menu item’s event handler.

'Call the OpenImage Function to retrieve a picture to be displayed ImgDisplay.Image = OpenImage()

Click the Add Menu Handler button again. This time, select the HelpAbout menu item from the MenuItem Name drop-down list. Add the following code statements to the menu

item’s event handler.

 

 

 

Dim mdgAbout as New MessageDialog

'Instantiate a MessageDialog object

Dim mdbResult as MessageDialogButton

'Declare a variable

to store result

mdgAbout.Title = cTitlebarMsg

'Display a message in the

titlebar

mdgAbout.icon=MessageDialog.GraphicNote

'Display a note

icon

mdgAbout.ActionButton.Caption="OK"

'Specify text for the ActionButton

mdgAbout.Message="RB Movie Player" mdgAbout.Explanation="By Jerry Ford - 2006"

mdbResult = mdgAbout.ShowModal 'Display the popup dialog

Now, click the Add Menu Handler button again. This time, select the HelpOnlineSupport menu item from the MenuItem Name drop-down list. Add the following code statements to the menu item’s event handler.

URLSite.Show 'Display the URLSite window

You are finished adding the program code required for Windows1. Before you begin testing your new application, though, you need to perform one final step. Specifically, you need to open the Code Editor for the URLSite window and add the following code statement to the HTMLViewer control’s (hvrDisplay) Open Event handler.

hvrDisplay.LoadURL "http://www.apress.com"

238 C H A P T E R 8 O B J E C T - O R I E N T E D P R O G R A M M I N G

Note The hvrDisplay.LoadURL "http://www.apress.com" statement used to display the application’s Online Support pages points to the website of this book’s publisher. In real life, you’d want to substitute your own website for this URL.

Testing RB Picture Viewer

If you have not done so yet, go ahead and save your application, and name it RB Picture Viewer. Then, test it to make sure it does not have any errors. If it does have errors, then you probably made a typo or two, which you’ll need to track down and correct. If you do not have any image files handy, you can find a small collection of graphics files included along with the source code for this application on the book’s companion CD-ROM. Figure 8-17 shows how the RB Picture Viewer looks when viewing one of these sample graphics images.

Figure 8-17. Viewing a picture using the RB Picture Viewer application, as seen on Linux

C H A P T E R 8 O B J E C T - O R I E N T E D P R O G R A M M I N G

239

Summary

In this chapter, you learned concepts critical to object-oriented programming. This included learning about REALbasic’s class hierarchy and how to work with various REALbasic classes. You learned how to customize REALbasic’s built-in classes by adding your own properties, methods, and constants. You learned how to create, customize, export, and import your own control classes. Finally, you learned how to work with modules as a means of making resources available globally to your REALbasic applications.

P A R T 3

■ ■ ■

Advanced Topics

C H A P T E R 9

■ ■ ■

Processing Text Files

In Chapter 8, you learned about object-oriented programming and how to work with a number of REALbasic classes. In this chapter, you learn how to use REALbasic to build applications that can open, create, and save text files. This includes learning how to work with a number of new functions and classes. You learn how to interact with files and folders, and how to work with styled text that contains different font types, font sizes, and various styles, such as boldface, italics, and underlined. You also learn how to print text documents. Finally, you put all this new information to work through the development of your own word processing application.

Specifically, you learn how to

• Define different file types within your REALbasic applications

• Work with files and folders

• Work with standard dialog windows

• Read to and write from text files

• Work with styled text

Working with Plain and Styled Text Documents

Nearly any application you might work with today reads and writes to text files in one way or another. A text file is a document made up of plain-text characters with no special formatting. On Windows, text documents can be created using Notepad. On Macintosh, you can use the SimpleText application. Different implementations of Linux include an assortment of different text editors, including the VI editor.

Today, most people are used to working with styled text. For example, word processors like Microsoft Word enable you to create formatted documents that include bold, italic, and underlined text. Styled text also includes text files that can mix and match font types and font sizes. Using REALbasic, you can create and modify both plain text and styled text files.

Defining File Types

To work with files, REALbasic needs to know a little something about their content. You pro-

 

vide REALbasic with this information by telling it about the file type of the files you want to

 

work within your applications. A file type provides information about the content of a file and

 

the manner in which information is stored within it.

243

244

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

On Mac OS X, Windows, and Linux, the file type is identified by the three-character file extension added to the end of filenames. For example, a file with the name SampleReport.doc is a Microsoft Word file, whereas files with extensions of .gif or .jpg are different kinds of graphic files.

Note Mac Classic file types are identified by four-character File Type and Creator codes.

Specifying File Types in Your REALbasic Applications

Telling REALbasic about the different file types you plan on supporting in your applications is easy. REALbasic does this in the form of File Type Sets. A File Type Set is a REALbasic item you add to the list of files managed in the Project Editor, and then use to define each file type your application will support.

To add a File Type Set, open the Project Editor screen and click Project Add File Type Set. This adds a File Type Set Editor to a REALbasic project, as you see in Figure 9-1.

Figure 9-1. Adding a File Type Set to a REALbasic project, as shown on Linux

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

245

The File Type Set Editor lets you define information about the different types of files your application will support. This information includes the following:

Display Name. The name displayed in a standard dialog window, indicating the file types the application works with.

Object Name. The name of the file type as known to the application. Note, an object name cannot include blank spaces.

MacType. A four-character code that identifies a file type on Mac Classic.

MacCreator. A four-character code used on Mac Classic to identify the file type.

Extensions. A three-character filename extension that identifies the file type on Windows, Linux, and Mac OS X.

Icon. An optional graphic icon that can be used to associate files generated by the application with the application.

To define a new file type, all you have to do is open the File Type Set Editor, click the Add File Types button, and then add an entry for each of the previously discussed fields.

Working with File Types

Once defined, you can begin working with specified file types. For example, the following statement shows how to display the Open file dialog window and configure it to display JPEG files.

TargetFile = GetOpenFolderItem(FileTypes1.JPEG)

In this example, the Open dialog window is restricted to opening only JPEG files. You can configure the Open dialog window to open multiple files by separating each file type using semicolons, as you see in the following:

TargetFile = GetOpenFolderItem(FileTypes1.JPEG;FileTypes1.GIF)

If you want, you can configure the Open dialog window so it can open any file types you have defined, as the following shows.

TargetFile = GetOpenFolderItem(FileTypes1.All)

Accessing Files and Folders

The next step after defining the types of files you’ll work with in your application is to set up references to specific files and folders. To REALbasic, files, folders, and the disk drives (or volumes) they reside on are all considered FolderItems. REALbasic provides you with a number of ways of establishing references to FolderItems. For example, you can use the Open dialog window or you can specify the pathname to a specific file or folder.