Fl_Table_Row.H 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // "$Id: Fl_Table_Row.H 12390 2017-08-18 15:16:08Z 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. Distribution and use rights are outlined in
  15. // the file "COPYING" which should have been included with this file. If this
  16. // file is missing or damaged, see the license at:
  17. //
  18. // http://www.fltk.org/COPYING.php
  19. //
  20. // Please report all bugs and problems to "erco at seriss dot com".
  21. //
  22. #include <FL/Fl_Table.H>
  23. /**
  24. A table with row selection capabilities.
  25. This class implements a simple table with the ability to select
  26. rows. This widget is similar to an Fl_Browser with columns. Most
  27. methods of importance will be found in the Fl_Table widget, such
  28. as Fl_Table::rows() and Fl_Table::cols().
  29. To be useful it must be subclassed and at minimum the draw_cell()
  30. method must be overridden to provide the content of the cells. This widget
  31. does \em not manage the cell's data content; it is up to the parent
  32. class's draw_cell() method override to provide this.
  33. Events on the cells and/or headings generate callbacks when they are
  34. clicked by the user. You control when events are generated based on
  35. the values you supply for Fl_Table::when().
  36. */
  37. class FL_EXPORT Fl_Table_Row : public Fl_Table {
  38. public:
  39. enum TableRowSelectMode {
  40. SELECT_NONE, // no selection allowed
  41. SELECT_SINGLE, // single row selection
  42. SELECT_MULTI // multiple row selection (default)
  43. };
  44. private:
  45. // An STL-ish vector without templates
  46. class FL_EXPORT CharVector {
  47. char *arr;
  48. int _size;
  49. void init() {
  50. arr = 0;
  51. _size = 0;
  52. }
  53. void copy(char *newarr, int newsize);
  54. public:
  55. CharVector() { // CTOR
  56. init();
  57. }
  58. ~CharVector(); // DTOR
  59. CharVector(CharVector&o) { // COPY CTOR
  60. init();
  61. copy(o.arr, o._size);
  62. }
  63. CharVector& operator=(CharVector&o) { // ASSIGN
  64. init();
  65. copy(o.arr, o._size);
  66. return(*this);
  67. }
  68. char operator[](int x) const {
  69. return(arr[x]);
  70. }
  71. char& operator[](int x) {
  72. return(arr[x]);
  73. }
  74. int size() {
  75. return(_size);
  76. }
  77. void size(int count);
  78. char pop_back() {
  79. char tmp = arr[_size-1];
  80. _size--;
  81. return(tmp);
  82. }
  83. void push_back(char val) {
  84. int x = _size;
  85. size(_size+1);
  86. arr[x] = val;
  87. }
  88. char back() {
  89. return(arr[_size-1]);
  90. }
  91. };
  92. CharVector _rowselect; // selection flag for each row
  93. // handle() state variables.
  94. // Put here instead of local statics in handle(), so more
  95. // than one instance can exist without crosstalk between.
  96. //
  97. int _dragging_select; // dragging out a selection?
  98. int _last_row;
  99. int _last_y; // last event's Y position
  100. int _last_push_x; // last PUSH event's X position
  101. int _last_push_y; // last PUSH event's Y position
  102. TableRowSelectMode _selectmode;
  103. protected:
  104. int handle(int event);
  105. int find_cell(TableContext context, // find cell's x/y/w/h given r/c
  106. int R, int C, int &X, int &Y, int &W, int &H) {
  107. return(Fl_Table::find_cell(context, R, C, X, Y, W, H));
  108. }
  109. public:
  110. /**
  111. The constructor for the Fl_Table_Row.
  112. This creates an empty table with no rows or columns,
  113. with headers and row/column resize behavior disabled.
  114. */
  115. Fl_Table_Row(int X, int Y, int W, int H, const char *l=0) : Fl_Table(X,Y,W,H,l) {
  116. _dragging_select = 0;
  117. _last_row = -1;
  118. _last_y = -1;
  119. _last_push_x = -1;
  120. _last_push_y = -1;
  121. _selectmode = SELECT_MULTI;
  122. }
  123. /**
  124. The destructor for the Fl_Table_Row.
  125. Destroys the table and its associated widgets.
  126. */
  127. ~Fl_Table_Row() { }
  128. void rows(int val); // set number of rows
  129. int rows() { // get number of rows
  130. return(Fl_Table::rows());
  131. }
  132. /**
  133. Sets the table selection mode.
  134. - \p Fl_Table_Row::SELECT_NONE - No selection allowed
  135. - \p Fl_Table_Row::SELECT_SINGLE - Only single rows can be selected
  136. - \p Fl_Table_Row::SELECT_MULTI - Multiple rows can be selected
  137. */
  138. void type(TableRowSelectMode val); // set selection mode
  139. TableRowSelectMode type() const { // get selection mode
  140. return(_selectmode);
  141. }
  142. /**
  143. Checks to see if 'row' is selected. Returns 1 if selected, 0 if not. You can
  144. change the selection of a row by clicking on it, or by using
  145. select_row(row, flag)
  146. */
  147. int row_selected(int row); // is row selected? (0=no, 1=yes, -1=range err)
  148. /**
  149. Changes the selection state for 'row', depending on the value
  150. of 'flag'. 0=deselected, 1=select, 2=toggle existing state.
  151. */
  152. int select_row(int row, int flag=1); // select state for row: flag:0=off, 1=on, 2=toggle
  153. // returns: 0=no change, 1=changed, -1=range err
  154. /**
  155. This convenience function changes the selection state
  156. for \em all rows based on 'flag'. 0=deselect, 1=select, 2=toggle existing state.
  157. */
  158. void select_all_rows(int flag=1); // all rows to a known state
  159. void clear() {
  160. rows(0); // implies clearing selection
  161. cols(0);
  162. Fl_Table::clear(); // clear the table
  163. }
  164. DECLARE_CLASS_CHEAP_RTTI_2(Fl_Table_Row, Fl_Table)
  165. };
  166. #endif /*_FL_TABLE_ROW_H*/
  167. //
  168. // End of "$Id: Fl_Table_Row.H 12390 2017-08-18 15:16:08Z AlbrechtS $".
  169. //