Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

sxy_task_mvpoint.cpp

Go to the documentation of this file.
00001 
00002 // =======================================================================
00003 
00004 char *task_mvpoint_cpp = "$Id: sxy_task_mvpoint.cpp,v 1.3 2003/01/03 21:35:19 clinio Exp $";
00005 
00006 // =======================================================================
00007 
00008 #include <assert.h>
00009 #include <xy.h>
00010 
00011 extern "C" {
00012 #include <iup.h>
00013 #include <iupgc.h>
00014 #include <cd.h>
00015 }
00016 
00017 #include "sxy_chart.h"
00018 #include "sxy_graph.h"
00019 #include "sxy_task.h"
00020 #include "sxy_task_mvpoint.h"
00021 
00022 #define INVALID 99999
00023 
00024 // =======================================================================
00025 
00036 SXYTaskReturn SXYTaskMovePoint::mouseClick(SXYGraph* graph, int b, 
00037 int x, int y, int sft, int ctr) {
00038   const int npixels = 5;
00039 
00040   if ( b != (int)button ) return CALLBACK_NOT_TREATED;
00041 
00042   lockHorizontal( sft ? 1 : 0 );
00043   lockVertical( ctr ? 1 : 0 );
00044 
00045   Ihandle* iup_canvas = graph->getIupCanvas();
00046 
00047   int ncht = graph->getChartOfEvent(x,y);
00048   if( ncht < 0 ) return CALLBACK_NOT_TREATED;
00049   SXYChart* chart = graph->getChart(ncht);
00050 
00051   unsigned int ncurves = chart->getNumCurves();
00052   for( unsigned int c = 0; c < ncurves; c++ ) {
00053     SXYCurve* curve = chart->getCurve(c);
00054     unsigned int nmasks = curve->getNumMasks();
00055     for( unsigned int m = 0; m < nmasks; m++ ) {
00056       XYCartesianMask* mask = curve->getMask(m);
00057       if (mask->pick(x, y)) {
00058          SXYAxis* xaxis = curve->getHorizontalAxis();
00059          SXYAxis* yaxis = curve->getVerticalAxis();
00060          unsigned int s = curve->mapMaskToSeriesCallback(m);
00061          SXYSeries* series = curve->getSeries(s);
00062          unsigned int npoints = series->getNumPoints();
00063          for( unsigned int p = 0; p < npoints; p++ ) {
00064             double xpoint, ypoint;
00065             unsigned int has_point = series->getPoint(p, xpoint, ypoint);
00066             int xpixel = xaxis->getPixelValue(xpoint);
00067             int ypixel = yaxis->getPixelValue(ypoint);
00068             if ( has_point && abs(xpixel-x) < npixels && 
00069                  abs(ypixel-y) < npixels ) {
00070                if ( curve->tryPointEditionCallback(m, s, p) ) {
00071                  original_xvalue = xpoint;
00072                  original_yvalue = ypoint;
00073                  user_mask = m;
00074                  user_series = s;
00075                  user_point = p;
00076                  user_chart = chart;
00077                  user_curve = curve;
00078                  is_moving = 1;
00079                  ref_x = x; ref_y = y;
00080                  shift_x = 0; shift_y = 0;
00081                  IupSetAttribute( iup_canvas, IUP_CURSOR, IUP_CROSS );
00082                  drawCoordinate();
00083                  return CALLBACK_TREATED;
00084                }
00085             }
00086          }
00087       }
00088     }
00089   }
00090   is_moving = 0;
00091   IupSetAttribute( iup_canvas, IUP_CURSOR, IUP_ARROW );
00092   return CALLBACK_NOT_TREATED;
00093 }
00094 
00095 // ......................................................................
00099 void SXYTaskMovePoint::lockHorizontal(unsigned int l) {
00100    lock_x = l;
00101 }
00102 
00103 // ......................................................................
00107 void SXYTaskMovePoint::lockVertical(unsigned int l) {
00108    lock_y = l;
00109 }
00110 
00111 // ......................................................................
00115 unsigned int SXYTaskMovePoint::isHorizontallyLocked(void) {
00116    return lock_x;
00117 }
00118 
00119 // ......................................................................
00123 unsigned int SXYTaskMovePoint::isVerticallyLocked(void) {
00124    return lock_y;
00125 }
00126 
00127 // ......................................................................
00140 SXYTaskReturn SXYTaskMovePoint::mouseUnclick(SXYGraph* graph, int b, 
00141 int x, int y, int sft, int ctr) {
00142   if ( !is_moving ) return CALLBACK_NOT_TREATED;
00143   if ( !user_chart || !user_curve || 
00144        user_mask == INVALID || user_point == INVALID || 
00145        user_series == INVALID ) 
00146      return CALLBACK_NOT_TREATED;
00147 
00148   if ( b != (int)button ) {
00149      return CALLBACK_NOT_TREATED;
00150   }
00151   int cht = graph->getChartOfEvent(x, y);
00152   if (cht < 0) return CALLBACK_NOT_TREATED;
00153 
00154   drawCoordinate();
00155 
00156   SXYAxis* xaxis = user_curve->getHorizontalAxis();
00157   SXYAxis* yaxis = user_curve->getVerticalAxis();
00158   double xvalue = xaxis->getPositionValue(x);
00159   double yvalue = yaxis->getPositionValue(y);
00160   if ( isHorizontallyLocked() ) xvalue = original_xvalue;
00161   if ( isVerticallyLocked() ) yvalue = original_yvalue;
00162 
00163   unsigned int ok_edit = user_curve->editedPointCallback(user_mask, 
00164         user_series, user_point, xvalue, yvalue); 
00165   if (ok_edit) {
00166      SXYSeries* srs = user_curve->getSeries(user_series);
00167      assert(srs);
00168      srs->writePoint(user_point, xvalue, yvalue);
00169   }
00170 
00171   shift_x = 0; shift_y = 0;
00172   ref_x = x; ref_y = y;
00173 
00174   Ihandle* iup_canvas = graph->getIupCanvas();
00175   IupSetAttribute( iup_canvas, IUP_CURSOR, IUP_ARROW );
00176   is_moving = 0;
00177 
00178   user_chart = NULL;
00179   user_curve = NULL;
00180   user_mask = INVALID;
00181   graph->redrawGraph();
00182   return CALLBACK_TREATED;
00183 }
00184 
00185 // ......................................................................
00198 SXYTaskReturn SXYTaskMovePoint::mouseMotion(SXYGraph* graph, int x, int y, 
00199 int sft, int ctr) {
00200   if ( !is_moving ) return CALLBACK_NOT_TREATED;
00201   int cht = graph->getChartOfEvent(x, y);
00202   if (cht < 0) return CALLBACK_NOT_TREATED;
00203 
00204   if ( !user_chart || !user_curve || user_series == INVALID ||
00205        user_point == INVALID || user_mask == INVALID ) 
00206      return CALLBACK_NOT_TREATED;
00207 
00208   SXYAxis* xaxis = user_curve->getHorizontalAxis();
00209   SXYAxis* yaxis = user_curve->getVerticalAxis();
00210   double xvalue = xaxis->getPositionValue(x);
00211   double yvalue = yaxis->getPositionValue(y);
00212   if ( isHorizontallyLocked() ) xvalue = original_xvalue;
00213   if ( isVerticallyLocked() ) yvalue = original_yvalue;
00214 
00215   user_curve->movingPointCallback(user_mask, user_series, 
00216         user_point, xvalue, yvalue); 
00217 
00218   drawCoordinate();
00219   if ( !isHorizontallyLocked() ) shift_x = x - ref_x;
00220   if ( !isVerticallyLocked() ) shift_y = y - ref_y;
00221   drawCoordinate();
00222 
00223   return CALLBACK_TREATED;
00224 }
00225 
00226 // ......................................................................
00231 SXYTaskReturn SXYTaskMovePoint::mouseEnter(SXYGraph* graph) {
00232    Ihandle* iup_canvas = graph->getIupCanvas();
00233    IupSetAttribute( iup_canvas, IUP_CURSOR, IUP_ARROW );
00234 
00235    is_moving = 0;
00236    return CALLBACK_NOT_TREATED;
00237 }
00238 
00239 // ......................................................................
00244 SXYTaskReturn SXYTaskMovePoint::mouseLeave(SXYGraph* graph) {
00245    Ihandle* iup_canvas = graph->getIupCanvas();
00246    IupSetAttribute( iup_canvas, IUP_CURSOR, IUP_ARROW );
00247 
00248    is_moving = 0;
00249    graph->redrawGraph();
00250    return CALLBACK_NOT_TREATED;
00251 }
00252 
00253 // ......................................................................
00256 void SXYTaskMovePoint::drawCoordinate(void) {
00257    int old_write_mode = cdWriteMode( CD_NOT_XOR );
00258    long int old_color = cdForeground( getXorColor() );
00259 
00260    int xmax, ymax;
00261    int x = ref_x+shift_x;
00262    int y = ref_y+shift_y;
00263    cdGetCanvasSize( &xmax, &ymax, NULL, NULL );
00264 
00265    int size = 5;
00266    cdLine( ref_x, ref_y, x, y );
00267 
00268    cdRect( x-size, x+size, y-size, y+size );
00269    if ( !isVerticallyLocked() ) {
00270      cdLine( 0, y, x-size, y ); cdLine( x+size, y, xmax, y );
00271    }
00272    if ( !isHorizontallyLocked() ) {
00273      cdLine( x, 0, x, y-size); cdLine( x, y+size, x, ymax);
00274    }
00275 
00276    cdWriteMode( old_write_mode );
00277    cdForeground( old_color );
00278 }
00279 
00280 // ......................................................................
00283 void SXYTaskMovePoint::resetAttributes(void) {
00284    button = INVALID;
00285    is_moving = 0;
00286    user_chart = NULL;
00287    user_curve = NULL;
00288    user_mask = INVALID;
00289    user_series = INVALID;
00290    user_point = INVALID;
00291    ref_x = ref_y = 0;
00292    shift_x = shift_y = 0;
00293    lock_x = 0; lock_y = 0;
00294    original_xvalue = -9999.0;
00295    original_yvalue = -9999.0;
00296 }
00297 
00298 // ......................................................................
00302 SXYTaskMovePoint::SXYTaskMovePoint(unsigned int button) {
00303    resetAttributes();
00304    this->button = button;
00305 }
00306 
00307 // ......................................................................
00310 SXYTaskMovePoint::~SXYTaskMovePoint() {
00311    resetAttributes();
00312 }
00313 

SXY
Tecgraf / PUC-Rio - Computer Graphics Technology Group