Exemplo n.º 1
0
        public void FlowLayoutSettings_WrapContents_Set_GetReturnsExpected(bool value, int expectedLayoutCallCount)
        {
            using var control = new ToolStrip
                  {
                      LayoutStyle = ToolStripLayoutStyle.Flow
                  };
            FlowLayoutSettings settings = Assert.IsType <FlowLayoutSettings>(control.LayoutSettings);
            int layoutCallCount         = 0;

            control.Layout += (sender, e) =>
            {
                Assert.Same(control, sender);
                Assert.Same(control, e.AffectedControl);
                Assert.Equal("WrapContents", e.AffectedProperty);
                layoutCallCount++;
            };

            settings.WrapContents = value;
            Assert.Equal(value, settings.WrapContents);
            Assert.Equal(expectedLayoutCallCount, layoutCallCount);
            Assert.False(control.IsHandleCreated);

            // Set same
            settings.WrapContents = value;
            Assert.Equal(value, settings.WrapContents);
            Assert.Equal(expectedLayoutCallCount + 1, layoutCallCount);
            Assert.False(control.IsHandleCreated);

            // Set different
            settings.WrapContents = !value;
            Assert.Equal(!value, settings.WrapContents);
            Assert.Equal(expectedLayoutCallCount + 1 + 1, layoutCallCount);
            Assert.False(control.IsHandleCreated);
        }
Exemplo n.º 2
0
        public void FlowLayoutSettings_FlowDirection_Set_GetReturnsExpected(FlowDirection value, int expectedLayoutCallCount)
        {
            using var control = new ToolStrip
                  {
                      LayoutStyle = ToolStripLayoutStyle.Flow
                  };
            int layoutCallCount = 0;

            control.Layout += (sender, e) =>
            {
                Assert.Same(control, sender);
                Assert.Same(control, e.AffectedControl);
                Assert.Equal("FlowDirection", e.AffectedProperty);
                layoutCallCount++;
            };
            FlowLayoutSettings settings = Assert.IsType <FlowLayoutSettings>(control.LayoutSettings);

            settings.FlowDirection = value;
            Assert.Equal(value, settings.FlowDirection);
            Assert.Equal(expectedLayoutCallCount, layoutCallCount);
            Assert.False(control.IsHandleCreated);

            // Set same
            settings.FlowDirection = value;
            Assert.Equal(value, settings.FlowDirection);
            Assert.Equal(expectedLayoutCallCount * 2, layoutCallCount);
            Assert.False(control.IsHandleCreated);
        }
Exemplo n.º 3
0
        private void AddColor(ToolStripDropDownButton tsddb)
        {
            ToolStripDropDown tsdd = tsddb.DropDown;
            ToolStripButton   stb;
            Bitmap            bmp;
            Graphics          g;

            tsdd.AutoSize = false;
            tsdd.Size     = new Size(120, 80);
            FlowLayoutSettings fls = tsdd.LayoutSettings as FlowLayoutSettings;

            fls.FlowDirection = FlowDirection.LeftToRight;
            fls.WrapContents  = true;
            foreach (KeyValuePair <string, Color> c in Colors)
            {
                stb              = new ToolStripButton();
                stb.Name         = c.Key;
                stb.DisplayStyle = ToolStripItemDisplayStyle.Image;
                bmp              = new Bitmap(16, 16);
                using (g = Graphics.FromImage(bmp))
                    using (Brush br = new SolidBrush(c.Value))
                        g.FillRectangle(br, 0, 0, 16, 16);
                stb.Image  = bmp;
                stb.Text   = c.Key;
                stb.Tag    = c.Value;
                stb.Click += new EventHandler(stb_Click);
                tsdd.Items.Add(stb);
            }
        }
        public void GetFlowBreak_ValidControl_ReturnsExpected()
        {
            var toolStrip = new ToolStrip
            {
                LayoutStyle = ToolStripLayoutStyle.Flow
            };
            FlowLayoutSettings settings = Assert.IsType <FlowLayoutSettings>(toolStrip.LayoutSettings);

            Assert.False(settings.GetFlowBreak(new Control()));
        }
        public void FlowLayoutSettings_FlowDirection_SetInvalidValue_ThrowsInvalidEnumArgumentException(FlowDirection value)
        {
            var toolStrip = new ToolStrip
            {
                LayoutStyle = ToolStripLayoutStyle.Flow
            };
            FlowLayoutSettings settings = Assert.IsType <FlowLayoutSettings>(toolStrip.LayoutSettings);

            Assert.Throws <InvalidEnumArgumentException>("value", () => settings.FlowDirection = value);
        }
        public void GetFlowBreak_InvalidChild_ThrowsNotSupportedException()
        {
            var toolStrip = new ToolStrip
            {
                LayoutStyle = ToolStripLayoutStyle.Flow
            };
            FlowLayoutSettings settings = Assert.IsType <FlowLayoutSettings>(toolStrip.LayoutSettings);

            Assert.Throws <NotSupportedException>(() => settings.GetFlowBreak(new object()));
        }
