All pastes #27686 Raw Edit

Something

public text v1 · immutable
#27686 ·published 2005-11-05 04:34 UTC
rendered paste body
// LAB5WIN.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "LAB5WIN.h"
#define MAX_LOADSTRING 100

// Global Variables:
object Data_Sequence data;
object Pop_Rec_Map m;

HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_LAB5WIN, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_LAB5WIN);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_LAB5WIN);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCTSTR)IDC_LAB5WIN;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//-------------------------------------------------------------------

void Do_Paint_Data (
		preserves HDC hdc,
		alters Data_Sequence& s
	)
{
	variable SIZE str_size;
	object Integer i, z;
	object Integer y_loc, x_loc;
	object Country_Loc_Record lr;
	object Integer spacer = 5;
	     
    x_loc = 10;
    y_loc = 10;

	z = s.Size ();
	for (i = 0; i < z; i++) {
		s.Remove (i, lr);
		GetTextExtentPoint32 (hdc, lr->country, lr->country.Size (), &str_size);
		lr->r.top = y_loc - 2;
		lr->r.left = x_loc - 2; 
		lr->r.bottom = y_loc + str_size.cy + 2;
		lr->r.right = x_loc + str_size.cx + 2;
		Rectangle (hdc, lr->r.left, lr->r.top, lr->r.right, lr->r.bottom);
		TextOut (hdc, x_loc, y_loc, lr->country, lr->country.Size ()); 
		s.Add (i, lr);

		y_loc += (str_size.cy + spacer);
	} // end for

} // Do_Paint_Data

//-------------------------------------------------------------------

void Do_Load_Data (
		produces Data_Sequence& s,
		alters Pop_Rec_Map& m
	)
{
	object Boolean successful;
	object Integer i, size;
	object Text filename, country_name;
	object File input;
	object Population_Record pop_rec;
	object Country_Loc_Record lr;

	filename = "pop.dat";

	input.Open_File (filename, File::read, successful);

	if (!successful) {
		MessageBox (NULL, "Could not open data file", "Error: Open", MB_OK);
	} else {
		s.Clear ();
		input.Read (size, successful);
		for (i = 0; i < size; i++) {
			input.Read (pop_rec->country, successful);
			input.Read (pop_rec->continent, successful);
			input.Read (pop_rec->pop_2001, successful);
			input.Read (pop_rec->births, successful);
			input.Read (pop_rec->deaths, successful);
			input.Read (pop_rec->rate_increase, successful);
			input.Read (pop_rec->pop_change, successful);
			input.Read (pop_rec->pop_2025, successful);
			input.Read (pop_rec->pop_2050, successful);
			input.Read (pop_rec->imr, successful);
			input.Read (pop_rec->tfr, successful);
			input.Read (pop_rec->area, successful);
			input.Read (pop_rec->pop_density, successful);

			country_name = pop_rec->country;

			m.Define (country_name, pop_rec);

			s.Add (0, lr);

		}	// end for
		input.Close_File (successful);
	}	// end if
}	// Do_Load_Data

//-------------------------------------------------------------------

void  Do_LButton_Down (
				preserves HWND hWnd, 
				preserves LPARAM lParam,
				preserves Data_Sequence& s
				)
				//  pre: lParam contains (x, y) coordinates
				//       where left mouse button was clicked
				// post: if mouse clicked over a word in 's', then a MessageBox
				//       is displayed on the screen showing which word was clicked
{
} // end Do_LButton_Down

//-------------------------------------------------------------------

void Format_Text (
         preserves Population_Record& pr, // you'll need to substitute the name of your Record in for Population_Record
         produces Text& formated_text
   )
   // pre: pr
   // post: formatted_text contains Population_Record information for display by MessageBox
{
   variable char c_string[200];

   sprintf (c_string,
      "Continent: %s\n\n2001 Population: %5.1f (M)\n2025 Population: %5.1f (M)\n2050 Population: %5.1f (M)\n\n2001 Density: %d (per sq mi)",
      (char *)pr->continent,
      (float)pr->pop_2001,
      (float)pr->pop_2025,
      (float)pr->pop_2050,
      (int)pr->pop_density
      );
   formated_text = c_string;
} // Format_Text

//-------------------------------------------------------------------

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message) 
	{
	case WM_CREATE:
		Do_Load_Data (data, m);
		break;
	case WM_COMMAND:
		wmId    = LOWORD(wParam); 
		wmEvent = HIWORD(wParam); 
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		Do_Paint_Data (hdc, data);
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Message handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_INITDIALOG:
		return TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
		{
			EndDialog(hDlg, LOWORD(wParam));
			return TRUE;
		}
		break;
	}
	return FALSE;
}