proppanel_dependencies.cpp

00001 
00002 // Name:        proppanel_dependencies.cpp
00003 // Purpose:     wxPackagePropertiesPanel
00004 //              (stuff for handling the "depedencies" page)
00005 // Author:      Francesco Montorsi
00006 // Modified by:
00007 // Created:     28/06/2006 19:22:47
00008 // RCS-ID:      $Id: proppanel_dependencies.cpp,v 1.10 2007/01/29 23:26:51 frm Exp $
00009 // Copyright:   (c) 2006 Julian Smart, Francesco Montorsi
00010 // Licence:     wxWidgets license
00012 
00013 
00014 // For compilers that support precompilation, includes "wx/wx.h".
00015 #include "wx/wxprec.h"
00016 
00017 #ifdef __BORLANDC__
00018 #pragma hdrstop
00019 #endif
00020 
00021 #ifndef WX_PRECOMP
00022 #include "wx/wx.h"
00023 #endif
00024 
00025 #include <wx/splitter.h>
00026 
00027 #include "guipkg/proppanel.h"
00028 #include "guipkg/depeditdlg.h"
00029 
00030 #include "wxp/package.h"
00031 #include "wxp/wxp.h"
00032 #include "wxp/packagewxp.h"
00033 
00034 #include "wx/miscutils.h"
00035 
00036 
00037 
00038 // ----------------------------------------------------------------------------
00039 // Global helpers
00040 // ----------------------------------------------------------------------------
00041 
00042 // some globals wxListCtrl helpers
00043 
00044 wxPackageDependency &wxGetPackageDependencyFromListctrl(wxListCtrl *lc, unsigned int idx)
00045 { return *((wxPackageDependency *)lc->GetItemData(idx)); }
00046 
00047 void wxUpdatePackageDependencyInListctrl(wxListCtrl *lc, unsigned int idx,
00048                                          const wxPackageDependency &dep)
00049 { wxGetPackageDependencyFromListctrl(lc, idx) = dep; }
00050 
00051 void wxDeletePackageDependencyFromListctrl(wxListCtrl *lc, int idx)
00052 {
00053     if (idx == -1)
00054     {
00055         while (lc->GetItemCount() > 0)
00056             wxDeletePackageDependencyFromListctrl(lc, 0);
00057     }
00058     else
00059     {
00060         // don't leak memory!
00061         delete ((wxPackageDependency*)lc->GetItemData(idx));
00062         lc->DeleteItem(idx);
00063     }
00064 }
00065 
00066 bool IsSelected(wxListCtrl *lc, int i)
00067 {
00068     if (lc->GetItemState(i, wxLIST_STATE_SELECTED) & wxLIST_STATE_SELECTED)
00069         return true;
00070     return false;
00071 }
00072 
00073 int GetSelection(wxListCtrl *lc)
00074 {
00075     for (size_t i=0; i<(size_t)lc->GetItemCount(); i++)
00076         if (IsSelected(lc, i))
00077             return i;
00078     return wxNOT_FOUND;
00079 }
00080 
00081 
00082 
00083 // ----------------------------------------------------------------------------
00084 // wxPackagePropertiesPanel - dependencies stuff - I/O
00085 // ----------------------------------------------------------------------------
00086 
00087 void wxPackagePropertiesPanel::InitDependenciesPage()
00088 {
00089     m_pDepList->InsertColumn(0, wxT("Package name"), wxLIST_FORMAT_LEFT, 150);
00090     m_pDepList->InsertColumn(1, wxT("Version range"), wxLIST_FORMAT_LEFT, 150);
00091     m_pDepList->InsertColumn(2, wxT("Type"), wxLIST_FORMAT_LEFT, 150);
00092 
00093     // init supported/tested checklistboxes with a default version range
00094     for (size_t i=0; i<wxPORT_MAX; i++)
00095     {
00096         wxString name = wxPlatformInfo::GetPortIdName((wxPortId)(1 << i), false);
00097         m_pSupportedPorts->Append(name);
00098         m_pTestedPorts->Append(name);
00099     }
00100 
00101     // we don't need to call UpdatePortVersions() because we already added
00102     // to m_pSupportedPorts and m_pTestedPorts all strings correctly
00103 }
00104 
00105 void wxPackagePropertiesPanel::GetDependenciesPackageInfo(wxPackageInfo &ret) const
00106 {
00107     // DEPENDENCIES info
00108     for (size_t i=0; i<m_pSupportedPorts->GetCount(); i++)
00109         if (m_pSupportedPorts->IsChecked(i))
00110             ret.AddSupportedPort((wxPortId)(1 << i), m_supportedVer[i]);
00111     for (size_t i=0; i<m_pTestedPorts->GetCount(); i++)
00112         if (m_pTestedPorts->IsChecked(i))
00113             ret.AddTestedPort((wxPortId)(1 << i), m_testedVer[i]);
00114 
00115     for (size_t i=0; i<(size_t)m_pDepList->GetItemCount(); i++)
00116         ret.AddDependency(wxGetPackageDependencyFromListctrl(m_pDepList, i));
00117 }
00118 
00119 void wxPackagePropertiesPanel::DoSetDependenciesPackageInfo(const wxPackageInfo &p)
00120 {
00121     // DEPENDENCIES info
00122     wxFlags2CheckListBox(m_pSupportedPorts, p.GetSupportedPortFlag(), wxPORT_MAX);
00123     wxFlags2CheckListBox(m_pTestedPorts, p.GetTestedPortFlag(), wxPORT_MAX);
00124     for (size_t i=0,j=0; i<wxPORT_MAX; i++)
00125     {
00126         if (m_pSupportedPorts->IsChecked(i))
00127             m_supportedVer[i] = p.GetSupportedVersions().Item(j++);
00128         else
00129             m_supportedVer[i].Set(wxEmptyString);
00130     }
00131     for (size_t i=0,j=0; i<wxPORT_MAX; i++)
00132     {
00133         if (m_pTestedPorts->IsChecked(i))
00134             m_testedVer[i] = p.GetTestedVersions().Item(j++);
00135         else
00136             m_testedVer[i].Set(wxEmptyString);
00137     }
00138     UpdatePortVersions();
00139 
00140     // delete all existing dependency items before adding the new ones
00141     wxDeletePackageDependencyFromListctrl(m_pDepList, -1);
00142     for (size_t i=0; i<p.GetDependencies().GetCount(); i++)
00143     {
00144         wxPackageDependency dep = p.GetDependencies().Item(i);
00145         m_pDepList->InsertItem(i, dep.GetName());
00146         m_pDepList->SetItem(i, 1, dep.GetVersion().GetAsString());
00147         m_pDepList->SetItem(i, 2, dep.GetTypeStr());
00148         m_pDepList->SetItemData(i, (long)new wxPackageDependency(dep));
00149     }
00150 }
00151 
00152 bool wxPackagePropertiesPanel::IsDependenciesPageOk() const
00153 {
00154     bool isok = true;
00155 
00156     if (wxCheckListBox2Flags(m_pSupportedPorts) == 0)
00157     {
00158         wxLogWarning(wxT("Please check at least one wxWidgets port as supported"));
00159         isok = false;
00160     }
00161 
00162     return isok;
00163 }
00164 
00165 
00166 
00167 // ----------------------------------------------------------------------------
00168 // wxPackagePropertiesPanel - dependencies stuff
00169 // ----------------------------------------------------------------------------
00170 
00171 bool wxPackagePropertiesPanel::GetSupportedOrTestedVersionRange(bool issupported,
00172                                                                 wxString *ret,
00173                                                                 const wxString &portname,
00174                                                                 const wxString &initial)
00175 {
00176     wxString newver(initial);
00177 
00178     do
00179     {
00180         newver = wxGetTextFromUser(
00181                 wxString::Format(_("Please enter the %s version range for %s."),
00182                                  issupported ? wxT("supported") : wxT("tested"),
00183                                  portname.c_str()),
00184                 _("Edit version range"),
00185                 newver,
00186                 this);
00187         if (newver.IsEmpty())
00188             return false;     // user hit cancel
00189 
00190         // is the version valid ?
00191         if (!wxVersionRange(newver).IsOk())
00192             wxLogError(wxT("Invalid version range '%s'."), newver.c_str());
00193         else
00194             break;
00195 
00196     } while (1);
00197 
00198     if (ret) *ret = newver;
00199     return true;
00200 }
00201 
00202 bool wxPackagePropertiesPanel::GetDep(wxPackageDependency *ret,
00203                                       const wxPackageDependency &initial)
00204 {
00205     wxPackageDependencyEditDlg dlg(this);
00206     dlg.SetDependency(initial);
00207     if (dlg.ShowModal() == wxID_OK)
00208     {
00209         if (ret) *ret = dlg.GetDependency();
00210         return true;
00211     }
00212 
00213     return false;
00214 }
00215 
00216 void wxPackagePropertiesPanel::UpdatePortVersions()
00217 {
00218     // save currently checked ports
00219     long suppchecks = wxCheckListBox2Flags(m_pSupportedPorts);
00220     long testedchecks = wxCheckListBox2Flags(m_pTestedPorts);
00221 
00222     for (size_t i=0; i<wxPORT_MAX; i++)
00223     {
00224         wxString name = wxPlatformInfo::GetPortIdName((wxPortId)(1 << i), false);
00225 
00226         if (m_supportedVer[i].IsOk())
00227             m_pSupportedPorts->SetString(i, name + wxT(", ") + m_supportedVer[i].GetAsString());
00228         else
00229             m_pSupportedPorts->SetString(i, name);
00230 
00231         if (m_testedVer[i].IsOk())
00232             m_pTestedPorts->SetString(i, name + wxT(", ") + m_testedVer[i].GetAsString());
00233         else
00234             m_pTestedPorts->SetString(i, name);
00235     }
00236 
00237     // restore checked ports
00238     wxFlags2CheckListBox(m_pSupportedPorts, suppchecks, wxPORT_MAX);
00239     wxFlags2CheckListBox(m_pTestedPorts, testedchecks, wxPORT_MAX);
00240 }
00241 
00242 void wxPackagePropertiesPanel::SetPortVersion(int id)
00243 {
00244     bool issupported = id == ID_PROP_SUPPORTEDPORTS ||
00245                        id == ID_PROP_QUICKSET_SUPPORTED_VERSIONS;
00246     wxCheckListBox *p = issupported ? m_pSupportedPorts : m_pTestedPorts;
00247     wxVersionRange *r = issupported ? m_supportedVer : m_testedVer;
00248 
00249     wxArrayInt sel;
00250     int n = p->GetSelections(sel);
00251     if (n == 0)
00252     {
00253         // this may happen on wxMSW's listboxes when they have > 0 items
00254         // and the user clicks on the empty space
00255         
00256         // if nothing is selected, consider all items selected:
00257         for (size_t i=0; i<p->GetCount(); i++)
00258             sel.Add(i);
00259     }
00260 
00261     wxString portname;
00262     wxString newver;
00263 
00264     // create the list of the ports where we need to set the version:
00265     if (p->GetCount() == sel.GetCount())
00266     {
00267         portname = wxT("all ports");
00268     }
00269     else
00270     {
00271         for (size_t i=0; i<sel.GetCount(); i++)
00272             portname += p->GetString(sel[i]).BeforeFirst(wxT(',')) + wxT(", ");
00273         portname.RemoveLast();
00274         portname.RemoveLast();
00275 
00276         portname = wxT("the ") + portname + 
00277                     (const wxChar*)(sel.GetCount() > 0 ? wxT("ports") : wxT("port"));
00278     }
00279 
00280     // get the initial version range to use from the first selected string
00281     wxString initialVer = r[sel[0]].GetAsString();
00282 
00283     if (GetSupportedOrTestedVersionRange(issupported, &newver, portname,
00284                                          initialVer))
00285     {
00286         wxASSERT(wxVersionRange(newver).IsOk());
00287 
00288         for (size_t i=0; i<sel.GetCount(); i++)
00289             r[sel[i]].Set(newver);
00290         UpdatePortVersions();
00291     }
00292 }
00293 
00294 
00295 
00296 // ----------------------------------------------------------------------------
00297 // wxPackagePropertiesPanel - event handlers
00298 // ----------------------------------------------------------------------------
00299 
00300 void wxPackagePropertiesPanel::OnDependenciesListBoxDClick(wxCommandEvent &ev)
00301 {
00302     OnUserChange();
00303 
00304     wxLogDebug(wxT("the top panel min size is: %d;%d\n"), 
00305                 m_pTopPanel->GetMinSize().GetWidth(),
00306                 m_pTopPanel->GetMinSize().GetHeight());
00307 
00308 
00309     switch (ev.GetId())
00310     {
00311         case ID_PROP_SUPPORTEDPORTS:
00312         case ID_PROP_TESTEDPORTS:
00313         {
00314             SetPortVersion(ev.GetId());
00315         }
00316         break;
00317     }
00318 }
00319 
00320 void wxPackagePropertiesPanel::OnDependenciesButton(wxCommandEvent &ev)
00321 {
00322     OnUserChange();
00323 
00324     switch (ev.GetId())
00325     {
00326     case ID_PROP_NEW_DEP:
00327         {
00328             wxPackageDependency dep;
00329             if (GetDep(&dep))
00330             {
00331                 int idx = m_pDepList->GetItemCount();
00332                 m_pDepList->InsertItem(idx, dep.GetName());
00333                 m_pDepList->SetItem(idx, 1, dep.GetVersion().GetAsString());
00334                 m_pDepList->SetItem(idx, 2, dep.GetTypeStr());
00335                 m_pDepList->SetItemData(idx, (long)new wxPackageDependency(dep));
00336             }
00337         }
00338         break;
00339 
00340     case ID_PROP_DELETE_DEP:
00341         wxDeletePackageDependencyFromListctrl(m_pDepList, GetSelection(m_pDepList));
00342         break;
00343 
00344     case ID_PROP_QUICKSET_SUPPORTED_VERSIONS:
00345     case ID_PROP_QUICKSET_TESTED_VERSIONS:
00346         SetPortVersion(ev.GetId());
00347         break;
00348     }
00349 }
00350 
00351 void wxPackagePropertiesPanel::OnDepDClick(wxListEvent &WXUNUSED(ev))
00352 {
00353     int idx = GetSelection(m_pDepList);
00354     wxPackageDependency dep;
00355     if (GetDep(&dep, wxGetPackageDependencyFromListctrl(m_pDepList, idx)))
00356     {
00357         m_pDepList->SetItem(idx, 0, dep.GetName());
00358         m_pDepList->SetItem(idx, 1, dep.GetVersion().GetAsString());
00359         m_pDepList->SetItem(idx, 2, dep.GetTypeStr());
00360         wxUpdatePackageDependencyInListctrl(m_pDepList, idx, dep);
00361     }
00362 
00363     OnUserChange();
00364 }
00365 
00366 void wxPackagePropertiesPanel::OnDependenciesPageUpdateUI(wxUpdateUIEvent &WXUNUSED(ev))
00367 {
00368     // controls with an interface which does not provide a wxControlWithItems::GetSelection()
00369     FindWindowById(ID_PROP_DELETE_DEP)->Enable(GetSelection(m_pDepList) != wxNOT_FOUND);
00370 }

Generated on Thu Feb 1 22:14:31 2007 for wxWidgets Package Manager by  doxygen 1.5.1-p1