~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Main/ICSharpCode.Core.WinForms/Util/ClipboardWrapper.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Runtime.InteropServices;
 
6
using System.Windows.Forms;
 
7
 
 
8
namespace ICSharpCode.Core.WinForms
 
9
{
 
10
        /// <summary>
 
11
        /// Helper class to access the clipboard without worrying about ExternalExceptions
 
12
        /// </summary>
 
13
        public static class ClipboardWrapper
 
14
        {
 
15
                [Obsolete("Avoid using this property: it is problematic because it requires exclusive clipboard access. " +
 
16
                          "The Clipboard.ContainsText() implementation in WPF is much better than the one in WinForms.")]
 
17
                public static bool ContainsText {
 
18
                        get {
 
19
                                try {
 
20
                                        return Clipboard.ContainsText();
 
21
                                } catch (ExternalException) {
 
22
                                        return false;
 
23
                                }
 
24
                        }
 
25
                }
 
26
                
 
27
                public static string GetText()
 
28
                {
 
29
                        // retry 2 times should be enough for read access
 
30
                        try {
 
31
                                return Clipboard.GetText();
 
32
                        } catch (ExternalException) {
 
33
                                return Clipboard.GetText();
 
34
                        }
 
35
                }
 
36
                
 
37
                public static void SetText(string text)
 
38
                {
 
39
                        DataObject data = new DataObject();
 
40
                        data.SetData(DataFormats.UnicodeText, true, text);
 
41
                        SetDataObject(data);
 
42
                }
 
43
                
 
44
                /// <summary>
 
45
                /// Gets the current clipboard content.
 
46
                /// Can return null!
 
47
                /// </summary>
 
48
                public static IDataObject GetDataObject()
 
49
                {
 
50
                        // retry 2 times should be enough for read access
 
51
                        try {
 
52
                                return Clipboard.GetDataObject();
 
53
                        } catch (ExternalException) {
 
54
                                try {
 
55
                                        return Clipboard.GetDataObject();
 
56
                                } catch (ExternalException) {
 
57
                                        return null;
 
58
                                }
 
59
                        }
 
60
                }
 
61
                
 
62
                public static void SetDataObject(object data)
 
63
                {
 
64
                        SafeSetClipboard(data);
 
65
                }
 
66
                
 
67
                // Code duplication: TextAreaClipboardHandler.cs also has SafeSetClipboard
 
68
                [ThreadStatic] static int SafeSetClipboardDataVersion;
 
69
                
 
70
                static void SafeSetClipboard(object dataObject)
 
71
                {
 
72
                        // Work around ExternalException bug. (SD2-426)
 
73
                        // Best reproducable inside Virtual PC.
 
74
                        int version = unchecked(++SafeSetClipboardDataVersion);
 
75
                        try {
 
76
                                Clipboard.SetDataObject(dataObject, true);
 
77
                        } catch (ExternalException) {
 
78
                                Timer timer = new Timer();
 
79
                                timer.Interval = 100;
 
80
                                timer.Tick += delegate {
 
81
                                        timer.Stop();
 
82
                                        timer.Dispose();
 
83
                                        if (SafeSetClipboardDataVersion == version) {
 
84
                                                try {
 
85
                                                        Clipboard.SetDataObject(dataObject, true, 10, 50);
 
86
                                                } catch (ExternalException) { }
 
87
                                        }
 
88
                                };
 
89
                                timer.Start();
 
90
                        }
 
91
                }
 
92
        }
 
93
}