Example #1
0
        private void LoadQatItemIfMatchesControl(
            QatItemCollection previouslyMatchedItems,
            QatItemCollection matchedItems,
            int matchLevel,
            int controlIndex,
            object control,
            ref int remainingItemsCount)
        {
            for (int qatIndex = 0; qatIndex < previouslyMatchedItems.Count; qatIndex++)
            {
                QatItem qatItem = previouslyMatchedItems[qatIndex];

                if (qatItem.ControlIndices[matchLevel] == controlIndex)
                {
                    if (qatItem.ControlIndices.Count == matchLevel + 1)
                    {
                        qatItem.Instance = control;
                        remainingItemsCount--;
                    }
                    else if (qatItem.ControlIndices.Count == matchLevel + 2 && qatItem.ControlIndices[matchLevel + 1] == -1)
                    {
                        qatItem.IsSplitHeader = true;
                        Control element = control as Control;

                        if (element != null)
                        {
                            object splitControl = element.Template.FindName("PART_HeaderButton", element);

                            if (splitControl == null)
                            {
                                element.ApplyTemplate();
                                splitControl = element.Template.FindName("PART_HeaderButton", element);
                            }

                            if (splitControl != null)
                            {
                                qatItem.Instance = splitControl;
                            }
                        }

                        remainingItemsCount--;
                    }
                    else
                    {
                        matchedItems.Add(qatItem);
                    }
                }
            }
        }
Example #2
0
        private void SaveQatItems(RibbonQuickAccessToolBar qat)
        {
            if (qat != null)
            {
                QatItemCollection qatItems       = new QatItemCollection();
                QatItemCollection remainingItems = new QatItemCollection();

                if (qat.Items.Count > 0)
                {
                    for (int qatIndex = 0; qatIndex < qat.Items.Count; qatIndex++)
                    {
                        object instance      = qat.Items[qatIndex];
                        bool   isSplitHeader = false;

                        if (instance is ICommandSource)
                        {
                            ICommandSource element = (ICommandSource)instance;

                            if (element.Command != null)
                            {
                                isSplitHeader = instance is RibbonSplitMenuItem || instance is RibbonSplitButton;
                                instance      = element.Command;
                            }
                        }

                        QatItem qatItem = new QatItem(instance, isSplitHeader);
                        qatItems.Add(qatItem);
                        remainingItems.Add(qatItem);
                    }

                    for (int tabIndex = 0; tabIndex < _ribbon.Items.Count && remainingItems.Count > 0; tabIndex++)
                    {
                        RibbonTab tab = _ribbon.Items[tabIndex] as RibbonTab;
                        SaveQatItemsAmongChildren(remainingItems, tab, tabIndex);
                    }
                }

                if (!Directory.Exists(Static.LocalAppData))
                {
                    Directory.CreateDirectory(Static.LocalAppData);
                }

                XmlWriter xmlWriter = XmlWriter.Create(_qatFileName);
                XamlWriter.Save(qatItems, xmlWriter);
                xmlWriter.Close();
            }
        }
Example #3
0
        private void SaveQatItemsAmongChildrenInner(QatItemCollection remainingItems, object parent)
        {
            SaveQatItemsIfMatchesControl(remainingItems, parent);

            if (IsLeaf(parent))
            {
                return;
            }

            int childIndex = 0;
            DependencyObject dependencyObject = parent as DependencyObject;

            if (dependencyObject != null)
            {
                IEnumerable children = LogicalTreeHelper.GetChildren(dependencyObject);

                foreach (object child in children)
                {
                    SaveQatItemsAmongChildren(remainingItems, child, childIndex);
                    childIndex++;
                }
            }

            if (childIndex != 0)
            {
                return;
            }

            // if we failed to get any logical children, enumerate the visual ones
            Visual visual = parent as Visual;

            if (visual == null)
            {
                return;
            }

            for (childIndex = 0; childIndex < VisualTreeHelper.GetChildrenCount(visual); childIndex++)
            {
                Visual child = VisualTreeHelper.GetChild(visual, childIndex) as Visual;
                SaveQatItemsAmongChildren(remainingItems, child, childIndex);
            }
        }
Example #4
0
        private void SaveQatItemsAmongChildren(QatItemCollection remainingItems, object control, int controlIndex)
        {
            if (control == null)
            {
                return;
            }

            for (int qatIndex = 0; qatIndex < remainingItems.Count; qatIndex++)
            {
                QatItem qatItem = remainingItems[qatIndex];
                qatItem.ControlIndices.Add(controlIndex);
            }

            SaveQatItemsAmongChildrenInner(remainingItems, control);

            for (int qatIndex = 0; qatIndex < remainingItems.Count; qatIndex++)
            {
                QatItem qatItem = remainingItems[qatIndex];
                int     tail    = qatItem.ControlIndices.Count - 1;
                qatItem.ControlIndices.RemoveAt(tail);
            }
        }
