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.

171 lines
4.6KB

  1. //
  2. // "$Id: table-as-container.cxx 8183 2011-01-04 17:31:56Z AlbrechtS $"
  3. //
  4. // Show how FLTK widgets can be parented by Fl_Table. -erco 03/30/2003
  5. //
  6. // Originally the 'widgettable.cxx' example program that came with
  7. // erco's Fl_Table widget. Added to FLTK in 2010.
  8. //
  9. // This demonstrates how to use Fl_Table as a 'container' for FLTK
  10. // widgets; one widget per cell. This isn't optimal for large tables,
  11. // where it's better to make one instance of a widget, and move it to
  12. // where it's needed. For an example of this, see the example program
  13. // "table-spreadsheet.cxx".
  14. //
  15. // Copyright 2010 Greg Ercolano.
  16. // Copyright 1998-2010 by Bill Spitzak and others.
  17. //
  18. // This library is free software; you can redistribute it and/or
  19. // modify it under the terms of the GNU Library General Public
  20. // License as published by the Free Software Foundation; either
  21. // version 2 of the License, or (at your option) any later version.
  22. //
  23. // This library is distributed in the hope that it will be useful,
  24. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. // Library General Public License for more details.
  27. //
  28. // You should have received a copy of the GNU Library General Public
  29. // License along with this library; if not, write to the Free Software
  30. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  31. // USA.
  32. //
  33. // Please report all bugs and problems to:
  34. //
  35. // http://www.fltk.org/str.php
  36. //
  37. #include <stdio.h>
  38. #include <FL/Fl.H>
  39. #include <FL/Fl_Double_Window.H>
  40. #include <FL/Fl_Light_Button.H>
  41. #include <FL/Fl_Input.H>
  42. #include <FL/fl_draw.H>
  43. #include <FL/Fl_Table.H>
  44. void button_cb(Fl_Widget *w, void*);
  45. //
  46. // Simple demonstration class deriving from Fl_Table
  47. //
  48. class WidgetTable : public Fl_Table {
  49. protected:
  50. void draw_cell(TableContext context, // table cell drawing
  51. int R=0, int C=0, int X=0, int Y=0, int W=0, int H=0);
  52. public:
  53. WidgetTable(int x, int y, int w, int h, const char *l=0) : Fl_Table(x,y,w,h,l) {
  54. col_header(1);
  55. col_resize(1);
  56. col_header_height(25);
  57. row_header(1);
  58. row_resize(1);
  59. row_header_width(80);
  60. end();
  61. }
  62. ~WidgetTable() { }
  63. void SetSize(int newrows, int newcols) {
  64. rows(newrows);
  65. cols(newcols);
  66. begin(); // start adding widgets to group
  67. {
  68. for ( int r = 0; r<newrows; r++ ) {
  69. for ( int c = 0; c<newcols; c++ ) {
  70. int X,Y,W,H;
  71. find_cell(CONTEXT_TABLE, r, c, X, Y, W, H);
  72. char s[40];
  73. if ( c & 1 ) {
  74. // Create the input widgets
  75. sprintf(s, "%d.%d", r, c);
  76. Fl_Input *in = new Fl_Input(X,Y,W,H);
  77. in->value(s);
  78. } else {
  79. // Create the light buttons
  80. sprintf(s, "%d/%d ", r, c);
  81. Fl_Light_Button *butt = new Fl_Light_Button(X,Y,W,H,strdup(s));
  82. butt->align(FL_ALIGN_CENTER|FL_ALIGN_INSIDE);
  83. butt->callback(button_cb, (void*)0);
  84. butt->value( ((r+c*2) & 4 ) ? 1 : 0);
  85. }
  86. }
  87. }
  88. }
  89. end();
  90. }
  91. };
  92. // Handle drawing all cells in table
  93. void WidgetTable::draw_cell(TableContext context,
  94. int R, int C, int X, int Y, int W, int H) {
  95. switch ( context ) {
  96. case CONTEXT_STARTPAGE:
  97. fl_font(FL_HELVETICA, 12); // font used by all headers
  98. break;
  99. case CONTEXT_RC_RESIZE: {
  100. int X, Y, W, H;
  101. int index = 0;
  102. for ( int r = 0; r<rows(); r++ ) {
  103. for ( int c = 0; c<cols(); c++ ) {
  104. if ( index >= children() ) break;
  105. find_cell(CONTEXT_TABLE, r, c, X, Y, W, H);
  106. child(index++)->resize(X,Y,W,H);
  107. }
  108. }
  109. init_sizes(); // tell group children resized
  110. return;
  111. }
  112. case CONTEXT_ROW_HEADER:
  113. fl_push_clip(X, Y, W, H);
  114. {
  115. static char s[40];
  116. sprintf(s, "Row %d", R);
  117. fl_draw_box(FL_THIN_UP_BOX, X, Y, W, H, row_header_color());
  118. fl_color(FL_BLACK);
  119. fl_draw(s, X, Y, W, H, FL_ALIGN_CENTER);
  120. }
  121. fl_pop_clip();
  122. return;
  123. case CONTEXT_COL_HEADER:
  124. fl_push_clip(X, Y, W, H);
  125. {
  126. static char s[40];
  127. sprintf(s, "Column %d", C);
  128. fl_draw_box(FL_THIN_UP_BOX, X, Y, W, H, col_header_color());
  129. fl_color(FL_BLACK);
  130. fl_draw(s, X, Y, W, H, FL_ALIGN_CENTER);
  131. }
  132. fl_pop_clip();
  133. return;
  134. case CONTEXT_CELL:
  135. return; // fltk handles drawing the widgets
  136. default:
  137. return;
  138. }
  139. }
  140. void button_cb(Fl_Widget *w, void*) {
  141. fprintf(stderr, "BUTTON: %s\n", (const char*)w->label());
  142. }
  143. int main() {
  144. Fl_Double_Window win(940, 500, "table as container");
  145. WidgetTable table(20, 20, win.w()-40, win.h()-40, "FLTK widget table");
  146. table.SetSize(50, 50);
  147. win.end();
  148. win.resizable(table);
  149. win.show();
  150. return(Fl::run());
  151. }
  152. //
  153. // End of "$Id: table-as-container.cxx 8183 2011-01-04 17:31:56Z AlbrechtS $".
  154. //