// This may look like C code, but it is really -*- C++ -*-
// Standard Image App      R. Ansari  97-98
// LAL (Orsay) / IN2P3-CNRS  DAPNIA/SPP (Saclay) / CEA

#ifndef PISTIMGAPP_H_SEEN
#define PISTIMGAPP_H_SEEN

#include <string.h>
#include <string>
#include <map>
#include <functional>

#include "pisysdep.h"

#include PIAPP_H
#include PIWIN_H 
#include PIMENU_H 
#include PIOPTMENU_H 

#include PISTDWDG_H 

#include PIFILECHO_H 
#include "picons.h"

#include "piimage.h"
#include "pi3ddrw.h"
#include "nobjmgr.h"
#include "piacmd.h"
#include "pisiadw.h"
#include "pistzwin.h"


enum {Disp_Next=0, Disp_Win=1, Disp_Same=2, Disp_Stack=3, Disp_Inset=4};

typedef map<long, PIWindow*, less<long> > WindMList;
typedef map<long, PIBaseWdg*, less<long> > BWMList;
typedef map<long, PIDrawer*, less<long> > DrwMList;

class PIStdImgApp : public PIApplication  {
public:
     PIStdImgApp(int narg=0, char* arg[]=NULL);
     ~PIStdImgApp();

     virtual void   Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
     virtual void   SetReady();
     virtual void   SetBusy(); 
     virtual void   SetBlocked();

     int  DispImage(P2DArrayAdapter* nouv, string const & name, int opt, int oid=0);
     int  DispScDrawer(PIDrawer* scd, string const & name, int opt, string title="", int oid=0);
     int  Disp3DDrawer(PIDrawer3D* scd, string const & name, int opt, string title="", int oid=0);

  //  Fonction d'ajout de texte (provisoire - Aout 99) 
     void AddText(string const & txt, double xp, double yp);
  //  Ajout de lignes, rectangles, cercles (provisoires - Aout 2001)
     void AddLine(double xp1, double yp1, double xp2, double yp2);
     void AddRectangle(double xp1, double yp1, double xp2, double yp2, bool fgfill=false);
     void AddCircle(double xc, double yc, double r, bool fgfill=false);

  //  Fonction Ajout de titre de trace
     void SetTitle(string const & titleup, string const & titledown);

  //  Acces au BaseDrawer (PIElDrawer) du PIBaseWdg courant
     PIElDrawer* CurrentElDrawer();

     void CreateGraphWin(int nx=1, int ny=1, int sx=0, int sy = 0);
     void CreateStackWin(int sx=0, int sy = 0);
     PIWindow * GetWindow(int typ, int& sx, int& sy, int& px, int& py, int& flag, char * nom);
     void SetZone(int nzx=1, int nzy=1);
     void StackWinNext();

     void DeleteWindow(PIWindow* w);
     void DeleteBaseWidget(PIBaseWdg* w, bool dw=true, bool dwin=true);
     void DelWRsId(int k);
     bool CheckWRsId(int k);
     void CloseAllWindows();

     inline NamedObjMgr* ObjMgr() { return mObjMgr; }
     inline PIACmd* CmdInterpreter() { return mCmd; }

     PIWindow* CurrentWindow() { return (mCurWin); };
     PIBaseWdg* CurrentBaseWdg() { return (mCurWdg); };

     inline PIPixmap* ZoomW() { return zoom; }
     inline PIPixmap* GloVW() { return gimv; }
     inline PICMapView* CMapVW() { return cmapv; }
     inline PIConsole* GetConsole() { return mCons; }

     inline PIMenu* SpecialMenu() { return m[5]; }
     inline PIMenu* ModulesMenu() { return m[6]; }

