/////////////////////////////////////////////////////////////////////
//
//      test
//
//                                               author: I. Niessen
//
//        Disclaimer:  
//      
/////////////////////////////////////////////////////////////////////

#include <TGClient.h>
#include <TCanvas.h>
#include <TGButton.h>
#include <TGFrame.h>
#include <RQ_OBJECT.h>
#include <TGMsgBox.h>
#include <TGFileDialog.h>
#include <TRootEmbeddedCanvas.h>
#include <fstream.h>
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

class MyMainFrame {

RQ_OBJECT("MyMainFrame")

private:
  TGMainFrame         *fMain;
  TGListView          *lv ;
  TString filename;
  Bool_t              fClose;

public:
  MyMainFrame(const TGWindow *p,UInt_t w,UInt_t h);
  virtual ~MyMainFrame();
  void readfiles();
};

class TestFileList {

RQ_OBJECT("TestFileList")

protected:
   TGMainFrame      *fMainL;
   TGFileContainer  *fContents;

   virtual void DisplayDirectory(const TString &fname);
  
public: 
   TString filename;
   TestFileList(const TGWindow *p, const TGWindow *main, UInt_t w, UInt_t h);
   virtual ~TestFileList();
   // slots
   virtual void OnDoubleClick(TGLVEntry*,Int_t);
   virtual void DoMenu(Int_t);    
   void DoClose();
   char ReturnFileName();
};

MyMainFrame::MyMainFrame(const TGWindow *p,UInt_t w,UInt_t h)
{

  // Crate Main frame

  fMain = new TGMainFrame(p,w,h);
  fMain->ChangeOptions((fMain->GetOptions() & ~kVerticalFrame) | kHorizontalFrame);

  fL21 = new TGLayoutHints(kLHintsTop , 10, 10, 10, 10);

   TGTextButton *open_file = new TGTextButton(fMain,"&Open file");
   fMain->AddFrame(open_file, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,10,10,3,4));
   open_file->Connect("Clicked()","MyMainFrame",this,"readfiles()");
  
   TGTextButton *exit = new TGTextButton(fMain,"&Exit","gApplication->Terminate(0)");
   fMain->AddFrame(exit, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,10,10,3,4));

   fMain->SetWindowName("Main window");

   fMain->MapSubwindows();

   fMain->Resize(300,200);

   fMain->MapWindow();

}  
 

void MyMainFrame::readfiles()
{
   TestFileList *rf = new TestFileList(gClient->GetRoot(), fMain, 400, 200);  
   cout << "Filename -->>>> " << rf->ReturnFileName() << endl;
   filename = rf->ReturnFileName();
}

MyMainFrame::~MyMainFrame()
{
   fMain->Cleanup();
   delete fMain;
}

void test()
{
   MyMainFrame *mf = new MyMainFrame(gClient->GetRoot(),200,200);
}


TestFileList::TestFileList(const TGWindow *p, const TGWindow *main, UInt_t w, UInt_t h)
{
   // Create transient frame containing a filelist widget.

   TGLayoutHints *lo;

   fMainL = new TGTransientFrame(p, main, w, h);

   TGMenuBar* mb = new TGMenuBar(fMainL);
   lo = new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX, 0, 0, 1, 1);
   fMainL->AddFrame(mb, lo);

   TGPopupMenu *menu = mb->AddPopup("&View");
   menu->AddEntry("&Close",      10);
   menu->Connect("Activated(Int_t)","TestFileList",this,"DoMenu(Int_t)");

   TGListView* lv = new TGListView(fMainL, w, h);
   lo = new TGLayoutHints(kLHintsExpandX | kLHintsExpandY);
   fMainL->AddFrame(lv,lo);

   Pixel_t white;
   gClient->GetColorByName("white",white);
   fContents = new TGFileContainer(lv,kSunkenFrame,white);

   fContents->Connect("DoubleClicked(TGFrame*,Int_t)","TestFileList",this,"OnDoubleClick(TGLVEntry*,Int_t)");

   fMainL->SetWindowName("File List");
   fMainL->MapSubwindows();
   fMainL->MapWindow();
   fContents->SetDefaultHeaders();
   fContents->DisplayDirectory();
   fContents->AddFile("..");        // up level directory
   fContents->Resize();
   fMainL->Resize();
}

TestFileList::~TestFileList()
{
   // dtor.

   delete fContents;
   fMainL->Cleanup();
   delete fMainL;
}

void TestFileList::DoMenu(Int_t mode)
{
   // switch view mode
   if (mode<10) {
      fContents->SetViewMode((EListViewMode)mode);
   } else {
      delete this;
   }
}

void TestFileList::DisplayDirectory(const TString &fname)
{
   // display content of directory

   fContents->SetDefaultHeaders();
   gSystem->ChangeDirectory(fname);
   fContents->ChangeDirectory(fname);
   fContents->DisplayDirectory();
   fContents->AddFile("..");  // up level directory
   fMainL->Resize();
}


void TestFileList::OnDoubleClick(TGLVEntry* f, Int_t btn)
{
   // handle double click

   if (btn!=kButton1) return;

   TString name(f->GetName());
  
   const char* fname = (const char*)f->GetUserData();
  
   if (name.EndsWith(".root")) { 
       filename =  name ;
       fMainL->SendCloseMessage();
   }
   if (name.EndsWith(".txt")) { 
       filename =  name ;
       fMainL->SendCloseMessage();
   } 
   else {
       DisplayDirectory(name);
   }
}

TString TestFileList::ReturnFileName()
{
  return filename ;
}


