PageRenderTime 15ms CodeModel.GetById 11ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/packager/openinstalledpkgdlg.pas

http://github.com/graemeg/lazarus
Pascal | 218 lines | 161 code | 27 blank | 30 comment | 14 complexity | 43f39f43bc4bbb12e2beb9d527b7fa2b MD5 | raw file
  1{
  2 ***************************************************************************
  3 *                                                                         *
  4 *   This source is free software; you can redistribute it and/or modify   *
  5 *   it under the terms of the GNU General Public License as published by  *
  6 *   the Free Software Foundation; either version 2 of the License, or     *
  7 *   (at your option) any later version.                                   *
  8 *                                                                         *
  9 *   This code is distributed in the hope that it will be useful, but      *
 10 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 11 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 12 *   General Public License for more details.                              *
 13 *                                                                         *
 14 *   A copy of the GNU General Public License is available on the World    *
 15 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 16 *   obtain it by writing to the Free Software Foundation,                 *
 17 *   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.        *
 18 *                                                                         *
 19 ***************************************************************************
 20
 21  Author: Mattias Gaertner
 22
 23  Abstract:
 24    Defines TOpenLoadedPackagesDlg - The dialog let the user choose one of
 25    the loaded packages.
 26}
 27unit OpenInstalledPkgDlg;
 28
 29{$mode objfpc}{$H+}
 30
 31interface
 32
 33uses
 34  SysUtils, Forms, Controls, ComCtrls, StdCtrls, ExtCtrls, LCLProc, ButtonPanel,
 35  IDEHelpIntf, IDEWindowIntf, PackageIntf, ListViewFilterEdit,
 36  PackageDefs, LazarusIDEStrConsts, PackageSystem;
 37
 38type
 39
 40  { TOpenLoadedPackagesDlg }
 41
 42  TOpenLoadedPackagesDlg = class(TForm)
 43    ButtonPanel1: TButtonPanel;
 44    FilterEdit: TListViewFilterEdit;
 45    PkgListView: TListView;
 46    HintMemo: TMemo;
 47    Splitter1: TSplitter;
 48    procedure FormClose(Sender: TObject; var {%H-}CloseAction: TCloseAction);
 49    procedure FormCreate(Sender: TObject);
 50    procedure HelpButtonClick(Sender: TObject);
 51    procedure OpenButtonClick(Sender: TObject);
 52    procedure PkgListViewDblClick(Sender: TObject);
 53    procedure PkgListViewSelectItem(Sender: TObject; {%H-}Item: TListItem; {%H-}Selected: Boolean);
 54  private
 55  public
 56    Package: TLazPackage;
 57    procedure UpdateSelection;
 58    procedure UpdatePackageList;
 59  end;
 60  
 61function ShowOpenLoadedPkgDlg(out OpenPackage: TLazPackage): TModalResult;
 62
 63implementation
 64
 65{$R *.lfm}
 66
 67function ShowOpenLoadedPkgDlg(out OpenPackage: TLazPackage): TModalResult;
 68var
 69  Dlg: TOpenLoadedPackagesDlg;
 70begin
 71  OpenPackage:=nil;
 72  Dlg:=TOpenLoadedPackagesDlg.Create(nil);
 73  try
 74    Dlg.UpdatePackageList;
 75    Dlg.UpdateSelection;
 76    Result:=Dlg.ShowModal;
 77    if (Result=mrOK) and (Dlg.Package<>nil) then
 78      OpenPackage:=Dlg.Package
 79    else
 80      OpenPackage:=nil;
 81  finally
 82    Dlg.Free;
 83  end;
 84end;
 85
 86{ TOpenLoadedPackagesDlg }
 87
 88procedure TOpenLoadedPackagesDlg.PkgListViewDblClick(Sender: TObject);
 89begin
 90  OpenButtonClick(Sender);
 91end;
 92
 93procedure TOpenLoadedPackagesDlg.PkgListViewSelectItem(Sender: TObject;
 94  Item: TListItem; Selected: Boolean);
 95begin
 96  UpdateSelection;
 97end;
 98
 99procedure TOpenLoadedPackagesDlg.UpdateSelection;
