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

#include "sopnamsp.h"
#include "pisysdep.h"

#include "piapplx.h"
#include "pifilechox.h"


/************* PIFileChooserX ************/
#include <X11/Shell.h>
#include <Xm/FileSB.h>

// Pour ces sal... de XmString  (ds piwdgx.cc)
int CStrFrXmStr(XmString xms, char* buff, int nmx);

// Fonctions privees (CallBack)
//static void filesel_okact(Widget w, XtPointer *usd, XtPointer *calld);
static void filesel_cancelact(Widget w, XtPointer *usd, XtPointer *calld);

/* Nouvelle-Fonction */
void  filesel_okact(Widget /*w*/, XtPointer *usd, 
                    XmFileSelectionBoxCallbackStruct *calld)
{
PIFileChooserX *pif;
pif = (PIFileChooserX *) usd ;
pif->mSt = 1;
pif->MWin()->UnManage();
//pif->Send(pif->Msg(), (void *) (pif->GetFileName().c_str()));
CStrFrXmStr(calld->value, pif->mFName, 512); 
pif->Send( pif->Msg(), PIMsg_OK, (void *) pif->mFName);
return;
}

/* Nouvelle-Fonction */
void  filesel_cancelact(Widget /*w*/, XtPointer *usd, XtPointer */*calld*/)
{
PIFileChooserX *pif;
pif = (PIFileChooserX *) usd ;
pif->mSt = 0;
pif->mFName[0] = '\0';
pif->MWin()->UnManage();
pif->Send(pif->Msg(), PIMsg_Cancel, NULL);
return;
}

static int PIFC_SzX, PIFC_SzY;
/* --Methode-- */
PIFileChooserX::PIFileChooserX(PIMsgHandler* par, const char* nom, PIMessage msg)
: PIFileChooserGen(par, nom, msg)
{
SysDWdg wdg;

mWin = NULL;

int sx, sy;
PIApplicationPrefCompSize(sx, sy);
PIFC_SzX = sx*6;  PIFC_SzY = sy*10;
mWin = new PIWdgX(NULL, nom, PIFC_SzX, PIFC_SzY, 200, 200);
mWin->CreateXtWdg(nom, topLevelShellWidgetClass, NULL, PIFC_SzX, PIFC_SzY, 200, 200);

mSt = 0;
mFName[0] = mFName[511] = '\0';

CreateXtWdg(nom, xmFileSelectionBoxWidgetClass, mWin->XtWdg(), sx, sy, 0, 0);
XtAddCallback(XtWdg(), XmNokCallback, 
              (XtCallbackProc)filesel_okact, (XtPointer)this);
XtAddCallback(XtWdg(), XmNcancelCallback, 
              (XtCallbackProc)filesel_cancelact, (XtPointer)this);
// Pour avoir une version simplifiee 
/* wdg = XmFileSelectionBoxGetChild(XtWdg(),XmDIALOG_DIR_LIST);
XtUnmanageChild(wdg);
wdg = XmFileSelectionBoxGetChild(XtWdg(),XmDIALOG_DIR_LIST_LABEL);
XtUnmanageChild(wdg);
*/
wdg = XmFileSelectionBoxGetChild(XtWdg(),XmDIALOG_HELP_BUTTON);
XtUnmanageChild(wdg);

fnl = XmFileSelectionBoxGetChild(XtWdg(),XmDIALOG_SELECTION_LABEL);
fnw = XmFileSelectionBoxGetChild(XtWdg(),XmDIALOG_TEXT);

Arg warg[2];
XtSetArg(warg[0], XmNfileTypeMask, XmFILE_REGULAR);
XtSetValues(XtWdg(), warg, 1);

AcceptNewFile(false);
Manage();
}

/* --Methode-- */
PIFileChooserX::~PIFileChooserX()
{
delete mWin;
}

/* --Methode-- */
void PIFileChooserX::AcceptNewFile(bool acc)
{
if (acc) 
  {
  mWin->SetSize( PIFC_SzX, PIFC_SzY*1.25);
  XtManageChild(fnl);  XtManageChild(fnw); 
  }
else 
  { 
  mWin->SetSize( PIFC_SzX, PIFC_SzY*1.1);
  XtUnmanageChild(fnl); XtUnmanageChild(fnw); 
  }
}

/* --Methode-- */
void PIFileChooserX::Show()
{
mWin->Manage();
}

/* --Methode-- */
void PIFileChooserX::SetPath(string const& path)
{
XmString xmstr;
xmstr = XmStringLtoRCreate((char *)path.c_str(), XmSTRING_DEFAULT_CHARSET);
Arg warg[2];
XtSetArg(warg[0], XmNdirectory, xmstr); 
XtSetValues(XtWdg(), warg, 1);
XmStringFree(xmstr);
return;
}

/* --Methode-- */
void PIFileChooserX::SetFileName(string const& fname)
{
XmString xmstr;
xmstr = XmStringLtoRCreate((char *)fname.c_str(), XmSTRING_DEFAULT_CHARSET);
Arg warg[2];
XtSetArg(warg[0], XmNtextString, xmstr); 
XtSetValues(XtWdg(), warg, 1);
XmStringFree(xmstr);
return;
}


/* --Methode-- */
string PIFileChooserX::GetFileName()
{

if (mSt)
  {
  XmString xmstr;
  Arg warg[2];

  XtSetArg(warg[0], XmNtextString, &xmstr);
  XtGetValues(XtWdg(), warg, 1);
  CStrFrXmStr(xmstr, mFName, 512); 
  string s(mFName);
  XmStringFree(xmstr);
  return(s);
  }
else
  {
  mFName[0] = '\0';
  string s(mFName);
  return(s);
  }
}

/* --Methode-- */
string PIFileChooserX::GetPath()
{
printf("PIFileChooserX::GetPath() not implemented ! \n");
string s("");
return(s);
}

