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

📄 bookmark.cs

📁 c#精彩编程百例(源代码)
💻 CS
字号:
//  Bookmarks.cs
//  Copyright (c) 2001 Mike Krueger
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Collections;

namespace SharpDevelop.Internal.Text {
	
	/// <summary>
	/// This class handles the bookmarks for a buffer
	/// </summary>
	public class Bookmark
	{
		ArrayList bookmark = new ArrayList();
		
		public event EventHandler BeforeChanged;
		public event EventHandler Changed;
		
		public ArrayList Marks {
			get {
				return bookmark;
			}
		}
			
		public Bookmark(TextBuffer buffer)
		{
			buffer.MoveIndices += new MoveIndicesHandler(MoveIndices);
		}
		
		void OnChanged() 
		{
			if (Changed != null)
				Changed(this, null);
		}
		void OnBeforeChanged() 
		{
			if (BeforeChanged != null)
				BeforeChanged(this, null);
		}
			
		/// <summary>
		/// If a mark exists at position <num> it will be removed, if not it will be inserted
		/// </summary>
		public void ToggleMark(int num)
		{
			OnBeforeChanged();
			for (int i = 0; i < bookmark.Count; ++i) {
				if ((int)bookmark[i] == num) {
					bookmark.RemoveAt(i);
					OnChanged();
					return;
				}
			}
			bookmark.Add(num);
			OnChanged();
		}
		
		/// <returns>
		/// true, if a mark at <mark> exists, otherwise false
		/// </returns>
		public bool IsMark(int mark)
		{
			for (int i = 0; i < bookmark.Count; ++i) 
				if ((int)bookmark[i] == mark) 
					return true;
			return false;
		}
		
		/// <summary>
		/// This method moves all indices from index upward count lines
		/// (useful for deletion/insertion of text)
		/// </summary>
		public void MoveIndices(int index, int count)
		{
			bool changed = false;
			OnBeforeChanged();
			for (int i = 0; i < bookmark.Count; ++i) {
				int mark = (int)bookmark[i];
				if (count < 0 && mark == index) {
					bookmark.RemoveAt(i);
					--i;
					changed = true;
				} else
				if (mark >= index)  {
					changed = true;
					bookmark[i] = mark + count;
				}
			}
			if (changed)
				OnChanged();
		}
		
		/// <summary>
		/// Clears all bookmark
		/// </summary>
		public void ClearAll()
		{
			OnBeforeChanged();
			bookmark.Clear();
			OnChanged();
		}
		
		/// <returns>
		/// returns the lowest mark, if no marks exists it returns -1
		/// </returns>
		public int FirstMark()
		{
			if (bookmark.Count < 1)
				return -1;
			int first = (int)bookmark[0];
			for (int i = 1; i < bookmark.Count; ++i) 
				first = Math.Min(first, (int)bookmark[i]);
			return first;
		}
		
		/// <returns>
		/// returns the highest mark, if no marks exists it returns -1
		/// </returns>
		public int LastMark()
		{
			if (bookmark.Count < 1)
				return -1;
			int last = (int)bookmark[0];
			for (int i = 1; i < bookmark.Count; ++i) 
				last = Math.Max(last, (int)bookmark[i]);
			return last;
		}
		
		/// <returns>
		/// returns the next mark > cur, if it not exists it returns FirstMark()
		/// </returns>
		public int NextMark(int cur)
		{
			int next = -1;
			for (int i = 0; i < bookmark.Count; ++i) {
				int j = (int)bookmark[i];
				if (j > cur)
					next = next == -1 ? j : Math.Min(next, j);
			}
			return next == -1 ? FirstMark() : next;
		}
		
		/// <returns>
		/// returns the next mark < cur, if it not exists it returns LastMark()
		/// </returns>
		public int PrevMark(int cur)
		{
			int prev = -1;
			for (int i = 0; i < bookmark.Count; ++i) {
				int j = (int)bookmark[i];
				if (j < cur)
					prev = prev == -1 ? j : Math.Max(prev, j);
			}
			return prev == -1 ? LastMark() : prev;
		}
	}
}

⌨️ 快捷键说明

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