]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - tools/src/tools/configtool/common/win32/FailingRulesDialog.cpp
Initial revision
[karo-tx-redboot.git] / tools / src / tools / configtool / common / win32 / FailingRulesDialog.cpp
1 //####COPYRIGHTBEGIN####
2 //                                                                          
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
5 //
6 // This program is part of the eCos host tools.
7 //
8 // This program is free software; you can redistribute it and/or modify it 
9 // under the terms of the GNU General Public License as published by the Free 
10 // Software Foundation; either version 2 of the License, or (at your option) 
11 // any later version.
12 // 
13 // This program is distributed in the hope that it will be useful, but WITHOUT 
14 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
15 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
16 // more details.
17 // 
18 // You should have received a copy of the GNU General Public License along with
19 // this program; if not, write to the Free Software Foundation, Inc., 
20 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 //
22 // ----------------------------------------------------------------------------
23 //                                                                          
24 //####COPYRIGHTEND####
25 // FailingRulesDialog.cpp : implementation file
26 //
27
28 #include "stdafx.h"
29 #ifndef PLUGIN
30 #include "BCMenu.h"
31 #endif
32 #include "FailingRulesDialog.h"
33 #include "ConfigItem.h"
34 #include "ConfigTool.h"
35 #include "ConfigToolDoc.h"
36 #include "ControlView.h"
37 #include "CTUtils.h"
38
39 #ifdef _DEBUG
40 #define new DEBUG_NEW
41 #undef THIS_FILE
42 static char THIS_FILE[] = __FILE__;
43 #endif
44
45 /////////////////////////////////////////////////////////////////////////////
46 // CFailingRulesDialog dialog
47
48
49 CFailingRulesDialog::CFailingRulesDialog(std::list<CdlConflict> conflicts,CdlTransaction transaction/*=NULL*/,CPtrArray *parConflictsOfInterest/*=NULL*/)
50         : CeCosDialog(CFailingRulesDialog::IDD, NULL),
51         m_conflicts(conflicts),
52   m_Transaction(transaction),
53   m_parConflictsOfInterest(parConflictsOfInterest)
54 {
55   for (std::list<CdlConflict>::const_iterator conf_i= m_conflicts.begin (); conf_i != m_conflicts.end (); conf_i++) { // for each conflict
56     int nSolutions=(*conf_i)->get_solution().size();
57     SolutionInfo *pInfo=(SolutionInfo *)malloc(sizeof(SolutionInfo)+(nSolutions-1)*sizeof(int));
58     pInfo->nCount=nSolutions;
59     for(int i=0;i<nSolutions;i++){
60       pInfo->arItem[i]=SolutionInfo::CHECKED;
61     }
62     m_Map.SetAt(*conf_i,pInfo);
63   }
64
65         //{{AFX_DATA_INIT(CFailingRulesDialog)
66                 // NOTE: the ClassWizard will add member initialization here
67         //}}AFX_DATA_INIT
68 }
69
70 CFailingRulesDialog::~CFailingRulesDialog()
71 {
72   for(POSITION pos = m_Map.GetStartPosition(); pos != NULL; ){
73     CdlConflict conflict;
74     SolutionInfo *pInfo=NULL;
75     m_Map.GetNextAssoc(pos, (void *&)conflict, (void *&)pInfo);
76     free(pInfo);
77   }
78 }
79
80 void CFailingRulesDialog::DoDataExchange(CDataExchange* pDX)
81 {
82         CeCosDialog::DoDataExchange(pDX);
83         //{{AFX_DATA_MAP(CFailingRulesDialog)
84         DDX_Control(pDX, IDC_LIST2, m_List);
85         //}}AFX_DATA_MAP
86 }
87
88
89 BEGIN_MESSAGE_MAP(CFailingRulesDialog, CeCosDialog)
90         //{{AFX_MSG_MAP(CFailingRulesDialog)
91         ON_BN_CLICKED(IDC_RESET, OnReset)
92         ON_BN_CLICKED(IDC_CONFLICTS_NONE, OnConflictsNone)
93         ON_BN_CLICKED(ID_RESOLVE_CONFLICTS_CONTINUE, OnOK)
94   ON_BN_CLICKED(ID_RESOLVE_CONFLICTS_CANCEL,OnCancel)
95   ON_COMMAND(ID_LOCATE,OnLocate)
96         //}}AFX_MSG_MAP
97 END_MESSAGE_MAP()
98
99 /////////////////////////////////////////////////////////////////////////////
100 // CFailingRulesDialog message handlers
101
102 BOOL CFailingRulesDialog::OnInitDialog() 
103 {
104   CeCosDialog::OnInitDialog();
105   m_List.SetExtendedStyle(LVS_EX_CHECKBOXES);
106   m_List.InsertColumn(0,_T("Item"));
107   m_List.InsertColumn(1,_T("Value"));
108   
109   CRect rect;
110   GetDlgItem(IDC_LIST1)->GetWindowRect(rect);
111   ScreenToClient(rect);
112
113   m_RulesList.CreateEx(WS_EX_CLIENTEDGE,WC_LISTVIEW,NULL,WS_VISIBLE|WS_CHILD|LVS_SHOWSELALWAYS,rect,this,IDC_LIST1);
114   // Select the first item and fill the solution set
115   m_RulesList.AddConflicts(m_conflicts);
116
117   if(m_parConflictsOfInterest && m_parConflictsOfInterest->GetSize()>0){  
118     CPtrArray &arConflictsOfInterest=*m_parConflictsOfInterest;
119     for(int i=m_RulesList.GetItemCount()-1;i>=0;--i){
120       for(int j=arConflictsOfInterest.GetSize()-1;j>=0;--j){
121         CdlConflict conflict=(CdlConflict)m_RulesList.GetItemData(i);
122         if(arConflictsOfInterest[j]==conflict){
123           m_RulesList.SetItemState(i,LVIS_SELECTED, LVIS_SELECTED);
124           m_RulesList.EnsureVisible(i,false);
125           arConflictsOfInterest.RemoveAt(j);
126           break;
127         }
128       }
129     }
130   } else {
131     for(int i=m_RulesList.GetItemCount()-1;i>=0;--i){
132       m_RulesList.SetItemState(i,LVIS_SELECTED, LVIS_SELECTED);
133     }
134   }
135   SetButtons();
136         return false;  // return TRUE unless you set the focus to a control
137                       // EXCEPTION: OCX Property Pages should return FALSE
138 }
139
140 void CFailingRulesDialog::OnCancel() 
141 {
142   CeCosDialog::OnCancel();
143 }
144
145 void CFailingRulesDialog::OnOK() 
146 {
147   // Ensure we have the current conflict check array
148   for(POSITION pos = m_RulesList.GetFirstSelectedItemPosition();pos;){
149     int nItem = m_RulesList.GetNextSelectedItem(pos);
150     RemoveConflictSolutions((CdlConflict)m_RulesList.GetItemData(nItem));
151   }
152
153   // Dismiss the window
154   CeCosDialog::OnOK();
155
156   for (std::list<CdlConflict>::const_iterator conf_i= m_conflicts.begin (); conf_i != m_conflicts.end (); conf_i++) { // for each conflict
157     CdlConflict conflict=*conf_i;
158     //int nSolutions=conflict->get_solution().size();
159     SolutionInfo &info=Info(conflict);
160     int nIndex=0;
161     const std::vector<std::pair<CdlValuable, CdlValue> >&Solution=conflict->get_solution();
162     for (std::vector<std::pair<CdlValuable, CdlValue> >::const_iterator soln_i = Solution.begin();soln_i != Solution.end(); soln_i++) {
163       if(SolutionInfo::CHECKED==info.arItem[nIndex++]){
164         CdlValuable valuable  = soln_i->first;
165         CdlValue value=soln_i->second;
166         CdlValueFlavor flavor = valuable->get_flavor();
167         const CString strName(valuable->get_name().c_str());
168         const CString strValue(value.get_value().c_str());
169         bool rc=true;
170         CString str;
171         try {
172           switch(flavor) {
173             case CdlValueFlavor_None :
174               str=_T("set CdlValueFlavor_None");
175               rc=false;
176               break;
177             case CdlValueFlavor_Bool :
178               str.Format(_T("%s %s\n"),value.is_enabled()?_T("disable"):_T("enable"),strName);
179               valuable->set_enabled (m_Transaction, value.is_enabled(), CdlValueSource_User);
180               break;
181             case CdlValueFlavor_BoolData :
182               {
183                 bool bEnabled=value.is_enabled();
184                 str.Format(_T("%s %s and set value to %s\n"),bEnabled?_T("disable"):_T("enable"),strName,strValue);
185                 // This is wrong: it should set the NEW value. This is the cause of a long-standing bug...
186                 // CdlSimpleValue simple_value = valuable->get_simple_value ();
187                 //valuable->set_enabled_and_value (m_Transaction, bEnabled, simple_value, CdlValueSource_User);
188                 valuable->set_enabled_and_value (m_Transaction, bEnabled, CUtils::UnicodeToStdStr (strValue), CdlValueSource_User);
189
190               }
191               break;
192             case CdlValueFlavor_Data :
193               str.Format(_T("set %s to %s\n"),strName,strValue);
194               valuable->set_value (m_Transaction, CUtils::UnicodeToStdStr (strValue), CdlValueSource_User);
195               break;
196           }
197         }
198         catch(...){
199           rc=false;
200         }
201         if(rc){
202           CConfigTool::GetConfigToolDoc()->SetModifiedFlag();
203         } else {
204           CUtils::MessageBoxF(_T("Failed to %s\n"),str);
205         }
206       }
207     }
208   }
209 }
210
211 void CFailingRulesDialog::SetAll(bool bOnOff)
212 {
213   for(int i=m_List.GetItemCount()-1;i>=0;--i){
214     m_List.SetCheck(i,bOnOff);
215   }
216 }
217
218 void CFailingRulesDialog::OnReset()
219 {
220   SetAll(true);
221 }
222
223 void CFailingRulesDialog::OnConflictsNone() 
224 {
225   SetAll(false);
226 }
227
228 BOOL CFailingRulesDialog::OnItemChanged(UINT, LPNMLISTVIEW pnmv, LRESULT* pResult) 
229 {
230   bool bWasSelected=(pnmv->uOldState & LVIS_SELECTED);
231   bool bIsSelected =(pnmv->uNewState & LVIS_SELECTED);
232   
233   if(bWasSelected != bIsSelected) {
234     CdlConflict conflict=(CdlConflict) m_RulesList.GetItemData(pnmv->iItem);
235     if(bIsSelected){
236       if(1==m_List.GetSelectedCount()){
237         GetDlgItem(IDC_STATIC1)->ShowWindow(SW_HIDE);
238         m_List.ShowWindow(SW_SHOW);
239       }
240       AddConflictSolutions(conflict);
241     } else {
242       RemoveConflictSolutions(conflict);
243     }
244   }
245   *pResult = 0;
246   return false;
247 }
248
249 // We need to use this because the OnItemChanged handler successive receives "not selected" followed by "selected"
250 // notifications.  The result is that the "Select one or more conflicts to display available solutions" message
251 // would be seen briefly when clicking from one selection to another.
252 BOOL CFailingRulesDialog::OnClick(UINT,LPNMLISTVIEW pnmv, LRESULT* pResult) 
253 {
254   if(-1==pnmv->iItem && 0==m_List.GetSelectedCount()){
255     SetDlgItemText(IDC_STATIC1,_T("Select one or more conflicts to display available solutions"));
256     m_List.ShowWindow(SW_HIDE);
257     GetDlgItem(IDC_STATIC1)->ShowWindow(SW_SHOW);
258     GetDlgItem(IDC_RESET)->EnableWindow(false);
259     GetDlgItem(IDC_CONFLICTS_NONE)->EnableWindow(false);
260   }
261   *pResult = 0;
262   return false; // not handled
263 }
264
265 void CFailingRulesDialog::RemoveConflictSolutions(CdlConflict conflict)
266 {
267   SolutionInfo &info=Info(conflict);
268   for(int i=0;i<info.nCount;i++){
269     int nItem=info.arItem[i];
270     ASSERT(nItem>=0);
271     info.arItem[i]=(1==m_List.GetCheck(nItem)?SolutionInfo::CHECKED:SolutionInfo::UNCHECKED);
272     int nRefs=m_List.GetItemData(nItem);
273     if(1==nRefs){
274       m_List.DeleteItem(nItem);
275       for(int i=0;i<m_RulesList.GetItemCount();i++){
276         SolutionInfo &info=Info((CdlConflict)m_RulesList.GetItemData(i));
277         for(int j=0;j<info.nCount;j++){
278           if(info.arItem[j]>nItem){
279             info.arItem[j]--;
280           }
281         }
282       }
283     } else {
284       m_List.SetItemData(nItem,nRefs-1);
285     }
286   }
287 }
288
289 void CFailingRulesDialog::AddConflictSolutions(CdlConflict conflict)
290 {
291   SolutionInfo &info=Info(conflict);
292
293   const std::vector<std::pair<CdlValuable, CdlValue> >&Solution=conflict->get_solution();
294
295   int i=0;
296   for (std::vector<std::pair<CdlValuable, CdlValue> >::const_iterator soln_i = Solution.begin();
297        soln_i != Solution.end(); soln_i++) {
298     CdlValuable valuable  = soln_i->first;
299     CdlValue value=soln_i->second;
300     CdlValueFlavor flavor = valuable->get_flavor();
301
302     CString strValue;
303     switch(flavor) {
304       case CdlValueFlavor_None :
305         break;
306       case CdlValueFlavor_Bool :
307         strValue=value.is_enabled() ? _T("Enabled") : _T("Disabled");
308         break;
309       case CdlValueFlavor_BoolData :
310         strValue.Format(_T("%s, %s"), value.is_enabled() ? _T("Enabled") : _T("Disabled"), CString(value.get_value().c_str()));
311         break;
312       case CdlValueFlavor_Data :
313         strValue=value.get_value().c_str();
314         break;
315     }
316     
317     const CString strName(soln_i->first->get_name().c_str());
318     LVFINDINFO fi;
319     fi.flags=LVFI_STRING;
320     fi.psz=strName;
321     int nIndex=m_List.FindItem(&fi);
322     if(-1==nIndex || strValue!=m_List.GetItemText(nIndex,1)){
323       // We don't have an existing solution that matches this one
324       nIndex=m_List.GetItemCount();
325       m_List.InsertItem(nIndex,strName);
326       m_List.SetItemData(nIndex,1);
327       m_List.SetItemText(nIndex,1,strValue);
328       ASSERT(info.arItem[i]<0);
329       m_List.SetCheck(nIndex,SolutionInfo::CHECKED==info.arItem[i]);
330     } else {
331       // We do - to avoid duplicates, increment the "ref count"
332       m_List.SetItemData(nIndex,m_List.GetItemData(nIndex)+1);
333     }
334     info.arItem[i++]=nIndex; 
335   }
336   if(0==i){
337     SetDlgItemText(IDC_STATIC1,_T("No solution is available for this conflict"));
338     m_List.ShowWindow(SW_HIDE);
339   } else {
340     SetDlgItemText(IDC_STATIC1,_T("Proposed solution:"));
341     m_List.ShowWindow(SW_SHOW);
342     m_List.SetColumnWidth(0,LVSCW_AUTOSIZE);
343     CRect rect;
344     m_List.GetClientRect(rect);
345     m_List.SetColumnWidth(1,rect.Width()-m_List.GetColumnWidth(0));
346   }
347 }
348
349 BOOL CFailingRulesDialog::OnSolutionItemChanged(UINT,LPNMLISTVIEW, LRESULT*) 
350 {
351   SetButtons();
352   return false; // not handled
353 }
354
355 CFailingRulesDialog::SolutionInfo & CFailingRulesDialog::Info(const CdlConflict conflict)
356 {
357   SolutionInfo *pInfo;   
358   VERIFY(m_Map.Lookup(conflict,(void *&)pInfo));
359   return *pInfo;
360 }
361
362 BOOL CFailingRulesDialog::OnRClick(UINT, LPNMITEMACTIVATE pnmv, LRESULT* pResult) 
363 {
364   DWORD dwPos=GetMessagePos();
365   CPoint pt(GET_X_LPARAM(dwPos),GET_Y_LPARAM(dwPos));
366   m_nContextItem=pnmv->iItem;
367   m_nContextRow=pnmv->iSubItem;
368   if(-1!=m_nContextItem){
369     //m_RulesList.SetItemState(m_nContextItem,LVIS_SELECTED,LVIS_SELECTED);
370         Menu menu;
371     menu.CreatePopupMenu();
372     menu.AppendMenu(1==m_RulesList.GetSelectedCount() && m_RulesList.AssociatedItem(m_nContextItem,m_nContextRow)?MF_STRING:(MF_STRING|MF_GRAYED),ID_LOCATE,_T("&Locate"));
373 #ifndef PLUGIN
374     SuppressNextContextMenuMessage();
375 #endif
376     menu.TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON, pt.x,pt.y,this);
377   }
378
379   *pResult = 0;
380   return TRUE; // handled
381 }
382
383 void CFailingRulesDialog::OnLocate()
384 {
385   CConfigItem *pItem=m_RulesList.AssociatedItem(m_nContextItem,m_nContextRow);
386   if (pItem) {
387     CConfigTool::GetControlView()->SelectItem(pItem);
388   }
389 }
390
391
392 void CFailingRulesDialog::SetButtons()
393 {
394   int nCheckCount=0;
395   int nItemCount=m_List.GetItemCount();
396   for(int i=nItemCount-1;i>=0;--i){
397     nCheckCount+=m_List.GetCheck(i);
398   }
399   GetDlgItem(IDC_RESET)->EnableWindow(nItemCount>0 && nCheckCount<nItemCount);
400   GetDlgItem(IDC_CONFLICTS_NONE)->EnableWindow(nItemCount>0 && nCheckCount>0);
401 }
402
403 // We have to dispatch our own notify messages because the multiple inheritance of CCSHDialog prevents
404 // the message map from compiling properly for ON_NOTIFY messages.
405 BOOL CFailingRulesDialog::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult )
406 {
407   LPNMHDR pHdr=(LPNMHDR)lParam;
408   bool bHandled=false;
409   switch(pHdr->idFrom){
410     case IDC_LIST1:
411       switch (pHdr->code) {
412         case LVN_ITEMCHANGED: 
413           bHandled=OnItemChanged(wParam, (LPNMLISTVIEW)lParam, pResult);
414           break;
415         case NM_CLICK: 
416           bHandled=OnClick(wParam, (LPNMLISTVIEW)lParam, pResult);
417           break;
418         case NM_RCLICK: 
419           bHandled=OnRClick(wParam, (LPNMITEMACTIVATE)lParam, pResult);
420           break;
421         default:
422           break;
423       }
424       break;
425     case IDC_LIST2:
426       switch (pHdr->code) {
427         case LVN_ITEMCHANGED: 
428           bHandled=OnSolutionItemChanged(wParam,(LPNMLISTVIEW)lParam, pResult);
429           break;
430         default:
431           break;
432       }
433       break;
434   }
435   return bHandled || CeCosDialog::OnNotify(wParam,lParam,pResult);
436 }