-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextForm.cs
198 lines (176 loc) · 6.65 KB
/
TextForm.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using TextReader.Configuration;
using TextReader.Parsing;
using TextReader.ScrollingView;
using TextReader.TreeBrowse;
using TextReader.TreeBrowse.FileSystem;
using TextReader.TreeBrowse.Bookmarks;
namespace TextReader {
class TextForm : System.Windows.Forms.Form {
private ScrollablePanel panel;
private BookFile bookFile;
private MenuItem setBookmarkMenuItem;
private MenuItem tocMenuItem;
private DirectoryInfo lastDir;
public BookFile BookFile {
get {
return bookFile;
}
set {
if (bookFile != null) {
bookFile.Dispose();
}
bookFile = value;
setBookmarkMenuItem.Enabled = bookFile != null;
tocMenuItem.Enabled = bookFile != null;
if (bookFile != null) {
panel.RowProvider = new CachingRowProvider(bookFile.RowProvider, 300);
if (bookFile.Index.Autosave != null) {
panel.Position = bookFile.Index.Autosave.Position;
}
}
}
}
public static void Main() {
var textForm = new TextForm();
Application.Run(textForm);
}
public TextForm() {
Text = "PE Text Reader";
panel = new ScrollablePanel();
Rectangle r = this.ClientRectangle;
r.Inflate(-6, -6);
panel.Left = r.Left;
panel.Top = r.Top;
panel.Size = new Size(r.Right - r.Left, r.Bottom - r.Top);
panel.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
this.Controls.Add(panel);
this.Deactivate += onDeactivate;
this.Activated += onActivate;
MenuItem exitMenuItem = new MenuItem() { Text = "Exit" };
exitMenuItem.Click += exitClick;
MenuItem openMenuItem = new MenuItem() { Text = "Open" };
openMenuItem.Click += openClick;
tocMenuItem = new MenuItem() { Text = "TOC", Enabled = false };
tocMenuItem.Click += tocClick;
setBookmarkMenuItem = new MenuItem() { Text = "Set Bookmark Here", Enabled = false };
setBookmarkMenuItem.Click += setBookmarkClick;
MenuItem menuRoot = new MenuItem() { Text = "Menu" };
menuRoot.MenuItems.Add(openMenuItem);
menuRoot.MenuItems.Add(tocMenuItem);
menuRoot.MenuItems.Add(setBookmarkMenuItem);
menuRoot.MenuItems.Add(exitMenuItem);
MenuItem autoscrollMenuItem = new MenuItem() { Text = "Autoscroll" };
autoscrollMenuItem.Click += autoscrollClick;
MainMenu menu = new MainMenu();
menu.MenuItems.Add(menuRoot);
menu.MenuItems.Add(autoscrollMenuItem);
this.Menu = menu;
}
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
var book = SelectBook();
if (book == null) {
Close();
}
BookFile = book;
}
public BookFile SelectBook() {
BookFile file;
List<FileInfo> recent = Config.LoadRecentFiles();
using (TreeBrowser fileBrowser = new TreeBrowser()) {
fileBrowser.Font = new Font("Arial", 10, FontStyle.Regular);
if (lastDir == null) {
lastDir = new DirectoryInfo(".");
}
if (recent.Count == 0) {
fileBrowser.Current = new DirectoryTreeItem(lastDir, new DrawParams(fileBrowser.Font, fileBrowser.ClientRectangle.Width));
} else {
fileBrowser.Current = new RecentRootItem(recent.ToArray(), lastDir, new DrawParams(fileBrowser.Font, fileBrowser.ClientRectangle.Width));
}
fileBrowser.ShowDialog();
if (fileBrowser.Selected == null) {
return null;
}
FileInfo f;
if (fileBrowser.Selected is FileTreeItem) {
f = (fileBrowser.Selected as FileTreeItem).File;
} else if (fileBrowser.Selected is RecentItem) {
f = (fileBrowser.Selected as RecentItem).File;
} else {
throw new ArgumentException("Unknown item type");
}
lastDir = f.Directory;
file = BookFile.Create(f);
}
recent.RemoveAll(f => f.FullName.Equals(file.File.FullName));
recent.Insert(0, file.File);
if (recent.Count > 20) {
recent.RemoveRange(20, recent.Count - 20);
}
Config.SaveRecentFiles(recent);
return file;
}
private void tocClick(object sender, EventArgs e) {
using (TreeBrowser browser = new TreeBrowser()) {
browser.Current = new BookmarkRootItem(BookFile.Index, new DrawParams(browser.Font, browser.Width));
browser.ShowDialog();
if (browser.Selected == null) {
return;
}
panel.Position = (browser.Selected as BookmarkItem).Bookmark.Position;
}
}
private void openClick(object sender, EventArgs e) {
var book = SelectBook();
if (book != null) {
BookFile = book;
}
}
private void exitClick(object sender, EventArgs e) {
autosaveBookmark();
Close();
}
private void onDeactivate(object sender, EventArgs e) {
panel.Freeze();
autosaveBookmark();
}
private void onActivate(object sender, EventArgs e) {
panel.Unfreeze();
}
private void autoscrollClick(object sender, EventArgs e) {
panel.Autoscroll = !panel.Autoscroll;
if (panel.Autoscroll) {
(sender as MenuItem).Text = "Stop";
} else {
(sender as MenuItem).Text = "Autoscroll";
}
}
private string getTopLineText() {
int y = 0;
Row row = panel.RowAt(ref y);
return row == null ? "" : row.Text;
}
private void setBookmarkClick(object sender, EventArgs e) {
int n = BookFile.Index.UserBookmarks.Count + 1;
Bookmark bookmark;
lock (panel.RowProvider) {
bookmark = new Bookmark(n + ": " + getTopLineText() + "\u2026", panel.Position);
}
BookFile.Index.UserBookmarks.Add(bookmark);
BookFile.SaveIndex();
}
private void autosaveBookmark() {
Bookmark bookmark;
lock (panel.RowProvider) {
bookmark = new Bookmark("A: " + getTopLineText() + "\u2026", panel.Position);
}
bookFile.Index.Autosave = bookmark;
bookFile.SaveIndex();
}
}
}