/*
	Copyright (c) 2024 PTC Inc. and/or Its Subsidiary Companies. All Rights Reserved.
*/


#include <ProToolkit.h>
#include <ProUI.h>
#include <ProUIDialog.h>
#include <ProUIPushbutton.h>
#include <ProUITable.h>
#include <ProUIOptionmenu.h>

#define UGUITABLEDLG "uguitableexample"

/*====================================================================*\
FUNCTION: UserTableDialogCloseAction
PURPOSE:  Close action for "Close" button in table examples
\*====================================================================*/
static void UserTableDialogCloseAction (char* dialog, char* button, ProAppData data)
{
	ProUIDialogExit (UGUITABLEDLG, PRO_TK_NO_ERROR);
}

/*====================================================================*\
FUNCTION: UserUITableRedesignExample 
PURPOSE:  Change the table columns and rows before displaying the dialog
\*====================================================================*/
int UserUITableRedesignExample ()
 {
     int status;
	 int column_count;
	 wchar_t** column_labels;
	 char** column_names;

	 int new_column_count = 3;
	 char* new_column_names [] = {"Column A", "Column B", "Column C"};
	 wchar_t* new_column_labels [] = {L"First", L"Second", L"Third"};

/*--------------------------------------------------------------------*\ 
    Load the dialog from the resource file 
\*--------------------------------------------------------------------*/ 
    ProUIDialogCreate(UGUITABLEDLG, UGUITABLEDLG);

/*--------------------------------------------------------------------*\ 
    Set the Close button actions 
\*--------------------------------------------------------------------*/ 
    ProUIPushbuttonActivateActionSet(UGUITABLEDLG,"CloseButton",
										UserTableDialogCloseAction, NULL);

/*--------------------------------------------------------------------*\
	Get the list of column names and labels
\*--------------------------------------------------------------------*/
    ProUITableColumnnamesGet (UGUITABLEDLG, "LargeTable", &column_count, 
								&column_names);
	ProUITableColumnlabelsGet (UGUITABLEDLG, "LargeTable", &column_count,
								&column_labels);

/*--------------------------------------------------------------------*\
	Assign the column names to the rows
\*--------------------------------------------------------------------*/
	ProUITableRownamesSet (UGUITABLEDLG, "LargeTable", column_count,
								column_names);
	ProUITableRowlabelsSet (UGUITABLEDLG, "LargeTable", column_count,
								column_labels);

/*--------------------------------------------------------------------*\
	Free the retrieved arrays
\*--------------------------------------------------------------------*/
	ProStringarrayFree (column_names, column_count);
	ProWstringarrayFree (column_labels, column_count);

/*--------------------------------------------------------------------*\
	Modify the column names and labels
\*--------------------------------------------------------------------*/
	ProUITableColumnnamesSet (UGUITABLEDLG, "LargeTable", new_column_count,
								new_column_names);
	ProUITableColumnlabelsSet (UGUITABLEDLG, "LargeTable", new_column_count,
								new_column_labels);

/*--------------------------------------------------------------------*\ 
    Display and activate the dialog 
\*--------------------------------------------------------------------*/ 
    ProUIDialogActivate(UGUITABLEDLG, &status);

/*--------------------------------------------------------------------*\ 
    Remove the dialog from memory 
\*--------------------------------------------------------------------*/ 
    ProUIDialogDestroy(UGUITABLEDLG);

    return(1);
 }

/*====================================================================*\
FUNCTION: UserTableDialogSelectionsToggle
PURPOSE:  UI callback function to toggle contents of table cells
\*====================================================================*/
static void UserTableDialogSelectionsToggle (char* dialog, char* button, ProAppData data)
{
	char** selections;
	int selection_count;
	wchar_t* label;
	int i;

	ProUITableSelectednamesGet (UGUITABLEDLG, "LargeTable", 
								&selection_count, &selections);

/*--------------------------------------------------------------------*\
	Selected names are arranged in sets of 2 ("Row", "Column")
\*--------------------------------------------------------------------*/
	for (i = 0; i < selection_count; i = i + 2)
	{
		ProUITableCellLabelGet (UGUITABLEDLG, "LargeTable", 
								selections [i], 
								selections [i+1], &label);

		if (label != NULL && ProUtilWstrcmp (label, L"ON") == 0)
		{
			ProUITableCellLabelSet (UGUITABLEDLG, "LargeTable", 
								selections [i], 
								selections [i+1], L"OFF");
		}
		else
		{
			ProUITableCellLabelSet (UGUITABLEDLG, "LargeTable", 
								selections [i], 
								selections [i+1], L"ON");
		}
		if (label != NULL)
			ProWstringFree (label);
	}

	ProStringarrayFree (selections, selection_count);

}

