Lawrence.book Page 1 Sunday, November 4, 2001 8:53 PM

1 Getting Started

1.1 A look at Developer Studio

Compaq Visual Fortran (Visual Fortran) uses the same development envi- ronment as Visual C++. This development environment is shown in Figure 1.1, and it is known as the Microsoft Developer Studio. In Figure 1.1, the left pane has tabs to select a FileView, a ResourceView, and a Class- View. The FileView shows each project and the project files associated with the Workspace. The ResourceView appears if the Workspace uses Resources such as dialogs and icons. The ClassView appears only if Visual C++ is installed. ClassView is not used by Visual Fortran. The FileView pane shown in Figure 1.1 contains the files associated with the Workspace for the project Plots. The Source Files folder indicates that there are six files in this project workspace: Callbacks.f90, Globals.f90, Main.f90, Menu.f90, Plot.f90, and Resource.rc. The Resource Files folder contains one bitmap and three icons, while the External Dependencies folder lists the files global.mod and resource.fd. Double-click on any of the file icons in the FileView pane to open that file with the Developer Studio text editor in the right-hand pane. In the right-hand pane of Figure 1.1, the file Main.f90 has been opened in the default text editor. This editor uses green to identify comments in the code, blue for Fortran standard code, and black for other text. There are actu- ally 12 different “colorable items” (including comments and Fortran key- words), and the default color of each item can be changed in Developer Studio by using the Tools menu, selecting the Options menu item, and then choosing the tab labeled Format (Tools -> Options -> Format). The bottom of Figure 1.1 shows the output pane, which shows text dis- played from building the project. Currently it is displaying information about compiler and link errors. The output pane has multiple tabs. The

1 www.bh.com Copyright 2002 Butterworth-Heinemann All rights reserved. Lawrence.book Page 2 Sunday, November 4, 2001 8:53 PM

2 1.2 Visual Fortran project types

Figure 1.1 Developer Studio window.

output pane Build tab is selected after you open a Workspace and compile one or more project files or build the project. Windows in the Visual Development Studio can be set to have either docking or floating properties. Docking windows do not have a title bar, and they are docked (attached) along an edge of the visual development environment window. Floating windows have a title bar and can be moved by dragging the title bar. By holding down the Ctrl key and pressing the Tab key, you can cycle through each floating window in turn. The docking or floating properties of a window are set by selecting Options from the Tools menu. Click the Workspace tab in the docking view list and click the check box for each window to be displayed with a docking view (see Figure 1.2). Unchecked boxes indicate floating windows.

1.2 Visual Fortran project types

Projects are contained in a workspace and consist of the source files required for an application, along with the specifications for building the project. A project type must be chosen every time you create a new project. The

www.bh.com Copyright 2002 Butterworth-Heinemann All rights reserved. Lawrence.book Page 3 Sunday, November 4, 2001 8:53 PM

1.2 Visual Fortran project types 3

Figure 1.2 Setting the docking properties for windows in the Developer Studio.

project type determines some of the options that Developer Studio will set by default for the project and what needs to be generated. It determines, for instance, the options that the compiler uses to compile the source files, the static libraries that the linker uses to build the project, the default locations for output files, and defined constants. The following seven project types are available with Visual Fortran:  Fortran Console Applications—These are single-window applications without graphics (character-cell applications), used for a traditional Fortran program. Standard Fortran I/O statements are used to write to the window.  Fortran Standard Graphics Applications—Standard Graphics Applica- tions have a single maximized window covering the entire screen. Standard Graphic Applications do not have programmable menus, but they can use QuickWin graphic routines to draw to the window. Standard Fortran I/O statements are used to write to the window.  Fortran QuickWin Applications—QuickWin Applications can have a range of graphics capabilities and receive user input through menu selections, dialog boxes, and mouse button actions. QuickWin Appli- cations can have multiple windows. Standard Fortran I/O statements are used to write to the window.  Fortran Windows Applications—These are single- or multiple-window projects with a full graphical interface, which can include menus, dia- log boxes, and graphical routines and can use any of the available Win32 API routines. In Fortran Windows Applications, the pro- grammer directly calls any Win32 routines that are required. Win32 routines are used to write to the window.

Chapter 1 www.bh.com Copyright 2002 Butterworth-Heinemann All rights reserved. Lawrence.book Page 4 Sunday, November 4, 2001 8:53 PM

4 1.3 Developing graphical user interfaces

 Fortran COM Server—Uses the component object model (COM) to implement a COM server. COM supports client/server interaction between a user of an object, the client, and the server. Clients may be written in Visual Fortran using the Fortran Module Wizard or in other languages, such as Visual C++ and .  Fortran Static Library—These are library routines that encapsulate some specific functionality. They are linked into .exe files.  Fortran Dynamic-Link Library—These are library routines that encapsulate some specific functionality. They are linked into .DLL files and are used by an .exe file during execution.

