⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textview.cs

📁 c#源代码
💻 CS
📖 第 1 页 / 共 3 页
字号:
					break;
			}
		}
		
		/// <summary>
		/// Get the marker brush (for solid block markers) at a given position.
		/// </summary>
		/// <param name="offset">The offset.</param>
		/// <param name="length">The length.</param>
		/// <param name="markers">All markers that have been found.</param>
		/// <returns>The Brush or null when no marker was found.</returns>
		Brush GetMarkerBrushAt(int offset, int length, out ArrayList markers)
		{
			markers = Document.MarkerStrategy.GetMarkers(offset, length);
			foreach (TextMarker marker in markers) {
				if (marker.TextMarkerType == TextMarkerType.SolidBlock) {
					return BrushRegistry.GetBrush(marker.Color);
				}
			}
			return null;
		}
		
		
		float PaintLinePart(Graphics g, int lineNumber, int startColumn, int endColumn, Rectangle lineRectangle, float physicalXPos)
		{
			bool  drawLineMarker  = DrawLineMarkerAtLine(lineNumber);
			Brush bgColorBrush    = GetBgColorBrush(lineNumber);
			Brush backgroundBrush = textArea.Enabled ? bgColorBrush : SystemBrushes.InactiveBorder;
			
			HighlightColor selectionColor = textArea.Document.HighlightingStrategy.GetColorFor("Selection");
			ColumnRange    selectionRange = textArea.SelectionManager.GetSelectionAtLine(lineNumber);
			HighlightColor tabMarkerColor   = textArea.Document.HighlightingStrategy.GetColorFor("TabMarkers");
			HighlightColor spaceMarkerColor = textArea.Document.HighlightingStrategy.GetColorFor("SpaceMarkers");
			
			float spaceWidth   = GetWidth(g, ' ');
			
			LineSegment currentLine    = textArea.Document.GetLineSegment(lineNumber);
			
			int logicalColumn  = startColumn;
			
			Brush selectionBackgroundBrush  = BrushRegistry.GetBrush(selectionColor.BackgroundColor);
			Brush unselectedBackgroundBrush = backgroundBrush;

			if (currentLine.Words != null) {
				int startword = 0;
				// search the first word after startColumn and update physicalColumn if a word is Tab
				int wordOffset = 0;
				for (; startword < currentLine.Words.Count; ++startword) {
					if (wordOffset >= startColumn) {
						break;
					}
					TextWord currentWord = ((TextWord)currentLine.Words[startword]);
					if (currentWord.Type == TextWordType.Tab) {
						++wordOffset;
					} else if (currentWord.Type == TextWordType.Space) {
						++wordOffset;
					} else {
						wordOffset     += currentWord.Length;
					}
				}
				
				
				for (int i = startword; i < currentLine.Words.Count; ++i) {
					
					// if already all words before endColumn are drawen: break
					if (logicalColumn >= endColumn) {
						break;
					}
					
					ArrayList markers = Document.MarkerStrategy.GetMarkers(currentLine.Offset + wordOffset);
					foreach (TextMarker marker in markers) {
						if (marker.TextMarkerType == TextMarkerType.SolidBlock) {
//							if (unselectedBackgroundBrush != null) {
//								unselectedBackgroundBrush.Dispose();
//							}
							unselectedBackgroundBrush = BrushRegistry.GetBrush(marker.Color);
							break;
						}
					}
					// Clear old marker arrary		
					
					
					// TODO: cut the word if startColumn or endColimn is in the word;
					// needed for foldings wich can start or end in the middle of a word
					TextWord currentWord = ((TextWord)currentLine.Words[i]);
					switch (currentWord.Type) {
						case TextWordType.Space:
							RectangleF spaceRectangle = new RectangleF(physicalXPos, lineRectangle.Y, (float)Math.Ceiling(spaceWidth), lineRectangle.Height);
							
							Brush spaceBackgroundBrush;
							
							if (ColumnRange.WholeColumn.Equals(selectionRange) || logicalColumn >= selectionRange.StartColumn && logicalColumn < selectionRange.EndColumn) {
								spaceBackgroundBrush = selectionBackgroundBrush;
							} else {
								Brush markerBrush = GetMarkerBrushAt(currentLine.Offset + logicalColumn, 1, out markers);
								if (!drawLineMarker && markerBrush != null) {
									spaceBackgroundBrush = markerBrush;
								} else if (!drawLineMarker && currentWord.SyntaxColor != null && currentWord.SyntaxColor.HasBackground) {
									spaceBackgroundBrush = BrushRegistry.GetBrush(currentWord.SyntaxColor.BackgroundColor);
								} else {
									spaceBackgroundBrush = unselectedBackgroundBrush;
								}
							}
							g.FillRectangle(spaceBackgroundBrush, spaceRectangle);
							
							if (TextEditorProperties.ShowSpaces) {
								DrawSpaceMarker(g, spaceMarkerColor.Color, physicalXPos, lineRectangle.Y);
							}
							foreach (TextMarker marker in markers) {
								DrawMarker(g, marker, spaceRectangle);
							}
							
							physicalXPos += spaceWidth;
							
							++logicalColumn;
							++physicalColumn;
							break;
						
						case TextWordType.Tab:
							
							int oldPhysicalColumn = physicalColumn;
							physicalColumn += TextEditorProperties.TabIndent;
							physicalColumn = (physicalColumn / TextEditorProperties.TabIndent) * TextEditorProperties.TabIndent;
							float tabWidth = (physicalColumn - oldPhysicalColumn) * spaceWidth;
							RectangleF tabRectangle = new RectangleF(physicalXPos, lineRectangle.Y, (float)Math.Ceiling(tabWidth), lineRectangle.Height);
							
							if (ColumnRange.WholeColumn.Equals(selectionRange) || logicalColumn >= selectionRange.StartColumn && logicalColumn <= selectionRange.EndColumn - 1) {
								spaceBackgroundBrush = selectionBackgroundBrush;
							} else {
								Brush markerBrush = GetMarkerBrushAt(currentLine.Offset + logicalColumn, 1, out markers);
								if (!drawLineMarker && markerBrush != null) {
									spaceBackgroundBrush = markerBrush;
								} else if (!drawLineMarker && currentWord.SyntaxColor != null && currentWord.SyntaxColor.HasBackground) {
									spaceBackgroundBrush = BrushRegistry.GetBrush(currentWord.SyntaxColor.BackgroundColor);
								} else {
									spaceBackgroundBrush = unselectedBackgroundBrush;
								}
							}
							g.FillRectangle(spaceBackgroundBrush, tabRectangle);
							
							if (TextEditorProperties.ShowTabs) {
								DrawTabMarker(g, tabMarkerColor.Color, physicalXPos, lineRectangle.Y);
							}
							
							foreach (TextMarker marker in markers) {
								DrawMarker(g, marker, tabRectangle);
							}
							
							physicalXPos += tabWidth;
							
							++logicalColumn;
							break;
						
						case TextWordType.Word:
							string word    = currentWord.Word;
							float  lastPos = physicalXPos;
							
							Brush bgMarkerBrush = GetMarkerBrushAt(currentLine.Offset + logicalColumn,  word.Length, out markers);
							Brush wordBackgroundBrush;
							if (!drawLineMarker && bgMarkerBrush != null) {
								wordBackgroundBrush = bgMarkerBrush;
							} else if (!drawLineMarker && currentWord.SyntaxColor.HasBackground) {
								wordBackgroundBrush = BrushRegistry.GetBrush(currentWord.SyntaxColor.BackgroundColor);
							} else {
								wordBackgroundBrush = unselectedBackgroundBrush;
							}
							
							
							if (ColumnRange.WholeColumn.Equals(selectionRange) || selectionRange.EndColumn - 1  >= word.Length + logicalColumn &&
							                                                      selectionRange.StartColumn <= logicalColumn) {
								physicalXPos += DrawDocumentWord(g,
								                                 word,
								                                 new PointF(physicalXPos, lineRectangle.Y),
								                                 currentWord.Font,
								                                 selectionColor.HasForgeground ? selectionColor.Color : currentWord.Color,
								                                 selectionBackgroundBrush);
							} else {
								if (ColumnRange.NoColumn.Equals(selectionRange)  /* || selectionRange.StartColumn > logicalColumn + word.Length || selectionRange.EndColumn  - 1 <= logicalColumn */) {
									physicalXPos += DrawDocumentWord(g,
									                                 word,
									                                 new PointF(physicalXPos, lineRectangle.Y),
									                                 currentWord.Font,
									                                 currentWord.Color,
									                                 wordBackgroundBrush);
								} else {
									int offset1 = Math.Min(word.Length, Math.Max(0, selectionRange.StartColumn - logicalColumn ));
									int offset2 = Math.Max(offset1, Math.Min(word.Length, selectionRange.EndColumn - logicalColumn));
									
									physicalXPos += DrawDocumentWord(g,
									                                 word.Substring(0, offset1),
									                                 new PointF(physicalXPos, lineRectangle.Y),
									                                 currentWord.Font,
									                                 currentWord.Color,
									                                 wordBackgroundBrush);
									
									physicalXPos += DrawDocumentWord(g,
									                                 word.Substring(offset1, offset2 - offset1),
									                                 new PointF(physicalXPos, lineRectangle.Y),
									                                 currentWord.Font,
									                                 selectionColor.HasForgeground ? selectionColor.Color : currentWord.Color,
									                                 selectionBackgroundBrush);
									
									physicalXPos += DrawDocumentWord(g,
									                                 word.Substring(offset2),
									                                 new PointF(physicalXPos, lineRectangle.Y),
									                                 currentWord.Font,
									                                 currentWord.Color,
									                                 wordBackgroundBrush);
								}
							}
//							if (markerBrush != null) {
//								markerBrush.Dispose();
//							}
							foreach (TextMarker marker in markers) {
								if (marker.TextMarkerType != TextMarkerType.SolidBlock) {
									DrawMarker(g, marker, new RectangleF(lastPos, lineRectangle.Y, (physicalXPos - lastPos), lineRectangle.Height));
								}
							}
							
							// draw bracket highlight
							if (highlight != null) {
								if (highlight.OpenBrace.Y == lineNumber && highlight.OpenBrace.X == logicalColumn ||
								    highlight.CloseBrace.Y == lineNumber && highlight.CloseBrace.X == logicalColumn) {
									DrawBracketHighlight(g, new Rectangle((int)lastPos, lineRectangle.Y, (int)(physicalXPos - lastPos) - 1, lineRectangle.Height - 1));
								}
							}
							physicalColumn += word.Length;
							logicalColumn += word.Length;
							break;
					}
					//markers.Clear();
				}
			}
			
//			if (bgColorBrush != null) {
//				bgColorBrush.Dispose();
//				bgColorBrush = null;
//			}
//			
//			if (selectionBackgroundBrush != null) {
//				selectionBackgroundBrush.Dispose();
//				selectionBackgroundBrush = null;
//			}
//			
//			if (unselectedBackgroundBrush != null) {
//				unselectedBackgroundBrush.Dispose();
//				unselectedBackgroundBrush = null;
//			}
		
			return physicalXPos;
		}
		
		float DrawDocumentWord(Graphics g, string word, PointF position, Font font, Color foreColor, Brush backBrush)
		{
			if (word == null || word.Length == 0) {
				return 0f;
			}
			float wordWidth = g.MeasureString(word, font, 32768, measureStringFormat).Width;
			g.FillRectangle(backBrush,
			                new RectangleF(position.X, position.Y, (float)Math.Ceiling(wordWidth), FontHeight));
			
			g.DrawString(word,
			             font,
			             BrushRegistry.GetBrush(foreColor),
			             position.X,
			             position.Y, 
			             measureStringFormat);
			return wordWidth;
		}
