/******************************************************************************
 SyGTreeDir.cc

	BASE CLASS = JXWindowDirector

	Copyright © 1997 by Glenn Bach. All rights reserved.

 ******************************************************************************/

#include "SyGSet.h"
#include "SyGTreeDir.h"
#include "SyGApplication.h"
#include "SyGGlobals.h"
#include "SyGPrefsMgr.h"
#include "folder.xpm"

#include <JXScrollbarSet.h>
#include <JXWindow.h>
#include <JXImage.h>
#include <JXDisplay.h>

#include <JString.h>

#include <jFileUtil.h>
#include <jDirUtil.h>

#include <fstream.h>
#include <strstream.h>
#include <iostream.h>
#include <iomanip.h>
#include <jAssert.h>

const JCharacter* kSyGWMInstance	= "systemg";
const JCharacter* kSyGWMClass 		= "SystemG_File_Tree_Window";
const JCharacter* kDirPrefsName		= ".systemG.Desktop";

const JFileVersion kCurrentFileVersion	= 1;

/******************************************************************************
 Constructor

 ******************************************************************************/

SyGTreeDir::SyGTreeDir
	(
	JXDirector* supervisor,
	const JString& startPath,
	JBoolean isMain, 
	JCoordinate width, 
	JCoordinate height,
	JCoordinate locX, 
	JCoordinate locY, 
	JCoordinate partWidth
	)
	:
	JXWindowDirector(supervisor)
{
	itsIsMain = isMain;
	BuildWindow(startPath, isMain, width, height, locX, locY, partWidth);
}

/******************************************************************************
 Destructor

 ******************************************************************************/

SyGTreeDir::~SyGTreeDir()
{
	delete itsWindowIcon;
}

/******************************************************************************
 BuildWindow (private)

 ******************************************************************************/

void
SyGTreeDir::BuildWindow
	(
	const JString& startPath,
	JBoolean isMain,
	JCoordinate width, 
	JCoordinate height, 
	JCoordinate locX, 
	JCoordinate locY, 
	JCoordinate partWidth
	)
{
	JString dTFile = startPath;
	JAppendDirSeparator(&dTFile);
	dTFile += kDirPrefsName;
	ifstream is(dTFile);
	
   	JXWindow* window = new JXWindow(this, width,height, "");
    assert( window != NULL );
    SetWindow(window);

	JFileVersion version;
	if (!is.fail())
		{
		is >> version;
		window->ReadGeometry(is);
		}
	else
		{
		JSize w;
		JSize h;
		SyGGetPrefsMgr()->GetDefaultWindowSize(&w, &h);
		window->SetSize(w,h);
		}
   	window->SetCloseAction(JXWindow::kCloseDirector);
   	window->SetWMClass(kSyGWMInstance, kSyGWMClass);

	itsFMSet = 
		new SyGSet(is, startPath, isMain, partWidth, window, 
					JXWidget::kHElastic, JXWidget::kVElastic, 0,0, 400,300);
					
	assert (itsFMSet != NULL);

	JString file = startPath;
	JString path;
	JString name;
	if ((file.GetLastCharacter() == '/') && (file != "/"))
		{
		file.RemoveSubstring(file.GetLength(), file.GetLength());
		}
	if (file != "/")
		{
		JSplitPathAndName(file, &path, &name);
		}
//	name.Prepend("G: ");
	window->SetTitle(name);
	window->SetMinSize(100,200);
	
	itsFMSet->FitToEnclosure(kTrue, kTrue);
	ListenTo(window);

// This apparently has to be done after SetWMClass

	JXDisplay* display = GetDisplay();
    itsWindowIcon = new JXImage(display, display->GetColormap(), JXPM(folder));
    assert(itsWindowIcon != NULL);
    itsWindowIcon->ConvertToRemoteStorage();
    window->SetIcon(*itsWindowIcon);
    window->SetFocusWhenShow(kTrue);
}

/******************************************************************************
 GetCurrentPath

 ******************************************************************************/

const JString& 
SyGTreeDir::GetCurrentPath()
{
	return itsFMSet->GetCurrentPath();
}

/******************************************************************************
 Close

 ******************************************************************************/

JBoolean 
SyGTreeDir::Close()
{
	JXWindow* window = GetWindow();
	JString dTFile = itsFMSet->GetCurrentPath();
	JAppendDirSeparator(&dTFile);
	dTFile += kDirPrefsName;
	ofstream os(dTFile, ios::out | ios::trunc);
	
	if ( !(os.fail()) )
		{
		os << kCurrentFileVersion << ' ';
		window->WriteGeometry(os);
		itsFMSet->SavePreferences(os);
		}
		
	return JXWindowDirector::Close();
}

/******************************************************************************
 SelectName 

 ******************************************************************************/

void
SyGTreeDir::SelectName
	(
	const JString& selectName
	)
{
	itsFMSet->SelectName(selectName);
}

/******************************************************************************
 Receive (virtual protected)

 ******************************************************************************/

void
SyGTreeDir::Receive
	(
	JBroadcaster*	sender,
	const Message&	message
	)
{
	if (sender == GetWindow())
		{
		if (message.Is(JXWindow::kIconified))
			{
			itsFMSet->WindowIconified();
			}
		else if (message.Is(JXWindow::kDeiconified))
			{
			itsFMSet->WindowDeiconified();
			}
		}
	JXWindowDirector::Receive(sender, message);
}