/******************************************************************************
 GNBTreeDir.cc

	<Description>

	BASE CLASS = public JXDocument

	Copyright © 1998 by Glenn W. Bach.  All rights reserved.
	
	Base code generated by Codemill v0.1.0

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

// includes

//Class Header
#include <GNBTreeDir.h>
#include <JTree.h>
#include <GNBRootNode.h>
#include <GNBTreeWidget.h>
#include <JNamedTreeList.h>
#include <GNBGlobals.h>
#include <GNBApp.h>
#include <GNBPrefsMgr.h>
#include "GNBHelpText.h"

#include <notebook.xpm>
#include <GNBTreeIcon.xpm>


#include <JXToolBar.h>

#include <JXHelpManager.h>
#include <JXWindow.h>
#include <JXTextMenu.h>
#include <JXMenuBar.h>
#include <JXScrollbarSet.h>
#include <JXApplication.h>
#include <JXTextButton.h>
#include <JXDocumentManager.h>
#include <JXDocumentMenu.h>
#include <JXImage.h>

#include <JChooseSaveFile.h>
#include <JString.h>
#include <JFileArray.h>

#include <jXCSFIcons.h>

#include <jAssert.h>

const JCoordinate kCloseButtonWidth 	= 50;
const JCoordinate kCurrentPrefsVersion	= 0;

const JCharacter* kTreeWindowMenuTitleStr = "Windows";

static const JCharacter* kHelpMenuTitleStr = "Help";
static const JCharacter* kHelpMenuStr = 
	"About"
	"%l|Table of Contents|Getting started |This window"
	"%l|Changes|Credits";

enum
{	
	kAboutCmd = 1,
	kTOCCmd,
	kOverviewCmd,
	kThisWindowCmd,
	kChangesCmd,
	kCreditsCmd
};

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

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

GNBTreeDir::GNBTreeDir
	(
	JXDirector* supervisor
	)
	:
   JXDocument(supervisor),
   itsTreeChanged(kFalse)
{
	BuildWindow();
}

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

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

GNBTreeDir::~GNBTreeDir()
{
	itsWidget->Save();
	delete itsTree;
	delete itsIcon;
}

/******************************************************************************
 BuildWindow 


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

void
GNBTreeDir::BuildWindow()
{
	JCoordinate w = 300;
	JCoordinate h = 500;
	JXWindow* window = new JXWindow(this, w,h, "Notebook");
    assert( window != NULL );
    SetWindow(window);
	window->SetCloseAction(JXWindow::kQuitApp);
    window->PlaceAsDialogWindow();
    window->LockCurrentMinSize();
    
	JXMenuBar* menuBar =
		new JXMenuBar(window, JXWidget::kHElastic, JXWidget::kFixedTop, 
			0,0, w,kJXStdMenuBarHeight);
	assert( menuBar != NULL );

	JXToolBar* toolBar =
		new JXToolBar(GNBGetPrefsMgr(), kGTreeToolBarID,
			menuBar, 300, 500,
			window, JXWidget::kHElastic, JXWidget::kVElastic, 
			0,kJXStdMenuBarHeight, w,h - kJXStdMenuBarHeight);
	assert( toolBar != NULL );

	JSize newHeight = toolBar->GetWidgetEnclosure()->GetBoundsHeight();
	
	JXScrollbarSet* scrollbarSet =
		new JXScrollbarSet(toolBar->GetWidgetEnclosure(), 
						   JXWidget::kHElastic,JXWidget::kVElastic,
						   0, 0,
						   w, newHeight);
	assert( scrollbarSet != NULL );
    
    GNBRootNode* base = new GNBRootNode(itsTree, "");
    assert(base != NULL);
    itsTree = new JTree(base);
    assert(itsTree != NULL);
    JNamedTreeList* list = new JNamedTreeList(itsTree);
    
    itsWidget = 
    	GNBTreeWidget::Create(itsTree, list, menuBar, toolBar, 
    		scrollbarSet, scrollbarSet->GetScrollEnclosure(),
    		JXWidget::kHElastic, JXWidget::kVElastic,
    		0,0,w,newHeight);
    assert(itsWidget != NULL);
    ListenTo(itsWidget);
    
	JXDocumentMenu* fileListMenu =
		new JXDocumentMenu(kTreeWindowMenuTitleStr, menuBar,
						   JXWidget::kFixedLeft, JXWidget::kVElastic, 0,0, 10,10);
	assert( fileListMenu != NULL );
	menuBar->AppendMenu(fileListMenu);

	itsIcon = new JXImage(GetDisplay(), GetColormap(), JXPM(notebook));
	assert( itsIcon != NULL );
	window->SetIcon(*itsIcon);

	itsHelpMenu = menuBar->AppendTextMenu(kHelpMenuTitleStr);
	itsHelpMenu->SetMenuItems(kHelpMenuStr);
	itsHelpMenu->SetUpdateAction(JXMenu::kDisableNone);
	ListenTo(itsHelpMenu);
}

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


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

void
GNBTreeDir::Receive
	(
	JBroadcaster*	sender,
	const Message&	message
	)
{
	if (sender == itsWidget && message.Is(GNBTreeWidgetBase::kDataChanged))
		{
		DataModified();
		}
	else if (sender == itsHelpMenu && message.Is(JXMenu::kItemSelected))
		{
		const JXMenu::ItemSelected* selection =
			dynamic_cast(const JXMenu::ItemSelected*, &message);
		assert( selection != NULL );
		HandleHelpMenu(selection->GetIndex());
		}
	else
		{
		JXDocument::Receive(sender, message);
		}
}

/******************************************************************************
 OKToClose

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

JBoolean
GNBTreeDir::OKToClose()
{
	return kTrue;	
}

/******************************************************************************
 NeedsSave

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

JBoolean
GNBTreeDir::NeedsSave()
	const
{
	return itsTreeChanged;
}

/******************************************************************************
 SafetySave

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

void
GNBTreeDir::SafetySave
	(
	const JXDocumentManager::SafetySaveReason reason
	)
{
	JBoolean tellApp = kFalse;
	if (reason == JXDocumentManager::kTimer)
		{
		tellApp = kTrue;
		}
	itsWidget->Save();
	DataReverted();
	if (tellApp)
		{
		GNBGetApplication()->NeedsSave();
		}
}

/******************************************************************************
 DiscardChanges

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

void
GNBTreeDir::DiscardChanges()
{

}

/******************************************************************************
 GetMenuIcon (virtual)

	Override of JXDocument::GetMenuIcon().

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

JXPM
GNBTreeDir::GetMenuIcon()
	const
{
	return JXPM(GNBTreeIcon);
}

/******************************************************************************
 DataModified (public)

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

void
GNBTreeDir::DataModified()
{
	itsTreeChanged = kTrue;
}

/******************************************************************************
 DataReverted (public)

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

void
GNBTreeDir::DataReverted()
{
	itsTreeChanged = kFalse;
}

/******************************************************************************
 HandleHelpMenu

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

void  
GNBTreeDir::HandleHelpMenu
	(
	const JIndex index
	)
{
	if (index == kAboutCmd)
		{
		GNBGetApplication()->DisplayAbout();
		}
	else if (index == kTOCCmd)
		{
		(JXGetHelpManager())->ShowSection(kTOCHelpName);
		}
	else if (index == kOverviewCmd)
		{
		(JXGetHelpManager())->ShowSection(kGettingStartedHelpName);
		}
	else if (index == kThisWindowCmd)
		{
		(JXGetHelpManager())->ShowSection(kMainHelpName);
		}
	else if (index == kChangesCmd)
		{
		(JXGetHelpManager())->ShowSection(kChangeLogName);
		}
	else if (index == kCreditsCmd)
		{
		(JXGetHelpManager())->ShowSection(kCreditsName);
		}
}
