Browse Source

- Commands routing (for integrated and standalone WPF Designer, CCP still not implemented)

- Rework DesignSurface (more xaml, popular controls through metadata)

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3435 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Ivan Shumilin 18 years ago
parent
commit
945d491f87
  1. 1
      samples/XamlDesigner/MainWindow.xaml.cs
  2. 28
      samples/XamlDesigner/MainWindow_Commands.cs
  3. 33
      samples/XamlDesigner/Toolbox.cs
  4. 20
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/SharpDevelopElementHost.cs
  5. 2
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/WpfToolbox.cs
  6. 22
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/WpfViewContent.cs
  7. 32
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/BasicMetadata.cs
  8. 78
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/SingleVisualChildElement.cs
  9. 12
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/DesignSurface.xaml
  10. 283
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/DesignSurface.xaml.cs
  11. 21
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/ExtensionMethods.cs
  12. 2
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/PropertyGrid/PropertyGrid.cs
  13. 9
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/WpfDesign.Designer.csproj
  14. 1
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Xaml/XamlDesignContext.cs
  15. 49
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign/Project/Metadata.cs
  16. 2
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign/Project/PropertyGrid/PropertyNode.cs

1
samples/XamlDesigner/MainWindow.xaml.cs

@ -31,6 +31,7 @@ namespace ICSharpCode.XamlDesigner
Shell.Instance.PropertyGrid = uxPropertyGridView.PropertyGrid; Shell.Instance.PropertyGrid = uxPropertyGridView.PropertyGrid;
AvalonDockWorkaround(); AvalonDockWorkaround();
RouteDesignSurfaceCommands();
LoadSettings(); LoadSettings();
ProcessPaths(App.Args); ProcessPaths(App.Args);
} }

28
samples/XamlDesigner/MainWindow_Commands.cs

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Windows.Input; using System.Windows.Input;
using System.Windows;
namespace ICSharpCode.XamlDesigner namespace ICSharpCode.XamlDesigner
{ {
@ -67,5 +68,32 @@ namespace ICSharpCode.XamlDesigner
{ {
e.CanExecute = Shell.Instance.CurrentDocument != null; e.CanExecute = Shell.Instance.CurrentDocument != null;
} }
void RouteDesignSurfaceCommands()
{
RouteDesignSurfaceCommand(ApplicationCommands.Undo);
RouteDesignSurfaceCommand(ApplicationCommands.Redo);
RouteDesignSurfaceCommand(ApplicationCommands.Copy);
RouteDesignSurfaceCommand(ApplicationCommands.Cut);
RouteDesignSurfaceCommand(ApplicationCommands.Paste);
RouteDesignSurfaceCommand(ApplicationCommands.SelectAll);
RouteDesignSurfaceCommand(ApplicationCommands.Delete);
}
void RouteDesignSurfaceCommand(RoutedCommand command)
{
var cb = new CommandBinding(command);
cb.CanExecute += delegate(object sender, CanExecuteRoutedEventArgs e) {
if (Shell.Instance.CurrentDocument != null) {
Shell.Instance.CurrentDocument.DesignSurface.RaiseEvent(e);
}else {
e.CanExecute = false;
}
};
cb.Executed += delegate(object sender, ExecutedRoutedEventArgs e) {
Shell.Instance.CurrentDocument.DesignSurface.RaiseEvent(e);
};
CommandBindings.Add(cb);
}
} }
} }

33
samples/XamlDesigner/Toolbox.cs

