formatlabelcontrol.cs

来自「Fireball.CodeEditor is an source code ed」· CS 代码 · 共 1,329 行 · 第 1/3 页

CS
1,329
字号
			bbuff.Dispose();
		}

		private GDIFont GetFont(Font font)
		{
			GDIFont gf = (GDIFont) _Fonts[GetFontKey(font)];
			if (gf == null)
			{
				gf = new GDIFont(font.Name, font.Size, font.Bold, font.Italic, font.Underline, false);
				_Fonts[GetFontKey(font)] = gf;
			}
			return gf;
		}

		private string GetFontKey(Font font)
		{
			return font.Name + font.Bold.ToString() + font.Italic.ToString() + font.Underline.ToString() + font.Size.ToString();

		}


		private void CreateRows()
		{
			if (_Elements != null)
			{
				int x = 0;
				_Rows = new ArrayList();

				//build rows---------------------------------------------
				FormatLabelRow row = new FormatLabelRow();
				_Rows.Add(row);
				bool WhiteSpace = false;
				foreach (FormatLabelElement Element in _Elements)
				{
					if (Element.words == null)
						return;

					if (Element.NewLine)
					{
						//tag forces a new line
						x = 0;
						row = new FormatLabelRow();
						_Rows.Add(row);
						WhiteSpace = true;
					}
					if (Element.TagName == "hr")
					{
						row.RenderSeparator = true;
					}

					//else
					//{


					foreach (FormatLabelWord word in Element.words)
					{
						if (WordWrap)
						{
							int scrollwdh = 0;
							if (ScrollBars == ScrollBars.Both || ScrollBars == ScrollBars.Vertical)
								scrollwdh = vScroll.Width;

							if ((word.Width + x) > this.ClientWidth - LabelMargin - scrollwdh)
							{
								//new line due to wordwrap
								x = 0;
								row = new FormatLabelRow();
								_Rows.Add(row);
								WhiteSpace = true;
							}
						}

						if (word.Text.Replace(" ", "") != "" || word.Image != null)
							WhiteSpace = false;

						if (!WhiteSpace)
						{
							row.Words.Add(word);

							x += word.Width;
						}
					}
					//}
				}

				//apply width and height to all rows
				int index = 0;
				foreach (FormatLabelRow r in this._Rows)
				{
					int width = 0;
					int height = 0;
					int padd = 0;

					if (index > 0)
					{
						int previndex = index - 1;
						FormatLabelRow prev = (FormatLabelRow) _Rows[previndex];
						while (previndex >= 0 && prev.Words.Count == 0)
						{
							prev = (FormatLabelRow) _Rows[previndex];
							previndex--;
						}

						if (previndex >= 0)
						{
							prev = (FormatLabelRow) _Rows[previndex];
							if (prev.Words.Count > 0)
							{
								FormatLabelWord w = (FormatLabelWord) prev.Words[prev.Words.Count - 1];
								height = w.Height;
							}
						}

					}


					foreach (FormatLabelWord w in r.Words)
					{
						if (w.Height > height && (w.Text != ""))
							height = w.Height;

						width += w.Width;

					}
					r.Height = height;

					int MaxImageH = 0;
					foreach (FormatLabelWord w in r.Words)
					{
						if (w.Image != null)
						{
							if (w.Height > height)
								MaxImageH = w.Height;
						}
					}

					foreach (FormatLabelWord w in r.Words)
					{
						int imgH = 0;
						int imgPadd = 0;
						if (w.Image != null)
						{
							string valign = GetAttrib("valign", w.Element.Tag);
							switch (valign)
							{
								case "top":
									{
										imgH = r.Height;
										imgPadd = w.Height - imgH;
										break;
									}
								case "middle":
								case "center":
									{
										int tmp = 0;
										imgH = r.Height;
										tmp = (w.Height - imgH)/2;
										imgH += tmp;
										imgPadd = tmp;

										break;
									}
								case "bottom":
									{
										imgH = w.Height;
										imgPadd = 0;
										break;
									}
								default:
									{
										imgH = w.Height;
										imgPadd = 0;
										break;
									}

							}

							if (imgH > height)
								height = imgH;

							if (imgPadd > padd)
								padd = imgPadd;


							width += w.Width;
						}
					}
					r.Width = width;
					r.Height = height;
					r.BottomPadd = padd;
					index++;
				}

				this.vScroll.Maximum = this._Rows.Count;
			}
		}


		private void InitScrollbars()
		{
			if (vScroll == null || hScroll == null)
				return;

			if (this.ScrollBars == ScrollBars.Both)
			{
				vScroll.Left = this.ClientWidth - vScroll.Width;
				vScroll.Top = 0;
				vScroll.Height = this.ClientHeight - hScroll.Height;

				hScroll.Left = 0;
				hScroll.Top = this.ClientHeight - hScroll.Height;
				hScroll.Width = this.ClientWidth - vScroll.Width;

				Filler.Left = vScroll.Left;
				Filler.Top = hScroll.Top;

				Filler.Visible = true;
				vScroll.Visible = true;
				hScroll.Visible = true;
			}
			else if (this.ScrollBars == ScrollBars.Vertical)
			{
				vScroll.Left = this.ClientWidth - vScroll.Width;
				vScroll.Top = 0;
				vScroll.Height = this.ClientHeight;

				hScroll.Left = 0;
				hScroll.Top = this.ClientHeight - hScroll.Height;
				hScroll.Width = this.ClientWidth - vScroll.Width;

				Filler.Left = vScroll.Left;
				Filler.Top = hScroll.Top;

				Filler.Visible = false;
				vScroll.Visible = true;
				hScroll.Visible = false;

			}
			else if (this.ScrollBars == ScrollBars.Horizontal)
			{
				vScroll.Left = this.ClientWidth - vScroll.Width;
				vScroll.Top = 0;
				vScroll.Height = this.ClientHeight;

				hScroll.Left = 0;
				hScroll.Top = this.ClientHeight - hScroll.Height;
				hScroll.Width = this.ClientWidth;

				Filler.Left = vScroll.Left;
				Filler.Top = hScroll.Top;

				Filler.Visible = false;
				vScroll.Visible = false;
				hScroll.Visible = true;

			}
			else if (this.ScrollBars == ScrollBars.None)
			{
				vScroll.Left = this.ClientWidth - vScroll.Width;
				vScroll.Top = 0;
				vScroll.Height = this.ClientHeight;

				hScroll.Left = 0;
				hScroll.Top = this.ClientHeight - hScroll.Height;
				hScroll.Width = this.ClientWidth;

				Filler.Left = vScroll.Left;
				Filler.Top = hScroll.Top;

				Filler.Visible = false;
				vScroll.Visible = false;
				hScroll.Visible = false;

			}

		}


		protected override void OnResize(EventArgs e)
		{
			try
			{
				InitScrollbars();
				SetAutoSize();

			}
			catch
			{
			}
			CreateRows();
			base.OnResize(e);
		}

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            if (this.IsHandleCreated)
                RedrawBuffer();
        }
		protected override void OnMouseUp(MouseEventArgs e)
		{
			int y = e.Y;
			int x = e.X;

			int index = 0;
			bool Link = false;
			//this.Cursor =Cursors.Arrow;
			_ActiveElement = null;
			if (this._Rows != null)
			{
				foreach (FormatLabelRow r in this._Rows)
				{
					if (y >= r.Top && y <= r.Top + r.Height)
					{
						foreach (FormatLabelWord w in r.Words)
						{
							if (y >= w.ScreenArea.Top && y <= w.ScreenArea.Bottom)
							{
								if (x >= w.ScreenArea.Left && x <= w.ScreenArea.Right)
								{
									//MessageBox.Show (w.Text);
									if (w.Element.Link != null)
									{
										Link = true;
										_ActiveElement = w.Element.Link;
										break;
									}
									//this.Cursor =Cursors.Hand;

								}
							}
						}
						break;
					}
					index++;
				}
			}
			if (Link)
			{
				this.Cursor = Cursors.Hand;
				this.Invalidate();
				OnClickLink(GetAttrib("href", _ActiveElement.Tag));
			}
			else
			{
				this.Cursor = Cursors.Arrow;
				this.Invalidate();
			}


			base.OnMouseUp(e);
		}

		protected override void OnMouseMove(MouseEventArgs e)
		{
			int y = e.Y;
			int x = e.X;

			int index = 0;
			bool Link = false;
			//this.Cursor =Cursors.Arrow;
			_ActiveElement = null;
			if (this._Rows != null)
			{
				foreach (FormatLabelRow r in this._Rows)
				{
					if (y >= r.Top && y <= r.Top + r.Height)
					{
						foreach (FormatLabelWord w in r.Words)
						{
							if (y >= w.ScreenArea.Top && y <= w.ScreenArea.Bottom)
							{
								if (x >= w.ScreenArea.Left && x <= w.ScreenArea.Right)
								{
									//MessageBox.Show (w.Text);
									if (w.Element.Link != null)
									{
										Link = true;
										_ActiveElement = w.Element.Link;
										break;
									}
									//this.Cursor =Cursors.Hand;									
								}
							}
						}
						break;
					}
					index++;
				}
			}
			if (Link)
			{
				this.Cursor = Cursors.Hand;
				this.Invalidate();
			}
			else
			{
				this.Cursor = Cursors.Arrow;
				this.Invalidate();
			}
			base.OnMouseMove(e);
		}

		private void vScroll_Scroll(object sender, ScrollEventArgs e)
		{
			this.Invalidate();
		}


		public ScrollBars ScrollBars
		{
			get { return _ScrollBars; }
			set
			{
				_ScrollBars = value;
				InitScrollbars();
			}
		}

		public int GetWidth()
		{
			int max = 0;
			foreach (FormatLabelRow r in this._Rows)
			{
				if (r.Width > max)
					max = r.Width;
			}

			return max + LabelMargin*2 + this.BorderWidth*2;
		}

		public int GetHeight()
		{
			int max = 0;
			foreach (FormatLabelRow r in this._Rows)
			{
				max += r.Height;
			}

			return max + LabelMargin*2 + this.BorderWidth*2;
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?