/
MainWindow.cs
324 lines (272 loc) · 12.5 KB
/
MainWindow.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
using System;
using Gtk;
using UI = Gtk.Builder.ObjectAttribute;
using sio = System.IO;
// My libraries
using Log;
using Tree;
using FileSystem;
using Export;
using NonJankExporter;
using System.Collections.Generic;
namespace docmaker
{
class MainWindow : Window
{
[UI] private Label _label1 = null;
private FileTree tree = null;
private int _counter;
private Logger log = new Logger("MainWindow");
public MainWindow() : this(new Builder("MainWindow.glade")) { }
private FileSystem.FileSystem files;
private Editor editor;
String configDir;
String configFile;
private MainWindow(Builder builder) : base(builder.GetObject("MainWindow").Handle)
{
builder.Autoconnect(this);
tree = new FileTree(true, (TreeView)builder.GetObject("tre"),(Entry)builder.GetObject("searc"));
editor = new Editor((TextView)builder.GetObject("editor"));
//TreeView treewid = (TreeView)builder.GetObject("tree");
//treewid.SearchEntry = (Entry)builder.GetObject("search");
DeleteEvent += Window_DeleteEvent;
MenuItem insert_page = (MenuItem)builder.GetObject("menuInsertPage");
insert_page.Activated += insertPage;
MenuItem insert_folder = (MenuItem)builder.GetObject("menuInsertFolder");
insert_folder.Activated += insertFolder;
MenuItem insert_module = (MenuItem)builder.GetObject("menuInsertModule");
insert_module.Activated += insertModule;
MenuItem insert_class = (MenuItem)builder.GetObject("menuInsertClass");
insert_class.Activated += insertClass;
MenuItem insert_function = (MenuItem)builder.GetObject("menuInsertFunction");
insert_function.Activated += insertFunction;
MenuItem delete = (MenuItem)builder.GetObject("delete");
delete.Activated += deleteItem;
MenuItem file_save = (MenuItem)builder.GetObject("fileSave");
file_save.Activated += save;
MenuItem file_open = (MenuItem)builder.GetObject("open");
file_open.Activated += openDialog;
MenuItem file_export = (MenuItem)builder.GetObject("export");
file_export.Activated += exportHtml;
//log.info(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
configDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)+sio.Path.DirectorySeparatorChar+"docmaker";
configFile = configDir+sio.Path.DirectorySeparatorChar+"lastUsedPath";
if (!sio.Directory.Exists(configDir)) {
sio.Directory.CreateDirectory(configDir);
sio.Directory.CreateDirectory(configDir+sio.Path.DirectorySeparatorChar+"templates");
sio.Directory.Move(AppDomain.CurrentDomain.BaseDirectory+"default",configDir+sio.Path.DirectorySeparatorChar+"templates"+sio.Path.DirectorySeparatorChar+"default");
sio.File.WriteAllText(configFile,"wazzah!!!");
}
String file_data = sio.File.ReadAllText(configFile);
if (sio.Directory.Exists(file_data)) {
openDir(file_data);
}
//openDir("docs");
}
private void Window_DeleteEvent(object sender, DeleteEventArgs a)
{
Application.Quit();
}
public void openDir(String dir) {
tree.flush();
files = new FileSystem.FileSystem(dir,tree,editor);
tree.getIndexAtPath("root").path = dir;
sio.File.WriteAllText(configFile,dir);
}
void openDialog(object sender, EventArgs a) {
Gtk.FileChooserDialog fc=
new Gtk.FileChooserDialog("Choose docs folder",
this,
Gtk.FileChooserAction.SelectFolder,
"Cancel",Gtk.ResponseType.Cancel,
"Open",Gtk.ResponseType.Accept);
if (fc.Run() == (int)Gtk.ResponseType.Accept)
{
openDir(fc.Filename);
}
//Destroy() to close the File Dialog
fc.Destroy();
}
private String entryDialog(String body, String defau) {
MessageDialog d = new MessageDialog(this,DialogFlags.Modal,MessageType.Question,ButtonsType.OkCancel, body);
Entry entry = new Entry();
entry.Buffer.Text = defau;
entry.Show();
d.ContentArea.PackEnd(entry,true,true,10);
//d.Activate += _ => d.Respond(ResponseType.Ok);
int r = d.Run();
String text = entry.Buffer.Text;
d.Destroy();
if (r == (int)ResponseType.Ok) {
return text;
}
else {
return "";
}
}
private String comboDialog(List<string> items) {
MessageDialog d = new MessageDialog(this,DialogFlags.Modal,MessageType.Question,ButtonsType.OkCancel, "Select a Template:");
var combo = new ComboBox(items.ToArray());
combo.Show();
d.ContentArea.PackEnd(combo,true,true,10);
//d.Activate += _ => d.Respond(ResponseType.Ok);
int r = d.Run();
String text = items[combo.Active];
d.Destroy();
if (r == (int)ResponseType.Ok) {
return text;
}
else {
return "";
}
}
void insertPage(object sender, EventArgs e) {
bool err = false;
try {
TreeIndex item = tree.getSelectedItem();
err = files.insertPage(item,entryDialog("Name for new item: ","UntitledPage"));
}
catch {
log.error("Error caught while adding new item. Probably caused by there being no selection. (This happening is impossible, so if you see this message, you've glitched the system way more than I thought possible)");
err = true;
}
if (err == true) {
MessageDialog md = new MessageDialog(this,
DialogFlags.DestroyWithParent, MessageType.Error,
ButtonsType.Close, "Cannot add new item. Are you sure you selected a correct parent from the tree?");
md.Run();
md.Destroy();
}
log.info("Page inserted!");
}
void insertFolder(object sender, EventArgs e) {
bool err = false;
try {
TreeIndex item = tree.getSelectedItem();
err = files.insertFolder(item,entryDialog("Name for new item: ","UntitledFolder"));
}
catch {
log.error("Error caught while adding new item. Probably caused by there being no selection. (This happening is impossible, so if you see this message, you've glitched the system way more than I thought possible)");
err = true;
}
if (err == true) {
MessageDialog md = new MessageDialog(this,
DialogFlags.DestroyWithParent, MessageType.Error,
ButtonsType.Close, "Cannot add new item. Are you sure you selected a correct parent from the tree?");
md.Run();
md.Destroy();
}
log.info("Page inserted!");
}
void insertClass(object sender, EventArgs e) {
bool err = false;
try {
TreeIndex item = tree.getSelectedItem();
err = files.insertClass(item,entryDialog("Name for new item: ","UntitledClass"));
}
catch {
log.error("Error caught while adding new item. Probably caused by there being no selection. (This happening is impossible, so if you see this message, you've glitched the system way more than I thought possible)");
err = true;
}
if (err == true) {
MessageDialog md = new MessageDialog(this,
DialogFlags.DestroyWithParent, MessageType.Error,
ButtonsType.Close, "Cannot add new item. Are you sure you selected a correct parent from the tree?");
md.Run();
md.Destroy();
}
log.info("Page inserted!");
}
void insertModule(object sender, EventArgs e) {
bool err = false;
try {
TreeIndex item = tree.getSelectedItem();
err = files.insertModule(item,entryDialog("Name for new item: ","UntitledModule"));
}
catch {
log.error("Error caught while adding new item");
err = true;
}
if (err == true) {
MessageDialog md = new MessageDialog(this,
DialogFlags.DestroyWithParent, MessageType.Error,
ButtonsType.Close, "Cannot add new item. Are you sure you selected a correct parent from the tree?");
md.Run();
md.Destroy();
}
log.info("Page inserted!");
}
void insertFunction(object sender, EventArgs e) {
bool err = false;
try {
TreeIndex item = tree.getSelectedItem();
err = files.insertFunction(item,entryDialog("Name for new item: ","UntitledFunction"));
}
catch {
log.error("Error caught while adding new item. Probably caused by there being no selection. (This happening is impossible, so if you see this message, you've glitched the system way more than I thought possible)");
err = true;
}
if (err == true) {
MessageDialog md = new MessageDialog(this,
DialogFlags.DestroyWithParent, MessageType.Error,
ButtonsType.Close, "Cannot add new item. Are you sure you selected a correct parent from the tree?");
md.Run();
md.Destroy();
}
log.info("Page inserted!");
}
void deleteItem(object sender, EventArgs e) {
bool err = false;
try {
TreeIndex item = tree.getSelectedItem();
MessageDialog d = new MessageDialog(this,DialogFlags.Modal,MessageType.Question,ButtonsType.OkCancel, $"Are you sure you want to delete item '{item.label}'");
//err = files.insertFunction(item,entryDialog("Name for new item: ","UntitledFunction"));
ResponseType r = (ResponseType)d.Run();
d.Destroy();
if (r == ResponseType.Ok) {
files.deleteNode(item,item.label);
}
}
catch {
log.error("Error caught while deleting file.");
err = true;
}
if (err == true) {
MessageDialog md = new MessageDialog(this,
DialogFlags.DestroyWithParent, MessageType.Error,
ButtonsType.Close, "Unknown error deleting the selected item. Are you sure you have the right permissions set for your file tree?");
md.Run();
md.Destroy();
}
log.info("Item deleted");
}
void save(object sender, EventArgs e) {
editor.saveFile();
}
void exportHtml(object sender, EventArgs e) {
Gtk.FileChooserDialog fc=
new Gtk.FileChooserDialog("Choose docs folder",
this,
Gtk.FileChooserAction.SelectFolder,
"Cancel",Gtk.ResponseType.Cancel,
"Open",Gtk.ResponseType.Accept);
String dname;
if (fc.Run() == (int)Gtk.ResponseType.Accept)
{
dname = fc.Filename;
fc.Destroy();
Templates templates = new Templates();
String tmplate = comboDialog(templates.getTemplates());
if (tmplate != "") {
String title = entryDialog("Title of the html documents", "UntitledExport");
new NoJankExport(files.path,dname,tmplate, title).export();
}
}
else {
fc.Destroy();
return;
}
//Destroy() to close the File Dialog
}
}
}