예제 #1
0
        public static void Add(string key, ClipboardItem value)
        {
            try
            {
                // add to the back-end dictionary
                LocalClipboard.Dict.Add(key, value);
            }
            // exception thrown on attempt to add a duplicate key to the dict
            catch (ArgumentException)
            {
                // store item with the same key
                ClipboardItem duplicateKeyItem = LocalClipboard.Dict[key];

                // remove old item and prepare to replace with new item
                LocalClipboard.Remove(duplicateKeyItem.KeyText, duplicateKeyItem);

                // add new item
                LocalClipboard.Dict.Add(key, value);
            }

            // add to back-end string collection of keys
            LocalClipboard.Keys.Insert(0, key);

            // add to visual clipboard last
            LocalClipboard.MainWindow.ListBox.Items.Insert(0, key);
        }
예제 #2
0
        public TextItem(MainWindow mainWindow, string text) : base(mainWindow, TypeEnum.Text)
        {
            // ensure param str is valid before continuing
            if (text == null || text.Length == 0)
            {
                return;
            }

            // store param str
            this.Text = text;

            // set beginning part of KeyText
            base.KeyText = "Text: ";

            // append to KeyText given Text
            if (this.Text.Length > LocalClipboard.CHAR_LIMIT - base.KeyText.Length)
            {
                base.KeyText += Text.Substring(0, LocalClipboard.CHAR_LIMIT - base.KeyText.Length) + "...";
            }
            else
            {
                base.KeyText += Text;
            }

            // if setting KeyDiff returns false, then there is an equivalent item at index 0
            if (!base.SetKeyDiff())
            {
                return;
            }

            // add KeyDiff to KeyText if needed
            if (base.KeyDiff != 0)
            {
                base.KeyText += base.KeyDiff;
            }

            /// Char order:
            ///
            /// 1. Type
            /// 2. KeyDiff
            /// 3. Text.Length
            /// 4. Text
            ///
            string keyDiffStr = base.KeyDiff.ToString();
            string textLenStr = this.Text.Length.ToString();

            base.FileChars = (char)base.Type + keyDiffStr + Environment.NewLine +
                             textLenStr + Environment.NewLine + this.Text;

            // add to local clipboard with CLIPBOARD file
            LocalClipboard.AddWithFile(base.KeyText, this);

            // move selected index to that of this instance if box is checked
            if (base.mainWindow.MoveToCopied.Checked)
            {
                base.mainWindow.ListBox.SelectedIndex = LocalClipboard.Keys.IndexOf(base.KeyText);
            }

            MsgLabel.Normal("Text item added!");
        }
예제 #3
0
        protected bool SetKeyDiff()
        {
            // calculate KeyDiff so that we can differentiate the items with duplicate key text
            for (string key = this.KeyText; LocalClipboard.Dict.ContainsKey(key); key = this.KeyText + this.KeyDiff)
            {
                // store item with the same key
                ClipboardItem duplicateKeyItem = LocalClipboard.Dict[key];

                // if keys are equivalent but the items are not, increment KeyDiff and continue loop
                if (!this.IsEquivalent(duplicateKeyItem))
                {
                    this.KeyDiff++;
                    continue;
                }

                // if old item is at the target index, then there is no need to add this item
                if (LocalClipboard.Keys.IndexOf(key) == 0)
                {
                    return(false);
                }

                // if this point is reached, remove the old item and break from the loop
                LocalClipboard.Remove(duplicateKeyItem.KeyText, duplicateKeyItem);
                break;
            }

            return(true);
        }
예제 #4
0
        public static void RemoveFromFile(string fileChars)
        {
            // base case: CLIPBOARD file is missing or empty
            if (!LocalClipboard.ClipboardFile.Exists || LocalClipboard.ClipboardFile.Length == 0)
            {
                return;
            }

            // store oldText, the old contents of the CLIPBOARD file
            string oldText;

            using (StreamReader sr = LocalClipboard.ClipboardFile.OpenText())
            {
                oldText = sr.ReadToEnd();
            }

            // newText = oldText - this item's FileChars
            string newText = LocalClipboard.ReplaceFirst(oldText, fileChars, "");

            // replace old contents of the CLIPBOARD file with newText
            using (StreamWriter sw = LocalClipboard.ClipboardFile.CreateText())
            {
                sw.Write(newText);
            }
        }
