Index: /trunk/SophyaPI/PI/Makefile
===================================================================
--- /trunk/SophyaPI/PI/Makefile	(revision 17)
+++ /trunk/SophyaPI/PI/Makefile	(revision 18)
@@ -82,5 +82,5 @@
  
 all: $(LIB)libPI.a 
-$(LIB)libPI.a : $(OBJ)piapplgen.o $(OBJ)piapplx.o $(OBJ)pibwdggen.o $(OBJ)pibwdgx.o $(OBJ)picmap.o $(OBJ)picmapgen.o $(OBJ)picmapx.o $(OBJ)picontainergen.o $(OBJ)picontainerx.o $(OBJ)pidrawwin.o $(OBJ)pifilechogen.o $(OBJ)pifilechox.o $(OBJ)pihisto.o $(OBJ)piimage.o $(OBJ)pimenubargen.o $(OBJ)pimenubarx.o $(OBJ)pimenugen.o $(OBJ)pimenux.o $(OBJ)pimsghandler.o $(OBJ)pioptmenugen.o $(OBJ)pioptmenux.o $(OBJ)pipixmapgen.o $(OBJ)pipixmapx.o $(OBJ)piscdrawwdg.o $(OBJ)pistdwdggen.o $(OBJ)pistdwdgx.o $(OBJ)piwdggen.o $(OBJ)piwdgx.o $(OBJ)piwindowgen.o $(OBJ)piwindowx.o $(OBJ)pixtbase.o
+$(LIB)libPI.a : $(OBJ)piapplgen.o $(OBJ)piapplx.o $(OBJ)pibwdggen.o $(OBJ)pibwdgx.o $(OBJ)picmap.o $(OBJ)picmapgen.o $(OBJ)picmapx.o $(OBJ)picontainergen.o $(OBJ)picontainerx.o $(OBJ)pidrawwin.o $(OBJ)pifilechogen.o $(OBJ)pifilechox.o $(OBJ)pihisto.o $(OBJ)piimage.o $(OBJ)pimenubargen.o $(OBJ)pimenubarx.o $(OBJ)pimenugen.o $(OBJ)pimenux.o $(OBJ)pimsghandler.o $(OBJ)pioptmenugen.o $(OBJ)pioptmenux.o $(OBJ)piperiodx.o $(OBJ)pipixmapgen.o $(OBJ)pipixmapx.o $(OBJ)piscdrawwdg.o $(OBJ)pistdwdggen.o $(OBJ)pistdwdgx.o $(OBJ)piwdggen.o $(OBJ)piwdgx.o $(OBJ)piwindowgen.o $(OBJ)piwindowx.o $(OBJ)pixtbase.o
 	$(AR) $(ARFLAGS)  $@ $?
  
@@ -180,4 +180,5 @@
  pibwdggen.h piwdgx.h piwdggen.h pimsghandler.h picmap.h picmapx.h \
  picmapgen.h
+$(OBJ)piperiodx.o: ./piperiodx.cc ./piperiodx.h $(INC)periodic.h
 $(OBJ)pipixmapx.o: ./pipixmapx.cc pipixmapx.h pipixmapgen.h pisysdep.h \
  pibwdgx.h pibwdggen.h piwdgx.h piwdggen.h pimsghandler.h picmap.h \
Index: /trunk/SophyaPI/PI/piapplgen.cc
===================================================================
--- /trunk/SophyaPI/PI/piapplgen.cc	(revision 17)
+++ /trunk/SophyaPI/PI/piapplgen.cc	(revision 18)
@@ -2,5 +2,5 @@
 
 PIApplicationGen::PIApplicationGen()
-:topcont(NULL), menubar(NULL)
+:topcont(NULL), menubar(NULL), mState(0)
 {}
 
@@ -15,2 +15,23 @@
 return;
 }
+
+void PIApplicationGen::SetReady()
+{
+mState = kReadyState;
+}
+
+void PIApplicationGen::SetBusy()
+{
+mState = kBusyState;
+}
+
+void PIApplicationGen::SetBlocked()
+{
+mState = kBlockedState;
+}
+
+int PIApplicationGen::GetState()
+{
+return mState;
+}
+
Index: /trunk/SophyaPI/PI/piapplgen.h
===================================================================
--- /trunk/SophyaPI/PI/piapplgen.h	(revision 17)
+++ /trunk/SophyaPI/PI/piapplgen.h	(revision 18)
@@ -9,4 +9,6 @@
 class PIApplicationGen : public PIMsgHandler {
 public:
+        enum { kReadyState=0, kBusyState=1, kBlockedState=2 };
+
 				  PIApplicationGen();
 	virtual		          ~PIApplicationGen();
@@ -14,4 +16,10 @@
 	virtual void              Run()=0;
 	virtual void              Stop()=0;
+
+        virtual void              SetReady();
+        virtual void              SetBusy(); 
+        virtual void              SetBlocked();
+        virtual int               GetState();
+
 
 	virtual PIContainer*      MainWin() {return topcont;}
@@ -22,4 +30,5 @@
     PIContainer               *topcont;
     PIMenubar                 *menubar;
+    int                       mState;
 };
 
Index: /trunk/SophyaPI/PI/piapplx.cc
===================================================================
--- /trunk/SophyaPI/PI/piapplx.cc	(revision 17)
+++ /trunk/SophyaPI/PI/piapplx.cc	(revision 18)
@@ -1,3 +1,4 @@
 #include <stdio.h>
+#include <X11/cursorfont.h>
 #include "piapplx.h"
 #include "picontainerx.h"
@@ -5,4 +6,7 @@
 
 #define DEBUG_APPLX
+
+static Cursor a_curs[3];
+static bool a_fgcur=false;
 
 /* --Methode-- */
@@ -38,4 +42,16 @@
 MBCont()->Show();
 topwdg->Manage();
+
+if (!a_fgcur)
+  {
+  Display *mdsp;
+  mdsp = XtDisplay (topwdg->XtWdg()); 
+  a_curs[0] = XCreateFontCursor(mdsp, XC_arrow);
+  a_curs[1] = XCreateFontCursor(mdsp, XC_X_cursor);
+  a_curs[2] = XCreateFontCursor(mdsp, XC_watch);
+  }
+
+mState = -1;
+SetReady();
 }
 
@@ -46,4 +62,6 @@
 puts("PIApplicationX::~PIApplicationX()_info : App delete");
 #endif
+Display *mdsp;
+mdsp = XtDisplay (topwdg->XtWdg()); 
 topwdg->Unmanage();
 delete menubar;
@@ -51,5 +69,6 @@
 delete intcont;
 delete topwdg;
-exit(0);
+XtCloseDisplay(mdsp);
+return;
 }
 
@@ -71,2 +90,44 @@
 return;
 }
+
+
+/* --Methode-- */
+void PIApplicationX::SetReady()
+{
+if (mState != kReadyState)
+  {
+  Display * mdsp;
+  mState = kReadyState;
+  mdsp = XtDisplay (topwdg->XtWdg());
+  XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[0]);
+  XFlush(mdsp);
+  }
+}
+
+/* --Methode-- */
+void PIApplicationX::SetBusy()
+{
+if (mState != kBusyState)
+  {
+  Display * mdsp;
+  mState = kBusyState;
+  mdsp = XtDisplay (topwdg->XtWdg());
+//  if ( XtIsRealized(topwdg->XtWdg()) )
+  XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[2]);
+  XFlush(mdsp);
+  }
+return;
+}
+
+/* --Methode-- */
+void PIApplicationX::SetBlocked()
+{
+if (mState != kBlockedState)
+  {
+  Display * mdsp;
+  mState = kBlockedState;
+  mdsp = XtDisplay (topwdg->XtWdg());
+  XDefineCursor(mdsp, XtWindow(topwdg->XtWdg()), a_curs[1]);
+  XFlush(mdsp);
+  }
+}
Index: /trunk/SophyaPI/PI/piapplx.h
===================================================================
--- /trunk/SophyaPI/PI/piapplx.h	(revision 17)
+++ /trunk/SophyaPI/PI/piapplx.h	(revision 18)
@@ -16,4 +16,8 @@
   virtual void    Stop() { mStop = false; }
 
+  virtual void    SetReady();
+  virtual void    SetBusy(); 
+  virtual void    SetBlocked();
+
   PIContainer*    MBCont() { return intcont; }
 protected:
Index: /trunk/SophyaPI/PI/pibwdggen.cc
===================================================================
--- /trunk/SophyaPI/PI/pibwdggen.cc	(revision 17)
+++ /trunk/SophyaPI/PI/pibwdggen.cc	(revision 18)
@@ -43,4 +43,5 @@
 void PIBaseWdgGen::But1Press(int /*x*/, int /*y*/)
 {
+Send(PIMsg_ActiveWdg);
 #ifdef DEBUG_RZXB
 printf ("PIBaseWdgGen::But1Press PosX,Y= %d %d \n", x, y);
Index: /trunk/SophyaPI/PI/pibwdgx.cc
===================================================================
--- /trunk/SophyaPI/PI/pibwdgx.cc	(revision 17)
+++ /trunk/SophyaPI/PI/pibwdgx.cc	(revision 18)
@@ -584,4 +584,5 @@
 void PIBaseWdgX::But1Release(int x, int y)
 {
+Send(PIMsg_ActiveWdg);
 #ifdef DEBUG_PIBWDGX
 printf ("PIBaseWdgX::ButReleasePosX,Y= %d %d \n", x, y);
@@ -643,5 +644,5 @@
 #ifdef DEBUG_PIBWDGX
 printf("PIBaseWdgX::Keyboard: Key= %d (%c)  Mod= %d \n", key, (char)key, (int)kmod); 
-XSync(XtDisplay(this->XtWdg()), 0);
+XFlush(XtDisplay(this->XtWdg()));
 #endif
 return;
Index: /trunk/SophyaPI/PI/picontainergen.cc
===================================================================
--- /trunk/SophyaPI/PI/picontainergen.cc	(revision 17)
+++ /trunk/SophyaPI/PI/picontainergen.cc	(revision 18)
@@ -1,3 +1,6 @@
 #include "picontainergen.h"
+
+
+#define NCHALGRP 4
 
 /* --Methode-- */
@@ -5,10 +8,20 @@
                                int sx, int sy, int px, int py)
 : PIWdg(par, nom, sx, sy, px, py) , stvis(0) 
-{}
+{
+mNCh = 0;
+mACSup = mPaSup = false;
+mChilds = new (PIWdgGen * [NCHALGRP]);
+mMxCh = NCHALGRP;
+}
 
 
 /* --Methode-- */
 PIContainerGen::~PIContainerGen()
-{}
+{
+mPaSup = true;   // Pour prevenir ChildDel()
+if (mACSup)
+  for(int i=0; i<mNCh; i++)  delete mChilds[i];
+delete[] mChilds;
+}
 
 /* --Methode-- */
@@ -16,10 +29,4 @@
 {
 stvis = 1;
-return;
-}
-
-/* --Methode-- */
-void PIContainerGen::NewChild(PIWdgGen* /*child*/)
-{
 return;
 }
@@ -38,2 +45,40 @@
 }
 
+/* --Methode-- */
+void PIContainerGen::ChildAdd(PIWdgGen* child)
+{
+if (child == NULL)  return;
+if (mNCh == mMxCh)
+  {
+  PIWdgGen ** och = mChilds;
+  mChilds = new (PIWdgGen * [mMxCh+NCHALGRP]);
+  for(int i=0; i<mNCh; i++)  mChilds[i] = och[i];
+  delete[] och;
+  mMxCh += NCHALGRP;
+  }  
+mChilds[mNCh] = child;   mNCh++; 
+return;
+}
+
+/* --Methode-- */
+void PIContainerGen::ChildDel(PIWdgGen* child)
+{
+if (mPaSup) return;  // Ne rien faire si delete initie par Container-parent
+if ((child == NULL) || (mNCh < 1))  return;
+for(int i=0; i<mNCh; i++)  
+  if (mChilds[i] == child)
+    {
+    mNCh--;
+    mChilds[i] = mChilds[mNCh];
+    break;
+    }
+return;
+}
+
+
+/* --Methode-- */
+void PIContainerGen::SetAutoDelChilds(bool ad)
+{
+mACSup = ad;
+return;
+}
Index: /trunk/SophyaPI/PI/picontainergen.h
===================================================================
--- /trunk/SophyaPI/PI/picontainergen.h	(revision 17)
+++ /trunk/SophyaPI/PI/picontainergen.h	(revision 18)
@@ -16,5 +16,5 @@
   virtual      ~PIContainerGen();
 
-  virtual void         NewChild(PIWdgGen * child);
+  virtual long         kind() {return ClassId; }
 
   virtual void         Show();
@@ -22,8 +22,13 @@
   virtual int          Visible();
 
-  virtual long         kind() {return ClassId; }
+  virtual void         ChildAdd(PIWdgGen * child);
+  virtual void         ChildDel(PIWdgGen * child);
 
+  void                 SetAutoDelChilds(bool ad = false);
 protected:
   int                  stvis;
+  PIWdgGen          ** mChilds;
+  int                  mNCh, mMxCh;
+  bool                 mPaSup,mACSup;
 };
 
Index: /trunk/SophyaPI/PI/pidemo.cc
===================================================================
--- /trunk/SophyaPI/PI/pidemo.cc	(revision 17)
+++ /trunk/SophyaPI/PI/pidemo.cc	(revision 18)
@@ -5,7 +5,12 @@
 
 #include "perrors.h"
+#include "ctimer.h"
 #include "fitsimage.h"
+#include "dynccd.h"
 
 #include "strecinit.h"
+
+#include "psf.h"
+#include "nbmath.h"
 
 #include "pidemo.h"
@@ -14,38 +19,65 @@
 
 /*  Pour le link dynamique  */
-typedef FitsImageR4 * (* UsPrFunc) (FitsImageR4 * myimg, int xp, int yp, int dx, int dy,
-                                    char * arg1, char * arg2);
+typedef ImageR4 * (* UsPrFunc) (ImageR4 * myimg, int xp, int yp, int dx, int dy,
+                                char * arg1, char * arg2);
 static UsPrFunc  userFunc[3] = { NULL, NULL, NULL };
 static void *dlhandle = NULL;
 