#endregion
		
#region Conversion Functions
		public float GetWidth(char ch)
		{
			if (!charWitdh.ContainsKey(ch)) {
				using (Graphics g = textArea.CreateGraphics()) {
					return GetWidth(g, ch);
				}
			}
			return (float)charWitdh[ch];
		}
		
		public float GetWidth(string text)
		{
			float width = 0;
			for (int i = 0; i < text.Length; ++i) {
				width += GetWidth(text[i]);
			}
			return width;
		}
		
		public float GetWidth(Graphics g, char ch)
		{
			if (!charWitdh.ContainsKey(ch)) {
				charWitdh.Add(ch, g.MeasureString(ch.ToString(), TextEditorProperties.Font, 2000, measureStringFormat).Width);
			}
			return (float)charWitdh[ch];
		}
		
		public int GetVisualColumn(int logicalLine, int logicalColumn)
		{
			return GetVisualColumn(Document.GetLineSegment(logicalLine), logicalColumn);
		}
		public int GetVisualColumn(LineSegment line, int logicalColumn)
		{
			int tabIndent = Document.TextEditorProperties.TabIndent;
			int column    = 0;
			for (int i = 0; i < logicalColumn; ++i) {
				char ch;
				if (i >= line.Length) {
					ch = ' ';
				} else {
					ch = Document.GetCharAt(line.Offset + i);
				}
				
				switch (ch) {
					case '\t':
						int oldColumn = column;
						column += tabIndent;

⌨️ 快捷键说明

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