Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FL/Fl_Input.H>
- #include <FL/Fl.H>
- #include <FL/Fl_Double_Window.H>
- #include <FL/fl_draw.H>
- #include <FL/Fl_Table_Row.H>
- #include <FL/names.h>
- #if defined( _WIN32 )
- #pragma warning(disable:4996)
- #endif
- class CustomTable : public Fl_Table_Row
- {
- protected:
- void draw_cell(TableContext context,int R=0, int C=0, int X=0, int Y=0, int W=0, int H=0);
- public:
- CustomTable(int x, int y, int w, int h, const char *l);
- int active_row;
- int handle(int e);
- static void table_cb(Fl_Widget* o, void* data);
- void scrollIfOutOfBounds();
- };
- CustomTable::CustomTable(int x, int y, int w, int h, const char *l=0) : Fl_Table_Row(x,y,w,h,l)
- {
- active_row=-1;
- end();
- }
- void CustomTable::scrollIfOutOfBounds()
- {
- int current_scrollbar_height = this->h();
- int current_rows_on_screen = current_scrollbar_height/this->row_height(1);
- int current_top_row = this->row_position();
- int current_last_row = current_top_row+current_rows_on_screen;
- if((this->active_row)<current_top_row)
- {
- this->row_position(this->active_row);
- }
- else if((this->active_row)>(current_last_row-1))
- {
- this->row_position(this->active_row+1-current_rows_on_screen);
- }
- }
- void CustomTable::draw_cell(TableContext context,
- int R, int C, int X, int Y, int W, int H)
- {
- static char s[40];
- sprintf(s,"%d",R);
- switch ( context )
- {
- case CONTEXT_STARTPAGE:
- fl_font(FL_HELVETICA, 12);
- return;
- case CONTEXT_ROW_HEADER:
- case CONTEXT_COL_HEADER:
- fl_push_clip(X, Y, W, H);
- {
- fl_draw_box(FL_THIN_UP_BOX, X, Y, W, H, color());
- fl_color(FL_BLACK);
- }
- fl_pop_clip();
- return;
- case CONTEXT_CELL:
- {
- fl_push_clip(X, Y, W, H);
- {
- fl_color( row_selected(R) ? selection_color() : FL_WHITE);
- }
- fl_rectf(X, Y, W, H);
- // TEXT
- fl_color(row_selected(R) ? FL_WHITE : FL_BLACK);
- fl_draw(s, X, Y, W, H, FL_ALIGN_LEFT);
- // BORDER
- fl_color(FL_LIGHT2);
- fl_rect(X, Y, W, H);
- fl_pop_clip();
- return;
- }
- case CONTEXT_RC_RESIZE:
- {
- return;
- }
- default:
- return;
- }
- }
- int CustomTable::handle(int e)
- {
- printf("CUSTABLE EVENT %s (%d)\n", fl_eventnames[e],e);
- switch(e) {
- case FL_KEYDOWN:
- if (Fl::event_key()) {
- if(Fl::event_key()==FL_Up)
- {
- this->selection_color(FL_BLUE);
- this->select_row(active_row,0);
- active_row>=1 ? active_row-- : active_row=0;
- this->select_row(active_row,1);
- this->scrollIfOutOfBounds();
- }
- if(Fl::event_key()==FL_Down)
- {
- this->selection_color(FL_BLUE);
- this->select_row(active_row,0);
- active_row++;
- this->select_row(active_row,1);
- this->scrollIfOutOfBounds();
- }
- }
- break;
- case FL_RELEASE:
- if (Fl::event_button()) {
- if(Fl::event_button()==1)
- {
- active_row=this->callback_row();
- }
- }
- break;
- }
- return Fl_Table_Row::handle(e);
- }
- int main() {
- Fl_Double_Window win(130, 262, "Ex");
- CustomTable* custom_table = new CustomTable(10, 40, win.w()-20, win.h()-50);
- custom_table->rows(40);
- custom_table->row_height_all(22);
- custom_table->col_header_height(20);
- custom_table->cols(1);
- custom_table->when(FL_WHEN_RELEASE);
- custom_table->selection_color(FL_BLUE);
- Fl_Input* input = new Fl_Input(10,10,100,25);
- win.resizable(custom_table);
- win.show();
- input->take_focus();
- return Fl::run();
- }
Advertisement
Add Comment
Please, Sign In to add comment