예제 #5
0
        public static void Remove(int index)
        {
            // return if index is invalid
            if (index < 0)
            {
                return;
            }

            // remove the item at the index
            string        key           = (string)MainWindow.ListBox.Items[index];
            ClipboardItem clipboardItem = LocalClipboard.Dict[key];

            LocalClipboard.Remove(clipboardItem.KeyText, clipboardItem);

            // if there was an item located after the removed item, select that item
            if (LocalClipboard.MainWindow.ListBox.Items.Count > index)
            {
                LocalClipboard.MainWindow.ListBox.SelectedIndex = index;
            }
            // else select the item located before the removed item
            else
            {
                LocalClipboard.MainWindow.ListBox.SelectedIndex = index - 1;
            }

            // notify the user of the successful operation for 3 seconds
            MsgLabel.Normal("Item removed!");
        }
예제 #6
0
        public AudioItem(MainWindow mainWindow, ushort keyDiff, StringReader strRead)
            : base(mainWindow, TypeEnum.Audio, keyDiff)
        {
            // retrieving ByteLength and KeyText from the stream
            this.ByteLength = long.Parse(strRead.ReadLine());
            base.KeyText    = strRead.ReadLine();

            /// Char order:
            ///
            /// 1. Type
            /// 2. KeyDiff
            /// 3. ByteLength
            /// 4. KeyText
            ///

            base.FileChars = (char)base.Type + base.KeyDiff.ToString() + Environment.NewLine +
                             this.ByteLength.ToString() + Environment.NewLine + base.KeyText + Environment.NewLine;

            // if audio file is missing, remove item data from the CLIPBOARD file and return
            FileInfo audioFile = new FileInfo(Path.Combine(LocalClipboard.AudioFolder.FullName, base.KeyText));

            if (!audioFile.Exists)
            {
                LocalClipboard.RemoveFromFile(base.FileChars);
                return;
            }

            // add to local clipboard
            LocalClipboard.Add(base.KeyText, this);
        }
예제 #7
0
        public static void AddWithFile(string key, ClipboardItem value)
        {
            // if CLIPBOARD file is missing or empty
            if (!LocalClipboard.ClipboardFile.Exists || LocalClipboard.ClipboardFile.Length == 0)
            {
                // create new file and prepare to write
                using (StreamWriter sw = LocalClipboard.ClipboardFile.CreateText())
                {
                    // write each item's FileChars to the CLIPBOARD file
                    for (int i = LocalClipboard.Keys.Count - 1; i >= 0; i--)
                    {
                        sw.Write(LocalClipboard.Dict[LocalClipboard.Keys[i]].FileChars);
                    }
                }
            }

            // append the item's FileChars to the CLIPBOARD file
            using (StreamWriter sw = LocalClipboard.ClipboardFile.AppendText())
            {
                sw.Write(value.FileChars);
            }

            // add to data structures in the local clipboard
            LocalClipboard.Add(key, value);
        }
예제 #8
0
        //public static void Rename(string okey, string nkey)
        //{
        //    int i = MainWindow.ListBox.Items.IndexOf(okey);
        //    var val = Dict[okey];

        //    switch (val.Type)
        //    {
        //        case ClipboardItem.TypeEnum.Text:


        //            break;
        //        case ClipboardItem.TypeEnum.FileDropList:
        //            break;
        //        case ClipboardItem.TypeEnum.Image:
        //            break;
        //        case ClipboardItem.TypeEnum.Audio:
        //            break;
        //        case ClipboardItem.TypeEnum.Custom:
        //            break;
        //    }

        //    Remove(okey, val);
        //    Insert(nkey, val, i);
        //}

        /// <summary>
        /// This method clears the structures, files, and folders involved in
        /// the local clipboard.
        ///
        /// Note that the backup file is preserved in case the clear operation
        /// was accidental.
        /// </summary>
        public static void Clear()
        {
            // first, write the current clipboard contents to the backup file
            LocalClipboard.BackupClipboard();

            // clear each data structure associated with the local clipboard
            LocalClipboard.MainWindow.ListBox.Items.Clear();
            LocalClipboard.Keys.Clear();
            LocalClipboard.Dict.Clear();

            // delete the CLIPBOARD file
            LocalClipboard.ClipboardFile.Delete();

            // recursively delete each item folder if it exists
            if (LocalClipboard.ImageFolder.Exists)
            {
                LocalClipboard.ImageFolder.Delete(true);
            }
            if (LocalClipboard.AudioFolder.Exists)
            {
                LocalClipboard.AudioFolder.Delete(true);
            }
            if (LocalClipboard.CustomFolder.Exists)
            {
                LocalClipboard.CustomFolder.Delete(true);
            }

            MsgLabel.Normal("All items cleared!");
        }
예제 #9
0
        private void RemoveBtn_Click(object sender, EventArgs e)
        {
            // set focus back to the local clipboard
            LocalClipboard.Focus();

            // remove the current index of the local clipboard
            LocalClipboard.Remove();
        }