Exemplo n.º 7
0
        public void FlowLayoutSettings_GetFlowBreak_InvokeValidControl_ReturnsExpected()
        {
            using var control = new ToolStrip
                  {
                      LayoutStyle = ToolStripLayoutStyle.Flow
                  };
            FlowLayoutSettings settings = Assert.IsType <FlowLayoutSettings>(control.LayoutSettings);

            Assert.False(settings.GetFlowBreak(new Control()));
        }
Exemplo n.º 8
0
        public void FlowLayoutSettings_SetFlowBreak_InvalidChild_ThrowsNotSupportedException(bool value)
        {
            using var control = new ToolStrip
                  {
                      LayoutStyle = ToolStripLayoutStyle.Flow
                  };
            FlowLayoutSettings settings = Assert.IsType <FlowLayoutSettings>(control.LayoutSettings);

            Assert.Throws <NotSupportedException>(() => settings.SetFlowBreak(new object(), value));
        }
Exemplo n.º 9
0
        public void FlowLayoutSettings_SetFlowBreak_NullChild_ThrowsArgumentNullException(bool value)
        {
            using var control = new ToolStrip
                  {
                      LayoutStyle = ToolStripLayoutStyle.Flow
                  };
            FlowLayoutSettings settings = Assert.IsType <FlowLayoutSettings>(control.LayoutSettings);

            Assert.Throws <ArgumentNullException>("child", () => settings.SetFlowBreak(null, value));
        }
        public void GetFlowBreak_NullChild_ThrowsArgumentNullException()
        {
            var toolStrip = new ToolStrip
            {
                LayoutStyle = ToolStripLayoutStyle.Flow
            };
            FlowLayoutSettings settings = Assert.IsType <FlowLayoutSettings>(toolStrip.LayoutSettings);

            Assert.Throws <ArgumentNullException>("child", () => settings.GetFlowBreak(null));
        }
Exemplo n.º 11
0
        public void ToolStrip_CreateLayoutSettings_InvokeFlow_ReturnsExpected()
        {
            var toolStrip = new SubToolStrip();
            FlowLayoutSettings settings = Assert.IsType <FlowLayoutSettings>(toolStrip.CreateLayoutSettings(ToolStripLayoutStyle.Flow));

            Assert.Equal(FlowDirection.LeftToRight, settings.FlowDirection);
            Assert.NotNull(settings.LayoutEngine);
            Assert.Same(settings.LayoutEngine, settings.LayoutEngine);
            Assert.Same(toolStrip, settings.Owner);
            Assert.True(settings.WrapContents);
        }
