Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
john.bayly@tipstrade.net committed Nov 13, 2017
1 parent a868871 commit 2b70c77
Show file tree
Hide file tree
Showing 7 changed files with 428 additions and 55 deletions.
9 changes: 6 additions & 3 deletions DragDropViewer/DragDropViewer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Form1.cs">
<Compile Include="MainForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
<Compile Include="MainForm.Designer.cs">
<DependentUpon>MainForm.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="MainForm.resx">
<DependentUpon>MainForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
Expand Down
34 changes: 0 additions & 34 deletions DragDropViewer/Form1.Designer.cs

This file was deleted.

17 changes: 0 additions & 17 deletions DragDropViewer/Form1.cs

This file was deleted.

124 changes: 124 additions & 0 deletions DragDropViewer/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

177 changes: 177 additions & 0 deletions DragDropViewer/MainForm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
using System;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace DragDropViewer {
public partial class MainForm : Form {
#region Fields
private Control preview;
#endregion

#region Properties
private Control PreviewControl {
get {
return preview;
}
set {
var current = PreviewControl;
if (current != null) {
tableLayoutPanel1.Controls.Remove(current);
current.Dispose();
current = null;
}

preview = value;

if (preview != null) {
// Some special cases
if (preview is TextBoxBase) {
((TextBoxBase)preview).Multiline = true;
}

value.Dock = DockStyle.Fill;

tableLayoutPanel1.Controls.Add(value, 1, 1);
tableLayoutPanel1.SetRowSpan(value, 2);
}
}
}

private DataFormat SelectedItem {
get {
return listFormats.SelectedIndex == -1 ? null : (DataFormat)listFormats.SelectedItem;
}
}
#endregion

#region Constructors
public MainForm() {
InitializeComponent();
}
#endregion

#region Methods
private void LoadData(IDataObject data) {
listFormats.SelectedIndexChanged -= listFormats_SelectedIndexChanged;
listFormats.BeginUpdate();

listFormats.Items.Clear();
if (data != null) {
foreach (var fmt in data.GetFormats(true)) {
var item = new DataFormat() {
Data = data.GetData(fmt),
Name = fmt
};
listFormats.Items.Add(item);
}
}

listFormats.EndUpdate();
listFormats.SelectedIndex = -1;
listFormats.SelectedIndexChanged += listFormats_SelectedIndexChanged;

}

private void OnSelectedItemChanged() {
var selected = SelectedItem;

propertyGrid.SelectedObject = selected?.Data;

if ((selected == null) || (selected.Data == null)) {
PreviewControl = null;
return;
}

if (selected.Name == "Rich Text Format") {
PreviewControl = new RichTextBox() {
Rtf = (string)selected.Data
};

} else if (selected.Name == "text/html") {
var stream = selected.Data as Stream;
stream.Position = 0;

PreviewControl = new WebBrowser() {
DocumentStream = stream
};

} else if (selected.Type == typeof(string)) {
PreviewControl = new TextBox() {
Text = (string)selected.Data
};

} else if (selected.Type == typeof(string[])) {
PreviewControl = new TextBox() {
Lines = (string[])selected.Data
};

} else if (selected.Type == typeof(MemoryStream)) {
var ms = selected.Data as MemoryStream;
ms.Position = 0;

Encoding enc;
if (
selected.Name.EndsWith("W")
|| selected.Name.Contains("x-moz")
) {
enc = Encoding.Unicode;
} else {
enc = Encoding.UTF8;
}

using (var reader = new StreamReader(ms, enc, true, 1024, true)) {
PreviewControl = new TextBox() {
Text = reader.ReadToEnd()
};
}

} else if (selected.Type == typeof(Bitmap)) {
PreviewControl = new PictureBox() {
Image = (Bitmap)selected.Data,
SizeMode = PictureBoxSizeMode.Zoom
};

} else {
PreviewControl = null;
}

}
#endregion

#region Event handlers
private void butFromClipboard_Click(object sender, EventArgs e) {
LoadData(Clipboard.GetDataObject());
}

private void listFormats_SelectedIndexChanged(object sender, EventArgs e) {
OnSelectedItemChanged();
}

private void MainForm_DragEnter(object sender, DragEventArgs e) {
e.Effect = e.AllowedEffect;
}

private void MainForm_DragDrop(object sender, DragEventArgs e) {
LoadData(e.Data);
}
#endregion

#region Inner classes
public class DataFormat {
public object Data { get; set; }

public string Name { get; set; }

public Type Type => Data?.GetType();

public string TypeName => Type == null ? "null" : Type.Name;

public override string ToString() {
return $"{TypeName} {Name}";
}
}
#endregion
}
}
Loading

0 comments on commit 2b70c77

Please sign in to comment.