-static char userlibname[256];
-
-static int DynLink(char *usln);
-
-int DynLink(char *usln)
+/*  Variables globales pour les options et parametres de pidemo  */
+static int pav_sz=15;
+static bool rect_pav=true;
+static int typ_psf=1;
+static bool prt_pval=true;
+static DynCCD *mdynccd;   // Initialise ds main()
+static bool his_ckd=true;
+static float his_min=0.;
+static float his_max=2000.;
+static int his_nbin=200; 
+static bool vis_cursshow=true;
+static int vis_zoom=1;
+/*  Fin des options / variables globales                         */
+
+static int DynLink(bool fgmk);
+
+/* Nouvelle-Fonction */
+int DynLink(bool fgmk)
 {
 UsPrFunc uf;
 int i, rc;
 
-if (usln != NULL)  strncpy(userlibname, usln, 255);  userlibname[255] = '\0';
-
-printf("PIDemo_DynLink(): Relinking from %s \n", userlibname);
+#ifdef C_NO_SHLIBS
+puts("PIDemo/DynLink() Warning: Cette version a ete cree sans ShLibs ");
+puts(" ... Pas de possibite de link dynamique -> Pas de UserProc ... ");
+for(i=0; i<3; i++)  userFunc[i] = NULL;
+return(0);
+#endif
+
+TIMEF
 if (dlhandle != NULL)  dlclose(dlhandle);
 for(i=0; i<3; i++)  userFunc[i] = NULL;
-dlhandle =  dlopen(userlibname, RTLD_NOW);
+
+if (fgmk)
+  {
+  printf("PIDemo_DynLink(): Making user.so, Relinking from user.so -> piup.so \n");
+  puts("PIDemo_DynLink(): Executing make -f piusmak piup.so ");
+  system("time make -f piusmak piup.so");
+  SPLITTIME
+  }
+
+dlhandle =  dlopen("piup.so", RTLD_NOW);
 if (dlhandle == NULL)  
-  { printf("PIDemo_DynLink(), Erreur d'ouverture UserShLib %s \n", userlibname);
+  { printf("PIDemo_DynLink(), Erreur d'ouverture UserShLib piup.so \n");
   return(100); }
 rc = 0;
 printf("PIDemo_DynLink(): Searching for PIUserProc_1 (Rc=%d) \n", rc);
-uf = dlsym(dlhandle, "PIUserProc_1__FPt9FitsImage1ZfiiiiPcT5");
+uf = (UsPrFunc) dlsym(dlhandle, "PIUserProc_1__FPt5Image1ZfiiiiPcT5");
 if (uf != NULL)  userFunc[0] = uf;
 else rc += 2;
 printf("PIDemo_DynLink(): Searching for PIUserProc_3 (Rc=%d) \n", rc);
-uf = dlsym(dlhandle, "PIUserProc_2__FPt9FitsImage1ZfiiiiPcT5");
+uf = (UsPrFunc) dlsym(dlhandle, "PIUserProc_2__FPt5Image1ZfiiiiPcT5");
 if (uf != NULL)  userFunc[1] = uf;
 else rc += 4;
 printf("PIDemo_DynLink(): Searching for PIUserProc_3 (Rc=%d) \n", rc);
-uf = dlsym(dlhandle, "PIUserProc_3__FPt9FitsImage1ZfiiiiPcT5");
+uf = (UsPrFunc) dlsym(dlhandle, "PIUserProc_3__FPt5Image1ZfiiiiPcT5");
 if (uf != NULL)  userFunc[2] = uf;
 else rc += 8;
@@ -61,12 +93,12 @@
 /* --Methode-- */
 PIDemoApp::PIDemoApp(char *path)
-: PIApplication(300, 200)
-{
-for(int i=0; i<NMXIMG; i++)
-  { wpii[i] = NULL;  pii[i] = NULL;  img[i] = NULL; }
-mNimg = 0;   mCur = NULL;
-mState = 0;
-
-m[0] = new PIPDMenu((PIMsgHandler *)Menubar(),"File");
+: PIApplication(400, 230)
+{
+for(int i=0; i<NMXWIN; i++)
+  { awp[i] = NULL;  taw[i] = 0; }
+mNwin = 0;   mTnw = 0;  mCurWin = NULL;  mCurPII = NULL;   
+
+m[0] = new PIPDMenu((PIMsgHandler *)Menubar(),"Fichier");
+m[0]->AppendItem("Options", 10110);
 m[0]->AppendItem("Open", 10101);
 m[0]->AppendItem("Close", 10102);
@@ -75,68 +107,50 @@
 m[0]->AppendItem("Exit", 10105);
 
-m[1] = new PIPDMenu((PIMsgHandler *)Menubar(),"Image");
-m[1]->AppendItem("Lut...", 10201);
-m[1]->AppendItem("Size*1", 10202);
-m[1]->AppendItem("Expand*2", 10203);
-m[1]->AppendItem("Compress*2", 10204);
-m[1]->AppendItem("Expand*4", 10205);
+m[1] = new PIPDMenu((PIMsgHandler *)Menubar(),"Visual.");
+m[1]->AppendItem("Lut...", 10221);
+m[1]->AppendItem("Size*1", 10211);
+m[1]->AppendItem("Expand*2", 10212);
+m[1]->AppendItem("Compress*2", 10208);
+m[1]->AppendItem("Expand*4", 10214);
 m[1]->AppendItem("Compress*4", 10206);
-
-m[2] = new PIPDMenu((PIMsgHandler *)Menubar(),"Operation");
+m[1]->AppendItem("CursorOFF", 10231);
+m[1]->AppendItem("CursorON", 10232);
+m[1]->AppendItem("PrintLut", 10222);
+
+m[2] = new PIPDMenu((PIMsgHandler *)Menubar(),"Ope.Image");
 m[2]->AppendItem("UserProc-1", 10301);
 m[2]->AppendItem("UserProc-2", 10302);
 m[2]->AppendItem("UserProc-3", 10303);
-m[2]->AppendItem("PixelHisto", 10304);
-m[2]->AppendItem("PrintLut", 10305);
-m[2]->AppendItem("CheckDyn", 10306);
+m[2]->AppendItem("PixelHisto", 10331);
+m[2]->AppendItem("Info (dyn)", 10332);
+
+m[3] = new PIPDMenu((PIMsgHandler *)Menubar(),"Ope.Pave");
+m[3]->AppendItem("Extract", 10401);
+m[3]->AppendItem("Coupe", 10410);
+m[3]->AppendItem("Fit-PSF", 10420);
+m[3]->AppendItem("PixelHisto", 10431);
+m[3]->AppendItem("Print", 10432);
+
+m[4] = NULL;
 
 AppendMenu(m[0]);
 AppendMenu(m[1]);   
 AppendMenu(m[2]);
-
-zoom = new PIPixmap(this->MainWin(), "Zoom", 120,120,10,10);
-labimg = new PILabel(this->MainWin(), "PixelValue", 280, 25, 10, 150);
+AppendMenu(m[3]);
+
+
+zoom = new PIPixmap(this->MainWin(), "Zoom", 140,140,10,20);
+
+mTlb[0] = new PILabel(this->MainWin(), "Img: ", 40, 25, 5, 200);
+labimg = new PILabel(this->MainWin(), "PixelValue", 345, 25, 50, 200);
 labimg->SetLabel("");
-
-draw = new PIDrawWindow(this, "histo", PIWK_normal, 300, 300, 300, 300);
-pixelHisto = new Histo(0,2000, 200);
-draw->AddDrawer(piHisto=new PIHisto(pixelHisto));
-
+mTlb[1] = new PILabel(this->MainWin(), "Drw: ", 40, 25, 5, 170);
+labdrw = new PILabel(this->MainWin(), "Coord", 140, 25, 50, 170);
+labdrw->SetLabel("");
+
+lab = NULL;
 // lab =  new PILabel(this->MainWin(), "State", 60, 20, 150, 10);
 // lab->SetBorderWidth(1);
  
-olb[0] = new PILabel(this->MainWin(), "AutoLut :", 60, 25, 140, 30);
-pum[0] = new PIPUMenu((PIMsgHandler *)this, "AutoLut");
-pum[0]->AppendItem("+3 Sig", 20303);
-pum[0]->AppendItem("+2 Sig", 20302);
-pum[0]->AppendItem("+1 Sig", 20301);
-pum[0]->AppendItem("-1 Sig", 20299);
-pum[0]->AppendItem("-2 Sig", 20298);
-opm[0] = new PIOptMenu(this->MainWin(), pum[0], 80, 25, 210, 30);
-opm[0]->SetValue(20302);
-
-olb[1] = new PILabel(this->MainWin(), "Zoom :", 60, 25, 140, 70);
-pum[1] = new PIPUMenu((PIMsgHandler *)this, "Zoom");
-pum[1]->AppendItem("Agr. * 5", 20105);
-pum[1]->AppendItem("Agr. * 4", 20104);
-pum[1]->AppendItem("Agr. * 3", 20103);
-pum[1]->AppendItem("Agr. * 2", 20102);
-pum[1]->AppendItem("* 1", 20101);
-pum[1]->AppendItem("Red. / 2", 20098);
-pum[1]->AppendItem("Red. / 3", 20097);
-pum[1]->AppendItem("Red. / 4", 20096);
-pum[1]->AppendItem("Red. / 5", 20095);
-opm[1] = new PIOptMenu(this->MainWin(), pum[1], 80, 25, 210, 70);
-opm[1]->SetValue(20101);
-
-olb[2] = new PILabel(this->MainWin(), "ColMap :", 60, 25, 140, 110);
-pum[2] = new PIPUMenu((PIMsgHandler *)this, "ColMap");
-pum[2]->AppendItem("Grey32", 20201);
-pum[2]->AppendItem("GreyInv32", 20202);
-pum[2]->AppendItem("ColRJ32", 20203);
-pum[2]->AppendItem("ColBR32", 20204);
-pum[2]->AppendItem("ColRV32", 20205);
-opm[2] = new PIOptMenu(this->MainWin(), pum[2], 80, 25, 210, 110);
-opm[2]->SetValue(20203);
 
 pfc = new PIFileChooser(this,"FileChooser", 5000); 
@@ -144,7 +158,13 @@
   pfc->SetPath(path);
 
+mVisTC = new VisuTools(this, this->MainWin());
+mCutTC = new CutTools(this, this->MainWin());
+
+mVisTC->Show();
+mTCId = 1;
 
 mLutw = new LutWind(this);
 mUP = new UserProcWind(this);
+mOptw = new OptionWind(this);
 
 SetReady();
@@ -155,30 +175,24 @@
 {
 int i;
-for(i=0; i<3; i++)
-  { 
-  delete m[i];
-  delete opm[i];
-  delete pum[i];
-  delete olb[i];
-  }
-delete pfc;
+for(i=0; i<5; i++)  delete m[i];
+
 delete lab;
 
-for(i=0; i<mNimg; i++)
-  {  
-  delete pii[i];
-  delete img[i];
-  delete wpii[i];
-  }
+for(i=0; i<mNwin; i++)
+  delete awp[i];
 
 delete zoom;
 delete labimg;
-
-delete draw;
-delete pixelHisto;
-delete piHisto;
+delete labdrw;
+delete mTlb[0]; 
+delete mTlb[1]; 
+
+delete pfc;
+delete mVisTC;
+delete mCutTC;
 
 delete mLutw;
 delete mUP;
+delete mOptw;
 
 }  
@@ -187,29 +201,47 @@
 void PIDemoApp::Process(long msg, PIMsgHandler* sender, void* data)
 {
-CMapId cmap[5] = { CMAP_GREY32, CMAP_GREYINV32, CMAP_COLRJ32, 
-                   CMAP_COLBR32, CMAP_COLRV32 } ;
-
-
-if  ((mState != 0) && (msg > 9999)) { PIBeep(); return; }
-
-
-if ( (msg > 20000)  && (CurrentPIImage()) )
-  {
-  msg -= 20000;
-  if ((msg < 110) && (msg > 90))  // Zoom
-    CurrentPIImage()->SetZoom(msg-100);
-  else if ((msg > 200) && (msg < 206)) // Changement de couleur 
-    CurrentPIImage()->SetColMap(cmap[msg-201]);
-  else if ((msg > 290) && (msg < 310))
-    CurrentPIImage()->SetLut( CurrentImage()->minPix, CurrentImage()->maxPix,
-                               kLutType_Lin, msg-300);
-  }
-
-else
-  {
-  switch (msg)
+PIWdg *sndw;
+
+if  ((GetState()) && (msg > 9999)) { PIBeep(); return; }
+
+if ( (msg >= 5100) && (msg < 5200) )    MBProcess1(msg, sender, data);
+else if ( (msg >= 10100) && (msg < 10200) )  MBProcess1(msg, sender, data);
+else if ( (msg >= 10200) && (msg < 10300) )  MBProcess2(msg, sender, data);
+else if ( (msg >= 10300) && (msg < 10400) )  MBProcess3(msg, sender, data);
+else if ( (msg >= 10400) && (msg < 10500) )  MBProcess4(msg, sender, data);
+else if (msg == PIMsg_ActiveWdg)
+  {
+  sndw = (PIWdg *)sender;
+  switch(sndw->kind())
     {
-    case  PIMsg_ActiveWindow :
-      mCur = (PIImage *)sender;
+    case PIWindow::ClassId :
+      mCurWin = (PIWindow *)sender;
+      break;
+    case PIImage::ClassId :
+      mCurPII = (PIImage *)sender;
+      break;
+    }
+  }
+else printf("PIDemoApp::Process() BUG?? Msg %d  \n", (int)msg);
+
+/*   Changement palette outil  */
+
+if (msg != 10410)   
+  {
+  if (mTCId != 1)  { mCutTC->Hide();  mVisTC->Show();  mTCId = 1; }
+  }
+
+return;
+}
+
+/* --Methode-- */
+void PIDemoApp::MBProcess1(long msg, PIMsgHandler* /*sender*/, void* data)
+{
+
+  switch(msg)
+    {
+    case 10110:
+      SetBlocked();
+      mOptw->Show();
       break;
 
@@ -234,5 +266,5 @@
     case 10104:
       SetBusy();
-      DynLink(NULL);
+      DynLink(true);
       SetReady();
       break;
@@ -243,4 +275,5 @@
 
     case 5101 :
+      SetBusy();
       if (data)  OpenImage(pfc->GetFileName());
       SetReady();
@@ -248,123 +281,320 @@
 
     case 5103 :
-      if (data)  SaveCurrent(pfc->GetFileName());
+      SetBusy();
+      if (data)  SaveCurrentImg(pfc->GetFileName());
       SetReady();
       break;      
-
-    case 10201 :
-      if ( CurrentPIImage() )  mLutw->Show();
-      break;
-
-    case 10202 :
-      if ( CurrentPIImage()) 
-        {
-        CurrentPIImage()->SetZoom(1, false);
-        CurrentPIImage()->SetOffset(0, 0, false);
-        CurrentImgWin()->SetSize(CurrentImage()->XSize(), 
-                                  CurrentImage()->YSize());
-        }
-      break;
-
-    case 10203 :
-      if ( CurrentPIImage()) 
-        {
-        CurrentPIImage()->SetZoom(2, false);
-        CurrentPIImage()->SetOffset(0, 0, false);
-        CurrentImgWin()->SetSize(CurrentImage()->XSize()*2, 
-                                  CurrentImage()->YSize()*2);
-        }
-      break;
-
-    case 10204 :
-      if ( CurrentPIImage()) 
-        {
-        CurrentPIImage()->SetZoom(-2, false);
-        CurrentPIImage()->SetOffset(0, 0, false);
-        CurrentImgWin()->SetSize(CurrentImage()->XSize()/2, 
-                                  CurrentImage()->YSize()/2);
-        }
-      break;
+  }
+return;
+}
+
+/* --Methode-- */
+void PIDemoApp::MBProcess2(long msg, PIMsgHandler* /*sender*/, void* /*data*/)
+{
+int snx,sny,sx,sy,zm,wt;
+
+if (CurrentPIImage() == NULL) return;
+wt = GetCurrentWType();
+if ( (wt<1) || (wt>9) ) return;
+
+switch (msg)
+  {
+  case 10221 :
+    mLutw->Show();
+    break;
+
+  case 10222 :
+    CurrentPIImage()->Lut()->Print();
+    break;
+
+  case 10231 :
+    CurrentPIImage()->ShowCursor(false);
+    break;
+
+  case 10232 :
+    CurrentPIImage()->ShowCursor(true);
+    break;
+
+  case 10211 :
+  case 10212 :
+  case 10214 :
+  case 10208 :
+  case 10206 :
+    SetBusy();
+    if (wt > 3) return;
+    zm = msg-10210;
+    CurrentPIImage()->SetZoom(zm, false);
+    sx = CurrentWindow()->XSize();
+    sy = CurrentWindow()->YSize();
+    if (zm > 0)
+      {
+      snx = CurrentImage()->XSize()*zm;
+      sny = CurrentImage()->YSize()*zm;
+      }
+    else 
+      {
+      snx = CurrentImage()->XSize()/(-zm);
+      sny = CurrentImage()->YSize()/(-zm);
+      }
+    if ((snx != sx) || (sny != sy))
+      {
+      CurrentPIImage()->SetOffset(0, 0, false);
+      CurrentWindow()->SetSize(snx, sny);
+      }
+    else   CurrentPIImage()->SetOffset(0, 0);
+    SetReady();
+    break;
+
+  default:
+    printf("PIDemoApp::MBProcess2() BUG?? Msg %d  \n", (int)msg);    
+    break;
+
+  }
+
        
-    case 10205 :
-      if ( CurrentPIImage()) 
-        {
-        CurrentPIImage()->SetZoom(4, false);
-        CurrentPIImage()->SetOffset(0, 0, false);
-        CurrentImgWin()->SetSize(CurrentImage()->XSize()*4, 
-                                  CurrentImage()->YSize()*4);
-        }
-      break;
-
-    case 10206 :
-      if ( CurrentPIImage()) 
-        {
-        CurrentPIImage()->SetZoom(-4, false);
-        CurrentPIImage()->SetOffset(0, 0);
-        CurrentImgWin()->SetSize(CurrentImage()->XSize()/4, 
-                                  CurrentImage()->YSize()/4);
-        }
-      break;
-       
-
-    case 10301:
-    case 10302:
-    case 10303:
-      if ( CurrentPIImage() )
-        {
-        SetBlocked();
-        mUP->SelectUP(msg-10300);
-        mUP->Show();
-        }
-      break;
+}
+
+/* --Methode-- */
+void PIDemoApp::MBProcess3(long msg, PIMsgHandler* /*sender*/, void* /*data*/)
+{
+ImageR4 * pim;
+Histo * pixelhis;
+float xmn, xmx, vmn, vsg; 
+int k;
+
+if (CurrentPIImage() == NULL) return;
+
+switch (msg)
+  {
+  case 10301:
+  case 10302:
+  case 10303:
+    mUP->SelectUP(msg-10300);
+    mUP->Show();
+  break;
       
-    case 10304:
-      pixelHisto->Zero();
-      for (int i=0; i<CurrentImage()->XSize(); i++)
-        for (int j=0; j<CurrentImage()->YSize(); j++)
-          pixelHisto->Add((*CurrentImage())(i,j));
-      piHisto->Refresh();
-      draw->Show();
-      break;
-
-    case 10305:
-      if ( CurrentPIImage() )
-        (CurrentPIImage()->Lut())->Print();
-      break;
-
-    case 10306:
-      if ( CurrentPIImage() )
-        {
-        SetBusy();
-        CurrentImage()->CheckDyn();
-        CurrentImage()->Print();
-        SetReady();
-        }
-      break;
-
-              
-    }
-  }
-}
-
-/* --Methode-- */
-void PIDemoApp::SetBusy()
-{
-mState = 2;
-// lab->SetLabel("Working ...");
-}
-
-/* --Methode-- */
-void PIDemoApp::SetReady()
-{
-mState = 0;
-// lab->SetLabel("**Ready**");
-}
-
-/* --Methode-- */
-void PIDemoApp::SetBlocked()
-{
-mState = 1;
-// lab->SetLabel("--Dialog--");
-}
+  case 10331:
+    SetBusy();
+    pim = (ImageR4 *)CurrentImage();
+    if (his_ckd)
+      {
+      pim->MoySigma(vmn, vsg, his_min, his_max, 5);
+//    pim->Print();
+      xmn = vmn-4.*vsg;  xmx = vmn+4.*vsg;
+      if (xmn < his_min) xmn = his_min;
+      if (xmx > his_max) xmx = his_max;
+      }
+    else { xmn=his_min;  xmx = his_max; }
+    if ((xmx-xmn) < 1.e-6)  xmx = xmn+1.e-3;
+    pixelhis = new Histo(xmn, xmx, his_nbin); 
+    for (k=0; k<pim->XSize()*pim->YSize(); k++)
+      pixelhis->Add((*pim)[k]);
+    AddHisto(pixelhis);
+    SetReady();
+    break;
+
+  case 10332:
+    SetBusy();
+    printf("\n --- Info (Dynamique) de l'image %s ---- \n", 
+           CurrentPIImage()->Nom().c_str());
+    CurrentImage()->CheckDyn((double)mdynccd->MinADU, (double)mdynccd->MaxADU);
+    CurrentImage()->Print();
+    SetReady();
+    break;
+
+  default:
+    printf("PIDemoApp::MBProcess3() BUG?? Msg %d  \n", (int)msg);    
+    break;
+
+  }
+
+}
+
+/* --Methode-- */
+void PIDemoApp::MBProcess4(long msg, PIMsgHandler* /*sender*/, void* /*data*/)
+{
+ImageR4 * pim;
+Histo * pixelhis;
+float xmn, xmx, vmn, vsg; 
+int zm, k;
+int dx, dy;
+float xc, yc;
+char strg[128];
+string s;
+bool keep=false;
+
+if (CurrentPIImage() == NULL) return;
+
+if (rect_pav)
+  {
+  dx = CurrentPIImage()->XSzPave();
+  dy = CurrentPIImage()->YSzPave();  
+  }
+else  { dx = dy = pav_sz; }
+
+xc = CurrentPIImage()->XPave()+0.5;
+yc = CurrentPIImage()->YPave()+0.5;
+
+pim = NULL;
+pim = new ImageR4(dx, dy);
+keep = false;
+CopiePave((ImageR4 &)(*pim), (ImageR4 &)(*CurrentImage()), xc, yc);
+
+switch (msg)
+  {
+  case 10401:
+    sprintf(strg,"P(%d,%d)%s",CurrentPIImage()->XPave(),
+            CurrentPIImage()->YPave(),CurrentPIImage()->Nom().c_str());
+    strg[32] = '\0';
+    zm = vis_zoom+1;
+    if (zm == 0)  zm = 1;
+    AddImage(pim, strg, zm);
+    keep = true;   
+    break;
+      
+  case 10410:
+    if (mTCId != 2)  { mVisTC->Hide();  mCutTC->Show();  mTCId = 2; } 
+    break;
+
+  case 10420:
+    SetBusy();
+    keep = FitPSF_Res(pim);
+    SetReady();
+    break;
+
+  case 10431:
+    SetBusy();
+    if (his_ckd)
+      {
+      pim->MoySigma(vmn, vsg, his_min, his_max, 2);
+//    pim->Print();
+      xmn = vmn-4.*vsg;  xmx = vmn+4.*vsg;
+      if (xmn < his_min) xmn = his_min;
+      if (xmx > his_max) xmx = his_max;
+      }
+    else { xmn=his_min;  xmx = his_max; }
+    if ((xmx-xmn) < 1.e-6)  xmx = xmn+1.e-3;
+    pixelhis = new Histo(xmn, xmx, his_nbin); 
+    for (k=0; k<pim->XSize()*pim->YSize(); k++)
+      pixelhis->Add((*pim)[k]);
+    AddHisto(pixelhis);
+    SetReady();
+    break;
+
+  case 10432:
+    SetBusy();
+    pim->CheckDyn((double)mdynccd->MinADU, (double)mdynccd->MaxADU);
+    printf("\n --- Pave en (%d,%d) de l'image %s ---- \n", 
+           CurrentPIImage()->XPave(), CurrentPIImage()->YPave(),
+           CurrentPIImage()->Nom().c_str());
+     if (prt_pval) 
+      pim->PrintImage(); 
+    else pim->Print();
+    SetReady();
+    break;
+
+  default:
+    printf("PIDemoApp::MBProcess4() BUG?? Msg %d  \n", (int)msg);    
+    break;
+
+  }
+
+if (!keep) delete pim;
+return;
+}
+
+/* --Methode-- */
+bool PIDemoApp::FitPSF_Res(ImageR4 *pim)
+{
+ImageR4 *nois, *resid, *pav;
+PIImage *pii[2];
+PILabel *lab[2];
+PIWindow *win;
+int rc;
+
+if (pim == NULL)  return(false);
+
+nois = resid = pav = 0;
+if(typ_psf<1 || typ_psf>4) typ_psf = 1;
+PSF mypsf(typ_psf,100.0);
+
+TIMEF
+printf("\n -------- Resultat PIDemoApp::FitPSF_Res() ----------- \n"); 
+
+pav = pim;
+nois = NoiseImage(pav, mdynccd);
+PSFStar mypsfstar(&mypsf);
+rc = mypsfstar.IniFit(*pav, *nois);
+if(rc!=0)
+  {
+  printf("PIDemoApp::FitPSF_Res(), Echec IniFit (rc= %d) \n", rc);
+  delete nois;  return(false); 
+  }
+rc = mypsfstar.Fit(*pav, *nois);
+if(rc!=0)
+  {
+  printf("PIDemoApp::FitPSF_Res(), Echec Fit (rc= %d) \n", rc);
+  delete nois;  return(false); 
+  }
+
+mypsfstar.Print(2);
+
+float smax,axisrat,tiltdeg;
+rc = paramga((float) mypsfstar.SigX(),(float) mypsfstar.SigY()
+            ,(float) mypsfstar.Rho(),&smax,&axisrat,&tiltdeg);
+if(rc==0) 
+  printf("...   sa=%g sb=%g (%g) teta=%g\n",
+         smax,smax*axisrat,axisrat,tiltdeg);
+else puts("Erreur paramga ");
+
+resid = new ImageR4(pav->XSize(), pav->YSize());
+CopieImage((ImageR4 &)(*resid), (ImageR4 &)(*pav));
+*resid -= mypsfstar;
+
+SPLITTIME
+
+char strg[256];
+sprintf(strg, "FitPSF Pave[%d %d]_%s" ,CurrentPIImage()->XPave(),
+            CurrentPIImage()->YPave(),CurrentPIImage()->Nom().c_str());
+
+if ((win=GetWindow(340,230,9,strg)) == NULL)  
+  { delete nois; delete resid; 
+  return(false); }
+
+lab[0] = new PILabel(win, "Info1", 330, 25, 5, 5);
+sprintf(strg,"Sx= %5.2f Sy= %5.2f Rho=%4.2f Xi2Fit= %g",
+        (float)mypsfstar.SigX(),(float) mypsfstar.SigY(),
+        (float)mypsfstar.Rho(), mypsfstar.Xi2());
+lab[0]->SetLabel((string)strg);
+lab[1] = new PILabel(win, "Info2", 330, 25, 5, 35);
+sprintf(strg,"Sa= %5.2f Sb= %5.2f Tet= %5.1f deg",
+        smax, smax*axisrat, tiltdeg);
+lab[1]->SetLabel((string)strg);
+
+int zm;
+zm = 150/pav->XSize();
+if (zm < 1) zm = 1;
+
+else zm++;
+
+for(int i=0; i<2; i++)
+  {
+  pii[i] = new PIImage(win, "Pave", 160, 160, 5+170*i, 65);
+  pii[i]->SetBinding(true, true, true, true);
+  pii[i]->SetZoomWin(zoom);
+  pii[i]->SetTextWin(labimg);
+  pii[i]->SetZoom(zm, false);
+  pii[i]->SetAutoDelImage(true);
+  pii[i]->ShowCursor(false);
+  }
+
+pii[0]->SetImage(pav);
+pii[1]->SetImage(resid);
+
+delete nois;
+return(true);
+}
+
+
 
 /* --Methode-- */
@@ -377,7 +607,9 @@
 FitsImageR4 * myimg;
 
-if (mNimg >= NMXIMG)
-  {
-  printf("PIDemoApp::OpenImage_Error Two many images (%d) opened ...\n",mNimg);
+TIMEF
+
+if (mNwin >= NMXWIN)
+  {
+  printf("PIDemoApp::OpenImage_Error Two many images (%d) opened ...\n",mNwin);
   printf("   Close some of the images ... ");
   return;
@@ -406,12 +638,12 @@
   {
   string s(buff);
-  AddImage(myimg, s);
-  }
-}
-
-/* --Methode-- */
-void PIDemoApp::AddImage(FitsImageR4 * nouv, string const & name)
-{
-int nimg = mNimg;
+  AddImage(myimg, s, 0, 1);
+  }
+}
+
+/* --Methode-- */
+void PIDemoApp::AddImage(ImageR4 * nouv, string const & name, int zm, int typ)
+{
+PIImage *pii;
 int sx, sy;
 
@@ -421,35 +653,75 @@
   return;
   }
-if (mNimg >= NMXIMG)
-  {
-  printf("PIDemoApp::AddImage_Error Two many images (%d) opened ...\n",mNimg);
-  printf("   Close some of the images ... ");
+if (mNwin >= NMXWIN)
+  {
+  printf("PIDemoApp::AddImage_Error Too many (%d) opened windows...\n",mNwin);
+  printf("   Close some of the windows ... ");
   return;
   }
 
-img[nimg] = nouv;
-if ( (img[nimg]->XSize() <= 0)  || (img[nimg]->YSize() <= 0))
-  {
-  printf("PIDemoApp::OpenImage_Error Pb lecture (FitsImage::Read) \n");
+if ( (nouv->XSize() <= 0)  || (nouv->YSize() <= 0))
+  {
+  printf("PIDemoApp::AddImage_Error Pb lecture (FitsImage::Read ?) \n");
   return;
   }
 
-sx = img[nimg]->XSize();  sy = img[nimg]->YSize();
+if (zm == 0)  zm = vis_zoom;
+sx = nouv->XSize()*zm;  sy = nouv->YSize()*zm;
 if (sx > 512) sx = 512;
 if (sy > 512) sy = 512;
 
-wpii[nimg] = new PIWindow(this, (char *)name.c_str(), PIWK_normal, 
+awp[mNwin] = new PIWindow(this, (char *)name.c_str(), PIWK_normal, 
                           sx, sy, 200, 200);
-pii[nimg] = new PIImage(wpii[nimg], (char *)name.c_str(), sx, sy, 0, 0);
-pii[nimg]->SetBinding(true, true, true, true);
-pii[nimg]->SetZoomWin(zoom);
-pii[nimg]->SetTextWin(labimg);
-pii[nimg]->SetImage(img[nimg]);
-// pii[nimg]->Apply();
-wpii[nimg]->Show();
-mCur = pii[nimg];
-mNimg++;
+pii = new PIImage(awp[mNwin], (char *)name.c_str(), sx, sy, 0, 0);
+pii->SetBinding(true, true, true, true);
+pii->SetZoomWin(zoom);
+pii->SetTextWin(labimg);
+pii->SetZoom(zm, false);
+pii->SetAutoDelImage(true);
+pii->ShowCursor(vis_cursshow);
+pii->SetImage(nouv);
+awp[mNwin]->SetAutoDelChilds(true);
+awp[mNwin]->Show();
+if ((typ < 0) || (typ > 9)) typ = 3;
+taw[mNwin] = typ;
+mNwin++;  mTnw++;
 return; 
 }
+
+
+/* --Methode-- */
+void PIDemoApp::AddHisto(Histo *h)
+{
+PIDrawWindow *  draw = NULL;
+PIHisto *pih;
+char strg[64];
+
+if (h == NULL)
+  {
+  printf("PIDemoApp::AddHisto_Error  Cannot Add NULL Histo \n");
+  return;
+  }
+if (mNwin >= NMXWIN)
+  {
+  printf("PIDemoApp::AddHisto_Error Too many (%d) opened windows...\n",mNwin);
+  printf("   Close some of the windows ... ");
+  return;
+  }
+
+mTnw++;
+sprintf(strg,"Histo (W %d)",mTnw);
+draw = new PIDrawWindow(this, strg, PIWK_normal, 300, 300, 300, 300);
+pih = new PIHisto(h);
+pih->SetAutoDelHisto(true);
+draw->AddDrawer(pih);
+draw->DrawWdg()->SetTextWin(labdrw);
+awp[mNwin] = draw;
+draw->Show();
+draw->SetAutoDelChilds(true);
+taw[mNwin] = 10;
+mNwin++; 
+return; 
+}
+
 
 /* --Methode-- */
@@ -458,25 +730,30 @@
 int n;
 if ((n=GetNumCurrent()) < 0)  return;
-zoom->SetPixmap(NULL,0,0);
-zoom->EraseWindow();
-labimg->SetLabel("");
-wpii[n]->Hide();
-delete pii[n];
-delete img[n];
-delete wpii[n];
-pii[n] = pii[mNimg-1];
-wpii[n] = wpii[mNimg-1];
-img[n] = img[mNimg-1];
-mNimg--;
-mCur = NULL;
+if ((taw[n] > 0) && (taw[n] < 10))
+  {
+  zoom->SetPixmap(NULL,0,0);
+  zoom->EraseWindow();
+  labimg->SetLabel("");
+  mCurPII = NULL;
+  }
+else if ((taw[n] > 9) && (taw[n] < 20))
+  labdrw->SetLabel("");
+
+awp[n]->Hide();
+delete awp[n];
+awp[n] = awp[mNwin-1];
+taw[n] = taw[mNwin-1];
+mNwin--;
+mCurWin = NULL;
 return;
 }
 
 /* --Methode-- */
-void PIDemoApp::SaveCurrent(string const & flnm)
-{
-int n;
-if ((n=GetNumCurrent()) < 0)  return;
-
+void PIDemoApp::SaveCurrentImg(string const & flnm)
+{
+FitsImageR4 *pim = NULL;
+bool fgcr=false;
+
+if (CurrentImage() == NULL)  return;
 if (strlen(flnm.c_str()) <= 0) 
   { 
@@ -486,9 +763,24 @@
 
 printf("PIDemoApp::SaveCurrent: Image %s saved to %s \n", 
-       (char *)pii[n]->Nom().c_str(), (char *)flnm.c_str());
+       (char *)CurrentPIImage()->Nom().c_str(), (char *)flnm.c_str());
+/*  $CHECK$ Ca ne marche pas ??? pb avec IsFits()
+if (CurrentImage()->IsFits())  */    
+if (GetCurrentWType() == 1)
+  { pim = (FitsImageR4 *) CurrentImage(); 
+  fgcr = false; }
+else 
+  { pim = new FitsImageR4(*CurrentImage(),1); 
+  fgcr = true; }
+
+
 TRY {
-  img[n]->Save(flnm); 
+  pim->Save(flnm); 
 }  CATCH(merr) 
-  { printf("PIDemoApp::SaveCurrent_Error Exception= %d (%s) \n", merr, PeidaExc(merr));  } ENDTRY;
+  { 
+  printf("PIDemoApp::SaveCurrent_Error Exception= %d (%s) \n", merr, PeidaExc(merr));  
+  if (fgcr) { delete pim; fgcr=false; } 
+  } ENDTRY;
+
+if (fgcr)  delete pim;
 return;
 }
@@ -498,10 +790,10 @@
 {
 int n=-1;
-if (mNimg <= 0)  return(-1);
-for(int i=0; i<mNimg; i++)
-  if (mCur == pii[i])  { n = i; break; }
+if (mNwin <= 0)  return(-1);
+for(int i=0; i<mNwin; i++)
+  if (mCurWin == awp[i])  { n = i; break; }
 
 if (n < 0)  
-  printf(" PIDemoApp::GetNumImg() Curret image not found (%lx)\n",(long)mCur);
+  printf(" PIDemoApp::GetNumCurrent() Current Window not found (%lx)\n",(long)mCurWin);
 
 return(n);  
@@ -509,19 +801,357 @@
 
 /* --Methode-- */
-FitsImageR4 * PIDemoApp::CurrentImage()
-{
-int n;
-n = GetNumCurrent();
-if (n < 0)  return(NULL);
-else return(img[n]);
-}
-
-/* --Methode-- */
-PIWindow * PIDemoApp::CurrentImgWin()
-{
-int n;
-n = GetNumCurrent();
-if (n < 0)  return(NULL);
-else return(wpii[n]);
+int PIDemoApp::GetCurrentWType()
+{
+int k;
+k = GetNumCurrent();
+if (k < 0)  return(0);
+else return(taw[k]); 
+}
+
+/* --Methode-- */
+ImageR4 * PIDemoApp::CurrentImage()
+{
+if (mCurPII == NULL)  return(NULL);
+return((ImageR4 *)mCurPII->Image());
+}
+
+/* --Methode-- */
+PIWindow * PIDemoApp::GetWindow(int sx, int sy, int typ, char * nom)
+{
+PIWindow * win = NULL;
+
+if (mNwin >= NMXWIN)
+  {
+  printf("PIDemoApp::GetWindow_Error Too many (%d) opened windows...\n",mNwin);
+  printf("   Close some of the windows ... ");
+  return(win);
+  }
+
+awp[mNwin] = new PIWindow(this, nom, PIWK_normal,
+                          sx, sy, 250, 250);
+awp[mNwin]->SetAutoDelChilds(true);
+taw[mNwin] = typ;
+win = awp[mNwin];
+win->Show();
+mNwin++; 
+return(win);
+}
+
+
+
+/* ........................................................... */
+/*       Classe VisuTools  (Controle de visualisation)         */
+/* ........................................................... */
+
+VisuTools::VisuTools(PIDemoApp *par, PIContainer *pw)
+: PIContainer(pw, "visutools", 200, 160, 185, 5)
+{
+dap = par;
+mTlb = new PILabel(this, "VisuTools", 100, 25, 50, 5); 
+
+mOlb[0] = new PILabel(this, "AutoLut :", 70, 25, 5, 45);
+mPum[0] = new PIPUMenu((PIMsgHandler *)this, "AutoLut");
+mPum[0]->AppendItem("+3 Sig", 303);
+mPum[0]->AppendItem("+2 Sig", 302);
+mPum[0]->AppendItem("+1 Sig", 301);
+mPum[0]->AppendItem("-1 Sig", 299);
+mPum[0]->AppendItem("-2 Sig", 298);
+mOpt[0] = new PIOptMenu(this, mPum[0], 90, 25, 80, 45);
+mOpt[0]->SetValue(302);
+
+mOlb[1] = new PILabel(this, "Zoom :", 70, 25, 5, 85);
+mPum[1] = new PIPUMenu((PIMsgHandler *)this, "Zoom");
+mPum[1]->AppendItem("Agr. * 8", 108);
+mPum[1]->AppendItem("Agr. * 6", 106);
+mPum[1]->AppendItem("Agr. * 5", 105);
+mPum[1]->AppendItem("Agr. * 4", 104);
+mPum[1]->AppendItem("Agr. * 3", 103);
+mPum[1]->AppendItem("Agr. * 2", 102);
+mPum[1]->AppendItem("* 1", 101);
+mPum[1]->AppendItem("Red. / 2", 98);
+mPum[1]->AppendItem("Red. / 3", 97);
+mPum[1]->AppendItem("Red. / 4", 96);
+mPum[1]->AppendItem("Red. / 5", 95);
+mPum[1]->AppendItem("Red. / 6", 94);
+mPum[1]->AppendItem("Red. / 8", 92);
+mOpt[1] = new PIOptMenu(this, mPum[1], 90, 25, 80, 85);
+mOpt[1]->SetValue(101);
+
+mOlb[2] = new PILabel(this, "ColMap :", 70, 25, 5, 125);
+mPum[2] = new PIPUMenu((PIMsgHandler *)this, "ColMap");
+mPum[2]->AppendItem("Grey32", 201);
+mPum[2]->AppendItem("GreyInv32", 202);
+mPum[2]->AppendItem("ColRJ32", 203);
+mPum[2]->AppendItem("ColBR32", 204);
+mPum[2]->AppendItem("ColRV32", 205);
+mOpt[2] = new PIOptMenu(this, mPum[2], 90, 25, 80, 125);
+mOpt[2]->SetValue(203);
+
+FinishCreate();
+}
+
+
+/* --Methode-- */
+VisuTools::~VisuTools()
+{
+
+delete mTlb;
+for(int i=0; i<3; i++)
+  {
+  delete mOpt[i];
+  delete mPum[i];
+  delete mOlb[i];
+  }
+}
+
+/* --Methode-- */
+void VisuTools::Process(long msg, PIMsgHandler* /*sender*/, void* /*data*/)
+{
+CMapId cmap[5] = { CMAP_GREY32, CMAP_GREYINV32, CMAP_COLRJ32, 
+                   CMAP_COLBR32, CMAP_COLRV32 } ;
+
+if (dap->CurrentPIImage() == NULL)  return;
+dap->SetBusy();
+if ((msg < 110) && (msg > 90))  // Zoom
+  dap->CurrentPIImage()->SetZoom((int)(msg-100));
+else if ((msg > 200) && (msg < 206)) // Changement de couleur 
+  dap->CurrentPIImage()->SetColMap(cmap[msg-201]);
+else if ((msg > 290) && (msg < 310))
+  dap->CurrentPIImage()->SetLut( dap->CurrentImage()->minPix, dap->CurrentImage()->maxPix,
+                                 kLutType_Lin, msg-300);
+dap->SetReady();
+return;
+}
+
+
+/* ........................................................... */
+/*           Classe CutTools  (Controle de coupe)              */
+/* ........................................................... */
+
+/* --Methode-- */
+CutTools::CutTools(PIDemoApp *par, PIContainer *pw)
+: PIContainer(pw, "cuttools", 200, 160, 185, 5)
+{
+int i;
+
+dap = par;
+
+mOlb = new PILabel(this, "Coupe:", 70, 25, 5, 5);
+mPum = new PIPUMenu((PIMsgHandler *)this, "Coupe");
+mPum->AppendItem("H-V", 101);
+mPum->AppendItem("Diagonal", 102);
+mOpt = new PIOptMenu(this, mPum, 90, 25, 80, 5);
+mOpt->SetValue(101);
+
+mBut[0] = new PIButton(this, "Y-", 501, 30, 25, 85, 60);
+mBut[1] = new PIButton(this, "Y+", 502, 30, 25, 85, 120);
+mBut[2] = new PIButton(this, "X-", 503, 30, 25, 50, 90);
+mBut[3] = new PIButton(this, "X+", 504, 30, 25, 120, 90);
+
+fgpb = true;
+fgdiag = false;
+
+for(i=0; i<2; i++)
+  { mDrw[i] = NULL;  mPIH[i] = NULL; }
+ 
+FinishCreate();
+
+}
+
+
+/* --Methode-- */
+CutTools::~CutTools()
+{
+int i;
+
+for(i=0; i<4; i++)  delete mBut[i];
+delete mOpt;
+delete mPum;
+delete mOlb;
+}
+
+/* --Methode-- */
+void CutTools::Show()
+{
+PIWindow *win;
+
+PIContainer::Show();
+fgpb = true;
+
+if (dap->CurrentPIImage() == NULL) return;
+if (dap->CurrentImage() == NULL) return;
+
+mPx = dap->CurrentPIImage()->XPave();
+mPy = dap->CurrentPIImage()->YPave();
+if (rect_pav)
+  {
+  mDx = dap->CurrentPIImage()->XSzPave();
+  mDy = dap->CurrentPIImage()->YSzPave();
+  }
+else  { mDx = mDy = pav_sz; }
+if( mDx%2 == 0 ) mDx++; 
+if( mDy%2 == 0 ) mDy++;
+mDxy = ( mDx > mDy ) ? mDx : mDy;
+
+
+char strg[128];
+sprintf(strg, "Coupe Pave_%s" ,dap->CurrentPIImage()->Nom().c_str());
+
+if ((win=dap->GetWindow(600,330,12,strg)) == NULL)  
+  { fgpb=true;  return; }
+mLab = new PILabel(win, "infc", 590, 25, 5, 5);
+for(int i=0; i<2; i++)
+  {
+  mDrw[i] = new PIScDrawWdg(win, "Coupe", 300, 300, 300*i, 30);
+  mPIH[i] = NULL;
+  }  
+fgpb = false;
+FillHistos();
+return;
+}
+
+
+/* --Methode-- */
+void CutTools::Process(long msg, PIMsgHandler* /*sender*/, void* /*data*/)
+{
+if ((fgpb) || (dap->CurrentPIImage() == NULL))
+  {  printf("CutTools::Process, Msg= %d , --PB--\n", (int)msg);
+  return; }
+
+
+switch(msg)
+  {
+  case 101:
+    if (fgdiag)
+      { fgdiag = false;   FillHistos(); }
+    break;
+
+  case 102:
+    if (!fgdiag)
+      { fgdiag = true;   FillHistos(); }
+    break;
+
+  case 501:
+  case 502:
+  case 503:
+  case 504:
+    switch(msg)
+      {
+      case 501:
+        mPy--;
+        break;
+      case 502:
+        mPy++;
+        break;
+      case 503:
+        mPx--;
+        break;
+      case 504:
+        mPx++;
+        break;
+      }
+    dap->CurrentPIImage()->SetPave(mPx, mPy);  
+    mPx = dap->CurrentPIImage()->XPave();
+    mPy = dap->CurrentPIImage()->YPave();
+    FillHistos();
+    break;
+
+  default:
+    printf("CutTools::Process/BUG ??? Msg = %d \n", (int)msg);
+    break;
+  }
+return;
+}
+
+/* --Methode-- */
+void CutTools::FillHistos()
+{
+int i;
+int xp,yp;
+int dx,dy,dxy;
+Histo * his[2];
+float xmin,xmax;
+int ip,jp,imax,jmax;
+ImageR4 *myimg;
+char buff[256];
+char *txt[2] = {"H-V ","Diag"};
+
+xp = mPx; yp = mPy;
+dx = mDx; dy = mDy;  dxy = mDxy;
+
+myimg = dap->CurrentImage();
+
+/*  Recherche du pixel maxi  */
+imax = xp;
+jmax = yp;
+xmax = (float) (*myimg)(imax,jmax);
+for(i=0;i<dx;i++) {
+  ip = xp -(dx-1)/2 + i;
+  if( ip < 0 || ip >= myimg->XSize() ) continue;
+  for(int j=0;j<dy;j++) {
+    jp = yp -(dy-1)/2 + j;
+    if( jp < 0 || jp >= myimg->YSize() ) continue;
+    if( (float) (*myimg)(ip,jp) < xmax ) continue;
+    imax = ip;
+    jmax = jp;
+    xmax = (float) (*myimg)(imax,jmax);
+  }
+}
+
+i = (fgdiag) ? 1 : 0;
+sprintf(buff,"Coupe %s en (%d %d) PixMax= %g en (%d %d)", 
+        txt[i], xp, yp, xmax, imax, jmax);
+mLab->SetLabel((string)buff);
+
+for(i=0; i<2; i++)  delete mPIH[i];
+
+if (!fgdiag)   // Histos H-V
+  {
+  xmin = (float) (xp - (dx-1)/2) - 0.5;
+  xmax = (float) (xp + (dx-1)/2) + 0.5;
+  his[0] = new Histo(xmin,xmax,dx);
+  xmin = (float) (yp - (dy-1)/2) - 0.5;
+  xmax = (float) (yp + (dy-1)/2) + 0.5;
+  his[1] = new Histo(xmin,xmax,dy);
+
+  for(i=0;i<dx;i++) {
+    ip = xp -(dx-1)/2 + i;
+    if( ip < 0 || ip >= myimg->XSize() ) continue;
+    his[0]->Add((float) ip,(float) (*myimg)(ip,yp));
+    }
+  for(i=0;i<dy;i++) {
+    ip = yp -(dy-1)/2 + i;
+    if( ip < 0 || ip >= myimg->YSize() ) continue;
+    his[1]->Add((float) ip,(float) (*myimg)(xp,ip));
+    }  
+  }
+else    // Histo diagonals....
+  {
+  xmin = (float) -(dxy-1)/2 - 0.5;
+  xmax = (float)  (dxy-1)/2 + 0.5;
+  his[0] = new Histo(xmin,xmax,dxy);
+  his[1] = new Histo(xmin,xmax,dxy);
+  for(i=0;i<dxy;i++) {
+    ip = xp -(dxy-1)/2 + i;
+    jp = yp -(dxy-1)/2 + i;
+    if( ip < 0 || ip >= myimg->XSize() ) continue;
+    if( jp >= 0 && jp < myimg->YSize() )
+           his[0]->Add((float) ip-xp,(float) (*myimg)(ip,jp));
+    jp = yp +(dxy-1)/2 - i;
+    if( jp >= 0 && jp < myimg->YSize() )
+           his[1]->Add((float) ip-xp,(float) (*myimg)(ip,jp));
+    }
+  
+  }
+for(i=0; i<2; i++)
+  {
+  mPIH[i] = new PIHisto(his[i]);
+  mPIH[i]->SetAutoDelHisto(true);
+  mDrw[i]->AddDrawer(mPIH[i]);
+  mDrw[i]->SetTextWin(dap->DrawLabelWdg());
+  mPIH[i]->Refresh();
+  }
+
+return;
 }
 
@@ -529,6 +1159,4 @@
 /*       Classe LutWind  (Changement de Lut d'image)           */
 /* ........................................................... */
-
-
 
 /* --Methode-- */
@@ -581,4 +1209,5 @@
   delete mOpt[i];
   delete mPum[i];
+  delete mOlb[i];
   }
 }
@@ -597,5 +1226,6 @@
   mOpt[0]->SetValue(1100);
 else mOpt[0]->SetValue(1101);
-mOpt[1]->SetValueStr("NoAuto");
+string s("NoAuto");
+mOpt[1]->SetValueStr(s);
 dap->SetBlocked();
 PIWindow::Show();
@@ -605,5 +1235,5 @@
 
 /* --Methode-- */
-void LutWind::Process(long msg, PIMsgHandler* sender, void* data)
+void LutWind::Process(long msg, PIMsgHandler* /*sender*/, void* /*data*/)
 {
 float min, max;
@@ -677,7 +1307,16 @@
 
 /* --Methode-- */
-void UserProcWind::Process(long msg, PIMsgHandler* sender, void* data)
-{
-FitsImageR4 *mimg, *oimg;
+void UserProcWind::Show()
+{
+dap->SetBlocked();
+PIWindow::Show();
+return;
+}
+
+
+/* --Methode-- */
+void UserProcWind::Process(long msg, PIMsgHandler* /*sender*/, void* /*data*/)
+{
+ImageR4 *mimg, *oimg;
 int xp, yp, dx, dy;
 
@@ -685,9 +1324,13 @@
   {
   case 2600:
+    this->Hide();
     dap->SetReady();
-    this->Hide();
     break;
 
   case 2500:
+    if ((mUpr < 1) || (mUpr > 3))  mUpr=1;
+    if (userFunc[mUpr-1] == NULL)
+      { printf("UserProcWind::Process()/Warning: pas de UserProcFunc%d \n", mUpr);
+      this->Hide(); dap->SetReady(); return; }
     mimg = dap->CurrentImage();
     if (mimg == NULL)  return;
@@ -696,6 +1339,6 @@
     dx = (dap->CurrentPIImage())->XSzPave();
     dy = (dap->CurrentPIImage())->YSzPave();
+    this->Hide();
     dap->SetBusy();
-    this->Hide();
     oimg = NULL;
     switch (mUpr)
@@ -708,17 +1351,4 @@
                             (char *) (mText[2]->GetText().c_str()));
         break;
-/*
-      case 2 :
-        oimg = PIUserProc_2(mimg, xp, yp, dx, dy,
-                            (char *) (mText[1]->GetText().c_str()), 
-                            (char *) (mText[2]->GetText().c_str()));
-        break;
-
-      case 3 :
-        oimg = PIUserProc_3(mimg, xp, yp, dx, dy,
-                            (char *) (mText[1]->GetText().c_str()), 
-                            (char *) (mText[2]->GetText().c_str()));
-        break;
-*/
       }    
     if (oimg)  dap->AddImage(oimg,mText[0]->GetText());
@@ -732,28 +1362,247 @@
 // Le main  ....
 
+/* ........................................................... */
+/*    Classe OptionWind  Options et valeurs de parametres      */
+/* ........................................................... */
+
+
+/* --Methode-- */
+OptionWind::OptionWind(PIDemoApp *par)
+: PIWindow((PIMsgHandler *)par, "Options (PIDemo)", PIWK_dialog, 
+           360, 550, 150, 150)
+{
+dap = par;
+
+mLab[0] = new PILabel(this, "PaveSz", 60, 30, 20, 60); 
+
+mLab[1] = new PILabel(this, "MinADU", 60, 30, 20, 190); 
+mLab[2] = new PILabel(this, "MaxADU", 60, 30, 185, 190); 
+mLab[3] = new PILabel(this, "Gain", 60, 30, 20, 230); 
+mLab[4] = new PILabel(this, "RONoise", 60, 30, 185, 230); 
+
+mLab[5] = new PILabel(this, "NBinHis", 60, 30, 20, 320); 
+mLab[6] = new PILabel(this, "MinHis", 60, 30, 20, 360); 
+mLab[7] = new PILabel(this, "MaxHis", 60, 30, 185, 360); 
+
+mText[0] = new PIText(this, "PaveSz", 90, 30, 85, 60);
+mText[0]->SetText("15");
+mText[1] = new PIText(this, "MinADU", 90, 30, 85, 190);
+mText[1]->SetText("1.");
+mText[2] = new PIText(this, "MaxADU", 90, 30, 250, 190);
+mText[2]->SetText("32000.");
+mText[3] = new PIText(this, "Gain", 90, 30, 85, 230);
+mText[3]->SetText("3.");
+mText[4] = new PIText(this, "RONoise", 90, 30, 250, 230);
+mText[4]->SetText("10.");
+mText[5] = new PIText(this, "NBin", 90, 30, 85, 320);
+mText[5]->SetText("200");
+mText[6] = new PIText(this, "MinHis", 90, 30, 85, 360);
+mText[6]->SetText("0.");
+mText[7] = new PIText(this, "MaxHis", 90, 30, 250, 360);
+mText[7]->SetText("2000.");
+
+mTlb[0] = new PILabel(this, "--- Fit/Coupe/PrintPave ---", 160, 25, 100, 20);
+mTlb[1] = new PILabel(this, "--- DynCCD ---", 160, 25, 100, 150);
+mTlb[2] = new PILabel(this, "--- Histogramme ---", 160, 25, 100, 280);
+mTlb[3] = new PILabel(this, "--- Visualisation  ---", 160, 25, 100, 410);
+
+mOlb[0] = new PILabel(this, "TypePSF", 60, 25, 20, 100); 
+mPum[0] = new PIPUMenu((PIMsgHandler *)this, "TypPSF");
+mPum[0]->AppendItem("Gaussienne", 3101);
+mPum[0]->AppendItem("Gauss.Int.", 3102);
+mPum[0]->AppendItem("DL-Gauss.", 3103);
+mPum[0]->AppendItem("DL-GauInt", 3104);
+mOpt[0] = new PIOptMenu(this, mPum[0], 90, 25, 85, 100);
+mOpt[0]->SetValue(3101);
+
+mOlb[1] = new PILabel(this, "Def.Zoom", 60, 25, 20, 450); 
+mPum[1] = NULL;
+mOpt[1] = new PIOptMenu(this, dap->VisTC()->ZoomOptMenu(), 90, 25, 85, 450);
+mOpt[1]->SetValue(101);
+
+mCkb[0] = new PICheckBox(this,"ou Rect.Sel.",3001,140, 30, 200, 60); 
+mCkb[1] = new PICheckBox(this,"PrintPave-PixVal",3002,140, 30, 200, 100); 
+mCkb[2] = new PICheckBox(this,"AutoMin/Max",3003,140, 30, 200, 320); 
+mCkb[3] = new PICheckBox(this,"ShowCursor",3004,140, 30, 200, 450); 
+for(int i=0; i<4; i++) mCkv[i] = true;
+
+mBut[0] = new PIButton(this, "Apply", 3500, 70, 30, 90, 500);
+mBut[1] = new PIButton(this, "Cancel", 3600, 70, 30, 200, 500);
+
+FinishCreate();
+}
+
+/* --Methode-- */
+OptionWind::~OptionWind()
+{
+int i;
+for(i=0; i<2; i++)
+  {
+  delete mBut[i];
+  delete mOpt[i];
+  delete mPum[i];
+  delete mOlb[i];
+  }
+for(i=0; i<8; i++)
+  {
+  delete mLab[i];
+  delete mText[i];
+  }
+for(i=0; i<4; i++)  
+  {
+  delete mTlb[i];
+  delete mCkb[i];
+  }
+}
+
+/* --Methode-- */
+void OptionWind::Show()
+{
+char buff[64];
+
+dap->SetBlocked();
+sprintf(buff,"%d", pav_sz);
+mText[0]->SetText(buff);
+sprintf(buff,"%g", mdynccd->MinADU);
+mText[1]->SetText(buff);
+sprintf(buff,"%g", mdynccd->MaxADU);
+mText[2]->SetText(buff);
+sprintf(buff,"%g", mdynccd->Gain);
+mText[3]->SetText(buff);
+sprintf(buff,"%g", mdynccd->RONoise);
+mText[4]->SetText(buff);
+sprintf(buff,"%d", his_nbin);
+mText[5]->SetText(buff);
+sprintf(buff,"%g", his_min);
+mText[6]->SetText(buff);
+sprintf(buff,"%g", his_max);
+mText[7]->SetText(buff);
+
+mOpt[0]->SetValue(3100+typ_psf);
+mOpt[1]->SetValue(100+vis_zoom);
+mCkv[0] = rect_pav;
+mCkv[1] = prt_pval;
+mCkv[2] = his_ckd;
+mCkv[3] = vis_cursshow;
+for(int i=0; i<4; i++)
+  mCkb[i]->SetState(mCkv[i]);
+
+PIWindow::Show();
+return;
+}
+
+
+/* --Methode-- */
+void OptionWind::Process(long msg, PIMsgHandler* /*sender*/, void* data)
+{
+
+switch (msg)
+  {
+  case 3001:
+  case 3002:
+  case 3003:
+  case 3004:
+    if (data) mCkv[msg-3001] = true;   // J'ai un pb avec les CheckBox->GetState()  Reza ??
+    else mCkv[msg-3001] = false;
+    break;
+
+  case 3600:
+    dap->SetReady();
+    this->Hide();
+    break;
+
+  case 3500:
+    pav_sz = atoi(mText[0]->GetText().c_str());
+    mdynccd->MinADU = atof(mText[1]->GetText().c_str());
+    mdynccd->MaxADU = atof(mText[2]->GetText().c_str());
+    mdynccd->Gain = atof(mText[3]->GetText().c_str());
+    mdynccd->RONoise = atof(mText[4]->GetText().c_str());
+    his_nbin = atoi(mText[5]->GetText().c_str());
+    his_min = atof(mText[6]->GetText().c_str());
+    his_max = atof(mText[7]->GetText().c_str());
+
+    typ_psf = mOpt[0]->GetValue() - 3100;
+    vis_zoom = mOpt[1]->GetValue() - 100;
+
+/*
+  Je ne sais pas pourquoi ca ne marche pas comme ca Reza $CHECK$ ??? 
+    rect_pav = mCkb[0]->GetState();
+    prt_pval = mCkb[1]->GetState();
+    his_ckd = mCkb[2]->GetState();
+    vis_cursshow = mCkb[3]->GetState();
+*/
+    rect_pav = mCkv[0];
+    prt_pval = mCkv[1];
+    his_ckd = mCkv[2];
+    vis_cursshow = mCkv[3];
+/*
+    printf("\n-- %d %d %d %d -- \n", (int)rect_pav, (int)prt_pval, (int)his_ckd,
+           (int)vis_cursshow);
+    printf("OptionWind::Process(OK) : %d %d (%d) %d %g %g - %d \n",
+            typ_psf, pav_sz, (int)rect_pav, his_nbin, his_min, his_max, vis_zoom);
+    for(int i=0; i<4; i++)
+       printf("ckb[%d] = %d (%d) - ", i, (int)mCkb[i]->GetState(),mCkv[i]); 
+    puts(" ... ");        
+    mdynccd->Print();
+*/
+    dap->SetReady();
+    this->Hide();
+
+    break;
+  default:
+//    printf("OptionWind::Process Msg %d received data= %ld\n", (int)msg, (long)data);
+    break;
+  }
+
+}
+
+
 int main(int narg, char *arg[])
 {
 
 char *path;
-int rc;
+int i, rc, ofa;
+char strg[512];
 
 PeidaStarRecoInitiator InitStarReco;
-  
-if (narg > 1)  path = arg[1];
-else path = NULL;
-
-if (narg > 2)  rc = DynLink(arg[2]);
-else rc = DynLink("piup.so");
-if (rc != 0)
-  {
-  printf("PIApp:: Erreur d'ouverture UserProcFunc Shared-Object ... \n");
-  exit(0);
-  }
-
-PIDemoApp app(path);
-
-app.Run();
-
+
+ofa = 1;   path = NULL; 
+if (narg > 1)
+  {
+  if (strcmp(arg[1],"-h") == 0) 
+    {
+    puts("\n piapp: Programme d analyse interactive EROS-2 (Version demo)");
+    puts("Usage: piapp [-p ImagePath] [img1 img2 ...]");
+    exit(0);
+    }
+
+  if (strcmp(arg[1],"-p") == 0)
+    {
+    if (narg == 2)  
+      { puts("piapp_Erreur: Pb. d argument (piapp -h) ");  exit(1); }
+    else { path = arg[2];  ofa = 3; }
+    }
+  }
+
+
+mdynccd = new DynCCD(1, 1., 32000., 3., 10.);
+PIDemoApp * app = new PIDemoApp(path);
+app->SetBusy();
+rc = DynLink(false);
+for(i=ofa; i<narg; i++)
+  {
+  if (path)  sprintf(strg,"%s/%s", path, arg[i]);
+  else strcpy(strg, arg[i]);
+  string s(strg);
+  app->OpenImage(s);
+  }
+puts("appel de Application.Run() ... ");
+app->SetReady();
+app->Run();
+
+delete app;
 dlclose(dlhandle);
 exit(0);
 }
+
+
Index: /trunk/SophyaPI/PI/pidemo.h
===================================================================
--- /trunk/SophyaPI/PI/pidemo.h	(revision 17)
+++ /trunk/SophyaPI/PI/pidemo.h	(revision 18)
@@ -16,8 +16,11 @@
 
 
-#define NMXIMG 15   // Nb maxi d'images ouvertes simultanement 
+#define NMXWIN 25   // Nb maxi de fenetres ouvertes simultanement 
 
+class VisuTools;
+class CutTools;
 class LutWind;
 class UserProcWind;
+class OptionWind;
 
 class PIDemoApp : public PIApplication {
@@ -28,40 +31,106 @@
      virtual void   Process(long msg, PIMsgHandler* sender, void* data=NULL);
 
-     void SetBusy();
-     void SetReady();
-     void SetBlocked();
-     void AddImage(FitsImageR4 * nouv, string const & name);
+     void AddImage(ImageR4 * nouv, string const & name, int zm=0, int typ=3);
+     void AddHisto(Histo *h);
      void OpenImage(string const & flnm);
      void CloseCurrent();
-     void SaveCurrent(string const & flnm);
-     int GetNumCurrent();
-     inline PIImage * CurrentPIImage() { return (mCur); }
-     FitsImageR4 * CurrentImage();
-     PIWindow * CurrentImgWin();
+     void SaveCurrentImg(string const & flnm);
+     inline PIImage * CurrentPIImage() { return (mCurPII); }
+     ImageR4 * CurrentImage();
+     PIWindow * CurrentWindow() { return (mCurWin); };
+     int  GetNumCurrent();
+     int  GetCurrentWType();
+
+     PIWindow * GetWindow(int sx, int sy, int typ, char * nom);
+
+     inline VisuTools * VisTC()  { return mVisTC; } 
+     inline CutTools *  CutTC()  { return mCutTC; } 
+
+     inline PILabel * ImagLabelWdg() { return labimg; }
+     inline PILabel * DrawLabelWdg() { return labdrw; }
 
 private:
-    PIPDMenu  *m[3];
-    PIPUMenu *pum[3];
-    PIOptMenu * opm[3];
-    PILabel *olb[3];
+    void MBProcess1(long msg, PIMsgHandler* sender, void* data=NULL);
+    void MBProcess2(long msg, PIMsgHandler* sender, void* data=NULL);
+    void MBProcess3(long msg, PIMsgHandler* sender, void* data=NULL);
+    void MBProcess4(long msg, PIMsgHandler* sender, void* data=NULL);
+    bool FitPSF_Res(ImageR4 *pim);
+
+    PIPDMenu  *m[5];
     PIButton * but;
     PIPixmap * zoom;
-    PILabel  * labimg, *lab;
+    PILabel  * labimg, *labdrw;
+    PILabel  *lab, * mTlb[2];
     PIFileChooser * pfc;
 
-    PIDrawWindow *  draw;
-    PIHisto    *    piHisto;
-    Histo*          pixelHisto;
-    PIWindow * wpii[NMXIMG];
-    PIImage *  pii[NMXIMG];
-    FitsImageR4 * img[NMXIMG];
-    int mNimg;
-    PIImage * mCur;
-    int mState;
+    VisuTools *mVisTC;
+    CutTools *mCutTC;
+
+    int mTCId;
+
+    PIWindow * awp[NMXWIN];
+    int taw[NMXWIN];
+    int mNwin,mTnw;
+    PIWindow * mCurWin;
+    PIImage * mCurPII;
 
     LutWind * mLutw;
     UserProcWind * mUP;
-    
+    OptionWind * mOptw;
 };
+
+/* --------------------------------------------------------------------------- */
+
+class VisuTools : public PIContainer {
+public :
+  VisuTools(PIDemoApp *par, PIContainer *pw);
+  ~VisuTools();
+  virtual void  Process(long msg, PIMsgHandler* sender, void* data=NULL);
+
+  PIPUMenu * ZoomOptMenu() { return mPum[1]; }
+  PIPUMenu * LutOptMenu() { return mPum[0]; }
+  PIPUMenu * ColMapOptMenu() { return mPum[2]; }
+
+private:
+  PIDemoApp * dap;
+  PILabel * mTlb;
+  PIPUMenu * mPum[3];
+  PIOptMenu * mOpt[3];
+  PILabel * mOlb[3];
+  
+};
+
+/* --------------------------------------------------------------------------- */
+
+class CutTools : public PIContainer {
+public :
+  CutTools(PIDemoApp *par, PIContainer *pw);
+  ~CutTools();
+  virtual void  Show();
+  virtual void  Process(long msg, PIMsgHandler* sender, void* data=NULL);
+
+private:
+  void FillHistos();
+
+  PIDemoApp * dap;
+  PIButton * mBut[4];
+  PIPUMenu * mPum;
+  PIOptMenu * mOpt;
+  PILabel * mOlb;
+  
+  int mDx, mDy, mDxy;
+  int mPx, mPy;
+
+  PIHisto * mPIH[2];
+  PIScDrawWdg * mDrw[2]; 
+  PILabel * mLab;
+
+  bool fgpb;
+  bool fgdiag;
+  
+};
+
+
+/* --------------------------------------------------------------------------- */
 
 class LutWind : public PIWindow {
@@ -83,8 +152,11 @@
 };
 
+/* --------------------------------------------------------------------------- */
+
 class UserProcWind : public PIWindow {
 public :
                 UserProcWind(PIDemoApp *par);
                 ~UserProcWind();
+  virtual void  Show();
   virtual void  Process(long msg, PIMsgHandler* sender, void* data=NULL);
   void          SelectUP(int n) { mUpr = n; };
@@ -99,2 +171,25 @@
 };
 
+
+/* --------------------------------------------------------------------------- */
+
+class OptionWind : public PIWindow {
+public :
+  OptionWind(PIDemoApp *par);
+  ~OptionWind();
+  virtual void  Show();
+  virtual void  Process(long msg, PIMsgHandler* sender, void* data=NULL);
+
+private:
+  PIDemoApp * dap;
+  PILabel * mLab[8];
+  PIButton * mBut[2];
+  PIText * mText[8];
+  PIPUMenu * mPum[2];
+  PIOptMenu * mOpt[2];
+  PILabel * mOlb[2];
+  PILabel * mTlb[4];
+  PICheckBox * mCkb[4];
+  bool mCkv[4];
+  
+};
Index: /trunk/SophyaPI/PI/pidrawwin.cc
===================================================================
--- /trunk/SophyaPI/PI/pidrawwin.cc	(revision 17)
+++ /trunk/SophyaPI/PI/pidrawwin.cc	(revision 18)
@@ -5,5 +5,5 @@
 :PIWindow(par, nom, k, sx, sy, px, py)
 {
-   mDrawWdg = new PIScDrawWdg(this, "", sx-20, sy-20, 10, 10);
+   mDrawWdg = new PIScDrawWdg(this, nom, sx-20, sy-20, 10, 10);
    mDrawWdg->SetBinding(true, true, true, true);   
 }
Index: /trunk/SophyaPI/PI/pidrawwin.h
===================================================================
--- /trunk/SophyaPI/PI/pidrawwin.h	(revision 17)
+++ /trunk/SophyaPI/PI/pidrawwin.h	(revision 18)
@@ -17,4 +17,5 @@
  virtual void    AddDrawer(PIScDrawer*);                
  virtual void    SetAxesFlags(int flags=-1);
+ inline PIScDrawWdg * DrawWdg() { return mDrawWdg; }
 private:
   PIScDrawWdg*   mDrawWdg;
Index: /trunk/SophyaPI/PI/pifilechox.cc
===================================================================
--- /trunk/SophyaPI/PI/pifilechox.cc	(revision 17)
+++ /trunk/SophyaPI/PI/pifilechox.cc	(revision 18)
@@ -21,6 +21,6 @@
 pif = (PIFileChooserX *) usd ;
 pif->mSt = 1;
+pif->MWin()->Unmanage();
 pif->Send(pif->Msg(), (void *) (pif->GetFileName().c_str()));
-pif->MWin()->Unmanage();
 return;
 }
@@ -32,6 +32,6 @@
 pif = (PIFileChooserX *) usd ;
 pif->mSt = 0;
+pif->MWin()->Unmanage();
 pif->Send(pif->Msg(), NULL);
-pif->MWin()->Unmanage();
 return;
 }
Index: /trunk/SophyaPI/PI/pihisto.cc
===================================================================
--- /trunk/SophyaPI/PI/pihisto.cc	(revision 17)
+++ /trunk/SophyaPI/PI/pihisto.cc	(revision 18)
@@ -5,9 +5,19 @@
 : PIScDrawer(), mHisto(histo)
 {
+  ahisup = false;     // Flag pour suppression automatique de mHisto
 }
 
 PIHisto::~PIHisto()
 {
+  if (ahisup)
+    delete mHisto;
 }
+
+void
+PIHisto::SetAutoDelHisto(bool ad)
+{
+  ahisup = ad;
+}
+
 
 void
Index: /trunk/SophyaPI/PI/pihisto.h
===================================================================
--- /trunk/SophyaPI/PI/pihisto.h	(revision 17)
+++ /trunk/SophyaPI/PI/pihisto.h	(revision 18)
@@ -13,6 +13,9 @@
   virtual void       DrawStats();
   virtual void       Refresh();  // Recalcule les limites, et reaffiche
+  void               SetAutoDelHisto(bool ad=false);
 protected:
   Histo* mHisto;
+  bool   ahisup;
+
   virtual void       AttachTo(PIScDrawWdg*);
   virtual void       UpdateSize();
Index: /trunk/SophyaPI/PI/piimage.cc
===================================================================
--- /trunk/SophyaPI/PI/piimage.cc	(revision 17)
+++ /trunk/SophyaPI/PI/piimage.cc	(revision 18)
@@ -21,4 +21,7 @@
 
 img = NULL;
+aisup = false;
+curshow = true;
+winovis = true;     // $CHECK$ Reza A enlever si possible 17/6/96
 zow = NULL; 
 txw = NULL;
@@ -32,4 +35,5 @@
 xmv0 = ymv0 = -1;
 dxmv = dymv = 0;
+xcurs = ycurs = -1;
 zoom = 1;
 
@@ -46,5 +50,4 @@
 ActivateMove(3);
 
-
 }
 
@@ -55,4 +58,5 @@
 if (zpixm) delete zpixm;
 if (lut) delete lut;
+if (aisup)  delete img;   // Suppression automatique d'image
 }
 
@@ -68,5 +72,5 @@
   {
 //  SetSize(img->XSize(), img->YSize());
-  SetPave();
+  CenterPave();
   if (img->minPix>img->maxPix) img->CheckDyn();
   SetLut(img->minPix, img->maxPix, kLutType_Lin, 1, refr);
@@ -88,5 +92,20 @@
 }
 
-
+/* --Methode-- */
+void PIImage::SetAutoDelImage(bool ad)
+{
+aisup = ad;
+return;
+}
+
+/* --Methode-- */
+void PIImage::ShowCursor(bool sc)
+{
+if (curshow == sc) return;
+curshow = sc;
+if (curshow)  DrawCursor();
+else DrawCursor(-1, -1);
+return;
+}
 
 /* --Methode-- */
@@ -94,19 +113,31 @@
 {
 if (lut) delete(lut);
-if (lauto)
+if (lauto && img)
   {
   float fracnul, fracsat;
   float fnd, sigfnd;
-
+  float minpix = min;  
+  float maxpix = max; 
   int npix = img->XSize()*img->YSize();
-  int vitesse=npix/20000;
-  vitesse = (vitesse < 1) ? 1 : ((vitesse > 10) ? 10 : vitesse );
-  int nbsig = lauto;  
-  float fracmax = 0.95;
-  float minpix = min;  
-  float maxpix = max;  
-
-  RzFondSig2Ciel(img, vitesse, fracmax, nbsig, 500, &minpix, &maxpix, 
+  int rc = -1;
+  if (npix > 2500)  // Ca ne marche pas quand il n'y a pas assez de pixels ...
+    {  
+    int vitesse=npix/20000;
+    vitesse = (vitesse < 1) ? 1 : ((vitesse > 25) ? 25 : vitesse );
+    int nbin=npix/10;
+    nbin = (nbin < 1) ? 1 : ((nbin > 500) ? 500 : nbin );
+    int nbsig = lauto;  
+    float fracmax = 0.95;
+    rc = RzFondSig2Ciel(img, vitesse, fracmax, nbsig, nbin, &minpix, &maxpix, 
                  &fnd, &sigfnd, &min, &max, &fracnul, &fracsat);
+    }
+  if (rc != 0)
+    {
+    img->CheckDyn(minpix, maxpix);
+    min = img->moyPix-3.*img->sigPix;
+    max = img->moyPix+3.*img->sigPix;
+    if (min < minpix)  min = minpix;
+    if (max > maxpix)  max = maxpix;
+    }
   }
 
@@ -129,4 +160,15 @@
 }
 
+/* --Methode-- */
+void PIImage::SetZoom(float fzm, bool refr)
+{
+int zm;
+
+if (fzm < 0.01) return;
+if (fzm < 1.)  zm = -(1./fzm);
+else zm = (int)(fzm+0.25);
+SetZoom(zm, refr);
+return;
+}
 
 /* --Methode-- */
@@ -137,8 +179,17 @@
 else if (zm < -10) zoom = -10;
 else zoom = zm;
-SetOffset();
-if (refr) Apply();
-return;
-}
+if (img)
+  { SetOffsetFromPave();
+  if (refr) Apply(); }
+return;
+}
+
+/* --Methode-- */
+float PIImage::GetZoom()
+{
+if (zoom >= 1)  return((float)zoom);
+else return(-1./(float)zoom);
+}
+
 
 /* --Methode-- */
@@ -151,7 +202,33 @@
 if (oy >= (img->YSize()-1))  oy = img->YSize()-2;
 offx = ox;  offy = oy;
+CenterPave();
 if (refr) Apply();
 return;
 }