예제 #10
0
        public static void FromFile()
        {
            // if CLIPBOARD file is missing or empty
            if (!LocalClipboard.ClipboardFile.Exists || LocalClipboard.ClipboardFile.Length == 0)
            {
                return;
            }

            // store the current contents of the clipboard file
            string clipboardString = LocalClipboard.GetClipboard();

            // update the backup file with clipboardString
            LocalClipboard.BackupClipboard(clipboardString);

            // traverse clipboardString via StringReader
            using (StringReader strRead = new StringReader(clipboardString))
            {
                // peek returns -1 if no more characters are available
                while (strRead.Peek() != -1)
                {
                    // Type
                    ClipboardItem.TypeEnum type = (ClipboardItem.TypeEnum)strRead.Read();

                    // KeyDiff
                    ushort keyDiff = ushort.Parse(strRead.ReadLine());

                    // remaining operations depend on the type of item
                    switch (type)
                    {
                    case ClipboardItem.TypeEnum.Text:
                        _ = new TextItem(LocalClipboard.MainWindow, keyDiff, strRead);

                        break;

                    case ClipboardItem.TypeEnum.FileDropList:
                        _ = new FileItem(LocalClipboard.MainWindow, keyDiff, strRead);

                        break;

                    case ClipboardItem.TypeEnum.Image:
                        _ = new ImageItem(LocalClipboard.MainWindow, keyDiff, strRead);

                        break;

                    case ClipboardItem.TypeEnum.Audio:
                        _ = new AudioItem(LocalClipboard.MainWindow, keyDiff, strRead);

                        break;

                    case ClipboardItem.TypeEnum.Custom:
                        _ = new CustomItem(LocalClipboard.MainWindow, keyDiff, strRead);

                        break;
                    }
                }
            }
        }
예제 #11
0
        public FileItem(MainWindow mainWindow, ushort keyDiff, StringReader strRead)
            : base(mainWindow, TypeEnum.FileDropList, keyDiff)
        {
            // retrieve number of strings in FileDropList
            int listCount = int.Parse(strRead.ReadLine());

            // read each string into FileDropList
            this.FileDropList = new StringCollection();
            for (int i = 0; i < listCount; i++)
            {
                this.FileDropList.Add(strRead.ReadLine());
            }

            // if 1 file was copied, KeyText will store FileDropList[0]'s filename
            if (this.FileDropList.Count == 1)
            {
                base.KeyText = "File: " + Path.GetFileName(this.FileDropList[0]);
            }
            // else KeyText will store FileDropList[0]'s filename + how many more files there are
            else
            {
                base.KeyText = "Files: " + Path.GetFileName(this.FileDropList[0]) + " + " + (this.FileDropList.Count - 1) + " more";
            }

            // shorten KeyText to fit the character limit if needed
            if (base.KeyText.Length > LocalClipboard.CHAR_LIMIT)
            {
                base.KeyText = base.KeyText.Substring(0, LocalClipboard.CHAR_LIMIT) + "...";
            }

            // add KeyDiff to KeyText if needed
            if (base.KeyDiff != 0)
            {
                base.KeyText += base.KeyDiff;
            }

            /// Char order:
            ///
            /// 1. Type
            /// 2. KeyDiff
            /// 3. # strings in FileDropList
            /// 4. FileDropList
            ///

            base.FileChars = (char)base.Type + base.KeyDiff.ToString() + Environment.NewLine +
                             this.FileDropList.Count.ToString() + Environment.NewLine;

            foreach (string fileDir in this.FileDropList)
            {
                base.FileChars += fileDir + Environment.NewLine;
            }

            // add to local clipboard
            LocalClipboard.Add(base.KeyText, this);
        }
예제 #12
0
        /// <summary>
        /// This method writes the current contents of the clipboard file to
        /// the backup file.
        ///
        /// This is used when clearing the local clipboard and when reading
        /// from the clipboard file on program startup.
        /// </summary>
        private static void BackupClipboard()
        {
            // if CLIPBOARD file is missing or empty
            if (!LocalClipboard.ClipboardFile.Exists || LocalClipboard.ClipboardFile.Length == 0)
            {
                return;
            }

            // first, get contents of the clipboard file
            string clipboardString = LocalClipboard.GetClipboard();

            // call helper with clipboardString as an arg
            LocalClipboard.BackupClipboard(clipboardString);
        }
