// ToolStrips use the same FlowLayout, but is made up of ToolStripItems which
        // are Components instead of Controls, so we have to duplicate this login for
        // ToolStripItems.
        private bool LayoutToolStrip(ToolStrip parent)
        {
            FlowLayoutSettings settings;

            settings = (FlowLayoutSettings)parent.LayoutSettings;

            // Nothing to layout, exit method
            if (parent.Items.Count == 0)
            {
                return(false);
            }

            foreach (ToolStripItem tsi in parent.Items)
            {
                tsi.SetPlacement(ToolStripItemPlacement.Main);
            }

            // 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 <ToolStripItem> rowControls = new List <ToolStripItem> ();

            foreach (ToolStripItem c in parent.Items)
            {
                // Only apply layout to visible controls.
                if (!c.Available)
                {
                    continue;
                }

                // Resize any AutoSize controls to their preferred width
                // (the height should fill the ToolStrip)
                if (c.AutoSize == true)
                {
                    Size preferred_size = c.GetPreferredSize(c.Size);
                    preferred_size.Height = parentDisplayRectangle.Height;
                    c.SetBounds(new Rectangle(c.Location, preferred_size));
                }

                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.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.Location = new Point(currentLocation.X + c.Margin.Left, currentLocation.Y - c.Height);

                    // Update our location pointer
                    currentLocation.Y -= (c.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)
                    {
                        if ((parentDisplayRectangle.Width - currentLocation.X) < (c.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.Location = new Point(currentLocation.X, currentLocation.Y + c.Margin.Top);

                    // Update our location pointer
                    currentLocation.X += c.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.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.Location = new Point(currentLocation.X - c.Width, currentLocation.Y + c.Margin.Top);

                    // Update our location pointer
                    currentLocation.X -= (c.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 - currentLocation.Y) < (c.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.Location = new Point(currentLocation.X + c.Margin.Left, currentLocation.Y);

                    // Update our location pointer
                    currentLocation.Y += c.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;
                }
            }

            int final_height = 0;

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

            if (final_height > 0)
            {
                parent.SetBoundsInternal(parent.Left, parent.Top, parent.Width, final_height + parent.Padding.Bottom, BoundsSpecified.None);
            }

            return(false);
        }
예제 #2
0
		// ToolStrips use the same FlowLayout, but is made up of ToolStripItems which
		// are Components instead of Controls, so we have to duplicate this login for
		// ToolStripItems.
		private bool LayoutToolStrip (ToolStrip parent)
		{
			FlowLayoutSettings settings;
			settings = (FlowLayoutSettings)parent.LayoutSettings;

			// Nothing to layout, exit method
			if (parent.Items.Count == 0) return false;

			foreach (ToolStripItem tsi in parent.Items)
				tsi.SetPlacement (ToolStripItemPlacement.Main);
				
			// 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<ToolStripItem> rowControls = new List<ToolStripItem> ();

			foreach (ToolStripItem c in parent.Items) {
				// Only apply layout to visible controls.
				if (!c.Available) { continue; }

				// Resize any AutoSize controls to their preferred size
				if (c.AutoSize == true)
					c.SetBounds (new Rectangle (c.Location, c.GetPreferredSize (c.Size)));

				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.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.Location = new Point (currentLocation.X + c.Margin.Left, currentLocation.Y - c.Height);

						// Update our location pointer
						currentLocation.Y -= (c.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)
							if ((parentDisplayRectangle.Width - currentLocation.X) < (c.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.Location = new Point (currentLocation.X, currentLocation.Y + c.Margin.Top);

						// Update our location pointer
						currentLocation.X += c.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.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.Location = new Point (currentLocation.X - c.Width, currentLocation.Y + c.Margin.Top);

						// Update our location pointer
						currentLocation.X -= (c.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 - currentLocation.Y) < (c.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.Location = new Point (currentLocation.X + c.Margin.Left, currentLocation.Y);

						// Update our location pointer
						currentLocation.Y += c.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;
			}

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

			if (final_height > 0)
				parent.SetBoundsInternal (parent.Left, parent.Top, parent.Width, final_height + parent.Padding.Bottom, BoundsSpecified.None);
				
			return false;

		}