From 1bc7311da64bff4e80bae024790edae888513812 Mon Sep 17 00:00:00 2001 From: Solomon Blount <74916907+siblount@users.noreply.github.com> Date: Sat, 13 Jan 2024 17:19:00 -0500 Subject: [PATCH] auto switch page on file drag --- .../Forms/MainForm.Designer.cs | 3 + src/DAZ_Installer.Windows/Forms/MainForm.cs | 15 +++ .../Pages/Home.Designer.cs | 6 +- src/DAZ_Installer.Windows/Pages/Home.cs | 103 ++++++++++-------- 4 files changed, 77 insertions(+), 50 deletions(-) diff --git a/src/DAZ_Installer.Windows/Forms/MainForm.Designer.cs b/src/DAZ_Installer.Windows/Forms/MainForm.Designer.cs index c988eb2..a9c3bef 100644 --- a/src/DAZ_Installer.Windows/Forms/MainForm.Designer.cs +++ b/src/DAZ_Installer.Windows/Forms/MainForm.Designer.cs @@ -213,6 +213,7 @@ private void InitializeComponent() // // MainForm // + AllowDrop = true; AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; ClientSize = new System.Drawing.Size(659, 344); @@ -225,6 +226,8 @@ private void InitializeComponent() Text = "Product Manager for DAZ Studio"; FormClosing += Form1_FormClosing; Load += Form1_Load; + DragDrop += MainForm_DragDrop; + DragEnter += MainForm_DragEnter; tableLayoutPanel1.ResumeLayout(false); tableLayoutPanel1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit(); diff --git a/src/DAZ_Installer.Windows/Forms/MainForm.cs b/src/DAZ_Installer.Windows/Forms/MainForm.cs index 4858d62..c60e051 100644 --- a/src/DAZ_Installer.Windows/Forms/MainForm.cs +++ b/src/DAZ_Installer.Windows/Forms/MainForm.cs @@ -122,5 +122,20 @@ private void libraryLbl_Click(object sender, EventArgs e) } private void pictureBox1_Click(object sender, EventArgs e) => new AboutForm().ShowDialog(); + + private void MainForm_DragDrop(object sender, DragEventArgs e) + { + + } + + private void MainForm_DragEnter(object sender, DragEventArgs e) + { + e.Effect = Program.DropEffect; + // Get the page we are currently in... If we are not on the home page, then switch to it. + if (visiblePage != homePage1) + { + SwitchPage(homePage1); + } + } } } diff --git a/src/DAZ_Installer.Windows/Pages/Home.Designer.cs b/src/DAZ_Installer.Windows/Pages/Home.Designer.cs index 02cdab3..20a9caf 100644 --- a/src/DAZ_Installer.Windows/Pages/Home.Designer.cs +++ b/src/DAZ_Installer.Windows/Pages/Home.Designer.cs @@ -107,8 +107,6 @@ private void InitializeComponent() listView1.TabIndex = 4; listView1.UseCompatibleStateImageBehavior = false; listView1.View = System.Windows.Forms.View.Details; - listView1.DragDrop += dropBtn_DragDrop; - listView1.DragEnter += listView1_DragEnter; // // columnHeader1 // @@ -183,12 +181,12 @@ private void InitializeComponent() dropBtn.Text = "Click here to select file(s) or drag them here."; dropBtn.UseVisualStyleBackColor = false; dropBtn.Click += dropBtn_Click; - dropBtn.DragDrop += dropBtn_DragDrop; dropBtn.DragEnter += dropBtn_DragEnter; dropBtn.DragLeave += dropBtn_DragLeave; // // Home // + AllowDrop = true; AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit; AutoSize = true; BackColor = System.Drawing.Color.White; @@ -197,6 +195,8 @@ private void InitializeComponent() Controls.Add(tableLayoutPanel1); Name = "Home"; Size = new System.Drawing.Size(542, 344); + DragDrop += Home_DragDrop; + DragEnter += Home_DragEnter; homeListContextMenuStrip.ResumeLayout(false); tableLayoutPanel1.ResumeLayout(false); ResumeLayout(false); diff --git a/src/DAZ_Installer.Windows/Pages/Home.cs b/src/DAZ_Installer.Windows/Pages/Home.cs index c1f34c2..e74ff8f 100644 --- a/src/DAZ_Installer.Windows/Pages/Home.cs +++ b/src/DAZ_Installer.Windows/Pages/Home.cs @@ -21,6 +21,19 @@ public Home() InitializeComponent(); HomePage = this; titleLbl.Text = Program.AppName; + + RegisterGlobalEvents(this); + } + + private void RegisterGlobalEvents(Control parent) + { + foreach (Control control in parent.Controls) + { + control.AllowDrop = true; + control.DragDrop += Home_DragDrop; + control.DragEnter += Home_DragEnter; + RegisterGlobalEvents(control); + } } private void dropBtn_Click(object sender, EventArgs e) => HandleOpenDialogue(); @@ -51,55 +64,10 @@ internal void button1_Click(object sender, EventArgs e) clearListBtn_Click(null, null); } - private void dropBtn_DragEnter(object sender, DragEventArgs e) - { - dropBtn.Text = "Drop here!"; - e.Effect = Program.DropEffect; - } + private void dropBtn_DragEnter(object sender, DragEventArgs e) => dropBtn.Text = "Drop here!"; private void dropBtn_DragLeave(object sender, EventArgs e) => dropBtn.Text = "Click here to select file(s) or drag them here."; - private void dropBtn_DragDrop(object sender, DragEventArgs e) - { - if (e.Data is null) return; - var tmp = (string[])e.Data.GetData(DataFormats.FileDrop, false); - Queue invalidFiles = new(); - listView1.BeginUpdate(); - // Check for string if it's valid. - foreach (var path in tmp) - { - var fileInfo = new FileInfo(path); - var ext = fileInfo.Extension; - ext = ext.IndexOf('.') != -1 ? ext.Substring(1) : ext; - if (fileInfo.Exists && DPFile.ValidImportExtension(ext)) - { - // Add to list. - listView1.Items.Add(path); - } - else - { - ArchiveFormat type = DPArchive.DetermineArchiveFormatPrecise(path); // TODO: I'm pretty sure this can be removed. - if (type == ArchiveFormat.SevenZ && ext.EndsWith("001")) - listView1.Items.Add(path); - else invalidFiles.Enqueue(path); - } - } - listView1.EndUpdate(); - if (invalidFiles.Count > 0) - { - var builder = new StringBuilder(50); - while (invalidFiles.Count != 0) - builder.AppendLine(" \u2022 " + invalidFiles.Dequeue()); - MessageBox.Show("Files that cannot be processed where removed from the list." + - "\nRemoved files:\n" + builder.ToString(), "Invalid files removed", MessageBoxButtons.OK, - MessageBoxIcon.Warning); - } - if (listView1.Items.Count != 0) - dropBtn.Visible = dropBtn.Enabled = false; - - dropBtn.Text = "Click here to select file(s) or drag them here."; - } - private void removeToolStripMenuItem_Click(object sender, EventArgs e) { for (var i = listView1.SelectedItems.Count - 1; i >= 0; i--) @@ -148,6 +116,47 @@ private void homeListContextMenuStrip_Opening(object sender, CancelEventArgs e) private void addMoreItemsToolStripMenuItem_Click(object sender, EventArgs e) => HandleOpenDialogue(); - private void listView1_DragEnter(object sender, DragEventArgs e) => e.Effect = Program.DropEffect; + private void Home_DragEnter(object sender, DragEventArgs e) => e.Effect = Program.DropEffect; + + private void Home_DragDrop(object sender, DragEventArgs e) + { + if (e.Data is null) return; + var tmp = (string[])e.Data.GetData(DataFormats.FileDrop, false); + Queue invalidFiles = new(); + listView1.BeginUpdate(); + // Check for string if it's valid. + foreach (var path in tmp) + { + var fileInfo = new FileInfo(path); + var ext = fileInfo.Extension; + ext = ext.IndexOf('.') != -1 ? ext.Substring(1) : ext; + if (fileInfo.Exists && DPFile.ValidImportExtension(ext)) + { + // Add to list. + listView1.Items.Add(path); + } + else + { + ArchiveFormat type = DPArchive.DetermineArchiveFormatPrecise(path); // TODO: I'm pretty sure this can be removed. + if (type == ArchiveFormat.SevenZ && ext.EndsWith("001")) + listView1.Items.Add(path); + else invalidFiles.Enqueue(path); + } + } + listView1.EndUpdate(); + if (invalidFiles.Count > 0) + { + var builder = new StringBuilder(50); + while (invalidFiles.Count != 0) + builder.AppendLine(" \u2022 " + invalidFiles.Dequeue()); + MessageBox.Show("Files that cannot be processed where removed from the list." + + "\nRemoved files:\n" + builder.ToString(), "Invalid files removed", MessageBoxButtons.OK, + MessageBoxIcon.Warning); + } + if (listView1.Items.Count != 0) + dropBtn.Visible = dropBtn.Enabled = false; + + dropBtn.Text = "Click here to select file(s) or drag them here."; + } } }