1 // ConditionPage.cpp : implementation file
5 #include "logic_editor.h"
6 #include "ConditionPage.h"
11 #include "logic_editorDoc.h"
12 #include "EditorFormView.h"
19 static char THIS_FILE
[] = __FILE__
;
22 /////////////////////////////////////////////////////////////////////////////
23 // CConditionPage property page
25 IMPLEMENT_DYNCREATE(CConditionPage
, CPropertyPage
)
27 CConditionPage::CConditionPage() : CPropertyPage(CConditionPage::IDD
)
29 //{{AFX_DATA_INIT(CConditionPage)
30 m_sType
= _T("Comparison");
31 m_sOperator
= _T("<");
33 m_sSubCondName
= _T("");
34 m_sConditionName
= _T("");
39 CConditionPage::~CConditionPage()
43 void CConditionPage::DoDataExchange(CDataExchange
* pDX
)
45 CPropertyPage::DoDataExchange(pDX
);
46 //{{AFX_DATA_MAP(CConditionPage)
47 DDX_Control(pDX
, IDC_EDIT_NODE_COMPARAND
, m_ctrlComparand
);
48 DDX_CBString(pDX
, IDC_COMBO_NODE_TYPE
, m_sType
);
49 DDX_CBString(pDX
, IDC_COMBO_NODE_OPERATOR
, m_sOperator
);
50 DDX_CBString(pDX
, IDC_COMBO_NODE_VAR_NAME
, m_sVarName
);
51 DDX_CBString(pDX
, IDC_COMBO_NODE_COND_NAME
, m_sSubCondName
);
52 DDX_Text(pDX
, IDC_EDIT_CONDITION_NAME
, m_sConditionName
);
53 DDX_Text(pDX
, IDC_EDIT_NODE_COMPARAND
, m_dComparand
);
58 BEGIN_MESSAGE_MAP(CConditionPage
, CPropertyPage
)
59 //{{AFX_MSG_MAP(CConditionPage)
60 ON_CBN_SELCHANGE(IDC_COMBO_NODE_TYPE
, OnSelchangeComboNodeType
)
61 ON_BN_CLICKED(IDC_BUTTON_ADD_CONDITION
, OnButtonAddCondition
)
62 ON_BN_CLICKED(IDC_BUTTON_ADD_NODE
, OnButtonAddNode
)
63 ON_BN_CLICKED(IDC_BUTTON_COND_APPLY
, OnButtonCondApply
)
64 ON_BN_CLICKED(IDC_BUTTON_DELETE_CONDITION
, OnButtonDeleteCondition
)
65 ON_BN_CLICKED(IDC_BUTTON_NODE_APPLY
, OnButtonNodeApply
)
66 ON_BN_CLICKED(IDC_BUTTON_DELETE_NODE
, OnButtonDeleteNode
)
75 void CConditionPage::Update()
77 // get the variables existing in the doc
78 CMainFrame
*pFrame
= (CMainFrame
*)AfxGetApp()->m_pMainWnd
;
79 CChildFrame
*pChild
= (CChildFrame
*) pFrame
->GetActiveFrame();
80 CLogic_editorDoc
*pDoc
= static_cast<CLogic_editorDoc
*> (pChild
->GetActiveDocument());
84 CComboBox
*comboBox
= static_cast<CComboBox
*> (GetDlgItem(IDC_COMBO_NODE_VAR_NAME
) );
86 comboBox
->ResetContent();
88 POSITION pos
= pDoc
->m_variables
.GetHeadPosition();
92 comboBox
->AddString( pDoc
->m_variables
.GetNext(pos
) );
95 // counters are variables too
96 pos
= pDoc
->m_counters
.GetStartPosition();
102 pDoc
->m_counters
.GetNextAssoc( pos
, name
, pointer
);
103 comboBox
->AddString( name
);
106 // list of conditions
107 comboBox
= static_cast<CComboBox
*> (GetDlgItem(IDC_COMBO_NODE_COND_NAME
) );
109 comboBox
->ResetContent();
111 pos
= pDoc
->m_conditions
.GetStartPosition();
114 pDoc
->m_conditions
.GetNextAssoc( pos
, name
, pointer
);
115 comboBox
->AddString( name
);
119 if ( m_pSelectedCondition
!= NULL
)
121 m_sConditionName
= m_pSelectedCondition
->m_sName
;
123 const CString tempStr
= m_sType
;
125 if ( m_pSelectedConditionNode
!= NULL
)
127 switch( m_pSelectedConditionNode
->m_type
)
129 case CConditionNode::NOT
:
132 case CConditionNode::TERMINATOR
:
133 m_sType
= "Terminator";
135 case CConditionNode::SUB_CONDITION
:
136 m_sType
= "Sub-condition";
138 case CConditionNode::COMPARISON
:
139 m_sType
= "Comparison";
143 m_dComparand
= m_pSelectedConditionNode
->m_dComparand
;
144 m_sSubCondName
= m_pSelectedConditionNode
->m_sConditionName
;
145 m_sOperator
= m_pSelectedConditionNode
->m_sOperator
;
146 m_sVarName
= m_pSelectedConditionNode
->m_sVariableName
;
150 if (tempStr
!= m_sType
)
151 OnSelchangeComboNodeType();
158 BOOL
CConditionPage::checkNodeValidity()
162 if ( m_sType
== "NOT" )
167 else if ( m_sType
== "Terminator" )
172 else if ( m_sType
== "Sub-condition" )
174 if ( m_sConditionName
.IsEmpty() )
177 else if ( m_sType
== "Comparison" )
179 if ( m_sOperator
.IsEmpty() || m_sVarName
.IsEmpty() )
188 /////////////////////////////////////////////////////////////////////////////
189 // CConditionPage message handlers
191 BOOL
CConditionPage::OnInitDialog()
193 CPropertyPage::OnInitDialog();
195 // TODO: Add extra initialization here
197 m_pSelectedCondition
= NULL
;
198 m_pSelectedConditionNode
= NULL
;
200 //disable all optionnal controls
202 GetDlgItem(IDC_COMBO_NODE_VAR_NAME)->EnableWindow(FALSE);
203 GetDlgItem(IDC_COMBO_NODE_OPERATOR)->EnableWindow(FALSE);
204 GetDlgItem(IDC_EDIT_NODE_COMPARAND)->EnableWindow(FALSE);
205 GetDlgItem(IDC_STATIC_COMPARISON)->EnableWindow(FALSE);
206 GetDlgItem(IDC_STATIC_COMPARAND)->EnableWindow(FALSE);
207 GetDlgItem(IDC_STATIC_OPERATOR)->EnableWindow(FALSE);
208 GetDlgItem(IDC_STATIC_VAR_NAME)->EnableWindow(FALSE);
210 GetDlgItem(IDC_COMBO_NODE_COND_NAME
)->EnableWindow(FALSE
);
211 GetDlgItem(IDC_STATIC_COND_NAME
)->EnableWindow(FALSE
);
212 GetDlgItem(IDC_STATIC_SUB_COND
)->EnableWindow(FALSE
);
216 return TRUE
; // return TRUE unless you set the focus to a control
217 // EXCEPTION: OCX Property Pages should return FALSE
220 void CConditionPage::OnSelchangeComboNodeType()
224 //disable all optionnal controls
225 GetDlgItem(IDC_COMBO_NODE_VAR_NAME
)->EnableWindow(FALSE
);
226 GetDlgItem(IDC_COMBO_NODE_OPERATOR
)->EnableWindow(FALSE
);
227 GetDlgItem(IDC_EDIT_NODE_COMPARAND
)->EnableWindow(FALSE
);
228 GetDlgItem(IDC_STATIC_COMPARISON
)->EnableWindow(FALSE
);
229 GetDlgItem(IDC_STATIC_COMPARAND
)->EnableWindow(FALSE
);
230 GetDlgItem(IDC_STATIC_OPERATOR
)->EnableWindow(FALSE
);
231 GetDlgItem(IDC_STATIC_VAR_NAME
)->EnableWindow(FALSE
);
232 GetDlgItem(IDC_COMBO_NODE_COND_NAME
)->EnableWindow(FALSE
);
233 GetDlgItem(IDC_STATIC_COND_NAME
)->EnableWindow(FALSE
);
234 GetDlgItem(IDC_STATIC_SUB_COND
)->EnableWindow(FALSE
);
236 if ( m_sType
== _T("Comparison") )
238 //enable comparison related controls
239 GetDlgItem(IDC_COMBO_NODE_VAR_NAME
)->EnableWindow(TRUE
);
240 GetDlgItem(IDC_COMBO_NODE_OPERATOR
)->EnableWindow(TRUE
);
241 GetDlgItem(IDC_EDIT_NODE_COMPARAND
)->EnableWindow(TRUE
);
242 GetDlgItem(IDC_STATIC_COMPARISON
)->EnableWindow(TRUE
);
243 GetDlgItem(IDC_STATIC_COMPARAND
)->EnableWindow(TRUE
);
244 GetDlgItem(IDC_STATIC_OPERATOR
)->EnableWindow(TRUE
);
245 GetDlgItem(IDC_STATIC_VAR_NAME
)->EnableWindow(TRUE
);
247 else if ( m_sType
== _T("Sub-condition") )
249 GetDlgItem(IDC_COMBO_NODE_COND_NAME
)->EnableWindow(TRUE
);
250 GetDlgItem(IDC_STATIC_COND_NAME
)->EnableWindow(TRUE
);
251 GetDlgItem(IDC_STATIC_SUB_COND
)->EnableWindow(TRUE
);
253 /* else if ( m_sType == _T("NOT") )
260 // UpdateData(FALSE);
265 //--------------------------------------------------------
268 //--------------------------------------------------------
269 void CConditionPage::addCondition( CLogic_editorDoc
*pDoc
, CCondition
* condition
)
271 // check if a condition with the same name already exist
272 CCondition
*pCondition
;
273 if (pDoc
->m_conditions
.Lookup( condition
->m_sName
, (void*&)pCondition
))
275 AfxMessageBox(_T("A condition with this name already exist..."));
280 pDoc
->m_conditions
.SetAt( condition
->m_sName
, condition
);
283 pDoc
->UpdateAllViews( (CView
*)this->GetParent() );
289 //--------------------------------------------------------
290 // OnButtonAddCondition
292 //--------------------------------------------------------
293 void CConditionPage::OnButtonAddCondition()
297 if (m_sConditionName
.IsEmpty())
299 AfxMessageBox(_T("Condition Name cannot be empty, please choose a name"));
303 // if a counter with this name already exist, do nothing
304 CMainFrame
*pFrame
= (CMainFrame
*)AfxGetApp()->m_pMainWnd
;
305 // Get the active MDI child window.
306 CChildFrame
*pChild
= (CChildFrame
*) pFrame
->GetActiveFrame();
309 CLogic_editorDoc
*pDoc
= static_cast<CLogic_editorDoc
*> (pChild
->GetActiveDocument());
313 CCondition
* pCondition
= new CCondition();
314 pCondition
->m_sName
= m_sConditionName
;
317 addCondition( pDoc
, pCondition
);
319 } // OnButtonAddCondition //
324 void CConditionPage::OnButtonAddNode()
326 // check that data are valid
327 if ( checkNodeValidity() == FALSE
)
329 AfxMessageBox(_T("Invalid node datas - cannot add node"));
334 if (m_pSelectedCondition
== NULL
)
337 CConditionNode
*newNode
= new CConditionNode( m_pSelectedConditionNode
);
339 if (this->m_sType
== "NOT")
341 newNode
->m_type
= CConditionNode::NOT
;
343 else if (this->m_sType
== "Terminator")
345 newNode
->m_type
= CConditionNode::TERMINATOR
;
347 else if (this->m_sType
== "Sub-condition")
349 newNode
->m_type
= CConditionNode::SUB_CONDITION
;
350 newNode
->m_sConditionName
= this->m_sSubCondName
;
352 else if (this->m_sType
== "Comparison")
354 newNode
->m_type
= CConditionNode::COMPARISON
;
355 newNode
->m_dComparand
= this->m_dComparand
;
356 newNode
->m_sOperator
= this->m_sOperator
;
357 newNode
->m_sVariableName
=this->m_sVarName
;
360 if (m_pSelectedConditionNode
!= NULL
)
362 m_pSelectedConditionNode
->m_ctSubTree
.AddTail( newNode
);
366 m_pSelectedCondition
->m_ctConditionTree
.AddTail( newNode
);
370 CMainFrame
*pFrame
= (CMainFrame
*)AfxGetApp()->m_pMainWnd
;
371 CChildFrame
*pChild
= (CChildFrame
*) pFrame
->GetActiveFrame();
372 CLogic_editorDoc
*pDoc
= static_cast<CLogic_editorDoc
*> (pChild
->GetActiveDocument());
375 pDoc
->UpdateAllViews( (CView
*)this->GetParent() );
380 //---------------------------------------------------------
383 //---------------------------------------------------------
384 BOOL
CConditionPage::OnSetActive()
386 // get the child frame
387 CMainFrame
*pFrame
= (CMainFrame
*)AfxGetApp()->m_pMainWnd
;
388 CChildFrame
*pChild
= (CChildFrame
*) pFrame
->MDIGetActive();
391 CEditorFormView
*pFormView
= static_cast<CEditorFormView
*> ( pChild
->m_wndSplitter
.GetPane(0,1) );
392 ASSERT_VALID(pFormView
);
395 CLogic_editorDoc
* pDoc
= (CLogic_editorDoc
*)pFormView
->GetDocument();
398 if( pDoc->InitConditionPage )
400 // init the conditions
403 for( pos = pDoc->m_conditions.GetStartPosition(); pos != NULL; )
405 CCondition * pCondition = new CCondition();
406 pDoc->m_conditions.GetNextAssoc( pos, eltName, (void*&)pCondition );
407 addCondition( pDoc, pCondition );
410 pDoc->InitConditionPage = FALSE;
415 return CPropertyPage::OnSetActive();
421 void CConditionPage::OnButtonCondApply()
423 if (m_pSelectedCondition
!= NULL
)
427 if (m_sConditionName
.IsEmpty())
429 AfxMessageBox(_T("Condition Name cannot be empty, please choose a name"));
433 CMainFrame
*pFrame
= (CMainFrame
*)AfxGetApp()->m_pMainWnd
;
434 CChildFrame
*pChild
= (CChildFrame
*) pFrame
->GetActiveFrame();
435 CLogic_editorDoc
*pDoc
= static_cast<CLogic_editorDoc
*> (pChild
->GetActiveDocument());
438 pDoc
->changeConditionName( m_pSelectedCondition
->m_sName
, m_sConditionName
);
441 pDoc
->UpdateAllViews( (CView
*)this->GetParent() );
445 AfxMessageBox(_T("No condition selected ! Choose a condition first"));
450 void CConditionPage::OnButtonDeleteCondition()
452 if (m_pSelectedCondition
!= NULL
)
454 CMainFrame
*pFrame
= (CMainFrame
*)AfxGetApp()->m_pMainWnd
;
455 CChildFrame
*pChild
= (CChildFrame
*) pFrame
->GetActiveFrame();
456 CLogic_editorDoc
*pDoc
= static_cast<CLogic_editorDoc
*> (pChild
->GetActiveDocument());
459 pDoc
->deleteCondition(m_pSelectedCondition
->m_sName
);
461 m_pSelectedCondition
= NULL
;
462 m_pSelectedConditionNode
= NULL
;
465 pDoc
->UpdateAllViews( (CView
*)this->GetParent() );
469 AfxMessageBox(_T("No condition selected ! Choose a condition first"));
473 void CConditionPage::OnButtonNodeApply()
475 if (this->m_pSelectedConditionNode
!= NULL
)
477 // check that data are valid
478 if ( checkNodeValidity() == FALSE
)
480 AfxMessageBox(_T("Invalid node datas - cancel node modification"));
484 m_pSelectedConditionNode
->m_sConditionName
.Empty();
485 m_pSelectedConditionNode
->m_sOperator
.Empty();
486 m_pSelectedConditionNode
->m_sVariableName
.Empty();
487 m_pSelectedConditionNode
->m_dComparand
= 0;
489 if (this->m_sType
== "NOT")
491 m_pSelectedConditionNode
->m_type
= CConditionNode::NOT
;
493 if (this->m_sType
== "Terminator")
495 m_pSelectedConditionNode
->m_type
= CConditionNode::TERMINATOR
;
497 if (this->m_sType
== "Sub-condition")
499 m_pSelectedConditionNode
->m_type
= CConditionNode::SUB_CONDITION
;
500 m_pSelectedConditionNode
->m_sConditionName
= this->m_sSubCondName
;
502 if (this->m_sType
== "Comparison")
504 m_pSelectedConditionNode
->m_type
= CConditionNode::COMPARISON
;
505 m_pSelectedConditionNode
->m_dComparand
= this->m_dComparand
;
506 m_pSelectedConditionNode
->m_sOperator
= this->m_sOperator
;
507 m_pSelectedConditionNode
->m_sVariableName
=this->m_sVarName
;
511 CMainFrame
*pFrame
= (CMainFrame
*)AfxGetApp()->m_pMainWnd
;
512 CChildFrame
*pChild
= (CChildFrame
*) pFrame
->GetActiveFrame();
513 CLogic_editorDoc
*pDoc
= static_cast<CLogic_editorDoc
*> (pChild
->GetActiveDocument());
516 pDoc
->UpdateAllViews( (CView
*)this->GetParent() );
520 AfxMessageBox(_T("No condition node selected ! Choose a node first"));
524 void CConditionPage::OnButtonDeleteNode()
526 if ( m_pSelectedConditionNode
!= NULL
)
528 CConditionNode
*parentNode
= m_pSelectedConditionNode
->getParentNode();
529 if ( parentNode
!= NULL
)
531 POSITION pos
= parentNode
->m_ctSubTree
.Find( m_pSelectedConditionNode
);
532 parentNode
->m_ctSubTree
.RemoveAt( pos
);
536 ASSERT( m_pSelectedCondition
);
538 POSITION pos
= m_pSelectedCondition
->m_ctConditionTree
.Find( m_pSelectedConditionNode
);
539 m_pSelectedCondition
->m_ctConditionTree
.RemoveAt( pos
);
542 delete m_pSelectedConditionNode
;
543 m_pSelectedConditionNode
= NULL
;
547 CMainFrame
*pFrame
= (CMainFrame
*)AfxGetApp()->m_pMainWnd
;
548 CChildFrame
*pChild
= (CChildFrame
*) pFrame
->GetActiveFrame();
549 CLogic_editorDoc
*pDoc
= static_cast<CLogic_editorDoc
*> (pChild
->GetActiveDocument());
552 pDoc
->UpdateAllViews( (CView
*)this->GetParent() );
556 AfxMessageBox(_T("No condition node selected ! Choose a node first"));