Browse Source
- Fix a bug which caused NullReferenceException in hiding MarginHandles in case of a panel other than Grid. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/wpfdesigner@6104 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61pull/1/head
6 changed files with 286 additions and 10 deletions
@ -0,0 +1,133 @@
@@ -0,0 +1,133 @@
|
||||
// <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.Windows; |
||||
using System.Windows.Controls; |
||||
using System.Windows.Input; |
||||
|
||||
namespace ICSharpCode.WpfDesign.Designer.Controls |
||||
{ |
||||
/// <summary>
|
||||
/// A Small icon which shows up a menu containing common properties
|
||||
/// </summary>
|
||||
public class QuickOperationMenu : Control |
||||
{ |
||||
static QuickOperationMenu() |
||||
{ |
||||
DefaultStyleKeyProperty.OverrideMetadata(typeof (QuickOperationMenu), new FrameworkPropertyMetadata(typeof (QuickOperationMenu))); |
||||
} |
||||
|
||||
private MenuItem _mainHeader; |
||||
|
||||
/// <summary>
|
||||
/// Contains Default values in the Sub menu for example "HorizontalAlignment" has "HorizontalAlignment.Stretch" as it's value.
|
||||
/// </summary>
|
||||
private readonly Dictionary<MenuItem, MenuItem> _defaults = new Dictionary<MenuItem, MenuItem>(); |
||||
|
||||
/// <summary>
|
||||
/// Is the main header menu which brings up all the menus.
|
||||
/// </summary>
|
||||
public MenuItem MainHeader { |
||||
get { return _mainHeader; } |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Add a submenu with checkable values.
|
||||
/// </summary>
|
||||
/// <param name="parent">The parent menu under which to add.</param>
|
||||
/// <param name="enumValues">All the values of an enum to be showed in the menu</param>
|
||||
/// <param name="defaultValue">The default value out of all the enums.</param>
|
||||
/// <param name="setValue">The presently set value out of the enums</param>
|
||||
public void AddSubMenuCheckable(MenuItem parent, Array enumValues, string defaultValue, string setValue) |
||||
{ |
||||
foreach (var enumValue in enumValues) { |
||||
var menuItem = new MenuItem {Header = enumValue.ToString(), IsCheckable = true}; |
||||
parent.Items.Add(menuItem); |
||||
if (enumValue.ToString() == defaultValue) |
||||
_defaults.Add(parent, menuItem); |
||||
if (enumValue.ToString() == setValue) |
||||
menuItem.IsChecked = true; |
||||
} |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Add a menu in the main header.
|
||||
/// </summary>
|
||||
/// <param name="menuItem">The menu to add.</param>
|
||||
public void AddSubMenuInTheHeader(MenuItem menuItem) |
||||
{ |
||||
if (_mainHeader != null) |
||||
_mainHeader.Items.Add(menuItem); |
||||
} |
||||
|
||||
public override void OnApplyTemplate() |
||||
{ |
||||
base.OnApplyTemplate(); |
||||
var mainHeader = Template.FindName("MainHeader", this) as MenuItem; |
||||
if (mainHeader != null) { |
||||
_mainHeader = mainHeader; |
||||
_mainHeader.MouseLeave += OnMouseOut; |
||||
} |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Checks a menu item and making it exclusive. If the check was toggled then the default menu item is selected.
|
||||
/// </summary>
|
||||
/// <param name="parent">The parent item of the sub menu</param>
|
||||
/// <param name="clickedOn">The Item clicked on</param>
|
||||
/// <returns>Returns the Default value if the checkable menu item is toggled or otherwise the new checked menu item.</returns>
|
||||
public string UncheckChildrenAndSelectClicked(MenuItem parent, MenuItem clickedOn) |
||||
{ |
||||
MenuItem defaultMenuItem; |
||||
_defaults.TryGetValue(parent, out defaultMenuItem); |
||||
if (IsAnyItemChecked(parent)) { |
||||
foreach (var item in parent.Items) { |
||||
var menuItem = item as MenuItem; |
||||
if (menuItem != null) menuItem.IsChecked = false; |
||||
} |
||||
clickedOn.IsChecked = true; |
||||
return (string) clickedOn.Header; |
||||
} else { |
||||
if (defaultMenuItem != null) { |
||||
defaultMenuItem.IsChecked = true; |
||||
return (string) defaultMenuItem.Header; |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Checks in the sub-menu whether aby items has been checked or not
|
||||
/// </summary>
|
||||
/// <param name="parent"></param>
|
||||
/// <returns></returns>
|
||||
private bool IsAnyItemChecked(MenuItem parent) |
||||
{ |
||||
bool check = false; |
||||
if (parent.HasItems) { |
||||
foreach (var item in parent.Items) { |
||||
var menuItem = item as MenuItem; |
||||
if (menuItem != null && menuItem.IsChecked) |
||||
check = true; |
||||
} |
||||
} |
||||
return check; |
||||
} |
||||
|
||||
protected override void OnMouseEnter(MouseEventArgs e) |
||||
{ |
||||
base.OnMouseEnter(e); |
||||
MainHeader.IsSubmenuOpen = true; |
||||
} |
||||
|
||||
private void OnMouseOut(object sender, MouseEventArgs e) |
||||
{ |
||||
MainHeader.IsSubmenuOpen = false; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,111 @@
@@ -0,0 +1,111 @@
|
||||
// <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.Windows; |
||||
using System.Windows.Controls; |
||||
|
||||
using ICSharpCode.WpfDesign.Designer.Controls; |
||||
using ICSharpCode.WpfDesign.Extensions; |
||||
using ICSharpCode.WpfDesign.Adorners; |
||||
|
||||
namespace ICSharpCode.WpfDesign.Designer.Extensions |
||||
{ |
||||
/// <summary>
|
||||
/// Extends the Quick operation menu for the designer.
|
||||
/// </summary>
|
||||
[ExtensionFor(typeof (FrameworkElement))] |
||||
class QuickOperationMenuExtension : PrimarySelectionAdornerProvider |
||||
{ |
||||
private QuickOperationMenu _menu; |
||||
|
||||
protected override void OnInitialized() |
||||
{ |
||||
base.OnInitialized(); |
||||
_menu = new QuickOperationMenu(); |
||||
_menu.Loaded += OnMenuLoaded; |
||||
var placement = new RelativePlacement(HorizontalAlignment.Right, VerticalAlignment.Top) {XOffset = 7}; |
||||
this.AddAdorners(placement, _menu); |
||||
} |
||||
|
||||
private void OnMenuLoaded(object sender, EventArgs e) |
||||
{ |
||||
_menu.MainHeader.Click += MainHeaderClick; |
||||
int menuItemsAdded = 0; |
||||
var view = this.ExtendedItem.View; |
||||
|
||||
if (view != null) { |
||||
string setValue; |
||||
if (view is StackPanel) { |
||||
var ch = new MenuItem() {Header = "Change Orientation"}; |
||||
_menu.AddSubMenuInTheHeader(ch); |
||||
setValue = this.ExtendedItem.Properties[StackPanel.OrientationProperty].ValueOnInstance.ToString(); |
||||
_menu.AddSubMenuCheckable(ch, Enum.GetValues(typeof (Orientation)), Orientation.Vertical.ToString(), setValue); |
||||
_menu.MainHeader.Items.Add(new Separator()); |
||||
menuItemsAdded++; |
||||
} |
||||
|
||||
var ha = new MenuItem() {Header = "Horizontal Alignment"}; |
||||
_menu.AddSubMenuInTheHeader(ha); |
||||
setValue = this.ExtendedItem.Properties[FrameworkElement.HorizontalAlignmentProperty].ValueOnInstance.ToString(); |
||||
_menu.AddSubMenuCheckable(ha, Enum.GetValues(typeof (HorizontalAlignment)), HorizontalAlignment.Stretch.ToString(), setValue); |
||||
menuItemsAdded++; |
||||
|
||||
var va = new MenuItem() {Header = "Vertical Alignment"}; |
||||
_menu.AddSubMenuInTheHeader(va); |
||||
setValue = this.ExtendedItem.Properties[FrameworkElement.VerticalAlignmentProperty].ValueOnInstance.ToString(); |
||||
_menu.AddSubMenuCheckable(va, Enum.GetValues(typeof (VerticalAlignment)), VerticalAlignment.Stretch.ToString(), setValue); |
||||
menuItemsAdded++; |
||||
} |
||||
|
||||
if (menuItemsAdded == 0) { |
||||
OnRemove(); |
||||
} |
||||
} |
||||
|
||||
private void MainHeaderClick(object sender, RoutedEventArgs e) |
||||
{ |
||||
var clickedOn = e.Source as MenuItem; |
||||
if (clickedOn != null) { |
||||
var parent = clickedOn.Parent as MenuItem; |
||||
if (parent != null) { |
||||
if ((string) parent.Header == "Change Orientation") { |
||||
var value = _menu.UncheckChildrenAndSelectClicked(parent, clickedOn); |
||||
if (value != null) { |
||||
var orientation = Enum.Parse(typeof (Orientation), value); |
||||
if (orientation != null) |
||||
this.ExtendedItem.Properties[StackPanel.OrientationProperty].SetValue(orientation); |
||||
} |
||||
} |
||||
|
||||
if ((string) parent.Header == "Horizontal Alignment") { |
||||
var value = _menu.UncheckChildrenAndSelectClicked(parent, clickedOn); |
||||
if (value != null) { |
||||
var ha = Enum.Parse(typeof (HorizontalAlignment), value); |
||||
if (ha != null) |
||||
this.ExtendedItem.Properties[FrameworkElement.HorizontalAlignmentProperty].SetValue(ha); |
||||
} |
||||
} |
||||
|
||||
if ((string) parent.Header == "Vertical Alignment") { |
||||
var value = _menu.UncheckChildrenAndSelectClicked(parent, clickedOn); |
||||
if (value != null) { |
||||
var va = Enum.Parse(typeof (VerticalAlignment), value); |
||||
if (va != null) |
||||
this.ExtendedItem.Properties[FrameworkElement.VerticalAlignmentProperty].SetValue(va); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
protected override void OnRemove() |
||||
{ |
||||
base.OnRemove(); |
||||
_menu.Loaded -= OnMenuLoaded; |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue