DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 1 | // |
| 2 | // "$Id: Fl_Table_Row.H 8301 2011-01-22 22:40:11Z AlbrechtS $" |
| 3 | // |
| 4 | |
| 5 | #ifndef _FL_TABLE_ROW_H |
| 6 | #define _FL_TABLE_ROW_H |
| 7 | |
| 8 | // |
| 9 | // Fl_Table_Row -- A row oriented table widget |
| 10 | // |
| 11 | // A class specializing in a table of rows. |
| 12 | // Handles row-specific selection behavior. |
| 13 | // |
| 14 | // Copyright 2002 by Greg Ercolano. |
| 15 | // |
| 16 | // This library is free software; you can redistribute it and/or |
| 17 | // modify it under the terms of the GNU Library General Public |
| 18 | // License as published by the Free Software Foundation; either |
| 19 | // version 2 of the License, or (at your option) any later version. |
| 20 | // |
| 21 | // This library is distributed in the hope that it will be useful, |
| 22 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 24 | // Library General Public License for more details. |
| 25 | // |
| 26 | // You should have received a copy of the GNU Library General Public |
| 27 | // License along with this library; if not, write to the Free Software |
| 28 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 29 | // USA. |
| 30 | // |
| 31 | // Please report all bugs and problems to "erco at seriss dot com". |
| 32 | // |
| 33 | // |
| 34 | |
| 35 | #include "Fl_Table.H" |
| 36 | |
| 37 | /** |
| 38 | A table with row selection capabilities. |
| 39 | |
| 40 | This class implements a simple table with the ability to select |
| 41 | rows. This widget is similar to an Fl_Browser with columns. Most |
| 42 | methods of importance will be found in the Fl_Table widget, such |
| 43 | as Fl_Table::rows() and Fl_Table::cols(). |
| 44 | |
| 45 | To be useful it must be subclassed and at minimum the draw_cell() |
| 46 | method must be overridden to provide the content of the cells. This widget |
| 47 | does \em not manage the cell's data content; it is up to the parent |
| 48 | class's draw_cell() method override to provide this. |
| 49 | |
| 50 | Events on the cells and/or headings generate callbacks when they are |
| 51 | clicked by the user. You control when events are generated based on |
| 52 | the values you supply for Fl_Table::when(). |
| 53 | */ |
| 54 | class FL_EXPORT Fl_Table_Row : public Fl_Table { |
| 55 | public: |
| 56 | enum TableRowSelectMode { |
| 57 | SELECT_NONE, // no selection allowed |
| 58 | SELECT_SINGLE, // single row selection |
| 59 | SELECT_MULTI // multiple row selection (default) |
| 60 | }; |
| 61 | private: |
| 62 | // An STL-ish vector without templates |
| 63 | class FL_EXPORT CharVector { |
| 64 | char *arr; |
| 65 | int _size; |
| 66 | void init() { |
| 67 | arr = NULL; |
| 68 | _size = 0; |
| 69 | } |
| 70 | void copy(char *newarr, int newsize) { |
| 71 | size(newsize); |
| 72 | memcpy(arr, newarr, newsize * sizeof(char)); |
| 73 | } |
| 74 | public: |
| 75 | CharVector() { // CTOR |
| 76 | init(); |
| 77 | } |
| 78 | ~CharVector() { // DTOR |
| 79 | if ( arr ) free(arr); |
| 80 | arr = NULL; |
| 81 | } |
| 82 | CharVector(CharVector&o) { // COPY CTOR |
| 83 | init(); |
| 84 | copy(o.arr, o._size); |
| 85 | } |
| 86 | CharVector& operator=(CharVector&o) { // ASSIGN |
| 87 | init(); |
| 88 | copy(o.arr, o._size); |
| 89 | return(*this); |
| 90 | } |
| 91 | char operator[](int x) const { |
| 92 | return(arr[x]); |
| 93 | } |
| 94 | char& operator[](int x) { |
| 95 | return(arr[x]); |
| 96 | } |
| 97 | int size() { |
| 98 | return(_size); |
| 99 | } |
| 100 | void size(int count) { |
| 101 | if ( count != _size ) { |
| 102 | arr = (char*)realloc(arr, count * sizeof(char)); |
| 103 | _size = count; |
| 104 | } |
| 105 | } |
| 106 | char pop_back() { |
| 107 | char tmp = arr[_size-1]; |
| 108 | _size--; |
| 109 | return(tmp); |
| 110 | } |
| 111 | void push_back(char val) { |
| 112 | int x = _size; |
| 113 | size(_size+1); |
| 114 | arr[x] = val; |
| 115 | } |
| 116 | char back() { |
| 117 | return(arr[_size-1]); |
| 118 | } |
| 119 | }; |
| 120 | CharVector _rowselect; // selection flag for each row |
| 121 | |
| 122 | // handle() state variables. |
| 123 | // Put here instead of local statics in handle(), so more |
| 124 | // than one instance can exist without crosstalk between. |
| 125 | // |
| 126 | int _dragging_select; // dragging out a selection? |
| 127 | int _last_row; |
| 128 | int _last_y; // last event's Y position |
| 129 | int _last_push_x; // last PUSH event's X position |
| 130 | int _last_push_y; // last PUSH event's Y position |
| 131 | |
| 132 | TableRowSelectMode _selectmode; |
| 133 | |
| 134 | protected: |
| 135 | int handle(int event); |
| 136 | int find_cell(TableContext context, // find cell's x/y/w/h given r/c |
| 137 | int R, int C, int &X, int &Y, int &W, int &H) { |
| 138 | return(Fl_Table::find_cell(context, R, C, X, Y, W, H)); |
| 139 | } |
| 140 | |
| 141 | public: |
| 142 | /** |
| 143 | The constructor for the Fl_Table_Row. |
| 144 | This creates an empty table with no rows or columns, |
| 145 | with headers and row/column resize behavior disabled. |
| 146 | */ |
| 147 | Fl_Table_Row(int X, int Y, int W, int H, const char *l=0) : Fl_Table(X,Y,W,H,l) { |
| 148 | _dragging_select = 0; |
| 149 | _last_row = -1; |
| 150 | _last_y = -1; |
| 151 | _last_push_x = -1; |
| 152 | _last_push_y = -1; |
| 153 | _selectmode = SELECT_MULTI; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | The destructor for the Fl_Table_Row. |
| 158 | Destroys the table and its associated widgets. |
| 159 | */ |
| 160 | ~Fl_Table_Row() { } |
| 161 | |
| 162 | void rows(int val); // set number of rows |
| 163 | int rows() { // get number of rows |
| 164 | return(Fl_Table::rows()); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | Sets the table selection mode. |
| 169 | |
| 170 | - \p Fl_Table_Row::SELECT_NONE - No selection allowed |
| 171 | - \p Fl_Table_Row::SELECT_SINGLE - Only single rows can be selected |
| 172 | - \p Fl_Table_Row::SELECT_MULTI - Multiple rows can be selected |
| 173 | */ |
| 174 | void type(TableRowSelectMode val); // set selection mode |
| 175 | |
| 176 | TableRowSelectMode type() const { // get selection mode |
| 177 | return(_selectmode); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | Checks to see if 'row' is selected. Returns 1 if selected, 0 if not. You can |
| 182 | change the selection of a row by clicking on it, or by using |
| 183 | select_row(row, flag) |
| 184 | */ |
| 185 | int row_selected(int row); // is row selected? (0=no, 1=yes, -1=range err) |
| 186 | |
| 187 | /** |
| 188 | Changes the selection state for 'row', depending on the value |
| 189 | of 'flag'. 0=deselected, 1=select, 2=toggle existing state. |
| 190 | */ |
| 191 | int select_row(int row, int flag=1); // select state for row: flag:0=off, 1=on, 2=toggle |
| 192 | // returns: 0=no change, 1=changed, -1=range err |
| 193 | |
| 194 | /** |
| 195 | This convenience function changes the selection state |
| 196 | for \em all rows based on 'flag'. 0=deselect, 1=select, 2=toggle existing state. |
| 197 | */ |
| 198 | void select_all_rows(int flag=1); // all rows to a known state |
| 199 | |
| 200 | void clear() { |
| 201 | rows(0); // implies clearing selection |
| 202 | cols(0); |
| 203 | Fl_Table::clear(); // clear the table |
| 204 | } |
| 205 | }; |
| 206 | |
| 207 | #endif /*_FL_TABLE_ROW_H*/ |
| 208 | |
| 209 | // |
| 210 | // End of "$Id: Fl_Table_Row.H 8301 2011-01-22 22:40:11Z AlbrechtS $". |
| 211 | // |