Exemplo n.º 1
0
		/// <summary>
		/// Paint the node text
		/// </summary>
		/// <param name="oNode"></param>
		/// <param name="oGraphics"></param>
		/// <param name="nX"></param>
		/// <param name="nY"></param>		
		private void PaintNodeText(Node oNode, Graphics oGraphics, int nX, int nY, 
			ref Hashtable m_mapRectToItemCheck, ref Hashtable m_mapItemCheckToRect)
		{
			int nAlpha = oNode.TreeView.GetNodeAlpha(oNode);
			Font oFont = oNode.GetFont();

			if (oNode.Flash == true)
				nAlpha = 255;

			//if (oNode.GetShowPlusMinus() == false)
			//	nX -= 15;
			
			SizeF oTextSize = oGraphics.MeasureString(oNode.GetText(), oNode.GetFont(), oNode.GetTreeView().GetDrawWidth() - nX - 8);
			if (m_oTreeView.Multiline == false)
				oTextSize = oGraphics.MeasureString(oNode.GetText(), oFont);

			oNode.Top = nY + (int)oTextSize.Height + 2;

			int nTextWidth = (int)oTextSize.Width;
			oNode.TextWidth = nTextWidth;

			if (oTextSize.Height == 0)
			{
				oTextSize.Height = oNode.GetFont().Height;
			}
			
			// draw check boxes
			if (oNode.GetCheckBoxes() == true && oNode.CheckBoxVisible == true)
			{												
				Pen oPen = null;		
			
				if (oNode.Flash == true)
					nAlpha = 255;

				#region draw check background
				if (oNode.GetCheckBackColor() != Color.Transparent)
				{
					if (oNode.Parent == null || oNode.Parent.SubNodesCheckExclusive == false)
					{
						Rectangle rectCheckBack = new Rectangle(nX + 4, nY + 2, 11, 11);
						Color backColor = Color.FromArgb(nAlpha, oNode.GetCheckBackColor());

						Brush brush = new SolidBrush(backColor);

						if (oNode.GetCheckBorderStyle() == CheckBoxBorderStyle.XP)
						{					
							Color fadeColor = oNode.GetCheckBackColor();
							if (fadeColor == Color.White)
								fadeColor = Color.LightGray;

							brush = new LinearGradientBrush
								(
								rectCheckBack,
								Color.FromArgb(nAlpha == 255 ? 255 : 48, fadeColor),
								Color.White,
								LinearGradientMode.ForwardDiagonal						
								);  
						}

						oGraphics.FillRectangle(brush, rectCheckBack);
						brush.Dispose();
						brush = null;
					}
					else
					{
						Rectangle rectCheckBack = new Rectangle(nX + 4, nY + 2, 11, 11);
						Color backColor = Color.FromArgb(nAlpha, oNode.GetCheckBackColor());

						Brush brush = new SolidBrush(backColor);

						if (oNode.GetCheckBorderStyle() == CheckBoxBorderStyle.XP)
						{					
							Color fadeColor = oNode.GetCheckBackColor();
							if (fadeColor == Color.White)
								fadeColor = Color.LightGray;

							brush = new LinearGradientBrush
								(
								rectCheckBack,
								Color.FromArgb(nAlpha == 255 ? 255 : 48, fadeColor),
								Color.White,
								LinearGradientMode.ForwardDiagonal						
								);  
						}

						GraphicsPath path = new GraphicsPath();
						path.AddEllipse(nX + 4, nY + 2, 10, 10);
						Region region = new Region(path);
						
						oGraphics.Clip = region;
						oGraphics.FillRectangle(brush, rectCheckBack);
						oGraphics.Clip = new Region(new Rectangle(0, 0, m_oTreeView.Width, m_oTreeView.Height));

						brush.Dispose();
						brush = null;
					}
				}
				#endregion

				oPen = new Pen(Color.FromArgb(nAlpha, oNode.GetCheckBorderColor()), 1);																	
				Rectangle rectCheck = new Rectangle(nX + 3, nY + 1, 12, 12);

				#region draw check rectangle

				if (oNode.Parent == null || oNode.Parent.SubNodesCheckExclusive == false)
				{
					if (oNode.GetCheckBorderStyle() == CheckBoxBorderStyle.Solid || oNode.GetCheckBorderStyle() == CheckBoxBorderStyle.XP)
						oGraphics.DrawRectangle(oPen, rectCheck);

					if (oNode.GetCheckBorderStyle() == CheckBoxBorderStyle.Dot)
					{
						oPen.DashStyle = DashStyle.Dot;
						oGraphics.DrawRectangle(oPen, rectCheck);
					}
				}
				else
				{
					if (oNode.GetCheckBorderStyle() == CheckBoxBorderStyle.Dot)					
						oPen.DashStyle = DashStyle.Dot;

					if (oNode.GetCheckBorderStyle() == CheckBoxBorderStyle.Solid || oNode.GetCheckBorderStyle() == CheckBoxBorderStyle.XP
						|| oNode.GetCheckBorderStyle() == CheckBoxBorderStyle.Dot)
						oGraphics.DrawEllipse(oPen, nX + 4, nY + 2, 10, 10);
				}

				oPen.DashStyle = DashStyle.Solid;
				#endregion

				m_mapItemCheckToRect.Add(oNode, rectCheck);
				m_mapRectToItemCheck.Add(rectCheck, oNode);

				if (oNode.Checked == true)
				{
					if (oNode.Parent == null || oNode.Parent.SubNodesCheckExclusive == false)
					{
						oPen.Color = Color.FromArgb(nAlpha, oNode.GetCheckCheckColor());
					
						oGraphics.DrawLine(oPen, nX + 6, nY + 6, nX + 6, nY + 8);
						oGraphics.DrawLine(oPen, nX + 7, nY + 7, nX + 7, nY + 9);
						oGraphics.DrawLine(oPen, nX + 8, nY + 8, nX + 8, nY + 10);
						oGraphics.DrawLine(oPen, nX + 9, nY + 7, nX + 9, nY + 9);
						oGraphics.DrawLine(oPen, nX + 10, nY + 6, nX + 10, nY + 8);
						oGraphics.DrawLine(oPen, nX + 11, nY + 5, nX + 11, nY + 7);
						oGraphics.DrawLine(oPen, nX + 12, nY + 4, nX + 12, nY + 6);
					}
					else
					{
						oPen.Color = Color.FromArgb(nAlpha, oNode.GetCheckCheckColor());

						oGraphics.DrawEllipse(oPen, nX + 6, nY + 4, 6, 6);
						oGraphics.DrawEllipse(oPen, nX + 7, nY + 5, 4, 4);
						oGraphics.DrawEllipse(oPen, nX + 8, nY + 6, 2, 2);						
						oGraphics.DrawEllipse(oPen, nX + 9, nY + 7, 1, 1);
						oGraphics.DrawRectangle(oPen, nX + 8, nY + 6, 2, 2);
					}
				}

				oPen.Dispose();

				nX += 17;
			}

			// paint the picture first if needed then draw the text
			if (oNode.Image != null)
			{								
				oGraphics.DrawImage(oNode.Image, 
					nX + 4, 
					nY + (int)(oTextSize.Height / 2.0) - (int)((float)oNode.Image.Height / 2.0) + 1,
					oNode.Image.Width, oNode.Image.Height);

				nX += oNode.Image.Width + 2;
			}
			else
			{
				if (oNode.ImageIndex != -1 && oNode.TreeView.ImageList != null 
					&& oNode.ImageIndex < oNode.TreeView.ImageList.Images.Count)
				{					
					oNode.TreeView.ImageList.Draw(oGraphics, nX + 2, 
						nY + (int)(oTextSize.Height / 2.0) - (int)((float)oNode.TreeView.ImageList.ImageSize.Height / 2.0), 
						oNode.TreeView.ImageList.ImageSize.Width,
						oNode.TreeView.ImageList.ImageSize.Height,
						oNode.ImageIndex);

					nX += oNode.TreeView.ImageList.ImageSize.Width;
				}
			}

			// get the right side of the text. if it is more far away than the width of the TreeView, truncate the text
			string sText = oNode.GetText();

			// render the text
			SolidBrush oTextBrush = new SolidBrush(Color.FromArgb(nAlpha, oNode.GetForeColor()));						

			// when draging the mouse over nodes, test if the node is being highlighted
			if (oNode == oNode.TreeView.HighlightedNode)
				oTextBrush.Color = oNode.GetHighlightedForeColor();

			Rectangle textRect = new Rectangle(nX + 2, nY, oNode.GetTreeView().GetDrawWidth() - nX - 15, 
				(int)oNode.GetTreeView().Height);

			SizeF textSize = oGraphics.MeasureString(StringDrawUtils.GetInstance().GetTextFromFormattedString(sText), oFont, 
				oNode.GetTreeView().GetDrawWidth() - nX - 15);			

			// clear the truncated flag
			oNode.TextTruncated = false;
 
			if (m_oTreeView.Multiline)
			{
				if (oNode.UseFormatting == true)
				{
					CharacterFormat chrFormat = new CharacterFormat(
						oFont, 
						oTextBrush, 
						0, 
						HotkeyPrefix.None, 
						true);

					ParagraphFormat paraFormat = new ParagraphFormat(
						ParagraphAlignment.Left, 
						ParagraphVerticalAlignment.Top, 
						true, 
						true, 
						StringTrimming.None, 
						Brushes.Transparent);

					StringDrawUtils.GetInstance().DrawStringInRectangle(oGraphics, sText, textRect, 
						chrFormat, paraFormat);
				}
				else
					oGraphics.DrawString(sText, oFont, oTextBrush, textRect);
			}
			else
			{
				sText = oNode.GetText();

				oTextSize = oGraphics.MeasureString(StringDrawUtils.GetInstance().GetTextFromFormattedString(sText), oFont);
				nTextWidth = (int)oTextSize.Width;

				if (nX + 2 + nTextWidth > m_oTreeView.m_LastNX)
					m_oTreeView.m_LastNX = nX + 2 + nTextWidth;

				if (nX + nTextWidth > oNode.GetTreeView().GetDrawWidth() - 15)
				{
					sText = StringDrawUtils.GetInstance().GetTextFromFormattedString(sText);

					float fChar = oTextSize.Width / float.Parse(sText.Length.ToString());

					int nChar = (int)((float)(nX + nTextWidth - oNode.GetTreeView().GetDrawWidth() + 15) / fChar) + 4;

					if (nChar < 0)
						nChar = 0;

					int nLength = sText.Length - nChar;
					if (nLength < 0)
						nLength = 0;

					sText = sText.Substring(0, nLength) + "...";
					oNode.TextTruncated = true;
				}
				
				// draw string (text) of the node in the proper system, based on the information whether it has formatting or not
				if (oNode.UseFormatting == true)
				{
					CharacterFormat chrFormat = new CharacterFormat(
						oFont, 
						oTextBrush, 
						0, 
						HotkeyPrefix.None, 
						true);

					StringDrawUtils.GetInstance().DrawString(oGraphics, sText,  new PointF(nX + 2, nY), 
						chrFormat, ParagraphAlignment.Left);
				}
				else
					oGraphics.DrawString(sText, oFont, oTextBrush, nX + 2, nY);				
			}

			if (m_oTreeView.Multiline == false)
				textSize = oGraphics.MeasureString(StringDrawUtils.GetInstance().GetTextFromFormattedString(sText), oFont);

			oTextBrush.Dispose();			

			// create the expand icon if specified
			if (oNode.GetShowSubitemsIndicator() == true && oNode.Nodes.Count > 0 && oNode.IsExpanded == false)
			{	
				int nIconX = (int)textRect.Left + (int)textSize.Width + 4;

				Pen oIndicatorPen = new Pen(Color.FromArgb(nAlpha, oNode.GetForeColor()), 1);
				oGraphics.DrawLine(oIndicatorPen, nIconX, nY + textSize.Height - 4, nIconX + 3, nY + textSize.Height - 4);
				oGraphics.DrawLine(oIndicatorPen, nIconX + 1, nY + textSize.Height - 5, nIconX + 3, nY + textSize.Height - 5);
				oGraphics.DrawLine(oIndicatorPen, nIconX + 2, nY + textSize.Height - 6, nIconX + 3, nY + textSize.Height - 6);
				oGraphics.DrawLine(oIndicatorPen, nIconX + 3, nY + textSize.Height - 6, nIconX + 3, nY + textSize.Height - 7);
				oIndicatorPen.Dispose();
			}

			if (oNode.Underline == true)
				PaintNodeUnderline(oNode, sText, oGraphics, nX, nY);
		}