]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - tools/src/tools/configtool/common/win32/CdlTemplatesDialog.cpp
Initial revision
[karo-tx-redboot.git] / tools / src / tools / configtool / common / win32 / CdlTemplatesDialog.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 // CdlTemplatesDialog.cpp : implementation file
26 //
27
28 #include "stdafx.h"
29 #include "Configtool.h"
30 #include "ConfigtoolDoc.h"
31 #include "CTUtils.h"
32 #include "CdlTemplatesDialog.h"
33
34 #ifdef _DEBUG
35 #define new DEBUG_NEW
36 #undef THIS_FILE
37 static char THIS_FILE[] = __FILE__;
38 #endif
39
40 /////////////////////////////////////////////////////////////////////////////
41 // CCdlTemplatesDialog dialog
42
43
44 CCdlTemplatesDialog::CCdlTemplatesDialog(CWnd* pParent /*=NULL*/)
45         : CeCosDialog(IDD, pParent)
46 {
47         //{{AFX_DATA_INIT(CCdlTemplatesDialog)
48         m_strCdlTemplateDescription = _T("");
49         m_strCdlTemplatePackages = _T("");
50         //}}AFX_DATA_INIT
51 }
52
53
54 void CCdlTemplatesDialog::DoDataExchange(CDataExchange* pDX)
55 {
56         CeCosDialog::DoDataExchange(pDX);
57         //{{AFX_DATA_MAP(CCdlTemplatesDialog)
58         DDX_Control(pDX, IDC_CDL_TEMPLATE_PACKAGES, m_edtCdlTemplatePackages);
59         DDX_Control(pDX, IDC_CDL_TEMPLATE, m_cboCdlTemplate);
60         DDX_Control(pDX, IDC_CDL_TEMPLATE_VER, m_cboCdlTemplateVersion);
61         DDX_Control(pDX, IDC_CDL_HARDWARE, m_cboCdlHardware);
62         DDX_Text(pDX, IDC_CDL_HARDWARE_DESC, m_strCdlHardwareDescription);
63         DDX_Text(pDX, IDC_CDL_TEMPLATE_DESC, m_strCdlTemplateDescription);
64         DDX_Text(pDX, IDC_CDL_TEMPLATE_PACKAGES, m_strCdlTemplatePackages);
65         //}}AFX_DATA_MAP
66 }
67
68
69 BEGIN_MESSAGE_MAP(CCdlTemplatesDialog, CeCosDialog)
70         //{{AFX_MSG_MAP(CCdlTemplatesDialog)
71         ON_CBN_SELCHANGE(IDC_CDL_HARDWARE, OnSelchangeCdlHardware)
72         ON_BN_CLICKED(IDC_DETAILS, OnDetails)
73         ON_WM_CANCELMODE()
74         ON_CBN_SELCHANGE(IDC_CDL_TEMPLATE, OnSelchangeCdlTemplate)
75         ON_CBN_SELCHANGE(IDC_CDL_TEMPLATE_VER, OnSelchangeCdlTemplateVersion)
76         //}}AFX_MSG_MAP
77 END_MESSAGE_MAP()
78
79 /////////////////////////////////////////////////////////////////////////////
80 // CCdlTemplatesDialog message handlers
81
82 BOOL CCdlTemplatesDialog::OnInitDialog() 
83 {
84         CeCosDialog::OnInitDialog();
85
86         ShowDetails (false); // hide the details initially
87
88         CConfigToolDoc * pDoc = CConfigTool::GetConfigToolDoc ();
89
90         // populate the hardware combo box
91         m_hardware = pDoc->GetCdlConfig ()->get_hardware ();
92         const std::vector<std::string> & targets = pDoc->GetCdlPkgData ()->get_targets ();
93         std::vector<std::string>::const_iterator target_i;
94         for (target_i = targets.begin (); target_i != targets.end (); target_i++)
95         {
96                 const std::vector<std::string> & aliases = pDoc->GetCdlPkgData ()->get_target_aliases (* target_i);
97
98                 // use the first alias (if any) as the description
99                 CString strTargetDescription = aliases.size () ? aliases [0].c_str () : target_i->c_str ();
100                 int nIndex = m_cboCdlHardware.AddString (strTargetDescription);
101                 m_cboCdlHardware.SetItemData (nIndex, (DWORD) target_i); // store the target iterator
102                 if (m_hardware == * target_i)            // if current target...
103                         m_cboCdlHardware.SetCurSel (nIndex); // ...select the string
104         }
105
106         if (CB_ERR == m_cboCdlHardware.GetCurSel ()) // if no target selected...
107                 m_cboCdlHardware.SetCurSel (0);          // ...select the first one
108
109         // populate the template combo box
110         m_template = pDoc->GetCdlConfig ()->get_template ();
111         const std::vector<std::string> & templates = pDoc->GetCdlPkgData ()->get_templates ();
112         std::vector<std::string>::const_iterator template_i;
113         for (template_i = templates.begin (); template_i != templates.end (); template_i++)
114         {
115                 CString strTemplateDescription = template_i->c_str ();
116                 int nIndex = m_cboCdlTemplate.AddString (strTemplateDescription);
117                 m_cboCdlTemplate.SetItemData (nIndex, (DWORD) template_i); // store the template iterator
118                 if (m_template == * template_i)          // if current template...
119                         m_cboCdlTemplate.SetCurSel (nIndex); // ...select the string
120         }
121
122         if (CB_ERR == m_cboCdlTemplate.GetCurSel ()) // if no template selected...
123                 m_cboCdlTemplate.SetCurSel (0);          // ...select the first one
124
125         // display initial target and template descriptions
126         OnSelchangeCdlHardware ();
127         OnSelchangeCdlTemplate ();
128
129         // populate the template version combo box
130         UpdateVersionList (pDoc->GetTemplateVersion ());
131
132         return TRUE;  // return TRUE unless you set the focus to a control
133                       // EXCEPTION: OCX Property Pages should return FALSE
134 }
135
136 void CCdlTemplatesDialog::OnSelchangeCdlHardware() 
137 {
138         CConfigToolDoc * pDoc = CConfigTool::GetConfigToolDoc ();
139
140         // the target has changed so retrieve the new target description
141         const int nIndex = m_cboCdlHardware.GetCurSel ();
142         m_hardware = * (std::vector<std::string>::const_iterator) m_cboCdlHardware.GetItemData (nIndex);
143         m_strCdlHardwareDescription = pDoc->GetCdlPkgData ()->get_target_description (m_hardware).c_str ();
144         m_strCdlHardwareDescription = CUtils::StripExtraWhitespace (m_strCdlHardwareDescription);
145
146     UpdateDetails (); // display new hardware packages in details box
147     UpdateData (FALSE); // display new target description
148 }
149
150 void CCdlTemplatesDialog::OnSelchangeCdlTemplate() 
151 {
152         // the template has changed so update the version combo box
153         int nIndex = m_cboCdlTemplate.GetCurSel ();
154         m_template = * (std::vector<std::string>::const_iterator) m_cboCdlTemplate.GetItemData (nIndex);
155         UpdateVersionList (""); // repopulate template versions combo box and select most recent version
156 }
157
158 void CCdlTemplatesDialog::UpdateVersionList(std::string default_version)
159 {
160   // clear the version combo box
161   m_cboCdlTemplateVersion.ResetContent ();
162
163   // get the template version information
164   CConfigToolDoc * pDoc = CConfigTool::GetConfigToolDoc ();
165   const std::vector<std::string>& versions = pDoc->GetCdlPkgData ()->get_template_versions (m_template);
166   ASSERT (versions.size () > 0);
167
168   // add the template versions to the version combo box
169   for (unsigned int version = 0; version < versions.size (); version++) {
170     TRACE (_T("Adding version '%s'\n"), CString (versions [version].c_str ()));
171     m_cboCdlTemplateVersion.AddString (CString (versions [version].c_str ()));
172   }
173
174   // select the appropriate version in the version combo box
175   if ("" == default_version) { // if no default version specified
176     m_cboCdlTemplateVersion.SetCurSel (versions.size () - 1); // select the most recent version
177   } else { // a default version was specified
178     m_cboCdlTemplateVersion.SelectString (-1, CString (default_version.c_str ()));
179   }
180   OnSelchangeCdlTemplateVersion ();
181
182   // enable the version combo box only if there are multiple versions
183   m_cboCdlTemplateVersion.EnableWindow (versions.size () > 1);
184 }
185
186 void CCdlTemplatesDialog::UpdateDetails()
187 {
188   // retrieve the template and target package names
189   CConfigToolDoc * pDoc = CConfigTool::GetConfigToolDoc ();
190   const std::vector<std::string> & template_packages = pDoc->GetCdlPkgData ()->get_template_packages (m_template, m_template_version);
191   std::vector<std::string> packages = pDoc->GetCdlPkgData ()->get_target_packages (m_hardware);
192   packages.insert (packages.end (), template_packages.begin (), template_packages.end ());
193
194   // retrieve the zeroth (verbose) package alias for each package
195   std::vector<std::string> aliases;
196   for (unsigned int i = 0; i < packages.size (); i++)
197   {
198     if (pDoc->GetCdlPkgData ()->is_known_package (packages [i])) // if the package is installed
199     {
200       aliases.push_back (pDoc->GetCdlPkgData ()->get_package_aliases (packages [i]) [0]);
201     }
202     else // package is not installed
203     {
204       aliases.push_back ("Unknown package " + packages [i]);
205     }
206   }
207
208   // sort the aliases into alphabetical order
209   std::sort (aliases.begin (), aliases.end ());
210
211   // copy the aliases into the details box
212   m_strCdlTemplatePackages = _T("");
213   for (i = 0; i < aliases.size (); i++)
214   {
215     m_strCdlTemplatePackages += aliases [i].c_str ();
216     m_strCdlTemplatePackages += _T("\r\n"); // add a CRLF between each alias
217   }
218   m_strCdlTemplatePackages.TrimRight (); // remove the trailing CRLF
219 }
220
221 void CCdlTemplatesDialog::OnSelchangeCdlTemplateVersion()
222 {
223   CString strVersion;
224   CConfigToolDoc * pDoc = CConfigTool::GetConfigToolDoc ();
225   m_cboCdlTemplateVersion.GetLBText (m_cboCdlTemplateVersion.GetCurSel (), strVersion);
226   TRACE (_T("Version '%s' selected\n"), strVersion);
227   m_template_version = CUtils::UnicodeToStdStr (strVersion);
228   m_strCdlTemplateDescription = pDoc->GetCdlPkgData ()->get_template_description (m_template, m_template_version).c_str ();
229   m_strCdlTemplateDescription = CUtils::StripExtraWhitespace (m_strCdlTemplateDescription);
230
231   UpdateDetails (); // display new template packages in details box
232   UpdateData (FALSE); // display new template description
233 }
234
235 void CCdlTemplatesDialog::OnDetails() 
236 {
237     ShowDetails (! m_edtCdlTemplatePackages.IsWindowVisible ());
238 }
239
240 void CCdlTemplatesDialog::ShowDetails(bool bShow)
241 {
242     // show or hide the windows
243     m_edtCdlTemplatePackages.ShowWindow (bShow ? SW_SHOW : SW_HIDE);
244     GetDlgItem (IDC_CDL_TEMPLATE_PACKAGES_STATIC)->ShowWindow (bShow ? SW_SHOW : SW_HIDE);
245     GetDlgItem (IDC_DETAILS)->SetWindowText (bShow ? _T("&Details <<") : _T("&Details >>"));
246     
247     // resize the dialog box
248     CRect rect1,rect2;
249     GetDlgItem (IDC_DETAILS)->GetWindowRect (rect1);
250     m_edtCdlTemplatePackages.GetWindowRect (rect2);
251     int delta = rect2.bottom - rect1.bottom;
252     CRect rcDlg;
253     GetWindowRect (rcDlg);
254     rcDlg.bottom += bShow ? delta : - delta;
255     MoveWindow (rcDlg);
256 }