+
+
+/* --Methode-- */
+void PIImage::SetPave(int x, int y, bool refr)
+{
+int xc, yc;
+
+if (img == NULL)  
+  { offx = offy = 0; 
+  xpav = ypav = 0; 
+  return; }
+
+if ( (x >= (img->XSize()-1)) || ( x < 0)  ||
+     (y >= (img->YSize()-1)) || ( y < 0)  ) return;
+
+xpav = x;  ypav = y;
+if (PosImg2W(xpav,ypav,&xc, &yc))  // Il faut recenter l'image
+  { SetOffsetFromPave();  
+  if (refr) Apply(true);  }
+else if (refr)
+  { Apply(false);  if (curshow)  DrawCursor();  }
+
+return;
+}
+
 
 /* --Methode-- */
@@ -167,5 +244,10 @@
 void PIImage::Apply(bool mw)
 {
-if (mw) ComputePixmap();
+if (img == NULL)  return;
+if (mw)
+  { 
+  ComputePixmap();
+  Send(PIMsg_ActiveWdg);
+  }
 if (zow != NULL) 
   {
@@ -180,5 +262,5 @@
   txw->SetLabel((string)buff);
   }
-Send(PIMsg_ActiveWindow, (char *)this);
+
 return;
 }
@@ -189,5 +271,5 @@
 // printf("PIImage::Resize\n");
 PIPixmap::Resize();
