SidebarLibrary.Menus.MenuControl.DrawSingleCommand C# (CSharp) Method

DrawSingleCommand() private method

private DrawSingleCommand ( Graphics g, DrawCommand dc, bool tracked ) : void
g System.Drawing.Graphics
dc DrawCommand
tracked bool
return void
		internal void DrawSingleCommand(Graphics g, DrawCommand dc, bool tracked)
		{
			Rectangle drawRect = dc.DrawRect;
			MenuCommand mc = dc.MenuCommand;

			// Copy the rectangle used for drawing cell
			Rectangle shadowRect = drawRect;

			// Expand to right and bottom to cover the area used to draw shadows
			shadowRect.Width += _shadowGap;
			shadowRect.Height += _shadowGap;

			// Draw background color over cell and shadow area to the right
			using (SolidBrush back = new SolidBrush(SystemColors.Control))
				g.FillRectangle(back, shadowRect);

			if (!dc.Separator)
			{
				Rectangle textRect;

				// Text rectangle size depends on type of draw command we are drawing
				if (dc.Chevron)
				{
					// Create chevron drawing rectangle
					textRect = new Rectangle(drawRect.Left + _lengthGap, drawRect.Top + _boxExpandUpper,
											 drawRect.Width - _lengthGap * 2, drawRect.Height - (_boxExpandUpper * 2));
				}
				else
				{
					// Create text drawing rectangle
					textRect = new Rectangle(drawRect.Left + _lengthGap, drawRect.Top + _lengthGap,
											 drawRect.Width - _lengthGap * 2, drawRect.Height - _lengthGap * 2);
				}

				if (dc.Enabled)
				{
					// Draw selection
					if (tracked)
					{
						Rectangle boxRect;

						// Create the rectangle for box around the text
						if (_direction == Direction.Horizontal)
						{
							boxRect = new Rectangle(textRect.Left - _boxExpandSides,
													textRect.Top - _boxExpandUpper,
													textRect.Width + _boxExpandSides * 2,
													textRect.Height + _boxExpandUpper);
						}
						else
						{
							if (!dc.Chevron)
							{
								boxRect = new Rectangle(textRect.Left,
														textRect.Top - _boxExpandSides,
														textRect.Width - _boxExpandSides,
														textRect.Height + _boxExpandSides * 2);
							}
							else
								boxRect = textRect;
						}

						switch (_style)
						{
							case VisualStyle.IDE:
								if (_selected)
								{
									// Fill the entire inside
									g.FillRectangle(SystemBrushes.ControlLight, boxRect);

								int rightLeft = boxRect.Right + 1;
									int rightBottom = boxRect.Bottom;

								if (_drawUpwards && (_direction == Direction.Horizontal))
									{
										// Draw the box around the selection area
										using (Pen dark = new Pen(SystemColors.ControlDark))
											g.DrawRectangle(dark, boxRect.Left, boxRect.Top - _shadowGap,
															  boxRect.Width, boxRect.Height + _shadowGap);

										// Remove the top line of the selection area
										using (Pen dark = new Pen(SystemColors.ControlLight))
											g.DrawLine(dark, boxRect.Left + 1, boxRect.Top, boxRect.Right - 2, boxRect.Top);

										int rightTop = boxRect.Top;
										//int leftLeft = boxRect.Left + _shadowGap;

										SolidBrush shadowBrush = null;
										try
										{
											// Decide if we need to use an alpha brush
											if (_supportsLayered && (_style == VisualStyle.IDE))
												shadowBrush = new SolidBrush(Color.FromArgb(64, 0, 0, 0));
											else
												shadowBrush = new SolidBrush(SystemColors.ControlDark);

											g.FillRectangle(shadowBrush, new Rectangle(rightLeft, rightTop + _shadowGap - 1, _shadowGap, rightBottom - rightTop - _shadowGap * 2));
										}
										finally
										{
											shadowBrush.Dispose();
										}
								}
								else
								{
									// Draw the box around the selection area
									using(Pen dark = new Pen(SystemColors.ControlDark))
										g.DrawRectangle(dark, boxRect);

									if (_direction == Direction.Horizontal)
									{
										// Remove the bottom line of the selection area
										using(Pen dark = new Pen(SystemColors.ControlLight))
											g.DrawLine(dark, boxRect.Left, boxRect.Bottom, boxRect.Right, boxRect.Bottom);

										int rightTop = boxRect.Top + _shadowYOffset;

										SolidBrush shadowBrush = null;
										try
										{
											// Decide if we need to use an alpha brush
											if (_supportsLayered && (_style == VisualStyle.IDE))
												shadowBrush = new SolidBrush(Color.FromArgb(64, 0, 0, 0));
											else
												shadowBrush = new SolidBrush(SystemColors.ControlDark);

											g.FillRectangle(shadowBrush, new Rectangle(rightLeft, rightTop, _shadowGap, rightBottom - rightTop));
										}
										finally
										{
											shadowBrush.Dispose();
										}
									}
									else
									{
										// Remove the right line of the selection area
										using(Pen dark = new Pen(SystemColors.ControlLight))
											g.DrawLine(dark, boxRect.Right, boxRect.Top, boxRect.Right, boxRect.Bottom);

										int leftLeft = boxRect.Left + _shadowYOffset;

										SolidBrush shadowBrush = null;
										try
										{
											// Decide if we need to use an alpha brush
											if (_supportsLayered && (_style == VisualStyle.IDE))
												shadowBrush = new SolidBrush(Color.FromArgb(64, 0, 0, 0));
											else
												shadowBrush = new SolidBrush(SystemColors.ControlDark);

											g.FillRectangle(shadowBrush, new Rectangle(leftLeft, rightBottom + 1, rightBottom - leftLeft - _shadowGap, _shadowGap));
										}
										finally
										{
											shadowBrush.Dispose();
										}
									}
								}
							}
							else
							{
								using (Pen selectPen = new Pen(ColorUtil.VSNetBorderColor))
								{
									// Draw the selection area in white so can alpha draw over the top
									using (SolidBrush whiteBrush = new SolidBrush(Color.White))
										g.FillRectangle(whiteBrush, boxRect);

									using (SolidBrush selectBrush = new SolidBrush(Color.FromArgb(70, ColorUtil.VSNetBorderColor)))
									{
										// Draw the selection area
										g.FillRectangle(selectBrush, boxRect);

										// Draw a border around the selection area
										g.DrawRectangle(selectPen, boxRect);
									}
								}
							}
							break;
						case VisualStyle.Plain:
							if (_plainAsBlock)
							{
								using (SolidBrush selectBrush = new SolidBrush(ColorUtil.VSNetBorderColor))
									g.FillRectangle(selectBrush, drawRect);
							}
							else
							{
								using(Pen lighlight = new Pen(SystemColors.ControlLightLight),
										  dark = new Pen(SystemColors.ControlDark))
								{
									if (_selected)
									{
										g.DrawLine(dark, boxRect.Left, boxRect.Bottom, boxRect.Left, boxRect.Top);
										g.DrawLine(dark, boxRect.Left, boxRect.Top, boxRect.Right, boxRect.Top);
										g.DrawLine(lighlight, boxRect.Right, boxRect.Top, boxRect.Right, boxRect.Bottom);
										g.DrawLine(lighlight, boxRect.Right, boxRect.Bottom, boxRect.Left, boxRect.Bottom);
									}
									else
									{
										g.DrawLine(lighlight, boxRect.Left, boxRect.Bottom, boxRect.Left, boxRect.Top);
										g.DrawLine(lighlight, boxRect.Left, boxRect.Top, boxRect.Right, boxRect.Top);
										g.DrawLine(dark, boxRect.Right, boxRect.Top, boxRect.Right, boxRect.Bottom);
										g.DrawLine(dark, boxRect.Right, boxRect.Bottom, boxRect.Left, boxRect.Bottom);
									}
								}
							}
							break;
						}
					}
				}

				if (dc.Chevron)
				{
					// Draw the chevron image in the centre of the text area
					int yPos = drawRect.Top;
					int xPos = drawRect.X + ((drawRect.Width - _chevronLength) / 2);

					// When selected...
					if (_selected)
					{
						// ...offset down and to the right
						xPos += 1;
						yPos += 1;
					}

					g.DrawImage(_menuImages.Images[_chevronIndex], xPos, yPos);
				}
				else
				{
					// Left align the text drawing on a single line centered vertically
					// and process the & character to be shown as an underscore on next character
					using (StringFormat format = new StringFormat())
					{
						format.FormatFlags = StringFormatFlags.NoClip | StringFormatFlags.NoWrap;
						format.Alignment = StringAlignment.Center;
						format.LineAlignment = StringAlignment.Center;
						format.HotkeyPrefix = HotkeyPrefix.Show;

						if (_direction == Direction.Vertical)
							format.FormatFlags |= StringFormatFlags.DirectionVertical;


						if (dc.Enabled)
						{
							if (tracked && (_style == VisualStyle.Plain) && _plainAsBlock)
							{
								// Is the item selected as well as tracked?
								if (_selected)
								{
									// Offset to show it is selected
									textRect.X += 2;
									textRect.Y += 2;
								}

								using (SolidBrush textBrush = new SolidBrush(SystemColors.HighlightText))
									g.DrawString(mc.Text, this.Font, textBrush, textRect, format);
							}
							else
								using (SolidBrush textBrush = new SolidBrush(SystemColors.MenuText))
									g.DrawString(mc.Text, this.Font, textBrush, textRect, format);
						}
						else
						{
							// Helper values used when drawing grayed text in plain style
							Rectangle rectDownRight = textRect;
							rectDownRight.Offset(1,1);

							// Draw then text offset down and right
							using (SolidBrush whiteBrush = new SolidBrush(SystemColors.HighlightText))
								g.DrawString(mc.Text, this.Font, whiteBrush, rectDownRight, format);

							// Draw then text offset up and left
							using (SolidBrush grayBrush = new SolidBrush(SystemColors.GrayText))
								g.DrawString(mc.Text, this.Font, grayBrush, textRect, format);
						}
					}
				}
			}
		}