示例#1
0
        internal static RibbonContextMenu ChooseContextMenu(DependencyObject owner)
        {
            if (owner is Ribbon)
            {
                return(GetDefaultRibbonClientAreaContextMenu());
            }
            else if (RibbonControlService.GetCanAddToQuickAccessToolBarDirectly(owner))
            {
                if (owner is RibbonGallery)
                {
                    return(GetGalleryContextMenu());
                }
                else
                {
                    if (RibbonControlService.GetIsInQuickAccessToolBar(owner))
                    {
                        return(GetQATControlContextMenu());
                    }
                    else
                    {
                        return(GetRibbonControlContextMenu());
                    }
                }
            }

            return(null);
        }
示例#2
0
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                DependencyObject d = value as DependencyObject;

                if (d != null)
                {
                    Ribbon ribbon = RibbonControlService.GetRibbon(d);
                    if (ribbon != null &&
                        !ribbon.ShowQuickAccessToolBarOnTop)
                    {
                        if (_mode == ConverterMode.Header)
                        {
                            return(ShowQATAboveText);
                        }
                        else
                        {
                            return(RibbonCommands.ShowQuickAccessToolBarAboveRibbonCommand);
                        }
                    }
                }

                if (_mode == ConverterMode.Header)
                {
                    return(ShowQATBelowText);
                }
                else
                {
                    return(RibbonCommands.ShowQuickAccessToolBarBelowRibbonCommand);
                }
            }
示例#3
0
        private void SaveQatItems()
        {
            string text = string.Empty;

            if (this.QuickAccessToolBar != null && this.QuickAccessToolBar.Items != null)
            {
                List <QatItem> qatItems = this.QuickAccessToolBar.Items.Cast <object>().
                                          Select(i => i as FrameworkElement).Where(e => e != null).
                                          Select(e => RibbonControlService.GetQuickAccessToolBarId(e)).Where(id => id != null).
                                          Select(q => new QatItem(q.GetHashCode())).ToList();

                List <QatItem> remainingItems = new List <QatItem>();
                remainingItems.AddRange(qatItems);

                // add -1 to show from application menu
                remainingItems.ForEach(qat => qat.ControlIndices.Add(-1));
                SaveQatItems(remainingItems, this.ApplicationMenu);
                remainingItems.ForEach(qat => qat.ControlIndices.Clear());
                SaveQatItems(remainingItems, this);

                text = qatItems.Aggregate("", (a, b) => a + "," + b.ControlIndices.ToString()).TrimStart(',');
            }
            Properties.Settings.Default.QuickAccessToolBar = text;
            Properties.Settings.Default.Save();
        }
示例#4
0
        /// <summary>
        ///     Tries to restore focus to the first focusable element across
        ///     the ancestor chain of ContextMenuOriginalSource. Should
        ///     be called only when Ribbon is supposed to retain the focus.
        /// </summary>
        private void RestoreFocusToRibbon()
        {
            DependencyObject current = GetDismissPopupSource();

            if (current == null)
            {
                return;
            }
            Ribbon ribbon = RibbonControlService.GetRibbon(current);

            if (ribbon == null)
            {
                return;
            }
            while (current != null)
            {
                UIElement uie = current as UIElement;
                if (uie != null && uie.Focusable)
                {
                    uie.Dispatcher.BeginInvoke(
                        (Action) delegate()
                    {
                        if (!ribbon.IsKeyboardFocusWithin)
                        {
                            uie.Focus();
                        }
                    },
                        DispatcherPriority.Input,
                        null);
                    break;
                }
                current = TreeHelper.GetParent(current);
            }
        }
示例#5
0
        private static void OnIsOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RibbonContextMenu contextMenu = (RibbonContextMenu)d;

            if (!(bool)(e.NewValue))
            {
                if (!contextMenu._ignoreDismissPopupsOnNextClose)
                {
                    UIElement dismissPopupSource = contextMenu.GetDismissPopupSource();
                    if (CanRaiseDismissPopups(dismissPopupSource))
                    {
                        // Raise DismissPopup on owner if can raise and if
                        // was not asked to ignore.
                        dismissPopupSource.RaiseEvent(new RibbonDismissPopupEventArgs(RibbonDismissPopupMode.Always));
                        ((Ribbon)(RibbonControlService.GetRibbon(dismissPopupSource))).RestoreFocusOnContextMenuClose();
                    }
                }
                else
                {
                    contextMenu.RestoreFocusToRibbon();
                    contextMenu._ignoreDismissPopupsOnNextClose = false;
                }
            }
            else
            {
                contextMenu._ignoreDismissPopupsOnNextClose = false;
            }
        }
