using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Forms; using System.Security; using ADE; namespace MsgBoxInterface2 { [ComVisible(true)] public class UICallbacks : ADE.IAdeUICallbacks, ADE.IAdeUIWindowCallbacks { public int MsgBox(string title, string text, int buttons) { MessageBoxButtons btns = (MessageBoxButtons)(buttons & 0x7); MessageBoxIcon icon = (MessageBoxIcon)(buttons & 0xfffffff0); DialogResult res = MessageBox.Show(text, title, btns, icon); return (int)res; } public bool ShowProgressBar(string title, string text, double p) { Console.WriteLine("ShowProgressBar(title:\"" + title + "\",text:\"" + text + "\", p:" + p + ")"); return false; // not cancelled } public void HideProgressBar() { Console.WriteLine("HideProgressBar()"); } private void PrintOptions(Array optionsList) { bool bComma = false; foreach (string opt in optionsList) { if (bComma) Console.Write(','); bComma = true; Console.Write("\"" + opt + "\""); } } public int AskMsgChoice(string question, string title, [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BSTR)] Array optionList, int defaultPosition, bool bShowAll, out bool bCancel) { Console.Write("callback AskMsgChoice(question:\"" + question + "\", title:\"" + title + "\", options: ["); PrintOptions(optionList); Console.WriteLine("], pos:" + defaultPosition + ", bShowAll:" + bShowAll); Console.WriteLine(" - selecting 2nd item"); bCancel = false; int res = 2; return res; } public string AskMsgComboBox(string question, string title, [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BSTR)] Array optionList, string defaultText, bool bShowAll, out bool bCancel) { Console.Write("callback AskMsgComboBox(question:\"" + question + "\", title:\"" + title + "\", options: ["); PrintOptions(optionList); Console.WriteLine("], defaulttext:\"" + defaultText + "\", bShowAll:" + bShowAll); Console.WriteLine(" - returning 'ten'"); bCancel = false; return "ten"; } public double AskMsgNumber(string question, string title, bool bShowDefault, double defaultNumber, out bool bCancel) { Console.Write("callback AskMsgNumber(question:\"" + question + "\", title:\"" + title + "\""); if (bShowDefault) Console.Write(", default:" + defaultNumber); Console.WriteLine(")"); bCancel = false; return 12.3456; } public string AskMsgText(string question, string title, int maxText, string defaultText, bool bPassword, out bool bCancel) { Console.WriteLine("callback AskMsgText(question:\"" + question + "\",title:\"" + title + "\",maxText:" + maxText + ", default:\"" + defaultText + "\", bPassword:" + bPassword); bCancel = false; return "two"; } public string GetFilename(string function, string defaultName, string directory, string extensions) { Console.WriteLine("called GetFileName(\"" + function + "\", \"" + defaultName + "\", \"" + directory + "\", \"" + extensions + "\""); if (function == "SpreadsheetOpen" || function == "OpenExcelFile") { Console.WriteLine(@" - returning: C:\Temp\Book1.xlsx"); return @"C:\Temp\Book1.xlsx"; } else if (function == "ReadExportFile") { Console.WriteLine(@" - returning: C:\Temp\try.export"); return @"C:\Temp\try.export"; } else { Console.WriteLine(@" - returning: C:\Temp\try.txt"); return @"C:\Temp\try.txt"; } } public void FileOpenCompleted(string function, string fullFilePath) { Console.WriteLine("called FileOpenCompleted(\"" + function + "\",\"" + fullFilePath + "\")"); } public void ShowPdfFile(string filename, string bookmark, bool bSearch, bool bBookmarks) { Console.WriteLine("callback ShowPdfFile(\"" + filename + "\", bookmark:\"" + bookmark + "\", bSearch:" + bSearch + ", bBookmarks:" + bBookmarks); } public string GetFilename(string function, string defaultName, string directory, string extensions, string caption, CAObject callingIdent) { throw new NotImplementedException(); } public void OpenURL(string url, string window, string specs) { throw new NotImplementedException(); } // LDC 12/1/2015 ER 15553 public void CloseWindow(string objIdent, string windowType) { Console.WriteLine("callback CloseWindow(" + objIdent + ", \"" + windowType + "\")"); } public void ShowWindow(string objIdent, string windowType) { Console.WriteLine("callback ShowWindow(" + objIdent + ", \"" + windowType + "\")"); } }; class Program { UICallbacks cb_ = new UICallbacks(); // Hang onto one copy here to ensure Garbage Collection doesn't clean it up. CAEngine ade_; void PrintResult(object res, string desc) { if (ade_.ErrorCode != 0) { Console.WriteLine("Error code: " + ade_.ErrorCode + " ( " + ade_.ErrorText + " )"); Console.WriteLine(ade_.OutputBuffer); } else { Console.WriteLine("Result of " + desc + ":" + res.ToString()); } } void TestMsgBox() { CAObject x = ade_.CreateObject("x", "Variable"); for (int i = 0; i < 10; ++i) { string txt = (i % 2) == 0 ? "Hello" : "There"; x.SetAttribute("definition", "MsgBox('" + txt + "'," + (i % 6 + (i % 5) * 16).ToString() + ")"); Console.Write("Should spawn MsgBox. Result = "); object result = x.Result(); Console.WriteLine(result); Console.WriteLine("Output Buffer is"); Console.WriteLine(ade_.OutputBuffer); } Marshal.ReleaseComObject(x); } void TestSpreadsheetOpen() { CAObject wb = ade_.CreateObject("wb", "Variable"); wb.SetAttribute("Definition", "SpreadsheetOpen(\"data.xls\",showDialog:true)"); Console.WriteLine("\nEvaluating wb to test SpreadsheetOpen()"); PrintResult(wb.Result(), "SpreadsheetOpen:"); Marshal.ReleaseComObject(wb); } void TestReadTextFile() { CAObject obj = ade_.CreateObject("txt", "Variable"); obj.SetAttribute("Definition", "ReadTextFile('textFile.txt',showDialog:true)"); Console.WriteLine("\nEvaluating ReadTextFile():"); obj.RenderingStyle.NewLineAsCRLF = true; PrintResult(obj.Result(), "ReadTextFile"); Marshal.ReleaseComObject(obj); } void TestReadExportFile() { CAObject obj = ade_.CreateObject("data", "Variable"); obj.SetAttribute("Definition", "ReadExportFile('exportFile.txt',showDialog:true)"); Console.WriteLine("\nEvaluating ReadExportFile():"); obj.RenderingStyle.NewLineAsCRLF = true; PrintResult(obj.Result(), "ReadExportFile"); Marshal.ReleaseComObject(obj); } void TestProgressBar() { CAObject obj = ade_.CreateObject("lengthy_compute", "Variable"); obj.SetAttribute("Definition", "For i:=1..10 do ShowProgressBar('myTitle','myText',i/10)"); Console.WriteLine("\nEvaluating ShowProgressBar():"); obj.RenderingStyle.NewLineAsCRLF = true; PrintResult(obj.Result(), "ShowProgressBar()"); Marshal.ReleaseComObject(obj); } void TestAskFunctions() { CAObject objText = ade_.CreateObject("test_AskMsgText", "Variable"); CAObject objNumber = ade_.CreateObject("test_AskMsgNumber", "Variable"); CAObject objChoice = ade_.CreateObject("test_AskMsgChoice", "Variable"); CAObject objCombo = ade_.CreateObject("test_AskMsgCombo", "Variable"); CAObject indOpts = ade_.CreateObject("opts", "Index"); CAObject objPdfFile = ade_.CreateObject("pdfFile", "Variable"); objText.SetAttribute("Definition", "AskMsgText('Enter some text','myCaption',default:'initial text')"); objNumber.SetAttribute("Definition", "AskMsgNumber('Enter number','asking #')"); indOpts.SetAttribute("Definition", "['one','two','three','four']"); objChoice.SetAttribute("Definition", "AskMsgChoice('select an item','Testing choice',Opts)"); objCombo.SetAttribute("Definition", "AskMsgChoice('enter the name of a number','Testing combo',Opts,combobox:true)"); objPdfFile.SetAttribute("Definition", "ShowPdfFile('myPdfFile.pdf')"); Console.WriteLine("\nTesting AskMsgText():"); PrintResult(objText.Result(), "AskMsgText()"); Console.WriteLine("\nTesting AskMsgNumber() without default:"); PrintResult(objNumber.Result(), "AskMsgNumber()"); Console.WriteLine("\nTesting AskMsgNumber() with default:"); objNumber.SetAttribute("Definition", "AskMsgNumber('Enter number','asking #',default:-23.4)"); PrintResult(objNumber.Result(), "AskMsgNumber()"); Console.WriteLine("\nTesting AskMsgChoice()"); PrintResult(objChoice.Result(), "AskMsgChoice()"); Console.WriteLine("\nTesting AskMsgChoice(..., comboBox:true)"); PrintResult(objCombo.Result(), "AskMsgChoice(...,combobox:true)"); Console.WriteLine("\nTesting ShowPdfFile():"); PrintResult(objPdfFile.Result(), "ShowPdfFile()"); Marshal.ReleaseComObject(objPdfFile); Marshal.ReleaseComObject(indOpts); Marshal.ReleaseComObject(objCombo); Marshal.ReleaseComObject(objChoice); Marshal.ReleaseComObject(objNumber); Marshal.ReleaseComObject(objText); } void TestShowCloseWindow() { ade_.SendCommand("Show Diagram Test_callbacks"); ade_.SendCommand("ShowWindow(SysLib_Root,'Object')"); ade_.SendCommand("Close Object SysLib_Root"); ade_.SendCommand("CloseWindow(Test_callbacks,'Diagram')"); ade_.SendCommand("ShowWindow(Time,\"Graph\")"); ade_.SendCommand("ShowWindow(Time,'Table')"); ade_.SendCommand("ShowWindow(SampleWeighting,'Result')"); CAObject inputTab = ade_.CreateObject("inputTab", "Variable"); inputTab.SetAttribute("Definition", "Table(Time)"); ade_.SendCommand("ShowWindow(inputTab,'EditTable')"); ade_.SendCommand("Show Definition inputTab"); } void Run() { ade_ = new CAEngine(); ade_.MonitorProcess(System.Diagnostics.Process.GetCurrentProcess().Id); ade_.SetCallbackObject(cb_); ade_.CreateModel("Test_Callbacks"); //TestMsgBox(); //TestSpreadsheetOpen(); //TestReadTextFile(); //TestReadExportFile(); //TestProgressBar(); //TestAskFunctions(); TestShowCloseWindow(); Marshal.ReleaseComObject(ade_); } static void Main(string[] args) { Program program = new Program(); program.Run(); } } }