예제 #13
0
        public TextItem(MainWindow mainWindow, ushort keyDiff, StringReader strRead)
            : base(mainWindow, TypeEnum.Text, keyDiff)
        {
            // retrieve number of chars in Text
            int textSize = int.Parse(strRead.ReadLine());

            // read textSize num chars from the file to a char array
            char[] textArr = new char[textSize];
            strRead.Read(textArr, 0, textSize);

            // store char array as Text
            this.Text = new string(textArr);

            // set beginning part of KeyText
            base.KeyText = "Text: ";

            // append to KeyText given Text
            if (this.Text.Length > LocalClipboard.CHAR_LIMIT - base.KeyText.Length)
            {
                base.KeyText += this.Text.Substring(0, LocalClipboard.CHAR_LIMIT - base.KeyText.Length) + "...";
            }
            else
            {
                base.KeyText += this.Text;
            }

            // add KeyDiff to KeyText if needed
            if (base.KeyDiff != 0)
            {
                base.KeyText += base.KeyDiff;
            }

            /// Char order:
            ///
            /// 1. Type
            /// 2. KeyDiff
            /// 3. Text.Length
            /// 4. Text
            ///
            string keyDiffStr = base.KeyDiff.ToString();
            string textLenStr = this.Text.Length.ToString();

            base.FileChars = (char)base.Type + keyDiffStr + Environment.NewLine +
                             textLenStr + Environment.NewLine + this.Text;

            // add to local clipboard
            LocalClipboard.Add(base.KeyText, this);
        }
예제 #14
0
        private void MainWindow_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
            case Keys.Up:
                this.OnArrowKeyUp(e);
                break;

            case Keys.Down:
                this.OnArrowKeyDown(e);
                break;

            case Keys.Enter:
                // copy selected item to the Windows clipboard
                LocalClipboard.Copy();
                break;

            case Keys.C:
                // copy selected item to clipboard on ctrl + c
                if (e.Control)
                {
                    LocalClipboard.Copy();
                }
                break;

            case Keys.Delete:
                // programmatically click the Remove button
                this.removeBtn.PerformClick();
                break;

            case Keys.Escape:
                // minimize to the taskbar
                this.WindowState = FormWindowState.Minimized;
                break;

            case Keys.F4:
                // minimize to system tray on Alt + F4
                if (e.Alt)
                {
                    this.Visible = false;
                }
                break;
            }

            // stop event handling chain
            e.Handled = true;
        }
예제 #15
0
        public static void Remove(string key, ClipboardItem value)
        {
            // first, remove the item's FileChars from the CLIPBOARD file
            LocalClipboard.RemoveFromFile(value.FileChars);

            // remove item by its key
            LocalClipboard.MainWindow.ListBox.Items.Remove(key);
            LocalClipboard.Keys.Remove(key);
            LocalClipboard.Dict.Remove(key);

            // determine if we need to delete any files along with the item
            DirectoryInfo folder = null;

            if (value is ImageItem)
            {
                folder = LocalClipboard.ImageFolder;
            }
            else if (value is AudioItem)
            {
                folder = LocalClipboard.AudioFolder;
            }
            else if (value is CustomItem)
            {
                folder = LocalClipboard.CustomFolder;
            }

            // nothing to do if folder is null or missing
            if (folder == null || !folder.Exists)
            {
                return;
            }

            // delete image/audio/custom file if it exists
            FileInfo fileToDelete = new FileInfo(Path.Combine(folder.FullName, key));

            if (fileToDelete.Exists)
            {
                fileToDelete.Delete();
            }

            // delete the folder if it's empty, i.e. contains 0 files
            FileInfo[] fileInfos = folder.GetFiles();
            if (fileInfos.Length == 0)
            {
                folder.Delete();
            }
        }
예제 #16
0
        public ImageItem(MainWindow mainWindow, ushort keyDiff, StringReader strRead)
            : base(mainWindow, TypeEnum.Image, keyDiff)
        {
            // retrieve width int
            int width = int.Parse(strRead.ReadLine());

            // retrieve height int
            int height = int.Parse(strRead.ReadLine());

            // init Size with width and height args
            this.Size = new Size(width, height);

            // set KeyText using image dimensions
            base.KeyText = "Image (" + this.Size.Width + " x " + this.Size.Height + ")";

            // add KeyDiff to KeyText if needed
            if (base.KeyDiff != 0)
            {
                base.KeyText += base.KeyDiff;
            }

            /// Char order:
            ///
            /// 1. Type
            /// 2. KeyDiff
            /// 3. Size.Width
            /// 4. Size.Height
            ///

            base.FileChars = (char)base.Type + base.KeyDiff.ToString() + Environment.NewLine +
                             this.Size.Width.ToString() + Environment.NewLine + this.Size.Height.ToString() +
                             Environment.NewLine;

            // if image file is missing, remove item data from the CLIPBOARD file and return
            FileInfo imageFile = new FileInfo(Path.Combine(LocalClipboard.ImageFolder.FullName, base.KeyText));

            if (!imageFile.Exists)
            {
                LocalClipboard.RemoveFromFile(base.FileChars);
                return;
            }

            // add to local clipboard
            LocalClipboard.Add(base.KeyText, this);
        }
