예제 #1
0
        /// <summary>
        /// Creates a visual for a command.
        /// </summary>
        /// <param name="command">The command for which a visual must be created.</param>
        /// <param name="requiresParentCommand">If <c>true</c>, this command requires a parent visual.</param>
        /// <returns>The visual for the command.</returns>
        public static Gtk.Widget CreateVisualForCommand(this VisualRelayCommand command, bool requiresParentCommand)
        {
            System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(command.UniqueId), "Command's UniqueId is not defined.");
            Gtk.Widget visual        = null;
            var        parentCommand = (VisualRelayCommand)command.VisualParent;

            if (parentCommand != null)
            {
                var parentVisual = parentCommand.Visual.NativeVisual;
                if (parentVisual is Gtk.MenuBar)
                {
                    throw new System.InvalidOperationException("This should be done by CreateMenuItem!");

                    // Make a submenu for the command, and add the menu item to the menubar.
                    ////var menu = new Gtk.Menu() { Name = command.UniqueId };
                    ////var menuItem = new Gtk.MenuItem(command.MenuItemName) { Name = command.UniqueId, Submenu = menu };
                    ////((Gtk.MenuBar)parentVisual).Add(menuItem);
                    ////visual = menuItem;
                }
                if (parentVisual is Gtk.Toolbar)
                {
                    // The default toolbar command visual will be a button. If a different visual is desired,
                    // the CommandGroup should override visual creation for that command in ICommandGroup.CreateVisualForCommand().
                    var          toolbar = parentVisual as Gtk.Toolbar;
                    Gtk.ToolItem toolbarItem;
                    if (command.UniqueId == RootCommandGroup.ToolbarSeparatorCommand.UniqueId)
                    {
                        toolbarItem = new Gtk.SeparatorToolItem();
                    }
                    else
                    {
                        var group = command.GetCommandGroup() as CommandGroup;
                        toolbarItem = group.CreateToolbarItemForCommand(command).AsType <Gtk.ToolItem>();
                    }
                    var insertLocation = FindInsertLocation(parentCommand, command, false);
                    toolbar.Insert(toolbarItem, insertLocation); // if <0, appends
                    visual = toolbarItem;
                }
                if (parentCommand.Visual.IsEmpty)
                {
                    var group = parentCommand.GetCommandGroup();
                    if (group != null)
                    {
                        parentCommand.Visual = group.CreateVisualForCommand(parentCommand);
                    }
                }

                ErrorReporting.ReportErrorIf(requiresParentCommand && parentCommand.Visual.IsEmpty, "Failed to create parent visual for command: " + command.Name + "(" + command.UniqueId + ")");
                parentVisual = parentCommand.Visual;
            }

            DebugOutputIf(requiresParentCommand && (parentCommand == null), "No parent visual for command: " + command.Name + "(" + command.UniqueId + ")");
            if (visual != null)
            {
                visual.SetValue(CommandGroup.AttachedCommandPropertyName, command);
                var group = command.GetCommandGroup() as CommandGroup;
                if (group != null)
                {
                    var context = group.GetCommandContext(command, null);
                    visual.SetValue(IFakeDependencyObjectHelpers.DataContextPropertyName, context);
                    group.AttachCanExecuteChangeHandler(command);
                }
            }
            return(visual);
        }