/
NoteStatsDialog.cs
226 lines (181 loc) · 5.97 KB
/
NoteStatsDialog.cs
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//
// NoteStatsDialog.cs
//
// Author:
// Eitan Isaacson <eitan@monotonous.org>
//
// Copyright (c) 2009 Eitan Isaacson
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using Tomboy;
using System.Text.RegularExpressions;
using Gtk;
namespace Tomboy.NoteStatistics
{
public partial class NoteStatsDialog : Gtk.Dialog
{
private Note note;
private bool needs_refresh;
private struct StringStatistics
{
public int Lines;
public int Words;
public int CharsWithSpaces;
public int CharsWithoutSpaces;
public StringStatistics (int lines, int words, int chars_with_spaces, int chars_without_spaces)
{
Lines = lines;
Words = words;
CharsWithSpaces = chars_with_spaces;
CharsWithoutSpaces = chars_without_spaces;
}
public static StringStatistics operator -(StringStatistics stats1, StringStatistics stats2)
{
return new StringStatistics (stats1.Lines - stats2.Lines,
stats1.Words - stats2.Words,
stats1.CharsWithSpaces - stats2.CharsWithSpaces,
stats1.CharsWithoutSpaces - stats2.CharsWithoutSpaces);
}
}
protected virtual void OnResponse (object o, Gtk.ResponseArgs args)
{
Destroy ();
}
private void Refresh ()
{
StringStatistics stats = GetStatistics (countStrikeout.Active);
noteLines.Text = stats.Lines.ToString();
noteWords.Text = stats.Words.ToString();
noteCharsSpaces.Text = stats.CharsWithSpaces.ToString();
noteCharsNoSpaces.Text = stats.CharsWithoutSpaces.ToString();
if (note.Buffer.Selection != null)
{
TextIter start;
TextIter end;
note.Buffer.GetSelectionBounds(out start, out end);
stats = GetStatistics (start, end, countStrikeout.Active);
selLines.Text = stats.Lines.ToString();
selWords.Text = stats.Words.ToString();
selCharsSpaces.Text = stats.CharsWithSpaces.ToString();
selCharsNoSpaces.Text = stats.CharsWithoutSpaces.ToString();
SetSelectionColumnSensitive (true);
} else {
selLines.Text = "0";
selWords.Text = "0";
selCharsSpaces.Text = "0";
selCharsNoSpaces.Text = "0";
SetSelectionColumnSensitive (false);
}
}
private void SetSelectionColumnSensitive (bool sensitive)
{
selHeader.Sensitive = sensitive;
selLines.Sensitive = sensitive;
selWords.Sensitive = sensitive;
selCharsSpaces.Sensitive = sensitive;
selCharsNoSpaces.Sensitive = sensitive;
}
private StringStatistics GetStatistics (TextIter start, TextIter end, bool include_strikethrough)
{
string str = "";
if (include_strikethrough)
{
str = note.Buffer.GetText(start, end, false);
} else
{
TextTagEnumerator enumerator =
new TextTagEnumerator (note.Buffer, "strikethrough");
TextIter splice_start = start;
TextIter splice_end = end;
string splice = "";
foreach (TextRange range in enumerator) {
if (range.Start.Offset > end.Offset)
break;
if (range.End.Offset < start.Offset)
continue;
str += splice;
if (range.Start.InRange (splice_start, splice_end))
splice = splice_start.GetVisibleText (range.Start);
if (!range.End.InRange (splice_start, splice_end))
splice_start = splice_end;
else
splice_start = range.End;
}
str += splice;
str += splice_start.GetVisibleText (splice_end);
}
return GetTextStatistics (str);
}
private StringStatistics GetStatistics (bool include_strikethrough)
{
return GetStatistics (note.Buffer.StartIter, note.Buffer.EndIter, include_strikethrough);
}
private StringStatistics GetTextStatistics (string s)
{
StringStatistics stats = new StringStatistics();
stats.Words = Regex.Matches(s, @"[^\s•]+").Count;
stats.Lines = Regex.Matches(s, @"^.+", RegexOptions.Multiline).Count;
stats.CharsWithoutSpaces = Regex.Matches(s, @"[^\s•]").Count;
stats.CharsWithSpaces = Regex.Matches(s, @"[^•]").Count;
return stats;
}
private void SetTitle ()
{
noteTitle.Markup = String.Format("<b>{0}</b>", note.Title);
}
public NoteStatsDialog(Note note)
{
this.note = note;
this.Build();
SetTitle();
Refresh();
needs_refresh = false;
note.Buffer.Changed += OnTextChanged;
note.Buffer.MarkSet += OnMarkSet;
note.Renamed += OnRenamed;
GLib.Timeout.Add (500, OnAutoRefresh);
}
bool OnAutoRefresh ()
{
if (needs_refresh)
Refresh();
needs_refresh = false;
return IsRealized;
}
void OnRenamed(Note sender, string old_title)
{
SetTitle();
}
void OnMarkSet(object o, MarkSetArgs args)
{
if (args.Mark.Name == "insert" ||
args.Mark.Name == "selection_bound")
needs_refresh = true;
}
void OnTextChanged(object sender, EventArgs e)
{
needs_refresh = true;
}
protected virtual void OnStrikeoutToggled (object sender, System.EventArgs e)
{
Refresh();
}
}
}