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.

265 lines
9.0KB

  1. //
  2. // "$Id: table-spreadsheet.cxx 8183 2011-01-04 17:31:56Z AlbrechtS $"
  3. //
  4. // Simple example of an interactive spreadsheet using Fl_Table.
  5. // Uses Mr. Satan's technique of instancing an Fl_Input around.
  6. //
  7. // Copyright 1998-2010 by Bill Spitzak and others.
  8. //
  9. // This library is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU Library General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. //
  14. // This library is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. // Library General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU Library General Public
  20. // License along with this library; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  22. // USA.
  23. //
  24. // Please report all bugs and problems on the following page:
  25. //
  26. // http://www.fltk.org/str.php
  27. //
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <FL/Fl.H>
  31. #include <FL/Fl_Double_Window.H>
  32. #include <FL/Fl_Table.H>
  33. #include <FL/Fl_Int_Input.H>
  34. #include <FL/fl_draw.H>
  35. const int MAX_COLS = 10;
  36. const int MAX_ROWS = 10;
  37. class Spreadsheet : public Fl_Table {
  38. Fl_Int_Input *input; // single instance of Fl_Int_Input widget
  39. int values[MAX_ROWS][MAX_COLS]; // array of data for cells
  40. int row_edit, col_edit; // row/col being modified
  41. protected:
  42. void draw_cell(TableContext context,int=0,int=0,int=0,int=0,int=0,int=0);
  43. void event_callback2(); // table's event callback (instance)
  44. static void event_callback(Fl_Widget*,void *v) { // table's event callback (static)
  45. ((Spreadsheet*)v)->event_callback2();
  46. }
  47. static void input_cb(Fl_Widget*,void* v) { // input widget's callback
  48. ((Spreadsheet*)v)->set_value_hide();
  49. }
  50. public:
  51. Spreadsheet(int X,int Y,int W,int H,const char* L=0) : Fl_Table(X,Y,W,H,L) {
  52. callback(&event_callback, (void*)this);
  53. when(FL_WHEN_NOT_CHANGED|when());
  54. // Create input widget that we'll use whenever user clicks on a cell
  55. input = new Fl_Int_Input(W/2,H/2,0,0);
  56. input->hide();
  57. input->callback(input_cb, (void*)this);
  58. input->when(FL_WHEN_ENTER_KEY_ALWAYS); // callback triggered when user hits Enter
  59. input->maximum_size(5);
  60. for (int c = 0; c < MAX_COLS; c++)
  61. for (int r = 0; r < MAX_ROWS; r++)
  62. values[r][c] = c + (r*MAX_COLS); // initialize cells
  63. end();
  64. }
  65. ~Spreadsheet() { }
  66. // Apply value from input widget to values[row][col] array and hide (done editing)
  67. void set_value_hide() {
  68. values[row_edit][col_edit] = atoi(input->value());
  69. input->hide();
  70. window()->cursor(FL_CURSOR_DEFAULT); // XXX: if we don't do this, cursor can disappear!
  71. }
  72. // Start editing a new cell: move the Fl_Int_Input widget to specified row/column
  73. // Preload the widget with the cell's current value,
  74. // and make the widget 'appear' at the cell's location.
  75. //
  76. void start_editing(int R, int C) {
  77. row_edit = R; // Now editing this row/col
  78. col_edit = C;
  79. int X,Y,W,H;
  80. find_cell(CONTEXT_CELL, R,C, X,Y,W,H); // Find X/Y/W/H of cell
  81. input->resize(X,Y,W,H); // Move Fl_Input widget there
  82. char s[30]; sprintf(s, "%d", values[R][C]); // Load input widget with cell's current value
  83. input->value(s);
  84. input->position(0,strlen(s)); // Select entire input field
  85. input->show(); // Show the input widget, now that we've positioned it
  86. input->take_focus();
  87. }
  88. // Tell the input widget it's done editing, and to 'hide'
  89. void done_editing() {
  90. if (input->visible()) { // input widget visible, ie. edit in progress?
  91. set_value_hide(); // Transfer its current contents to cell and hide
  92. }
  93. }
  94. // Return the sum of all rows in this column
  95. int sum_rows(int C) {
  96. int sum = 0;
  97. for (int r=0; r<rows()-1; ++r) // -1: don't include cell data in 'totals' column
  98. sum += values[r][C];
  99. return(sum);
  100. }
  101. // Return the sum of all cols in this row
  102. int sum_cols(int R) {
  103. int sum = 0;
  104. for (int c=0; c<cols()-1; ++c) // -1: don't include cell data in 'totals' column
  105. sum += values[R][c];
  106. return(sum);
  107. }
  108. // Return the sum of all cells in table
  109. int sum_all() {
  110. int sum = 0;
  111. for (int c=0; c<cols()-1; ++c) // -1: don't include cell data in 'totals' column
  112. for (int r=0; r<rows()-1; ++r) // -1: ""
  113. sum += values[r][c];
  114. return(sum);
  115. }
  116. };
  117. // Handle drawing all cells in table
  118. void Spreadsheet::draw_cell(TableContext context, int R,int C, int X,int Y,int W,int H) {
  119. static char s[30];
  120. switch ( context ) {
  121. case CONTEXT_STARTPAGE: // table about to redraw
  122. break;
  123. case CONTEXT_COL_HEADER: // table wants us to draw a column heading (C is column)
  124. fl_font(FL_HELVETICA | FL_BOLD, 14); // set font for heading to bold
  125. fl_push_clip(X,Y,W,H); // clip region for text
  126. {
  127. fl_draw_box(FL_THIN_UP_BOX, X,Y,W,H, col_header_color());
  128. fl_color(FL_BLACK);
  129. if (C == cols()-1) { // Last column? show 'TOTAL'
  130. fl_draw("TOTAL", X,Y,W,H, FL_ALIGN_CENTER);
  131. } else { // Not last column? show column letter
  132. sprintf(s, "%c", 'A' + C);
  133. fl_draw(s, X,Y,W,H, FL_ALIGN_CENTER);
  134. }
  135. }
  136. fl_pop_clip();
  137. return;
  138. case CONTEXT_ROW_HEADER: // table wants us to draw a row heading (R is row)
  139. fl_font(FL_HELVETICA | FL_BOLD, 14); // set font for row heading to bold
  140. fl_push_clip(X,Y,W,H);
  141. {
  142. fl_draw_box(FL_THIN_UP_BOX, X,Y,W,H, row_header_color());
  143. fl_color(FL_BLACK);
  144. if (R == rows()-1) { // Last row? Show 'Total'
  145. fl_draw("TOTAL", X,Y,W,H, FL_ALIGN_CENTER);
  146. } else { // Not last row? show row#
  147. sprintf(s, "%d", R+1);
  148. fl_draw(s, X,Y,W,H, FL_ALIGN_CENTER);
  149. }
  150. }
  151. fl_pop_clip();
  152. return;
  153. case CONTEXT_CELL: { // table wants us to draw a cell
  154. if (R == row_edit && C == col_edit && input->visible()) {
  155. return; // dont draw for cell with input widget over it
  156. }
  157. // Background
  158. if ( C < cols()-1 && R < rows()-1 ) {
  159. fl_draw_box(FL_THIN_UP_BOX, X,Y,W,H, FL_WHITE);
  160. } else {
  161. fl_draw_box(FL_THIN_UP_BOX, X,Y,W,H, 0xbbddbb00); // money green
  162. }
  163. // Text
  164. fl_push_clip(X+3, Y+3, W-6, H-6);
  165. {
  166. fl_color(FL_BLACK);
  167. if (C == cols()-1 || R == rows()-1) { // Last row or col? Show total
  168. fl_font(FL_HELVETICA | FL_BOLD, 14); // ..in bold font
  169. if (C == cols()-1 && R == rows()-1) { // Last row+col? Total all cells
  170. sprintf(s, "%d", sum_all());
  171. } else if (C == cols()-1) { // Row subtotal
  172. sprintf(s, "%d", sum_cols(R));
  173. } else if (R == rows()-1) { // Col subtotal
  174. sprintf(s, "%d", sum_rows(C));
  175. }
  176. fl_draw(s, X+3,Y+3,W-6,H-6, FL_ALIGN_RIGHT);
  177. } else { // Not last row or col? Show cell contents
  178. fl_font(FL_HELVETICA, 14); // ..in regular font
  179. sprintf(s, "%d", values[R][C]);
  180. fl_draw(s, X+3,Y+3,W-6,H-6, FL_ALIGN_RIGHT);
  181. }
  182. }
  183. fl_pop_clip();
  184. return;
  185. }
  186. default:
  187. return;
  188. }
  189. }
  190. // Callback whenever someone clicks on different parts of the table
  191. void Spreadsheet::event_callback2() {
  192. int R = callback_row();
  193. int C = callback_col();
  194. TableContext context = callback_context();
  195. switch ( context ) {
  196. case CONTEXT_CELL: { // A table event occurred on a cell
  197. switch (Fl::event()) { // see what FLTK event caused it
  198. case FL_PUSH: // mouse click?
  199. done_editing(); // finish editing previous
  200. if (R != rows()-1 && C != cols()-1 ) // only edit cells not in total's columns
  201. start_editing(R,C); // start new edit
  202. return;
  203. case FL_KEYBOARD: // key press in table?
  204. if ( Fl::event_key() == FL_Escape ) exit(0); // ESC closes app
  205. if (C == cols()-1 || R == rows()-1) return; // no editing of totals column
  206. done_editing(); // finish any previous editing
  207. start_editing(R,C); // start new edit
  208. if (Fl::event() == FL_KEYBOARD && Fl::e_text[0] != '\r') {
  209. input->handle(Fl::event()); // pass keypress to input widget
  210. }
  211. return;
  212. }
  213. return;
  214. }
  215. case CONTEXT_TABLE: // A table event occurred on dead zone in table
  216. case CONTEXT_ROW_HEADER: // A table event occurred on row/column header
  217. case CONTEXT_COL_HEADER:
  218. done_editing(); // done editing, hide
  219. return;
  220. default:
  221. return;
  222. }
  223. }
  224. int main() {
  225. Fl_Double_Window *win = new Fl_Double_Window(862, 322, "Fl_Table Spreadsheet");
  226. Spreadsheet *table = new Spreadsheet(10, 10, win->w()-20, win->h()-20);
  227. // Table rows
  228. table->row_header(1);
  229. table->row_header_width(70);
  230. table->row_resize(1);
  231. table->rows(MAX_ROWS+1); // +1: leaves room for 'total row'
  232. table->row_height_all(25);
  233. // Table cols
  234. table->col_header(1);
  235. table->col_header_height(25);
  236. table->col_resize(1);
  237. table->cols(MAX_COLS+1); // +1: leaves room for 'total column'
  238. table->col_width_all(70);
  239. // Show window
  240. win->end();
  241. win->resizable(table);
  242. win->show();
  243. return Fl::run();
  244. }
  245. //
  246. // End of "$Id: table-spreadsheet.cxx 8183 2011-01-04 17:31:56Z AlbrechtS $".
  247. //