private void UpdateKeyTipPositions()
        {
            this.LogDebug("UpdateKeyTipPositions");

            if (this.keyTipInformations.Count == 0)
            {
                return;
            }

            double[] rows     = null;
            var      groupBox = this.oneOfAssociatedElements as RibbonGroupBox ?? UIHelper.GetParent <RibbonGroupBox>(this.oneOfAssociatedElements);
            var      panel    = groupBox?.GetPanel();

            if (panel != null)
            {
                var height = groupBox.GetLayoutRoot().DesiredSize.Height;
                rows = new[]
                {
                    groupBox.GetLayoutRoot().TranslatePoint(new Point(0, 0), this.AdornedElement).Y,
                    groupBox.GetLayoutRoot().TranslatePoint(new Point(0, panel.DesiredSize.Height / 2.0), this.AdornedElement).Y,
                    groupBox.GetLayoutRoot().TranslatePoint(new Point(0, panel.DesiredSize.Height), this.AdornedElement).Y,
                    groupBox.GetLayoutRoot().TranslatePoint(new Point(0, height + 1), this.AdornedElement).Y
                };
            }

            foreach (var keyTipInformation in this.keyTipInformations)
            {
                // Skip invisible keytips
                if (keyTipInformation.Visibility != Visibility.Visible)
                {
                    continue;
                }

                // Update KeyTip Visibility
                var visualTargetIsVisible    = keyTipInformation.VisualTarget.IsVisible;
                var visualTargetInVisualTree = VisualTreeHelper.GetParent(keyTipInformation.VisualTarget) != null;
                keyTipInformation.Visibility = visualTargetIsVisible && visualTargetInVisualTree ? Visibility.Visible : Visibility.Collapsed;

                keyTipInformation.KeyTip.Margin = KeyTip.GetMargin(keyTipInformation.AssociatedElement);

                if (IsWithinQuickAccessToolbar(keyTipInformation.AssociatedElement))
                {
                    var x = (keyTipInformation.VisualTarget.DesiredSize.Width / 2.0) - (keyTipInformation.KeyTip.DesiredSize.Width / 2.0);
                    var y = keyTipInformation.VisualTarget.DesiredSize.Height - (keyTipInformation.KeyTip.DesiredSize.Height / 2.0);

                    if (KeyTip.GetAutoPlacement(keyTipInformation.AssociatedElement) == false)
                    {
                        switch (KeyTip.GetHorizontalAlignment(keyTipInformation.AssociatedElement))
                        {
                        case HorizontalAlignment.Left:
                            x = 0;
                            break;

                        case HorizontalAlignment.Right:
                            x = keyTipInformation.VisualTarget.DesiredSize.Width - keyTipInformation.KeyTip.DesiredSize.Width;
                            break;
                        }
                    }

                    keyTipInformation.Position = keyTipInformation.VisualTarget.TranslatePoint(new Point(x, y), this.AdornedElement);
                }
                else if (keyTipInformation.AssociatedElement.Name == "PART_DialogLauncherButton")
                {
                    // Dialog Launcher Button Exclusive Placement
                    var keyTipSize  = keyTipInformation.KeyTip.DesiredSize;
                    var elementSize = keyTipInformation.VisualTarget.RenderSize;
                    if (rows == null)
                    {
                        continue;
                    }

                    keyTipInformation.Position = keyTipInformation.VisualTarget.TranslatePoint(new Point(
                                                                                                   (elementSize.Width / 2.0) - (keyTipSize.Width / 2.0),
                                                                                                   0), this.AdornedElement);
                    keyTipInformation.Position = new Point(keyTipInformation.Position.X, rows[3]);
                }
                else if (KeyTip.GetAutoPlacement(keyTipInformation.AssociatedElement) == false)
                {
                    var keyTipSize = keyTipInformation.KeyTip.DesiredSize;

                    var elementSize = keyTipInformation.VisualTarget.RenderSize;

                    double x = 0, y = 0;

                    switch (KeyTip.GetHorizontalAlignment(keyTipInformation.AssociatedElement))
                    {
                    case HorizontalAlignment.Left:
                        break;

                    case HorizontalAlignment.Right:
                        x = elementSize.Width - keyTipSize.Width;
                        break;

                    case HorizontalAlignment.Center:
                    case HorizontalAlignment.Stretch:
                        x = (elementSize.Width / 2.0) - (keyTipSize.Width / 2.0);
                        break;
                    }

                    switch (KeyTip.GetVerticalAlignment(keyTipInformation.AssociatedElement))
                    {
                    case VerticalAlignment.Top:
                        break;

                    case VerticalAlignment.Bottom:
                        y = elementSize.Height - keyTipSize.Height;
                        break;

                    case VerticalAlignment.Center:
                    case VerticalAlignment.Stretch:
                        y = (elementSize.Height / 2.0) - (keyTipSize.Height / 2.0);
                        break;
                    }

                    keyTipInformation.Position = keyTipInformation.VisualTarget.TranslatePoint(new Point(x, y), this.AdornedElement);
                }
                else if (keyTipInformation.AssociatedElement is InRibbonGallery && !((InRibbonGallery)keyTipInformation.AssociatedElement).IsCollapsed)
                {
                    // InRibbonGallery Exclusive Placement
                    var keyTipSize  = keyTipInformation.KeyTip.DesiredSize;
                    var elementSize = keyTipInformation.VisualTarget.RenderSize;
                    if (rows == null)
                    {
                        continue;
                    }

                    keyTipInformation.Position = keyTipInformation.VisualTarget.TranslatePoint(new Point(
                                                                                                   elementSize.Width - (keyTipSize.Width / 2.0),
                                                                                                   0), this.AdornedElement);
                    keyTipInformation.Position = new Point(keyTipInformation.Position.X, rows[2] - (keyTipSize.Height / 2));
                }
                else if (keyTipInformation.AssociatedElement is RibbonTabItem || keyTipInformation.AssociatedElement is Backstage)
                {
                    // Ribbon Tab Item Exclusive Placement
                    var keyTipSize  = keyTipInformation.KeyTip.DesiredSize;
                    var elementSize = keyTipInformation.VisualTarget.RenderSize;
                    keyTipInformation.Position = keyTipInformation.VisualTarget.TranslatePoint(new Point(
                                                                                                   (elementSize.Width / 2.0) - (keyTipSize.Width / 2.0),
                                                                                                   elementSize.Height - (keyTipSize.Height / 2.0)), this.AdornedElement);
                }
                else if (keyTipInformation.AssociatedElement is MenuItem)
                {
                    // MenuItem Exclusive Placement
                    var elementSize = keyTipInformation.VisualTarget.DesiredSize;
                    keyTipInformation.Position = keyTipInformation.VisualTarget.TranslatePoint(
                        new Point(
                            (elementSize.Height / 3.0) + 2,
                            (elementSize.Height / 4.0) + 2), this.AdornedElement);
                }
                else if (keyTipInformation.AssociatedElement.Parent is BackstageTabControl)
                {
                    // Backstage Items Exclusive Placement
                    var keyTipSize       = keyTipInformation.KeyTip.DesiredSize;
                    var elementSize      = keyTipInformation.VisualTarget.DesiredSize;
                    var parent           = (UIElement)keyTipInformation.VisualTarget.Parent;
                    var positionInParent = keyTipInformation.VisualTarget.TranslatePoint(default(Point), parent);
                    keyTipInformation.Position = parent.TranslatePoint(
                        new Point(
                            5,
                            positionInParent.Y + ((elementSize.Height / 2.0) - keyTipSize.Height)), this.AdornedElement);
                }
                else
                {
                    if (RibbonProperties.GetSize(keyTipInformation.AssociatedElement) != RibbonControlSize.Large ||
                        IsTextBoxShapedControl(keyTipInformation.AssociatedElement))
                    {
                        var withinRibbonToolbar = IsWithinRibbonToolbarInTwoLine(keyTipInformation.VisualTarget);
                        var x               = keyTipInformation.KeyTip.DesiredSize.Width / 2.0;
                        var y               = keyTipInformation.KeyTip.DesiredSize.Height / 2.0;
                        var point           = new Point(x, y);
                        var translatedPoint = keyTipInformation.VisualTarget.TranslatePoint(point, this.AdornedElement);

                        // Snapping to rows if it present
                        if (rows != null)
                        {
                            var index       = 0;
                            var mindistance = Math.Abs(rows[0] - translatedPoint.Y);
                            for (var j = 1; j < rows.Length; j++)
                            {
                                if (withinRibbonToolbar &&
                                    j == 1)
                                {
                                    continue;
                                }

                                var distance = Math.Abs(rows[j] - translatedPoint.Y);
                                if (distance < mindistance)
                                {
                                    mindistance = distance;
                                    index       = j;
                                }
                            }

                            translatedPoint.Y = rows[index] - (keyTipInformation.KeyTip.DesiredSize.Height / 2.0);
                        }

                        keyTipInformation.Position = translatedPoint;
                    }
                    else
                    {
                        var x               = (keyTipInformation.VisualTarget.DesiredSize.Width / 2.0) - (keyTipInformation.KeyTip.DesiredSize.Width / 2.0);
                        var y               = keyTipInformation.VisualTarget.DesiredSize.Height - 8;
                        var point           = new Point(x, y);
                        var translatedPoint = keyTipInformation.VisualTarget.TranslatePoint(point, this.AdornedElement);

                        if (rows != null)
                        {
                            translatedPoint.Y = rows[2] - (keyTipInformation.KeyTip.DesiredSize.Height / 2.0);
                        }

                        keyTipInformation.Position = translatedPoint;
                    }
                }
            }
        }
