Skip to content

Commit

Permalink
~In Clipboard class Owner initialization moved to ctor/
Browse files Browse the repository at this point in the history
~Removed extra using direcitvesStrings::SplitString string parts sequence was incorrect+ClipbordWatcher: Added Cipboard chage events\
+Msbuid target for copying ClipboardWatcher to Helper/Tests
  • Loading branch information
Stadub committed Nov 22, 2014
1 parent 9e389db commit 693dee3
Show file tree
Hide file tree
Showing 31 changed files with 358 additions and 332 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@
################################################################################

*.suo
*.sdf
*.opensdf
*.sln.DotSettings.user
*proj.user
bin/
obj/
Debug/
ipch/
11 changes: 11 additions & 0 deletions BuildActions.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<Target Name="CopyWatcherOut" BeforeTargets="AfterBuild">
<ItemGroup>
<ClipboardWatcherOut Include="$(SolutionDir)$(Configuration)\*.*"/>
</ItemGroup>
<Copy
SourceFiles="@(ClipboardWatcherOut)"
DestinationFolder="$(OutputPath)"/>
<Message Text='Files "@(ClipboardWatcherOut)" copied to "$(OutputPath)"' Importance="High"/>
</Target>
</Project>
3 changes: 3 additions & 0 deletions ClipboardHelper/ClipboardHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@
<Compile Include="FormatProviders\HtmlFormatProvider.cs" />
<Compile Include="FormatProviders\SkypeFormatProvider.cs" />
<Compile Include="Helpers\Enumerable.cs" />
<Compile Include="Helpers\GenericEventArgs.cs" />
<Compile Include="Helpers\Strings.cs" />
<Compile Include="Win32\ClipbordWatcherException.cs" />
<Compile Include="Win32\ClipbordWatcher.cs" />
<Compile Include="Win32\WatcherEnums.cs" />
<Compile Include="WinApi\Window.cs" />
Expand All @@ -111,6 +113,7 @@
</Target>
-->
<Import Project="$(SolutionDir)\BuildTargets.target" />
<Import Project="$(SolutionDir)\BuildActions.target" />
<!--<Target Name="TargetsGlue" DependsOnTargets="DetectWinVersionConstants" BeforeTargets="BeforeBuild">
<PropertyGroup>
<DefineConstants>$(DefineConstants);$(WinVersion)</DefineConstants>
Expand Down
80 changes: 48 additions & 32 deletions ClipboardHelper/ClipboardWinApi.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using ClipboardHelper.FormatProviders;
using ClipboardHelper.Win32;
Expand All @@ -13,71 +13,94 @@ public class Clipboard : IDisposable
{
private bool disposed;

private static readonly IntPtr notOwned = new IntPtr(-1);
private bool Owned;

private IntPtr ClipboardOwner;
public Clipboard()
{
ClipboardOwner=new IntPtr(-1);

var formats=Enum.GetValues(typeof (StandartClipboardFormats));
protected Clipboard(IntPtr ownerHwnd)
{
var formats = Enum.GetValues(typeof(StandartClipboardFormats));
foreach (StandartClipboardFormats format in formats)
{
var formatIdWraper = new StandardFormatIdWraper(format);

registeredFormats.Add(formatIdWraper.FormatName, formatIdWraper.FormatId);
}

ClipboardOwner = ownerHwnd;
}

public void OpenRead()
protected Clipboard()
: this(IntPtr.Zero)
{
this.Open(IntPtr.Zero);
}

public void Open(IntPtr hWnd)
public static Clipboard CreateReadOnly()
{
if (ClipboardOwner != notOwned)
throw new ClipboardOpenedException("Clipboard allready opened");
return new Clipboard();
}

public static Clipboard CreateReadWrite(IntPtr ownerHwnd)
{
return new Clipboard(ownerHwnd);
}

public void OpenReadOnly()
{
OpenInt();
}

var opened = OpenClipboard(hWnd);
public void Open()
{
if (ClipboardOwner == IntPtr.Zero)
throw new OpenClipboardException("Empty window handle is allowed only for Read only mode.To be able to write to clipboard Clipboard(IntPtr ownerHwnd) constructor should be used.");
OpenInt();
}

protected void OpenInt()
{
if (Owned)
throw new ClipboardOpenedException("Clipboard allready opened");
Owned = true;
var opened = OpenClipboard(ClipboardOwner);
if (!opened)
{
var errcode = Marshal.GetLastWin32Error();
var innerException=Marshal.GetExceptionForHR(errcode);
throw new OpenClipboardException(new System.ComponentModel.Win32Exception());
var innerException = Marshal.GetExceptionForHR(errcode);
Owned = false;
throw new OpenClipboardException(new Win32Exception());
}
ClipboardOwner = hWnd;
}

public void Clear()
{
GuardClipbordOpened();
if (!EmptyClipboard())
{
throw new ClipboardException("Error clearing clipboard", ExceptionHelpers.GetLastWin32Exception());
throw new ClipboardException("Can't clear clipboard content", ExceptionHelpers.GetLastWin32Exception());
}
}

private void GuardClipbordOpened(bool checkWindowHandle=false)
{
if (ClipboardOwner == notOwned)
throw new ClipboardClosedException("Cannot perform operation on closed clipbord");
if (!Owned)
throw new ClipboardClosedException("Operation cannot be performed on the closed clipbord");
if (checkWindowHandle && ClipboardOwner == IntPtr.Zero)
throw new ClipboardClosedException("This operation reqare clipboard to be opened with set hwnd.");
throw new ClipboardClosedException("This operation requare clipboard oened in Write mode.");
}


public T GetData<T>(IClipbordFormatProvider<T> provider)
{
var formatId = GetFormatId(provider.FormatId);
if (!IsDataAvailable(provider))
throw new ClipboardDataException("No data avalible in selected format", ExceptionHelpers.GetLastWin32Exception());
throw new ClipboardDataException("There no data of selected format in the Clipboard", ExceptionHelpers.GetLastWin32Exception());

GuardClipbordOpened();

IntPtr memHandle = GetClipboardData(formatId);
if (memHandle == IntPtr.Zero)
throw new ClipboardDataException("Error receiving clipbord data", ExceptionHelpers.GetLastWin32Exception());
throw new ClipboardDataException("Can't receive data from clipbord", ExceptionHelpers.GetLastWin32Exception());

try
{
Expand All @@ -93,7 +116,7 @@ public T GetData<T>(IClipbordFormatProvider<T> provider)
}
catch (GlobalMemoryException exception)
{
throw new ClipboardDataException("Error receiving data from clipbord",exception);
throw new ClipboardDataException("Can't receive data from clipbord",exception);
}
}

Expand Down Expand Up @@ -126,7 +149,7 @@ public void SetData<T>(T data,IClipbordFormatProvider<T> provider)
}
catch (GlobalMemoryException exception)
{
throw new ClipboardDataException("Error saveing data to clipbord", exception);
throw new ClipboardDataException("Can't save data to clipbord", exception);
}
}

