optlistctrl.cpp

00001 
00002 // Name:        optlistctrl.cpp
00003 // Purpose:     wxOptionListCtrl
00004 // Author:      Francesco Montorsi
00005 // Modified by:
00006 // Created:     10/7/2006
00007 // RCS-ID:      $Id: optlistctrl.cpp,v 1.1.1.1 2006/12/12 09:38:28 frm Exp $
00008 // Copyright:   (c) Francesco Montorsi
00009 // Licence:     wxWindows licence
00011 
00012 // ============================================================================
00013 // declarations
00014 // ============================================================================
00015 
00016 // ----------------------------------------------------------------------------
00017 // headers
00018 // ----------------------------------------------------------------------------
00019 
00020 #include "wx/wxprec.h"
00021 
00022 #ifdef __BORLANDC__
00023     #pragma hdrstop
00024 #endif
00025 
00026 #include "guipkg/optlistctrl.h"
00027 
00028 #ifndef WX_PRECOMP
00029     #include "wx/log.h"
00030     #include "wx/combobox.h"
00031     #include "wx/dcclient.h"
00032     #include "wx/settings.h"
00033 #endif
00034 
00035 #include "wx/config.h"
00036 #include "wxp/package.h"
00037 
00038 
00039 // the number of columns to show
00040 #define NUMCOLUMNS      3
00041 
00042 
00043 
00044 
00045 // ============================================================================
00046 // implementation
00047 // ============================================================================
00048 
00049 IMPLEMENT_DYNAMIC_CLASS( wxOptionListCtrl, wxTreeListCtrl )
00050 DEFINE_EVENT_TYPE(wxEVT_COMMAND_OPTION_DCLICK)
00051 BEGIN_EVENT_TABLE( wxOptionListCtrl, wxTreeListCtrl )
00052     EVT_TREE_ITEM_ACTIVATED( wxID_ANY, wxOptionListCtrl::OnItemActivated )
00053 END_EVENT_TABLE()
00054 
00055 // ----------------------------------------------------------------------------
00056 // wxOptionListCtrl
00057 // ----------------------------------------------------------------------------
00058 
00059 bool wxOptionListCtrl::Create(wxWindow* parent, wxWindowID id,
00060                                 const wxPoint& pos, const wxSize& size,
00061                                 long WXUNUSED(style), const wxString& name)
00062 {
00063     if (!wxTreeListCtrl::Create(parent, id, pos, size,
00064          wxTR_HAS_BUTTONS|wxTR_HIDE_ROOT|wxTR_NO_LINES|wxTR_FULL_ROW_HIGHLIGHT,
00065                                 wxDefaultValidator, name))
00066         return false;
00067 
00068     m_buildsys = wxPBST_INVALID;
00069     m_formats = wxGetFullPackageCompilerFormatList();
00070     m_stages = wxGetFullPackageBuildSystemStageList();
00071 
00072     AddColumn(wxT("Name"), 150, wxALIGN_LEFT);
00073     AddColumn(wxT("Allowed values"), 100, wxALIGN_CENTER);
00074     AddColumn(wxT("Default"), 100, wxALIGN_CENTER);
00075 
00076 #if NUMCOLUMNS >= 3
00077     AddColumn(wxT("Stages"), 100, wxALIGN_CENTER);
00078 #endif
00079 #if NUMCOLUMNS >= 4
00080     AddColumn(wxT("Formats"), 100, wxALIGN_RIGHT);
00081 #endif
00082     return true;
00083 }
00084 
00085 /*
00086 bool wxOptionEditorCtrl::Load(wxConfigBase *p, const wxString &path)
00087 {
00088     Clear();
00089 
00090     int num = 0;
00091     if (!p->Read(path + wxT("/ComboStrNum"), &num))
00092         return false;
00093 
00094     wxString str;
00095     for (int i=0; i < num; i++)
00096         if (p->Read(path + wxString::Format(wxT("/ComboStr%d"), i), &str))
00097             Append(str);
00098     if (GetCount() > 0)
00099         Select(0);      // by default select first item
00100 
00101     // load was okay only if we could read all the saved strings
00102     return GetCount() == (size_t)num;
00103 }
00104 
00105 void wxOptionEditorCtrl::Save(wxConfigBase *p, const wxString &path) const
00106 {
00107     // order is important !
00108     for (size_t i=0; i < GetCount(); i++)
00109         p->Write(path + wxString::Format(wxT("/ComboStr%d"), i),
00110                  GetString(i));
00111     p->Write(path + wxT("/ComboStrNum"), (long)GetCount());
00112 }
00113 */
00114 
00115 bool wxOptionListCtrl::TransferDataToWindow()
00116 {
00117     if (!wxTreeListCtrl::TransferDataToWindow())
00118         return false;
00119 
00120     // remove old options
00121     DeleteRoot();
00122 
00123     // add our hidden root
00124     wxTreeItemId root = AddRoot(wxT("Options"));
00125 
00126     // add options related to this stage & compiler
00127     wxArrayString alreadyadded;
00128     wxArrayTreeItemIds groupsid;
00129     wxString str;
00130     for (size_t i=0; i<m_arr.GetCount(); i++)
00131     {
00132         // NB: here we won't do the check on the platforms supported by the
00133         //     option as we want to list all options, even for platforms != current.
00134         if ((m_stages != 0 && !m_arr[i].GetCondition().IsValidForStages(m_stages)) ||
00135             (m_formats != 0 && !m_arr[i].GetCondition().IsValidForCompilers(m_formats)) ||
00136             (m_buildsys != wxPBST_INVALID && !m_arr[i].GetCondition().IsValidForBuildSys(m_buildsys)))
00137             continue;   // skip this
00138 
00139         // add option group if necessary
00140         wxTreeItemId id;
00141         if (alreadyadded.Index(m_arr[i].GetGroup()) == wxNOT_FOUND)
00142         {
00143             id = AppendItem( root, m_arr[i].GetGroup() );
00144             alreadyadded.Add( m_arr[i].GetGroup() );
00145             groupsid.Add( id );
00146         }
00147         else
00148         {
00149             int idx = alreadyadded.Index(m_arr[i].GetGroup());
00150             wxASSERT(idx != wxNOT_FOUND);
00151             id = groupsid[idx];
00152         }
00153 
00154         // option name
00155         wxTreeItemId item = AppendItem(id, m_arr[i].GetName());
00156 
00157         // allowed values
00158         str = m_arr[i].GetAllowedValues();
00159         str.Replace(wxT(","), wxT(", "));
00160         SetItemText(item, 1, str);
00161 
00162         // default value
00163         SetItemText(item, 2, m_arr[i].GetDefaultValue());
00164 
00165 #if NUMCOLUMNS >= 3
00166         // stage
00167         if (m_arr[i].GetCondition().GetStages() ==
00168                     wxGetFullPackageBuildSystemStageList())
00169             str = wxT("All");
00170         else
00171         {
00172             str = m_arr[i].GetCondition().GetStagesStr();
00173             str.Replace(wxT(","), wxT(", "));
00174         }
00175 
00176         SetItemText(item, 3, str);
00177 #endif
00178 
00179 #if NUMCOLUMNS >= 4
00180         // formats
00181         if (m_arr[i].GetCondition().GetFormats() ==
00182             wxGetFullPackageCompilerFormatList())
00183             str = wxT("All");
00184         else
00185         {
00186             str = m_arr[i].GetCondition().GetFormatsStr();
00187             str.Replace(wxT(","), wxT(", "));
00188         }
00189 
00190         SetItemText(item, 4, str);
00191 #endif
00192     }
00193 
00194     ExpandAll(GetRootItem());
00195     Refresh();
00196     return true;
00197 }
00198 
00199 bool wxOptionListCtrl::TransferDataFromWindow()
00200 {
00201     if (!wxTreeListCtrl::TransferDataFromWindow())
00202         return false;
00203 
00204     // do nothing:the user cannot edit the options shown in this control
00205     // thus m_arr does not need any update
00206 
00207     return true;
00208 }
00209 
00210 void wxOptionListCtrl::DeleteSelected()
00211 {
00212     int n = m_arr.IndexByName(GetSelectedOptName());
00213     wxASSERT(n != wxNOT_FOUND);
00214 
00215     // remove this option from our array
00216     m_arr.RemoveAt(n);
00217 
00218     // and update the control; we can't just do a
00219     // Delete(GetSelection()) because if this option is the last option
00220     // of its group, we would leave behind an empty group...
00221     TransferDataToWindow();
00222 }
00223 
00224 wxString wxOptionListCtrl::GetSelectedOptName() const
00225 {
00226     wxASSERT(GetSelection().IsOk());
00227 
00228     // if this is an option group, then it's not an option!
00229     if (GetItemParent(GetSelection()) == GetRootItem())
00230         return wxEmptyString;     // skip this
00231 
00232     // the first column always contains the name of the option
00233     return GetItemText(GetSelection(), 0);
00234 }
00235 
00236 
00237 // ----------------------------------------------------------------------------
00238 // wxOptionListCtrl - event handlers
00239 // ----------------------------------------------------------------------------
00240 
00241 void wxOptionListCtrl::OnItemActivated(wxTreeEvent &ev)
00242 {
00243     // did the user clicked on the name of an option group ?
00244     if (GetItemParent(ev.GetItem()) == GetRootItem())
00245         return;     // skip this
00246 
00247     wxCommandEvent optdclick(wxEVT_COMMAND_OPTION_DCLICK, GetId());
00248 
00249     // set in the command event's string the name of the option double clicked
00250     optdclick.SetString(GetItemText(ev.GetItem(), 0));
00251     GetEventHandler()->AddPendingEvent(optdclick);
00252 }

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