// Get the first control and measure, its height accepts as row height double GetRowHeight(RibbonToolBarLayoutDefinition layoutDefinition) { const double defaultRowHeight = 0; foreach (RibbonToolBarRow row in layoutDefinition.Rows) { foreach (DependencyObject item in row.Children) { RibbonToolBarControlDefinition controlDefinition = item as RibbonToolBarControlDefinition; RibbonToolBarControlGroupDefinition controlGroupDefinition = item as RibbonToolBarControlGroupDefinition; FrameworkElement control = null; if (controlDefinition != null) { control = GetControl(controlDefinition); } else if (controlGroupDefinition != null) { control = GetControlGroup(controlGroupDefinition); } if (control == null) { return(defaultRowHeight); } control.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); return(control.DesiredSize.Height); } } return(defaultRowHeight); }
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); }
/// <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)); }