#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
//put additional include files here

//put your own support static functions here

/*------------------------------------------------------------------------------*
 * Modification Log:															*
 *	Folger 10/24/09 THROW_ERROR_WHEN_NO_COLUMN_SELECTED_TO_SLIDE_SHOW			*
 *	Sophy 3/25/2010 QA80-15232 SLIDE_SHOW_DISPLAY_ON_SAME_MONITOR_AS_ORIGIN_APP_WIN_SUPPORT
 *------------------------------------------------------------------------------*/

#define STR_COL_SLIDESHOW_REMINDER	"ColSlideShowHint"

#define IS_NUMBER_BETWEEN(num, n1, n2)	(n1 == num || n2 == num || (n1 < num && num < n2) || (n2 < num && num < n1) )

bool _is_plot_dependent_on_wks_col(const DataPlot& dp, const Worksheet& wks, const vector<int>& vnCols)
{
	ASSERT( dp.IsValid() && wks.IsValid() );
	DataRange dr;
	if ( dp.GetDataRange(dr) && dr )
	{
		for (int ii = 0; ii < dr.GetNumRanges(); ++ii)
		{
			Datasheet wksCur;
			int c1, c2;
			if ( dr.GetRange(wksCur, c1, c2, ii) )
			{
				for (int nn = 0; nn < vnCols.GetSize(); ++nn)
				{
					if ( IS_NUMBER_BETWEEN(vnCols[nn], c1, c2) && is_same_layer(wks, wksCur) )
					{
						return true;
					}
				}
			}
		}
	}
	return false;
}

bool _is_graph_slide_hidden(GraphPage& gp)
{
	ASSERT(gp.IsValid());
	Folder fd = gp.GetFolder();
	if (fd)
	{
		DWORD dw = gp.GetStatus(fd);
		if ( dw & PBFS_SLIDE_HIDDEN )
		{
			return true;
		}
	}
	return false;
}

int _get_wks_columns_dependents_pages(const Worksheet& wks, const vector<int>& vnCols, vector<string>& vsPageNames)
{
	vsPageNames.RemoveAll();
	if ( wks )
	{
		get_folder_pages_name(vsPageNames, OPTION_ALL_IN_OPJ);
		const int nPageSize = vsPageNames.GetSize();
		for (int ii = nPageSize-1; ii >= 0; --ii)
		{
			bool bIsDependent = false;
			GraphPage gp( vsPageNames[ii] );
			if ( gp && !_is_graph_slide_hidden(gp) )
			{
				foreach(GraphLayer gl in gp.Layers)
				{
					foreach(DataPlot dp in gl.DataPlots)
					{
						if ( _is_plot_dependent_on_wks_col(dp, wks, vnCols) )
						{
							bIsDependent = true;
							break;
						}
					}
				}
			}
			if ( !bIsDependent )
			{
				vsPageNames.RemoveAt(ii);
			}
		}
	}
	return vsPageNames.GetSize();
}


void colslideshow()
{
	Worksheet wks = Project.ActiveLayer();
	if ( wks )
	{
		vector<int> vnCols;
		if ( wks.GetSelectedColumns(vnCols) && vnCols.GetSize() > 0 )
		{
			vector<string> vsPageNames;
			int nDependentPage = _get_wks_columns_dependents_pages(wks, vnCols, vsPageNames);
			if ( vsPageNames.GetSize() <= 0 )
			{
				warning_msg_box(_L("There is no dependent graph of this column to show!"), false);
				return;
			}
			
			OptionalMessage(STR_COL_SLIDESHOW_REMINDER, MB_OK);
	
			int nTimer = 0;
			///Sophy 3/25/2010 QA80-15232 SLIDE_SHOW_DISPLAY_ON_SAME_MONITOR_AS_ORIGIN_APP_WIN_SUPPORT
			//DWORD dwCntrl = SLIDE_SHOW_TERMINATE | SLIDE_SHOW_INFOLINE_TOP;
			DWORD dwCntrl = SLIDE_SHOW_TERMINATE | SLIDE_SHOW_INFOLINE_TOP | SLIDE_SHOW_FOLLOW_ORIGIN;
			///end SLIDE_SHOW_DISPLAY_ON_SAME_MONITOR_AS_ORIGIN_APP_WIN_SUPPORT
			Project.StartSlideShow(vsPageNames, nTimer, dwCntrl);
		}
		///------ Folger 10/24/09 THROW_ERROR_WHEN_NO_COLUMN_SELECTED_TO_SLIDE_SHOW
		else
		{
			XF_THROW(XFERR_NO_ACTIVE_COLUMN);
		}
		///------ End THROW_ERROR_WHEN_NO_COLUMN_SELECTED_TO_SLIDE_SHOW
	}
}

//nGetNDialog = 0,1,2, corresponding to the XF GetN dropdown, 0 = dialog is not used
//you can change nRet to be one of XFEVT_PROCEED (defualt), XFEVT_ABORT, XFEVT_PROCEED_NO_DLG or XFEVT_PROCEED_WITH_DLG(when nGetNDialog = 0)
//nGetNDialog = -1 when Ok on GetN dialog to allow handler to preocess <auto> etc variables
//dwCntrl is combination of bits
//LTXF_FROM_GUI_MENU, LTXF_FROM_GUI_PROMPT, LTXF_FROM_AUTO_UPDATE, LTXF_FROM_FUNCTION, LTXF_THEME_USED, LTXF_CHANGE_PARAM
void colslideshow_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{

}