示例#6
0
 private static DependencyObject FindElementThatCanBeAddedToQAT(DependencyObject obj)
 {
     while (obj != null && !RibbonControlService.GetCanAddToQuickAccessToolBarDirectly(obj))
     {
         obj = TreeHelper.GetParent(obj);
     }
     return(obj);
 }
        private static object CoerceDefaultControlSizeDefinition(DependencyObject d, object baseValue)
        {
            if (baseValue == null)
            {
                RibbonControlGroup controlGroup = (RibbonControlGroup)d;
                RibbonImageSize    imageSize    = RibbonImageSize.Collapsed;
                bool isLabelVisible             = false;
                int  itemCount  = controlGroup.Items.Count;
                bool childFound = false;

                // Get the largest ControlSizeDefinition variant for all
                // the child controls and construct a union ControlSizeDefinition.
                for (int i = 0; i < itemCount; i++)
                {
                    RibbonControl ribbonControl = controlGroup.ItemContainerGenerator.ContainerFromIndex(i) as RibbonControl;
                    if (ribbonControl != null && ribbonControl.Visibility != Visibility.Collapsed)
                    {
                        UIElement contentChild = ribbonControl.ContentChild;
                        if (contentChild != null && contentChild.Visibility != Visibility.Collapsed)
                        {
                            RibbonControlSizeDefinition currentLargeCsd = RibbonControlService.GetDefaultControlSizeDefinition(contentChild);
                            if (currentLargeCsd == null)
                            {
                                contentChild.CoerceValue(RibbonControlService.DefaultControlSizeDefinitionProperty);
                                currentLargeCsd = RibbonControlService.GetDefaultControlSizeDefinition(contentChild);
                            }

                            if (currentLargeCsd != null)
                            {
                                childFound = true;
                                if (imageSize == RibbonImageSize.Collapsed)
                                {
                                    imageSize = currentLargeCsd.ImageSize;
                                }
                                else if (currentLargeCsd.ImageSize == RibbonImageSize.Large)
                                {
                                    imageSize = RibbonImageSize.Large;
                                }

                                isLabelVisible |= currentLargeCsd.IsLabelVisible;

                                if (isLabelVisible && imageSize == RibbonImageSize.Large)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
                if (childFound)
                {
                    return(RibbonControlSizeDefinition.GetFrozenControlSizeDefinition(imageSize, isLabelVisible));
                }
            }
            return(baseValue);
        }
示例#8
0
        private static void AddToQATCanExecute(object sender, CanExecuteRoutedEventArgs args)
        {
            DependencyObject thatCanBeAddedToQat = MyRibbon.FindElementThatCanBeAddedToQAT(args.OriginalSource as DependencyObject);

            if (thatCanBeAddedToQat == null || RibbonControlService.GetQuickAccessToolBarId(thatCanBeAddedToQat) == null || RibbonHelper.ExistsInQAT(thatCanBeAddedToQat))
            {
                return;
            }
            args.CanExecute = true;
        }
示例#9
0
        private static bool IsStarChild(UIElement child, out double weight)
        {
            weight = 0;
            RibbonControlSizeDefinition controlDef = RibbonControlService.GetControlSizeDefinition(child);

            if (controlDef != null)
            {
                weight = controlDef.Width.Value;
                return(controlDef.Width.IsStar);
            }
            return(false);
        }
示例#10
0
        public static DependencyObject GetQATButton(RibbonQuickAccessToolBar qatBar, object id)
        {
            foreach (DependencyObject each in qatBar.Items)
            {
                if (RibbonControlService.GetQuickAccessToolBarId(each) == id)
                {
                    return(each);
                }
            }

            return(null);
        }
 private static object CoerceControlSizeDefinition(DependencyObject d, object baseValue)
 {
     if (baseValue == null)
     {
         RibbonControlSizeDefinition defaultControlSizeDefinition = RibbonControlService.GetDefaultControlSizeDefinition(d);
         if (defaultControlSizeDefinition == null)
         {
             d.CoerceValue(DefaultControlSizeDefinitionProperty);
             defaultControlSizeDefinition = RibbonControlService.GetDefaultControlSizeDefinition(d);
         }
         return(defaultControlSizeDefinition);
     }
     return(baseValue);
 }
示例#12
0
        /// <summary>
        ///     Property changed callback for tooltip PlacementTarget property.
        /// </summary>
        private static void OnPlacementTargetPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RibbonToolTip ribbonToolTip = (RibbonToolTip)d;
            UIElement     target        = e.NewValue as UIElement;

            if (target == null)
            {
                ribbonToolTip.Ribbon = null;
            }
            else
            {
                ribbonToolTip.Ribbon = RibbonControlService.GetRibbon(target);
            }
        }
        private static void UpdateDefaultControlSizeDefinition(DependencyObject d)
        {
            d.CoerceValue(DefaultControlSizeDefinitionProperty);

            // If the element belongs to a ControlGroup, then
            // coerce DefaultControlSizeDefinition for the ControlGroup too.
            if (RibbonControlService.GetIsInControlGroup(d))
            {
                RibbonControlGroup controlGroup = TreeHelper.FindVisualAncestor <RibbonControlGroup>(d);
                if (controlGroup != null)
                {
                    controlGroup.CoerceValue(DefaultControlSizeDefinitionProperty);
                }
            }
        }
示例#14
0
        private bool SaveQatItemsIfMatchesControl(List <QatItem> remainingItems, object control)
        {
            bool matched = false;

            if (control is FrameworkElement element)
            {
                object getQuickAccessToolBarId = RibbonControlService.GetQuickAccessToolBarId(element);
                if (getQuickAccessToolBarId != null)
                {
                    int remove = remainingItems.RemoveAll(qat => qat.HashCode == getQuickAccessToolBarId.GetHashCode());
                    matched = remove > 0;
                }
            }

            return(matched);
        }
示例#15
0
        // Determine whether the QAT contains an element with the given QAT ID.
        internal bool ContainsId(object targetID)
        {
            foreach (object o in this.Items)
            {
                DependencyObject dependencyObject = o as DependencyObject;
                if (dependencyObject != null)
                {
                    object currentID = RibbonControlService.GetQuickAccessToolBarId(dependencyObject);
                    if (object.Equals(currentID, targetID))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
 private void TransferPseudoInheritedProperties()
 {
     if (_templateRoot != null)
     {
         // Ribbon is an inherited property. In non-MVVM scenarios where
         // controls are directly added under RibbonGroup in XAML, RibbonGroup
         // is the logical parent of those controls and they get the value
         // of RibbonParent set from their logical parent. When a RibbonGroup
         // get collapsed and its template changes, due to a bug in framework
         // the inheritance value of those controls is lost during visual tree
         // change and never again gets updated. The workaround is to set the
         // local value on those controls from RibbonContentPresenter which
         // would be their visual parent. This works because Ribbon property is
         // readonly.
         RibbonControlService.SetRibbon(_templateRoot, RibbonControlService.GetRibbon(this));
         RibbonHelper.TransferPseudoInheritedProperties(this, _templateRoot);
     }
 }
        private static object CoerceDefaultControlSizeDefinition(DependencyObject d, object baseValue)
        {
            if (baseValue == null)
            {
                RibbonImageSize imageSize = RibbonImageSize.Collapsed;
                if (RibbonControlService.GetLargeImageSource(d) != null)
                {
                    imageSize = RibbonImageSize.Large;
                }
                else if (RibbonControlService.GetSmallImageSource(d) != null)
                {
                    imageSize = RibbonImageSize.Small;
                }
                bool isLabelVisible = !string.IsNullOrEmpty(RibbonControlService.GetLabel(d));

                return(RibbonControlSizeDefinition.GetFrozenControlSizeDefinition(imageSize, isLabelVisible));
            }
            return(baseValue);
        }
示例#18
0
        /// <summary>
        ///     Links the given KeyTipControl as the visual child of self.
        ///     In the process sets various properties of the control.
        /// </summary>
        public void LinkKeyTipControl(DependencyObject keyTipElement, KeyTipControl keyTipControl)
        {
            Debug.Assert(_keyTipControl == null && keyTipControl.KeyTipAdorner == null);
            _keyTipControl = keyTipControl;
            _keyTipControl.KeyTipAdorner = this;
            _keyTipControl.Text          = KeyTipService.GetKeyTip(keyTipElement).ToUpper(KeyTipService.GetCultureForElement(keyTipElement));
            _keyTipControl.IsEnabled     = (bool)keyTipElement.GetValue(UIElement.IsEnabledProperty);
            Style keyTipStyle = KeyTipService.GetKeyTipStyle(keyTipElement);

            _keyTipControl.Style           = keyTipStyle;
            _keyTipControl.RenderTransform = _keyTipTransform;
            bool clearCustomProperties = true;

            if (keyTipStyle == null)
            {
                Ribbon.Ribbon ribbon = RibbonControlService.GetRibbon(PlacementTarget);
                if (ribbon != null)
                {
                    // Use Ribbon properties if the owner element belongs to a Ribbon.
                    keyTipStyle = KeyTipService.GetKeyTipStyle(ribbon);
                    if (keyTipStyle != null)
                    {
                        _keyTipControl.Style = keyTipStyle;
                    }
                    else
                    {
                        clearCustomProperties      = false;
                        _keyTipControl.Background  = ribbon.Background;
                        _keyTipControl.BorderBrush = ribbon.BorderBrush;
                        _keyTipControl.Foreground  = ribbon.Foreground;
                    }
                }
            }
            if (clearCustomProperties)
            {
                _keyTipControl.ClearValue(Control.BackgroundProperty);
                _keyTipControl.ClearValue(Control.BorderBrushProperty);
                _keyTipControl.ClearValue(Control.ForegroundProperty);
            }
            AddVisualChild(_keyTipControl);
            EnsureTransform();
        }
示例#19
0
        private static bool CanRaiseDismissPopups(UIElement dismissPopupSource)
        {
            // Contextmenu raises DismissPopup event on the source only
            // if it is in a popup and is in a Ribbon.
            if (dismissPopupSource == null ||
                RibbonControlService.GetRibbon(dismissPopupSource) == null)
            {
                return(false);
            }

            Popup ancestorPopup = TreeHelper.FindAncestor(dismissPopupSource, delegate(DependencyObject element) { return(element is Popup); }) as Popup;

            if (ancestorPopup == null ||
                !ancestorPopup.IsOpen)
            {
                return(false);
            }

            return(true);
        }
        /// <summary>
        ///     Helper method which scrolls item at given index into view.
        ///     Can be used as a dispatcher operation.
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        private object ScrollContainerIntoView(object arg)
        {
            int index = (int)arg;
            FrameworkElement element = ItemContainerGenerator.ContainerFromIndex(index) as FrameworkElement;

            if (element != null)
            {
                element.BringIntoView();

                // If there is a margin on TabHeader on the end, BringIntoView call
                // may not scroll to the end. Explicitly scroll to end in such cases.
                IScrollInfo scrollInfo = InternalItemsHost as IScrollInfo;
                if (scrollInfo != null)
                {
                    ScrollViewer scrollViewer = scrollInfo.ScrollOwner;
                    if (scrollViewer != null)
                    {
                        Ribbon ribbon = RibbonControlService.GetRibbon(this);
                        if (ribbon != null)
                        {
                            int displayIndex = ribbon.GetTabDisplayIndexForIndex(index);
                            if (displayIndex == 0)
                            {
                                // If this tab header is the first tab header displayed
                                // then scroll to the left end.
                                scrollViewer.ScrollToLeftEnd();
                            }
                            else if (ribbon.GetTabIndexForDisplayIndex(displayIndex + 1) < 0)
                            {
                                // If this tab header is the last tab header displayed
                                // then scroll to the right end.
                                scrollViewer.ScrollToRightEnd();
                            }
                        }
                    }
                }
            }
            return(null);
        }
示例#21
0
        private static void AddToQATExecuted(object sender, ExecutedRoutedEventArgs args)
        {
            var    ribbon0 = (MyRibbon)sender;
            object model   = AttachedProperties.GetModel(ribbon0);

            if ((model is PrimaryRibbonModel primary))
            {
                if (!(MyRibbon.FindElementThatCanBeAddedToQAT((DependencyObject)(args.OriginalSource as UIElement)) is UIElement thatCanBeAddedToQat))
                {
                    return;
                }
                RibbonQuickAccessToolBarCloneEventArgs barCloneEventArgs = new RibbonQuickAccessToolBarCloneEventArgs(thatCanBeAddedToQat);
                thatCanBeAddedToQat.RaiseEvent((RoutedEventArgs)barCloneEventArgs);
                MyRibbon ribbon = RibbonControlService.GetRibbon((DependencyObject)thatCanBeAddedToQat) as MyRibbon;
                if (barCloneEventArgs.CloneInstance == null)
                {
                    return;
                }
                primary.QuickAccessToolBar.Items.Add((object)barCloneEventArgs.CloneInstance);
                args.Handled = true;
            }
        }
示例#22
0
        private UIElement GetDismissPopupSource()
        {
            UIElement placementTarget = PlacementTarget;

            if (placementTarget == null)
            {
                return(null);
            }
            Ribbon ribbon = RibbonControlService.GetRibbon(placementTarget);

            if (ribbon == null)
            {
                return(null);
            }
            // The original source for corresponding ContextMenuOpening will
            // be the original source for DismissPopup event.
            UIElement returnValue = ribbon.ContextMenuOriginalSource;

            if (returnValue == null)
            {
                returnValue = placementTarget;
            }
            return(returnValue);
        }