-SetPave();
+CenterPave();
 Apply();
 return;
@@ -195,8 +277,24 @@
 
 /* --Methode-- */
+void PIImage::Draw(int x0, int y0, int dx, int dy)
+{
+PIPixmap::Draw(x0, y0, dx, dy);
+winovis = false;    // $CHECK$ Reza A enlever si possible 17/6/96
+if (curshow)
+  {
+  int xc,yc;
+  PosImg2W(xpav, ypav, &xc, &yc);
+  if ((xc >= x0) && (xc < x0+dx) && 
+      (yc >= y0) && (yc < y0+dy) )  DrawCursor(xc, yc);
+  }
+return;
+}
+
+/* --Methode-- */
 void PIImage::But1Press(int x, int y)
 {
 int xp, yp;
 
+Send(PIMsg_ActiveWdg);
 if (img == NULL)  return;
 if ( PosW2Img(x, y, &xp, &yp) )  return;
@@ -204,4 +302,5 @@
 xpav = xp;  ypav = yp;
 Apply(false);
+if (curshow)  DrawCursor(-1,-1);
 return;
 }
@@ -224,4 +323,5 @@
 {
 SelPointerShape(PI_ArrowPointer);
+if (curshow)  DrawCursor();
 return;
 }
@@ -233,11 +333,14 @@
 int xp, yp;
 
