#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <iostream.h>
#include <X11/Intrinsic.h>
#include <Xm/Xm.h>  // Pour *DBG*

#include "pisysdep.h"

#include PIAPP_H
#include PIMENU_H 
#include PISTDWDG_H 
#include PIWIN_H
#include "picons.h"

class PITApp : public PIApplication {
public:
     PITApp(int narg=0, char* arg[]=NULL);
     ~PITApp();
     virtual void   Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL
);

// protected :
     
  PIMenu * m[2];
  PIWindow *mCurW;
  PIButton *but[2];
};


static PIConsole* scons=NULL;
static PIConsole* cons=NULL;

/* --Methode-- */
PITApp::PITApp(int narg, char* arg[])
: PIApplication(600, 300, narg, arg)
{


m[0] = new PIMenu(Menubar(),"Fichier");
m[0]->AppendItem("NewConsole", 10101);
m[0]->AppendItem("StdOut", 10102);
m[0]->AppendItem("StdOut-2", 10103);
m[0]->AppendItem("StdErr", 10104);
m[0]->AppendItem("StdErr-2", 10105);
m[0]->AppendItem("AccCmd", 10110);
m[0]->AppendItem("AccCmd-2", 10111);
m[0]->AppendItem("Exit", 10177,"Q");
// On accroche le menu au Menubar
AppendMenu(m[0]);


m[1] = new PIMenu(Menubar(),"Actions");
m[1]->AppendItem("Normal", 10201);
m[1]->AppendItem("Bold", 10202);
m[1]->AppendItem("Italique", 10203);
m[1]->AppendItem("Reverse", 10204);
m[1]->AppendItem("Print", 10205);
m[1]->AppendItem("PrintNNL", 10206);
m[1]->AppendItem("COUT", 10207);
m[1]->AppendItem("PrintErr", 10208);
m[1]->AppendItem("CERR", 10209);

AppendMenu(m[1]);

but[0] = but[1] = NULL;

but[0] = new PIButton(MainWin(), "OK", 501, 50,  25, 30, 10);
but[1] = new PIButton(MainWin(), "Cancel", 502, 50,  25, 100, 10);
// printf("---DBG--- %lx %lx %lx \n", XtWindow(MainWin()->XtWdg()), 
//       XtWindow(but[0]->XtWdg()), XtWindow(but[1]->XtWdg()) ); 

scons = new PIConsole(MainWin(), "PIConsole", 4540, 120, 70, 600, 250, 0, 50 );

// printf("---DBG-2=== %lx %lx % \n", XtWindow(PIXtTopWdg()), 
//       XtWindow(scons->XtWdg()));

int kkk;
for(kkk=0; kkk<10; kkk++) { 
  scons->AddStr("A-B-C-JHGTREWQ====Reza Ansari");
  scons->AddChar('\n'); 
  }
for(kkk=0; kkk<5; kkk++) {
  scons->AddStr("**** CMV ****", PIVA_Bold);
  scons->AddStr(" Hello, Comment ca va ? Salam - Bye, Bye ",PIVA_Ital); 
  scons->AddChar('\n'); 
  }
  

scons->AcceptCmd(true);
// RedirectOutStream(scons);
// RedirectErrStream(scons);


 
mCurW = NULL;    // Fenetre courante
SetReady();
}

/* --Methode-- */
PITApp::~PITApp()
{
}  

static int lines = 0;

