| 0: | // Bookmarks.cs | |
| 1: | // Copyright (c) 2001 Mike Krueger | |
| 2: | // | |
| 3: | // This program is free software; you can redistribute it and/or modify | |
| 4: | // it under the terms of the GNU General Public License as published by | |
| 5: | // the Free Software Foundation; either version 2 of the License, or | |
| 6: | // (at your option) any later version. | |
| 7: | // | |
| 8: | // This program is distributed in the hope that it will be useful, | |
| 9: | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 10: | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 11: | // GNU General Public License for more details. | |
| 12: | // | |
| 13: | // You should have received a copy of the GNU General Public License | |
| 14: | // along with this program; if not, write to the Free Software | |
| 15: | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 16: | ||
| 17: | using System; | |
| 18: | using System.Collections; | |
| 19: | ||
| 20: | namespace SharpDevelop.Internal.Text { | |
| 21: | ||
| 22: | /// <summary> | |
| 23: | /// This class handles the bookmarks for a buffer | |
| 24: | /// </summary> | |
| 25: | public class Bookmark | |
| 26: | { | |
| 27: | ArrayList bookmark = new ArrayList(); | |
| 28: | ||
| 29: | public event EventHandler BeforeChanged; | |
| 30: | public event EventHandler Changed; | |
| 31: | ||
| 32: | public ArrayList Marks { | |
| 33: | get { | |
| 34: | return bookmark; | |
| 35: | } | |
| 36: | } | |
| 37: | ||
| 38: | public Bookmark(TextBuffer buffer) | |
| 39: | { | |
| 40: | buffer.MoveIndices += new MoveIndicesHandler(MoveIndices); | |
| 41: | } | |
| 42: | ||
| 43: | void OnChanged() | |
| 44: | { | |
| 45: | if (Changed != null) | |
| 46: | Changed(this, null); | |
| 47: | } | |
| 48: | void OnBeforeChanged() | |
| 49: | { | |
| 50: | if (BeforeChanged != null) | |
| 51: | BeforeChanged(this, null); | |
| 52: | } | |
| 53: | ||
| 54: | /// <summary> | |
| 55: | /// If a mark exists at position num it will be removed, if not it will be inserted | |
| 56: | /// </summary> | |
| 57: | public void ToggleMark(int num) | |
| 58: | { | |
| 59: | OnBeforeChanged(); | |
| 60: | for (int i = 0; i < bookmark.Count; ++i) { | |
| 61: | if ((int)bookmark[i] == num) { | |
| 62: | bookmark.RemoveAt(i); | |
| 63: | OnChanged(); | |
| 64: | return; | |
| 65: | } | |
| 66: | } | |
| 67: | bookmark.Add(num); | |
| 68: | OnChanged(); | |
| 69: | } | |
| 70: | ||
| 71: | /// <returns> | |
| 72: | /// true, if a mark at mark exists, otherwise false | |
| 73: | /// </returns> | |
| 74: | public bool IsMark(int mark) | |
| 75: | { | |
| 76: | for (int i = 0; i < bookmark.Count; ++i) | |
| 77: | if ((int)bookmark[i] == mark) | |
| 78: | return true; | |
| 79: | return false; | |
| 80: | } | |
| 81: | ||
| 82: | /// <summary> | |
| 83: | /// This method moves all indices from index upward count lines | |
| 84: | /// (useful for deletion/insertion of text) | |
| 85: | /// </summary> | |
| 86: | public void MoveIndices(int index, int count) | |
| 87: | { | |
| 88: | bool changed = false; | |
| 89: | OnBeforeChanged(); | |
| 90: | for (int i = 0; i < bookmark.Count; ++i) { | |
| 91: | int mark = (int)bookmark[i]; | |
| 92: | if (count < 0 && mark == index) { | |
| 93: | bookmark.RemoveAt(i); | |
| 94: | --i; | |
| 95: | changed = true; | |
| 96: | } else | |
| 97: | if (mark >= index) { | |
| 98: | changed = true; | |
| 99: | bookmark[i] = mark + count; | |
| 100: | } | |
| 101: | } | |
| 102: | if (changed) | |
| 103: | OnChanged(); | |
| 104: | } | |
| 105: | ||
| 106: | /// <summary> | |
| 107: | /// Clears all bookmark | |
| 108: | /// </summary> | |
| 109: | public void ClearAll() | |
| 110: | { | |
| 111: | OnBeforeChanged(); | |
| 112: | bookmark.Clear(); | |
| 113: | OnChanged(); | |
| 114: | } | |
| 115: | ||
| 116: | /// <returns> | |
| 117: | /// returns the lowest mark, if no marks exists it returns -1 | |
| 118: | /// </returns> | |
| 119: | public int FirstMark() | |
| 120: | { | |
| 121: | if (bookmark.Count < 1) | |
| 122: | return -1; | |
| 123: | int first = (int)bookmark[0]; | |
| 124: | for (int i = 1; i < bookmark.Count; ++i) | |
| 125: | first = Math.Min(first, (int)bookmark[i]); | |
| 126: | return first; | |
| 127: | } | |
| 128: | ||
| 129: | /// <returns> | |
| 130: | /// returns the highest mark, if no marks exists it returns -1 | |
| 131: | /// </returns> | |
| 132: | public int LastMark() | |
| 133: | { | |
| 134: | if (bookmark.Count < 1) | |
| 135: | return -1; | |
| 136: | int last = (int)bookmark[0]; | |
| 137: | for (int i = 1; i < bookmark.Count; ++i) | |
| 138: | last = Math.Max(last, (int)bookmark[i]); | |
| 139: | return last; | |
| 140: | } | |
| 141: | ||
| 142: | /// <returns> | |
| 143: | /// returns the next mark > cur, if it not exists it returns FirstMark() | |
| 144: | /// </returns> | |
| 145: | public int NextMark(int cur) | |
| 146: | { | |
| 147: | int next = -1; | |
| 148: | for (int i = 0; i < bookmark.Count; ++i) { | |
| 149: | int j = (int)bookmark[i]; | |
| 150: | if (j > cur) | |
| 151: | next = next == -1 ? j : Math.Min(next, j); | |
| 152: | } | |
| 153: | return next == -1 ? FirstMark() : next; | |
| 154: | } | |
| 155: | ||
| 156: | /// <returns> | |
| 157: | /// returns the next mark lower than cur, if it not exists it returns LastMark() | |
| 158: | /// </returns> | |
| 159: | public int PrevMark(int cur) | |
| 160: | { | |
| 161: | int prev = -1; | |
| 162: | for (int i = 0; i < bookmark.Count; ++i) { | |
| 163: | int j = (int)bookmark[i]; | |
| 164: | if (j < cur) | |
| 165: | prev = prev == -1 ? j : Math.Max(prev, j); | |
| 166: | } | |
| 167: | return prev == -1 ? LastMark() : prev; | |
| 168: | } | |
| 169: | } | |
| 170: | } |
This page was automatically generated by SharpDevelop.