Exemplo n.º 12
0
        public void MethodCreateLayoutSettings()
        {
            ExposeProtectedProperties ts = new ExposeProtectedProperties();

            Assert.AreEqual("System.Windows.Forms.FlowLayoutSettings", ts.PublicCreateLayoutSettings(ToolStripLayoutStyle.Flow).ToString(), "A1");
            Assert.AreEqual(null, ts.PublicCreateLayoutSettings(ToolStripLayoutStyle.HorizontalStackWithOverflow), "A2");
            Assert.AreEqual(null, ts.PublicCreateLayoutSettings(ToolStripLayoutStyle.StackWithOverflow), "A3");
            Assert.AreEqual("System.Windows.Forms.TableLayoutSettings", ts.PublicCreateLayoutSettings(ToolStripLayoutStyle.Table).ToString(), "A4");
            Assert.AreEqual(null, ts.PublicCreateLayoutSettings(ToolStripLayoutStyle.VerticalStackWithOverflow), "A5");

            FlowLayoutSettings flow_settings = (FlowLayoutSettings)ts.PublicCreateLayoutSettings(ToolStripLayoutStyle.Flow);

            Assert.AreEqual(FlowDirection.TopDown, flow_settings.FlowDirection, "A6");
        }
        public void FlowLayoutSettings_FlowDirection_Set_GetReturnsExpected(FlowDirection value)
        {
            var toolStrip = new ToolStrip
            {
                LayoutStyle = ToolStripLayoutStyle.Flow
            };
            FlowLayoutSettings settings = Assert.IsType <FlowLayoutSettings>(toolStrip.LayoutSettings);

            settings.FlowDirection = value;
            Assert.Equal(value, settings.FlowDirection);

            // Set same
            settings.FlowDirection = value;
            Assert.Equal(value, settings.FlowDirection);
        }
        public void FlowLayoutSettings_WrapContents_Set_GetReturnsExpected(bool value)
        {
            var toolStrip = new ToolStrip
            {
                LayoutStyle = ToolStripLayoutStyle.Flow
            };
            FlowLayoutSettings settings = Assert.IsType <FlowLayoutSettings>(toolStrip.LayoutSettings);

            settings.WrapContents = value;
            Assert.Equal(value, settings.WrapContents);

            // Set same
            settings.WrapContents = value;
            Assert.Equal(value, settings.WrapContents);

            // Set different
            settings.WrapContents = !value;
            Assert.Equal(!value, settings.WrapContents);
        }
        public void SetFlowBreak_Invoke_GetFlowBreakReturnsExpected(bool value)
        {
            var toolStrip = new ToolStrip
            {
                LayoutStyle = ToolStripLayoutStyle.Flow
            };
            FlowLayoutSettings settings = Assert.IsType <FlowLayoutSettings>(toolStrip.LayoutSettings);

            var control = new Control();

            settings.SetFlowBreak(control, value);
            Assert.Equal(value, settings.GetFlowBreak(control));

            // Set same.
            settings.SetFlowBreak(control, value);
            Assert.Equal(value, settings.GetFlowBreak(control));

            // Set different.
            settings.SetFlowBreak(control, !value);
            Assert.Equal(!value, settings.GetFlowBreak(control));
        }
Exemplo n.º 16
0
        public void FlowLayoutSettings_SetFlowBreak_Invoke_GetFlowBreakReturnsExpected(bool value)
        {
            using var control = new ToolStrip
                  {
                      LayoutStyle = ToolStripLayoutStyle.Flow
                  };
            FlowLayoutSettings settings = Assert.IsType <FlowLayoutSettings>(control.LayoutSettings);

            using var child = new Control();
            int layoutCallCount = 0;

            control.Layout += (sender, e) => layoutCallCount++;
            int childLayoutCallCount = 0;

            child.Layout += (sender, e) => childLayoutCallCount++;

            settings.SetFlowBreak(child, value);
            Assert.Equal(value, settings.GetFlowBreak(child));
            Assert.Equal(0, layoutCallCount);
            Assert.Equal(0, childLayoutCallCount);
            Assert.False(control.IsHandleCreated);
            Assert.False(child.IsHandleCreated);

            // Set same.
            settings.SetFlowBreak(child, value);
            Assert.Equal(value, settings.GetFlowBreak(child));
            Assert.Equal(0, layoutCallCount);
            Assert.Equal(0, childLayoutCallCount);
            Assert.False(control.IsHandleCreated);
            Assert.False(child.IsHandleCreated);

            // Set different.
            settings.SetFlowBreak(child, !value);
            Assert.Equal(!value, settings.GetFlowBreak(child));
            Assert.Equal(0, layoutCallCount);
            Assert.Equal(0, childLayoutCallCount);
            Assert.False(control.IsHandleCreated);
            Assert.False(child.IsHandleCreated);
        }
