RibbonToolBarControlGroup GetControlGroup(RibbonToolBarControlGroupDefinition controlGroupDefinition)
        {
            RibbonToolBarControlGroup controlGroup = null;

            if (!cachedControlGroups.TryGetValue(controlGroupDefinition, out controlGroup))
            {
                controlGroup = new RibbonToolBarControlGroup();
                // Add items to the group
                foreach (RibbonToolBarControlDefinition child in controlGroupDefinition.Children)
                {
                    controlGroup.Items.Add(GetControl(child));
                }
                cachedControlGroups.Add(controlGroupDefinition, controlGroup);
            }
            return(controlGroup);
        }
        private RibbonToolBarControlGroup GetControlGroup(RibbonToolBarControlGroupDefinition controlGroupDefinition)
        {
            if (this.cachedControlGroups.TryGetValue(controlGroupDefinition, out var controlGroup))
            {
                return(controlGroup);
            }

            controlGroup = new RibbonToolBarControlGroup();

            // Add items to the group
            foreach (var child in controlGroupDefinition.Children)
            {
                controlGroup.Items.Add(this.GetControl(child));
            }

            this.cachedControlGroups.Add(controlGroupDefinition, controlGroup);
            return(controlGroup);
        }
        /// <summary>
        /// Measures all of the RibbonGroupBox, and resize them appropriately
        /// to fit within the available room
        /// </summary>
        /// <param name="availableSize">The available size that
        /// this element can give to child elements.</param>
        /// <returns>The size that the panel determines it needs during
        /// layout, based on its calculations of child element sizes.
        /// </returns>
        protected override Size MeasureOverride(Size availableSize)
        {
            RibbonToolBarLayoutDefinition layoutDefinition = GetCurrentLayoutDefinition();

            // Rebuilding actual children (visual & logical)
            if (rebuildVisualAndLogicalChildren)
            {
                // Clear previous children
                foreach (FrameworkElement child in actualChildren)
                {
                    RibbonToolBarControlGroup controlGroup = child as RibbonToolBarControlGroup;
                    if (controlGroup != null)
                    {
                        controlGroup.Items.Clear();
                    }
                    RemoveVisualChild(child);
                    RemoveLogicalChild(child);
                }
                actualChildren.Clear();
                cachedControlGroups.Clear();
            }

            if (layoutDefinition == null)
            {
                if (rebuildVisualAndLogicalChildren)
                {
                    // If default layout is used add all children
                    foreach (FrameworkElement child in Children)
                    {
                        actualChildren.Add(child);
                        AddVisualChild(child);
                        AddLogicalChild(child);
                    }
                    rebuildVisualAndLogicalChildren = false;
                }
                return(WrapPanelLayuot(availableSize, true));
            }
            else
            {
                Size result = CustomLayout(layoutDefinition, availableSize, true, rebuildVisualAndLogicalChildren);
                rebuildVisualAndLogicalChildren = false;
                return(result);
            }
        }
 RibbonToolBarControlGroup GetControlGroup(RibbonToolBarControlGroupDefinition controlGroupDefinition)
 {
     RibbonToolBarControlGroup controlGroup = null;
     if (!this.cachedControlGroups.TryGetValue(controlGroupDefinition, out controlGroup))
     {
         controlGroup = new RibbonToolBarControlGroup();
         // Add items to the group
         foreach (RibbonToolBarControlDefinition child in controlGroupDefinition.Children)
         {
             controlGroup.Items.Add(this.GetControl(child));
         }
         this.cachedControlGroups.Add(controlGroupDefinition, controlGroup);
     }
     return controlGroup;
 }
        /// <summary>
        /// Layout logic for the given layout definition
        /// </summary>
        /// <param name="layoutDefinition">Current layout definition</param>
        /// <param name="availableSize">Available or final size</param>
        /// <param name="measure">Pass true if measure required; pass false if arrange required</param>
        /// <param name="addchildren">Determines whether we have to add children to the logical and visual tree</param>
        /// <returns>Final size</returns>
        Size CustomLayout(RibbonToolBarLayoutDefinition layoutDefinition, Size availableSize, bool measure, bool addchildren)
        {
            bool   arrange         = !measure;
            double availableHeight = Double.IsPositiveInfinity(availableSize.Height) ? 0 : availableSize.Height;

            // Clear separator cahce
            if (addchildren)
            {
                separatorCache.Clear();
            }

            // Get the first control and measure, its height accepts as row height
            double rowHeight = GetRowHeight(layoutDefinition);


            // Calculate whitespace
            int    rowCountInColumn = Math.Min(layoutDefinition.RowCount, layoutDefinition.Rows.Count);
            double whitespace       = (availableHeight - ((double)rowCountInColumn * rowHeight)) / (double)(rowCountInColumn + 1);

            double y = 0;
            double x = 0;
            double currentRowBegin = 0;
            double currentMaxX     = 0;
            double maxy            = 0;

            for (int rowIndex = 0; rowIndex < layoutDefinition.Rows.Count; rowIndex++)
            {
                RibbonToolBarRow row = layoutDefinition.Rows[rowIndex];

                x = currentRowBegin;

                if (rowIndex % rowCountInColumn == 0)
                {
                    // Reset vars at new column
                    x = currentRowBegin = currentMaxX;
                    y = 0;

                    if (rowIndex != 0)
                    {
                        #region Add separator

                        Separator separator = null;
                        if (!separatorCache.TryGetValue(rowIndex, out separator))
                        {
                            separator       = new Separator();
                            separator.Style = SeparatorStyle;
                            separatorCache.Add(rowIndex, separator);
                        }
                        if (measure)
                        {
                            separator.Height = availableHeight - separator.Margin.Bottom - separator.Margin.Top;
                            separator.Measure(availableSize);
                        }
                        if (arrange)
                        {
                            separator.Arrange(new Rect(x, y,
                                                       separator.DesiredSize.Width,
                                                       separator.DesiredSize.Height));
                        }
                        x += separator.DesiredSize.Width;

                        if (addchildren)
                        {
                            // Add control in the children
                            AddVisualChild(separator);
                            AddLogicalChild(separator);
                            actualChildren.Add(separator);
                        }

                        #endregion
                    }
                }
                y += whitespace;


                // Measure & arrange new row
                for (int i = 0; i < row.Children.Count; i++)
                {
                    if (row.Children[i] is RibbonToolBarControlDefinition)
                    {
                        // Control Definition Case
                        RibbonToolBarControlDefinition controlDefinition =
                            (RibbonToolBarControlDefinition)row.Children[i];
                        FrameworkElement control = GetControl(controlDefinition);
                        if (control == null)
                        {
                            continue;
                        }

                        if (addchildren)
                        {
                            // Add control in the children
                            AddVisualChild(control);
                            AddLogicalChild(control);
                            actualChildren.Add(control);
                        }

                        if (measure)
                        {
                            // Apply Control Definition Properties
                            RibbonAttachedProperties.SetRibbonSize(control, RibbonAttachedProperties.GetRibbonSize(controlDefinition));
                            control.Width = controlDefinition.Width;
                            control.Measure(availableSize);
                        }
                        if (arrange)
                        {
                            control.Arrange(new Rect(x, y,
                                                     control.DesiredSize.Width,
                                                     control.DesiredSize.Height));
                        }

                        x += control.DesiredSize.Width;
                    }
                    if (row.Children[i] is RibbonToolBarControlGroupDefinition)
                    {
                        // Control Definition Case
                        RibbonToolBarControlGroupDefinition controlGroupDefinition =
                            (RibbonToolBarControlGroupDefinition)row.Children[i];

                        RibbonToolBarControlGroup control = GetControlGroup(controlGroupDefinition);

                        if (addchildren)
                        {
                            // Add control in the children
                            AddVisualChild(control);
                            AddLogicalChild(control);
                            actualChildren.Add(control);
                        }

                        if (measure)
                        {
                            // Apply Control Definition Properties
                            control.IsFirstInRow = (i == 0);
                            control.IsLastInRow  = (i == row.Children.Count - 1);
                            control.Measure(availableSize);
                        }
                        if (arrange)
                        {
                            control.Arrange(new Rect(x, y,
                                                     control.DesiredSize.Width,
                                                     control.DesiredSize.Height));
                        }

                        x += control.DesiredSize.Width;
                    }
                }

                y += rowHeight;
                if (currentMaxX < x)
                {
                    currentMaxX = x;
                }
                if (maxy < y)
                {
                    maxy = y;
                }
            }

            return(new Size(currentMaxX, maxy + whitespace));
        }