//MyDocument.cs //Addapted from MyDocument.cs by Dr. McVey using System; using System.IO; using System.Drawing; using System.Windows.Forms; using System.Collections.Generic; using Microsoft.Win32; // needed for registry? using System.Text; namespace TrackMaker4 { class MyDocument { //application specific data private int docx;//to bring a location in from form1 private int docy;//to bring a location in from form1 private int docxvals;//number of x vals on grid private int docyvals;//number of y vals on grid private Square[,] docBoard; // "any document" data private bool hasBeenModified = false; private string fileName = ""; private Form host; // the added 'nice' stuff private string defaultFileName = "Untitled"; private string fileExtension = "JW"; private string fileExtensionDesc = "TrainTrack"; private bool registerFileExtension = true; private string filter = "BMV Files (*.bmv) |*.bmv"; private string iconResourceIndex = "bmvIcon2.ico"; /* EVENTS */ public event EventHandler NewDocument; public event EventHandler OpenDocument; public event EventHandler UpdateCaption; //public event EventHandler AddPiece; public MyDocument() { Square[,] docBoard = new Square[docxvals, docyvals];//New board of size for (int i = 0; i < docxvals; i++) { for (int j = 0; j < docyvals; j++) { docBoard[i, j] = new Square(i, j); } } hasBeenModified = false; if (this.UpdateCaption != null) this.UpdateCaption(this, EventArgs.Empty); } public void AddSquare(int x, int y,Square s) { docBoard[x, y] = new Square(s); } public Square [,] DocBoard { set { docBoard = value; } get { return docBoard; } } /* PROPERTIES - generic document */ public bool RegisterFileExtensionWithShell /* SPELLS/MCVEY */ { get { return this.registerFileExtension; } set { this.registerFileExtension = value; } } public bool HasBeenModified { set { hasBeenModified = value; } get { return hasBeenModified; } } public string FileName { set { fileName = value; } get { return fileName; } } public Form Host { set { host = value; } get { return host; } } /* PROPERTIES - pecific to this document */ public int DocX { set { docx = value; } get { return docx; } } public int DocY { set { docy = value; } get { return docy; } } public int DocXvals { set { docxvals = value; } get { return docxvals; } } public int DocYvals { set { docyvals = value; } get { return docyvals; } } /* METHODS */ /*** New document ***/ public bool NewFile() { hasBeenModified = false;//Global fileName = defaultFileName;//Global // Let user know a "new" document has been "created" if (this.NewDocument != null) this.NewDocument(this, EventArgs.Empty); if (this.UpdateCaption != null) this.UpdateCaption(this, EventArgs.Empty); return true; } /*** Open existing file ***/ public bool Open(string newFileName = "") { if (string.IsNullOrEmpty(newFileName)) { OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog(); // Set up dialog dlg.DefaultExt = fileExtension; dlg.Filter = filter; // Get the file to open DialogResult res = dlg.ShowDialog(); if (res != DialogResult.OK) return false; newFileName = dlg.FileName; //MessageBox.Show(newFileName); } // Read the data try { using (StreamReader sr = new StreamReader(newFileName)) { DocXvals=Convert.ToInt32(sr.ReadLine()); DocYvals = Convert.ToInt32(sr.ReadLine()); //int cnt = 0; DocBoard = new Square[DocXvals, DocYvals]; for (int i = 0; i < DocXvals; i++) { for (int j = 0; j < DocYvals; j++) { DocBoard[i, j] = new Square(i, j); } } // int n = 0; // int m = 0; while (sr.EndOfStream==false)//-1 becasue it counts extra space in length { int x = Convert.ToInt32(sr.ReadLine()); int y = Convert.ToInt32(sr.ReadLine()); bool t = Convert.ToBoolean(sr.ReadLine());//turnout int p0 = Convert.ToInt32(sr.ReadLine()); int p1 = Convert.ToInt32(sr.ReadLine()); DocBoard[x, y].Is_Turnout = t; docBoard[x, y].Set_Piece(0, p0); if (t) { DocBoard[x, y].Set_Turnout(p0, p1); } //docBoard[x, y].Set_Piece(1, p1); docBoard[x, y].Set_Current_Piece(0); docBoard[x, y].Is_Active = true; } } //MessageBox.Show(playerslog + coordslog);//TESTING } catch (Exception e) { MessageBox.Show("Can't open file: " + newFileName + "\r\n" + e.Message, Application.ProductName); return false; } // Clear dirty bit, cache the file name and set the caption this.hasBeenModified = false; this.fileName = newFileName; // Success if (this.OpenDocument != null) this.OpenDocument(this, EventArgs.Empty); if (this.UpdateCaption != null) this.UpdateCaption(this, EventArgs.Empty); return true; } /*** Saving the data ***/ public bool Save() { return Save(false); } public bool SaveAs() { return Save(true); } protected bool Save(bool type)//players,timer,cpu,moves { // Get the file name string newFileName = fileName;//fileName?? if (type || string.IsNullOrEmpty(newFileName))//type is the sent param { // Set up the dialog SaveFileDialog dlg = new SaveFileDialog(); dlg.DefaultExt = fileExtension; dlg.Filter = filter; if (!string.IsNullOrEmpty(newFileName)) { dlg.InitialDirectory = Path.GetDirectoryName(newFileName); dlg.FileName = Path.GetFileName(newFileName); } else { dlg.FileName = this.fileName; } DialogResult res = dlg.ShowDialog(); if (res != DialogResult.OK) return false; newFileName = dlg.FileName; } // Write the data try { using (StreamWriter sw = new StreamWriter(newFileName)) { sw.WriteLine(DocXvals); sw.WriteLine(DocYvals); //Write Things for(int i=0; i< docxvals;i++) { for(int j=0; j< docyvals;j++) { Square csq = docBoard[i, j]; if(csq.Is_Active==true) { sw.WriteLine(csq.X); sw.WriteLine(csq.Y); sw.WriteLine(csq.Is_Turnout); sw.WriteLine(csq.Get_Piece(0).Id); if (csq.Is_Turnout) sw.WriteLine(csq.Get_Piece(1).Id); else sw.WriteLine(-1); } } } } } catch (Exception e) { MessageBox.Show("Can't save file: " + newFileName + "\r\n" + e.Message, Application.ProductName); return false; } // Clear the dirty bit, cache the new file name and set the caption hasBeenModified = false; fileName = newFileName; if (this.UpdateCaption != null) this.UpdateCaption(this, EventArgs.Empty); return true; } public DialogResult Close() { // if (hasBeenModified) // { // DialogResult result = MessageBox.Show(fileName, "Do you wish to save?", // MessageBoxButtons.YesNoCancel); //return result; // yes, no or cancel // } //else return DialogResult.OK; } //Meant to undo drawing of a point /********************* SELLS/MCVEY ****************************/ public void CheckRegisterFileExtension() { if (!this.registerFileExtension || string.IsNullOrEmpty(this.fileExtension)) return; // Register custom extension with the shell string progID = this.fileExtension + "file"; using (RegistryKey key = Registry.ClassesRoot.CreateSubKey("." + this.fileExtension)) { // Map custom extension to a ProgID key.SetValue(null, progID); } // Set ProgID description if (!string.IsNullOrEmpty(this.fileExtensionDesc)) { using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(progID)) { key.SetValue(null, this.fileExtensionDesc); } } // Register open command with the shell string cmdkey = progID + @"\shell\open\command"; using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(cmdkey)) { // Map ProgID to an Open action for the shell key.SetValue(null, string.Format("\"{0}\" \"%L\"", Application.ExecutablePath)); } // Register document icon with the shell - doesn't seem to work if (string.IsNullOrEmpty(this.iconResourceIndex)) return; string icokey = progID + @"\DefaultIcon"; using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(icokey)) { // Map ProgID to a document icon for the shell key.SetValue(null, Application.ExecutablePath + "," + this.iconResourceIndex); } } //*********************************Functions for Manipulating Arrays**************************// public void docAddPiece(object sender, EventArgs e) { // MessageBox.Show(docRow.ToString() + " " + docCol.ToString() + " " + docVal.ToString());//TESTING if it is in the right place. HasBeenModified = true; //Add values from the form } //*****************************End of Functions fro Manipulating Arrays// } }