// TableDlg.cpp : implementation file // #include "stdafx.h" #include "KBEditors.h" #include "KBEditorsDoc.h" #include "TableDlg.h" #include "AddDeleteDlg.h" #include "OperationVlaueDlg.h" #include "TableNameDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CTableDlg dialog CTableDlg::CTableDlg(CWnd* pParent /*=NULL*/) : CDialog(CTableDlg::IDD, pParent) { TableObj =NULL; //{{AFX_DATA_INIT(CTableDlg) //}}AFX_DATA_INIT } void CTableDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CTableDlg) DDX_Control(pDX, IDC_TableList, m_TableListCtrl); DDX_Control(pDX, IDC_MSFLEXGRID1, m_FxTable); DDX_Control(pDX, IDC_MSFLEXGRIDRusltList, m_FxTableResult); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CTableDlg, CDialog) //{{AFX_MSG_MAP(CTableDlg) ON_BN_CLICKED(IDC_UpdateTable, OnUpdateTable) ON_BN_CLICKED(IDC_DeleteTable, OnDeleteTable) ON_CBN_SELCHANGE(IDC_TableList, OnSelchangeTableList) ON_CBN_KILLFOCUS(IDC_TableList, OnKillfocusTableList) ON_BN_CLICKED(IDC_PLAY, OnPlay) ON_BN_CLICKED(IDC_Del_RAW, OnDelRAW) ON_BN_CLICKED(IDC_ADD_RAW, OnAddRaw) ON_BN_CLICKED(IDC_Add_Col, OnAddCol) ON_BN_CLICKED(IDC_Del_Col, OnDelCol) ON_COMMAND(ID_TABLE_ADDTABLE, OnTableAddtable) ON_COMMAND(ID_TABLE_RENAMETABLE, OnTableRenametable) ON_COMMAND(ID_TABLE_DELETETABLE, OnTableDeletetable) ON_COMMAND(ID_TABLE_DUPLICATETABLE, OnTableDuplicatetable) ON_COMMAND(ID_AddRow, OnAddRow) ON_COMMAND(ID_DeleteRow, OnDeleteRow) ON_COMMAND(ID_AddCol, OnAddCol) ON_COMMAND(ID_DelCol, OnDelCol) //}}AFX_MSG_MAP END_MESSAGE_MAP() BEGIN_EVENTSINK_MAP(CTableDlg, CDialog) //{{AFX_EVENTSINK_MAP(CTableDlg) ON_EVENT(CTableDlg, IDC_MSFLEXGRID1, -601 /* DblClick */, OnDblClickMsflexgrid1, VTS_NONE) ON_EVENT(CTableDlg, IDC_MSFLEXGRIDRusltList, -601 /* DblClick */, OnDblClickGridRusltList, VTS_NONE) //}}AFX_EVENTSINK_MAP END_EVENTSINK_MAP() ///////////////////////////////////////////////////////////////////////////// // CTableDlg message handlers void CTableDlg::OnUpdateTable() { int Sel= m_TableListCtrl.GetCurSel(); CString str; if (Sel < 0 ) { m_TableListCtrl.GetWindowText(str); }else m_TableListCtrl.GetLBText(Sel,str); if ( str.IsEmpty()) return; str.TrimLeft(); str.TrimRight(); if(!m_Doc->m_TableList.Lookup(str,TableObj)) { TableObj = new CTable; m_TableListCtrl.AddString(str); } m_Doc->m_TableList.SetAt(str,TableObj); } void CTableDlg::SeeDoc(CKBEditorsDoc*Doc) { m_Doc = Doc; } BOOL CTableDlg::OnInitDialog() { CDialog::OnInitDialog(); CenterWindow(GetDesktopWindow()); POSITION pos = m_Doc->m_TableList.GetStartPosition(); while (pos !=NULL) { CString str; m_Doc->m_TableList.GetNextAssoc(pos,str,TableObj); m_TableListCtrl.AddString(str); } // GetSchema(); // SetBodyToFlixGride(); // m_TableListCtrl.SetCurSel(m_TableListCtrl.GetCount()-1); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CTableDlg::OnDeleteTable() { int Sel= m_TableListCtrl.GetCurSel(); CString str; if (Sel < 0 ) return; else m_TableListCtrl.GetLBText(Sel,str); if(!m_Doc->m_TableList.Lookup(str,TableObj)) return; if(AfxMessageBox("Are you sure you want to delete table "+str +" .?",MB_YESNO)==IDNO) return; delete TableObj; m_Doc->m_TableList.RemoveKey(str); m_TableListCtrl.DeleteString(Sel); m_FxTable.Clear(); m_FxTableResult.Clear(); m_FxTable.SetCols(1); m_FxTable.SetRows(2); m_FxTableResult.SetCols(1); m_FxTableResult.SetRows(2); if ( m_TableListCtrl.GetCount() == 0 ) { m_TableListCtrl.SetWindowText(""); } } void CTableDlg::OnSelchangeTableList() { int Sel= m_TableListCtrl.GetCurSel(); CString str; m_FxTable.Clear(); m_FxTableResult.Clear(); if (Sel < 0 ) m_TableListCtrl.GetWindowText(str); else m_TableListCtrl.GetLBText(Sel,str); if(m_Doc->m_TableList.Lookup(str,TableObj)) { GetSchema(); SetBodyToFlixGride(); }else { m_FxTable.SetRows(2); m_FxTable.SetCols(1); m_FxTableResult.SetRows(2); m_FxTableResult.SetCols(1); } } void CTableDlg::GetSchema() { CString FormulationSchema; int Col=0; if( TableObj == NULL ) return ; for (int i=0;i < TableObj->Schema.GetSize();i++) { Col++; FormulationSchema += TableObj->Schema.GetAt( i ) + '|' ; } FormulationSchema.Delete(FormulationSchema.GetLength()-1 , 1 ); if( Col == 0 ) { m_FxTable.SetCols(1); m_FxTable.SetTextMatrix(0,0," "); } else { m_FxTable.SetCols(Col); m_FxTableResult.SetCols(1); m_FxTable.SetFormatString(FormulationSchema); CString str ; TableObj->ResultList.Lookup(0,str); m_FxTableResult.SetFormatString(str); } } void CTableDlg::SetBodyToFlixGride() { int Col,Row; CString RowCol , RowStr , ColStr ;// Row-Col CCell* cell; if( TableObj == NULL ) return ; m_FxTable.SetRows( TableObj->MaxRows + 1 ); m_FxTableResult.SetRows( TableObj->MaxRows + 1 ); POSITION pos = TableObj->Body.GetStartPosition(); while (pos != NULL) { TableObj->Body.GetNextAssoc( pos, RowCol, cell ); //CString Buffer = cell->Attribute + cell->Operator + cell->Value; OperationVlaueDlg Temp; RowStr = Temp.GetCpt(RowCol,'-'); ColStr = Temp.GetProp(RowCol,'-'); // convert string to integer Row = atoi((LPCTSTR)RowStr); Col = atoi((LPCTSTR)ColStr); m_FxTable.SetTextMatrix(Row,Col,cell->Operator + cell->Value); } pos = TableObj->ResultList.GetStartPosition(); while (pos != NULL) { TableObj->ResultList.GetNextAssoc( pos, Row, RowStr ); // convert string to integer //Row = atoi((LPCTSTR)RowStr); if( Row != 0) { RowStr.Remove('-'); m_FxTableResult.SetTextMatrix(Row,0,RowStr); } } } void CTableDlg::OnDblClickMsflexgrid1() { // TODO: Add your control notification handler code here int Col = m_FxTable.GetCol(); int Row = m_FxTable.GetRow(); if (Row == 0) return ; CString Value; Value = m_FxTable.GetTextMatrix(Row,Col); Attribute = m_FxTable.GetTextMatrix(0,Col); if(Attribute.IsEmpty()) return ; OperationVlaueDlg OpValDlg; OpValDlg.strIn = Value; OpValDlg.SeeDoc(CTableDlg::m_Doc,Attribute); OpValDlg.DoModal(); CString strOut= OpValDlg.strOut; // bug in add empty opval in new table TableObj->SetCell(Row,Col,strOut); int i = strOut.Remove('-'); if(Row>TableObj->MaxRows) { TableObj->MaxRows = Row; } m_FxTable.SetTextMatrix(Row,Col, strOut); m_FxTable.Refresh(); } void CTableDlg::OnKillfocusTableList() { OnSelchangeTableList(); } void CTableDlg::OnDblClickGridRusltList() { int Row = m_FxTableResult.GetRow(); if (Row == 0) return ; CString Value; Value = m_FxTableResult.GetTextMatrix(Row,0); Attribute = m_FxTableResult.GetTextMatrix(0,0); if(Attribute.IsEmpty()) return ; OperationVlaueDlg OpValDlg; OpValDlg.strIn = Value; OpValDlg.SeeDoc(CTableDlg::m_Doc,Attribute,TRUE); OpValDlg.DoModal(); CString strOut= OpValDlg.strOut; TableObj->ResultList.SetAt(Row,strOut); int i = strOut.Remove('-'); if(Row>TableObj->MaxRows) { TableObj->MaxRows = Row; } m_FxTableResult.SetTextMatrix(Row,0, strOut); } void CTableDlg::ShitRow() { int Col,Row,DeletedCell; CString RowCol , RowStr , ColStr; Row = m_FxTable.GetRow(); CCell* cell; OperationVlaueDlg Temp; POSITION pos; for (int i = Row; i < TableObj->MaxRows ;i++) { pos = TableObj->Body.GetStartPosition(); while (pos != NULL) { TableObj->Body.GetNextAssoc( pos, RowCol, cell ); RowStr = Temp.GetCpt(RowCol,'-'); ColStr = Temp.GetProp(RowCol,'-'); // convert string to integer DeletedCell = atoi((LPCTSTR)RowStr); Col = atoi((LPCTSTR)ColStr); if( DeletedCell == i+1 ) { TableObj->Body.RemoveKey(RowCol); RowCol.Format("%d-%d",i,Col); TableObj->Body.SetAt(RowCol,cell); } } } ////////////////////////////////// ////////////////////////////////////////// for ( i = Row; i < TableObj->MaxRows ;i++) { pos = TableObj->ResultList.GetStartPosition(); while (pos != NULL) { TableObj->ResultList.GetNextAssoc( pos, Col, ColStr ); if( Col == i+1 ) { TableObj->ResultList.RemoveKey(Col); TableObj->ResultList.SetAt(i,ColStr); } } } ////////////////////////////////////////// ////////////////////////// } void CTableDlg::ShitCol() { int Col,SelcCol,Row; CString RowCol , RowStr , ColStr; SelcCol = m_FxTable.GetRow(); CCell* cell; OperationVlaueDlg Temp; POSITION pos; for (int i = SelcCol; i < m_FxTable.GetCols() ;i++) { pos = TableObj->Body.GetStartPosition(); while (pos != NULL) { TableObj->Body.GetNextAssoc( pos, RowCol, cell ); RowStr = Temp.GetCpt(RowCol,'-'); ColStr = Temp.GetProp(RowCol,'-'); // convert string to integer Row = atoi((LPCTSTR)RowStr); Col = atoi((LPCTSTR)ColStr); if( Col == i+1 ) { TableObj->Body.RemoveKey(RowCol); RowCol.Format("%d-%d",Row,i); TableObj->Body.SetAt(RowCol,cell); } } } } void CTableDlg::OnPlay() { AfxMessageBox("Play") ; TableObj->Play(NULL,&m_Doc->m_CptList); } void CTableDlg::OnOK() { // AfxMessageBox("ONOK") ; CDialog::OnOK(); } void CTableDlg::OnDelRAW() //delete row from botton { int Col,Row,DeletedCell; CString RowCol , RowStr , ColStr; Row = m_FxTable.GetRow(); TableObj->ResultList.RemoveKey (Row); CCell* cell; OperationVlaueDlg Temp; POSITION pos = TableObj->Body.GetStartPosition(); while (pos != NULL) { TableObj->Body.GetNextAssoc( pos, RowCol, cell ); RowStr = Temp.GetCpt(RowCol,'-'); ColStr = Temp.GetProp(RowCol,'-'); // convert string to integer DeletedCell = atoi((LPCTSTR)RowStr); Col = atoi((LPCTSTR)ColStr); if( DeletedCell == Row ) { TableObj->Body.RemoveKey(RowCol); } } //////////////////////////////////////////// ShitRow(); //////////////////////////////////////////// //////////////////////////////////////////// m_FxTable.RemoveItem( Row); m_FxTableResult.RemoveItem(Row); TableObj->MaxRows --; //////////////////////////////////////////// /*int sel = m_TableListCtrl.GetCurSel(); m_TableListCtrl.GetLBText(sel,ColStr); m_Doc->m_TableList.SetAt(ColStr,TableObj);*/ //????????? } void CTableDlg::OnAddRaw() //add row from button { m_FxTable.SetRows(m_FxTable.GetRows()+1); m_FxTableResult.SetRows(m_FxTableResult.GetRows()+1); } void CTableDlg::OnAddCol() { if (TableObj == NULL) { AfxMessageBox("There is no current table .. "); return; } AddDeleteDlg AddCptDlg; AddCptDlg.SeeDoc(m_Doc,TableObj); AddCptDlg.DoModal(); TableObj->ResultList.SetAt(0,AddCptDlg.m_Result); if (AddCptDlg.FlxResultChanged == TRUE){ m_FxTableResult.Clear(); } GetSchema(); } void CTableDlg::OnDelCol() { int Col,DeletedCell; CString RowCol , ColStr; Col = m_FxTable.GetCol(); TableObj->Schema.RemoveAt(Col); CCell* cell; OperationVlaueDlg Temp; POSITION pos = TableObj->Body.GetStartPosition(); while (pos != NULL) { TableObj->Body.GetNextAssoc( pos, RowCol, cell ); ColStr = Temp.GetProp(RowCol,'-'); // convert string to integer DeletedCell = atoi((LPCTSTR)ColStr); if( DeletedCell == Col ) { TableObj->Body.RemoveKey(RowCol); } } //////////////////////////////////////////// ShitCol(); //////////////////////////////////////////// Col = m_FxTable.GetCols(); //////////////////////////////////////////// m_FxTable.SetCols( Col-1); CTableDlg::OnSelchangeTableList(); } void CTableDlg::OnTableAddtable() //add table from menu { CTableNameDlg Dlg; Dlg.OpStr = "Enter Name of new table"; Dlg.DoModal(); CString str = Dlg.strTableName; if(!m_Doc->m_TableList.Lookup(str,TableObj)) { TableObj = new CTable; m_Doc->m_TableList.SetAt(str,TableObj); m_TableListCtrl.AddString(str); } else AfxMessageBox("Table "+ str +" is already exit"); m_TableListCtrl.SelectString(0,str); OnSelchangeTableList(); } void CTableDlg::OnTableRenametable() ///rename table from menu { int Sel= m_TableListCtrl.GetCurSel(); CString str; if (Sel < 0 ) return; else m_TableListCtrl.GetLBText(Sel,str); if(!m_Doc->m_TableList.Lookup(str,TableObj)) return; if(AfxMessageBox("Are you sure you want to rename table "+str +" .?",MB_YESNO)==IDNO) return; CTableNameDlg Dlg; Dlg.OpStr = "Enter a new name for table " + str; Dlg.DoModal(); CString str1 = Dlg.strTableName; if(m_Doc->m_TableList.Lookup(str1,TableObj)) { AfxMessageBox("Table "+ str1 +" is already exit"); return; } m_TableListCtrl.DeleteString(Sel); m_TableListCtrl.AddString(str1); m_Doc->m_TableList.SetAt(str1,TableObj); m_Doc->m_TableList.RemoveKey(str); m_TableListCtrl.SelectString(0,str1); OnSelchangeTableList(); } void CTableDlg::OnTableDeletetable() //delete table from menu { OnDeleteTable(); } void CTableDlg::OnTableDuplicatetable() //Duplicate table { int Sel= m_TableListCtrl.GetCurSel(); CString str; if (Sel < 0 ) return; else m_TableListCtrl.GetLBText(Sel,str); if(!m_Doc->m_TableList.Lookup(str,TableObj)) return; CTableNameDlg Dlg; Dlg.OpStr = "Enter a new name for table " + str +" to be duplicated."; Dlg.DoModal(); CString str1 = Dlg.strTableName; if(m_Doc->m_TableList.Lookup(str1,TableObj)) { AfxMessageBox("Table "+ str1 +" is already exit"); return; } CTable *newTableObj = new CTable; // copy maxrows newTableObj->MaxRows = TableObj->MaxRows; // copy the results POSITION pos = TableObj->ResultList.GetStartPosition(); while (pos != NULL) { int Row; CString RowStr; TableObj->ResultList.GetNextAssoc( pos, Row, RowStr ); newTableObj->ResultList.SetAt(Row,RowStr); } // copy schema for (int i=0;i < TableObj->Schema.GetSize();i++) newTableObj->Schema.SetAtGrow( i ,TableObj->Schema.GetAt( i )); // copy body CCell* cell; pos = TableObj->Body.GetStartPosition(); while (pos != NULL) { CString RowCol; TableObj->Body.GetNextAssoc( pos, RowCol, cell ); CCell* newcell= new CCell; newcell->Attribute = cell->Attribute; newcell->Operator = cell->Operator; newcell->Value = cell->Value; newTableObj->Body.SetAt(RowCol,newcell); } m_Doc->m_TableList.SetAt(str1,newTableObj); m_TableListCtrl.AddString(str1); m_TableListCtrl.SelectString(0,str1); OnSelchangeTableList(); } void CTableDlg::OnAddRow() //add row from menu { m_FxTable.SetRows(m_FxTable.GetRows()+1); m_FxTableResult.SetRows(m_FxTableResult.GetRows()+1); } void CTableDlg::OnDeleteRow() //delete row from menu { int Col,Row,DeletedCell; CString RowCol , RowStr , ColStr; Row = m_FxTable.GetRow(); TableObj->ResultList.RemoveKey (Row); CCell* cell; OperationVlaueDlg Temp; POSITION pos = TableObj->Body.GetStartPosition(); while (pos != NULL) { TableObj->Body.GetNextAssoc( pos, RowCol, cell ); RowStr = Temp.GetCpt(RowCol,'-'); ColStr = Temp.GetProp(RowCol,'-'); // convert string to integer DeletedCell = atoi((LPCTSTR)RowStr); Col = atoi((LPCTSTR)ColStr); if( DeletedCell == Row ) { TableObj->Body.RemoveKey(RowCol); } } //////////////////////////////////////////// ShitRow(); //////////////////////////////////////////// //////////////////////////////////////////// m_FxTable.RemoveItem( Row); m_FxTableResult.RemoveItem(Row); TableObj->MaxRows --; //////////////////////////////////////////// /*int sel = m_TableListCtrl.GetCurSel(); m_TableListCtrl.GetLBText(sel,ColStr); m_Doc->m_TableList.SetAt(ColStr,TableObj);*/ //????????? }