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.

351 lines
12KB

  1. //
  2. // "$Id: table-spreadsheet-with-keyboard-nav.cxx 8321 2011-01-28 01:34:22Z greg.ercolano $"
  3. //
  4. // Simple example of an interactive spreadsheet using Fl_Table.
  5. // Uses Mr. Satan's technique of instancing an Fl_Input around.
  6. // Modified to test Jean-Marc's mods for keyboard nav and mouse selection.
  7. //
  8. // Fl_Table[1.00/LGPL] 04/18/03 Mister Satan -- Initial implementation, submitted to erco for Fl_Table
  9. // Fl_Table[1.10/LGPL] 05/17/03 Greg Ercolano -- Small mods to follow changes to Fl_Table
  10. // Fl_Table[1.20/LGPL] 02/22/04 Jean-Marc Lienher -- Keyboard nav and mouse selection
  11. // Fl_Table[1.21/LGPL] 02/22/04 Greg Ercolano -- Small reformatting mods, comments
  12. // FLTK[1.3.0/LGPL] 10/26/10 Greg Ercolano -- Moved from Fl_Table to FLTK 1.3.x, CMP compliance
  13. //
  14. // Copyright 1998-2010 by Bill Spitzak and others.
  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 on the following page:
  32. //
  33. // http://www.fltk.org/str.php
  34. //
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <FL/Fl.H>
  38. #include <FL/Fl_Double_Window.H>
  39. #include <FL/Fl_Table.H>
  40. #include <FL/Fl_Int_Input.H>
  41. #include <FL/Fl_Value_Slider.H>
  42. #include <FL/fl_draw.H>
  43. const int MAX_COLS = 26;
  44. const int MAX_ROWS = 500;
  45. class Spreadsheet : public Fl_Table {
  46. Fl_Int_Input *input; // single instance of Fl_Int_Input widget
  47. int values[MAX_ROWS][MAX_COLS]; // array of data for cells
  48. int row_edit, col_edit; // row/col being modified
  49. int s_left, s_top, s_right, s_bottom; // kb nav + mouse selection
  50. protected:
  51. void draw_cell(TableContext context,int=0,int=0,int=0,int=0,int=0,int=0);
  52. void event_callback2(); // table's event callback (instance)
  53. static void event_callback(Fl_Widget*, void *v) { // table's event callback (static)
  54. ((Spreadsheet*)v)->event_callback2();
  55. }
  56. static void input_cb(Fl_Widget*, void* v) { // input widget's callback
  57. ((Spreadsheet*)v)->set_value_hide();
  58. }
  59. public:
  60. Spreadsheet(int X,int Y,int W,int H,const char* L=0) : Fl_Table(X,Y,W,H,L) {
  61. callback(&event_callback, (void*)this);
  62. when(FL_WHEN_NOT_CHANGED|when());
  63. // Create input widget that we'll use whenever user clicks on a cell
  64. input = new Fl_Int_Input(W/2,H/2,0,0);
  65. input->hide();
  66. input->callback(input_cb, (void*)this);
  67. input->when(FL_WHEN_ENTER_KEY_ALWAYS); // callback triggered when user hits Enter
  68. input->maximum_size(5);
  69. row_edit = col_edit = 0;
  70. s_left = s_top = s_right = s_bottom = 0;
  71. for (int c = 0; c < MAX_COLS; c++)
  72. for (int r = 0; r < MAX_ROWS; r++)
  73. values[r][c] = (r + 2) * (c + 3); // initialize cells
  74. end();
  75. }
  76. ~Spreadsheet() { }
  77. // Apply value from input widget to values[row][col] array and hide (done editing)
  78. void set_value_hide() {
  79. values[row_edit][col_edit] = atoi(input->value());
  80. input->hide();
  81. window()->cursor(FL_CURSOR_DEFAULT); // XXX: if we don't do this, cursor can disappear!
  82. }
  83. // Change number of rows
  84. void rows(int val) {
  85. Fl_Table::rows(val);
  86. }
  87. // Change number of columns
  88. void cols(int val) {
  89. Fl_Table::cols(val);
  90. }
  91. // Get number of rows
  92. inline int rows() {
  93. return Fl_Table::rows();
  94. }
  95. // Get number of columns
  96. inline int cols() {
  97. return Fl_Table::cols();
  98. }
  99. // Start editing a new cell: move the Fl_Int_Input widget to specified row/column
  100. // Preload the widget with the cell's current value,
  101. // and make the widget 'appear' at the cell's location.
  102. //
  103. void start_editing(int R, int C) {
  104. row_edit = R; // Now editing this row/col
  105. col_edit = C;
  106. int X,Y,W,H;
  107. find_cell(CONTEXT_CELL, R,C, X,Y,W,H); // Find X/Y/W/H of cell
  108. input->resize(X,Y,W,H); // Move Fl_Input widget there
  109. char s[30]; sprintf(s, "%d", values[R][C]); // Load input widget with cell's current value
  110. input->value(s);
  111. input->position(0,strlen(s)); // Select entire input field
  112. input->show(); // Show the input widget, now that we've positioned it
  113. input->take_focus();
  114. }
  115. // Tell the input widget it's done editing, and to 'hide'
  116. void done_editing() {
  117. if (input->visible()) { // input widget visible, ie. edit in progress?
  118. set_value_hide(); // Transfer its current contents to cell and hide
  119. }
  120. }
  121. // Return the sum of all rows in this column
  122. int sum_rows(int C) {
  123. int sum = 0;
  124. for (int r=0; r<rows()-1; ++r) // -1: don't include cell data in 'totals' column
  125. sum += values[r][C];
  126. return(sum);
  127. }
  128. // Return the sum of all cols in this row
  129. int sum_cols(int R) {
  130. int sum = 0;
  131. for (int c=0; c<cols()-1; ++c) // -1: don't include cell data in 'totals' column
  132. sum += values[R][c];
  133. return(sum);
  134. }
  135. // Return the sum of all cells in table
  136. int sum_all() {
  137. int sum = 0;
  138. for (int c=0; c<cols()-1; ++c) // -1: don't include cell data in 'totals' column
  139. for (int r=0; r<rows()-1; ++r) // -1: ""
  140. sum += values[r][c];
  141. return(sum);
  142. }
  143. };
  144. // Handle drawing all cells in table
  145. void Spreadsheet::draw_cell(TableContext context, int R,int C, int X,int Y,int W,int H) {
  146. static char s[30];
  147. switch ( context ) {
  148. case CONTEXT_STARTPAGE: // table about to redraw
  149. // Get kb nav + mouse 'selection region' for use below
  150. get_selection(s_top, s_left, s_bottom, s_right);
  151. break;
  152. case CONTEXT_COL_HEADER: // table wants us to draw a column heading (C is column)
  153. fl_font(FL_HELVETICA | FL_BOLD, 14); // set font for heading to bold
  154. fl_push_clip(X,Y,W,H); // clip region for text
  155. {
  156. fl_draw_box(FL_THIN_UP_BOX, X,Y,W,H, col_header_color());
  157. fl_color(FL_BLACK);
  158. if (C == cols()-1) { // Last column? show 'TOTAL'
  159. fl_draw("TOTAL", X,Y,W,H, FL_ALIGN_CENTER);
  160. } else { // Not last column? show column letter
  161. sprintf(s, "%c", 'A' + C);
  162. fl_draw(s, X,Y,W,H, FL_ALIGN_CENTER);
  163. }
  164. }
  165. fl_pop_clip();
  166. return;
  167. case CONTEXT_ROW_HEADER: // table wants us to draw a row heading (R is row)
  168. fl_font(FL_HELVETICA | FL_BOLD, 14); // set font for row heading to bold
  169. fl_push_clip(X,Y,W,H);
  170. {
  171. fl_draw_box(FL_THIN_UP_BOX, X,Y,W,H, row_header_color());
  172. fl_color(FL_BLACK);
  173. if (R == rows()-1) { // Last row? Show 'Total'
  174. fl_draw("TOTAL", X,Y,W,H, FL_ALIGN_CENTER);
  175. } else { // Not last row? show row#
  176. sprintf(s, "%d", R+1);
  177. fl_draw(s, X,Y,W,H, FL_ALIGN_CENTER);
  178. }
  179. }
  180. fl_pop_clip();
  181. return;
  182. case CONTEXT_CELL: { // table wants us to draw a cell
  183. if (R == row_edit && C == col_edit && input->visible()) {
  184. return; // dont draw for cell with input widget over it
  185. }
  186. // Background
  187. // Keyboard nav and mouse selection highlighting
  188. if (R >= s_top && R <= s_bottom && C >= s_left && C <= s_right) {
  189. fl_draw_box(FL_THIN_UP_BOX, X,Y,W,H, FL_YELLOW);
  190. } else if ( C < cols()-1 && R < rows()-1 ) {
  191. fl_draw_box(FL_THIN_UP_BOX, X,Y,W,H, FL_WHITE);
  192. } else {
  193. fl_draw_box(FL_THIN_UP_BOX, X,Y,W,H, 0xbbddbb00); // money green
  194. }
  195. // Text
  196. fl_push_clip(X+3, Y+3, W-6, H-6);
  197. {
  198. fl_color(FL_BLACK);
  199. if (C == cols()-1 || R == rows()-1) { // Last row or col? Show total
  200. fl_font(FL_HELVETICA | FL_BOLD, 14); // ..in bold font
  201. if (C == cols()-1 && R == rows()-1) { // Last row+col? Total all cells
  202. sprintf(s, "%d", sum_all());
  203. } else if (C == cols()-1) { // Row subtotal
  204. sprintf(s, "%d", sum_cols(R));
  205. } else if (R == rows()-1) { // Col subtotal
  206. sprintf(s, "%d", sum_rows(C));
  207. }
  208. fl_draw(s, X+3,Y+3,W-6,H-6, FL_ALIGN_RIGHT);
  209. } else { // Not last row or col? Show cell contents
  210. fl_font(FL_HELVETICA, 14); // ..in regular font
  211. sprintf(s, "%d", values[R][C]);
  212. fl_draw(s, X+3,Y+3,W-6,H-6, FL_ALIGN_RIGHT);
  213. }
  214. }
  215. fl_pop_clip();
  216. return;
  217. }
  218. case CONTEXT_RC_RESIZE: { // table resizing rows or columns
  219. if (!input->visible()) return;
  220. find_cell(CONTEXT_TABLE, row_edit, col_edit, X, Y, W, H);
  221. if (X==input->x() && Y==input->y() && W==input->w() && H==input->h()) {
  222. return; // no change? ignore
  223. }
  224. input->resize(X,Y,W,H);
  225. return;
  226. }
  227. default:
  228. return;
  229. }
  230. }
  231. // Callback whenever someone clicks on different parts of the table
  232. void Spreadsheet::event_callback2() {
  233. int R = callback_row();
  234. int C = callback_col();
  235. TableContext context = callback_context();
  236. switch ( context ) {
  237. case CONTEXT_CELL: { // A table event occurred on a cell
  238. switch (Fl::event()) { // see what FLTK event caused it
  239. case FL_PUSH: // mouse click?
  240. done_editing(); // finish editing previous
  241. if (R != rows()-1 && C != cols()-1 ) // only edit cells not in total's columns
  242. start_editing(R,C); // start new edit
  243. return;
  244. case FL_KEYBOARD: // key press in table?
  245. if ( Fl::event_key() == FL_Escape ) exit(0); // ESC closes app
  246. if (C == cols()-1 || R == rows()-1) return; // no editing of totals column
  247. done_editing(); // finish any previous editing
  248. set_selection(R, C, R, C); // select the current cell
  249. start_editing(R,C); // start new edit
  250. if (Fl::event() == FL_KEYBOARD && Fl::e_text[0] != '\r') {
  251. input->handle(Fl::event()); // pass keypress to input widget
  252. }
  253. return;
  254. }
  255. return;
  256. }
  257. case CONTEXT_TABLE: // A table event occurred on dead zone in table
  258. case CONTEXT_ROW_HEADER: // A table event occurred on row/column header
  259. case CONTEXT_COL_HEADER:
  260. done_editing(); // done editing, hide
  261. return;
  262. default:
  263. return;
  264. }
  265. }
  266. // Change number of columns
  267. void setcols_cb(Fl_Widget* w, void* v) {
  268. Spreadsheet* table = (Spreadsheet*)v;
  269. Fl_Valuator* in = (Fl_Valuator*)w;
  270. int cols = int(in->value()) + 1;
  271. table->cols(cols);
  272. table->redraw();
  273. }
  274. // Change number of rows
  275. void setrows_cb(Fl_Widget* w, void* v) {
  276. Spreadsheet* table = (Spreadsheet*)v;
  277. Fl_Valuator* in = (Fl_Valuator*)w;
  278. int rows = int(in->value()) + 1;
  279. table->rows(rows);
  280. table->redraw();
  281. }
  282. int main() {
  283. Fl_Double_Window *win = new Fl_Double_Window(922, 382, "Fl_Table Spreadsheet with Keyboard Navigation");
  284. Spreadsheet* table = new Spreadsheet(20, 20, win->w()-80, win->h()-80);
  285. // Table rows
  286. table->row_header(1);
  287. table->row_header_width(70);
  288. table->row_resize(1);
  289. table->rows(11);
  290. table->row_height_all(25);
  291. // Table cols
  292. table->col_header(1);
  293. table->col_header_height(25);
  294. table->col_resize(1);
  295. table->cols(11);
  296. table->col_width_all(70);
  297. table->set_selection(0,0,0,0); // select top/left cell
  298. // Add children to window
  299. win->begin();
  300. // Row slider
  301. Fl_Value_Slider setrows(win->w()-40,20,20,win->h()-80, 0);
  302. setrows.type(FL_VERT_NICE_SLIDER);
  303. setrows.bounds(2,MAX_ROWS);
  304. setrows.step(1);
  305. setrows.value(table->rows()-1);
  306. setrows.callback(setrows_cb, (void*)table);
  307. setrows.when(FL_WHEN_CHANGED);
  308. setrows.clear_visible_focus();
  309. // Column slider
  310. Fl_Value_Slider setcols(20,win->h()-40,win->w()-80,20, 0);
  311. setcols.type(FL_HOR_NICE_SLIDER);
  312. setcols.bounds(2,MAX_COLS);
  313. setcols.step(1);
  314. setcols.value(table->cols()-1);
  315. setcols.callback(setcols_cb, (void*)table);
  316. setcols.when(FL_WHEN_CHANGED);
  317. setcols.clear_visible_focus();
  318. win->end();
  319. win->resizable(table);
  320. win->show();
  321. return Fl::run();
  322. }
  323. //
  324. // End of "$Id: table-spreadsheet-with-keyboard-nav.cxx 8321 2011-01-28 01:34:22Z greg.ercolano $".
  325. //