Browse Source

Add editor in the property grid for properties of type ImageSource.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/wpfdesigner@5964 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
pull/1/head
Kumar Devvrat 16 years ago
parent
commit
0448eb6924
  1. 164
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ImageSourceEditor/ChooseImageDialog.xaml
  2. 186
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ImageSourceEditor/ChooseImageDialog.xaml.cs
  3. 19
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ImageSourceEditor/ImageSourceEditor.xaml
  4. 46
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ImageSourceEditor/ImageSourceEditor.xaml.cs
  5. 68
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ProjectTools.cs
  6. 14
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/WpfDesign.AddIn.csproj

164
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ImageSourceEditor/ChooseImageDialog.xaml

@ -0,0 +1,164 @@ @@ -0,0 +1,164 @@
<?xml version="1.0" encoding="utf-8"?>
<Window
x:Class="ICSharpCode.WpfDesign.AddIn.ImageSourceEditor.ChooseImageDialog" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="{x:Static SystemColors.ControlBrush}"
SnapsToDevicePixels="True"
WindowStartupLocation="CenterScreen"
ResizeMode="CanResizeWithGrip"
Title="Choose Image"
Height="438"
Width="488">
<Window.Resources>
<Style
TargetType="ListBox">
<Setter
Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel
Orientation="Horizontal"
VerticalAlignment="Top"
HorizontalAlignment="Center" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
<CollectionViewSource
x:Key="groups"
Source="{Binding}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription
PropertyName="Header" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Window.Resources>
<Grid>
<TextBlock
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="12,12,0,0"
Text="Existing images"
Height="24" />
<Rectangle
Fill="White"
Margin="12,36,12,120" />
<ListBox
Margin="12,36,12,120"
Name="Display"
ItemsSource="{Binding}">
<ItemsControl.GroupStyle>
<GroupStyle />
</ItemsControl.GroupStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<!-- Set animation for the underlying listbox which displays the images -->
<Grid.Triggers>
<EventTrigger
SourceName="expander"
RoutedEvent="Expander.Expanded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
From="0"
To="1"
Duration="0:0:0.1"
Storyboard.TargetName="imgDisplay"
Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(ScaleTransform.ScaleY)" />
<DoubleAnimation
From="0"
To="1"
Duration="0:0:0.1"
Storyboard.TargetName="imgDisplay"
Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(ScaleTransform.ScaleX)" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Grid.Triggers>
<Expander
Header="{Binding Header}"
HorizontalAlignment="Stretch"
Name="expander"
Margin="2,3,0,3"
BorderThickness="0">
<ListBox
ItemsSource="{Binding Images}"
Name="imgDisplay"
SelectionChanged="imgDisplaySelectionChanged"
Loaded="imgDisplayLoaded"
BorderThickness="0">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid
Margin="10">
<Border
BorderThickness="1"
BorderBrush="#FF717171"
Background="White">
<Border.Effect>
<DropShadowEffect
Color="#FFA1A1A1" />
</Border.Effect>
<Image
Source="{Binding}"
Height="100"
Width="100"
Margin="2,5,2,5" />
</Border>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.LayoutTransform>
<ScaleTransform
ScaleX="0"
ScaleY="0" />
</ListBox.LayoutTransform>
</ListBox>
</Expander>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListBox>
<Button
Height="23"
Width="75"
Margin="0,0,12,88"
VerticalAlignment="Bottom"
HorizontalAlignment="Right"
Content="Add"
Click="AddClick" />
<TextBlock
Text="URL :"
VerticalAlignment="Bottom"
HorizontalAlignment="Left"
Margin="12,0,0,53"
Height="24" />
<TextBox
HorizontalAlignment="Stretch"
VerticalAlignment="Bottom"
Margin="50,0,12,56"
Height="24"
Name="txURL" />
<Button
x:Name="uxOk"
Content="OK"
Height="23"
Margin="0,0,93,12"
VerticalAlignment="Bottom"
HorizontalAlignment="Right"
Width="75"
Click="OkClick" />
<Button
Content="Cancel"
Name="uxCancel"
Click="Cancel"
Height="23"
HorizontalAlignment="Right"
Margin="0,0,12,12"
VerticalAlignment="Bottom"
Width="75"
IsCancel="True" />
</Grid>
</Window>

186
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ImageSourceEditor/ChooseImageDialog.xaml.cs

