// kompact.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"



#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// The title bar text
char  line[255], line2[255], line3[255];		// Fam beziehungen und Pers Daten				
HWND GhWnd;

// Foward 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);
LRESULT CALLBACK	adressProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	familProc(HWND, UINT, WPARAM, LPARAM);

extern int read_famil_relation(persn *psn, famil *fml);
extern int read_famil_adresse(famil *fml, adresse *a);
extern int read_person(persn *p, famil *fml, int i);
extern void frage_antwort(famil *fml);
	

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     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_KOMPACT, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);


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

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

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

	return msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is 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_KOMPACT);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCSTR)IDC_KOMPACT;
	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;
}

//
//  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;
	TCHAR szHello[MAX_LOADSTRING];
	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

	switch (message) 
	{
		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;
				case IDM_VERWANDT:
					DialogBox(hInst, (LPCTSTR)IDD_PROPPAGE_MEDIUM, hWnd, (DLGPROC)familProc);
					hWnd = GhWnd;
					break;
				case IDM_ADRESSE:
                    DialogBox(hInst, (LPCTSTR)IDD_ADRESSBOX, hWnd, (DLGPROC)adressProc);
				    break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			
			RECT rt;
			GetClientRect(hWnd, &rt);
			DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
			
			// TODO: Add any drawing code here...
 
			int len;
			len = strlen(line);
			DrawText(hdc, line, len, &rt, DT_CENTER);
			
			//len += strlen(g_line2);
			//DrawText(hdc, g_line2, len, &rt, DT_CENTER);


			EndPaint(hWnd, &ps);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

// Mesage 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;
}




// Mesage handler for famil box.
LRESULT CALLBACK familProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
struct famil familie[5];
struct persn person[10];

char Feld1[255], Feld2[255], Feld3[255], Feld4[255], Feld5[255], Feld6[255], 
	 Feld7[255], Feld8[255], Feld9[255];	

     switch (message)
	{
		case WM_INITDIALOG:
				return TRUE;

		case WM_COMMAND:
		     		
			switch (wParam)
			{
			case IDOK://if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
				{

					int i;

					for (i = 0; i<5;i++)
						familie[i].mann = familie[i].frau = familie[i].sohn = familie[i].tochter = NULL;
						familie[i].ad = NULL;
					for (i = 0; i < 10; i++)
						{
							person[i].name[0] = person[i].vname[0] = person[i].Typ[0] = '\0';
							person[i].alt = 0;
							person[i].fl = NULL;
						}
                
		                GetDlgItemText(hDlg,IDC_EDIT1, Feld1,255);		// einlesen der FamDaten aus der Maske
						GetDlgItemText(hDlg,IDC_EDIT2, Feld2,255);
						GetDlgItemText(hDlg,IDC_EDIT3, Feld3,255);
						GetDlgItemText(hDlg,IDC_EDIT4, Feld4,255);
						GetDlgItemText(hDlg,IDC_EDIT5, Feld5,255);
						GetDlgItemText(hDlg,IDC_EDIT6, Feld6,255);
						GetDlgItemText(hDlg,IDC_EDIT7, Feld7,255);
						GetDlgItemText(hDlg,IDC_EDIT8, Feld8,255);
						GetDlgItemText(hDlg,IDC_EDIT9, Feld9,255);
				
						// zuweisen der Parameter an "line"
					    if (!strcmp(Feld3, "") & !strcmp(Feld4, "") & !strcmp(Feld2, ""))
						{	
						strcpy(line , "");				// person X == familie Y				
						strcat(line, "person ");
						strcat(line, Feld5);
						strcat(line, " == ");
						strcat(line, "familie ");
						strcat(line, Feld1);
						while (1)
						{ read_famil_relation(person, familie);
						  DestroyWindow(GhWnd);
						  DialogBox(hInst, (LPCTSTR)IDD_PROPPAGE_MEDIUM, GhWnd, (DLGPROC)familProc);
						  break;}
							
						}

							else if
								 (!strcmp(Feld3, "") & !strcmp(Feld4, ""))
							{
							strcat(line, Feld1);
							strcpy(line, "");				// familie Y sohn/tochter == person X
							strcat(line, "familie ");
							strcat(line, " ");
							strcat(line, Feld2);
							strcat(line, " == ");
							strcat(line, "person ");
							strcat(line,Feld5);
							while (1)
							{ read_famil_relation(person, familie);
							  DestroyWindow(GhWnd);
							  DialogBox(hInst, (LPCTSTR)IDD_PROPPAGE_MEDIUM, GhWnd, (DLGPROC)familProc);
							  break;}
							}
								else 
								{
								strcpy(line, "");				// familie Y sohn/tochter == familie Z mann/frau == person X
								strcat(line, "familie ");
								strcat(line, Feld1);
								strcat(line, " ");
								strcat(line, Feld2);
								strcat(line, " == ");
								strcat(line, "familie ");
								strcat(line, Feld3);
								strcat(line, " ");
								strcat(line,Feld4);
								strcat(line, " == ");
								strcat(line, "person ");
								strcat(line,Feld5);
								while (1)
								{ read_famil_relation(person, familie);
								  DestroyWindow(GhWnd);
								  DialogBox(hInst, (LPCTSTR)IDD_PROPPAGE_MEDIUM, GhWnd, (DLGPROC)familProc);
								  break;}
								}
				
						//line fr Familienbeziehung, line2 fr Personenbeschreibung
						strcat(line2,"");
						strcat(line2,Feld6);
						strcat(line2," ");
						strcat(line2,Feld7);
						strcat(line2," ");
						strcat(line2,Feld8);
						strcat(line2," ");
						strcat(line2,Feld9);
						
						read_person(&person[i], familie, i);

				}				
			case IDCANCEL:  
				{
				 //	strcpy(line, "Ende");		
				 //	read_famil_relation(person, familie);
				}
				
			EndDialog(hDlg, LOWORD(wParam));
			}		        
	 return TRUE;			
	 break;
}	// switch
    return FALSE;
}

// Mesage handler for adress box.
LRESULT CALLBACK adressProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
struct famil familie[5];
struct adresse adress[5];

char Feld10[255], Feld11[255], Feld12[255], Feld13[255], Feld14[255];

     switch (message)
	 {
	 case WM_INITDIALOG:
		     return TRUE;

	 case WM_COMMAND:

		  switch (wParam)
		  {
		  case IDOK:
			  {
						GetDlgItemText(hDlg,IDC_EDIT10, Feld10,255);		// einlesen der AdressDaten aus der Maske
						GetDlgItemText(hDlg,IDC_EDIT11, Feld11,255);
						GetDlgItemText(hDlg,IDC_EDIT12, Feld12,255);
						GetDlgItemText(hDlg,IDC_EDIT13, Feld13,255);
						GetDlgItemText(hDlg,IDC_EDIT14, Feld14,255);
						
						strcpy(line3, "");
						strcat(line3, "familie ");
						strcat(line3, Feld14);
						strcat(line3, Feld10);
                        strcat(line3, Feld11);
						strcat(line3, Feld12);
						strcat(line3, Feld13);
			            			
			            read_famil_adresse(familie, adress);
			  
			  }
           
		  case IDCANCEL:
              {

			  }

          EndDialog(hDlg, LOWORD(wParam));
	 return TRUE;			
	 break;
		  }	// switch
     return FALSE;
}
}