/******************************************************************************
 GNBTrashWidget.cc

	<Description>

	BASE CLASS = public JXNamedTreeListWidget

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

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

#include <GNBTrashWidget.h>
#include <GNBTreeDir.h>
#include <GNBCategoryNode.h>
#include <GNBNoteNode.h>
#include <GNBGlobals.h>
#include "GNBApp.h"

#include <JTree.h>
#include <JNamedTreeList.h>
#include <JNamedTreeNode.h>

#include <JXMenuBar.h>
#include <JXTextMenu.h>
#include <JString.h>
#include <JXWindow.h>
#include <JXWindowPainter.h>
#include <JXDirector.h>
#include <JXSelectionManager.h>
#include <JXUserNotification.h>
#include <JXApplication.h>
#include <JXDNDManager.h>
#include <JXChooseSaveFile.h>
#include <JXImage.h>
#include <JXDisplay.h>
#include <JXColormap.h>

#include <JFontManager.h>
#include <JTableSelection.h>
#include <JTableSelectionIterator.h>
#include <JOrderedSetIterator.h>
#include <JUNIXDirInfo.h>
#include <JUNIXDirEntry.h>
#include <JError.h>
#include <JStaticBuffer.h>

#include <jDirUtil.h>
#include <jFileUtil.h>
#include <jProcessUtil.h>
#include <jASCIIConstants.h>
#include <jStrStreamUtil.h>
#include <jStreamUtil.h>
#include <jErrno.h>
#include <jMath.h>

#include <X11/keysym.h>
#include <strstream.h>
#include <iostream.h>
#include <fstream.h>
#include <jAssert.h>

static const JCharacter* kGNBMenuTitleStr = "Discards";
static const JCharacter* kGNBMenuStr = 
	"Edit tool bar... %i \"EditToolBar::Notebook\""
	"%l|Flush discards %i \"FlushDiscards::Notebook\""
	"%l|Close %k Meta-W %i \"CloseTrash::Notebook\"";

enum
{
	kEditToolBarCmd = 1,
	kFlushCmd,
	kCloseCmd
};

static const JIndex kGNBTrashTreePrefsIndex = 2;

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

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

GNBTrashWidget*
GNBTrashWidget::Create
	(
	JTree*				tree,
	JNamedTreeList* 	treeList, 
	JXMenuBar* 			menuBar,
	JXScrollbarSet* 	scrollbarSet, 
	JXContainer* 		enclosure,
	const HSizingOption	hSizing, 
	const VSizingOption vSizing,
	const JCoordinate 	x, 
	const JCoordinate 	y,
	const JCoordinate 	w, 
	const JCoordinate 	h
	)
{
	GNBTrashWidget* widget = 
		new GNBTrashWidget(tree, treeList, menuBar,
   					  scrollbarSet,	enclosure, hSizing, vSizing, x, y, w, h);
   	assert(widget != NULL);

	widget->JXTreeListWidgetX();

	widget->ReadSetup();
	
	return widget;
}

GNBTrashWidget::GNBTrashWidget
	(
	JTree*				tree,
	JNamedTreeList* 	treeList, 
	JXMenuBar* 			menuBar,
	JXScrollbarSet* 	scrollbarSet, 
	JXContainer* 		enclosure,
	const HSizingOption	hSizing, 
	const VSizingOption vSizing,
	const JCoordinate 	x, 
	const JCoordinate 	y,
	const JCoordinate 	w, 
	const JCoordinate 	h
	)
	:
   GNBTreeWidgetBase(kGNBTrashTreePrefsIndex, tree, treeList, 
   					  scrollbarSet,	enclosure, hSizing, vSizing, x, y, w, h)
{
	itsNBMenu = menuBar->AppendTextMenu(kGNBMenuTitleStr);
	itsNBMenu->SetMenuItems(kGNBMenuStr);
	itsNBMenu->SetUpdateAction(JXMenu::kDisableNone);
	ListenTo(itsNBMenu);
}

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

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

GNBTrashWidget::~GNBTrashWidget()
{
}

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


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

void
GNBTrashWidget::Receive
	(
	JBroadcaster*	sender,
	const Message&	message
	)
{
	if (sender == itsNBMenu && message.Is(JXMenu::kItemSelected))
		{
		 const JXMenu::ItemSelected* selection =
			dynamic_cast(const JXMenu::ItemSelected*, &message);
		assert( selection != NULL );
		HandleNBMenu(selection->GetIndex());
		}
	else if (sender == itsNBMenu && message.Is(JXMenu::kNeedsUpdate))
		{
		UpdateNBMenu();
		}
	else
		{
		GNBTreeWidgetBase::Receive(sender, message);
		}
}

/******************************************************************************
 UpdateNBMenu 


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

void
GNBTrashWidget::UpdateNBMenu()
{
	itsNBMenu->EnableItem(kFlushCmd);
	itsNBMenu->EnableItem(kCloseCmd);
}

/******************************************************************************
 HandleNBMenu 


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

void
GNBTrashWidget::HandleNBMenu
	(
	const JIndex index
	)
{
	if (index == kFlushCmd)
		{
		FlushDiscards();
		}
	else if (index == kCloseCmd)
		{
		GetWindow()->GetDirector()->Deactivate();
		}
}

/******************************************************************************
 FlushDiscards (public)

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

void
GNBTrashWidget::FlushDiscards()
{
	JTreeNode* base = GetTreeList()->GetTree()->GetRoot();
	GNBBaseNode* node = dynamic_cast(GNBBaseNode*, base);
	assert(node != NULL);
	node->DiscardBranch();
	SelectedFlushed();
}
