//********************************************
// Modeless dialog simple with checking column
// LABPP 2022
//********************************************

#include "items.h"
// 
string sProcName = "Modeless dialog simple with checking column";

int main()
{
	cout << sProcName << "\n";

	int iarg1; double darg2; string sarg3;
	run_cpp("get_args",iarg1, darg2, sarg3);

	if (sarg3 == "Get PCG item name")
	{
		var_extern_set("PCG item name", sProcName);
		return 0;
	}


	if (sarg3 == "Help")
	{
		tsalert(-3, "Information", "",
			"");
		return 0;
	}

//	string genesis, v_type, varname, svalue, sdescription, sgroup;
//	programcodegen("get_bimmaster_cur_selected_parameter_info", genesis, v_type, varname, svalue, sdescription, sgroup);

	string prg_code_gen;
	string CheckItems20221207="MDCheck20221207";

	int res = ac_request("editstringdialog", "Enter Dialog Suffix Name", CheckItems20221207);
	if (res == 1)
	{
		CheckItems20221207 = ac_getstrvalue();
	}

	prg_code_gen = "// " +sProcName+ " -------------------------------->\n\
int iDialog_"+CheckItems20221207+";\n\
int iListBox_"+CheckItems20221207+", iButton_Ok_"+CheckItems20221207+";\n\
int iTable_"+CheckItems20221207+";\n\
\n\
int main()\n\
{\n\
	int ires;\n\
	ac_request(\"clearmessagewindow\");\n\
	object(\"create\", \"ts_table\", iTable_"+CheckItems20221207+");\n\
\n\
	ts_table(iTable_"+CheckItems20221207+", \"add_column\", -1, \"string\", \"Column 1\");\n\
	ts_table(iTable_"+CheckItems20221207+", \"add_column\", -1, \"int\", \"Column 2\");\n\
	ts_table(iTable_"+CheckItems20221207+", \"add_column\", -1, \"double\", \"Column 3\");\n\
	ts_table(iTable_"+CheckItems20221207+", \"add_column\", -1, \"bool\", \"Checked\");\n\
	ts_table(iTable_"+CheckItems20221207+", \"add_column\", -1, \"string\", \"Column 4\");\n\
	//object(\"KeepInMemory\", iTable_"+CheckItems20221207+");\n\
	var_extern_set(\"iTable_"+CheckItems20221207+"\", iTable_"+CheckItems20221207+");\n\
\n\
	int i, icount = 20;\n\
	for (i = 0; i < icount; i++)\n\
	{\n\
		ts_table(iTable_"+CheckItems20221207+", \"add_row\", 0, \"Value \" + itoa(i), \"Column 2\", i, \"Column 3\", i * 1.01,\n\
			\"Column 4\", itoa(i), \"Checked\", false);\n\
	}\n\
\n\
	string str;\n\
	ts_table(iTable_"+CheckItems20221207+", \"print_to_str\", str);\n\
	coutvar << str;\n\
\n\
	// Create Dialog\n\
	int x, y, w, h, offset;\n\
\n\
	object(\"create\", \"ts_dialog\", iDialog_"+CheckItems20221207+");\n\
	ts_dialog(iDialog_"+CheckItems20221207+", \"init_dialog\", \"palette\", 0, 0, 350, 400);\n\
	ts_dialog(iDialog_"+CheckItems20221207+", \"eventreaction\", \"Event_PanelCloseRequested\", \"Event_PanelCloseRequested_"+CheckItems20221207+"\");\n\
	ts_dialog(iDialog_"+CheckItems20221207+", \"set_as_main_panel\");\n\
	ts_dialog(iDialog_"+CheckItems20221207+", \"SetGrowType\", 3);\n\
\n\
	ts_dialog(iDialog_"+CheckItems20221207+", \"SetTitle\", \"The Dialog Header\");\n\
\n\
	object(\"create\", \"ts_dialogcontrol\", iListBox_"+CheckItems20221207+", \"iListBox_"+CheckItems20221207+"\");\n\
\n\
	ts_dialogcontrol(iListBox_"+CheckItems20221207+", \"init_control\", \"singlesellistbox\", iDialog_"+CheckItems20221207+", 5, 5, 340, 350, 48, 20);\n\
	ts_dialogcontrol(iListBox_"+CheckItems20221207+", \"eventreaction\", \"Event_ListBoxClicked\", \"Event_ListBoxClicked_"+CheckItems20221207+"\");\n\
	ts_dialogcontrol(iListBox_"+CheckItems20221207+", \"SetAnchorToPanelResize\", 0, 0, 1, 1);\n\
\n\
	ts_table(iTable_"+CheckItems20221207+", \"set_columns_width_when_export\", 100);\n\
\n\
	ts_table(iTable_"+CheckItems20221207+", \"mapping_columns_to_export\",\n\
		-1, \"Empty column\", \"\",\n\
		\"Column 1\", \"Header 2\", \"\",\n\
		\"Column 3\", \"Header 3\", \"\",\n\
		\"Column 4\", \"Header 4\", \"\",\n\
		-1, \"Checked\", \"\",\n\
		\"Column 2\", \"Column 2\", \"\"\n\
		);\n\
\n\
	int iconid;\n\
\n\
	ts_table(iTable_"+CheckItems20221207+", \"export_to_dialogcontrol\", iListBox_"+CheckItems20221207+", -1, -1);\n\
	ts_dialogcontrol(iListBox_"+CheckItems20221207+", \"getitemcount\", icount);\n\
\n\
	bool bChecked;\n\
	for (i = 0; i < icount; i++)\n\
	{\n\
		ts_table(iTable_"+CheckItems20221207+", \"select_row\", i);\n\
		ts_table(iTable_"+CheckItems20221207+", \"get_value_of\", \"Checked\", bChecked);\n\
		if (bChecked) {\n\
			iconid = 32001; // icon code for checked. unchecked - 32000\n\
		}\n\
		else {\n\
			iconid = 32000; // icon code for checked. unchecked - 32000\n\
		}\n\
		ts_dialogcontrol(iListBox_"+CheckItems20221207+", \"SetTabItemIconId\", i + 1, 5, iconid);\n\
	}\n\
\n\
	ts_dialogcontrol(iListBox_"+CheckItems20221207+", \"SetTabFieldBEPosition\", 1, 0, 30);\n\
	ts_dialogcontrol(iListBox_"+CheckItems20221207+", \"SetTabFieldBEPosition\", 2, 30, 60);\n\
	ts_dialogcontrol(iListBox_"+CheckItems20221207+", \"SetTabFieldBEPosition\", 3, 60, 130);\n\
	ts_dialogcontrol(iListBox_"+CheckItems20221207+", \"SetTabFieldBEPosition\", 4, 130, 260);\n\
	ts_dialogcontrol(iListBox_"+CheckItems20221207+", \"SetTabFieldBEPosition\", 5, 260, 320);\n\
	ts_dialogcontrol(iListBox_"+CheckItems20221207+", \"SetTabFieldBEPosition\", 6, 320, 390);\n\
\n\
	ts_dialogcontrol(iListBox_"+CheckItems20221207+", \"EnableSeparatorLines\", 1);\n\
	ts_dialogcontrol(iListBox_"+CheckItems20221207+", \"RepaintBackgroundItemsByColumnValue\", 3, 255, 255, 255, 250, 250, 250);\n\
\n\
	x = 10;\n\
	w = 98;\n\
	int delta = 10;\n\
	x = x + w + delta;\n\
	y = 370;\n\
	h = 20;\n\
\n\
	x = x + w + delta;\n\
	object(\"create\", \"ts_dialogcontrol\", iButton_Ok_"+CheckItems20221207+", \"iButton_Ok_"+CheckItems20221207+"\");\n\
	ts_dialogcontrol(iButton_Ok_"+CheckItems20221207+", \"init_control\", \"button\", iDialog_"+CheckItems20221207+", x, y, w, h);\n\
	ts_dialogcontrol(iButton_Ok_"+CheckItems20221207+", \"eventreaction\", \"Event_ButtonClicked\", \"Event_ButtonClicked_"+CheckItems20221207+"\");\n\
	ts_dialogcontrol(iButton_Ok_"+CheckItems20221207+", \"settext\", \"Ok\");\n\
	ts_dialogcontrol(iButton_Ok_"+CheckItems20221207+", \"SetAnchorToPanelResize\", 1, 1, 0, 0);\n\
\n\
	bool bres;\n\
	ts_dialog(iDialog_"+CheckItems20221207+", \"invoke\", bres);\n\
\n\
	return 0;\n\
}\n\
\n\
int Event_PanelCloseRequested_"+CheckItems20221207+"(int iDescr, string sDescr)\n\
{\n\
	int ires = tsalert(-3, \"Message\", \"Are you sure you want to close the panel?\", \"\", \"Ok\", \"Cancel\");\n\
	if (ires == 1)\n\
	{\n\
		return 0;    // Allow panel to close;\n\
	}\n\
	else\n\
	{\n\
		return -1;   // No close panel\n\
	}\n\
}\n\
\n\
int Event_ListBoxClicked_"+CheckItems20221207+"(int iDescr, string sDescr)\n\
{\n\
	if (sDescr == \"iListBox_"+CheckItems20221207+"\") { switch_onofficon(iListBox_"+CheckItems20221207+", 5); }\n\
}\n\
int Event_ButtonClicked_"+CheckItems20221207+"(int iDescr, string sDescr)\n\
{\n\
	if (sDescr == \"iButton_Ok_"+CheckItems20221207+"\") {\n\
		int i, icount, iconid;\n\
		bool bChecked;\n\
		ts_dialogcontrol(iListBox_"+CheckItems20221207+", \"getitemcount\", icount);\n\
		for (i = 0; i < icount; i++)\n\
		{\n\
			ts_table(iTable_"+CheckItems20221207+", \"select_row\", i); \n\
			ts_dialogcontrol(iListBox_"+CheckItems20221207+", \"GetTabItemIconId\", i + 1, 5, iconid); \n\
			if (iconid == 32001) {\n\
				bChecked = true;\n\
			}\n\
			else {\n\
				bChecked = false;\n\
			}\n\
			ts_table(iTable_"+CheckItems20221207+", \"set_value_of\", \"Checked\", bChecked);\n\
		}\n\
		string str;\n\
		ts_table(iTable_"+CheckItems20221207+", \"print_to_str\", str);\n\
		coutvar << str;\n\
		ts_dialog(iDialog_"+CheckItems20221207+", \"SendCloseRequest\");\n\
	}\n\
}\n\
int switch_onofficon(int iListBoxWhereDo, int iColumnWhereDo)\n\
{\n\
	int selecteditem;\n\
	int iconid;\n\
	int mousex, mousey;\n\
	int sx, ex;\n\
	ts_dialogcontrol(iListBoxWhereDo, \"GetMouseClickedPosXY\", mousex, mousey);\n\
	ts_dialogcontrol(iListBoxWhereDo, \"GetTabFieldPosition\", iColumnWhereDo, sx, ex);\n\
	ex = sx + 22;\n\
	if ((mousex > sx) && (mousex < ex))\n\
	{\n\
		ts_dialogcontrol(iListBoxWhereDo, \"GetSelectedItem\", selecteditem);\n\
		if (selecteditem != 0)\n\
		{\n\
			ts_dialogcontrol(iListBoxWhereDo, \"GetTabItemIconId\", selecteditem, iColumnWhereDo, iconid);\n\
			if (iconid == 32000) { ts_dialogcontrol(iListBoxWhereDo, \"SetTabItemIconId\", selecteditem, iColumnWhereDo, 32001); }\n\
			else { ts_dialogcontrol(iListBoxWhereDo, \"SetTabItemIconId\", selecteditem, iColumnWhereDo, 32000); }\n\
		}\n\
	}\n\
}";

	var_extern_set("PCG item result", prg_code_gen);
}

//<--------
