File: fMain.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (444 lines) | stat: -rw-r--r-- 10,914 bytes parent folder | download | duplicates (7)
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
{(*}
(*------------------------------------------------------------------------------
 Delphi Code formatter source code

The Original Code is fMain.pas, released April 2000.
The Initial Developer of the Original Code is Anthony Steele.
Portions created by Anthony Steele are Copyright (C) 1999-2008 Anthony Steele.
All Rights Reserved. 
Contributor(s): Michael Beck.
                        
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"). you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.mozilla.org/NPL/

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied.
See the License for the specific language governing rights and limitations
under the License.

Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 2 or later (the "GPL") 
See http://www.gnu.org/licenses/gpl.html
------------------------------------------------------------------------------*)
{*)}

unit fMain;

{ Created AFS 27 November 1999
  Main form for code formatting utility program
}


{$I ..\Include\JcfGlobal.inc}

interface

uses
  { delphi }
  SysUtils, Classes, Controls, Forms, Dialogs, StdCtrls, ComCtrls, Menus,
  ActnList, StdActns, ToolWin, ImgList, ShellAPI,
  { local }
  FileConverter, JcfSettings,  ConvertTypes,
  frBasicSettings, JvMRUManager, JvFormPlacement,
  JvMemo, frDrop, frmBaseSettingsFrame, JvComponent, JvExStdCtrls,
  JvComponentBase;

type
  TfrmMain = class(TForm)
    mnuMain: TMainMenu;
    mnuFile: TMenuItem;
    mnuGo:   TMenuItem;
    mnuClose: TMenuItem;
    mnuHelp: TMenuItem;
    mnuAbout: TMenuItem;
    tlbTop:  TToolBar;
    tbtnOpenFiles: TToolButton;
    btnGo:   TToolButton;
    btnAbout: TToolButton;
    tbtnToolButton6: TToolButton;
    btnSettings: TToolButton;
    ilStandardImages: TImageList;
    tbtnToolButton8: TToolButton;
    tbtnToolButton2: TToolButton;
    ActionList: TActionList;
    aOpenFiles: TAction;
    aOptions: TAction;
    aGo:     TAction;
    aAbout:  TAction;
    aExit:   TAction;
    tbtnToolButton4: TToolButton;
    btnClose: TToolButton;
    OpenFile1: TMenuItem;
    mnuFormatSettings: TMenuItem;
    mnuViewLog: TMenuItem;
    dlgSaveConfig: TSaveDialog;
    actHelpContents: THelpContents;
    mnuContents: TMenuItem;
    tbHelp:  TToolButton;
    mnuSettings: TMenuItem;
    mnuRegistrySettings: TMenuItem;
    mruFiles: TJvMRUManager;
    dlgOpen: TOpenDialog;
    frBasic: TfrBasic;
    N1:      TMenuItem;
    mnuSaveSettingsAs: TMenuItem;
    aSaveSettingsAs: TAction;
    JvFormStorage1: TJvFormStorage;
    mOutput: TJvMemo;
    lblLog:  TLabel;
    N2:      TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure mnuGoClick(Sender: TObject);
    procedure mnuCloseClick(Sender: TObject);
    procedure aFormatExecute(Sender: TObject);
    procedure aAboutExecue(Sender: TObject);
    procedure aExitExecute(Sender: TObject);
    procedure mnuAboutClick(Sender: TObject);
    procedure aOpenFilesExecute(Sender: TObject);
    procedure aOptionsExecute(Sender: TObject);
    procedure mnuViewLogClick(Sender: TObject);
    procedure mnuSaveSettingsAsClick(Sender: TObject);
    procedure actHelpContentsExecute(Sender: TObject);
    procedure mnuRegistrySettingsClick(Sender: TObject);
    procedure mruFilesClick(Sender: TObject; const RecentName, Caption: string;
      UserData: integer);
    procedure FormKeyUp(Sender: TObject; var Key: word; Shift: TShiftState);
    procedure FormResize(Sender: TObject);
    procedure frBasicsbOpenClick(Sender: TObject);
  private
    fcConverter: TFileConverter;

    procedure ShowStatusMesssage(const psFile, psMessage: string;
      const peMessageType: TStatusMessageType;
      const piY, piX: integer);

    procedure DoFormat;
    procedure ShowAbout;
    procedure SettingsChange(Sender: TObject);

  public
  end;