Beispiel #2
0
        private void UpdateKeyTipPositions()
        {
            this.Log("UpdateKeyTipPositions");

            if (this.keyTips.Count == 0)
            {
                return;
            }

            double[] rows     = null;
            var      groupBox = GetGroupBox(this.oneOfAssociatedElements);

            if (groupBox != null)
            {
                var panel = groupBox.GetPanel();
                if (panel != null)
                {
                    var height = groupBox.GetLayoutRoot().DesiredSize.Height;
                    rows = new[]
                    {
                        groupBox.GetLayoutRoot().TranslatePoint(new Point(0, 0), this.AdornedElement).Y,
                        groupBox.GetLayoutRoot().TranslatePoint(new Point(0, panel.DesiredSize.Height / 2.0), this.AdornedElement).Y,
                        groupBox.GetLayoutRoot().TranslatePoint(new Point(0, panel.DesiredSize.Height), this.AdornedElement).Y,
                        groupBox.GetLayoutRoot().TranslatePoint(new Point(0, height + 1), this.AdornedElement).Y
                    };
                }
            }

            for (var i = 0; i < this.keyTips.Count; i++)
            {
                // Skip invisible keytips
                if (this.keyTips[i].Visibility != Visibility.Visible)
                {
                    continue;
                }

                // Update KeyTip Visibility
                var associatedElementIsVisible    = this.associatedElements[i].IsVisible;
                var associatedElementInVisualTree = VisualTreeHelper.GetParent(this.associatedElements[i]) != null;
                this.keyTips[i].Visibility = associatedElementIsVisible && associatedElementInVisualTree ? Visibility.Visible : Visibility.Collapsed;

                if (!KeyTip.GetAutoPlacement(this.associatedElements[i]))
                {
                    #region Custom Placement

                    var keyTipSize  = this.keyTips[i].DesiredSize;
                    var elementSize = this.associatedElements[i].RenderSize;

                    double x = 0, y = 0;
                    var    margin = KeyTip.GetMargin(this.associatedElements[i]);

                    switch (KeyTip.GetHorizontalAlignment(this.associatedElements[i]))
                    {
                    case HorizontalAlignment.Left:
                        x = margin.Left;
                        break;

                    case HorizontalAlignment.Right:
                        x = elementSize.Width - keyTipSize.Width - margin.Right;
                        break;

                    case HorizontalAlignment.Center:
                    case HorizontalAlignment.Stretch:
                        x = elementSize.Width / 2.0 - keyTipSize.Width / 2.0 + margin.Left;
                        break;
                    }

                    switch (KeyTip.GetVerticalAlignment(this.associatedElements[i]))
                    {
                    case VerticalAlignment.Top:
                        y = margin.Top;
                        break;

                    case VerticalAlignment.Bottom:
                        y = elementSize.Height - keyTipSize.Height - margin.Bottom;
                        break;

                    case VerticalAlignment.Center:
                    case VerticalAlignment.Stretch:
                        y = elementSize.Height / 2.0 - keyTipSize.Height / 2.0 + margin.Top;
                        break;
                    }

                    this.keyTipPositions[i] = this.associatedElements[i].TranslatePoint(new Point(x, y), this.AdornedElement);

                    #endregion
                }
                else if (((FrameworkElement)this.associatedElements[i]).Name == "PART_DialogLauncherButton")
                {
                    // Dialog Launcher Button Exclusive Placement
                    var keyTipSize  = this.keyTips[i].DesiredSize;
                    var elementSize = this.associatedElements[i].RenderSize;
                    if (rows == null)
                    {
                        continue;
                    }

                    this.keyTipPositions[i] = this.associatedElements[i].TranslatePoint(new Point(
                                                                                            elementSize.Width / 2.0 - keyTipSize.Width / 2.0,
                                                                                            0), this.AdornedElement);
                    this.keyTipPositions[i].Y = rows[3];
                }
                else if ((this.associatedElements[i] is InRibbonGallery && !((InRibbonGallery)this.associatedElements[i]).IsCollapsed))
                {
                    // InRibbonGallery Exclusive Placement
                    var keyTipSize  = this.keyTips[i].DesiredSize;
                    var elementSize = this.associatedElements[i].RenderSize;
                    if (rows == null)
                    {
                        continue;
                    }

                    this.keyTipPositions[i] = this.associatedElements[i].TranslatePoint(new Point(
                                                                                            elementSize.Width - keyTipSize.Width / 2.0,
                                                                                            0), this.AdornedElement);
                    this.keyTipPositions[i].Y = rows[2] - keyTipSize.Height / 2;
                }
                else if ((this.associatedElements[i] is RibbonTabItem) || (this.associatedElements[i] is Backstage))
                {
                    // Ribbon Tab Item Exclusive Placement
                    var keyTipSize  = this.keyTips[i].DesiredSize;
                    var elementSize = this.associatedElements[i].RenderSize;
                    this.keyTipPositions[i] = this.associatedElements[i].TranslatePoint(new Point(
                                                                                            elementSize.Width / 2.0 - keyTipSize.Width / 2.0,
                                                                                            elementSize.Height - keyTipSize.Height / 2.0), this.AdornedElement);
                }
                else if (this.associatedElements[i] is RibbonGroupBox)
                {
                    // Ribbon Group Box Exclusive Placement
                    var keyTipSize  = this.keyTips[i].DesiredSize;
                    var elementSize = this.associatedElements[i].DesiredSize;
                    this.keyTipPositions[i] = this.associatedElements[i].TranslatePoint(new Point(
                                                                                            elementSize.Width / 2.0 - keyTipSize.Width / 2.0,
                                                                                            elementSize.Height + 1), this.AdornedElement);
                }
                else if (IsWithinQuickAccessToolbar(this.associatedElements[i]))
                {
                    var translatedPoint = this.associatedElements[i].TranslatePoint(new Point(this.associatedElements[i].DesiredSize.Width / 2.0 - this.keyTips[i].DesiredSize.Width / 2.0, this.associatedElements[i].DesiredSize.Height - this.keyTips[i].DesiredSize.Height / 2.0), this.AdornedElement);
                    this.keyTipPositions[i] = translatedPoint;
                }
                else if (this.associatedElements[i] is MenuItem)
                {
                    // MenuItem Exclusive Placement
                    var keyTipSize  = this.keyTips[i].DesiredSize;
                    var elementSize = this.associatedElements[i].DesiredSize;
                    this.keyTipPositions[i] = this.associatedElements[i].TranslatePoint(
                        new Point(
                            elementSize.Height / 2.0 + 2,
                            elementSize.Height / 2.0 + 2), this.AdornedElement);
                }
                else if (this.associatedElements[i] is BackstageTabItem)
                {
                    // BackstageButton Exclusive Placement
                    var keyTipSize  = this.keyTips[i].DesiredSize;
                    var elementSize = this.associatedElements[i].DesiredSize;
                    this.keyTipPositions[i] = this.associatedElements[i].TranslatePoint(
                        new Point(
                            5,
                            elementSize.Height / 2.0 - keyTipSize.Height), this.AdornedElement);
                }
                else if (((FrameworkElement)this.associatedElements[i]).Parent is BackstageTabControl)
                {
                    // Backstage Items Exclusive Placement
                    var keyTipSize  = this.keyTips[i].DesiredSize;
                    var elementSize = this.associatedElements[i].DesiredSize;
                    this.keyTipPositions[i] = this.associatedElements[i].TranslatePoint(
                        new Point(
                            20,
                            elementSize.Height / 2.0 - keyTipSize.Height), this.AdornedElement);
                }
                else
                {
                    if ((RibbonProperties.GetSize(this.associatedElements[i]) != RibbonControlSize.Large) ||
                        (this.associatedElements[i] is Spinner) ||
                        (this.associatedElements[i] is ComboBox) ||
                        (this.associatedElements[i] is TextBox) ||
                        (this.associatedElements[i] is CheckBox))
                    {
                        var withinRibbonToolbar = IsWithinRibbonToolbarInTwoLine(this.associatedElements[i]);
                        var translatedPoint     = this.associatedElements[i].TranslatePoint(new Point(this.keyTips[i].DesiredSize.Width / 2.0, this.keyTips[i].DesiredSize.Height / 2.0), this.AdornedElement);

                        // Snapping to rows if it present
                        if (rows != null)
                        {
                            var index       = 0;
                            var mindistance = Math.Abs(rows[0] - translatedPoint.Y);
                            for (var j = 1; j < rows.Length; j++)
                            {
                                if (withinRibbonToolbar && j == 1)
                                {
                                    continue;
                                }

                                var distance = Math.Abs(rows[j] - translatedPoint.Y);
                                if (distance < mindistance)
                                {
                                    mindistance = distance;
                                    index       = j;
                                }
                            }

                            translatedPoint.Y = rows[index] - this.keyTips[i].DesiredSize.Height / 2.0;
                        }

                        this.keyTipPositions[i] = translatedPoint;
                    }
                    else
                    {
                        var translatedPoint = this.associatedElements[i].TranslatePoint(new Point(this.associatedElements[i].DesiredSize.Width / 2.0 - this.keyTips[i].DesiredSize.Width / 2.0, this.associatedElements[i].DesiredSize.Height - 8), this.AdornedElement);
                        if (rows != null)
                        {
                            translatedPoint.Y = rows[2] - this.keyTips[i].DesiredSize.Height / 2.0;
                        }

                        this.keyTipPositions[i] = translatedPoint;
                    }
                }
            }
        }
