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

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.

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.

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
-----------Configuration: Planets1 - Win32 Debug--------------
Compiling...
main.cpp
Linking...
Planets1.exe - 0 error(s), 0 warning(s)
Congradulations. You have completed your first VC++ project.
Planets (Version 2)
#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;
}
Planets for Windows (Version 3)
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.
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!");
}

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.


Improving Planets (Version 2.1)
#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;
}
Adding a Text File to a Project
Planets displays a complete list of
all eight planets. If you enjoy it,
please send me $100!
Planets for the Web (Version 4)
<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>
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:

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