1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace DncnetEdt
{
public class LineChangedEventArgs : EventArgs
{
public int NewLineNr;
public LineChangedEventArgs(int newLineNr)
{
NewLineNr = newLineNr;
}
}
public class TRTBPlus : RichTextBox
{
public delegate void LineChangedEventHandler(object sender, LineChangedEventArgs e);
public event LineChangedEventHandler LineChanged;
# region Propriétés privées
private bool EnableHighlightCurrentLine; //évite la récursivité sans fin dans HighlightCurrentLine quand il est appelé dans TRTBPlus_SelectionChanged
private int OldLineIndex; //permet de savoir si on a changé de ligne pour éviter les accès trop fréquents à HighlightCurrentLine
private int OldLineSelStart;
private int OldLineSelLength;
# endregion
# region Propriétés publiques
# endregion
# region Déclaration privéess
private void TRTBPlus_LineChanged(object sender, LineChangedEventArgs e)
{
HighlightCurrentLine(e.NewLineNr);
}
private void TRTBPlus_MouseUp(object sender, MouseEventArgs e)
{
//Desactive le surlignage de la ligne en cours (qui supprime la sélection après surlignage)
//pour permettre la sélection de plusieurs lignes
EnableHighlightCurrentLine = true;
}
private void TRTBPlus_MouseDown(object sender, MouseEventArgs e)
{
//Réactive le surlignage de la ligne en cours (desactivé dans MouseDown)
EnableHighlightCurrentLine = false;
}
private void TRTBPlus_SelectionChanged(object sender, EventArgs e)
{
int NoLine;
int FirstCar;
FirstCar = GetFirstCharIndexOfCurrentLine();
NoLine = GetLineFromCharIndex(FirstCar);
if ((NoLine != OldLineIndex) && (LineChanged != null))
{
LineChanged(this, new LineChangedEventArgs(NoLine));
}
//Doit rester en fin de fonction
OldLineIndex = NoLine;
}
private void HighlightCurrentLine(int NewLineNr)
{
Color OldCol;
int OldSelStart;
int FirstCar;
int LastCar;
int OldLineNr;
FirstCar = GetFirstCharIndexOfCurrentLine();
LastCar = GetFirstCharIndexFromLine(NewLineNr + 1);
if ((LastCar == -1) && (NewLineNr + 1 >= Lines.Length))
{
LastCar = Text.Length;
}
if (EnableHighlightCurrentLine && (LastCar >= FirstCar))
{
EnableHighlightCurrentLine = false; //pour éviter une récursivité dans TRTBPlus_SelectionChanged
OldSelStart = SelectionStart;
//TODO prendre la couleur définie par défaut
OldCol = Color.FromKnownColor(KnownColor.Window);
//déselection de l'ancienne ligne
SelectionStart = OldLineSelStart;
OldLineNr = GetLineFromCharIndex(OldLineSelStart);
//Si on n'a pas encore de ligne dans le fichier on ne traite pas (nouveau fichier)
if (OldLineNr < Lines.Length)
{
SelectionLength = Lines[OldLineNr].Length;
SelectionBackColor = OldCol;
SelectionLength = 0;
}
SelectionStart = FirstCar;
OldLineSelStart = SelectionStart;
SelectionLength = LastCar - FirstCar;
OldLineSelLength = SelectionLength;
SelectionBackColor = Color.FromKnownColor(KnownColor.ActiveBorder);
SelectionLength = 0;
SelectionStart = OldSelStart; //remise du curseur à son ancienne position
EnableHighlightCurrentLine = true;
}
}
# endregion
# region Déclaration publiques
public TRTBPlus()
: base()
{
EnableHighlightCurrentLine = true;
SelectionChanged += new EventHandler(TRTBPlus_SelectionChanged);
MouseDown += new MouseEventHandler(TRTBPlus_MouseDown);
MouseUp += new MouseEventHandler(TRTBPlus_MouseUp);
TextChanged += new EventHandler(TRTBPlus_TextChanged);
LineChanged += new LineChangedEventHandler(TRTBPlus_LineChanged);
KeyPress += new KeyPressEventHandler(TRTBPlus_KeyPress);
}
/// <summary>
/// Surcharge spéciale de la fonction LoadFile qui met en évidence la première ligne
/// d'un fichier à son ouverture. Le type de texte est forcé à PlainText
/// </summary>
/// <param name="path">Chemin du nouveau fichier</param>
public new void LoadFile(string path)
{
LoadFile(path, RichTextBoxStreamType.PlainText);
OldLineIndex = -1;
HighlightCurrentLine(0);
}
# endregion
} |
Partager