Beispiel #3
0
        private void UpdateKeyTipPositions()
        {
            this.LogDebug("UpdateKeyTipPositions");

            if (this.keyTipInformations.Count == 0)
            {
                return;
            }

            double[] rows     = null;
            var      groupBox = this.oneOfAssociatedElements as RibbonGroupBox ?? UIHelper.GetParent <RibbonGroupBox>(this.oneOfAssociatedElements);
            var      panel    = groupBox?.GetPanel();

            if (panel != null)
            {
                var height = groupBox.GetLayoutRoot().DesiredSize.Height;
                rows = new[]
                {
                    groupBox.GetLayoutRoot().TranslatePoint(new Point(0, 0), this.AdornedElement).Y,
                    groupBox.GetLayoutRoot().TranslatePoint(new Point(0, panel.DesiredSize.Height / 2.0), this.AdornedElement).Y,
                    groupBox.GetLayoutRoot().TranslatePoint(new Point(0, panel.DesiredSize.Height), this.AdornedElement).Y,
                    groupBox.GetLayoutRoot().TranslatePoint(new Point(0, height + 1), this.AdornedElement).Y
                };
            }

            foreach (var keyTipInformation in this.keyTipInformations)
            {
                // Skip invisible keytips
                if (keyTipInformation.Visibility != Visibility.Visible)
                {
                    continue;
                }

                // Update KeyTip Visibility
                var visualTargetIsVisible    = keyTipInformation.VisualTarget.IsVisible;
                var visualTargetInVisualTree = VisualTreeHelper.GetParent(keyTipInformation.VisualTarget) != null;
                keyTipInformation.Visibility = visualTargetIsVisible && visualTargetInVisualTree ? Visibility.Visible : Visibility.Collapsed;

                keyTipInformation.KeyTip.Margin = KeyTip.GetMargin(keyTipInformation.AssociatedElement);

                if (IsWithinQuickAccessToolbar(keyTipInformation.AssociatedElement))
                {
                    var x = (keyTipInformation.VisualTarget.DesiredSize.Width / 2.0) - (keyTipInformation.KeyTip.DesiredSize.Width / 2.0);
                    var y = keyTipInformation.VisualTarget.DesiredSize.Height - (keyTipInformation.KeyTip.DesiredSize.Height / 2.0);

                    if (KeyTip.GetAutoPlacement(keyTipInformation.AssociatedElement) == false)
                    {
                        switch (KeyTip.GetHorizontalAlignment(keyTipInformation.AssociatedElement))
                        {
                        case HorizontalAlignment.Left:
                            x = 0;
                            break;

                        case HorizontalAlignment.Right:
                            x = keyTipInformation.VisualTarget.DesiredSize.Width - keyTipInformation.KeyTip.DesiredSize.Width;
                            break;
                        }
                    }

                    keyTipInformation.Position = keyTipInformation.VisualTarget.TranslatePoint(new Point(x, y), this.AdornedElement);
                }
                else if (keyTipInformation.AssociatedElement.Name == "PART_DialogLauncherButton")
                {
                    // Dialog Launcher Button Exclusive Placement
                    var keyTipSize  = keyTipInformation.KeyTip.DesiredSize;
                    var elementSize = keyTipInformation.VisualTarget.RenderSize;
                    if (rows is null)
                    {
                        continue;
                    }

                    keyTipInformation.Position = keyTipInformation.VisualTarget.TranslatePoint(new Point(
                                                                                                   (elementSize.Width / 2.0) - (keyTipSize.Width / 2.0),
                                                                                                   0), this.AdornedElement);
                    keyTipInformation.Position = new Point(keyTipInformation.Position.X, rows[3]);
                }
                else if (KeyTip.GetAutoPlacement(keyTipInformation.AssociatedElement) == false)
                {
                    var keyTipSize = keyTipInformation.KeyTip.DesiredSize;

                    var elementSize = keyTipInformation.VisualTarget.RenderSize;

                    double x = 0, y = 0;

                    switch (KeyTip.GetHorizontalAlignment(keyTipInformation.AssociatedElement))
                    {
                    case HorizontalAlignment.Left:
                        break;

                    case HorizontalAlignment.Right:
                        x = elementSize.Width - keyTipSize.Width;
                        break;

                    case HorizontalAlignment.Center:
                    case HorizontalAlignment.Stretch:
                        x = (elementSize.Width / 2.0) - (keyTipSize.Width / 2.0);
                        break;
                    }

                    switch (KeyTip.GetVerticalAlignment(keyTipInformation.AssociatedElement))
                    {
                    case VerticalAlignment.Top:
                        break;

                    case VerticalAlignment.Bottom:
                        y = elementSize.Height - keyTipSize.Height;
                        break;

                    case VerticalAlignment.Center:
                    case VerticalAlignment.Stretch:
                        y = (elementSize.Height / 2.0) - (keyTipSize.Height / 2.0);
                        break;
                    }

                    keyTipInformation.Position = keyTipInformation.VisualTarget.TranslatePoint(new Point(x, y), this.AdornedElement);
                }
                else if (keyTipInformation.AssociatedElement is InRibbonGallery gallery &&
                         gallery.IsCollapsed == false)
                {
                    // InRibbonGallery Exclusive Placement
                    var keyTipSize  = keyTipInformation.KeyTip.DesiredSize;
                    var elementSize = keyTipInformation.VisualTarget.RenderSize;
                    if (rows is null)
                    {
                        continue;
                    }

                    keyTipInformation.Position = keyTipInformation.VisualTarget.TranslatePoint(new Point(
                                                                                                   elementSize.Width - (keyTipSize.Width / 2.0),
                                                                                                   0), this.AdornedElement);
                    keyTipInformation.Position = new Point(keyTipInformation.Position.X, rows[2] - (keyTipSize.Height / 2));
                }