-
Notifications
You must be signed in to change notification settings - Fork 19
/
unMainExe.pas
647 lines (573 loc) · 13.8 KB
/
unMainExe.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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
{
SynWrite main form, for exe version.
The UI form is TfmMain, it's created as a child of this form.
}
unit unMainExe;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, Forms,
AppEvnts,
TntForms, TntClasses,
IniFiles,
unMain,
unPyApi,
unGlobData,
ATSynPlugins;
type
TfmSynwrite = class(TTntForm)
AppEv: TApplicationEvents;
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure AppEvDeactivate(Sender: TObject);
procedure AppEvMessage(var Msg: tagMSG;
var Handled: Boolean);
private
{ Private declarations }
procedure SavePos;
procedure LoadPos;
function GetSessionFN: string;
public
{ Public declarations }
protected
procedure WMDropFiles(var m: TMessage); message WM_DROPFILES;
procedure WMCopyData(var Msg: TWMCopyData); message WM_COPYDATA;
procedure WMQueryEndSession(var Msg : TWMQueryEndSession); message WM_QUERYENDSESSION;
procedure WMEndSession(var Msg: TWMEndSession); message WM_ENDSESSION;
end;
var
fmSynwrite: TfmSynwrite;
function SynParamsOK: boolean;
function SynInstanceNeeded: boolean;
implementation
uses
VistaAltFixUnit, DKLang,
ShellApi,
ATxSProc, ATxFProc, unProc,
TntSystem, TntSysUtils;
{$R *.dfm}
const
cMainFormClass = 'TfmSynwrite.UnicodeClass';
var
SynExePath,
SynIniPath,
SynIni,
SynHistoryIni: string;
SynTextOnly: TSynBinaryAct;
function GetPassedLineNumber: integer;
var
i: Integer;
s: string;
begin
Result:= 0;
for i:= 1 to ParamCount do
begin
s:= ParamStr(i);
if SBegin(s, cSynParamLineNum) then
begin
Delete(s, 1, Length(cSynParamLineNum));
Result:= StrToIntDef(s, 0);
Exit
end;
end;
end;
function IsUnneededParam(var S: Widestring): boolean;
//need to skip parameter with filename of "notepad.exe",
//special case for debugger under Win
begin
Result:= (S='') or (S[1]='/') or
(WideLowerCase(WideExtractFileName(S))='notepad.exe');
//work for relative paths
if not Result then
S:= WideExpandFileName(S);
end;
function SynInstanceNeeded: boolean;
var
Force: boolean;
Data: TCopyDataStruct;
f: THandle;
S: Widestring;
i, NParams: integer;
begin
Result:= true;
Force:= false;
//check command-line for "force single instance" key
for i:= 1 to ParamCount do
if ParamStr(i)=cSynParamSingleInst then
begin
Force:= true;
Break
end;
//check ini option, if false - exit
if not Force then
with TIniFile.Create(SynIni) do
try
if not ReadBool('Setup', 'Inst', false) then Exit;
finally
Free;
end;
//check other Syn instances
f:= FindWindowW(cMainFormClass, nil);
if f = 0 then Exit;
Result:= false;
NParams:= WideParamCount;
if NParams=0 then
begin
FillChar(Data, SizeOf(Data), 0);
Data.dwData:= 100;
Data.cbData:= 0;
Data.lpData:= nil;
SendMessage(f, WM_COPYDATA, 0, integer(@Data));
end
else
for i:= 1 to NParams do
begin
S:= WideParamStr(i);
if IsUnneededParam(S) then Continue;
FillChar(Data, SizeOf(Data), 0);
Data.dwData:= 100 + (GetPassedLineNumber shl 8);
Data.cbData:= (Length(S) + 1) * 2;
Data.lpData:= PWideChar(S);
SendMessage(f, WM_COPYDATA, 0, integer(@Data));
end;
end;
function IsFileCreatedOk(const S: WideString): boolean;
var
h: THandle;
begin
Result:= true;
if (S <> '') and (not IsFileExist(S)) then
begin
h:= CreateFileW(PWChar(S), generic_read or generic_write,
FILE_SHARE_read or FILE_SHARE_write, nil,
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
if h <> INVALID_HANDLE_VALUE then
CloseHandle(h)
else
begin
MsgCannotCreate(S, 0);
Result:= false;
end;
end;
end;
procedure InitLang;
var
Num: Integer;
begin
with TIniFile.Create(SynIni) do
try
LangManager.ScanForLangFiles(SynExePath+'Lang', '*.lng', false);
Num:= ReadInteger('Setup', 'Lang', LangManager.LanguageID);
LangManager.LanguageID:= Num;
finally
Free;
end;
end;
procedure InitSynTextOnly;
begin
with TIniFile.Create(SynIni) do
try
SynTextOnly:= TSynBinaryAct(ReadInteger('Setup', 'TxOnly', 0));
finally
Free;
end;
end;
//check filename
function CheckFile(const SF: Widestring): boolean;
begin
Result:= true;
if (SF<>'') and not IsFileExist(SF) then
begin
if MsgConfirmCreate(SF, 0) then
begin
Result:= IsFileCreatedOk(SF);
if not Result then Exit;
end
else
begin
Result:= false;
Exit
end;
end;
if (SF<>'') and IsFileTooBig(SF) then
begin
InitLang;
MsgFileTooBig(SF, 0);
Result:= false;
Exit
end;
if (SF<>'') and (SynTextOnly<>cBinaryAlwaysOpen) and (not IsFileText(SF)) then
if not IsFileArchive(SF) then
begin
InitLang;
if not MsgConfirmBinary(SF, 0) then
Result:= false;
end;
end;
function SynParamsOK: boolean;
var
i: integer;
S: Widestring;
begin
Result:= True;
InitSynTextOnly;
for i:= 1 to WideParamCount do
begin
S:= WideParamStr(i);
if IsUnneededParam(S) then
Continue;
if IsFileProject(S) then
begin
SynCommandlineProjectFN:= S;
Exit;
end;
if IsFileSession(S) then
begin
SynCommandlineSessionFN:= S;
Exit;
end;
if IsDirExist(S) then
begin
Exit
end;
if not CheckFile(S) then
begin
Result:= False;
Exit
end;
end;
end;
procedure TfmSynwrite.FormCreate(Sender: TObject);
begin
fmMain:= TfmMain.Create(Self);
fmMain.Parent:= Self;
fmMain.Align:= alClient;
//
TVistaAltFix.Create(Self);
end;
function TfmSynwrite.GetSessionFN: string;
begin
//first, check if project-session is set
Result:= fmMain.SynProjectSessionFN;
if Result<>'' then
begin
//MsgInfo('load project session', Handle);////////
Exit;
end;
//next check for last MRU session
with TIniFile.Create(SynHistoryIni) do
try
Result:= UTF8Decode(ReadString('MRU_Sess', '0', ''));
finally
Free;
end;
//use default session
if Result='' then
Result:= SynIniPath + SynDefaultSyn;
end;
procedure TfmSynwrite.FormShow(Sender: TObject);
var
i: integer;
S: Widestring;
begin
fmMain.hLister:= Handle;
fmMain.Show;
fmMain.acNewTab.Execute;
fmMain.DoCheckCommandLineTwo;
LoadPos;
DragAcceptFiles(Handle, True);
if WideParamCount=0 then
begin
//reopen session (only when no params)
if fmMain.opHistSessionLoad and (SynCommandlineSessionFN='') then
begin
S:= GetSessionFN;
if S<>'' then
if IsFileExist(S) then
fmMain.DoOpenSession(S)
else
begin
fmMain.DoHint(DKLangConstW('MNFound')+' '+S);
MsgBeep;
end;
end;
end
else
begin
if SynCommandlineProjectFN<>'' then
begin
//open cmd-line project
fmMain.DoOpenProject(SynCommandlineProjectFN);
end
else
if SynCommandlineSessionFN<>'' then
begin
//open cmd-line session
fmMain.DoOpenSession(SynCommandlineSessionFN);
end
else
begin
//read filenames from cmd-line
for i:= 1 to WideParamCount do
begin
S:= WideParamStr(i);
if IsUnneededParam(S) then Continue;
//open cmd-line parameter
if IsDirExist(S) then
fmMain.DoOpenFolder(S)
else
fmMain.DoOpenFile(S);
fmMain.UpdateRO;
//if file empty (new), uncheck R/O
if FGetFileSize(S)=0 then
if fmMain.ecReadOnly.Checked then
fmMain.ecReadOnly.Execute;
end;
fmMain.DoCheckCommandLinePrint;
end;
end;
end;
procedure TfmSynwrite.FormClose(Sender: TObject; var Action: TCloseAction);
begin
//Dont call fmMain.Close: flicker
fmMain.FormClose(Self, Action);
DragAcceptFiles(Handle, False);
SavePos;
end;
procedure TfmSynwrite.LoadPos;
const
cReservePixels=80;
begin
with TIniFile.Create(SynHistoryIni) do
try
Left:= ReadInteger('Win', 'Left', 200);
Top:= ReadInteger('Win', 'Top', 200);
Width:= ReadInteger('Win', 'Width', 780);
Height:= ReadInteger('Win', 'Height', 550);
//2nd monitor removed: fix pos
if (Left>Screen.DesktopWidth-cReservePixels) or
(Top>Screen.DesktopHeight-cReservePixels) then
begin
Left:= 0;
Top:= 0;
end;
if ReadBool('Win', 'Max', false) then
begin
WindowState:= wsMaximized;
fmMain.DoRepaint; //need repaintin for Maximized window
end;
if ReadBool('Win', 'OnTop', false) then
fmMain.ecOnTop.Execute;
finally
Free;
end;
//for the bad case
if ClientHeight=0 then
begin
SetBounds(20, 20, 780, 560);
WindowState:= wsNormal;
end;
end;
procedure TfmSynwrite.SavePos;
begin
try
with TIniFile.Create(SynHistoryIni) do
try
WriteBool('Win', 'OnTop', fmMain.ShowOnTop);
if fmMain.ShowFullScreen then Exit;
if not fmMain.opSaveWndPos then Exit;
if WindowState <> wsMaximized then
begin
WriteInteger('Win', 'Left', Left);
WriteInteger('Win', 'Top', Top);
WriteInteger('Win', 'Width', Width);
WriteInteger('Win', 'Height', Height);
end;
WriteBool('Win', 'Max', WindowState = wsMaximized);
finally
Free;
end;
except
end;
end;
procedure TfmSynwrite.WMDropFiles(var m: TMessage);
var
fn: array[0..MAX_PATH-1] of WideChar;
nCount, i: Integer;
InProj: boolean;
begin
InProj:= fmMain.IsMouseOverProject;
FillChar(fn, Sizeof(fn), 0);
nCount:= DragQueryFileW(THandle(M.WParam), $FFFFFFFF, fn, MAX_PATH);
for i:= 0 to nCount-1 do
begin
FillChar(fn, Sizeof(fn), 0);
DragQueryFileW(THandle(M.WParam), i, fn, MAX_PATH);
fmMain.DoDropFile(Widestring(fn), InProj);
end;
DragFinish(THandle(M.WParam));
end;
procedure TfmSynwrite.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (Key = vk_escape) and (Shift = []) then
begin
PyEscapeFlag:= True;
//Esc in QSearch
if fmMain.edQs.Focused then
fmMain.DoHandleQuickSearchEscape
else
//Esc in progress form
if Assigned(fmMain.fmProgress) and (fmMain.fmProgress.Visible) then
fmMain.fmProgress.bCan.Click
else
//Esc in panels
if (fmMain.CurrentEditor<>nil) and (not fmMain.CurrentEditor.Focused) then
fmMain.FocusEditor
else
//Esc in editor
case fmMain.opEsc of
cEscNothing:
Exit;
cEscCloseApp:
Close;
cEscCloseTab:
fmMain.acClose.Execute;
cEscCloseTabOrApp:
begin
if fmMain.FrameAllCount=1 then
Close
else
fmMain.acClose.Execute;
end;
cEscMinimizeApp:
Application.Minimize;
end;
Key:= 0;
Exit
end;
//Ctrl+Tab, Ctrl+Shift+Tab
if (Key = vk_tab) and ((Shift = [ssCtrl]) or (Shift = [ssCtrl, ssShift])) then
begin
fmMain.DoTabSwitch(Shift = [ssCtrl]);
Key:= 0;
Exit
end;
end;
procedure TfmSynwrite.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
fmMain.OnCloseQuery(nil, CanClose);
end;
procedure TfmSynwrite.WMCopyData(var Msg: TWMCopyData);
var
S: WideString;
N: Integer;
begin
Msg.Result:= 1;
with Msg.CopyDataStruct^ do
case dwData and $FF of
100: //"Open Unicode filename"
begin
S:= WideString(PWideChar(lpData));
N:= dwData shr 8;
end
else
begin
S:= '';
N:= -1;
end;
end;
Application.Restore;
Application.BringToFront;
//for W7: it doesn't restore window
if GetForegroundWindow <> Handle then
begin
Application.Minimize;
Application.Restore;
Application.BringToFront;
end;
if (S <> '') and CheckFile(S) then
begin
fmMain.DoOpenFile(S);
if N > 0 then
fmMain.CurrentEditor.CaretPos:= Point(0, N-1);
fmMain.DoRepaint;
end;
end;
procedure TfmSynwrite.AppEvDeactivate(Sender: TObject);
begin
if fmMain.opASaveOnFocus then
fmMain.DoAutoSave;
end;
function PluginAction(AHandle: Pointer; AName: PWideChar; A1, A2, A3, A4: Pointer): Integer; stdcall;
begin
Result:= fmMain.PluginAction(AHandle, AName, A1, A2, A3, A4);
end;
procedure TfmSynwrite.AppEvMessage(var Msg: tagMSG;
var Handled: Boolean);
var
h: THandle;
begin
//this needed mainly for SynFTP which doesn't react to
//cursor keys by default
Handled:= false;
if ((Msg.message=wm_keydown) or (Msg.message=wm_keyup)) and
Assigned(fmMain) and fmMain.IsPluginWindowActive(h) then
begin
//there's issue with Enter key: if it's passed to IsDialogMessage
//then it doesn't work. Others (eg. arrows) need to be passed.
if (Msg.wParam <> vk_return) then
Handled:= IsDialogMessage(h, Msg);
end
end;
procedure InitPaths;
var
SynHideIni,
SynHideSampleIni: string;
begin
SynExePath:= ExtractFilePath(ParamStr(0));
SynIniPath:= SynExePath+'Settings\';
SynIni:= SynIniPath + 'Syn.ini';
SynHistoryIni:= SynIniPath + 'SynHistory.ini';
SynHideIni:= SynIniPath + 'SynHide.ini';
SynHideSampleIni:= SynIniPath + 'SynHide.sample.ini';
{
if not IsFileExist(SynIni) then
if IsFileExist(SynSampleIni) then
CopyFile(PChar(SynSampleIni), PChar(SynIni), true);
}
if not IsFileExist(SynHideIni) then
if IsFileExist(SynHideSampleIni) then
CopyFile(PChar(SynHideSampleIni), PChar(SynHideIni), true);
end;
procedure InitFirst;
begin
CreateMutex(nil, false, 'UVViewSoft.SynWrite');
_SynActionProc:= @PluginAction;
end;
procedure InitOther;
begin
//later
end;
procedure TfmSynwrite.WMQueryEndSession(var Msg: TWMQueryEndSession);
begin
Msg.Result:= 1;
Close;
end;
procedure TfmSynwrite.WMEndSession(var Msg: TWMEndSession);
begin
Msg.Result:= 1;
Close;
end;
initialization
InitFirst;
InitPaths;
InitOther;
finalization
SynIni:= '';
SynHistoryIni:= '';
SynIniPath:= '';
end.