var
  frmMain: TfrmMain;

implementation

{$ifdef FPC}
  {$R *.lfm}
{$else}
  {$R *.dfm}
{$endif}

uses
  { deplhi }
  Windows,
  { jcl }
  JclFileUtils,
  { local }
  fAbout, fAllSettings, fRegistrySettings,
  SettingsStream, JcfHelp, JcfRegistrySettings, JcfFontSetFunctions;

function OkDialog(const psMsg: string): boolean;
begin
  Result := MessageDlg(psMsg, mtWarning, [mbYes, mbCancel], 0) = mrYes;
end;

procedure ErrorDialog(const psMsg: string);
begin
  MessageDlg(psMsg, mtError, [mbOK], 0);
end;

{------------------------------------------------------------------------------
  worker procs }


procedure TfrmMain.DoFormat;
var
  lcRegSet: TJCFRegistrySettings;
  lsSource, lsFileDesc, lsMessage: string;
begin
  frBasic.Write;

  lcRegSet := GetRegSettings;

  lsSource := lcRegSet.Input;

  if (lcRegSet.SourceMode = fmSingleFile) and (ExtractFileName(lsSource) = '') then
  begin
    ErrorDialog('No file specified in direcory');
    Exit;
  end;

  if (lsSource = '') then
  begin
    ErrorDialog('No files to format');
    exit;
  end;

  if lcRegSet.SourceMode = fmSingleFile then
    lsFileDesc := 'the file ' + ExtractFileName(lsSource)
  else
    lsFileDesc := 'the files in ' + lsSource;

  { confirm before obfuscate }
  if FormatSettings.Obfuscate.Enabled then
  begin
    lsMessage := 'Are you sure that you want to obfuscate ' + lsFileDesc;

    if lcRegSet.BackupMode = cmInPlace then
      lsMessage := lsMessage + ' without backup';

    lsMessage := lsMessage + '?';
    if not OkDialog(lsMessage) then
      exit;
  end
  else if (lcRegSet.BackupMode = cmInPlace) then
  begin
    lsMessage := 'Are you sure you want to convert ' + lsFileDesc + ' without backup?';
    if not OkDialog(lsMessage) then
      exit;
  end;

  fcConverter.Input := lsSource;
  fcConverter.BackupMode := frBasic.GetCurrentBackupMode;
  fcConverter.SourceMode := frBasic.GetCurrentSourceMode;

  fcConverter.Convert;
end;

procedure TfrmMain.ShowAbout;
var
  fAbout: TfrmAboutBox;
begin
  fAbout := TfrmAboutBox.Create(self);
  try
    fAbout.ShowModal;
  finally
    fAbout.Release;
  end;
end;



{------------------------------------------------------------------------------
  event handlers}

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  SetObjectFontToSystemFont(Self);

  Application.HelpFile := GetHelpFilePath;

  Randomize;

  GetRegSettings.MRUFiles := mruFiles.Strings;
  GetRegSettings.ReadAll;

  fcConverter := TFileConverter.Create;
  fcConverter.OnStatusMessage := ShowStatusMesssage;

  frBasic.mruFiles := mruFiles;
  frBasic.Read;
  frBasic.OnChange := SettingsChange;
  SettingsChange(nil);

  FormResize(nil);
end;

procedure TfrmMain.FormDestroy(Sender: TObject);
begin
  frBasic.Write;
  FreeAndNil(fcConverter);

  GetRegSettings.WriteAll;
  GetRegSettings.MRUFiles := nil;
end;


procedure TfrmMain.ShowStatusMesssage(const psFile, psMessage: string;
  const peMessageType: TStatusMessageType;
  const piY, piX: integer);
var
  lsMessage: string;
begin
  { show the message }
  lsMessage := psMessage;
  if (piX > 0) and (piY > 0) then
    lsMessage := lsMessage + ' near line ' + IntToStr(piY) + ' col ' + IntToStr(piX);
  mOutput.Lines.Add(lsMessage);

  { scroll into view and check the srollbar }
  mOutput.CurrentLine := mOutput.Lines.Count - 1;
  if mOutput.Lines.Count > 1 then
    mOutput.ScrollBars := ssVertical
  else
    mOutput.ScrollBars := ssNone;

  Application.ProcessMessages;
end;

procedure TfrmMain.mnuGoClick(Sender: TObject);
begin
  DoFormat;
end;