예제 #17
0
        private const string SEARCH_DEFAULT = "Search for an item..."; // store searchTextBox's default text

        public MainWindow()
        {
            // required method for Designer support
            this.InitializeComponent();

            // hand this instance to the static classes that utilize MainWindow
            MsgLabel.MainWindow       = this;
            Config.MainWindow         = this;
            LocalClipboard.MainWindow = this;

            // read from CONFIG file and update items accordingly
            Config.FromFile();

            // read from CLIPBOARD file and write to local clipboard
            LocalClipboard.FromFile();

            // init WndProc event hook
            _ = new GlobalEventHook(this);

            wnd = new PopupWindow();
        }
예제 #18
0
        private void EditTextItem_Click(object sender, EventArgs e)
        {
            // check for valid SelectedIndex val before continuing
            if (listBox.SelectedIndex < 0)
            {
                MsgLabel.Normal("No text item is selected!");
                return;
            }

            // check for TextItem
            var oitem = LocalClipboard.Dict[(string)listBox.SelectedItem];

            if (oitem.Type != ClipboardItem.TypeEnum.Text)
            {
                MsgLabel.Normal("Selected item is not a text item!");
                return;
            }

            // get new text
            string otext = ((TextItem)oitem).Text;
            string ntext = wnd.ShowDialog(otext);

            // edit item if text is valid
            if (ntext == null)
            {
                return;
            }
            else
            {
                int i = listBox.SelectedIndex;
                LocalClipboard.Remove(i);

                var nitem = new TextItem(this, ntext);
                LocalClipboard.Move(nitem.KeyText, nitem, i);

                MsgLabel.Normal("Text item edited!");
            }
        }
예제 #19
0
        public CustomItem(MainWindow mainWindow, ushort keyDiff, StringReader strRead)
            : base(mainWindow, TypeEnum.Custom, keyDiff)
        {
            // retrieving WritableFormat from the stream
            this.WritableFormat = strRead.ReadLine();

            // set KeyText using WritableFormat
            base.KeyText = "Custom (" + this.WritableFormat + ")";

            // add KeyDiff to KeyText if needed
            if (base.KeyDiff != 0)
            {
                base.KeyText += base.KeyDiff;
            }

            /// Char order:
            ///
            /// 1. Type
            /// 2. KeyDiff
            /// 3. WritableFormat
            ///

            base.FileChars = (char)base.Type + base.KeyDiff.ToString() + Environment.NewLine + this.WritableFormat + Environment.NewLine;

            // if custom file is missing, remove item data from the CLIPBOARD file and return
            FileInfo customFile = new FileInfo(Path.Combine(LocalClipboard.CustomFolder.FullName, base.KeyText));

            if (!customFile.Exists)
            {
                LocalClipboard.RemoveFromFile(base.FileChars);
                return;
            }

            // add to local clipboard
            LocalClipboard.Add(base.KeyText, this);
        }
예제 #20
0
        private void OnArrowKeyDown(KeyEventArgs e)
        {
            // vars regarding listbox data
            int total   = this.listBox.Items.Count;
            int current = this.listBox.SelectedIndex;

            // base case 1: nothing in the clipboard
            if (total == 0)
            {
                return;
            }

            // base case 2: no item is selected, or only one item exists
            if (current < 0 || total == 1)
            {
                this.listBox.SelectedIndex = 0;
                return;
            }

            // store the item at current index
            ClipboardItem clipboardItem = LocalClipboard.Dict[(string)listBox.Items[current]];

            // store new index for the item and/or index to be moved
            int newIndex;

            // base case 3: current is at the last index
            if (current == total - 1)
            {
                // if box isn't checked, no wrapping or moving is needed
                if (!this.wrapKeysItem.Checked)
                {
                    return;
                }

                // we will be wrapping to the top, so newIndex is set to the top index, i.e. 0
                newIndex = 0;

                // move current item to the top index if shift is pressed
                if (e.Shift)
                {
                    LocalClipboard.Move(clipboardItem.KeyText, clipboardItem, newIndex);

                    // wrap selected index to the top index only if the box is checked
                    if (this.ChangeTopBottom.Checked)
                    {
                        this.listBox.SelectedIndex = newIndex;
                    }
                    else
                    {
                        this.listBox.SelectedIndex = current;
                    }
                }
                // else don't move the current item, and wrap the selected index unconditionally
                else
                {
                    this.listBox.SelectedIndex = newIndex;
                }

                return;
            }

            // store whether the selected index should be changed
            bool changeIndex = true;

            // if ctrl is being pressed, set newIndex to the bottom index
            if (e.Control)
            {
                newIndex = total - 1;

                // selected index should be unchanged if we're moving an item
                // to the top/bottom, but the box is unchecked
                if (e.Shift && !this.ChangeTopBottom.Checked)
                {
                    changeIndex = false;
                }
            }
            // else set newIndex to current + 1
            else
            {
                newIndex = current + 1;
                // changeIndex is unconditionally true if ctrl isn't being pressed
            }

            // move item to new index if shift is pressed
            if (e.Shift)
            {
                LocalClipboard.Move(clipboardItem.KeyText, clipboardItem, newIndex);
            }

            // move selected index to new index if bool is satisfied
            if (changeIndex)
            {
                this.listBox.SelectedIndex = newIndex;
            }
            else
            {
                this.listBox.SelectedIndex = current;
            }
        }