     inline ObjMgrWind* ObjMgrW() { return mObjmgrw; }
     inline PPInMgrWind* PPInMgrW() { return mPpinmgrw; }

// Redirection ou Non de StdOut/StdErr
     void  RedirectStdOutErr(bool fg = true);
     inline bool HasRedirectedStdOutErr() { return(redirecout); }
// Gestion des signaux
     void  CatchSignals(bool fgfpe=true, bool fgsegv=true);

//  Gestion d attributs graphiques courants
     void  SetColAtt(PIColors fg=PI_NotDefColor, 
                     PIColors bg=PI_NotDefColor);
     void  SetLineAtt(PILineAtt lat=PI_NotDefLineAtt);
     void  SetFontName(PIFontName fn=PI_DefaultFont);
     void  SetFontAtt(PIFontSize fsz=PI_NotDefFontSize,
                      PIFontAtt fat=PI_NotDefFontAtt);
     void  SetMarkerAtt(int sz=-1, PIMarker mrk=PI_NotDefMarker);
     inline void  SetColMapId(CMapId cid=CMAP_OTHER) { mCmapid = cid; }
     inline void  SetReverseColMapFlag(bool fg=false) { mRevCmap = fg; }
     void  SetZoomAtt(int zoom=0);
     void  SetAxesAtt(unsigned int axfl=kBoxAxes | kExtTicks | kLabels);
     void  SetXYLimits(double xmin=-1., double xmax=1., double ymin=-1., double ymax=1.);
     void  SetInsetLimits(double xmin=0.4, double xmax=0.6, double ymin=0.4, double ymax=0.6);
     void  SetImageCenterPosition(int x=-1, int y=-1);
     inline void  UseXYLimits(bool fg=false) {  mFXYlim=fg; }
     inline void  UseImageCenter(bool fg=false) { mFImgCenter=fg; }
     inline void  SetXLogScale(bool logx=false) { maXlog = logx; }
     inline void  SetYLogScale(bool logy=false) { maYlog = logy; }
     inline void  SetAutoAddTitle(bool aatit=true) { mAddTitle = aatit; }

// Pour sauvegarder-restauration de  l'etat des attributs graphiques
     void  SaveGraphicAtt();   
     void  RestoreGraphicAtt();   

// Gestion fichiers PS
    int num_eps;
    PSFile *mpsfile;
    string name_ps;

// Gestion OUT-PPF
    POutPersist* mPpfout;
    string name_outppf;    

private:
    void MBProcess1(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
    void MBProcess2(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
    void MBProcess3(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
    void MBProcess4(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
    void MBProcess5(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
    void MBProcess6(PIMessage msg, PIMsgHandler* sender, void* data=NULL);


    NamedObjMgr* mObjMgr;
    PIACmd* mCmd;

    PIMenu* m[7];
    PIMenu* mc;

    PIPixmap* zoom;
    PIPixmap* gimv;
    PICMapView* cmapv;

    PIFileChooser * pfc_fits;   // Pour les fichiers FITS
    PIFileChooser * pfc_ppf;    //  Pour les PPF 
    PIFileChooser * pfc_ps;     // Pour les PostScript

    PIConsole* mCons;
    bool redirecout;    // true if stdout/err redirected to mCons;

    WindMList mWList;
    BWMList mBWList;
    DrwMList mDrwList;

    int mWId,mDId,mBWId;

    PIBaseWdg* mCurWdg;
    PIBaseWdg* mLastWdg;
    PIWindow* mCurWin;
    PIZoneWindow* mGrW;
    PIStackWindow* mStW;
    int mGrIdx;
    int mStIdx;

    ObjMgrWind* mObjmgrw;
    PPInMgrWind* mPpinmgrw;

//  Gestion d attributs graphiques courants, etat sauvegarde
    PIColors     mFCol, mBCol, mSFCol, mSBCol;
    PILineAtt    mLAtt, mSLAtt;
    PIFontName   mFName, mSFName;
    PIFontSize   mFSz, mSFSz;
    PIFontAtt    mFAtt, mSFAtt;
    int          mMSz, mSMSz;
    PIMarker     mMrk,mSMrk;  
    CMapId       mCmapid, mSCmapid;
    bool         mRevCmap, mSRevCmap;
    int 	 mZoom, mSZoom;
    unsigned int mAxesFlags, mSAxesFlags;
    double	 mXmin, mXmax, mYmin, mYmax;
    double	 mSXmin, mSXmax, mSYmin, mSYmax;
    double	 mIXmin, mIXmax, mIYmin, mIYmax;
    double	 mSIXmin, mSIXmax, mSIYmin, mSIYmax;
    int		 mXImgCenter, mYImgCenter;
    int		 mSXImgCenter, mSYImgCenter;
    bool	 mFXYlim, mSFXYlim;
    bool	 mFImgCenter, mSFImgCenter;
    bool	 maXlog, mSaXlog;
    bool	 maYlog, mSaYlog;
    bool         mAddTitle, mSAddTitle;

    PIMessage mFCMsg;  // Message for processing FileOpen
    int mFgScSz;   // Dlag ScreenSize
    bool mFgCWImg; // true -> Current Widget is PIImage
};

#endif
