You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
6.0KB

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