Exemplo n.º 17
0
        internal override Size GetPreferredSize(object container, Size proposedSize)
        {
            IArrangedContainer parent   = (IArrangedContainer)container;
            FlowLayoutSettings settings = GetLayoutSettings(parent);
            int  width                   = 0;
            int  height                  = 0;
            bool horizontal              = settings.FlowDirection == FlowDirection.LeftToRight || settings.FlowDirection == FlowDirection.RightToLeft;
            int  size_in_flow_direction  = 0;
            int  size_in_other_direction = 0;
            int  increase;
            Size margin           = new Size(0, 0);
            bool force_flow_break = false;
            bool wrap             = settings.WrapContents;

            foreach (IArrangedElement control in parent.Controls)
            {
                if (!control.Visible)
                {
                    continue;
                }
                Size control_preferred_size;
                if (control.AutoSize)
                {
                    Size proposed_constraints = new Size(int.MaxValue, Math.Max(1, proposedSize.Height - control.Margin.Vertical));
                    if (size_in_flow_direction == 0)
                    {
                        proposed_constraints.Width = Math.Max(1, proposedSize.Width - control.Margin.Horizontal);
                    }
                    control_preferred_size = control.GetPreferredSize(proposed_constraints);
                }
                else
                {
                    control_preferred_size = control.ExplicitBounds.Size;
                    if ((control.Anchor & (AnchorStyles.Top | AnchorStyles.Bottom)) == (AnchorStyles.Top | AnchorStyles.Bottom))
                    {
                        control_preferred_size.Height = control.MinimumSize.Height;
                    }
                }
                Padding control_margin = control.Margin;
                if (horizontal)
                {
                    increase = control_preferred_size.Width + control_margin.Horizontal;
                    if (wrap && ((proposedSize.Width > 0 && size_in_flow_direction != 0 && size_in_flow_direction + increase >= proposedSize.Width) || force_flow_break))
                    {
                        width = Math.Max(width, size_in_flow_direction);
                        size_in_flow_direction = 0;
                        height += size_in_other_direction;
                        size_in_other_direction = 0;
                    }
                    size_in_flow_direction += increase;
                    size_in_other_direction = Math.Max(size_in_other_direction, control_preferred_size.Height + control_margin.Vertical);
                }
                else
                {
                    increase = control_preferred_size.Height + control_margin.Vertical;
                    if (wrap && ((proposedSize.Height > 0 && size_in_flow_direction != 0 && size_in_flow_direction + increase >= proposedSize.Height) || force_flow_break))
                    {
                        height = Math.Max(height, size_in_flow_direction);
                        size_in_flow_direction = 0;
                        width += size_in_other_direction;
                        size_in_other_direction = 0;
                    }
                    size_in_flow_direction += increase;
                    size_in_other_direction = Math.Max(size_in_other_direction, control_preferred_size.Width + control_margin.Horizontal);
                }

                if (parent != null)
                {
                    force_flow_break = settings.GetFlowBreak(control);
                }
            }

            if (horizontal)
            {
                width   = Math.Max(width, size_in_flow_direction);
                height += size_in_other_direction;
            }
            else
            {
                height = Math.Max(height, size_in_flow_direction);
                width += size_in_other_direction;
            }

            return(new Size(width, height));
        }
