Source

GBRead / gbread / Forms / MainForm.cs

Full commit
  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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
namespace GBRead.Forms
{
    using System;
    using System.Drawing;
    using System.IO;
    using System.Threading;
    using System.Windows.Forms;
    using GBRead.Base;
    using GBRead.Patch;
    using ICSharpCode.AvalonEdit;

    public partial class MainForm : Form
    {
        private string currentFileLoaded = "";

        private LabelContainer labelContainer;

        private Disassembler disassembler;

        private Assembler assembler;

        private BinFile romFile;

        private MainFormOptions mainFormOptions = new MainFormOptions();

        private TextEditor mainTextBox;

        public MainForm(BinFile cs, Disassembler ds, Assembler ac, LabelContainer lcnew)
        {
            InitializeComponent();
            mainTextBox = ((TextBoxHost)elementHost2.Child).mainTextBox;
            disassembler = ds;
            assembler = ac;
            labelContainer = lcnew;
            romFile = cs;
            funcLabelBox.DataSource = labelContainer.FuncList;
            dataLabelBox.DataSource = labelContainer.DataList;
            varLabelBox.DataSource = labelContainer.VarList;
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
        }

        public void GetOptions(Options options)
        {
            mainTextBox.WordWrap = options.MainForm_WordWrap;
        }

        public void SetOptions(ref Options options)
        {
            options.MainForm_WordWrap = mainTextBox.WordWrap;
        }

        #region Menu Item Handlers

        #region FuncLabelBox Menu

        private void addFuncLabelMenuItem_Click(object sender, EventArgs e)
        {
            if (romFile.FileLoaded)
            {
                AddFunctionLabelForm af = new AddFunctionLabelForm(labelContainer, LabelEditMode.Add);
                af.ShowDialog();
                UpdateFuncBoxView();
            }
            else
            {
                Error.ShowErrorMessage(ErrorMessage.General_NoFileLoaded);
            }
        }

        private void renameFuncLabelMenuItem_Click(object sender, EventArgs e)
        {
            AddFunctionLabelForm af = new AddFunctionLabelForm(labelContainer, LabelEditMode.Edit, (FunctionLabel)funcLabelBox.SelectedItem);
            af.ShowDialog();
            UpdateFuncBoxView();
        }

        private void removeFuncLabelMenuItem_Click(object sender, EventArgs e)
        {
            labelContainer.RemoveFuncLabel((FunctionLabel)funcLabelBox.SelectedItem);
            UpdateFuncBoxView();
        }

        #endregion FuncLabelBox Menu

        #region DataLabelBox Menus

        private void addDataLabelMenuItem_Click(object sender, EventArgs e)
        {
            if (romFile.FileLoaded)
            {
                AddDataLabelForm ad = new AddDataLabelForm(labelContainer, LabelEditMode.Add);
                ad.ShowDialog();
                UpdateDataBoxView();
            }
            else
            {
                Error.ShowErrorMessage(ErrorMessage.General_NoFileLoaded);
            }
        }

        private void renameDataLabelMenuItem_Click(object sender, EventArgs e)
        {
            AddDataLabelForm ad = new AddDataLabelForm(labelContainer, LabelEditMode.Edit, (DataLabel)dataLabelBox.SelectedItem);
            ad.ShowDialog();
            UpdateDataBoxView();
        }

        private void removeDataLabelMenuItem_Click(object sender, EventArgs e)
        {
            labelContainer.RemoveDataLabel((DataLabel)dataLabelBox.SelectedItem);
            UpdateDataBoxView();
        }

        private void findReferencesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataLabel selectedLabel = (DataLabel)dataLabelBox.SelectedItem;
            UpdateMainTextBox(disassembler.SearchForReference(selectedLabel), TextBoxWriteMode.Overwrite);
        }

        #endregion DataLabelBox Menus

        #region VarLabelBox Menus

