MechSys  1.0
Computing library for simulations in continuum and discrete mechanics
/home/dorival/mechsys/lib/gui/wxarrayint.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_WXARRAYINT_H
00020 #define MECHSYS_WXARRAYINT_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 WxArrayIntTable : public wxGridTableBase, public Array<int>
00037 {
00038 public:
00039     // Constructor
00040     WxArrayIntTable () : Transposed(false) {}
00041 
00042     // Derived Methods
00043     int      GetNumberRows () { return (Transposed ? 1 : Size()); }
00044     int      GetNumberCols () { return (Transposed ? Size() : 1); }
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     // Methods
00050     void DeleteCols (wxArrayInt const & ColsToDelete);
00051     void DeleteRows (wxArrayInt const & RowsToDelete);
00052     void Set        (Array<int> const & R);
00053 
00054     // Data
00055     bool Transposed; 
00056 };
00057 
00058 class WxArrayInt : public wxWindow
00059 {
00060 public:
00061     // Constructor
00062     WxArrayInt (wxWindow * Parent, WxArrayIntTable * tab=NULL);
00063 
00064     // Methods
00065     void ReBuild (bool ReadControls=true); 
00066 
00067     // Data
00068     WxArrayIntTable * Tab;    
00069     wxGrid          * Grd;    
00070     bool              FitCol; 
00071     bool              Transp; 
00072 
00073     // Events
00074     void OnReBuild (wxCommandEvent & Event) { ReBuild (); }
00075     void OnAdd     (wxCommandEvent & Event);
00076     void OnDel     (wxCommandEvent & Event);
00077     DECLARE_EVENT_TABLE()
00078 };
00079 
00080 
00082 
00083 
00084 inline wxString WxArrayIntTable::GetValue (int Row, int Col)
00085 {
00086     int row = Row;
00087     int col = Col;
00088     if (Transposed) { row=Col; col=Row; }
00089     wxString buf;
00090     buf.Printf ("%d", (*this)[row]);
00091     return buf;
00092 }
00093 
00094 inline void WxArrayIntTable::SetValue (int Row, int Col, wxString const & Str)
00095 {
00096     int row = Row;
00097     int col = Col;
00098     if (Transposed) { row=Col; col=Row; }
00099     long val;
00100     Str.ToLong (&val);
00101     (*this)[row] = val;
00102 }
00103 
00104 inline void WxArrayIntTable::DeleteCols (wxArrayInt const & ColsToDelete)
00105 {
00106     if (!Transposed) return;
00107     Array<int> idxs_to_delete(ColsToDelete.size());
00108     for (size_t i=0; i<ColsToDelete.size(); ++i) idxs_to_delete[i] = ColsToDelete[i];
00109     DelItems (idxs_to_delete);
00110 }
00111 
00112 inline void WxArrayIntTable::DeleteRows (wxArrayInt const & RowsToDelete)
00113 {
00114     if (Transposed) return;
00115     Array<int> idxs_to_delete(RowsToDelete.size());
00116     for (size_t i=0; i<RowsToDelete.size(); ++i) idxs_to_delete[i] = RowsToDelete[i];
00117     DelItems (idxs_to_delete);
00118 }
00119 
00120 inline void WxArrayIntTable::Set (Array<int> const & R)
00121 {
00122     Resize (R.Size());
00123     for (size_t i=0; i<R.Size(); ++i) (*this)[i] = R[i];
00124 }
00125 
00126 
00128 
00129 
00130 enum
00131 {
00132     ID_WXARRAYINT_FITCOL = wxID_HIGHEST+3000,
00133     ID_WXARRAYINT_TRANSP,
00134     ID_WXARRAYINT_ADD,
00135     ID_WXARRAYINT_DEL,
00136 };
00137 
00138 BEGIN_EVENT_TABLE (WxArrayInt, wxWindow)
00139     EVT_CHECKBOX (ID_WXARRAYINT_FITCOL, WxArrayInt::OnReBuild)
00140     EVT_CHECKBOX (ID_WXARRAYINT_TRANSP, WxArrayInt::OnReBuild)
00141     EVT_BUTTON   (ID_WXARRAYINT_ADD,    WxArrayInt::OnAdd)
00142     EVT_BUTTON   (ID_WXARRAYINT_DEL,    WxArrayInt::OnDel)
00143 END_EVENT_TABLE ()
00144 
00145 WxArrayInt::WxArrayInt (wxWindow * Parent, WxArrayIntTable * tab)
00146     : wxWindow (Parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE),
00147       FitCol   (false),
00148       Transp   (false)
00149 {
00150     // force validation of child controls
00151     SetExtraStyle (wxWS_EX_VALIDATE_RECURSIVELY);
00152 
00153     // controls
00154     wxBoxSizer * szt = new wxBoxSizer(wxVERTICAL);
00155     wxBoxSizer * szr = new wxBoxSizer(wxHORIZONTAL);
00156     SetSizer          (szt);
00157     szt->Fit          (this);                                           
00158     szt->SetSizeHints (this);
00159     ADD_WXCHECKBOX    (this, szr, ID_WXARRAYINT_TRANSP, c0, "Transposed",          Transp);
00160     ADD_WXCHECKBOX    (this, szr, ID_WXARRAYINT_FITCOL, c1, "Fit columns to data", FitCol);
00161     ADD_WXBUTTON      (this, szr, ID_WXARRAYINT_ADD,    c2, "Add"                        );
00162     ADD_WXBUTTON      (this, szr, ID_WXARRAYINT_DEL,    c3, "Del"                        );
00163     szt->Add          (szr);
00164 
00165     // create table and grid
00166     Tab = (tab==NULL ? new GUI::WxArrayIntTable() : tab);
00167     Grd = new wxGrid (this, wxID_ANY);
00168     szt->Add (Grd, 0,wxALIGN_LEFT|wxALL|wxEXPAND,2);
00169     Transp = Tab->Transposed;
00170     ReBuild (false);
00171 }
00172 
00173 void WxArrayInt::ReBuild (bool ReadControls)
00174 {
00175     // synchronise data from/to controls
00176     if (ReadControls) TransferDataFromWindow ();
00177     else              TransferDataToWindow   ();
00178 
00179     // rebuild grid based on updated table
00180     Tab->Transposed = Transp;
00181     Grd->SetTable     (Tab, /*take_ownership*/false);
00182     Grd->ForceRefresh ();
00183 
00184     // reshape window
00185     if (FitCol) Grd->Fit ();
00186     Layout ();
00187 }
00188 
00189 inline void WxArrayInt::OnAdd (wxCommandEvent & Event)
00190 {
00191     Tab->Push (0);
00192     ReBuild   ();
00193 }
00194 
00195 inline void WxArrayInt::OnDel (wxCommandEvent & Event)
00196 {
00197     wxArrayInt sel = (Tab->Transposed ? Grd->GetSelectedCols() : Grd->GetSelectedRows());
00198     if (Tab->Transposed) Tab->DeleteCols (sel);
00199     else                 Tab->DeleteRows (sel);
00200     ReBuild ();
00201 }
00202 
00203 }; // namespace GUI
00204 
00205 #endif // MECHSYS_WXARRAYINT_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines