chipnetics

Fl_Table Example

Apr 2nd, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.42 KB | None | 0 0
  1. #include <FL/Fl_Input.H>
  2. #include <FL/Fl.H>
  3. #include <FL/Fl_Double_Window.H>
  4. #include <FL/fl_draw.H>
  5. #include <FL/Fl_Table_Row.H>
  6. #include <FL/names.h>
  7.  
  8. #if defined( _WIN32 )
  9. #pragma warning(disable:4996)
  10. #endif
  11.  
  12. class CustomTable : public Fl_Table_Row
  13. {
  14. protected:
  15.     void draw_cell(TableContext context,int R=0, int C=0, int X=0, int Y=0, int W=0, int H=0);
  16.  
  17. public:
  18.     CustomTable(int x, int y, int w, int h, const char *l);
  19.  
  20.     int active_row;
  21.  
  22.     int handle(int e);
  23.     static void table_cb(Fl_Widget* o, void* data);
  24.  
  25.     void scrollIfOutOfBounds();
  26. };
  27.  
  28. CustomTable::CustomTable(int x, int y, int w, int h, const char *l=0) : Fl_Table_Row(x,y,w,h,l)
  29. {  
  30.         active_row=-1;
  31.         end();
  32. }
  33.  
  34. void CustomTable::scrollIfOutOfBounds()
  35. {
  36.     int current_scrollbar_height = this->h();
  37.     int current_rows_on_screen = current_scrollbar_height/this->row_height(1);
  38.    
  39.     int current_top_row = this->row_position();
  40.     int current_last_row = current_top_row+current_rows_on_screen;
  41.  
  42.     if((this->active_row)<current_top_row)
  43.     {
  44.         this->row_position(this->active_row);
  45.     }
  46.     else if((this->active_row)>(current_last_row-1))
  47.     {
  48.         this->row_position(this->active_row+1-current_rows_on_screen);
  49.     }
  50. }
  51.  
  52. void CustomTable::draw_cell(TableContext context,
  53.               int R, int C, int X, int Y, int W, int H)
  54. {
  55.     static char s[40];
  56.     sprintf(s,"%d",R);
  57.  
  58.     switch ( context )
  59.     {
  60.     case CONTEXT_STARTPAGE:
  61.         fl_font(FL_HELVETICA, 12);
  62.         return;
  63.  
  64.     case CONTEXT_ROW_HEADER:
  65.     case CONTEXT_COL_HEADER:
  66.         fl_push_clip(X, Y, W, H);
  67.         {
  68.         fl_draw_box(FL_THIN_UP_BOX, X, Y, W, H, color());
  69.         fl_color(FL_BLACK);
  70.        
  71.         }
  72.         fl_pop_clip();
  73.         return;
  74.  
  75.     case CONTEXT_CELL:
  76.     {
  77.         fl_push_clip(X, Y, W, H);
  78.         {  
  79.             fl_color( row_selected(R) ? selection_color() : FL_WHITE);
  80.                 }  
  81.         fl_rectf(X, Y, W, H);
  82.  
  83.         // TEXT
  84.         fl_color(row_selected(R) ? FL_WHITE : FL_BLACK);
  85.         fl_draw(s, X, Y, W, H, FL_ALIGN_LEFT);
  86.  
  87.         // BORDER
  88.         fl_color(FL_LIGHT2);
  89.         fl_rect(X, Y, W, H);
  90.        
  91.         fl_pop_clip();
  92.         return;
  93.     }
  94.  
  95.     case CONTEXT_RC_RESIZE:
  96.      {    
  97.  
  98.         return;
  99.      }
  100.  
  101.     default:
  102.         return;
  103.     }
  104. }
  105.  
  106. int CustomTable::handle(int e)
  107. {
  108.     printf("CUSTABLE EVENT %s (%d)\n", fl_eventnames[e],e);
  109.    
  110.     switch(e) {
  111.    
  112.     case FL_KEYDOWN:
  113.     if (Fl::event_key()) {
  114.         if(Fl::event_key()==FL_Up)
  115.         {
  116.             this->selection_color(FL_BLUE);
  117.             this->select_row(active_row,0);
  118.             active_row>=1 ? active_row-- : active_row=0;
  119.             this->select_row(active_row,1);
  120.             this->scrollIfOutOfBounds();
  121.         }
  122.         if(Fl::event_key()==FL_Down)
  123.         {
  124.             this->selection_color(FL_BLUE);
  125.             this->select_row(active_row,0);
  126.             active_row++;
  127.             this->select_row(active_row,1);
  128.             this->scrollIfOutOfBounds();
  129.         }
  130.     }
  131.         break;
  132.  
  133.     case FL_RELEASE:
  134.  
  135.         if (Fl::event_button()) {
  136.        
  137.             if(Fl::event_button()==1)
  138.             {
  139.                 active_row=this->callback_row();
  140.             }
  141.    
  142.         }
  143.         break;
  144.     }
  145.     return Fl_Table_Row::handle(e);
  146. }
  147.  
  148. int main() {
  149.     Fl_Double_Window win(130, 262, "Ex");
  150.     CustomTable* custom_table = new CustomTable(10, 40, win.w()-20, win.h()-50);
  151.    
  152.     custom_table->rows(40);
  153.     custom_table->row_height_all(22);
  154.     custom_table->col_header_height(20);
  155.     custom_table->cols(1);
  156.     custom_table->when(FL_WHEN_RELEASE);
  157.     custom_table->selection_color(FL_BLUE);
  158.    
  159.     Fl_Input* input = new Fl_Input(10,10,100,25);
  160.  
  161.     win.resizable(custom_table);
  162.     win.show();
  163.  
  164.     input->take_focus();
  165.  
  166.     return Fl::run();
  167. }
Advertisement
Add Comment
Please, Sign In to add comment