Attached is a patch to sort the listbox shown in "View Unit", "View
Forms" and "Remove from Project".  This makes it much easier to find
units in a large project.

I replaced the TList with a TStringList so I could take advantage of
the Sort method in the TStringList.

The patch must be applied from the root Lazarus directory.

Regards,
  - Graeme -

--
There's no place like 127.0.0.1
Index: ide/main.pp
===================================================================
--- ide/main.pp	(revision 9039)
+++ ide/main.pp	(working copy)
@@ -6015,7 +6015,8 @@
 end;
 
 function TMainIDE.DoViewUnitsAndForms(OnlyForms: boolean): TModalResult;
-var UnitList: TList;
+var
+  UnitList: TStringList;
   i: integer;
   MainUnitName, DlgCaption: string;
   MainUnitInfo, AnUnitInfo: TUnitInfo;
@@ -6024,29 +6025,38 @@
   AForm: TCustomForm;
 Begin
   GetCurrentUnit(ActiveSourceEditor,ActiveUnitInfo);
-  UnitList:=TList.Create;
+  UnitList := TStringList.Create;
+  UnitList.Sorted := True;
   try
     for i:=0 to Project1.UnitCount-1 do begin
       if not Project1.Units[i].IsPartOfProject then continue;
       //debugln('TMainIDE.DoViewUnitsAndForms OnlyForms=',dbgs(OnlyForms),' CompName=',Project1.Units[i].ComponentName,' UnitName=',Project1.Units[i].UnitName);
-      if OnlyForms then begin
+      if OnlyForms then
+      begin
         // add all form names of project
-        if Project1.Units[i].ComponentName<>'' then begin
-          UnitList.Add(TViewUnitsEntry.Create(
-            Project1.Units[i].ComponentName,i,Project1.Units[i]=ActiveUnitInfo));
+        if Project1.Units[i].ComponentName<>'' then
+        begin
+          UnitList.AddObject(Project1.Units[i].UnitName, TViewUnitsEntry.Create(
+            Project1.Units[i].ComponentName, i, Project1.Units[i]=ActiveUnitInfo));
         end;
-      end else begin
+      end else
+      begin
         // add all unit names of project
