using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MyNamespace
{
	/// <summary>
	///		Startup form for XPCalc - Extra Precision Floating-Point Calculator
	///		Developed in TURBO Pascal 5.5 and 6.0, converted to Visual Basic 6
	///		and now in Visual C# .Net
	///
	/// ---------------- *** LEGAL NOTICE: *** ------------------
	///
	/// This program is free software; you can redistribute it and/or modify
	/// it under the terms of the GNU General Public License (GPL) as published
	/// by the Free Software Foundation. cf. the file COPYING.txt.
	///
	/// ------------ *** end of legal notice *** ------------------
	/// </summary>

	public class StartupForm : Form
	{
		private Button buttonDefault;
		private Button buttonExit;
		private Button buttonRun;
		private Label labelMaxCommands;
		private Label labelMaxDigits;
		private RichTextBox richTextIntro;
		private System.ComponentModel.IContainer components;
		private TextBox textBoxMaxCommands;
		private TextBox textBoxMaxDigits;
		private Timer timerStartup;

		public StartupForm()
		{
			InitializeComponent();
			MultiID.muDMax = 8;			// max number of decimal digits in a super digit
			MultiID.muNMax = 16777300;	// max number of super digits in a multi-precision
			// number. Allows for 134,218,400 = 2^27 + 672 decimal digits
										// each super digit is from 0 to 99,999,999, i.e. base = 10^8
			maxDigits = MultiID.muDMax * MultiID.muNMax;
			defaultDigits = maxDigits;
			maxCommands = 100000; // [1, 100000] default = 1000
			defaultCommands = 1000;
			SetDefault();
			this.Text = Global.title + " - Startup";
			Pr("");
			Pr("   " + Global.name1);
			Pr("   " + Global.name2);
			Pr("   " + Global.version);
			Pr("   " + Global.author);
			Pr("   " + Global.address);
			Pr("");
			Pr("XPCalc is a calculator program with the following features:");
			Pr("");
			Pr("     Extra precision to millions of decimal places");
			Pr("     Uses Fast Hartley Transform to speed-up long multiplies");
			Pr("     Separate input and output notation, base 2 to base 36");
			Pr("     Efficient Transcendental functions");
			Pr("     Hyperbolic functions");
			Pr("     Greatest Common Divisor function");
			Pr("     Modulo arithmetic options");
			Pr("     Bernoulli numbers, Euler numbers, Euler's constant");
			Pr("     Boolean functions, Gamma and Psi functions");
			Pr("     Random number generator");
			Pr("     Can compute Pi to 67.1 million decimal digits");
			Pr("     Algebraic notation (not reverse polish)");
			Pr("     If-Then-Else statement");
			Pr("     Goto statement");
			Pr("     Can run XPCalc code from files");
			Pr("     Hot Help from F1 key");
			Pr("     Written in Microsoft Visual C# .NET 2005 for Windows");
			Pr("     Recommended by Dr. Clifford A. Pickover's in his books");
			Pr("     COMPUTERS AND THE IMAGINATION page 380, and");
			Pr("     MAZES FOR THE MIND pages 388 and 412.");
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(StartupForm));
			this.richTextIntro = new RichTextBox();
			this.buttonRun = new Button();
			this.buttonExit = new Button();
			this.labelMaxDigits = new Label();
			this.labelMaxCommands = new Label();
			this.textBoxMaxDigits = new TextBox();
			this.textBoxMaxCommands = new TextBox();
			this.buttonDefault = new Button();
			this.timerStartup = new Timer(this.components);
			this.SuspendLayout();
			// 
			// richTextIntro
			// 
			this.richTextIntro.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(208)))), ((int)(((byte)(244)))), ((int)(((byte)(255)))));
			this.richTextIntro.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.richTextIntro.ForeColor = System.Drawing.Color.Blue;
			this.richTextIntro.Location = new System.Drawing.Point(0, -8);
			this.richTextIntro.Name = "richTextIntro";
			this.richTextIntro.ReadOnly = true;
			this.richTextIntro.ScrollBars = RichTextBoxScrollBars.None;
			this.richTextIntro.Size = new System.Drawing.Size(464, 480);
			this.richTextIntro.TabIndex = 7;
			this.richTextIntro.Text = "";
			this.richTextIntro.KeyDown += new KeyEventHandler(this.buttonRun_KeyDown);
			// 
			// buttonRun
			// 
			this.buttonRun.Cursor = Cursors.Arrow;
			this.buttonRun.Location = new System.Drawing.Point(480, 432);
			this.buttonRun.Name = "buttonRun";
			this.buttonRun.Size = new System.Drawing.Size(40, 24);
			this.buttonRun.TabIndex = 0;
			this.buttonRun.Text = "Run";
			this.buttonRun.Click += new System.EventHandler(this.buttonRun_Click);
			this.buttonRun.KeyDown += new KeyEventHandler(this.buttonRun_KeyDown);
			// 
			// buttonExit
			// 
			this.buttonExit.Cursor = Cursors.Arrow;
			this.buttonExit.Location = new System.Drawing.Point(536, 432);
			this.buttonExit.Name = "buttonExit";
			this.buttonExit.Size = new System.Drawing.Size(40, 23);
			this.buttonExit.TabIndex = 1;
			this.buttonExit.Text = "Exit";
			this.buttonExit.Click += new System.EventHandler(this.buttonExit_Click);
			this.buttonExit.KeyDown += new KeyEventHandler(this.buttonRun_KeyDown);
			// 
			// labelMaxDigits
			// 
			this.labelMaxDigits.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
			this.labelMaxDigits.Location = new System.Drawing.Point(472, 88);
			this.labelMaxDigits.Name = "labelMaxDigits";
			this.labelMaxDigits.Size = new System.Drawing.Size(128, 16);
			this.labelMaxDigits.TabIndex = 2;
			this.labelMaxDigits.Text = "Max Digits in a Number";
			// 
			// labelMaxCommands
			// 
			this.labelMaxCommands.Location = new System.Drawing.Point(472, 192);
			this.labelMaxCommands.Name = "labelMaxCommands";
			this.labelMaxCommands.Size = new System.Drawing.Size(144, 16);
			this.labelMaxCommands.TabIndex = 3;
			this.labelMaxCommands.Text = "Max Commands in History";
			// 
			// textBoxMaxDigits
			// 
			this.textBoxMaxDigits.Cursor = Cursors.IBeam;
			this.textBoxMaxDigits.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.textBoxMaxDigits.Location = new System.Drawing.Point(496, 104);
			this.textBoxMaxDigits.Name = "textBoxMaxDigits";
			this.textBoxMaxDigits.Size = new System.Drawing.Size(72, 20);
			this.textBoxMaxDigits.TabIndex = 4;
			this.textBoxMaxDigits.Text = "33554600";
			this.textBoxMaxDigits.TextChanged += new System.EventHandler(this.textBoxMaxDigits_TextChanged);
			this.textBoxMaxDigits.KeyDown += new KeyEventHandler(this.buttonRun_KeyDown);
			// 
			// textBoxMaxCommands
			// 
			this.textBoxMaxCommands.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.textBoxMaxCommands.Location = new System.Drawing.Point(496, 208);
			this.textBoxMaxCommands.Name = "textBoxMaxCommands";
			this.textBoxMaxCommands.Size = new System.Drawing.Size(72, 20);
			this.textBoxMaxCommands.TabIndex = 5;
			this.textBoxMaxCommands.Text = "100000";
			this.textBoxMaxCommands.TextChanged += new System.EventHandler(this.textBoxMaxCommands_TextChanged);
			this.textBoxMaxCommands.KeyDown += new KeyEventHandler(this.buttonRun_KeyDown);
			// 
			// buttonDefault
			// 
			this.buttonDefault.Location = new System.Drawing.Point(480, 280);
			this.buttonDefault.Name = "buttonDefault";
			this.buttonDefault.Size = new System.Drawing.Size(104, 24);
			this.buttonDefault.TabIndex = 6;
			this.buttonDefault.Text = "Default Settings";
			this.buttonDefault.Click += new System.EventHandler(this.buttonDefault_Click);
			this.buttonDefault.KeyDown += new KeyEventHandler(this.buttonRun_KeyDown);
			// 
			// timerStartup
			// 
			this.timerStartup.Enabled = true;
			this.timerStartup.Tick += new System.EventHandler(this.timerStartup_Tick);
			// 
			// StartupForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.SystemColors.Control;
			this.ClientSize = new System.Drawing.Size(608, 470);
			this.Controls.Add(this.buttonDefault);
			this.Controls.Add(this.textBoxMaxCommands);
			this.Controls.Add(this.textBoxMaxDigits);
			this.Controls.Add(this.labelMaxCommands);
			this.Controls.Add(this.labelMaxDigits);
			this.Controls.Add(this.buttonExit);
			this.Controls.Add(this.buttonRun);
			this.Controls.Add(this.richTextIntro);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "StartupForm";
			this.StartPosition = FormStartPosition.CenterScreen;
			this.Text = "<App.Title> - Startup";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.memberFormClosing);
			this.KeyDown += new KeyEventHandler(this.buttonRun_KeyDown);
			this.ResumeLayout(false);
			this.PerformLayout();

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args) // get command line arguments and bring up the frame
		{
			string st;
			new Global();	// initialize class Global
			for (int i = 0; i < args.Length; i++)
			{
				st = args[i];
				if (i == 0)
				{
					// reconstruct first arg for XPCalc to interpret
					// arg one may be @"C:\xxx xxx\xx\xx.XPC" or ... . XPN with the "s stripped off
					// commands like Run("xx.XPC") on the command line will not work because the "s are stripped off
					// this worked in Visual Basic because the args are input as a single string
					if ((st.Length > 2 && st.Substring(1, 2) == @":\") || st.IndexOf(' ') >= 0)
						st = "\"" + st + "\""; // put the "s back on
				}
				else
				{
					if (st.IndexOf(' ') >= 0) st = "\"" + st + "\""; // put the "s back on
				}
				Global.command += " " + st;
			}
			st = ""; // release memory
			Global.parmsDone = Global.command == "";
			StartupForm objStartupForm = new StartupForm();
			if (!Global.parmsDone) objStartupForm.buttonRunCalc();
			startupTicks = 1;
			if (!objStartupForm.IsDisposed) Application.Run(objStartupForm);
		}

		private void Pr(string st)
		{
			richTextIntro.Text += "   " + st + "\n";
		}

		private void textBoxMaxDigits_TextChanged(object sender, System.EventArgs e)
		// max digits in a number [1, maxDigits]
		{
			string sNum = textBoxMaxDigits.Text.TrimStart(' ');	// remove leading spaces
			textBoxMaxDigits.Text = sNum;						// from text box
			if (Global.IsInt32(sNum))
			{
				int iNum = System.Convert.ToInt32(sNum);
				bMaxDigits = (1 <= iNum) && (iNum <= maxDigits);
			}
			else
			{
				bMaxDigits = false;
			}
			int n = sNum.Length;
			if (n > 0 && sNum[n-1] == ' ') // remove space at end
			{
				sNum = sNum.TrimEnd(' ');
				textBoxMaxDigits.Text = sNum;
				textBoxMaxDigits.SelectionStart = sNum.Length; // stay at end
			}
			SetFonts();
		}

		private void textBoxMaxCommands_TextChanged(object sender, System.EventArgs e)
		// max commands in history [1, maxCommands]
		{
			string sNum = textBoxMaxCommands.Text.TrimStart(' ');	// remove leading spaces
			textBoxMaxCommands.Text = sNum;							// from text box
			if (Global.IsInt32(sNum))
			{
				int iNum = System.Convert.ToInt32(sNum);
				bMaxCommands = (1 <= iNum) && (iNum <= maxCommands);
			}
			else
			{
				bMaxCommands = false;
			}
			int n = sNum.Length;
			if (n > 0 && sNum[n-1] == ' ') // remove space at end
			{
				sNum = sNum.TrimEnd(' ');
				textBoxMaxCommands.Text = sNum;
				textBoxMaxCommands.SelectionStart = sNum.Length; // stay at end
			}
			SetFonts();
		}

		private void buttonDefault_Click(object sender, System.EventArgs e)
		{
			SetDefault();
		}

		private void SetDefault()
		{
			textBoxMaxDigits.Text = defaultDigits.ToString();
			textBoxMaxCommands.Text = defaultCommands.ToString();
			buttonRun.Enabled = bMaxDigits = bMaxCommands = true;
		}

		private void buttonRun_Click(object sender, System.EventArgs e)
			// start running the calculator program
		{
			buttonRunCalc();
		}

		private void buttonRunCalc() // start running the calculator program
		{
			int iMaxDigits = System.Convert.ToInt32(textBoxMaxDigits.Text);
			int iMaxCommands = System.Convert.ToInt32(textBoxMaxCommands.Text);
			this.Hide();
			RunForm objRunForm = new RunForm();
			objRunForm.Show();
			objRunForm.Run(iMaxDigits, iMaxCommands, this); // call sub Run in the form frmRun
		}

		private void SetFonts()
		{
			if (bMaxDigits)
				this.textBoxMaxDigits.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold);
			else
				this.textBoxMaxDigits.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular);
			if (bMaxCommands)
				this.textBoxMaxCommands.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold);
			else
				this.textBoxMaxCommands.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular);
			buttonRun.Enabled = bMaxDigits && bMaxCommands;
		}

		public void TotalQuit()
		{
			Dispose(true);
		}

		private void buttonExit_Click(object sender, System.EventArgs e)
		{
			Dispose(true);
		}

		private void buttonRun_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)		// ESC key pressed so Exit Startup
			{
				Dispose(true);
			}
		}

		private void timerStartup_Tick(object sender, System.EventArgs e)
		{
			if (startupTicks > 0) startupTicks++;
			if (Global.runFormUp) this.Hide();	// this is only needed when there command line arguments are input
			if (startupTicks > 100)
				timerStartup.Enabled = false;	// and then only needed once
		}

		private void memberFormClosing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			Dispose(true);
		}

		private static bool bMaxCommands;		// maxCommands good flag
		private static bool bMaxDigits;			// maxDigits good flag
		private static int defaultCommands;		// maxCommands default
		private static int defaultDigits;		// maxDigits default
		private static int maxCommands;			// maxCommands allowed
		private static int maxDigits;			// maxDigits allowed
		private static int startupTicks = 0;	// startupTicks helps implement the requirement that the
												// StartupForm is bypassed if command line arguments are present
	}
}