@ -7,6 +7,7 @@ using System.Collections.ObjectModel;
using ICSharpCode.XamlDesigner.Configuration; using ICSharpCode.XamlDesigner.Configuration;
using System.Windows; using System.Windows;
using System.Collections.Specialized; using System.Collections.Specialized;
using ICSharpCode.WpfDesign;
namespace ICSharpCode.XamlDesigner namespace ICSharpCode.XamlDesigner
{ {
@ -26,36 +27,6 @@ namespace ICSharpCode.XamlDesigner
AddAssembly(path, true); AddAssembly(path, true);
} }
string[] popularControls = new string[] {
"Border",
"Button",
"Canvas",
"CheckBox",
"ComboBox",
"DockPanel",
"Ellipse",
"FlowDocumentScrollViewer",
"Grid",
"GridSplitter",
"Label",
"Line",
"ListBox",
"PasswordBox",
"RadioButton",
"Rectangle",
"RichTextBox",
"ScrollBar",
"ScrollViewer",
"Slider",
"StackPanel",
"TabControl",
"TextBlock",
"TextBox",
"UniformGrid",
"Viewbox",
"WrapPanel"
};
void AddAssembly(string path, bool updateSettings) void AddAssembly(string path, bool updateSettings)
{ {
var assembly = Assembly.LoadFile(path); var assembly = Assembly.LoadFile(path);
@ -64,7 +35,7 @@ namespace ICSharpCode.XamlDesigner
node.Assembly = assembly; node.Assembly = assembly;
node.Path = path; node.Path = path;
foreach (var t in assembly.GetExportedTypes()) { foreach (var t in assembly.GetExportedTypes()) {
if (IsControl(t) && popularControls.Contains(t.Name)) { if (IsControl(t) && Metadata.IsPopularControl(t)) {
node.Controls.Add(new ControlNode() { Type = t }); node.Controls.Add(new ControlNode() { Type = t });
} }
} }

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

@ -22,28 +22,38 @@ namespace ICSharpCode.WpfDesign.AddIn
[ThreadStatic] [ThreadStatic]
static bool registeredErrorHandler; static bool registeredErrorHandler;
public SharpDevelopElementHost() public SharpDevelopElementHost(WpfViewContent viewContent, UIElement child)
{ {
if (!registeredErrorHandler) { if (!registeredErrorHandler) {
registeredErrorHandler = true; registeredErrorHandler = true;
Dispatcher.CurrentDispatcher.UnhandledException += CurrentDispatcher_UnhandledException; Dispatcher.CurrentDispatcher.UnhandledException += CurrentDispatcher_UnhandledException;
} }
this.viewContent = viewContent;
this.Child = child;
} }
WpfViewContent viewContent;
static void CurrentDispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) static void CurrentDispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{ {
ICSharpCode.Core.MessageService.ShowError(e.Exception, "Unhandled WPF exception"); ICSharpCode.Core.MessageService.ShowError(e.Exception, "Unhandled WPF exception");
e.Handled = true; e.Handled = true;
} }
static bool IsEnabled(ICommand command) bool IsEnabled(RoutedCommand command)
{ {
return command.CanExecute(null); if (command.CanExecute(null, null)) return true;
return command.CanExecute(null, viewContent.DesignSurface);
} }
static void Run(ICommand command) void Run(RoutedCommand command)
{ {
command.Execute(null); if (command.CanExecute(null, null)) {
command.Execute(null, null);
} else {
command.Execute(null, viewContent.DesignSurface);
}
} }
public bool EnableUndo { public bool EnableUndo {

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

@ -45,7 +45,7 @@ namespace ICSharpCode.WpfDesign.AddIn
sideTab.ChoosedItemChanged += OnChoosedItemChanged; sideTab.ChoosedItemChanged += OnChoosedItemChanged;
sideTab.Items.Add(new WpfSideTabItem()); sideTab.Items.Add(new WpfSideTabItem());
foreach (Type t in Designer.DesignSurface.SupportedToolboxControls) foreach (Type t in Metadata.GetPopularControls())
sideTab.Items.Add(new WpfSideTabItem(t)); sideTab.Items.Add(new WpfSideTabItem(t));
sideBar.Tabs.Add(sideTab); sideBar.Tabs.Add(sideTab);

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

@ -29,17 +29,21 @@ namespace ICSharpCode.WpfDesign.AddIn
/// </summary> /// </summary>
public class WpfViewContent : AbstractViewContentHandlingLoadErrors, IHasPropertyContainer, IToolsHost public class WpfViewContent : AbstractViewContentHandlingLoadErrors, IHasPropertyContainer, IToolsHost
{ {
public WpfViewContent(OpenedFile file) : base(file)
{
this.TabPageText = "${res:FormsDesigner.DesignTabPages.DesignTabPage}";
this.IsActiveViewContentChanged += OnIsActiveViewContentChanged;
}
ElementHost wpfHost; ElementHost wpfHost;
DesignSurface designer; DesignSurface designer;
public DesignContext DesignContext { public DesignSurface DesignSurface {
get { return designer.DesignContext; } get { return designer; }
} }
public WpfViewContent(OpenedFile file) : base(file) public DesignContext DesignContext {
{ get { return designer.DesignContext; }
this.TabPageText = "${res:FormsDesigner.DesignTabPages.DesignTabPage}";
this.IsActiveViewContentChanged += OnIsActiveViewContentChanged;
} }
protected override void LoadInternal(OpenedFile file, System.IO.Stream stream) protected override void LoadInternal(OpenedFile file, System.IO.Stream stream)
@ -48,9 +52,8 @@ namespace ICSharpCode.WpfDesign.AddIn
if (designer == null) { if (designer == null) {
// initialize designer on first load // initialize designer on first load
DragDropExceptionHandler.HandleException = ICSharpCode.Core.MessageService.ShowError; DragDropExceptionHandler.HandleException = ICSharpCode.Core.MessageService.ShowError;
wpfHost = new SharpDevelopElementHost();
designer = new DesignSurface(); designer = new DesignSurface();
wpfHost.Child = designer; wpfHost = new SharpDevelopElementHost(this, designer);
this.UserControl = wpfHost; this.UserControl = wpfHost;
InitPropertyEditor(); InitPropertyEditor();
} }
@ -99,9 +102,8 @@ namespace ICSharpCode.WpfDesign.AddIn
void InitPropertyEditor() void InitPropertyEditor()
{ {
propertyEditorHost = new SharpDevelopElementHost();
propertyGridView = new PropertyGridView(); propertyGridView = new PropertyGridView();
propertyEditorHost.Child = propertyGridView; propertyEditorHost = new SharpDevelopElementHost(this, propertyGridView);
propertyContainer.PropertyGridReplacementControl = propertyEditorHost; propertyContainer.PropertyGridReplacementControl = propertyEditorHost;
} }

32
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/BasicMetadata.cs

@ -19,7 +19,7 @@ using System.Windows.Navigation;
namespace ICSharpCode.WpfDesign.Designer namespace ICSharpCode.WpfDesign.Designer
{ {
public class BasicMetadata public class BasicMetadata : IMetadata
{ {
public BasicMetadata() public BasicMetadata()
{ {
@ -190,6 +190,36 @@ namespace ICSharpCode.WpfDesign.Designer
Metadata.HideProperty(typeof(Window), "Owner"); Metadata.HideProperty(typeof(Window), "Owner");
//Metadata.DisablePlacement(typeof(Button)); //Metadata.DisablePlacement(typeof(Button));
Metadata.AddPopularControl(typeof(Button));
Metadata.AddPopularControl(typeof(CheckBox));
Metadata.AddPopularControl(typeof(ComboBox));
Metadata.AddPopularControl(typeof(Label));
Metadata.AddPopularControl(typeof(TextBox));
Metadata.AddPopularControl(typeof(RadioButton));
Metadata.AddPopularControl(typeof(Canvas));
Metadata.AddPopularControl(typeof(Grid));
Metadata.AddPopularControl(typeof(Border));
Metadata.AddPopularControl(typeof(DockPanel));
Metadata.AddPopularControl(typeof(Expander));
Metadata.AddPopularControl(typeof(GroupBox));
Metadata.AddPopularControl(typeof(Image));
Metadata.AddPopularControl(typeof(InkCanvas));
Metadata.AddPopularControl(typeof(ListBox));
Metadata.AddPopularControl(typeof(ListView));
Metadata.AddPopularControl(typeof(Menu));
Metadata.AddPopularControl(typeof(PasswordBox));
Metadata.AddPopularControl(typeof(ProgressBar));
Metadata.AddPopularControl(typeof(RichTextBox));
Metadata.AddPopularControl(typeof(ScrollViewer));
Metadata.AddPopularControl(typeof(Slider));
Metadata.AddPopularControl(typeof(StackPanel));
Metadata.AddPopularControl(typeof(TabControl));
Metadata.AddPopularControl(typeof(ToolBar));
Metadata.AddPopularControl(typeof(TreeView));
Metadata.AddPopularControl(typeof(Viewbox));
Metadata.AddPopularControl(typeof(Viewport3D));
Metadata.AddPopularControl(typeof(WrapPanel));
} }
} }
} }

78
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Controls/SingleVisualChildElement.cs

@ -1,78 +0,0 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Windows;
using System.Windows.Media;
namespace ICSharpCode.WpfDesign.Designer.Controls
{
/// <summary>
/// Base class for elements that have a single visual child.
/// </summary>
public class SingleVisualChildElement : FrameworkElement
{
UIElement _visualChild;
/// <summary>
/// Gets/sets the visual child.
/// </summary>
protected UIElement VisualChild {
get { return _visualChild; }
set {
RemoveVisualChild(_visualChild);
_visualChild = value;
AddVisualChild(_visualChild);
InvalidateMeasure();
}
}
/// <summary>
/// Gets the visual child.
/// </summary>
protected override Visual GetVisualChild(int index)
{
if (index == 0 && _visualChild != null)
return _visualChild;
else
throw new ArgumentOutOfRangeException("index");
}
/// <summary>
/// Gets the number of visual children.
/// </summary>
protected override int VisualChildrenCount {
get { return _visualChild != null ? 1 : 0; }
}
/// <summary>
/// Measure the visual child.
/// </summary>
protected override Size MeasureOverride(Size availableSize)
{
if (_visualChild != null) {
_visualChild.Measure(availableSize);
return _visualChild.DesiredSize;
} else {
return base.MeasureOverride(availableSize);
}
}
/// <summary>
/// Arrange the visual child.
/// </summary>
protected override Size ArrangeOverride(Size finalSize)
{
if (_visualChild != null) {
_visualChild.Arrange(new Rect(new Point(0, 0), finalSize));
return finalSize;
} else {
return base.ArrangeOverride(finalSize);
}
}
}
}

12
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/DesignSurface.xaml

@ -0,0 +1,12 @@
<UserControl x:Class="ICSharpCode.WpfDesign.Designer.DesignSurface"
xmlns="http://schemas.microsoft.com/netfx/2007/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Default="clr-namespace:ICSharpCode.WpfDesign.Designer"
DataContext="{x:Null}">
<ScrollViewer HorizontalScrollBarVisibility="Visible"
VerticalScrollBarVisibility="Visible">
<Default:DesignPanel x:Name="_designPanel">
<Border x:Name="_sceneContainer" Padding="10" />
</Default:DesignPanel>
</ScrollViewer>
</UserControl>

283
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/DesignSurface.cs → src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/DesignSurface.xaml.cs

@ -1,94 +1,111 @@
// <file> using System;
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Text;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml; using System.Xml;
using ICSharpCode.WpfDesign.Designer.Xaml;
using ICSharpCode.WpfDesign.Designer.Controls;
using ICSharpCode.WpfDesign.Designer.Services; using ICSharpCode.WpfDesign.Designer.Services;
using System.Diagnostics;
namespace ICSharpCode.WpfDesign.Designer namespace ICSharpCode.WpfDesign.Designer
{ {
/// <summary> /// <summary>
/// Surface hosting the WPF designer. /// Surface hosting the WPF designer.
/// </summary> /// </summary>
public sealed class DesignSurface : SingleVisualChildElement public partial class DesignSurface
{ {
public static Type[] SupportedToolboxControls = { public DesignSurface()
typeof(Button), {
typeof(CheckBox), InitializeComponent();
typeof(ComboBox),
typeof(Label), this.AddCommandHandler(ApplicationCommands.Undo, Undo, CanUndo);
typeof(TextBox), this.AddCommandHandler(ApplicationCommands.Redo, Redo, CanRedo);
typeof(RadioButton), this.AddCommandHandler(ApplicationCommands.Copy, Copy, HasSelection);
typeof(Canvas), this.AddCommandHandler(ApplicationCommands.Cut, Cut, HasSelection);
typeof(Grid), this.AddCommandHandler(ApplicationCommands.Delete, Delete, CanDelete);
typeof(Border), this.AddCommandHandler(ApplicationCommands.Paste, Paste, CanPaste);
typeof(DockPanel), this.AddCommandHandler(ApplicationCommands.SelectAll, SelectAll, CanSelectAll);
typeof(Expander), }
typeof(GroupBox),
typeof(Image),
typeof(InkCanvas),
typeof(ListBox),
typeof(ListView),
typeof(Menu),
typeof(PasswordBox),
typeof(ProgressBar),
typeof(RichTextBox),
typeof(ScrollViewer),
typeof(Slider),
typeof(StackPanel),
typeof(TabControl),
typeof(ToolBar),
typeof(TreeView),
typeof(Viewbox),
typeof(Viewport3D),
typeof(WrapPanel)
};
readonly ScrollViewer _scrollViewer;
readonly DesignPanel _designPanel;
DesignContext _designContext; DesignContext _designContext;
/// <summary> /// <summary>
/// Create a new DesignSurface instance. /// Gets the active design context.
/// </summary> /// </summary>
public DesignSurface() public DesignContext DesignContext {
get { return _designContext; }
}
/// <summary>
/// Initializes the designer content from the specified XmlReader.
/// </summary>
public void LoadDesigner(XmlReader xamlReader, XamlLoadSettings loadSettings)
{
UnloadDesigner();
InitializeDesigner(new XamlDesignContext(xamlReader, loadSettings ?? new XamlLoadSettings()));
}
/// <summary>
/// Saves the designer content into the specified XmlWriter.
/// </summary>
public void SaveDesigner(XmlWriter writer)
{ {
_scrollViewer = new ScrollViewer(); _designContext.Save(writer);
_designPanel = new DesignPanel(); }
_scrollViewer.Content = _designPanel;
_scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
_scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
this.VisualChild = _scrollViewer; void InitializeDesigner(DesignContext context)
this.DataContext = null; {
context.Services.AddService(typeof(IDesignPanel), _designPanel);
_designContext = context;
_designPanel.Context = context;
_sceneContainer.Child = context.RootItem.View;
this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Undo, OnUndoExecuted, OnUndoCanExecute)); context.Services.RunWhenAvailable<UndoService>(
this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Redo, OnRedoExecuted, OnRedoCanExecute)); undoService => undoService.UndoStackChanged += delegate {
this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Delete, OnDeleteExecuted, OnDeleteCanExecute)); CommandManager.InvalidateRequerySuggested();
}
);
context.Services.Selection.SelectionChanged += delegate {
CommandManager.InvalidateRequerySuggested();
};
} }
T GetService<T>() where T : class /// <summary>
/// Unloads the designer content.
/// </summary>
public void UnloadDesigner()
{ {
if (_designContext != null) if (_designContext != null) {
return _designContext.Services.GetService<T>(); foreach (object o in _designContext.Services.AllServices) {
else IDisposable d = o as IDisposable;
return null; if (d != null) d.Dispose();
}
}
_designContext = null;
_designPanel.Context = null;
_sceneContainer.Child = null;
_designPanel.Adorners.Clear();
} }
#region Command: Undo/Redo #region Commands
void OnUndoExecuted(object sender, ExecutedRoutedEventArgs e)
public bool CanUndo()
{
UndoService undoService = GetService<UndoService>();
return undoService != null && undoService.CanUndo;
}
public void Undo()
{ {
UndoService undoService = GetService<UndoService>(); UndoService undoService = GetService<UndoService>();
IUndoAction action = undoService.UndoActions.First(); IUndoAction action = undoService.UndoActions.First();
@ -97,13 +114,13 @@ namespace ICSharpCode.WpfDesign.Designer
_designContext.Services.Selection.SetSelectedComponents(GetLiveElements(action.AffectedElements)); _designContext.Services.Selection.SetSelectedComponents(GetLiveElements(action.AffectedElements));
} }
void OnUndoCanExecute(object sender, CanExecuteRoutedEventArgs e) public bool CanRedo()
{ {
UndoService undoService = GetService<UndoService>(); UndoService undoService = GetService<UndoService>();
e.CanExecute = undoService != null && undoService.CanUndo; return undoService != null && undoService.CanRedo;
} }
void OnRedoExecuted(object sender, ExecutedRoutedEventArgs e) public void Redo()
{ {
UndoService undoService = GetService<UndoService>(); UndoService undoService = GetService<UndoService>();
IUndoAction action = undoService.RedoActions.First(); IUndoAction action = undoService.RedoActions.First();
@ -112,103 +129,105 @@ namespace ICSharpCode.WpfDesign.Designer
_designContext.Services.Selection.SetSelectedComponents(GetLiveElements(action.AffectedElements)); _designContext.Services.Selection.SetSelectedComponents(GetLiveElements(action.AffectedElements));
} }
void OnRedoCanExecute(object sender, CanExecuteRoutedEventArgs e) public bool HasSelection()
{ {
UndoService undoService = GetService<UndoService>(); return false;
e.CanExecute = undoService != null && undoService.CanRedo;
} }
// Filters an element list, dropping all elements that are not part of the xaml document public void Copy()
// (e.g. because they were deleted).
static List<DesignItem> GetLiveElements(ICollection<DesignItem> items)
{ {
List<DesignItem> result = new List<DesignItem>(items.Count);
foreach (DesignItem item in items) {
if (ModelTools.IsInDocument(item) && ModelTools.CanSelectComponent(item)) {
result.Add(item);
} }
public void Cut()
{
} }
return result;
}
#endregion
#region Command: Delete public bool CanDelete()
void OnDeleteExecuted(object sender, ExecutedRoutedEventArgs e)
{ {
if (_designContext != null) { if (_designContext != null) {
ModelTools.DeleteComponents(_designContext.Services.Selection.SelectedItems); return ModelTools.CanDeleteComponents(_designContext.Services.Selection.SelectedItems);
} }
return false;
} }
void OnDeleteCanExecute(object sender, CanExecuteRoutedEventArgs e) public void Delete()
{ {
if (_designContext != null) { if (_designContext != null) {
e.CanExecute = ModelTools.CanDeleteComponents(_designContext.Services.Selection.SelectedItems); ModelTools.DeleteComponents(_designContext.Services.Selection.SelectedItems);
} else {
e.CanExecute = false;
} }
} }
#endregion
/// <summary> public bool CanPaste()
/// Gets the active design context. {
/// </summary> return false;
public DesignContext DesignContext {
get { return _designContext; }
} }
/// <summary> public void Paste()
/// Initializes the designer content from the specified XmlReader.
/// </summary>
public void LoadDesigner(XmlReader xamlReader, Xaml.XamlLoadSettings loadSettings)
{ {
UnloadDesigner();
InitializeDesigner(new Xaml.XamlDesignContext(xamlReader, loadSettings ?? new Xaml.XamlLoadSettings()));
} }
/// <summary> public bool CanSelectAll()
/// Saves the designer content into the specified XmlWriter.
/// </summary>
public void SaveDesigner(XmlWriter writer)
{ {
_designContext.Save(writer); return DesignContext != null;
} }
void InitializeDesigner(DesignContext context) //TODO: Do not select layout root
public void SelectAll()
{ {
context.Services.AddService(typeof(IDesignPanel), _designPanel); var items = Descendants(DesignContext.RootItem).Where(item => ModelTools.CanSelectComponent(item)).ToArray();
DesignContext.Services.Selection.SetSelectedComponents(items);
}
_designContext = context; //TODO: Share with Outline / PlacementBehavior
_designPanel.Context = context; public static IEnumerable<DesignItem> DescendantsAndSelf(DesignItem item)
Border designPanelBorder = new Border(); {
designPanelBorder.Padding = new Thickness(10); yield return item;
_designPanel.Child = designPanelBorder; foreach (var child in Descendants(item)) {
designPanelBorder.Child = context.RootItem.View; yield return child;
context.Services.RunWhenAvailable<UndoService>(
undoService => undoService.UndoStackChanged += delegate {
CommandManager.InvalidateRequerySuggested();
} }
);
context.Services.Selection.SelectionChanged += delegate {
CommandManager.InvalidateRequerySuggested();
};
} }
/// <summary> public static IEnumerable<DesignItem> Descendants(DesignItem item)
/// Unloads the designer content.
/// </summary>
public void UnloadDesigner()
{ {
if (_designContext != null) { if (item.ContentPropertyName != null) {
foreach (object o in _designContext.Services.AllServices) { var content = item.ContentProperty;
IDisposable d = o as IDisposable; if (content.IsCollection) {
if (d != null) d.Dispose(); foreach (var child in content.CollectionElements) {
foreach (var child2 in DescendantsAndSelf(child)) {
yield return child2;
} }
} }
_designContext = null; } else {
_designPanel.Context = null; if (content.Value != null) {
_designPanel.Child = null; foreach (var child2 in DescendantsAndSelf(content.Value)) {
_designPanel.Adorners.Clear(); yield return child2;
} }
} }
} }
}
}
// Filters an element list, dropping all elements that are not part of the xaml document
// (e.g. because they were deleted).
static List<DesignItem> GetLiveElements(ICollection<DesignItem> items)
{
List<DesignItem> result = new List<DesignItem>(items.Count);
foreach (DesignItem item in items) {
if (ModelTools.IsInDocument(item) && ModelTools.CanSelectComponent(item)) {
result.Add(item);
}
}
return result;
}
T GetService<T>() where T : class
{
if (_designContext != null)
return _designContext.Services.GetService<T>();
else
return null;
}
#endregion
}
}

