FLTK 1.3.0
Fl_Table_Row.H
00001 //
00002 // "$Id$"
00003 //
00004 
00005 #ifndef _FL_TABLE_ROW_H
00006 #define _FL_TABLE_ROW_H
00007 
00008 //
00009 // Fl_Table_Row -- A row oriented table widget
00010 //
00011 //    A class specializing in a table of rows.
00012 //    Handles row-specific selection behavior.
00013 //
00014 // Copyright 2002 by Greg Ercolano.
00015 //
00016 // This library is free software; you can redistribute it and/or
00017 // modify it under the terms of the GNU Library General Public
00018 // License as published by the Free Software Foundation; either
00019 // version 2 of the License, or (at your option) any later version.
00020 //
00021 // This library is distributed in the hope that it will be useful,
00022 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00023 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00024 // Library General Public License for more details.
00025 //
00026 // You should have received a copy of the GNU Library General Public
00027 // License along with this library; if not, write to the Free Software
00028 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00029 // USA.
00030 //
00031 // Please report all bugs and problems to "erco at seriss dot com".
00032 //
00033 //
00034 
00035 #include "Fl_Table.H"
00036 
00054 class FL_EXPORT Fl_Table_Row : public Fl_Table {
00055 public:
00056   enum TableRowSelectMode {
00057     SELECT_NONE,                // no selection allowed
00058     SELECT_SINGLE,              // single row selection
00059     SELECT_MULTI                // multiple row selection (default)
00060   }; 
00061 private:
00062   // An STL-ish vector without templates
00063   class FL_EXPORT CharVector {
00064     char *arr;
00065     int _size;
00066     void init() {
00067       arr = NULL;
00068       _size = 0;
00069     }
00070     void copy(char *newarr, int newsize) {
00071       size(newsize);
00072       memcpy(arr, newarr, newsize * sizeof(char));
00073     }
00074   public:
00075     CharVector() {                              // CTOR
00076       init();
00077     }
00078     ~CharVector() {                             // DTOR
00079       if ( arr ) free(arr);
00080       arr = NULL;
00081     }
00082     CharVector(CharVector&o) {                  // COPY CTOR
00083       init();
00084       copy(o.arr, o._size);
00085     }
00086     CharVector& operator=(CharVector&o) {       // ASSIGN
00087       init();
00088       copy(o.arr, o._size);
00089       return(*this);
00090     }
00091     char operator[](int x) const {
00092       return(arr[x]);
00093     }
00094     char& operator[](int x) {
00095       return(arr[x]);
00096     }
00097     int size() {
00098       return(_size);
00099     }
00100     void size(int count) {
00101       if ( count != _size ) {
00102         arr = (char*)realloc(arr, count * sizeof(char));
00103         _size = count;
00104       }
00105     }
00106     char pop_back() {
00107       char tmp = arr[_size-1];
00108       _size--;
00109       return(tmp);
00110     }
00111     void push_back(char val) {
00112       int x = _size;
00113       size(_size+1);
00114       arr[x] = val;
00115     }
00116     char back() {
00117       return(arr[_size-1]);
00118     }
00119   };
00120   CharVector _rowselect;                // selection flag for each row
00121   
00122   // handle() state variables.
00123   //    Put here instead of local statics in handle(), so more
00124   //    than one instance can exist without crosstalk between.
00125   //
00126   int _dragging_select;         // dragging out a selection?
00127   int _last_row;
00128   int _last_y;                  // last event's Y position
00129   int _last_push_x;             // last PUSH event's X position
00130   int _last_push_y;             // last PUSH event's Y position
00131   
00132   TableRowSelectMode _selectmode;
00133   
00134 protected:
00135   int handle(int event);
00136   int find_cell(TableContext context,           // find cell's x/y/w/h given r/c
00137                 int R, int C, int &X, int &Y, int &W, int &H) {
00138     return(Fl_Table::find_cell(context, R, C, X, Y, W, H));
00139   }
00140   
00141 public:
00147   Fl_Table_Row(int X, int Y, int W, int H, const char *l=0) : Fl_Table(X,Y,W,H,l) {
00148     _dragging_select = 0;
00149     _last_row        = -1;
00150     _last_y          = -1;
00151     _last_push_x     = -1;
00152     _last_push_y     = -1;
00153     _selectmode      = SELECT_MULTI;
00154   }
00155   
00160   ~Fl_Table_Row() { }
00161   
00162   void rows(int val);                   // set number of rows
00163   int rows() {                          // get number of rows
00164     return(Fl_Table::rows());
00165   }
00166   
00174   void type(TableRowSelectMode val);    // set selection mode
00175   
00176   TableRowSelectMode type() const {     // get selection mode
00177     return(_selectmode);
00178   }
00179   
00185   int row_selected(int row);            // is row selected? (0=no, 1=yes, -1=range err)
00186   
00191   int select_row(int row, int flag=1);  // select state for row: flag:0=off, 1=on, 2=toggle
00192   // returns: 0=no change, 1=changed, -1=range err
00193   
00198   void select_all_rows(int flag=1);     // all rows to a known state
00199   
00200   void clear() {
00201     rows(0);            // implies clearing selection
00202     cols(0);
00203     Fl_Table::clear();  // clear the table
00204   }
00205 };
00206 
00207 #endif /*_FL_TABLE_ROW_H*/
00208 
00209 //
00210 // End of "$Id$".
00211 //