1 //####COPYRIGHTBEGIN####
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
6 // This program is part of the eCos host tools.
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)
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
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.
22 // ----------------------------------------------------------------------------
24 //####COPYRIGHTEND####
25 // FailingRulesDialog.cpp : implementation file
32 #include "FailingRulesDialog.h"
33 #include "ConfigItem.h"
34 #include "ConfigTool.h"
35 #include "ConfigToolDoc.h"
36 #include "ControlView.h"
42 static char THIS_FILE[] = __FILE__;
45 /////////////////////////////////////////////////////////////////////////////
46 // CFailingRulesDialog dialog
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)
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;
62 m_Map.SetAt(*conf_i,pInfo);
65 //{{AFX_DATA_INIT(CFailingRulesDialog)
66 // NOTE: the ClassWizard will add member initialization here
70 CFailingRulesDialog::~CFailingRulesDialog()
72 for(POSITION pos = m_Map.GetStartPosition(); pos != NULL; ){
74 SolutionInfo *pInfo=NULL;
75 m_Map.GetNextAssoc(pos, (void *&)conflict, (void *&)pInfo);
80 void CFailingRulesDialog::DoDataExchange(CDataExchange* pDX)
82 CeCosDialog::DoDataExchange(pDX);
83 //{{AFX_DATA_MAP(CFailingRulesDialog)
84 DDX_Control(pDX, IDC_LIST2, m_List);
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)
99 /////////////////////////////////////////////////////////////////////////////
100 // CFailingRulesDialog message handlers
102 BOOL CFailingRulesDialog::OnInitDialog()
104 CeCosDialog::OnInitDialog();
105 m_List.SetExtendedStyle(LVS_EX_CHECKBOXES);
106 m_List.InsertColumn(0,_T("Item"));
107 m_List.InsertColumn(1,_T("Value"));
110 GetDlgItem(IDC_LIST1)->GetWindowRect(rect);
111 ScreenToClient(rect);
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);
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);
131 for(int i=m_RulesList.GetItemCount()-1;i>=0;--i){
132 m_RulesList.SetItemState(i,LVIS_SELECTED, LVIS_SELECTED);
136 return false; // return TRUE unless you set the focus to a control
137 // EXCEPTION: OCX Property Pages should return FALSE
140 void CFailingRulesDialog::OnCancel()
142 CeCosDialog::OnCancel();
145 void CFailingRulesDialog::OnOK()
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));
153 // Dismiss the window
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);
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());
173 case CdlValueFlavor_None :
174 str=_T("set CdlValueFlavor_None");
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);
181 case CdlValueFlavor_BoolData :
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);
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);
202 CConfigTool::GetConfigToolDoc()->SetModifiedFlag();
204 CUtils::MessageBoxF(_T("Failed to %s\n"),str);
211 void CFailingRulesDialog::SetAll(bool bOnOff)
213 for(int i=m_List.GetItemCount()-1;i>=0;--i){
214 m_List.SetCheck(i,bOnOff);
218 void CFailingRulesDialog::OnReset()
223 void CFailingRulesDialog::OnConflictsNone()
228 BOOL CFailingRulesDialog::OnItemChanged(UINT, LPNMLISTVIEW pnmv, LRESULT* pResult)
230 bool bWasSelected=(pnmv->uOldState & LVIS_SELECTED);
231 bool bIsSelected =(pnmv->uNewState & LVIS_SELECTED);
233 if(bWasSelected != bIsSelected) {
234 CdlConflict conflict=(CdlConflict) m_RulesList.GetItemData(pnmv->iItem);
236 if(1==m_List.GetSelectedCount()){
237 GetDlgItem(IDC_STATIC1)->ShowWindow(SW_HIDE);
238 m_List.ShowWindow(SW_SHOW);
240 AddConflictSolutions(conflict);
242 RemoveConflictSolutions(conflict);
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)
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);
262 return false; // not handled
265 void CFailingRulesDialog::RemoveConflictSolutions(CdlConflict conflict)
267 SolutionInfo &info=Info(conflict);
268 for(int i=0;i<info.nCount;i++){
269 int nItem=info.arItem[i];
271 info.arItem[i]=(1==m_List.GetCheck(nItem)?SolutionInfo::CHECKED:SolutionInfo::UNCHECKED);
272 int nRefs=m_List.GetItemData(nItem);
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){
284 m_List.SetItemData(nItem,nRefs-1);
289 void CFailingRulesDialog::AddConflictSolutions(CdlConflict conflict)
291 SolutionInfo &info=Info(conflict);
293 const std::vector<std::pair<CdlValuable, CdlValue> >&Solution=conflict->get_solution();
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();
304 case CdlValueFlavor_None :
306 case CdlValueFlavor_Bool :
307 strValue=value.is_enabled() ? _T("Enabled") : _T("Disabled");
309 case CdlValueFlavor_BoolData :
310 strValue.Format(_T("%s, %s"), value.is_enabled() ? _T("Enabled") : _T("Disabled"), CString(value.get_value().c_str()));
312 case CdlValueFlavor_Data :
313 strValue=value.get_value().c_str();
317 const CString strName(soln_i->first->get_name().c_str());
319 fi.flags=LVFI_STRING;
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]);
331 // We do - to avoid duplicates, increment the "ref count"
332 m_List.SetItemData(nIndex,m_List.GetItemData(nIndex)+1);
334 info.arItem[i++]=nIndex;
337 SetDlgItemText(IDC_STATIC1,_T("No solution is available for this conflict"));
338 m_List.ShowWindow(SW_HIDE);
340 SetDlgItemText(IDC_STATIC1,_T("Proposed solution:"));
341 m_List.ShowWindow(SW_SHOW);
342 m_List.SetColumnWidth(0,LVSCW_AUTOSIZE);
344 m_List.GetClientRect(rect);
345 m_List.SetColumnWidth(1,rect.Width()-m_List.GetColumnWidth(0));
349 BOOL CFailingRulesDialog::OnSolutionItemChanged(UINT,LPNMLISTVIEW, LRESULT*)
352 return false; // not handled
355 CFailingRulesDialog::SolutionInfo & CFailingRulesDialog::Info(const CdlConflict conflict)
358 VERIFY(m_Map.Lookup(conflict,(void *&)pInfo));
362 BOOL CFailingRulesDialog::OnRClick(UINT, LPNMITEMACTIVATE pnmv, LRESULT* pResult)
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);
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"));
374 SuppressNextContextMenuMessage();
376 menu.TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON, pt.x,pt.y,this);
380 return TRUE; // handled
383 void CFailingRulesDialog::OnLocate()
385 CConfigItem *pItem=m_RulesList.AssociatedItem(m_nContextItem,m_nContextRow);
387 CConfigTool::GetControlView()->SelectItem(pItem);
392 void CFailingRulesDialog::SetButtons()
395 int nItemCount=m_List.GetItemCount();
396 for(int i=nItemCount-1;i>=0;--i){
397 nCheckCount+=m_List.GetCheck(i);
399 GetDlgItem(IDC_RESET)->EnableWindow(nItemCount>0 && nCheckCount<nItemCount);
400 GetDlgItem(IDC_CONFLICTS_NONE)->EnableWindow(nItemCount>0 && nCheckCount>0);
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 )
407 LPNMHDR pHdr=(LPNMHDR)lParam;
409 switch(pHdr->idFrom){
411 switch (pHdr->code) {
412 case LVN_ITEMCHANGED:
413 bHandled=OnItemChanged(wParam, (LPNMLISTVIEW)lParam, pResult);
416 bHandled=OnClick(wParam, (LPNMLISTVIEW)lParam, pResult);
419 bHandled=OnRClick(wParam, (LPNMITEMACTIVATE)lParam, pResult);
426 switch (pHdr->code) {
427 case LVN_ITEMCHANGED:
428 bHandled=OnSolutionItemChanged(wParam,(LPNMLISTVIEW)lParam, pResult);
435 return bHandled || CeCosDialog::OnNotify(wParam,lParam,pResult);