100var
101  CurPkg: TLazPackage;
102  LI: TListItem;
103  HintStr: String;
104begin
105  LI:=PkgListView.Selected;
106  ButtonPanel1.OKButton.Enabled:=Assigned(LI);
107  if Assigned(LI) then begin
108    CurPkg:=PackageGraph.FindPackageWithName(LI.Caption,nil);
109    if CurPkg=nil then
110      HintMemo.Text:='Package "'+LI.Caption+'" was already closed'
111    else begin
112      HintStr:=Format(lisOIPFilename, [CurPkg.Filename]);
113      if CurPkg.Missing then
114        HintStr:=Format(lisOIPThisPackageIsInstalledButTheLpkFileWasNotFound,[HintStr+LineEnding]);
115      HintStr:=Format(lisOIPDescriptionDescription, [HintStr+LineEnding,
116        BreakString(CurPkg.Description, 60, length(lisOIPDescription))]);
117      if CurPkg.License<>'' then //use same indent as previous entry
118        HintStr:=Format(lisOIPLicenseLicense, [HintStr+LineEnding,
119        BreakString(CurPkg.License, 60, length(lisOIPDescription))]);
120      HintMemo.Text:=HintStr;
121    end;
122  end else begin
123    HintMemo.Text:=lisOIPPleaseSelectAPackage;
124  end;
125end;
126
127procedure TOpenLoadedPackagesDlg.OpenButtonClick(Sender: TObject);
128begin
129  if PkgListView.Selected=nil then exit;
130  Package:=PackageGraph.FindPackageWithName(PkgListView.Selected.Caption,nil);
131  if Package=nil then
132    ModalResult:=mrCancel
133  else
134    ModalResult:=mrOk;
135end;
136
137procedure TOpenLoadedPackagesDlg.FormCreate(Sender: TObject);
138var
139  NewColumn: TListColumn;
140begin
141  Caption:=lisOIPOpenLoadedPackage;
142  IDEDialogLayoutList.ApplyLayout(Self,450,450);
143
144  with PkgListView do begin
145    NewColumn:=Columns.Add;
146    NewColumn.Caption:=lisOIPPackageName;
147    NewColumn.Width:=150;
148    NewColumn:=Columns.Add;
149    NewColumn.Caption:=lisVersion;
150    NewColumn.Width:=80;
151    NewColumn:=Columns.Add;
152    NewColumn.Caption:=lisOIPState;
153    NewColumn.Width:=300;
154  end;
155  ButtonPanel1.OKButton.Caption:=lisOpen;
156  ButtonPanel1.OKButton.Enabled:=False;
157end;
158
159procedure TOpenLoadedPackagesDlg.FormClose(Sender: TObject; var CloseAction: TCloseAction);
160begin
161  IDEDialogLayoutList.SaveLayout(Self);
162end;
163
164procedure TOpenLoadedPackagesDlg.HelpButtonClick(Sender: TObject);
165begin
166  LazarusHelp.ShowHelpForIDEControl(Self);
167end;
168
169function PkgStateToString(APackage: TLazPackage): string;
170  
171  procedure AddState(const s: string);
172  begin
173    if Result='' then
174      Result:=Result+s
175    else
176      Result:=Result+', '+s;
177  end;
178  
179begin
180  Result:='';
181  if APackage.Modified then AddState(lisOIPmodified);
182  if APackage.Missing then AddState(lisOIPmissing);
183  case APackage.Installed of
184  pitStatic: AddState(lisOIPinstalledStatic);
185  pitDynamic: AddState(lisOIPinstalledDynamic);
186  end;
187  case APackage.AutoInstall of
188  pitStatic: AddState(lisOIPautoInstallStatic);
189  pitDynamic: AddState(lisOIPautoInstallDynamic);
190  end;
191  if APackage.ReadOnly then AddState(lisOIPreadonly);
192end;
193
194procedure TOpenLoadedPackagesDlg.UpdatePackageList;
195
196  procedure UpdateOnePackage(aPkg: TLazPackage);
197  var
198    Data: TListViewDataItem;
199  begin
200    Data.Data := nil;
201    SetLength(Data.StringArray, 3);
202    Data.StringArray[0] := aPkg.Name;
203    Data.StringArray[1] := aPkg.Version.AsString;
204    Data.StringArray[2] := PkgStateToString(aPkg);
205    FilterEdit.Items.Add(Data);
206  end;
207
208var
209  i: Integer;
210begin
211  FilterEdit.Items.Clear;
212  for i:=0 to PackageGraph.Count-1 do
213    UpdateOnePackage(PackageGraph[i]);
214  FilterEdit.InvalidateFilter;
215end;
216
217end.
218