view FrmConfig.cs @ 0:8439bec53421

Initial commit under the GPLv3
author IBBoard <dev@ibboard.co.uk>
date Sat, 06 Oct 2018 20:00:56 +0100
parents
children
line wrap: on
line source

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

using IBBoard;

namespace IBBoard.Relic.IChunkyViewer
{
	/// <summary>
	/// Summary description for FrmConfig.
	/// </summary>
	public class FrmConfig : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label lblDoWPath;
		private System.Windows.Forms.TextBox txtDoWPath;
		private System.Windows.Forms.Button bttnDoWPath;
		private System.Windows.Forms.Button bttnOK;
		private System.Windows.Forms.Button bttnCancel;
		private System.Windows.Forms.FolderBrowserDialog fbdPath;
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.Button bttnSave;
		private ArrayList changed;
		private System.Windows.Forms.RadioButton rbSizeNone;
		private System.Windows.Forms.RadioButton rbSizeContent;
		private System.Windows.Forms.RadioButton rbSizeChunk;
		private System.Windows.Forms.Label lblShowSizes;
		private System.Windows.Forms.ToolTip toolTip;

		private bool saved = false;
		private Preferences preferences;

		public FrmConfig(Preferences prefs)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			preferences = prefs;
            changed = new ArrayList();
		}

		/// <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.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FrmConfig));
			this.lblDoWPath = new System.Windows.Forms.Label();
			this.fbdPath = new System.Windows.Forms.FolderBrowserDialog();
			this.txtDoWPath = new System.Windows.Forms.TextBox();
			this.bttnDoWPath = new System.Windows.Forms.Button();
			this.bttnOK = new System.Windows.Forms.Button();
			this.bttnCancel = new System.Windows.Forms.Button();
			this.lblShowSizes = new System.Windows.Forms.Label();
			this.rbSizeNone = new System.Windows.Forms.RadioButton();
			this.rbSizeContent = new System.Windows.Forms.RadioButton();
			this.bttnSave = new System.Windows.Forms.Button();
			this.rbSizeChunk = new System.Windows.Forms.RadioButton();
			this.toolTip = new System.Windows.Forms.ToolTip(this.components);
			this.SuspendLayout();
			// 
			// lblDoWPath
			// 
			this.lblDoWPath.Location = new System.Drawing.Point(8, 8);
			this.lblDoWPath.Name = "lblDoWPath";
			this.lblDoWPath.TabIndex = 0;
			this.lblDoWPath.Text = "Game Path:";
			// 
			// txtDoWPath
			// 
			this.txtDoWPath.Location = new System.Drawing.Point(112, 8);
			this.txtDoWPath.Name = "txtDoWPath";
			this.txtDoWPath.Size = new System.Drawing.Size(288, 20);
			this.txtDoWPath.TabIndex = 2;
			this.txtDoWPath.Tag = "DoWPath";
			this.txtDoWPath.Text = "";
			this.txtDoWPath.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textbox_KeyDown);
			this.txtDoWPath.TextChanged += new System.EventHandler(this.textbox_TextChanged);
			this.txtDoWPath.Leave += new System.EventHandler(this.textbox_Leave);
			// 
			// bttnDoWPath
			// 
			this.bttnDoWPath.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.bttnDoWPath.Location = new System.Drawing.Point(408, 8);
			this.bttnDoWPath.Name = "bttnDoWPath";
			this.bttnDoWPath.TabIndex = 3;
			this.bttnDoWPath.Text = "Browse";
			this.bttnDoWPath.Click += new System.EventHandler(this.bttnDoWPath_Click);
			// 
			// bttnOK
			// 
			this.bttnOK.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.bttnOK.Location = new System.Drawing.Point(408, 184);
			this.bttnOK.Name = "bttnOK";
			this.bttnOK.TabIndex = 10;
			this.bttnOK.Text = "OK";
			this.bttnOK.Click += new System.EventHandler(this.bttnOK_Click);
			// 
			// bttnCancel
			// 
			this.bttnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.bttnCancel.Location = new System.Drawing.Point(320, 184);
			this.bttnCancel.Name = "bttnCancel";
			this.bttnCancel.TabIndex = 1;
			this.bttnCancel.Text = "Cancel";
			this.bttnCancel.Click += new System.EventHandler(this.bttnCancel_Click);
			// 
			// lblShowSizes
			// 
			this.lblShowSizes.Location = new System.Drawing.Point(8, 40);
			this.lblShowSizes.Name = "lblShowSizes";
			this.lblShowSizes.TabIndex = 14;
			this.lblShowSizes.Text = "Show sizes:";
			this.lblShowSizes.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// rbSizeNone
			// 
			this.rbSizeNone.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.rbSizeNone.Location = new System.Drawing.Point(112, 40);
			this.rbSizeNone.Name = "rbSizeNone";
			this.rbSizeNone.Size = new System.Drawing.Size(72, 24);
			this.rbSizeNone.TabIndex = 6;
			this.rbSizeNone.Text = "None";
			this.toolTip.SetToolTip(this.rbSizeNone, "Do not show chunk sizes");
			this.rbSizeNone.CheckedChanged += new System.EventHandler(this.radiobutton_CheckedChanged);
			// 
			// rbSizeContent
			// 
			this.rbSizeContent.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.rbSizeContent.Location = new System.Drawing.Point(192, 40);
			this.rbSizeContent.Name = "rbSizeContent";
			this.rbSizeContent.TabIndex = 7;
			this.rbSizeContent.Text = "Chunk Contents";
			this.toolTip.SetToolTip(this.rbSizeContent, "Show size of data in chunk (like Relic\'s Chunky Viewer)");
			this.rbSizeContent.CheckedChanged += new System.EventHandler(this.radiobutton_CheckedChanged);
			// 
			// bttnSave
			// 
			this.bttnSave.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.bttnSave.Location = new System.Drawing.Point(8, 184);
			this.bttnSave.Name = "bttnSave";
			this.bttnSave.TabIndex = 9;
			this.bttnSave.Text = "Save";
			this.bttnSave.Click += new System.EventHandler(this.bttnSave_Click);
			// 
			// rbSizeChunk
			// 
			this.rbSizeChunk.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.rbSizeChunk.Location = new System.Drawing.Point(304, 40);
			this.rbSizeChunk.Name = "rbSizeChunk";
			this.rbSizeChunk.TabIndex = 8;
			this.rbSizeChunk.Text = "Chunk Size";
			this.toolTip.SetToolTip(this.rbSizeChunk, "Show size of data in chunk plus chunk headers");
			this.rbSizeChunk.CheckedChanged += new System.EventHandler(this.radiobutton_CheckedChanged);
			// 
			// FrmConfig
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(496, 214);
			this.Controls.Add(this.rbSizeChunk);
			this.Controls.Add(this.bttnSave);
			this.Controls.Add(this.rbSizeContent);
			this.Controls.Add(this.rbSizeNone);
			this.Controls.Add(this.lblShowSizes);
			this.Controls.Add(this.txtDoWPath);
			this.Controls.Add(this.bttnCancel);
			this.Controls.Add(this.bttnOK);
			this.Controls.Add(this.bttnDoWPath);
			this.Controls.Add(this.lblDoWPath);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "FrmConfig";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Configuration";
			this.Load += new System.EventHandler(this.FrmConfig_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void bttnCancel_Click(object sender, System.EventArgs e)
		{
			if (!saved)
			{
				this.DialogResult = DialogResult.Cancel;
			}

			this.Close();
			this.Dispose();
		}

		private void bttnOK_Click(object sender, System.EventArgs e)
		{
			if (setValues())
			{
				this.DialogResult = DialogResult.OK;				
				this.Close();
			}
		}

		private void FrmConfig_Load(object sender, System.EventArgs e)
		{
            txtDoWPath.Text = preferences["DoWPath"].ToString();

            int size = (int)preferences["ShowChunkSizes"];
			rbSizeNone.Checked = (size!=1 && size!=2);
			rbSizeContent.Checked = (size==1);
			rbSizeChunk.Checked = (size==2);
			bttnSave.Enabled = false;
		}

		private void bttnDoWPath_Click(object sender, System.EventArgs e)
		{
			fbdPath.ShowNewFolderButton = false;
			fbdPath.SelectedPath = txtDoWPath.Text;
			DialogResult dr = fbdPath.ShowDialog();
			if (dr == DialogResult.OK)
			{
				txtDoWPath.Text = fbdPath.SelectedPath;
			}
		}

		private void bttnSave_Click(object sender, System.EventArgs e)
		{
			saved = true; //Save but don't close
			setValues();
            preferences.Save();
			bttnSave.Enabled = false;
		}

		private void textbox_TextChanged(object sender, System.EventArgs e)
		{
			if (sender is TextBox)
			{
				TextBox box = (TextBox)sender;
                string prefString = preferences[box.Tag.ToString()].ToString();

				if (box.Text != prefString && box.Text+Constants.DirectoryString != prefString)
				{
					if (!changed.Contains(box))
					{
						changed.Add(box);
					}
				}
				else if (changed.Contains(box))
				{
					changed.Remove(box);
				}

				if (box==txtDoWPath)
				{
					if (!validPath())
					{
						box.ForeColor = Color.Red;
					}
					else
					{
						box.ForeColor = SystemColors.WindowText;
					}
				}
				
				setSaveButtonEnabled();
			}
		}

		private void radiobutton_CheckedChanged(object sender, System.EventArgs e)
		{
			if (sender is RadioButton)
			{
				if (((RadioButton)sender).Checked)
				{
                    int showValue = (int)preferences["ShowChunkSizes"];

					if (sender==rbSizeNone)
					{
                        if (showValue == 1 || showValue == 2)
						{
							if (!changed.Contains(lblShowSizes))
							{
								changed.Add(lblShowSizes);
							}
						}
						else
						{
							changed.Remove(lblShowSizes);
						}
					}
					else if (sender==rbSizeContent)
					{
                        if (showValue != 1)
						{
							if (!changed.Contains(lblShowSizes))
							{
								changed.Add(lblShowSizes);
							}
						}
						else
						{
							changed.Remove(lblShowSizes);
						}
					}
					else if (sender==rbSizeChunk)
					{
                        if (showValue != 2)
						{
							if (!changed.Contains(lblShowSizes))
							{
								changed.Add(lblShowSizes);
							}
						}
						else
						{
							changed.Remove(lblShowSizes);
						}
					}
				}

				setSaveButtonEnabled();
			}
		}

		private void textbox_Leave(object sender, System.EventArgs e)
		{	
			if (sender is TextBox)
			{
				TextBox txt = (TextBox)sender;

				if (txt.Text!="" && !txt.Text.EndsWith(Constants.DirectoryString))
					txt.Text = txt.Text + Constants.DirectoryString;
			}		
		}

		private void textbox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (sender is TextBox)
			{
				if(e.KeyData == (Keys.Control|Keys.A) && e.Control)
				{
					((TextBox)sender).SelectAll();
				}
			}
		}

		private bool validPath()
		{
			string path = txtDoWPath.Text;

			if (path=="")
			{
				return false;
			}

			if (!path.EndsWith(Constants.DirectoryString))
			{
				path+=Constants.DirectoryString;
			}

			return Directory.Exists(path) && (File.Exists(path+"W40k.exe") || File.Exists(path+"DarkCrusade.exe"));
		}

		private bool setValues()
		{
            //TODO: Propogate refresh after setting so that the GUI knows even if we save, change one value then cancel (which would return DialogResult.Cancel)
			if (!validPath())
			{
				MessageBox.Show(this, "Could not find the Dawn of War exe in the 'Game Path' specified. Please make sure you specified the correct folder.","Dawn of War Missing", MessageBoxButtons.OK, MessageBoxIcon.Error); 
				return false;
			}

            preferences["DoWPath"] = txtDoWPath.Text;

			if (rbSizeNone.Checked)
			{
                preferences["ShowChunkSizes"] = 0;
			}
			else if (rbSizeContent.Checked)
			{
                preferences["ShowChunkSizes"] = 1;
			}
			else if (rbSizeChunk.Checked)
			{
                preferences["ShowChunkSizes"] = 2;
			}

			changed.Clear();

			return true;
		}

		private void setSaveButtonEnabled()
		{
			bttnSave.Enabled = changed.Count>0;
		}
	}
}