-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuMain.pas
582 lines (519 loc) · 15.7 KB
/
uMain.pas
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
unit uMain;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.ExtCtrls, Vcl.ComCtrls,
Vcl.Menus, Vcl.ExtDlgs, Generics.Collections, System.IOUtils, IniFiles, ShellAPI,
Vcl.ToolWin, System.ImageList, Vcl.ImgList, Vcl.StdCtrls,
uTableFrame, uTableFrameTabsheet, Vcl.Buttons;
type
TExcelPlusMainForm = class(TForm)
{$region 'Components'}
plSideBar: TPanel;
pcFiles: TPageControl;
mmMain: TMainMenu;
miFile: TMenuItem;
miOpen: TMenuItem;
miExit: TMenuItem;
odOpenDialog: TFileOpenDialog;
miSave: TMenuItem;
imlIcons32: TImageList;
miSaveAll: TMenuItem;
pmFiles: TPopupMenu;
miCloseFile: TMenuItem;
tvWorkspace: TTreeView;
odFolder: TFileOpenDialog;
miOpenWS: TMenuItem;
imlIcons16: TImageList;
miClose: TMenuItem;
N1: TMenuItem;
N3: TMenuItem;
miCloseAll: TMenuItem;
miCloseAllFiles: TMenuItem;
spSplitter: TSplitter;
N2: TMenuItem;
miCloseOtherFiles: TMenuItem;
fdSearch: TFindDialog;
Edit1: TMenuItem;
miFind: TMenuItem;
miHelp: TMenuItem;
miAbout: TMenuItem;
Panel1: TPanel;
sbOpen: TSpeedButton;
sbOpenFolder: TSpeedButton;
sbSave: TSpeedButton;
sbSaveAll: TSpeedButton;
sbSearch: TSpeedButton;
Panel2: TPanel;
Panel3: TPanel;
Panel4: TPanel;
sbRedo: TSpeedButton;
sbUndo: TSpeedButton;
miUndo: TMenuItem;
miRedo: TMenuItem;
N4: TMenuItem;
{$endregion}
{$region 'Eventhandler'}
procedure miOpenClick(Sender: TObject);
procedure miExitClick(Sender: TObject);
procedure miCloseFileClick(Sender: TObject);
procedure miSaveClick(Sender: TObject);
procedure pcFilesChange(Sender: TObject);
procedure miOpenWSClick(Sender: TObject);
procedure tvWorkspaceCollapsing(Sender: TObject; Node: TTreeNode;
var AllowCollapse: Boolean);
procedure tvWorkspaceDblClick(Sender: TObject);
procedure pcFilesContextPopup(Sender: TObject; MousePos: TPoint;
var Handled: Boolean);
procedure miSaveAllClick(Sender: TObject);
procedure pcFilesMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure pcFilesMouseLeave(Sender: TObject);
procedure miCloseAllFilesClick(Sender: TObject);
procedure miCloseOtherFilesClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure TabModifiedChanged(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure miCloseClick(Sender: TObject);
procedure miFindClick(Sender: TObject);
procedure fdSearchFind(Sender: TObject);
procedure miAboutClick(Sender: TObject);
procedure miUndoClick(Sender: TObject);
procedure miRedoClick(Sender: TObject);
{$endregion}
private
FWorkspacePath: String;
FLastFindPos: TPoint;
FFiles: TDictionary<string, TTableFrameTabsheet>;
procedure InitToolbar;
procedure SetControls(ASetFocus: Boolean = true);
procedure LoadSettings;
procedure SaveSettings;
function IsAnyModified: Boolean;
procedure LoadWorkspace(AWorkspacePath: String);
procedure OpenFile(AFilename: String);
procedure CloseFile(AFilename: String);
protected
procedure WMDropFiles(var Msg: TMessage); message WM_DROPFILES;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;
var
ExcelPlusMainForm: TExcelPlusMainForm;
implementation
{$R *.dfm}
uses
System.UITypes, System.Types, Math, uAbout;
procedure TExcelPlusMainForm.CloseFile(AFilename: String);
var tab: TTableFrameTabsheet;
dlgResult: Integer;
begin
if FFiles.ContainsKey(AFilename) then
begin
tab := FFiles[AFilename];
if tab.Frame.Modified then
begin
dlgResult := MessageDlg('Do you want to save your changes to "' + ExtractFileName(AFilename) + '"?', mtWarning, [mbYes, mbNo, mbCancel], 0);
if dlgResult <> mrCancel then
begin
if dlgResult = mrYes then
tab.Frame.SaveFile();
FFiles[AFilename].Free;
FFiles.Remove(AFilename);
end;
end
else
begin
FFiles[AFilename].Free;
FFiles.Remove(AFilename);
end;
SetControls;
end;
end;
constructor TExcelPlusMainForm.Create(AOwner: TComponent);
begin
inherited;
FFiles := TDictionary<String, TTableFrameTabsheet>.Create;
DragAcceptFiles(Handle, true);
InitToolbar();
end;
destructor TExcelPlusMainForm.Destroy;
begin
DragAcceptFiles(Handle, false);
FreeAndNil(FFiles);
inherited;
end;
procedure TExcelPlusMainForm.fdSearchFind(Sender: TObject);
var tab: TTableFrameTabsheet;
begin
if frFindNext in fdSearch.Options then
begin
tab := TTableFrameTabsheet(pcFiles.ActivePage);
if Assigned(tab) then
begin
FLastFindPos := Point(tab.Frame.sgTable.Selection.Left, tab.Frame.sgTable.Selection.Top);
if tab.Frame.Find(fdSearch.FindText, FLastFindPos, fdSearch.Options, FLastFindPos) then
tab.Frame.Select(FLastFindPos.X, FLastFindPos.Y, true)
else
begin
if (frDown in fdSearch.Options) then
FLastFindPos := Point(0,0)
else
FLastFindPos := Point(tab.Frame.sgTable.ColCount, tab.Frame.sgTable.RowCount);
if tab.Frame.Find(fdSearch.FindText, FLastFindPos, fdSearch.Options, FLastFindPos) then
tab.Frame.Select(FLastFindPos.X, FLastFindPos.Y, true)
else
MessageDlg('No results found!', mtInformation, [mbOK], 0);
end
end;
end;
end;
procedure TExcelPlusMainForm.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
SaveSettings;
end;
procedure TExcelPlusMainForm.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
var msgResult: Integer;
tab: TTableFrameTabsheet;
begin
if IsAnyModified() then
begin
msgResult := MessageDlg('Do you want to save your changes before quitting?', mtWarning, [mbYes, mbNo, mbCancel], 0, mbCancel);
if msgResult = mrYes then
begin
for tab in FFiles.Values do
begin
if tab.Frame.Modified then
tab.Frame.SaveFile();
end;
end;
CanClose := (msgResult <> mrCancel);
end
else
CanClose := true;
end;
procedure TExcelPlusMainForm.FormCreate(Sender: TObject);
begin
InitToolbar();
LoadSettings();
SetControls();
end;
procedure TExcelPlusMainForm.InitToolbar;
begin
imlIcons32.GetBitmap(0, sbOpen.Glyph);
imlIcons32.GetBitmap(1, sbOpenFolder.Glyph);
imlIcons32.GetBitmap(2, sbSave.Glyph);
imlIcons32.GetBitmap(3, sbSaveAll.Glyph);
imlIcons32.GetBitmap(4, sbSearch.Glyph);
imlIcons32.GetBitmap(5, sbUndo.Glyph);
imlIcons32.GetBitmap(6, sbRedo.Glyph);
end;
function TExcelPlusMainForm.IsAnyModified: Boolean;
var tab: TTableFrameTabsheet;
begin
Result := false;
for tab in FFiles.Values do
begin
if tab.Frame.Modified then
begin
Result := true;
break;
end;
end;
end;
procedure TExcelPlusMainForm.miAboutClick(Sender: TObject);
var aboutDlg: TAboutDlg;
begin
aboutDlg := TAboutDlg.Create(Self);
try
aboutDlg.ShowModal;
finally
aboutDlg.Free;
end;
end;
procedure TExcelPlusMainForm.miCloseAllFilesClick(Sender: TObject);
var i: Integer;
begin
for i := pcFiles.PageCount-1 downto 0 do
CloseFile(TTableFrameTabsheet(pcFiles.Pages[i]).Frame.Filename);
end;
procedure TExcelPlusMainForm.miCloseClick(Sender: TObject);
var tab: TTableFrameTabsheet;
begin
tab := TTableFrameTabsheet(pcFiles.ActivePage);
if Assigned(tab) then
CloseFile(tab.Frame.Filename);
end;
procedure TExcelPlusMainForm.miCloseFileClick(Sender: TObject);
var tabIndex: Integer;
tab: TTableFrameTabsheet;
pt: TPoint;
begin
pt := pcFiles.ScreenToClient(pmFiles.PopupPoint);
tabIndex := pcFiles.IndexOfTabAt(pt.X, pt.Y);
if tabIndex > -1 then
begin
tab := TTableFrameTabsheet(pcFiles.Pages[tabIndex]);
CloseFile(tab.Frame.Filename);
end;
end;
procedure TExcelPlusMainForm.miCloseOtherFilesClick(Sender: TObject);
var i, tabIndex: Integer;
currPage: TTabsheet;
pt: TPoint;
begin
pt := pcFiles.ScreenToClient(pmFiles.PopupPoint);
tabIndex := pcFiles.IndexOfTabAt(pt.X, pt.Y);
if tabIndex > -1 then
begin
currPage := pcFiles.Pages[tabIndex];
for i := pcFiles.PageCount-1 downto 0 do
begin
if pcFiles.Pages[i] <> currPage then
CloseFile(TTableFrameTabsheet(pcFiles.Pages[i]).Frame.Filename);
end;
end;
end;
procedure TExcelPlusMainForm.miExitClick(Sender: TObject);
begin
Close;
end;
procedure TExcelPlusMainForm.miFindClick(Sender: TObject);
var tab: TTableFrameTabsheet;
begin
tab := TTableFrameTabsheet(pcFiles.ActivePage);
if Assigned(tab) then
begin
FLastFindPos := Point(tab.Frame.sgTable.Selection.Left, tab.Frame.sgTable.Selection.Top);
fdSearch.Execute();
end;
end;
procedure TExcelPlusMainForm.miOpenClick(Sender: TObject);
var i: Integer;
begin
if odOpenDialog.Execute() then
begin
for i := 0 to odOpenDialog.Files.Count-1 do
OpenFile(odOpenDialog.Files[i]);
end;
end;
procedure TExcelPlusMainForm.miOpenWSClick(Sender: TObject);
begin
if odFolder.Execute then
LoadWorkspace(odFolder.FileName);
end;
procedure TExcelPlusMainForm.miRedoClick(Sender: TObject);
var currTab: TTableFrameTabsheet;
begin
currTab := TTableFrameTabsheet(pcFiles.ActivePage);
if Assigned(currTab) then
currTab.Frame.Redo;
end;
procedure TExcelPlusMainForm.miSaveAllClick(Sender: TObject);
var i: Integer;
tab: TTableFrameTabsheet;
begin
for i := 0 to pcFiles.PageCount-1 do
begin
tab := TTableFrameTabsheet(pcFiles.Pages[i]);
tab.Frame.SaveFile();
end;
SetControls;
end;
procedure TExcelPlusMainForm.miSaveClick(Sender: TObject);
var tab: TTableFrameTabsheet;
begin
if Assigned(pcFiles.ActivePage) then
begin
tab := TTableFrameTabsheet(pcFiles.ActivePage);
tab.Frame.SaveFile();
end;
end;
procedure TExcelPlusMainForm.miUndoClick(Sender: TObject);
var currTab: TTableFrameTabsheet;
begin
currTab := TTableFrameTabsheet(pcFiles.ActivePage);
if Assigned(currTab) then
currTab.Frame.Undo;
end;
procedure TExcelPlusMainForm.OpenFile(AFilename: String);
var tabsheet: TTableFrameTabsheet;
begin
if FFiles.ContainsKey(AFilename) then
pcFiles.ActivePage := FFiles[AFilename]
else
begin
tabsheet := TTableFrameTabsheet.Create(Self);
tabsheet.PageControl := pcFiles;
tabsheet.Caption := ExtractFileName(AFilename);
tabsheet.Hint := AFilename;
tabsheet.ParentShowHint := false;
tabsheet.ShowHint := false;
tabsheet.Frame := TTableFrame.Create(Self, AFilename);
tabsheet.Frame.OnModifiedChanged := TabModifiedChanged;
pcFiles.ActivePage := tabsheet;
FFiles.Add(AFilename, tabsheet);
end;
SetControls;
end;
procedure TExcelPlusMainForm.pcFilesChange(Sender: TObject);
begin
SetControls;
end;
procedure TExcelPlusMainForm.pcFilesContextPopup(Sender: TObject;
MousePos: TPoint; var Handled: Boolean);
begin
if not (htOnItem in pcFiles.GetHitTestInfoAt(MousePos.X, MousePos.Y)) then
Handled := true;
end;
procedure TExcelPlusMainForm.pcFilesMouseLeave(Sender: TObject);
begin
pcFiles.Hint := '';
end;
procedure TExcelPlusMainForm.pcFilesMouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
var tabindex: integer;
ht: THitTests;
begin
tabindex := pcFiles.IndexOfTabAt(X, Y);
if (tabindex >= 0) and (pcFiles.Hint <> pcFiles.Pages[tabindex].Hint) then
begin
Application.CancelHint;
pcFiles.Hint := pcFiles.Pages[tabindex].Hint;
ht := pcFiles.GetHitTestInfoAt(X, Y);
pcFiles.ShowHint := htOnItem in ht;
end;
end;
procedure TExcelPlusMainForm.LoadSettings;
var path: String;
ini: TIniFile;
begin
path := IncludeTrailingPathDelimiter(GetEnvironmentVariable('APPDATA'));
path := path + 'D2ExcelPlus\';
if FileExists(path + 'settings.ini') then
begin
ini := TIniFile.Create(path + 'settings.ini');
try
FWorkspacePath := ini.ReadString('SETTINGS', 'workspace', '');
LoadWorkSpace(FWorkspacePath);
finally
ini.Free;
end;
end;
end;
procedure TExcelPlusMainForm.LoadWorkspace(AWorkspacePath: String);
var wsNode: TTreeNode;
sr: TSearchRec;
begin
FWorkspacePath := AWorkspacePath;
tvWorkspace.Items.Clear;
wsNode := tvWorkspace.Items.AddChild(nil, 'Workspace');
wsNode.ImageIndex := 1;
wsNode.ExpandedImageIndex := 1;
wsNode.OverlayIndex := 1;
wsNode.StateIndex := 1;
wsNode.SelectedIndex := 1;
try
if FindFirst(IncludeTrailingPathDelimiter(FWorkspacePath) + '*.txt', faAnyFile, sr) = 0 then
begin
repeat
tvWorkspace.Items.AddChild(wsNode, sr.Name);
until FindNext(sr) <> 0;
end;
wsNode.Expand(true);
finally
FindClose(sr);
end;
end;
procedure TExcelPlusMainForm.SaveSettings;
var path: String;
ini: TIniFile;
begin
path := IncludeTrailingPathDelimiter(GetEnvironmentVariable('APPDATA'));
path := path + 'D2ExcelPlus\';
ForceDirectories(path);
ini := TIniFile.Create(path + 'settings.ini');
try
ini.WriteString('SETTINGS', 'workspace', FWorkspacePath);
ini.UpdateFile;
finally
ini.Free;
end;
end;
procedure TExcelPlusMainForm.SetControls(ASetFocus: Boolean);
var currTab: TTableFrameTabsheet;
anyModified: Boolean;
begin
currTab := TTableFrameTabsheet(pcFiles.ActivePage);
if Assigned(currTab) then
begin
if ASetFocus then
currTab.SetFocus;
anyModified := IsAnyModified();
Caption := ExtractFileName(currTab.Frame.Filename) + ' - D2Excel Plus';
sbUndo.Enabled := currTab.Frame.CanUndo;
sbRedo.Enabled := currTab.Frame.CanRedo;
miSave.Enabled := currTab.Frame.Modified;
sbSave.Enabled := currTab.Frame.Modified;
miSaveAll.Enabled := anyModified;
sbSaveAll.Enabled := anyModified;
miClose.Enabled := true;
miCloseAll.Enabled := true;
sbSearch.Enabled := true;
end
else
begin
Caption := 'D2Excel Plus';
sbUndo.Enabled := false;
sbRedo.Enabled := false;
sbSearch.Enabled := false;
miClose.Enabled := false;
miCloseAll.Enabled := false;
miSave.Enabled := false;
sbSave.Enabled := false;
miSaveAll.Enabled := false;
sbSaveAll.Enabled := false;
end;
end;
procedure TExcelPlusMainForm.TabModifiedChanged(Sender: TObject);
begin
if Sender = pcFiles.ActivePage then
SetControls(false);
end;
procedure TExcelPlusMainForm.tvWorkspaceCollapsing(Sender: TObject;
Node: TTreeNode; var AllowCollapse: Boolean);
begin
AllowCollapse := Assigned(Node.Parent);
end;
procedure TExcelPlusMainForm.tvWorkspaceDblClick(Sender: TObject);
var selNode: TTreeNode;
begin
selNode := tvWorkspace.Selected;
if Assigned(selNode) and Assigned(selNode.Parent) then
OpenFile(IncludeTrailingPathDelimiter(FWorkspacePath) + selNode.Text);
end;
procedure TExcelPlusMainForm.WMDropFiles(var Msg: TMessage);
var drop: THandle;
fileCount: Integer;
i, fileLen: Integer;
filename: String;
begin
drop := Msg.WParam;
try
fileCount := DragQueryFile(drop, $FFFFFFFF, nil, 0);
for i := 0 to fileCount-1 do
begin
fileLen := DragQueryFile(drop, i, nil, 0) + 1;
SetLength(filename, fileLen);
DragQueryFile(drop, i, @filename[1], fileLen);
OpenFile(Trim(filename));
end;
finally
DragFinish(drop);
end;
end;
end.