        private void addVariableToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (romFile.FileLoaded)
            {
                AddVarLabelForm av = new AddVarLabelForm(labelContainer, LabelEditMode.Add);
                av.ShowDialog();
                UpdateVarBoxView();
            }
            else
            {
                Error.ShowErrorMessage(ErrorMessage.General_NoFileLoaded);
            }
        }

        private void editVariableToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            AddVarLabelForm av = new AddVarLabelForm(labelContainer, LabelEditMode.Edit, (VarLabel)varLabelBox.SelectedItem);
            av.ShowDialog();
            UpdateVarBoxView();
        }

        private void removeVariableToolStripMenuItem3_Click(object sender, EventArgs e)
        {
            labelContainer.RemoveVarLabel((VarLabel)varLabelBox.SelectedItem);
            UpdateVarBoxView();
        }

        #endregion VarLabelBox Menus

        #region Toolstrip Menus

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Open GB/GBC File...";
            ofd.Filter = "GB/GBC Files|*.gb;*.gbc|All Files|*";
            ofd.FileName = "";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                FileInfo fs = new FileInfo(ofd.FileName);
                romFile.LoadFile(ofd.FileName);
                this.Text = "GBRead - " + ofd.SafeFileName;
                UpdateMainTextBox(romFile.GetBinInfo(), TextBoxWriteMode.Overwrite);
                currentFileLoaded = ofd.FileName;
                labelContainer.Initialize();
                UpdateFuncBoxView();
                UpdateDataBoxView();
                UpdateVarBoxView();
                startBox.Focus();
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void loadLabelsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (romFile.FileLoaded)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Title = "Load Labels...";
                ofd.Filter = "Label File|*.txt|All Files|*";
                ofd.FileName = "";
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    labelContainer.LoadLabelFile(ofd.FileName);
                    UpdateFuncBoxView();
                    UpdateDataBoxView();
                    UpdateVarBoxView();
                }
            }
            else
            {
                Error.ShowErrorMessage(ErrorMessage.General_NoFileLoaded);
            }
        }

        private void saveLabelsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "Save Labels...";
            sfd.FileName = "";
            sfd.Filter = "Label File|*.txt|All Files|*";
            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                labelContainer.SaveLabelFile(sfd.FileName);
                MessageBox.Show(String.Format("Saved Labels to{0}{1}", Environment.NewLine, sfd.FileName), "Success", MessageBoxButtons.OK);
            }
        }

        private void saveEntireFileASMToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (romFile.FileLoaded)
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Title = "Save All ASM...";
                sfd.Filter = "Text File|*.txt|All Files|*";
                sfd.FileName = "";
                if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    new Thread(new ThreadStart(() => {
                        if (romFile.FileLoaded)
                        {
                            UpdateProgressLabel("Working...", TextBoxWriteMode.Overwrite);
                            using (StreamWriter sw = new StreamWriter(sfd.FileName, false))
                            {
                                sw.Write(disassembler.GetFullASM());
                            }
                            UpdateProgressLabel("", TextBoxWriteMode.Overwrite);
                            MessageBox.Show(String.Format("Saved All ASM to{0}{1}", Environment.NewLine, sfd.FileName), "Success", MessageBoxButtons.OK);
                        }
                    })).Start();
                }
            }
            else
            {
                Error.ShowErrorMessage(ErrorMessage.General_NoFileLoaded);
            }
        }

        private void saveChangedFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (romFile.FileLoaded)
            {
                SaveFileDialog afd = new SaveFileDialog();
                afd.Title = "Save GB/GBC File...";
                afd.Filter = "GB/GBC Files|*.gb;*.gbc|All Files|*";
                afd.FileName = "";
                if (afd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    if (MessageBox.Show("Do you want to correct the header checksum and complement bytes as well?", "Fix Header", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    {
                        if (romFile is GBBinFile)
                        {
                            ((GBBinFile)romFile).FixGBHeader();
                        }
                    }
                    if (romFile.SaveFile(afd.FileName))
                    {
                        MessageBox.Show("File successfuly written to " + Environment.NewLine + afd.FileName);
                    }
                    else
                    {
                        MessageBox.Show("File could not be written to " + Environment.NewLine + afd.FileName);
                    }
                }
            }
            else
            {
                Error.ShowErrorMessage(ErrorMessage.General_NoFileLoaded);
            }
        }

        private void optionsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            OptionsForm op = new OptionsForm(disassembler, labelContainer, mainFormOptions);
            op.ShowDialog();
            mainTextBox.WordWrap = mainFormOptions.isWordWrap;
        }

        private void exportToBinaryFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "Export Data Section To Binary...";
            sfd.Filter = "Binary File|*.bin|All Files|*";
            sfd.FileName = ((DataLabel)dataLabelBox.SelectedItem).Name;
            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                DataLabel ds = (DataLabel)dataLabelBox.SelectedItem;
                if (romFile.SaveFilePortion(sfd.FileName, ds.Offset, ds.Length))
                {
                    MessageBox.Show("Binary file successfuly written to " + Environment.NewLine + sfd.FileName);
                }
            }
        }

        private void insertExternalBinaryAtLocationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (romFile.FileLoaded)
            {
                InsertBinaryForm ib = new InsertBinaryForm(romFile);
                ib.ShowDialog();
            }
            else
            {
                Error.ShowErrorMessage(ErrorMessage.General_NoFileLoaded);
            }
        }

        private void addAllCallsMadeInCodeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (romFile.FileLoaded)
            {
                disassembler.AutoPopulateFunctionList();
                UpdateFuncBoxView();
            }
            else
            {
                Error.ShowErrorMessage(ErrorMessage.General_NoFileLoaded);
            }
        }

        private void iPSToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (romFile.FileLoaded)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Title = "Open original file";
                ofd.Filter = "GB/GBC Files|*.gb;*.gbc|All Files|*";
                ofd.FileName = "";
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    byte[] orig = File.ReadAllBytes(ofd.FileName);
                    if (orig.Length != romFile.Length)
                    {
                        Error.ShowErrorMessage(ErrorMessage.IPS_FileSizeMismatch);
                    }
                    SaveFileDialog afd = new SaveFileDialog();
                    afd.Title = "Save IPS File...";
                    afd.Filter = "GB/GBC Files|*.gb;*.gbc|All Files|*";
                    afd.FileName = "";
                    if (afd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        IPS ips = new IPS();
                        ErrorMessage emt = ips.GenerateIPS(orig, romFile.MainFile);
                        if (emt != ErrorMessage.General_NoError)
                        {
                            File.WriteAllBytes(afd.FileName, ips.GetIPS());
                            MessageBox.Show("Patch successfuly written to " + Environment.NewLine + afd.FileName);
                        }
                        else
                        {
                            Error.ShowErrorMessage(emt);
                        }
                    }
                }
            }
            else
            {
                Error.ShowErrorMessage(ErrorMessage.General_NoFileLoaded);
            }
        }

        private void loadTableFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Open Table File...";
            ofd.Filter = "Table Files|*.tbl;*.txt|All Files|*";
            ofd.FileName = "";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                labelContainer.TableFile.LoadTableFile(ofd.FileName);
            }
        }

        private void loadShiftJISTableFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Open Table File...";
            ofd.Filter = "Table Files|*.tbl;*.txt|All Files|*";
            ofd.FileName = "";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                labelContainer.TableFile.LoadTableFile(ofd.FileName, true);
            }
        }

        #endregion Toolstrip Menus

        private void printASMButton_Click(object sender, EventArgs e)
        {
            int start;
            int end;
            InputValidation.TryParseOffsetString(startBox.Text, out start);
            InputValidation.TryParseOffsetString(endBox.Text, out end);
            new Thread(new ThreadStart(() => {
                if (romFile.FileLoaded)
                {
                    if (start < 0 || end < 0)
                    {
                        Error.ShowErrorMessage(ErrorMessage.Disassembly_StartOrEndInvalid);
                    }
                    else if (start < 0 || start > romFile.Length)
                    {
                        Error.ShowErrorMessage(ErrorMessage.Disassembly_StartInvalid);
                    }
                    else if (end <= 0 || end >= romFile.Length)
                    {
                        Error.ShowErrorMessage(ErrorMessage.Disassembly_EndInvalid);
                    }
                    else if (end < start)
                    {
                        Error.ShowErrorMessage(ErrorMessage.Disassembly_StartAfterEnd);
                    }
                    else
                    {
                        UpdateMainTextBox(disassembler.PrintASM(start, end - start), TextBoxWriteMode.Overwrite);
                    }
                }
                else
                {
                    Error.ShowErrorMessage(ErrorMessage.General_NoFileLoaded);
                }
            })).Start();
        }

        private void startEndBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((Keys)e.KeyChar == Keys.Enter)
            {
                printASMButton_Click(new object(), new EventArgs());
                e.Handled = true;
            }
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar) && !(e.KeyChar >= 'A' && e.KeyChar <= 'F') && !(e.KeyChar >= 'a' && e.KeyChar <= 'f') && !(e.KeyChar == ':'))
            {
                e.Handled = true;
            }
        }

        private void funcLabelBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            if (funcLabelBox.Items.Count > 0)
            {
                FunctionLabel ds = (FunctionLabel)funcLabelBox.Items[e.Index];
                Brush itemBrush = Brushes.Black;
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near;
                sf.LineAlignment = StringAlignment.Center;
                e.Graphics.DrawString(ds.ToString(), e.Font, itemBrush, funcLabelBox.GetItemRectangle(e.Index), sf);
            }
            e.DrawFocusRectangle();
        }

        private void dataLabelBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            if (dataLabelBox.Items.Count > 0)
            {
                DataLabel ds = (DataLabel)dataLabelBox.Items[e.Index];
                Brush itemBrush = Brushes.Black;
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near;
                sf.LineAlignment = StringAlignment.Center;
                switch (ds.DSectionType)
                {
                    case DataSectionType.Image:
                        itemBrush = Brushes.DeepPink;
                        break;

                    default:
                        break;
                }
                e.Graphics.DrawString(ds.ToString(), e.Font, itemBrush, dataLabelBox.GetItemRectangle(e.Index), sf);
            }
            e.DrawFocusRectangle();
        }

        private void varLabelBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            if (varLabelBox.Items.Count > 0)
            {
                VarLabel ds = (VarLabel)varLabelBox.Items[e.Index];
                Brush itemBrush = Brushes.Black;
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near;
                sf.LineAlignment = StringAlignment.Center;
                e.Graphics.DrawString(ds.ToString(), e.Font, itemBrush, varLabelBox.GetItemRectangle(e.Index), sf);
            }
            e.DrawFocusRectangle();
        }

        private void insertASMAtLocationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (romFile.FileLoaded)
            {
                InsertCodeForm iC = new InsertCodeForm(romFile, disassembler, assembler);
                iC.ShowDialog();
            }
            else
            {
                Error.ShowErrorMessage(ErrorMessage.General_NoFileLoaded);
            }
        }

        private void searchForFunctionsThatCallThisOneToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (romFile.FileLoaded)
            {
                UpdateMainTextBox(disassembler.SearchForFunctionCall((FunctionLabel)funcLabelBox.SelectedItem), TextBoxWriteMode.Overwrite);
            }
        }

        #endregion Menu Item Handlers

        #region Function Label Box Handlers

        private void funcLabelBox_DoubleClick(object sender, EventArgs e)
        {
            if (funcLabelBox.SelectedItem != null)
            {
                UpdateMainTextBox(disassembler.ShowFuncLabel((FunctionLabel)funcLabelBox.SelectedItem), TextBoxWriteMode.Overwrite);
            }
        }

        private void funcLabelBox_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                funcLabelBox.SelectedIndex = funcLabelBox.IndexFromPoint(e.X, e.Y);
                if (funcLabelBox.SelectedItem != null)
                    funcLabelBoxContextMenu.Show(MousePosition);
                else
                    funcLabelBoxContextMenu2.Show(MousePosition);
            }
        }

        private void funcLabelBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (funcLabelBox.SelectedIndex != -1)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    UpdateMainTextBox(disassembler.ShowFuncLabel((FunctionLabel)funcLabelBox.SelectedItem), TextBoxWriteMode.Overwrite);
                }
                else if (e.KeyCode == Keys.Delete)
                {
                    labelContainer.RemoveFuncLabel((FunctionLabel)funcLabelBox.SelectedItem);
                    UpdateFuncBoxView();
                }
            }
        }

        #endregion Function Label Box Handlers

        #region Data Label Box Handlers

        private void dataLabelBox_DoubleClick(object sender, EventArgs e)
        {
            if (dataLabelBox.SelectedItem != null)
            {
                if (((DataLabel)dataLabelBox.SelectedItem).DSectionType == DataSectionType.Image)
                {
                    ImageDisplayForm img = new ImageDisplayForm(romFile, (DataLabel)dataLabelBox.SelectedItem);
                    img.ShowDialog();
                }
                else
                {
                    UpdateMainTextBox(disassembler.ShowDataLabel((DataLabel)dataLabelBox.SelectedItem), TextBoxWriteMode.Overwrite);
                }
            }
        }

        private void dataLabelBox_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                dataLabelBox.SelectedIndex = dataLabelBox.IndexFromPoint(e.X, e.Y);
                if (dataLabelBox.SelectedItem != null)
                    dataLabelBoxContextMenu.Show(MousePosition);
                else
                    dataLabelContextMenu2.Show(MousePosition);
            }
        }

        private void dataLabelBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                UpdateMainTextBox(disassembler.ShowDataLabel((DataLabel)dataLabelBox.SelectedItem), TextBoxWriteMode.Overwrite);
            }
            else if (e.KeyCode == Keys.Delete)
            {
                labelContainer.RemoveDataLabel((DataLabel)dataLabelBox.SelectedItem);
                UpdateDataBoxView();
            }
        }

        #endregion Data Label Box Handlers

        #region Variable Label Box Handlers

        private void varLabelBox_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                varLabelBox.SelectedIndex = varLabelBox.IndexFromPoint(e.X, e.Y);
                if (varLabelBox.SelectedItem != null)
                    varLabelBoxContextMenu.Show(MousePosition);
                else
                    varLabelBoxContextMenu2.Show(MousePosition);
            }
        }

        private void varLabelBox_DoubleClick(object sender, EventArgs e)
        {
            if (varLabelBox.SelectedItem != null)
            {
                VarLabel selectedLabel = (VarLabel)varLabelBox.SelectedItem;
                UpdateMainTextBox(disassembler.ShowVarLabel(selectedLabel), TextBoxWriteMode.Overwrite);
            }
        }

        private void varLabelBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                UpdateMainTextBox(disassembler.ShowVarLabel((VarLabel)varLabelBox.SelectedItem), TextBoxWriteMode.Overwrite);
            }
            else if (e.KeyCode == Keys.Delete)
            {
                labelContainer.RemoveVarLabel((VarLabel)varLabelBox.SelectedItem);
                UpdateVarBoxView();
            }
        }

        #endregion Variable Label Box Handlers

        #region List Management

        private void UpdateFuncBoxView()
        {
            ((CurrencyManager)funcLabelBox.BindingContext[labelContainer.FuncList]).Refresh();
        }

        private void UpdateDataBoxView()
        {
            ((CurrencyManager)dataLabelBox.BindingContext[labelContainer.DataList]).Refresh();
        }

        private void UpdateVarBoxView()
        {
            ((CurrencyManager)varLabelBox.BindingContext[labelContainer.VarList]).Refresh();
        }

        #endregion List Management

        #region Box Update Invocations

        private delegate void UpdateTextBoxDelegate(string updateText, TextBoxWriteMode overwriteExistingText);

        private void UpdateProgressLabel(string newLabel, TextBoxWriteMode overwriteExistingText = TextBoxWriteMode.Append)
        {
            if (progressLabel.InvokeRequired)
            {
                UpdateTextBoxDelegate del = new UpdateTextBoxDelegate(UpdateProgressLabel);
                progressLabel.Invoke(del, new object[] { newLabel, overwriteExistingText });
            }
            else
            {
                if (overwriteExistingText == TextBoxWriteMode.Overwrite)
                    progressLabel.Text = newLabel;
                else
                    progressLabel.Text += newLabel;
            }
        }

        public enum TextBoxWriteMode
        {
            Append,
            Overwrite
        }

        private void UpdateMainTextBox(string text, TextBoxWriteMode overwriteExistingText)
        {
            if (mainTextBox.Dispatcher.Thread != Thread.CurrentThread)
            {
                mainTextBox.Dispatcher.BeginInvoke(new UpdateTextBoxDelegate(UpdateMainTextBox), text, overwriteExistingText);
            }
            else
            {
                if (overwriteExistingText == TextBoxWriteMode.Overwrite)
                {
                    mainTextBox.Clear();
                }
                mainTextBox.AppendText(text);
            }
        }

        #endregion Box Update Invocations

        private void addNewButton_Click(object sender, EventArgs e)
        {
            switch (LabelTabControl.SelectedIndex)
            {
                case 0:
                    addFuncLabelMenuItem_Click(sender, e);
                    return;

                case 1:
                    addDataLabelMenuItem_Click(sender, e);
                    return;

                case 2:
                    addVariableToolStripMenuItem_Click(sender, e);
                    return;
            }
        }

        private void addCommentButton_Click(object sender, EventArgs e)
        {
            if (romFile.FileLoaded)
            {
                var af = new AddCommentForm(labelContainer, LabelEditMode.Add);
                af.ShowDialog();
            }
            else
            {
                Error.ShowErrorMessage(ErrorMessage.General_NoFileLoaded);
            }
        }
    }

    public class MainFormOptions
    {
        public bool isWordWrap;

        public MainFormOptions()
        {
            isWordWrap = false;
        }
    }
}