/// <summary> /// Discover the preferred size of the element. /// </summary> /// <param name="context">Layout context.</param> public override Size GetPreferredSize(ViewLayoutContext context) { // Clear down the preferred size caches switch (_currentSize) { case GroupItemSize.Small: _smallCache.Clear(); _smallWidest = 0; break; case GroupItemSize.Medium: _mediumCache.Clear(); _mediumWidest = 0; break; case GroupItemSize.Large: _largeCache.Clear(); break; } // Sync child elements to the current group items SyncChildrenToRibbonGroupItems(); Size preferredSize = Size.Empty; // Are we sizing horizontal or vertical? bool horizontal = (_currentSize == GroupItemSize.Large); // Find total width and maximum height across all child elements for (int i = 0; i < this.Count; i++) { ViewBase child = this[i]; // Only interested in visible items if (child.Visible) { // Cache preferred size of the child Size childSize = child.GetPreferredSize(context); // Add into the size cache for use in layout code switch (_currentSize) { case GroupItemSize.Small: _smallCache.Add(child, childSize); _smallWidest = Math.Max(_smallWidest, childSize.Width); break; case GroupItemSize.Medium: _mediumCache.Add(child, childSize); _mediumWidest = Math.Max(_mediumWidest, childSize.Width); break; case GroupItemSize.Large: _largeCache.Add(child, childSize); break; } if (horizontal) { // If not the first item positioned if (preferredSize.Width > 0) { // Add on a single pixel spacing gap preferredSize.Width++; } // Always add on to the width preferredSize.Width += childSize.Width; // Find maximum height encountered preferredSize.Height = Math.Max(preferredSize.Height, childSize.Height); } else { // Always add on to the width preferredSize.Height += childSize.Height; // Find maximum height encountered preferredSize.Width = Math.Max(preferredSize.Width, childSize.Width); } } } // At design time we add space for the selection flap if (_ribbon.InDesignHelperMode) { preferredSize.Width += DesignTimeDraw.FlapWidth + DesignTimeDraw.SepWidth; } return(preferredSize); }
/// <summary> /// Discover the preferred size of the element. /// </summary> /// <param name="context">Layout context.</param> public override Size GetPreferredSize(ViewLayoutContext context) { Debug.Assert(context != null); switch (CurrentSize) { case GroupItemSize.Small: _viewToSmall.Clear(); break; case GroupItemSize.Medium: _viewToMedium.Clear(); break; case GroupItemSize.Large: _viewToLarge.Clear(); break; default: // Should never happen! Debug.Assert(false); break; } Size preferredSize = Size.Empty; foreach (ViewBase child in this) { // Only investigate visible children if (child.Visible) { // Ask child for it's own preferred size Size childPreferred = child.GetPreferredSize(context); // Cache the child preferred size for use in layout switch (CurrentSize) { case GroupItemSize.Small: _viewToSmall.Add(child, childPreferred); break; case GroupItemSize.Medium: _viewToMedium.Add(child, childPreferred); break; case GroupItemSize.Large: _viewToLarge.Add(child, childPreferred); break; } // Always add on the width of the child preferredSize.Width += childPreferred.Width; // Find the tallest of the children preferredSize.Height = Math.Max(preferredSize.Height, childPreferred.Height); } } // Cache the size for the current item switch (CurrentSize) { case GroupItemSize.Small: _preferredSizeSmall = preferredSize; break; case GroupItemSize.Medium: _preferredSizeMedium = preferredSize; break; case GroupItemSize.Large: _preferredSizeLarge = preferredSize; break; } return(preferredSize); }