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 // CdlPackagesDialog.cpp : implementation file
29 #include "ConfigTool.h"
30 #include "ConfigtoolDoc.h"
31 #include "AddRemoveDialog.h"
32 #include "CdlPackagesDialog.h"
38 static char THIS_FILE[] = __FILE__;
41 /////////////////////////////////////////////////////////////////////////////
42 // CCdlPackagesDialog dialog
45 CCdlPackagesDialog::CCdlPackagesDialog(CWnd* pParent /*=NULL*/)
46 : CAddRemoveDialog(IDD_CDL_PACKAGES, pParent)
48 //{{AFX_DATA_INIT(CCdlPackagesDialog)
49 m_strPackageDescription = _T("");
54 void CCdlPackagesDialog::DoDataExchange(CDataExchange* pDX)
56 CAddRemoveDialog::DoDataExchange(pDX);
57 //{{AFX_DATA_MAP(CCdlPackagesDialog)
58 DDX_Control(pDX, IDC_PACKAGE_VER, m_cboPackageVersion);
59 DDX_Text(pDX, IDC_PACKAGE_DESC, m_strPackageDescription);
64 BEGIN_MESSAGE_MAP(CCdlPackagesDialog, CAddRemoveDialog)
65 //{{AFX_MSG_MAP(CCdlPackagesDialog)
66 ON_LBN_SELCHANGE(IDC_ADDREMOVE_LIST1, OnSelchangeList1)
67 ON_LBN_SELCHANGE(IDC_ADDREMOVE_LIST2, OnSelchangeList2)
68 ON_CBN_SELCHANGE(IDC_PACKAGE_VER, OnSelchangePackageVersion)
69 ON_BN_CLICKED(IDC_ADDREMOVE_ADD, OnAdd)
70 ON_BN_CLICKED(IDC_ADDREMOVE_REMOVE, OnRemove)
71 ON_LBN_DBLCLK(IDC_ADDREMOVE_LIST1, OnDblclkList1)
72 ON_LBN_DBLCLK(IDC_ADDREMOVE_LIST2, OnDblclkList2)
76 /////////////////////////////////////////////////////////////////////////////
77 // CCdlPackagesDialog message handlers
79 void CCdlPackagesDialog::OnSelchangeList1 ()
81 CAddRemoveDialog::OnSelchangeList1 ();
82 UpdatePackageDescription ();
84 UpdateHardwareSelectionFlag ();
87 void CCdlPackagesDialog::OnSelchangeList2 ()
89 CAddRemoveDialog::OnSelchangeList2 ();
90 UpdatePackageDescription ();
92 UpdateHardwareSelectionFlag ();
95 void CCdlPackagesDialog::UpdateVersionList ()
97 m_cboPackageVersion.ResetContent (); // clear the version combo box
99 CListBox * pListBox = NULL;
100 int nListSelCount = m_List1.GetSelCount ();
107 nListSelCount = m_List2.GetSelCount ();
112 if (pListBox) // if there are packages selected
114 std::list<std::string> common_versions;
115 bool bCommonSelectedVersion = true;
116 int nCommonVersionIndex=-1;
118 // retrieve the list box indices of the selected packages
120 int * arnIndices = new int [nListSelCount];
121 pListBox->GetSelItems (nListSelCount, arnIndices);
122 for (int nIndex = 0; nIndex < nListSelCount; nIndex++) // for each selected package
124 // retrieve the first package alias
126 CString strPackageAlias;
127 pListBox->GetText (arnIndices [nIndex], strPackageAlias);
129 // retrieve the dialog item array index for use in
130 // comparing current version strings
132 const int nVersionIndex = (int) pListBox->GetItemData (arnIndices [nIndex]);
134 // retrieve the installed version array
136 TRACE (_T("Retrieving versions for '%s'\n"), strPackageAlias);
137 CConfigToolDoc * pDoc = CConfigTool::GetConfigToolDoc ();
138 const std::vector<std::string>& versions = pDoc->GetCdlPkgData ()->get_package_versions (CUtils::UnicodeToStdStr (pDoc->GetPackageName (strPackageAlias)));
140 if (0 == nIndex) // if this is the first selected package
142 // use the version array to initialise a linked list of version
143 // strings held in common between the selected packages
144 for (unsigned int uCount = 0; uCount < versions.size (); uCount++)
146 TRACE (_T("Adding common version '%s'\n"), CString (versions [uCount].c_str ()));
147 common_versions.push_back (versions [uCount]);
149 nCommonVersionIndex = nVersionIndex; // save the item array index
151 else // this is not the first selected package
153 std::list<std::string>::iterator i_common_versions = common_versions.begin ();
154 while (i_common_versions != common_versions.end ()) // iterate through the common versions
156 if (versions.end () == std::find (versions.begin (), versions.end (), * i_common_versions)) // if the common version is not in the versions list
158 TRACE (_T("Removing common version '%s'\n"), CString (i_common_versions->c_str ()));
159 common_versions.erase (i_common_versions++); // remove the version from the common versions list
166 if (bCommonSelectedVersion) // if the selected versions of all preceding packages are identical
168 // check if the selected version of this package matches that of the preceding ones
169 bCommonSelectedVersion = (m_arstrVersions [nVersionIndex] == m_arstrVersions [nCommonVersionIndex]);
174 // add the common versions to the version combo box
176 std::list<std::string>::iterator i_common_versions;
177 for (i_common_versions = common_versions.begin (); i_common_versions != common_versions.end (); i_common_versions++)
179 TRACE (_T("Adding version '%s'\n"), CString (i_common_versions->c_str ()));
180 m_cboPackageVersion.AddString (CString (i_common_versions->c_str ()));
183 // select the common current version (if any) in the version combo box
185 if (bCommonSelectedVersion)
187 TRACE (_T("Selecting version '%s'\n"), m_arstrVersions [nCommonVersionIndex]);
188 m_cboPackageVersion.SelectString (-1, m_arstrVersions [nCommonVersionIndex]);
191 // enable the version combo box only if there are multiple common versions
193 m_cboPackageVersion.EnableWindow (common_versions.size () > 1);
195 delete [] arnIndices;
197 else // there are no packages selected
199 m_cboPackageVersion.EnableWindow (FALSE); // disable the version combo box
203 void CCdlPackagesDialog::UpdatePackageDescription ()
205 CConfigToolDoc * pDoc = CConfigTool::GetConfigToolDoc ();
207 CListBox * pListBox = NULL;
208 if (1 == m_List1.GetSelCount ())
210 else if (1 == m_List2.GetSelCount ())
216 pListBox->GetSelItems (1, &nIndex);
217 CString strPackageAlias;
218 pListBox->GetText (nIndex, strPackageAlias);
219 m_strPackageDescription = pDoc->GetCdlPkgData ()->get_package_description (CUtils::UnicodeToStdStr (pDoc->GetPackageName (strPackageAlias))).c_str ();
220 m_strPackageDescription = CUtils::StripExtraWhitespace (m_strPackageDescription);
224 m_strPackageDescription = _T("");
230 void CCdlPackagesDialog::Insert (LPCTSTR pszItem, bool bAdded, LPCTSTR pszDesc /* = NULL */, LPCTSTR pszVersion /* = NULL */)
232 TRACE (_T("CCdlPackagesDialog::Insert() adding package %s version '%s'\n"), pszItem, pszVersion);
233 CAddRemoveDialog::Insert (pszItem, bAdded, pszDesc);
234 m_arstrVersions.Add (pszVersion);
238 BOOL CCdlPackagesDialog::OnInitDialog()
240 m_arbSel=new int [GetCount ()];
241 CeCosDialog::OnInitDialog ();
243 // prepare for the measurement of listbox item widths
245 CDC * pDC = m_List1.GetDC ();
246 CFont * pOldFont = pDC->SelectObject (m_List1.GetFont ());
248 // add the items to the listboxes and measure their widths in pixels
249 for (int i = GetCount () - 1; i >= 0; --i)
251 TRACE (_T("Adding item '%s' index %d\n"), m_arstrItems [i], i);
252 CListBox & lb = m_arnItems [i] ? m_List2 : m_List1; // determine which listbox
253 lb.SetItemData (lb.AddString (m_arstrItems [i]), (DWORD) i); // add the item
254 CSize sizeText = pDC->GetTextExtent (m_arstrItems [i]); // measure the width of the item
255 nWidth = max (nWidth, sizeText.cx); // record the maximum width of items to date
258 // restore the device context following measurements
259 pDC->SelectObject (pOldFont);
261 // enable horizontal scrolling if necessary, assuming the
262 // listboxes have identical widths and accommodating a
263 // 2 pixel border at each side of each listbox
264 m_List1.SetHorizontalExtent (nWidth + 4);
265 m_List2.SetHorizontalExtent (nWidth + 4);
267 // enable listboxes only if they have any content
268 m_Add.EnableWindow (m_List1.GetSelCount () > 0);
269 m_Remove.EnableWindow (m_List2.GetSelCount () > 0);
271 return TRUE; // return TRUE unless you set the focus to a control
272 // EXCEPTION: OCX Property Pages should return FALSE
275 void CCdlPackagesDialog::OnSelchangePackageVersion()
277 if (CB_ERR == m_cboPackageVersion.GetCurSel ()) // if there is no version selection
278 return; // do nothing
280 CListBox * pListBox = NULL;
281 int nListSelCount = m_List1.GetSelCount ();
288 nListSelCount = m_List2.GetSelCount ();
295 // retrieve the list box indices of the selected packages
297 int * arnIndices = new int [nListSelCount];
298 pListBox->GetSelItems (nListSelCount, arnIndices);
300 for (int nIndex = 0; nIndex < nListSelCount; nIndex++) // for each selected package
302 // set the package version to that specified in the version combo box
303 m_cboPackageVersion.GetLBText (m_cboPackageVersion.GetCurSel (), m_arstrVersions [pListBox->GetItemData (arnIndices [nIndex])]);
304 TRACE (_T("Version '%s' selected for package %s\n"), m_arstrVersions [pListBox->GetItemData (arnIndices [nIndex])], m_arstrItems [pListBox->GetItemData (arnIndices [nIndex])]);
306 delete [] arnIndices;
309 void CCdlPackagesDialog::OnAdd()
311 if (m_bHardwarePackageSelected)
312 HardwarePackageMessageBox ();
314 CAddRemoveDialog::OnAdd ();
317 void CCdlPackagesDialog::OnRemove()
319 if (m_bHardwarePackageSelected)
320 HardwarePackageMessageBox ();
322 CAddRemoveDialog::OnRemove ();
325 void CCdlPackagesDialog::OnDblclkList1()
327 if (m_bHardwarePackageSelected)
328 HardwarePackageMessageBox ();
330 CAddRemoveDialog::OnDblclkList1 ();
333 void CCdlPackagesDialog::OnDblclkList2()
335 if (m_bHardwarePackageSelected)
336 HardwarePackageMessageBox ();
338 CAddRemoveDialog::OnDblclkList2 ();
341 CString CCdlPackagesDialog::GetVersion(LPCTSTR pszItem)
343 for (int nCount = GetCount () - 1; nCount >= 0; --nCount)
345 if (m_arstrItems [nCount] == pszItem)
347 return m_arstrVersions [nCount];
354 void CCdlPackagesDialog::UpdateHardwareSelectionFlag()
356 m_bHardwarePackageSelected = false;
358 CListBox * pListBox = NULL;
359 int nListSelCount = m_List1.GetSelCount ();
366 nListSelCount = m_List2.GetSelCount ();
371 if (pListBox) // if there are packages selected
373 CConfigToolDoc * pDoc = CConfigTool::GetConfigToolDoc ();
375 // retrieve the list box indices of the selected packages
377 int * arnIndices = new int [nListSelCount];
378 pListBox->GetSelItems (nListSelCount, arnIndices);
380 for (int nIndex = 0; nIndex < nListSelCount; nIndex++) // for each selected package
382 CString strPackageAlias;
383 pListBox->GetText (arnIndices [nIndex], strPackageAlias);
385 // check if the package is a hardware package
387 TRACE (_T("Checking '%s' for hardware status\n"), strPackageAlias);
388 if (pDoc->GetCdlPkgData ()->is_hardware_package (CUtils::UnicodeToStdStr (pDoc->GetPackageName (strPackageAlias))))
390 m_bHardwarePackageSelected = true;
395 delete [] arnIndices;
399 void CCdlPackagesDialog::HardwarePackageMessageBox()
401 AfxMessageBox (_T("Add and remove hardware packages by selecting a new hardware template."));