@ -0,0 +1,186 @@ @@ -0,0 +1,186 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <author name="Kumar Devvrat"/>
// <version>$Revision: $</version>
// </file>
using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Win32;
using ICSharpCode.WpfDesign.PropertyGrid;
namespace ICSharpCode.WpfDesign.AddIn.ImageSourceEditor
{
/// <summary>
/// Dialog which allows user to add images to the project
/// </summary>
public partial class ChooseImageDialog : Window
{
/// <summary>
/// Inner ListBox which displays the images
/// </summary>
private ListBox _imgDisplay;
/// <summary>
/// Contains the allowed extensions for image files.
/// </summary>
private static string[] Extension;
private PropertyNode _node;
private List<ImageData> _data;
static ChooseImageDialog()
{
Extension = new String[]{".jpg", ".bmp", ".png", ".gif", ".ico", ".dib", ".jpe", ".jpeg", ".tif", ".tiff"};
}
public ChooseImageDialog(PropertyNode node)
{
this._node=node;
this._data=new List<ImageData>();
InitializeComponent();
}
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
List<KeyValuePair<string, string>> images=new List<KeyValuePair<string, string>>();
/* Get image file with allowed extensions and group them with their directory */
images=ProjectTools.RetrieveFiles(ChooseImageDialog.Extension);
IEnumerable<IGrouping<string, string>> grouping = images.GroupBy(image => image.Key, image => image.Value);
/* Set values for _data and bind to the ListBox */
foreach(IGrouping<string, string> group in grouping){
List<string> temp=new List<string>();
foreach(var name in group){
temp.Add(name);
}
_data.Add(new ImageData(group.Key + Path.DirectorySeparatorChar,temp));
}
Display.ItemsSource=_data;
}
#region Event Handlers
private void AddClick(object sender, RoutedEventArgs e)
{
OpenFileDialog dialog=new OpenFileDialog();
dialog.Filter="Image Files | *" + String.Join(";*",Extension);
dialog.Multiselect=true;
dialog.CheckFileExists=true;
dialog.Title="Choose Image";
if(dialog.ShowDialog() == true){
string []fileNames=dialog.FileNames;
ProjectTools.AddFiles(fileNames);
/* Add files to _data so that the ListBox is updated. Note that as Images are added to the project directory,
* images will be added in the _data with ImageData having header Path.DirectorySeparatorChar. */
List<string> temp=new List<string>();
foreach(var file in fileNames){
temp.Add(Path.GetFileName(file));
}
_data.OrderBy(image => image.Header).ElementAt(0).Images.AddRange(fileNames);
}
}
private void imgDisplayLoaded(object sender, RoutedEventArgs e)
{
_imgDisplay=new ListBox();
_imgDisplay=sender as ListBox;
_imgDisplay.MouseDoubleClick+=ImageDisplayDoubleClick;
}
private void imgDisplaySelectionChanged(object sender, RoutedEventArgs e)
{
_imgDisplay=sender as ListBox;
if(_imgDisplay.SelectedItem!=null)
txURL.Text=(string)_imgDisplay.SelectedItem;
}
private void OkClick(object sender, RoutedEventArgs e)
{
Save();
}
private void ImageDisplayDoubleClick(object sender, RoutedEventArgs e)
{
if(_imgDisplay.SelectedItem!=null){
_node.Value=_imgDisplay.SelectedItem;
Close();
}
}
protected override void OnPreviewKeyUp(KeyEventArgs e)
{
base.OnPreviewKeyUp(e);
if(e.Key==Key.Enter)
Save();
}
private void Save()
{
Debug.Assert(_imgDisplay!=null);
if(_imgDisplay.SelectedItem!=null){
_node.Value=_imgDisplay.SelectedItem;
Close();
}else{
if(File.Exists(txURL.Text) && uxOk.IsFocused){
if(Extension.Contains(Path.GetExtension(txURL.Text)))
_node.Value=Path.GetFullPath(txURL.Text);
else
MessageBox.Show(this, "The specified file is not a valid image file", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Close();
}
else{
MessageBox.Show(this, "The specified file does not exist on the disk", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
private void Cancel(object sender, RoutedEventArgs e)
{
Close();
}
void DisplaySelectionChanged(object sender, SelectionChangedEventArgs e)
{
Display.SelectedItem=null;
}
#endregion
}
public class ImageData
{
/// <summary>
/// Stores the directory where <see cref="Images"/> are there.
/// </summary>
public string Header { get; set; }
/// <summary>
/// Contains the name of all images in <see cref="Header"/>.
/// </summary>
public List<string> Images { get; set; }
public ImageData(string header,List<string> images)
{
this.Header=header;
this.Images=images;
}
}
}

19
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ImageSourceEditor/ImageSourceEditor.xaml

@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Grid
x:Class="ICSharpCode.WpfDesign.AddIn.ImageSourceEditor.ImageSourceEditor" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition
Width="20" />
</Grid.ColumnDefinitions>
<TextBox
Text="{Binding Value}"
Background="{x:Null}"
Grid.Column="0"
BorderThickness="0" />
<Button
Content="..."
Name="ChooseImage"
Grid.Column="1"
Click="ChooseImageClick" />
</Grid>

46
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ImageSourceEditor/ImageSourceEditor.xaml.cs

@ -0,0 +1,46 @@ @@ -0,0 +1,46 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <author name="Kumar Devvrat"/>
// <version>$Revision: $</version>
// </file>
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using ICSharpCode.WpfDesign.PropertyGrid;
namespace ICSharpCode.WpfDesign.AddIn.ImageSourceEditor
{
/// <summary>
/// Editor to edit properties of type of ImageSource such as Windows.Icon or Image.Source
/// </summary>
[TypeEditor(typeof (ImageSource))]
public partial class ImageSourceEditor
{
public ImageSourceEditor()
{
InitializeComponent();
}
private void ChooseImageClick(object sender, RoutedEventArgs e)
{
ChooseImageDialog cid = new ChooseImageDialog(PropertyNode);
cid.ShowActivated = true;
cid.Show();
}
/// <summary>
/// Gets the property node that the editor is editing.
/// </summary>
public PropertyNode PropertyNode{
get { return DataContext as PropertyNode; }
}
}
}

68
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ProjectTools.cs

@ -0,0 +1,68 @@ @@ -0,0 +1,68 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <author name="Kumar Devvrat"/>
// <version>$Revision: $</version>
// </file>
using System;
using System.IO;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.SharpDevelop.Project;
using ICSharpCode.Core;
namespace ICSharpCode.WpfDesign.AddIn
{
/// <summary>
/// Static helper methods to interact with the project
/// </summary>
public class ProjectTools
{
/// <summary>
/// Add files to the current project at the project node.
/// </summary>
/// <param name="fileNames"></param>
internal static void AddFiles(string []fileNames)
{
IProject project=ProjectService.CurrentProject;
ProjectNode projectNode=ProjectBrowserPad.Instance.CurrentProject;
Debug.Assert(project!=null);
Debug.Assert(projectNode!=null);
foreach(var file in fileNames){
string relFileName=FileUtility.GetRelativePath(project.Directory,file);
FileProjectItem fileProjectItem=new FileProjectItem(project,project.GetDefaultItemType(file),relFileName);
FileNode fileNode=new FileNode(file,FileNodeStatus.InProject);
fileNode.ProjectItem=fileProjectItem;
fileNode.InsertSorted(projectNode);
ProjectService.AddProjectItem(project,fileProjectItem);
}
project.Save();
}
/// <summary>
/// Get all files with given <param name="Extension"/> in the project and returns directory and the filename
/// </summary>
/// <param name="Extension"></param>
/// <returns></returns>
internal static List<KeyValuePair<string, string>> RetrieveFiles(string []Extension)
{
List<KeyValuePair<string, string>> files=new List<KeyValuePair<string, string>>();
IProject project=ProjectService.CurrentProject;
Debug.Assert(project!=null);
foreach(var item in project.Items){
FileProjectItem fileProjectItem=item as FileProjectItem;
if(fileProjectItem!=null){
string dirName=Path.GetDirectoryName(fileProjectItem.VirtualName);
if(Extension.Contains(Path.GetExtension(fileProjectItem.VirtualName)))
files.Add(new KeyValuePair<string, string>(dirName, fileProjectItem.FileName));
}
}
return files;
}
}
}

14
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/WpfDesign.AddIn.csproj

@ -63,11 +63,20 @@ @@ -63,11 +63,20 @@
<Compile Include="Src\CSharpEventHandlerService.cs" />
<Compile Include="Src\FileUriContext.cs" />
<Compile Include="Src\IdeChooseClassService.cs" />
<Compile Include="Src\ImageSourceEditor\ChooseImageDialog.xaml.cs">
<DependentUpon>ChooseImageDialog.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="Src\ImageSourceEditor\ImageSourceEditor.xaml.cs">
<DependentUpon>ImageSourceEditor.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="Src\MyTypeFinder.cs" />
<Compile Include="Src\ObjectEditor.xaml.cs">
<DependentUpon>ObjectEditor.xaml</DependentUpon>
</Compile>
<Compile Include="Src\OutlineViewPad.cs" />
<Compile Include="Src\ProjectTools.cs" />
<Compile Include="Src\PropertyDescriptionService.cs" />
<Compile Include="Src\WpfAndWinFormsTopLevelWindowService.cs" />
<Compile Include="Src\WpfDisplayBinding.cs" />
@ -131,6 +140,11 @@ @@ -131,6 +140,11 @@
<Name>WpfDesign</Name>
<Private>False</Private>
</ProjectReference>
<Page Include="Src\ImageSourceEditor\ChooseImageDialog.xaml" />
<Page Include="Src\ImageSourceEditor\ImageSourceEditor.xaml" />
<Page Include="Src\ObjectEditor.xaml" />
</ItemGroup>
<ItemGroup>
<Folder Include="Src\ImageSourceEditor" />
</ItemGroup>
</Project>
Loading…
Cancel
Save