예제 #21
0
 private void ClearItem_Click(object sender, EventArgs e)
 {
     LocalClipboard.Clear();
 }
예제 #22
0
        public FileItem(MainWindow mainWindow, StringCollection fileDropList) : base(mainWindow, TypeEnum.FileDropList)
        {
            // ensure param is valid before continuing
            if (fileDropList == null || fileDropList.Count == 0)
            {
                return;
            }

            // get file drop list from param
            this.FileDropList = fileDropList;

            // if 1 file was copied, KeyText will store FileDropList[0]'s filename
            if (this.FileDropList.Count == 1)
            {
                base.KeyText = "File: " + Path.GetFileName(this.FileDropList[0]);
            }
            // else KeyText will store FileDropList[0]'s filename + how many more files there are
            else
            {
                base.KeyText = "Files: " + Path.GetFileName(this.FileDropList[0]) + " + " + (this.FileDropList.Count - 1) + " more";
            }

            // shorten KeyText to fit the character limit if needed
            if (base.KeyText.Length > LocalClipboard.CHAR_LIMIT)
            {
                base.KeyText = base.KeyText.Substring(0, LocalClipboard.CHAR_LIMIT) + "...";
            }

            // if setting KeyDiff returns false, then there is an equivalent item at index 0
            if (!base.SetKeyDiff())
            {
                return;
            }

            // add KeyDiff to KeyText if needed
            if (base.KeyDiff != 0)
            {
                base.KeyText += base.KeyDiff;
            }

            /// Char order:
            ///
            /// 1. Type
            /// 2. KeyDiff
            /// 3. # strings in FileDropList
            /// 4. FileDropList
            ///

            base.FileChars = (char)base.Type + base.KeyDiff.ToString() + Environment.NewLine +
                             this.FileDropList.Count.ToString() + Environment.NewLine;

            foreach (string fileDir in this.FileDropList)
            {
                base.FileChars += fileDir + Environment.NewLine;
            }

            // add to local clipboard with CLIPBOARD file
            LocalClipboard.AddWithFile(base.KeyText, this);

            // move selected index to that of this instance if box is checked
            if (base.mainWindow.MoveToCopied.Checked)
            {
                base.mainWindow.ListBox.SelectedIndex = LocalClipboard.Keys.IndexOf(base.KeyText);
            }

            MsgLabel.Normal("File item added!");
        }
예제 #23
0
 private void ListBox_DoubleClick(object sender, EventArgs e)
 {
     // copy selected item to the Windows clipboard
     LocalClipboard.Copy();
 }
예제 #24
0
        public ImageItem(MainWindow mainWindow, Image image) : base(mainWindow, TypeEnum.Image)
        {
            using (image)
            {
                // ensure image isn't null before continuing
                if (image == null)
                {
                    return;
                }

                // copy size struct of the image
                this.Size = image.Size;

                // set KeyText using image dimensions
                base.KeyText = "Image (" + this.Size.Width + " x " + this.Size.Height + ")";

                // if setting KeyDiff returns false, then there is an equivalent item at index 0
                if (!base.SetKeyDiff())
                {
                    return;
                }

                // add KeyDiff to KeyText if needed
                if (base.KeyDiff != 0)
                {
                    base.KeyText += base.KeyDiff;
                }

                // create Images folder if it's missing
                if (!LocalClipboard.ImageFolder.Exists)
                {
                    LocalClipboard.ImageFolder.Create();
                }

                // create image file in the Images folder
                image.Save(Path.Combine(LocalClipboard.ImageFolder.FullName, base.KeyText));
            }

            /// Char order:
            ///
            /// 1. Type
            /// 2. KeyDiff
            /// 3. Size.Width
            /// 4. Size.Height
            ///

            base.FileChars = (char)base.Type + base.KeyDiff.ToString() + Environment.NewLine +
                             this.Size.Width.ToString() + Environment.NewLine + this.Size.Height.ToString() +
                             Environment.NewLine;

            // add to local clipboard with CLIPBOARD file
            LocalClipboard.AddWithFile(base.KeyText, this);

            // move selected index to that of this instance if box is checked
            if (base.mainWindow.MoveToCopied.Checked)
            {
                base.mainWindow.ListBox.SelectedIndex = LocalClipboard.Keys.IndexOf(base.KeyText);
            }

            MsgLabel.Normal("Image item added!");
        }
