From fe6e3c7a4f631237aec358ff98d02a52ff17b245 Mon Sep 17 00:00:00 2001 From: Igor Velikorossov Date: Wed, 16 Oct 2019 15:01:47 +1100 Subject: [PATCH 1/2] Port BinaryEditor from NET Fx Closes #2017 --- .../Kernel32/Interop.MultiByteToWideChar.cs | 20 + .../tests/UnitTests/EnsureEditorsTests.cs | 6 +- .../src/Resources/SR.resx | 17 +- .../ComponentModel/Design/BinaryEditor.resx | 480 +++++++++++ .../ComponentModel/Design/BinaryEditor.cs | 408 ++++++++++ .../ComponentModel/Design/ByteViewer.cs | 745 ++++++++++++++++++ .../ComponentModel/Design/DisplayMode.cs | 53 ++ 7 files changed, 1726 insertions(+), 3 deletions(-) create mode 100644 src/Common/src/Interop/Kernel32/Interop.MultiByteToWideChar.cs create mode 100644 src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/BinaryEditor.resx create mode 100644 src/System.Windows.Forms.Design/src/System/ComponentModel/Design/BinaryEditor.cs create mode 100644 src/System.Windows.Forms.Design/src/System/ComponentModel/Design/ByteViewer.cs create mode 100644 src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DisplayMode.cs diff --git a/src/Common/src/Interop/Kernel32/Interop.MultiByteToWideChar.cs b/src/Common/src/Interop/Kernel32/Interop.MultiByteToWideChar.cs new file mode 100644 index 00000000000..9ae5bdb6c0a --- /dev/null +++ b/src/Common/src/Interop/Kernel32/Interop.MultiByteToWideChar.cs @@ -0,0 +1,20 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Kernel32 + { + // SECREVIEW: From MSDN: Using the MultiByteToWideChar/WideCharToMultiByte function incorrectly can compromise the security of your application. + // Calling the WideCharToMultiByte function can easily cause a buffer overrun because the size of the In buffer equals the number + // of WCHARs in the string, while the size of the Out buffer equals the number of bytes. To avoid a buffer overrun, be sure to specify + // a buffer size appropriate for the data type the buffer receives. For more information, see Security Considerations: International Features. + // Always call these functions passing a null destination buffer to get its size and the create the buffer with the exact size. + [DllImport(ExternDll.Kernel32, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)] + public static extern int MultiByteToWideChar(int CodePage, int dwFlags, byte[] lpMultiByteStr, int cchMultiByte, char[] lpWideCharStr, int cchWideChar); + } +} diff --git a/src/System.Windows.Forms.Design.Editors/tests/UnitTests/EnsureEditorsTests.cs b/src/System.Windows.Forms.Design.Editors/tests/UnitTests/EnsureEditorsTests.cs index 12b95a2c209..b52a672590b 100644 --- a/src/System.Windows.Forms.Design.Editors/tests/UnitTests/EnsureEditorsTests.cs +++ b/src/System.Windows.Forms.Design.Editors/tests/UnitTests/EnsureEditorsTests.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. @@ -9,6 +9,7 @@ using System.Drawing; using System.Drawing.Design; using System.Drawing.Imaging; +using System.IO; using System.Linq; using System.Reflection; using Xunit; @@ -22,7 +23,8 @@ public class EnsureEditorsTests [InlineData(typeof(Array), typeof(ArrayEditor))] [InlineData(typeof(IList), typeof(CollectionEditor))] [InlineData(typeof(ICollection), typeof(CollectionEditor))] - [InlineData(typeof(byte[]), typeof(ArrayEditor))] + [InlineData(typeof(byte[]), typeof(BinaryEditor))] + [InlineData(typeof(Stream), typeof(BinaryEditor))] [InlineData(typeof(string[]), typeof(StringArrayEditor))] [InlineData(typeof(Bitmap), typeof(BitmapEditor))] [InlineData(typeof(Color), typeof(ColorEditor))] diff --git a/src/System.Windows.Forms.Design/src/Resources/SR.resx b/src/System.Windows.Forms.Design/src/Resources/SR.resx index edb4b780160..ff7b603fc93 100644 --- a/src/System.Windows.Forms.Design/src/Resources/SR.resx +++ b/src/System.Windows.Forms.Design/src/Resources/SR.resx @@ -753,4 +753,19 @@ '{0}' - + + All Files + + + Error writing to file: {0} + + + ByteArray + + + Save File As + + + Byte Array Property Editor + + \ No newline at end of file diff --git a/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/BinaryEditor.resx b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/BinaryEditor.resx new file mode 100644 index 00000000000..9847a579d99 --- /dev/null +++ b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/BinaryEditor.resx @@ -0,0 +1,480 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + + + 0, 0 + + + 634, 192 + + + + 0 + + + + + + byteViewer + + + System.Windows.Forms.RichTextBox, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + overarchingTableLayoutPanel + + + 0 + + + Left, Right + + + True + + + 0, 0 + + + 75, 23 + + + 0 + + + OK + + + buttonOK + + + System.Windows.Forms.Button, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + okSaveTableLayoutPanel + + + 0 + + + Left, Right + + + True + + + 81, 0 + + + 75, 23 + + + 1 + + + &Save... + + + buttonSave + + + System.Windows.Forms.Button, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + okSaveTableLayoutPanel + + + 1 + + + Left, Right + + + True + + + True + + + 4 + + + Left + + + True + + + 462, 0 + + + 61, 17 + + + 3 + + + &Unicode + + + radioUnicode + + + System.Windows.Forms.RadioButton, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + radioButtonsTableLayoutPanel + + + 0 + + + Left + + + True + + + 0, 0 + + + 43, 17 + + + 0 + + + &Auto + + + radioAuto + + + System.Windows.Forms.RadioButton, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + radioButtonsTableLayoutPanel + + + 1 + + + Left + + + True + + + 309, 0 + + + 46, 17 + + + 2 + + + A&NSI + + + radioAnsi + + + System.Windows.Forms.RadioButton, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + radioButtonsTableLayoutPanel + + + 2 + + + Left + + + True + + + 156, 0 + + + 82, 17 + + + 1 + + + &Hexadecimal + + + radioHex + + + System.Windows.Forms.RadioButton, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + radioButtonsTableLayoutPanel + + + 3 + + + 9, 22 + + + 1 + + + 613, 17 + + + 0 + + + radioButtonsTableLayoutPanel + + + System.Windows.Forms.TableLayoutPanel, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + groupBoxMode + + + 0 + + + <?xml version="1.0" encoding="utf-16"?><TableLayoutSettings><Controls><Control Name="radioUnicode" Row="0" RowSpan="1" Column="3" ColumnSpan="1" /><Control Name="radioAuto" Row="0" RowSpan="1" Column="0" ColumnSpan="1" /><Control Name="radioAnsi" Row="0" RowSpan="1" Column="2" ColumnSpan="1" /><Control Name="radioHex" Row="0" RowSpan="1" Column="1" ColumnSpan="1" /></Controls><Columns Styles="Percent,25,Percent,25,Percent,25,Percent,25" /><Rows Styles="AutoSize,0" /></TableLayoutSettings> + + + 0, 198 + + + 634, 48 + + + 1 + + + Display Mode + + + groupBoxMode + + + System.Windows.Forms.GroupBox, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + overarchingTableLayoutPanel + + + 1 + + + Right + + + True + + + 2 + + + 472, 255 + + + 1 + + + 156, 23 + + + 2 + + + okSaveTableLayoutPanel + + + System.Windows.Forms.TableLayoutPanel, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + overarchingTableLayoutPanel + + + 2 + + + <?xml version="1.0" encoding="utf-16"?><TableLayoutSettings><Controls><Control Name="buttonOK" Row="0" RowSpan="1" Column="0" ColumnSpan="1" /><Control Name="buttonSave" Row="0" RowSpan="1" Column="1" ColumnSpan="1" /></Controls><Columns Styles="Percent,50,Percent,50" /><Rows Styles="Percent,50" /></TableLayoutSettings> + + + Top, Bottom, Left, Right + + + 1 + + + 12, 12 + + + 3 + + + True + + + 634, 291 + + + 0 + + + overarchingTableLayoutPanel + + + System.Windows.Forms.TableLayoutPanel, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + $this + + + 0 + + + <?xml version="1.0" encoding="utf-16"?><TableLayoutSettings><Controls><Control Name="byteViewer" Row="0" RowSpan="1" Column="0" ColumnSpan="1" /><Control Name="groupBoxMode" Row="1" RowSpan="1" Column="0" ColumnSpan="1" /><Control Name="okSaveTableLayoutPanel" Row="2" RowSpan="1" Column="0" ColumnSpan="1" /></Controls><Columns Styles="Percent,50" /><Rows Styles="Percent,100,AutoSize,0,AutoSize,0" /></TableLayoutSettings> + + + True + + + 6, 13 + + + 658, 315 + + + CenterParent + + + Byte Array Property Editor + + + BinaryUI + + + System.Windows.Forms.Form, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + diff --git a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/BinaryEditor.cs b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/BinaryEditor.cs new file mode 100644 index 00000000000..fc5ac478271 --- /dev/null +++ b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/BinaryEditor.cs @@ -0,0 +1,408 @@ +//------------------------------------------------------------------------------ +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// +//------------------------------------------------------------------------------ + +namespace System.ComponentModel.Design { + + using System.Design; + using System; + using System.Text; + using System.IO; + using System.ComponentModel; + using System.Diagnostics; + using System.Diagnostics.CodeAnalysis; + using System.Windows.Forms; + using System.Drawing; + using System.Drawing.Design; + using Microsoft.Win32; + using System.Windows.Forms.Design; + using System.Windows.Forms.ComponentModel; + + /// + internal class BinaryUI : System.Windows.Forms.Form { + private BinaryEditor editor; + object value; + + private RadioButton radioAuto; + private Button buttonSave; + private Button buttonOK; + private ByteViewer byteViewer; + private GroupBox groupBoxMode; + private RadioButton radioHex; + private RadioButton radioAnsi; + private TableLayoutPanel radioButtonsTableLayoutPanel; + private TableLayoutPanel okSaveTableLayoutPanel; + private TableLayoutPanel overarchingTableLayoutPanel; + private RadioButton radioUnicode; + + public BinaryUI(BinaryEditor editor) { + this.editor = editor; + InitializeComponent(); + } + + public object Value { + get { + return value; + } + set { + this.value = value; + byte[] bytes = null; + + if (value != null) { + bytes = editor.ConvertToBytes(value); + } + + if (bytes != null) { + byteViewer.SetBytes(bytes); + byteViewer.Enabled = true; + } else { + byteViewer.SetBytes(new byte[0]); + byteViewer.Enabled = false; + } + } + } + + private void RadioAuto_checkedChanged(object source, EventArgs e) { + if (radioAuto.Checked) + byteViewer.SetDisplayMode(DisplayMode.Auto); + } + + private void RadioHex_checkedChanged(object source, EventArgs e) { + if (radioHex.Checked) + byteViewer.SetDisplayMode(DisplayMode.Hexdump); + } + + private void RadioAnsi_checkedChanged(object source, EventArgs e) { + if (radioAnsi.Checked) + byteViewer.SetDisplayMode(DisplayMode.Ansi); + } + + private void RadioUnicode_checkedChanged(object source, EventArgs e) { + if (radioUnicode.Checked) + byteViewer.SetDisplayMode(DisplayMode.Unicode); + } + + private void ButtonOK_click(object source, EventArgs e) { + object localValue = value; + editor.ConvertToValue(byteViewer.GetBytes(), ref localValue); + value = localValue; + } + + private void ButtonSave_click(object source, EventArgs e) { + try { + SaveFileDialog sfd = new SaveFileDialog(); + sfd.FileName = SR.GetString(SR.BinaryEditorFileName); + sfd.Title = SR.GetString(SR.BinaryEditorSaveFile); + sfd.Filter = SR.GetString(SR.BinaryEditorAllFiles) + " (*.*)|*.*"; + + DialogResult result = sfd.ShowDialog(); + if (result == DialogResult.OK) { + byteViewer.SaveToFile(sfd.FileName); + } + } catch (IOException x) { + RTLAwareMessageBox.Show(null, SR.GetString(SR.BinaryEditorFileError, x.Message), + SR.GetString(SR.BinaryEditorTitle), MessageBoxButtons.OK, + MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0); + } + } + + private void Form_HelpRequested(object sender, HelpEventArgs e) { + editor.ShowHelp(); + } + + private void Form_HelpButtonClicked(object sender, CancelEventArgs e) + { + e.Cancel = true; + editor.ShowHelp(); + } + + private void InitializeComponent() { + System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(BinaryEditor)); + this.byteViewer = new ByteViewer(); + this.buttonOK = new System.Windows.Forms.Button(); + this.buttonSave = new System.Windows.Forms.Button(); + this.groupBoxMode = new System.Windows.Forms.GroupBox(); + this.radioButtonsTableLayoutPanel = new System.Windows.Forms.TableLayoutPanel(); + this.radioUnicode = new System.Windows.Forms.RadioButton(); + this.radioAuto = new System.Windows.Forms.RadioButton(); + this.radioAnsi = new System.Windows.Forms.RadioButton(); + this.radioHex = new System.Windows.Forms.RadioButton(); + this.okSaveTableLayoutPanel = new System.Windows.Forms.TableLayoutPanel(); + this.overarchingTableLayoutPanel = new System.Windows.Forms.TableLayoutPanel(); + this.byteViewer.SuspendLayout(); + this.groupBoxMode.SuspendLayout(); + this.radioButtonsTableLayoutPanel.SuspendLayout(); + this.okSaveTableLayoutPanel.SuspendLayout(); + this.overarchingTableLayoutPanel.SuspendLayout(); + this.SuspendLayout(); + + // + // byteViewer + // + resources.ApplyResources(this.byteViewer, "byteViewer"); + this.byteViewer.SetDisplayMode(DisplayMode.Auto); + this.byteViewer.Name = "byteViewer"; + this.byteViewer.Margin = Padding.Empty; + this.byteViewer.Dock = DockStyle.Fill; + // + // buttonOK + // + resources.ApplyResources(this.buttonOK, "buttonOK"); + this.buttonOK.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; + this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.OK; + this.buttonOK.Margin = new System.Windows.Forms.Padding(0, 0, 3, 0); + this.buttonOK.MinimumSize = new System.Drawing.Size(75, 23); + this.buttonOK.Name = "buttonOK"; + this.buttonOK.Padding = new System.Windows.Forms.Padding(10, 0, 10, 0); + this.buttonOK.Click += new System.EventHandler(this.ButtonOK_click); + // + // buttonSave + // + resources.ApplyResources(this.buttonSave, "buttonSave"); + this.buttonSave.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; + this.buttonSave.Margin = new System.Windows.Forms.Padding(3, 0, 0, 0); + this.buttonSave.MinimumSize = new System.Drawing.Size(75, 23); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Padding = new System.Windows.Forms.Padding(10, 0, 10, 0); + this.buttonSave.Click += new System.EventHandler(this.ButtonSave_click); + // + // groupBoxMode + // + resources.ApplyResources(this.groupBoxMode, "groupBoxMode"); + this.groupBoxMode.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; + this.groupBoxMode.Controls.Add(this.radioButtonsTableLayoutPanel); + this.groupBoxMode.Margin = new System.Windows.Forms.Padding(0, 3, 0, 3); + this.groupBoxMode.Name = "groupBoxMode"; + this.groupBoxMode.Padding = new System.Windows.Forms.Padding(0); + this.groupBoxMode.TabStop = false; + // + // radioButtonsTableLayoutPanel + // + resources.ApplyResources(this.radioButtonsTableLayoutPanel, "radioButtonsTableLayoutPanel"); + this.radioButtonsTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F)); + this.radioButtonsTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F)); + this.radioButtonsTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F)); + this.radioButtonsTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F)); + this.radioButtonsTableLayoutPanel.Controls.Add(this.radioUnicode, 3, 0); + this.radioButtonsTableLayoutPanel.Controls.Add(this.radioAuto, 0, 0); + this.radioButtonsTableLayoutPanel.Controls.Add(this.radioAnsi, 2, 0); + this.radioButtonsTableLayoutPanel.Controls.Add(this.radioHex, 1, 0); + this.radioButtonsTableLayoutPanel.Margin = new System.Windows.Forms.Padding(9); + this.radioButtonsTableLayoutPanel.Name = "radioButtonsTableLayoutPanel"; + this.radioButtonsTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle()); + // + // radioUnicode + // + resources.ApplyResources(this.radioUnicode, "radioUnicode"); + this.radioUnicode.Margin = new System.Windows.Forms.Padding(3, 0, 0, 0); + this.radioUnicode.Name = "radioUnicode"; + this.radioUnicode.CheckedChanged += new System.EventHandler(this.RadioUnicode_checkedChanged); + // + // radioAuto + // + resources.ApplyResources(this.radioAuto, "radioAuto"); + this.radioAuto.Checked = true; + this.radioAuto.Margin = new System.Windows.Forms.Padding(0, 0, 3, 0); + this.radioAuto.Name = "radioAuto"; + this.radioAuto.CheckedChanged += new System.EventHandler(this.RadioAuto_checkedChanged); + // + // radioAnsi + // + resources.ApplyResources(this.radioAnsi, "radioAnsi"); + this.radioAnsi.Margin = new System.Windows.Forms.Padding(3, 0, 3, 0); + this.radioAnsi.Name = "radioAnsi"; + this.radioAnsi.CheckedChanged += new System.EventHandler(this.RadioAnsi_checkedChanged); + // + // radioHex + // + resources.ApplyResources(this.radioHex, "radioHex"); + this.radioHex.Margin = new System.Windows.Forms.Padding(3, 0, 3, 0); + this.radioHex.Name = "radioHex"; + this.radioHex.CheckedChanged += new System.EventHandler(this.RadioHex_checkedChanged); + // + // okSaveTableLayoutPanel + // + resources.ApplyResources(this.okSaveTableLayoutPanel, "okSaveTableLayoutPanel"); + this.okSaveTableLayoutPanel.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; + this.okSaveTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); + this.okSaveTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); + this.okSaveTableLayoutPanel.Controls.Add(this.buttonOK, 0, 0); + this.okSaveTableLayoutPanel.Controls.Add(this.buttonSave, 1, 0); + this.okSaveTableLayoutPanel.Margin = new System.Windows.Forms.Padding(0, 9, 0, 0); + this.okSaveTableLayoutPanel.Name = "okSaveTableLayoutPanel"; + this.okSaveTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); + // + // overarchingTableLayoutPanel + // + resources.ApplyResources(this.overarchingTableLayoutPanel, "overarchingTableLayoutPanel"); + this.overarchingTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); + this.overarchingTableLayoutPanel.Controls.Add(this.byteViewer, 0, 0); + this.overarchingTableLayoutPanel.Controls.Add(this.groupBoxMode, 0, 1); + this.overarchingTableLayoutPanel.Controls.Add(this.okSaveTableLayoutPanel, 0, 2); + this.overarchingTableLayoutPanel.Name = "overarchingTableLayoutPanel"; + this.overarchingTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); + this.overarchingTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle()); + this.overarchingTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle()); + + // + // BinaryUI + // + this.AcceptButton = this.buttonOK; + resources.ApplyResources(this, "$this"); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.CancelButton = this.buttonOK; + this.Controls.Add(this.overarchingTableLayoutPanel); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; + this.HelpButton = true; + this.MaximizeBox = false; + this.MinimizeBox = false; + this.Name = "BinaryUI"; + this.ShowIcon = false; + this.ShowInTaskbar = false; + this.HelpRequested += new System.Windows.Forms.HelpEventHandler(this.Form_HelpRequested); + this.HelpButtonClicked += new System.ComponentModel.CancelEventHandler(this.Form_HelpButtonClicked); + this.byteViewer.ResumeLayout(false); + this.byteViewer.PerformLayout(); + this.groupBoxMode.ResumeLayout(false); + this.groupBoxMode.PerformLayout(); + this.radioButtonsTableLayoutPanel.ResumeLayout(false); + this.radioButtonsTableLayoutPanel.PerformLayout(); + this.okSaveTableLayoutPanel.ResumeLayout(false); + this.okSaveTableLayoutPanel.PerformLayout(); + this.overarchingTableLayoutPanel.ResumeLayout(false); + this.overarchingTableLayoutPanel.PerformLayout(); + this.ResumeLayout(false); + + } + } + + /// + /// + /// Generic editor for editing binary data. This presents + /// a hex editing window to the user. + /// + public sealed class BinaryEditor : UITypeEditor { + private static readonly string HELP_KEYWORD = "System.ComponentModel.Design.BinaryEditor"; + private ITypeDescriptorContext context; + private BinaryUI binaryUI; + + internal object GetService(Type serviceType) { + if (this.context != null) { + IDesignerHost host = this.context.GetService(typeof(IDesignerHost)) as IDesignerHost; + if (host == null) + return this.context.GetService(serviceType); + else + return host.GetService(serviceType); + } + return null; + } + + /// + /// + /// Converts the given object to an array of bytes to be manipulated + /// by the editor. The default implementation of this supports + /// byte[] and stream objects. + /// + internal byte[] ConvertToBytes(object value) { + if (value is Stream) { + Stream s = (Stream)value; + s.Position = 0; + int byteCount = (int)(s.Length - s.Position); + byte[] bytes = new byte[byteCount]; + s.Read(bytes, 0, byteCount); + return bytes; + } + + if (value is byte[]) { + return (byte[])value; + } + + if (value is string) { + int size = ((string)value).Length * 2; + byte[] buffer = new byte[size]; + Encoding.Unicode.GetBytes(((string)value).ToCharArray(), 0, size / 2, buffer, 0); + return buffer; + } + + Debug.Fail("No conversion from " + value == null ? "null" : value.GetType().FullName + " to byte[]"); + return null; + } + + /// + /// + /// Converts the given byte array back into a native object. If + /// the object itself needs to be replace (as is the case for arrays), + /// then a new object may be assigned out through the parameter. + /// + internal void ConvertToValue(byte[] bytes, ref object value) { + + if (value is Stream) { + Stream s = (Stream)value; + s.Position = 0; + s.Write(bytes, 0, bytes.Length); + } else if (value is byte[]) { + value = bytes; + } else if (value is string) { + value = BitConverter.ToString(bytes); + } else { + Debug.Fail("No conversion from byte[] to " + value == null ? "null" : value.GetType().FullName); + } + } + + /// + /// + /// Edits the given object value using the editor style provided by + /// GetEditorStyle. A service provider is provided so that any + /// required editing services can be obtained. + /// + [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")] // everything in this assembly is full trust. + public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { + if (provider != null) { + this.context = context; + + IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); + + if (edSvc != null) { + if (binaryUI == null) { + + // child modal dialog -launching in System Aware mode + binaryUI = DpiHelper.CreateInstanceInSystemAwareContext(() => new BinaryUI(this)); + } + + binaryUI.Value = value; + + if (edSvc.ShowDialog(binaryUI) == DialogResult.OK) { + value = binaryUI.Value; + } + + binaryUI.Value = null; + } + } + + return value; + } + + /// + /// + /// Retrieves the editing style of the Edit method. If the method + /// is not supported, this will return None. + /// + [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")] // everything in this assembly is full trust. + public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { + return UITypeEditorEditStyle.Modal; + } + + internal void ShowHelp() { + IHelpService helpService = GetService(typeof(IHelpService)) as IHelpService; + if (helpService != null) { + helpService.ShowHelpFromKeyword(HELP_KEYWORD); + } else { + Debug.Fail("Unable to get IHelpService."); + } + } + } +} + + diff --git a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/ByteViewer.cs b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/ByteViewer.cs new file mode 100644 index 00000000000..90912cde98a --- /dev/null +++ b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/ByteViewer.cs @@ -0,0 +1,745 @@ +//------------------------------------------------------------------------------ +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// +//------------------------------------------------------------------------------ + +namespace System.ComponentModel.Design { + using System.Design; + using System.Text; + using System.Runtime.Serialization.Formatters; + using System.Runtime.InteropServices; + using System.Diagnostics; + using System; + using System.ComponentModel; + using System.Windows.Forms; + using System.IO; + using System.Globalization; + using System.Drawing; + using Microsoft.Win32; + + /// + /// + /// Displays byte arrays in + /// HEXDUMP, ANSI and Unicode formats. + /// + [ + ToolboxItem(false), + DesignTimeVisible(false) + ] + public class ByteViewer : TableLayoutPanel { + private const int DEFAULT_COLUMN_COUNT = 16; + private const int DEFAULT_ROW_COUNT = 25; + + private const int COLUMN_COUNT = 16; + + private const int BORDER_GAP = 2; + private const int INSET_GAP = 3; + + private const int CELL_HEIGHT = 21; + private const int CELL_WIDTH = 25; + private const int CHAR_WIDTH = 8; + + private const int ADDRESS_WIDTH = 69; // this is ceiling(sizeof("DDDDDDDD").width) + 1 + private const int HEX_WIDTH = CELL_WIDTH * COLUMN_COUNT; + private const int DUMP_WIDTH = CHAR_WIDTH * COLUMN_COUNT; + + private int SCROLLBAR_HEIGHT; + private int SCROLLBAR_WIDTH; + + + private const int HEX_DUMP_GAP = 5; + + private const int ADDRESS_START_X = BORDER_GAP + INSET_GAP; + private const int CLIENT_START_Y = BORDER_GAP + INSET_GAP; + private const int LINE_START_Y = CLIENT_START_Y + CELL_HEIGHT / 8; + private const int HEX_START_X = ADDRESS_START_X + ADDRESS_WIDTH; + private const int DUMP_START_X = HEX_START_X + HEX_WIDTH + HEX_DUMP_GAP; + private const int SCROLLBAR_START_X = DUMP_START_X + DUMP_WIDTH + HEX_DUMP_GAP; + + private static readonly Font ADDRESS_FONT = new Font("Microsoft Sans Serif", 8.0f); + private static readonly Font HEXDUMP_FONT = new Font("Courier New", 8.0f); + + private VScrollBar scrollBar; + private TextBox edit; + + private int columnCount = DEFAULT_COLUMN_COUNT; + private int rowCount = DEFAULT_ROW_COUNT; + + private byte[] dataBuf; + + private int startLine; + private int displayLinesCount; + private int linesCount; + + private DisplayMode displayMode; + private DisplayMode realDisplayMode; + + /// + /// + /// + /// Initializes a new instance of the class. + /// + /// + public ByteViewer() + : base() { + + this.SuspendLayout(); + this.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset; + + this.ColumnCount = 1; + this.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F)); + + this.RowCount = 1; + this.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); + + InitUI(); + + this.ResumeLayout(); + + displayMode = DisplayMode.Hexdump; + realDisplayMode = DisplayMode.Hexdump; + DoubleBuffered = true; + SetStyle(ControlStyles.ResizeRedraw, true); + + } + + //Stole this code from XmlSanner + private static int AnalizeByteOrderMark(byte[] buffer, int index) { + int c1 = buffer[index + 0] << 8 | buffer[index + 1]; + int c2 = buffer[index + 2] << 8 | buffer[index + 3]; + int c4,c5; + + //Assign an index (label) value for first two bytes + c4 = GetEncodingIndex(c1); + //Assign an index (label) value for 3rd and 4th byte + c5 = GetEncodingIndex(c2); + + //Bellow table is to identify Encoding type based on + //first four bytes, those we have converted in index + //values for this look up table + //values on column are first two bytes and + //values on rows are 3rd and 4th byte + + int[,] encodings = { + //Unknown 0000 feff fffe efbb 3c00 003c 3f00 003f 3c3f 786d 4c6f a794 + /*Unknown*/ {1 ,5 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 }, + /*0000*/ {1 ,1 ,1 ,11 ,1 ,10 ,4 ,1 ,1 ,1 ,1 ,1 ,1 }, + /*feff*/ {2 ,9 ,5 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 }, + /*fffe*/ {3 ,7 ,3 ,7 ,3 ,3 ,3 ,3 ,3 ,3 ,3 ,3 ,3 }, + /*efbb*/ {14 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 }, + /*3c00*/ {1 ,6 ,1 ,1 ,1 ,1 ,1 ,3 ,1 ,1 ,1 ,1 ,1 }, + /*003c*/ {1 ,8 ,1 ,1 ,1 ,1 ,1 ,1 ,2 ,1 ,1 ,1 ,1 }, + /*3f00*/ {1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 }, + /*003f*/ {1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 }, + /*3c3f*/ {1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,13 ,1 ,1 }, + /*786d*/ {1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 }, + /*4c6f*/ {1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,12 }, + /*a794*/ {1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 } + }; + + return encodings[c4,c5]; + } + + + /// + /// + /// Calculates an index for a cell in the HEX grid + /// + /// + private int CellToIndex(int column, int row) { + return row * columnCount + column; + } + + /// + /// + /// Copies the line from main data buffer to a line buffer + /// + /// + private byte[] ComposeLineBuffer(int startLine, int line) { + byte[] lineBuffer; + + int offset = startLine * columnCount; + if (offset + (line+1) * columnCount > dataBuf.Length) + lineBuffer = new byte[dataBuf.Length % columnCount]; + else + lineBuffer = new byte[columnCount]; + for (int i = 0; i < lineBuffer.Length; i++) + lineBuffer[i] = dataBuf[offset + CellToIndex(i, line)]; + return lineBuffer; + } + + /// + /// + /// Draws an adress part in the HEXDUMP view + /// + /// + private void DrawAddress(System.Drawing.Graphics g, int startLine, int line) { + Font font = ADDRESS_FONT; + + string hexString = ((startLine + line) * columnCount).ToString("X8", CultureInfo.InvariantCulture); + + Brush foreground = new SolidBrush(ForeColor); + try { + g.DrawString(hexString, font, foreground, + ADDRESS_START_X,LINE_START_Y + line * CELL_HEIGHT); + } + + finally { + if (foreground != null) { + foreground.Dispose(); + } + } + } + + /// + /// + /// Draws the client background and frames + /// + /// + private void DrawClient(System.Drawing.Graphics g) { + + using (Brush brush = new SolidBrush(SystemColors.ControlLightLight)) { + g.FillRectangle(brush, new Rectangle(HEX_START_X,CLIENT_START_Y + ,HEX_WIDTH + + HEX_DUMP_GAP + DUMP_WIDTH + HEX_DUMP_GAP, + rowCount * CELL_HEIGHT)); + } + + using (Pen pen = new Pen(SystemColors.ControlDark)) { + g.DrawRectangle(pen, new Rectangle(HEX_START_X,CLIENT_START_Y + ,HEX_WIDTH + + HEX_DUMP_GAP + DUMP_WIDTH + HEX_DUMP_GAP - 1, + rowCount * CELL_HEIGHT - 1)); + g.DrawLine(pen, DUMP_START_X - HEX_DUMP_GAP, CLIENT_START_Y, + DUMP_START_X - HEX_DUMP_GAP, CLIENT_START_Y + rowCount * CELL_HEIGHT - 1); + } + } + + // Char.IsPrintable is going away because it's a mostly meaningless concept. + // Copied code here to preserve semantics. -- BrianGru, 10/3/2000 + private static bool CharIsPrintable(char c) { + UnicodeCategory uc = Char.GetUnicodeCategory(c); + return (!(uc == UnicodeCategory.Control) || (uc == UnicodeCategory.Format) || + (uc == UnicodeCategory.LineSeparator) || (uc == UnicodeCategory.ParagraphSeparator) || + (uc == UnicodeCategory.OtherNotAssigned)); + } + + /// + /// + /// Draws the "DUMP" part in the HEXDUMP view + /// + /// + private void DrawDump(System.Drawing.Graphics g, byte[] lineBuffer, int line) { + char c; + StringBuilder sb = new StringBuilder(lineBuffer.Length); + for (int i = 0; i < lineBuffer.Length; i++) { + c = Convert.ToChar(lineBuffer[i]); + if (CharIsPrintable(c)) + sb.Append(c); + else + sb.Append('.'); + } + Font font = HEXDUMP_FONT; + Brush foreground = new SolidBrush(ForeColor); + try { + g.DrawString(sb.ToString(), font, foreground, DUMP_START_X, LINE_START_Y + line * CELL_HEIGHT); + } + + finally { + if (foreground != null) { + foreground.Dispose(); + } + } + } + + /// + /// + /// Draws the "HEX" part in the HEXDUMP view + /// + /// + private void DrawHex(System.Drawing.Graphics g, byte[] lineBuffer, int line) { + Font font = HEXDUMP_FONT; + + StringBuilder result = new StringBuilder(lineBuffer.Length * 3 + 1); + for (int i = 0; i < lineBuffer.Length; i++) { + result.Append(lineBuffer[i].ToString("X2", CultureInfo.InvariantCulture)); + result.Append(" "); + if (i == columnCount/2 - 1) + result.Append(" "); //add one extra in the middle + } + Brush foreground = new SolidBrush(ForeColor); + try { + g.DrawString(result.ToString(), font, foreground, HEX_START_X + BORDER_GAP, LINE_START_Y + line * CELL_HEIGHT); + } + + finally { + if (foreground != null) { + foreground.Dispose(); + } + } + + /* ISSUE a-gregka: If perf problem, could be done this way to eliminate drawing twice on repaint + The current solution good enough for a dialog box + int hdc = g.getHandle(); + Windows.SelectObject(hdc, HEXDUMP_FONT.getHandle(g)); + Windows.ExtTextOut(hdc, HEX_START_X, LINE_START_Y - 1 + line * CELL_HEIGHT, + win.ETO_OPAQUE, + Utils.createRECT(HEX_START_X, LINE_START_Y -1 + line * CELL_HEIGHT, HEX_WIDTH, CELL_HEIGHT), + out, columnCount * 3 + 1, null); + */ + } + + /// + /// + /// Draws the all lines (a line includes address, hex and dump part) + /// for the HEXDUMP view + /// + /// + private void DrawLines(System.Drawing.Graphics g, int startLine, int linesCount) { + for (int i = 0; i < linesCount; i++) { + byte[] lineBuffer = ComposeLineBuffer(startLine,i); + DrawAddress(g, startLine, i); + DrawHex(g,lineBuffer,i); + DrawDump(g,lineBuffer,i); + } + } + + /// + /// + /// Establishes the display mode for the control based on the + /// contents of the buffer. + /// This is based on the following algorithm: + /// Count number of zeros, prinables and other characters in the half of the dataBuffer + /// Base on the following table establish the mode: + /// 80% Characters or digits -> ANSI + /// 80% Valid Unicode chars -> Unicode + /// All other cases -> HEXDUMP + /// Also for the buffer of size [0..5] it returns the HEXDUMP mode + /// + /// + private DisplayMode GetAutoDisplayMode() { + int printablesCount = 0; + int unicodeCount = 0; + int size; + + if ((dataBuf == null) || (dataBuf.Length >= 0 && (dataBuf.Length < 8))) + return DisplayMode.Hexdump; + + switch(AnalizeByteOrderMark(dataBuf, 0)) { + case 2: + //_Encoding = Encoding.BigEndianUnicode; + return DisplayMode.Hexdump; + case 3: + //_Encoding = Encoding.Unicode; + return DisplayMode.Unicode; + case 4: + case 5: + //_Encoding = Ucs4Encoding.UCS4_Bigendian; + return DisplayMode.Hexdump; + case 6: + case 7: + //_Encoding = Ucs4Encoding.UCS4_Littleendian; + return DisplayMode.Hexdump; + case 8: + case 9: + //_Encoding = Ucs4Encoding.UCS4_3412; + return DisplayMode.Hexdump; + case 10: + case 11: + //_Encoding = Ucs4Encoding.UCS4_2143; + return DisplayMode.Hexdump; + case 12: + //8 ebcdic + return DisplayMode.Hexdump; + case 13: //9 + //_Encoding = new UTF8Encoding(false); + return DisplayMode.Ansi; + case 14: + return DisplayMode.Ansi; + default: + //If ByteOrderMark not detected try + if (dataBuf.Length > 1024) + size = 512; + else + size = dataBuf.Length / 2; + + for (int i = 0; i < size; i++) { + char c = (char) dataBuf[i]; //OK we do not care for Unicode now + if (Char.IsLetterOrDigit(c) || Char.IsWhiteSpace(c)) + printablesCount++; + } + for (int i = 0; i < size; i+=2) { + char[] unicodeChars = new char[1]; + Encoding.Unicode.GetChars(dataBuf, i, 2, unicodeChars, 0); + if (CharIsPrintable(unicodeChars[0])) + unicodeCount++; + } + + if (unicodeCount * 100 / (size/2) > 80) + return DisplayMode.Unicode; + + if (printablesCount * 100 / size > 80) + return DisplayMode.Ansi; + + return DisplayMode.Hexdump; + } + } + + /// + /// + /// Gets the bytes in the buffer. + /// + public virtual byte[] GetBytes() { + return dataBuf; + } + + /// + /// + /// + /// Gets the display mode for the control. + /// + /// + public virtual DisplayMode GetDisplayMode() { + return displayMode; + } + + //Stole this code from XmlSanner + private static int GetEncodingIndex(int c1) { + switch(c1) { + case 0x0000: return 1; + case 0xfeff: return 2; + case 0xfffe: return 3; + case 0xefbb: return 4; + case 0x3c00: return 5; + case 0x003c: return 6; + case 0x3f00: return 7; + case 0x003f: return 8; + case 0x3c3f: return 9; + case 0x786d: return 10; + case 0x4c6f: return 11; + case 0xa794: return 12; + default: return 0; //unknown + } + } + + /// + /// + /// Initializes the ansi string variable that will be assigned to the edit box. + /// + /// + private void InitAnsi() { + int size = dataBuf.Length; + char[] text = new char[size + 1]; + size = NativeMethods.MultiByteToWideChar(0, 0, dataBuf, size, text, size); + text[size] = (char)0; + + for (int i = 0; i < size; i++) + if (text[i] == '\0') text[i] = (char)0x0B; + + edit.Text = new string(text); + } + + /// + /// + /// Initializes the Unicode string varible that will be assigned to the edit box + /// + /// + private void InitUnicode() { + char[] text = new char[dataBuf.Length/2+1]; + Encoding.Unicode.GetChars(dataBuf, 0, dataBuf.Length, text, 0); + for (int i = 0; i < text.Length; i++) + if (text[i] == '\0') text[i] = (char)0x0B; + + text[text.Length - 1] = '\0'; + edit.Text = new string(text); + } + + /// + /// + /// Initializes the UI components of a control + /// + /// + private void InitUI() { + + SCROLLBAR_HEIGHT = SystemInformation.HorizontalScrollBarHeight; + SCROLLBAR_WIDTH = SystemInformation.VerticalScrollBarWidth; + // For backwards compat + this.Size = new Size(SCROLLBAR_START_X + SCROLLBAR_WIDTH + BORDER_GAP + INSET_GAP, + 2 * (BORDER_GAP + INSET_GAP) + rowCount * (CELL_HEIGHT)); + + + scrollBar = new VScrollBar(); + scrollBar.ValueChanged += new EventHandler(this.ScrollChanged); + scrollBar.TabStop = true; + scrollBar.TabIndex = 0; + scrollBar.Dock = DockStyle.Right; + scrollBar.Visible = false; + + edit = new TextBox(); + edit.AutoSize = false; + edit.BorderStyle = BorderStyle.None; + edit.Multiline = true; + edit.ReadOnly = true; + edit.ScrollBars = ScrollBars.Both; + edit.AcceptsTab = true; + edit.AcceptsReturn = true; + edit.Dock = DockStyle.Fill; + edit.Margin = Padding.Empty; + edit.WordWrap = false; + edit.Visible = false; + + Controls.Add(scrollBar, 0, 0); + Controls.Add(edit, 0 ,0); + } + + /// + /// + /// Initializes some important variables + /// + /// + private void InitState() { + // calculate number of lines required (being careful to count 1 for the last partial line) + linesCount = (dataBuf.Length + columnCount - 1) / columnCount; + + startLine = 0; + if (linesCount > rowCount) { + displayLinesCount = rowCount; + scrollBar.Hide(); + scrollBar.Maximum = linesCount - 1; + scrollBar.LargeChange = rowCount; + scrollBar.Show(); + scrollBar.Enabled = true; + } + else { + displayLinesCount = linesCount; + scrollBar.Hide(); + scrollBar.Maximum = rowCount; + scrollBar.LargeChange = rowCount; + scrollBar.Show(); + scrollBar.Enabled = false; + } + scrollBar.Select(); + Invalidate(); + } + + /// + /// + /// KeyDown handler. + /// + /// + protected override void OnKeyDown(KeyEventArgs e) { + scrollBar.Select(); + } + + /// + /// + /// + /// + /// Paint handler + /// for the control. + /// + /// + protected override void OnPaint(PaintEventArgs e) { + base.OnPaint(e); + Graphics g = e.Graphics; + + switch (realDisplayMode) { + case DisplayMode.Hexdump: + this.SuspendLayout(); + edit.Hide(); + scrollBar.Show(); + this.ResumeLayout(); + DrawClient(g); + DrawLines(g, startLine, displayLinesCount); + break; + case DisplayMode.Ansi: + edit.Invalidate(); + break; + case DisplayMode.Unicode: + edit.Invalidate(); + break; + } + } + + /// + /// + /// + /// + /// Resize handler for the control. + /// + /// + + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1725:ParameterNamesShouldMatchBaseDeclaration", MessageId="0#")] + protected override void OnLayout(LayoutEventArgs e) { + + // Must call this first since we might return + base.OnLayout(e); + + int rows = (ClientSize.Height - 2 * (BORDER_GAP + INSET_GAP)) / CELL_HEIGHT; + if (rows >= 0 && rows != rowCount) + rowCount = rows; + else + return; + + if (Dock == DockStyle.None) { + // For backwards compatibility + Size = new Size(SCROLLBAR_START_X + SCROLLBAR_WIDTH + BORDER_GAP + INSET_GAP, 2 * (BORDER_GAP + INSET_GAP) + rowCount * (CELL_HEIGHT)); + } + + if (scrollBar != null) { + if (linesCount > rowCount) { + scrollBar.Hide(); + scrollBar.Maximum = linesCount - 1; + scrollBar.LargeChange = rowCount; + scrollBar.Show(); + scrollBar.Enabled = true; + scrollBar.Select(); + } + else + scrollBar.Enabled = false; + } + displayLinesCount = (startLine + rowCount < linesCount) ? rowCount : linesCount - startLine; + + } + + /// + /// + /// + /// Writes the raw data from the data buffer to a file. + /// + /// + public virtual void SaveToFile(string path) { + if (dataBuf != null) { + FileStream currentFile = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); + try { + currentFile.Write(dataBuf,0,dataBuf.Length); + currentFile.Close(); + } + catch { + currentFile.Close(); + throw; + } + } + } + + /// + /// + /// + /// + /// Scroll event handler. + /// + /// + protected virtual void ScrollChanged(object source, EventArgs e) { + startLine = scrollBar.Value; + + Invalidate(); + } + + + /// + /// + /// + /// Sets the byte array to be displayed in the viewer. + /// + /// + public virtual void SetBytes(byte[] bytes) { + if (bytes == null) + throw new ArgumentNullException("bytes"); + + if (dataBuf != null) + dataBuf = null; + dataBuf = bytes; + InitState(); + SetDisplayMode(displayMode); + } + + /// + /// + /// + /// Sets the current display mode. + /// + /// + public virtual void SetDisplayMode(DisplayMode mode) { + if (!ClientUtils.IsEnumValid(mode,(int)mode, (int)DisplayMode.Hexdump, (int)DisplayMode.Auto)) + throw new InvalidEnumArgumentException("mode", (int)mode, typeof(DisplayMode)); + + displayMode = mode; + + realDisplayMode = (mode == DisplayMode.Auto) ? GetAutoDisplayMode() : mode; + + + switch (realDisplayMode) { + case DisplayMode.Ansi: + InitAnsi(); + this.SuspendLayout(); + edit.Show(); + scrollBar.Hide(); + this.ResumeLayout(); + Invalidate(); + break; + case DisplayMode.Unicode: + InitUnicode(); + this.SuspendLayout(); + edit.Show(); + scrollBar.Hide(); + this.ResumeLayout(); + Invalidate(); + break; + case DisplayMode.Hexdump: + this.SuspendLayout(); + edit.Hide(); + if (linesCount > rowCount) { + if (!scrollBar.Visible) { + scrollBar.Show(); + this.ResumeLayout(); + scrollBar.Invalidate(); + scrollBar.Select(); + } + else { + this.ResumeLayout(); + } + } + else { + this.ResumeLayout(); + } + break; + } + + } + + /// + /// + /// + /// Sets the file to be displayed in the viewer. + /// + /// + public virtual void SetFile(string path) { + FileStream currentFile = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None); + try { + int length = (int)currentFile.Length; + byte[] buf = new byte[length+1]; + currentFile.Read(buf,0,length); + SetBytes(buf); + currentFile.Close(); + } + catch { + currentFile.Close(); + throw; + } + } + + /// + /// + /// + /// Sets the current line for the HEXDUMP view. + /// + /// + public virtual void SetStartLine(int line) { + if (line < 0 || line >= linesCount || line > dataBuf.Length / columnCount) + startLine = 0; + else + startLine = line; + } + } +} diff --git a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DisplayMode.cs b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DisplayMode.cs new file mode 100644 index 00000000000..e9500a080cd --- /dev/null +++ b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DisplayMode.cs @@ -0,0 +1,53 @@ +//------------------------------------------------------------------------------ +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// +//------------------------------------------------------------------------------ + +namespace System.ComponentModel.Design { + + using System.Diagnostics; + + using System; + using System.ComponentModel; + using Microsoft.Win32; + + /// + /// + /// Specifies identifiers to indicate the display modes used + /// by . + /// + public enum DisplayMode { + + /// + /// + /// + /// Indicates using + /// Hexadecimal format. + /// + /// + Hexdump = 1, + /// + /// + /// + /// Indicates using ANSI format. + /// + /// + Ansi = 2, + /// + /// + /// + /// Indicates using Unicode format. + /// + /// + Unicode = 3, + /// + /// + /// + /// Indicates using automatic format selection. + /// + /// + Auto = 4, + + } +} From 0c8c86f541c774a2755dc842872165a328f29011 Mon Sep 17 00:00:00 2001 From: Igor Velikorossov Date: Fri, 18 Oct 2019 16:30:21 +1100 Subject: [PATCH 2/2] chore: Code cleanup --- .../Design/xlf/BinaryEditor.cs.xlf | 47 + .../Design/xlf/BinaryEditor.de.xlf | 47 + .../Design/xlf/BinaryEditor.es.xlf | 47 + .../Design/xlf/BinaryEditor.fr.xlf | 47 + .../Design/xlf/BinaryEditor.it.xlf | 47 + .../Design/xlf/BinaryEditor.ja.xlf | 47 + .../Design/xlf/BinaryEditor.ko.xlf | 47 + .../Design/xlf/BinaryEditor.pl.xlf | 47 + .../Design/xlf/BinaryEditor.pt-BR.xlf | 47 + .../Design/xlf/BinaryEditor.ru.xlf | 47 + .../Design/xlf/BinaryEditor.tr.xlf | 47 + .../Design/xlf/BinaryEditor.zh-Hans.xlf | 47 + .../Design/xlf/BinaryEditor.zh-Hant.xlf | 47 + .../src/Resources/xlf/SR.cs.xlf | 25 + .../src/Resources/xlf/SR.de.xlf | 25 + .../src/Resources/xlf/SR.es.xlf | 25 + .../src/Resources/xlf/SR.fr.xlf | 25 + .../src/Resources/xlf/SR.it.xlf | 25 + .../src/Resources/xlf/SR.ja.xlf | 25 + .../src/Resources/xlf/SR.ko.xlf | 25 + .../src/Resources/xlf/SR.pl.xlf | 25 + .../src/Resources/xlf/SR.pt-BR.xlf | 25 + .../src/Resources/xlf/SR.ru.xlf | 25 + .../src/Resources/xlf/SR.tr.xlf | 25 + .../src/Resources/xlf/SR.zh-Hans.xlf | 25 + .../src/Resources/xlf/SR.zh-Hant.xlf | 25 + .../src/System.Windows.Forms.Design.csproj | 2 + .../ComponentModel/Design/BinaryEditor.cs | 737 ++++++------ .../ComponentModel/Design/ByteViewer.cs | 1022 +++++++++-------- .../ComponentModel/Design/DisplayMode.cs | 72 +- 30 files changed, 1861 insertions(+), 908 deletions(-) create mode 100644 src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.cs.xlf create mode 100644 src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.de.xlf create mode 100644 src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.es.xlf create mode 100644 src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.fr.xlf create mode 100644 src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.it.xlf create mode 100644 src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.ja.xlf create mode 100644 src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.ko.xlf create mode 100644 src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.pl.xlf create mode 100644 src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.pt-BR.xlf create mode 100644 src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.ru.xlf create mode 100644 src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.tr.xlf create mode 100644 src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.zh-Hans.xlf create mode 100644 src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.zh-Hant.xlf diff --git a/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.cs.xlf b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.cs.xlf new file mode 100644 index 00000000000..2050162a26f --- /dev/null +++ b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.cs.xlf @@ -0,0 +1,47 @@ + + + + + + Byte Array Property Editor + Byte Array Property Editor + + + + OK + OK + + + + &Save... + &Save... + + + + Display Mode + Display Mode + + + + A&NSI + A&NSI + + + + &Auto + &Auto + + + + &Hexadecimal + &Hexadecimal + + + + &Unicode + &Unicode + + + + + \ No newline at end of file diff --git a/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.de.xlf b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.de.xlf new file mode 100644 index 00000000000..e93c7231415 --- /dev/null +++ b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.de.xlf @@ -0,0 +1,47 @@ + + + + + + Byte Array Property Editor + Byte Array Property Editor + + + + OK + OK + + + + &Save... + &Save... + + + + Display Mode + Display Mode + + + + A&NSI + A&NSI + + + + &Auto + &Auto + + + + &Hexadecimal + &Hexadecimal + + + + &Unicode + &Unicode + + + + + \ No newline at end of file diff --git a/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.es.xlf b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.es.xlf new file mode 100644 index 00000000000..ea2f5050488 --- /dev/null +++ b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.es.xlf @@ -0,0 +1,47 @@ + + + + + + Byte Array Property Editor + Byte Array Property Editor + + + + OK + OK + + + + &Save... + &Save... + + + + Display Mode + Display Mode + + + + A&NSI + A&NSI + + + + &Auto + &Auto + + + + &Hexadecimal + &Hexadecimal + + + + &Unicode + &Unicode + + + + + \ No newline at end of file diff --git a/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.fr.xlf b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.fr.xlf new file mode 100644 index 00000000000..bfdbbd7e74f --- /dev/null +++ b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.fr.xlf @@ -0,0 +1,47 @@ + + + + + + Byte Array Property Editor + Byte Array Property Editor + + + + OK + OK + + + + &Save... + &Save... + + + + Display Mode + Display Mode + + + + A&NSI + A&NSI + + + + &Auto + &Auto + + + + &Hexadecimal + &Hexadecimal + + + + &Unicode + &Unicode + + + + + \ No newline at end of file diff --git a/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.it.xlf b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.it.xlf new file mode 100644 index 00000000000..f19c035af3c --- /dev/null +++ b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.it.xlf @@ -0,0 +1,47 @@ + + + + + + Byte Array Property Editor + Byte Array Property Editor + + + + OK + OK + + + + &Save... + &Save... + + + + Display Mode + Display Mode + + + + A&NSI + A&NSI + + + + &Auto + &Auto + + + + &Hexadecimal + &Hexadecimal + + + + &Unicode + &Unicode + + + + + \ No newline at end of file diff --git a/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.ja.xlf b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.ja.xlf new file mode 100644 index 00000000000..8c1d9e1a145 --- /dev/null +++ b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.ja.xlf @@ -0,0 +1,47 @@ + + + + + + Byte Array Property Editor + Byte Array Property Editor + + + + OK + OK + + + + &Save... + &Save... + + + + Display Mode + Display Mode + + + + A&NSI + A&NSI + + + + &Auto + &Auto + + + + &Hexadecimal + &Hexadecimal + + + + &Unicode + &Unicode + + + + + \ No newline at end of file diff --git a/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.ko.xlf b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.ko.xlf new file mode 100644 index 00000000000..395e5ab6a12 --- /dev/null +++ b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.ko.xlf @@ -0,0 +1,47 @@ + + + + + + Byte Array Property Editor + Byte Array Property Editor + + + + OK + OK + + + + &Save... + &Save... + + + + Display Mode + Display Mode + + + + A&NSI + A&NSI + + + + &Auto + &Auto + + + + &Hexadecimal + &Hexadecimal + + + + &Unicode + &Unicode + + + + + \ No newline at end of file diff --git a/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.pl.xlf b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.pl.xlf new file mode 100644 index 00000000000..ed96ab7ffc5 --- /dev/null +++ b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.pl.xlf @@ -0,0 +1,47 @@ + + + + + + Byte Array Property Editor + Byte Array Property Editor + + + + OK + OK + + + + &Save... + &Save... + + + + Display Mode + Display Mode + + + + A&NSI + A&NSI + + + + &Auto + &Auto + + + + &Hexadecimal + &Hexadecimal + + + + &Unicode + &Unicode + + + + + \ No newline at end of file diff --git a/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.pt-BR.xlf b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.pt-BR.xlf new file mode 100644 index 00000000000..efce5f05dec --- /dev/null +++ b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.pt-BR.xlf @@ -0,0 +1,47 @@ + + + + + + Byte Array Property Editor + Byte Array Property Editor + + + + OK + OK + + + + &Save... + &Save... + + + + Display Mode + Display Mode + + + + A&NSI + A&NSI + + + + &Auto + &Auto + + + + &Hexadecimal + &Hexadecimal + + + + &Unicode + &Unicode + + + + + \ No newline at end of file diff --git a/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.ru.xlf b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.ru.xlf new file mode 100644 index 00000000000..6d2ee06df78 --- /dev/null +++ b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.ru.xlf @@ -0,0 +1,47 @@ + + + + + + Byte Array Property Editor + Byte Array Property Editor + + + + OK + OK + + + + &Save... + &Save... + + + + Display Mode + Display Mode + + + + A&NSI + A&NSI + + + + &Auto + &Auto + + + + &Hexadecimal + &Hexadecimal + + + + &Unicode + &Unicode + + + + + \ No newline at end of file diff --git a/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.tr.xlf b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.tr.xlf new file mode 100644 index 00000000000..caa7030fb6c --- /dev/null +++ b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.tr.xlf @@ -0,0 +1,47 @@ + + + + + + Byte Array Property Editor + Byte Array Property Editor + + + + OK + OK + + + + &Save... + &Save... + + + + Display Mode + Display Mode + + + + A&NSI + A&NSI + + + + &Auto + &Auto + + + + &Hexadecimal + &Hexadecimal + + + + &Unicode + &Unicode + + + + + \ No newline at end of file diff --git a/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.zh-Hans.xlf b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.zh-Hans.xlf new file mode 100644 index 00000000000..4f8e3fb8a11 --- /dev/null +++ b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.zh-Hans.xlf @@ -0,0 +1,47 @@ + + + + + + Byte Array Property Editor + Byte Array Property Editor + + + + OK + OK + + + + &Save... + &Save... + + + + Display Mode + Display Mode + + + + A&NSI + A&NSI + + + + &Auto + &Auto + + + + &Hexadecimal + &Hexadecimal + + + + &Unicode + &Unicode + + + + + \ No newline at end of file diff --git a/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.zh-Hant.xlf b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.zh-Hant.xlf new file mode 100644 index 00000000000..68854707dc0 --- /dev/null +++ b/src/System.Windows.Forms.Design/src/Resources/System/ComponentModel/Design/xlf/BinaryEditor.zh-Hant.xlf @@ -0,0 +1,47 @@ + + + + + + Byte Array Property Editor + Byte Array Property Editor + + + + OK + OK + + + + &Save... + &Save... + + + + Display Mode + Display Mode + + + + A&NSI + A&NSI + + + + &Auto + &Auto + + + + &Hexadecimal + &Hexadecimal + + + + &Unicode + &Unicode + + + + + \ No newline at end of file diff --git a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.cs.xlf b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.cs.xlf index 905626c3538..8da3c304732 100644 --- a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.cs.xlf +++ b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.cs.xlf @@ -32,6 +32,31 @@ Změna velikosti - {0} ovládacích prvků + + All Files + All Files + + + + Error writing to file: {0} + Error writing to file: {0} + + + + ByteArray + ByteArray + + + + Save File As + Save File As + + + + Byte Array Property Editor + Byte Array Property Editor + + The serialization store is closed. New objects cannot be added to a closed store. Úložiště serializace je zavřeno. Do zavřeného úložiště nelze přidávat nové objekty. diff --git a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.de.xlf b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.de.xlf index 66b6f0a960d..2416d455fce 100644 --- a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.de.xlf +++ b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.de.xlf @@ -32,6 +32,31 @@ Größe von {0}-Steuerelementen ändern + + All Files + All Files + + + + Error writing to file: {0} + Error writing to file: {0} + + + + ByteArray + ByteArray + + + + Save File As + Save File As + + + + Byte Array Property Editor + Byte Array Property Editor + + The serialization store is closed. New objects cannot be added to a closed store. Der Serialisierungsspeicher ist geschlossen. Neue Objekte können nicht zu einem geschlossenen Speicher hinzugefügt werden. diff --git a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.es.xlf b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.es.xlf index 2e082e3aa2b..15f1284670c 100644 --- a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.es.xlf +++ b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.es.xlf @@ -32,6 +32,31 @@ Cambiar tamaño de {0} controles + + All Files + All Files + + + + Error writing to file: {0} + Error writing to file: {0} + + + + ByteArray + ByteArray + + + + Save File As + Save File As + + + + Byte Array Property Editor + Byte Array Property Editor + + The serialization store is closed. New objects cannot be added to a closed store. El almacén de serialización está cerrado. No se pueden agregar nuevos objetos a un almacén cerrado. diff --git a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.fr.xlf b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.fr.xlf index 8f654146236..c05bd3f45cb 100644 --- a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.fr.xlf +++ b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.fr.xlf @@ -32,6 +32,31 @@ Redimensionner les contrôles {0} + + All Files + All Files + + + + Error writing to file: {0} + Error writing to file: {0} + + + + ByteArray + ByteArray + + + + Save File As + Save File As + + + + Byte Array Property Editor + Byte Array Property Editor + + The serialization store is closed. New objects cannot be added to a closed store. Le magasin de sérialisation est fermé. Impossible d'ajouter de nouveaux objets à un magasin fermé. diff --git a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.it.xlf b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.it.xlf index 1230b0f888b..1957559e7db 100644 --- a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.it.xlf +++ b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.it.xlf @@ -32,6 +32,31 @@ Ridimensiona {0} controlli + + All Files + All Files + + + + Error writing to file: {0} + Error writing to file: {0} + + + + ByteArray + ByteArray + + + + Save File As + Save File As + + + + Byte Array Property Editor + Byte Array Property Editor + + The serialization store is closed. New objects cannot be added to a closed store. L'archivio di serializzazione è chiuso. Non è possibile aggiungere nuovi oggetti a un archivio chiuso. diff --git a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.ja.xlf b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.ja.xlf index e5bf346044d..d3edea42914 100644 --- a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.ja.xlf +++ b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.ja.xlf @@ -32,6 +32,31 @@ {0} コントロールのサイズの変更 + + All Files + All Files + + + + Error writing to file: {0} + Error writing to file: {0} + + + + ByteArray + ByteArray + + + + Save File As + Save File As + + + + Byte Array Property Editor + Byte Array Property Editor + + The serialization store is closed. New objects cannot be added to a closed store. シリアル化ストアは閉じられています。 閉じられたストアに新しいオブジェクトを追加することはできません。 diff --git a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.ko.xlf b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.ko.xlf index e6d9d0fcecd..ab5114348c2 100644 --- a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.ko.xlf +++ b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.ko.xlf @@ -32,6 +32,31 @@ {0}개 컨트롤 크기 조정 + + All Files + All Files + + + + Error writing to file: {0} + Error writing to file: {0} + + + + ByteArray + ByteArray + + + + Save File As + Save File As + + + + Byte Array Property Editor + Byte Array Property Editor + + The serialization store is closed. New objects cannot be added to a closed store. Serialization 저장소가 닫혀 있습니다. 닫힌 저장소에는 새 개체를 추가할 수 없습니다. diff --git a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.pl.xlf b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.pl.xlf index 12b8d5404e5..2a616876053 100644 --- a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.pl.xlf +++ b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.pl.xlf @@ -32,6 +32,31 @@ Zmień rozmiar formantów {0} + + All Files + All Files + + + + Error writing to file: {0} + Error writing to file: {0} + + + + ByteArray + ByteArray + + + + Save File As + Save File As + + + + Byte Array Property Editor + Byte Array Property Editor + + The serialization store is closed. New objects cannot be added to a closed store. Magazyn serializacji jest zamknięty. Do zamkniętego magazynu nie można dodawać nowych obiektów. diff --git a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.pt-BR.xlf b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.pt-BR.xlf index a3960ce4e93..215bdd54917 100644 --- a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.pt-BR.xlf +++ b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.pt-BR.xlf @@ -32,6 +32,31 @@ Redimensionar {0} Controles + + All Files + All Files + + + + Error writing to file: {0} + Error writing to file: {0} + + + + ByteArray + ByteArray + + + + Save File As + Save File As + + + + Byte Array Property Editor + Byte Array Property Editor + + The serialization store is closed. New objects cannot be added to a closed store. O repositório de serialização está fechado. Não é possível adicionar novos objetos a um repositório fechado. diff --git a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.ru.xlf b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.ru.xlf index 19c80274b5d..2e2badfb4cf 100644 --- a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.ru.xlf +++ b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.ru.xlf @@ -32,6 +32,31 @@ Изменить размер {0} элементов управления + + All Files + All Files + + + + Error writing to file: {0} + Error writing to file: {0} + + + + ByteArray + ByteArray + + + + Save File As + Save File As + + + + Byte Array Property Editor + Byte Array Property Editor + + The serialization store is closed. New objects cannot be added to a closed store. Хранилище сериализованных объектов закрыто. Добавление новых объектов в закрытое хранилище невозможно. diff --git a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.tr.xlf b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.tr.xlf index d562b7bd4ad..125ec5cd2b5 100644 --- a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.tr.xlf +++ b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.tr.xlf @@ -32,6 +32,31 @@ {0} Denetimi yeniden boyutlandır + + All Files + All Files + + + + Error writing to file: {0} + Error writing to file: {0} + + + + ByteArray + ByteArray + + + + Save File As + Save File As + + + + Byte Array Property Editor + Byte Array Property Editor + + The serialization store is closed. New objects cannot be added to a closed store. Serileştirme deposu kapalı. Kapalı depoya yeni nesneler eklenemez. diff --git a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.zh-Hans.xlf b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.zh-Hans.xlf index a69d9eecce0..43556a2d9e9 100644 --- a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.zh-Hans.xlf +++ b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.zh-Hans.xlf @@ -32,6 +32,31 @@ 调整 {0} 控件的大小 + + All Files + All Files + + + + Error writing to file: {0} + Error writing to file: {0} + + + + ByteArray + ByteArray + + + + Save File As + Save File As + + + + Byte Array Property Editor + Byte Array Property Editor + + The serialization store is closed. New objects cannot be added to a closed store. 序列化存储区已关闭。新对象无法添加到已关闭的存储区中。 diff --git a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.zh-Hant.xlf b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.zh-Hant.xlf index 562cf2a4ded..159261f2c7f 100644 --- a/src/System.Windows.Forms.Design/src/Resources/xlf/SR.zh-Hant.xlf +++ b/src/System.Windows.Forms.Design/src/Resources/xlf/SR.zh-Hant.xlf @@ -32,6 +32,31 @@ 重新調整 {0} 控制項大小 + + All Files + All Files + + + + Error writing to file: {0} + Error writing to file: {0} + + + + ByteArray + ByteArray + + + + Save File As + Save File As + + + + Byte Array Property Editor + Byte Array Property Editor + + The serialization store is closed. New objects cannot be added to a closed store. 序列化存放區已關閉。新物件無法加入已關閉的存放區。 diff --git a/src/System.Windows.Forms.Design/src/System.Windows.Forms.Design.csproj b/src/System.Windows.Forms.Design/src/System.Windows.Forms.Design.csproj index d935ace9788..3d531ab25d5 100644 --- a/src/System.Windows.Forms.Design/src/System.Windows.Forms.Design.csproj +++ b/src/System.Windows.Forms.Design/src/System.Windows.Forms.Design.csproj @@ -33,6 +33,7 @@ + @@ -56,6 +57,7 @@ + diff --git a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/BinaryEditor.cs b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/BinaryEditor.cs index fc5ac478271..9b7997ffc76 100644 --- a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/BinaryEditor.cs +++ b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/BinaryEditor.cs @@ -1,313 +1,51 @@ -//------------------------------------------------------------------------------ -// -// Copyright (c) Microsoft Corporation. All rights reserved. -// -//------------------------------------------------------------------------------ - -namespace System.ComponentModel.Design { - - using System.Design; - using System; - using System.Text; - using System.IO; - using System.ComponentModel; - using System.Diagnostics; - using System.Diagnostics.CodeAnalysis; - using System.Windows.Forms; - using System.Drawing; - using System.Drawing.Design; - using Microsoft.Win32; - using System.Windows.Forms.Design; - using System.Windows.Forms.ComponentModel; - - /// - internal class BinaryUI : System.Windows.Forms.Form { - private BinaryEditor editor; - object value; - - private RadioButton radioAuto; - private Button buttonSave; - private Button buttonOK; - private ByteViewer byteViewer; - private GroupBox groupBoxMode; - private RadioButton radioHex; - private RadioButton radioAnsi; - private TableLayoutPanel radioButtonsTableLayoutPanel; - private TableLayoutPanel okSaveTableLayoutPanel; - private TableLayoutPanel overarchingTableLayoutPanel; - private RadioButton radioUnicode; - - public BinaryUI(BinaryEditor editor) { - this.editor = editor; - InitializeComponent(); - } - - public object Value { - get { - return value; +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Diagnostics; +using System.Drawing; +using System.Drawing.Design; +using System.IO; +using System.Text; +using System.Windows.Forms; +using System.Windows.Forms.Design; + +namespace System.ComponentModel.Design +{ + /// + /// Generic editor for editing binary data. This presents a hex editing window to the user. + /// + public sealed class BinaryEditor : UITypeEditor + { + private const string HELP_KEYWORD = "System.ComponentModel.Design.BinaryEditor"; + private ITypeDescriptorContext _context; + private BinaryUI _binaryUI; + + internal object GetService(Type serviceType) + { + if (_context == null) + { + return null; } - set { - this.value = value; - byte[] bytes = null; - - if (value != null) { - bytes = editor.ConvertToBytes(value); - } - if (bytes != null) { - byteViewer.SetBytes(bytes); - byteViewer.Enabled = true; - } else { - byteViewer.SetBytes(new byte[0]); - byteViewer.Enabled = false; - } + IDesignerHost host = _context.GetService(typeof(IDesignerHost)) as IDesignerHost; + if (host == null) + { + return _context.GetService(serviceType); } - } - - private void RadioAuto_checkedChanged(object source, EventArgs e) { - if (radioAuto.Checked) - byteViewer.SetDisplayMode(DisplayMode.Auto); - } - - private void RadioHex_checkedChanged(object source, EventArgs e) { - if (radioHex.Checked) - byteViewer.SetDisplayMode(DisplayMode.Hexdump); - } - - private void RadioAnsi_checkedChanged(object source, EventArgs e) { - if (radioAnsi.Checked) - byteViewer.SetDisplayMode(DisplayMode.Ansi); - } - private void RadioUnicode_checkedChanged(object source, EventArgs e) { - if (radioUnicode.Checked) - byteViewer.SetDisplayMode(DisplayMode.Unicode); + return host.GetService(serviceType); } - private void ButtonOK_click(object source, EventArgs e) { - object localValue = value; - editor.ConvertToValue(byteViewer.GetBytes(), ref localValue); - value = localValue; - } - - private void ButtonSave_click(object source, EventArgs e) { - try { - SaveFileDialog sfd = new SaveFileDialog(); - sfd.FileName = SR.GetString(SR.BinaryEditorFileName); - sfd.Title = SR.GetString(SR.BinaryEditorSaveFile); - sfd.Filter = SR.GetString(SR.BinaryEditorAllFiles) + " (*.*)|*.*"; - - DialogResult result = sfd.ShowDialog(); - if (result == DialogResult.OK) { - byteViewer.SaveToFile(sfd.FileName); - } - } catch (IOException x) { - RTLAwareMessageBox.Show(null, SR.GetString(SR.BinaryEditorFileError, x.Message), - SR.GetString(SR.BinaryEditorTitle), MessageBoxButtons.OK, - MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0); - } - } - - private void Form_HelpRequested(object sender, HelpEventArgs e) { - editor.ShowHelp(); - } - - private void Form_HelpButtonClicked(object sender, CancelEventArgs e) + /// + /// Converts the given object to an array of bytes to be manipulated + /// by the editor. The default implementation of this supports + /// byte[] and stream objects. + /// + internal byte[] ConvertToBytes(object value) { - e.Cancel = true; - editor.ShowHelp(); - } - - private void InitializeComponent() { - System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(BinaryEditor)); - this.byteViewer = new ByteViewer(); - this.buttonOK = new System.Windows.Forms.Button(); - this.buttonSave = new System.Windows.Forms.Button(); - this.groupBoxMode = new System.Windows.Forms.GroupBox(); - this.radioButtonsTableLayoutPanel = new System.Windows.Forms.TableLayoutPanel(); - this.radioUnicode = new System.Windows.Forms.RadioButton(); - this.radioAuto = new System.Windows.Forms.RadioButton(); - this.radioAnsi = new System.Windows.Forms.RadioButton(); - this.radioHex = new System.Windows.Forms.RadioButton(); - this.okSaveTableLayoutPanel = new System.Windows.Forms.TableLayoutPanel(); - this.overarchingTableLayoutPanel = new System.Windows.Forms.TableLayoutPanel(); - this.byteViewer.SuspendLayout(); - this.groupBoxMode.SuspendLayout(); - this.radioButtonsTableLayoutPanel.SuspendLayout(); - this.okSaveTableLayoutPanel.SuspendLayout(); - this.overarchingTableLayoutPanel.SuspendLayout(); - this.SuspendLayout(); - - // - // byteViewer - // - resources.ApplyResources(this.byteViewer, "byteViewer"); - this.byteViewer.SetDisplayMode(DisplayMode.Auto); - this.byteViewer.Name = "byteViewer"; - this.byteViewer.Margin = Padding.Empty; - this.byteViewer.Dock = DockStyle.Fill; - // - // buttonOK - // - resources.ApplyResources(this.buttonOK, "buttonOK"); - this.buttonOK.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; - this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.OK; - this.buttonOK.Margin = new System.Windows.Forms.Padding(0, 0, 3, 0); - this.buttonOK.MinimumSize = new System.Drawing.Size(75, 23); - this.buttonOK.Name = "buttonOK"; - this.buttonOK.Padding = new System.Windows.Forms.Padding(10, 0, 10, 0); - this.buttonOK.Click += new System.EventHandler(this.ButtonOK_click); - // - // buttonSave - // - resources.ApplyResources(this.buttonSave, "buttonSave"); - this.buttonSave.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; - this.buttonSave.Margin = new System.Windows.Forms.Padding(3, 0, 0, 0); - this.buttonSave.MinimumSize = new System.Drawing.Size(75, 23); - this.buttonSave.Name = "buttonSave"; - this.buttonSave.Padding = new System.Windows.Forms.Padding(10, 0, 10, 0); - this.buttonSave.Click += new System.EventHandler(this.ButtonSave_click); - // - // groupBoxMode - // - resources.ApplyResources(this.groupBoxMode, "groupBoxMode"); - this.groupBoxMode.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; - this.groupBoxMode.Controls.Add(this.radioButtonsTableLayoutPanel); - this.groupBoxMode.Margin = new System.Windows.Forms.Padding(0, 3, 0, 3); - this.groupBoxMode.Name = "groupBoxMode"; - this.groupBoxMode.Padding = new System.Windows.Forms.Padding(0); - this.groupBoxMode.TabStop = false; - // - // radioButtonsTableLayoutPanel - // - resources.ApplyResources(this.radioButtonsTableLayoutPanel, "radioButtonsTableLayoutPanel"); - this.radioButtonsTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F)); - this.radioButtonsTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F)); - this.radioButtonsTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F)); - this.radioButtonsTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F)); - this.radioButtonsTableLayoutPanel.Controls.Add(this.radioUnicode, 3, 0); - this.radioButtonsTableLayoutPanel.Controls.Add(this.radioAuto, 0, 0); - this.radioButtonsTableLayoutPanel.Controls.Add(this.radioAnsi, 2, 0); - this.radioButtonsTableLayoutPanel.Controls.Add(this.radioHex, 1, 0); - this.radioButtonsTableLayoutPanel.Margin = new System.Windows.Forms.Padding(9); - this.radioButtonsTableLayoutPanel.Name = "radioButtonsTableLayoutPanel"; - this.radioButtonsTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle()); - // - // radioUnicode - // - resources.ApplyResources(this.radioUnicode, "radioUnicode"); - this.radioUnicode.Margin = new System.Windows.Forms.Padding(3, 0, 0, 0); - this.radioUnicode.Name = "radioUnicode"; - this.radioUnicode.CheckedChanged += new System.EventHandler(this.RadioUnicode_checkedChanged); - // - // radioAuto - // - resources.ApplyResources(this.radioAuto, "radioAuto"); - this.radioAuto.Checked = true; - this.radioAuto.Margin = new System.Windows.Forms.Padding(0, 0, 3, 0); - this.radioAuto.Name = "radioAuto"; - this.radioAuto.CheckedChanged += new System.EventHandler(this.RadioAuto_checkedChanged); - // - // radioAnsi - // - resources.ApplyResources(this.radioAnsi, "radioAnsi"); - this.radioAnsi.Margin = new System.Windows.Forms.Padding(3, 0, 3, 0); - this.radioAnsi.Name = "radioAnsi"; - this.radioAnsi.CheckedChanged += new System.EventHandler(this.RadioAnsi_checkedChanged); - // - // radioHex - // - resources.ApplyResources(this.radioHex, "radioHex"); - this.radioHex.Margin = new System.Windows.Forms.Padding(3, 0, 3, 0); - this.radioHex.Name = "radioHex"; - this.radioHex.CheckedChanged += new System.EventHandler(this.RadioHex_checkedChanged); - // - // okSaveTableLayoutPanel - // - resources.ApplyResources(this.okSaveTableLayoutPanel, "okSaveTableLayoutPanel"); - this.okSaveTableLayoutPanel.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; - this.okSaveTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); - this.okSaveTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); - this.okSaveTableLayoutPanel.Controls.Add(this.buttonOK, 0, 0); - this.okSaveTableLayoutPanel.Controls.Add(this.buttonSave, 1, 0); - this.okSaveTableLayoutPanel.Margin = new System.Windows.Forms.Padding(0, 9, 0, 0); - this.okSaveTableLayoutPanel.Name = "okSaveTableLayoutPanel"; - this.okSaveTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); - // - // overarchingTableLayoutPanel - // - resources.ApplyResources(this.overarchingTableLayoutPanel, "overarchingTableLayoutPanel"); - this.overarchingTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); - this.overarchingTableLayoutPanel.Controls.Add(this.byteViewer, 0, 0); - this.overarchingTableLayoutPanel.Controls.Add(this.groupBoxMode, 0, 1); - this.overarchingTableLayoutPanel.Controls.Add(this.okSaveTableLayoutPanel, 0, 2); - this.overarchingTableLayoutPanel.Name = "overarchingTableLayoutPanel"; - this.overarchingTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); - this.overarchingTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle()); - this.overarchingTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle()); - - // - // BinaryUI - // - this.AcceptButton = this.buttonOK; - resources.ApplyResources(this, "$this"); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.CancelButton = this.buttonOK; - this.Controls.Add(this.overarchingTableLayoutPanel); - this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; - this.HelpButton = true; - this.MaximizeBox = false; - this.MinimizeBox = false; - this.Name = "BinaryUI"; - this.ShowIcon = false; - this.ShowInTaskbar = false; - this.HelpRequested += new System.Windows.Forms.HelpEventHandler(this.Form_HelpRequested); - this.HelpButtonClicked += new System.ComponentModel.CancelEventHandler(this.Form_HelpButtonClicked); - this.byteViewer.ResumeLayout(false); - this.byteViewer.PerformLayout(); - this.groupBoxMode.ResumeLayout(false); - this.groupBoxMode.PerformLayout(); - this.radioButtonsTableLayoutPanel.ResumeLayout(false); - this.radioButtonsTableLayoutPanel.PerformLayout(); - this.okSaveTableLayoutPanel.ResumeLayout(false); - this.okSaveTableLayoutPanel.PerformLayout(); - this.overarchingTableLayoutPanel.ResumeLayout(false); - this.overarchingTableLayoutPanel.PerformLayout(); - this.ResumeLayout(false); - - } - } - - /// - /// - /// Generic editor for editing binary data. This presents - /// a hex editing window to the user. - /// - public sealed class BinaryEditor : UITypeEditor { - private static readonly string HELP_KEYWORD = "System.ComponentModel.Design.BinaryEditor"; - private ITypeDescriptorContext context; - private BinaryUI binaryUI; - - internal object GetService(Type serviceType) { - if (this.context != null) { - IDesignerHost host = this.context.GetService(typeof(IDesignerHost)) as IDesignerHost; - if (host == null) - return this.context.GetService(serviceType); - else - return host.GetService(serviceType); - } - return null; - } - - /// - /// - /// Converts the given object to an array of bytes to be manipulated - /// by the editor. The default implementation of this supports - /// byte[] and stream objects. - /// - internal byte[] ConvertToBytes(object value) { - if (value is Stream) { - Stream s = (Stream)value; + if (value is Stream s) + { s.Position = 0; int byteCount = (int)(s.Length - s.Position); byte[] bytes = new byte[byteCount]; @@ -315,14 +53,16 @@ internal byte[] ConvertToBytes(object value) { return bytes; } - if (value is byte[]) { - return (byte[])value; + if (value is byte[] b) + { + return b; } - if (value is string) { - int size = ((string)value).Length * 2; + if (value is string str) + { + int size = str.Length * 2; byte[] buffer = new byte[size]; - Encoding.Unicode.GetBytes(((string)value).ToCharArray(), 0, size / 2, buffer, 0); + Encoding.Unicode.GetBytes(str.ToCharArray(), 0, size / 2, buffer, 0); return buffer; } @@ -330,78 +70,365 @@ internal byte[] ConvertToBytes(object value) { return null; } - /// - /// - /// Converts the given byte array back into a native object. If - /// the object itself needs to be replace (as is the case for arrays), - /// then a new object may be assigned out through the parameter. - /// - internal void ConvertToValue(byte[] bytes, ref object value) { - - if (value is Stream) { - Stream s = (Stream)value; + /// + /// Converts the given byte array back into a native object. If + /// the object itself needs to be replace (as is the case for arrays), + /// then a new object may be assigned out through the parameter. + /// + internal void ConvertToValue(byte[] bytes, ref object value) + { + if (value is Stream s) + { s.Position = 0; s.Write(bytes, 0, bytes.Length); - } else if (value is byte[]) { + } + else if (value is byte[]) + { value = bytes; - } else if (value is string) { + } + else if (value is string) + { value = BitConverter.ToString(bytes); - } else { + } + else + { Debug.Fail("No conversion from byte[] to " + value == null ? "null" : value.GetType().FullName); } } - /// - /// - /// Edits the given object value using the editor style provided by - /// GetEditorStyle. A service provider is provided so that any - /// required editing services can be obtained. - /// - [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")] // everything in this assembly is full trust. - public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { - if (provider != null) { - this.context = context; - - IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); - - if (edSvc != null) { - if (binaryUI == null) { + /// + /// Edits the given object value using the editor style provided by GetEditorStyle. + /// A service provider is provided so that any required editing services can be obtained. + /// + public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) + { + if (provider == null) + { + return value; + } - // child modal dialog -launching in System Aware mode - binaryUI = DpiHelper.CreateInstanceInSystemAwareContext(() => new BinaryUI(this)); - } + _context = context; - binaryUI.Value = value; + IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); + if (edSvc == null) + { + return value; + } - if (edSvc.ShowDialog(binaryUI) == DialogResult.OK) { - value = binaryUI.Value; - } + if (_binaryUI == null) + { + // child modal dialog -launching in System Aware mode + _binaryUI = DpiHelper.CreateInstanceInSystemAwareContext(() => new BinaryUI(this)); + } - binaryUI.Value = null; - } + _binaryUI.Value = value; + if (edSvc.ShowDialog(_binaryUI) == DialogResult.OK) + { + value = _binaryUI.Value; } + _binaryUI.Value = null; return value; } - /// - /// - /// Retrieves the editing style of the Edit method. If the method - /// is not supported, this will return None. - /// - [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")] // everything in this assembly is full trust. - public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { + /// + /// Retrieves the editing style of the Edit method. If the method is not supported, this will return None. + /// + public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) + { return UITypeEditorEditStyle.Modal; } - internal void ShowHelp() { + internal void ShowHelp() + { IHelpService helpService = GetService(typeof(IHelpService)) as IHelpService; - if (helpService != null) { + if (helpService != null) + { helpService.ShowHelpFromKeyword(HELP_KEYWORD); - } else { + } + else + { Debug.Fail("Unable to get IHelpService."); } } + + private class BinaryUI : Form + { + private readonly BinaryEditor _editor; + private object _value; + + private RadioButton _radioAuto; + private Button _buttonSave; + private Button _buttonOK; + private ByteViewer _byteViewer; + private GroupBox _groupBoxMode; + private RadioButton _radioHex; + private RadioButton _radioAnsi; + private TableLayoutPanel _radioButtonsTableLayoutPanel; + private TableLayoutPanel _okSaveTableLayoutPanel; + private TableLayoutPanel _overarchingTableLayoutPanel; + private RadioButton _radioUnicode; + + public BinaryUI(BinaryEditor editor) + { + _editor = editor; + InitializeComponent(); + } + + public object Value + { + get + { + return _value; + } + set + { + _value = value; + byte[] bytes = null; + + if (value != null) + { + bytes = _editor.ConvertToBytes(value); + } + + if (bytes != null) + { + _byteViewer.SetBytes(bytes); + _byteViewer.Enabled = true; + } + else + { + _byteViewer.SetBytes(new byte[0]); + _byteViewer.Enabled = false; + } + } + } + + private void RadioAuto_checkedChanged(object source, EventArgs e) + { + if (_radioAuto.Checked) + _byteViewer.SetDisplayMode(DisplayMode.Auto); + } + + private void RadioHex_checkedChanged(object source, EventArgs e) + { + if (_radioHex.Checked) + _byteViewer.SetDisplayMode(DisplayMode.Hexdump); + } + + private void RadioAnsi_checkedChanged(object source, EventArgs e) + { + if (_radioAnsi.Checked) + _byteViewer.SetDisplayMode(DisplayMode.Ansi); + } + + private void RadioUnicode_checkedChanged(object source, EventArgs e) + { + if (_radioUnicode.Checked) + _byteViewer.SetDisplayMode(DisplayMode.Unicode); + } + + private void ButtonOK_click(object source, EventArgs e) + { + object localValue = _value; + _editor.ConvertToValue(_byteViewer.GetBytes(), ref localValue); + _value = localValue; + } + + private void ButtonSave_click(object source, EventArgs e) + { + try + { + SaveFileDialog sfd = new SaveFileDialog(); + sfd.FileName = SR.BinaryEditorFileName; + sfd.Title = SR.BinaryEditorSaveFile; + sfd.Filter = SR.BinaryEditorAllFiles + " (*.*)|*.*"; + + DialogResult result = sfd.ShowDialog(); + if (result == DialogResult.OK) + { + _byteViewer.SaveToFile(sfd.FileName); + } + } + catch (IOException x) + { + RTLAwareMessageBox.Show(null, string.Format(SR.BinaryEditorFileError, x.Message), + SR.BinaryEditorTitle, MessageBoxButtons.OK, + MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0); + } + } + + private void Form_HelpRequested(object sender, HelpEventArgs e) + { + _editor.ShowHelp(); + } + + private void Form_HelpButtonClicked(object sender, CancelEventArgs e) + { + e.Cancel = true; + _editor.ShowHelp(); + } + + private void InitializeComponent() + { + ComponentResourceManager resources = new ComponentResourceManager(typeof(BinaryEditor)); + _byteViewer = new ByteViewer(); + _buttonOK = new Button(); + _buttonSave = new Button(); + _groupBoxMode = new GroupBox(); + _radioButtonsTableLayoutPanel = new TableLayoutPanel(); + _radioUnicode = new RadioButton(); + _radioAuto = new RadioButton(); + _radioAnsi = new RadioButton(); + _radioHex = new RadioButton(); + _okSaveTableLayoutPanel = new TableLayoutPanel(); + _overarchingTableLayoutPanel = new TableLayoutPanel(); + _byteViewer.SuspendLayout(); + _groupBoxMode.SuspendLayout(); + _radioButtonsTableLayoutPanel.SuspendLayout(); + _okSaveTableLayoutPanel.SuspendLayout(); + _overarchingTableLayoutPanel.SuspendLayout(); + SuspendLayout(); + + // + // byteViewer + // + resources.ApplyResources(_byteViewer, "byteViewer"); + _byteViewer.SetDisplayMode(DisplayMode.Auto); + _byteViewer.Name = "byteViewer"; + _byteViewer.Margin = Padding.Empty; + _byteViewer.Dock = DockStyle.Fill; + // + // buttonOK + // + resources.ApplyResources(_buttonOK, "buttonOK"); + _buttonOK.AutoSizeMode = AutoSizeMode.GrowAndShrink; + _buttonOK.DialogResult = DialogResult.OK; + _buttonOK.Margin = new Padding(0, 0, 3, 0); + _buttonOK.MinimumSize = new Size(75, 23); + _buttonOK.Name = "buttonOK"; + _buttonOK.Padding = new Padding(10, 0, 10, 0); + _buttonOK.Click += new EventHandler(ButtonOK_click); + // + // buttonSave + // + resources.ApplyResources(_buttonSave, "buttonSave"); + _buttonSave.AutoSizeMode = AutoSizeMode.GrowAndShrink; + _buttonSave.Margin = new Padding(3, 0, 0, 0); + _buttonSave.MinimumSize = new Size(75, 23); + _buttonSave.Name = "buttonSave"; + _buttonSave.Padding = new Padding(10, 0, 10, 0); + _buttonSave.Click += new EventHandler(ButtonSave_click); + // + // groupBoxMode + // + resources.ApplyResources(_groupBoxMode, "groupBoxMode"); + _groupBoxMode.AutoSizeMode = AutoSizeMode.GrowAndShrink; + _groupBoxMode.Controls.Add(_radioButtonsTableLayoutPanel); + _groupBoxMode.Margin = new Padding(0, 3, 0, 3); + _groupBoxMode.Name = "groupBoxMode"; + _groupBoxMode.Padding = new Padding(0); + _groupBoxMode.TabStop = false; + // + // radioButtonsTableLayoutPanel + // + resources.ApplyResources(_radioButtonsTableLayoutPanel, "radioButtonsTableLayoutPanel"); + _radioButtonsTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25F)); + _radioButtonsTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25F)); + _radioButtonsTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25F)); + _radioButtonsTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25F)); + _radioButtonsTableLayoutPanel.Controls.Add(_radioUnicode, 3, 0); + _radioButtonsTableLayoutPanel.Controls.Add(_radioAuto, 0, 0); + _radioButtonsTableLayoutPanel.Controls.Add(_radioAnsi, 2, 0); + _radioButtonsTableLayoutPanel.Controls.Add(_radioHex, 1, 0); + _radioButtonsTableLayoutPanel.Margin = new Padding(9); + _radioButtonsTableLayoutPanel.Name = "radioButtonsTableLayoutPanel"; + _radioButtonsTableLayoutPanel.RowStyles.Add(new RowStyle()); + // + // radioUnicode + // + resources.ApplyResources(_radioUnicode, "radioUnicode"); + _radioUnicode.Margin = new Padding(3, 0, 0, 0); + _radioUnicode.Name = "radioUnicode"; + _radioUnicode.CheckedChanged += new EventHandler(RadioUnicode_checkedChanged); + // + // radioAuto + // + resources.ApplyResources(_radioAuto, "radioAuto"); + _radioAuto.Checked = true; + _radioAuto.Margin = new Padding(0, 0, 3, 0); + _radioAuto.Name = "radioAuto"; + _radioAuto.CheckedChanged += new EventHandler(RadioAuto_checkedChanged); + // + // radioAnsi + // + resources.ApplyResources(_radioAnsi, "radioAnsi"); + _radioAnsi.Margin = new Padding(3, 0, 3, 0); + _radioAnsi.Name = "radioAnsi"; + _radioAnsi.CheckedChanged += new EventHandler(RadioAnsi_checkedChanged); + // + // radioHex + // + resources.ApplyResources(_radioHex, "radioHex"); + _radioHex.Margin = new Padding(3, 0, 3, 0); + _radioHex.Name = "radioHex"; + _radioHex.CheckedChanged += new EventHandler(RadioHex_checkedChanged); + // + // okSaveTableLayoutPanel + // + resources.ApplyResources(_okSaveTableLayoutPanel, "okSaveTableLayoutPanel"); + _okSaveTableLayoutPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink; + _okSaveTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F)); + _okSaveTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F)); + _okSaveTableLayoutPanel.Controls.Add(_buttonOK, 0, 0); + _okSaveTableLayoutPanel.Controls.Add(_buttonSave, 1, 0); + _okSaveTableLayoutPanel.Margin = new Padding(0, 9, 0, 0); + _okSaveTableLayoutPanel.Name = "okSaveTableLayoutPanel"; + _okSaveTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 50F)); + // + // overarchingTableLayoutPanel + // + resources.ApplyResources(_overarchingTableLayoutPanel, "overarchingTableLayoutPanel"); + _overarchingTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F)); + _overarchingTableLayoutPanel.Controls.Add(_byteViewer, 0, 0); + _overarchingTableLayoutPanel.Controls.Add(_groupBoxMode, 0, 1); + _overarchingTableLayoutPanel.Controls.Add(_okSaveTableLayoutPanel, 0, 2); + _overarchingTableLayoutPanel.Name = "overarchingTableLayoutPanel"; + _overarchingTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100F)); + _overarchingTableLayoutPanel.RowStyles.Add(new RowStyle()); + _overarchingTableLayoutPanel.RowStyles.Add(new RowStyle()); + + // + // BinaryUI + // + AcceptButton = _buttonOK; + resources.ApplyResources(this, "$this"); + AutoScaleMode = AutoScaleMode.Font; + CancelButton = _buttonOK; + Controls.Add(_overarchingTableLayoutPanel); + FormBorderStyle = FormBorderStyle.FixedDialog; + HelpButton = true; + MaximizeBox = false; + MinimizeBox = false; + Name = "BinaryUI"; + ShowIcon = false; + ShowInTaskbar = false; + HelpRequested += new HelpEventHandler(Form_HelpRequested); + HelpButtonClicked += new CancelEventHandler(Form_HelpButtonClicked); + _byteViewer.ResumeLayout(false); + _byteViewer.PerformLayout(); + _groupBoxMode.ResumeLayout(false); + _groupBoxMode.PerformLayout(); + _radioButtonsTableLayoutPanel.ResumeLayout(false); + _radioButtonsTableLayoutPanel.PerformLayout(); + _okSaveTableLayoutPanel.ResumeLayout(false); + _okSaveTableLayoutPanel.PerformLayout(); + _overarchingTableLayoutPanel.ResumeLayout(false); + _overarchingTableLayoutPanel.PerformLayout(); + ResumeLayout(false); + + } + } } } diff --git a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/ByteViewer.cs b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/ByteViewer.cs index 90912cde98a..911696f07a5 100644 --- a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/ByteViewer.cs +++ b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/ByteViewer.cs @@ -1,55 +1,34 @@ -//------------------------------------------------------------------------------ -// -// Copyright (c) Microsoft Corporation. All rights reserved. -// -//------------------------------------------------------------------------------ - -namespace System.ComponentModel.Design { - using System.Design; - using System.Text; - using System.Runtime.Serialization.Formatters; - using System.Runtime.InteropServices; - using System.Diagnostics; - using System; - using System.ComponentModel; - using System.Windows.Forms; - using System.IO; - using System.Globalization; - using System.Drawing; - using Microsoft.Win32; - - /// - /// - /// Displays byte arrays in - /// HEXDUMP, ANSI and Unicode formats. - /// - [ - ToolboxItem(false), - DesignTimeVisible(false) - ] - public class ByteViewer : TableLayoutPanel { +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Drawing; +using System.Globalization; +using System.IO; +using System.Text; +using System.Windows.Forms; + +namespace System.ComponentModel.Design +{ + /// + /// Displays byte arrays in HEXDUMP, ANSI and Unicode formats. + /// + [ToolboxItem(false)] + [DesignTimeVisible(false)] + public class ByteViewer : TableLayoutPanel + { private const int DEFAULT_COLUMN_COUNT = 16; private const int DEFAULT_ROW_COUNT = 25; - private const int COLUMN_COUNT = 16; - private const int BORDER_GAP = 2; private const int INSET_GAP = 3; - private const int CELL_HEIGHT = 21; private const int CELL_WIDTH = 25; private const int CHAR_WIDTH = 8; - private const int ADDRESS_WIDTH = 69; // this is ceiling(sizeof("DDDDDDDD").width) + 1 private const int HEX_WIDTH = CELL_WIDTH * COLUMN_COUNT; private const int DUMP_WIDTH = CHAR_WIDTH * COLUMN_COUNT; - - private int SCROLLBAR_HEIGHT; - private int SCROLLBAR_WIDTH; - - private const int HEX_DUMP_GAP = 5; - private const int ADDRESS_START_X = BORDER_GAP + INSET_GAP; private const int CLIENT_START_Y = BORDER_GAP + INSET_GAP; private const int LINE_START_Y = CLIENT_START_Y + CELL_HEIGHT / 8; @@ -60,55 +39,50 @@ public class ByteViewer : TableLayoutPanel { private static readonly Font ADDRESS_FONT = new Font("Microsoft Sans Serif", 8.0f); private static readonly Font HEXDUMP_FONT = new Font("Courier New", 8.0f); - private VScrollBar scrollBar; - private TextBox edit; - - private int columnCount = DEFAULT_COLUMN_COUNT; - private int rowCount = DEFAULT_ROW_COUNT; - - private byte[] dataBuf; - - private int startLine; - private int displayLinesCount; - private int linesCount; - - private DisplayMode displayMode; - private DisplayMode realDisplayMode; - - /// - /// - /// - /// Initializes a new instance of the class. - /// - /// + private int SCROLLBAR_HEIGHT; + private int SCROLLBAR_WIDTH; + private VScrollBar _scrollBar; + private TextBox _edit; + private readonly int _columnCount = DEFAULT_COLUMN_COUNT; + private int _rowCount = DEFAULT_ROW_COUNT; + private byte[] _dataBuf; + private int _startLine; + private int _displayLinesCount; + private int _linesCount; + private DisplayMode _displayMode; + private DisplayMode _realDisplayMode; + + /// + /// Initializes a new instance of the class. + /// public ByteViewer() - : base() { + : base() + { + SuspendLayout(); + CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset; - this.SuspendLayout(); - this.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset; + ColumnCount = 1; + ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F)); - this.ColumnCount = 1; - this.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F)); + RowCount = 1; + RowStyles.Add(new RowStyle(SizeType.Percent, 100F)); - this.RowCount = 1; - this.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); - InitUI(); - this.ResumeLayout(); - - displayMode = DisplayMode.Hexdump; - realDisplayMode = DisplayMode.Hexdump; + ResumeLayout(); + + _displayMode = DisplayMode.Hexdump; + _realDisplayMode = DisplayMode.Hexdump; DoubleBuffered = true; SetStyle(ControlStyles.ResizeRedraw, true); - } - //Stole this code from XmlSanner - private static int AnalizeByteOrderMark(byte[] buffer, int index) { + // Stole this code from XmlSanner + private static int AnalizeByteOrderMark(byte[] buffer, int index) + { int c1 = buffer[index + 0] << 8 | buffer[index + 1]; int c2 = buffer[index + 2] << 8 | buffer[index + 3]; - int c4,c5; + int c4, c5; //Assign an index (label) value for first two bytes c4 = GetEncodingIndex(c1); @@ -120,8 +94,8 @@ private static int AnalizeByteOrderMark(byte[] buffer, int index) { //values for this look up table //values on column are first two bytes and //values on rows are 3rd and 4th byte - - int[,] encodings = { + + int[,] encodings = { //Unknown 0000 feff fffe efbb 3c00 003c 3f00 003f 3c3f 786d 4c6f a794 /*Unknown*/ {1 ,5 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 }, /*0000*/ {1 ,1 ,1 ,11 ,1 ,10 ,4 ,1 ,1 ,1 ,1 ,1 ,1 }, @@ -138,147 +112,140 @@ private static int AnalizeByteOrderMark(byte[] buffer, int index) { /*a794*/ {1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 } }; - return encodings[c4,c5]; + return encodings[c4, c5]; } - - /// - /// - /// Calculates an index for a cell in the HEX grid - /// - /// - private int CellToIndex(int column, int row) { - return row * columnCount + column; + /// + /// Calculates an index for a cell in the HEX grid + /// + private int CellToIndex(int column, int row) + { + return row * _columnCount + column; } - /// - /// - /// Copies the line from main data buffer to a line buffer - /// - /// - private byte[] ComposeLineBuffer(int startLine, int line) { + /// + /// Copies the line from main data buffer to a line buffer + /// + private byte[] ComposeLineBuffer(int startLine, int line) + { byte[] lineBuffer; - int offset = startLine * columnCount; - if (offset + (line+1) * columnCount > dataBuf.Length) - lineBuffer = new byte[dataBuf.Length % columnCount]; + int offset = startLine * _columnCount; + if (offset + (line + 1) * _columnCount > _dataBuf.Length) + { + lineBuffer = new byte[_dataBuf.Length % _columnCount]; + } else - lineBuffer = new byte[columnCount]; + { + lineBuffer = new byte[_columnCount]; + } + for (int i = 0; i < lineBuffer.Length; i++) - lineBuffer[i] = dataBuf[offset + CellToIndex(i, line)]; + { + lineBuffer[i] = _dataBuf[offset + CellToIndex(i, line)]; + } + return lineBuffer; } - /// - /// - /// Draws an adress part in the HEXDUMP view - /// - /// - private void DrawAddress(System.Drawing.Graphics g, int startLine, int line) { + /// + /// Draws an adress part in the HEXDUMP view + /// + private void DrawAddress(Graphics g, int startLine, int line) + { Font font = ADDRESS_FONT; - string hexString = ((startLine + line) * columnCount).ToString("X8", CultureInfo.InvariantCulture); + string hexString = ((startLine + line) * _columnCount).ToString("X8", CultureInfo.InvariantCulture); - Brush foreground = new SolidBrush(ForeColor); - try { - g.DrawString(hexString, font, foreground, - ADDRESS_START_X,LINE_START_Y + line * CELL_HEIGHT); - } - - finally { - if (foreground != null) { - foreground.Dispose(); - } - } + using Brush foreground = new SolidBrush(ForeColor); + g.DrawString(hexString, font, foreground, ADDRESS_START_X, LINE_START_Y + line * CELL_HEIGHT); } - /// - /// + /// /// Draws the client background and frames - /// + /// /// - private void DrawClient(System.Drawing.Graphics g) { - - using (Brush brush = new SolidBrush(SystemColors.ControlLightLight)) { - g.FillRectangle(brush, new Rectangle(HEX_START_X,CLIENT_START_Y - ,HEX_WIDTH + - HEX_DUMP_GAP + DUMP_WIDTH + HEX_DUMP_GAP, - rowCount * CELL_HEIGHT)); + private void DrawClient(Graphics g) + { + using (Brush brush = new SolidBrush(SystemColors.ControlLightLight)) + { + g.FillRectangle(brush, new Rectangle(HEX_START_X, + CLIENT_START_Y, + HEX_WIDTH + HEX_DUMP_GAP + DUMP_WIDTH + HEX_DUMP_GAP, + _rowCount * CELL_HEIGHT)); } - using (Pen pen = new Pen(SystemColors.ControlDark)) { - g.DrawRectangle(pen, new Rectangle(HEX_START_X,CLIENT_START_Y - ,HEX_WIDTH + - HEX_DUMP_GAP + DUMP_WIDTH + HEX_DUMP_GAP - 1, - rowCount * CELL_HEIGHT - 1)); - g.DrawLine(pen, DUMP_START_X - HEX_DUMP_GAP, CLIENT_START_Y, - DUMP_START_X - HEX_DUMP_GAP, CLIENT_START_Y + rowCount * CELL_HEIGHT - 1); + using (Pen pen = new Pen(SystemColors.ControlDark)) + { + g.DrawRectangle(pen, new Rectangle(HEX_START_X, + CLIENT_START_Y, + HEX_WIDTH + HEX_DUMP_GAP + DUMP_WIDTH + HEX_DUMP_GAP - 1, + _rowCount * CELL_HEIGHT - 1)); + g.DrawLine(pen, DUMP_START_X - HEX_DUMP_GAP, + CLIENT_START_Y, + DUMP_START_X - HEX_DUMP_GAP, + CLIENT_START_Y + _rowCount * CELL_HEIGHT - 1); } } // Char.IsPrintable is going away because it's a mostly meaningless concept. // Copied code here to preserve semantics. -- BrianGru, 10/3/2000 - private static bool CharIsPrintable(char c) { + private static bool CharIsPrintable(char c) + { UnicodeCategory uc = Char.GetUnicodeCategory(c); - return (!(uc == UnicodeCategory.Control) || (uc == UnicodeCategory.Format) || + return (!(uc == UnicodeCategory.Control) || (uc == UnicodeCategory.Format) || (uc == UnicodeCategory.LineSeparator) || (uc == UnicodeCategory.ParagraphSeparator) || (uc == UnicodeCategory.OtherNotAssigned)); } - /// - /// - /// Draws the "DUMP" part in the HEXDUMP view - /// - /// - private void DrawDump(System.Drawing.Graphics g, byte[] lineBuffer, int line) { + /// + /// Draws the "DUMP" part in the HEXDUMP view + /// + private void DrawDump(Graphics g, byte[] lineBuffer, int line) + { char c; StringBuilder sb = new StringBuilder(lineBuffer.Length); - for (int i = 0; i < lineBuffer.Length; i++) { + for (int i = 0; i < lineBuffer.Length; i++) + { c = Convert.ToChar(lineBuffer[i]); if (CharIsPrintable(c)) + { sb.Append(c); + } else + { sb.Append('.'); + } } + Font font = HEXDUMP_FONT; - Brush foreground = new SolidBrush(ForeColor); - try { - g.DrawString(sb.ToString(), font, foreground, DUMP_START_X, LINE_START_Y + line * CELL_HEIGHT); - } - finally { - if (foreground != null) { - foreground.Dispose(); - } - } + using Brush foreground = new SolidBrush(ForeColor); + g.DrawString(sb.ToString(), font, foreground, DUMP_START_X, LINE_START_Y + line * CELL_HEIGHT); } - /// - /// + /// /// Draws the "HEX" part in the HEXDUMP view - /// + /// /// - private void DrawHex(System.Drawing.Graphics g, byte[] lineBuffer, int line) { + private void DrawHex(Graphics g, byte[] lineBuffer, int line) + { Font font = HEXDUMP_FONT; StringBuilder result = new StringBuilder(lineBuffer.Length * 3 + 1); - for (int i = 0; i < lineBuffer.Length; i++) { + for (int i = 0; i < lineBuffer.Length; i++) + { result.Append(lineBuffer[i].ToString("X2", CultureInfo.InvariantCulture)); result.Append(" "); - if (i == columnCount/2 - 1) + if (i == _columnCount / 2 - 1) + { result.Append(" "); //add one extra in the middle - } - Brush foreground = new SolidBrush(ForeColor); - try { - g.DrawString(result.ToString(), font, foreground, HEX_START_X + BORDER_GAP, LINE_START_Y + line * CELL_HEIGHT); - } - - finally { - if (foreground != null) { - foreground.Dispose(); } } + using Brush foreground = new SolidBrush(ForeColor); + g.DrawString(result.ToString(), font, foreground, HEX_START_X + BORDER_GAP, LINE_START_Y + line * CELL_HEIGHT); + /* ISSUE a-gregka: If perf problem, could be done this way to eliminate drawing twice on repaint The current solution good enough for a dialog box int hdc = g.getHandle(); @@ -290,456 +257,497 @@ private void DrawHex(System.Drawing.Graphics g, byte[] lineBuffer, int line) { */ } - /// - /// - /// Draws the all lines (a line includes address, hex and dump part) - /// for the HEXDUMP view - /// - /// - private void DrawLines(System.Drawing.Graphics g, int startLine, int linesCount) { - for (int i = 0; i < linesCount; i++) { - byte[] lineBuffer = ComposeLineBuffer(startLine,i); + /// + /// Draws the all lines (a line includes address, hex and dump part) for the HEXDUMP view + /// + private void DrawLines(Graphics g, int startLine, int linesCount) + { + for (int i = 0; i < linesCount; i++) + { + byte[] lineBuffer = ComposeLineBuffer(startLine, i); DrawAddress(g, startLine, i); - DrawHex(g,lineBuffer,i); - DrawDump(g,lineBuffer,i); + DrawHex(g, lineBuffer, i); + DrawDump(g, lineBuffer, i); } } - /// - /// - /// Establishes the display mode for the control based on the - /// contents of the buffer. - /// This is based on the following algorithm: - /// Count number of zeros, prinables and other characters in the half of the dataBuffer - /// Base on the following table establish the mode: - /// 80% Characters or digits -> ANSI - /// 80% Valid Unicode chars -> Unicode - /// All other cases -> HEXDUMP - /// Also for the buffer of size [0..5] it returns the HEXDUMP mode - /// + /// + /// Establishes the display mode for the control based on the contents of the buffer. + /// This is based on the following algorithm: + /// * Count number of zeros, prinables and other characters in the half of the dataBuffer + /// * Base on the following table establish the mode: + /// - 80% Characters or digits -> ANSI + /// - 80% Valid Unicode chars -> Unicode + /// - All other cases -> HEXDUMP + /// Also for the buffer of size [0..5] it returns the HEXDUMP mode + /// /// - private DisplayMode GetAutoDisplayMode() { + private DisplayMode GetAutoDisplayMode() + { int printablesCount = 0; int unicodeCount = 0; int size; - if ((dataBuf == null) || (dataBuf.Length >= 0 && (dataBuf.Length < 8))) + if ((_dataBuf == null) || (_dataBuf.Length >= 0 && (_dataBuf.Length < 8))) + { return DisplayMode.Hexdump; + } - switch(AnalizeByteOrderMark(dataBuf, 0)) { - case 2: - //_Encoding = Encoding.BigEndianUnicode; - return DisplayMode.Hexdump; - case 3: - //_Encoding = Encoding.Unicode; - return DisplayMode.Unicode; - case 4: - case 5: - //_Encoding = Ucs4Encoding.UCS4_Bigendian; - return DisplayMode.Hexdump; - case 6: - case 7: - //_Encoding = Ucs4Encoding.UCS4_Littleendian; - return DisplayMode.Hexdump; - case 8: - case 9: - //_Encoding = Ucs4Encoding.UCS4_3412; - return DisplayMode.Hexdump; - case 10: - case 11: - //_Encoding = Ucs4Encoding.UCS4_2143; - return DisplayMode.Hexdump; - case 12: - //8 ebcdic - return DisplayMode.Hexdump; - case 13: //9 - //_Encoding = new UTF8Encoding(false); - return DisplayMode.Ansi; - case 14: - return DisplayMode.Ansi; - default: - //If ByteOrderMark not detected try - if (dataBuf.Length > 1024) - size = 512; - else - size = dataBuf.Length / 2; - - for (int i = 0; i < size; i++) { - char c = (char) dataBuf[i]; //OK we do not care for Unicode now - if (Char.IsLetterOrDigit(c) || Char.IsWhiteSpace(c)) - printablesCount++; - } - for (int i = 0; i < size; i+=2) { - char[] unicodeChars = new char[1]; - Encoding.Unicode.GetChars(dataBuf, i, 2, unicodeChars, 0); - if (CharIsPrintable(unicodeChars[0])) - unicodeCount++; - } - - if (unicodeCount * 100 / (size/2) > 80) + switch (AnalizeByteOrderMark(_dataBuf, 0)) + { + case 2: + //_Encoding = Encoding.BigEndianUnicode; + return DisplayMode.Hexdump; + case 3: + //_Encoding = Encoding.Unicode; return DisplayMode.Unicode; - - if (printablesCount * 100 / size > 80) + case 4: + case 5: + //_Encoding = Ucs4Encoding.UCS4_Bigendian; + return DisplayMode.Hexdump; + case 6: + case 7: + //_Encoding = Ucs4Encoding.UCS4_Littleendian; + return DisplayMode.Hexdump; + case 8: + case 9: + //_Encoding = Ucs4Encoding.UCS4_3412; + return DisplayMode.Hexdump; + case 10: + case 11: + //_Encoding = Ucs4Encoding.UCS4_2143; + return DisplayMode.Hexdump; + case 12: + //8 ebcdic + return DisplayMode.Hexdump; + case 13: //9 + //_Encoding = new UTF8Encoding(false); + return DisplayMode.Ansi; + case 14: return DisplayMode.Ansi; - - return DisplayMode.Hexdump; - } + default: + //If ByteOrderMark not detected try + if (_dataBuf.Length > 1024) + { + size = 512; + } + else + { + size = _dataBuf.Length / 2; + } + + for (int i = 0; i < size; i++) + { + char c = (char)_dataBuf[i]; //OK we do not care for Unicode now + if (Char.IsLetterOrDigit(c) || Char.IsWhiteSpace(c)) + { + printablesCount++; + } + } + for (int i = 0; i < size; i += 2) + { + char[] unicodeChars = new char[1]; + Encoding.Unicode.GetChars(_dataBuf, i, 2, unicodeChars, 0); + if (CharIsPrintable(unicodeChars[0])) + { + unicodeCount++; + } + } + + if (unicodeCount * 100 / (size / 2) > 80) + { + return DisplayMode.Unicode; + } + + if (printablesCount * 100 / size > 80) + { + return DisplayMode.Ansi; + } + + return DisplayMode.Hexdump; + } } - /// - /// - /// Gets the bytes in the buffer. - /// - public virtual byte[] GetBytes() { - return dataBuf; + /// + /// Gets the bytes in the buffer. + /// + public virtual byte[] GetBytes() + { + return _dataBuf; } - /// - /// - /// - /// Gets the display mode for the control. - /// - /// - public virtual DisplayMode GetDisplayMode() { - return displayMode; + /// + /// Gets the display mode for the control. + /// + public virtual DisplayMode GetDisplayMode() + { + return _displayMode; } - //Stole this code from XmlSanner - private static int GetEncodingIndex(int c1) { - switch(c1) { - case 0x0000: return 1; - case 0xfeff: return 2; - case 0xfffe: return 3; - case 0xefbb: return 4; - case 0x3c00: return 5; - case 0x003c: return 6; - case 0x3f00: return 7; - case 0x003f: return 8; - case 0x3c3f: return 9; - case 0x786d: return 10; - case 0x4c6f: return 11; - case 0xa794: return 12; - default: return 0; //unknown + // Stole this code from XmlSanner + private static int GetEncodingIndex(int c1) + { + switch (c1) + { + case 0x0000: + return 1; + case 0xfeff: + return 2; + case 0xfffe: + return 3; + case 0xefbb: + return 4; + case 0x3c00: + return 5; + case 0x003c: + return 6; + case 0x3f00: + return 7; + case 0x003f: + return 8; + case 0x3c3f: + return 9; + case 0x786d: + return 10; + case 0x4c6f: + return 11; + case 0xa794: + return 12; + default: + return 0; //unknown } } - - /// - /// - /// Initializes the ansi string variable that will be assigned to the edit box. - /// - /// - private void InitAnsi() { - int size = dataBuf.Length; + + /// + /// Initializes the ansi string variable that will be assigned to the edit box. + /// + private void InitAnsi() + { + int size = _dataBuf.Length; char[] text = new char[size + 1]; - size = NativeMethods.MultiByteToWideChar(0, 0, dataBuf, size, text, size); + size = Interop.Kernel32.MultiByteToWideChar(0, 0, _dataBuf, size, text, size); text[size] = (char)0; for (int i = 0; i < size; i++) - if (text[i] == '\0') text[i] = (char)0x0B; - - edit.Text = new string(text); + { + if (text[i] == '\0') + { + text[i] = (char)0x0B; + } + } + + _edit.Text = new string(text); } - /// - /// - /// Initializes the Unicode string varible that will be assigned to the edit box - /// - /// - private void InitUnicode() { - char[] text = new char[dataBuf.Length/2+1]; - Encoding.Unicode.GetChars(dataBuf, 0, dataBuf.Length, text, 0); + /// + /// Initializes the Unicode string varible that will be assigned to the edit box + /// + private void InitUnicode() + { + char[] text = new char[_dataBuf.Length / 2 + 1]; + Encoding.Unicode.GetChars(_dataBuf, 0, _dataBuf.Length, text, 0); for (int i = 0; i < text.Length; i++) - if (text[i] == '\0') text[i] = (char)0x0B; - + if (text[i] == '\0') + text[i] = (char)0x0B; + text[text.Length - 1] = '\0'; - edit.Text = new string(text); + _edit.Text = new string(text); } - /// - /// - /// Initializes the UI components of a control - /// - /// - private void InitUI() { - - SCROLLBAR_HEIGHT = SystemInformation.HorizontalScrollBarHeight; - SCROLLBAR_WIDTH = SystemInformation.VerticalScrollBarWidth; + /// + /// Initializes the UI components of a control + /// + private void InitUI() + { + SCROLLBAR_HEIGHT = SystemInformation.HorizontalScrollBarHeight; + SCROLLBAR_WIDTH = SystemInformation.VerticalScrollBarWidth; // For backwards compat - this.Size = new Size(SCROLLBAR_START_X + SCROLLBAR_WIDTH + BORDER_GAP + INSET_GAP, - 2 * (BORDER_GAP + INSET_GAP) + rowCount * (CELL_HEIGHT)); - - - scrollBar = new VScrollBar(); - scrollBar.ValueChanged += new EventHandler(this.ScrollChanged); - scrollBar.TabStop = true; - scrollBar.TabIndex = 0; - scrollBar.Dock = DockStyle.Right; - scrollBar.Visible = false; - - edit = new TextBox(); - edit.AutoSize = false; - edit.BorderStyle = BorderStyle.None; - edit.Multiline = true; - edit.ReadOnly = true; - edit.ScrollBars = ScrollBars.Both; - edit.AcceptsTab = true; - edit.AcceptsReturn = true; - edit.Dock = DockStyle.Fill; - edit.Margin = Padding.Empty; - edit.WordWrap = false; - edit.Visible = false; - - Controls.Add(scrollBar, 0, 0); - Controls.Add(edit, 0 ,0); + Size = new Size(SCROLLBAR_START_X + SCROLLBAR_WIDTH + BORDER_GAP + INSET_GAP, + 2 * (BORDER_GAP + INSET_GAP) + _rowCount * (CELL_HEIGHT)); + + _scrollBar = new VScrollBar(); + _scrollBar.ValueChanged += new EventHandler(ScrollChanged); + _scrollBar.TabStop = true; + _scrollBar.TabIndex = 0; + _scrollBar.Dock = DockStyle.Right; + _scrollBar.Visible = false; + + _edit = new TextBox(); + _edit.AutoSize = false; + _edit.BorderStyle = BorderStyle.None; + _edit.Multiline = true; + _edit.ReadOnly = true; + _edit.ScrollBars = ScrollBars.Both; + _edit.AcceptsTab = true; + _edit.AcceptsReturn = true; + _edit.Dock = DockStyle.Fill; + _edit.Margin = Padding.Empty; + _edit.WordWrap = false; + _edit.Visible = false; + + Controls.Add(_scrollBar, 0, 0); + Controls.Add(_edit, 0, 0); } - /// - /// - /// Initializes some important variables - /// - /// - private void InitState() { + /// + /// Initializes some important variables + /// + private void InitState() + { // calculate number of lines required (being careful to count 1 for the last partial line) - linesCount = (dataBuf.Length + columnCount - 1) / columnCount; - - startLine = 0; - if (linesCount > rowCount) { - displayLinesCount = rowCount; - scrollBar.Hide(); - scrollBar.Maximum = linesCount - 1; - scrollBar.LargeChange = rowCount; - scrollBar.Show(); - scrollBar.Enabled = true; + _linesCount = (_dataBuf.Length + _columnCount - 1) / _columnCount; + + _startLine = 0; + if (_linesCount > _rowCount) + { + _displayLinesCount = _rowCount; + _scrollBar.Hide(); + _scrollBar.Maximum = _linesCount - 1; + _scrollBar.LargeChange = _rowCount; + _scrollBar.Show(); + _scrollBar.Enabled = true; } - else { - displayLinesCount = linesCount; - scrollBar.Hide(); - scrollBar.Maximum = rowCount; - scrollBar.LargeChange = rowCount; - scrollBar.Show(); - scrollBar.Enabled = false; + else + { + _displayLinesCount = _linesCount; + _scrollBar.Hide(); + _scrollBar.Maximum = _rowCount; + _scrollBar.LargeChange = _rowCount; + _scrollBar.Show(); + _scrollBar.Enabled = false; } - scrollBar.Select(); + _scrollBar.Select(); Invalidate(); } - - /// - /// - /// KeyDown handler. - /// - /// - protected override void OnKeyDown(KeyEventArgs e) { - scrollBar.Select(); + + /// + /// KeyDown handler. + /// + protected override void OnKeyDown(KeyEventArgs e) + { + _scrollBar.Select(); } - /// - /// - /// - /// - /// Paint handler - /// for the control. - /// - /// - protected override void OnPaint(PaintEventArgs e) { + /// + /// Paint handler for the control. + /// + protected override void OnPaint(PaintEventArgs e) + { base.OnPaint(e); Graphics g = e.Graphics; - switch (realDisplayMode) { + switch (_realDisplayMode) + { case DisplayMode.Hexdump: - this.SuspendLayout(); - edit.Hide(); - scrollBar.Show(); - this.ResumeLayout(); + SuspendLayout(); + _edit.Hide(); + _scrollBar.Show(); + ResumeLayout(); DrawClient(g); - DrawLines(g, startLine, displayLinesCount); + DrawLines(g, _startLine, _displayLinesCount); break; + case DisplayMode.Ansi: - edit.Invalidate(); + _edit.Invalidate(); break; + case DisplayMode.Unicode: - edit.Invalidate(); + _edit.Invalidate(); break; } } - /// - /// - /// - /// - /// Resize handler for the control. - /// - /// - - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1725:ParameterNamesShouldMatchBaseDeclaration", MessageId="0#")] - protected override void OnLayout(LayoutEventArgs e) { - + /// + /// Resize handler for the control. + /// + protected override void OnLayout(LayoutEventArgs e) + { // Must call this first since we might return base.OnLayout(e); - + int rows = (ClientSize.Height - 2 * (BORDER_GAP + INSET_GAP)) / CELL_HEIGHT; - if (rows >= 0 && rows != rowCount) - rowCount = rows; - else + if (rows >= 0 && rows != _rowCount) + { + _rowCount = rows; + } + else + { return; + } - if (Dock == DockStyle.None) { + if (Dock == DockStyle.None) + { // For backwards compatibility - Size = new Size(SCROLLBAR_START_X + SCROLLBAR_WIDTH + BORDER_GAP + INSET_GAP, 2 * (BORDER_GAP + INSET_GAP) + rowCount * (CELL_HEIGHT)); + Size = new Size(SCROLLBAR_START_X + SCROLLBAR_WIDTH + BORDER_GAP + INSET_GAP, + 2 * (BORDER_GAP + INSET_GAP) + _rowCount * (CELL_HEIGHT)); } - if (scrollBar != null) { - if (linesCount > rowCount) { - scrollBar.Hide(); - scrollBar.Maximum = linesCount - 1; - scrollBar.LargeChange = rowCount; - scrollBar.Show(); - scrollBar.Enabled = true; - scrollBar.Select(); + if (_scrollBar != null) + { + if (_linesCount > _rowCount) + { + _scrollBar.Hide(); + _scrollBar.Maximum = _linesCount - 1; + _scrollBar.LargeChange = _rowCount; + _scrollBar.Show(); + _scrollBar.Enabled = true; + _scrollBar.Select(); } else - scrollBar.Enabled = false; + { + _scrollBar.Enabled = false; + } } - displayLinesCount = (startLine + rowCount < linesCount) ? rowCount : linesCount - startLine; + + _displayLinesCount = (_startLine + _rowCount < _linesCount) ? _rowCount : _linesCount - _startLine; } - /// - /// - /// - /// Writes the raw data from the data buffer to a file. - /// - /// - public virtual void SaveToFile(string path) { - if (dataBuf != null) { - FileStream currentFile = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); - try { - currentFile.Write(dataBuf,0,dataBuf.Length); - currentFile.Close(); - } - catch { - currentFile.Close(); - throw; - } + /// + /// Writes the raw data from the data buffer to a file. + /// + public virtual void SaveToFile(string path) + { + if (_dataBuf == null) + { + return; + } + + FileStream currentFile = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); + try + { + currentFile.Write(_dataBuf, 0, _dataBuf.Length); + currentFile.Close(); + } + catch + { + currentFile.Close(); + throw; } } - /// - /// - /// - /// - /// Scroll event handler. - /// - /// - protected virtual void ScrollChanged(object source, EventArgs e) { - startLine = scrollBar.Value; - + /// + /// Scroll event handler. + /// + protected virtual void ScrollChanged(object source, EventArgs e) + { + _startLine = _scrollBar.Value; + Invalidate(); } - - /// - /// - /// - /// Sets the byte array to be displayed in the viewer. - /// - /// - public virtual void SetBytes(byte[] bytes) { + /// + /// Sets the byte array to be displayed in the viewer. + /// + public virtual void SetBytes(byte[] bytes) + { if (bytes == null) + { throw new ArgumentNullException("bytes"); - - if (dataBuf != null) - dataBuf = null; - dataBuf = bytes; + } + + if (_dataBuf != null) + { + _dataBuf = null; + } + + _dataBuf = bytes; InitState(); - SetDisplayMode(displayMode); + SetDisplayMode(_displayMode); } - /// - /// - /// - /// Sets the current display mode. - /// - /// - public virtual void SetDisplayMode(DisplayMode mode) { - if (!ClientUtils.IsEnumValid(mode,(int)mode, (int)DisplayMode.Hexdump, (int)DisplayMode.Auto)) + /// + /// Sets the current display mode. + /// + public virtual void SetDisplayMode(DisplayMode mode) + { + if (!ClientUtils.IsEnumValid(mode, (int)mode, (int)DisplayMode.Hexdump, (int)DisplayMode.Auto)) + { throw new InvalidEnumArgumentException("mode", (int)mode, typeof(DisplayMode)); - - displayMode = mode; + } - realDisplayMode = (mode == DisplayMode.Auto) ? GetAutoDisplayMode() : mode; + _displayMode = mode; + _realDisplayMode = (mode == DisplayMode.Auto) ? GetAutoDisplayMode() : mode; - - switch (realDisplayMode) { + switch (_realDisplayMode) + { case DisplayMode.Ansi: InitAnsi(); - this.SuspendLayout(); - edit.Show(); - scrollBar.Hide(); - this.ResumeLayout(); + SuspendLayout(); + _edit.Show(); + _scrollBar.Hide(); + ResumeLayout(); Invalidate(); break; + case DisplayMode.Unicode: InitUnicode(); - this.SuspendLayout(); - edit.Show(); - scrollBar.Hide(); - this.ResumeLayout(); + SuspendLayout(); + _edit.Show(); + _scrollBar.Hide(); + ResumeLayout(); Invalidate(); break; + case DisplayMode.Hexdump: - this.SuspendLayout(); - edit.Hide(); - if (linesCount > rowCount) { - if (!scrollBar.Visible) { - scrollBar.Show(); - this.ResumeLayout(); - scrollBar.Invalidate(); - scrollBar.Select(); + SuspendLayout(); + _edit.Hide(); + if (_linesCount > _rowCount) + { + if (!_scrollBar.Visible) + { + _scrollBar.Show(); + ResumeLayout(); + _scrollBar.Invalidate(); + _scrollBar.Select(); } - else { - this.ResumeLayout(); + else + { + ResumeLayout(); } } - else { - this.ResumeLayout(); + else + { + ResumeLayout(); } break; } - } - /// - /// - /// - /// Sets the file to be displayed in the viewer. - /// - /// - public virtual void SetFile(string path) { + /// + /// Sets the file to be displayed in the viewer. + /// + public virtual void SetFile(string path) + { FileStream currentFile = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None); - try { + try + { int length = (int)currentFile.Length; - byte[] buf = new byte[length+1]; - currentFile.Read(buf,0,length); + byte[] buf = new byte[length + 1]; + currentFile.Read(buf, 0, length); SetBytes(buf); currentFile.Close(); } - catch { + catch + { currentFile.Close(); throw; } } - /// - /// - /// - /// Sets the current line for the HEXDUMP view. - /// - /// - public virtual void SetStartLine(int line) { - if (line < 0 || line >= linesCount || line > dataBuf.Length / columnCount) - startLine = 0; + /// + /// Sets the current line for the HEXDUMP view. + /// + public virtual void SetStartLine(int line) + { + if (line < 0 || line >= _linesCount || line > _dataBuf.Length / _columnCount) + { + _startLine = 0; + } else - startLine = line; + { + _startLine = line; + } } } } diff --git a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DisplayMode.cs b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DisplayMode.cs index e9500a080cd..f71404e4577 100644 --- a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DisplayMode.cs +++ b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DisplayMode.cs @@ -1,53 +1,33 @@ -//------------------------------------------------------------------------------ -// -// Copyright (c) Microsoft Corporation. All rights reserved. -// -//------------------------------------------------------------------------------ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. -namespace System.ComponentModel.Design { - - using System.Diagnostics; - - using System; - using System.ComponentModel; - using Microsoft.Win32; +namespace System.ComponentModel.Design +{ + /// + /// Specifies identifiers to indicate the display modes used by . + /// + public enum DisplayMode + { + /// + /// Indicates using Hexadecimal format. + /// + Hexdump = 1, - /// - /// - /// Specifies identifiers to indicate the display modes used - /// by . - /// - public enum DisplayMode { + /// + /// Indicates using ANSI format. + /// + Ansi = 2, - /// - /// - /// - /// Indicates using - /// Hexadecimal format. - /// - /// - Hexdump = 1, - /// - /// - /// - /// Indicates using ANSI format. - /// - /// - Ansi = 2, - /// - /// - /// - /// Indicates using Unicode format. - /// - /// + /// + /// Indicates using Unicode format. + /// Unicode = 3, - /// - /// - /// - /// Indicates using automatic format selection. - /// - /// - Auto = 4, + + /// + /// Indicates using automatic format selection. + /// + Auto = 4, } }