+Send(PIMsg_ActiveWdg);
 if (img == NULL)  return;
 if ( PosW2Img(x, y, &xp, &yp) )  { xmv0 = -1;  return; }
 xmv0 = x;  ymv0 = y;  dxmv = dymv = 0;
-Send(PIMsg_ActiveWindow, (char *)this);
+if (curshow)  DrawCursor(-1,-1);
 SelGOMode(PI_GOXOR);
+if (GetColMapId() == CMAP_COLRJ32)
 SelForeground(PI_Yellow);
-SelLine(PI_ThinLine);
+else SelForeground(PI_Magenta);
+SelLine(PI_NormalLine);
 return;
 }
@@ -261,5 +364,4 @@
 int xp, yp, xp2, yp2;
 
-if (img == NULL)  return;
 if (img == NULL)  return;
 if (xmv0 < 0)  return;
@@ -276,4 +378,5 @@
 
 Apply(false);
+if (curshow)  DrawCursor();
 return;
 }
@@ -285,10 +388,11 @@
 int xp, yp;
 
+Send(PIMsg_ActiveWdg);
 if (img == NULL)  return;
 if ( PosW2Img(x, y, &xp, &yp) )  { xmv0 = -1;  return; }
+if (curshow)  DrawCursor(-1,-1);
 SelPointerShape(PI_TDLRArrowPointer);
 xmv0 = xp;
 ymv0 = yp;
