internal static void UpdateToolBarPosition(RadToolBarTray tray, RadToolBar toolBar, ToolBarPositionInfo positionInfo, bool allowNewBandCreation)
 {
     toolBar.Band = (allowNewBandCreation && positionInfo.NewBand.HasValue) ? positionInfo.NewBand.Value : (positionInfo.Band.HasValue ? positionInfo.Band.Value : 0);
     toolBar.BandIndex = positionInfo.BandIndex;
     for (int i = 0; i < positionInfo.BandIndex; i++)
     {
         positionInfo.ToolBars[i].BandIndex = i < positionInfo.BandIndex ? i : i + 1;
     }
 }
            private static Dictionary <int, List <RadToolBar> > GetBandsDict(RadToolBarTray tray)
            {
                Dictionary <int, List <RadToolBar> > bandsDict = new Dictionary <int, List <RadToolBar> >();

                foreach (RadToolBar toolBar in tray.Items)
                {
                    if (!bandsDict.ContainsKey(toolBar.Band))
                    {
                        bandsDict[toolBar.Band] = new List <RadToolBar>();
                    }

                    bandsDict[toolBar.Band].Add(toolBar);
                }
                return(bandsDict);
            }
        private static void OnTrayOwnerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }

            RadToolBarTray trayOwner = e.NewValue as RadToolBarTray;
            Border         border    = d as Border;

            if (trayOwner != null && border != null)
            {
                TrayToIndicatorBorderDict.Add(trayOwner, border);
            }
        }
        private static void TrayDraggedOver(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
        {
            RadToolBarTray tray = sender as RadToolBarTray;
            DragDropInfo   info = GetDragDropInfo(e.Data);

            if (tray == null || info == null)
            {
                return;
            }

            var  positionInfo         = BandsUtilities.CalculateToolBarPositionInfo(info.ToolBar, tray, e.GetPosition(tray));
            bool allowNewBandCreation = GetNewBandMode(tray) == NewBandMode.Live;

            BandsUtilities.UpdateToolBarPosition(tray, info.ToolBar, positionInfo, allowNewBandCreation);
            UpdateNewBandIndicator(info, positionInfo, tray);
        }
            internal static ToolBarPositionInfo CalculateToolBarPositionInfo(RadToolBar sourceToolBar, RadToolBarTray tray, Point mousePosition)
            {
                var band = CalculateBand(sourceToolBar, tray, mousePosition);

                List<RadToolBar> toolBars = new List<RadToolBar>();
                foreach (RadToolBar toolBar in tray.Items)
                {
                    if (toolBar != sourceToolBar && toolBar.Band == band.Band)
                    {
                        toolBars.Add(toolBar);
                    }
                }
                toolBars = toolBars.OrderBy(tb => tb.BandIndex).ToList();

                int bandIndex = CalculateBandIndex(tray.Orientation, sourceToolBar, toolBars, mousePosition);

                return new ToolBarPositionInfo(band.Band, band.NewBand, bandIndex, toolBars);
            }
        private static void InitializeHost(RadToolBar toolBar, double left, double top)
        {
            Border border = new Border();

            border.Background = new SolidColorBrush(new Color {
                A = 50, R = 127, G = 127, B = 127
            });
            border.CornerRadius = new CornerRadius(3);
            RadToolBarTray tray = new RadToolBarTray();

            tray.Orientation = toolBar.Orientation;
            tray.Items.Add(toolBar);
            border.Child = tray;
            double hMargin = DragDropManager.MinimumHorizontalDragDistance;
            double vMargin = DragDropManager.MinimumVerticalDragDistance;

            tray.Margin = new Thickness(hMargin, vMargin, hMargin, vMargin);
            DragDropManager.AddDragInitializeHandler(tray, TrayDragInitialized);
            FrameworkElement host = InitializeWindowHost(border, left - hMargin - 1, top - vMargin);

            trayToHostDict[tray] = host;
        }
        private static void OnIsDragDropEnabledChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
        {
            RadToolBarTray tray = (RadToolBarTray)target;

            if ((bool)args.OldValue)
            {
                DragDropManager.RemoveDragInitializeHandler(tray, TrayDragInitialized);
                DragDropManager.RemoveDragEnterHandler(tray, TrayDragEntered);
                DragDropManager.RemoveDragOverHandler(tray, TrayDraggedOver);
                DragDropManager.RemoveDragLeaveHandler(tray, TrayDragLeft);
                DragDropManager.RemoveDropHandler(tray, TrayDropped);
            }

            if ((bool)args.NewValue)
            {
                DragDropManager.AddDragInitializeHandler(tray, TrayDragInitialized);
                DragDropManager.AddDragEnterHandler(tray, TrayDragEntered);
                DragDropManager.AddDragOverHandler(tray, TrayDraggedOver);
                DragDropManager.AddDragLeaveHandler(tray, TrayDragLeft);
                DragDropManager.AddDropHandler(tray, TrayDropped);
            }
        }
        private static void TrayDragEntered(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
        {
            RadToolBarTray tray = sender as RadToolBarTray;
            DragDropInfo   info = GetDragDropInfo(e.Data);

            if (tray == null || info == null)
            {
                return;
            }

            lastInitializedInfo = null;

            if (!tray.Items.Contains(info.ToolBar))
            {
                var positionInfo = BandsUtilities.CalculateToolBarPositionInfo(info.ToolBar, tray, e.GetPosition(tray));
                MoveToolBarToTray(info, tray);
                bool allowNewBandCreation = GetNewBandMode(tray) == NewBandMode.Live;
                BandsUtilities.UpdateToolBarPosition(tray, info.ToolBar, positionInfo, allowNewBandCreation);
                UpdateNewBandIndicator(info, positionInfo, tray);
            }

            e.Handled = true;
        }
        private static void TrayDragInitialized(object sender, DragInitializeEventArgs e)
        {
            RadToolBarTray tray    = sender as RadToolBarTray;
            RadToolBar     toolBar = e.Source as RadToolBar;

            if (tray == null || toolBar == null)
            {
                return;
            }

            DragDropInfo info = new DragDropInfo(toolBar, tray);

            lastInitializedInfo = info;
            if (trayToHostDict.ContainsKey(tray) || !tray.IsMouseOver)
            {
                MoveToolBarToDragVisual(info, tray);
            }
            else
            {
                SetHitTesting(info.Tray.Items);
            }

            SetActiveToolBarStyle(info.ToolBar);
            if (trayToHostDict.ContainsKey(info.Tray))
            {
                trayToHostDict[info.Tray].Opacity = 0;
            }

            DragDropManager.AddDragDropCompletedHandler(info.ToolBar, ToolBarDragDropCompleted);
            DragDropManager.AddGiveFeedbackHandler(info.ToolBar, ToolBarGiveFeedback);

            e.Data             = info;
            e.DragVisual       = info.DragVisual;
            e.DragVisualOffset = e.RelativeStartPoint;
            e.AllowedEffects   = DragDropEffects.All;
            e.Handled          = true;
        }
        private static void TrayDragInitialized(object sender, DragInitializeEventArgs e)
        {
            RadToolBarTray tray    = sender as RadToolBarTray;
            RadToolBar     toolBar = e.Source as RadToolBar;

            if (tray == null || toolBar == null)
            {
                return;
            }

            DragDropInfo info = new DragDropInfo(toolBar, tray);

            lastInitializedInfo = info;

            SetActiveToolBarStyle(info.ToolBar);

            DragDropManager.AddDragDropCompletedHandler(info.ToolBar, ToolBarDragDropCompleted);

            e.DragVisual       = CreateDragImageFromToolBar(info.ToolBar);
            e.Data             = info;
            e.DragVisualOffset = e.RelativeStartPoint;
            e.AllowedEffects   = DragDropEffects.All;
            e.Handled          = true;
        }
 public static void SetTrayOwner(DependencyObject obj, RadToolBarTray value)
 {
     obj.SetValue(TrayOwnerProperty, value);
 }
        private static void UpdateNewBandIndicator(DragDropInfo info, BandsUtilities.ToolBarPositionInfo positionInfo, RadToolBarTray tray)
        {
            if (GetNewBandMode(tray) != NewBandMode.Indicator)
            {
                return;
            }

            if (positionInfo.NewBand.HasValue)
            {
                double margin = 3;
                int    band   = positionInfo.NewBand.Value;

                if (tray.Orientation == Orientation.Horizontal)
                {
                    ToolBarTrayUtilitiesSL.TrayToIndicatorBorderDict[tray].BorderThickness = new Thickness(0, band < 0 ? margin : 0, 0, band < 0 ? 0 : margin);
                }
                else
                {
                    ToolBarTrayUtilitiesSL.TrayToIndicatorBorderDict[tray].BorderThickness = new Thickness(band < 0 ? margin : 0, 0, band < 0 ? 0 : margin, 0);
                }
            }
            else
            {
                HideNewBandIndicator(tray);
            }
        }
 internal DragDropInfo(RadToolBar toolBar, RadToolBarTray tray)
 {
     this.ToolBar    = toolBar;
     this.Tray       = tray;
     this.DragVisual = new Border();
 }
            internal static ToolBarPositionInfo CalculateToolBarPositionInfo(RadToolBar sourceToolBar, RadToolBarTray tray, Point mousePosition)
            {
                var band = CalculateBand(sourceToolBar, tray, mousePosition);

                List <RadToolBar> toolBars = new List <RadToolBar>();

                foreach (RadToolBar toolBar in tray.Items)
                {
                    if (toolBar != sourceToolBar && toolBar.Band == band.Band)
                    {
                        toolBars.Add(toolBar);
                    }
                }
                toolBars = toolBars.OrderBy(tb => tb.BandIndex).ToList();

                int bandIndex = CalculateBandIndex(tray.Orientation, sourceToolBar, toolBars, mousePosition);

                return(new ToolBarPositionInfo(band.Band, band.NewBand, bandIndex, toolBars));
            }
 private static void MoveToolBarToDragVisual(DragDropInfo info, RadToolBarTray tray)
 {
     tray.Items.Remove(info.ToolBar);
     info.DragVisual.Child = info.ToolBar;
 }
            private static BandInfo CalculateBand(RadToolBar toolBar, RadToolBarTray tray, Point mousePosition)
            {
                double position = tray.Orientation == Orientation.Horizontal ? mousePosition.Y : mousePosition.X;
                double newBandVicinity = 6;

                int? newBand = null;
                if (position < newBandVicinity)
                {
                    // Create new band, to left or to top.
                    newBand = -1;
                }

                var bands = GetBandsDict(tray);
                double trayLength = tray.Orientation == Orientation.Horizontal ? tray.ActualHeight : tray.ActualWidth;

                if (trayLength - newBandVicinity < position)
                {
                    // Create new band, to right or to bottom.
                    newBand = bands.Keys.Count;
                }

                Func<RadToolBar, double> length = tb => tray.Orientation == Orientation.Horizontal ?
                    (tb.ActualHeight + tb.Margin.Top + tb.Margin.Bottom) :
                    (tb.ActualWidth + tb.Margin.Left + tb.Margin.Right);
                List<int> keys = bands.Keys.ToList();
                int? existingBand = null;
                double currentPosition = 0;

                for (int i = 0; i < keys.Count; i++)
                {
                    existingBand = keys[i];
                    double bandLength = length(bands[keys[i]][0]);
                    if (position <= currentPosition + bandLength)
                    {
                        if (keys[i] == 1 &&
                            bands[keys[0]].Count == 1 &&
                            bands[keys[0]][0] == toolBar &&
                            position < currentPosition + (1 * newBandVicinity))
                        {
                            // Avoid flicker when the dragged toolbar is the only element in the first band.
                            existingBand = 0;
                        }
                        else if (keys[i] == keys.Count - 2 &&
                            bands[keys[keys.Count - 1]].Count == 1 &&
                            bands[keys[keys.Count - 1]][0] == toolBar &&
                            currentPosition + bandLength - (1 * newBandVicinity) < position)
                        {
                            // Avoid flicker when the dragged toolbar is the only element in the last band.
                            existingBand = keys.Count - 1;
                        }

                        break;
                    }

                    currentPosition += bandLength;
                }

                if (newBand.HasValue && existingBand.HasValue && bands[existingBand.Value].Count == 1 && bands[existingBand.Value][0] == toolBar)
                {
                    // A new band will not be created because the dragged bar is the only element in the existing band.
                    newBand = null;
                }

                return new BandInfo { Band = existingBand, NewBand = newBand };
            }
            private static SortedDictionary<int, List<RadToolBar>> GetBandsDict(RadToolBarTray tray)
            {
                SortedDictionary<int, List<RadToolBar>> bandsDict = new SortedDictionary<int, List<RadToolBar>>();
                foreach (RadToolBar toolBar in tray.Items)
                {
                    if (!bandsDict.ContainsKey(toolBar.Band))
                    {
                        bandsDict[toolBar.Band] = new List<RadToolBar>();
                    }

                    bandsDict[toolBar.Band].Add(toolBar);
                }
                return bandsDict;
            }
        private static void MoveToolBarToTray(DragDropInfo info, RadToolBarTray tray)
        {
            info.Tray.Items.Remove(info.ToolBar);
            tray.Items.Add(info.ToolBar);
            info.Tray = tray;

            info.DragVisual.Child = null;

            // Prevents Flickering when dragging over tray and the mouse is over.
            info.ToolBar.IsHitTestVisible = false;
        }
 public static void SetTrayOwner(DependencyObject obj, RadToolBarTray value)
 {
     obj.SetValue(TrayOwnerProperty, value);
 }
 internal DragDropInfo(RadToolBar toolBar, RadToolBarTray tray)
 {
     this.ToolBar = toolBar;
     this.Tray = tray;
     this.DragVisual = new Border();
 }
 private static void MoveToolBarToTray(DragDropInfo info, RadToolBarTray tray)
 {
     info.DragVisual.Child = null;
     info.Tray.Items.Remove(info.ToolBar);
     tray.Items.Add(info.ToolBar);
 }
        private static void HideNewBandIndicator(RadToolBarTray tray)
        {
            if (GetNewBandMode(tray) != NewBandMode.Indicator)
            {
                return;
            }

            ToolBarTrayUtilities.TrayToIndicatorBorderDict[tray].BorderThickness = new Thickness();
        }
        private static void UpdateNewBandIndicator(DragDropInfo info, BandsUtilities.ToolBarPositionInfo positionInfo, RadToolBarTray tray)
        {
            if (GetNewBandMode(tray) != NewBandMode.Indicator)
            {
                return;
            }

            if (positionInfo.NewBand.HasValue)
            {
                double margin = 3;
                int band = positionInfo.NewBand.Value;

                if (tray.Orientation == Orientation.Horizontal)
                {
                    ToolBarTrayUtilities.TrayToIndicatorBorderDict[tray].BorderThickness = new Thickness(0, band < 0 ? margin : 0, 0, band < 0 ? 0 : margin);
                }
                else
                {
                    ToolBarTrayUtilities.TrayToIndicatorBorderDict[tray].BorderThickness = new Thickness(band < 0 ? margin : 0, 0, band < 0 ? 0 : margin, 0);
                }
            }
            else
            {
                HideNewBandIndicator(tray);
            }
        }
 private static void MoveToolBarToDragVisual(DragDropInfo info, RadToolBarTray tray)
 {
     tray.Items.Remove(info.ToolBar);
     info.DragVisual.Child = info.ToolBar;
 }
            private static BandInfo CalculateBand(RadToolBar toolBar, RadToolBarTray tray, Point mousePosition)
            {
                double position        = tray.Orientation == Orientation.Horizontal ? mousePosition.Y : mousePosition.X;
                double newBandVicinity = 6;

                int?newBand = null;

                if (position < newBandVicinity)
                {
                    // Create new band, to left or to top.
                    newBand = -1;
                }

                var    bands      = GetBandsDict(tray);
                double trayLength = tray.Orientation == Orientation.Horizontal ? tray.ActualHeight : tray.ActualWidth;

                if (trayLength - newBandVicinity < position)
                {
                    // Create new band, to right or to bottom.
                    newBand = bands.Keys.Count;
                }

                Func <RadToolBar, double> length = tb => tray.Orientation == Orientation.Horizontal ?
                                                   (tb.ActualHeight + tb.Margin.Top + tb.Margin.Bottom) :
                                                   (tb.ActualWidth + tb.Margin.Left + tb.Margin.Right);
                List <int> keys            = bands.Keys.ToList();
                int?       existingBand    = null;
                double     currentPosition = 0;

                for (int i = 0; i < keys.Count; i++)
                {
                    existingBand = keys[i];
                    double bandLength = length(bands[keys[i]][0]);
                    if (position <= currentPosition + bandLength)
                    {
                        if (keys[i] == 1 &&
                            bands[keys[0]].Count == 1 &&
                            bands[keys[0]][0] == toolBar &&
                            position < currentPosition + (1 * newBandVicinity))
                        {
                            // Avoid flicker when the dragged toolbar is the only element in the first band.
                            existingBand = 0;
                        }
                        else if (keys[i] == keys.Count - 2 &&
                                 bands[keys[keys.Count - 1]].Count == 1 &&
                                 bands[keys[keys.Count - 1]][0] == toolBar &&
                                 currentPosition + bandLength - (1 * newBandVicinity) < position)
                        {
                            // Avoid flicker when the dragged toolbar is the only element in the last band.
                            existingBand = keys.Count - 1;
                        }

                        break;
                    }

                    currentPosition += bandLength;
                }

                if (newBand.HasValue && existingBand.HasValue && bands[existingBand.Value].Count == 1 && bands[existingBand.Value][0] == toolBar)
                {
                    // A new band will not be created because the dragged bar is the only element in the existing band.
                    newBand = null;
                }

                return(new BandInfo {
                    Band = existingBand, NewBand = newBand
                });
            }
 private static void InitializeHost(RadToolBar toolBar, double left, double top)
 {
     Border border = new Border();
     border.Background = new SolidColorBrush(new Color { A = 50, R = 127, G = 127, B = 127 });
     border.CornerRadius = new CornerRadius(3);
     RadToolBarTray tray = new RadToolBarTray();
     tray.Orientation = toolBar.Orientation;
     tray.Items.Add(toolBar);
     border.Child = tray;
     double hMargin = DragDropManager.MinimumHorizontalDragDistance;
     double vMargin = DragDropManager.MinimumVerticalDragDistance;
     tray.Margin = new Thickness(hMargin, vMargin, hMargin, vMargin);
     DragDropManager.AddDragInitializeHandler(tray, TrayDragInitialized);
     FrameworkElement host = InitializeWindowHost(border, left - hMargin - 1, top - vMargin);
     trayToHostDict[tray] = host;
 }
 private static void MoveToolBarToTray(DragDropInfo info, RadToolBarTray tray)
 {
     info.DragVisual.Child = null;
     info.Tray.Items.Remove(info.ToolBar);
     tray.Items.Add(info.ToolBar);
 }