Пример #1
0
        /// <summary>
        /// Execute the placement
        /// </summary>
        /// <param name="tablecontrol"></param>
        /// <param name="columns"></param>
        /// <param name="dx"></param>
        /// <param name="newRect"></param>
        /// <param name="considerEditors"></param>
        /// <param name="maxColumnWithEditor"></param>
        /// <param name="tablePlacementStrategy"></param>
        public void ExecutePlacement(TableControl tablecontrol, List <ILogicalColumn> columns, int dx, Rectangle newRect, bool considerEditors, int maxColumnWithEditor, ITablePlacementStrategy tablePlacementStrategy)
        {
            int colNum = 0;

            // compute number of columns for placement
            for (int i = 0; i < columns.Count; i++)
            {
                ILogicalColumn column = columns[i];
                if (dx <= 0 && tablePlacementStrategy.GetColumnWidth(column) == 0)
                {
                    continue;
                }
                if (PlacementAllowed(column, tablePlacementStrategy.ShouldApplyPlacementToHiddenColumns(ShouldApplyPlacementToHiddenColumns)))
                {
                    colNum++;
                }
            }

            // Handle Column Placement
            if (colNum != 0)
            {
                int  colDx       = dx / colNum; // dx to move a column
                int  rem         = Math.Abs(dx) - Math.Abs(colDx) * colNum;
                int  addOn       = 0;
                bool secondRound = false;

                for (int i = columns.Count - 1; i >= 0; i--)
                {
                    ILogicalColumn column = columns[i];

                    int colWidth = tablePlacementStrategy.GetColumnWidth(column);

                    if (!PlacementAllowed(column, tablePlacementStrategy.ShouldApplyPlacementToHiddenColumns(ShouldApplyPlacementToHiddenColumns)))
                    {
                        continue;
                    }
                    if (colDx < 0 && colWidth == 0)
                    {
                        continue;
                    }
                    if (colDx < 0 && (colWidth < Math.Abs(colDx) + Math.Abs(addOn)) && colWidth > 0)
                    {
                        // the column width is less then the amount need to shrink it, pass the remainder of ColMoveDX to
                        // the next col's.
                        addOn += colWidth + colDx; // save the remainder
                        column.setWidthOnPlacement(0, false);
                        secondRound = true;
                    }
                    else
                    {
                        column.setWidthOnPlacement(Math.Max(0, colWidth + colDx + addOn), false);
                        secondRound = false;
                        addOn       = 0;
                    }
                }
                if (secondRound)
                {
                    for (int i = columns.Count - 1; i >= 0; i--)
                    {
                        ILogicalColumn column   = columns[i];
                        int            colWidth = column.getWidth();
                        if (addOn < 0 && PlacementAllowed(column, tablePlacementStrategy.ShouldApplyPlacementToHiddenColumns(ShouldApplyPlacementToHiddenColumns)) && colWidth > 0)
                        {
                            if (colWidth < Math.Abs(addOn))
                            {
                                addOn += colWidth;
                                column.setWidthOnPlacement(0, false);
                            }
                            else
                            {
                                column.setWidthOnPlacement(colWidth + addOn, false);
                                addOn = 0;
                            }
                        }
                    }
                }

                // resize columns the remainder of dx
                int count = 0;

                while (rem > 0 && count < columns.Count)
                {
                    ILogicalColumn column = columns[_placementIdx];
                    if (PlacementAllowed(column, tablePlacementStrategy.ShouldApplyPlacementToHiddenColumns(ShouldApplyPlacementToHiddenColumns)))
                    {
                        int colWidth = column.getWidth();
                        if (dx > 0 || (dx < 0 && colWidth > 0))
                        {
                            long ResizeDx = rem;

                            if (dx < 0)
                            {
                                // can not set widht less then 0
                                ResizeDx = Math.Min(ResizeDx, colWidth);
                            }
                            colWidth = (int)(colWidth + (dx > 0 ? ResizeDx : -ResizeDx));
                            column.setWidthOnPlacement(colWidth, false);
                            rem = (int)(rem - ResizeDx);
                        }
                    }
                    if (--_placementIdx < 0)
                    {
                        _placementIdx = columns.Count - 1;
                    }
                    count++;
                }
                //QCR #913300, column.setWidthOnPlacement causes repaints,
                //this is will improve performance

                tablecontrol.AllowPaint = false;

                if (considerEditors)
                {
                    if (tablecontrol.RightToLeft == System.Windows.Forms.RightToLeft.Yes)
                    {
                        for (int i = columns.Count - 1; i >= 0; i--)
                        {
                            ILogicalColumn column = columns[i];
                            HandleEditors(tablecontrol, column, i <= maxColumnWithEditor, tablePlacementStrategy.ShouldApplyPlacementToHiddenColumns(ShouldApplyPlacementToHiddenColumns));
                        }
                    }
                    else
                    {
                        for (int i = 0; i < columns.Count; i++)
                        {
                            ILogicalColumn column = columns[i];
                            HandleEditors(tablecontrol, column, i <= maxColumnWithEditor, tablePlacementStrategy.ShouldApplyPlacementToHiddenColumns(ShouldApplyPlacementToHiddenColumns));
                        }
                    }
                }

                tablecontrol.MoveColumns(0);

                tablecontrol.AllowPaint = true;
                tablecontrol.Invalidate();
            }
        }