예제 #25
0
 /// <summary>
 /// Moves item to new index
 /// </summary>
 /// <param name="key"></param>
 /// <param name="value"></param>
 /// <param name="index"></param>
 public static void Move(string key, ClipboardItem value, int index)
 {
     LocalClipboard.Remove(key, value);
     LocalClipboard.Insert(key, value, index);
 }
예제 #26
0
 public static void Remove()
 {
     LocalClipboard.Remove(MainWindow.ListBox.SelectedIndex);
 }
예제 #27
0
        public AudioItem(MainWindow mainWindow, Stream audioStream) : base(mainWindow, TypeEnum.Audio)
        {
            using (audioStream)
            {
                // ensure the stream is not null before continuing
                if (audioStream == null)
                {
                    return;
                }

                // store length in bytes of the stream
                this.ByteLength = audioStream.Length;

                // byteRate is stored at byte offset 28 of the WAVE file
                byte[] byteRateBuffer = new byte[4];
                audioStream.Read(byteRateBuffer, 28, 4);
                int byteRate = BitConverter.ToInt32(byteRateBuffer, 0);

                // subchunk2Size, i.e. byte length of the audio data, is stored at byte offset 40 (28 + 4 + 8) of the WAVE file
                byte[] subchunk2SizeBuffer = new byte[4];
                audioStream.Read(subchunk2SizeBuffer, 8, 4);
                int subchunk2Size = BitConverter.ToInt32(subchunk2SizeBuffer, 0);

                // compute the total length of the audio file in hours
                int fileLengthSeconds = subchunk2Size / byteRate;
                int fileLengthMinutes = fileLengthSeconds / 60;
                int fileLengthHours   = fileLengthMinutes / 60;

                // modify to split correct time in hours, minutes, and seconds
                fileLengthMinutes %= 60;
                fileLengthSeconds %= 60;

                // calculate KeyText given the data
                base.KeyText = "Audio (" + (fileLengthHours == 0 ? "" : fileLengthHours + "h:") + (fileLengthMinutes
                                                                                                   == 0 ? "" : fileLengthMinutes + "m:") + fileLengthSeconds + "s)";

                // if setting KeyDiff returns false, then there is an equivalent item at index 0
                if (!base.SetKeyDiff())
                {
                    return;
                }

                // add KeyDiff to KeyText if needed
                if (base.KeyDiff != 0)
                {
                    base.KeyText += base.KeyDiff;
                }

                // create Audio folder if it's missing
                if (!LocalClipboard.AudioFolder.Exists)
                {
                    LocalClipboard.AudioFolder.Create();
                }

                // create audio file in the Audio folder
                FileInfo audioFile = new FileInfo(Path.Combine(LocalClipboard.AudioFolder.FullName, base.KeyText));
                audioStream.Seek(0, SeekOrigin.Begin);
                using (FileStream fileStream = audioFile.Create())
                {
                    audioStream.CopyTo(fileStream);
                }
            }

            /// Char order:
            ///
            /// 1. Type
            /// 2. KeyDiff
            /// 3. ByteLength
            /// 4. KeyText
            ///

            base.FileChars = (char)base.Type + base.KeyDiff.ToString() + Environment.NewLine +
                             this.ByteLength.ToString() + Environment.NewLine + base.KeyText + Environment.NewLine;

            // add to local clipboard with CLIPBOARD file
            LocalClipboard.AddWithFile(base.KeyText, this);

            // move selected index to that of this instance if box is checked
            if (base.mainWindow.MoveToCopied.Checked)
            {
                base.mainWindow.ListBox.SelectedIndex = LocalClipboard.Keys.IndexOf(base.KeyText);
            }

            MsgLabel.Normal("Audio item added!");
        }