-Send(PIMsg_ActiveWindow, (char *)this);
 return;
 }
@@ -476,4 +580,38 @@
 }
 
+/* --Methode-- */
+void PIImage::DrawCursor() 
+{
+int xc, yc;
+if (PosImg2W(xpav, ypav, &xc, &yc)) DrawCursor(-1, -1);
+else DrawCursor(xc, yc);
+return;
+}
+
+#define CURSHSIZE  25
+ 
+/* --Methode-- */
+void PIImage::DrawCursor(int xc, int yc)
+{
+
+if ((xcurs>=0) && (ycurs>=0))
+  PIPixmap::Draw(xcurs-CURSHSIZE, ycurs-CURSHSIZE, CURSHSIZE*2, CURSHSIZE*2); 
+
+if ((xc >= 0) && (yc >= 0)) 
+  {
+  xcurs = xc;   ycurs = yc;
+  if (winovis)  return;      // $CHECK$ Reza A enlever si possible 17/6/96
+  if (zoom < 3)  SelLine(PI_NormalLine);
+  else SelLine(PI_ThickLine);
+  SelGOMode(PI_GOCopy);
+  DrawLine(xcurs-CURSHSIZE, ycurs, xcurs-4, ycurs);
+  DrawLine(xcurs+4, ycurs, xcurs+CURSHSIZE, ycurs);
+  DrawLine(xcurs, ycurs-CURSHSIZE, xcurs, ycurs-4);
+  DrawLine(xcurs, ycurs+4, xcurs, ycurs+CURSHSIZE);
+  }
+else { xcurs = ycurs = -1; }
+
+return;
+}
 
 /* --Methode-- */
