CS 46A Lab 4
Workspaces, Projects, and Files

Department of Mathematics and Computer Science
San Jose State University

The Document-View Architecture

Most Windows applications have two types of objects: documents and views. A document contains application specific data. A view is an object- usually a window -that shows some or all of the data in a particular document. There may be many views of a single document.

Saving a document writes its data into a document file. Opening a document file reads the application data into a document object in the application's memory. A document file usually has an extension indicating what type of data it contains. For example, a Word document file has a .doc extension. An Excel document file has a .xls extension. Naturally, trying to open a .xls document using Word won't work.

Creating Workspaces, Projects, and Files

MSDS is unusual because it creates, edits, saves, and opens several types of documents. The basic document is a workspace. Workspace files have a .dsw extension. Views of a workspaces are contained in the workspace window. There are several views, including File View and Class View. MSDS can only have one workspace opened at a time.

The Planets Workspace

  1. Start MSDS. Select New from the File menu and press the Workspace tab. Type the name "Planets" into the "Workspace name" edit control. Press the button next to the "Location" edit control and choose some place to put the new workspace. Always choose a directory (folder) on a local hard disk (i.e., the C: drive) because compiling files over a network or on a floppy can be very slow.
  2. The Planets Project (Version 1)

    A workspace is a container that holds projects. A project is another kind of document that MSDS creates, edits, saves, and opens. Project documents are saved in document files with .dsp extensions. The file and class views of the workspace are tree controls that can be expanded into views of the projects contained inside.

  3. Select New again from the File menu. This time select the Project tab. Select "Win32 Console Application" from the list box, type "Planets1", the name of the project, into the "Project name" edit control, and make sure the "Add to current workspace" radio button is pressed. Press the OK button. A dialog will appear asking if you want to create an empty project, a simple application, a "hello world" application, or an application that supports MFC. Always choose "empty project." Press the "Finish" button. A second dialog box appears showing the features of your new project. Press the "OK" button.
  4. We have just added a new project called Planets1 to the Planets workspace.

    (A console application is a program that must be run from a DOS console. Users interact with console applications by typing and reading text. A Windows application (Win32 Application) has its own graphical user interface (GUI) and runs directly on top of the Windows operating system.)

    main() for Planets1

    A project is also a container! It contains files and Configurations. Many types of files can be put into a project, including Java, C, and C++ source files; header files; html files; text files; even Word and Excel files. A setting is a set of compiler and linker options. Document windows are file views.

  5. Select New a third time from the File menu. Select the File tab this time. Select "C++ source file" from the list box, then type the name "main" into the "File name" edit control. Make sure the "add to project" box is checked. Press the OK button.
  6. A document window for main.cpp should appear in the view area. Type the following C++ program into this window:

    #include <iostream>
    using namespace std;

    int main()

    {

    cout << "Hello, Earth!\n";

    return 0;

    }

     

    Build and Test Planets1

  7. Select "Build Planets1.exe" from the Build menu. You should see the following messages displayed in the build tab of the output window:
  8. -----------Configuration: Planets1 - Win32 Debug--------------

    Compiling...

    main.cpp

    Linking...

    Planets1.exe - 0 error(s), 0 warning(s)

     

  9. Select "Execute Planets1.exe" from the Build menu. A DOS console window should appear displaying the text "Hello, Earth!".
  10. Congradulations. You have completed your first VC++ project.

    Planets (Version 2)

  11. Follow the same steps to create a second project called Planets2 in the same workspace. Create a new C++ source file for this project, also called main. This time main is a little more ambitious:
  12. #include <iostream>

    using namespace std;

    int main()

    {

    cout << "Hello, Earth!\n";

    cout << "Hello, Mars!\n";

    cout << "Hello, Venus!\n";

    cout << "Hello, Mercury!\n";

    return 0;

    }

     

  13. Build and test this program.
  14. Planets for Windows (Version 3)

  15. Build a third Planets project called Planets3, only this time select MFC App Wizard (exe) as the project type. MFC stands for Microsoft Foundation Class. It is a framework for building windows applications (as opposed to console applications). A dialog box appears asking what type of application you want. Just press the Finish button. Press the OK button in the dialog that appears next.
  16. The App Wizard automatically generates many files and classes for you. When it is done, you can build and test the application. It does quite a bit considering you haven't written any code yet.

    Look at the File View in the workspace window. Notice that the files from all three projects are listed in a tree control. Switch to the Class View tab. Planets 1 and 2 don't have any classes, but Planets3 has six classes, including CPlanets3Doc (the document class) and CPlanets3View (the view class). The class view of a project can be used to quickly navigate through the project's many definitions.

  17. Expand the CPlanets3View class and double click on the OnDraw() function. A document view should pop up displaying the file Planets3View.cpp. The OnDraw function displays shapes and text in the application's document windows. Add the bold face lines shown below to your OnDraw function. (The numbers are window coordinates, CDC stands for "Class: Device Context", pDC stands for "pointer to Device Context", and TextOut() is a device context member function. Anything that can accept output in Windows, such as a window or printer, is represented by a device context.):
  18. void CPlanets3View::OnDraw(CDC* pDC)

    {

    CPlanets3Doc* pDoc = GetDocument();

    ASSERT_VALID(pDoc);

    // TODO: add draw code for native data here

    pDC->TextOut(5, 0, "Hello, Earth!");

    pDC->TextOut(5, 50, "Hello, Mars!");

    pDC->TextOut(5, 100, "Hello, Venus!");

    }

     

  19. Rebuild and test Planets3. You should see a desktop window containing a document window like the one below:
  20. This is a full fledged windows application! Play with it. Select New from its File menu. What happens? Select New Window from the Windows menu. What happens? How is it different from the earlier console versions of Planets?
  21. Executing Planets from the Desktop or a DOS Console

    Quit MSDS, then locate the Planets folder on the desktop. This folder contains the planets workspace in the file Planets.dsw. The three projects are contained in the subfolders Plenets1, Planets2, and Planets3.

    Open these folders. Inside each one is a file with a .dsp extension. These files contain the corresponding project documents. These folders also include the project's C++ files.

  22. Open the Debug folder inside the Project3 folder. This folder contains Planet3's object and executable files. See the Planets3.exe icon? Double click on it with the left mouse button. What happens?
  23. Open the Debug folder inside the Planets2 folder. The icon called Planets2 represents the Planets2 executable. Double clicking on this produces a momentary flash. What happened?
  24. Start the command prompt from the Start menu. (This is the same as the DOS console.) Use the cd command to change to the planets2\debug directory, then type planets2:
  25. Improving Planets (Version 2.1)

  26. Restart MSDS. Select "Open Workspace" from the File menu. Find the file Planets.dsw in the Open Workspace dialog box and open it. Select "Set Active Project" submenu from the Project menu and select Planets2. Use the File View tab of the Workspace window to open Planet2's main.cpp file, and add a few more planets:
  27. #include <iostream>
    using namespace std;

    int main()
    {
    cout << "Hello, Earth!\n";
    cout << "Hello, Mars!\n";
    cout << "Hello, Venus!\n";
    cout << "Hello, Mercury!\n";
    cout << "Hello, Uranus!\n";
    cout << "Hello, Saturn!\n";
    cout << "Hello, Jupiter!\n";
    cout << "Hello, Neptune!\n";
    // sorry, Pluto

    return 0;
    }

     

  28. Rebuild and test Planets2.
  29. Adding a Text File to a Project

  30. Add a readme.txt file to your project containing instructions to the user:
  31. Planets displays a complete list of

    all eight planets. If you enjoy it,
    please send me $100!

     

    Planets for the Web (Version 4)

  32. Create Planets4, a fourth project added in the Planets workspace. Add an HTML file to the project. To do this select New from the File menu. Select the file tab from the New dialog box, select "HTML page" from the list box, give it a name, then press OK. This generates an HTML boilerplate. Replace the line:

    <!-- Insert HTML here -->

    with the lines shown in boldface below:
  33. <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Developer Studio">
    <META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
    <TITLE>Document Title</TITLE>
    </HEAD>
    <BODY>

    Hello, Earth! <br>

    Hello, Mars! <br>

    Hello, Venus! <br>

    Hello, Mercury! <br>

    Hello, Uranus! <br>

    Hello, Saturn! <br>

    Hello, Jupiter! <br>

    Hello, Neptune!

    </BODY>

    </HTML>

     

  34. Use your favorite web browser to test the HTML version.
  35. Release vs. Debug Versions

    In order to execute a program in the debugger, special library functions that help the debugger have to be linked into the program. This is why the executables for even the simple console versions of Planets are large. Of course, when we decide to go commercial with Planets, we'll want to rebuild the executable without these special debugger functions. Certainly our customers (and teachers) aren't interested in debugging our program, and they'll appreciate a smaller, more compact executable.

    Open the Planets workspace and set the active project to Planets2. Assuming debugging is complete, select "Select Active Configuration" from the build menu. The following dialog box appears:

  36. Select "Planets2-Win32 Release" and press OK. Rebuild and test your application. Browsing through the Planets\Planets2 folder, you will discover that a new folder called Release has been created. It contains the .obj and .exe files for the release version of Planets. Compare the size of the executable with the size of the debug version. (If you didn't write down the size earlier, you'll have to reset the active configuration to debug and rebuild the executable.)

Removing and Adding Files from a Project

To remove a file from a project, click once on the file's icon in the File View tree control in the Workspace window, then press the Delete key. The same technique can also be used to remove projects from a workspace.

To add a file that already exists to a project, select Files from the "Add to Project" submenu of the Project menu. (If your workspace contains multiple projects, make sure the right project is selected.) Note: the file doesn't need to be in any particular directory.

Configurations

Besides files, a project also includes configurations. There are two default configurations: Release and Debug. The "Set Active Configuration" command on the Build menu allows programmers to select a setting. The Configurations command on the Build menu allows programmers to add and remove configurations to their project.

A configuration is a collection of compiler, linker, debugger, and other settings. Select settings from the Project menu to display the settings dialog box. It is a tabbed window that allows programmers to change settings on these components:

For more information about configurations, settings, and working with projects in general, read:

Visual C++ Documentation
Using Visual C++
Visual C++ User's Guide
Working with Projects
How do I ...

in the on-line help system