Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Confirm
Close Dialog

Export Selected History Text to Single File

Description
This macro will prompt you to choose history items to export to a text file.
Language
C#.net
Minimum Version
Created By
Thomas Malloch (BFS)
Contributors
-
Date Created
Oct 29, 2020
Date Last Modified
Oct 29, 2020

Macro Code

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Drawing.Imaging;

public static class ClipboardFusionHelper
{
	public static string ProcessText(string text)
	{
		//open the custom form
		using(ClipboardHistorySelectForm dialog = new ClipboardHistorySelectForm())
            dialog.ShowDialog();
			
        return null;		
	}
	
	//this form will display a multi-select list with all the clipboard history items
	private class ClipboardHistorySelectForm : Form
	{
		//the form's controls
		private ListView lvClipboardHistory;
		private Button btnExport;
		private Button btnCancel;
		
		//the constrcutor will build the form and the controls
		public ClipboardHistorySelectForm()
		{
			this.lvClipboardHistory = new ListView();
			this.btnExport = new Button();
			this.btnCancel = new Button();
			
			this.SuspendLayout();
			
			//add a column to the list so we get a vertical scroll bar
			ColumnHeader column = new ColumnHeader();
			column.Width = 288;
			
			//set up the listview
			this.lvClipboardHistory.Location = new Point(16, 16);
			this.lvClipboardHistory.Size = new Size(312, 392);
			this.lvClipboardHistory.View = View.Details;
			this.lvClipboardHistory.HeaderStyle = ColumnHeaderStyle.None;
			this.lvClipboardHistory.Columns.Add(column);
			this.lvClipboardHistory.FullRowSelect = true;
			
			//set up the export button
			this.btnExport.Location = new System.Drawing.Point(160, 416);
			this.btnExport.Size = new System.Drawing.Size(80, 24);
			this.btnExport.Text = "Export";

			//set up the cancel button
			this.btnCancel.Location = new System.Drawing.Point(248, 416);
			this.btnCancel.Size = new System.Drawing.Size(80, 24);
			this.btnCancel.Text = "Cancel";
			
			//set up the form
			this.AcceptButton = this.btnExport;
			this.CancelButton = this.btnCancel;
			this.ClientSize = new Size(344, 456);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnExport);
			this.Controls.Add(this.lvClipboardHistory);
			this.Text = "Clipboard History";
			
			this.ResumeLayout(false);
			
			//set up the events
			this.btnCancel.Click += this.btnCancel_Click;
			this.btnExport.Click += this.btnExport_Click;
			this.Load += this.Form_Load;
		}
		
		//when the form loads, fill the listview with the items from the clipboard history
		private void Form_Load(object sender, EventArgs e)
		{
			this.lvClipboardHistory.Items.Clear();
			for(int i = 0; i < BFS.ClipboardFusion.GetHistoryItemCount(); i++)
			{
                HistoryListViewItem item = new HistoryListViewItem();
                item.HasText = BFS.ClipboardFusion.GetHistoryIndexContainsText(i);
                item.HasImage = BFS.ClipboardFusion.GetHistoryIndexContainsImage(i);
                if(item.HasText)
                    item.HistoryText = BFS.ClipboardFusion.GetHistoryText(i);
                
                if(item.HasImage)
                    item.HistoryImage = BFS.ClipboardFusion.GetHistoryImage(i);
                
                item.Text = item.HistoryText;
                if(item.Text.Length > 50)
                    item.Text = item.Text.Substring(0, 50);
                
                if((string.IsNullOrEmpty(item.Text)) && (item.HasImage))
                    item.Text = "[Image]";
                
				this.lvClipboardHistory.Items.Add(item);
            }
		}
		
		//this function gets called when the Export button is clicked
		private void btnExport_Click(object sender, EventArgs e)
		{
			// build the text to export
			StringBuilder builder = new StringBuilder();
			for(int i = 0; i < this.lvClipboardHistory.SelectedItems.Count; i++)
			{
                HistoryListViewItem historyItem = this.lvClipboardHistory.SelectedItems[i] as HistoryListViewItem;
                if(historyItem == null)
                    continue;
			
                if(!historyItem.HasText)
                    continue;
                    
                builder.AppendLine(historyItem.HistoryText);
			}
			
			// ask the users where it should be saved	
            using(SaveFileDialog dialog = new SaveFileDialog())
            {
                dialog.Title = "Save History Text";
                dialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
                dialog.FileName = "text";
                if(dialog.ShowDialog(this) != DialogResult.OK)
                    return;
                
                // save the text
                using(FileStream file = File.Open(dialog.FileName, FileMode.OpenOrCreate, FileAccess.Write))
                using(StreamWriter writer = new StreamWriter(file, Encoding.Unicode))
                    writer.Write(builder.ToString());
            }
				
			//set the dialog result and close the form
			this.DialogResult = DialogResult.OK;
			this.Close();
		}
		
		private void btnCancel_Click(object sender, EventArgs e)
		{			
			//set the dialog result and close the form
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}
	}
	
	private class HistoryListViewItem : ListViewItem
	{
        public bool HasImage { get; set; }
        public bool HasText { get; set; }
        public string HistoryText { get; set; }
        public Bitmap HistoryImage { get; set; }
	}
}