/*====================================================================*\
FUNCTION: UserUITableSelectionExample 
PURPOSE:  Change the table columns and rows before displaying the dialog
\*====================================================================*/
int UserUITableSelectionExample ()
 {
     int status;
	
/*--------------------------------------------------------------------*\ 
    Load the dialog from the resource file 
\*--------------------------------------------------------------------*/ 
    ProUIDialogCreate(UGUITABLEDLG, UGUITABLEDLG);

/*--------------------------------------------------------------------*\ 
    Set the Close button actions 
\*--------------------------------------------------------------------*/ 
    ProUIPushbuttonActivateActionSet(UGUITABLEDLG,"CloseButton",
										UserTableDialogCloseAction, 
										NULL);

/*--------------------------------------------------------------------*\
	Set up the Modify Select button
\*--------------------------------------------------------------------*/
	ProUIPushbuttonShow (UGUITABLEDLG, "ModifySelectButton");
	ProUIPushbuttonActivateActionSet(UGUITABLEDLG,"ModifySelectButton",
										UserTableDialogSelectionsToggle, 
										NULL);

/*--------------------------------------------------------------------*\
	Set the selection policy to multiple
\*--------------------------------------------------------------------*/
    ProUITableSelectionpolicySet (UGUITABLEDLG, "LargeTable", 
									PROUISELPOLICY_MULTIPLE);

/*--------------------------------------------------------------------*\ 
    Display and activate the dialog 
\*--------------------------------------------------------------------*/ 
    ProUIDialogActivate(UGUITABLEDLG, &status);

/*--------------------------------------------------------------------*\ 
    Remove the dialog from memory 
\*--------------------------------------------------------------------*/ 
    ProUIDialogDestroy(UGUITABLEDLG);

    return(1);
 }

/*====================================================================*\
FUNCTION: UserUITableComponentExample 
PURPOSE:  Add components to the table cells
\*====================================================================*/
int UserUITableComponentExample ()
 {
     int status;
	
/*--------------------------------------------------------------------*\ 
    Load the dialog from the resource file 
\*--------------------------------------------------------------------*/ 
    ProUIDialogCreate(UGUITABLEDLG, UGUITABLEDLG);

/*--------------------------------------------------------------------*\ 
    Set the Close button actions 
\*--------------------------------------------------------------------*/ 
    ProUIPushbuttonActivateActionSet(UGUITABLEDLG,"CloseButton",
										UserTableDialogCloseAction, 
										NULL);

/*--------------------------------------------------------------------*\
	Assign a predefined table components from the table layout
\*--------------------------------------------------------------------*/
	ProUITableCellComponentNameSet (UGUITABLEDLG, "LargeTable",
									"A", "1",
									"BaseButton");

/*--------------------------------------------------------------------*\
	Copy a table component into a table cell
\*--------------------------------------------------------------------*/
	ProUITableCellComponentCopy (UGUITABLEDLG, "LargeTable", 
									"C", "3",
									UGUITABLEDLG, "BaseCheckButton",
									"CheckButtonC3");

	ProUICheckbuttonSet (UGUITABLEDLG, "CheckButtonC3");

/*--------------------------------------------------------------------*\
	Copy a non-table component into a table & assign it to a table cell
\*--------------------------------------------------------------------*/
	ProUITableComponentCopy (UGUITABLEDLG, "LargeTable", UGUITABLEDLG, 
									"ToCopy",
									"CopyOfOptionMenu");

	ProUITableCellComponentNameSet (UGUITABLEDLG, "LargeTable", 
									"B", "2",
									"CopyOfOptionMenu");

	ProUIOptionmenuShow (UGUITABLEDLG, "CopyOfOptionMenu");

/*--------------------------------------------------------------------*\
	Copy a component from another dialog and assign it to a table cell
\*--------------------------------------------------------------------*/
	ProUIDialogCreate ("uguitablecomponents", "uguitablecomponents");

	ProUITableCellComponentCopy (UGUITABLEDLG, "LargeTable", 
									"D", "4",
									"uguitablecomponents", 
									"ExternalButtonToCopy",
									"CopyOfExternalButton");

	ProUIDialogDestroy ("uguitablecomponents");

/*--------------------------------------------------------------------*\ 
    Display and activate the dialog 
\*--------------------------------------------------------------------*/ 
    ProUIDialogActivate(UGUITABLEDLG, &status);

/*--------------------------------------------------------------------*\ 
    Remove the dialog from memory 
\*--------------------------------------------------------------------*/ 
    ProUIDialogDestroy(UGUITABLEDLG);

    return(1);
 }