From 54d5474c13f8fe1ebf330b1fa352fd357d9a30be Mon Sep 17 00:00:00 2001 From: miku-666 <74728189+NessieHax@users.noreply.github.com> Date: Wed, 5 Jul 2023 08:50:56 +0200 Subject: [PATCH] MainForm - Added mechanism to prevent the same file from being opened twice --- PCK-Studio/MainForm.cs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/PCK-Studio/MainForm.cs b/PCK-Studio/MainForm.cs index a1c45cd8..6d95f71c 100644 --- a/PCK-Studio/MainForm.cs +++ b/PCK-Studio/MainForm.cs @@ -43,6 +43,8 @@ namespace PckStudio { private PckManager PckManager = null; + private Dictionary openFiles = new Dictionary(); + IComparer NodeSorter = new PckNodeSorter(); public MainForm() @@ -74,13 +76,14 @@ namespace PckStudio AddEditorPage(filepath); } - private void AddPage(string caption, Control control) + private TabPage AddPage(string caption, Control control) { control.Dock = DockStyle.Fill; var page = new TabPage(caption); page.Controls.Add(control); tabControl.TabPages.Add(page); tabControl.SelectTab(page); + return page; } private void AddEditorPage(PckFile pck) @@ -92,10 +95,17 @@ namespace PckStudio private void AddEditorPage(string filepath) { + if (openFiles.ContainsKey(filepath)) + { + tabControl.SelectTab(openFiles[filepath]); + return; + } + var editor = new PckEditor(); if (editor.Open(filepath, Settings.Default.UseLittleEndianAsDefault ? OMI.Endianness.LittleEndian : OMI.Endianness.BigEndian)) { - AddPage(Path.GetFileName(filepath), editor); + var page = AddPage(Path.GetFileName(filepath), editor); + openFiles.Add(filepath, page); return; } MessageBox.Show(string.Format("Failed to load {0}", Path.GetFileName(filepath)), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); @@ -366,10 +376,20 @@ namespace PckStudio if (TryGetEditor(out IPckEditor editor)) { editor.Close(); + RemoveOpenFile(); tabControl.TabPages.Remove(tabControl.SelectedTab); } } + private void RemoveOpenFile() + { + var kv = openFiles.First((kv) => kv.Value == tabControl.SelectedTab); + if (kv.Key != default && kv.Value != default) + { + openFiles.Remove(kv.Key); + } + } + private void programInfoToolStripMenuItem_Click(object sender, EventArgs e) { using CreditsForm info = new CreditsForm(); @@ -790,6 +810,7 @@ namespace PckStudio if (TryGetEditor(e.Page, out IPckEditor editor)) { editor.Close(); + RemoveOpenFile(); } }