1.3 Developing graphical user interfaces

The focus of this book is on using Visual Fortran Version 6.6 to create For- tran applications that have a Windows-based graphical user interface (GUI). The traditional character-cell console applications, including those written in Fortran, are procedural in nature. That is, execution begins with the first line of executable code and then sequentially follows a defined pathway through the application, calling functions and subroutines as required until it reaches the end of the program. Windows-based GUI pro- grams are event-driven in nature. The order in which the code executes depends on which events have been chosen. An event occurs every time the user makes a selection from a range of choices. It could be to open or close a file, to enter data, or to copy the contents of the screen to the clipboard. GUI applications should always present the user with a valid set of options, respond according to the selections made by the user, and ensure that the user has responded correctly to any input information that the pro- gram requested. This may mean that some options are disabled, or it may entail checking that a text box has valid input before proceeding with the next operation.

1.3.1 Graphical user interface etiquette

It is reasonable to assume that most, if not all, of the people who use your applications will have previously used Windows-based applications such as word processors and spreadsheets. This being the case, your application users will have certain expectations of your GUI because applications in the Windows environment share a common “feel” in their user interfaces. Referring to Figure 1.3, users will expect File and Help to be the first and

www.bh.com Copyright 2002 Butterworth-Heinemann All rights reserved. Lawrence.book Page 5 Sunday, November 4, 2001 8:53 PM

1.3 Developing graphical user interfaces 5

Figure 1.3 Typical Windows graphical interface.

last items, respectively, across the menu bar. The last item in the File menu should be Exit. Normally, the main window should not have controls such as radio buttons, edit boxes, and slider bars. In most cases, such controls should be reserved for dialog boxes. The user will often expect certain key combinations to do certain things—for example, Ctrl+X to cut selected text or objects, Ctrl+C to copy selected text or objects, and Ctrl+V to insert text or objects at the caret position. Windows applications should be able to provide information whenever the user has performed an incorrect action or when a system resource is not available, such as no disk in the floppy drive. Error checking should always be included in any programming code. It is good programming practice to check the return value of every function called to determine the success of the action requested and for the program to take the appropriate action.

Note: In the interest of holding the reader’s attention by keeping the exam- ple code as short and simple as possible, the examples contained in this book do not model this principle.

Chapter 1 www.bh.com Copyright 2002 Butterworth-Heinemann All rights reserved. Lawrence.book Page 6 Sunday, November 4, 2001 8:53 PM

6 1.5 Additional resources

1.4 Do I need to know C?

Understanding C is not a prerequisite for creating Fortran programs with graphical interfaces. However, some knowledge of C can be useful when you wish to exploit more advanced features of the Windows operating sys- tem. Also, when you do not know how to program a particular Win32 fea- ture, you can look up code examples given in C and determine the methodology; then it is usually a reasonably simple process to write your own Fortran code. If you are not familiar with C, it may be helpful for you to think of C as a type of pseudo-code that can be translated into Fortran. Appendix A provides an overview of C for Fortran programmers.

1.5 Additional resources

Visual Fortran comes complete with a very comprehensive set of sample programs, which illustrate a range of ideas for developing applications using QuickWin, Win32, OpenGL, dynamic-link libraries, and many other pos- sibilities. It is a good idea to work through the sample code, compiling it and thereby seeing the wide range of programming possibilities that exist with CVF. Look through the program code and try to spot useful tech- niques that you can incorporate into your own code. This book on creating Windows interfaces is not a substitute for the official documentation of the Windows API. The Windows API informa- tion is available as part of the online help documentation that comes with the Visual Fortran CD-ROM, as shown in Figure 1.4. The online docu- mentation may seem a little daunting at first, but it is well worth investing time in learning how to navigate around the information. It contains a lot of guidance not only about programming in Fortran but also about pro- gramming in the Windows environment. Select Online Documentation from the Visual Fortran menu group. Referring to Figure 1.4, click the Contents tab and open the book entitled Compaq Visual Fortran 6.6, to reveal four books: Visual Fortran, Devel- oper Studio, Platform SDK, and SDK Documentation. The Platform SDK book covers just about every possible topic for programming Win32 appli- cations. In particular, the reference section lists alphabetically all the Win32 API routines. The chapter on Graphics and Multimedia Services contains some great information under the headings GDI (Graphical Device Inter- face) and OpenGL. When using Developer Studio, it is useful to have the online documentation opened at the index section to aid in confirming the

www.bh.com Copyright 2002 Butterworth-Heinemann All rights reserved. Lawrence.book Page 7 Sunday, November 4, 2001 8:53 PM

1.5 Additional resources 7

