home *** CD-ROM | disk | FTP | other *** search
- // ******************************************************************
- //
- // persdlg.cpp : implementation file
- //
- // Implemented classes:
- // CEditPersDlg
- //
- // Author: Nils Behncke
- // POET Software GmbH, August 1993
- //
- // ******************************************************************
-
-
- #include <stdafx.h> // header to MFC
- #include <afxdlgs.h> // header to standarddialogs of MFC
- #include <persdlg.h> // header to this file
- #include <adrdlg.h> // header to addressdialog
- #include <stepdlg.h> // header to stepdialog
- #include <pictdlg.h> // header to picturedialog
- #include <helloapp.h> // header to application and window-class
-
-
- extern CHelloWinApp NEAR theApp;
-
- // class CEditPersDlg derived from CDialog and created by ClassWizard
-
- #ifdef _DEBUG // added by ClassWizard
- #undef THIS_FILE
- static char BASED_CODE THIS_FILE[] = __FILE__;
- #endif
-
-
- /////////////////////////////////////////////////////////////////////////////
- // CEditPersDlg dialog
-
- CEditPersDlg::CEditPersDlg(CWnd* pParent, UINT nIDTemp ) // extended constructor (see comment in headerfile)
- : CDialog(nIDTemp, pParent)
- {
- //{{AFX_DATA_INIT(CEditPersDlg)
- // member variables will be initialized in member funtion Init
- //}}AFX_DATA_INIT
- }
-
-
- CEditPersDlg::~CEditPersDlg ()
- {
- if (allperson)
- delete allperson;
- if (person)
- person->Forget ();
- }
-
- void CEditPersDlg::POET_Error (char *text, int error)
- {
- char buffer[100];
- char title[100];
-
- PtError (buffer, text, error); // build errortext
- GetWindowText (title, sizeof (title)); // get title of current window
- MessageBox (buffer, title, MB_OK); // show errormessage
- }
-
-
- void CEditPersDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CEditPersDlg)
- DDX_Control(pDX, IDC_LIST_FIRSTNAMES, m_CListFirstnames);
- DDX_Control(pDX, IDC_LIST_ADDRESSES, m_CListAddresses);
- DDX_Text(pDX, IDC_EDIT_NAME, m_strName);
- DDX_Text(pDX, IDC_EDIT_FIRSTNAME, m_strFirstname);
- //}}AFX_DATA_MAP
- }
-
- void CEditPersDlg::ClearControls ()
- {
- m_CListFirstnames.ResetContent ();
- m_CListAddresses.ResetContent ();
- m_strName = "";
- m_strFirstname = "";
- }
-
- BEGIN_MESSAGE_MAP(CEditPersDlg, CDialog)
- //{{AFX_MSG_MAP(CEditPersDlg)
- ON_BN_CLICKED(IDC_ADR_UPDATE, OnClickedAdrUpdate)
- ON_BN_CLICKED(IDC_ADR_NEW, OnClickedAdrNew)
- ON_BN_CLICKED(IDC_STEP, OnClickedStep)
- ON_BN_CLICKED(IDC_STORE, OnClickedStore)
- ON_BN_CLICKED(IDC_NEXT, OnClickedNext)
- ON_BN_CLICKED(IDC_COPY, OnClickedCopy)
- ON_BN_CLICKED(IDC_DELETE, OnClickedDelete)
- ON_BN_CLICKED(IDC_PREV, OnClickedPrev)
- ON_BN_CLICKED(IDC_ASSPIC, OnClickedAsspic)
- ON_EN_CHANGE(IDC_EDIT_FIRSTNAME, OnChangeEditFirstname)
- ON_EN_KILLFOCUS(IDC_EDIT_FIRSTNAME, OnKillfocusEditFirstname)
- ON_BN_CLICKED(IDC_ADR_DELETE, OnClickedAdrDelete)
- ON_BN_CLICKED(IDC_CLEAR, OnClickedClear)
- ON_LBN_DBLCLK(IDC_LIST_ADDRESSES, OnDblclkListAddresses)
- ON_LBN_DBLCLK(IDC_LIST_FIRSTNAMES, OnDblclkListFirstnames)
- ON_BN_CLICKED(IDC_SHOWPIC, OnClickedShowpic)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
-
-
- void CEditPersDlg::Show ()
- {
- PtString fname;
- Address *adr;
- int register err = 0;
- char buffer[100];
-
- ClearControls (); // initialize Controls
-
- m_strName = person->GetName (); // set name
-
- if (person->firstnames.Seek (0L, PtSTART) == 0 && person->firstnames.Get (fname) == 0)
- m_strFirstname = CString (fname); // set firstname
-
- for ( // set firstnames
- err=person->firstnames.Seek (0L, PtSTART);
- (err==0) && (person->firstnames.Get (fname) == 0);
- err = person->firstnames.Seek (1L, PtCURRENT)
- )
- m_CListFirstnames.AddString (CString(fname));
-
- for ( // set addresses
- err=person->adrs.Seek (0L, PtSTART);
- (err==0) && person->adrs.Get (adr) == 0;
- person->adrs.Unget (adr),
- err = person->adrs.Seek (1L, PtCURRENT)
- )
- m_CListAddresses.AddString (adr->Print (buffer));
- UpdateData (FALSE); // set values to dialog membervariables
- }
-
-
-
-
- void CEditPersDlg::Init ()
- {
- int err = 0;
-
- firstname_is_modified = 0; // editbox firstnames not changed
- NewAllSetPtr (); // create polymorph AllSet
-
- if ((err=allperson->Seek (0L, PtSTART)) == 0) // looking for first entry
- err = allperson->Get (person);
- if (err != 0) // if no person in allset
- {
- NewPersonPtr (); // allocate memory for polymorph person
- ClearControls ();
- person->Init ();
- person->Assign (theApp.GetOA ());
- }
- Show (); // show person in dialog
- }
-
- void CEditPersDlg::Fill ()
- {
- UpdateData ();
- person->SetName (m_strName.GetBuffer (m_strName.GetLength ())); // actualize name
- m_strName.ReleaseBuffer ();
-
- int index;
- char buffer[100];
-
- person->firstnames.Clear ();
- for (index = 0; index < m_CListFirstnames.GetCount (); ) // actualize firstnames
- {
- m_CListFirstnames.GetText (index++, buffer);
- PtString fname;
- fname = buffer;
- person->firstnames.Append (fname);
- }
- } // addresses already updated
-
- static char filebuffer[10240];
-
- void CEditPersDlg::LoadBitmap ( char * Name, PtBlob * pic )
- {
- HFILE file = _lopen((LPSTR) Name, OF_READ); // try to read file
- if (file == -1)
- {
- MessageBox("filename does not exist", "file name alert", MB_OK);
- return;
- }
- UINT Len = 0;
- while ( Len = _lread ( file, filebuffer, sizeof(filebuffer))) // write buffer into Blob
- pic->Append ( filebuffer, Len );
-
- _lclose(file);
- return;
- }
-
- void CEditPersDlg::NewPersonPtr ()
- {
- person = new Person ();
- }
-
- void CEditPersDlg::NewAllSetPtr ()
- {
- allperson = new PersonAllSet (theApp.GetOA ()); // create PersonAllSet
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CEditPersDlg message handlers
-
-
- BOOL CEditPersDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
- Init (); // initialize controls befor show dialogwindow
-
- return TRUE; // return TRUE unless you set the focus to a control
- }
-
-
- void CEditPersDlg::OnClickedAdrUpdate()
- {
-
- if ((item = m_CListAddresses.GetCurSel ()) >= 0)
- {
- Address *a; // new address
- int nResult;
-
- person->adrs.Seek ( (long) item, PtSTART); // set position in set on current selection in listbox
- person->adrs.Get (a); // fill new address
-
- CAddressDlg updateaddress (this); // create dialog
- updateaddress.SetAddress (a->GetStreet (), a->GetZip (), a->GetCity ()); // initialize dialog
- nResult = updateaddress.DoModal (); // start dialog
-
- if (nResult == IDOK) // update address only on clicked OK button
- {
- char buffer [100];
-
- a->SetAddress ( updateaddress.GetStreet (), // update new address a
- updateaddress.GetZip (),
- updateaddress.GetCity () );
- m_CListAddresses.DeleteString (item); // delete old address in listbox
- m_CListAddresses.InsertString (item, a->Print (buffer)); // insert address-string in listbox
- a->Assign (theApp.GetOA ()); // assign new address to database
- person->adrs.Put (a); // replace old with new address in the set of addresses
- }
- person->adrs.Unget (a); // free resources allocated by Get
- updateaddress.EndDialog (nResult); // end dialog
- }
- }
-
-
- void CEditPersDlg::OnClickedAdrNew()
- {
- Address *a = new Address (); // new address
-
- CAddressDlg newaddress (this); // create dialog
- newaddress.SetAddress ("", 0, ""); // initialize dialog
- int nResult = newaddress.DoModal (); // call dialog
-
- if (nResult == IDOK) // add new address only on OK
- {
- char buffer [100];
-
- a->SetAddress ( newaddress.GetStreet (), // update new address a
- newaddress.GetZip (),
- newaddress.GetCity () );
- m_CListAddresses.AddString (a->Print (buffer)); // add address-string to listbox
- a->Assign (theApp.GetOA ()); // assign new address to database
- person->adrs.Append (a); // append new address to set of addresses
- a->Forget ();
- }
- else
- delete a;
- newaddress.EndDialog (nResult); // end dialog
-
- }
-
- void CEditPersDlg::OnClickedAdrDelete()
- {
- if ((item = m_CListAddresses.GetCurSel ()) >= 0)
- {
- person->adrs.Seek ( (long) item, PtSTART); // set position to address to delete
- person->adrs.Delete (); // delete address
- m_CListAddresses.DeleteString (item); // remove address from listbox
- }
- }
-
-
- void CEditPersDlg::OnDblclkListAddresses()
- {
- OnClickedAdrUpdate (); // update address if doubleclicked
- }
-
-
- void CEditPersDlg::OnClickedStep()
- {
- int nResult;
- PtSeekMode mode;
- long step;
- int erc;
-
- CStepDlg stepdlg (this);
- nResult = stepdlg.DoModal (); // start dialog
- if (nResult == IDOK)
- {
- step = (long) stepdlg.GetStep ();
- switch (stepdlg.GetSeekmode ())
- {
- case 0 :
- mode = PtSTART;
- break;
- case 1 :
- mode = PtCURRENT;
- break;
- case 2 :
- mode = PtEND;
- break;
- }
- Person *newperson;
-
- if ((erc = allperson->Seek (step, mode)) == 0) // try to step
- erc = allperson->Get (newperson);
- if (erc == 0)
- {
- person->Forget ();
- person = newperson;
- ClearControls ();
- Show (); // show actual person
- }
- else
- POET_Error ("Step", erc);
- }
- stepdlg.EndDialog (nResult); // end dialog
- }
-
-
- void CEditPersDlg::OnClickedStore()
- {
- int err;
- Fill (); // actualize current person
- err = person->Store (PtDEEP); // store person
- POET_Error ("Object is stored", err); // show status
- }
-
-
- void CEditPersDlg::OnClickedNext()
- {
- Person *newperson;
- int erc;
-
- if ((erc = allperson->Seek (1L, PtCURRENT)) == 0) // try to step 1
- erc = allperson->Get (newperson);
- if (erc == 0)
- {
- person->Forget ();
- person = newperson;
- ClearControls ();
- Show (); // show actual person
- }
- else
- POET_Error ("Next", erc); // show status
- }
-
-
- void CEditPersDlg::OnClickedCopy()
- {
- int erc;
- Address *adr, *newadr;
-
- for ( // set new references to copied addresses
- erc=person->adrs.Seek (0L, PtSTART);
- (erc==0) && person->adrs.Get (adr) == 0;
- person->adrs.Unget (adr),
- erc = person->adrs.Seek (1L, PtCURRENT)
- )
- {
- char *city = new char[100];
- char *street = new char[100];
- newadr = new Address ();
- newadr->SetAddress ( strcpy (street, adr->GetStreet ()),
- adr->GetZip (),
- strcpy (city, adr->GetCity ()) );
- newadr->Assign (theApp.GetOA ());
- person->adrs.Put (newadr);
- delete city;
- delete street;
- newadr->Forget (); // Remove new address
- }
-
- Picture pic, *oldpic = NULL;
- PtBlob *blob = NULL;
-
- if ( person->pict.IsReferenced () )
- {
- int err = person->pict.Get(oldpic); // Get old picture
- blob = new PtBlob(oldpic->picture); // Copy Blob using copy-constructor
- person->pict.Unget (oldpic); // Remove old picture from memory
- pic.SetPicture (*blob); // Set new Blob to new picture object
-
- if ( (err = pic.Assign(theApp.GetOA())) != 0) // Assign the Picture object to the data base
- POET_Error ("Did not copy picture", err);
- else
- person->pict.SetReference(&pic); // Set the reference of the Picture in the
- // Person object to the new object
- }
-
- person->UnAssign (); // cut connection to database
- Fill (); // fill independend person with current data
-
- person->Assign (theApp.GetOA ()); // assign new person to database
- erc = person->Store (PtDEEP); // store person
- POET_Error (" Object is copied ", erc); // show status
-
- if (blob) // Delete blob, if needed
- delete blob;
- }
-
-
- void CEditPersDlg::OnClickedDelete()
- {
- int erc;
-
- if (person->IsStored ()) // only delete if exist
- {
- erc = person->Delete (PtDEEP); // delete current person
- POET_Error (" Object is deleted ", erc); // show status
- person->UnAssign (); // deleted person is able to be stored again
- person->Assign (theApp.GetOA ()); //
- }
- }
-
-
- void CEditPersDlg::OnClickedPrev()
- {
- Person *newperson;
- int erc;
-
- if ((erc = allperson->Seek (-1L, PtCURRENT)) == 0) // same as next with negative step 1
- erc = allperson->Get (newperson);
- if (erc == 0)
- {
- person->Forget ();
- person = newperson;
- ClearControls ();
- Show (); // show actual person
- }
- else
- POET_Error ("Previous", erc); // show status
- }
-
-
- void CEditPersDlg::OnClickedAsspic()
- {
- int nResult;
- CString fullpath;
-
- CFileDialog filedlg (TRUE,"*.bmp", "*.bmp", OFN_HIDEREADONLY | // initialize dialog
- OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR,
- "Windows Bitmapfile (*.bmp)|*.bmp||",
- this);
- filedlg.m_ofn.lpstrTitle = "Select Image"; // start dialog
- nResult = filedlg.DoModal ();
- if (nResult == IDOK)
- {
- char *szFileName;
-
- fullpath = filedlg.GetPathName ();
- szFileName = fullpath.GetBuffer (fullpath.GetLength ()); // get filename and path
- fullpath.ReleaseBuffer ();
-
- Picture pic; // Create a new Picture object
-
- LoadBitmap ( szFileName, &pic.picture ); // Load Bitmap from File
-
- int err = 0;
-
- if ( (err = pic.Assign(theApp.GetOA())) != 0) // Assign the Picture object to the data base
- POET_Error ("Assign", err);
- else
- { // Delete the old picture from the database, if available
- if ( person->pict.IsReferenced () )
- person->pict.Delete ();
-
- person->pict.SetReference(&pic); // Set the reference of the Picture in the
- // Person object to the new object
- Fill (); // Update person
- if ((err = person->Store( PtDEEP )) != 0) // Store the Person, the reference and the picture
- POET_Error ("Store Person", err);
- else
- MessageBox("Picture Assigned and Stored", "Hello Message", MB_OK);
- }
- }
-
- filedlg.EndDialog (nResult); // end dialog
- }
-
-
-
- void CEditPersDlg::OnChangeEditFirstname()
- {
- firstname_is_modified = 1; // set modify-flag to TRUE
- }
-
-
- void CEditPersDlg::OnKillfocusEditFirstname()
- {
- if (firstname_is_modified != 0)
- {
- UpdateData (); // recieve data from dialog
- if ((item = m_CListFirstnames.GetCurSel ()) >= 0) // firstname selected in the list ?
- {
- m_CListFirstnames.DeleteString (item); // delete firstname in list
- if (m_strFirstname.GetLength () != 0) // firstname in edit not empty ?
- m_CListFirstnames.InsertString (item, m_strFirstname); // insert firstname in list
- }
- else
- m_CListFirstnames.AddString (m_strFirstname); // puts new element in list
- firstname_is_modified = 0; // set modify-flag to FALSE
- UpdateData (FALSE); // send data to dialog
- }
- }
-
- void CEditPersDlg::OnDblclkListFirstnames()
- {
- UpdateData (); // recieve data from dialog
- m_CListFirstnames.GetText (m_CListFirstnames.GetCurSel (), m_strFirstname); // set doubleclicked firstname
- UpdateData (FALSE); // send data to dialog
- PrevDlgCtrl (); // set focus to firstname-editcontrol
- }
-
-
- void CEditPersDlg::OnClickedClear()
- {
- person->UnAssign (); // cut person's connection to database
- person->Forget (); // actual type of person is not safe,
- NewPersonPtr (); // set correct type of person
- person->Init (); // initialize person's attributes
- person->Assign (theApp.GetOA ()); // assign it to database
- Show (); // show actual person
- }
-
-
- void CEditPersDlg::OnClickedShowpic()
- {
- Picture *picture = NULL;
-
- int err = person->pict.Get(picture); // get Blob
-
- if (!err && picture && picture->IsAssigned())
- {
- int nResult;
-
- CShowPictDlg showpict (this, picture->GetPicture ());
- nResult = showpict.DoModal (); // start dialog
- showpict.EndDialog (nResult);
- person->pict.Unget (picture); // free memory allocated by Get
- }
- else
- MessageBox("No Picture Assigned", "Hello Alert", MB_OK);
- }
-
-
-
-