From 00dfe2666eef23621e04381dc7398032db01ecdb Mon Sep 17 00:00:00 2001 From: harpreet31 Date: Mon, 06 Dec 2010 13:51:50 +0000 Subject: Chsarp code --- (limited to 'WindowsFormsApplication2/WindowsFormsApplication2/MainWindow.cs') diff --git a/WindowsFormsApplication2/WindowsFormsApplication2/MainWindow.cs b/WindowsFormsApplication2/WindowsFormsApplication2/MainWindow.cs new file mode 100644 index 0000000..2733db2 --- /dev/null +++ b/WindowsFormsApplication2/WindowsFormsApplication2/MainWindow.cs @@ -0,0 +1,631 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Windows.Forms; +using System.Diagnostics; +using disc1; +using MSDNForums; +using disc1.XML;//Object XML serializer class +using System.Drawing.Drawing2D;// +using SpeechLib;//For converting Text to Speech +using System.IO; + +namespace MSDNForums +{ + public partial class MainWindow : Form + { + private List m_items; + Output output = new Output(); + // the current edited file + private int CurrentFileId = 1; + private bool doClose = false; + private string SaveFileName; + private int selectedControl = -1; + private Color selectedColor; + // Application will store collection of files using + // an identifier for the file, + // and the strategy used to parse the file + int number = 0; + readonly Dictionary files = new Dictionary(); + public MainWindow() + { + InitializeComponent(); + m_items = new List(); + teacherModeToolStripMenuItem.Checked = true; + } + public MainWindow(string[] args) + { + InitializeComponent(); + + //check if args[0] is not a directory + if (File.Exists(args[0])) + { + FileInfo fi = new FileInfo(args[0]); + if (fi.Extension == ".lmv") + { + m_items = new List(); + this.Text = "Lend me your voice - Child Mode"; + + //do it before LoadPerson, otherwise it doesn't look good + SwitchToChildMode(); + + if (!files.ContainsKey(2)) + // add the new file to the list and specify what strategy is used to parse the file + files.Add(2, new ParseOpenedFile()); + + CurrentFileId = 2; + LoadPerson(fi.FullName); + } + else + { + m_items = new List(); + teacherModeToolStripMenuItem.Checked = true; + } + } + else + { + m_items = new List(); + teacherModeToolStripMenuItem.Checked = true; + } + + } + private void button1_Click(object sender, EventArgs e)//To Create TableLayout on button1_Click + { + if (rowsTextBox.Text=="" | columnsTextBox.Text == "") + { + MessageBox.Show("Plese specify the rows and columns"); + } + else + { + this.DoubleBuffered = true; + output.Person.Clear(); + output.Row = int.Parse(rowsTextBox.Text); + output.Column = int.Parse(columnsTextBox.Text); + //Suspend the layout to avoid redraw frequently. + tableLayoutPanel1.SuspendLayout(); + tableLayoutPanel1.Controls.Clear(); + tableLayoutPanel1.RowCount = int.Parse(rowsTextBox.Text); + tableLayoutPanel1.ColumnCount = int.Parse(columnsTextBox.Text); + for (int i = 0; i < tableLayoutPanel1.ColumnCount; i++) + { + this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent)); + } + for (int i = 0; i < this.tableLayoutPanel1.RowCount; i++) + { + this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent)); + } + tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetDouble; + for (int rows = 0; rows <= tableLayoutPanel1.RowCount - 1; rows++) + { + for (int col = 0; col <= tableLayoutPanel1.ColumnCount - 1; col++) + { + Panel p = new Panel(); + p.Dock = DockStyle.Fill; + PictureBox picb = new PictureBox(); + //Picture Box properties + picb.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage; + picb.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; + picb.BackColor = Color.White; + picb.Dock = DockStyle.Fill; + p.Controls.Add(picb); + picb.MouseClick += pb_Click; + picb.MouseLeave += pb_Leave; + tableLayoutPanel1.Controls.Add(p, col, rows); + LayoutItem item = new LayoutItem() + { + //This assignment is new in Visual studio 2008 + ItemPcitureBox = picb, + pnlcolor = p + }; + m_items.Add(item); + Customer c = new Customer(); + c.Index = col * tableLayoutPanel1.RowCount + rows; + } + } + MakeSameSize(); + tableLayoutPanel1.ResumeLayout(true); + if (!files.ContainsKey(1)) + // add the new file to the list and specify what strategy is used to parse the file + files.Add(1, new ParseNotSavedFile()); + CurrentFileId = 1; + } + } + //Resize the cell height and width according to the size of the form + private void MakeSameSize() + { + foreach (RowStyle style in tableLayoutPanel1.RowStyles) + { + style.SizeType = SizeType.Percent; + style.Height = 50; + } + + foreach (ColumnStyle style in tableLayoutPanel1.ColumnStyles) + { + style.SizeType = SizeType.Percent; + style.Width = 50; + } + + } + void pb_Click(object sender, EventArgs e)//Picture Box click event + { + if (teacherModeToolStripMenuItem.Checked) + { + LayoutItem item = FindObject(sender as PictureBox); + if (item != null)//Check if FindObject suceeded. + { + ChooseOptions form = new ChooseOptions(item); + form.ShowDialog(this); + } + } + else + { + PictureBox pbsp = (PictureBox)sender; + //Since a file path always contains a : + //For eg- C:\Recording.wav + // We check for the column + if (pbsp.Tag == null) + { + MessageBox.Show("Please specify the text or recording for the cell fist"); + return; + } + int n = pbsp.Tag.ToString().IndexOf(":"); + if (n!=-1) + { + //Play the recorded sound + System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer(); + myPlayer.SoundLocation = pbsp.Tag.ToString(); + myPlayer.Play(); + } + else + { + //The name of an object never contains a " : " + //Text to speech + SpVoice objSpeech = new SpVoice(); + objSpeech.Speak(pbsp.Tag.ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync); + } + } + } + private LayoutItem FindObject(PictureBox pictureBox) + { + foreach (LayoutItem item in m_items) + { + if (item.ItemPcitureBox == pictureBox) + return item; + } + return null; + } + + private void pb_Leave(object sender, EventArgs e) + { + PictureBox PicBox = (PictureBox)sender; + PicBox.BorderStyle = BorderStyle.None; + } + + private void SavePerson(string FileName) + { + for (int i = 0; i < tableLayoutPanel1.Controls.Count; i++) + { + Panel p = tableLayoutPanel1.Controls[i] as Panel; + Image image = (p.Controls[0] as PictureBox).Image; + String PicTag = (p.Controls[0] as PictureBox).Tag.ToString(); + Customer c = new Customer(); + c.Index = i; + c.Picture = (Bitmap)image; + c.CustomerName = PicTag; + if (p.BackColor != null) + { + c.ColorName = p.BackColor.ToArgb(); + c.PanelPadding = 2; + } + output.Person.Add(c); + } + output.Save(FileName); + } + + private void LoadPerson(string OFile) + { + //Load code + output.Load(OFile); + flowLayoutPanel1.Visible = false; + this.Text = "Lend me your voice - Child Mode " + OFile.ToString(); + childModeToolStripMenuItem.Checked = true; + this.DoubleBuffered = true; + tableLayoutPanel1.SuspendLayout(); + tableLayoutPanel1.Controls.Clear(); + tableLayoutPanel1.ColumnCount = output.Column; + tableLayoutPanel1.RowCount = output.Row; + for (int i = 0; i < tableLayoutPanel1.ColumnCount; i++) + { + this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent)); + } + for (int i = 0; i < this.tableLayoutPanel1.RowCount; i++) + { + this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent)); + } + tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetDouble; + foreach (Customer c in output.Person) + { + int rows = c.Index / output.Column; + int col = c.Index % output.Column; + Panel p = new Panel(); + //Panel properties + p.Dock = DockStyle.Fill; + p.BackColor = Color.FromArgb(c.ColorName); + p.Padding = new Padding { All = c.PanelPadding }; + PictureBox picb = new PictureBox(); + //Picture Box properties + picb.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; + picb.BackColor = Color.White; + picb.Dock = DockStyle.Fill; + picb.Image = c.Picture; + picb.Tag = c.CustomerName; + p.Controls.Add(picb); + m_items.Add(new LayoutItem { ItemPcitureBox = picb, pnlcolor = p }); + picb.MouseClick += pb_Click; + picb.MouseLeave += pb_Leave; + picb.GotFocus += new EventHandler(picb_GotFocus); + picb.LostFocus += new EventHandler(picb_LostFocus); + tableLayoutPanel1.Controls.Add(p, col, rows); + } + MakeSameSize(); + tableLayoutPanel1.ResumeLayout(true); + m_items[0].ItemPcitureBox.Focus(); + } + + void picb_LostFocus(object sender, EventArgs e) + { + LayoutItem item = FindObject(sender as PictureBox); + + if (this.tableLayoutPanel1.Contains(item.pnlcolor)) + { + item.pnlcolor.Margin = new Padding(3); + item.pnlcolor.Padding = new Padding(item.pnlcolor.Padding.All - item.pnlcolor.Margin.All); + item.pnlcolor.BackColor = this.selectedColor; + } + } + + void picb_GotFocus(object sender, EventArgs e) + { + LayoutItem item = FindObject(sender as PictureBox); + + if (this.tableLayoutPanel1.Contains(item.pnlcolor)) + { + item.pnlcolor.Padding = new Padding(item.pnlcolor.Padding.All + item.pnlcolor.Margin.All); + this.selectedColor = item.pnlcolor.BackColor; + item.pnlcolor.BackColor = Color.Lime; + item.pnlcolor.Margin = new Padding(0); + + this.selectedControl = m_items.IndexOf(item); + } + } + + protected override bool ProcessCmdKey(ref Message msg, Keys keyData) + { + if (keyData == Keys.Right) + { + if (this.selectedControl > -1) + { + if(this.selectedControl < m_items.Count - 1) + m_items[this.selectedControl + 1].ItemPcitureBox.Focus(); + else + m_items[0].ItemPcitureBox.Focus(); + } + + return true; + } + if (keyData == Keys.Left) + { + if (this.selectedControl > -1) + { + if (this.selectedControl > 0) + m_items[this.selectedControl - 1].ItemPcitureBox.Focus(); + else + m_items[m_items.Count - 1].ItemPcitureBox.Focus(); + } + + return true; + } + + if (keyData == Keys.Return) + { + if (this.selectedControl > -1) + { + pb_Click(m_items[this.selectedControl].ItemPcitureBox, new EventArgs()); + } + + return true; + } + + return base.ProcessCmdKey(ref msg, keyData); + } + + + #region CheckSavedorNotMethods + // the application will parse the file differently + // depending on whether it's not saved, or has been open + public interface IFileParserStrategy + { + MainWindow Window { get; set; } + System.Windows.Forms.DialogResult ParseFile(); + } + + // different strategies used to parse a file + public class ParseOpenedFile : IFileParserStrategy + { + MainWindow window; + public MainWindow Window + { + get { return window; } + set { window = value; } + } + public ParseOpenedFile(MainWindow window) + { + this.window = window; + } + public ParseOpenedFile() + { + } + public System.Windows.Forms.DialogResult ParseFile() + { + //if the opened file is not changed + //return yes + if (window.teacherModeToolStripMenuItem.Checked) + { + DialogResult result; + result = MessageBox.Show("Are you sure you want to quit without saving?", "Save the lesson", MessageBoxButtons.YesNoCancel); + return result; + } + else + { + //If its the child mode return yes, and quit without saving + return System.Windows.Forms.DialogResult.Yes; + } + } + } + public class ParseNotSavedFile : IFileParserStrategy + { + MainWindow window; + public MainWindow Window + { + get { return window; } + set { window = value; } + } + + public System.Windows.Forms.DialogResult ParseFile() + { + // Files that haven't been saved + DialogResult result; + result= MessageBox.Show("Are you sure you want to quit without saving?","Save the lesson",MessageBoxButtons.YesNoCancel); + return result; + } + } + #endregion + + //ToolStrip Click Events + private void newToolStripMenuItem_Click(object sender, EventArgs e) + { + tableLayoutPanel1.Controls.Clear(); + tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.None; + rowsTextBox.Text = String.Empty; + columnsTextBox.Text = String.Empty; + if (!files.ContainsKey(1)) + // add the new file to the list and specify what strategy is used to parse the file + files.Add(1, new ParseNotSavedFile()); + CurrentFileId = 1; + } + private void saveToolStripMenuItem_Click(object sender, EventArgs e) + { + //The cells in which no image is specified are saved as blank images + if (tableLayoutPanel1.Controls.Count == 0) + return; + while (number < tableLayoutPanel1.Controls.Count) + { + PictureBox pb = tableLayoutPanel1.Controls[number].Controls[0] as PictureBox; + if (pb.Image == null) + { + pb.Image = System.Drawing.Bitmap.FromFile("WhiteImage.png"); + pb.Tag = ""; + } + number++; + } + if (number >= tableLayoutPanel1.Controls.Count) + number = 0; + + SaveFileDialog saveFileDialog = new SaveFileDialog(); + saveFileDialog.Title = "Save the lesson file:"; + saveFileDialog.Filter = "LMV Files (*.lmv)|*.lmv"; + + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + try + { + SaveFileName = saveFileDialog.FileName; + if (!files.ContainsKey(2)) + // add the new file to the list and specify what strategy is used to parse the file + files.Add(2, new ParseOpenedFile()); + + CurrentFileId = 2; + SavePerson(SaveFileName); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Failed to save the file", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + private void openToolStripMenuItem_Click(object sender, EventArgs e) + { + OpenFileDialog ofdLessonFile = new OpenFileDialog(); + ofdLessonFile.Title = "Select the lesson file"; + ofdLessonFile.Multiselect = false; + string OpenFileName; + ofdLessonFile.Filter = "Lend me your voice files(LMV)|*.lmv|All Files(*.*)|*.*"; + if (ofdLessonFile.ShowDialog() == System.Windows.Forms.DialogResult.OK) + { + try + { + //do it before LoadPerson, otherwise it doesn't look good + SwitchToChildMode(); + + OpenFileName = ofdLessonFile.FileName; + if (!files.ContainsKey(2)) + // add the new file to the list and specify what strategy is used to parse the file + files.Add(2, new ParseOpenedFile()); + + CurrentFileId = 2; + LoadPerson(OpenFileName); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Failed to open the file", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + } + private void exitToolStripMenuItem_Click(object sender, EventArgs e) + { + + //if (files.Count > 0) + //{ + // if (files[CurrentFileId].Window == null) + // files[CurrentFileId].Window = this; + + // DialogResult dlg = files[CurrentFileId].ParseFile(); + // if (dlg == System.Windows.Forms.DialogResult.Yes) + // { + // //avoid endless turns + // this.doClose = true; + // this.Close(); + // } + // else if (dlg == System.Windows.Forms.DialogResult.No) + // { + // saveToolStripMenuItem.PerformClick(); + // } + // else + // { + // return; + // } + //} + //else + //{ + // this.doClose = true; + this.Close(); + // } + } + protected override void OnFormClosing(FormClosingEventArgs e) + { + if (files.Count > 0) + { + if (files[CurrentFileId].Window == null) + files[CurrentFileId].Window = this; + + DialogResult dlg = files[CurrentFileId].ParseFile(); + switch (dlg) + { + case DialogResult.No: + saveToolStripMenuItem.PerformClick(); + return; + case DialogResult.Cancel: + e.Cancel = true; + return; + } + } + } + + private void teacherModeToolStripMenuItem_Click(object sender, EventArgs e) + { + teacherModeToolStripMenuItem.Checked = false; + childModeToolStripMenuItem.Checked = false; + ((ToolStripMenuItem)sender).Checked = true; + } + private void automaticToolStripMenuItem_Click(object sender, EventArgs e) + { + if (((ToolStripMenuItem)sender).Checked) + { + automaticToolStripMenuItem.Checked = false; + //directionalKeysToolStripMenuItem.Checked = false; + //Navigation of Picture Boxes on the form + //Feature dropped for next version + ((ToolStripMenuItem)sender).Checked = true; + } + if (automaticToolStripMenuItem.Checked) + { + timer1.Start(); + } + } + private void automaticToolStripMenuItem_CheckStateChanged(object sender, EventArgs e) + { + if (automaticToolStripMenuItem.Checked) + { + timer1.Start(); + } + if (!automaticToolStripMenuItem.Checked) + { + timer1.Stop(); + } + } + private void CheckMode(object sender, EventArgs e) + { + if (teacherModeToolStripMenuItem.Checked) + { + this.Text = "Lend me your voice - Teacher Mode"; + flowLayoutPanel1.Visible = true; + childModeToolStripMenuItem.Checked = false; + } + if(childModeToolStripMenuItem.Checked) + { + this.Text = "Lend me your voice - Child mode"; + flowLayoutPanel1.Visible = false; + teacherModeToolStripMenuItem.Checked = false; + } + } + //Timer used for Automatic navigation within Picture Boxes + //Alt + S shortcut key to start and stop Automatic Directional Navigation + private void timer1_Tick(object sender, EventArgs e) + { + if (tableLayoutPanel1.Controls.Count == 0) + return; + PictureBox pb = tableLayoutPanel1.Controls[number++].Controls[0] as PictureBox; + pb.BorderStyle = BorderStyle.Fixed3D; + if (number >= tableLayoutPanel1.Controls.Count) + number = 0; + + Point center = new Point(pb.Width / 2, pb.Height / 2); + Cursor.Position = pb.PointToScreen(center); + } + private void MainWindow_KeyPress(object sender, KeyPressEventArgs e) + { + //Key Preview = True + if (e.KeyChar == (char)(Keys.S)) + { + timer1.Stop(); + //Stop the automatic navigation + } + } + + private void aboutToolStripMenuItem_Click(object sender, EventArgs e) + { + About abt = new About(); + abt.ShowDialog(this); + } + + private void MainWindow_FormClosing(object sender, FormClosingEventArgs e) + { + if (!doClose) + { + e.Cancel = true; + } + } + private void SwitchToChildMode() + { + teacherModeToolStripMenuItem.Checked = false; + childModeToolStripMenuItem.Checked = true; + } + } +} + + -- cgit v0.9.1