// ModelCatsDlg.cpp : implementation file // #include "stdafx.h" #include "sp.h" #include "ModelCatsDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CModelCatsDlg dialog CModelCatsDlg::CModelCatsDlg(CWnd* pParent /*=NULL*/) : CDialog(CModelCatsDlg::IDD, pParent) { //{{AFX_DATA_INIT(CModelCatsDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT m_bAddMode=false; m_pApp = (CSpApp*)AfxGetApp(); /*rs=new GZRset("Error: Unit model categories dialog"); rs->SetConnect(m_pApp->strConnectString); */ //Initialize recordset pointer rs=m_pApp->rsPool->GetRS("CModelCatsDlg"); m_pstrReturnValue=NULL; } CModelCatsDlg::~CModelCatsDlg() { m_pApp->rsPool->ReleaseRS(&rs->m_nID); } void CModelCatsDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CModelCatsDlg) DDX_Control(pDX, IDC_LBLCATEGORY, m_lblCategory); DDX_Control(pDX, IDC_EDNOTES, m_edNotes); DDX_Control(pDX, IDC_EDCATEGORY, m_edCategory); DDX_Control(pDX, IDC_CBCATEGORIES, m_cbCategories); DDX_Control(pDX, IDC_BTNDONE, m_btnDone); DDX_Control(pDX, IDC_BTNDELETE, m_btnDelete); DDX_Control(pDX, IDC_BTNADD, m_btnAdd); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CModelCatsDlg, CDialog) //{{AFX_MSG_MAP(CModelCatsDlg) ON_BN_CLICKED(IDC_BTNADD, OnBtnadd) ON_BN_CLICKED(IDC_BTNDELETE, OnBtndelete) ON_BN_CLICKED(IDC_BTNDONE, OnBtndone) ON_CBN_CLOSEUP(IDC_CBCATEGORIES, OnCloseupCbcategories) ON_EN_KILLFOCUS(IDC_EDCATEGORY, OnKillfocusEdcategory) ON_EN_KILLFOCUS(IDC_EDNOTES, OnKillfocusEdnotes) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CModelCatsDlg message handlers BOOL CModelCatsDlg::OnInitDialog() { CDialog::OnInitDialog(); Security(); FillList(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CModelCatsDlg::OnBtnadd() { CString strName,strNotes,q; if(!m_bAddMode) { //go add mode m_btnDone.ShowWindow(FALSE); m_btnAdd.SetWindowText("SAVE"); m_lblCategory.ShowWindow(FALSE); m_cbCategories.ShowWindow(FALSE); m_btnDelete.SetWindowText("CANCEL"); m_edCategory.SetWindowText(""); m_edNotes.SetWindowText(""); m_bAddMode=true; } else { //save m_edCategory.GetWindowText(strName); m_edNotes.GetWindowText(strNotes); if(strName.IsEmpty()) { AfxMessageBox("A category name is required to save this record"); return; } q.Format("INSERT INTO unitmodelcats ( name, notes )" "SELECT \"%s\" , \"%s\";",strName,strNotes); if(!rs->Ex(q)) return; rs->Close(); //go normal mode m_btnDone.ShowWindow(TRUE); m_btnAdd.SetWindowText("Add"); m_lblCategory.ShowWindow(TRUE); m_cbCategories.ShowWindow(TRUE); m_btnDelete.SetWindowText("Delete"); m_strSelectedCategory=""; m_bAddMode=false; FillList(); } } void CModelCatsDlg::OnBtndelete() { CString q; if(m_bAddMode) { m_btnDone.ShowWindow(TRUE); m_btnAdd.SetWindowText("Add"); m_lblCategory.ShowWindow(TRUE); m_cbCategories.ShowWindow(TRUE); m_btnDelete.SetWindowText("Delete"); m_strSelectedCategory=""; m_bAddMode=false; FillList(); return; } //DELETE //see if it can be deleted first q.Format("SELECT unitmodels.id " "FROM unitmodels WHERE (((unitmodels.category)=%s));",m_strSelectedCategory); rs->Query(q); if(!rs->IsEmpty()) { AfxMessageBox("Data integrity protection!\r\n" "You can't delete this record at this time because\r\n" "it's attached to one or more Unit models."); return; } if(AfxMessageBox("Permanently delete this category.\r\nAre you sure?",MB_YESNO)==IDYES) { //run a delete query on it q.Format("DELETE unitmodelcats.*, unitmodelcats.id " "FROM unitmodelcats WHERE (((unitmodelcats.id)=%s));",m_strSelectedCategory); rs->Ex(q); m_strSelectedCategory.Empty(); FillList(); } } void CModelCatsDlg::OnBtndone() { if(m_pstrReturnValue!=NULL) *m_pstrReturnValue=m_strSelectedCategory; CDialog::OnOK(); } void CModelCatsDlg::OnCloseupCbcategories() { m_strSelectedCategory=m_cbCategories.GetCurrentRowID(); FillFields(); } void CModelCatsDlg::OnKillfocusEdcategory() { SaveField(&m_edCategory,"name",false); if(!m_bAddMode) FillList(); } void CModelCatsDlg::OnKillfocusEdnotes() { SaveField(&m_edNotes,"notes",false); } void CModelCatsDlg::SetReturnString(CString *strReturn) { m_pstrReturnValue=strReturn; } //**************************************** //FILL LIST BOX //**************************************** void CModelCatsDlg::FillList() { CString strData; CString strIndex; long lData; m_cbCategories.Clear(); rs->Query("SELECT unitmodelcats.* FROM unitmodelcats;"); if(rs->IsEmpty()) return; rs->MoveFirst(); do { rs->FetchField("name",&strData); rs->FetchField("id",&lData); strIndex.Format("%u",lData); m_cbCategories.AddRow(strData,strIndex); }while(rs->MoveForward()); //pretend user has selected so that other fields get filled in if(m_strSelectedCategory.IsEmpty()) //first time in { m_cbCategories.SetCurSel(0); m_strSelectedCategory=m_cbCategories.GetCurrentRowID(); } else//something valid was selected before so stick with it m_cbCategories.Select(m_strSelectedCategory); FillFields(); } //Fillout contents of fields void CModelCatsDlg::FillFields() { CString q,strData; q.Format("SELECT unitmodelcats.* " "FROM unitmodelcats " "WHERE (((unitmodelcats.id)=%s));",m_cbCategories.GetCurrentRowID()); rs->Query(q); if(rs->IsEmpty()) return; rs->FetchField("name",&strData); m_edCategory.SetWindowText(strData); rs->FetchField("notes",&strData); m_edNotes.SetWindowText(strData); } //************************************************************ //SAVE EDIT CONTROL FIELD //************************************************************ bool CModelCatsDlg::SaveField(CEdit *edControl,CString fldname,bool AllowEmpty) { if(m_bAddMode) return true;//dont attempt to update CString str; //do nothing if not changed if(edControl->GetModify()!=TRUE) return true; edControl->GetWindowText(str); //dont save empty fields if not allowed to if(!AllowEmpty) { if(str.IsEmpty()) { edControl->Undo(); return false; } } rs->UpdateField(fldname,&str); rs->SaveRecord(); return true; } void CModelCatsDlg::Security() { int x=m_pApp->Allowed(RMODELS,true); if(x==0)//no access allowed { m_pApp->SecurityWarning(); CDialog::OnCancel(); } if(x==2)//read only { m_btnAdd.ShowWindow(FALSE); m_btnDelete.ShowWindow(FALSE); //m_cbCategories.EnableWindow(FALSE); m_edCategory.SetReadOnly(TRUE); m_edNotes.SetReadOnly(TRUE); m_lblCategory.EnableWindow(FALSE); } }