MechSys  1.0
Computing library for simulations in continuum and discrete mechanics
/home/dorival/mechsys/lib/gui/wxdict.h
Go to the documentation of this file.
00001 /************************************************************************
00002  * MechSys - Open Library for Mechanical Systems                        *
00003  * Copyright (C) 2005 Dorival M. Pedroso, Raúl D. D. Farfan             *
00004  *                                                                      *
00005  * This program is free software: you can redistribute it and/or modify *
00006  * it under the terms of the GNU General Public License as published by *
00007  * the Free Software Foundation, either version 3 of the License, or    *
00008  * any later version.                                                   *
00009  *                                                                      *
00010  * This program is distributed in the hope that it will be useful,      *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         *
00013  * GNU General Public License for more details.                         *
00014  *                                                                      *
00015  * You should have received a copy of the GNU General Public License    *
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>  *
00017  ************************************************************************/
00018 
00019 #ifndef MECHSYS_WXDICT_H
00020 #define MECHSYS_WXDICT_H
00021 
00022 // wxWidgets
00023 #ifdef HAS_WXW
00024   #include <wx/grid.h>
00025   #include <mechsys/gui/common.h>
00026 #endif
00027 
00028 // MechSys
00029 #include <mechsys/util/maps.h>
00030 #include <mechsys/util/fatal.h>
00031 #include <mechsys/util/util.h>
00032 
00033 namespace GUI
00034 {
00035 
00036 class WxDictTable : public wxGridTableBase, public Dict
00037 {
00038 public:
00039     // Constructor & Destructor
00040     WxDictTable () : Val2Name(NULL), Transposed(false) {}
00041 
00042     // Derived Methods
00043     int      GetNumberRows ();
00044     int      GetNumberCols ();
00045     wxString GetValue      (int row, int col);
00046     void     SetValue      (int row, int col, wxString const & Str);
00047     bool     IsEmptyCell   (int row, int col) { return false; }
00048 
00049     // Derived formatting methods
00050     wxString GetColLabelValue (int col);
00051     wxString GetRowLabelValue (int row);
00052 
00053     // operators
00054     void operator= (Dict const & R); 
00055 
00056     // Methods
00057     void DeleteCols (wxArrayInt const & ColsToDelete);
00058     void DeleteRows (wxArrayInt const & RowsToDelete);
00059 
00060     // Data
00061     SDPair const * Val2Name;   
00062     bool           Transposed; 
00063 };
00064 
00065 class WxDict : public wxWindow
00066 {
00067 public:
00068     // Constructor & Destructor
00069      WxDict (wxWindow * Parent, WxDictTable * tab=NULL);
00070 
00071     // Methods
00072     void ReBuild (bool ReadControls=true); 
00073 
00074     // Data
00075     WxDictTable * Tab;    
00076     wxGrid      * Grd;    
00077     bool          FitCol; 
00078     bool          Transp; 
00079 
00080     // Events
00081     void OnReBuild (wxCommandEvent & Event) { ReBuild (); }
00082     DECLARE_EVENT_TABLE()
00083 };
00084 
00085 
00087 
00088 
00089 inline int WxDictTable::GetNumberRows ()
00090 {
00091     if (Transposed) return Keys.Size();
00092     size_t nrows = 0;
00093     for (Dict_t::const_iterator it=begin(); it!=end(); ++it)
00094     {
00095         if (it->second.size()>nrows) nrows = it->second.size();
00096     }
00097     return nrows;
00098 }
00099 
00100 inline int WxDictTable::GetNumberCols ()
00101 {
00102     if (!Transposed) return Keys.Size();
00103     size_t ncols = 0;
00104     for (Dict_t::const_iterator it=begin(); it!=end(); ++it)
00105     {
00106         if (it->second.size()>ncols) ncols = it->second.size();
00107     }
00108     return ncols;
00109 }
00110 
00111 inline wxString WxDictTable::GetValue (int Row, int Col)
00112 {
00113     int row = Row;
00114     int col = Col;
00115     if (Transposed) { row=Col; col=Row; }
00116     SDPair const & pair = (*this)(Keys[col]);
00117     wxString buf;
00118     if (pair.Keys.Size()>(size_t)row)
00119     {
00120         double val = pair(pair.Keys[row]);
00121         if (pair.Keys[row]=="name" && Val2Name!=NULL)
00122         {
00123             String name;
00124             Val2Name->Val2Key (val, name);
00125             buf = name;
00126         }
00127         else buf.Printf("%s: %g", pair.Keys[row].CStr(), val);
00128     }
00129     return buf;
00130 }
00131 
00132 inline void WxDictTable::SetValue (int Row, int Col, wxString const & Str)
00133 {
00134     int row = Row;
00135     int col = Col;
00136     if (Transposed) { row=Col; col=Row; }
00137     SDPair & pair = (*this)(Keys[col]);
00138     if (pair.Keys.Size()>(size_t)row)
00139     {
00140         if (pair.Keys[row]=="name" && Val2Name!=NULL) return;
00141         double val;
00142         if (Str.ToDouble(&val)) pair(pair.Keys[row]) = val;
00143     }
00144 }
00145 
00146 inline wxString WxDictTable::GetColLabelValue (int col)
00147 {
00148     if (Transposed) return wxGridTableBase::GetColLabelValue (col);
00149     if (Keys.Size()>(size_t)col)
00150     {
00151         wxString buf;  buf.Printf("%d", Keys[col]);
00152         return buf;
00153     }
00154     return wxEmptyString;
00155 }
00156 
00157 inline wxString WxDictTable::GetRowLabelValue (int row)
00158 {
00159     if (!Transposed) return wxGridTableBase::GetColLabelValue (row); // also Col => A, B, C, ...
00160     if (Keys.Size()>(size_t)row)
00161     {
00162         wxString buf;  buf.Printf("%d", Keys[row]);
00163         return buf;
00164     }
00165     return wxEmptyString;
00166 }
00167 
00168 inline void WxDictTable::operator= (Dict const & R)
00169 {
00170     clear();
00171     Keys.Resize (R.Keys.Size());
00172     for (size_t i=0; i<R.Keys.Size(); ++i)
00173     {
00174         Keys[i]          = R.Keys[i];
00175         (*this)[Keys[i]] = R(Keys[i]);
00176     }
00177 }
00178 
00179 inline void WxDictTable::DeleteCols (wxArrayInt const & ColsToDelete)
00180 {
00181     if (Transposed) return;
00182     wxArrayInt keys_to_delete(ColsToDelete.size());
00183     for (size_t i=0; i<ColsToDelete  .size(); ++i) keys_to_delete[i] = Keys[ColsToDelete[i]];
00184     for (size_t i=0; i<keys_to_delete.size(); ++i) Del(keys_to_delete[i]);
00185 }
00186 
00187 inline void WxDictTable::DeleteRows (wxArrayInt const & RowsToDelete)
00188 {
00189     if (!Transposed) return;
00190     wxArrayInt keys_to_delete(RowsToDelete.size());
00191     for (size_t i=0; i<RowsToDelete  .size(); ++i) keys_to_delete[i] = Keys[RowsToDelete[i]];
00192     for (size_t i=0; i<keys_to_delete.size(); ++i) Del(keys_to_delete[i]);
00193 }
00194 
00195 
00197 
00198 
00199 enum
00200 {
00201     ID_WXDICT_FITCOL = wxID_HIGHEST+3000,
00202     ID_WXDICT_TRANSP,
00203 };
00204 
00205 BEGIN_EVENT_TABLE (WxDict, wxWindow)
00206     EVT_CHECKBOX (ID_WXDICT_FITCOL, WxDict::OnReBuild)
00207     EVT_CHECKBOX (ID_WXDICT_TRANSP, WxDict::OnReBuild)
00208 END_EVENT_TABLE ()
00209 
00210 WxDict::WxDict (wxWindow * Parent, WxDictTable * tab)
00211     : wxWindow (Parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE),
00212       FitCol   (false),
00213       Transp   (false)
00214 {
00215     // force validation of child controls
00216     SetExtraStyle (wxWS_EX_VALIDATE_RECURSIVELY);
00217 
00218     // controls
00219     wxBoxSizer * szt = new wxBoxSizer(wxVERTICAL);
00220     wxBoxSizer * szr = new wxBoxSizer(wxHORIZONTAL);
00221     SetSizer          (szt);
00222     szt->Fit          (this);                                           
00223     szt->SetSizeHints (this);
00224     ADD_WXCHECKBOX    (this, szr, ID_WXDICT_TRANSP, c0, "Transposed",          Transp);
00225     ADD_WXCHECKBOX    (this, szr, ID_WXDICT_FITCOL, c1, "Fit columns to data", FitCol);
00226     szt->Add          (szr);
00227 
00228     // create table and grid
00229     Tab = (tab==NULL ? new GUI::WxDictTable() : tab);
00230     Grd = new wxGrid (this, wxID_ANY);
00231     szt->Add (Grd, 0,wxALIGN_LEFT|wxALL|wxEXPAND,2);
00232     Transp = Tab->Transposed;
00233     ReBuild (false);
00234 }
00235 
00236 void WxDict::ReBuild (bool ReadControls)
00237 {
00238     // synchronise data from/to controls
00239     if (ReadControls) TransferDataFromWindow ();
00240     else              TransferDataToWindow   ();
00241 
00242     // rebuild grid based on updated table
00243     Tab->Transposed = Transp;
00244     Grd->SetTable     (Tab, /*take_ownership*/false);
00245     Grd->ForceRefresh ();
00246 
00247     // reshape window
00248     if (FitCol) Grd->Fit ();
00249     Layout ();
00250 }
00251 
00252 }; // namespace GUI
00253 
00254 #endif // MECHSYS_WXDICT_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines