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.

131 lines
4.5KB

  1. //
  2. // "$Id: table-simple.cxx 8220 2011-01-08 18:41:47Z greg.ercolano $"
  3. //
  4. // Simple example of using Fl_Table - Greg Ercolano 11/29/2010
  5. //
  6. // Demonstrates the simplest use of Fl_Table possible.
  7. // Display a 10x10 array of integers with row/col headers.
  8. // No interaction; simple display of data only.
  9. // See other examples for more complex interactions with the table.
  10. //
  11. // Copyright 2010 Greg Ercolano.
  12. // Copyright 1998-2010 by Bill Spitzak and others.
  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 on the following page:
  30. //
  31. // http://www.fltk.org/str.php
  32. //
  33. #include <FL/Fl.H>
  34. #include <FL/Fl_Double_Window.H>
  35. #include <FL/Fl_Table.H>
  36. #include <FL/fl_draw.H>
  37. #define MAX_ROWS 30
  38. #define MAX_COLS 30
  39. // Derive a class from Fl_Table
  40. class MyTable : public Fl_Table {
  41. int data[MAX_ROWS][MAX_COLS]; // data array for cells
  42. // Draw the row/col headings
  43. // Make this a dark thin upbox with the text inside.
  44. //
  45. void DrawHeader(const char *s, int X, int Y, int W, int H) {
  46. fl_push_clip(X,Y,W,H);
  47. fl_draw_box(FL_THIN_UP_BOX, X,Y,W,H, row_header_color());
  48. fl_color(FL_BLACK);
  49. fl_draw(s, X,Y,W,H, FL_ALIGN_CENTER);
  50. fl_pop_clip();
  51. }
  52. // Draw the cell data
  53. // Dark gray text on white background with subtle border
  54. //
  55. void DrawData(const char *s, int X, int Y, int W, int H) {
  56. fl_push_clip(X,Y,W,H);
  57. // Draw cell bg
  58. fl_color(FL_WHITE); fl_rectf(X,Y,W,H);
  59. // Draw cell data
  60. fl_color(FL_GRAY0); fl_draw(s, X,Y,W,H, FL_ALIGN_CENTER);
  61. // Draw box border
  62. fl_color(color()); fl_rect(X,Y,W,H);
  63. fl_pop_clip();
  64. }
  65. // Handle drawing table's cells
  66. // Fl_Table calls this function to draw each visible cell in the table.
  67. // It's up to us to use FLTK's drawing functions to draw the cells the way we want.
  68. //
  69. void draw_cell(TableContext context, int ROW=0, int COL=0, int X=0, int Y=0, int W=0, int H=0) {
  70. static char s[40];
  71. switch ( context ) {
  72. case CONTEXT_STARTPAGE: // before page is drawn..
  73. fl_font(FL_HELVETICA, 16); // set the font for our drawing operations
  74. return;
  75. case CONTEXT_COL_HEADER: // Draw column headers
  76. sprintf(s,"%c",'A'+COL); // "A", "B", "C", etc.
  77. DrawHeader(s,X,Y,W,H);
  78. return;
  79. case CONTEXT_ROW_HEADER: // Draw row headers
  80. sprintf(s,"%03d:",ROW); // "001:", "002:", etc
  81. DrawHeader(s,X,Y,W,H);
  82. return;
  83. case CONTEXT_CELL: // Draw data in cells
  84. sprintf(s,"%d",data[ROW][COL]);
  85. DrawData(s,X,Y,W,H);
  86. return;
  87. default:
  88. return;
  89. }
  90. }
  91. public:
  92. // Constructor
  93. // Make our data array, and initialize the table options.
  94. //
  95. MyTable(int X, int Y, int W, int H, const char *L=0) : Fl_Table(X,Y,W,H,L) {
  96. // Fill data array
  97. for ( int r=0; r<MAX_ROWS; r++ )
  98. for ( int c=0; c<MAX_COLS; c++ )
  99. data[r][c] = 1000+(r*1000)+c;
  100. // Rows
  101. rows(MAX_ROWS); // how many rows
  102. row_header(1); // enable row headers (along left)
  103. row_height_all(20); // default height of rows
  104. row_resize(0); // disable row resizing
  105. // Cols
  106. cols(MAX_COLS); // how many columns
  107. col_header(1); // enable column headers (along top)
  108. col_width_all(80); // default width of columns
  109. col_resize(1); // enable column resizing
  110. end(); // end the Fl_Table group
  111. }
  112. ~MyTable() { }
  113. };
  114. int main(int argc, char **argv) {
  115. Fl_Double_Window win(900, 400, "Simple Table");
  116. MyTable table(10,10,880,380);
  117. win.end();
  118. win.resizable(table);
  119. win.show(argc,argv);
  120. return(Fl::run());
  121. }
  122. //
  123. // End of "$Id: table-simple.cxx 8220 2011-01-08 18:41:47Z greg.ercolano $".
  124. //