-        if (Project1.Units[i].UnitName<>'') then begin
-          UnitList.Add(TViewUnitsEntry.Create(
-            Project1.Units[i].UnitName,i,Project1.Units[i]=ActiveUnitInfo));
-        end else if Project1.MainUnitID=i then begin
-          MainUnitInfo:=Project1.MainUnitInfo;
-          if pfMainUnitIsPascalSource in Project1.Flags then begin
-            MainUnitName:=CreateSrcEditPageName('',
+        if (Project1.Units[i].UnitName <> '') then
+        begin
+          UnitList.AddObject(Project1.Units[i].UnitName, TViewUnitsEntry.Create(
+            Project1.Units[i].UnitName, i, Project1.Units[i]=ActiveUnitInfo));
+        end
+        else if Project1.MainUnitID = i then
+        begin
+          MainUnitInfo := Project1.MainUnitInfo;
+          if pfMainUnitIsPascalSource in Project1.Flags then
+          begin
+            MainUnitName := CreateSrcEditPageName('',
               MainUnitInfo.Filename,MainUnitInfo.EditorIndex);
-            if MainUnitName<>'' then begin
-              UnitList.Add(TViewUnitsEntry.Create(
+            if MainUnitName <> '' then
+            begin
+              UnitList.AddObject(MainUnitName, TViewUnitsEntry.Create(
                 MainUnitName,i,MainUnitInfo=ActiveUnitInfo));
             end;
           end;
@@ -6054,18 +6064,21 @@
       end;
     end;
     if OnlyForms then
-      DlgCaption:=dlgMainViewForms
+      DlgCaption := dlgMainViewForms
     else
-      DlgCaption:=dlgMainViewUnits ;
-    if ShowViewUnitsDlg(UnitList,true,DlgCaption)=mrOk then begin
+      DlgCaption := dlgMainViewUnits ;
+    if ShowViewUnitsDlg(UnitList,true,DlgCaption) = mrOk then
+    begin
+      { This is where we check what the user selected. }
       AnUnitInfo:=nil;
-      for i:=0 to UnitList.Count-1 do begin
-        if TViewUnitsEntry(UnitList[i]).Selected then begin
-          AnUnitInfo:=Project1.Units[TViewUnitsEntry(UnitList[i]).ID];
-          if AnUnitInfo.EditorIndex>=0 then begin
-            SourceNoteBook.Notebook.PageIndex:=AnUnitInfo.EditorIndex;
+      for i := 0 to UnitList.Count-1 do
+      begin
+        if TViewUnitsEntry(UnitList.Objects[i]).Selected then begin
+          AnUnitInfo := Project1.Units[TViewUnitsEntry(UnitList.Objects[i]).ID];
+          if AnUnitInfo.EditorIndex >= 0 then begin
+            SourceNoteBook.Notebook.PageIndex := AnUnitInfo.EditorIndex;
           end else begin
-            if Project1.MainUnitInfo=AnUnitInfo then
+            if Project1.MainUnitInfo = AnUnitInfo then
               Result:=DoOpenMainUnit(false)
             else
               Result:=DoOpenEditorFile(AnUnitInfo.Filename,-1,[ofOnlyIfExists]);
@@ -6077,14 +6090,15 @@
               ShowDesignerForm(AForm);
           end;
         end;
-      end;
-      if (AnUnitInfo<>nil) and (not OnlyForms) then begin
+      end;  { for }
+      if (AnUnitInfo<>nil) and (not OnlyForms) then
+      begin
         SourceNotebook.ShowOnTop;
       end;
-    end;
+    end;  { if ShowViewUnitDlg... }
   finally
     for i:=0 to UnitList.Count-1 do
-      TViewUnitsEntry(UnitList[i]).Free;
+      TViewUnitsEntry(UnitList.Objects[i]).Free;
     UnitList.Free;
   end;
   Result:=mrOk;
@@ -7052,47 +7066,59 @@
 end;
 
 function TMainIDE.DoRemoveFromProjectDialog: TModalResult;
-var UnitList: TList;
+var
+  UnitList: TStringList;
   i:integer;
   AName: string;
   AnUnitInfo: TUnitInfo;
 Begin
-  UnitList:=TList.Create;
+  UnitList := TStringList.Create;
+  UnitList.Sorted := True;
+
   try
-    for i:=0 to Project1.UnitCount-1 do begin
+    for i := 0 to Project1.UnitCount-1 do
+    begin
       AnUnitInfo:=Project1.Units[i];
-      if (AnUnitInfo.IsPartOfProject) and (i<>Project1.MainUnitID) then begin
-        AName:=Project1.RemoveProjectPathFromFilename(AnUnitInfo.FileName);
-        UnitList.Add(TViewUnitsEntry.Create(AName,i,false));
+      if (AnUnitInfo.IsPartOfProject) and (i<>Project1.MainUnitID) then
+      begin
+        AName := Project1.RemoveProjectPathFromFilename(AnUnitInfo.FileName);
+        UnitList.AddObject(AName, TViewUnitsEntry.Create(AName,i,false));
       end;
     end;
-    if ShowViewUnitsDlg(UnitList, true, lisRemoveFromProject)=mrOk then begin
-      for i:=0 to UnitList.Count-1 do begin
-        if TViewUnitsEntry(UnitList[i]).Selected then begin
-          AnUnitInfo:=Project1.Units[TViewUnitsEntry(UnitList[i]).ID];
-          AnUnitInfo.IsPartOfProject:=false;
-          if (Project1.MainUnitID>=0)
-          and (pfMainUnitHasUsesSectionForAllUnits in Project1.Flags)
-          then begin
-            if (AnUnitInfo.UnitName<>'') then begin
+    if ShowViewUnitsDlg(UnitList, true, lisRemoveFromProject) = mrOk then
+    begin
+      { This is where we check what the user selected. }
+      for i:=0 to UnitList.Count-1 do
+      begin
+        if TViewUnitsEntry(UnitList.Objects[i]).Selected then
+        begin
+          AnUnitInfo:=Project1.Units[TViewUnitsEntry(UnitList.Objects[i]).ID];
+          AnUnitInfo.IsPartOfProject := false;
+          if (Project1.MainUnitID >= 0) and
+             (pfMainUnitHasUsesSectionForAllUnits in Project1.Flags) then
+          begin
+            if (AnUnitInfo.UnitName <> '') then
+            begin
               if CodeToolBoss.RemoveUnitFromAllUsesSections(
-                Project1.MainUnitInfo.Source,AnUnitInfo.UnitName)
+                Project1.MainUnitInfo.Source, AnUnitInfo.UnitName)
               then
-                Project1.MainUnitInfo.Modified:=true;
+                Project1.MainUnitInfo.Modified := true;
             end;
-            if (AnUnitInfo.ComponentName<>'') then begin
+            if (AnUnitInfo.ComponentName <> '') then
+            begin
               Project1.RemoveCreateFormFromProjectFile(
-                  'T'+AnUnitInfo.ComponentName,AnUnitInfo.ComponentName);
+                  'T' + AnUnitInfo.ComponentName, AnUnitInfo.ComponentName);
             end;
           end;
         end;
-      end;
-    end;
+      end;  { for }
+    end;  { if ShowViewUnitsDlg.. }
   finally
-    for i:=0 to UnitList.Count-1 do TViewUnitsEntry(UnitList[i]).Free;
+    for i := 0 to UnitList.Count-1 do
+      TViewUnitsEntry(UnitList.Objects[i]).Free;
     UnitList.Free;
   end;
-  Result:=mrOk;
+  Result := mrOk;
 end;
 
 function TMainIDE.DoWarnAmbiguousFiles: TModalResult;
Index: ide/viewunit_dlg.pp
===================================================================
--- ide/viewunit_dlg.pp	(revision 9039)
+++ ide/viewunit_dlg.pp	(working copy)
@@ -4,6 +4,8 @@
                           ViewUnit_dlg.pp
                           ---------------
    TViewUnit is the application dialog for displaying all units in a project.
+   It gets used for the "View Units", "View Forms" and "Remove from Project"
+   menu items.
 
 
    Initial Revision  : Sat Feb 19 17:42 CST 1999
@@ -59,15 +61,14 @@
     MultiSelectCheckBox: TCheckBox;
     Procedure btnOKClick(Sender :TObject);
     Procedure btnCancelClick(Sender :TObject);
-    procedure ListboxKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
-      );
+    procedure ListboxKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
     procedure MultiselectCheckBoxClick(Sender :TObject);
   public
     constructor Create(TheOwner: TComponent); override;
   end;
 
 