Exemplo n.º 18
0
        public void FlowLayoutSettings_SetFlowBreak_InvokeControlWithParent_GetFlowBreakReturnsExpected(bool value, int expectedParentLayoutCallCount)
        {
            using var control = new ToolStrip
                  {
                      LayoutStyle = ToolStripLayoutStyle.Flow
                  };
            FlowLayoutSettings settings = Assert.IsType <FlowLayoutSettings>(control.LayoutSettings);

            using var parent = new Control();
            using var child  = new Control
                  {
                      Parent = parent
                  };
            int layoutCallCount = 0;

            control.Layout += (sender, e) => layoutCallCount++;
            int childLayoutCallCount = 0;

            child.Layout += (sender, e) => childLayoutCallCount++;
            int parentLayoutCallCount = 0;

            void parentHandler(object sender, LayoutEventArgs eventArgs)
            {
                Assert.Same(parent, sender);
                Assert.Same(child, eventArgs.AffectedControl);
                Assert.Equal("FlowBreak", eventArgs.AffectedProperty);
                parentLayoutCallCount++;
            }

            parent.Layout += parentHandler;

            try
            {
                settings.SetFlowBreak(child, value);
                Assert.Equal(value, settings.GetFlowBreak(child));
                Assert.Equal(0, layoutCallCount);
                Assert.Equal(0, childLayoutCallCount);
                Assert.Equal(expectedParentLayoutCallCount, parentLayoutCallCount);
                Assert.False(control.IsHandleCreated);
                Assert.False(child.IsHandleCreated);

                // Set same.
                settings.SetFlowBreak(child, value);
                Assert.Equal(value, settings.GetFlowBreak(child));
                Assert.Equal(0, layoutCallCount);
                Assert.Equal(0, childLayoutCallCount);
                Assert.Equal(expectedParentLayoutCallCount, parentLayoutCallCount);
                Assert.False(control.IsHandleCreated);
                Assert.False(child.IsHandleCreated);

                // Set different.
                settings.SetFlowBreak(child, !value);
                Assert.Equal(!value, settings.GetFlowBreak(child));
                Assert.Equal(0, layoutCallCount);
                Assert.Equal(0, childLayoutCallCount);
                Assert.Equal(expectedParentLayoutCallCount + 1, parentLayoutCallCount);
                Assert.False(control.IsHandleCreated);
                Assert.False(child.IsHandleCreated);
            }
            finally
            {
                parent.Layout -= parentHandler;
            }
        }
