System.Windows.Forms.StatusStrip.OnSpringTableLayoutCore C# (CSharp) Method

OnSpringTableLayoutCore() protected method

protected OnSpringTableLayoutCore ( ) : void
return void
		protected virtual void OnSpringTableLayoutCore ()
		{
			if (!this.Created)
				return;

			ToolStripItemOverflow[] overflow = new ToolStripItemOverflow[this.Items.Count];
			ToolStripItemPlacement[] placement = new ToolStripItemPlacement[this.Items.Count];
			Size proposedSize = new Size (0, Bounds.Height);
			int[] widths = new int[this.Items.Count];
			int total_width = 0;
			int toolstrip_width = DisplayRectangle.Width;
			int i = 0;
			int spring_count = 0;

			foreach (ToolStripItem tsi in this.Items) {
				overflow[i] = tsi.Overflow;
				widths[i] = tsi.GetPreferredSize (proposedSize).Width + tsi.Margin.Horizontal;
				placement[i] = tsi.Overflow == ToolStripItemOverflow.Always ? ToolStripItemPlacement.None : ToolStripItemPlacement.Main;
				placement[i] = tsi.Available && tsi.InternalVisible ? placement[i] : ToolStripItemPlacement.None;
				total_width += placement[i] == ToolStripItemPlacement.Main ? widths[i] : 0;
				if (tsi is ToolStripStatusLabel && (tsi as ToolStripStatusLabel).Spring)
					spring_count++;
					
				i++;
			}

			while (total_width > toolstrip_width) {
				bool removed_one = false;

				// Start at the right, removing Overflow.AsNeeded first
				for (int j = widths.Length - 1; j >= 0; j--)
					if (overflow[j] == ToolStripItemOverflow.AsNeeded && placement[j] == ToolStripItemPlacement.Main) {
						placement[j] = ToolStripItemPlacement.None;
						total_width -= widths[j];
						removed_one = true;
						break;
					}

				// If we didn't remove any AsNeeded ones, we have to start removing Never ones
				// These are not put on the Overflow, they are simply not shown
				if (!removed_one)
					for (int j = widths.Length - 1; j >= 0; j--)
						if (overflow[j] == ToolStripItemOverflow.Never && placement[j] == ToolStripItemPlacement.Main) {
							placement[j] = ToolStripItemPlacement.None;
							total_width -= widths[j];
							removed_one = true;
							break;
						}

				// There's nothing left to remove, break or we will loop forever	
				if (!removed_one)
					break;
			}

			if (spring_count > 0) {
				int per_item = (toolstrip_width - total_width) / spring_count;
				i = 0;
				
				foreach (ToolStripItem tsi in this.Items) {
					if (tsi is ToolStripStatusLabel && (tsi as ToolStripStatusLabel).Spring)
						widths[i] += per_item;
						
					i++;
				}
			}

			i = 0;
			Point layout_pointer = new Point (this.DisplayRectangle.Left, this.DisplayRectangle.Top);
			int button_height = this.DisplayRectangle.Height;

			// Now we should know where everything goes, so lay everything out
			foreach (ToolStripItem tsi in this.Items) {
				tsi.SetPlacement (placement[i]);

				if (placement[i] == ToolStripItemPlacement.Main) {
					tsi.SetBounds (new Rectangle (layout_pointer.X + tsi.Margin.Left, layout_pointer.Y + tsi.Margin.Top, widths[i] - tsi.Margin.Horizontal, button_height - tsi.Margin.Vertical));
					layout_pointer.X += widths[i];
				}

				i++;
			}

			this.SetDisplayedItems ();
		}