Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/WindowsFormsApplication2/WindowsFormsApplication2/MainWindow.cs
blob: 2733db25940878679346fd7e6aaf0b3d9140f8f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
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<LayoutItem> 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<int, IFileParserStrategy> files = new Dictionary<int, IFileParserStrategy>();
        public MainWindow()
        {
            InitializeComponent();
            m_items = new List<LayoutItem>();
            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<LayoutItem>();
                    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<LayoutItem>();
                    teacherModeToolStripMenuItem.Checked = true;
                }
            }
            else
            {
                m_items = new List<LayoutItem>();
                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;
        }
    }
}