@@ -498,7 +636,28 @@
 
 
-
-/* --Methode-- */
-void PIImage::SetOffset()
+/* --Methode-- */
+int PIImage::PosImg2W(int xp, int yp, int * xiw, int * yiw)
+{
+if (zoom > 0)
+  {
+  *xiw = (xp-offx)*zoom+(zoom/2);
+  *yiw = (yp-offy)*zoom+(zoom/2);
+  }
+else
+  {
+  *xiw = (offx-xp)/zoom;
+  *yiw = (offy-yp)/zoom;
+  }
+
+if (*xiw > XSize())  *xiw = -1;
+if (*yiw > YSize())  *yiw = -1;
+
+if ((*xiw < 0) || (*yiw < 0))  return(1);
+else return(0);
+}
+
+
+/* --Methode-- */
+void PIImage::SetOffsetFromPave()
 {
 if (zoom > 0)
@@ -522,9 +681,15 @@
 
 /* --Methode-- */
-void PIImage::SetPave()
+void PIImage::CenterPave()
 {
 int xwsz, ywsz;
 
-xwsz = XSize();  ywsz = YSize();
+if (zoom > 0)
+  { xwsz = XSize()/zoom;  
+  ywsz = YSize()/zoom;  }
+else
+  { xwsz = -(XSize()*zoom); 
+  ywsz = -(YSize()*zoom); }
+  
 if (xwsz > (img->XSize()-offx)) xwsz = (img->XSize()-offx);
 if (ywsz > (img->YSize()-offy)) ywsz = (img->YSize()-offy);
Index: /trunk/SophyaPI/PI/piimage.h
===================================================================
--- /trunk/SophyaPI/PI/piimage.h	(revision 17)
+++ /trunk/SophyaPI/PI/piimage.h	(revision 18)
@@ -25,6 +25,9 @@
 
   void SetImage(RzImage *pim, bool refr=true);
+  void SetAutoDelImage(bool ad = false);
+
   void SetZoomWin(PIPixmap * zw, bool refr=true);
   inline void SetTextWin(PILabel * tw) { txw = tw; };
+  void ShowCursor(bool sc=true); 
 
   void SetLut(float min, float max, int typlut=kLutType_Lin,
@@ -33,5 +36,9 @@
 
   void SetZoom(int zm, bool refr=true);
+  void SetZoom(float fzm, bool refr=true);
+  float GetZoom();
+
   void SetOffset(int ox, int oy, bool refr=true);
+  void SetPave(int x, int y, bool refr=true);
 
   virtual void SetColMap(CMapId cmap, bool refr=true);
@@ -48,4 +55,5 @@
 
   virtual void Resize();
+  virtual void Draw(int x0, int y0, int dx, int dy);
 
   virtual void But1Press(int x, int y);
@@ -67,12 +75,19 @@
   void ComputeZoomPixmap();
 
+  void DrawCursor();
+  void DrawCursor(int xc, int yc);
+
   int PosW2Img(int xiw, int yiw, int * xp, int * yp);
-  void SetOffset();
-  void SetPave();
-  void SetPave(int x, int y, int dx=-1, int dy = -1);
+  int PosImg2W(int xp, int yp, int * xiw, int * yiw);
+
+  void SetOffsetFromPave();
+  void CenterPave();
 
 // Les donnees 
 
   RzImage * img;
+  bool aisup;
+  bool curshow;
+  bool winovis;   // $CHECK$ Reza A enlever si possible 17/6/96
 
   unsigned char * pixm;
@@ -91,4 +106,5 @@
   int xmv0, ymv0;
   int dxmv, dymv;
+  int xcurs, ycurs;
   int xszpav, yszpav;
 
Index: /trunk/SophyaPI/PI/pimak
===================================================================
--- /trunk/SophyaPI/PI/pimak	(revision 17)
+++ /trunk/SophyaPI/PI/pimak	(revision 18)
@@ -76,7 +76,9 @@
 
 #------------------------------------------------- End of Makefile.h ------- 
-all : piapp 
+all : piapp piapp_nosh
 
-piapp : $(EXE)piapp $(SLB)piup.so user.so
+piapp : $(EXE)piapp $(SLB)piup.so 
+ 
+piapp_nosh : $(EXE)piapp_nosh
  
 
@@ -86,4 +88,11 @@
               -L$(SLB) -lPI -lOuIm -lerosc -lStarReco \
               -lgcxx -lXm -lXt -lX11 -lm  
+
+$(EXE)piapp_nosh  : $(OBJ)pidemo_nosh.o $(LIB)libPI.a $(LIB)libStarReco.a  \
+               $(LIB)libOutils.a $(LIB)libImages.a $(LIB)libmath.a $(LIB)libdiv.a 
+	g++ -o $(EXE)piapp_nosh $(OBJ)pidemo_nosh.o \
+              -L$(LIB) -lPI -lStarReco -lImages -lOutils -lmath -ldiv \
+              -lXm -lXt -lX11 -lm 
+ 
 $(SLB)piup.so : $(OBJ)piup_def.o \
                 $(SLB)libOuIm.so $(SLB)liberosc.so 
@@ -91,9 +100,8 @@
         -none -L$(SLB) -lOuIm -lerosc -lgcxx -lm -lc
 
-user.so : $(OBJ)piup.o \
-          $(SLB)libOuIm.so $(SLB)liberosc.so 
-	ld -shared -o user.so -all $(OBJ)piup.o \
-        -none -L$(SLB) -lOuIm -lerosc -lgcxx -lm -lc
-
+$(OBJ)pidemo_nosh.o: pidemo.cc pidemo.h
+	g++ -g -O -c -Wall -Wpointer-arith \
+            -Wmissing-prototypes -Wsynth -I$(INC) -I$(CXI) \
+            -D C_NO_SHLIBS -o $(OBJ)pidemo_nosh.o pidemo.cc
 
 $(OBJ)pidemo.o: ./pidemo.cc \
@@ -120,17 +128,4 @@
  $(INC)utils.h pidrawwin.h \
  $(INC)piup.h
-$(OBJ)piup.o: piup.cc  piup.h \
- $(INC)perrors.h \
- $(INC)exceptions.h \
- $(INC)defs.h \
- $(INC)fitsimage.h \
- $(INC)cimage.h \
- $(INC)rzimage.h \
- $(INC)rzvect.h \
- $(INC)machine.h \
- $(INC)datatypes.h \
- $(INC)fitsheader.h \
- $(INC)fitskeys.h \
- $(INC)utils.h $(INC)peida.h 
 $(OBJ)piup_def.o: piup_def.cc  piup.h \
  $(INC)perrors.h \
Index: /trunk/SophyaPI/PI/pimsghandler.cc
===================================================================
--- /trunk/SophyaPI/PI/pimsghandler.cc	(revision 17)
+++ /trunk/SophyaPI/PI/pimsghandler.cc	(revision 18)
@@ -18,5 +18,9 @@
 PIMsgHandler::Send(long msg, void* data)
 {
-	Process(msg, this, data);
+//	Process(msg, this, data);  $CHECK$  Reza 12/06/96
+// Send par defaut appelle le process du parent $CHECK$
+if (msgParent)
+  msgParent->Process(msg, this, data);
+
 }
 
Index: /trunk/SophyaPI/PI/pimsghandler.h
===================================================================
--- /trunk/SophyaPI/PI/pimsghandler.h	(revision 17)
+++ /trunk/SophyaPI/PI/pimsghandler.h	(revision 18)
@@ -8,5 +8,5 @@
 enum {
  // Les messages
-	PIMsg_ActiveWindow  = 1,
+	PIMsg_ActiveWdg     = 1,
 	PIMsg_OK            = 2,
 	PIMsg_Cancel        = 3,
Index: /trunk/SophyaPI/PI/piperiodx.cc
===================================================================
--- /trunk/SophyaPI/PI/piperiodx.cc	(revision 17)
+++ /trunk/SophyaPI/PI/piperiodx.cc	(revision 18)
@@ -1,25 +1,38 @@
 #include "piperiodx.h"
-#include <algo.h>
-#include <signal.h>
 
-PIPeriodX::PIPeriodX(int d)
-: PIPeriodGen(d), it(-1)
-{}
+/* Nouvelle-Fonction */
+static void xttimer_action(XtPointer usd, XtIntervalId * /*tid*/)
+{
+PIPeriodX * pip;
+XtIntervalId id;
+pip = (PIPeriodX *)usd;
+pip->DoPeriodic();
+id = XtAddTimeOut(pip->Interval()*1000, xttimer_action, pip);
+pip->SetTimerId(id);
+return;
+}
 
+
+/* --Methode-- */
+PIPeriodX::PIPeriodX(int dt, UsPeriodicAction act, void * usp)
+: Periodic(dt, act, usp)
+{
+}
+
+/* --Methode-- */
 PIPeriodX::~PIPeriodX()
-{}
+{
+if (mFgact) Stop();
+}
 
-void
-PIPeriodX::Start()
+/* --Methode-- */
+void PIPeriodX::Start(int dt)
 {
-  if (it < 0) {
-    if (!actifs) actifs = new PIPeriodList;
-    it = 0;
-    if (actifs->size() == 0) {
-      signal(SIGALRM, CallBack);
-      alarm(1);
-    }
-    actifs->push_back(this);
-  }
+if (mFgact) return;
+if (dt > 0)  mDt = dt;
+
+mTId = XtAddTimeOut(mDt*1000, xttimer_action, this);
+mFgact = true;
+return;
 }
 
@@ -27,30 +40,8 @@
 PIPeriodX::Stop()
 {
-  if (it >= 0) {
-    it = -1;
-    remove(actifs->begin(), actifs->end(), this);
-    if (actifs->size() == 0) {
-      signal(SIGALRM, NULL);
-      delete actifs;
-      actifs = NULL;
-    }
-  }
+if(mFgact)
+  XtRemoveTimeOut(mTId);
+mFgact = false;
+return;
 }
 
-PIPeriodList* PIPeriodX::actifs = NULL;
-
-void PIPeriodX::CallBack(int)
-{
-  if (!actifs) return;
-
-  for (PIPeriodList::iterator i = actifs->begin(); i != actifs->end(); i++) {
-    PIPeriodX* p = (PIPeriodX*) *i;
-    p->it ++;
-    if (p->it >= p->dt) {
-      p->it = 0;
-      p->DoPeriodic();
-    }
-  }
-  alarm(1);
-}
-
Index: /trunk/SophyaPI/PI/piperiodx.h
===================================================================
--- /trunk/SophyaPI/PI/piperiodx.h	(revision 17)
+++ /trunk/SophyaPI/PI/piperiodx.h	(revision 18)
@@ -3,19 +3,19 @@
 #define PIPERIODX_H
 
-#include "piperiodgen.h"
+#include <X11/Intrinsic.h>
+#include "periodic.h"
 
-class PIPeriodX : public PIPeriodGen {
+class PIPeriodX : public Periodic {
 public:
-  PIPeriodX(int dt);
+  PIPeriodX(int dt, UsPeriodicAction act=NULL, void * usp=NULL);
   ~PIPeriodX();
 
-  void Start();
-  void Stop();
+  virtual void Start(int dt=-1);
+  virtual void Stop();
 
-  static PIPeriodList* actifs;
-  static void CallBack(int);
+  inline void SetTimerId(XtIntervalId id) {mTId=id; }
 
 protected:
-  int it;
+XtIntervalId mTId;
 };
 
Index: /trunk/SophyaPI/PI/pipixmapx.cc
===================================================================
--- /trunk/SophyaPI/PI/pipixmapx.cc	(revision 17)
+++ /trunk/SophyaPI/PI/pipixmapx.cc	(revision 18)
@@ -32,15 +32,17 @@
 unsigned char *pp;
 
-for(j=0; j<ny; j++)
+if (pixmap)
   {
-  pp = pixmap+j*nx;
-  for(i=0; i<nx; i++)
-    { XPutPixel(myximg, i, j, cmap->Color(*pp) );  pp++; }
+  for(j=0; j<ny; j++)
+    {
+    pp = pixmap+j*nx;
+    for(i=0; i<nx; i++)
+      { XPutPixel(myximg, i, j, cmap->Color(*pp) );  pp++; }
+    }
+
+  for(j=ny; j<YSize(); j++)
+    for(i=nx; i<XSize(); i++)
+      XPutPixel(myximg, i, j, 0 );
   }
-
-for(j=ny; j<YSize(); j++)
-  for(i=nx; i<XSize(); i++)
-    XPutPixel(myximg, i, j, 0 );
-
 PIBaseWdgX::Refresh();
 }
@@ -86,5 +88,5 @@
   
 XPutImage(mdsp, xw, mygc, myximg, x0, y0, x0, y0, dx, dy);
-XSync(mdsp, 0);
+XFlush(mdsp);
 
 return;
@@ -129,4 +131,5 @@
 int depth;
 int pad;
+int i,j;
 
 xw = XtWindow(XtWdg());
@@ -140,5 +143,11 @@
 if (myximg == NULL)  return;
 myximg->data = new char [YSize()*myximg->bytes_per_line];
-if (myximg->data == NULL)  XFree(myximg); 
+if (myximg->data == NULL)  
+  { XFree(myximg); myximg = NULL; } 
+
+for(j=0; j<YSize(); j++)
+  for(i=0; i<XSize(); i++)
+    XPutPixel(myximg, i, j, 0 );
+
 return;
 }
Index: /trunk/SophyaPI/PI/piscdrawwdg.cc
===================================================================
--- /trunk/SophyaPI/PI/piscdrawwdg.cc	(revision 17)
+++ /trunk/SophyaPI/PI/piscdrawwdg.cc	(revision 18)
@@ -2,4 +2,5 @@
 #include "generalfit.h"
 #include <algo.h>
+
 
 PIScDrawWdg::PIScDrawWdg(PIContainerGen *par, char *nom,
@@ -12,4 +13,8 @@
   SetAxesFlags(kAxesDflt);
   clip = true;
+  padsup = false;
+  mTxw = NULL;
+  ActivateButton(1);    // Pour pouvoir activer la fenetre et coordonnees 
+  ActivateMove(1);      //    "         "         "        "
   ActivateButton(2);
   ActivateMove(2);
@@ -488,4 +493,5 @@
 PIScDrawWdg::RemoveDrawer(PIScDrawer* d)
 {
+  if (padsup)  return;
   mDrawers.remove(d);
   d->mDrawWdg = NULL;
@@ -495,8 +501,9 @@
 PIScDrawWdg::DeleteDrawers()
 {
+  padsup = true;
   for(list<PIScDrawer*>::iterator i = mDrawers.begin(); i != mDrawers.end(); i++)
     delete *i;
-    
   mDrawers.erase(mDrawers.begin(),mDrawers.end());
+  padsup = false;
 }
 
@@ -535,6 +542,44 @@
 
 
+
+// Gestion affichage coordonnees du point 
+void
+PIScDrawWdg::But1Press(int x, int y)
+{
+  if (UpdateText(x, y)) 
+     SelPointerShape(PI_CrossPointer);
+  Send(PIMsg_ActiveWdg);
+
+}
+
+void
+PIScDrawWdg::Ptr1Move(int x, int y)
+{
+  UpdateText(x, y);
+}
+
+void
+PIScDrawWdg::But1Release(int x, int y)
+{
+  if (UpdateText(x, y)) 
+    SelPointerShape(PI_ArrowPointer);
+
+}
+
+bool
+PIScDrawWdg::UpdateText(int xp, int yp)
+{
+  if (mTxw == NULL)  return(false);
+
+  char buff[128];
+  double dx, dy;
+  Local2Sc(xp, yp, dx, dy); 
+  sprintf(buff, "X= %g  Y= %g",  dx, dy);
+  mTxw->SetLabel((string)buff);
+  return(true);
+
+}
+
 // Gestion du zoom
-
 void
 PIScDrawWdg::But2Press(int x, int y)
@@ -550,4 +595,5 @@
   SelGOMode(PI_GOXOR);
   SelLine(PI_ThinLine);
+  Send(PIMsg_ActiveWdg);
 }
 
Index: /trunk/SophyaPI/PI/piscdrawwdg.h
===================================================================
--- /trunk/SophyaPI/PI/piscdrawwdg.h	(revision 17)
+++ /trunk/SophyaPI/PI/piscdrawwdg.h	(revision 18)
@@ -5,4 +5,5 @@
 #include "pisysdep.h"
 #include PIBWDG_H
+#include PISTDWDG_H
 
 #include <list.h>
@@ -67,4 +68,6 @@
   virtual void       SetAxesFlags(int flags=kAxesDflt);
   
+  void   SetTextWin(PILabel * tw) {mTxw = tw; }
+
   double XMin() const {return xMin;}
   double XMax() const {return xMax;}
@@ -75,6 +78,10 @@
   void   NoClip() {clip = false;}
   void   Clip() {clip = true;}
-  
-  
+    
+
+  virtual void       But1Press(int x, int y);
+  virtual void       But1Release(int x, int y);
+  virtual void       Ptr1Move(int x, int y);
+
   virtual void       But2Press(int x, int y);
   virtual void       But2Release(int x, int y);
@@ -103,6 +110,9 @@
   PIPointer cPointer;
   PILineAtt cLatt;
+  PILabel * mTxw;  
   bool clip;
-  
+  bool padsup;
+
+
 private:
   void DrawHTicks(double y, double tickUp, double tickDown, double xBeg, double xStep);
@@ -110,4 +120,6 @@
   void DrawHLabels(double y, double xBeg, double xStep, int just=0);
   void DrawVLabels(double x, double yBeg, double yStep, int just=0);
+  
+  bool  UpdateText(int xp, int yp);
 };
 
Index: /trunk/SophyaPI/PI/pisysdep.h
===================================================================
--- /trunk/SophyaPI/PI/pisysdep.h	(revision 17)
+++ /trunk/SophyaPI/PI/pisysdep.h	(revision 18)
@@ -40,4 +40,6 @@
   #define PIPIXMAP_H "pipixmapx.h"
 
+  #define PIPERIODIC_H "piperiodicx.h"
+
 #endif
 
Index: /trunk/SophyaPI/PI/pitestapp.cc
===================================================================
--- /trunk/SophyaPI/PI/pitestapp.cc	(revision 17)
+++ /trunk/SophyaPI/PI/pitestapp.cc	(revision 18)
@@ -207,5 +207,5 @@
   this->label->SetLabel(truc);
 
-  if (msg == PIMsg_ActiveWindow) 
+  if (msg == PIMsg_ActiveWdg) 
     { /* printf("Process: Setting active ImageWindow (%lx) \n", (long)data); */
     cur = (PIImage *)data; }
Index: /trunk/SophyaPI/PI/piup.cc
===================================================================
--- /trunk/SophyaPI/PI/piup.cc	(revision 17)
+++ /trunk/SophyaPI/PI/piup.cc	(revision 18)
@@ -8,11 +8,11 @@
 #include "piup.h"
 
-FitsImageR4 * PIUserProc_1(FitsImageR4 * myimg, int xp, int yp, int dx, int dy,
-                           char * arg1, char * arg2)
+ImageR4 * PIUserProc_1(ImageR4 * myimg, int xp, int yp, int dx, int dy,
+                       char * arg1, char * arg2)
 {
 printf("**** PIUserProc_1 Pave[%d*%d] en (%d,%d) %s %s  Filtrage passe-bas 3*3 \n",
        dx, dy, xp, yp, arg1, arg2);
 ImageR4 filt(3,3);
-FitsImageR4 * out;
+ImageR4 * out;
 
 // Definition du filtre passe-bas
@@ -23,5 +23,5 @@
 
 filt.PrintImage();
-out = new FitsImageR4(myimg->XSize(), myimg->YSize());
+out = new ImageR4(myimg->XSize(), myimg->YSize());
 TIMEF ;
 FilterImage((ImageR4 *) myimg, (ImageR4 *)out, &filt); 
@@ -29,6 +29,6 @@
 }
  
-FitsImageR4 * PIUserProc_2(FitsImageR4 * myimg, int xp, int yp, int dx, int dy,
-                           char * arg1, char * arg2)
+ImageR4 * PIUserProc_2(ImageR4 * myimg, int xp, int yp, int dx, int dy,
+                       char * arg1, char * arg2)
 {
 printf("**** PIUserProc_2 Pave[%d*%d] en (%d,%d) %s %s  Ombre 3*3 \n",
@@ -36,5 +36,5 @@
 printf(" PIUserProc_2(%d %d %s %s)  Ombre 3*3 \n", xp, yp, arg1, arg2);
 ImageR4 filt(3,3);
-FitsImageR4 * out;
+ImageR4 * out;
 
 // Definition du filtre ombre
@@ -46,5 +46,5 @@
 filt(2,0) = 2.;
 filt.PrintImage();
-out = new FitsImageR4(myimg->XSize(), myimg->YSize());
+out = new ImageR4(myimg->XSize(), myimg->YSize());
 TIMEF ;
 FilterImage((ImageR4 *) myimg, (ImageR4 *)out, &filt); 
@@ -53,6 +53,6 @@
 
  
-FitsImageR4 * PIUserProc_3(FitsImageR4 * , int xp, int yp, int dx, int dy,
-                           char * arg1, char * arg2)
+ImageR4 * PIUserProc_3(ImageR4 * , int xp, int yp, int dx, int dy,
+                       char * arg1, char * arg2)
 {
 printf("PIUserProc_3 Pave[%d,%d] en (%d %d) %s %s ne fait rien !\n", 
Index: /trunk/SophyaPI/PI/piup.h
===================================================================
--- /trunk/SophyaPI/PI/piup.h	(revision 17)
+++ /trunk/SophyaPI/PI/piup.h	(revision 18)
@@ -1,8 +1,8 @@
-#include "fitsimage.h"
+#include "cimage.h"
 
-FitsImageR4 * PIUserProc_1(FitsImageR4 * myimg, int xp, int yp, int dx, int dy,
-                           char * arg1, char * arg2);
-FitsImageR4 * PIUserProc_2(FitsImageR4 * myimg, int xp, int yp, int dx, int dy,
-                           char * arg1, char * arg2);
-FitsImageR4 * PIUserProc_3(FitsImageR4 * myimg, int xp, int yp, int dx, int dy, 
-                           char * arg1, char * arg2);
+ImageR4 * PIUserProc_1(ImageR4 * myimg, int xp, int yp, int dx, int dy,
+                       char * arg1, char * arg2);
+ImageR4 * PIUserProc_2(ImageR4 * myimg, int xp, int yp, int dx, int dy,
+                       char * arg1, char * arg2);
+ImageR4 * PIUserProc_3(ImageR4 * myimg, int xp, int yp, int dx, int dy, 
+                       char * arg1, char * arg2);
Index: /trunk/SophyaPI/PI/piup_def.cc
===================================================================
--- /trunk/SophyaPI/PI/piup_def.cc	(revision 17)
+++ /trunk/SophyaPI/PI/piup_def.cc	(revision 18)
@@ -8,6 +8,6 @@
 #include "piup.h"
 
-FitsImageR4 * PIUserProc_1(FitsImageR4 * , int xp, int yp, int dx, int dy,
-                           char * arg1, char * arg2)
+ImageR4 * PIUserProc_1(ImageR4 * , int xp, int yp, int dx, int dy,
+                       char * arg1, char * arg2)
 {
 printf("PIUserProc_1/Dummy Pave[%d,%d] en (%d %d) %s %s ne fait rien !\n", 
@@ -16,6 +16,6 @@
 }
  
-FitsImageR4 * PIUserProc_2(FitsImageR4 * , int xp, int yp, int dx, int dy,
-                           char * arg1, char * arg2)
+ImageR4 * PIUserProc_2(ImageR4 * , int xp, int yp, int dx, int dy,
+                       char * arg1, char * arg2)
 {
 printf("PIUserProc_2/Dummy Pave[%d,%d] en (%d %d) %s %s ne fait rien !\n", 
@@ -24,6 +24,6 @@
 }
  
-FitsImageR4 * PIUserProc_3(FitsImageR4 * , int xp, int yp, int dx, int dy,
-                           char * arg1, char * arg2)
+ImageR4 * PIUserProc_3(ImageR4 * , int xp, int yp, int dx, int dy,
+                       char * arg1, char * arg2)
 {
 printf("PIUserProc_3/Dummy Pave[%d,%d] en (%d %d) %s %s ne fait rien !\n", 
Index: /trunk/SophyaPI/PI/piwdggen.cc
===================================================================
--- /trunk/SophyaPI/PI/piwdggen.cc	(revision 17)
+++ /trunk/SophyaPI/PI/piwdggen.cc	(revision 18)
@@ -12,5 +12,5 @@
 parent = par;
 SetMsg(0);
-if (par)  par->NewChild(this);
+if (par)  par->ChildAdd(this);   //  On previent le container parent  
 return;
 }
@@ -20,4 +20,5 @@
 PIWdgGen::~PIWdgGen()
 {
+if (parent)  parent->ChildDel(this);   //  On previent le container parent 
 }
 
Index: /trunk/SophyaPI/PI/piwindowgen.cc
===================================================================
--- /trunk/SophyaPI/PI/piwindowgen.cc	(revision 17)
+++ /trunk/SophyaPI/PI/piwindowgen.cc	(revision 18)
@@ -1,4 +1,6 @@
 #include "piwindowgen.h"
 
+
+/* --Methode-- */
 PIWindowGen::PIWindowGen(PIMsgHandler *par, char *nom, PIWindowKind k, 
                          int sx, int sy, int px, int py)
@@ -9,7 +11,17 @@
 
 
+/* --Methode-- */
 PIWindowGen::~PIWindowGen()
 {}
 
 
-
+/* --Methode-- */
+void PIWindowGen::Process(long msg, PIMsgHandler* sender, void* data)
+/*  On intercepte le message ActiveWdg, avant de le renvoyer, */
+/*  avec un message ActiveWdg de la part de la fenetre        */
+{
+if (msg == PIMsg_ActiveWdg)
+  Send(msg);
+ReSend(msg, sender, data);
+return;  
+}
Index: /trunk/SophyaPI/PI/piwindowgen.h
===================================================================
--- /trunk/SophyaPI/PI/piwindowgen.h	(revision 17)
+++ /trunk/SophyaPI/PI/piwindowgen.h	(revision 18)
@@ -20,6 +20,8 @@
 
   virtual long      kind() {return ClassId; }
+
+  virtual void      Process(long msg, PIMsgHandler* sender, void* data=NULL);
   
-protected:
+protected:  
   PIWindowKind	       mWtyp;
 };
