00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 #undef MAIN
00016 #include "afni_graph.h"
00017 #include "afni.h"
00018 #include <X11/keysym.h>  
00019 
00020 static int show_grapher_pixmap = 1 ;
00021 static void fd_line( MCW_grapher *, int,int,int,int ) ;
00022 
00023 
00024 
00025 
00026 #undef  CALL_getser
00027 #define CALL_getser(gr,aa,bb,rtyp,rval)                         \
00028  do{ XtPointer (*gs)(int,int,XtPointer) =                       \
00029       (XtPointer (*)(int,int,XtPointer))(gr->getser) ;          \
00030     rval = (rtyp) gs(aa,bb,gr->getaux) ;                        \
00031  } while(0)
00032 
00033 
00034 
00035 
00036 #undef  CALL_sendback
00037 #define CALL_sendback(gr,scb)                                                 \
00038  do{ void (*sb)(MCW_grapher *,XtPointer,GRA_cbs *) =                          \
00039       (void (*)(MCW_grapher *,XtPointer,GRA_cbs *))(gr->status->send_CB) ;    \
00040      if( sb != NULL ) sb(gr,gr->getaux,&scb) ;                                \
00041  } while(0)
00042 
00043 
00044 
00045 
00046 MCW_grapher * new_MCW_grapher( MCW_DC *dc , get_ptr getser , XtPointer aux )
00047 {
00048    int ii ;
00049    MCW_grapher *grapher ;
00050    static int new_xsize = -1 , new_ysize = -1 ;
00051    char *buf , *cpt ;
00052    Widget rc_tmp , mb_tmp , form_tmp ;  
00053 
00054 ENTRY("new_MCW_grapher") ;
00055 
00056    grapher = myXtNew( MCW_grapher ) ;
00057 
00058    grapher->type   = MCW_GRAPHER_TYPE ;
00059    grapher->dc     = dc ;
00060    grapher->getser = getser ;
00061    grapher->getaux = aux ;
00062    grapher->parent = NULL ;
00063    grapher->valid  = 1 ;
00064 
00065    grapher->dont_redraw     = 0 ;  
00066    grapher->timer_id        = 0 ;  
00067    grapher->never_drawn     = 1 ;
00068    grapher->button2_enabled = 0 ;  
00069    grapher->mirror          = 0 ;  
00070 
00071    grapher->tschosen        = 0 ;  
00072 
00073    grapher->gx_max = 0 ;
00074    grapher->gy_max = 0 ;
00075    grapher->fWIDE  = 0 ;
00076    grapher->fHIGH  = 0 ;
00077 
00078    grapher->glogo_pixmap = XmUNSPECIFIED_PIXMAP ;
00079    grapher->glogo_width  = grapher->glogo_height = 0 ;
00080 
00081 #if 0
00082    grapher->status = (MCW_grapher_status *) getser(0,graCR_getstatus,aux) ;
00083 #else
00084    CALL_getser( grapher , 0,graCR_getstatus , MCW_grapher_status *,grapher->status ) ;
00085 #endif
00086 
00087    if( grapher->status->num_series < 1 ){
00088      fprintf(stderr,"*** Attempt to create grapher with < 1 time points! ***\a\n") ;
00089      myXtFree(grapher) ;
00090      RETURN(NULL) ;
00091    }
00092 
00093    GRA_NULL_tuser(grapher) ;  
00094 
00095 
00096 
00097    grapher->fdw_graph =
00098       XtVaAppCreateShell(
00099          "AFNI" , "AFNI" , topLevelShellWidgetClass , dc->display ,
00100            XmNminHeight , MIN_XSIZE + GL_DLX + GR_DLX ,
00101            XmNmaxHeight , dc->height ,
00102            XmNminWidth  , MIN_YSIZE + GT_DLY + GB_DLY ,
00103            XmNmaxWidth  , dc->width ,
00104            XmNdeleteResponse   , XmDO_NOTHING ,   
00105            XmNallowShellResize , False ,          
00106            XmNinitialResourcesPersistent , False ,
00107       NULL ) ;
00108 
00109    DC_yokify( grapher->fdw_graph , dc ) ;  
00110 
00111 
00112 
00113    if( new_xsize < 0 ){
00114       new_xsize = GX_MAX ;
00115 #if 0
00116       buf = XGetDefault(dc->display,"AFNI","graph_width") ;
00117 #else
00118       buf = RWC_getname(dc->display,"graph_width") ;
00119 #endif
00120       if( buf != NULL ){
00121          ii = strtol( buf , &cpt , 10 ) ;
00122          if( *cpt == '\0' || new_xsize >= MIN_XSIZE ||
00123                              new_xsize <= dc->width - GL_DLX - GR_DLX )
00124             new_xsize = ii ;
00125       }
00126 
00127       new_ysize = GY_MAX ;
00128 #if 0
00129       buf = XGetDefault(dc->display,"AFNI","graph_height") ;
00130 #else
00131       buf = RWC_getname(dc->display,"graph_height") ;
00132 #endif
00133       if( buf != NULL ){
00134          ii = strtol( buf , &cpt , 10 ) ;
00135          if( *cpt == '\0' || new_ysize >= MIN_YSIZE ||
00136                              new_ysize <= dc->width - GT_DLY - GB_DLY )
00137             new_ysize = ii ;
00138       }
00139    }
00140 
00141 
00142 
00143    form_tmp = XtVaCreateWidget(
00144                   "dialog" , xmFormWidgetClass , grapher->fdw_graph ,
00145                     XmNwidth  , new_xsize + GL_DLX + GR_DLX ,
00146                     XmNheight , new_ysize + GT_DLY + GB_DLY ,
00147                     XmNborderWidth , 0 ,
00148                     XmNtraversalOn , False ,
00149                     XmNinitialResourcesPersistent , False ,
00150               NULL ) ;
00151 
00152 
00153 
00154    grapher->draw_fd =
00155        XtVaCreateManagedWidget(
00156          "dialog" , xmDrawingAreaWidgetClass , form_tmp ,
00157 
00158 #if 0
00159           XmNwidth  , new_xsize + GL_DLX + GR_DLX ,
00160           XmNheight , new_ysize + GT_DLY + GB_DLY ,
00161 #endif
00162 
00163           XmNtopAttachment    , XmATTACH_FORM ,
00164           XmNleftAttachment   , XmATTACH_FORM ,
00165           XmNrightAttachment  , XmATTACH_FORM ,
00166           XmNbottomAttachment , XmATTACH_FORM ,
00167 
00168           XmNmarginWidth  , 0 ,
00169           XmNmarginHeight , 0 ,
00170 
00171           XmNtraversalOn , False ,
00172           XmNinitialResourcesPersistent , False ,
00173        NULL ) ;
00174 
00175    XtInsertEventHandler( grapher->draw_fd ,     
00176 
00177                             0
00178                           | KeyPressMask        
00179                           | ButtonPressMask     
00180                           | ExposureMask        
00181                           | StructureNotifyMask 
00182                          ,
00183                          FALSE ,                
00184                          GRA_drawing_EV ,       
00185                          (XtPointer) grapher ,  
00186                          XtListTail ) ;         
00187 
00188    MCW_register_help( grapher->draw_fd ,
00189                        "Button 1 in a sub-graph --> move it to center\n"
00190                        "Button 1 in the central --> move time index\n"
00191                        "              sub-graph     to closest point\n"
00192                        "                            on the graph\n"
00193 #if 0
00194                        "Shift or Ctrl keys with --> single-step time\n"
00195                        "Button 1 in the central     index up or down\n"
00196                        "              sub-graph\n"
00197 #endif
00198                        "\n"
00199                        "The red dot in the central sub-graph shows\n"
00200                        "the location of the current time index.\n"
00201                        "\n"
00202                        "Button 3 in a sub-graph --> show statistics\n"
00203                        "                            of time series\n"
00204                        "\n"
00205                        "To turn off the AFNI logo, click Button 1\n"
00206                        "inside the logo.\n"
00207                        "\n"
00208                        "Miscellaneous Keystrokes:\n"
00209                        "< = move back in time 1 point\n"
00210                        "> = move forward in time 1 point\n"
00211                        "1 = move to 1st time point\n"
00212                        "l = move to last time point\n"
00213                        "L = turn AFNI logo on/off\n"
00214                        "v/V = Video up/down in time\n"
00215                        "r/R = Video ricochet up/down in time\n"
00216                        "\n"
00217                        "See the 'Opt' menu for other keypress actions\n"
00218                        "and for other options to control graph display."
00219                     ) ;
00220 
00221    
00222    
00223    
00224 
00225 #ifdef BAD_BUTTON3_POPUPS    
00226    grapher->but3_menu =
00227       XmCreatePopupMenu(          form_tmp, "menu" , NULL , 0 ) ;
00228 #else
00229    grapher->but3_menu =
00230       XmCreatePopupMenu( grapher->draw_fd , "menu" , NULL , 0 ) ;
00231 #endif
00232 
00233    SAVEUNDERIZE(XtParent(grapher->but3_menu)) ; 
00234 
00235    VISIBILIZE_WHEN_MAPPED(grapher->but3_menu) ;
00236 
00237    grapher->but3_label =
00238       XtVaCreateManagedWidget(
00239          "dialog" , xmLabelWidgetClass , grapher->but3_menu ,
00240             XmNalignment , XmALIGNMENT_BEGINNING ,
00241             XmNtraversalOn , False ,
00242             XmNinitialResourcesPersistent , False ,
00243          NULL ) ;
00244 
00245    
00246    
00247    
00248 
00249    grapher->option_rowcol =
00250       XtVaCreateWidget(
00251          "dialog" , xmRowColumnWidgetClass , form_tmp ,
00252             XmNpacking     , XmPACK_TIGHT ,
00253             XmNorientation , XmHORIZONTAL ,
00254             XmNmarginWidth , 0 ,
00255             XmNmarginHeight, 0 ,
00256             XmNspacing     , 2 ,
00257             XmNbackground  , grapher->dc->ovc->pixov_brightest ,
00258             XmNtraversalOn , False ,
00259             XmNinitialResourcesPersistent , False ,
00260             XmNleftAttachment   , XmATTACH_NONE ,
00261             XmNtopAttachment    , XmATTACH_NONE ,
00262             XmNrightAttachment  , XmATTACH_FORM ,
00263             XmNbottomAttachment , XmATTACH_FORM ,
00264          NULL ) ;
00265 
00266 #if 0
00267    allow_MCW_optmenu_popup( 0 ) ;  
00268 #endif
00269 
00270    
00271    
00272    
00273 
00274    
00275 
00276    rc_tmp = XtVaCreateWidget(
00277               "dialog" , xmRowColumnWidgetClass , grapher->option_rowcol ,
00278                  XmNorientation , XmHORIZONTAL ,
00279                  XmNpacking , XmPACK_TIGHT ,
00280                  XmNmarginWidth , 0 ,
00281                  XmNmarginHeight, 0 ,
00282                  XmNspacing     , 0 ,
00283                  XmNbackground  , grapher->dc->ovc->pixov_brightest ,
00284                  XmNtraversalOn , False ,
00285                  XmNinitialResourcesPersistent , False ,
00286               NULL ) ;
00287    mb_tmp = XmCreateMenuBar( rc_tmp , "dialog" , NULL,0 ) ;
00288    XtVaSetValues( mb_tmp ,
00289                      XmNmarginWidth  , 0 ,
00290                      XmNmarginHeight , 0 ,
00291                      XmNspacing      , 0 ,
00292                      XmNborderWidth  , 0 ,
00293                      XmNborderColor  , 0 ,
00294                      XmNtraversalOn  , False ,
00295                      XmNbackground   , grapher->dc->ovc->pixov_brightest ,
00296                   NULL ) ;
00297    XtManageChild( mb_tmp ) ;
00298 
00299    grapher->fmenu = AFNI_new_fim_menu( mb_tmp , GRA_fim_CB , 1 ) ;
00300    grapher->fmenu->parent = (XtPointer) grapher ;
00301    XtManageChild( rc_tmp ) ;
00302 
00303    grapher->polort = 1 ;  
00304 
00305    
00306 
00307 #define MENU_DLINE(wmenu)                                          \
00308    (void) XtVaCreateManagedWidget(                                 \
00309             "dialog" , xmSeparatorWidgetClass , grapher -> wmenu , \
00310              XmNseparatorType , XmDOUBLE_LINE , NULL )
00311 
00312 #define MENU_SLINE(wmenu)                                          \
00313    (void) XtVaCreateManagedWidget(                                 \
00314             "dialog" , xmSeparatorWidgetClass , grapher -> wmenu , \
00315              XmNseparatorType , XmSINGLE_LINE , NULL )
00316 
00317    
00318    
00319    
00320 
00321    
00322 
00323    rc_tmp = XtVaCreateWidget(
00324               "dialog" , xmRowColumnWidgetClass , grapher->option_rowcol ,
00325                  XmNorientation , XmHORIZONTAL ,
00326                  XmNpacking , XmPACK_TIGHT ,
00327                  XmNmarginWidth , 0 ,
00328                  XmNmarginHeight, 0 ,
00329                  XmNspacing     , 0 ,
00330                  XmNbackground  , grapher->dc->ovc->pixov_brightest ,
00331                  XmNtraversalOn , False ,
00332                  XmNinitialResourcesPersistent , False ,
00333               NULL ) ;
00334    mb_tmp = XmCreateMenuBar( rc_tmp , "dialog" , NULL,0 ) ;
00335    XtVaSetValues( mb_tmp ,
00336                      XmNmarginWidth  , 0 ,
00337                      XmNmarginHeight , 0 ,
00338                      XmNspacing      , 0 ,
00339                      XmNborderWidth  , 0 ,
00340                      XmNborderColor  , 0 ,
00341                      XmNtraversalOn  , False ,
00342                      XmNbackground   , grapher->dc->ovc->pixov_brightest ,
00343                   NULL ) ;
00344    XtManageChild( mb_tmp ) ;
00345 
00346    grapher->opt_menu =
00347          XmCreatePulldownMenu( mb_tmp , "menu" , NULL,0 ) ;
00348 
00349    VISIBILIZE_WHEN_MAPPED(grapher->opt_menu) ;  
00350 
00351    grapher->opt_cbut =
00352          XtVaCreateManagedWidget(
00353             "dialog" , xmCascadeButtonWidgetClass , mb_tmp ,
00354                LABEL_ARG("Opt") ,
00355                XmNsubMenuId , grapher->opt_menu ,
00356                XmNmarginWidth  , 0 ,
00357                XmNmarginHeight , 0 ,
00358                XmNmarginBottom , 0 ,
00359                XmNmarginTop    , 0 ,
00360                XmNmarginRight  , 0 ,
00361                XmNmarginLeft   , 0 ,
00362                XmNtraversalOn  , False ,
00363                XmNinitialResourcesPersistent , False ,
00364             NULL ) ;
00365 
00366    XtManageChild( rc_tmp ) ;
00367 
00368    MCW_register_hint( grapher->opt_cbut , "Graphing options menu" ) ;
00369 
00370    MCW_register_help( grapher->opt_cbut ,
00371                       "********  Graph Display Options:  ********\n"
00372                       "\n"
00373                       "Scale       --> Change vertical scaling\n"
00374                       "Matrix      --> Change number of sub-graphs\n"
00375                       "Grid        --> Change number of grid lines;\n"
00376                       "                 also can Pin the number of\n"
00377                       "                 time points displayed.\n"
00378                       "Slice       --> Change slice number\n"
00379                       "Colors, Etc --> Change colors of various\n"
00380                       "                 parts of the graph window\n"
00381                       "Baseline    --> Display each sub-graph with\n"
00382                       "                 its minimum at the bottom of\n"
00383                       "                 its window (the default), OR\n"
00384                       "                 with the minimum of all sub-\n"
00385                       "                 graphs as the common baseline\n"
00386                       "Show Text?  --> Instead of graphs, show the\n"
00387                       "                 numerical values of the data\n"
00388                       "                 at the current time index\n"
00389                       "                 in each sub-graph box\n"
00390                       "Save PNM    --> Save the graph window as an\n"
00391                       "                 image to a PNM format file\n"
00392                       "Write Center--> Central voxel timeseries will\n"
00393                       "                 be written to a file with a\n"
00394                       "                 name like 'X_Y_Z.suffix.1D'\n"
00395                       "                 where X,Y,Z are voxel indexes\n"
00396                       "Tran 0D     --> Choose a function to apply to\n"
00397                       "                 each point in each timeseries\n"
00398                       "Tran 1D     --> Choose a function to apply to\n"
00399                       "                 the timeseries as a whole\n"
00400                       "Double Plot --> If 'Tran 1D' is active, then\n"
00401                       "                 plot the data timeseries AND\n"
00402                       "                 the transformed timeseries;\n"
00403                       "                Plus/Minus -> transformed data\n"
00404                       "                 is added/subtracted from real\n"
00405                       "                 data timeseries (use this with\n"
00406                       "                 Dataset#2 to plot error bands)\n"
00407                       "Done        --> Close this graphing window\n"
00408                       "\n"
00409                       "The keystrokes indicated in the menus will\n"
00410                       "carry out the same functions, if pressed\n"
00411                       "when the cursor focus is in the graph window.\n"
00412                       "\n"
00413                       "N.B.: keystrokes without menu items are:\n"
00414                       " <  -->  move time index down by 1\n"
00415                       " >  -->  move time index up by 1\n"
00416                       " 1  -->  move to first image (time index 0)\n"
00417                       " l  -->  move to last image in time series\n"
00418                       " L  -->  turn off the AFNI logo in the corner"
00419                     ) ;
00420 
00421 
00422 
00423 #define OPT_MENU_BUT(wname,label,hhh)                                \
00424    grapher -> wname =                                                \
00425          XtVaCreateManagedWidget(                                    \
00426             "dialog" , xmPushButtonWidgetClass , grapher->opt_menu , \
00427                LABEL_ARG( label ) ,                                  \
00428                XmNmarginHeight , 0 ,                                 \
00429                XmNtraversalOn , False ,                              \
00430                XmNinitialResourcesPersistent , False ,               \
00431             NULL ) ;                                                 \
00432       XtAddCallback( grapher -> wname , XmNactivateCallback ,        \
00433                      GRA_opt_CB , (XtPointer) grapher ) ;            \
00434       MCW_register_hint( grapher -> wname , hhh ) ;
00435 
00436 
00437 
00438 
00439 #define OPT_MENU_PULLRIGHT(wmenu,wcbut,label,hhh)                      \
00440    grapher -> wmenu =                                                  \
00441      XmCreatePulldownMenu( grapher->opt_menu , "menu" , NULL , 0 ) ;   \
00442    grapher -> wcbut =                                                  \
00443      XtVaCreateManagedWidget(                                          \
00444        "dialog" , xmCascadeButtonWidgetClass , grapher->opt_menu ,     \
00445           LABEL_ARG( label ) ,                                         \
00446           XmNsubMenuId , grapher -> wmenu ,                            \
00447           XmNtraversalOn , False ,                                     \
00448           XmNinitialResourcesPersistent , False ,                      \
00449        NULL ) ;                                                        \
00450    MCW_register_hint( grapher -> wcbut , hhh ) ;                       \
00451    XtAddCallback( grapher -> wmenu, XmNmapCallback, GRA_mapmenu_CB, NULL ) ;
00452 
00453 
00454 
00455 #define OPT_MENU_PULL_BUT(wmenu,wname,label,hhh)                    \
00456    grapher -> wname =                                               \
00457          XtVaCreateManagedWidget(                                   \
00458             "dialog" , xmPushButtonWidgetClass , grapher -> wmenu , \
00459                LABEL_ARG( label ) ,                                 \
00460                XmNmarginHeight , 0 ,                                \
00461                XmNtraversalOn , False ,                             \
00462                XmNinitialResourcesPersistent , False ,              \
00463             NULL ) ;                                                \
00464       XtAddCallback( grapher -> wname , XmNactivateCallback ,       \
00465                      GRA_opt_CB , (XtPointer) grapher ) ;           \
00466    MCW_register_hint( grapher -> wname , hhh ) ;
00467 
00468 #ifdef USE_OPTMENUS
00469 
00470 
00471 
00472 #define OPT_MENU_OPTMENU(wmenu,wname,label,cb,hhh)                  \
00473    grapher -> wname =                                               \
00474       new_MCW_optmenu( grapher -> wmenu , label , 0,1,0,0 ,         \
00475                        cb , (XtPointer) grapher , NULL , NULL ) ;   \
00476    MCW_reghint_children( grapher -> wname -> wrowcol , hhh ) ;
00477 
00478 #endif 
00479 
00480    
00481 
00482    (void) XtVaCreateManagedWidget(
00483             "dialog" , xmLabelWidgetClass , grapher->opt_menu ,
00484                LABEL_ARG("--- Cancel ---") ,
00485                XmNrecomputeSize , False ,
00486                XmNinitialResourcesPersistent , False ,
00487             NULL ) ;
00488 
00489    MENU_SLINE(opt_menu) ;
00490 
00491    OPT_MENU_PULLRIGHT(opt_scale_menu,opt_scale_cbut     ,"Scale"   ,"Change vertical scale" );
00492    OPT_MENU_PULL_BUT(opt_scale_menu,opt_scale_down_pb  ,"Down [-]","Shrink graph heights"  );
00493    OPT_MENU_PULL_BUT(opt_scale_menu,opt_scale_up_pb    ,"Up   [+]","Increase graph heights");
00494    OPT_MENU_PULL_BUT(opt_scale_menu,opt_scale_choose_pb,"Choose"  ,"Set vertical scale"    );
00495    OPT_MENU_PULL_BUT(opt_scale_menu,opt_scale_auto_pb  ,"Auto [a]","Scale automatically"   );
00496 
00497    OPT_MENU_PULLRIGHT(opt_mat_menu,opt_mat_cbut      ,"Matrix"  , "Change number of graphs"   ) ;
00498    OPT_MENU_PULL_BUT( opt_mat_menu,opt_mat_down_pb   ,"Down [m]", "Reduce number of graphs"   ) ;
00499    OPT_MENU_PULL_BUT( opt_mat_menu,opt_mat_up_pb     ,"Up   [M]", "Increase number of graphs" ) ;
00500 #ifdef USE_OPTMENUS
00501    OPT_MENU_OPTMENU( opt_mat_menu,opt_mat_choose_av , "# " , GRA_mat_choose_CB , "Set number of graphs" ) ;
00502 #else
00503    OPT_MENU_PULL_BUT( opt_mat_menu,opt_mat_choose_pb ,"Choose" , "Set number of graphs" ) ;
00504 #endif
00505 
00506    OPT_MENU_PULLRIGHT(opt_grid_menu,opt_grid_cbut     ,"Grid"    , "Change vertical grid spacing" ) ;
00507    OPT_MENU_PULL_BUT( opt_grid_menu,opt_grid_down_pb  ,"Down [g]", "Reduce vertical grid spacing" ) ;
00508    OPT_MENU_PULL_BUT( opt_grid_menu,opt_grid_up_pb    ,"Up   [G]", "Increase vertical grid spacing" ) ;
00509    OPT_MENU_PULL_BUT( opt_grid_menu,opt_grid_auto_pb  ,"AutoGrid", "Set grid spacing automatically" ) ;
00510    OPT_MENU_PULL_BUT( opt_grid_menu,opt_grid_choose_pb,"Choose"  , "Set vertical grid spacing" ) ;
00511    OPT_MENU_PULL_BUT( opt_grid_menu,opt_pin_choose_pb ,"Index Pin","Fix index range of graph window" ) ;  
00512    OPT_MENU_PULL_BUT( opt_grid_menu,opt_grid_HorZ_pb  ,"HorZ [h]", "Horizontal line at Zero" ) ; 
00513 
00514    OPT_MENU_PULLRIGHT(opt_slice_menu,opt_slice_cbut      ,"Slice"   , "Change slice"  ) ;
00515    OPT_MENU_PULL_BUT( opt_slice_menu,opt_slice_down_pb   ,"Down [z]", "Decrement slice" ) ;
00516    OPT_MENU_PULL_BUT( opt_slice_menu,opt_slice_up_pb     ,"Up   [Z]", "Increment slice" ) ;
00517 #ifdef USE_OPTMENUS
00518    OPT_MENU_OPTMENU( opt_slice_menu,opt_slice_choose_av , "# " , GRA_slice_choose_CB , "Set slice" ) ;
00519 #else
00520    OPT_MENU_PULL_BUT( opt_slice_menu,opt_slice_choose_pb ,"Choose" , "Set slice" ) ;
00521 #endif
00522 
00523    
00524 
00525    { static char * bbox_label[1] = { "Use Thick Lines" } ;
00526      static char * pts_label[2]  = { "Graph Points" , "Points+Lines" } ;
00527      char     toplabel[64] ;
00528      XmString xstr ;
00529 
00530      OPT_MENU_PULLRIGHT(opt_colors_menu,opt_colors_cbut,"Colors, Etc.","Change graph appearance");
00531 
00532      if( strlen(grapher->status->namecode) > 0 ){
00533 
00534         sprintf( toplabel , "--- %s ---" , grapher->status->namecode ) ;
00535         xstr = XmStringCreateLtoR( toplabel , XmFONTLIST_DEFAULT_TAG ) ;
00536 
00537         (void) XtVaCreateManagedWidget(
00538                  "dialog" , xmLabelWidgetClass , grapher->opt_colors_menu ,
00539                     XmNlabelString , xstr ,
00540                     XmNrecomputeSize , False ,
00541                     XmNinitialResourcesPersistent , False ,
00542                  NULL ) ;
00543 
00544         XmStringFree( xstr ) ;
00545 
00546         MENU_DLINE(opt_colors_menu) ;
00547      }
00548 
00549      if( gr_setup_default ){
00550         gr_color_default[0] = INIT_GR_boxes_color  ;
00551         gr_color_default[1] = INIT_GR_backg_color  ;
00552         gr_color_default[2] = INIT_GR_grid_color   ;
00553         gr_color_default[3] = INIT_GR_text_color   ;
00554         gr_color_default[4] = INIT_GR_data_color   ;
00555         gr_color_default[5] = INIT_GR_ideal_color  ;
00556         gr_color_default[6] = INIT_GR_ort_color    ;
00557         gr_color_default[7] = INIT_GR_ignore_color ;
00558         gr_color_default[8] = INIT_GR_dplot_color  ;
00559 
00560         gr_thick_default[0] = INIT_GR_boxes_thick  ;
00561         gr_thick_default[1] = -1  ;
00562         gr_thick_default[2] = INIT_GR_grid_thick   ;
00563         gr_thick_default[3] = -1   ;
00564         gr_thick_default[4] = INIT_GR_data_thick   ;
00565         gr_thick_default[5] = INIT_GR_ideal_thick  ;
00566         gr_thick_default[6] = INIT_GR_ort_thick    ;
00567         gr_thick_default[7] = -1 ;
00568         gr_thick_default[8] = INIT_GR_dplot_thick  ;
00569 
00570         gr_setup_default = 0 ;
00571      }
00572 
00573      for( ii=0 ; ii < NUM_COLOR_ITEMS ; ii++ ){
00574 
00575         grapher->color_index[ii] = GRA_COLOR(gr_color_default[ii]) ;
00576         grapher->thick_index[ii] = gr_thick_default[ii] ;
00577         grapher->points_index[ii]= gr_points_default[ii] ;  
00578 
00579         grapher->opt_color_av[ii] =
00580            new_MCW_colormenu( grapher->opt_colors_menu ,
00581                               gr_color_label[ii] ,
00582                               grapher->dc ,
00583                               gr_color_start[ii] , grapher->dc->ovc->ncol_ov-1,
00584                               grapher->color_index[ii] ,
00585                               GRA_color_CB , (XtPointer) grapher ) ;
00586         MCW_reghint_children( grapher->opt_color_av[ii]->wrowcol ,
00587                               gr_color_hint[ii] ) ;           
00588 
00589         if( grapher->thick_index[ii] >= 0 ){
00590            grapher->opt_thick_bbox[ii] =
00591               new_MCW_bbox( grapher->opt_colors_menu ,
00592                             1 , bbox_label , MCW_BB_check , MCW_BB_noframe ,
00593                             GRA_thick_CB , (XtPointer) grapher ) ;
00594            MCW_reghint_children( grapher->opt_thick_bbox[ii]->wrowcol ,
00595                                  "Draw these lines thicker" ) ;
00596 
00597            if( grapher->thick_index[ii] )
00598               MCW_set_bbox( grapher->opt_thick_bbox[ii] , 1 ) ;
00599         } else {
00600            grapher->opt_thick_bbox[ii] = NULL ;
00601         }
00602 
00603         
00604         
00605 
00606         if( grapher->points_index[ii] >= 0 ){
00607            grapher->opt_points_bbox[ii] =
00608               new_MCW_bbox( grapher->opt_colors_menu ,
00609                             2 , pts_label , MCW_BB_radio_zero , MCW_BB_noframe ,
00610                             GRA_thick_CB , (XtPointer) grapher ) ;
00611            MCW_reghint_children(  grapher->opt_points_bbox[ii]->wrowcol ,
00612                                   "Plot graph as Points only, or as Points and Lines" ) ;
00613 
00614            if( grapher->points_index[ii] )
00615               MCW_set_bbox( grapher->opt_points_bbox[ii] ,
00616                             1 << (grapher->points_index[ii]-1) ) ;
00617         } else {
00618            grapher->opt_points_bbox[ii] = NULL ;
00619         }
00620 
00621         MENU_DLINE( opt_colors_menu ) ;
00622      }
00623 
00624      
00625 
00626      grapher->opt_ggap_av =
00627         new_MCW_optmenu( grapher->opt_colors_menu , "Graph Gap" ,
00628                          0 , 19 , INIT_GR_ggap , 0 ,
00629                          GRA_ggap_CB , (XtPointer) grapher , NULL , NULL ) ;
00630      AVOPT_columnize( grapher->opt_ggap_av , 4 ) ;
00631      MCW_reghint_children( grapher->opt_ggap_av->wrowcol ,
00632                            "Space sub-graphs apart" ) ;
00633 
00634      
00635 
00636      grapher->opt_gthick_av =
00637         new_MCW_optmenu( grapher->opt_colors_menu , "'Thick'  " ,
00638                          2 , 10 , INIT_GR_gthick , 0 ,
00639                          GRA_gthick_CB , (XtPointer) grapher , NULL , NULL ) ;
00640      AVOPT_columnize( grapher->opt_gthick_av , 2 ) ;
00641      MCW_reghint_children( grapher->opt_gthick_av->wrowcol ,
00642                            "Width of 'Thick' lines" ) ;
00643 
00644    }
00645    
00646 
00647 #if 0
00648    OPT_MENU_BUT(opt_color_up_pb     ,"Grid Color   [r]" , "Rotate grid color" ) ;
00649 #endif
00650 
00651    
00652 
00653    { char * bbox_label[3] = { "Individual [b]" ,
00654                               "Common     [b]" ,
00655                               "Global     [b]" } ;
00656      XmString xstr ;
00657      char gbuf[32] ;  
00658 
00659      
00660 
00661      cpt = getenv( "AFNI_GRAPH_GLOBALBASE" ) ;
00662      if( cpt != NULL )
00663        grapher->global_base = strtod( cpt , NULL ) ;
00664      else
00665        grapher->global_base = 0.0 ;
00666 
00667      cpt = getenv( "AFNI_GRAPH_BASELINE" ) ;
00668      if( cpt != NULL ){
00669        switch( *cpt ){
00670          default:  grapher->common_base = BASELINE_INDIVIDUAL ; break ;
00671 
00672          case 'C':
00673          case 'c': grapher->common_base = BASELINE_COMMON     ; break ;
00674 
00675          case 'G':
00676          case 'g': grapher->common_base = BASELINE_GLOBAL     ; break ;
00677        }
00678      } else {
00679        grapher->common_base = BASELINE_INDIVIDUAL ;
00680      }
00681 
00682      
00683 
00684      OPT_MENU_PULLRIGHT(opt_baseline_menu,opt_baseline_cbut,
00685                         "Baseline","Change sub-graphs baseline");
00686 
00687      grapher->opt_baseline_bbox =
00688          new_MCW_bbox( grapher->opt_baseline_menu ,
00689                        3 , bbox_label , MCW_BB_radio_one , MCW_BB_noframe ,
00690                        GRA_baseline_CB , (XtPointer)grapher ) ;
00691      MCW_set_bbox( grapher->opt_baseline_bbox , grapher->common_base ) ;
00692 
00693      MCW_reghint_children( grapher->opt_baseline_bbox->wrowcol ,
00694                           "Graph baseline methods" ) ;
00695 
00696      MENU_SLINE( opt_baseline_menu ) ;
00697 
00698      OPT_MENU_PULL_BUT( opt_baseline_menu,opt_baseline_setglobal_pb ,
00699                         "Set Global" , "Global baseline level" ) ;
00700 
00701      MENU_SLINE( opt_baseline_menu ) ;
00702 
00703      strcpy(gbuf,"Global:") ;
00704      AV_fval_to_char(grapher->global_base,gbuf+7) ;
00705      xstr = XmStringCreateLtoR(gbuf,XmFONTLIST_DEFAULT_TAG) ;
00706      grapher->opt_baseline_global_label =
00707         XtVaCreateManagedWidget(
00708                  "dialog" , xmLabelWidgetClass , grapher->opt_baseline_menu ,
00709                     XmNlabelString , xstr ,
00710                     XmNrecomputeSize , False ,
00711                     XmNtraversalOn , False ,
00712                     XmNinitialResourcesPersistent , False ,
00713                  NULL ) ;
00714      XmStringFree( xstr ) ;
00715    }
00716 
00717    
00718 
00719    { static char * bbox_label[1] = { "Show Text? [t]" } ;
00720 
00721     grapher->opt_textgraph_bbox =
00722          new_MCW_bbox( grapher->opt_menu ,
00723                        1 , bbox_label , MCW_BB_check , MCW_BB_noframe ,
00724                        GRA_textgraph_CB , (XtPointer)grapher ) ;
00725 
00726     MCW_reghint_children( grapher->opt_textgraph_bbox->wrowcol ,
00727                           "Display text, not graphs" ) ;
00728 
00729     grapher->textgraph = 0 ;
00730    }
00731 
00732    MENU_SLINE(opt_menu) ;
00733    OPT_MENU_BUT(opt_save_pb         ,"Save PNM     [S]" , "Save graph as an image" ) ;
00734 
00735    MENU_SLINE(opt_menu) ;
00736    OPT_MENU_BUT(opt_write_center_pb ,"Write Center [w]" , "Write central graph as a *.1D file" ) ;
00737    OPT_MENU_BUT(opt_write_suffix_pb ,"Set 'w' Suffix"   , "Set suffix for graph writing" ) ;
00738 
00739    
00740    
00741    
00742 
00743 #define COLSIZE AV_colsize()
00744 
00745    if( grapher->status->transforms0D != NULL &&
00746        grapher->status->transforms0D->num > 0  ){  
00747 
00748       MENU_DLINE(opt_menu) ;
00749 
00750       grapher->transform0D_av =
00751          new_MCW_optmenu( grapher->opt_menu ,
00752                           "Tran 0D" ,
00753                           0 , grapher->status->transforms0D->num , 0 , 0 ,
00754                           GRA_transform_CB , (XtPointer) grapher ,
00755                           GRA_transform_label , (XtPointer) grapher->status->transforms0D ) ;
00756 
00757       if( grapher->status->transforms0D->num >= COLSIZE )
00758          AVOPT_columnize( grapher->transform0D_av ,
00759                           (grapher->status->transforms0D->num/COLSIZE)+1 ) ;
00760 
00761       MCW_reghint_children( grapher->transform0D_av->wrowcol ,
00762                             "Pointwise data transformations" ) ;
00763 
00764    } else {
00765       grapher->transform0D_av = NULL ;
00766    }
00767    grapher->transform0D_func  = NULL ;  
00768    grapher->transform0D_index = 0 ;
00769 
00770    
00771    
00772    
00773 
00774    if( grapher->status->transforms1D != NULL &&
00775        grapher->status->transforms1D->num > 0  ){  
00776 
00777       MENU_DLINE(opt_menu) ;
00778 
00779       grapher->transform1D_av =
00780          new_MCW_optmenu( grapher->opt_menu ,
00781                           "Tran 1D" ,
00782                           0 , grapher->status->transforms1D->num , 0 , 0 ,
00783                           GRA_transform_CB , (XtPointer) grapher ,
00784                           GRA_transform_label , (XtPointer) grapher->status->transforms1D ) ;
00785 
00786       if( grapher->status->transforms1D->num >= COLSIZE )
00787          AVOPT_columnize( grapher->transform1D_av ,
00788                           (grapher->status->transforms1D->num/COLSIZE)+1 ) ;
00789 
00790       MCW_reghint_children( grapher->transform1D_av->wrowcol ,
00791                             "Time series transformations" ) ;
00792 
00793       
00794       
00795 
00796       { char * bbox_label[3] = { "DPlot Off" ,
00797                                  "Overlay"   ,
00798                                  "Plus/Minus" } ;
00799 
00800         OPT_MENU_PULLRIGHT(opt_dplot_menu,opt_dplot_cbut,
00801                            "Double Plot","Graph Dataset and Tran 1D?");
00802 
00803         grapher->opt_dplot_bbox =
00804             new_MCW_bbox( grapher->opt_dplot_menu ,
00805                           3 , bbox_label , MCW_BB_radio_one , MCW_BB_noframe ,
00806                           GRA_dplot_change_CB , (XtPointer)grapher ) ;
00807         MCW_set_bbox( grapher->opt_dplot_bbox , DPLOT_OFF ) ;
00808 
00809         MCW_reghint_children( grapher->opt_dplot_bbox->wrowcol ,
00810                               "How to show 2 curves" ) ;
00811       }
00812 
00813    } else {
00814       grapher->transform1D_av = NULL ;
00815       grapher->opt_dplot_bbox = NULL ;
00816    }
00817    grapher->transform1D_func  = NULL ;  
00818    grapher->transform1D_index = 0 ;
00819 
00820    
00821 
00822    MENU_DLINE(opt_menu) ;
00823 
00824    OPT_MENU_PULLRIGHT( opt_xaxis_menu , opt_xaxis_cbut    , "X-axis" , "Alter x-axis" ) ;
00825    OPT_MENU_PULL_BUT(  opt_xaxis_menu , opt_xaxis_pick_pb ,
00826                        "Pick x-axis" , "Set timeseries for x-axis" ) ;
00827    OPT_MENU_PULL_BUT(  opt_xaxis_menu , opt_xaxis_center_pb ,
00828                        "X-axis=center" , "X-axis = center voxel" ) ;
00829    OPT_MENU_PULL_BUT(  opt_xaxis_menu , opt_xaxis_clear_pb ,
00830                        "Clear x-axis" , "Clear x-axis timeseries" ) ;
00831 
00832    
00833 
00834    MENU_DLINE(opt_menu) ;
00835    OPT_MENU_BUT(opt_quit_pb         ,"Done         [q]" , "Close window" ) ;
00836    MCW_set_widget_bg( grapher->opt_quit_pb ,
00837                       MCW_hotcolor(grapher->opt_quit_pb) , 0 ) ;
00838 
00839 
00840 
00841    XtManageChild( grapher->option_rowcol ) ;
00842 
00843 #if 0
00844    allow_MCW_optmenu_popup( 1 ) ;  
00845 #endif
00846 
00847 
00848 
00849 if(PRINT_TRACING)
00850 { char str[128] ;
00851   sprintf(str,"STATUS: num_series=%d nx=%d ny=%d",
00852           grapher->status->num_series,grapher->status->nx,grapher->status->ny ) ;
00853   STATUS(str) ; }
00854 
00855    grapher->fscale      =  0 ;
00856    grapher->mat         =  0 ;
00857    grapher->xpoint      = -1 ;
00858    grapher->ypoint      = -1 ;
00859    grapher->zpoint      = -1 ;
00860 #if 0
00861    grapher->grid_color  = GRID_COLOR(grapher) ;
00862 #endif
00863    grapher->grid_index  = -1 ;
00864    grapher->grid_fixed  =  0 ;  
00865    grapher->key_Nlock   =  0 ;
00866    grapher->xFD         =  0 ;
00867    grapher->yFD         =  0 ;
00868    grapher->time_index  =  0 ;
00869    grapher->pin_top     =  0 ;  
00870    grapher->pin_bot     =  0 ;  
00871    grapher->ggap        =  INIT_GR_ggap ;    
00872    grapher->gthick      =  INIT_GR_gthick ;  
00873 
00874    grapher->cen_line    =  NULL ;  
00875    grapher->ncen_line   =  0 ;
00876    grapher->nncen       =  0 ;
00877    grapher->cen_tsim    =  NULL ;
00878    grapher->xax_tsim    =  NULL ;  
00879    grapher->ave_tsim    =  NULL ;  
00880 
00881    grapher->xx_text_1    =
00882     grapher->xx_text_2   =
00883      grapher->xx_text_2p = grapher->xx_text_3 = 1 ;
00884 
00885    grapher->ref_ts = NULL ;
00886    grapher->ort_ts = NULL ;
00887 
00888    grapher->ref_ts_plotall = grapher->ort_ts_plotall = 1 ;
00889 
00890    init_const( grapher ) ;
00891 
00892    grapher->setshift_inc_av   = NULL ;
00893    grapher->setshift_left_av  = NULL ;
00894    grapher->setshift_right_av = NULL ;
00895    grapher->dialog            = NULL ;
00896    grapher->setshift_inc      = 0.5 ;
00897    grapher->setshift_left     = 0 ;
00898    grapher->setshift_right    = 0 ;
00899 
00900 
00901 
00902    XtManageChild( form_tmp ) ;  
00903 
00904 #if 0
00905 STATUS("realizing widgets") ;
00906    XtRealizeWidget( grapher->fdw_graph ) ;
00907 
00908    WAIT_for_window(grapher->form_tmp) ;
00909 
00910    XtVaSetValues( grapher->option_rowcol ,
00911                     XmNleftAttachment   , XmATTACH_NONE ,
00912                     XmNtopAttachment    , XmATTACH_NONE ,
00913                     XmNrightAttachment  , XmATTACH_FORM ,
00914                     XmNbottomAttachment , XmATTACH_FORM ,
00915                   NULL ) ;
00916    XMapRaised( XtDisplay(grapher->option_rowcol) ,
00917                XtWindow(grapher->option_rowcol)   ) ;
00918 
00919    NORMAL_cursorize( grapher->fdw_graph ) ;
00920 
00921    grapher->valid = 2 ;
00922 #ifdef USE_OPTMENUS
00923    GRA_fix_optmenus( grapher ) ;
00924 #endif
00925 #endif
00926 
00927    grapher->fd_pxWind = (Pixmap) 0 ;
00928 
00929    
00930 
00931    XmAddWMProtocolCallback(
00932         grapher->fdw_graph , XmInternAtom(dc->display,"WM_DELETE_WINDOW",False) ,
00933         end_fd_graph_CB , (XtPointer) grapher ) ;
00934 
00935    RETURN(grapher) ;
00936 }
00937 
00938 
00939 
00940 
00941 
00942 void end_fd_graph_CB( Widget w , XtPointer client_data , XtPointer call_data )
00943 {
00944    MCW_grapher * grapher = (MCW_grapher *) client_data ;
00945    int ii ;
00946 
00947 ENTRY("end_fd_graph_CB") ;
00948 
00949    if( ! GRA_VALID(grapher) ) EXRETURN ;
00950 
00951    GRA_timer_stop( grapher ) ;  
00952 
00953    grapher->valid = 0 ;  
00954 
00955    if( grapher->fd_pxWind != (Pixmap) 0 ){
00956 STATUS("freeing Pixmap") ;
00957      XFreePixmap( grapher->dc->display , grapher->fd_pxWind ) ;
00958    }
00959 
00960 #ifdef USE_OPTMENUS
00961 STATUS("destroying optmenus") ;
00962    FREE_AV(grapher->opt_mat_choose_av) ;
00963    FREE_AV(grapher->opt_slice_choose_av) ;
00964    FREE_AV(grapher->fmenu->fim_ignore_choose_av) ;
00965    FREE_AV(grapher->fmenu->fim_polort_choose_av) ;
00966 #endif
00967 
00968 STATUS("destroying arrowvals") ;
00969    FREE_AV( grapher->setshift_right_av)    ;
00970    FREE_AV( grapher->setshift_left_av)     ;
00971    FREE_AV( grapher->setshift_inc_av)      ;
00972    FREE_AV( grapher->transform0D_av )      ;  
00973    FREE_AV( grapher->transform1D_av )      ;  
00974    FREE_AV( grapher->opt_ggap_av )         ;  
00975    FREE_AV( grapher->opt_gthick_av )       ;  
00976 
00977    for( ii=0 ; ii < NUM_COLOR_ITEMS ; ii++ )  
00978      FREE_AV( grapher->opt_color_av[ii] ) ;
00979 
00980 STATUS("destroying fmenu") ;
00981    myXtFree( grapher->fmenu->fim_editref_winaver_bbox );  
00982    myXtFree( grapher->fmenu->fim_opt_bbox ) ;  
00983    myXtFree( grapher->fmenu->fimp_opt_bbox );  
00984    myXtFree( grapher->fmenu->fimp_user_bbox);  
00985    myXtFree( grapher->fmenu )               ;
00986    myXtFree( grapher->cen_line )            ;
00987 
00988 STATUS("destroying bboxes") ;
00989    myXtFree( grapher->opt_dplot_bbox ) ;         
00990 
00991    for( ii=0 ; ii < NUM_COLOR_ITEMS ; ii++ ){
00992      myXtFree( grapher->opt_thick_bbox[ii] ) ;   
00993      myXtFree( grapher->opt_points_bbox[ii] ) ;  
00994    }
00995 
00996    myXtFree( grapher->opt_baseline_bbox ) ;      
00997    myXtFree( grapher->opt_textgraph_bbox ) ;
00998 
00999 STATUS("freeing cen_tsim") ;
01000    mri_free( grapher->cen_tsim ) ;
01001    mri_free( grapher->xax_tsim ) ;  
01002    mri_free( grapher->ave_tsim ) ;  
01003 
01004 STATUS("freeing tuser") ;
01005    GRA_CLEAR_tuser( grapher ) ;  
01006 
01007    
01008 
01009 
01010 
01011 
01012 STATUS("destroying widgets") ;
01013 #ifdef DARWIN
01014    if( grapher->tschosen ) XtUnrealizeWidget( grapher->fdw_graph ) ;
01015    else                    XtDestroyWidget  ( grapher->fdw_graph ) ;
01016 #else
01017    XtUnrealizeWidget( grapher->fdw_graph ) ;
01018 #endif
01019 STATUS("widgets now destroyed") ;
01020 
01021 
01022 
01023    if( grapher->status->send_CB != NULL ){
01024       GRA_cbs cbs ;
01025       cbs.reason = graCR_destroy ;
01026 STATUS("calling AFNI") ;
01027 #if 0
01028       grapher->status->send_CB( grapher , grapher->getaux , &cbs ) ;
01029 #else
01030       CALL_sendback( grapher , cbs ) ;
01031 #endif
01032    } else {
01033 STATUS("freeing grapher") ;
01034       myXtFree( grapher ) ;    
01035    }
01036 
01037    EXRETURN ;
01038 }
01039 
01040 
01041 
01042 
01043 
01044 void erase_fdw( MCW_grapher *grapher )
01045 {
01046 ENTRY("erase_fdw") ;
01047 
01048    if( grapher->dont_redraw ) EXRETURN ;  
01049 
01050    DC_fg_color ( grapher->dc , BG_COLOR(grapher) ) ;
01051    DC_linewidth( grapher->dc , 0 ) ;
01052 
01053    XFillRectangle( grapher->dc->display ,
01054                    grapher->fd_pxWind , grapher->dc->myGC ,
01055                    0 , 0 , grapher->fWIDE , grapher->fHIGH ) ;
01056 
01057    if( show_grapher_pixmap &&
01058        grapher->glogo_pixmap != XmUNSPECIFIED_PIXMAP &&
01059        grapher->glogo_height > 0 && grapher->glogo_width > 0 ){
01060 
01061       XCopyArea( grapher->dc->display ,
01062                  grapher->glogo_pixmap , grapher->fd_pxWind , grapher->dc->myGC ,
01063                  0,0 , grapher->glogo_width,grapher->glogo_height ,
01064                  0,grapher->fHIGH - grapher->glogo_height + 1 ) ;
01065    }
01066 
01067    EXRETURN ;
01068 }
01069 
01070 
01071    
01072    
01073    
01074 
01075 
01076 void plot_fdX( MCW_grapher * grapher , int x , int y , int mod )
01077 {
01078    int iy = grapher->fHIGH - y;
01079 
01080    if( mod > 0 )
01081      XDrawLine( grapher->dc->display ,
01082                 grapher->fd_pxWind , grapher->dc->myGC ,
01083                 grapher->xFD , grapher->yFD , x , iy ) ;
01084 
01085    grapher->xFD = x ; grapher->yFD = iy ;
01086    return ;
01087 }
01088 
01089 
01090 
01091 
01092 
01093 void fd_px_store( MCW_grapher * grapher )
01094 {
01095 ENTRY("fd_px_store") ;
01096 
01097    if( ! MCW_widget_visible(grapher->draw_fd) ) EXRETURN ;  
01098 
01099    XtVaSetValues( grapher->draw_fd ,
01100                      XmNbackgroundPixmap , grapher->fd_pxWind ,
01101                   NULL ) ;
01102 
01103    XClearWindow( grapher->dc->display , XtWindow(grapher->draw_fd) ) ;
01104    XFlush( grapher->dc->display ) ;
01105    EXRETURN ;
01106 }
01107 
01108 
01109 
01110 
01111 
01112 
01113 
01114 
01115 #define NCIR 12  
01116 #define NBAL 21  
01117 #define NBAX 25  
01118 
01119 #define NBTOP NBAX  
01120 
01121 static XPoint xball[] = {
01122       {-1,-2},{ 0,-2},{ 1,-2},
01123       { 2,-1},{ 2, 0},{ 2, 1},
01124       { 1, 2},{ 0, 2},{-1, 2},
01125       {-2, 1},{-2, 0},{-2,-1},  
01126       {-1,-1},{-1, 0},{-1, 1},
01127       { 0,-1},{ 0, 0},{ 0, 1},
01128       { 1,-1},{ 1, 0},{ 1, 1},  
01129       { 0,-3},{ 0, 3},{ 3, 0},
01130       {-3, 0}                   
01131  } ;
01132 
01133 
01134 
01135 void GRA_small_circle( MCW_grapher *grapher, int xwin, int ywin, int filled )
01136 {
01137    int  i, ncirc ;
01138    XPoint a[NBTOP] ;
01139 
01140    switch( filled ){
01141       default: ncirc = NCIR ; break ;
01142       case 1:  ncirc = NBAL ; break ;
01143       case 2:  ncirc = NBAX ; break ;
01144    }
01145 
01146    for( i=0 ; i < ncirc ; i++ ){
01147       a[i].x = xball[i].x + xwin ;
01148       a[i].y = xball[i].y + ywin ;
01149    }
01150 
01151    XDrawPoints( grapher->dc->display, grapher->fd_pxWind,
01152                 grapher->dc->myGC, a, ncirc, CoordModeOrigin ) ;
01153    return ;
01154 }
01155 
01156 
01157 
01158 void GRA_overlay_circle( MCW_grapher *grapher, int xwin, int ywin, int filled )
01159 {
01160    int  i, ncirc ;
01161    XPoint a[NBTOP] ;
01162 
01163    switch( filled ){
01164       default: ncirc = NCIR ; break ;
01165       case 1:  ncirc = NBAL ; break ;
01166       case 2:  ncirc = NBAX ; break ;
01167    }
01168 
01169    for( i=0 ; i < ncirc ; i++ ){
01170       a[i].x = xball[i].x + xwin ;
01171       a[i].y = xball[i].y + ywin ;
01172    }
01173 
01174    DC_linewidth( grapher->dc, 0 ) ;
01175 
01176    XDrawPoints( grapher->dc->display, XtWindow(grapher->draw_fd),
01177                 grapher->dc->myGC, a, ncirc, CoordModeOrigin ) ;
01178    return ;
01179 }
01180 
01181 
01182 
01183 void GRA_draw_circle( MCW_grapher *grapher , int xc , int yc , int rad )
01184 {
01185    int xb,yb ;
01186    unsigned int ww ;
01187 
01188    if( rad < 0 ) rad = 0 ;
01189    xb = xc-rad ; yb = yc-rad ; ww = 2*rad ;
01190    XDrawArc( grapher->dc->display , XtWindow(grapher->draw_fd) ,
01191              grapher->dc->myGC , xb,yb , ww,ww , 0,360*64 ) ;
01192 }
01193 
01194 
01195 
01196 
01197 
01198 
01199 #define SHORT_NAME_WIDTH 384
01200 
01201 static char * long_index_name  = "index="  ;
01202 static char * short_index_name = "#"       ;
01203 static char * long_value_name  = " value=" ;
01204 static char * short_value_name = "="       ;
01205 static char * long_time_name   = " at "    ;
01206 static char * short_time_name  = "@"       ;
01207 
01208 void GRA_redraw_overlay( MCW_grapher * grapher )
01209 {
01210    Window    win ;
01211    Display * dis ;
01212    int       ii , xxx , jj ;
01213    float     val ;
01214    char buf[16] , strp[128] ;
01215    char * vbuf , *iname , *vname ;
01216 
01217 ENTRY("GRA_redraw_overlay") ;
01218 
01219    if( ! GRA_REALZ(grapher) ){ STATUS("ILLEGAL CALL") ; EXRETURN ; }
01220 
01221    if( ! MCW_widget_visible(grapher->draw_fd) ) EXRETURN ;  
01222    if( grapher->dont_redraw ) EXRETURN ;                    
01223 
01224    
01225 
01226    dis = grapher->dc->display ;
01227    win = XtWindow(grapher->draw_fd) ;
01228    XClearWindow( dis , win ) ;
01229 
01230    EXRONE(grapher) ;  
01231 
01232    
01233 
01234    if( grapher->init_ignore > 0 && !grapher->textgraph ){
01235      DC_fg_color( grapher->dc , IGNORE_COLOR(grapher) ) ;
01236      jj  = NBOT(grapher) ;                     
01237      xxx = NTOP(grapher) ;                     
01238      xxx = MIN (xxx , grapher->init_ignore) ;  
01239      xxx = MIN (xxx , ii+grapher->nncen) ;     
01240      for( ii=jj ; ii < xxx ; ii++ )
01241 #if 0
01242        GRA_overlay_circle( grapher , grapher->cen_line[ii-jj].x ,
01243                                      grapher->cen_line[ii-jj].y , 1 ) ;
01244 #else
01245        GRA_draw_circle( grapher , grapher->cen_line[ii-jj].x ,
01246                                   grapher->cen_line[ii-jj].y , 4 ) ;
01247 #endif
01248    }
01249 
01250    
01251 
01252 
01253    ii = grapher->time_index ; jj = NBOT(grapher) ;
01254    if( ii >= jj            &&
01255        ii <  NTOP(grapher) && ii-jj < grapher->nncen && !grapher->textgraph ){
01256 
01257       DC_fg_color( grapher->dc , IDEAL_COLOR(grapher) ) ;
01258       GRA_overlay_circle( grapher , grapher->cen_line[ii-jj].x ,
01259                                     grapher->cen_line[ii-jj].y , 2 ) ;
01260    }
01261 
01262    
01263 
01264    if( ii >= 0 && grapher->cen_tsim != NULL && ii < grapher->cen_tsim->nx ){
01265       val = MRI_FLOAT_PTR(grapher->cen_tsim)[ii] ;
01266       AV_fval_to_char( val , buf ) ;
01267       vbuf = (buf[0]==' ') ? buf+1 : buf ;
01268 
01269       if( grapher->fWIDE < SHORT_NAME_WIDTH ){
01270         iname = short_index_name ; vname = short_value_name ;
01271       } else {
01272         iname = long_index_name ; vname = long_value_name ;
01273       }
01274 
01275       sprintf( strp , "%s%d%s%s" , iname,ii , vname,vbuf ) ;
01276 
01277       if( grapher->cen_tsim->dx != 0.0 ){
01278         val = grapher->cen_tsim->xo + ii * grapher->cen_tsim->dx ;
01279         AV_fval_to_char( val , buf ) ;
01280         vbuf = (buf[0]==' ') ? buf+1 : buf ;
01281         ii = strlen(strp) ;
01282         sprintf( strp+ii , "%s%s" ,
01283                  (grapher->fWIDE < SHORT_NAME_WIDTH) ? short_time_name
01284                                                      : long_time_name, vbuf ) ;
01285       }
01286 
01287       xxx = MAX( grapher->xx_text_2 ,
01288                  grapher->xorigin[grapher->xc][grapher->yc] ) ;
01289 
01290       if( grapher->init_ignore > 0 ) xxx = MAX( xxx , grapher->xx_text_2p ) ;
01291 
01292       DC_fg_color( grapher->dc , IDEAL_COLOR(grapher) ) ;
01293       overlay_txt( grapher, xxx , GB_DLY-15 , strp ) ;
01294    }
01295 
01296    
01297 
01298    XFlush( dis ) ;
01299    EXRETURN ;
01300 }
01301 
01302 
01303 
01304 
01305 
01306 
01307 
01308 
01309 void redraw_graph( MCW_grapher *grapher , int code )
01310 {
01311    int x, y , www,xxx , rrr ;
01312    int xc = grapher->xc , yc = grapher->yc ;
01313    char strp[256] , buf[64] ;
01314    int xd,yd,zd ;
01315 
01316 ENTRY("redraw_graph") ;
01317 
01318    if( ! GRA_REALZ(grapher) ){ STATUS("ILLEGAL ENTRY"); EXRETURN; }
01319    if( grapher->fd_pxWind == (Pixmap) 0 ){ STATUS("ILLEGAL ENTRY"); EXRETURN; }
01320    if( grapher->dont_redraw ) EXRETURN ;  
01321 
01322    
01323 
01324    erase_fdw  ( grapher ) ;
01325    draw_grids ( grapher ) ;
01326    plot_graphs( grapher , code ) ;
01327 
01328    DC_fg_color( grapher->dc , TEXT_COLOR(grapher) ) ;
01329 
01330    if( TPTS(grapher) < 2 ){             
01331       fd_txt( grapher , GL_DLX+5, 35,
01332               "Can't draw graphs for this dataset: Num < 2" ) ;
01333       fd_px_store( grapher ) ;
01334       EXRETURN ;
01335    }
01336 
01337    
01338 
01339    DC_fg_color( grapher->dc , TEXT_COLOR(grapher) ) ;
01340 
01341    
01342 
01343    if( !grapher->textgraph ){
01344       AV_fval_to_char( grapher->pmax[xc][yc] , strp ) ;
01345       www = DC_text_width(grapher->dc,strp) ;
01346       xxx = GL_DLX - www - 2 ;
01347       xxx = MAX(0,xxx) ;
01348       fd_txt( grapher , xxx , GB_DLY + grapher->gy_max - MYTXT, strp) ;
01349 
01350       AV_fval_to_char( grapher->pmax[xc][yc] - grapher->pmin[xc][yc] , buf ) ;
01351       if( buf[0] == ' ' ) buf[0] = '+' ;
01352       sprintf( strp , "[%s]" , buf ) ;
01353       www = DC_text_width(grapher->dc,strp) ;
01354       xxx = GL_DLX - www + 2 ;
01355       xxx = MAX(0,xxx) ;
01356       fd_txt( grapher , xxx , GB_DLY + grapher->gy_max - MYTXT - 14 , strp) ;
01357 
01358       AV_fval_to_char( grapher->pmin[xc][yc] , strp ) ;
01359       www = DC_text_width(grapher->dc,strp) ;
01360       xxx = GL_DLX - www - 2 ;
01361       xxx = MAX(0,xxx) ;
01362       fd_txt( grapher , xxx , GB_DLY + 5, strp) ;
01363    }
01364 
01365    
01366 
01367    
01368 
01369    grapher->xx_text_1 = GL_DLX+5 ;
01370 
01371    xd = grapher->xpoint ; yd = grapher->ypoint ; zd = grapher->zpoint ;
01372 #ifndef DONT_MANGLE_XYZ
01373    { THD_ivec3 id ;
01374      id = THD_fdind_to_3dind( grapher->getaux , TEMP_IVEC3(xd,yd,zd) ) ;
01375      xd = id.ijk[0] ; yd = id.ijk[1] ; zd = id.ijk[2] ; }
01376 #endif
01377 
01378    sprintf(strp,"X: %d", xd) ;
01379    fd_txt( grapher , GL_DLX+5 , 35, strp) ;
01380    xxx = DC_text_width(grapher->dc,strp) ;
01381 
01382    sprintf(strp,"Y: %d", yd) ;
01383    fd_txt( grapher , GL_DLX+5 , 21, strp) ;
01384    www = DC_text_width(grapher->dc,strp) ; xxx = MAX(xxx,www) ;
01385 
01386    if( grapher->status->nz > 1 ){
01387       sprintf(strp,"Z: %d", zd) ;
01388       fd_txt( grapher , GL_DLX+5 ,  7, strp) ;
01389       www = DC_text_width(grapher->dc,strp) ; xxx = MAX(xxx,www) ;
01390    }
01391 
01392    
01393 
01394    grapher->xx_text_2 = xxx = xxx + GL_DLX + 15 ;
01395 
01396    DC_linewidth( grapher->dc , 0 ) ;
01397    fd_line( grapher , xxx-7 , 41 , xxx-7 , 5 ) ;
01398 
01399    if( grapher->init_ignore > 0 ){                    
01400      sprintf(strp,"Ignore%4d",grapher->init_ignore) ;
01401      fd_txt( grapher , xxx , 35, strp) ;
01402    }
01403 
01404    sprintf(strp,"Grid:%5d", grapher->grid_spacing ) ;
01405    rrr = DC_text_width(grapher->dc,strp) ;
01406 
01407    if( !grapher->textgraph ){
01408       if( grapher->fscale > 0 ){                        
01409          AV_fval_to_char( grapher->fscale , buf ) ;     
01410          www = strlen(strp) ;
01411          sprintf(strp+www," Scale:%s pix/datum",buf) ;
01412       } else if( grapher->fscale < 0 ){
01413          AV_fval_to_char( -grapher->fscale , buf ) ;
01414          www = strlen(strp) ;
01415          sprintf(strp+www," Scale:%s datum/pix",buf) ;
01416       }
01417    }
01418 
01419    fd_txt( grapher , xxx , 21, strp) ;
01420 
01421    xxx = DC_text_width(grapher->dc,strp) ;           
01422 
01423    { int bb=TBOT(grapher) , tt=TTOP(grapher)-1 ;
01424      if( bb > 99 || tt > 99 )
01425        sprintf(strp,"#%4d:%-4d" , bb,tt ) ;
01426      else
01427        sprintf(strp,"Num%3d:%-3d" , bb,tt ) ;
01428    }
01429    fd_line( grapher ,
01430             grapher->xx_text_2+rrr+3 , (grapher->init_ignore > 0) ? 41 : 31 ,
01431             grapher->xx_text_2+rrr+3 , 5 ) ;
01432 
01433    grapher->xx_text_2p = grapher->xx_text_2+rrr+7 ;  
01434 
01435    if( !grapher->textgraph ){
01436      switch( grapher->common_base ){
01437        default:
01438        case BASELINE_INDIVIDUAL:
01439          strcat(strp,"  Base: separate") ; break ;
01440 
01441        case BASELINE_COMMON:
01442          strcat(strp,"  Base: common") ; break ;
01443 
01444        case BASELINE_GLOBAL:
01445          strcat(strp,"  Base: global") ; break ;
01446      }
01447    }
01448 
01449    fd_txt( grapher , grapher->xx_text_2 ,  7, strp ) ;
01450 
01451    
01452 
01453    www = DC_text_width(grapher->dc,strp) ; xxx = MAX(xxx,www) ;
01454 
01455    grapher->xx_text_3 = grapher->xx_text_2 + xxx + 15 ;
01456 
01457    if( !grapher->textgraph && !ISONE(grapher) ){
01458       sprintf(strp,"Mean: %10s", MV_format_fval(grapher->tmean[xc][yc]) ) ;
01459 
01460       fd_txt( grapher , grapher->xx_text_3 ,  21, strp ) ;
01461 
01462       sprintf(strp,"Sigma:%10s", MV_format_fval(grapher->tstd[xc][yc]) ) ;
01463 
01464       fd_txt( grapher , grapher->xx_text_3 ,   7, strp ) ;
01465 
01466       fd_line( grapher , grapher->xx_text_3-7 , 31 , grapher->xx_text_3-7 , 5 ) ;
01467    }
01468 
01469    
01470 
01471    fd_px_store( grapher ) ;
01472 
01473    
01474 
01475    GRA_redraw_overlay( grapher ) ;
01476 
01477 #ifdef USE_OPTMENUS
01478    GRA_fix_optmenus( grapher ) ;
01479 #endif
01480 
01481 
01482 
01483    if( MCW_val_bbox(grapher->fmenu->fim_editref_winaver_bbox) )
01484      GRA_winaver_setref( grapher ) ;
01485 
01486    grapher->never_drawn = 0 ;
01487    EXRETURN ;
01488 }
01489 
01490 
01491 
01492 
01493 
01494 
01495 void fd_txt( MCW_grapher * grapher , int x , int y , char * str )
01496 {
01497    XDrawString( grapher->dc->display, grapher->fd_pxWind,
01498                 grapher->dc->myGC , x , grapher->fHIGH-y ,
01499                 str , strlen(str) ) ;
01500    return ;
01501 }
01502 
01503 
01504 
01505 void overlay_txt( MCW_grapher * grapher , int x , int y , char * str )
01506 {
01507    XDrawString( grapher->dc->display, XtWindow(grapher->draw_fd) ,
01508                 grapher->dc->myGC , x , grapher->fHIGH-y ,
01509                 str , strlen(str) ) ;
01510    return ;
01511 }
01512 
01513 
01514 
01515 static void fd_line( MCW_grapher *grapher , int x1,int y1, int x2,int y2 )
01516 {
01517    XDrawLine( grapher->dc->display , grapher->fd_pxWind ,
01518               grapher->dc->myGC , x1,grapher->fHIGH-y1,x2,grapher->fHIGH-y2 ) ;
01519    return ;
01520 }
01521 
01522 
01523 
01524 #define GRID_MAX 12
01525 static int grid_ar[GRID_MAX] =
01526    { 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000 } ;
01527 
01528 static void auto_grid( MCW_grapher *grapher , int npoints )
01529 {
01530    int ii ;
01531    if( npoints < 2 ) return ;            
01532    for( ii=GRID_MAX-1 ; ii > 0 ; ii-- )
01533      if( grid_ar[ii] <= npoints/3 ) break;
01534    grapher->grid_index   = ii ;
01535    grapher->grid_spacing = grid_ar[ii] ;
01536    grapher->grid_fixed   = 0 ;           
01537    return ;
01538 }
01539 
01540 
01541 
01542 void init_const( MCW_grapher *grapher )
01543 {
01544    int ii ;
01545 
01546 ENTRY("init_const") ;
01547 
01548    if( !GRA_VALID(grapher) ) EXRETURN ;
01549 
01550    if( grapher->fscale == 0 ) grapher->fscale = 1 ;
01551 
01552    grapher->mat_max = MAT_MAX ;
01553    grapher->mat_max = MIN( grapher->mat_max , grapher->status->nx ) ;
01554    grapher->mat_max = MIN( grapher->mat_max , grapher->status->ny ) ;
01555 
01556    if( grapher->mat <= 0 ) grapher->mat = INIT_GR_gmat ;
01557    grapher->mat = MIN( grapher->mat , grapher->mat_max ) ;
01558 
01559    if( grapher->xpoint < 0 || grapher->xpoint >= grapher->status->nx )
01560       grapher->xpoint = grapher->status->nx / 2 ;
01561 
01562    if( grapher->ypoint < 0 || grapher->ypoint >= grapher->status->ny )
01563       grapher->ypoint = grapher->status->ny / 2 ;
01564 
01565    if( grapher->zpoint < 0 || grapher->zpoint >= grapher->status->nz )
01566       grapher->zpoint = grapher->status->nz / 2 ;
01567 
01568    if( grapher->grid_index < 0 ) auto_grid( grapher, NPTS(grapher) ) ;
01569 
01570 #if 0
01571    if( grapher->grid_color < 0 )
01572       grapher->grid_color = 1 ;  
01573 #endif
01574 
01575    if( grapher->time_index < 0 )
01576      grapher->time_index = 0 ;
01577    else if( grapher->time_index >= grapher->status->num_series )
01578      grapher->time_index = grapher->status->num_series - 1 ;
01579 
01580    init_mat(grapher) ;
01581    EXRETURN ;
01582 }
01583 
01584 
01585 
01586 
01587 
01588 void text_graphs( MCW_grapher *grapher )
01589 {
01590    MRI_IMAGE *tsim ;
01591    int index, ix, iy, xtemp,ytemp,ztemp , xoff,yoff ;
01592    int iv , jv , www ;
01593    char str[64] , *strp ;
01594 
01595 ENTRY("text_graphs") ;
01596    if( grapher->dont_redraw ) EXRETURN ;  
01597 
01598    DC_fg_color( grapher->dc , TEXT_COLOR(grapher) ) ;
01599 
01600    iv = grapher->time_index ;
01601    if( iv < 0 )
01602      iv = 0 ;
01603    else if( iv >= grapher->status->num_series )
01604      iv = grapher->status->num_series - 1 ;
01605 
01606    ztemp = grapher->zpoint * grapher->status->ny * grapher->status->nx ;
01607 
01608    for( ix=0 ; ix < grapher->mat ; ix++ ){
01609       xtemp  = grapher->xpoint + ix - grapher->xc ;
01610            if( xtemp <  0                   ) xtemp += grapher->status->nx ;
01611       else if( xtemp >= grapher->status->nx ) xtemp -= grapher->status->nx ;
01612 
01613       for( iy=0 ; iy < grapher->mat ; iy++ ){
01614          ytemp = grapher->ypoint - iy + grapher->yc ;
01615               if( ytemp <  0                   ) ytemp += grapher->status->ny ;
01616          else if( ytemp >= grapher->status->ny ) ytemp -= grapher->status->ny ;
01617 
01618          index = ztemp + ytemp * grapher->status->nx + xtemp ;
01619 
01620 #if 0
01621          tsim  = (MRI_IMAGE *) grapher->getser( index , graCR_getseries ,
01622                                                         grapher->getaux ) ;
01623 #else
01624          CALL_getser( grapher , index,graCR_getseries , MRI_IMAGE *,tsim ) ;
01625 #endif
01626 
01627          if( tsim == NULL ) break ;
01628          if( tsim->nx < 1 ){ mri_free(tsim); break; }  
01629 
01630          if( tsim->kind != MRI_float ){
01631            MRI_IMAGE *qim = mri_to_float(tsim) ;
01632            mri_free(tsim) ; tsim = qim ;
01633          }
01634 
01635          if( ix == grapher->xc && iy == grapher->yc ){
01636            mri_free( grapher->cen_tsim ) ;             
01637            grapher->cen_tsim = mri_to_float( tsim ) ;
01638          }
01639 
01640 #if 0
01641          if( grapher->transform0D_func != NULL )
01642 # if 0
01643             grapher->transform0D_func( tsim->nx , MRI_FLOAT_PTR(tsim) ) ;
01644 # else
01645             AFNI_CALL_0D_function( grapher->transform0D_func ,
01646                                    tsim->nx , MRI_FLOAT_PTR(tsim) ) ;
01647 # endif
01648 #endif
01649 
01650          jv = iv ; if( jv >= tsim->nx ) jv = tsim->nx - 1 ;
01651          AV_fval_to_char( MRI_FLOAT_PTR(tsim)[jv] , str ) ;
01652          mri_free(tsim) ;
01653          strp = (str[0] == ' ') ? str+1 : str ;
01654          www = DC_text_width(grapher->dc,strp) ;
01655 
01656          fd_txt( grapher , grapher->xorigin[ix][iy] + (grapher->gx-www)/2 ,
01657                            grapher->yorigin[ix][iy] + 2 ,
01658                  strp ) ;
01659       }
01660    }
01661 
01662    EXRETURN ;
01663 }
01664 
01665 
01666 
01667 
01668 
01669 void plot_graphs( MCW_grapher *grapher , int code )
01670 {
01671    MRI_IMAGE *tsim ;
01672    MRI_IMARR *tsimar ;
01673    float     *tsar ;
01674    float       tsbot=0.0 , ftemp , tstop ;
01675    int i, m, index, ix, iy, xtemp,ytemp,ztemp , xoff,yoff , its,ibot,itop;
01676    int ptop,pbot,pnum,qnum , tbot,ttop,tnum , ntmax ;  
01677 
01678    static int      *plot = NULL ;  
01679    static XPoint *a_line = NULL ;
01680    static int      nplot = 0 ;
01681 
01682    MRI_IMARR *dplot_imar = NULL ;  
01683    int        dplot = 0 ;
01684 
01685    MRI_IMARR *eximar ;
01686    int        iex ;
01687 
01688    float nd_bot , nd_top , nd_dif ;                        
01689    int   set_scale = ( (code & PLOTCODE_AUTOSCALE) != 0 ||
01690                        grapher->never_drawn ) ;
01691 
01692    MRI_IMAGE *dsim ; 
01693    float     *dsar ;
01694 
01695 #define OVI_MAX 19
01696    int tt, use_ovi, ovi[OVI_MAX] ;  
01697 
01698 ENTRY("plot_graphs") ;
01699    if( grapher->dont_redraw ) EXRETURN ;  
01700 
01701    
01702 
01703    if( grapher->status->num_series < 1 ){
01704      EXRETURN ;
01705    } else if( grapher->status->num_series == 1 ||
01706               grapher->textgraph               || TPTS(grapher) < 2 ){
01707      text_graphs( grapher ) ;
01708      EXRETURN ;
01709    }
01710 
01711    GRA_fixup_xaxis( grapher ) ;   
01712 
01713    
01714 
01715    DC_fg_color ( grapher->dc , DATA_COLOR(grapher) ) ;
01716    DC_linewidth( grapher->dc , DATA_THICK(grapher) ) ;
01717 
01718    
01719 
01720    ptop = NTOP(grapher) ; pbot = NBOT(grapher) ;
01721    if( pbot >= ptop ){
01722      pbot = 0 ; ptop = grapher->status->num_series ;
01723    }
01724    ttop = TTOP(grapher) ; ttop = MIN(ttop,ptop) ; tbot = pbot ;
01725    if( ttop <= tbot || ttop > grapher->status->num_series ){
01726      ttop = MIN(ptop,grapher->status->num_series) ;
01727    }
01728    pnum = ptop-pbot ; tnum = ttop-tbot ;
01729    if( pnum <= 1 || tnum <= 1 ) EXRETURN ;   
01730 
01731    
01732 
01733 #define NPLOT_INIT 9999  
01734    itop = MAX( NPLOT_INIT , grapher->status->num_series ) ;
01735    if( nplot == 0 || nplot < itop ){
01736      myXtFree(a_line) ; myXtFree(plot) ;
01737      nplot  = itop ;
01738      plot   = (int *)    XtMalloc( sizeof(int)    * itop ) ;
01739      a_line = (XPoint *) XtMalloc( sizeof(XPoint) * itop ) ;
01740    }
01741    if( grapher->ncen_line < itop ){
01742      myXtFree(grapher->cen_line) ;
01743      grapher->cen_line  = (XPoint *) XtMalloc( sizeof(XPoint) * itop ) ;
01744      grapher->ncen_line = itop ;
01745    }
01746 
01747    
01748 
01749    ibot = grapher->init_ignore ;
01750    if( ibot >= ttop-1 ) ibot = 0 ;
01751    ibot = MAX(ibot,tbot) ;                     
01752 
01753 
01754 
01755    INIT_IMARR(tsimar) ;
01756 
01757 
01758 
01759 
01760    if( grapher->transform1D_func != NULL &&
01761        MCW_val_bbox(grapher->opt_dplot_bbox) != DPLOT_OFF ){
01762 
01763      INIT_IMARR(dplot_imar) ;
01764      dplot = MCW_val_bbox(grapher->opt_dplot_bbox) ; 
01765    }
01766 
01767    GRA_CLEAR_tuser( grapher ) ;  
01768 
01769    
01770    ztemp = grapher->zpoint * grapher->status->ny * grapher->status->nx ;
01771 
01772    ntmax = 0 ; 
01773 
01774    for( ix=0 ; ix < grapher->mat ; ix++ ){
01775 
01776 
01777 
01778       xtemp  = grapher->xpoint + ix - grapher->xc ;
01779            if( xtemp <  0                   ) xtemp += grapher->status->nx ;  
01780       else if( xtemp >= grapher->status->nx ) xtemp -= grapher->status->nx ;
01781 
01782       for( iy=0 ; iy < grapher->mat ; iy++ ){
01783          ytemp = grapher->ypoint - iy + grapher->yc ;
01784               if( ytemp <  0                   ) ytemp += grapher->status->ny;  
01785          else if( ytemp >= grapher->status->ny ) ytemp -= grapher->status->ny;
01786 
01787          index = ztemp + ytemp * grapher->status->nx + xtemp ;  
01788 
01789 
01790 
01791 #if 0
01792          tsim  = (MRI_IMAGE *) grapher->getser( index , graCR_getseries ,
01793                                                         grapher->getaux ) ;
01794 #else
01795          CALL_getser( grapher , index,graCR_getseries , MRI_IMAGE *,tsim ) ;
01796 #endif
01797 
01798          
01799 
01800          if( tsim == NULL ){
01801            ADDTO_IMARR(tsimar,NULL) ;
01802            if( dplot ) ADDTO_IMARR(dplot_imar,NULL) ;
01803            continue ;  
01804          }
01805 
01806          ntmax = MAX( ntmax , tsim->nx ) ;
01807 
01808 
01809 
01810          if( tsim->kind != MRI_float ){
01811            MRI_IMAGE *qim = mri_to_float(tsim) ;
01812            mri_free(tsim) ; tsim = qim ;
01813          }
01814 
01815          
01816 
01817          if( grapher->transform0D_func != NULL ){
01818 STATUS("about to perform 0D transformation") ;
01819 #if 0
01820             grapher->transform0D_func( tsim->nx , MRI_FLOAT_PTR(tsim) ) ;
01821 #else
01822             AFNI_CALL_0D_function( grapher->transform0D_func ,
01823                                    tsim->nx , MRI_FLOAT_PTR(tsim) ) ;
01824 #endif
01825          }
01826 
01827          
01828          
01829 
01830          if( grapher->transform1D_func != NULL ){
01831             MRI_IMAGE * qim ;                
01832 
01833             if( dplot ){                      
01834               qim = mri_to_float(tsim) ;       
01835               ADDTO_IMARR(dplot_imar,qim) ;
01836             }
01837             else
01838               qim = tsim ;                 
01839 
01840 STATUS("about to perform 1D transformation") ;
01841 
01842             if( grapher->transform1D_flags & NEEDS_DSET_INDEX ){ 
01843 #ifdef BE_AFNI_AWARE
01844                FD_brick *br = (FD_brick *) grapher->getaux ;
01845                THD_ivec3 id ;
01846                id = THD_fdind_to_3dind( br ,
01847                                         TEMP_IVEC3(xtemp,ytemp,grapher->zpoint) );
01848                AFNI_store_dset_index(
01849                              id.ijk[0]
01850                             +id.ijk[1] * br->nxyz.ijk[0]
01851                             +id.ijk[2] * br->nxyz.ijk[0] * br->nxyz.ijk[1] , 0 ) ;
01852 #else
01853                AFNI_store_dset_index(-1,0) ;
01854 #endif
01855             }
01856 
01857             if( ! (grapher->transform1D_flags & PROCESS_MRI_IMAGE) ){  
01858                                                                        
01859               if( ! (grapher->transform1D_flags & RETURNS_STRING) ){   
01860 #if 0
01861                  grapher->transform1D_func( qim->nx , qim->xo , qim->dx ,
01862                                             MRI_FLOAT_PTR(qim) ) ;
01863 #else
01864                  AFNI_CALL_1D_function( grapher->transform1D_func ,
01865                                         qim->nx , qim->xo , qim->dx ,
01866                                         MRI_FLOAT_PTR(qim) ) ;
01867 #endif
01868               } else {
01869                  char * quser = NULL ;
01870 #if 0
01871                  grapher->transform1D_func( qim->nx , qim->xo , qim->dx ,
01872                                             MRI_FLOAT_PTR(qim) , &quser ) ;
01873 #else
01874                  AFNI_CALL_1D_funcstr( grapher->transform1D_func ,
01875                                        qim->nx , qim->xo , qim->dx ,
01876                                        MRI_FLOAT_PTR(qim) , quser ) ;
01877 #endif
01878                  if( quser != NULL )
01879                    grapher->tuser[ix][iy] = XtNewString(quser) ;
01880               }
01881             } else {                           
01882                                                                             
01883               if( ! (grapher->transform1D_flags & RETURNS_STRING) ){
01884 #if 0
01885                  grapher->transform1D_func( qim ) ;
01886 #else
01887                  AFNI_CALL_1D_funcmrim( grapher->transform1D_func , qim ) ;
01888 #endif
01889               } else {
01890                  char *quser = NULL ;
01891 #if 0
01892                  grapher->transform1D_func( qim , &quser ) ;
01893 #else
01894                  AFNI_CALL_1D_funcmrimstr( grapher->transform1D_func , qim,quser ) ;
01895 #endif
01896                  if( quser != NULL )
01897                    grapher->tuser[ix][iy] = XtNewString(quser) ;
01898               }
01899             }
01900 
01901             
01902 
01903 
01904 
01905          } 
01906 
01907          
01908 
01909          ADDTO_IMARR(tsimar,tsim) ;
01910       }
01911    }
01912 
01913 
01914 
01915    if( ntmax > 1 && IMARR_COUNT(tsimar) > 0 ){
01916      float *avar , fac ; int nax ;
01917 STATUS("about to make average time series") ;
01918      if( grapher->ave_tsim != NULL ) mri_free(grapher->ave_tsim) ;
01919      grapher->ave_tsim = mri_new( ntmax , 1 , MRI_float ) ;
01920      avar = MRI_FLOAT_PTR(grapher->ave_tsim) ;  
01921      for( ix=0 ; ix < IMARR_COUNT(tsimar) ; ix++ ){
01922        tsim = IMARR_SUBIMAGE(tsimar,ix) ; if( tsim == NULL ) continue ;
01923        tsar = MRI_FLOAT_PTR(tsim)       ; if( tsar == NULL ) continue ;
01924        nax  = MIN( ntmax , tsim->nx ) ;
01925        for( i=0 ; i < nax ; i++ ) avar[i] += tsar[i] ;
01926      }
01927      fac = 1.0 / IMARR_COUNT(tsimar) ;
01928      for( i=0 ; i < grapher->ave_tsim->nx ; i++ ) avar[i] *= fac ;
01929 
01930      if( MCW_val_bbox(grapher->fmenu->fim_editref_winaver_bbox) ){
01931        if( grapher->ref_ts == NULL ) INIT_IMARR( grapher->ref_ts ) ;
01932        if( IMARR_COUNT(grapher->ref_ts) == 0 ){
01933          ADDTO_IMARR( grapher->ref_ts , grapher->ave_tsim ) ;     
01934        } else {
01935          IMARR_SUBIMAGE(grapher->ref_ts,0) = grapher->ave_tsim ;  
01936        }
01937      }
01938    } else if( grapher->ave_tsim != NULL ){
01939      mri_free(grapher->ave_tsim) ; grapher->ave_tsim = NULL ;
01940    }
01941 
01942 
01943 
01944 STATUS("finding statistics of time series") ;
01945 
01946    
01947 
01948    if( set_scale ){
01949      nd_bot = WAY_BIG ; nd_top = nd_dif = - WAY_BIG ;  
01950    }
01951 
01952    for( ix=0,its=0 ; ix < grapher->mat ; ix++ ){
01953       for( iy=0 ; iy < grapher->mat ; iy++,its++ ){
01954          float qbot,qtop ;
01955          double qsum , qsumq ;
01956 
01957          tsim = IMARR_SUBIMAGE(tsimar,its) ;
01958          if( tsim == NULL || tsim->nx < 2 ){
01959            grapher->tmean[ix][iy] = grapher->tbot[ix][iy] =
01960              grapher->ttop[ix][iy] = grapher->tstd[ix][iy] = 0.0 ;
01961            grapher->tmed[ix][iy] = grapher->tmad[ix][iy] = 0.0 ;  
01962            grapher->sbot[ix][iy] = grapher->stop[ix][iy] = 0 ;    
01963            continue ;
01964          }
01965 
01966          tsar = MRI_FLOAT_PTR(tsim) ;     
01967          itop = MIN( ttop , tsim->nx ) ;  
01968 
01969          if( itop-ibot < 2 ){                       
01970            grapher->tmean[ix][iy] = grapher->tbot[ix][iy] =
01971              grapher->ttop[ix][iy] = grapher->tstd[ix][iy] = 0.0 ;
01972            grapher->tmed[ix][iy] = grapher->tmad[ix][iy] = 0.0 ;  
01973            continue ;  
01974          }
01975 
01976          qbot = qtop  = tsar[ibot] ;
01977          qsum = qsumq = 0.0 ;
01978          for( i=ibot ; i < itop ; i++ ){      
01979            qbot   = MIN( qbot , tsar[i] ) ;
01980            qtop   = MAX( qtop , tsar[i] ) ;
01981            qsum  += tsar[i] ;
01982            qsumq += tsar[i] * tsar[i] ;
01983          }
01984          grapher->tbot[ix][iy] = qbot ; grapher->ttop[ix][iy] = qtop ;
01985          grapher->sbot[ix][iy] = ibot ; grapher->stop[ix][iy] = itop-1 ; 
01986          qsum  = qsum / (itop-ibot) ; grapher->tmean[ix][iy] = qsum ;
01987          qsumq = (qsumq - (itop-ibot) * qsum * qsum) / (itop-ibot-1.0) ;
01988          grapher->tstd[ix][iy] = (qsumq > 0.0) ? sqrt(qsumq) : 0.0 ;
01989 
01990          qmedmad_float( itop-ibot , tsar+ibot ,        
01991                         &(grapher->tmed[ix][iy]) ,
01992                         &(grapher->tmad[ix][iy]) ) ;
01993 
01994          if( set_scale ){        
01995 
01996            if( tsim->ny > 1 ){
01997              for( tt=1 ; tt < tsim->ny ; tt++ ){  
01998                tsar += tsim->nx ;                 
01999                for( i=ibot ; i < itop ; i++ ){    
02000                  qbot = MIN( qbot , tsar[i] ) ;
02001                  qtop = MAX( qtop , tsar[i] ) ;
02002                }
02003              }
02004            }
02005 
02006            nd_bot = MIN( nd_bot , qbot ) ;        
02007            nd_top = MAX( nd_top , qtop ) ;
02008            nd_dif = MAX( nd_dif , (qtop-qbot) ) ;
02009          }
02010       }
02011    }
02012 
02013    
02014 
02015    if( set_scale && nd_bot < nd_top && nd_dif > 0.0 ){
02016 
02017       switch( grapher->common_base ){
02018          default:
02019          case BASELINE_INDIVIDUAL:
02020             grapher->fscale = 0.9 * grapher->gy / nd_dif ;          
02021          break ;
02022 
02023          case BASELINE_COMMON:
02024             grapher->fscale = 0.9 * grapher->gy / (nd_top-nd_bot) ; 
02025          break ;
02026 
02027          case BASELINE_GLOBAL:{
02028             float vbot = (nd_top > grapher->global_base)
02029                         ? grapher->global_base : nd_bot ;
02030             grapher->fscale = 0.9 * grapher->gy / (nd_top-vbot) ;
02031          }
02032          break ;
02033       }
02034 
02035       if( grapher->fscale > 0.0 && grapher->fscale < 1.0 )       
02036          grapher->fscale = -1.0 / grapher->fscale ;
02037 
02038            if( grapher->fscale > 4.0 )                           
02039                   grapher->fscale = (int) grapher->fscale ;
02040 
02041       else if( grapher->fscale > 1.0 )
02042                   grapher->fscale = 0.5 * ((int)(2.0*grapher->fscale)) ;
02043 
02044       else if( grapher->fscale < -4.0 )
02045                   grapher->fscale = -((int)(1.0-grapher->fscale)) ;
02046 
02047       else if( grapher->fscale < -1.0 )
02048                   grapher->fscale = -0.5 * ((int)(1.0-2.0*grapher->fscale)) ;
02049    }
02050 
02051 
02052 
02053    if( grapher->common_base == BASELINE_COMMON ){
02054      int first = 1 ;
02055 
02056 STATUS("finding common base") ;
02057 
02058      for( ix=0 ; ix < tsimar->num ; ix++ ){
02059        tsim = IMARR_SUBIMAGE(tsimar,ix) ;
02060        if( tsim == NULL || tsim->nx < 2 ) continue ;   
02061        tsar = MRI_FLOAT_PTR(tsim) ;
02062        itop = MIN( ttop , tsim->nx ) ;
02063        if( first && ibot < itop ){ tsbot = tsar[ibot] ; first = 0 ; }
02064        for( tt=0 ; tt < tsim->ny ; tt++ ){  
02065          for( i=ibot ; i < itop ; i++ ) tsbot = MIN( tsbot , tsar[i] ) ;
02066          tsar += tsim->nx ;
02067        }
02068      }
02069    } else if( grapher->common_base == BASELINE_GLOBAL ){ 
02070      tsbot = grapher->global_base ;
02071    }
02072 
02073    
02074 
02075 STATUS("starting time series graph loop") ;
02076    for( ix=0,its=0 ; ix < grapher->mat ; ix++ ){
02077 
02078       for( iy=0 ; iy < grapher->mat ; iy++,its++ ){
02079 
02080          tsim = IMARR_SUBIMAGE(tsimar,its) ;
02081          if( tsim == NULL || tsim->nx < 2 ) continue ; 
02082          tsar = MRI_FLOAT_PTR(tsim) ;
02083          itop = MIN( ttop , tsim->nx ) ;
02084          qnum = itop - pbot ;          
02085          if( qnum < 2 ) continue ;     
02086 
02087 
02088 
02089          if( grapher->common_base == BASELINE_INDIVIDUAL ){
02090            tsbot = tsar[ibot] ;
02091            for( tt=0 ; tt < tsim->ny ; tt++ ){
02092              for( i=ibot ; i < itop ; i++ ) tsbot = MIN(tsbot,tsar[i]) ;
02093              tsar += tsim->nx ;
02094            }
02095          }
02096          grapher->pmin[ix][iy] = tsbot ;  
02097 
02098 
02099 
02100          use_ovi = (tsim->name!=NULL) && (strncmp(tsim->name,"color: ",7)==0) ;
02101          if( use_ovi ){
02102            char *cpt = tsim->name+6 ; int nuse, ngood ;
02103            for( tt=0 ; tt < OVI_MAX ; tt++ )
02104              ovi[tt] = DATA_COLOR(grapher) ;
02105            for( tt=0 ; tt < OVI_MAX ; tt++ ){
02106              ngood = sscanf(cpt,"%d%n",ovi+tt,&nuse) ;
02107              if( ngood < 1 ) break ;
02108              cpt += nuse ; if( *cpt == '\0' ) break ;
02109            }
02110          }
02111 
02112          tsar = MRI_FLOAT_PTR(tsim) ;
02113          for( tt=0 ; tt < tsim->ny ; tt++ ){  
02114 
02115 
02116 
02117 
02118 
02119           ftemp = grapher->fscale ;
02120                if( ftemp == 0.0 ) ftemp =  1.0 ;
02121           else if( ftemp <  0.0 ) ftemp = -1.0 / ftemp ;
02122 
02123           
02124 
02125           for( i=pbot ; i < MIN(ibot,itop) ; i++ )
02126             plot[i-pbot] = (tsar[ibot] - tsbot) * ftemp ;
02127 
02128           
02129 
02130           for( i=ibot ; i < itop ; i++ )
02131             plot[i-pbot] = (tsar[i] - tsbot) * ftemp ;
02132 
02133           
02134 
02135           grapher->pmax[ix][iy] = tsbot + grapher->gy / ftemp ; 
02136 
02137 
02138 
02139 
02140           ftemp = grapher->gx / (pnum-1.0) ;  
02141 
02142           
02143 
02144 
02145 
02146           xoff  = grapher->xorigin[ix][iy] ;
02147           yoff  = grapher->fHIGH - grapher->yorigin[ix][iy] ;
02148 
02149           
02150 
02151 
02152 #define XPIX(ii)                                                        \
02153    ( (grapher->xax_tsim != NULL && (ii) < grapher->xax_tsim->nx)        \
02154      ? (MRI_FLOAT_PTR(grapher->xax_tsim)[MAX((ii),ibot)] * grapher->gx) \
02155      : (((ii)-pbot) * ftemp) )
02156 
02157           for( i=0 ; i < qnum ; i++ ){         
02158             a_line[i].x = xoff + XPIX(i+pbot);
02159             a_line[i].y = yoff - plot[i] ;
02160           }
02161 
02162           if( use_ovi )                       
02163             DC_fg_color( grapher->dc , ovi[tt%OVI_MAX] ) ;
02164 
02165           if( DATA_POINTS(grapher) ){         
02166             for( i=0 ; i < qnum ; i++ )
02167               GRA_small_circle( grapher,a_line[i].x,a_line[i].y,DATA_IS_THICK(grapher) );
02168           }
02169           if( DATA_LINES(grapher) ){          
02170             XDrawLines( grapher->dc->display ,
02171                         grapher->fd_pxWind , grapher->dc->myGC ,
02172                         a_line , qnum ,  CoordModeOrigin ) ;
02173           }
02174 
02175          
02176 
02177           if( ix == grapher->xc && iy == grapher->yc && tt == 0 ){
02178             for( i=0 ; i < qnum ; i++ ) grapher->cen_line[i] = a_line[i] ;
02179             grapher->nncen = qnum ;
02180             mri_free( grapher->cen_tsim ) ;             
02181             grapher->cen_tsim = mri_to_float( tsim ) ;
02182           }
02183 
02184           tsar += tsim->nx ;  
02185          } 
02186 
02187          if( use_ovi )
02188            DC_fg_color( grapher->dc , DATA_COLOR(grapher) ) ; 
02189 
02190          
02191          
02192 
02193          
02194 
02195          if( dplot ){
02196             int dny , id , qq,qtop ;
02197             dsim = IMARR_SUBIMAGE(dplot_imar,its) ;
02198             if( dsim == NULL || dsim->nx < 2 ) continue ;  
02199             dsar = MRI_FLOAT_PTR(dsim) ;
02200             tsar = MRI_FLOAT_PTR(tsim) ;   
02201             itop = MIN( ttop , dsim->nx ); 
02202             qnum = itop - pbot ;           
02203             if( qnum < 2 ) continue ;      
02204 
02205             if( dplot == DPLOT_PLUSMINUS ) dny = 1 ;        
02206             else                           dny = dsim->ny ;
02207 
02208 
02209 
02210             use_ovi = (dsim->name!=NULL) && (strncmp(dsim->name,"color: ",7)==0) ;
02211             if( use_ovi ){
02212               char *cpt = dsim->name+6 ; int nuse, ngood ;
02213               for( tt=0 ; tt < OVI_MAX ; tt++ )
02214                 ovi[tt] = DPLOT_COLOR(grapher) ;
02215               for( tt=0 ; tt < OVI_MAX ; tt++ ){
02216                 ngood = sscanf(cpt,"%d%n",ovi+tt,&nuse) ;
02217                 if( ngood < 1 ) break ;
02218                 cpt += nuse ; if( *cpt == '\0' ) break ;
02219               }
02220             }
02221 
02222             for( id=0 ; id < dny ; id++ ){          
02223 
02224              ftemp = grapher->fscale ;
02225                   if( ftemp == 0.0 ) ftemp =  1.0 ;
02226              else if( ftemp <  0.0 ) ftemp = -1.0 / ftemp ;
02227 
02228              
02229 
02230              for( qtop=itop-1 ; qtop >= pbot ; qtop-- )
02231                if( dsar[qtop] < WAY_BIG ) break ;
02232              if( qtop <= ibot ){ dsar += dsim->nx; continue; }  
02233              qtop++ ; qnum = qtop-pbot ;
02234 
02235              switch( dplot ){
02236                default:
02237                case DPLOT_OVERLAY:                       
02238                  for( i=pbot ; i < MIN(ibot,qtop) ; i++ )
02239                    plot[i-pbot] = (dsar[ibot] - tsbot) * ftemp ;
02240                  for( i=ibot ; i < qtop ; i++ )
02241                    plot[i-pbot] = (dsar[i] - tsbot) * ftemp ;
02242                break ;
02243 
02244                case DPLOT_PLUSMINUS:                     
02245                  for( i=pbot ; i < MIN(ibot,qtop) ; i++ )
02246                    plot[i-pbot] = (tsar[ibot]+dsar[ibot] - tsbot) * ftemp ;
02247                  for( i=ibot ; i < qtop ; i++ )
02248                    plot[i-pbot] = (tsar[i]   +dsar[i]    - tsbot) * ftemp ;
02249                break ;
02250              }
02251 
02252              ftemp = grapher->gx / (pnum-1.0) ;  
02253              xoff  = grapher->xorigin[ix][iy] ;
02254              yoff  = grapher->fHIGH - grapher->yorigin[ix][iy] ;
02255 
02256              for( i=0 ; i < qnum ; i++ ){
02257                a_line[i].x = xoff + XPIX(i+pbot) ;  
02258                a_line[i].y = yoff - plot[i] ;
02259              }
02260 
02261              if( use_ovi )                      
02262                DC_fg_color( grapher->dc , ovi[id%OVI_MAX] ) ;
02263              else
02264                DC_fg_color( grapher->dc , DPLOT_COLOR(grapher) ) ;
02265 
02266              if( DPLOT_POINTS(grapher) ){       
02267                for( i=0 ; i < qnum ; i++ )
02268                 GRA_small_circle(grapher,a_line[i].x,a_line[i].y,DPLOT_IS_THICK(grapher));
02269              }
02270              if( DPLOT_LINES(grapher) ) {        
02271                DC_linewidth( grapher->dc , DPLOT_THICK(grapher) ) ;
02272                XDrawLines( grapher->dc->display ,
02273                            grapher->fd_pxWind , grapher->dc->myGC ,
02274                            a_line , qnum ,  CoordModeOrigin ) ;
02275              }
02276 
02277              
02278 
02279              if( dplot == DPLOT_PLUSMINUS ){  
02280               ftemp = grapher->fscale ;
02281                    if( ftemp == 0.0 ) ftemp =  1.0 ;
02282               else if( ftemp <  0.0 ) ftemp = -1.0 / ftemp ;
02283               for( i=pbot ; i < MIN(ibot,qtop) ; i++ )
02284                 plot[i-pbot] = (tsar[ibot]-dsar[ibot] - tsbot) * ftemp ;
02285               for( i=ibot ; i < qtop ; i++ )
02286                 plot[i-pbot] = (tsar[i]   -dsar[i]    - tsbot) * ftemp ;
02287               ftemp = grapher->gx / (pnum-1.0) ;
02288               for( i=0 ; i < qnum ; i++ ){
02289                 a_line[i].x = xoff + XPIX(i+pbot) ;
02290                 a_line[i].y = yoff - plot[i] ;
02291               }
02292               if( DPLOT_POINTS(grapher) ){
02293                for( i=0 ; i < qnum ; i++ )
02294                 GRA_small_circle(grapher,a_line[i].x,a_line[i].y,DPLOT_IS_THICK(grapher));
02295               }
02296               if( DPLOT_LINES(grapher) ) {
02297                 DC_linewidth( grapher->dc , DPLOT_THICK(grapher) ) ;
02298                 XDrawLines( grapher->dc->display ,
02299                             grapher->fd_pxWind , grapher->dc->myGC ,
02300                             a_line , qnum ,  CoordModeOrigin ) ;
02301               }
02302              }
02303 
02304              dsar += dsim->nx ;      
02305             } 
02306 
02307             DC_fg_color ( grapher->dc , DATA_COLOR(grapher) ) ;
02308             DC_linewidth( grapher->dc , DATA_THICK(grapher) ) ;
02309 
02310          } 
02311 
02312          
02313 
02314          if( grapher->HorZ && grapher->pmin[ix][iy] < 0.0 && grapher->pmax[ix][iy] > 0.0 ){
02315            DC_fg_color ( grapher->dc , GRID_COLOR(grapher) ) ;
02316            DC_linewidth( grapher->dc , GRID_THICK(grapher) ) ;
02317            DC_dashed_line( grapher->dc ) ;
02318 
02319            ftemp = grapher->fscale ;
02320                 if( ftemp == 0.0 ) ftemp =  1.0 ;
02321            else if( ftemp <  0.0 ) ftemp = -1.0 / ftemp ;
02322 
02323            XDrawLine( grapher->dc->display , grapher->fd_pxWind , grapher->dc->myGC ,
02324                       (int) xoff                , (int)(yoff + tsbot * ftemp) ,
02325                       (int)(xoff + grapher->gx) , (int)(yoff + tsbot * ftemp)  ) ;
02326 
02327            DC_fg_color ( grapher->dc , DATA_COLOR(grapher) ) ;
02328            DC_linewidth( grapher->dc , DATA_THICK(grapher) ) ;
02329            DC_solid_line( grapher->dc ) ;
02330          }
02331 
02332       } 
02333    } 
02334 
02335 
02336 
02337    DESTROY_IMARR(tsimar) ;
02338    if( dplot_imar != NULL ) DESTROY_IMARR(dplot_imar) ;  
02339 
02340    
02341 
02342 #define REFTS_FRAC 0.38  
02343 #define REFTS_TOP  0.98  
02344 
02345 #define ORTTS_FRAC 0.38
02346 #define ORTTS_TOP  0.78
02347 
02348    
02349 
02350    for( iex=0 ; iex <= 1 ; iex++ ){
02351 
02352       eximar = (iex==0) ? grapher->ref_ts : grapher->ort_ts ;
02353 
02354       if( eximar != NULL && IMARR_COUNT(eximar) > 0 ){
02355          float yscal , val , xscal , exfrac , extop ;
02356          int   nover , nvec , nx , ivec ;
02357          int   excolor , exthick ;
02358 
02359 STATUS("plotting extra graphs") ;
02360 
02361          exfrac  = (iex==0) ? REFTS_FRAC : ORTTS_FRAC ;
02362          extop   = (iex==0) ? REFTS_TOP  : ORTTS_TOP  ;
02363          excolor = (iex==0) ? IDEAL_COLOR(grapher) : ORT_COLOR(grapher) ;
02364          exthick = (iex==0) ? IDEAL_THICK(grapher) : ORT_THICK(grapher) ;
02365 
02366          for( its=0 ; its < IMARR_COUNT(eximar) ; its++ ){
02367 
02368             tsim = IMARR_SUBIMAGE(eximar,its) ;
02369 
02370             if( tsim == NULL || tsim->kind != MRI_float || tsim->nx < 2 ) continue ;
02371 
02372             nx   = tsim->nx ;
02373             itop = MIN( ttop , nx ) ;
02374             qnum = itop - pbot ; if( qnum < 2 ) continue ;
02375             nvec = (grapher->ref_ts_plotall) ? (tsim->ny) : 1 ;
02376 
02377             for( ivec=0 ; ivec < nvec ; ivec++ ){  
02378               tsar  = MRI_FLOAT_PTR(tsim) + (ivec*nx) ;
02379               tsbot = 99999999.0 ; tstop = -99999999.0 ;
02380               nover = grapher->init_ignore ;
02381               for( i=ibot ; i < itop ; i++ ){
02382                 val = tsar[i] ;
02383                 if( val < WAY_BIG ){ tstop = MAX(tstop,val); tsbot = MIN(tsbot,val); }
02384                 else               { nover++ ; }
02385               }
02386               if( tstop >= WAY_BIG || tstop <= tsbot ) continue ; 
02387 
02388               
02389 
02390               yscal = exfrac * grapher->gy / (tstop-tsbot) ;
02391               xscal = ftemp = grapher->gx / (pnum-1.0) ;
02392 
02393               xoff  = grapher->xorigin[grapher->xc][grapher->yc] ;
02394               yoff  = grapher->fHIGH - grapher->yorigin[grapher->xc][grapher->yc]
02395                                      - (extop - exfrac) * grapher->gy ;
02396 
02397               for( i=pbot ; i < itop; i++ ){
02398                 val = (i >= ibot &&  tsar[i] < WAY_BIG) ? tsar[i] : tsbot ;
02399 
02400                 a_line[i-pbot].x = xoff + XPIX(i) ;           
02401                 a_line[i-pbot].y = yoff - yscal*(val-tsbot) ;
02402               }
02403 
02404               
02405 
02406 
02407               if( nover == 0 ){
02408                 DC_fg_color ( grapher->dc , excolor ) ;
02409                 DC_linewidth( grapher->dc , exthick ) ;
02410                 XDrawLines( grapher->dc->display ,
02411                             grapher->fd_pxWind , grapher->dc->myGC ,
02412                             a_line , qnum ,  CoordModeOrigin ) ;
02413               } else {
02414                 for( i=pbot ; i < itop-1 ; i++ ){
02415                   if( i >= ibot && tsar[i] < WAY_BIG && tsar[i+1] < WAY_BIG ){
02416                     DC_fg_color ( grapher->dc , excolor ) ;
02417                     DC_linewidth( grapher->dc , exthick ) ;
02418                   } else {
02419                     DC_fg_color( grapher->dc , IGNORE_COLOR(grapher) ) ;
02420                     if( grapher->mat < 4 &&
02421                         ( i < ibot || tsar[i] >= WAY_BIG ) )
02422                       GRA_small_circle( grapher,a_line[i-pbot].x,a_line[i-pbot].y,0 );
02423                   }
02424 
02425                   XDrawLines( grapher->dc->display ,
02426                               grapher->fd_pxWind , grapher->dc->myGC ,
02427                               a_line + (i-pbot) , 2 ,  CoordModeOrigin ) ;
02428                 }
02429                 if( grapher->mat < 4 &&
02430                     ( i < ibot || tsar[i] >= WAY_BIG ) )
02431                   GRA_small_circle( grapher,a_line[i-pbot].x,a_line[i-pbot].y,0 );
02432               }
02433             } 
02434          } 
02435       } 
02436    } 
02437 
02438    
02439 
02440    if( grapher->xax_tsim != NULL ){
02441      float yscal , ftemp , xscal , yoff ;
02442      int   npt ;
02443 
02444      xscal = GL_DLX / (float) grapher->gx ;
02445      yscal = grapher->gy / (pnum-1.0) ;
02446      yoff  = grapher->fHIGH - grapher->yorigin[grapher->xc][grapher->yc] ;
02447      ftemp = 1.0 ;
02448      npt   = ttop ;
02449      if( npt > grapher->xax_tsim->nx ) npt = grapher->xax_tsim->nx ;
02450      if( npt > pbot+1 ){
02451        for( i=pbot ; i < npt ; i++ ){
02452          a_line[i-pbot].x = XPIX(i) * xscal ;
02453          a_line[i-pbot].y = yoff - yscal*(i-pbot) ;
02454        }
02455        DC_fg_color ( grapher->dc , IDEAL_COLOR(grapher) ) ;
02456        DC_linewidth( grapher->dc , IDEAL_THICK(grapher) ) ;
02457        XDrawLines( grapher->dc->display , grapher->fd_pxWind , grapher->dc->myGC ,
02458                    a_line , npt-pbot ,  CoordModeOrigin ) ;
02459      }
02460    }
02461 
02462    
02463 
02464    EXRETURN ;
02465 }
02466 
02467 
02468 
02469 
02470 
02471 
02472 void draw_grids( MCW_grapher * grapher )
02473 {
02474    int i , mat=grapher->mat , gx=grapher->gx , gy=grapher->gy ;
02475    int j, k, g, xo, yo, npoints , m ;
02476    int xc = grapher->xc , yc = grapher->yc ;
02477    float ftemp ;
02478 
02479 ENTRY("draw_grids") ;
02480    if( grapher->dont_redraw ) EXRETURN ;  
02481 
02482    
02483 
02484    if( GRID_COLOR(grapher) > 0 ){
02485       DC_fg_color ( grapher->dc , GRID_COLOR(grapher) ) ;
02486       DC_linewidth( grapher->dc , GRID_THICK(grapher) ) ;
02487 
02488       g       = grapher->grid_spacing ;
02489       npoints = NPTS(grapher) ;  
02490 
02491       if( npoints > 1 ){                
02492         ftemp = gx / (npoints-1.0) ;
02493         for( i=0 ; i < mat ; i++ ){
02494           for( m=0 ; m < mat ; m++ ){
02495             xo = grapher->xorigin[i][m] ; yo = grapher->yorigin[i][m] ;
02496             for( j=1 ; j <= (npoints-1)/g ; j++ ){
02497               k = xo + j * g * ftemp ;
02498               plot_fdX( grapher , k , yo    , 0 ) ;
02499               plot_fdX( grapher , k , yo+gy , 1 ) ;
02500             }
02501           }
02502         }
02503       }
02504 
02505       
02506 
02507       xo = grapher->xorigin[xc][yc] ; yo = grapher->yorigin[xc][yc] ;
02508       g  = MIN( grapher->gy/3 , grapher->gx/3 ) ; g  = MIN( g , 4 ) ;
02509       for( j=1 ; j <= g ; j++ ){
02510          plot_fdX( grapher , xo+j    , yo+j    , 0 ) ;
02511          plot_fdX( grapher , xo+j    , yo+gy-j , 1 ) ;
02512          plot_fdX( grapher , xo+gx-j , yo+gy-j , 1 ) ;
02513          plot_fdX( grapher , xo+gx-j , yo+j    , 1 ) ;
02514          plot_fdX( grapher , xo+j    , yo+j    , 1 ) ;
02515       }
02516    }
02517 
02518    
02519 
02520    DC_fg_color ( grapher->dc , FG_COLOR(grapher) ) ;
02521    DC_linewidth( grapher->dc , FG_THICK(grapher) ) ;
02522 
02523    for( i=0 ; i < mat ; i++ ){
02524       for( j=0 ; j < mat ; j++ ){
02525          xo = grapher->xorigin[i][j] ; yo = grapher->yorigin[i][j] ;
02526          plot_fdX( grapher , xo    , yo    , 0 ) ;
02527          plot_fdX( grapher , xo+gx , yo    , 1 ) ;
02528          plot_fdX( grapher , xo+gx , yo+gy , 1 ) ;
02529          plot_fdX( grapher , xo    , yo+gy , 1 ) ;
02530          plot_fdX( grapher , xo    , yo    , 1 ) ;
02531       }
02532    }
02533 
02534    EXRETURN ;
02535 }
02536 
02537 
02538 
02539 
02540 
02541 void send_newinfo( MCW_grapher *grapher )
02542 {
02543 ENTRY("send_newinfo") ;
02544 
02545    if( GRA_VALID(grapher) && grapher->status->send_CB != NULL ){
02546       GRA_cbs cbs ;
02547 
02548       cbs.reason = graCR_newxyzm   ;
02549       cbs.xcen   = grapher->xpoint ;
02550       cbs.ycen   = grapher->ypoint ;
02551       cbs.zcen   = grapher->zpoint ;
02552       cbs.mat    = grapher->mat ;
02553 #if 0
02554       grapher->status->send_CB( grapher , grapher->getaux , &cbs ) ;
02555 #else
02556       CALL_sendback( grapher , cbs ) ;
02557 #endif
02558    }
02559 
02560    EXRETURN ;
02561 }
02562 
02563 
02564 
02565 
02566 
02567 void init_mat( MCW_grapher *grapher )
02568 {
02569    int i, j ;
02570    int gg ;
02571 
02572 ENTRY("init_mat") ;
02573    if( !GRA_VALID(grapher) ) EXRETURN ;
02574 
02575    grapher->gx = grapher->gx_max / grapher->mat;
02576    grapher->gy = grapher->gy_max / grapher->mat;
02577 
02578    for (i=0;i<grapher->mat;i++) {
02579      for (j=0;j<grapher->mat;j++) {
02580        grapher->xorigin[i][j] = MDX1 + i * grapher->gx;
02581        grapher->yorigin[i][j] = MDY1 + j * grapher->gy;
02582      }
02583    }
02584 
02585    if( grapher->mirror && grapher->mat > 1 ){  
02586       int mm = grapher->mat , m2 = mm/2 ;      
02587 
02588       for( j=0 ; j < mm ; j++ ){
02589          for( i=0 ; i < m2 ; i++ ){
02590            gg                          = grapher->xorigin[i][j] ;
02591            grapher->xorigin[i][j]      = grapher->xorigin[mm-1-i][j] ;
02592            grapher->xorigin[mm-1-i][j] = gg ;
02593          }
02594       }
02595    }
02596 
02597    grapher->xc = grapher->mat/2;
02598    grapher->yc = (grapher->mat-1)/2;
02599 
02600    gg = grapher->ggap ;                
02601    if( gg > 0 ){
02602      gg = MIN( gg , grapher->gx / 2 ) ;  
02603      gg = MIN( gg , grapher->gy / 2 ) ;
02604      grapher->gx -= gg ;
02605      grapher->gy -= gg ;
02606    }
02607 
02608    EXRETURN ;
02609 }
02610 
02611    
02612 void scale_up( MCW_grapher * grapher )
02613 
02614 {
02615    if( !GRA_VALID(grapher) ) return ;
02616    if (grapher->fscale > 0) grapher->fscale *= 2;
02617    else if (grapher->fscale < -2) grapher->fscale /= 2;
02618    else grapher->fscale = 1;
02619 
02620    if( grapher->fscale > 1000000.0 ){
02621      static int nn=0 ;
02622      nn++ ; if( nn < 3 ) fprintf(stderr,"Is that you, Bellgowan?  If so, stop it!\a\n") ;
02623    }
02624    return ;
02625 }
02626 
02627    
02628 void scale_down( MCW_grapher * grapher )
02629 
02630 {
02631    if( !GRA_VALID(grapher) ) return ;
02632    if (grapher->fscale > 1) grapher->fscale /= 2;
02633    else if (grapher->fscale < 0) grapher->fscale *= 2;
02634    else grapher->fscale = -2;
02635    return ;
02636 }
02637 
02638    
02639 void mat_down( MCW_grapher * grapher )
02640 
02641 {
02642    int old;
02643 
02644    if( !GRA_VALID(grapher) ) return ;
02645    old = grapher->mat;
02646    grapher->mat--;
02647    if (grapher->mat < 1) grapher->mat = 1;
02648    else if (grapher->mat > grapher->mat_max) grapher->mat = grapher->mat_max;
02649    if (grapher->mat!= old) {
02650       init_mat( grapher ) ;
02651       redraw_graph( grapher , 0 ) ;
02652    }
02653    return ;
02654 }
02655 
02656    
02657 void mat_up( MCW_grapher * grapher )
02658 
02659 {
02660    int old;
02661 
02662    if( !GRA_VALID(grapher) ) return ;
02663    old = grapher->mat;
02664    grapher->mat++;
02665    if (grapher->mat < 1) grapher->mat = 1;
02666    else if (grapher->mat > grapher->mat_max) grapher->mat = grapher->mat_max;
02667    if (grapher->mat!= old) {
02668       init_mat(grapher) ;
02669       redraw_graph(grapher,0) ;
02670    }
02671    return ;
02672 }
02673 
02674    
02675 void grid_down( MCW_grapher * grapher )
02676 
02677 {
02678    int old;
02679 
02680    if( !GRA_VALID(grapher) ) return ;
02681    old = grapher->grid_index;
02682    grapher->grid_index--;
02683    if (grapher->grid_index < 0) grapher->grid_index = 0;
02684    grapher->grid_spacing = grid_ar[grapher->grid_index] ;
02685    grapher->grid_fixed   = 1 ;  
02686    redraw_graph(grapher,0) ;
02687    return ;
02688 }
02689 
02690    
02691 void grid_up( MCW_grapher * grapher )
02692 
02693 {
02694    int old;
02695 
02696    if( !GRA_VALID(grapher) ) return ;
02697    old = grapher->grid_index;
02698    grapher->grid_index++;
02699    if (grapher->grid_index >= GRID_MAX) grapher->grid_index = GRID_MAX - 1;
02700    grapher->grid_spacing = grid_ar[grapher->grid_index] ;
02701    grapher->grid_fixed   = 1 ;  
02702    redraw_graph(grapher,0) ;
02703    return ;
02704 }
02705 
02706 
02707 
02708 
02709 
02710 void GRA_drawing_EV( Widget w , XtPointer client_data ,
02711                      XEvent * ev , Boolean * continue_to_dispatch )
02712 {
02713    MCW_grapher * grapher = (MCW_grapher *) client_data ;
02714 
02715 ENTRY("GRA_drawing_EV") ;
02716 
02717    if( ! GRA_REALZ(grapher) ){
02718 if(PRINT_TRACING){
02719 char str[256] ;
02720 sprintf(str,"unrealized grapher! Event type = %d",(int)ev->type) ;
02721 STATUS(str) ; }
02722       EXRETURN ;
02723    }
02724 
02725    if( grapher->valid == 666 ){  
02726 if(PRINT_TRACING){
02727 char str[256] ;
02728 sprintf(str,"dying grapher! Event type = %d",(int)ev->type) ;
02729 STATUS(str) ; }
02730       EXRETURN ;
02731    }
02732 
02733    switch( ev->type ){
02734 
02735       
02736 
02737       case Expose:{
02738          XExposeEvent * event = (XExposeEvent *) ev ;
02739 
02740 if(PRINT_TRACING){
02741 char str[256] ;
02742 sprintf(str,"Expose event with count = %d",event->count) ;
02743 STATUS(str) ; }
02744 
02745 
02746 
02747 
02748 
02749 
02750 
02751 
02752          XSync( XtDisplay(w) , False ) ;  
02753 
02754          if( event->count == 0 ){
02755             if( grapher->fd_pxWind == (Pixmap) 0 ){
02756                int width , height ;
02757                MCW_widget_geom( grapher->draw_fd , &width , &height , NULL,NULL ) ;
02758                GRA_new_pixmap( grapher , width , height , 1 ) ;
02759             } else {
02760                GRA_redraw_overlay( grapher ) ;
02761             }
02762          }
02763       }
02764       break ;
02765 
02766       
02767 
02768       case KeyPress:{
02769          XKeyEvent * event = (XKeyEvent *) ev ;
02770          char           buf[32] ;
02771          KeySym         ks=0 ;
02772          int            nbuf ;
02773 
02774 STATUS("KeyPress event") ;
02775 
02776          GRA_timer_stop( grapher ) ;  
02777 
02778          if( grapher->fd_pxWind != (Pixmap) 0 ){
02779             buf[0] = '\0' ;
02780             nbuf = XLookupString( event , buf , 32 , &ks , NULL ) ;
02781             if( nbuf == 0 ){   
02782               switch(ks){
02783                 case XK_KP_Left:
02784                 case XK_Left:      buf[0] = '<' ; break ;
02785                 case XK_KP_Right:
02786                 case XK_Right:     buf[0] = '>' ; break ;
02787                 case XK_KP_Page_Up:
02788                 case XK_Page_Up:   buf[0] = 'Z' ; break ;
02789                 case XK_KP_Page_Down:
02790                 case XK_Page_Down: buf[0] = 'z' ; break ;
02791               }
02792             }
02793             if( buf[0] != '\0' ) GRA_handle_keypress( grapher , buf , ev ) ;
02794             else if(PRINT_TRACING){
02795                char str[256] ;
02796                sprintf(str,"*** KeyPress was empty!?  nbuf=%d",nbuf) ;
02797                STATUS(str) ;
02798             }
02799          }
02800       }
02801       break ;
02802 
02803       
02804 
02805       case ButtonPress:{
02806          XButtonEvent * event = (XButtonEvent *) ev ;
02807          int bx,by , width,height , but ;
02808          int i, j, gx , gy , mat , xloc,yloc ;
02809          unsigned int but_state ;
02810          int xd,yd,zd ;  
02811 
02812 STATUS("button press") ;
02813 
02814          bx  = event->x ;
02815          by  = event->y ;
02816          but = event->button ; but_state = event->state ;
02817          MCW_discard_events( w , ButtonPressMask ) ;
02818 
02819          
02820 
02821          if( but == Button1                                &&
02822              grapher->glogo_pixmap != XmUNSPECIFIED_PIXMAP &&
02823              bx < grapher->glogo_width                     &&
02824              grapher->fHIGH - by < grapher->glogo_height     ){
02825 
02826             show_grapher_pixmap = ! show_grapher_pixmap ;
02827 
02828             if( XtIsManaged(grapher->option_rowcol) )     
02829               XtUnmanageChild(grapher->option_rowcol) ;
02830             else
02831               XtManageChild(grapher->option_rowcol) ;
02832 
02833             redraw_graph( grapher , 0 )  ;
02834             break ; 
02835          }
02836 
02837          
02838 
02839          
02840 
02841 
02842 
02843          gx = grapher->gx ; gy = grapher->gy ; mat = grapher->mat ;
02844 
02845          for( i=0 ; i < mat ; i++ )
02846            if( bx > grapher->xorigin[i][0]      &&
02847                bx < grapher->xorigin[i][0] + gx   ) break ;  
02848 
02849          if( i == mat ) break ; 
02850 
02851          xloc = grapher->xpoint + i - grapher->xc ;
02852 
02853          for( j=0 ; j < mat ; j++ )                           
02854            if( by > grapher->fHIGH - grapher->yorigin[0][j] - gy &&
02855                by < grapher->fHIGH - grapher->yorigin[0][j]        ) break ;
02856 
02857          if( j == mat ) break ; 
02858 
02859          yloc = grapher->ypoint - j + grapher->yc ;
02860 
02861          
02862 
02863          if (xloc < 0)                    xloc += grapher->status->nx ;
02864          if (xloc >= grapher->status->nx) xloc -= grapher->status->nx ;
02865          if (yloc < 0)                    yloc += grapher->status->ny ;
02866          if (yloc >= grapher->status->ny) yloc -= grapher->status->ny ;
02867 
02868          
02869          
02870 
02871          if( but == Button2 ||
02872              ( but == Button1 && (event->state & ShiftMask) &&
02873                                 !(event->state & ControlMask) ) ){
02874 
02875             if( grapher->button2_enabled && (bx > GL_DLX) ){
02876                GRA_cbs cbs ;
02877                cbs.reason = graCR_button2_points ;
02878                cbs.xcen   = xloc ;
02879                cbs.ycen   = yloc ;
02880                cbs.zcen   = grapher->zpoint ;
02881 #if 0
02882                grapher->status->send_CB( grapher , grapher->getaux , &cbs ) ;
02883 #else
02884                CALL_sendback( grapher , cbs ) ;
02885 #endif
02886             } else {
02887                XBell(XtDisplay(w),100) ;
02888             }
02889          }
02890 
02891          
02892 
02893 
02894 
02895          if( grapher->fd_pxWind != (Pixmap) 0 &&
02896              but == Button1                   && (bx > GL_DLX) &&
02897              ( (xloc != grapher->xpoint) || (yloc != grapher->ypoint) ) ){
02898 
02899                grapher->xpoint = xloc ;
02900                grapher->ypoint = yloc ;
02901                redraw_graph( grapher , 0 ) ;
02902                send_newinfo( grapher ) ;
02903          }
02904 
02905          
02906 
02907 
02908          else if( grapher->fd_pxWind != (Pixmap)0 &&
02909                   NPTS(grapher) > 1               && !grapher->textgraph     &&
02910                   (but==Button1)                  && (bx > GL_DLX)           &&
02911                   (xloc == grapher->xpoint)       && yloc == grapher->ypoint &&
02912                   grapher->cen_line != NULL       && grapher->nncen > 1        ){
02913 
02914            float dist , dmin=999999.9 ;
02915            int imin = 0 ;
02916 
02917            
02918 
02919            for( i=0 ; i < grapher->nncen ; i++ ){
02920              dist =  abs( bx - grapher->cen_line[i].x )   
02921                    + abs( by - grapher->cen_line[i].y ) ;
02922              if( dist < dmin ){ dmin = dist; imin = i; if(dmin == 0) break; }
02923            }
02924            i = imin + NBOT(grapher) ;
02925 
02926            if( i >= 0 && i < TTOP(grapher) ){
02927              if( grapher->status->send_CB != NULL ){
02928                GRA_cbs cbs ;
02929 
02930                cbs.reason = graCR_setindex ;
02931                cbs.key    = i ;
02932                cbs.event  = ev ;
02933 #if 0
02934                grapher->status->send_CB( grapher , grapher->getaux , &cbs ) ;
02935 #else
02936                CALL_sendback( grapher , cbs ) ;
02937 #endif
02938              } else {
02939                (void) drive_MCW_grapher( grapher,graDR_setindex,(XtPointer)i );
02940              }
02941            }
02942          }
02943 
02944          
02945 
02946          if( but == Button3 && !ISONE(grapher) && !grapher->textgraph ){
02947             int ix , iy ;
02948 
02949             ix = xloc - grapher->xpoint + grapher->xc ;
02950                  if( ix <  0            ) ix += grapher->status->nx ;
02951             else if( ix >= grapher->mat ) ix -= grapher->status->nx ;
02952 
02953             iy = grapher->ypoint - yloc + grapher->yc ;
02954                  if( iy <  0            ) iy += grapher->status->ny ;
02955             else if( iy >= grapher->mat ) iy -= grapher->status->ny ;
02956 
02957             if( ix >= 0 && ix < grapher->mat && iy >= 0 && iy < grapher->mat ){
02958                XmString xstr ;
02959                char bmin[16],bmax[16],bmean[16],bstd[16] ;
02960                char bmed[16] , bmad[16] ; 
02961                char *qstr , *eee ;        
02962                int nlin , nltop=40 ;      
02963 
02964                AV_fval_to_char( grapher->tbot[ix][iy]  , bmin ) ;
02965                AV_fval_to_char( grapher->ttop[ix][iy]  , bmax ) ;
02966                AV_fval_to_char( grapher->tmean[ix][iy] , bmean) ;
02967                AV_fval_to_char( grapher->tstd[ix][iy]  , bstd ) ;
02968 
02969                AV_fval_to_char( grapher->tmed[ix][iy]  , bmed ) ; 
02970                AV_fval_to_char( grapher->tmad[ix][iy]  , bmad ) ;
02971 
02972                if( grapher->tuser[ix][iy] == NULL )
02973                  qstr = AFMALL(char, 912) ;
02974                else
02975                  qstr = AFMALL(char, 912+strlen(grapher->tuser[ix][iy])) ;
02976 
02977                
02978 
02979                xd = xloc; yd = yloc; zd = grapher->zpoint ;
02980 #ifndef DONT_MANGLE_XYZ
02981                { THD_ivec3 id ;
02982                  id = THD_fdind_to_3dind( grapher->getaux , TEMP_IVEC3(xd,yd,zd) ) ;
02983                  xd = id.ijk[0] ; yd = id.ijk[1] ; zd = id.ijk[2] ; }
02984 #endif
02985                sprintf( qstr, "Data Statistics\n"
02986                               "---------------\n"
02987                               "Indexes = %d:%-d\n" 
02988                               "x voxel = %d\n"
02989                               "y voxel = %d\n"
02990                               "z voxel = %d\n"
02991                               "Min     =%s\n"
02992                               "Max     =%s\n"
02993                               "Mean    =%s\n"
02994                               "Sigma   =%s\n"
02995                               "Median  =%s\n"     
02996                               "MAD     =%s" ,
02997                         grapher->sbot[ix][iy], grapher->stop[ix][iy], 
02998                         xd , yd , zd ,
02999                         bmin,bmax,bmean,bstd,bmed,bmad ) ;
03000 
03001 
03002 
03003                 if( grapher->tuser[ix][iy] != NULL ){
03004                   strcat( qstr , "\n------------------\n" ) ;
03005                   strcat( qstr , grapher->tuser[ix][iy] ) ;
03006                 }
03007 
03008                 
03009 
03010 
03011                 eee = getenv( "AFNI_GRAPH_TEXTLIMIT" ) ;
03012                 if( eee != NULL ){
03013                   nlin = strtol( eee , NULL , 10 ) ;
03014                   if( nlin > 0 ) nltop = nlin ;
03015                 }
03016 
03017                 for( nlin=1,eee=qstr ; *eee != '\0' ; eee++ )
03018                   if( *eee == '\n' ) nlin++ ;
03019 
03020                 if( nlin < nltop ){
03021                   xstr = XmStringCreateLtoR( qstr, XmFONTLIST_DEFAULT_TAG ) ;
03022                   XtVaSetValues( grapher->but3_label,XmNlabelString,xstr,NULL );
03023                   XmStringFree( xstr ) ;
03024                   XmMenuPosition( grapher->but3_menu , event ) ; 
03025                   XtManageChild ( grapher->but3_menu ) ;         
03026                 } else {
03027                   (void) new_MCW_textwin(grapher->fdw_graph,qstr,TEXT_READONLY);
03028                 }
03029                 free(qstr) ;
03030 
03031             } else {
03032               redraw_graph(grapher,0) ;  
03033             }
03034          }
03035       }
03036       break ;
03037 
03038       
03039 
03040       case ConfigureNotify:{
03041          XConfigureEvent * event = (XConfigureEvent *) ev ;
03042          int new_width , new_height ;
03043 
03044 STATUS("ConfigureNotify event") ;
03045 
03046          XSync( XtDisplay(w) , False ) ;
03047 
03048          new_width  = event->width ;
03049          new_height = event->height ;
03050 
03051          if( new_width != grapher->fWIDE || new_height != grapher->fHIGH ){
03052            GRA_new_pixmap( grapher , new_width , new_height , 1 ) ;
03053          }
03054       }
03055       break ;
03056 
03057       
03058 
03059       default:
03060 #ifdef AFNI_DEBUG
03061 {char str[256]; sprintf(str,"Event code = %d\n",(int)ev->type); STATUS(str);}
03062 #endif
03063       break ;
03064 
03065    } 
03066 
03067    EXRETURN ;
03068 }
03069 
03070 
03071 
03072 
03073 
03074 void GRA_new_pixmap( MCW_grapher * grapher ,
03075                      int new_width , int new_height , int redraw )
03076 {
03077    int ww,hh ;
03078 
03079 ENTRY("GRA_new_pixmap") ;
03080 
03081    if( ! GRA_REALZ(grapher) ) EXRETURN ;
03082 
03083    grapher->fWIDE  = new_width ;
03084    grapher->fHIGH  = new_height ;
03085    grapher->gx_max = new_width  - (GL_DLX + GR_DLX) ;
03086    grapher->gy_max = new_height - (GT_DLY + GB_DLY) ;
03087 
03088    if( grapher->fd_pxWind != (Pixmap) 0 ){
03089 STATUS("freeing old Pixmap") ;
03090       XFreePixmap( grapher->dc->display , grapher->fd_pxWind ) ;
03091    }
03092 
03093 STATUS("allocating new Pixmap") ;
03094    grapher->fd_pxWind = XCreatePixmap( grapher->dc->display ,
03095                                        XtWindow(grapher->draw_fd) ,
03096                                        grapher->fWIDE , grapher->fHIGH,
03097                                        grapher->dc->planes ) ;
03098 
03099    MCW_widget_geom( grapher->option_rowcol , &ww , &hh , NULL,NULL ) ;
03100    XtVaSetValues( grapher->option_rowcol ,
03101 #ifdef WANT_AFNI_BITMAP
03102                      XmNx , grapher->fWIDE - ww - 2 ,
03103 #else
03104                      XmNx , 2 ,
03105 #endif
03106                      XmNy , grapher->fHIGH - hh - 2 ,
03107                   NULL ) ;
03108 
03109    if( redraw ){
03110      init_mat( grapher ) ;
03111      redraw_graph( grapher , 0 ) ;
03112    }
03113 
03114    EXRETURN ;
03115 }
03116 
03117 
03118 
03119 
03120 
03121 void GRA_handle_keypress( MCW_grapher *grapher , char *buf , XEvent *ev )
03122 {
03123    int ii ;
03124 
03125 ENTRY("GRA_handle_keypress") ;
03126 
03127    if( buf[0] == '\0' ) EXRETURN ;
03128 
03129 if(PRINT_TRACING){
03130 char str[256] ;
03131 sprintf(str,"buf[0]=%c (%x)",(int)buf[0],(int)buf[0]) ;
03132 STATUS(str); }
03133 
03134    
03135 
03136    
03137 
03138    if( grapher->key_Nlock==0 && buf[0]=='N' ){
03139      grapher->key_Nlock = 1 ;
03140      HAND_cursorize( grapher->fdw_graph ) ;
03141      HAND_cursorize( grapher->draw_fd ) ;
03142      grapher->key_lock_sum = 0 ;
03143      EXRETURN ;
03144    }
03145 
03146    
03147 
03148    if( grapher->key_Nlock && buf[0] == 13 ){
03149 
03150       
03151 
03152       if( grapher->key_lock_sum > 0 )
03153          grapher->mat = MIN( grapher->mat_max , grapher->key_lock_sum ) ;
03154 
03155       NORMAL_cursorize( grapher->fdw_graph ) ;
03156       if( ISONE(grapher) )
03157         NORMAL_cursorize( grapher->draw_fd ) ;
03158       else
03159         POPUP_cursorize( grapher->draw_fd ) ;
03160 
03161       init_mat    ( grapher ) ;
03162       redraw_graph( grapher , 0 ) ;
03163       send_newinfo( grapher ) ;
03164       grapher->key_Nlock = grapher->key_lock_sum = 0 ;
03165       EXRETURN ;
03166    }
03167 
03168    
03169 
03170    if( grapher->key_Nlock ){
03171       if( isdigit(buf[0]) ){
03172          ii = buf[0] - 48;
03173          grapher->key_lock_sum = MIN( 10000, 10*grapher->key_lock_sum + ii ) ;
03174       }
03175       EXRETURN ;
03176    }
03177 
03178    
03179 
03180    MCW_discard_events( grapher->draw_fd , KeyPressMask ) ;
03181 
03182    switch (buf[0]) {
03183 
03184       case '-':
03185       case '+':
03186         if( buf[0] == '-' ) scale_down( grapher ) ;
03187         else                scale_up  ( grapher ) ;
03188         redraw_graph( grapher , 0 ) ;
03189       break;
03190 
03191       case 'a':
03192         redraw_graph( grapher , PLOTCODE_AUTOSCALE ) ;         
03193       break ;
03194 
03195       case 'i':
03196         if( !grapher->textgraph && grapher->init_ignore > 0 ){ 
03197           GRA_cbs cbs ;
03198           cbs.reason = graCR_setignore ; cbs.key = grapher->init_ignore - 1 ;
03199           CALL_sendback( grapher , cbs ) ;
03200         } else {
03201           XBell(grapher->dc->display,100) ;
03202         }
03203       break ;
03204 
03205       case 'I':
03206         if( !grapher->textgraph ){                             
03207           GRA_cbs cbs ;
03208           cbs.reason = graCR_setignore ; cbs.key = grapher->init_ignore + 1 ;
03209           CALL_sendback( grapher , cbs ) ;
03210         } else {
03211           XBell(grapher->dc->display,100) ;
03212         }
03213       break ;
03214 
03215       case 'm':
03216       case 'M':
03217         if( buf[0] == 'm' ) mat_down( grapher ) ;
03218         else                mat_up  ( grapher ) ;
03219         send_newinfo( grapher ) ;
03220       break;
03221 
03222       case 'g':
03223         grid_down( grapher ) ;
03224       break;
03225 
03226       case 'G':
03227         grid_up( grapher ) ;
03228       break;
03229 
03230       case 'h':   
03231         grapher->HorZ = ! grapher->HorZ ;
03232         redraw_graph( grapher , 0 ) ;
03233       break ;
03234 
03235       case 'q':
03236       case 'Q':
03237         end_fd_graph_CB( NULL , (XtPointer) grapher , NULL ) ;
03238       break ;
03239 
03240       
03241 
03242       case 'b':{
03243         int bbb = grapher->common_base << 1 ;
03244         if( bbb > BASELINE_GLOBAL ) bbb = BASELINE_INDIVIDUAL ;
03245         MCW_set_bbox( grapher->opt_baseline_bbox , bbb ) ;
03246         grapher->common_base = bbb ;
03247         redraw_graph( grapher , 0 ) ;
03248       }
03249       break ;
03250 
03251       case 't':{                                                  
03252         int bbb = ! grapher->textgraph ;
03253         MCW_set_bbox( grapher->opt_textgraph_bbox , bbb ) ;
03254         grapher->textgraph = bbb ;
03255         redraw_graph( grapher , 0 ) ;
03256       }
03257       break ;
03258 
03259       case 'S':
03260         MCW_choose_string( grapher->option_rowcol ,
03261                            "Save PNM Prefix:" , NULL ,
03262                            GRA_saver_CB , (XtPointer) grapher ) ;
03263       break ;
03264 
03265       case 'L':
03266         show_grapher_pixmap = ! show_grapher_pixmap ;
03267         if( grapher->glogo_pixmap != XmUNSPECIFIED_PIXMAP )
03268            redraw_graph( grapher , 0 ) ;
03269       break ;
03270 
03271       case '<': case ',':   
03272       case '>': case '.':
03273       case '1':
03274       case 'l':
03275          EXRONE(grapher) ;  
03276 
03277               if( buf[0] == '<' || buf[0] == ',' ) ii = grapher->time_index - 1;
03278          else if( buf[0] == '>' || buf[0] == '.' ) ii = grapher->time_index + 1;
03279          else if( buf[0] == '1'                  ) ii = 1 ;
03280          else if( buf[0] == 'l'                  ) ii = grapher->status->num_series-1;
03281 
03282          ii = (ii+grapher->status->num_series) % grapher->status->num_series ;
03283          if( ii >= 0 && ii < grapher->status->num_series ){
03284            if( grapher->status->send_CB != NULL ){
03285              GRA_cbs cbs ;
03286 
03287              cbs.reason = graCR_setindex ;
03288              cbs.key    = ii;
03289              cbs.event  = NULL ;
03290 #if 0
03291              grapher->status->send_CB( grapher, grapher->getaux, &cbs ) ;
03292 #else
03293              CALL_sendback( grapher , cbs ) ;
03294 #endif
03295            } else {
03296              (void) drive_MCW_grapher( grapher, graDR_setindex, (XtPointer)ii) ;
03297            }
03298          }
03299       break ;
03300 
03301       case 'v':  
03302       case 'V':
03303         if( grapher->status->num_series > 1 ){
03304           grapher->timer_func  = GRA_TIMERFUNC_INDEX ;
03305           grapher->timer_delay = (int) AFNI_numenv("AFNI_VIDEO_DELAY") ;
03306           if( grapher->timer_delay <= 0 ) grapher->timer_delay = 1 ;
03307           grapher->timer_param = (buf[0] == 'v') ? 1 : -1 ;
03308           grapher->timer_id    =
03309            XtAppAddTimeOut( XtWidgetToApplicationContext(grapher->opt_quit_pb),
03310                             grapher->timer_delay , GRA_timer_CB , grapher ) ;
03311         } else {
03312           XBell(grapher->dc->display,100) ;
03313         }
03314       break ;
03315 
03316       case 'r':
03317       case 'R':
03318         if( grapher->status->num_series > 1 ){
03319           grapher->timer_func  = GRA_TIMERFUNC_BOUNCE ;
03320           grapher->timer_delay = (int) AFNI_numenv("AFNI_VIDEO_DELAY") ;
03321           if( grapher->timer_delay <= 0 ) grapher->timer_delay = 1 ;
03322           grapher->timer_param = (buf[0] == 'r') ? 1 : -1 ;
03323           grapher->timer_id    =
03324            XtAppAddTimeOut( XtWidgetToApplicationContext(grapher->opt_quit_pb),
03325                             grapher->timer_delay , GRA_timer_CB , grapher ) ;
03326         } else {
03327           XBell(grapher->dc->display,100) ;
03328         }
03329       break ;
03330 
03331       case 'z':  
03332       case 'Z':
03333         if( buf[0] == 'z' ){
03334           grapher->zpoint -- ;
03335           if( grapher->zpoint < 0 ) grapher->zpoint = grapher->status->nz - 1 ;
03336         } else {
03337           grapher->zpoint ++ ;
03338           if( grapher->zpoint >= grapher->status->nz ) grapher->zpoint = 0 ;
03339         }
03340         redraw_graph( grapher , 0 ) ;
03341         send_newinfo( grapher ) ;
03342       break ;
03343 
03344       case 'w':{
03345          char * wcfname ;
03346          int ndig , ll ;
03347          MRI_IMAGE * tsim ;
03348          int xd,yd,zd ;     
03349 
03350          EXRONE(grapher) ;  
03351 
03352          ll   = MAX( grapher->status->nx , grapher->status->ny ) ;
03353          ll   = MAX( grapher->status->nz , ll ) ;
03354          ndig = (ll < 1000) ? 3 : 4 ;
03355 
03356          ll   = 3*ndig + 16 ;
03357          if( Grapher_Stuff.wcsuffix != NULL )
03358             ll += strlen(Grapher_Stuff.wcsuffix) ;
03359          wcfname = (char *) XtMalloc(ll) ;
03360 
03361          
03362 
03363          xd = grapher->xpoint; yd = grapher->ypoint; zd = grapher->zpoint;
03364 #ifndef DONT_MANGLE_XYZ
03365          { THD_ivec3 id;
03366            id = THD_fdind_to_3dind( grapher->getaux, TEMP_IVEC3(xd,yd,zd) );
03367            xd = id.ijk[0]; yd = id.ijk[1]; zd = id.ijk[2]; }
03368 #endif
03369 
03370          if( Grapher_Stuff.wcsuffix != NULL )
03371             sprintf(wcfname,"%0*d_%0*d_%0*d.%s.1D" ,
03372                     ndig,xd , ndig,yd , ndig,zd ,
03373                               Grapher_Stuff.wcsuffix ) ;
03374          else
03375             sprintf(wcfname,"%0*d_%0*d_%0*d.1D" ,
03376                     ndig,xd , ndig,yd , ndig,zd  ) ;
03377 
03378          ll = grapher->xpoint +
03379               grapher->ypoint * grapher->status->nx +
03380               grapher->zpoint * grapher->status->nx * grapher->status->ny ;
03381 
03382 #if 0
03383          tsim  = (MRI_IMAGE *) grapher->getser( ll , graCR_getseries ,
03384                                                      grapher->getaux ) ;
03385 #else
03386          CALL_getser( grapher , ll,graCR_getseries , MRI_IMAGE *,tsim ) ;
03387 #endif
03388 
03389          if( tsim != NULL ){
03390            mri_write_1D( wcfname , tsim ) ;  
03391            mri_free( tsim ) ;
03392          }
03393          myXtFree(wcfname) ;
03394       }
03395       break ;
03396 
03397       
03398 
03399 
03400       default:
03401         if( grapher->status->send_CB != NULL ){
03402           GRA_cbs cbs ;
03403 
03404           cbs.reason = graCR_keypress ;
03405           cbs.key    = buf[0] ;
03406           cbs.event  = ev ;
03407 #if 0
03408           grapher->status->send_CB( grapher , grapher->getaux , &cbs ) ;
03409 #else
03410           CALL_sendback( grapher , cbs ) ;
03411 #endif
03412         }
03413       break ;
03414    }
03415 
03416    EXRETURN ;
03417 }
03418 
03419 
03420 
03421 
03422 
03423 void GRA_quit_timeout_CB( XtPointer client_data , XtIntervalId * id )
03424 {
03425    MCW_grapher * grapher = (MCW_grapher *) client_data ;
03426 
03427 ENTRY("GRA_quit_timeout_CB") ;
03428    GRA_handle_keypress( grapher , "q" , NULL ) ;
03429    EXRETURN ;
03430 }
03431 
03432 
03433 
03434 
03435 
03436 void GRA_opt_CB( Widget w , XtPointer client_data , XtPointer call_data )
03437 {
03438    MCW_grapher * grapher = (MCW_grapher *) client_data ;
03439 
03440 ENTRY("GRA_opt_CB") ;
03441 
03442    if( w == grapher->opt_scale_down_pb ){
03443       GRA_handle_keypress( grapher , "-" , NULL ) ;
03444       EXRETURN ;
03445    }
03446 
03447    if( w == grapher->opt_scale_up_pb ){
03448       GRA_handle_keypress( grapher , "+" , NULL ) ;
03449       EXRETURN ;
03450    }
03451 
03452    if( w == grapher->opt_scale_auto_pb ){
03453       GRA_handle_keypress( grapher , "a" , NULL ) ;
03454       EXRETURN ;
03455    }
03456 
03457    if( w == grapher->opt_grid_down_pb ){
03458       GRA_handle_keypress( grapher , "g" , NULL ) ;
03459       EXRETURN ;
03460    }
03461 
03462    if( w == grapher->opt_grid_up_pb ){
03463       GRA_handle_keypress( grapher , "G" , NULL ) ;
03464       EXRETURN ;
03465    }
03466 
03467    if( w == grapher->opt_grid_auto_pb ){     
03468      auto_grid( grapher , NPTS(grapher) ) ;
03469      redraw_graph(grapher,0) ;
03470      EXRETURN ;
03471    }
03472 
03473    if( w == grapher->opt_grid_HorZ_pb ){     
03474       GRA_handle_keypress( grapher , "h" , NULL ) ;
03475       EXRETURN ;
03476    }
03477 
03478    if( w == grapher->opt_slice_down_pb ){
03479       GRA_handle_keypress( grapher , "z" , NULL ) ;
03480       EXRETURN ;
03481    }
03482 
03483    if( w == grapher->opt_slice_up_pb ){
03484       GRA_handle_keypress( grapher , "Z" , NULL ) ;
03485       EXRETURN ;
03486    }
03487 
03488    if( w == grapher->opt_mat_down_pb ){
03489       GRA_handle_keypress( grapher , "m" , NULL ) ;
03490       EXRETURN ;
03491    }
03492 
03493    if( w == grapher->opt_mat_up_pb ){
03494       GRA_handle_keypress( grapher , "M" , NULL ) ;
03495       EXRETURN ;
03496    }
03497 
03498 #if 0
03499    if( w == grapher->opt_color_up_pb ){
03500       GRA_handle_keypress( grapher , "r" , NULL ) ;
03501       EXRETURN ;
03502    }
03503 #endif
03504 
03505    if( w == grapher->opt_quit_pb ){
03506 #if 0
03507       GRA_handle_keypress( grapher , "q" , NULL ) ;
03508 #else
03509 STATUS("User pressed Done button: starting timeout") ;
03510       grapher->valid = 666 ;
03511       (void) XtAppAddTimeOut( XtWidgetToApplicationContext(w) ,
03512                               50 , GRA_quit_timeout_CB , grapher ) ;
03513 #endif
03514       EXRETURN ;
03515    }
03516 
03517    if( w == grapher->opt_save_pb ){
03518       GRA_timer_stop(grapher) ;   
03519       GRA_handle_keypress( grapher , "S" , NULL ) ;
03520       EXRETURN ;
03521    }
03522 
03523    if( w == grapher->opt_write_center_pb ){
03524       GRA_timer_stop(grapher) ;   
03525       GRA_handle_keypress( grapher , "w" , NULL ) ;
03526       EXRETURN ;
03527    }
03528 
03529    if( w == grapher->opt_write_suffix_pb ){
03530       EXRONE(grapher) ;  
03531       GRA_timer_stop(grapher) ;   
03532       MCW_choose_string( grapher->option_rowcol ,
03533                          "'Write Center' Suffix:" , Grapher_Stuff.wcsuffix ,
03534                          GRA_wcsuffix_choose_CB , NULL ) ;
03535       EXRETURN ;
03536    }
03537 
03538    if( w == grapher->opt_scale_choose_pb ){
03539      MCW_choose_integer( grapher->option_rowcol , "Scale" ,
03540                          -9999 , 9999 , (int)(grapher->fscale) ,
03541                          GRA_scale_choose_CB , (XtPointer) grapher ) ;
03542      EXRETURN ;
03543    }
03544 
03545 #ifndef USE_OPTMENUS
03546    if( w == grapher->opt_mat_choose_pb ){
03547      MCW_choose_integer( grapher->option_rowcol , "Matrix" ,
03548                          1 , grapher->mat_max , grapher->mat ,
03549                          GRA_mat_choose_CB , (XtPointer) grapher ) ;
03550      EXRETURN ;
03551    }
03552 #endif
03553 
03554    if( w == grapher->opt_grid_choose_pb ){
03555      MCW_choose_integer( grapher->option_rowcol , "Grid" ,
03556                          grid_ar[0] , grid_ar[GRID_MAX-1] , grapher->grid_spacing ,
03557                          GRA_grid_choose_CB , (XtPointer) grapher ) ;
03558      EXRETURN ;
03559    }
03560 
03561    if( w == grapher->opt_pin_choose_pb ){   
03562      char *lvec[2] = { "Bot" , "Top" } ;
03563      int   ivec[2] ;
03564      ivec[0] = grapher->pin_bot ; ivec[1] = grapher->pin_top ;
03565      MCW_choose_vector( grapher->option_rowcol , "Graph Pins: Bot..Top-1" ,
03566                         2 , lvec,ivec ,
03567                         GRA_pin_choose_CB , (XtPointer) grapher ) ;
03568      EXRETURN ;
03569    }
03570 
03571 #ifndef USE_OPTMENUS
03572    if( w == grapher->opt_slice_choose_pb && grapher->status->nz > 1 ){
03573      MCW_choose_integer( grapher->option_rowcol , "Slice" ,
03574                          0 , grapher->status->nz - 1 , grapher->zpoint ,
03575                          GRA_slice_choose_CB , (XtPointer) grapher ) ;
03576      EXRETURN ;
03577    }
03578 #endif
03579 
03580    
03581 
03582    if( w == grapher->opt_xaxis_clear_pb ){
03583      mri_free( grapher->xax_tsim ) ;
03584      grapher->xax_tsim = NULL ;
03585      GRA_timer_stop(grapher) ;   
03586      redraw_graph( grapher , 0 ) ;
03587      EXRETURN ;
03588    }
03589 
03590    if( w == grapher->opt_xaxis_pick_pb ){
03591      GRA_timer_stop(grapher) ;   
03592      if( IMARR_COUNT(GLOBAL_library.timeseries) > 0 ){
03593        MCW_choose_timeseries( grapher->fdw_graph , "Graph x-axis" ,
03594                               GLOBAL_library.timeseries , -1 ,
03595                               GRA_pick_xaxis_CB , (XtPointer) grapher ) ;
03596      } else {
03597        (void) MCW_popup_message(
03598                  grapher->option_rowcol ,
03599                  "No timeseries library\nexists to pick from!" ,
03600                  MCW_USER_KILL | MCW_TIMER_KILL ) ;
03601      }
03602      EXRETURN ;
03603    }
03604 
03605    if( w == grapher->opt_xaxis_center_pb ){
03606      EXRONE(grapher) ;  
03607      GRA_timer_stop(grapher) ;   
03608      if( grapher->cen_tsim != NULL ){
03609        mri_free( grapher->xax_tsim ) ;
03610        grapher->xax_tsim = mri_to_float( grapher->cen_tsim ) ;
03611        redraw_graph(grapher,0) ;
03612      } else {
03613        XBell(XtDisplay(w),100) ;
03614      }
03615      EXRETURN ;
03616    }
03617 
03618 
03619 
03620    if( w == grapher->opt_baseline_setglobal_pb ){
03621      MCW_choose_integer( grapher->option_rowcol , "Global Baseline" ,
03622                          -29999 , 29999 , (int)(grapher->global_base) ,
03623                          GRA_finalize_global_baseline_CB ,
03624                          (XtPointer) grapher ) ;
03625      EXRETURN ;
03626    }
03627 
03628 
03629 
03630    EXRETURN ;
03631 }
03632 
03633 
03634 
03635 void GRA_fixup_xaxis( MCW_grapher * grapher )  
03636 {
03637    int ii , npt , nx , ibot , nover=0 , pbot,ptop ;
03638    float top,bot , fac ;
03639    float * xxx ;
03640 
03641 ENTRY("GRA_fixup_xaxis") ;
03642 
03643    if( !GRA_VALID(grapher) || grapher->xax_tsim == NULL ) EXRETURN ;
03644 
03645    ptop = TTOP(grapher) ; pbot = TBOT(grapher) ;
03646    npt = ptop ; nx = grapher->xax_tsim->nx ; npt = MIN(npt,nx) ;
03647    xxx = MRI_FLOAT_PTR(grapher->xax_tsim) ;
03648 
03649    ibot = grapher->init_ignore ;
03650    if( ibot >= npt-1 ) ibot = 0 ;
03651    ibot = MAX(ibot,pbot) ;
03652 
03653    
03654 
03655    top = -WAY_BIG ; bot = WAY_BIG ;
03656    for( ii=ibot ; ii < npt ; ii++ ){
03657      if( xxx[ii] < WAY_BIG ){
03658        top = MAX(top,xxx[ii]) ; bot = MIN(bot,xxx[ii]) ;
03659      } else {
03660        nover++ ;
03661      }
03662    }
03663    if( bot >= top ){
03664      mri_free(grapher->xax_tsim) ;
03665      grapher->xax_tsim = NULL ;
03666      EXRETURN ;
03667    }
03668 
03669    
03670 
03671    fac = 1.0 / (top-bot) ;
03672    for( ii=0 ; ii < nx ; ii++ ){
03673      if( xxx[ii] < WAY_BIG ) xxx[ii] = fac * (xxx[ii]-bot) ;
03674      else                    xxx[ii] = 0.0 ;
03675    }
03676 
03677    EXRETURN ;
03678 }
03679 
03680 
03681 
03682 void GRA_pick_xaxis_CB( Widget wcall , XtPointer cd , MCW_choose_cbs *cbs )
03683 {
03684    MCW_grapher *grapher = (MCW_grapher *) cd ;
03685    int its ;
03686    MRI_IMAGE *tsim ;
03687 
03688 ENTRY("GRA_pick_xaxis_CB") ;
03689 
03690    if( !GRA_VALID(grapher) || cbs->reason != mcwCR_timeseries ) EXRETURN ;
03691    GRA_timer_stop( grapher ) ;
03692 
03693    its = cbs->ival ;
03694    if( its >= 0 && its < IMARR_COUNT(GLOBAL_library.timeseries) ){
03695      tsim = IMARR_SUBIMAGE(GLOBAL_library.timeseries,its) ;
03696      mri_free( grapher->xax_tsim ) ;
03697      grapher->xax_tsim = mri_to_float(tsim) ;
03698      redraw_graph( grapher , 0 ) ;
03699    }
03700 
03701    EXRETURN ;
03702 }
03703 
03704 
03705 
03706 
03707 
03708 void GRA_wcsuffix_choose_CB( Widget wcaller , XtPointer cd , MCW_choose_cbs *cbs )
03709 {
03710    int ll , ii ;
03711 
03712 ENTRY("GRA_wcsuffix_choose_CB") ;
03713 
03714    if( cbs->reason != mcwCR_string ||
03715        cbs->cval   == NULL         || (ll=strlen(cbs->cval)) == 0 ){
03716 
03717      XBell( XtDisplay(wcaller) , 100 ) ; EXRETURN ;
03718    }
03719 
03720    for( ii=0 ; ii < ll ; ii++ ){
03721      if( iscntrl(cbs->cval[ii]) ||
03722          isspace(cbs->cval[ii]) ||
03723          cbs->cval[ii] == '/'     ){
03724 
03725         XBell( XtDisplay(wcaller) , 100 ) ; EXRETURN ;
03726      }
03727    }
03728 
03729    if( Grapher_Stuff.wcsuffix != NULL ) myXtFree(Grapher_Stuff.wcsuffix) ;
03730 
03731    Grapher_Stuff.wcsuffix = XtNewString(cbs->cval) ;
03732    EXRETURN ;
03733 }
03734 
03735 
03736 
03737 #ifdef USE_OPTMENUS
03738 void GRA_mat_choose_CB( MCW_arrowval * cbs , XtPointer cd )
03739 #else
03740 void GRA_mat_choose_CB( Widget wcaller , XtPointer cd , MCW_choose_cbs *cbs )
03741 #endif
03742 {
03743    MCW_grapher *grapher = (MCW_grapher *) cd ;
03744 
03745 ENTRY("GRA_mat_choose_CB") ;
03746 
03747    if( ! GRA_VALID(grapher) || cbs->ival < 1 ) EXRETURN ;
03748 
03749    grapher->mat = MIN( grapher->mat_max , cbs->ival ) ;
03750    init_mat    ( grapher ) ;
03751    redraw_graph( grapher , 0 ) ;
03752    send_newinfo( grapher ) ;
03753    EXRETURN ;
03754 }
03755 
03756 
03757 
03758 void GRA_scale_choose_CB( Widget wcaller , XtPointer cd , MCW_choose_cbs *cbs )
03759 {
03760    MCW_grapher *grapher = (MCW_grapher *) cd ;
03761 
03762 ENTRY("GRA_scale_choose_CB") ;
03763 
03764    if( ! GRA_VALID(grapher) ) EXRETURN ;
03765 
03766    grapher->fscale = cbs->fval ;
03767    redraw_graph( grapher , 0 ) ;
03768    EXRETURN ;
03769 }
03770 
03771 
03772 
03773 void GRA_grid_choose_CB( Widget wcaller , XtPointer cd , MCW_choose_cbs *cbs )
03774 {
03775    MCW_grapher *grapher = (MCW_grapher *) cd ;
03776 
03777 ENTRY("GRA_grid_choose_CB") ;
03778 
03779    if( ! GRA_VALID(grapher) ) EXRETURN ;
03780    grapher->grid_spacing = cbs->ival ;
03781    grapher->grid_fixed   = 1 ;  
03782    redraw_graph(grapher,0) ;
03783    EXRETURN ;
03784 }
03785 
03786 
03787 
03788 void GRA_pin_choose_CB( Widget wcaller , XtPointer cd , MCW_choose_cbs *cbs )
03789 {
03790    MCW_grapher *grapher = (MCW_grapher *) cd ;
03791    float *vec = (float *)(cbs->cval) ;
03792    int pb=(int)vec[0] , pt=(int)vec[1] , ii ;
03793 
03794 ENTRY("GRA_pin_choose_CB") ;
03795 
03796    GRA_timer_stop( grapher ) ;
03797 
03798    if( pb >= grapher->status->num_series-2 ||
03799        pb <  0                             ||
03800        (pt > 0 && pt-pb < 2)                 ){   
03801 
03802       XBell(grapher->dc->display,100) ; EXRETURN ;
03803    }
03804 
03805    ii = 100000*pt + pb ;
03806    drive_MCW_grapher( grapher , graDR_setpins , (XtPointer)(ii) ) ;
03807    EXRETURN ;
03808 }
03809 
03810 
03811 
03812 void GRA_ggap_CB( MCW_arrowval *cbs , XtPointer cd )  
03813 {
03814    MCW_grapher *grapher = (MCW_grapher *) cd ;
03815    int gg ;
03816 
03817 ENTRY("GRA_ggap_CB") ;
03818 
03819    if( ! GRA_VALID(grapher) ) EXRETURN ;
03820 
03821    gg = grapher->ggap ; grapher->ggap = cbs->ival ;
03822    if( gg != grapher->ggap ){
03823      init_mat( grapher ) ; redraw_graph( grapher , 0 ) ;
03824    }
03825 
03826    EXRETURN ;
03827 }
03828 
03829 
03830 
03831 void GRA_gthick_CB( MCW_arrowval *cbs , XtPointer cd )  
03832 {
03833    MCW_grapher *grapher = (MCW_grapher *) cd ;
03834    int gg ;
03835 
03836 ENTRY("GRA_gthick_CB") ;
03837 
03838    if( ! GRA_VALID(grapher) ) EXRETURN ;
03839 
03840    gg = grapher->gthick ; grapher->gthick = cbs->ival ;
03841    if( gg != grapher->gthick ){
03842      init_mat( grapher ) ; redraw_graph( grapher , 0 ) ;
03843    }
03844 
03845    EXRETURN ;
03846 }
03847 
03848 
03849 
03850 #ifdef USE_OPTMENUS
03851 void GRA_slice_choose_CB( MCW_arrowval *cbs , XtPointer cd )
03852 #else
03853 void GRA_slice_choose_CB( Widget wcaller , XtPointer cd , MCW_choose_cbs *cbs )
03854 #endif
03855 {
03856    MCW_grapher *grapher = (MCW_grapher *) cd ;
03857 
03858 ENTRY("GRA_slice_choose_CB") ;
03859 
03860    if( ! GRA_VALID(grapher) ) EXRETURN ;
03861 
03862    grapher->zpoint = cbs->ival ;
03863    if( grapher->zpoint >= grapher->status->nz )
03864       grapher->zpoint = grapher->status->nz - 1 ;
03865    redraw_graph( grapher , 0 ) ;
03866    send_newinfo( grapher ) ;
03867    EXRETURN ;
03868 }
03869 
03870 
03871 
03872 #ifdef USE_OPTMENUS
03873 void GRA_ignore_choose_CB( MCW_arrowval * cbs , XtPointer cd )
03874 #else
03875 void GRA_ignore_choose_CB( Widget wcaller , XtPointer cd , MCW_choose_cbs *cbs )
03876 #endif
03877 {
03878    MCW_grapher *grapher = (MCW_grapher *) cd ;
03879 
03880 ENTRY("GRA_ignore_choose_CB") ;
03881 
03882    if( ! GRA_VALID(grapher) || grapher->status->send_CB == NULL ) EXRETURN ;
03883 
03884    if( cbs->ival >= 0 && cbs->ival < TTOP(grapher)-1 ){
03885       GRA_cbs gbs ;
03886 
03887       gbs.reason = graCR_setignore ;
03888       gbs.key    = cbs->ival ;
03889 #if 0
03890       grapher->status->send_CB( grapher , grapher->getaux , &gbs ) ;
03891 #else
03892       CALL_sendback( grapher , gbs ) ;
03893 #endif
03894    }
03895    EXRETURN ;
03896 }
03897 
03898 
03899 
03900 #ifdef USE_OPTMENUS
03901 void GRA_polort_choose_CB( MCW_arrowval * cbs , XtPointer cd )
03902 #else
03903 void GRA_polort_choose_CB( Widget wcaller , XtPointer cd , MCW_choose_cbs * cbs )
03904 #endif
03905 {
03906    MCW_grapher * grapher = (MCW_grapher *) cd ;
03907 
03908 ENTRY("GRA_polort_choose_CB") ;
03909 
03910    if( ! GRA_VALID(grapher) || grapher->status->send_CB == NULL ) EXRETURN ;
03911 
03912    if( cbs->ival >= 0 && cbs->ival <= MAX_POLORT ){
03913       GRA_cbs gbs ;
03914 
03915       gbs.reason = graCR_polort ;
03916       gbs.key    = cbs->ival ;
03917 #if 0
03918       grapher->status->send_CB( grapher , grapher->getaux , &gbs ) ;
03919 #else
03920       CALL_sendback( grapher , gbs ) ;
03921 #endif
03922    }
03923    EXRETURN ;
03924 }
03925 
03926 
03927 
03928 void GRA_bkthr_choose_CB( Widget wcaller , XtPointer cd , MCW_choose_cbs * cbs )
03929 {
03930 ENTRY("GRA_bkthr_choose_CB") ;
03931    SET_FIM_bkthr( cbs->fval ) ;
03932    EXRETURN ;
03933 }
03934 
03935 
03936 
03937 void GRA_refread_choose_CB( Widget wcaller , XtPointer cd , MCW_choose_cbs * cbs )
03938 {
03939    MCW_grapher * grapher = (MCW_grapher *) cd ;
03940    MRI_IMAGE * flim ;
03941    float * far ;
03942    int ii ;
03943    GRA_cbs gbs ;
03944 
03945 ENTRY("GRA_refread_choose_CB") ;
03946 
03947    if( ! GRA_VALID(grapher)             ||
03948        grapher->status->send_CB == NULL ||
03949        cbs->reason != mcwCR_string      ||
03950        cbs->cval == NULL                || strlen(cbs->cval) == 0 ) EXRETURN ;
03951 
03952    EXRONE(grapher) ;  
03953 
03954    flim = mri_read_1D( cbs->cval ) ;     
03955    if( flim == NULL || flim->nx < 2 ){
03956       XBell(grapher->dc->display,100) ; mri_free(flim) ; EXRETURN ;
03957    }
03958 
03959    far = MRI_FLOAT_PTR(flim) ;
03960    for( ii=0 ; ii < flim->nvox ; ii++ )
03961       if( fabs(far[ii]) >= 33333.0 ) far[ii] = WAY_BIG ;
03962 
03963    { GRA_cbs cbs; cbs.reason=graCR_winaver; CALL_sendback(grapher,cbs); }
03964    MCW_set_bbox( grapher->fmenu->fim_editref_winaver_bbox , 0 ) ;
03965    gbs.reason   = graCR_refequals ;
03966    gbs.userdata = (XtPointer) flim ;
03967 #if 0
03968    grapher->status->send_CB( grapher , grapher->getaux , &gbs ) ;
03969 #else
03970    CALL_sendback( grapher , gbs ) ;
03971 #endif
03972    mri_free(flim) ;
03973    EXRETURN ;
03974 }
03975 
03976 
03977 
03978 void GRA_refstore_choose_CB( Widget wcaller , XtPointer cd , MCW_choose_cbs * cbs )
03979 {
03980    MCW_grapher * grapher = (MCW_grapher *) cd ;
03981 
03982 ENTRY("GRA_refstore_choose_CB") ;
03983 
03984    if( ! GRA_VALID(grapher)             ||
03985        grapher->ref_ts == NULL          ||
03986        IMARR_COUNT(grapher->ref_ts) < 1 ||
03987        cbs->reason != mcwCR_string      ||
03988        cbs->cval == NULL                || strlen(cbs->cval) == 0 ) EXRETURN ;
03989 
03990    EXRONE(grapher) ;  
03991 
03992    PLUTO_register_timeseries( cbs->cval , IMARR_SUBIMAGE(grapher->ref_ts,0) ) ;
03993    EXRETURN ;
03994 }
03995 
03996 
03997 
03998 void GRA_refwrite_choose_CB( Widget wcaller , XtPointer cd , MCW_choose_cbs * cbs )
03999 {
04000    MCW_grapher * grapher = (MCW_grapher *) cd ;
04001    MRI_IMAGE * tsim ;
04002    int ii , ll ;
04003    GRA_cbs gbs ;
04004 
04005 ENTRY("GRA_refwrite_choose_CB") ;
04006 
04007    if( ! GRA_VALID(grapher)             ||
04008        grapher->ref_ts == NULL          ||
04009        IMARR_COUNT(grapher->ref_ts) < 1 ||
04010        cbs->reason != mcwCR_string      ||
04011        cbs->cval == NULL                || (ll=strlen(cbs->cval)) == 0 ) EXRETURN ;
04012 
04013    EXRONE(grapher) ;  
04014 
04015    for( ii=0 ; ii < ll ; ii++ ){
04016       if( iscntrl(cbs->cval[ii]) || isspace(cbs->cval[ii]) ||
04017           cbs->cval[ii] == '/'   || cbs->cval[ii] == ';'   ||
04018           cbs->cval[ii] == '*'   || cbs->cval[ii] == '?'   ||
04019           cbs->cval[ii] == '&'   || cbs->cval[ii] == '|'   ||
04020           cbs->cval[ii] == '"'   || cbs->cval[ii] == '>'   ||
04021           cbs->cval[ii] == '<'   || cbs->cval[ii] == '\''  ||
04022           cbs->cval[ii] == '['   || cbs->cval[ii] == ']'     ){
04023 
04024          XBell( XtDisplay(wcaller) , 100 ) ; EXRETURN ;
04025       }
04026    }
04027 
04028 #if 0
04029    tsim = mri_transpose( IMARR_SUBIMAGE(grapher->ref_ts,0) ) ;
04030    mri_write_ascii( cbs->cval , tsim ) ;
04031    mri_free( tsim ) ;
04032 #else
04033    mri_write_1D( cbs->cval , IMARR_SUBIMAGE(grapher->ref_ts,0) ) ; 
04034 #endif
04035 
04036    
04037 
04038    if( grapher->status->send_CB != NULL ){
04039       mri_add_name( cbs->cval , IMARR_SUBIMAGE(grapher->ref_ts,0) ) ;
04040       gbs.reason   = graCR_timeseries_library ;
04041       gbs.userdata = (XtPointer) IMARR_SUBIMAGE(grapher->ref_ts,0) ;
04042 #if 0
04043       grapher->status->send_CB( grapher , grapher->getaux , &gbs ) ;
04044 #else
04045       CALL_sendback( grapher , gbs ) ;
04046 #endif
04047    }
04048    EXRETURN ;
04049 }
04050 
04051 
04052 
04053 
04054 
04055 
04056 
04057 
04058 
04059 
04060 
04061 
04062 
04063 
04064 
04065 
04066 
04067 
04068 
04069 
04070 
04071 
04072 
04073 
04074 
04075 
04076 
04077 
04078 
04079 
04080 
04081 
04082 
04083 
04084 
04085 
04086 
04087 
04088 
04089 
04090 
04091 
04092 
04093 
04094 
04095 
04096 
04097 
04098 
04099 
04100 
04101 
04102 
04103 
04104 
04105 
04106 
04107 
04108 
04109 
04110 
04111 
04112 
04113 
04114 
04115 
04116 
04117 
04118 
04119 
04120 
04121 
04122 
04123 
04124 
04125 Boolean drive_MCW_grapher( MCW_grapher * grapher ,
04126                            int drive_code , XtPointer drive_data )
04127 {
04128 
04129 ENTRY("drive_MCW_grapher") ;
04130 
04131    if( ! GRA_VALID(grapher) ) RETURN(False) ;
04132 
04133    switch( drive_code ){
04134 
04135       
04136 
04137       default:{
04138          fprintf(stderr,"\a\n*** drive_MCW_grapher: code=%d illegal!\n",
04139                  drive_code) ;
04140          XBell( grapher->dc->display , 100 ) ;
04141          RETURN( False ) ;
04142       }
04143 
04144       
04145 
04146       case graDR_winaver:{
04147         int vvv = (int)drive_data ;
04148         MCW_set_bbox( grapher->fmenu->fim_editref_winaver_bbox , vvv ) ;
04149         RETURN( True ) ;
04150       }
04151 
04152       
04153 
04154       case graDR_setglobalbaseline:{
04155          float *vvv = (float *) drive_data ;   
04156          int ii = thd_floatscan( 1 , vvv ) ;   
04157          MCW_choose_cbs cb ;
04158          if( ii != 0 ) RETURN( False ) ;       
04159          cb.reason = mcwCR_integer ;
04160          cb.fval   = *vvv ;
04161          GRA_finalize_global_baseline_CB(NULL,(XtPointer)grapher,&cb) ;
04162          RETURN( True ) ;
04163       }
04164 
04165       
04166 
04167       case graDR_setmatrix:{
04168          int mm = (int) drive_data ;
04169          if( mm < 0 ) RETURN( False ) ;
04170          grapher->mat = MIN( grapher->mat_max , mm ) ;
04171          init_mat    ( grapher ) ;
04172          redraw_graph( grapher, PLOTCODE_AUTOSCALE ); 
04173          send_newinfo( grapher ) ;
04174          RETURN( True ) ;
04175       }
04176 
04177       
04178 
04179       case graDR_setgrid:{
04180          int mm = (int) drive_data ;
04181          if( mm < 2 ) RETURN( False ) ;
04182          grapher->grid_spacing = mm ;
04183          grapher->grid_fixed   = 1 ;  
04184          redraw_graph(grapher,0) ;
04185          RETURN( True ) ;
04186       }
04187 
04188       
04189 
04190       case graDR_mirror:{
04191          int ii = (int) drive_data ;
04192 
04193          if( ii != grapher->mirror ){
04194            grapher->mirror = ii ;
04195            init_mat( grapher ) ; redraw_graph( grapher , 0 ) ;
04196          }
04197       }
04198       break ;
04199 
04200       
04201 
04202       case graDR_fim_disable:{
04203          int ii ;
04204          XtUnmanageChild( grapher->fmenu->fim_cbut ) ;
04205          XtUnmanageChild( grapher->opt_xaxis_cbut ) ;
04206          for( ii=0 ; ii < NUM_COLOR_ITEMS ; ii++ ){
04207            if( gr_unfim[ii] ){
04208              if( grapher->opt_color_av[ii] != NULL )
04209                XtUnmanageChild( grapher->opt_color_av[ii]->wrowcol ) ;
04210              if( grapher->opt_thick_bbox[ii] != NULL )
04211                XtUnmanageChild( grapher->opt_thick_bbox[ii]->wtop  ) ;
04212              if( grapher->opt_points_bbox[ii] != NULL )
04213                XtUnmanageChild( grapher->opt_points_bbox[ii]->wtop ) ;
04214            }
04215          }
04216       }
04217       break ;
04218 
04219       
04220 
04221       case graDR_button2_enable:{
04222         grapher->button2_enabled = 1 ;
04223         GRA_timer_stop(grapher) ;   
04224         RETURN( True ) ;
04225       }
04226 
04227       case graDR_button2_disable:{
04228         grapher->button2_enabled = 0 ;
04229         RETURN( True ) ;
04230       }
04231 
04232       
04233 
04234       case graDR_setindex:{
04235          int new_index = (int) drive_data ;
04236 
04237          if( new_index < 0 || new_index >= grapher->status->num_series )
04238            RETURN( False ) ;
04239 
04240          if( new_index != grapher->time_index ){
04241            grapher->time_index = new_index ;
04242            if( grapher->textgraph )
04243              redraw_graph( grapher , 0 ) ;
04244            else
04245              GRA_redraw_overlay( grapher ) ;
04246          }
04247          RETURN( True ) ;
04248       }
04249 
04250       
04251       
04252 
04253       case graDR_newlength:{
04254          int newtop=(int)drive_data ;
04255 
04256          if( newtop < MIN_PIN ) newtop = 0 ;
04257          if( newtop > MAX_PIN ) newtop = MAX_PIN ;
04258 
04259          grapher->pin_top = newtop ;
04260          if( NPTS(grapher) < 2 ) grapher->pin_bot = 0 ;
04261 
04262 #ifdef USE_OPTMENUS
04263          GRA_fix_optmenus( grapher ) ;
04264 #endif
04265          redraw_graph( grapher, 0 ) ;
04266          RETURN( True ) ;
04267       }
04268 
04269       
04270 
04271       case graDR_setpinbot:{
04272          int newbot=(int)drive_data ;
04273 
04274          if( newbot < 0               ) newbot = 0 ;
04275          if( newbot >= TTOP(grapher)  ) newbot = 0 ;
04276 
04277          grapher->pin_bot = newbot ;
04278 
04279 #ifdef USE_OPTMENUS
04280          GRA_fix_optmenus( grapher ) ;
04281 #endif
04282          redraw_graph( grapher, 0 ) ;
04283          RETURN( True ) ;
04284       }
04285 
04286       
04287 
04288       case graDR_setpins:{
04289          int ii=(int)drive_data , newbot,newtop ;
04290 
04291          if( ii <= 0 ){
04292            newbot = newtop = 0 ;
04293          } else {
04294            newbot = ii % 100000 ;
04295            newtop = ii / 100000 ;
04296            if( newtop < MIN_PIN ) newtop = 0 ;
04297          }
04298          if( newtop > 0 && newtop-newbot < 2 ) newbot = 0 ;
04299          if( newbot >= TTOP(grapher)         ) newbot = 0 ;
04300 
04301          grapher->pin_bot = newbot ;
04302          grapher->pin_top = newtop ;
04303 
04304 #ifdef USE_OPTMENUS
04305          GRA_fix_optmenus( grapher ) ;
04306 #endif
04307          redraw_graph( grapher, 0 ) ;
04308          RETURN( True ) ;
04309       }
04310 
04311       
04312 
04313       case graDR_setignore:{
04314          int new_ignore = (int) drive_data ;
04315 
04316          if( new_ignore >= 0 && new_ignore < TTOP(grapher)-1 ){
04317            grapher->init_ignore = new_ignore ;
04318            redraw_graph( grapher , PLOTCODE_AUTOSCALE ) ;
04319            RETURN( True ) ;
04320          } else {
04321            RETURN( False ) ;
04322          }
04323       }
04324 
04325       
04326 
04327       case graDR_polort:{
04328          int new_polort = (int) drive_data ;
04329 
04330          if( new_polort >= 0 ){
04331             grapher->polort = new_polort ;
04332 #ifdef USE_OPTMENUS
04333             GRA_fix_optmenus( grapher ) ;
04334 #endif
04335          }
04336          RETURN( True ) ;
04337       }
04338 
04339       
04340 
04341       case graDR_addref_ts:{
04342          MRI_IMAGE *im = (MRI_IMAGE *) drive_data ;
04343 
04344          if( im == NULL ){                
04345 STATUS("freeing reference timeseries") ;
04346             FREE_IMARR(grapher->ref_ts) ;
04347             grapher->ref_ts = NULL ;
04348          } else{
04349             if( grapher->ref_ts == NULL ) INIT_IMARR( grapher->ref_ts ) ;
04350 
04351             if( IMARR_COUNT(grapher->ref_ts) == 0 ){
04352 STATUS("adding reference timeseries") ;
04353                ADDTO_IMARR( grapher->ref_ts , im ) ;     
04354             } else {
04355 STATUS("replacing reference timeseries") ;
04356                IMARR_SUBIMAGE(grapher->ref_ts,0) = im ;  
04357             }
04358          }
04359 
04360          redraw_graph( grapher , 0 ) ;
04361          RETURN( True ) ;
04362       }
04363 
04364       
04365 
04366       case graDR_addort_ts:{
04367          MRI_IMAGE *im = (MRI_IMAGE *) drive_data ;
04368 
04369          if( im == NULL ){                
04370 STATUS("freeing ort timeseries") ;
04371             FREE_IMARR(grapher->ort_ts) ;
04372             grapher->ort_ts = NULL ;
04373          } else{
04374             if( grapher->ort_ts == NULL ) INIT_IMARR( grapher->ort_ts ) ;
04375 
04376             if( IMARR_COUNT(grapher->ort_ts) == 0 ){
04377 STATUS("adding ort timeseries") ;
04378                ADDTO_IMARR( grapher->ort_ts , im ) ;     
04379             } else {
04380 STATUS("replacing ort timeseries") ;
04381                IMARR_SUBIMAGE(grapher->ort_ts,0) = im ;  
04382             }
04383          }
04384 
04385          redraw_graph( grapher , 0 ) ;
04386          RETURN( True ) ;
04387       }
04388 
04389       
04390 
04391       case graDR_icon:{
04392          int xret , yret ;
04393          unsigned int wret,hret,bret,dret ;
04394          Window rret ;
04395 
04396          XtVaSetValues( grapher->fdw_graph , XmNiconPixmap , (Pixmap) drive_data , NULL ) ;
04397          grapher->glogo_pixmap = (Pixmap) drive_data ;
04398 
04399          
04400 
04401          if( grapher->glogo_pixmap != XmUNSPECIFIED_PIXMAP ){
04402             (void) XGetGeometry( grapher->dc->display , grapher->glogo_pixmap ,
04403                                  &rret , &xret , &yret , &wret , &hret , &bret , &dret ) ;
04404 
04405             grapher->glogo_width  = wret ;
04406             grapher->glogo_height = hret ;
04407          } else {
04408             grapher->glogo_width  = 0 ;
04409             grapher->glogo_height = 0 ;
04410          }
04411          RETURN( True ) ;
04412       }
04413 
04414       
04415 
04416       case graDR_title:{
04417          char * title = (char *) drive_data ;
04418 
04419          if( title == NULL || strlen(title) == 0 ) RETURN( False ) ;
04420 
04421          XtVaSetValues( grapher->fdw_graph , XmNtitle , title , NULL ) ;
04422          RETURN( True ) ;
04423       }
04424 
04425       
04426 
04427       case graDR_destroy:{
04428          end_fd_graph_CB( NULL , (XtPointer) grapher , NULL ) ;
04429          RETURN( True ) ;
04430       }
04431 
04432       
04433 
04434       case graDR_unrealize:{
04435          GRA_timer_stop(grapher) ;   
04436          if( GRA_REALZ(grapher) ) XtUnrealizeWidget( grapher->fdw_graph ) ;
04437          grapher->valid = 1 ;
04438 
04439          if( grapher->fd_pxWind != (Pixmap) 0 )
04440             XFreePixmap( grapher->dc->display , grapher->fd_pxWind ) ;
04441          RETURN( True ) ;
04442       }
04443 
04444       
04445 
04446       case graDR_realize:{
04447          if( ! GRA_REALZ(grapher) ){
04448             int width , height ;
04449 
04450             grapher->valid = 2 ;
04451 
04452             XtRealizeWidget( grapher->fdw_graph ) ;
04453 
04454             WAIT_for_window(grapher->fdw_graph) ;
04455 
04456             
04457 
04458             XtVaSetValues( grapher->option_rowcol ,
04459                              XmNleftAttachment   , XmATTACH_NONE ,
04460                              XmNtopAttachment    , XmATTACH_NONE ,
04461                              XmNrightAttachment  , XmATTACH_FORM ,
04462                              XmNbottomAttachment , XmATTACH_FORM ,
04463                            NULL ) ;
04464             XMapRaised( XtDisplay(grapher->option_rowcol) ,
04465                         XtWindow(grapher->option_rowcol)   ) ;
04466 
04467             NORMAL_cursorize( grapher->fdw_graph ) ;
04468 
04469             if( ISONE(grapher) )
04470               NORMAL_cursorize( grapher->draw_fd ) ;  
04471             else
04472               POPUP_cursorize( grapher->draw_fd ) ;
04473 
04474             MCW_widget_geom( grapher->draw_fd , &width , &height , NULL,NULL ) ;
04475             GRA_new_pixmap( grapher , width , height , 0 ) ;
04476 #ifdef USE_OPTMENUS
04477             GRA_fix_optmenus( grapher ) ;
04478 #endif
04479             AFNI_sleep(1) ;  
04480          }
04481          RETURN( True ) ;
04482       }
04483 
04484       
04485 
04486       case graDR_cursor:{
04487          int cur = (int) drive_data ;
04488 
04489          MCW_alter_widget_cursor( grapher->fdw_graph , cur , "yellow" , "blue" ) ;
04490          RETURN( True ) ;
04491       }
04492 
04493       
04494 
04495       case graDR_newdata:{
04496          int npold = grapher->status->num_series ;  
04497 
04498          GRA_timer_stop(grapher) ;   
04499 
04500          grapher->getaux = drive_data ;
04501 #if 0
04502          grapher->status = (MCW_grapher_status *)
04503                               grapher->getser(0,graCR_getstatus,drive_data) ;
04504 #else
04505          CALL_getser( grapher , 0,graCR_getstatus , MCW_grapher_status *,grapher->status ) ;
04506 #endif
04507          init_const( grapher ) ;
04508 
04509          
04510 
04511          if( grapher->pin_bot >= grapher->status->num_series-1 ) grapher->pin_bot = 0 ;
04512 
04513          if( npold < 2 || !grapher->grid_fixed ){ 
04514            auto_grid( grapher , NPTS(grapher) ) ;
04515          }
04516 
04517 #ifdef USE_OPTMENUS
04518          GRA_fix_optmenus( grapher ) ;
04519 #endif
04520 
04521         if( ISONE(grapher) )                        
04522           NORMAL_cursorize( grapher->draw_fd ) ;
04523         else
04524           POPUP_cursorize( grapher->draw_fd ) ;
04525 
04526          RETURN( True ) ;
04527       }
04528 
04529       
04530 
04531       case graDR_redraw:{
04532          int *xym = (int *) drive_data ;
04533 
04534 STATUS("graDR_redraw") ;
04535 
04536          if( xym != NULL ){
04537             if( xym[0] >= 0 ) grapher->xpoint = xym[0] ;
04538             if( xym[1] >= 0 ) grapher->ypoint = xym[1] ;
04539             if( xym[2] >= 0 ) grapher->zpoint = xym[2] ;
04540             if( xym[3] >  0 ) grapher->mat    = xym[3] ;
04541             init_mat(grapher) ;
04542          }
04543          redraw_graph( grapher , 0 ) ;
04544          RETURN( True ) ;
04545       }
04546 
04547    }  
04548 
04549    RETURN( False ) ;  
04550 }
04551 
04552 
04553 
04554 
04555 
04556 void GRA_fim_CB( Widget w , XtPointer client_data , XtPointer call_data )
04557 {
04558    FIM_menu * fm = (FIM_menu *) client_data ;
04559    MCW_grapher * grapher = (MCW_grapher *) fm->parent ;
04560    GRA_cbs cbs ;
04561 
04562 ENTRY("GRA_fim_CB") ;
04563 
04564    if( ! GRA_VALID(grapher) || grapher->status->send_CB == NULL ) EXRETURN ;
04565 
04566    EXRONE(grapher) ;  
04567 
04568    
04569 
04570    
04571 
04572    if( w == grapher->fmenu->fim_pickref_pb ){
04573       cbs.reason = graCR_pickref ;
04574 #if 0
04575       grapher->status->send_CB( grapher , grapher->getaux , &cbs ) ;
04576 #else
04577       CALL_sendback( grapher , cbs ) ;
04578 #endif
04579       grapher->tschosen = 1 ;  
04580    }
04581 
04582    
04583 
04584    else if( w == grapher->fmenu->fim_pickort_pb ){
04585       cbs.reason = graCR_pickort ;
04586 #if 0
04587       grapher->status->send_CB( grapher , grapher->getaux , &cbs ) ;
04588 #else
04589       CALL_sendback( grapher , cbs ) ;
04590 #endif
04591       grapher->tschosen = 1 ;  
04592    }
04593 
04594    
04595 
04596    else if( w == grapher->fmenu->fim_editref_clear_pb ){
04597       cbs.reason = graCR_clearfim ;
04598 #if 0
04599       grapher->status->send_CB( grapher , grapher->getaux , &cbs ) ;
04600 #else
04601       CALL_sendback( grapher , cbs ) ;
04602 #endif
04603    }
04604 
04605    
04606 
04607    else if( w == grapher->fmenu->fim_editort_clear_pb ){
04608       cbs.reason = graCR_clearort ;
04609 #if 0
04610       grapher->status->send_CB( grapher , grapher->getaux , &cbs ) ;
04611 #else
04612       CALL_sendback( grapher , cbs ) ;
04613 #endif
04614    }
04615 
04616    
04617 
04618    else if( w == grapher->fmenu->fim_editref_equals_pb ||
04619             w == grapher->fmenu->fim_editref_add_pb      ){
04620       int ll ;
04621       MRI_IMAGE * tsim ;
04622 
04623       ll = grapher->xpoint +
04624            grapher->ypoint * grapher->status->nx +
04625            grapher->zpoint * grapher->status->nx * grapher->status->ny ;
04626 
04627 #if 0
04628       tsim  = (MRI_IMAGE *) grapher->getser( ll , graCR_getseries ,
04629                                                   grapher->getaux ) ;
04630 #else
04631       CALL_getser( grapher , ll,graCR_getseries , MRI_IMAGE *,tsim ) ;
04632 #endif
04633 
04634       if( tsim != NULL ){
04635          { GRA_cbs cbs; cbs.reason=graCR_winaver; CALL_sendback(grapher,cbs); }
04636          MCW_set_bbox( grapher->fmenu->fim_editref_winaver_bbox , 0 ) ;
04637          cbs.reason   = (w == grapher->fmenu->fim_editref_equals_pb)
04638                         ? graCR_refequals : graCR_refadd ;
04639          cbs.userdata = (XtPointer) tsim ;
04640 #if 0
04641          grapher->status->send_CB( grapher , grapher->getaux , &cbs ) ;
04642 #else
04643          CALL_sendback( grapher , cbs ) ;
04644 #endif
04645          mri_free( tsim ) ;
04646       }
04647    }
04648 
04649    
04650 
04651    else if( w == grapher->fmenu->fim_editref_read_pb ){
04652       MCW_choose_string( grapher->option_rowcol ,
04653                          "Ideal Input Filename:" , NULL ,
04654                          GRA_refread_choose_CB , (XtPointer) grapher ) ;
04655    }
04656 
04657    else if( w == grapher->fmenu->fim_editref_write_pb ){
04658       if( grapher->ref_ts != NULL && IMARR_COUNT(grapher->ref_ts) > 0 ){
04659          MCW_choose_string( grapher->option_rowcol ,
04660                             "Ideal Output Filename:" , NULL ,
04661                             GRA_refwrite_choose_CB , (XtPointer) grapher ) ;
04662       } else {
04663          XBell( grapher->dc->display , 100 ) ;
04664       }
04665    }
04666 
04667    else if( w == grapher->fmenu->fim_editref_store_pb ){
04668       if( grapher->ref_ts != NULL && IMARR_COUNT(grapher->ref_ts) > 0 ){
04669          MCW_choose_string( grapher->option_rowcol ,
04670                             "Label to Store Ideal:" , NULL ,
04671                             GRA_refstore_choose_CB , (XtPointer) grapher ) ;
04672       } else {
04673          XBell( grapher->dc->display , 100 ) ;
04674       }
04675    }
04676 
04677    else if( w == grapher->fmenu->fim_editref_smooth_pb ){
04678       cbs.reason = graCR_refsmooth ;
04679       if( grapher->ref_ts != NULL && IMARR_COUNT(grapher->ref_ts) > 0 ){
04680          { GRA_cbs cbs; cbs.reason=graCR_winaver; CALL_sendback(grapher,cbs); }
04681          MCW_set_bbox( grapher->fmenu->fim_editref_winaver_bbox , 0 ) ;
04682 #if 0
04683          grapher->status->send_CB( grapher , grapher->getaux , &cbs ) ;
04684 #else
04685          CALL_sendback( grapher , cbs ) ;
04686 #endif
04687       } else {
04688          XBell( grapher->dc->display , 100 ) ;
04689       }
04690    }
04691 
04692    
04693 
04694    else if( w == grapher->fmenu->fim_editref_setshift_pb ){
04695       GRA_setshift_startup( grapher ) ;
04696    }
04697 
04698    
04699 
04700    else if( w == grapher->fmenu->fim_execute_pb ){
04701       int val = MCW_val_bbox(grapher->fmenu->fim_opt_bbox) ;
04702       cbs.reason = graCR_dofim ;
04703       switch(val){
04704          default:  cbs.key = FIM_ALPHA_MASK | FIM_CORR_MASK ; break ;
04705          case 2:   cbs.key = FIM_PERC_MASK  | FIM_CORR_MASK ; break ;
04706          case 4:   cbs.key = FIM_PAVE_MASK  | FIM_CORR_MASK ; break ;
04707          case 3:   cbs.key = FIM_PTOP_MASK  | FIM_CORR_MASK ; break ;
04708       }
04709       cbs.mat = 0 ; 
04710       GRA_timer_stop(grapher) ;   
04711 #if 0
04712       grapher->status->send_CB( grapher , grapher->getaux , &cbs ) ;
04713 #else
04714       CALL_sendback( grapher , cbs ) ;
04715 #endif
04716    }
04717 
04718    else if( w == grapher->fmenu->fim_execfimp_pb ){
04719       cbs.reason = graCR_dofim ;
04720       cbs.key    = MCW_val_bbox(grapher->fmenu->fimp_opt_bbox) ;
04721       cbs.mat    = MCW_val_bbox(grapher->fmenu->fimp_user_bbox) ; 
04722       GRA_timer_stop(grapher) ;   
04723       if( cbs.key || cbs.mat )
04724 #if 0
04725          grapher->status->send_CB( grapher , grapher->getaux , &cbs ) ;
04726 #else
04727          CALL_sendback( grapher , cbs ) ;
04728 #endif
04729       else
04730          XBell( grapher->dc->display , 100 ) ;
04731    }
04732 
04733    
04734 
04735    else if( w == grapher->fmenu->fimp_setdefault_pb ){
04736      char * ff = my_getenv( "AFNI_FIM_MASK" ) ; int mm=0 ;
04737      if( ff != NULL ) mm = strtol(ff,NULL,10) ;
04738      if( mm <= 0 ) mm = FIM_DEFAULT_MASK ;
04739      MCW_set_bbox( grapher->fmenu->fimp_opt_bbox , mm ) ;
04740    }
04741 
04742    else if( w == grapher->fmenu->fimp_setall_pb ){
04743       int mm = (2 << FIM_NUM_OPTS) - 1 ;
04744       MCW_set_bbox( grapher->fmenu->fimp_opt_bbox , mm ) ;
04745    }
04746 
04747    else if( w == grapher->fmenu->fimp_unsetall_pb ){
04748       MCW_set_bbox( grapher->fmenu->fimp_opt_bbox , 0 ) ;
04749    }
04750 
04751    
04752 
04753    else if( w == grapher->fmenu->fim_plot_firstref_pb ){
04754       if( grapher->ref_ts_plotall != 0 ){
04755          grapher->ref_ts_plotall = 0 ;
04756          redraw_graph( grapher , 0 ) ;
04757       }
04758    }
04759 
04760    else if( w == grapher->fmenu->fim_plot_allrefs_pb ){
04761       if( grapher->ref_ts_plotall == 0 ){
04762          grapher->ref_ts_plotall = 1 ;
04763          redraw_graph( grapher , 0 ) ;
04764       }
04765    }
04766 
04767    
04768 
04769    else if( w == grapher->fmenu->fim_ignore_down_pb && grapher->status->send_CB != NULL ){
04770       GRA_cbs cbs ;
04771 
04772       cbs.reason = graCR_setignore ;
04773       cbs.key    = grapher->init_ignore - 1 ;
04774 #if 0
04775       grapher->status->send_CB( grapher , grapher->getaux , &cbs ) ;
04776 #else
04777       CALL_sendback( grapher , cbs ) ;
04778 #endif
04779    }
04780 
04781    else if( w == grapher->fmenu->fim_ignore_up_pb && grapher->status->send_CB != NULL ){
04782       GRA_cbs cbs ;
04783 
04784       cbs.reason = graCR_setignore ;
04785       cbs.key    = grapher->init_ignore + 1 ;
04786 #if 0
04787       grapher->status->send_CB( grapher , grapher->getaux , &cbs ) ;
04788 #else
04789       CALL_sendback( grapher , cbs ) ;
04790 #endif
04791    }
04792 
04793    else if( w == grapher->fmenu->fim_ignore_choose_pb && grapher->status->send_CB != NULL ){
04794 #ifdef USE_OPTMENUS
04795       GRA_ignore_choose_CB( grapher->fmenu->fim_ignore_choose_av , grapher ) ;
04796 #else
04797       MCW_choose_integer( grapher->option_rowcol , "Initial Ignore" ,
04798                           0 , grapher->status->num_series-1 , grapher->init_ignore ,
04799                           GRA_ignore_choose_CB , (XtPointer) grapher ) ;
04800 #endif
04801    }
04802 
04803    
04804 
04805    else if( w == grapher->fmenu->fim_polort_choose_pb && grapher->status->send_CB != NULL ){
04806 #ifdef USE_OPTMENUS
04807       GRA_polort_choose_CB( grapher->fmenu->fim_polort_choose_av , grapher ) ;
04808 #else
04809       MCW_choose_integer( grapher->option_rowcol , "Polort Order" ,
04810                           0 , MAX_POLORT , grapher->polort ,
04811                           GRA_polort_choose_CB , (XtPointer) grapher ) ;
04812 #endif
04813    }
04814 
04815    
04816 
04817    else if( w == grapher->fmenu->fim_bkthr_choose_pb ){
04818       MCW_choose_integer( grapher->option_rowcol , "Bkg Thresh %" ,
04819                           0 , 99 , (int)(100*FIM_THR) ,
04820                           GRA_bkthr_choose_CB , (XtPointer) grapher ) ;
04821    }
04822 
04823    
04824 
04825    else {
04826       XBell( grapher->dc->display , 100 ) ;
04827    }
04828 
04829    
04830 
04831    EXRETURN ;
04832 }
04833 
04834 
04835 
04836 #define SETSHIFT_quit_label  "Quit"
04837 #define SETSHIFT_apply_label "Apply"
04838 #define SETSHIFT_done_label  "Set"
04839 
04840 #define SETSHIFT_quit_help   "Press to close\nthis control box"
04841 #define SETSHIFT_apply_help  "Press to apply this choice\nand keep this control box"
04842 #define SETSHIFT_done_help   "Press to apply this choice\nand close this control box"
04843 
04844 #define NUM_SETSHIFT_ACT 3
04845 
04846 static MCW_action_item SETSHIFT_act[NUM_SETSHIFT_ACT] = {
04847  { SETSHIFT_quit_label  , GRA_setshift_action_CB, NULL, SETSHIFT_quit_help ,"Close window"                 , 0 },
04848  { SETSHIFT_apply_label , GRA_setshift_action_CB, NULL, SETSHIFT_apply_help,"Apply choice and keep window" , 0 },
04849  { SETSHIFT_done_label  , GRA_setshift_action_CB, NULL, SETSHIFT_done_help ,"Apply choice and close window", 1 }
04850 } ;
04851 
04852 #define SETSHIFT_QUIT  0
04853 #define SETSHIFT_APPLY 1
04854 #define SETSHIFT_DONE  2
04855 
04856 
04857 
04858 void GRA_setshift_startup( MCW_grapher * grapher )
04859 {
04860    int ib , xx,yy ;
04861    Widget wrc ;
04862 
04863 ENTRY("GRA_setshift_startup") ;
04864 
04865    if( ! GRA_REALZ(grapher) || grapher->dialog != NULL ) EXRETURN ;
04866 
04867    MCW_widget_geom( grapher->fdw_graph , NULL,NULL,&xx,&yy ) ;  
04868 
04869    grapher->dialog = XtVaCreatePopupShell(
04870                       "menu" , xmDialogShellWidgetClass , grapher->fdw_graph ,
04871                          XmNx , xx+15 ,
04872                          XmNy , yy+15 ,
04873                          XmNtitle , "Shifts" ,
04874                          XmNdeleteResponse , XmDO_NOTHING ,
04875                          XmNinitialResourcesPersistent , False ,
04876 
04877                          XmNvisual   , grapher->dc->visual ,         
04878                          XmNcolormap , grapher->dc->colormap ,
04879                          XmNdepth    , grapher->dc->depth ,
04880                          XmNscreen   , grapher->dc->screen ,
04881                          XmNbackground  , 0 ,
04882                          XmNborderColor , 0 ,
04883 
04884                       NULL ) ;
04885 
04886    if( MCW_isitmwm(grapher->fdw_graph) ){
04887       XtVaSetValues( grapher->dialog ,
04888                        XmNmwmDecorations , MWM_DECOR_BORDER ,
04889                        XmNmwmFunctions ,   MWM_FUNC_MOVE
04890                                          | MWM_FUNC_CLOSE ,
04891                      NULL ) ;
04892    }
04893 
04894    XmAddWMProtocolCallback(           
04895            grapher->dialog ,
04896            XmInternAtom( grapher->dc->display , "WM_DELETE_WINDOW" , False ) ,
04897            GRA_setshift_action_CB , grapher ) ;
04898 
04899    wrc = XtVaCreateWidget(                    
04900              "dialog" , xmRowColumnWidgetClass , grapher->dialog ,
04901                 XmNpacking     , XmPACK_TIGHT ,
04902                 XmNorientation , XmVERTICAL ,
04903                 XmNtraversalOn , False ,
04904                 XmNinitialResourcesPersistent , False ,
04905              NULL ) ;
04906 
04907    (void) XtVaCreateManagedWidget(
04908             "dialog" , xmLabelWidgetClass , wrc ,
04909                LABEL_ARG("-- Shift Controls --") ,
04910                XmNalignment  , XmALIGNMENT_CENTER ,
04911                XmNinitialResourcesPersistent , False ,
04912             NULL ) ;
04913 
04914    (void) XtVaCreateManagedWidget(
04915             "dialog" , xmSeparatorWidgetClass , wrc ,
04916                XmNseparatorType , XmSHADOW_ETCHED_IN ,
04917                XmNinitialResourcesPersistent , False ,
04918             NULL ) ;
04919 
04920    grapher->setshift_inc_av = new_MCW_arrowval(
04921                                 wrc ,   "Increment  " , MCW_AV_downup ,
04922                                 1 , 1000 , (int)(100.0*grapher->setshift_inc) ,
04923                                 MCW_AV_edittext , 2 ,
04924                                 NULL , NULL , NULL , NULL ) ;
04925 
04926    grapher->setshift_left_av = new_MCW_arrowval(
04927                                   wrc , "Steps Left " , MCW_AV_downup ,
04928                                   0 , 29 , grapher->setshift_left ,
04929                                   MCW_AV_edittext , 0 ,
04930                                   NULL , NULL , NULL , NULL ) ;
04931 
04932    grapher->setshift_right_av = new_MCW_arrowval(
04933                                   wrc , "Steps Right" , MCW_AV_downup ,
04934                                   0 , 29 , grapher->setshift_right ,
04935                                   MCW_AV_edittext , 0 ,
04936                                   NULL , NULL , NULL , NULL ) ;
04937 
04938    grapher->setshift_inc_av->allow_wrap   = 1 ;   
04939    grapher->setshift_left_av->allow_wrap  = 1 ;
04940    grapher->setshift_right_av->allow_wrap = 1 ;
04941 
04942    grapher->setshift_inc_av->fastdelay    = 250 ; 
04943    grapher->setshift_left_av->fastdelay   = 250 ;
04944    grapher->setshift_right_av->fastdelay  = 250 ;
04945 
04946    MCW_reghelp_children( grapher->setshift_inc_av->wrowcol ,
04947        "This controls the step size\n"
04948        "used to create the shifted\n"
04949        "time series -- for example,\n"
04950        "0.2 means a shift of 1/5\n"
04951        "of a time series stepsize."
04952    ) ;
04953    MCW_reghint_children( grapher->setshift_inc_av->wrowcol ,
04954                          "Size of shift" ) ;
04955 
04956    MCW_reghelp_children( grapher->setshift_left_av->wrowcol ,
04957       "This controls the number\n"
04958       "of left shifts used to\n"
04959       "create the desired multi-\n"
04960       "vector time series.\n\n"
04961       "N.B.: Steps Left plus\n"
04962       "      Steps Right should\n"
04963       "      be positive!"
04964    ) ;
04965    MCW_reghint_children( grapher->setshift_left_av->wrowcol ,
04966                          "Number of steps left" ) ;
04967 
04968    MCW_reghelp_children( grapher->setshift_right_av->wrowcol ,
04969       "This controls the number\n"
04970       "of right shifts used to\n"
04971       "create the desired multi-\n"
04972       "vector time series.\n\n"
04973       "N.B.: Steps Left plus\n"
04974       "      Steps Right should\n"
04975       "      be positive!"
04976    ) ;
04977    MCW_reghint_children( grapher->setshift_right_av->wrowcol ,
04978                          "Number of steps right" ) ;
04979 
04980    for( ib=0 ; ib < NUM_SETSHIFT_ACT ; ib++ )
04981       SETSHIFT_act[ib].data = (XtPointer) grapher ;
04982 
04983    (void) MCW_action_area( wrc , SETSHIFT_act , NUM_SETSHIFT_ACT ) ;
04984 
04985    XtManageChild( wrc ) ;
04986    XtPopup( grapher->dialog , XtGrabNone ) ;
04987    RWC_visibilize_widget( grapher->dialog ) ; 
04988    NORMAL_cursorize( grapher->dialog ) ;
04989    EXRETURN ;
04990 }
04991 
04992 
04993 
04994 void GRA_setshift_action_CB( Widget w , XtPointer client_data , XtPointer call_data )
04995 {
04996    MCW_grapher * grapher = (MCW_grapher *) client_data ;
04997    XmAnyCallbackStruct * cbs = (XmAnyCallbackStruct *) call_data ;
04998    char * wname ;
04999    int ib , close_window ;
05000 
05001 ENTRY("GRA_setshift_action_CB") ;
05002 
05003    if( !GRA_REALZ(grapher) || grapher->dialog==NULL ) EXRETURN ;
05004 
05005    wname = XtName(w) ;
05006 
05007    for( ib=0 ; ib < NUM_SETSHIFT_ACT ; ib++ )           
05008       if( strcmp(wname,SETSHIFT_act[ib].label) == 0 ) break ;
05009 
05010    close_window = (ib == SETSHIFT_DONE ||
05011                    ib == SETSHIFT_QUIT || ib == NUM_SETSHIFT_ACT) ;
05012 
05013    if( close_window ){
05014       RWC_XtPopdown( grapher->dialog ) ;
05015       XSync( XtDisplay(w) , False ) ;
05016       XmUpdateDisplay( w ) ;
05017    }
05018 
05019    switch( ib ){
05020 
05021       case SETSHIFT_APPLY:
05022       case SETSHIFT_DONE:{
05023          grapher->setshift_inc   = grapher->setshift_inc_av->fval ;
05024          grapher->setshift_left  = grapher->setshift_left_av->ival ;
05025          grapher->setshift_right = grapher->setshift_right_av->ival ;
05026 
05027          GRA_doshift( grapher ) ;
05028       }
05029       break ;
05030    }
05031 
05032    if( close_window ){                          
05033       XtDestroyWidget( grapher->dialog ) ;
05034       grapher->dialog = NULL ;
05035       FREE_AV( grapher->setshift_right_av ) ;
05036       FREE_AV( grapher->setshift_left_av )  ;
05037       FREE_AV( grapher->setshift_inc_av )   ;
05038    }
05039 
05040    EXRETURN ;
05041 }
05042 
05043 
05044 
05045 void GRA_doshift( MCW_grapher * grapher )
05046 {
05047    MRI_IMAGE * tsim , * newim , * tim ;
05048    float * tsar , * nar , * tar ;
05049    int ii,ivec , nx , newny , nleft,nright ;
05050    float shinc ;
05051    GRA_cbs gbs ;
05052 
05053 ENTRY("GRA_doshift") ;
05054 
05055    if( !GRA_VALID(grapher) ) EXRETURN ;
05056 
05057    if( grapher->status->send_CB == NULL                       ||
05058        grapher->setshift_inc <= 0.0                           ||
05059        grapher->setshift_left + grapher->setshift_right <= 0  ||
05060        grapher->ref_ts == NULL                                ||
05061        IMARR_COUNT(grapher->ref_ts) == 0                        ){
05062 
05063       XBell( grapher->dc->display , 100 ) ; EXRETURN ;
05064    }
05065 
05066    tsim = IMARR_SUBIMAGE(grapher->ref_ts,0) ; 
05067    tsar = MRI_FLOAT_PTR(tsim) ;
05068    nx   = tsim->nx ;
05069 
05070    shinc  = grapher->setshift_inc ;
05071    nleft  = grapher->setshift_left ;
05072    nright = grapher->setshift_right ;
05073    newny  = 1 + nleft + nright ;
05074    newim  = mri_new( nx , newny , MRI_float ) ;
05075    nar    = MRI_FLOAT_PTR(newim) ;
05076 
05077    for( ii=0 ; ii < nx ; ii++ )
05078       nar[ii] = (ii < grapher->init_ignore) ? WAY_BIG : tsar[ii] ;
05079 
05080    for( ivec=1 ; ivec <= nleft ; ivec++ ){
05081       tim = mri_shift_1D( newim , -ivec * shinc ) ;
05082       tar = MRI_FLOAT_PTR(tim) ;
05083       for( ii=0 ; ii < nx ; ii++ ) nar[ii+ivec*nx] = tar[ii] ;
05084       mri_free(tim) ;
05085    }
05086 
05087    for( ivec=1 ; ivec <= nright ; ivec++ ){
05088       tim = mri_shift_1D( newim , ivec * shinc ) ;
05089       tar = MRI_FLOAT_PTR(tim) ;
05090       for( ii=0 ; ii < nx ; ii++ ) nar[ii+(ivec+nleft)*nx] = tar[ii] ;
05091       mri_free(tim) ;
05092    }
05093 
05094    { GRA_cbs cbs; cbs.reason=graCR_winaver; CALL_sendback(grapher,cbs); }
05095    MCW_set_bbox( grapher->fmenu->fim_editref_winaver_bbox , 0 ) ;
05096    gbs.reason   = graCR_refequals ;
05097    gbs.userdata = (XtPointer) newim ;
05098 #if 0
05099    grapher->status->send_CB( grapher , grapher->getaux , &gbs ) ;
05100 #else
05101    CALL_sendback( grapher , gbs ) ;
05102 #endif
05103    mri_free( newim ) ;
05104    EXRETURN ;
05105 }
05106 
05107 
05108 
05109 
05110 
05111 
05112 FIM_menu * AFNI_new_fim_menu( Widget parent, XtCallbackProc cbfunc, int graphable )
05113 {
05114    FIM_menu *fmenu ;
05115    Widget qbut_menu = NULL ;
05116 
05117 ENTRY("AFNI_new_fim_menu") ;
05118 
05119    fmenu = myXtNew(FIM_menu) ;
05120    fmenu->cbfunc = cbfunc ;
05121 
05122    
05123    
05124    
05125 
05126    fmenu->fim_menu =
05127          XmCreatePulldownMenu( parent , "menu" , NULL,0 ) ;
05128 
05129    VISIBILIZE_WHEN_MAPPED(fmenu->fim_menu) ;  
05130 
05131    fmenu->fim_cbut =
05132          XtVaCreateManagedWidget(
05133             "dialog" , xmCascadeButtonWidgetClass , parent ,
05134                LABEL_ARG("FIM") ,
05135                XmNsubMenuId , fmenu->fim_menu ,
05136                XmNmarginWidth  , 0 ,
05137                XmNmarginHeight , 0 ,
05138                XmNmarginBottom , 0 ,
05139                XmNmarginTop    , 0 ,
05140                XmNmarginRight  , 0 ,
05141                XmNmarginLeft   , 0 ,
05142                XmNtraversalOn  , False ,
05143                XmNinitialResourcesPersistent , False ,
05144             NULL ) ;
05145 
05146    if( graphable ){
05147       MCW_register_hint( fmenu->fim_cbut , "Functional Imaging menu" ) ;
05148       MCW_register_help( fmenu->fim_cbut ,
05149                       "*******  Functional Imaging Controls:  *******\n"
05150                       "\n"
05151                       "Ideal Vector Operations:\n"
05152                       " Pick Ideal --> Choose from a list\n"
05153                       " Pick Ort   --> Choose from a list\n"
05154                       " Edit: = Center --> Use voxel timeseries\n"
05155                       "      += Center --> Average with voxel\n"
05156                       "      Smooth    --> A 3 point filter\n"
05157                       "      Shift     --> Time-shifted copies\n"
05158                       "      Clear     --> Turn ideal off\n"
05159                       "      WinAver   --> Average of sub-graphs\n"
05160                       "      Read      --> Input from external file\n"
05161                       "      Write     --> Output to external file\n"
05162                       "      Store     --> Save in internal list\n"
05163                       " Ignore: Set how many points to ignore\n"
05164                       "         at beginning of time series\n"
05165                       "         [ Applies both to graphing ]\n"
05166                       "         [ and to FIM computations. ]\n"
05167                       "\n"
05168                       "FIM Plots:\n"
05169                       "  Can choose to graph only first vector in\n"
05170                       "  ideal family, or all of them superimposed.\n"
05171                       "\n"
05172                       "Refresh Freq --> Choose number of time steps\n"
05173                       "   between redisplay of the functional overlay\n"
05174                       "   during FIM computations (0 == no redisplay).\n"
05175                       "Compute FIM  --> Use the recursive method to\n"
05176                       "   compute the correlation of each voxel time\n"
05177                       "   series with each ideal vector;  uses the best\n"
05178                       "   correlation as the 'correct' waveform for\n"
05179                       "   each voxel, individually.  Time points that\n"
05180                       "   have an ideal vector value >= 33333 will be\n"
05181                       "   ignored in the computations, as will those\n"
05182                       "   at the beginning specified in the Opt menu\n"
05183                       "   'Ignore' function."
05184                     ) ;
05185    } else {
05186       MCW_register_hint( fmenu->fim_cbut , "Functional Imaging menu" ) ;
05187       MCW_register_help( fmenu->fim_cbut ,
05188                       "*******  Functional Imaging Controls:  *******\n"
05189                       "\n"
05190                       "Pick Dataset --> Choose time-dependent dataset\n"
05191                       "   from a list.  If there is only one possible\n"
05192                       "   choice, it will be selected for you without\n"
05193                       "   displaying the list.\n"
05194                       "\n"
05195                       "Pick Ideal --> Choose time series from a list.\n"
05196                       "Pick Ort   --> Choose time series from a list.\n"
05197                       "\n"
05198                       "Ignore --> Set how many points to ignore at the\n"
05199                       "   beginning of time series.\n"
05200                       "   [ Applies both to graphing and FIM-ing. ]\n"
05201                       "\n"
05202                       "Refresh Freq --> Choose number of time steps\n"
05203                       "   between redisplay of the functional overlay\n"
05204                       "   during FIM computations (0 == no redisplay).\n"
05205                       "\n"
05206                       "Compute FIM --> Use the recursive method to\n"
05207                       "   compute the correlation of each voxel time\n"
05208                       "   series with each ideal vector;  uses the best\n"
05209                       "   correlation as the 'correct' waveform for\n"
05210                       "   each voxel, individually.  Time points that\n"
05211                       "   have an ideal vector value >= 33333 will be\n"
05212                       "   ignored in the computations, as will those\n"
05213                       "   at the beginning specified in the Opt menu\n"
05214                       "   'Ignore' function."
05215                     ) ;
05216    }
05217 
05218    
05219 
05220 #undef MENU_DLINE
05221 #define MENU_DLINE(wmenu)                                        \
05222    (void) XtVaCreateManagedWidget(                               \
05223             "dialog" , xmSeparatorWidgetClass , fmenu -> wmenu , \
05224              XmNseparatorType , XmDOUBLE_LINE , NULL )
05225 
05226 #undef MENU_SLINE
05227 #define MENU_SLINE(wmenu)                                        \
05228    (void) XtVaCreateManagedWidget(                               \
05229             "dialog" , xmSeparatorWidgetClass , fmenu -> wmenu , \
05230              XmNseparatorType , XmSINGLE_LINE , NULL )
05231 
05232    
05233 
05234 #define FIM_MENU_BUT(wname,label,hhh)                              \
05235    fmenu -> wname =                                                \
05236          XtVaCreateManagedWidget(                                  \
05237             "dialog" , xmPushButtonWidgetClass , fmenu->fim_menu , \
05238                LABEL_ARG( label ) ,                                \
05239                XmNmarginHeight , 0 ,                               \
05240                XmNtraversalOn , False ,                            \
05241                XmNinitialResourcesPersistent , False ,             \
05242             NULL ) ;                                               \
05243       XtAddCallback( fmenu -> wname , XmNactivateCallback ,        \
05244                      cbfunc , (XtPointer) fmenu ) ;                \
05245       MCW_register_hint( fmenu -> wname , hhh ) ;
05246 
05247 
05248 
05249 
05250 #define FIM_MENU_PULLRIGHT(wmenu,wcbut,label,hhh)                  \
05251    fmenu -> wmenu =                                                \
05252      XmCreatePulldownMenu( fmenu->fim_menu , "menu" , NULL , 0 ) ; \
05253    fmenu -> wcbut =                                                \
05254      XtVaCreateManagedWidget(                                      \
05255        "dialog" , xmCascadeButtonWidgetClass , fmenu->fim_menu ,   \
05256           LABEL_ARG( label ) ,                                     \
05257           XmNsubMenuId , fmenu -> wmenu ,                          \
05258           XmNtraversalOn , False ,                                 \
05259           XmNinitialResourcesPersistent , False ,                  \
05260        NULL ) ;                                                    \
05261    MCW_register_hint( fmenu -> wcbut , hhh ) ;                     \
05262    XtAddCallback( fmenu -> wmenu, XmNmapCallback, GRA_mapmenu_CB, NULL ) ;
05263 
05264 
05265 
05266 #define FIM_MENU_PULL_BUT(wmenu,wname,label,hhh)                  \
05267    fmenu -> wname =                                               \
05268          XtVaCreateManagedWidget(                                 \
05269             "dialog" , xmPushButtonWidgetClass , fmenu -> wmenu , \
05270                LABEL_ARG( label ) ,                               \
05271                XmNmarginHeight , 0 ,                              \
05272                XmNtraversalOn , False ,                           \
05273                XmNinitialResourcesPersistent , False ,            \
05274             NULL ) ;                                              \
05275       XtAddCallback( fmenu -> wname , XmNactivateCallback ,       \
05276                      cbfunc , (XtPointer) fmenu ) ;               \
05277       MCW_register_hint( fmenu -> wname , hhh ) ;
05278 
05279 #define EMPTY_BUT(wname) fmenu -> wname = NULL
05280 
05281 
05282 
05283 #define FIM_MENU_QBUT(wname,label,qlab,hhh)                               \
05284  do { Widget ccc ;                                                        \
05285       qbut_menu = XmCreatePulldownMenu(fmenu->fim_menu,"menu",NULL,0);    \
05286             ccc = XtVaCreateManagedWidget( "dialog" ,                     \
05287                      xmCascadeButtonWidgetClass , fmenu->fim_menu ,       \
05288                      LABEL_ARG( label ) ,                                 \
05289                      XmNsubMenuId , qbut_menu ,                           \
05290                      XmNtraversalOn , False ,                             \
05291                      XmNinitialResourcesPersistent , False , NULL ) ;     \
05292       fmenu -> wname = XtVaCreateManagedWidget( "dialog" ,                \
05293                          xmPushButtonWidgetClass , qbut_menu ,            \
05294                          LABEL_ARG( qlab ) ,                              \
05295                          XmNmarginHeight , 0 ,                            \
05296                          XmNtraversalOn , False ,                         \
05297                          XmNinitialResourcesPersistent , False , NULL ) ; \
05298       MCW_register_hint( fmenu -> wname , hhh ) ;                         \
05299       XtAddCallback( fmenu -> wname , XmNactivateCallback ,               \
05300                      cbfunc , (XtPointer) fmenu ) ;                       \
05301       XtAddCallback( qbut_menu, XmNmapCallback, GRA_mapmenu_CB, NULL ) ;  \
05302  } while(0)
05303 
05304    
05305 
05306    (void) XtVaCreateManagedWidget(
05307             "dialog" , xmLabelWidgetClass , fmenu->fim_menu ,
05308                LABEL_ARG("--- Cancel ---") ,
05309                XmNrecomputeSize , False ,
05310                XmNinitialResourcesPersistent , False ,
05311             NULL ) ;
05312 
05313    MENU_SLINE(fim_menu) ;
05314 
05315    if( graphable ){
05316       EMPTY_BUT(fim_pickdset_pb) ;
05317    } else {
05318       FIM_MENU_BUT( fim_pickdset_pb , "Pick Dataset" , "Choose Dataset to Graph" ) ;
05319    }
05320 
05321    FIM_MENU_BUT( fim_pickref_pb , "Pick Ideal" , "Pick Ideal Timeseries to Graph" ) ;
05322    FIM_MENU_BUT( fim_pickort_pb , "Pick Ort"   , "Pick Ort Timeseries to Graph"   ) ;
05323 
05324    if( graphable ){
05325       char *bbox_label[1] = { "Ideal=WinAver" } ;
05326       MENU_SLINE(fim_menu) ;
05327       FIM_MENU_PULLRIGHT(fim_editref_menu,fim_editref_cbut       ,"Edit Ideal"    , "Modify Ideal Timeseries" ) ;
05328       FIM_MENU_PULL_BUT (fim_editref_menu,fim_editref_equals_pb  ,"Ideal = Center", "Set to Center Sub-graph" ) ;
05329       FIM_MENU_PULL_BUT (fim_editref_menu,fim_editref_add_pb     ,"Ideal+= Center", "Add in Center Sub-graph" ) ;
05330       FIM_MENU_PULL_BUT (fim_editref_menu,fim_editref_smooth_pb  ,"Smooth Ideal"  , "Lowpass Filter Ideal"    ) ;
05331       FIM_MENU_PULL_BUT (fim_editref_menu,fim_editref_setshift_pb,"Shift Ideal"   , "Time Shift Ideal"        ) ;
05332       FIM_MENU_PULL_BUT (fim_editref_menu,fim_editref_clear_pb   ,"Clear Ideal"   , "Turn Ideal Off"          ) ;
05333       FIM_MENU_PULL_BUT (fim_editref_menu,fim_editort_clear_pb   ,"Clear Ort"     , "Turn Ort Off"            ) ;
05334 
05335       fmenu->fim_editref_winaver_bbox                      
05336        = new_MCW_bbox( fmenu->fim_editref_menu ,
05337                        1 , bbox_label , MCW_BB_check , MCW_BB_noframe ,
05338                        GRA_winaver_CB , (XtPointer)fmenu ) ;
05339       MCW_reghint_children( fmenu->fim_editref_winaver_bbox->wrowcol ,
05340                             "Ideal = Average of all Graphs in Window" ) ;
05341 #ifdef USE_OPTMENUS
05342       fmenu->fim_polort_choose_av =
05343          new_MCW_optmenu( fmenu->fim_editref_menu , "Polort " , 0,MAX_POLORT,1,0 ,
05344                           GRA_fmenu_av_CB , (XtPointer) fmenu , NULL , NULL ) ;
05345       fmenu->fim_polort_choose_pb = fmenu->fim_polort_choose_av->wrowcol ;
05346       MCW_reghint_children( fmenu->fim_polort_choose_av->wrowcol , "Order of Polynomial Baseline for FIM" ) ;
05347 #else
05348       FIM_MENU_PULL_BUT( fim_editref_menu,fim_polort_choose_pb ,"Polort?", "Order of Polynomial Baseline for FIM") ;
05349 #endif
05350       FIM_MENU_PULL_BUT( fim_editref_menu,fim_bkthr_choose_pb  ,"Bkg Thresh" , "Choose Background Threshold for FIM") ;
05351       MENU_SLINE        (fim_editref_menu) ;
05352       FIM_MENU_PULL_BUT (fim_editref_menu,fim_editref_read_pb    ,"Read Ideal" , "Read from .1D file"   ) ;
05353       FIM_MENU_PULL_BUT (fim_editref_menu,fim_editref_write_pb   ,"Write Ideal", "Write to .1D file"    ) ;
05354       FIM_MENU_PULL_BUT (fim_editref_menu,fim_editref_store_pb   ,"Store Ideal", "Save in internal list of timeseries" ) ;
05355    } else {
05356       EMPTY_BUT(fim_editref_cbut) ;
05357       EMPTY_BUT(fim_editref_equals_pb) ;
05358       EMPTY_BUT(fim_editref_add_pb) ;
05359       EMPTY_BUT(fim_editref_smooth_pb) ;
05360       EMPTY_BUT(fim_editref_setshift_pb) ;
05361       EMPTY_BUT(fim_editref_clear_pb) ;
05362       EMPTY_BUT(fim_editref_read_pb) ;
05363       EMPTY_BUT(fim_editref_write_pb) ;
05364       EMPTY_BUT(fim_editref_store_pb) ;
05365       EMPTY_BUT(fim_editort_clear_pb) ;
05366       EMPTY_BUT(fim_polort_choose_pb) ;
05367       fmenu->fim_editref_winaver_bbox = NULL ;  
05368    }
05369 
05370    FIM_MENU_PULLRIGHT(fim_ignore_menu,fim_ignore_cbut      ,"Ignore", "Number of initial timepoints to ignore" ) ;
05371    FIM_MENU_PULL_BUT( fim_ignore_menu,fim_ignore_down_pb   ,"Down"  , "Ignore fewer points" ) ;
05372    FIM_MENU_PULL_BUT( fim_ignore_menu,fim_ignore_up_pb     ,"Up"    , "Ignore more points"  ) ;
05373 #ifdef USE_OPTMENUS
05374    fmenu->fim_ignore_choose_av =
05375       new_MCW_optmenu( fmenu->fim_ignore_menu , "# " , 0,2,0,0 ,
05376                        GRA_fmenu_av_CB , (XtPointer) fmenu , NULL , NULL ) ;
05377    fmenu->fim_ignore_choose_pb = fmenu->fim_ignore_choose_av->wrowcol ;
05378    MCW_reghint_children( fmenu->fim_ignore_choose_av->wrowcol , "Pick number of ignored points" ) ;
05379 #else
05380    FIM_MENU_PULL_BUT( fim_ignore_menu,fim_ignore_choose_pb ,"Choose" , "Pick number of ignored points") ;
05381 #endif
05382 
05383    if( graphable ){
05384       FIM_MENU_PULLRIGHT(fim_plot_menu,fim_plot_cbut        ,"FIM Plots"   , "Number of Ideals to plot" ) ;
05385       FIM_MENU_PULL_BUT( fim_plot_menu,fim_plot_firstref_pb ,"First Ideal" , "Only plot 1 Ideal" ) ;
05386       FIM_MENU_PULL_BUT( fim_plot_menu,fim_plot_allrefs_pb  ,"All Ideals"  , "Plot all Ideals"   ) ;
05387    } else {
05388       EMPTY_BUT(fim_plot_cbut) ;
05389       EMPTY_BUT(fim_plot_firstref_pb) ;
05390       EMPTY_BUT(fim_plot_allrefs_pb) ;
05391    }
05392 
05393    MENU_DLINE(fim_menu) ;
05394    FIM_MENU_QBUT( fim_execute_pb   , "Compute FIM" , "-> fico" , "Correlation Analysis" ) ;
05395    MCW_set_widget_bg( fmenu->fim_execute_pb ,
05396                       MCW_hotcolor(fmenu->fim_execute_pb) , 0 ) ;
05397 
05398    { static char * blab[] = {"Fit Coef", "% Change", "% From Ave", "% From Top"};
05399      (void) XtVaCreateManagedWidget(
05400              "dialog" , xmSeparatorWidgetClass , qbut_menu ,
05401               XmNseparatorType , XmSINGLE_LINE , NULL ) ;
05402 
05403      fmenu->fim_opt_bbox = new_MCW_bbox( qbut_menu , 4 , blab ,
05404                                          MCW_BB_radio_one , MCW_BB_noframe ,
05405                                          NULL , NULL ) ;
05406      MCW_reghint_children( fmenu->fim_opt_bbox->wrowcol , "What to Compute" ) ;
05407    }
05408 
05409    MENU_DLINE(fim_menu) ;
05410    FIM_MENU_QBUT( fim_execfimp_pb  , "Compute FIM+" , "-> fbuc" , "Extended Correlation Analysis" ) ;
05411    MCW_set_widget_bg( fmenu->fim_execfimp_pb ,
05412                       MCW_hotcolor(fmenu->fim_execfimp_pb) , 0 ) ;
05413 
05414    (void) XtVaCreateManagedWidget(
05415            "dialog" , xmSeparatorWidgetClass , qbut_menu ,
05416             XmNseparatorType , XmSINGLE_LINE , NULL ) ;
05417 
05418    fmenu->fimp_opt_bbox = new_MCW_bbox( qbut_menu, FIM_NUM_OPTS, fim_opt_labels,
05419                                         MCW_BB_check , MCW_BB_noframe ,
05420                                         NULL , NULL ) ;
05421    MCW_reghint_children( fmenu->fimp_opt_bbox->wrowcol , "What to Compute" ) ;
05422 
05423    { char * ff = my_getenv( "AFNI_FIM_MASK" ) ; int mm=0 ;
05424      if( ff != NULL ) mm = strtol(ff,NULL,10) ;
05425      if( mm <= 0 ) mm = FIM_DEFAULT_MASK ;
05426      MCW_set_bbox( fmenu->fimp_opt_bbox , mm ) ;
05427    }
05428 
05429    
05430 
05431    (void) XtVaCreateManagedWidget(
05432            "dialog" , xmSeparatorWidgetClass , qbut_menu ,
05433             XmNseparatorType , XmSINGLE_LINE , NULL ) ;
05434 
05435    fmenu->fimp_setdefault_pb =
05436       XtVaCreateManagedWidget( "dialog" , xmPushButtonWidgetClass , qbut_menu ,
05437                                  LABEL_ARG( "Set Defaults" ) ,
05438                                  XmNmarginHeight , 0 ,
05439                                  XmNtraversalOn , False ,
05440                                  XmNinitialResourcesPersistent , False ,
05441                                NULL ) ;
05442    XtAddCallback( fmenu->fimp_setdefault_pb ,
05443                   XmNactivateCallback , cbfunc , (XtPointer) fmenu ) ;
05444    MCW_register_hint( fmenu->fimp_setdefault_pb , "Default computing options" ) ;
05445 
05446    fmenu->fimp_setall_pb =
05447       XtVaCreateManagedWidget( "dialog" , xmPushButtonWidgetClass , qbut_menu ,
05448                                  LABEL_ARG( "Set All" ) ,
05449                                  XmNmarginHeight , 0 ,
05450                                  XmNtraversalOn , False ,
05451                                  XmNinitialResourcesPersistent , False ,
05452                                NULL ) ;
05453    XtAddCallback( fmenu->fimp_setall_pb ,
05454                   XmNactivateCallback , cbfunc , (XtPointer) fmenu ) ;
05455    MCW_register_hint( fmenu->fimp_setall_pb , "Set all computing options on" ) ;
05456 
05457    fmenu->fimp_unsetall_pb =
05458       XtVaCreateManagedWidget( "dialog" , xmPushButtonWidgetClass , qbut_menu ,
05459                                  LABEL_ARG( "Unset All" ) ,
05460                                  XmNmarginHeight , 0 ,
05461                                  XmNtraversalOn , False ,
05462                                  XmNinitialResourcesPersistent , False ,
05463                                NULL ) ;
05464    XtAddCallback( fmenu->fimp_unsetall_pb ,
05465                   XmNactivateCallback , cbfunc , (XtPointer) fmenu ) ;
05466    MCW_register_hint( fmenu->fimp_unsetall_pb , "Set all computing options off" ) ;
05467 
05468    
05469 
05470    fmenu->fimp_user_bbox = NULL ; 
05471 
05472    if( GLOBAL_library.registered_fim.num > 0 ){
05473 
05474       (void) XtVaCreateManagedWidget(
05475               "dialog" , xmSeparatorWidgetClass , qbut_menu ,
05476                XmNseparatorType , XmDOUBLE_LINE , NULL ) ;
05477 
05478       (void) XtVaCreateManagedWidget(
05479                "dialog" , xmLabelWidgetClass , qbut_menu ,
05480                   LABEL_ARG("--Extra Funcs--") ,
05481                   XmNrecomputeSize , False ,
05482                   XmNinitialResourcesPersistent , False ,
05483                NULL ) ;
05484 
05485       fmenu->fimp_user_bbox = new_MCW_bbox( qbut_menu,
05486                                             GLOBAL_library.registered_fim.num ,
05487                                             GLOBAL_library.registered_fim.labels ,
05488                                             MCW_BB_check , MCW_BB_noframe ,
05489                                             NULL , NULL ) ;
05490       MCW_reghint_children( fmenu->fimp_user_bbox->wrowcol , "Other correlation functions" ) ;
05491    }
05492 
05493    RETURN(fmenu) ;
05494 }
05495 
05496 
05497 
05498 
05499 
05500 char * GRA_transform_label( MCW_arrowval * av , XtPointer cd )
05501 {
05502    MCW_function_list * xforms = (MCW_function_list *) cd ;
05503 
05504    if( av == NULL    || xforms == NULL        ||
05505        av->ival <= 0 || av->ival > xforms->num  ) return "-none-" ;
05506 
05507    return xforms->labels[av->ival - 1] ;  
05508 }
05509 
05510 
05511 
05512 
05513 void GRA_transform_CB( MCW_arrowval * av , XtPointer cd )
05514 {
05515    MCW_grapher * grapher = (MCW_grapher *) cd ;
05516 
05517 ENTRY("GRA_transform_CB") ;
05518 
05519    if( ! GRA_VALID(grapher) ) EXRETURN ;
05520 
05521 
05522 
05523    if( av == grapher->transform0D_av && av != NULL ){
05524       if( grapher->status->transforms0D == NULL || av->ival <= 0 ||
05525           av->ival > grapher->status->transforms0D->num            ){
05526 
05527          grapher->transform0D_func  = NULL ;  
05528          grapher->transform0D_index = 0 ;
05529       } else {
05530          grapher->transform0D_func  = grapher->status->transforms0D->funcs[av->ival-1];
05531          grapher->transform0D_index = av->ival ;
05532          grapher->transform0D_flags = grapher->status->transforms0D->flags[av->ival-1];
05533 
05534          
05535 
05536          if( grapher->status->transforms0D->func_init[av->ival-1] != NULL )
05537           grapher->status->transforms0D->func_init[av->ival-1]() ;
05538       }
05539    }
05540 
05541 
05542 
05543    if( av == grapher->transform1D_av && av != NULL ){
05544       if( grapher->status->transforms1D == NULL || av->ival <= 0 ||
05545           av->ival > grapher->status->transforms1D->num            ){
05546 
05547          grapher->transform1D_func  = NULL ;  
05548          grapher->transform1D_index = 0 ;
05549       } else {
05550          grapher->transform1D_func  = grapher->status->transforms1D->funcs[av->ival-1];
05551          grapher->transform1D_index = av->ival ;
05552          grapher->transform1D_flags = grapher->status->transforms1D->flags[av->ival-1];
05553 
05554          
05555 
05556          if( grapher->status->transforms1D->func_init[av->ival-1] != NULL )
05557           grapher->status->transforms1D->func_init[av->ival-1]() ;
05558       }
05559    }
05560 
05561    redraw_graph( grapher , 0 ) ;
05562    EXRETURN ;
05563 }
05564 
05565 
05566 
05567 
05568 
05569 void GRA_textgraph_CB( Widget w , XtPointer client_data , XtPointer call_data )
05570 {
05571    MCW_grapher *grapher = (MCW_grapher *) client_data ;
05572    int bbb ;
05573 
05574 ENTRY("GRA_textgraph_CB") ;
05575 
05576    if( ! GRA_VALID(grapher) ) EXRETURN ;
05577 
05578    bbb = MCW_val_bbox( grapher->opt_textgraph_bbox ) ;
05579    if( bbb != grapher->textgraph ){
05580      grapher->textgraph = bbb ;
05581      redraw_graph( grapher , 0 ) ;
05582    }
05583    EXRETURN ;
05584 }
05585 
05586 
05587 
05588 
05589 
05590 void GRA_baseline_CB( Widget w , XtPointer client_data , XtPointer call_data )
05591 {
05592    MCW_grapher *grapher = (MCW_grapher *) client_data ;
05593    int bbb ;
05594 
05595 ENTRY("GRA_baseline_CB") ;
05596 
05597    if( ! GRA_VALID(grapher) ) EXRETURN ;
05598 
05599    bbb = MCW_val_bbox( grapher->opt_baseline_bbox ) ;
05600    if( bbb != grapher->common_base ){
05601      grapher->common_base = bbb ;
05602      redraw_graph( grapher , 0 ) ;
05603    }
05604    EXRETURN ;
05605 }
05606 
05607 
05608 
05609 
05610 
05611 void GRA_finalize_global_baseline_CB( Widget w,
05612                                       XtPointer cd , MCW_choose_cbs *cbs )
05613 {
05614    MCW_grapher *grapher = (MCW_grapher *) cd ;
05615    XmString xstr ;
05616    char str[32] ;
05617 
05618 ENTRY("GRA_finalize_global_baseline_CB") ;
05619 
05620    if( !GRA_VALID(grapher) ) EXRETURN ;
05621 
05622    grapher->global_base = cbs->fval ;
05623    if( grapher->common_base == BASELINE_GLOBAL ) redraw_graph(grapher,0) ;
05624 
05625    strcpy(str,"Global:") ;
05626    AV_fval_to_char(grapher->global_base,str+7) ;
05627    xstr = XmStringCreateLtoR( str,XmFONTLIST_DEFAULT_TAG ) ;
05628    XtVaSetValues( grapher->opt_baseline_global_label ,
05629                      XmNlabelString,xstr ,
05630                   NULL ) ;
05631    XmStringFree(xstr) ;
05632    EXRETURN ;
05633 }
05634 
05635 
05636 
05637 
05638 void GRA_winaver_CB( Widget w , XtPointer client_data , XtPointer call_data )
05639 {
05640    FIM_menu *fm = (FIM_menu *)client_data ;
05641    MCW_grapher *grapher = (MCW_grapher *)fm->parent ;
05642 
05643    if( MCW_val_bbox(grapher->fmenu->fim_editref_winaver_bbox) ){
05644      GRA_cbs cbs ;
05645      cbs.reason  = graCR_winaver ;
05646      CALL_sendback( grapher , cbs ) ;
05647      redraw_graph( grapher , 0 ) ;
05648    }
05649 }
05650 
05651 
05652 void GRA_winavertimer_CB( XtPointer cd , XtIntervalId *id )
05653 {
05654    MCW_grapher *grapher = (MCW_grapher *)cd ;
05655    GRA_cbs cbs ;
05656 
05657    if( !GRA_REALZ(grapher) || grapher->ave_tsim == NULL ) return ;
05658 
05659    cbs.reason   = graCR_refequals ;
05660    cbs.userdata = (XtPointer)grapher->ave_tsim ;
05661    grapher->dont_redraw = 1 ;
05662    CALL_sendback( grapher , cbs ) ;
05663    grapher->dont_redraw = 0 ;
05664    return ;
05665 }
05666 
05667 
05668 
05669 
05670 void GRA_winaver_setref( MCW_grapher *grapher )
05671 {
05672    GRA_cbs cbs ;
05673 
05674 ENTRY("GRA_winaver_setref") ;
05675 
05676    if( !GRA_REALZ(grapher) || grapher->ave_tsim == NULL ){
05677     STATUS("nothing to do") ; EXRETURN ;
05678    }
05679 
05680    (void)XtAppAddTimeOut( XtWidgetToApplicationContext(grapher->opt_quit_pb) ,
05681                           1 , GRA_winavertimer_CB , grapher ) ;
05682    EXRETURN ;
05683 }
05684 
05685 
05686 
05687 
05688 
05689 void GRA_dplot_change_CB( Widget w , XtPointer client_data , XtPointer call_data )
05690 {
05691    MCW_grapher *grapher = (MCW_grapher *) client_data ;
05692 
05693 ENTRY("GRA_dplot_change_CB") ;
05694 
05695    if( ! GRA_REALZ(grapher) ) EXRETURN ;
05696    redraw_graph( grapher , 0 ) ;
05697    EXRETURN ;
05698 }
05699 
05700 #ifdef USE_OPTMENUS
05701 
05702 
05703 
05704 
05705 
05706 void GRA_fix_optmenus( MCW_grapher *grapher )
05707 {
05708    int igtop ;
05709 
05710 ENTRY("GRA_fix_optmenus") ;
05711 
05712    if( ! GRA_REALZ(grapher) ) EXRETURN ;
05713 
05714 
05715 
05716    if( grapher->opt_mat_choose_av->imax != grapher->mat_max )
05717       refit_MCW_optmenu( grapher->opt_mat_choose_av ,
05718                          1 , grapher->mat_max , grapher->mat , 0 ,
05719                          NULL , NULL ) ;
05720 
05721    else
05722       AV_assign_ival( grapher->opt_mat_choose_av , grapher->mat ) ;
05723 
05724 
05725 
05726    if( grapher->opt_slice_choose_av->imax != grapher->status->nz-1 )
05727       refit_MCW_optmenu( grapher->opt_slice_choose_av ,
05728                          0, grapher->status->nz - 1, grapher->zpoint, 0,
05729                          NULL , NULL ) ;
05730 
05731    else
05732       AV_assign_ival( grapher->opt_slice_choose_av , grapher->zpoint ) ;
05733 
05734 
05735 
05736    igtop = MIN( grapher->status->num_series-2 , 99 ) ;
05737    igtop = MAX( igtop , 1 ) ;
05738 
05739    if( grapher->fmenu->fim_ignore_choose_av->imax != igtop )
05740       refit_MCW_optmenu( grapher->fmenu->fim_ignore_choose_av ,
05741                          0 , igtop , grapher->init_ignore, 0,
05742                          NULL , NULL ) ;
05743    else
05744       AV_assign_ival( grapher->fmenu->fim_ignore_choose_av , grapher->init_ignore ) ;
05745 
05746 
05747 
05748    AV_assign_ival( grapher->fmenu->fim_polort_choose_av , grapher->polort ) ;
05749 
05750    EXRETURN ;
05751 }
05752 
05753 
05754 
05755 void GRA_fmenu_av_CB( MCW_arrowval* av , XtPointer cd )
05756 {
05757    FIM_menu *fmenu = (FIM_menu *) cd ;
05758 
05759 ENTRY("GRA_fmenu_av_CB") ;
05760    fmenu->cbfunc( av->wrowcol , cd , NULL ) ;
05761    EXRETURN ;
05762 }
05763 #endif 
05764 
05765 
05766 
05767 
05768 
05769 void GRA_color_CB( MCW_arrowval *av , XtPointer cd )
05770 {
05771    MCW_grapher *grapher = (MCW_grapher *) cd ;
05772    int ii , jj ;
05773 
05774 ENTRY("GRA_color_CB") ;
05775 
05776    if( ! GRA_VALID(grapher) ) EXRETURN ;
05777 
05778    for( ii=0 ; ii < NUM_COLOR_ITEMS ; ii++ )
05779      if( av == grapher->opt_color_av[ii] ) break ;
05780 
05781    if( ii < NUM_COLOR_ITEMS ){
05782      jj = grapher->color_index[ii] ;
05783      grapher->color_index[ii] = av->ival ;
05784      if( jj != grapher->color_index[ii] ) redraw_graph( grapher , 0 ) ;
05785    }
05786    EXRETURN ;
05787 }
05788 
05789 
05790 
05791 void GRA_thick_CB( Widget w , XtPointer cd , XtPointer call_data )
05792 {
05793    MCW_grapher * grapher = (MCW_grapher *) cd ;
05794    int ii , jj ;
05795 
05796 ENTRY("GRA_thick_CB") ;
05797 
05798    if( ! GRA_VALID(grapher) ) EXRETURN ;
05799 
05800    for( ii=0 ; ii < NUM_COLOR_ITEMS ; ii++ )
05801       if( grapher->opt_thick_bbox[ii] != NULL &&
05802           w == grapher->opt_thick_bbox[ii]->wbut[0] ) break ;
05803 
05804    if( ii < NUM_COLOR_ITEMS ){
05805       jj = grapher->thick_index[ii] ;
05806       grapher->thick_index[ii] = MCW_val_bbox( grapher->opt_thick_bbox[ii] ) ;
05807       if( jj != grapher->thick_index[ii] ) redraw_graph( grapher , 0 ) ;
05808       EXRETURN ;
05809    }
05810 
05811    
05812 
05813    for( ii=0 ; ii < NUM_COLOR_ITEMS ; ii++ )
05814       if( grapher->opt_points_bbox[ii] != NULL &&
05815           ( w == grapher->opt_points_bbox[ii]->wbut[0] ||
05816             w == grapher->opt_points_bbox[ii]->wbut[1]   ) ) break ;
05817 
05818    if( ii < NUM_COLOR_ITEMS ){
05819       jj = grapher->points_index[ii] ;
05820       grapher->points_index[ii] = MCW_val_bbox( grapher->opt_points_bbox[ii] ) ;
05821       if( jj != grapher->points_index[ii] ) redraw_graph( grapher , 0 ) ;
05822       EXRETURN ;
05823    }
05824 
05825    EXRETURN ;  
05826 }
05827 
05828 
05829 
05830 
05831 
05832 void GRA_saver_CB( Widget wcaller , XtPointer cd , MCW_choose_cbs * cbs )
05833 {
05834    int ll , ii ;
05835    MCW_grapher * grapher = (MCW_grapher *) cd ;
05836    char * fname , * ppnm ;
05837 
05838 ENTRY("GRA_saver_CB") ;
05839 
05840    if( ! GRA_REALZ(grapher) ) EXRETURN ;
05841 
05842    if( cbs->reason != mcwCR_string ||
05843        cbs->cval   == NULL         || (ll=strlen(cbs->cval)) == 0 ){
05844 
05845       XBell( XtDisplay(wcaller) , 100 ) ; EXRETURN ;
05846    }
05847 
05848    fname = (char *) malloc( sizeof(char) * (ll+8) ) ;
05849    strcpy( fname , cbs->cval ) ;
05850 
05851    for( ii=0 ; ii < ll ; ii++ )
05852       if( iscntrl(fname[ii]) || isspace(fname[ii]) ) break ;
05853 
05854    if( ii < ll || ll < 2 || ll > 240 ){
05855       XBell( XtDisplay(wcaller) , 100 ) ;
05856       free( fname ) ; EXRETURN ;
05857    }
05858 
05859                       ppnm = strstr( fname , ".ppm" ) ;
05860    if( ppnm == NULL ) ppnm = strstr( fname , ".pnm" ) ;
05861    if( ppnm == NULL ) ppnm = strstr( fname , ".jpg" ) ;
05862    if( ppnm == NULL ) strcat(fname,".ppm") ;
05863 
05864    GRA_file_pixmap( grapher , fname ) ;
05865    POPDOWN_string_chooser ;
05866    free(fname) ; EXRETURN ;
05867 }
05868 
05869 
05870 
05871 void GRA_file_pixmap( MCW_grapher *grapher , char *fname )
05872 {
05873    XImage *xim ;
05874    XGCValues gcv ;
05875    MRI_IMAGE *tim ;
05876    int ii ;
05877 
05878 ENTRY("GRA_file_pixmap") ;
05879 
05880    if( ! GRA_REALZ(grapher) ) EXRETURN ;
05881    if( grapher->fd_pxWind == (Pixmap) 0 ) EXRETURN ;
05882 
05883    ii = XGetGCValues( grapher->dc->display ,
05884                       grapher->dc->myGC , GCPlaneMask , &gcv ) ;
05885    if( ii == 0 ) EXRETURN ;
05886 
05887    xim = XGetImage( grapher->dc->display , grapher->fd_pxWind ,
05888                     0 , 0 , grapher->fWIDE , grapher->fHIGH ,
05889                     gcv.plane_mask , ZPixmap ) ;
05890    if( xim == NULL ) EXRETURN ;
05891 
05892    tim = XImage_to_mri( grapher->dc , xim , 0 ) ;
05893    if( tim == NULL ){ MCW_kill_XImage( xim ) ; EXRETURN ; }
05894 
05895    mri_write_pnm( fname , tim ) ;
05896    INFO_message("Writing grapher image to file %s\n",fname) ;
05897    mri_free( tim ) ;
05898    MCW_kill_XImage( xim ) ;  
05899    EXRETURN ;
05900 }
05901 
05902 
05903 
05904 
05905 
05906 void GRA_mapmenu_CB( Widget w , XtPointer client_data , XtPointer call_data )
05907 {
05908    int ww,hh,xx,yy ;
05909    int pw,ph,px,py ;
05910 
05911 ENTRY("GRA_mapmenu_CB") ;
05912 
05913    if( AFNI_yesenv("AFNI_DONT_MOVE_MENUS") ) EXRETURN ;  
05914 
05915    MCW_widget_geom( w                     , &ww,&hh , &xx,&yy ) ;
05916    MCW_widget_geom( XtParent(XtParent(w)) , &pw,&ph , &px,&py ) ;
05917 
05918 #if 1
05919 if(PRINT_TRACING){
05920  char str[256] ;
05921  sprintf(str,"menu:   width=%d height=%d x=%d y=%d",ww,hh,xx,yy); STATUS(str);
05922  sprintf(str,"parent: width=%d height=%d x=%d y=%d",pw,ph,px,py); STATUS(str); }
05923 #endif
05924 
05925    pw = pw >> 3 ;
05926    if( ! ( xx > px+7*pw || xx+ww < px+pw ) ){
05927       xx = px - ww ;  if( xx < 0 ) xx = 0 ;
05928 #if 1
05929 if(PRINT_TRACING){
05930  char str[256]; sprintf(str,"moving menu to x=%d",xx); STATUS(str); }
05931 #endif
05932       XtVaSetValues( w , XmNx , xx , NULL ) ;
05933    }
05934 
05935    RWC_xineramize( XtDisplay(w) , xx,yy,ww,hh , &xx,&yy ) ; 
05936    XtVaSetValues( w , XmNx,xx , XmNy,yy , NULL ) ;
05937    EXRETURN ;
05938 }
05939 
05940 
05941 
05942 
05943 void GRA_timer_CB( XtPointer cd , XtIntervalId *id ) 
05944 {
05945    MCW_grapher *grapher = (MCW_grapher *)cd ;
05946    int redo = 0 ;
05947 
05948 ENTRY("GRA_timer_CB") ;
05949 
05950    if( !GRA_REALZ(grapher) || grapher->timer_id == 0 ) EXRETURN ;
05951 
05952    switch( grapher->timer_func ){
05953 
05954      case GRA_TIMERFUNC_INDEX:{
05955        int nn = grapher->time_index , nt=grapher->status->num_series ;
05956        if( nt > 1 && grapher->timer_param != 0 ){
05957          nn = (nn+grapher->timer_param+nt) % nt ;
05958          redo = 1 ;
05959          if( grapher->status->send_CB != NULL ){
05960            GRA_cbs cbs ;
05961            cbs.reason = graCR_setindex ;
05962            cbs.key    = nn ;
05963            cbs.event  = NULL ;
05964 #if 0
05965            grapher->status->send_CB( grapher, grapher->getaux, &cbs ) ;
05966 #else
05967            CALL_sendback( grapher , cbs ) ;
05968 #endif
05969          } else {
05970            (void) drive_MCW_grapher( grapher, graDR_setindex, (XtPointer)nn) ;
05971          }
05972        }
05973      }
05974      break ;
05975 
05976      case GRA_TIMERFUNC_BOUNCE:{
05977        int nn = grapher->time_index , nt=grapher->status->num_series ;
05978        if( nt > 1 && grapher->timer_param != 0 ){
05979          nn = nn + grapher->timer_param ;
05980          if( nn <  0  ){
05981            nn = -nn; grapher->timer_param = -grapher->timer_param;
05982          } else if( nn >= nt ){
05983            nn = 2*(nt-1)-nn; grapher->timer_param = -grapher->timer_param;
05984          }
05985          redo = 1 ;
05986          if( grapher->status->send_CB != NULL ){
05987            GRA_cbs cbs ;
05988            cbs.reason = graCR_setindex ;
05989            cbs.key    = nn ;
05990            cbs.event  = NULL ;
05991 #if 0
05992            grapher->status->send_CB( grapher, grapher->getaux, &cbs ) ;
05993 #else
05994            CALL_sendback( grapher , cbs ) ;
05995 #endif
05996          } else {
05997            (void) drive_MCW_grapher( grapher, graDR_setindex, (XtPointer)nn) ;
05998          }
05999        }
06000      }
06001      break ;
06002 
06003    }
06004 
06005    if( redo ) grapher->timer_id = XtAppAddTimeOut(
06006                                    XtWidgetToApplicationContext(grapher->opt_quit_pb) ,
06007                                    grapher->timer_delay , GRA_timer_CB , grapher ) ;
06008    else       grapher->timer_id = 0 ;
06009 
06010    EXRETURN ;
06011 }
06012 
06013 
06014 
06015 void GRA_timer_stop( MCW_grapher *grapher )
06016 {
06017    if( grapher->timer_id > 0 ){ XtRemoveTimeOut(grapher->timer_id); grapher->timer_id = 0; }
06018 }