📄 uaddinteractively.pas
字号:
{ JADD - Just Another DelphiDoc: Documentation from Delphi Source Code
Copyright (C) 2003-2008 Gerold Veith
This file is part of JADD - Just Another DelphiDoc.
DelphiDoc is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3 as
published by the Free Software Foundation.
DelphiDoc is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
}
unit UAddInteractively;
{Contains a dialog, ~[link TFormAddInteractively], for the user to select the
files to parse interactively one by one. }
interface
uses
Windows, SysUtils, Classes,
{$IFNDEF LINUX}
Messages, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,
Buttons, ComCtrls, Grids,
{$ELSE}
QForms, QTypes, QStdCtrls, QControls, QExtCtrls, QComCtrls, QButtons, QGrids,
QDialogs,
{$ENDIF}
UBaseIdents;
type
{A dialog for the user to select the files to parse interactively one by
one. }
TFormAddInteractively = class(TForm)
PanelAdd: TPanel;
ButtonAdd: TButton;
Label1: TLabel;
PanelAddRecurse: TPanel;
ButtonAddRecurse: TButton;
Label2: TLabel;
BitBtnDelete: TBitBtn;
BitBtnCancel: TBitBtn;
BitBtnOK: TBitBtn;
StringGridFiles: TStringGrid;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormResize(Sender: TObject);
procedure ButtonAddClick(Sender: TObject);
procedure BitBtnDeleteClick(Sender: TObject);
procedure StringGridFilesTopLeftChanged(Sender: TObject);
private
//the list to add the files to parse to
FFileList: TFileList;
{$IFNDEF LINUX}
//Will be called when files are dropped on the form.
procedure FileDropped(var Msg: TWMDropFiles); message WM_DropFiles;
{$ENDIF}
//Shows the currently files to parse.
procedure RefreshStringGrid;
//Adds the file to the list to parse by preparsing it.
procedure ParseFile(FileName: String; Recurse: Boolean);
{ private declarations }
public
//Creates the dialog to select the file to parse interactively one by one.
constructor Create(FileList: TFileList); reintroduce;
{ public declarations }
end;
//Shows the dialog to select the file to parse interactively one by one.
function AddFilesInteractively(FileList: TFileList): Boolean;
implementation
{$R *.dfm}
uses
{$IFNDEF LINUX}
ShellAPI,
{$ENDIF}
{$IFDEF VER120}
FileCtrl,
{$ENDIF}
USettingsKeeper,
UParse;
{Shows the dialog to select the file to parse interactively one by one.
~param FileList the list to add the files to parse to
~result if the list of files should be parsed }
function AddFilesInteractively(FileList: TFileList): Boolean;
begin
with TFormAddInteractively.Create(FileList) do //create the dialog
try
Result := ShowModal = mrOK; //show the dialog
finally
Free; //free the dialog
end;
end;
{Creates the dialog to select the file to parse interactively one by one.
~param FileList the list to add the files to parse to }
constructor TFormAddInteractively.Create(FileList: TFileList);
begin
inherited Create(nil); //create the form
{$IFNDEF LINUX}
DragAcceptFiles(Handle, True); //can use drag and drop
{$ENDIF}
FFileList := FileList; //save the list of the files
StringGridFiles.Cells[0, 0] := 'Name'; //set the header of the list
StringGridFiles.Cells[1, 0] := 'Name of File';
StringGridFiles.Cells[2, 0] := 'Path to File';
TFormSettings.SaveBasicFormValues(Self); //read the basic form values
end;
{$IFNDEF LINUX}
{Will be called when files are dropped on the form.
~param Msg the msg of the dropped file }
procedure TFormAddInteractively.FileDropped(var Msg: TWMDropFiles);
//position where the files have been dropped
var MousePos :TPoint;
Bounds :TRect; //bounds of the drop targets
DropControl :TControl; //control files have been dropped on
FileName :String; //name of the files
Count :UINT; //number of dropped files
Index :UINT; //counter through the files
Length :UINT; //length of names of the files
begin
try
//searched control files have been dropped on
DropControl := nil; //not found so far
if DragQueryPoint(Msg.Drop, MousePos) then //get position
begin
Bounds := PanelAdd.ClientRect;
MapWindowPoints(PanelAdd.Handle, Handle, Bounds, 2);
if PtInRect(Bounds, MousePos) then //dropped on simple-add-panel?
DropControl := PanelAdd //use this
else
begin
Bounds := PanelAddRecurse.ClientRect;
MapWindowPoints(PanelAddRecurse.Handle, Handle, Bounds, 2);
if PtInRect(Bounds, MousePos) then //dropped on recurse-add-panel?
DropControl := PanelAddRecurse; //use it
end;
end;
if assigned(DropControl) then //dropped in valid panel
begin //get number of dropped files
Count := DragQueryFile(Msg.Drop, $FFFFFFFF, nil, 0);
if Count > 0 then //valid number of files?
begin
Index := 0;
while Index < Count do //for all dropped files
begin
//get length of the name of the dropped file
Length := DragQueryFile(Msg.Drop, Index, nil, 0);
SetLength(FileName, Length); //set buffer
if (Length = DragQueryFile(Msg.Drop, Index, //get name of the file
PChar(FileName), Length + 1)) then
ParseFile(FileName, DropControl = PanelAddRecurse); //parse the file
inc(Index); //next file
end; //while Index < Count
end; //if Count > 0
end; //if assigned(DropControl)
finally
DragFinish(Msg.Drop); //finish the dropping
Msg.Result := 0; //dropping of files has been handled
end;
end;
{$ENDIF}
{Shows the currently files to parse. }
procedure TFormAddInteractively.RefreshStringGrid;
var Count :Integer; //number of files in the list
i :Integer; //counter through the files
FileD :TPascalFile; //the files in the list
S :String; //content of the cells
SearchRec :TSearchRec; //to obtain the long name
begin
Count := FFileList.Count; //get number of files in the list
BitBtnDelete.Enabled := Count <> 0;
BitBtnOK.Enabled := Count <> 0;
StringGridFiles.Enabled := Count <> 0;
if Count = 0 then //list is empty?
begin
StringGridFiles.RowCount := 2; //clear the list
StringGridFiles.Cells[0, 1] := '';
StringGridFiles.Cells[1, 1] := '';
StringGridFiles.Cells[2, 1] := '';
end
else
begin
StringGridFiles.RowCount := Count + 1; //set number of entries (+header)
for i := 0 to Count - 1 do //for each file
begin
FileD := FFileList[i]; //get the file
S := FileD.InternalFileName; //internal name of the file
if assigned(FileD.ProjectFile) then
// if FileD.FileType = sftUnit then
S := ' ' + S; //indent, if in a project
StringGridFiles.Cells[0, Count - i] := S;
S := FileD.FilePath; //name of the file
//get long name of the file
if FindFirst(S, faAnyFile, SearchRec) = 0 then
try
S := SearchRec.Name; //use original (long) name
finally
FindClose(SearchRec);
end;
StringGridFiles.Cells[1, Count - i] := ExtractFileName(S);
//show the path/directory of the file
StringGridFiles.Cells[2, Count - i] := ExtractFileDir(FileD.FilePath);
end; //for i := 0 to Count - 1
end; //else Count = 0
end;
{Adds a file to the list of files to parse.
~param FileName the name of the file to parse or directory to search for files
to add
~param Recurse if used units (if it is a project file) should also be parsed
or subdirectories (if it is a directory) also be searched }
procedure TFormAddInteractively.ParseFile(FileName: String; Recurse: Boolean);
begin
//parse the file or all files in the directory
TParserManager(FFileList.ParserManager).PreparseOneFile(FileName,
DirectoryExists(FileName), Recurse);
RefreshStringGrid; //refresh the shown list
end;
{Called when the form is about to be closed.
~param Sender the sender of the event, the form
~param Action out: if and how the form should be closed }
procedure TFormAddInteractively.FormClose(Sender: TObject; var Action: TCloseAction);
var Settings :TFormSettings; //to save the settings of the form
begin
//get object to save the settings in
Settings := TFormSettings(TFormSettings.GetSettings(ClassName));
if assigned(Settings) then
Settings.GetValuesFromForm(Self); //save current settings
end;
{Called when the form is resized. Adjusts the width of the last column.
~param Sender the sender of the event, the form }
procedure TFormAddInteractively.FormResize(Sender: TObject);
var Count :Integer; //number of columns in the list
Width :Integer; //width of all columns
i :Integer; //counter through all columns
begin
StringGridFiles.Width := ClientWidth - 20; //set size of the list
StringGridFiles.Height := ClientHeight - StringGridFiles.Top - 10;
Count := StringGridFiles.ColCount - 1; //get number of columns
Width := (Count + 1) * 2; //initialize with width of lines
for i := 0 to Count do //get width of all columns but last
inc(Width, StringGridFiles.ColWidths[i]);
//calculate width of the last column
i := (StringGridFiles.Width - Width) + StringGridFiles.ColWidths[Count];
if (Width < StringGridFiles.GridWidth) or (i > 50) then
StringGridFiles.ColWidths[count] := i; //set width of the last column
end;
{Called when one of the buttons to add files is pressed.
~param Sender the sender of the event, ~[link ButtonAdd] or
~[link ButtonAddRecurse] }
procedure TFormAddInteractively.ButtonAddClick(Sender: TObject);
var i :Integer; //counter through the chosen files
begin
with TOpenDialog.Create(nil) do //create a file open - dialog
try
Options := [ofHideReadOnly, ofAllowMultiSelect, ofPathMustExist,
ofFileMustExist, ofEnableSizing, ofShowHelp];
assert(Sender is TWinControl);
HelpContext := TWinControl(Sender).HelpContext;
Title := 'Choose files to parse';
Filter := 'Pascal Files (*.pas;*.dpr;*.dpk;*.int)|*.pas;*.dpr;*.dpk;*.int|Units (*.pas)|*.pas|Delphi Projects (*.dpr)|*.dpr|Delphi Package (*.dpk)|*.dpk|All Files (*)|*';
DefaultExt := 'pas';
// InitialDir := '';
if Execute then //show the dialog; file chosen?
for i := 0 to Files.Count - 1 do //for all files
ParseFile(Files[i], Sender = ButtonAddRecurse); //add the file
finally
Free;
end;
end;
{Called when the button to delete files from the list to parse is pressed.
~param Sender the sender of the event, ~[link BitBtnDelete] }
procedure TFormAddInteractively.BitBtnDeleteClick(Sender: TObject);
var Selection :TGridRect; //the selected files in the list
Count :Integer; //number of files in the list
i :Integer; //counter through selected files
j :Integer; //counter through all files
FileD :TPascalFile; //all files to remove
FileD2 :TPascalFile; //all files
begin
Selection := StringGridFiles.Selection; //get selected files in the list
Count := FFileList.Count; //get number of files
if Count <> 0 then //list not empty?
begin
for i := Selection.Top to Selection.Bottom do //for all selected files
begin
FileD := FFileList[Count - i]; //get the selected file
FFileList.Remove(FileD, True); //and remove it
for j := 0 to FFileList.Count - 1 do //check all files
begin
FileD2 := FFileList[j];
if FileD2.ProjectFile = FileD then //it uses the deleted one?
FileD2.ProjectFile := nil; //remove the reference
end;
end; //for i := Selection.Top to Selection.Bottom
RefreshStringGrid; //refresh the list
end; //if Count <> 0
end;
{Is called when the list is scrolled. Disallows the horizontal scolling.
~param Sender the sender of the event, ~[link StringGridFiles] }
procedure TFormAddInteractively.StringGridFilesTopLeftChanged(Sender: TObject);
begin
StringGridFiles.LeftCol := 0; //always show the first column
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -