Browse Source

Applied patch by Ivan Shumilin: Choose class dialog for WPF Designer

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@3141 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 18 years ago
parent
commit
0d9676f6f5
  1. 110
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ChooseClass.cs
  2. 69
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ChooseClassDialog.xaml
  3. 114
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ChooseClassDialog.xaml.cs
  4. 20
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ChooseClassService.cs
  5. 44
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/GetBitmapExtension.cs
  6. 8
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ObjectEditor.xaml
  7. 25
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ObjectEditor.xaml.cs
  8. 2
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/WpfViewContent.cs
  9. 10
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/WpfDesign.AddIn.csproj

110
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ChooseClass.cs

@ -0,0 +1,110 @@ @@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Data;
using ICSharpCode.SharpDevelop.Dom;
namespace ICSharpCode.WpfDesign.AddIn
{
public class ChooseClass : INotifyPropertyChanged
{
public ChooseClass(IProjectContent projectContent)
{
this.projectContent = projectContent;
AddClassesRecursive("");
projectClasses.Sort((c1, c2) => c1.Name.CompareTo(c2.Name));
classes = new ListCollectionView(projectClasses);
classes.Filter = FilterPredicate;
}
IProjectContent projectContent;
List<IClass> projectClasses = new List<IClass>();
void AddClassesRecursive(string ns)
{
foreach (var item in projectContent.GetNamespaceContents(ns)) {
if (item is string) {
AddClassesRecursive(ns.Length == 0 ? item.ToString() : ns + "." + item);
} else if (item is IClass) {
IClass c = item as IClass;
if (c.IsPartial) {
if (projectClasses.Contains(c)) continue;
}
if (c.ClassType == ClassType.Class && c.IsPublic) {
projectClasses.Add(c);
}
}
}
}
ListCollectionView classes;
public ICollectionView Classes {
get { return classes; }
}
string filter;
public string Filter {
get {
return filter;
}
set {
filter = value;
Classes.Refresh();
RaisePropertyChanged("Filter");
}
}
bool showSystemClasses;
public bool ShowSystemClasses {
get {
return showSystemClasses;
}
set {
showSystemClasses = value;
Classes.Refresh();
RaisePropertyChanged("ShowSystemClasses");
}
}
public IClass CurrentClass {
get { return Classes.CurrentItem as IClass; }
}
bool FilterPredicate(object item)
{
IClass c = item as IClass;
if (!ShowSystemClasses) {
if (c.Namespace.StartsWith("System") || c.Namespace.StartsWith("Microsoft")) {
return false;
}
}
return Match(c.Name, Filter);
}
static bool Match(string className, string filter)
{
if (string.IsNullOrEmpty(filter))
return true;
else
return className.StartsWith(filter, StringComparison.InvariantCultureIgnoreCase);
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
void RaisePropertyChanged(string name)
{
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
#endregion
}
}

69
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ChooseClassDialog.xaml

@ -0,0 +1,69 @@ @@ -0,0 +1,69 @@
<Window x:Class="ICSharpCode.WpfDesign.AddIn.ChooseClassDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sd="clr-namespace:ICSharpCode.WpfDesign.AddIn"
Background="{x:Static SystemColors.ControlBrush}"
SnapsToDevicePixels="True"
WindowStartupLocation="CenterScreen"
ResizeMode="CanResizeWithGrip"
Title="Choose Class"
Height="438"
Width="488">
<Grid>
<TextBlock Text="Starts with:"
HorizontalAlignment="Left"
Margin="12,12,0,0"
VerticalAlignment="Top"
Height="13"
Width="55" />
<TextBox x:Name="uxFilter"
Text="{Binding Filter, UpdateSourceTrigger=PropertyChanged}"
Height="23"
Margin="12,31,12,0"
VerticalAlignment="Top" />
<sd:ClassListBox x:Name="uxList"
Margin="12,60,12,78"
ItemsSource="{Binding Classes}"
IsSynchronizedWithCurrentItem="True"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{sd:GetBitmap Icons.16x16.Class}"
Stretch="None" />
<TextBlock Margin="5 0 0 0"
Text="{Binding Converter={x:Static sd:ClassNameConverter.Instance}}"
VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</sd:ClassListBox>
<CheckBox Content="Show System Classes"
IsChecked="{Binding ShowSystemClasses}"
Height="16"
HorizontalAlignment="Left"
Margin="12,0,0,56"
VerticalAlignment="Bottom"
Width="120" />
<Button x:Name="uxOk"
Content="OK"
Height="23"
Margin="0,0,93,12"
VerticalAlignment="Bottom"
HorizontalAlignment="Right"
Width="75"
IsDefault="True"
IsEnabled="{Binding SelectedItem, ElementName=uxList, Converter={x:Static sd:NullToBoolConverter.Instance}}"/>
<Button Content="Cancel"
Height="23"
HorizontalAlignment="Right"
Margin="0,0,12,12"
VerticalAlignment="Bottom"
Width="75"
IsCancel="True" />
</Grid>
</Window>

114
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ChooseClassDialog.xaml.cs

@ -0,0 +1,114 @@ @@ -0,0 +1,114 @@
using System;
using System.Collections.Specialized;
using System.Globalization;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using ICSharpCode.SharpDevelop.Dom;
namespace ICSharpCode.WpfDesign.AddIn
{
public partial class ChooseClassDialog
{
public ChooseClassDialog(ChooseClass core)
{
DataContext = core;
InitializeComponent();
uxFilter.Focus();
uxList.MouseDoubleClick += uxList_MouseDoubleClick;
uxOk.Click += delegate { Ok(); };
AddHandler(Keyboard.GotKeyboardFocusEvent,
new KeyboardFocusChangedEventHandler(
(sender, e) => uxList.SetValue(IsSelectionActivePropertyKey, true)
),
true);
}
//HACK: listbox is always highlighted
public static DependencyPropertyKey IsSelectionActivePropertyKey =
(DependencyPropertyKey)typeof(Selector).GetField("IsSelectionActivePropertyKey",
BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Enter) {
Ok();
e.Handled = true;
} else if (e.Key == Key.Up) {
uxList.SelectedIndex = Math.Max(0, uxList.SelectedIndex - 1);
e.Handled = true;
} else if (e.Key == Key.Down) {
uxList.SelectedIndex++;
e.Handled = true;
}
}
void uxList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var f = e.OriginalSource as FrameworkElement;
if (f != null && f.DataContext is IClass) {
Ok();
}
}
void Ok()
{
DialogResult = true;
Close();
}
}
class ClassListBox : ListBox
{
protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
{
base.OnItemsChanged(e);
SelectedIndex = 0;
ScrollIntoView(SelectedItem);
}
protected override void OnSelectionChanged(SelectionChangedEventArgs e)
{
base.OnSelectionChanged(e);
ScrollIntoView(SelectedItem);
}
}
public class ClassNameConverter : IValueConverter
{
public static ClassNameConverter Instance = new ClassNameConverter();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var c = value as IClass;
if (c == null) return value;
return c.Name + " (" + c.Namespace + ")";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class NullToBoolConverter : IValueConverter
{
public static NullToBoolConverter Instance = new NullToBoolConverter();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value == null ? false : true;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

20
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ChooseClassService.cs

@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
using System;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Dom;
namespace ICSharpCode.WpfDesign.AddIn
{
public class ChooseClassService
{
public IClass ChooseClass()
{
var core = new ChooseClass(ParserService.CurrentProjectContent);
var window = new ChooseClassDialog(core);
if (window.ShowDialog().Value) {
return core.CurrentClass;
}
return null;
}
}
}

44
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/GetBitmapExtension.cs

@ -0,0 +1,44 @@ @@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Markup;
using System.Windows.Media.Imaging;
using ICSharpCode.Core;
namespace ICSharpCode.WpfDesign.AddIn
{
class GetBitmapExtension : MarkupExtension
{
public GetBitmapExtension(string key)
{
this.key = key;
}
static Dictionary<string, BitmapSource> cache = new Dictionary<string, BitmapSource>();
protected string key;
public override object ProvideValue(IServiceProvider sp)
{
BitmapSource result;
if (cache.TryGetValue(key, out result)) {
return result;
}
result = GetBitmapSource();
result.Freeze();
cache[key] = result;
return result;
}
BitmapSource GetBitmapSource()
{
using (Bitmap bitmap = ResourceService.GetBitmap(key)) {
return Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero,
Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
}
}
}

8
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ObjectEditor.xaml

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
<Button
x:Class="ICSharpCode.WpfDesign.AddIn.ObjectEditor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Content="New..."
HorizontalAlignment="Right"
>
</Button>

25
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ObjectEditor.xaml.cs

@ -0,0 +1,25 @@ @@ -0,0 +1,25 @@
using System;
using System.Windows;
using ICSharpCode.WpfDesign.PropertyEditor;
namespace ICSharpCode.WpfDesign.AddIn
{
[PropertyEditor(typeof(FrameworkElement), "DataContext")]
public partial class ObjectEditor
{
public ObjectEditor(IPropertyEditorDataProperty property)
{
InitializeComponent();
var s = property.OwnerDataSource.Services.GetService<ChooseClassService>();
if (s != null) {
Click += delegate {
var c = s.ChooseClass();
if (c != null) {
MessageBox.Show(c.Name);
}
};
}
}
}
}

2
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/WpfViewContent.cs

@ -55,12 +55,14 @@ namespace ICSharpCode.WpfDesign.AddIn @@ -55,12 +55,14 @@ namespace ICSharpCode.WpfDesign.AddIn
}
using (XmlTextReader r = new XmlTextReader(stream)) {
XamlLoadSettings settings = new XamlLoadSettings();
settings.DesignerAssemblies.Add(typeof(WpfViewContent).Assembly);
settings.CustomServiceRegisterFunctions.Add(
delegate(XamlDesignContext context) {
context.Services.AddService(typeof(IUriContext), new FileUriContext(this.PrimaryFile));
context.Services.AddService(typeof(IPropertyDescriptionService), new PropertyDescriptionService(this.PrimaryFile));
context.Services.AddService(typeof(IEventHandlerService), new CSharpEventHandlerService(this));
context.Services.AddService(typeof(ITopLevelWindowService), new WpfAndWinFormsTopLevelWindowService());
context.Services.AddService(typeof(ChooseClassService), new ChooseClassService());
});
settings.TypeFinder = MyTypeFinder.Create(this.PrimaryFile);

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

@ -38,6 +38,9 @@ @@ -38,6 +38,9 @@
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
@ -54,9 +57,14 @@ @@ -54,9 +57,14 @@
</Compile>
<Compile Include="Configuration\AssemblyInfo.cs" />
<Compile Include="Src\AbstractEventHandlerService.cs" />
<Compile Include="Src\ChooseClass.cs" />
<Compile Include="Src\ChooseClassDialog.xaml.cs" />
<Compile Include="Src\ChooseClassService.cs" />
<Compile Include="Src\CSharpEventHandlerService.cs" />
<Compile Include="Src\FileUriContext.cs" />
<Compile Include="Src\GetBitmapExtension.cs" />
<Compile Include="Src\MyTypeFinder.cs" />
<Compile Include="Src\ObjectEditor.xaml.cs" />
<Compile Include="Src\PropertyDescriptionService.cs" />
<Compile Include="Src\SharpDevelopElementHost.cs" />
<Compile Include="Src\WpfAndWinFormsTopLevelWindowService.cs" />
@ -111,5 +119,7 @@ @@ -111,5 +119,7 @@
<Name>WpfDesign</Name>
<Private>False</Private>
</ProjectReference>
<Page Include="Src\ChooseClassDialog.xaml" />
<Page Include="Src\ObjectEditor.xaml" />
</ItemGroup>
</Project>
Loading…
Cancel
Save