2009-11-14 16:05:37 +03:00
|
|
|
//
|
2009-11-18 15:43:21 +03:00
|
|
|
// "$Id$"
|
|
|
|
//
|
2009-11-14 16:05:37 +03:00
|
|
|
// Fl_Table_Row -- A row oriented table widget
|
|
|
|
//
|
|
|
|
// A class specializing in a table of rows.
|
|
|
|
// Handles row-specific selection behavior.
|
|
|
|
//
|
|
|
|
// Copyright 2002 by Greg Ercolano.
|
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
2009-11-14 16:05:37 +03:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// http://www.fltk.org/COPYING.php
|
2009-11-14 16:05:37 +03:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// Please report all bugs and problems on the following page:
|
2009-11-14 16:05:37 +03:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// http://www.fltk.org/str.php
|
2009-11-14 16:05:37 +03:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
|
2009-11-14 16:05:37 +03:00
|
|
|
//
|
|
|
|
// TODO:
|
|
|
|
// o Row headings (only column headings supported currently)
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <stdio.h> // for debugging
|
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/fl_draw.H>
|
|
|
|
#include <FL/Fl_Table_Row.H>
|
|
|
|
|
|
|
|
// Is row selected?
|
|
|
|
int Fl_Table_Row::row_selected(int row) {
|
|
|
|
if ( row < 0 || row >= rows() ) return(-1);
|
|
|
|
return(_rowselect[row]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change row selection type
|
|
|
|
void Fl_Table_Row::type(TableRowSelectMode val) {
|
|
|
|
_selectmode = val;
|
|
|
|
switch ( _selectmode ) {
|
2010-12-05 04:22:53 +03:00
|
|
|
case SELECT_NONE: {
|
2009-11-14 16:05:37 +03:00
|
|
|
for ( int row=0; row<rows(); row++ ) {
|
2009-11-14 18:49:12 +03:00
|
|
|
_rowselect[row] = 0;
|
2009-11-14 16:05:37 +03:00
|
|
|
}
|
|
|
|
redraw();
|
|
|
|
break;
|
|
|
|
}
|
2010-12-05 04:22:53 +03:00
|
|
|
case SELECT_SINGLE: {
|
2009-11-14 16:05:37 +03:00
|
|
|
int count = 0;
|
|
|
|
for ( int row=0; row<rows(); row++ ) {
|
2009-11-14 18:49:12 +03:00
|
|
|
if ( _rowselect[row] ) {
|
|
|
|
if ( ++count > 1 ) { // only one allowed
|
|
|
|
_rowselect[row] = 0;
|
|
|
|
}
|
|
|
|
}
|
2009-11-14 16:05:37 +03:00
|
|
|
}
|
|
|
|
redraw();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SELECT_MULTI:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change selection state for row
|
|
|
|
//
|
|
|
|
// flag:
|
|
|
|
// 0 - clear selection
|
|
|
|
// 1 - set selection
|
|
|
|
// 2 - toggle selection
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// 0 - selection state did not change
|
|
|
|
// 1 - selection state changed
|
|
|
|
// -1 - row out of range or incorrect selection mode
|
|
|
|
//
|
|
|
|
int Fl_Table_Row::select_row(int row, int flag) {
|
|
|
|
int ret = 0;
|
|
|
|
if ( row < 0 || row >= rows() ) { return(-1); }
|
|
|
|
switch ( _selectmode ) {
|
|
|
|
case SELECT_NONE:
|
|
|
|
return(-1);
|
2009-11-14 18:49:12 +03:00
|
|
|
|
2010-12-05 04:22:53 +03:00
|
|
|
case SELECT_SINGLE: {
|
2009-11-14 16:05:37 +03:00
|
|
|
int oldval;
|
|
|
|
for ( int t=0; t<rows(); t++ ) {
|
2009-11-14 18:49:12 +03:00
|
|
|
if ( t == row ) {
|
|
|
|
oldval = _rowselect[row];
|
|
|
|
if ( flag == 2 ) { _rowselect[row] ^= 1; }
|
|
|
|
else { _rowselect[row] = flag; }
|
|
|
|
if ( oldval != _rowselect[row] ) {
|
|
|
|
redraw_range(row, row, leftcol, rightcol);
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( _rowselect[t] ) {
|
|
|
|
_rowselect[t] = 0;
|
|
|
|
redraw_range(t, t, leftcol, rightcol);
|
|
|
|
}
|
2009-11-14 16:05:37 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-11-14 18:49:12 +03:00
|
|
|
|
2010-12-05 04:22:53 +03:00
|
|
|
case SELECT_MULTI: {
|
2009-11-14 16:05:37 +03:00
|
|
|
int oldval = _rowselect[row];
|
|
|
|
if ( flag == 2 ) { _rowselect[row] ^= 1; }
|
|
|
|
else { _rowselect[row] = flag; }
|
|
|
|
if ( _rowselect[row] != oldval ) { // select state changed?
|
2009-11-14 18:49:12 +03:00
|
|
|
if ( row >= toprow && row <= botrow ) { // row visible?
|
|
|
|
// Extend partial redraw range
|
|
|
|
redraw_range(row, row, leftcol, rightcol);
|
|
|
|
}
|
|
|
|
ret = 1;
|
2009-11-14 16:05:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select all rows to a known state
|
|
|
|
void Fl_Table_Row::select_all_rows(int flag) {
|
|
|
|
switch ( _selectmode ) {
|
|
|
|
case SELECT_NONE:
|
|
|
|
return;
|
2009-11-14 18:49:12 +03:00
|
|
|
|
2009-11-14 16:05:37 +03:00
|
|
|
case SELECT_SINGLE:
|
|
|
|
if ( flag != 0 ) return;
|
|
|
|
//FALLTHROUGH
|
2009-11-14 18:49:12 +03:00
|
|
|
|
2010-12-05 04:22:53 +03:00
|
|
|
case SELECT_MULTI: {
|
2009-11-14 16:05:37 +03:00
|
|
|
char changed = 0;
|
|
|
|
if ( flag == 2 ) {
|
2009-11-14 18:49:12 +03:00
|
|
|
for ( int row=0; row<(int)_rowselect.size(); row++ ) {
|
|
|
|
_rowselect[row] ^= 1;
|
|
|
|
}
|
|
|
|
changed = 1;
|
2009-11-14 16:05:37 +03:00
|
|
|
} else {
|
2009-11-14 18:49:12 +03:00
|
|
|
for ( int row=0; row<(int)_rowselect.size(); row++ ) {
|
|
|
|
changed |= (_rowselect[row] != flag)?1:0;
|
|
|
|
_rowselect[row] = flag;
|
|
|
|
}
|
2009-11-14 16:05:37 +03:00
|
|
|
}
|
|
|
|
if ( changed ) {
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set number of rows
|
|
|
|
void Fl_Table_Row::rows(int val) {
|
|
|
|
Fl_Table::rows(val);
|
|
|
|
while ( val > (int)_rowselect.size() ) { _rowselect.push_back(0); } // enlarge
|
|
|
|
while ( val < (int)_rowselect.size() ) { _rowselect.pop_back(); } // shrink
|
|
|
|
}
|
|
|
|
|
2011-11-13 06:46:10 +04:00
|
|
|
//#define DEBUG 1
|
|
|
|
#ifdef DEBUG
|
|
|
|
#include <FL/names.h>
|
|
|
|
#define PRINTEVENT \
|
|
|
|
fprintf(stderr,"TableRow %s: ** Event: %s --\n", (label()?label():"none"), fl_eventnames[event]);
|
|
|
|
#else
|
|
|
|
#define PRINTEVENT
|
|
|
|
#endif
|
2009-11-14 16:05:37 +03:00
|
|
|
|
|
|
|
// Handle events
|
|
|
|
int Fl_Table_Row::handle(int event) {
|
2011-11-13 06:46:10 +04:00
|
|
|
PRINTEVENT;
|
|
|
|
|
|
|
|
// Make snapshots of realtime event states *before* we service user's cb,
|
|
|
|
// which may do things like post popup menus that return with unexpected button states.
|
|
|
|
int _event_button = Fl::event_button();
|
|
|
|
//int _event_clicks = Fl::event_clicks(); // uncomment if needed
|
|
|
|
int _event_x = Fl::event_x();
|
|
|
|
int _event_y = Fl::event_y();
|
|
|
|
//int _event_key = Fl::event_key(); // uncomment if needed
|
|
|
|
int _event_state = Fl::event_state();
|
|
|
|
//Fl_Widget *_focus = Fl::focus(); // uncomment if needed
|
2009-11-14 18:49:12 +03:00
|
|
|
|
2009-11-14 16:05:37 +03:00
|
|
|
// Let base class handle event
|
2011-11-13 06:46:10 +04:00
|
|
|
// Note: base class may invoke user callbacks that post menus,
|
|
|
|
// so from here on use event state snapshots (above).
|
|
|
|
//
|
2009-11-14 16:05:37 +03:00
|
|
|
int ret = Fl_Table::handle(event);
|
2009-11-14 18:49:12 +03:00
|
|
|
|
2009-11-14 16:05:37 +03:00
|
|
|
// The following code disables cell selection.. why was it added? -erco 05/18/03
|
|
|
|
// if ( ret ) { _last_y = Fl::event_y(); return(1); } // base class 'handled' it (eg. column resize)
|
2009-11-14 18:49:12 +03:00
|
|
|
|
2011-11-13 06:46:10 +04:00
|
|
|
int shiftstate = (_event_state & FL_CTRL) ? FL_CTRL :
|
|
|
|
(_event_state & FL_SHIFT) ? FL_SHIFT : 0;
|
2009-11-14 18:49:12 +03:00
|
|
|
|
2009-11-14 16:05:37 +03:00
|
|
|
// Which row/column are we over?
|
|
|
|
int R, C; // row/column being worked on
|
|
|
|
ResizeFlag resizeflag; // which resizing area are we over? (0=none)
|
|
|
|
TableContext context = cursor2rowcol(R, C, resizeflag);
|
|
|
|
switch ( event ) {
|
|
|
|
case FL_PUSH:
|
2011-11-13 06:46:10 +04:00
|
|
|
if ( _event_button == 1 ) {
|
|
|
|
_last_push_x = _event_x; // save regardless of context
|
|
|
|
_last_push_y = _event_y; // " "
|
2009-11-14 18:49:12 +03:00
|
|
|
|
|
|
|
// Handle selection in table.
|
|
|
|
// Select cell under cursor, and enable drag selection mode.
|
|
|
|
//
|
|
|
|
if ( context == CONTEXT_CELL ) {
|
|
|
|
// Ctrl key? Toggle selection state
|
|
|
|
switch ( shiftstate ) {
|
|
|
|
case FL_CTRL:
|
|
|
|
select_row(R, 2); // toggle
|
|
|
|
break;
|
|
|
|
|
2010-12-05 04:22:53 +03:00
|
|
|
case FL_SHIFT: {
|
2009-11-14 18:49:12 +03:00
|
|
|
select_row(R, 1);
|
|
|
|
if ( _last_row > -1 ) {
|
|
|
|
int srow = R, erow = _last_row;
|
|
|
|
if ( srow > erow ) {
|
|
|
|
srow = _last_row;
|
|
|
|
erow = R;
|
|
|
|
}
|
|
|
|
for ( int row = srow; row <= erow; row++ ) {
|
|
|
|
select_row(row, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
select_all_rows(0); // clear all previous selections
|
|
|
|
select_row(R, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_last_row = R;
|
|
|
|
_dragging_select = 1;
|
|
|
|
ret = 1; // FL_PUSH handled (ensures FL_DRAG will be sent)
|
|
|
|
// redraw(); // redraw() handled by select_row()
|
|
|
|
}
|
2009-11-14 16:05:37 +03:00
|
|
|
}
|
|
|
|
break;
|
2009-11-14 18:49:12 +03:00
|
|
|
|
2010-12-05 04:22:53 +03:00
|
|
|
case FL_DRAG: {
|
2009-11-14 16:05:37 +03:00
|
|
|
if ( _dragging_select ) {
|
2009-11-14 18:49:12 +03:00
|
|
|
// Dragged off table edges? Handle scrolling
|
|
|
|
int offtop = toy - _last_y; // >0 if off top of table
|
|
|
|
int offbot = _last_y - (toy + toh); // >0 if off bottom of table
|
|
|
|
|
|
|
|
if ( offtop > 0 && row_position() > 0 ) {
|
|
|
|
// Only scroll in upward direction
|
2011-11-13 06:46:10 +04:00
|
|
|
int diff = _last_y - _event_y;
|
2009-11-14 18:49:12 +03:00
|
|
|
if ( diff < 1 ) {
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
row_position(row_position() - diff);
|
|
|
|
context = CONTEXT_CELL; C = 0; R = row_position(); // HACK: fake it
|
|
|
|
if ( R < 0 || R > rows() ) { ret = 1; break; } // HACK: ugly
|
|
|
|
}
|
|
|
|
else if ( offbot > 0 && botrow < rows() ) {
|
|
|
|
// Only scroll in downward direction
|
2011-11-13 06:46:10 +04:00
|
|
|
int diff = _event_y - _last_y;
|
2009-11-14 18:49:12 +03:00
|
|
|
if ( diff < 1 ) {
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
row_position(row_position() + diff);
|
|
|
|
context = CONTEXT_CELL; C = 0; R = botrow; // HACK: fake it
|
|
|
|
if ( R < 0 || R > rows() ) { ret = 1; break; } // HACK: ugly
|
|
|
|
}
|
|
|
|
if ( context == CONTEXT_CELL ) {
|
|
|
|
switch ( shiftstate ) {
|
|
|
|
case FL_CTRL:
|
|
|
|
if ( R != _last_row ) { // toggle if dragged to new row
|
|
|
|
select_row(R, 2); // 2=toggle
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FL_SHIFT:
|
|
|
|
default:
|
|
|
|
select_row(R, 1);
|
|
|
|
if ( _last_row > -1 ) {
|
|
|
|
int srow = R, erow = _last_row;
|
|
|
|
if ( srow > erow ) {
|
|
|
|
srow = _last_row;
|
|
|
|
erow = R;
|
|
|
|
}
|
|
|
|
for ( int row = srow; row <= erow; row++ ) {
|
|
|
|
select_row(row, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ret = 1; // drag handled
|
|
|
|
_last_row = R;
|
|
|
|
}
|
2009-11-14 16:05:37 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-11-14 18:49:12 +03:00
|
|
|
|
2009-11-14 16:05:37 +03:00
|
|
|
case FL_RELEASE:
|
2011-11-13 06:46:10 +04:00
|
|
|
if ( _event_button == 1 ) {
|
2009-11-14 18:49:12 +03:00
|
|
|
_dragging_select = 0;
|
|
|
|
ret = 1; // release handled
|
|
|
|
// Clicked off edges of data table?
|
|
|
|
// A way for user to clear the current selection.
|
|
|
|
//
|
|
|
|
int databot = tiy + table_h,
|
|
|
|
dataright = tix + table_w;
|
|
|
|
if (
|
2011-11-13 06:46:10 +04:00
|
|
|
( _last_push_x > dataright && _event_x > dataright ) ||
|
|
|
|
( _last_push_y > databot && _event_y > databot )
|
2009-11-14 18:49:12 +03:00
|
|
|
) {
|
|
|
|
select_all_rows(0); // clear previous selections
|
|
|
|
}
|
2009-11-14 16:05:37 +03:00
|
|
|
}
|
|
|
|
break;
|
2009-11-14 18:49:12 +03:00
|
|
|
|
2009-11-14 16:05:37 +03:00
|
|
|
default:
|
2009-11-14 18:49:12 +03:00
|
|
|
break;
|
2009-11-14 16:05:37 +03:00
|
|
|
}
|
2011-11-13 06:46:10 +04:00
|
|
|
_last_y = _event_y;
|
2009-11-14 16:05:37 +03:00
|
|
|
return(ret);
|
|
|
|
}
|
2009-11-18 15:43:21 +03:00
|
|
|
|
|
|
|
//
|
|
|
|
// End of "$Id$".
|
|
|
|
//
|