Figure 1.4 Online Help documentation.

syntax of any of the Win32 or their associated data structures that are being used. The books listed below are those I have personally found to be very use- ful. This is not an exhaustive list, merely a reflection of my own book col- lection. Indeed, some of the books are available in more recent editions. Note that with the obvious exception of the first two books and the DVF Programmers Guide, all the other books are aimed at C programmers. The OpenGL Programming Guide, often referred to as the “red book,” and the OpenGL Reference Manual, usually referred to as the “blue book,” are the official publications of the OpenGL Architecture Review Board. If you can afford only one OpenGL book, I would suggest that you get the OpenGL Superbible. The red and blue books are not aimed at any specific hardware platform, whereas the OpenGL Superbible is specifically aimed for Win- dows-based applications on PC platforms.

Chapter 1 www.bh.com Copyright 2002 Butterworth-Heinemann All rights reserved. Lawrence.book Page 8 Sunday, November 4, 2001 8:53 PM

8 1.6 Useful Web sites

Programming Windows by Petzold is regarded as a classic for anyone wanting to write Windows applications. Programming the Windows 95 User Interface by Nancy Cluts is a great companion to Petzold’s book because it covers the use of most of the common controls—ListViews, TreeViews, Toolbars, and many more. It can also be perused on the Internet in the book section of the MSDN library. The complete list of titles follows.  Chapman, S., 1998, Fortran 90/95 for Scientists and Engineers, WCB/ McGraw-Hill.  Cluts, N. W., 1998, Programming the Windows 95 User Interface, Microsoft Press.  Ellis, T. M. R., Philips, I. R., and Lahey, T. M., 1995, Fortran 90 Pro- gramming, Addison-Wesley.  Etzel, M., and Dickinson, K., 1999, Digital Visual Fortran Program- mer’s Guide, Digital Press.  Neider, J., Davis, T., and Woo, M., 1993, Open GL Programming Guide, The Official Guide to Learning OpenGL, release 1, Addison- Wesley.  Petzold, C., 1998, Programming Windows (5th ed.), Microsoft Press.  Shreiner, D., 1999, OpenGL Reference Manual: The Official Reference Document to OpenGL, Version 1.1, Addison-Wesley.  Simon, J. R., 1997, Windows NT Win32 API Superbible, Waite Group Press.  Wright, R. S., Jr., and Sweet, M., 1996, OpenGL Superbible, The Complete Guide to OpenGL Programming for Windows NT and Win- dows 95, Waite Group Press.

1.6 Useful Web sites

If you have access to the Internet, another good source of information is the Microsoft Developer Network (MSDN) library. There are a number of interesting items that can be searched in the book and partial book sections. A very large selection of code examples can also be browsed in the MSDN library. The Web addresses are as follows:  http://www.compaq.com/fortran  http://www.compaq.com/math  http://microsoft.com

www.bh.com Copyright 2002 Butterworth-Heinemann All rights reserved. Lawrence.book Page 9 Sunday, November 4, 2001 8:53 PM

1.8 Layout of this book 9

 http://msdn.microsoft.com  http://www.opengl.org  http://developer.intel.com

1.7 User groups

User groups are among the greatest resources that a software developer can access. They are peopled by unpaid, unsung heroes, who have “been there, done that,” and are happy to pass along the benefits of their experience. The Compaq Fortran users group is an obvious choice because you are in con- tact with an audience using the same product. However, some questions are often better addressed to other user groups. When asking for help about Fortran-related matters, it is important to be specific about the problem and to state the platform and compiler version that you are using. Often it is a good idea to include a small representative code fragment. Questions relat- ing to Windows and OpenGL should state that you are using Compaq Visual Fortran and want to do “___,” and can anyone provide you with an example in C to do “___”? Sometimes you can be very lucky, and a CVF programmer will post Fortran code for you, but most times you will have to do your own translation from C to Fortran. Some of the user groups that I have found helpful include:  comp.graphics.algorithms  comp.graphics..opengl  comp.lang.fortran  comp.os.ms-windows.programmer.win32  http://www.Compaq.com/fortran—select user message board

1.8 Layout of this book

This book is divided into three parts: QuickWin Applications, Win32 API Applications, and OpenGL Graphics. Each of these three parts is arranged to progress the reader from beginner-level topics to advanced-level tech- niques in multiple chapters. Where appropriate, the QuickWin part also shows some Win32 API tricks. Experienced programmers may wish to skip the introductory chapter in each of these three parts and move directly into the subsequent chapters.

Chapter 1 www.bh.com Copyright 2002 Butterworth-Heinemann All rights reserved. Lawrence.book Page 10 Sunday, November 4, 2001 8:53 PM

www.bh.com Copyright 2002 Butterworth-Heinemann All rights reserved.