/* --Methode-- */
void PITApp::Process(PIMessage msg, PIMsgHandler* sender, void* data)
{
int i,l;
char buff[128];

/*   Debug version Motif
XmNavigationType navtyp;
Boolean trav;

Arg  wargs[2];
navtyp = 0;
trav = FALSE;
XtSetArg(wargs[0],XmNnavigationType, &navtyp);  
XtSetArg(wargs[1],XmNtraversalOn, &trav);  
XtGetValues(((PIWdg*)sender)->XtWdg(), wargs, 2);

printf("-- DBG -- PITApp::Process() Widget= %lx (%lx) \n", (long) ((PIWdg*)sender)->XtWdg(), (long)sender);
printf(" NavType = %d  (None=%d)  Trav= %d (False=%d) \n", (int)navtyp, (int)XmNONE, (int)trav, (int)FALSE);
*/


if ( ( ModMsg(msg) == PIMsg_Active) || (ModMsg(msg) == PIMsg_Close) ) {
  PIWdg *sndw;
  sndw = (PIWdg *)sender;      
  if (sndw->kind() ==  PIWindow::ClassId)  {
    if (ModMsg(msg) == PIMsg_Close)  { 
      printf("PITApp::Process()/Debug : Closing Window %lx \n", (long)sender);
      delete (PIWindow *)sender;  
      mCurW = NULL; }
    else  mCurW = (PIWindow *)sender;
  }
}
else
  {
  switch(UserMsg(msg))
    {
    case 4540: 
      if (ModMsg(msg) == PIMsg_OK) 
        printf("Cmd from Console L=%d [%s] \n", strlen((char*)data), (char*)data ); 
      else 
        printf("Console Msg= %d Mod = %d \n",UserMsg(msg), ModMsg(msg));
      break; 

// --------- 1er menu ----------
    case 10101: 
      {
      PIWindow* win = new PIWindow(this, "Console-2", PIWK_normal, 500, 200, 200, 200);
      win->SetAutoDelChilds(true);
      win->Show();
      cons = new PIConsole(win, "PIConsole-2", 4560, 100, 60, 500, 200, 0, 0 );
      int kkk;
      for(kkk=0; kkk<20; kkk++) { 
        cons->AddStr("A-B-C-JHGTREWQ====Reza Ansari");
        cons->AddChar('\n'); 
        }
      for(kkk=0; kkk<25; kkk++) {
        cons->AddStr("**** CMV ****", PIVA_Bold);
        cons->AddStr(" Hello, Comment ca va ? Salam - Bye, Bye ",PIVA_Ital); 
        cons->AddChar('\n'); 
        }
      }
      break;
    case 10102: 
      RedirectOutStream(scons);
      break;

    case 10103: 
      RedirectOutStream(cons);
      break;

    case 10104: 
      RedirectErrStream(scons);
      break;

    case 10105: 
      RedirectErrStream(cons);
      break;

    case 10110 :
      if (scons) scons->AcceptCmd(true);
      break;
    case 10111 :
      if (cons) cons->AcceptCmd(true);
      break;

    case 10177:          // Sortie d'application
      Stop();
      break;

// ----------  2eme menu --------
  case 10207 :
    for(l=0; l<3; l++) 
      cout << "TApp: Test-Cout (K= " << l << ") " << endl ; 
    break;
  case 10205 :
    for(l=0; l<4; l++) 
      printf("TApp: Test-Redirect Print %d - %d \n", l, lines); lines++;
    break;
  case 10206 :
      printf("TApp: T-Print NNL %d n", lines);
    break;

  case 10209 :
    for(l=0; l<2; l++) 
      cerr << "TApp: CERR--Test (K= " << l << ") " << endl ; 
    break;
  case 10208 :
    for(l=0; l<8; l++) 
      fprintf(stderr, "TApp: StdErr Redirect  %d - %d \n", l, lines);  lines++;
    break;

  case 10204 :
    for(l=0; l<1; l++) {
      sprintf(buff, "TApp: Line type Reverse No %d ", lines);
      scons->AddStr(buff, PIVA_Reverse, false);
      sprintf(buff, "Normal  ");
      scons->AddStr(buff, PIVA_Def, false);
      sprintf(buff, "Bold  ");
      scons->AddStr(buff, PIVA_Bold, false);
      lines++;
    }   
    scons->Refresh();
    break;
  
  case 10202 :
    for(l=0; l<3; l++) {
      sprintf(buff, "TApp: Line type Bold No %d \n", lines);
      scons->AddStr(buff, PIVA_Bold, false);
      lines++;
    }   
    scons->Refresh();
    break;
  
  case 10203 :
    for(l=0; l<3; l++) {
      sprintf(buff, "Line type Italique No %d \n", lines);
      scons->AddStr(buff, PIVA_Ital, false);
      lines++;
    }   
    scons->Refresh();
    break;
  
  case 10201 :
    for(l=0; l<3; l++) {
      sprintf(buff, "Tapp: Line type Normal No %d \n", lines);
      scons->AddStr(buff, PIVA_Def, false);
      lines++;
    }   
    scons->Refresh();
    break;


    default :
      printf("PIDemoApp::Process() Msg= %d (%d - %d)??? \n", 
            (int)msg, (int)UserMsg(msg),(int)ModMsg(msg));
     break;
    }
  }
return;
}


static PITApp * app;


int main(int narg, char *arg[])
{

  // PeidaStarRecoInitiator InitStar;

app = new PITApp(narg, arg);
app->SetBusy();
puts("appel de Application.Run() ... ");
app->SetReady();
app->Run();

delete app;
exit(0);
}