procedure TfrmMain.mnuCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmMain.aFormatExecute(Sender: TObject);
begin
  DoFormat;
end;

procedure TfrmMain.aAboutExecue(Sender: TObject);
begin
  ShowAbout;
end;

procedure TfrmMain.aExitExecute(Sender: TObject);
begin
  Close;
end;

procedure TfrmMain.mnuAboutClick(Sender: TObject);
begin
  ShowAbout;
end;

procedure TfrmMain.aOpenFilesExecute(Sender: TObject);
begin
  frBasic.DoFileOpen;
end;

procedure TfrmMain.aOptionsExecute(Sender: TObject);
var
  lfSet: TFormAllSettings;
begin
  lfSet := TFormAllSettings.Create(Self);
  try
    lfSet.Execute;
    frBasic.DisplayOutputFile;
  finally
    lfSet.Release;
  end;
end;

procedure TfrmMain.SettingsChange(Sender: TObject);
begin
  btnGo.Hint := frBasic.GetGoHint;

  if frBasic.GetCurrentSourceMode = fmSingleFile then
    tbtnOpenFiles.Hint := 'Select a source file'
  else
    tbtnOpenFiles.Hint := 'Select a source directory';
end;

procedure TfrmMain.mnuViewLogClick(Sender: TObject);
begin
  GetRegSettings.ViewLog;
end;


const
  CONFIG_FILTER = 'Config files (*.cfg)|*.cfg|Text files (*.txt)|' +
    '*.txt|XML files (*.xml)|*.xml|All files (*.*)|*.*';

procedure TfrmMain.mnuSaveSettingsAsClick(Sender: TObject);
var
  lsName: string;
  dlgSaveConfig: TSaveDialog;
  lcFile: TSettingsStreamOutput;
begin
  lsName := '';

  dlgSaveConfig := TSaveDialog.Create(self);
  try
    dlgSaveConfig.FileName := 'Saved.cfg';
    dlgSaveConfig.InitialDir := ExtractFilePath(Application.ExeName);
    dlgSaveConfig.DefaultExt := '.cfg';
    dlgSaveCOnfig.Filter := CONFIG_FILTER;

    if dlgSaveConfig.Execute then
    begin
      lsName := dlgSaveCOnfig.FileName;
    end;

    if lsName = '' then
      exit;

  finally
    dlgSaveConfig.Free;
  end;

  lcFile := TSettingsStreamOutput.Create(lsName);
  try
    FormatSettings.ToStream(lcFile);
  finally
    lcFile.Free;
  end;
end;

procedure TfrmMain.actHelpContentsExecute(Sender: TObject);
begin
  try
    Application.HelpContext(HELP_MAIN);
  except
    if FileExists(Application.HelpFile) then
      ShellExecute(Handle, 'open', PChar(Application.HelpFile), nil, nil, SW_SHOWNORMAL);
  end;
end;

procedure TfrmMain.mnuRegistrySettingsClick(Sender: TObject);
var
  lfSettings: TfmRegistrySettings;
begin
  lfSettings := TfmRegistrySettings.Create(self);
  try
    lfSettings.Execute;
    frBasic.DisplayOutputFile;
  finally
    lfSettings.Release;
  end;
end;

procedure TfrmMain.mruFilesClick(Sender: TObject; const RecentName, Caption: string;
  UserData: integer);
begin
  frBasic.DoFileOpen(RecentName);
end;

procedure TfrmMain.FormKeyUp(Sender: TObject; var Key: word; Shift: TShiftState);
begin
  if Key = VK_F1 then
    try
      Application.HelpContext(HELP_MAIN);
    except
      if FileExists(Application.HelpFile) then
        ShellExecute(Handle, 'open', PChar(Application.HelpFile),
          nil, nil, SW_SHOWNORMAL);
    end;
end;

procedure TfrmMain.FormResize(Sender: TObject);
begin
  {frBasic.Left  := 0;
  frBasic.Top   := tlbTop.Top + tlbTop.Height;
  frBasic.Width := ClientWidth;

  mOutput.Left   := 2;
  mOutput.Width  := ClientWidth - 4;
  mOutput.Top    := lblLog.Top + lblLog.Height + 4;
  mOutput.Height := ClientHeight - (mOutput.Top + 4);}
end;

procedure TfrmMain.frBasicsbOpenClick(Sender: TObject);
begin
  frBasic.sbOpenClick(Sender);
end;

end.