Exemplo n.º 19
0
        public override bool Layout(object container, LayoutEventArgs args)
        {
            // TODO: Handle ToolStripPanel/ToolStripPanelRow
            if (container is ToolStripPanel)
            {
                return(false);
            }

            IArrangedContainer parent = (IArrangedContainer)container;

            if (parent.Controls.Count == 0)
            {
                return(false);
            }

            FlowLayoutSettings settings = GetLayoutSettings(parent);

            // Use DisplayRectangle so that parent.Padding is honored.
            Rectangle parentDisplayRectangle = parent.DisplayRectangle;
            Point     currentLocation;

            // Set our starting point based on flow direction
            switch (settings.FlowDirection)
            {
            case FlowDirection.BottomUp:
                currentLocation = new Point(parentDisplayRectangle.Left, parentDisplayRectangle.Bottom);
                break;

            case FlowDirection.LeftToRight:
            case FlowDirection.TopDown:
            default:
                currentLocation = parentDisplayRectangle.Location;
                break;

            case FlowDirection.RightToLeft:
                currentLocation = new Point(parentDisplayRectangle.Right, parentDisplayRectangle.Top);
                break;
            }

            bool forceFlowBreak = false;

            List <IArrangedElement> rowControls = new List <IArrangedElement> ();

            foreach (IArrangedElement c in parent.Controls)
            {
                // Only apply layout to visible controls.
                if (!c.Visible)
                {
                    continue;
                }

                // Resize any AutoSize controls to their preferred size
                if (c.AutoSize == true)
                {
                    Size proposed_constraints = new Size(int.MaxValue, Math.Max(1, parentDisplayRectangle.Height - c.Margin.Vertical));
                    if (currentLocation.X == parentDisplayRectangle.Left)
                    {
                        proposed_constraints.Width = Math.Max(1, parentDisplayRectangle.Width - c.Margin.Horizontal);
                    }
                    Size new_size = c.GetPreferredSize(proposed_constraints);
                    c.SetBounds(c.Bounds.Left, c.Bounds.Top, new_size.Width, new_size.Height, BoundsSpecified.None);
                }

                switch (settings.FlowDirection)
                {
                case FlowDirection.BottomUp:
                    // Decide if it's time to start a new column
                    // - Our settings must be WrapContents, and we ran out of room or the previous control's FlowBreak == true
                    if (settings.WrapContents)
                    {
                        if ((currentLocation.Y) < (c.Bounds.Height + c.Margin.Top + c.Margin.Bottom) || forceFlowBreak)
                        {
                            currentLocation.X = FinishColumn(rowControls);
                            currentLocation.Y = parentDisplayRectangle.Bottom;

                            forceFlowBreak = false;
                            rowControls.Clear();
                        }
                    }

                    // Offset the right margin and set the control to our point
                    currentLocation.Offset(0, c.Margin.Bottom * -1);
                    c.SetBounds(currentLocation.X + c.Margin.Left, currentLocation.Y - c.Bounds.Height, c.Bounds.Width, c.Bounds.Height, BoundsSpecified.None);

                    // Update our location pointer
                    currentLocation.Y -= (c.Bounds.Height + c.Margin.Top);
                    break;

                case FlowDirection.LeftToRight:
                default:
                    // Decide if it's time to start a new row
                    // - Our settings must be WrapContents, and we ran out of room or the previous control's FlowBreak == true
                    if (settings.WrapContents && !(parent is ToolStripPanel))
                    {
                        if ((parentDisplayRectangle.Width + parentDisplayRectangle.Left - currentLocation.X) < (c.Bounds.Width + c.Margin.Left + c.Margin.Right) || forceFlowBreak)
                        {
                            currentLocation.Y = FinishRow(rowControls);
                            currentLocation.X = parentDisplayRectangle.Left;

                            forceFlowBreak = false;
                            rowControls.Clear();
                        }
                    }

                    // Offset the left margin and set the control to our point
                    currentLocation.Offset(c.Margin.Left, 0);
                    c.SetBounds(currentLocation.X, currentLocation.Y + c.Margin.Top, c.Bounds.Width, c.Bounds.Height, BoundsSpecified.None);

                    // Update our location pointer
                    currentLocation.X += c.Bounds.Width + c.Margin.Right;
                    break;

                case FlowDirection.RightToLeft:
                    // Decide if it's time to start a new row
                    // - Our settings must be WrapContents, and we ran out of room or the previous control's FlowBreak == true
                    if (settings.WrapContents)
                    {
                        if ((currentLocation.X) < (c.Bounds.Width + c.Margin.Left + c.Margin.Right) || forceFlowBreak)
                        {
                            currentLocation.Y = FinishRow(rowControls);
                            currentLocation.X = parentDisplayRectangle.Right;

                            forceFlowBreak = false;
                            rowControls.Clear();
                        }
                    }

                    // Offset the right margin and set the control to our point
                    currentLocation.Offset(c.Margin.Right * -1, 0);
                    c.SetBounds(currentLocation.X - c.Bounds.Width, currentLocation.Y + c.Margin.Top, c.Bounds.Width, c.Bounds.Height, BoundsSpecified.None);

                    // Update our location pointer
                    currentLocation.X -= (c.Bounds.Width + c.Margin.Left);
                    break;

                case FlowDirection.TopDown:
                    // Decide if it's time to start a new column
                    // - Our settings must be WrapContents, and we ran out of room or the previous control's FlowBreak == true
                    if (settings.WrapContents)
                    {
                        if ((parentDisplayRectangle.Height + parentDisplayRectangle.Top - currentLocation.Y) < (c.Bounds.Height + c.Margin.Top + c.Margin.Bottom) || forceFlowBreak)
                        {
                            currentLocation.X = FinishColumn(rowControls);
                            currentLocation.Y = parentDisplayRectangle.Top;

                            forceFlowBreak = false;
                            rowControls.Clear();
                        }
                    }

                    // Offset the top margin and set the control to our point
                    currentLocation.Offset(0, c.Margin.Top);
                    c.SetBounds(currentLocation.X + c.Margin.Left, currentLocation.Y, c.Bounds.Width, c.Bounds.Height, BoundsSpecified.None);

                    // Update our location pointer
                    currentLocation.Y += c.Bounds.Height + c.Margin.Bottom;
                    break;
                }
                // Add it to our list of things to adjust the second dimension of
                rowControls.Add(c);

                // If user set a flowbreak on this control, it will be the last one in this row/column
                if (settings.GetFlowBreak(c))
                {
                    forceFlowBreak = true;
                }
            }

            // Set the control heights/widths for the last row/column
            if (settings.FlowDirection == FlowDirection.LeftToRight || settings.FlowDirection == FlowDirection.RightToLeft)
            {
                FinishRow(rowControls);
            }
            else
            {
                FinishColumn(rowControls);
            }

            return(parent.AutoSize);
        }