21
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/ExtensionMethods.cs

@ -6,6 +6,7 @@ using System.Reflection;
using System.Collections; using System.Collections;
using System.Windows; using System.Windows;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Input;
namespace ICSharpCode.WpfDesign.Designer namespace ICSharpCode.WpfDesign.Designer
{ {
@ -52,5 +53,25 @@ namespace ICSharpCode.WpfDesign.Designer
} }
return null; return null;
} }
public static void AddCommandHandler(this UIElement element, ICommand command, Action execute)
{
AddCommandHandler(element, command, execute, null);
}
public static void AddCommandHandler(this UIElement element, ICommand command, Action execute, Func<bool> canExecute)
{
var cb = new CommandBinding(command);
if (canExecute != null) {
cb.CanExecute += delegate(object sender, CanExecuteRoutedEventArgs e) {
e.CanExecute = canExecute();
e.Handled = true;
};
}
cb.Executed += delegate(object sender, ExecutedRoutedEventArgs e) {
execute();
};
element.CommandBindings.Add(cb);
}
} }
} }

2
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/PropertyGrid/PropertyGrid.cs

@ -180,7 +180,7 @@ namespace ICSharpCode.WpfDesign.Designer.PropertyGrid
Category PickCategory(PropertyNode node) Category PickCategory(PropertyNode node)
{ {
if (Metadata.IsPopular(node.FirstProperty)) return popularCategory; if (Metadata.IsPopularProperty(node.FirstProperty)) return popularCategory;
var typeName = node.FirstProperty.DeclaringType.FullName; var typeName = node.FirstProperty.DeclaringType.FullName;
if (typeName.StartsWith("System.Windows.") || typeName.StartsWith("ICSharpCode.WpfDesign.Designer.Controls.")) if (typeName.StartsWith("System.Windows.") || typeName.StartsWith("ICSharpCode.WpfDesign.Designer.Controls."))
return otherCategory; return otherCategory;

9
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/WpfDesign.Designer.csproj

@ -83,12 +83,14 @@
<Compile Include="Controls\SelectionFrame.cs" /> <Compile Include="Controls\SelectionFrame.cs" />
<Compile Include="Controls\ErrorBalloon.cs" /> <Compile Include="Controls\ErrorBalloon.cs" />
<Compile Include="Controls\ResizeThumb.cs" /> <Compile Include="Controls\ResizeThumb.cs" />
<Compile Include="Controls\SingleVisualChildElement.cs" />
<Compile Include="CallExtension.cs" /> <Compile Include="CallExtension.cs" />
<Compile Include="Controls\DragListener.cs" /> <Compile Include="Controls\DragListener.cs" />
<Compile Include="Controls\WindowClone.cs" /> <Compile Include="Controls\WindowClone.cs" />
<Compile Include="Converters.cs" /> <Compile Include="Converters.cs" />
<Compile Include="DesignPanel.cs" /> <Compile Include="DesignPanel.cs" />
<Compile Include="DesignSurface.xaml.cs">
<DependentUpon>DesignSurface.xaml</DependentUpon>
</Compile>
<Compile Include="DragDropExceptionHandler.cs" /> <Compile Include="DragDropExceptionHandler.cs" />
<Compile Include="ExtensionMethods.cs" /> <Compile Include="ExtensionMethods.cs" />
<Compile Include="Extensions\CanvasPlacementSupport.cs"> <Compile Include="Extensions\CanvasPlacementSupport.cs">
@ -173,7 +175,6 @@
<Compile Include="Services\ToolService.cs" /> <Compile Include="Services\ToolService.cs" />
<Compile Include="Services\UndoService.cs" /> <Compile Include="Services\UndoService.cs" />
<Compile Include="Services\ViewService.cs" /> <Compile Include="Services\ViewService.cs" />
<Compile Include="DesignSurface.cs" />
<Compile Include="Services\WpfTopLevelWindowService.cs" /> <Compile Include="Services\WpfTopLevelWindowService.cs" />
<Compile Include="SharedInstances.cs" /> <Compile Include="SharedInstances.cs" />
<Compile Include="Xaml\XamlLoadSettings.cs" /> <Compile Include="Xaml\XamlLoadSettings.cs" />
@ -205,6 +206,10 @@
<SubType>Designer</SubType> <SubType>Designer</SubType>
</Page> </Page>
<Page Include="Controls\ControlStyles.xaml" /> <Page Include="Controls\ControlStyles.xaml" />
<Page Include="DesignSurface.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="PropertyGrid\Editors\BoolEditor.xaml"> <Page Include="PropertyGrid\Editors\BoolEditor.xaml">
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>

1
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Xaml/XamlDesignContext.cs

@ -27,7 +27,6 @@ namespace ICSharpCode.WpfDesign.Designer.Xaml
static XamlDesignContext() static XamlDesignContext()
{ {
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
new BasicMetadata();
} }
readonly XamlDocument _doc; readonly XamlDocument _doc;

49
src/AddIns/DisplayBindings/WpfDesign/WpfDesign/Project/Metadata.cs

@ -6,11 +6,29 @@ using System.Reflection;
using System.Collections; using System.Collections;
using System.Windows; using System.Windows;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics;
namespace ICSharpCode.WpfDesign namespace ICSharpCode.WpfDesign
{ {
public static class Metadata public static class Metadata
{ {
//TODO: Another way?
static Metadata()
{
foreach (var a in AppDomain.CurrentDomain.GetAssemblies()) {
RegisterAssembly(a);
}
}
public static void RegisterAssembly(Assembly a)
{
foreach (var t in a.GetExportedTypes()) {
if (t.GetInterface("IMetadata") == typeof(IMetadata)) {
Activator.CreateInstance(t);
}
}
}
public static string GetFullName(this DependencyProperty p) public static string GetFullName(this DependencyProperty p)
{ {
return p.OwnerType.FullName + "." + p.Name; return p.OwnerType.FullName + "." + p.Name;
@ -150,7 +168,7 @@ namespace ICSharpCode.WpfDesign
} }
} }
public static bool IsPopular(DesignItemProperty p) public static bool IsPopularProperty(DesignItemProperty p)
{ {
lock (popularProperties) { lock (popularProperties) {
if (popularProperties.Contains(p.DependencyFullName)) { if (popularProperties.Contains(p.DependencyFullName)) {
@ -160,6 +178,31 @@ namespace ICSharpCode.WpfDesign
return false; return false;
} }
static HashSet<Type> popularControls = new HashSet<Type>();
public static void AddPopularControl(Type t)
{
lock (popularControls) {
popularControls.Add(t);
}
}
public static IEnumerable<Type> GetPopularControls()
{
lock (popularControls) {
foreach (var t in popularControls) {
yield return t;
}
}
}
public static bool IsPopularControl(Type t)
{
lock (popularControls) {
return popularControls.Contains(t);
}
}
static Dictionary<string, NumberRange> ranges = new Dictionary<string, NumberRange>(); static Dictionary<string, NumberRange> ranges = new Dictionary<string, NumberRange>();
public static void AddValueRange(DependencyProperty p, double min, double max) public static void AddValueRange(DependencyProperty p, double min, double max)
@ -197,6 +240,10 @@ namespace ICSharpCode.WpfDesign
} }
} }
public interface IMetadata
{
}
public class NumberRange public class NumberRange
{ {
public double Min; public double Min;

2
src/AddIns/DisplayBindings/WpfDesign/WpfDesign/Project/PropertyGrid/PropertyNode.cs

@ -275,7 +275,7 @@ namespace ICSharpCode.WpfDesign.PropertyGrid
foreach (var node in list) { foreach (var node in list) {
if (Metadata.IsBrowsable(node.FirstProperty)) { if (Metadata.IsBrowsable(node.FirstProperty)) {
node.IsVisible = true; node.IsVisible = true;
if (Metadata.IsPopular(node.FirstProperty)) { if (Metadata.IsPopularProperty(node.FirstProperty)) {
Children.Add(node); Children.Add(node);
} else { } else {
MoreChildren.Add(node); MoreChildren.Add(node);

Loading…
Cancel
Save