Linux multi-monitor fullscreen support
[ryzomcore.git] / nel / tools / logic / logic_editor_dll / ConditionPage.cpp
blobe7a4b15671307a1c2d6f1116ed35dc9d61dd80cd
1 // ConditionPage.cpp : implementation file
2 //
4 #include "stdafx.h"
5 #include "logic_editor.h"
6 #include "ConditionPage.h"
7 #include "Condition.h"
9 #include "MainFrm.h"
10 #include "ChildFrm.h"
11 #include "logic_editorDoc.h"
12 #include "EditorFormView.h"
16 #ifdef _DEBUG
17 #define new DEBUG_NEW
18 #undef THIS_FILE
19 static char THIS_FILE[] = __FILE__;
20 #endif
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("<");
32 m_sVarName = _T("");
33 m_sSubCondName = _T("");
34 m_sConditionName = _T("");
35 m_dComparand = 0.0;
36 //}}AFX_DATA_INIT
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);
54 //}}AFX_DATA_MAP
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)
67 //}}AFX_MSG_MAP
68 END_MESSAGE_MAP()
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());
81 ASSERT_VALID(pDoc);
83 // list of variables
84 CComboBox *comboBox = static_cast<CComboBox *> (GetDlgItem(IDC_COMBO_NODE_VAR_NAME) );
86 comboBox->ResetContent();
88 POSITION pos = pDoc->m_variables.GetHeadPosition();
90 while (pos != NULL)
92 comboBox->AddString( pDoc->m_variables.GetNext(pos) );
95 // counters are variables too
96 pos = pDoc->m_counters.GetStartPosition();
97 void *pointer;
98 CString name;
100 while (pos != NULL)
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();
112 while (pos != NULL)
114 pDoc->m_conditions.GetNextAssoc( pos, name, pointer);
115 comboBox->AddString( name );
118 /////
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:
130 m_sType = "NOT";
131 break;
132 case CConditionNode::TERMINATOR:
133 m_sType = "Terminator";
134 break;
135 case CConditionNode::SUB_CONDITION:
136 m_sType = "Sub-condition";
137 break;
138 case CConditionNode::COMPARISON:
139 m_sType = "Comparison";
140 break;
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;
149 UpdateData(FALSE);
150 if (tempStr != m_sType)
151 OnSelchangeComboNodeType();
158 BOOL CConditionPage::checkNodeValidity()
160 UpdateData();
162 if ( m_sType == "NOT" )
164 // always valid
165 return TRUE;
167 else if ( m_sType == "Terminator" )
169 // always valid
170 return TRUE;
172 else if ( m_sType == "Sub-condition" )
174 if ( m_sConditionName.IsEmpty() )
175 return FALSE;
177 else if ( m_sType == "Comparison" )
179 if ( m_sOperator.IsEmpty() || m_sVarName.IsEmpty() )
180 return FALSE;
183 return TRUE;
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);
214 Update();
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()
222 UpdateData();
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") )
256 else // terminator
260 // UpdateData(FALSE);
265 //--------------------------------------------------------
266 // addCondition
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..."));
276 return;
279 // add the condition
280 pDoc->m_conditions.SetAt( condition->m_sName, condition );
282 // update Views
283 pDoc->UpdateAllViews( (CView*)this->GetParent() );
285 } // addCondition //
289 //--------------------------------------------------------
290 // OnButtonAddCondition
292 //--------------------------------------------------------
293 void CConditionPage::OnButtonAddCondition()
295 UpdateData();
297 if (m_sConditionName.IsEmpty())
299 AfxMessageBox(_T("Condition Name cannot be empty, please choose a name"));
300 return;
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();
308 // get the document
309 CLogic_editorDoc *pDoc = static_cast<CLogic_editorDoc *> (pChild->GetActiveDocument());
310 ASSERT_VALID(pDoc);
312 // create condition
313 CCondition * pCondition = new CCondition();
314 pCondition->m_sName = m_sConditionName;
316 // add the condition
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"));
330 return;
334 if (m_pSelectedCondition == NULL)
335 return;
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 );
364 else
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());
373 ASSERT_VALID(pDoc);
374 // update views
375 pDoc->UpdateAllViews( (CView*)this->GetParent() );
380 //---------------------------------------------------------
381 // OnSetActive
383 //---------------------------------------------------------
384 BOOL CConditionPage::OnSetActive()
386 // get the child frame
387 CMainFrame *pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
388 CChildFrame *pChild = (CChildFrame *) pFrame->MDIGetActive();
390 // get the form view
391 CEditorFormView *pFormView = static_cast<CEditorFormView *> ( pChild->m_wndSplitter.GetPane(0,1) );
392 ASSERT_VALID(pFormView);
394 // get the document
395 CLogic_editorDoc * pDoc = (CLogic_editorDoc*)pFormView->GetDocument();
398 if( pDoc->InitConditionPage )
400 // init the conditions
401 POSITION pos;
402 CString eltName;
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;
413 Update();
415 return CPropertyPage::OnSetActive();
417 } // OnSetActive //
421 void CConditionPage::OnButtonCondApply()
423 if (m_pSelectedCondition != NULL)
425 UpdateData();
427 if (m_sConditionName.IsEmpty())
429 AfxMessageBox(_T("Condition Name cannot be empty, please choose a name"));
430 return;
433 CMainFrame *pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
434 CChildFrame *pChild = (CChildFrame *) pFrame->GetActiveFrame();
435 CLogic_editorDoc *pDoc = static_cast<CLogic_editorDoc *> (pChild->GetActiveDocument());
436 ASSERT_VALID(pDoc);
438 pDoc->changeConditionName( m_pSelectedCondition->m_sName, m_sConditionName );
440 Update();
441 pDoc->UpdateAllViews( (CView*)this->GetParent() );
443 else
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());
457 ASSERT_VALID(pDoc);
459 pDoc->deleteCondition(m_pSelectedCondition->m_sName );
461 m_pSelectedCondition = NULL;
462 m_pSelectedConditionNode = NULL;
464 Update();
465 pDoc->UpdateAllViews( (CView*)this->GetParent() );
467 else
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"));
481 return;
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());
514 ASSERT_VALID(pDoc);
516 pDoc->UpdateAllViews( (CView*)this->GetParent() );
518 else
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 );
534 else
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;
545 Update();
547 CMainFrame *pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
548 CChildFrame *pChild = (CChildFrame *) pFrame->GetActiveFrame();
549 CLogic_editorDoc *pDoc = static_cast<CLogic_editorDoc *> (pChild->GetActiveDocument());
550 ASSERT_VALID(pDoc);
552 pDoc->UpdateAllViews( (CView*)this->GetParent() );
554 else
556 AfxMessageBox(_T("No condition node selected ! Choose a node first"));