-
Notifications
You must be signed in to change notification settings - Fork 997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restructure DataObject #12792
Draft
lonitra
wants to merge
3
commits into
dotnet:main
Choose a base branch
from
lonitra:refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Restructure DataObject #12792
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
[assembly: SuppressMessage("Usage", "CA2201:Do not raise reserved exception types", Justification = "Compat", Scope = "member", Target = "~M:System.Private.Windows.Core.OLE.DesktopDataObject.Composition.NativeToDesktopAdapter.ReadByteStreamFromHGLOBAL(Windows.Win32.Foundation.HGLOBAL,System.Boolean@)~System.IO.MemoryStream")] | ||
[assembly: SuppressMessage("Usage", "CA2201:Do not raise reserved exception types", Justification = "Compat", Scope = "member", Target = "~M:System.Private.Windows.Core.OLE.DesktopDataObject.Composition.NativeToRuntimeAdapter.System#Runtime#InteropServices#ComTypes#IDataObject#EnumFormatEtc(System.Runtime.InteropServices.ComTypes.DATADIR)~System.Runtime.InteropServices.ComTypes.IEnumFORMATETC")] | ||
[assembly: SuppressMessage("Usage", "CA2201:Do not raise reserved exception types", Justification = "Compat", Scope = "member", Target = "~M:System.Private.Windows.Core.OLE.DesktopClipboard.SetDataObject(System.Object,System.Boolean,System.Int32,System.Int32)")] | ||
[assembly: SuppressMessage("Usage", "CA2201:Do not raise reserved exception types", Justification = "Compat", Scope = "member", Target = "~M:System.Private.Windows.Core.OLE.DesktopClipboard.GetDataObject~System.Private.Windows.Core.OLE.IDataObjectDesktop")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
273 changes: 128 additions & 145 deletions
273
...Private.Windows.Core/src/OLE/Clipboard.cs → ....Windows.Core/src/OLE/DesktopClipboard.cs
Large diffs are not rendered by default.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
src/System.Private.Windows.Core/src/OLE/DesktopDataFormats.Format.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace System.Private.Windows.Core.OLE; | ||
|
||
/// <summary> | ||
/// Translates between WinForms text-based <see cref="DesktopClipboard"/> | ||
/// formats and Win32 32-bit signed integer-based clipboard | ||
/// formats. Provides <see langword="static"/> methods to create new | ||
/// <see cref="DesktopClipboard"/> formats and add them to the Windows Registry. | ||
/// </summary> | ||
internal static partial class DesktopDataFormats | ||
{ | ||
/// <summary> | ||
/// Represents a format type. | ||
/// </summary> | ||
public class Format | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Format"/> class and | ||
/// specifies whether a Win32 handle is expected with this format. | ||
/// </summary> | ||
public Format(string name, int id) | ||
{ | ||
Name = name; | ||
Id = id; | ||
} | ||
|
||
/// <summary> | ||
/// Specifies the name of this format. | ||
/// </summary> | ||
public string Name { get; } | ||
|
||
/// <summary> | ||
/// Specifies the ID number for this format. | ||
/// </summary> | ||
public int Id { get; } | ||
} | ||
} |
193 changes: 193 additions & 0 deletions
193
src/System.Private.Windows.Core/src/OLE/DesktopDataFormats.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.ComponentModel; | ||
using System.Private.Windows.Core.Resources; | ||
using System.Runtime.InteropServices; | ||
using Windows.Win32; | ||
using Windows.Win32.System.Ole; | ||
|
||
namespace System.Private.Windows.Core.OLE; | ||
|
||
/// <summary> | ||
/// Translates between WinForms text-based <see cref="DesktopClipboard"/> | ||
/// formats and Win32 32-bit signed integer-based clipboard | ||
/// formats. Provides <see langword="static"/> methods to create new | ||
/// <see cref="DesktopClipboard"/> formats and add them to the Windows Registry. | ||
/// </summary> | ||
internal static partial class DesktopDataFormats | ||
{ | ||
internal const string TextConstant = "Text"; | ||
internal const string UnicodeTextConstant = "UnicodeText"; | ||
internal const string DibConstant = "DeviceIndependentBitmap"; | ||
internal const string BitmapConstant = "Bitmap"; | ||
internal const string EmfConstant = "EnhancedMetafile"; | ||
internal const string WmfConstant = "MetaFilePict"; | ||
internal const string SymbolicLinkConstant = "SymbolicLink"; | ||
internal const string DifConstant = "DataInterchangeFormat"; | ||
internal const string TiffConstant = "TaggedImageFileFormat"; | ||
internal const string OemTextConstant = "OEMText"; | ||
internal const string PaletteConstant = "Palette"; | ||
internal const string PenDataConstant = "PenData"; | ||
internal const string RiffConstant = "RiffAudio"; | ||
internal const string WaveAudioConstant = "WaveAudio"; | ||
internal const string FileDropConstant = "FileDrop"; | ||
internal const string LocaleConstant = "Locale"; | ||
internal const string HtmlConstant = "HTML Format"; | ||
internal const string RtfConstant = "Rich Text Format"; | ||
internal const string CsvConstant = "Csv"; | ||
internal const string StringConstant = "System.String"; | ||
internal const string SerializableConstant = "WindowsForms10PersistentObject"; | ||
|
||
private static Format[]? s_formatList; | ||
|
||
private static int s_formatCount; | ||
|
||
#if NET9_0_OR_GREATER | ||
private static readonly Lock s_internalSyncObject = new(); | ||
#else | ||
private static readonly object s_internalSyncObject = new(); | ||
#endif | ||
|
||
/// <summary> | ||
/// Gets a <see cref="Format"/> with the Windows Clipboard numeric ID and name for the specified format. | ||
/// </summary> | ||
public static Format GetFormat(string format) | ||
{ | ||
ArgumentException.ThrowIfNullOrWhiteSpace(format); | ||
|
||
lock (s_internalSyncObject) | ||
{ | ||
EnsurePredefined(); | ||
|
||
// It is much faster to do a case sensitive search here. | ||
// So do the case sensitive compare first, then the expensive one. | ||
for (int n = 0; n < s_formatCount; n++) | ||
{ | ||
if (s_formatList[n].Name.Equals(format)) | ||
{ | ||
return s_formatList[n]; | ||
} | ||
} | ||
|
||
for (int n = 0; n < s_formatCount; n++) | ||
{ | ||
if (string.Equals(s_formatList[n].Name, format, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return s_formatList[n]; | ||
} | ||
} | ||
|
||
// Need to add this format string | ||
uint formatId = PInvokeCore.RegisterClipboardFormat(format); | ||
if (formatId == 0) | ||
{ | ||
throw new Win32Exception(Marshal.GetLastWin32Error(), SR.RegisterCFFailed); | ||
} | ||
|
||
EnsureFormatSpace(1); | ||
s_formatList[s_formatCount] = new Format(format, (int)formatId); | ||
return s_formatList[s_formatCount++]; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets a <see cref="Format"/> with the Windows Clipboard numeric ID and name for the specified ID. | ||
/// </summary> | ||
internal static unsafe Format GetFormat(ushort id) | ||
{ | ||
lock (s_internalSyncObject) | ||
{ | ||
EnsurePredefined(); | ||
|
||
for (int n = 0; n < s_formatCount; n++) | ||
{ | ||
if (s_formatList[n].Id == id) | ||
{ | ||
return s_formatList[n]; | ||
} | ||
} | ||
|
||
string? name = null; | ||
|
||
// The max length of the name of clipboard formats is equal to the max length | ||
// of a Win32 Atom of 255 chars. An additional null terminator character is added, | ||
// giving a required capacity of 256 chars. | ||
Span<char> formatName = stackalloc char[256]; | ||
fixed (char* pFormatName = formatName) | ||
{ | ||
int length = PInvokeCore.GetClipboardFormatName(id, pFormatName, 256); | ||
if (length != 0) | ||
{ | ||
name = formatName[..length].ToString(); | ||
} | ||
} | ||
|
||
// This can happen if windows adds a standard format that we don't know about, | ||
// so we should play it safe. | ||
name ??= $"Format{id}"; | ||
|
||
EnsureFormatSpace(1); | ||
s_formatList[s_formatCount] = new Format(name, id); | ||
return s_formatList[s_formatCount++]; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Ensures that we have enough room in our format list | ||
/// </summary> | ||
[MemberNotNull(nameof(s_formatList))] | ||
private static void EnsureFormatSpace(int size) | ||
{ | ||
if (s_formatList is null || s_formatList.Length <= s_formatCount + size) | ||
{ | ||
int newSize = s_formatCount + 20; | ||
|
||
Format[] newList = new Format[newSize]; | ||
for (int n = 0; n < s_formatCount; n++) | ||
{ | ||
newList[n] = s_formatList![n]; | ||
} | ||
|
||
s_formatList = newList; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Ensures that the Win32 predefined formats are setup in our format list. | ||
/// This is called anytime we need to search the list | ||
/// </summary> | ||
[MemberNotNull(nameof(s_formatList))] | ||
private static void EnsurePredefined() | ||
{ | ||
if (s_formatCount == 0) | ||
{ | ||
s_formatList = | ||
[ | ||
// Text name Win32 format ID | ||
new(UnicodeTextConstant, (int)CLIPBOARD_FORMAT.CF_UNICODETEXT), | ||
new(TextConstant, (int)CLIPBOARD_FORMAT.CF_TEXT), | ||
new(BitmapConstant, (int)CLIPBOARD_FORMAT.CF_BITMAP), | ||
new(WmfConstant, (int)CLIPBOARD_FORMAT.CF_METAFILEPICT), | ||
new(EmfConstant, (int)CLIPBOARD_FORMAT.CF_ENHMETAFILE), | ||
new(DifConstant, (int)CLIPBOARD_FORMAT.CF_DIF), | ||
new(TiffConstant, (int)CLIPBOARD_FORMAT.CF_TIFF), | ||
new(OemTextConstant, (int)CLIPBOARD_FORMAT.CF_OEMTEXT), | ||
new(DibConstant, (int)CLIPBOARD_FORMAT.CF_DIB), | ||
new(PaletteConstant, (int)CLIPBOARD_FORMAT.CF_PALETTE), | ||
new(PenDataConstant, (int)CLIPBOARD_FORMAT.CF_PENDATA), | ||
new(RiffConstant, (int)CLIPBOARD_FORMAT.CF_RIFF), | ||
new(WaveAudioConstant, (int)CLIPBOARD_FORMAT.CF_WAVE), | ||
new(SymbolicLinkConstant, (int)CLIPBOARD_FORMAT.CF_SYLK), | ||
new(FileDropConstant, (int)CLIPBOARD_FORMAT.CF_HDROP), | ||
new(LocaleConstant, (int)CLIPBOARD_FORMAT.CF_LOCALE) | ||
]; | ||
|
||
s_formatCount = s_formatList.Length; | ||
} | ||
else | ||
{ | ||
s_formatList ??= []; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We used the word
Windows
, notDesktop
as a prefix to indicate applicability to both WPF and winforms in the AppContext switch name. Should these implementation classes follow this pattern? OrCore
to make it shorter?