Example #5
0
        private bool SaveQatItemsIfMatchesControl(QatItemCollection remainingItems, object control)
        {
            bool           matched = false;
            ICommandSource element = control as ICommandSource;

            if (element != null)
            {
                object data = element.Command;

                if (data != null)
                {
                    for (int qatIndex = 0; qatIndex < remainingItems.Count; qatIndex++)
                    {
                        QatItem qatItem = remainingItems[qatIndex];

                        if (qatItem.Instance == data)
                        {
                            if (qatItem.IsSplitHeader)
                            {
                                // This is the case of the Header of a SplitButton
                                // or a SplitMenuItem added to the QAT. Note -1 is
                                // the sentinel being used to indicate this case.

                                qatItem.ControlIndices.Add(-1);
                            }

                            remainingItems.Remove(qatItem);
                            qatIndex--;
                            matched = true;
                        }
                    }
                }
            }

            return(matched);
        }
Example #6
0
        private void LoadQatItems(RibbonQuickAccessToolBar qat)
        {
            Dispatcher.CurrentDispatcher.BeginInvoke(() =>
            {
                try
                {
                    if (qat != null)
                    {
                        if (File.Exists(_qatFileName))
                        {
                            XmlReader xmlReader        = XmlReader.Create(_qatFileName);
                            QatItemCollection qatItems = (QatItemCollection)XamlReader.Load(xmlReader);
                            xmlReader.Close();

                            if (qatItems != null)
                            {
                                int remainingItemsCount        = qatItems.Count;
                                QatItemCollection matchedItems = new QatItemCollection();

                                if (qatItems.Count > 0)
                                {
                                    for (int tabIndex = 0; tabIndex < _ribbon.Items.Count && remainingItemsCount > 0; tabIndex++)
                                    {
                                        matchedItems.Clear();

                                        for (int qatIndex = 0; qatIndex < qatItems.Count; qatIndex++)
                                        {
                                            QatItem qatItem = qatItems[qatIndex];

                                            if (qatItem.ControlIndices[0] == tabIndex)
                                            {
                                                matchedItems.Add(qatItem);
                                            }
                                        }

                                        RibbonTab tab = _ribbon.Items[tabIndex] as RibbonTab;

                                        if (tab != null)
                                        {
                                            LoadQatItemsAmongChildren(matchedItems, 0, tabIndex, tab, ref remainingItemsCount);
                                        }
                                    }

                                    for (int qatIndex = 0; qatIndex < qatItems.Count; qatIndex++)
                                    {
                                        QatItem qatItem = qatItems[qatIndex];
                                        Control control = qatItem.Instance as Control;

                                        if (control != null)
                                        {
                                            if (RibbonCommands.AddToQuickAccessToolBarCommand.CanExecute(null, control))
                                            {
                                                RibbonCommands.AddToQuickAccessToolBarCommand.Execute(null, control);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch { }
            });
        }
Example #7
0
        private void LoadQatItemsAmongChildren(
            QatItemCollection previouslyMatchedItems,
            int matchLevel,
            int controlIndex,
            object parent,
            ref int remainingItemsCount)
        {
            if (previouslyMatchedItems.Count == 0)
            {
                return;
            }

            if (IsLeaf(parent))
            {
                return;
            }

            int childIndex = 0;
            DependencyObject dependencyObject = parent as DependencyObject;

            if (dependencyObject != null)
            {
                IEnumerable children = LogicalTreeHelper.GetChildren(dependencyObject);

                foreach (object child in children)
                {
                    if (remainingItemsCount == 0)
                    {
                        break;
                    }

                    QatItemCollection matchedItems = new QatItemCollection();
                    LoadQatItemIfMatchesControl(previouslyMatchedItems, matchedItems, matchLevel + 1, childIndex, child, ref remainingItemsCount);
                    LoadQatItemsAmongChildren(matchedItems, matchLevel + 1, childIndex, child, ref remainingItemsCount);
                    childIndex++;
                }
            }

            if (childIndex != 0)
            {
                return;
            }

            // if we failed to get any logical children, enumerate the visual ones
            Visual visual = parent as Visual;

            if (visual == null)
            {
                return;
            }

            for (childIndex = 0; childIndex < VisualTreeHelper.GetChildrenCount(visual); childIndex++)
            {
                if (remainingItemsCount == 0)
                {
                    break;
                }

                Visual            child        = VisualTreeHelper.GetChild(visual, childIndex) as Visual;
                QatItemCollection matchedItems = new QatItemCollection();
                LoadQatItemIfMatchesControl(previouslyMatchedItems, matchedItems, matchLevel + 1, childIndex, child, ref remainingItemsCount);
                LoadQatItemsAmongChildren(matchedItems, matchLevel + 1, childIndex, child, ref remainingItemsCount);
            }
        }
        private void SaveQatItemsAmongChildrenInner(QatItemCollection remainingItems, object parent)
        {
            SaveQatItemsIfMatchesControl(remainingItems, parent);
            if (IsLeaf(parent))
            {
                return;
            }

            int childIndex = 0;
            DependencyObject dependencyObject = parent as DependencyObject;
            if (dependencyObject != null)
            {
                IEnumerable children = LogicalTreeHelper.GetChildren(dependencyObject);
                foreach (object child in children)
                {
                    SaveQatItemsAmongChildren(remainingItems, child, childIndex);
                    childIndex++;
                }
            }
            if (childIndex != 0)
            {
                return;
            }

            // if we failed to get any logical children, enumerate the visual ones
            Visual visual = parent as Visual;
            if (visual == null)
            {
                return;
            }
            for (childIndex = 0; childIndex < VisualTreeHelper.GetChildrenCount(visual); childIndex++)
            {
                Visual child = VisualTreeHelper.GetChild(visual, childIndex) as Visual;
                SaveQatItemsAmongChildren(remainingItems, child, childIndex);
            }
        }
        private bool SaveQatItemsIfMatchesControl(QatItemCollection remainingItems, object control)
        {
            bool matched = false;
            FrameworkElement element = control as FrameworkElement;
            if (element != null)
            {
                object data = element.DataContext;
                if (data != null)
                {
                    for (int qatIndex = 0; qatIndex < remainingItems.Count; qatIndex++)
                    {
                        QatItem qatItem = remainingItems[qatIndex];
                        if (qatItem.Instance == data)
                        {
                            if (qatItem.IsSplitHeader)
                            {
                                // This is the case of the Header of a SplitButton
                                // or a SplitMenuItem added to the QAT. Note -1 is
                                // the sentinel being used to indicate this case.

                                qatItem.ControlIndices.Add(-1);
                            }

                            remainingItems.Remove(qatItem);
                            qatIndex--;
                            matched = true;
                        }
                    }
                }
            }
            return matched;
        }
        private void SaveQatItemsAmongChildren(QatItemCollection remainingItems, object control, int controlIndex)
        {
            if (control == null)
            {
                return;
            }

            for (int qatIndex = 0; qatIndex < remainingItems.Count; qatIndex++)
            {
                QatItem qatItem = remainingItems[qatIndex];
                qatItem.ControlIndices.Add(controlIndex);
            }

            SaveQatItemsAmongChildrenInner(remainingItems, control);

            for (int qatIndex = 0; qatIndex < remainingItems.Count; qatIndex++)
            {
                QatItem qatItem = remainingItems[qatIndex];
                int tail = qatItem.ControlIndices.Count - 1;
                qatItem.ControlIndices.RemoveAt(tail);
            }
        }
        private void SaveQatItems(RibbonQuickAccessToolBar qat)
        {
            if (qat != null)
            {
                QatItemCollection qatItems = new QatItemCollection();
                QatItemCollection remainingItems = new QatItemCollection();

                if (qat.Items.Count > 0)
                {
                    for (int qatIndex = 0; qatIndex < qat.Items.Count; qatIndex++)
                    {
                        object instance = qat.Items[qatIndex];
                        bool isSplitHeader = false;

                        if (instance is FrameworkElement)
                        {
                            FrameworkElement element = (FrameworkElement)instance;

                            if (((FrameworkElement)instance).DataContext != null)
                            {
                                instance = ((FrameworkElement)instance).DataContext;
                                isSplitHeader =
                                    (instance is SplitMenuItemData && element is ButtonBase) ||
                                    (instance is SplitButtonData && element is ButtonBase);
                            }
                        }

                        QatItem qatItem = new QatItem(instance, isSplitHeader);
                        qatItems.Add(qatItem);
                        remainingItems.Add(qatItem);
                    }

                    for (int tabIndex = 0; tabIndex < ribbon.Items.Count && remainingItems.Count > 0; tabIndex++)
                    {
                        RibbonTab tab = ribbon.Items[tabIndex] as RibbonTab;
                        SaveQatItemsAmongChildren(remainingItems, tab, tabIndex);
                    }
                }

                XmlWriter xmlWriter = XmlWriter.Create(_qatFileName);
                XamlWriter.Save(qatItems, xmlWriter);
                xmlWriter.Close();
            }
        }
        private void LoadQatItemsAmongChildren(
            QatItemCollection previouslyMatchedItems,
            int matchLevel,
            int controlIndex,
            object parent,
            ref int remainingItemsCount)
        {
            if (previouslyMatchedItems.Count == 0)
            {
                return;
            }
            if (IsLeaf(parent))
            {
                return;
            }

            int childIndex = 0;
            DependencyObject dependencyObject = parent as DependencyObject;
            if (dependencyObject != null)
            {
                IEnumerable children = LogicalTreeHelper.GetChildren(dependencyObject);
                foreach (object child in children)
                {
                    if (remainingItemsCount == 0)
                    {
                        break;
                    }

                    QatItemCollection matchedItems = new QatItemCollection();
                    LoadQatItemIfMatchesControl(previouslyMatchedItems, matchedItems, matchLevel + 1, childIndex, child, ref remainingItemsCount);
                    LoadQatItemsAmongChildren(matchedItems, matchLevel + 1, childIndex, child, ref remainingItemsCount);
                    childIndex++;
                }
            }
            if (childIndex != 0)
            {
                return;
            }

            // if we failed to get any logical children, enumerate the visual ones
            Visual visual = parent as Visual;
            if (visual == null)
            {
                return;
            }
            for (childIndex = 0; childIndex < VisualTreeHelper.GetChildrenCount(visual); childIndex++)
            {
                if (remainingItemsCount == 0)
                {
                    break;
                }

                Visual child = VisualTreeHelper.GetChild(visual, childIndex) as Visual;
                QatItemCollection matchedItems = new QatItemCollection();
                LoadQatItemIfMatchesControl(previouslyMatchedItems, matchedItems, matchLevel + 1, childIndex, child, ref remainingItemsCount);
                LoadQatItemsAmongChildren(matchedItems, matchLevel + 1, childIndex, child, ref remainingItemsCount);
            }
        }
        private void LoadQatItems(RibbonQuickAccessToolBar qat)
        {
            if (qat != null)
            {
                if (File.Exists(_qatFileName))
                {
                    XmlReader xmlReader = XmlReader.Create(_qatFileName);
                    QatItemCollection qatItems = (QatItemCollection)XamlReader.Load(xmlReader);
                    xmlReader.Close();
                    if (qatItems != null)
                    {
                        int remainingItemsCount = qatItems.Count;
                        QatItemCollection matchedItems = new QatItemCollection();

                        if (qatItems.Count > 0)
                        {
                            for (int tabIndex = 0; tabIndex < ribbon.Items.Count && remainingItemsCount > 0; tabIndex++)
                            {
                                matchedItems.Clear();
                                for (int qatIndex = 0; qatIndex < qatItems.Count; qatIndex++)
                                {
                                    QatItem qatItem = qatItems[qatIndex];
                                    if (qatItem.ControlIndices[0] == tabIndex)
                                    {
                                        matchedItems.Add(qatItem);
                                    }
                                }

                                RibbonTab tab = ribbon.Items[tabIndex] as RibbonTab;
                                if (tab != null)
                                {
                                    LoadQatItemsAmongChildren(matchedItems, 0, tabIndex, tab, ref remainingItemsCount);
                                }
                            }

                            for (int qatIndex = 0; qatIndex < qatItems.Count; qatIndex++)
                            {
                                QatItem qatItem = qatItems[qatIndex];
                                Control control = qatItem.Instance as Control;
                                if (control != null)
                                {
                                    if (RibbonCommands.AddToQuickAccessToolBarCommand.CanExecute(null, control))
                                    {
                                        RibbonCommands.AddToQuickAccessToolBarCommand.Execute(null, control);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
 private void LoadQatItemIfMatchesControl(
     QatItemCollection previouslyMatchedItems,
     QatItemCollection matchedItems,
     int matchLevel,
     int controlIndex,
     object control,
     ref int remainingItemsCount)
 {
     for (int qatIndex = 0; qatIndex < previouslyMatchedItems.Count; qatIndex++)
     {
         QatItem qatItem = previouslyMatchedItems[qatIndex];
         if (qatItem.ControlIndices[matchLevel] == controlIndex)
         {
             if (qatItem.ControlIndices.Count == matchLevel + 1)
             {
                 qatItem.Instance = control;
                 remainingItemsCount--;
             }
             else if (qatItem.ControlIndices.Count == matchLevel + 2 && qatItem.ControlIndices[matchLevel + 1] == -1)
             {
                 qatItem.IsSplitHeader = true;
                 Control element = control as Control;
                 if (element != null)
                 {
                     object splitControl = element.Template.FindName("PART_HeaderButton", element);
                     if (splitControl == null)
                     {
                         element.ApplyTemplate();
                         splitControl = element.Template.FindName("PART_HeaderButton", element);
                     }
                     if (splitControl != null)
                     {
                         qatItem.Instance = splitControl;
                     }
                 }
                 remainingItemsCount--;
             }
             else
             {
                 matchedItems.Add(qatItem);
             }
         }
     }
 }