예제 #28
0
        public CustomItem(MainWindow mainWindow, IDataObject dataObject) : base(mainWindow, TypeEnum.Custom)
        {
            // ensure dataObject is valid before continuing
            if (dataObject == null)
            {
                return;
            }

            // set to null before doing checks
            this.WritableFormat = null;

            // store supported formats of dataObject
            string[] formats = dataObject.GetFormats();

            // ensure formats is a valid array before continuing
            if (formats == null || formats.Length == 0)
            {
                return;
            }

            // check for a format with writable data and return true if there is one
            foreach (string format in formats)
            {
                // store the first format whose data is serializable, then break
                if (dataObject.GetData(format).GetType().IsSerializable)
                {
                    this.WritableFormat = format;
                    break;
                }
            }

            // if no formats are writable, then we can't add this item
            if (this.WritableFormat == null)
            {
                return;
            }

            // set KeyText using WritableFormat
            base.KeyText = "Custom (" + this.WritableFormat + ")";

            // if setting KeyDiff returns false, then there is an equivalent item at index 0
            if (!base.SetKeyDiff())
            {
                return;
            }

            // add KeyDiff to KeyText if needed
            if (base.KeyDiff != 0)
            {
                base.KeyText += base.KeyDiff;
            }

            // create Custom folder if it's missing
            if (!LocalClipboard.CustomFolder.Exists)
            {
                LocalClipboard.CustomFolder.Create();
            }

            // create file in Custom folder with dataObject
            FileInfo        customFile = new FileInfo(Path.Combine(LocalClipboard.CustomFolder.FullName, base.KeyText));
            object          data       = dataObject.GetData(this.WritableFormat);
            BinaryFormatter formatter  = new BinaryFormatter();

            using (FileStream fileStream = customFile.Create())
            {
                formatter.Serialize(fileStream, data);
            }

            /// Char order:
            ///
            /// 1. Type
            /// 2. KeyDiff
            /// 3. WritableFormat
            ///

            base.FileChars = (char)base.Type + base.KeyDiff.ToString() + Environment.NewLine +
                             this.WritableFormat + Environment.NewLine;

            // add to local clipboard with CLIPBOARD file
            LocalClipboard.AddWithFile(base.KeyText, this);

            // move selected index to that of this instance if box is checked
            if (base.mainWindow.MoveToCopied.Checked)
            {
                base.mainWindow.ListBox.SelectedIndex = LocalClipboard.Keys.IndexOf(base.KeyText);
            }

            MsgLabel.Normal("Custom item added!");
        }
예제 #29
0
        protected override void WndProc(ref Message m)
        {
            //const int WM_NCPAINT = 0x0085; // message sent to a window when its frame must be painted
            const int WM_DRAWCLIPBOARD = 0x0308; // clipboard changed event
            const int WM_CHANGECBCHAIN = 0x030D; // change clipboard chain event
            const int WM_HOTKEY        = 0x0312; // hotkey pressed event

            switch (m.Msg)
            {
            //case WM_NCPAINT:
            //    // get MultiPaste's device context, which permits painting anywhere in the window
            //    IntPtr hDC = GetWindowDC(this.Handle);

            //    if ((int)hDC != 0)
            //    {
            //        Graphics graphics = Graphics.FromHdc(hDC);
            //        Rectangle rectangle = new Rectangle(0, 0, 4800, 23);

            //        graphics.FillRectangle(Brushes.Red, rectangle);
            //        graphics.Flush();

            //        ReleaseDC(this.Handle, hDC);
            //    }
            //    break;

            case WM_DRAWCLIPBOARD:
                // handle clipboard change if bool is true
                if (LocalClipboard.HandleClipboard)
                {
                    mainWindow.OnClipboardChange();
                }

                // send message to the next clipboard viewer
                SendMessage(clipboardViewerNext, m.Msg, m.WParam, m.LParam);

                break;

            case WM_CHANGECBCHAIN:
                // handle a change in the clipboard chain
                if (m.WParam == clipboardViewerNext)
                {
                    clipboardViewerNext = m.LParam;
                }
                else
                {
                    SendMessage(clipboardViewerNext, m.Msg, m.WParam, m.LParam);
                }

                break;

            case WM_HOTKEY:
                if (m.WParam.ToInt32() == DISP_ID)
                {
                    switch (mainWindow.WindowState)
                    {
                    case FormWindowState.Minimized:
                        mainWindow.WindowState = FormWindowState.Normal;
                        break;

                    case FormWindowState.Normal:
                        // if MultiPaste is the foreground window, minimize to the system tray
                        if (GetForegroundWindow() == mainWindow.Handle)
                        {
                            mainWindow.Visible = false;
                        }
                        // else bring MultiPaste to the foreground
                        else
                        {
                            SetForegroundWindow(mainWindow.Handle);
                            if (!mainWindow.Visible)
                            {
                                mainWindow.Visible = true;
                                LocalClipboard.Focus();
                            }
                        }
                        break;

                    case FormWindowState.Maximized:
                        mainWindow.WindowState = FormWindowState.Minimized;
                        break;
                    }
                }

                break;
            }

            // continue the WndProc chain
            base.WndProc(ref m);
        }