Expand Down Expand Up @@ -161,13 +184,6 @@ private uint GetFormatId(string formatId)
[DllImport("user32.dll")]
private static extern bool EmptyClipboard();

public static uint SequenceNumber
{
get { return GetClipboardSequenceNumber(); }
}

[DllImport("user32.dll")]
private static extern uint GetClipboardSequenceNumber();

[DllImport("user32.dll",SetLastError = true)]
private static extern IntPtr GetClipboardData(uint uFormat);
Expand Down Expand Up @@ -239,7 +255,7 @@ public IEnumerable<IClipbordFormatProvider> GetAvalibleFromats(IEnumerable<IClip

public void Close()
{
if (ClipboardOwner == notOwned)
if (!Owned)
return;
Dispose();
}
Expand All @@ -259,7 +275,7 @@ protected virtual void Dispose(bool disposing)
}
disposed = true;
}
if (ClipboardOwner != notOwned)
if (Owned)
CloseClipboard();
ClipboardOwner = new IntPtr(-1);
}
Expand Down
6 changes: 1 addition & 5 deletions ClipboardHelper/DataFormat.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace ClipboardHelper
{
Expand Down
2 changes: 0 additions & 2 deletions ClipboardHelper/FormatProviders/ClipboardFormatProviders.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.IO;
using System.Reflection.Emit;
using System.Text;
using ClipboardHelper.FormatProviders;

namespace ClipboardHelper
{
Expand Down
2 changes: 0 additions & 2 deletions ClipboardHelper/FormatProviders/HtmlFormatProvider.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ClipboardHelper.FormatProviders
{
Expand Down
1 change: 0 additions & 1 deletion ClipboardHelper/FormatProviders/SkypeFormatProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.Xml.Schema;
Expand Down
4 changes: 0 additions & 4 deletions ClipboardHelper/FormatProviders/UnknownFormatProvider.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClipboardHelper.FormatProviders
{
Expand Down
18 changes: 18 additions & 0 deletions ClipboardHelper/Helpers/GenericEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClipboardHelper.Helpers
{
public class EventArgs<T> : EventArgs
{
public T Value { get; private set; }

public EventArgs(T value)
{
this.Value = value;
}
}
}
8 changes: 2 additions & 6 deletions ClipboardHelper/Helpers/Strings.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClipboardHelper.Helpers
{
Expand All @@ -11,8 +7,8 @@ public static class Strings
public static Tuple<string, string> SplitString(this string str, char separator)
{
var index = str.IndexOf(separator);
var str1 = str.Substring(index);
var str2 = str.Substring(0, index);
var str2 = str.Length > index?str.Substring(index + 1):string.Empty;
var str1 = str.Substring(0, index);
return new Tuple<string, string>(str1, str2);
}
}
Expand Down
1 change: 0 additions & 1 deletion ClipboardHelper/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
Expand Down
Loading

0 comments on commit 693dee3

Please sign in to comment.