-function ShowViewUnitsDlg(Entries: TList; MultiSelect: boolean;
+function ShowViewUnitsDlg(Entries: TStringList; MultiSelect: boolean;
   const Caption: string): TModalResult;
   // Entries is a list of TViewUnitsEntry(s)
 
@@ -75,9 +76,10 @@
 implementation
 
 
-function ShowViewUnitsDlg(Entries: TList;
-  MultiSelect: boolean; const Caption: string): TModalResult;
-var ViewUnitDialog: TViewUnitDialog;
+function ShowViewUnitsDlg(Entries: TStringList; MultiSelect: boolean;
+  const Caption: string): TModalResult;
+var
+  ViewUnitDialog: TViewUnitDialog;
   i: integer;
 begin
   ViewUnitDialog:=TViewUnitDialog.Create(nil);
@@ -90,15 +92,15 @@
       BeginUpdate;
       Clear;
       for i:=0 to Entries.Count-1 do
-        Add(TViewUnitsEntry(Entries[i]).Name);
+        Add(TViewUnitsEntry(Entries.Objects[i]).Name);
       EndUpdate;
     end;
     for i:=0 to Entries.Count-1 do
-      ViewUnitDialog.ListBox.Selected[i]:=TViewUnitsEntry(Entries[i]).Selected;
+      ViewUnitDialog.ListBox.Selected[i]:=TViewUnitsEntry(Entries.Objects[i]).Selected;
     Result:=ViewUnitDialog.ShowModal;
     if Result=mrOk then begin
       for i:=0 to Entries.Count-1 do begin
-        TViewUnitsEntry(Entries[i]).Selected:=ViewUnitDialog.ListBox.Selected[i];
+        TViewUnitsEntry(Entries.Objects[i]).Selected:=ViewUnitDialog.ListBox.Selected[i];
       end;
     end;
   finally
@@ -123,13 +125,13 @@
 begin
   inherited Create(TheOwner);
   IDEDialogLayoutList.ApplyLayout(Self,450,300);
-  btnOK.Caption:= lisOkBtn;
-  btnOk.Left:=ClientWidth-btnOk.Width-5;
-  btnCancel.Caption:=dlgCancel;
-  btnCancel.Left:=btnOk.Left;
-  CancelControl:=btnCancel;
-  MultiSelectCheckBox.Caption:=dlgMultiSelect;
-  MultiSelectCheckBox.Left:=btnOk.Left;
+  btnOK.Caption               := lisOkBtn;
+  btnOk.Left                  := ClientWidth-btnOk.Width-5;
+  btnCancel.Caption           := dlgCancel;
+  btnCancel.Left              := btnOk.Left;
+  CancelControl               := btnCancel;
+  MultiSelectCheckBox.Caption := dlgMultiSelect;
+  MultiSelectCheckBox.Left    := btnOk.Left;
 end;
 
 Procedure TViewUnitDialog.btnOKClick(Sender : TOBject);

Reply via email to